Passed
Push — master ( edbfbd...89a048 )
by Alexey
05:17
created

Orders::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Parser Orders
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Exchange1c\Parser;
13
14
class Orders extends \Object {
15
16
    public $xml = null;
17
18
    public function __construct($xml) {
19
        $this->xml = $xml;
20
    }
21
22
    public function process() {
23
        if ($this->xml->Документ) {
24
            $this->parseOrders($this->xml->Документ);
25
        }
26
    }
27
28
    public function parseOrders($orders) {
29
        foreach ($orders as $order) {
30
31
            $cart = \Ecommerce\Cart::get((string) $order->Номер);
32
33
            if (!$cart) {
34
                continue;
35
            }
36
37
            $reqs = [];
38
39
            foreach ($order->ЗначенияРеквизитов->ЗначениеРеквизита as $req) {
40
                $reqs[(string) $req->Наименование] = (string) $req->Значение;
41
            }
42
43
            $payed = false;
44
            $cancel = false;
45
46
            if (!empty($reqs['Дата оплаты по 1С']) && $reqs['Дата оплаты по 1С'] != 'T') {
47
                $payed = true;
48
                $date = new \DateTime((string) $reqs['Дата оплаты по 1С']);
49
                $cart->payed_date = $date->format('Y-m-d H:i:s');
50
            } elseif (
51
                    (!empty($reqs['Отменен']) && $reqs['Отменен'] == 'true') ||
52
                    (!empty($reqs['Дата оплаты по 1С']) && $reqs['Дата оплаты по 1С'] == 'T')) {
53
                $cancel = true;
54
            }
55
56
            $this->updateCartItems($cart, $order->Товары->Товар);
57
            $cart->payed = $payed;
58
            if ($payed && $cart->cart_status_id == 3) {
59
                $cart->cart_status_id = 5;
60
                $cart->save();
61
            } elseif ($cancel && $cart->cart_status_id == 3) {
62
                $cart->cart_status_id = 4;
63
            }
64
            if ($cart->warehouse_block && !$payed && !$cancel && !empty($reqs['Проведен']) && $reqs['Проведен'] == 'true') {
65
                $cart->warehouse_block = 0;
66
                foreach ($cart->cartItems as $cci) {
67
                    if ($cci->price && $cci->price->offer) {
68
                        $cci->price->offer->changeWarehouse('-' . (float) $cci->count);
69
                    }
70
                }
71
            }
72
            $cart->save();
73
        }
74
    }
75
76
    public function updateCartItems($cart, $items) {
77
        $itemIds = [];
78
        $cItems = [];
79
        foreach ($items as $item) {
80
            $cItem = [];
81
            $id = \Migrations\Id::get([['parse_id', $item->Ид], ['type', 'Ecommerce\Item']]);
82
            if (!$id) {
83
                continue;
84
            }
85
            $itemIds[] = $id->object_id;
86
            $siteItem = \Ecommerce\Item::get($id->object_id);
87
88
            if (!$siteItem) {
89
                $cItem['item_id'] = 0;
90
                $cItem['item_offer_price_id'] = 0;
91
                $cItem['count'] = (string) $item->Количество;
92
                $cItem['name'] = (string) $item->Наименование;
93
94
                $cItems[] = $cItem;
95
                continue;
96
            }
97
98
            $pricesByPrice = $siteItem->prices(['key' => 'price']);
99
            $prices = $siteItem->prices;
100
            $default = key($prices);
101
            $itemPrice = number_format((string) $item->ЦенаЗаЕдиницу, 2, '.', '');
102
            if (!empty($pricesByPrice[$itemPrice])) {
103
                $price = $pricesByPrice[$itemPrice];
104
            } else {
105
                $rolePrice = 0;
106
                foreach ($siteItem->prices as $priceId => $itemPrice) {
107
                    if (!$itemPrice->type->cipt_roles) {
108
                        $default = $priceId;
109
                        continue;
110
                    }
111
                    if ($itemPrice->type->cipt_roles && $cart->user->user_role_id && false !== strpos($itemPrice->type->cipt_roles, "|{$cart->user->user_role_id}|")) {
112
                        $rolePrice = $priceId;
113
                    }
114
                }
115
                $price = $siteItem->prices[($rolePrice) ? $rolePrice : $default];
116
            }
117
118
            $cItem['item_id'] = $id->object_id;
119
            $cItem['item_offer_price_id'] = $price->id;
120
            $cItem['count'] = (string) $item->Количество;
121
            $cItem['final_price'] = (string) $item->ЦенаЗаЕдиницу;
122
            $cItem['name'] = (string) $item->Наименование;
123
124
            $cItems[] = $cItem;
125
        }
126
        foreach ($cart->cartItems as $cartItem) {
127
            $isset = false;
128
            foreach ($cItems as $key => $cItem) {
129
                if (!($cItem['item_id'] == $cartItem->item_id)) {
130
                    continue;
131
                }
132
                $isset = true;
133
                if ($cItem['final_price'] != $cartItem->final_price || $cItem['item_offer_price_id'] != $cartItem->item_offer_price_id || $cItem['count'] != $cartItem->count) {
134
                    $cartItem->item_offer_price_id = $cItem['item_offer_price_id'];
135
                    $cartItem->count = $cItem['count'];
136
                    $cartItem->final_price = $cItem['final_price'];
137
                    $cartItem->save();
138
                }
139
                unset($cItems[$key]);
140
            }
141
            if (!$isset && !empty($cItem['name']) && !in_array($cItem['name'], ['Доставка', 'Клубная карта', 'Пакет майка'])) {
142
                $cartItem->delete();
143
            }
144
        }
145
        if ($cItems) {
146
            foreach ($cItems as $cItem) {
147
                $cart->addItem($cItem['item_offer_price_id'], $cItem['count'], $cItem['final_price']);
148
            }
149
        }
150
        $cart->calc();
151
    }
152
153
}
154