Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
exportonecpackage.php
1<?php
3
4
25
26abstract class ExportOneCPackage extends ExportOneCBase
27{
28 use PackageTrait
29 {
30 PackageTrait::load as protected load_AliasTrait;
31 }
32 use LoggerTrait;
33 use BaseTrait;
34
35 private static $instance = null;
36
40 public static function getInstance()
41 {
42 if(self::$instance === null)
43 {
44 self::$instance = new static();
45 }
46 return self::$instance;
47 }
48
49 private function __clone() {}
50 private function __construct() {}
51
52
53 public static function configuration()
54 {
55 ManagerExport::registerInstance(static::getParentEntityTypeId(), OneC\ExportSettings::getCurrent());
56 ManagerExport::registerInstance(static::getShipmentEntityTypeId(), OneC\ExportSettings::getCurrent());
57 ManagerExport::registerInstance(static::getPaymentCashEntityTypeId(), OneC\ExportSettings::getCurrent());
58 ManagerExport::registerInstance(static::getPaymentCashLessEntityTypeId(), OneC\ExportSettings::getCurrent());
59 ManagerExport::registerInstance(static::getPaymentCardEntityTypeId(), OneC\ExportSettings::getCurrent());
61 }
62
66 static protected function getParentEntityTypeId()
67 {
68 return EntityType::ORDER;
69 }
70
71 static protected function getShipmentEntityTypeId()
72 {
74 }
75
76 static protected function getPaymentCardEntityTypeId()
77 {
79 }
80
81 static protected function getPaymentCashEntityTypeId()
82 {
84 }
85
86 static protected function getPaymentCashLessEntityTypeId()
87 {
89 }
90
95 protected function getItems(array $fields)
96 {
97 $result = new Result();
98
99 $orderId = $fields['ORDER_ID'];
100
101 $orderImport = $this->entityFactoryCreate(static::getParentEntityTypeId());
102 ManagerExport::configure($orderImport);
103 static::load($orderImport, array('ID'=>$orderId));
104
106 $order = $orderImport->getEntity();
107
108 if($order !== null)
109 {
110 $profileImport = $this->entityFactoryCreate(EntityType::USER_PROFILE);
111 ManagerExport::configure($profileImport);
112 static::load($profileImport, array('ID'=>$order->getUserId()));
113
114 $list = array_merge(
115 $this->loadItemsByCollection($order->getPaymentCollection(), $order),
116 $this->loadItemsByCollection($order->getShipmentCollection(), $order));
117
118 $list[] = $orderImport;
119 $list[] = $profileImport;
120
121 $this->initLogger($list);
122
123 $result->setData($list);
124 }
125 else
126 {
127 $result->addError(new Error(str_replace('#ID#',$orderId, DocumentBase::getLangByCodeField("ORDER_NOT_FOUND"))));
128 }
129
130 return $result;
131 }
132
138 protected function loadItemsByCollection(CollectionBase $collection, Order $order)
139 {
140 $list = array();
141
142 if(count($collection)>0)
143 {
144 foreach ($collection as $entity)
145 {
146 if($entity instanceof Shipment)
147 {
148 if($entity->isSystem())
149 continue;
150 }
151
152 $typeId = $this->resolveEntityTypeId($entity);
153
155 $entityExport = $this->entityFactoryCreate($typeId);
156 ManagerExport::configure($entityExport);
157 static::load($entityExport, array('ID'=>$entity->getId()), $order);
158
159 $list[] = $entityExport;
160 }
161 }
162
163 return $list;
164 }
165
170 protected function resolveEntityTypeId(Entity $entity)
171 {
172 $typeId = EntityType::UNDEFINED;
173
174 if($entity instanceof Order)
175 $typeId = OrderImport::resolveEntityTypeId($entity);
176 elseif($entity instanceof Payment)
177 $typeId = PaymentImport::resolveEntityTypeId($entity);
178 elseif($entity instanceof Shipment)
179 $typeId = ShipmentImport::resolveEntityTypeId($entity);
180
181 return $typeId;
182 }
183
188 protected function convert(array $items)
189 {
190 $result = new Result();
191 $list = array();
192
193 $this->convertEntityFields($items);
194
195 foreach ($items as $item)
196 {
197 $list[] = $this->convertEntity($item);
198 }
199
200 $this->convertDocumentFields($list);
201
202 $list = $this->modifyDocumentsCollection($list);
203
204 if($result->isSuccess())
205 {
206 $result->setData($list);
207 }
208 return $result;
209 }
210
214 protected function convertDocumentFields(array $documents)
215 {
216 $documentOrder = $this->getDocumentByTypeId(DocumentType::ORDER, $documents);
217 $orderFields = $documentOrder->getFieldValues();
218
220 $documentProfile = $this->getDocumentByTypeId(DocumentType::USER_PROFILE, $documents);
221 $orderFields['AGENT'] = $documentProfile->getFieldValues();
222
223 $documentOrder->setFields($orderFields);
224 }
225
230 protected function convertEntityFields(array $items)
231 {
232 $result = new Result();
233
234 $orderImport = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
236 $profileImport = $this->getEntityByTypeId(EntityType::USER_PROFILE, $items);
237
238 //region business value order profile shipment payment
239 $this->prepareEntityFieldsBusinessValue($orderImport);
240 $this->prepareEntityFieldsBusinessValue($profileImport, $orderImport);
241
242 foreach ($items as $item)
243 {
244 if($item instanceof ShipmentImport || $item instanceof PaymentImport)
245 {
246 $this->prepareEntityFieldsBusinessValue($item);
247 }
248 }
249 //endregion
250
251 //region payment.lid && payment.vesion
252 $orderFields = $orderImport->getFieldValues();
253 foreach ($items as $item)
254 {
255 if($item instanceof PaymentImport)
256 {
257 $paymentFields = $item->getFieldValues();
258 $paymentFields['TRAITS']['LID'] = $orderFields['TRAITS']['LID'];
259 $paymentFields['TRAITS']['VERSION'] = $orderFields['TRAITS']['VERSION'];
260 $item->setFields($paymentFields);
261 }
262 }
263 //endregion
264
265 //region shipment.lid && shipment.version
266 $orderFields = $orderImport->getFieldValues();
267 foreach ($items as $item)
268 {
269 if($item instanceof ShipmentImport)
270 {
271 $shipmentFields = $item->getFieldValues();
272 $shipmentFields['TRAITS']['LID'] = $orderFields['TRAITS']['LID'];
273 $shipmentFields['TRAITS']['VERSION'] = $orderFields['TRAITS']['VERSION'];
274 $item->setFields($shipmentFields);
275 }
276 }
277 //endregion
278
279 $this->addItemOrderDelivery($items);
280
281 return $result;
282 }
283
287 protected function addItemOrderDelivery(array $items)
288 {
289 $orderImport = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
290
291 //region shipment.items ORDER_DELIVERY
292 $orderFields = $orderImport->getFieldValues();
293 foreach ($items as $item)
294 {
295 if($item instanceof ShipmentImport)
296 {
297 $shipmentFields = $item->getFieldValues();
299 $shipmemt = $item->getEntity();
300 if($shipmemt->getPrice()>0)
301 {
302
303 $shipmentFields['ITEMS'][] = array_merge(
304 array(
306 'NAME'=>DocumentBase::getLangByCodeField(ImportOneCBase::DELIVERY_SERVICE_XMLID),
307 'MEASURE_CODE'=>Converter::MEASURE_CODE_DEFAULT,
308 'KOEF'=>Converter::KOEF_DEFAULT,
309 'PRICE'=>$shipmemt->getPrice(),
310 'QUANTITY'=>1
311 ),
312 $this->getVatRateByShipment($item, $this->getSummOrderTaxes($orderFields['TAXES']))
313 );
314 }
315 $item->setFields($shipmentFields);
316 }
317 }
318 //endregion
319
320 //region order.items ORDER_DELIVERY
321 $orderFields = $orderImport->getFieldValues();
322 foreach ($items as $item)
323 {
324 if($item instanceof ShipmentImport)
325 {
326 $shipmentItems = $this->getProductsItems($item->getFieldValues());
327 if($this->deliveryServiceExists($shipmentItems))
328 {
329 $orderFields['ITEMS'][] = $this->getDeliveryServiceItem($shipmentItems);
330 }
331 }
332 }
333 $orderImport->setFields($orderFields);
334 //endregion
335 }
336
341 protected function getDeliveryServiceItem(array $list)
342 {
343 foreach ($list as $k=>$items)
344 {
345 if($items['PRODUCT_XML_ID'] == ImportOneCBase::DELIVERY_SERVICE_XMLID)
346 {
347 return $items;
348 }
349 }
350
351 return null;
352 }
353
358 protected function getSummOrderTaxes($taxes)
359 {
360 $orderTax = 0;
361 foreach ($taxes as $tax)
362 {
363 $tax["VALUE_MONEY"] = roundEx($tax["VALUE_MONEY"], 2);
364 $orderTax += $tax["VALUE_MONEY"];
365 }
366 return $orderTax;
367 }
368
374 protected function getVatRateByShipment(ShipmentImport $item, $orderTax)
375 {
376 $result = array();
377
378 $shipmentFields = $item->getFieldValues();
380 $shipmemt = $item->getEntity();
381 if($shipmemt->getPrice()>0)
382 {
383 $vatRate = 0;
384 $vatSum = 0;
385 $order = $shipmemt->getParentOrder();
387 foreach ($order->getBasket() as $basket)
388 {
389 $vatRate = (float)$basket->getVatRate();
390 $basketVatSum = $basket->getPrice()/($vatRate+1) * $vatRate;
391 $vatSum += roundEx($basketVatSum * $basket->getQuantity(), 2);
392 }
393
394 $tax = roundEx((($shipmemt->getPrice() / ($vatRate+1)) * $vatRate), 2);
395
396 if($orderTax > $vatSum && $orderTax == roundEx($vatSum + $tax, 2))
397 {
398 $result = array('VAT_RATE'=>$vatRate*100);
399 }
400 }
401 return $result;
402 }
403
408 protected function prepareDocumentFieldsDeliveryAddress(DocumentBase $document, UserProfileDocument $documentProfile)
409 {
410 $fields = $document->getFieldValues();
411 $profileFields = $documentProfile->getFieldValues();
412
413 $fields['REK_VALUES'] = is_array($fields['REK_VALUES'])? $fields['REK_VALUES']:array();
414 $profileFields['REK_VALUES'] = is_array($profileFields['REK_VALUES'])? $profileFields['REK_VALUES']:array();
415
416 $fields['REK_VALUES'] = array_merge($fields['REK_VALUES'], $profileFields['REK_VALUES']);
417
418 $document->setFields($fields);
419 }
420
426 protected function prepareEntityFieldsBusinessValue(ImportBase $item, OrderImport $orderImport=null)
427 {
428 if(!($item instanceof OrderImport || $item instanceof ShipmentImport || $item instanceof PaymentImport || $item instanceof UserImportBase))
429 throw new ArgumentException("Entity must be instanceof OrderImport or ShipmentImport or PaymentImport or ProfileImport");
430
431 $fields = $item->getFieldValues();
432
434 $provider = ($item instanceof UserImportBase ? $orderImport->getEntity():$item->getEntity());
435 $fields['BUSINESS_VALUE'] = $item::getBusinessValue($provider);
436
437 $item->setFields($fields);
438 }
439
445 protected function outputXmlDocuments(array $documents, $level=0)
446 {
447 $result = new Result();
448 $list = array();
449
450 foreach ($documents as $document)
451 {
452 $list[] = $this->outputXmlDocument($document, $level);
453 }
454 $result->setData($list);
455
456 return $result;
457 }
458
464 protected function outputXmlDocument(DocumentBase $document, $level)
465 {
466 $xml = $document->openNodeDirectory($level, $document::getLangByCodeField($document->getNameNodeDocument()));
467 $xml .= $document->output($level+1);
468 $xml .= $document->closeNodeDirectory($level, $document::getLangByCodeField($document->getNameNodeDocument()));
469 return $xml;
470 }
471
476 protected function modifyDocumentsCollection(array $list)
477 {
478 $result = array();
479 foreach ($list as $document)
480 {
481 if(!($document instanceof UserProfileDocument))
482 $result[] = $document;
483 }
484 return $result;
485 }
486
490 protected function initLogger(array $items)
491 {
492 foreach ($items as $item)
493 {
494 $item->initLogger();
495 }
496 }
497
502 protected function convertEntity(ImportBase $item)
503 {
504 $params = $item->getFieldValues();
505
506 $documentTypeId = $this->resolveDocumentTypeId($item->getOwnerTypeId());
508
509 $convertor = $this->converterFactoryCreate($item->getOwnerTypeId());
510 $convertor->init(
511 $settings,
512 $item->getOwnerTypeId(),
513 $documentTypeId
514 );
515
516 $fields = $convertor->externalize($params);
517
518 $document = $this->documentFactoryCreate($documentTypeId);
519 $document->setFields($fields);
520
521 return $document;
522 }
523
528 protected function resolveDocumentTypeId($typeId)
529 {
530 $documentTypeId = DocumentType::UNDEFINED;
531
532 switch ($typeId)
533 {
534 case static::getParentEntityTypeId():
535 $documentTypeId = DocumentType::ORDER;
536 break;
537 case static::getPaymentCardEntityTypeId():
538 $documentTypeId = OneC\DocumentType::PAYMENT_CARD_TRANSACTION;
539 break;
540 case static::getPaymentCashEntityTypeId():
541 $documentTypeId = OneC\DocumentType::PAYMENT_CASH;
542 break;
543 case static::getPaymentCashLessEntityTypeId():
544 $documentTypeId = DocumentType::PAYMENT_CASH_LESS;
545 break;
546 case static::getShipmentEntityTypeId():
547 $documentTypeId = OneC\DocumentType::SHIPMENT;
548 break;
551 $documentTypeId = $typeId;
552 break;
553 }
554 return $documentTypeId;
555 }
556
561 protected function export(array $documents)
562 {
563 $result = new Result();
564
565 $xml = '';
566 $r = $this->outputXmlDocuments($documents);
567 if($r->isSuccess())
568 {
569 $xml = implode('', $r->getData());
570
571 $this->setRawData($xml);
572 }
573
574 return $result->setData([$xml]);
575 }
576
582 protected static function load(ImportBase $item, array $fields, $order=null)
583 {
584 static::load_AliasTrait($item, $fields, $order);
585
586 if($item->getEntity() !== null)
587 {
588 $item->initFields();
589 }
590 }
591
596 protected function logger(array $items)
597 {
599 $orderItem = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
600 return $this->loggerEntitiesPackage($items, $orderItem);
601 }
602}
outputXmlDocuments(array $documents, $level=0)
prepareDocumentFieldsDeliveryAddress(DocumentBase $document, UserProfileDocument $documentProfile)
outputXmlDocument(DocumentBase $document, $level)
static load(ImportBase $item, array $fields, $order=null)
static configure(ImportBase $entity)
static registerInstance($typeId, ISettingsExport $settings)
loggerEntitiesPackage(array $items, OrderImport $orderItem)