|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]> |
|
4
|
|
|
* @link https://github.com/shogodev/argilla/ |
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo |
|
6
|
|
|
* @license http://argilla.ru/LICENSE |
|
7
|
|
|
*/ |
|
8
|
|
|
class RetailCrmDataManager |
|
9
|
|
|
{ |
|
10
|
|
|
public $orderPrefix = 'order-'; |
|
11
|
|
|
|
|
12
|
|
|
public $callbackPrefix = 'call-'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string $idPrefix |
|
16
|
|
|
*/ |
|
17
|
|
|
public $idPrefix; |
|
18
|
|
|
|
|
19
|
|
|
public function getCallbackData(Callback $model) |
|
20
|
|
|
{ |
|
21
|
|
|
$data = array(); |
|
22
|
|
|
$data['call'] = true; |
|
23
|
|
|
$data['contragentType'] = 'individual'; |
|
24
|
|
|
$data['orderMethod'] = 'callback'; |
|
25
|
|
|
$data['status'] = 'new'; |
|
26
|
|
|
$data['number'] = $this->idPrefix.$this->callbackPrefix.$model->id; |
|
27
|
|
|
$data['firstName'] = (!empty($model->name) ? $model->name : 'Имя не указанно'); |
|
28
|
|
|
$data['phone'] = (!empty($model->phone)) ? ViewHelper::getClearPhone($model->phone) : ''; |
|
29
|
|
|
$data['email'] = (!empty($model->email) ? $model->email : ''); |
|
30
|
|
|
$data['customerComment'] = (!empty($model->time) ? 'Просьба перезвонить в '.$model->time.'.' : '').' '.$model->content; |
|
31
|
|
|
$data['items'] = array(); |
|
32
|
|
|
|
|
33
|
|
|
return $data; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @link http://www.retailcrm.ru/docs/%D0%A0%D0%B0%D0%B7%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D1%87%D0%B8%D0%BA%D0%B8/%D0%A1%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D1%87%D0%BD%D0%B8%D0%BA%D0%9C%D0%B5%D1%82%D0%BE%D0%B4%D0%BE%D0%B2APIV3 |
|
38
|
|
|
* @param Order $model |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getOrderData(Order $model) |
|
43
|
|
|
{ |
|
44
|
|
|
$model->refresh(); |
|
45
|
|
|
|
|
46
|
|
|
$data = array(); |
|
47
|
|
|
$data['call'] = false; |
|
48
|
|
|
$data['contragentType'] = 'individual'; |
|
49
|
|
|
$data['orderMethod'] = $model->type == Order::TYPE_FAST ? 'one-click' : 'shopping-cart'; |
|
50
|
|
|
$data['createdAt'] = $model->date_create; |
|
51
|
|
|
$data['status'] = 'new'; |
|
52
|
|
|
$data['number'] = $this->idPrefix.$this->orderPrefix.$model->id; |
|
53
|
|
|
$data['firstName'] = !empty($model->name) ? $model->name : 'Имя не указанно'; |
|
54
|
|
|
$data['phone'] = !empty($model->phone) ? ViewHelper::getClearPhone($model->phone) : ''; |
|
55
|
|
|
$data['email'] = !empty($model->email) ? $model->email : ''; |
|
56
|
|
|
$data['customerComment'] = !empty($model->comment) ? $model->comment : ''; |
|
57
|
|
|
|
|
58
|
|
|
if( $model->type !== Order::TYPE_FAST ) |
|
59
|
|
|
{ |
|
60
|
|
|
$data['delivery']['code'] = $this->getDeliveryCode($model); |
|
61
|
|
|
$data['delivery']['address']['text'] = !empty($model->address) ? $model->address : ''; |
|
62
|
|
|
$data['paymentType'] = $this->getPaymentCode($model); |
|
63
|
|
|
} |
|
64
|
|
|
$data['items'] = $this->getProductsData($model->products); |
|
65
|
|
|
|
|
66
|
|
|
return $data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param Order $model |
|
71
|
|
|
* |
|
72
|
|
|
* @throws ModelValidateException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function updateOrderStatus($model) |
|
75
|
|
|
{ |
|
76
|
|
|
try |
|
77
|
|
|
{ |
|
78
|
|
|
$model->updateByPk($model->id, array('status_id' => OrderStatus::STATUS_CONFIRMED)); |
|
79
|
|
|
} |
|
80
|
|
|
catch(Exception $e) |
|
81
|
|
|
{ |
|
82
|
|
|
throw new ModelValidateException($model); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param FActiveRecord $model |
|
88
|
|
|
* @param $id |
|
89
|
|
|
* @param $url |
|
90
|
|
|
* |
|
91
|
|
|
* @throws ModelValidateException |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setRetailCrmUrl($model, $id, $url) |
|
94
|
|
|
{ |
|
95
|
|
|
$retailCrmUrl = $url.'/orders/s2-'.mb_strtolower($id).'/edit'; |
|
96
|
|
|
|
|
97
|
|
|
try |
|
98
|
|
|
{ |
|
99
|
|
|
if( isset($model->getTableSchema()->columns['retail_crm_url']) ) |
|
100
|
|
|
$model->updateByPk($model->primaryKey, array('retail_crm_url' => $retailCrmUrl)); |
|
101
|
|
|
} |
|
102
|
|
|
catch(Exception $e) |
|
103
|
|
|
{ |
|
104
|
|
|
throw new ModelValidateException($model); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param OrderProduct[] $products |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
private function getProductsData(array $products) |
|
114
|
|
|
{ |
|
115
|
|
|
$data = array(); |
|
116
|
|
|
|
|
117
|
|
|
$index = 0; |
|
118
|
|
|
foreach($products as $product) |
|
119
|
|
|
{ |
|
120
|
|
|
$data[$index]['productId'] = $product->history->product_id; |
|
121
|
|
|
$data[$index]['initialPrice'] = round($product->price); |
|
122
|
|
|
$data[$index]['quantity'] = $product->count; |
|
123
|
|
|
$data[$index]['productName'] = $product->name; |
|
124
|
|
|
|
|
125
|
|
|
$data[$index]['properties'] = array(); |
|
126
|
|
|
|
|
127
|
|
|
if( !empty($product->history->articul) ) |
|
128
|
|
|
{ |
|
129
|
|
|
$data[$index]['properties'][] = array( |
|
130
|
|
|
'code' => 'article', |
|
131
|
|
|
'name' => 'Артикул', |
|
132
|
|
|
'value' => $product->history->articul |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->setParameters($data[$index]['properties'], $product); |
|
137
|
|
|
$this->setOptions($data, $index, $product); |
|
138
|
|
|
|
|
139
|
|
|
$index++; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $data; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
private function setParameters(&$data, OrderProduct $product) |
|
146
|
|
|
{ |
|
147
|
|
|
foreach($product->items as $option) |
|
148
|
|
|
{ |
|
149
|
|
|
if( $option->type != 'ProductParameter' ) |
|
150
|
|
|
continue; |
|
151
|
|
|
|
|
152
|
|
|
$parameter['name'] = $option->name; |
|
|
|
|
|
|
153
|
|
|
$parameter['value'] = $option->value; |
|
154
|
|
|
$parameter['code'] = $option->pk; |
|
155
|
|
|
|
|
156
|
|
|
$data[] = $parameter; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
private function setOptions(&$data, &$index, OrderProduct $product) |
|
161
|
|
|
{ |
|
162
|
|
|
foreach($product->items as $option) |
|
163
|
|
|
{ |
|
164
|
|
|
if( $option->type != 'ProductOption' ) |
|
165
|
|
|
continue; |
|
166
|
|
|
|
|
167
|
|
|
$index++; |
|
168
|
|
|
|
|
169
|
|
|
$data[$index]['initialPrice'] = round($option->price); |
|
170
|
|
|
$data[$index]['quantity'] = $option->amount; |
|
171
|
|
|
$data[$index]['productName'] = $option->value.' (Опция)'; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function getDeliveryCode(Order $model) |
|
176
|
|
|
{ |
|
177
|
|
|
if( !($model->delivery instanceof OrderDelivery) ) |
|
178
|
|
|
return ''; |
|
179
|
|
|
|
|
180
|
|
|
$deliverCodeList = array( |
|
181
|
|
|
OrderDeliveryType::SELF_DELIVERY => 'self-delivery', |
|
182
|
|
|
OrderDeliveryType::DELIVERY_MOSCOW => 'courier', |
|
183
|
|
|
OrderDeliveryType::DELIVERY_MOSCOW_REGION => 'courier', |
|
184
|
|
|
OrderDeliveryType::DELIVERY_REGION => 'russian-post' |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
return $deliverCodeList[$model->delivery->delivery_type_id]; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function getPaymentCode(Order $model) |
|
191
|
|
|
{ |
|
192
|
|
|
if( !($model->payment instanceof OrderPayment) ) |
|
193
|
|
|
return ''; |
|
194
|
|
|
|
|
195
|
|
|
$paymentCodeList = array( |
|
196
|
|
|
OrderPaymentType::CASH => 'cash', |
|
197
|
|
|
OrderPaymentType::NON_CASH => 'bank-card', |
|
198
|
|
|
OrderPaymentType::E_PAY => 'e-money', |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
return $paymentCodeList[$model->payment->payment_type_id]; |
|
202
|
|
|
} |
|
203
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.