1 <?php
2
3 class Orders1CExchange {
4
5 static $statuses1C = array(
6 'Unpaid' => 'Новый',
7 'Query' => 'В работе',
8 'Paid' => 'Оплачен',
9 'Processing' => 'В обработке',
10 'Sent' => 'Отправлен',
11 'Complete' => 'Завершен',
12 'AdminCancelled' => 'Отменен магазином',
13 'MemberCancelled' => 'Отменен пользователем',
14 );
15
16 function exportOrders() {
17 $document = array();
18 $statusesExportFrom = SiteConfig::current_site_config()->StatusesToExport();
19 $orders = DataObject::get('Order', 'IsExported = 0 AND Status IN(' . implode(', ', $statusesExportFrom) . ')', 'ID DESC');
20 if ($orders) {
21 $sc = SiteConfig::current_site_config();
22 $document_counter = 0;
23 foreach ($orders as $order) {
24 $date = date('Y-m-d', strtotime($order->Created));
25 $time = date('H:i:s', strtotime($order->Created));
26
27 $document['Документ' . $document_counter] = array(
28 'Ид' => $order->ID,
29 'Номер' => $order->ID,
30 'Дата' => $date,
31 'Время' => $time,
32 'Валюта' => $sc->CatalogCurrency,
33 'Курс' => 1,
34 'ХозОперация' => 'Заказ товара',
35 'Роль' => 'Продавец',
36 'Сумма' => $order->GrandTotal,
37 'Комментарий' => $order->ClientNotes
38 );
39
40 if ($order->Discount) {
41 $document['Документ' . $document_counter]['Скидки']['Скидка'] = array(
42 'Наименование' => 'Скидка',
43 'Сумма' => $order->Discount,
44 'УчтеноВСумме' => 1,
45 );
46 }
47 $document['Документ' . $document_counter]['Контрагенты']['Контрагент'] = array(
48 'Ид' => $order->MemberID . '#' . $order->Email,
49 'Наименование' => trim($order->ClientName),
50 'Роль' => 'Покупатель',
51 'ПолноеНаименование' => trim($order->ClientName),
52 'Имя' => $order->ClientName,
53
54 55 56 57 58 59 60 61 62 63 64 65 66 67
68 'Контакты' => array(
69 'Контакт1' => array(
70 'Тип' => 'ТелефонРабочий'
71 ,'Значение' => $order->Phone
72 )
73 ,'Контакт2' => array(
74 'Тип' => 'Почта'
75 ,'Значение' => $order->Email
76 )
77 )
78 );
79 80 81 82 83
84
85
86 $address = array();
87 if ($order->Address)
88 $address[] = $order->Address;
89 if ($order->City)
90 $address[] = $order->City;
91 if ($order->PostalCode)
92 $address[] = $order->PostalCode;
93 if ($order->Country)
94 $address[] = $order->Country;
95 if (count($address)) {
96
97 $document['Документ' . $document_counter]['Контрагенты']['Контрагент']['Адрес'] = array(
98 'Представление' => implode(', ', $address)
99 );
100 }
101
102
103 $products = $order->Items();
104
105 $product_counter = 0;
106 foreach ($products as $product) {
107 if (!$product->getProduct()) {
108 continue;
109 }
110 $id = $product->ImportID;
111
112 $document['Документ' . $document_counter]['Товары']['Товар' . $product_counter] = array(
113 'Ид' => $id,
114 'ИдКаталога' => $product->getProduct()->Parent()->ImportID,
115 'Артикул' => $product->ImportID,
116 'Наименование' => $product->Title,
117 'БазоваяЕдиница' => 'шт',
118 'ЦенаЗаЕдиницу' => $product->Price,
119 'Количество' => $product->Quantity,
120 'Сумма' => $product->TotalPrice,
121 );
122
123
124 $document['Документ' . $document_counter]['Товары']['Товар' . $product_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита0']= array(
125 'Наименование' => 'ВидНоменклатуры',
126 'Значение'=> 'Товар',
127 );
128 $document['Документ' . $document_counter]['Товары']['Товар' . $product_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита1']= array(
129 'Наименование' => 'ТипНоменклатуры',
130 'Значение'=> 'Товар',
131 );
132
133 $product_counter++;
134 }
135
136 if ($product_counter == 0) {
137 unset($document['Документ' . $document_counter]);
138 continue;
139 }
140
141
142
143 if ($order->ShippingTotal) {
144 $document['Документ' . $document_counter]['Товары']['Товар' . $product_counter] = array(
145 'Ид' => 'ORDER_DELIVERY'
146 ,'Наименование' => 'Доставка заказа'
147 ,'ЦенаЗаЕдиницу'=> $order->ShippingTotal
148 ,'Количество' => 1
149 ,'Сумма' => $order->ShippingTotal
150 );
151 $document['Документ' . $document_counter]['Товары']['Товар' . $product_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита0']= array(
152 'Наименование' => 'ВидНоменклатуры',
153 'Значение'=> 'Услуга',
154 );
155 $document['Документ' . $document_counter]['Товары']['Товар' . $product_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита1'] = array(
156 'Наименование' => 'ТипНоменклатуры',
157 'Значение'=> 'Услуга',
158 );
159 }
160
161
162 $document['Документ' . $document_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита0'] = array(
163 'Наименование' => 'Статус заказа',
164 'Значение'=> self::$statuses1C[$order->Status],
165 );
166
167
168 if ($order->hasMethod('PaymentType')) {
169 $method = $order->PaymentType();
170 if ($method) {
171 $document['Документ' . $document_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита1'] = array(
172 'Наименование' => 'Метод оплаты',
173 'Значение'=> $method->Title,
174 );
175 }
176 }
177
178
179
180 if ($order->hasMethod('ShippingMethod')) {
181 $method = $order->ShippingMethod();
182 if ($method) {
183 $document['Документ' . $document_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита2'] = array(
184 'Наименование' => 'Способ доставки',
185 'Значение'=> $method->Name,
186 );
187 }
188 }
189
190 $document['Документ' . $document_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита3'] = array(
191 'Наименование' => 'Заказ оплачен',
192 'Значение'=> (in_array($order->Status, array('Paid','Processing', 'Sent','Complete')) ? 'true' : 'false'),
193 );
194
195 $document['Документ' . $document_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита4'] = array(
196 'Наименование' => 'Отменен',
197 'Значение'=> (in_array($order->Status, array('AdminCancelled','MemberCancelled')) ? 'true' : 'false'),
198 );
199
200 $document['Документ' . $document_counter]['ЗначенияРеквизитов']['ЗначениеРеквизита5']= array(
201 'Наименование' => 'Финальный статус',
202 'Значение'=> 'false',
203 );
204 $document_counter++;
205 }
206
207 $root = '<?xml version="1.0" encoding="utf-8"?><КоммерческаяИнформация ВерсияСхемы="2.04" ДатаФормирования="' . date('Y-m-d', time()) . '" />';
208 $xml = $this->array_to_xml($document, new SimpleXMLElement($root));
209 file_put_contents(ASSETS_PATH . '/export_orders.xml', $xml->asXML());
210 return $xml->asXML();
211 }
212 return false;
213 }
214
215 function array_to_xml($data, &$xml) {
216 foreach($data as $key => $value) {
217 if (is_array($value)) {
218 if (!is_numeric($key)) {
219 $subnode = $xml->addChild(preg_replace('/\d/', '', $key));
220 $this->array_to_xml($value, $subnode);
221 }
222 }
223 else {
224 $xml->addChild($key, $value);
225 }
226 }
227 return $xml;
228 }
229
230 function setOrdersExported() {
231 $statusesExportFrom = SiteConfig::current_site_config()->StatusesToExport();
232 $orders = DataObject::get('Order', 'IsExported = 0 AND Status IN(' . implode(', ', $statusesExportFrom) . ')');
233 if ($orders) {
234 foreach($orders as $order) {
235 $order->IsExported = 1;
236 $order->Update1C = 1;
237 $order->write();
238 }
239 }
240 }
241
242 function importOrders($filePath) {
243 $file = simplexml_load_file($filePath);
244 foreach($file->Документ as $orderData) {
245
246 $order = DataObject::get_by_id('Order', (int)$orderData->Номер);
247 if (!$order) {
248
249 continue;
250 }
251 foreach($orderData->ЗначенияРеквизитов->ЗначениеРеквизита as $entry) {
252 if ($entry->Наименование == 'Статуса заказа ИД') {
253 $order->Status = (string)$entry->Значение;
254 $order->Update1C = 1;
255 $order->write();
256 file_put_contents(ASSETS_PATH . '/export_order_status_'.((int)$orderData->Номер).'.dat', $order->Status);
257 }
258 }
259 }
260 }
261
262 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-