25 parent::configuration();
32 protected function convert(array $documents)
38 $fieldsOrder = $documentOrder->getFieldValues();
39 $itemsOrder = $this->getProductsItems($fieldsOrder);
41 if(is_array($fieldsOrder[
'SUBORDINATES']))
43 foreach ($fieldsOrder[
'SUBORDINATES'] as $subordinateDocumentFields)
47 if($typeId == static::getShipmentEntityTypeId())
49 $subordinateDocumentItems = array();
50 $itemsSubordinate = $this->getProductsItems($subordinateDocumentFields);
52 foreach ($itemsSubordinate as $itemSubordinate)
54 $xmlId = key($itemSubordinate);
56 if($xmlId == self::DELIVERY_SERVICE_XMLID)
59 $subordinateDocumentItems[] = $itemSubordinate;
67 $item[$xmlId][
'QUANTITY'] = $itemSubordinate[$xmlId][
'QUANTITY'];
68 $subordinateDocumentItems[] = $item;
73 unset($subordinateDocumentFields[
'ITEMS']);
74 unset($subordinateDocumentFields[
'ITEMS_FIELDS']);
76 if(count($subordinateDocumentItems)>0)
78 $subordinateDocumentFields[
'ITEMS'] = $subordinateDocumentItems;
82 $document = OneC\DocumentImportFactory::create($typeId);
83 $document->setFields($subordinateDocumentFields);
84 $documents[] = $document;
86 $documentOrder->setField(
'SUBORDINATES',
'');
97 if(!$this->hasDocumentByTypeId(static::getShipmentEntityTypeId(), $documents))
99 if($this->deliveryServiceExists($itemsOrder))
103 if($entityOrder->getFieldValues()[
'TRAITS'][
'ID']>0)
105 self::load($entityOrder, [
'ID'=>$entityOrder->getFieldValues()[
'TRAITS'][
'ID']]);
107 $order = $entityOrder->getEntity();
111 $shipmentIsShipped =
false;
113 foreach (
$order->getShipmentCollection() as $shipment)
115 if($shipment->isShipped())
117 $shipmentIsShipped =
true;
121 if(!$shipment->isSystem())
123 $shipmentList[] = $shipment->getFieldValues();
127 if(!$shipmentIsShipped)
129 if(count($shipmentList)>0)
132 $externalId = current($shipmentList)[
'ID_1C'];
133 $shipmentFields[
'ID_1C'] = $externalId ==
''? $documentOrder->getField(
'ID_1C'):$externalId;
134 $shipmentFields[
'ID'] = current($shipmentList)[
'ID'];
139 $shipmentFields[
'ID_1C'] = $documentOrder->getField(
'ID_1C');
142 $shipmentFields[
'ITEMS'] = $itemsOrder;
144 $documentShipment =
new ShipmentDocument();
145 $documentShipment->setFields($shipmentFields);
146 $documents[] = $documentShipment;
163 if($entityOrder->getFieldValues()[
'TRAITS'][
'ID']>0)
165 self::load($entityOrder, [
'ID'=>$entityOrder->getFieldValues()[
'TRAITS'][
'ID']]);
167 $order = $entityOrder->getEntity();
171 $hasPayment =
$order->isPaid() || $this->orderPartiallyIsPaid(
$order);
178 if(
$order->getPrice() <= $this->getPaymentDocumentsPaidSum($documents))
186 $paymentDocumentsPaidSum = $this->getPaymentDocumentsPaidSum($documents);
187 if(
$order->getPrice() > $paymentDocumentsPaidSum)
196 $r = $this->deletePaymentToReplace(
$order, $documents);
199 $entityOrder->save();
202 if($r->getData()[
'IS_REPLACE'] ===
true)
204 static::setConfig(static::DELETE_IF_NOT_FOUND_RELATED_PAYMENT_DOCUMENT,
false);
215 $documentPayment =
new PaymentCardDocument();
216 $documentPayment->setFields([
217 'ID_1C'=>$documentOrder->getField(
'ID_1C'),
218 'AMOUNT'=>abs(
$order->getPrice() - $paymentDocumentsPaidSum)
221 $documents[] = $documentPayment;
232 static::setConfig(static::DELETE_IF_NOT_FOUND_RELATED_PAYMENT_DOCUMENT,
false);
242 return parent::convert($documents);
253 foreach ($documents as $document)
259 $result[] = $document;
264 $result[] = $document;
277 return ($document->
getField(
'REK_VALUES')[
'1C_PAYED'] ==
'Y');
286 return ($document->
getTypeId() == static::getPaymentCardEntityTypeId()
287 || $document->
getTypeId() == static::getPaymentCashLessEntityTypeId()
288 || $document->
getTypeId() == static::getPaymentCashEntityTypeId());
292 protected function documentPaymentReplaceId(
Order $order, $documents)
296 $paymentCollection =
$order->getPaymentCollection();
297 $paymentIsReplace =
false;
300 foreach($paymentCollection as $payment)
302 if(!$payment->isPaid())
308 $payment->getSum() == (
float)$document->getField(
'AMOUNT') &&
309 $this->resolveEntityTypeId($payment) == DocumentType::resolveID($document->getField(
'OPERATION'))
312 $document->setField(
'ID', $payment->getId());
313 $paymentIsReplace =
true;
319 $result->setData([
'IS_REPLACE'=>$paymentIsReplace]);
328 protected function deletePaymentToReplace(Order
$order, $documents)
330 $result =
new Result();
332 $paymentCollection =
$order->getPaymentCollection();
333 $paymentIsReplace =
false;
336 foreach ($documents as $document)
341 'AMOUNT'=>(float)$document->getField(
'AMOUNT'),
342 'OPERATION'=>DocumentType::resolveID($document->getField(
'OPERATION'))
350 foreach($paymentCollection as $payment)
352 if(!$payment->isPaid())
354 foreach($list as $k=>$documentPayment)
359 $payment->getSum() == $documentPayment[
'AMOUNT'] &&
360 $this->resolveEntityTypeId($payment) == $documentPayment[
'OPERATION']
363 $r = $this->paymentDelete($payment);
366 $result->addErrors($r->getErrors());
370 $paymentIsReplace =
true;
380 if($result->isSuccess() && $paymentIsReplace)
381 $result->setData([
'IS_REPLACE'=>
true]);
392 return ($this->hasDocumentByTypeId(static::getPaymentCardEntityTypeId(), $documents)
393 || $this->hasDocumentByTypeId(static::getPaymentCashLessEntityTypeId(), $documents)
394 || $this->hasDocumentByTypeId(static::getPaymentCashEntityTypeId(), $documents));
405 foreach ($documents as $document)
419 protected function getPaymentDocumentsPaidSum($documents)
428 $sum += (float)$document->getField(
'AMOUNT');
434 protected function orderPartiallyIsPaid(Order
$order)
436 $paymentCollection =
$order->getPaymentCollection();
437 if(count($paymentCollection)>0)
440 foreach ($paymentCollection as $payment)
442 if($payment->isPaid())
457 if(isset(
$fields[
'OPERATION']))
472 foreach ($items as $item)
474 if(array_key_exists($key, $item))
488 return ConverterFactory::create($typeId);
506 return EntityImportFactory::create($typeId);