2016-08-31 17:28:11 +03:00
|
|
|
<?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();
|
2018-02-26 13:23:36 +03:00
|
|
|
|
2016-08-31 17:28:11 +03:00
|
|
|
foreach ($orderHistory as $change) {
|
|
|
|
$change['order'] = self::removeEmpty($change['order']);
|
2018-02-26 13:23:36 +03:00
|
|
|
|
|
|
|
if(isset($change['order']['items']) && $change['order']['items']) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$items = array();
|
2018-02-26 13:23:36 +03:00
|
|
|
|
2016-08-31 17:28:11 +03:00
|
|
|
foreach($change['order']['items'] as $item) {
|
|
|
|
if(isset($change['created'])) {
|
|
|
|
$item['create'] = 1;
|
|
|
|
}
|
|
|
|
$items[$item['id']] = $item;
|
|
|
|
}
|
|
|
|
$change['order']['items'] = $items;
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
if (isset($change['order']['contragent']['contragentType'])
|
|
|
|
&& $change['order']['contragent']['contragentType']
|
|
|
|
) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$change['order']['contragentType'] = $change['order']['contragent']['contragentType'];
|
|
|
|
unset($change['order']['contragent']);
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
if (isset($orders[$change['order']['id']]) && $orders[$change['order']['id']]) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']);
|
|
|
|
} else {
|
|
|
|
$orders[$change['order']['id']] = $change['order'];
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
if (isset($change['item']) && $change['item']) {
|
|
|
|
if (isset($orders[$change['order']['id']]['items'][$change['item']['id']])
|
|
|
|
&& $orders[$change['order']['id']]['items'][$change['item']['id']]
|
|
|
|
) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$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'];
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
if (empty($change['oldValue']) && $change['field'] == 'order_product') {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = true;
|
|
|
|
}
|
2018-02-26 13:23:36 +03:00
|
|
|
if (empty($change['newValue']) && $change['field'] == 'order_product') {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = true;
|
|
|
|
}
|
2018-02-26 13:23:36 +03:00
|
|
|
if (!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue'];
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-26 13:23:36 +03:00
|
|
|
if (isset($fields['delivery'][$change['field']])
|
|
|
|
&& $fields['delivery'][$change['field']] == 'service'
|
|
|
|
) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['delivery']['service']['code'] = self::newValue($change['newValue']);
|
2018-02-26 13:23:36 +03:00
|
|
|
} elseif (isset($fields['delivery'][$change['field']]) && $fields['delivery'][$change['field']]) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['delivery'][$fields['delivery'][$change['field']]] = self::newValue($change['newValue']);
|
2018-02-26 13:23:36 +03:00
|
|
|
} elseif (isset($fields['orderAddress'][$change['field']]) && $fields['orderAddress'][$change['field']]) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['delivery']['address'][$fields['orderAddress'][$change['field']]] = $change['newValue'];
|
2018-02-26 13:23:36 +03:00
|
|
|
} elseif (isset($fields['integrationDelivery'][$change['field']]) && $fields['integrationDelivery'][$change['field']]) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['delivery']['service'][$fields['integrationDelivery'][$change['field']]] = self::newValue($change['newValue']);
|
2018-02-26 13:23:36 +03:00
|
|
|
} elseif (isset($fields['customerContragent'][$change['field']]) && $fields['customerContragent'][$change['field']]) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']][$fields['customerContragent'][$change['field']]] = self::newValue($change['newValue']);
|
2018-02-26 13:23:36 +03:00
|
|
|
} elseif (strripos($change['field'], 'custom_') !== false) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']);
|
2018-02-26 13:23:36 +03:00
|
|
|
} elseif (isset($fields['order'][$change['field']]) && $fields['order'][$change['field']]) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']);
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
if (isset($change['created'])) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$orders[$change['order']['id']]['create'] = 1;
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:23:36 +03:00
|
|
|
if (isset($change['deleted'])) {
|
2016-08-31 17:28:11 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|