mirror of
https://github.com/retailcrm/opencart-module.git
synced 2024-11-22 21:26:08 +03:00
109 lines
5.0 KiB
PHP
109 lines
5.0 KiB
PHP
<?php
|
|
|
|
class RetailcrmHistoryHelper {
|
|
public static function assemblyOrder($orderHistory)
|
|
{
|
|
if (file_exists(__DIR__ . '/objects.xml')) {
|
|
$objects = simplexml_load_file(__DIR__ . '/objects.xml');
|
|
foreach($objects->fields->field as $object) {
|
|
$fields[(string)$object["group"]][(string)$object["id"]] = (string)$object;
|
|
}
|
|
}
|
|
$orders = array();
|
|
foreach ($orderHistory as $change) {
|
|
$change['order'] = self::removeEmpty($change['order']);
|
|
if($change['order']['items']) {
|
|
$items = array();
|
|
foreach($change['order']['items'] as $item) {
|
|
if(isset($change['created'])) {
|
|
$item['create'] = 1;
|
|
}
|
|
$items[$item['id']] = $item;
|
|
}
|
|
$change['order']['items'] = $items;
|
|
}
|
|
|
|
if($change['order']['contragent']['contragentType']) {
|
|
$change['order']['contragentType'] = $change['order']['contragent']['contragentType'];
|
|
unset($change['order']['contragent']);
|
|
}
|
|
|
|
if($orders[$change['order']['id']]) {
|
|
$orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']);
|
|
} else {
|
|
$orders[$change['order']['id']] = $change['order'];
|
|
}
|
|
|
|
if($change['item']) {
|
|
if($orders[$change['order']['id']]['items'][$change['item']['id']]) {
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']);
|
|
} else {
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item'];
|
|
}
|
|
|
|
if(empty($change['oldValue']) && $change['field'] == 'order_product') {
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = true;
|
|
}
|
|
if(empty($change['newValue']) && $change['field'] == 'order_product') {
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = true;
|
|
}
|
|
if(!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) {
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue'];
|
|
}
|
|
} else {
|
|
if($fields['delivery'][$change['field']] == 'service') {
|
|
$orders[$change['order']['id']]['delivery']['service']['code'] = self::newValue($change['newValue']);
|
|
} elseif($fields['delivery'][$change['field']]) {
|
|
$orders[$change['order']['id']]['delivery'][$fields['delivery'][$change['field']]] = self::newValue($change['newValue']);
|
|
} elseif($fields['orderAddress'][$change['field']]) {
|
|
$orders[$change['order']['id']]['delivery']['address'][$fields['orderAddress'][$change['field']]] = $change['newValue'];
|
|
} elseif($fields['integrationDelivery'][$change['field']]) {
|
|
$orders[$change['order']['id']]['delivery']['service'][$fields['integrationDelivery'][$change['field']]] = self::newValue($change['newValue']);
|
|
} elseif($fields['customerContragent'][$change['field']]) {
|
|
$orders[$change['order']['id']][$fields['customerContragent'][$change['field']]] = self::newValue($change['newValue']);
|
|
} elseif(strripos($change['field'], 'custom_') !== false) {
|
|
$orders[$change['order']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']);
|
|
} elseif($fields['order'][$change['field']]) {
|
|
$orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']);
|
|
}
|
|
|
|
if(isset($change['created'])) {
|
|
$orders[$change['order']['id']]['create'] = 1;
|
|
}
|
|
|
|
if(isset($change['deleted'])) {
|
|
$orders[$change['order']['id']]['deleted'] = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $orders;
|
|
}
|
|
|
|
public static function newValue($value)
|
|
{
|
|
if(isset($value['code'])) {
|
|
return $value['code'];
|
|
} else {
|
|
return $value;
|
|
}
|
|
}
|
|
|
|
public static function removeEmpty($inputArray)
|
|
{
|
|
$outputArray = array();
|
|
if (!empty($inputArray)) {
|
|
foreach ($inputArray as $key => $element) {
|
|
if(!empty($element) || $element === 0 || $element === '0'){
|
|
if (is_array($element)) {
|
|
$element = self::removeEmpty($element);
|
|
}
|
|
$outputArray[$key] = $element;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $outputArray;
|
|
}
|
|
}
|