1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ordercompatibility.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\Compatible;
4
5use Bitrix\Main;
6use Bitrix\Sale\Compatible\Internals;
7use Bitrix\Sale\Internals\OrderTable;
8use Bitrix\Sale;
9use Bitrix\Sale\Delivery\Services;
10
12
18{
20 protected $order = null;
21
23 protected $requestFields = null;
24
26 protected $basket = null;
27
28 protected $externalPrice = null;
29
30
34
35 protected $runtimeFields = array();
37
41 protected static function getRegistryType()
42 {
44 }
45
51 protected static function getEntity()
52 {
53 return OrderTable::getEntity();
54 }
55
59 protected static function getBasketCompatibilityClassName()
60 {
61 return BasketCompatibility::class;
62 }
63
70 protected function __construct(array $fields = array())
71 {
73 $this->query = new OrderQuery(static::getEntity());
74 $this->fields = new Sale\Internals\Fields($fields);
75 }
76
80 public function getOrder()
81 {
82 return $this->order;
83 }
84
88 public function getRequestFields()
89 {
91 }
92
96 public function setBasketCompatibility(BasketCompatibility $basketCompatibility)
97 {
98 $this->basket = $basketCompatibility;
99 }
100
105 public static function create(array $fields)
106 {
107 $adminSection = (defined('ADMIN_SECTION') && ADMIN_SECTION === true);
108
109 $orderCompatibility = new static();
110
111
112
113 $registry = Sale\Registry::getInstance(static::getRegistryType());
115 $orderClassName = $registry->getOrderClassName();
116
117 if (isset($fields['ID']) && intval($fields['ID']) > 0)
118 {
119 if (!$order = $orderClassName::load($fields['ID']))
120 {
121 throw new Sale\UserMessageException('Order not found');
122 }
123 }
124 else
125 {
126 $lid = $fields['LID'] ?? '';
127 $userId = $fields['USER_ID'] ?? null;
128 $currency = $fields['CURRENCY'] ?? null;
129 if (!$order = $orderClassName::create($lid, $userId, $currency))
130 {
131 throw new Sale\UserMessageException('Order not create');
132 }
133 }
134
135 if (isset($fields['PERSON_TYPE_ID']) && intval($fields['PERSON_TYPE_ID']) > 0)
136 {
137 $order->setPersonTypeId($fields['PERSON_TYPE_ID']);
138 }
139
140 $orderFields = static::replaceFields($fields, static::getOrderReplaceFields());
141
142 $orderFields = $orderCompatibility->parseRawFields(static::ENTITY_ORDER, $orderFields);
143
144 $orderFields = static::clearFields($orderFields);
145 foreach (static::getFieldsFromOtherEntities() as $wrongField)
146 {
147 if (array_key_exists($wrongField, $fields))
148 unset($orderFields[$wrongField]);
149 }
150
151 $orderFields = static::convertDateFields($orderFields, static::getOrderDateFields());
152
153 unset($orderFields['MARKED']);
154 unset($orderFields['CANCELED']);
155
156 if (array_key_exists('PRICE', $orderFields))
157 {
158 $orderCompatibility->externalPrice = $orderFields['PRICE'];
159 }
160
161 if ($order->getId() > 0)
162 {
163 if ($adminSection)
164 {
165 unset($orderFields['PRICE']);
166 }
167
168 unset($orderFields['PRICE_DELIVERY']);
169 unset($orderFields['DISCOUNT_VALUE']);
170 unset($orderFields['TAX_VALUE']);
171 $order->setField('DATE_UPDATE', new Main\Type\DateTime());
172 }
173 else
174 {
175 if (!$adminSection)
176 unset($orderFields['SUM_PAID']);
177
178 $order->setField('DATE_INSERT', new Main\Type\DateTime());
179 $order->setField('DATE_UPDATE', new Main\Type\DateTime());
180 }
181
182 unset($orderFields['TAX_PRICE']);
183
184 if (array_key_exists('STATUS_ID', $orderFields) && $order->getId() > 0)
185 {
186 $order->setField('STATUS_ID', $orderFields['STATUS_ID']);
187 unset($orderFields['STATUS_ID']);
188 }
189
190 if (isset($orderFields['USE_VAT']) && $orderFields['USE_VAT'] === true)
191 {
192 $orderFields['USE_VAT'] = 'Y';
193 }
194
195 $order->setFieldsNoDemand($orderFields);
196
197 $orderCompatibility->order = $order;
198
199 $orderCompatibility->requestFields = $fields;
200
201 $order->getDiscount();
202
203 return $orderCompatibility;
204 }
205
216 public static function fillOrderFromRequest(Sale\Order $order, array $fields)
217 {
218 global $USER;
219 $result = new Sale\Result();
220 if (isset($fields['CANCELED']))
221 {
222 if ($order->getId() > 0 && $order->getField('CANCELED') != $fields['CANCELED'])
223 {
224 if (!(\CSaleOrder::CanUserCancelOrder($order->getId(), $USER->GetUserGroupArray(), $USER->GetID())))
225 {
226 $result->addError( new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_CANCEL_NO_PERMISSION'), 'SALE_COMPATIBLE_ORDER_CANCEL_NO_PERMISSION') );
227 return $result;
228 }
229
231 $r = $order->setField('CANCELED', $fields['CANCELED']);
232 if (!$r->isSuccess())
233 {
234 $result->addErrors($r->getErrors());
235 return $result;
236 }
237
238 if (array_key_exists("REASON_CANCELED", $fields))
239 {
241 $r = $order->setField('REASON_CANCELED', $fields['REASON_CANCELED']);
242 if (!$r->isSuccess())
243 {
244 $result->addErrors($r->getErrors());
245 return $result;
246 }
247 }
248
249 }
250 }
251
252
253 if (isset($fields['MARKED']))
254 {
255 if ($order->getId() > 0)
256 {
257 if ($fields['MARKED'] == 'Y')
258 {
259 $reasonMarked = '';
260 if (!empty($fields['REASON_MARKED']))
261 {
262 $reasonMarked = trim($fields['REASON_MARKED']);
263 }
264
265 $r = new Sale\Result();
266 $r->addError(new Sale\ResultWarning($reasonMarked, 'SALE_ORDER_MARKER_ERROR'));
267
268 $registry = Sale\Registry::getInstance(static::getRegistryType());
269
271 $entityMarkerClassName = $registry->getEntityMarkerClassName();
272 $entityMarkerClassName::addMarker($order, $order, $r);
273 }
274
275 if ($order->getField('MARKED') != $fields['MARKED'])
276 {
278 $r = $order->setField('MARKED', $fields['MARKED']);
279 if (!$r->isSuccess())
280 {
281 $result->addErrors($r->getErrors());
282 }
283 }
284 }
285 }
286
287 if ($order->getId() > 0 && !empty($fields['ACCOUNT_NUMBER']) && !empty($fields['SITE_ID']))
288 {
289 $filter = array(
290 'filter' => array(
291 '=ACCOUNT_NUMBER' => $fields['ACCOUNT_NUMBER'],
292 '!ID' => $order->getId()
293 ),
294 'select' => array('ID')
295 );
296
297 $registry = Sale\Registry::getInstance(static::getRegistryType());
298
300 $orderClassName = $registry->getOrderClassName();
301 if (($res = $orderClassName::getList($filter)) && ($res->fetch()))
302 {
303 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_ACCOUNT_NUMBER_ALREADY_EXISTS'), 'SALE_COMPATIBLE_ORDER_ACCOUNT_NUMBER_ALREADY_EXISTS'));
304 }
305 }
306
307
308 return $result;
309 }
310
311
328 public function fillShipmentCollectionFromRequest(Sale\ShipmentCollection $shipmentCollection, array $fields)
329 {
330 $result = new Sale\Result();
331
333 if (!$order = $shipmentCollection->getOrder())
334 {
335 throw new Main\ObjectNotFoundException('Entity "Order" not found');
336 }
337
338 $shipment = null;
339 $deliveryId = null;
340 $deliveryCode = trim((string)($fields['DELIVERY_ID'] ?? ''));
341 if ($deliveryCode === '')
342 {
343 $deliveryCode = null;
344 }
345
346 if ($deliveryCode !== null)
347 {
348 $deliveryId = \CSaleDelivery::getIdByCode($deliveryCode);
349 }
350
351 if ($order->getId() > 0)
352 {
353 //todo: check $deliveryId
354
355 if (count($shipmentCollection) == 2 && $shipmentCollection->isExistsSystemShipment())
356 {
358 foreach($shipmentCollection as $shipment)
359 {
360 if ($shipment->isSystem())
361 continue;
362
363 if ($deliveryId > 0 && $deliveryId != $shipment->getDeliveryId())
364 {
366 $r = $shipment->setField('DELIVERY_ID', $deliveryId);
367 if ($r->isSuccess())
368 {
370 $deliveryService = Sale\Delivery\Services\Manager::getObjectById($deliveryId);
371 if ($deliveryService)
372 {
373 $fields['DELIVERY_NAME'] = $deliveryService->isProfile()
374 ? $deliveryService->getNameWithParent()
375 : $deliveryService->getName();
376 }
377 else
378 {
379 $fields['DELIVERY_NAME'] = 'Not found [' . $deliveryId . ']';
380 }
381 }
382 else
383 {
384 $result->addErrors($r->getErrors());
385 }
386 }
387 elseif (intval($deliveryId) == 0 && array_key_exists('DELIVERY_ID', $fields) || (intval($deliveryId) !== intval($deliveryCode)))
388 {
389 unset($fields['DELIVERY_ID']);
390 }
391
392 if (array_key_exists('PRICE_DELIVERY', $fields) && (float)$fields['PRICE_DELIVERY'] != $shipment->getField('PRICE_DELIVERY'))
393 {
394 $fields['BASE_PRICE_DELIVERY'] = (float)$fields['PRICE_DELIVERY'];
395 $fields['CUSTOM_PRICE_DELIVERY'] = "Y";
396
397 unset($fields['PRICE_DELIVERY']);
398 }
399
400 $shipmentFields = static::convertDateFields($fields, static::getEntityDateFields($shipment));
401
402 unset($shipmentFields['ALLOW_DELIVERY']);
403 unset($shipmentFields['DEDUCTED']);
404
405 if (isset($fields['CURRENCY']))
406 {
407 if ($fields['CURRENCY'] != $shipmentFields['CURRENCY'])
408 {
409 $shipmentFields['CURRENCY'] = $fields['CURRENCY'];
410 }
411 }
412
414 $r = $shipment->setFields(static::clearFields($shipmentFields, static::getShipmentAvailableFields()));
415 if ($r->isSuccess())
416 {
417 static::fillOrderFieldsFromEntity($order, $shipment, $fields, static::getShipmentFieldsToConvert());
418 }
419 else
420 {
421 $result->addErrors($r->getErrors());
422 }
423
424 if ($shipment !== null)
425 {
426 DiscountCompatibility::setShipment($order->getId(), $shipment->getId());
427 }
428
429 unset($fields['DELIVERY_ID']);
430 }
431 }
432
433 }
434 else
435 {
436 if (intval($deliveryId) == 0)
437 {
438 $deliveryId = \Bitrix\Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId();
439 }
440
441 if (intval($deliveryId) > 0)
442 {
444 if ($shipment = static::createShipmentFromRequest($shipmentCollection, $deliveryId, $fields))
445 {
446 if (isset($fields['TRACKING_NUMBER']) && strval($fields['TRACKING_NUMBER']) != '')
447 {
448 $shipment->setField('TRACKING_NUMBER', $fields['TRACKING_NUMBER']);
449 }
450
451 if (isset($fields['DELIVERY_EXTRA_SERVICES']) && is_array($fields['DELIVERY_EXTRA_SERVICES']))
452 {
453 $shipment->setExtraServices($fields['DELIVERY_EXTRA_SERVICES']);
454 }
455
456 if (isset($fields['STORE_ID']) && intval($fields['STORE_ID']) > 0)
457 {
458 $shipment->setStoreId($fields['STORE_ID']);
459 }
460
461 if ($shipment !== null)
462 {
463 DiscountCompatibility::setShipment($order->getId(), $shipment->getId());
464 }
465
466 static::fillOrderFieldsFromEntity($order, $shipment, $fields, static::getShipmentFieldsToConvert());
467 }
468 }
469 }
470
471 if ($basket = $order->getBasket())
472 {
474 $basketCompatibilityClassName = static::getBasketCompatibilityClassName();
475
477 $r = $basketCompatibilityClassName::syncShipmentCollectionAndBasket($shipmentCollection, $basket);
478 if (!$r->isSuccess())
479 {
480 $result->addErrors($r->getErrors());
481 return $result;
482 }
483
484 }
485
487 $r = static::syncShipmentCollectionFromRequest($shipmentCollection, $fields);
488 if (!$r->isSuccess())
489 {
490 $result->addErrors($r->getErrors());
491 return $result;
492 }
493 if ($basket)
494 {
496 foreach ($shipmentCollection as $shipment)
497 {
498 if ($shipment->isSystem())
499 continue;
500
502 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
503 {
504 throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
505 }
506
507 if (!empty($fields['BARCODE_LIST']) && is_array($fields['BARCODE_LIST']))
508 {
510 $r = static::fillShipmentItemCollectionFromRequest($shipmentItemCollection, $fields['BARCODE_LIST'], $basket);
511 if (!$r->isSuccess())
512 {
513 $result->addErrors($r->getErrors());
514 return $result;
515 }
516 }
517
518 }
519 }
520 return $result;
521 }
522
529 private static function fillOrderFieldsFromEntity(Sale\Order $order, Sale\Internals\CollectableEntity $entity, array $requestFields, array $allowFields)
530 {
531 $dateFields = static::getEntityDateFields($entity);
532 foreach ($allowFields as $checkField)
533 {
534 $checkOrderField = $order->getField($checkField);
535
536 $isDate = false;
537
538 if (array_key_exists($checkField, $dateFields))
539 {
540 $isDate = true;
541 $checkOrderField = static::convertDateFieldToOldFormat($order->getField($checkField));
542 }
543
544 if (!empty($requestFields[$checkField]) && $checkOrderField != trim($requestFields[$checkField]))
545 {
546 $setValue = $entity->getField($checkField);
547 if ($isDate)
548 {
549 $setValue = static::convertDateField($checkField, $requestFields[$checkField], static::getEntityDateFields($entity));
550 }
551
552 if (in_array($checkField, static::getAvailableFields()))
553 {
554 $order->setFieldNoDemand($checkField, $setValue);
555 }
556 }
557 }
558 }
559
567 public static function createShipmentFromRequest(Sale\ShipmentCollection $shipmentCollection, $deliveryId, array $requestFields)
568 {
569
570 $shipment = null;
571
572 if (intval($deliveryId) > 0 && $service = Sale\Delivery\Services\Manager::getObjectById($deliveryId))
573 {
574
575 $shipment = $shipmentCollection->createItem($service);
576
577 if ($service->isProfile())
578 $serviceName = $service->getNameWithParent();
579 else
580 $serviceName = $service->getName();
581 $shipment->setField('DELIVERY_NAME', $serviceName);
582
583
584 if (isset($requestFields['DELIVERY_PRICE']) && floatval($requestFields['DELIVERY_PRICE']) > 0)
585 {
586 $basePriceDelivery = $requestFields['DELIVERY_PRICE'];
587 $priceDelivery = $requestFields['PRICE_DELIVERY'];
588
589 if (!empty($requestFields['PRICE_DELIVERY_DIFF']))
590 {
591 $basePriceDelivery = $priceDelivery + floatval($requestFields['PRICE_DELIVERY_DIFF']);
592 }
593
594 $shipment->setFieldNoDemand('BASE_PRICE_DELIVERY', $basePriceDelivery);
595 $shipment->setFieldNoDemand('CURRENCY', $requestFields['CURRENCY']);
596
597 $shipment->setFieldNoDemand('PRICE_DELIVERY', $priceDelivery);
598
599 if (isset($requestFields['PRICE_DELIVERY']) && $requestFields['PRICE_DELIVERY'] < $requestFields['DELIVERY_PRICE'])
600 $shipment->setFieldNoDemand('PRICE_DELIVERY', $requestFields['PRICE_DELIVERY']);
601 }
602 elseif (array_key_exists("PRICE_DELIVERY", $requestFields) && floatval($requestFields['PRICE_DELIVERY']) >= 0)
603 {
604 $shipment->setFieldNoDemand('PRICE_DELIVERY', floatval($requestFields['PRICE_DELIVERY']));
605 $shipment->setFieldNoDemand('BASE_PRICE_DELIVERY', floatval($requestFields['PRICE_DELIVERY']));
606 $shipment->setFieldNoDemand('CURRENCY', $requestFields['CURRENCY']);
607 $shipment->setFieldNoDemand('CUSTOM_PRICE_DELIVERY', "Y");
608 }
609 }
610
611 return $shipment;
612 }
613
625 public static function syncShipmentCollectionFromRequest(Sale\ShipmentCollection $shipmentCollection, array $fields)
626 {
627 $result = new Sale\Result();
628
629 $countShipments = count($shipmentCollection);
630 $baseShipment = null;
631
632 if ($countShipments <= 2 && $shipmentCollection->isExistsSystemShipment())
633 {
635 foreach ($shipmentCollection as $shipment)
636 {
637 if ($shipment->isSystem())
638 continue;
639
640 $baseShipment = $shipment;
641 }
642 }
643 else
644 {
645 return $result;
646 }
647
649 if (!$order = $shipmentCollection->getOrder())
650 {
651 throw new Main\ObjectNotFoundException('Entity "Order" not found');
652 }
653
654
655 if ($baseShipment === null)
656 {
657 return $result;
658 }
659
660 if (isset($fields['ALLOW_DELIVERY']) && strval($fields['ALLOW_DELIVERY']) != '')
661 {
662 if ($baseShipment->getField('ALLOW_DELIVERY') != $fields['ALLOW_DELIVERY'])
663 {
664 if ($fields['ALLOW_DELIVERY'] == "Y")
665 {
667 $r = $baseShipment->allowDelivery();
668 }
669 else
670 {
672 $r = $baseShipment->disallowDelivery();
673 }
674
675 if ($r->isSuccess())
676 {
677 $order->setFieldNoDemand('ALLOW_DELIVERY', $fields['ALLOW_DELIVERY']);
678 }
679 else
680 {
681 $result->addErrors($r->getErrors());
682 }
683 }
684 }
685
686
687 if (isset($fields['DEDUCTED']) && strval($fields['DEDUCTED']) != '')
688 {
689 if ($baseShipment->getField('DEDUCTED') != $fields['DEDUCTED'])
690 {
691 if ($fields['DEDUCTED'] == "Y")
692 {
694 $r = $baseShipment->tryShip();
695 }
696 else
697 {
699 $r = $baseShipment->tryUnship();
700 }
701
702 if ($r->isSuccess())
703 {
704 $order->setFieldNoDemand('DEDUCTED', $fields['DEDUCTED']);
705 }
706 else
707 {
708 $result->addErrors($r->getErrors());
709 }
710 }
711 }
712
713 return $result;
714 }
724 public function fillPaymentCollectionFromRequest(array $fields)
725 {
727 if (!$order = $this->getOrder())
728 {
729 throw new Main\ObjectNotFoundException('Entity "Order" not found');
730 }
731
733 if (!$paymentCollection = $order->getPaymentCollection())
734 {
735 throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
736 }
737
738 $result = new Sale\Result();
739 $sum = (float)($fields['PRICE'] ?? null);
740
741 if (isset($fields['SUM_PAID'])
742 && floatval($fields['SUM_PAID']) >= floatval($fields['PRICE']))
743 {
744 $sum = floatval($fields['SUM_PAID']);
745 }
746
747 $isPayFromUserBudget = null;
748 $backToUserBudget = null;
749
750 if (array_key_exists('ONLY_FULL_PAY_FROM_ACCOUNT', $fields))
751 {
752 $isPayFromUserBudget = $fields['ONLY_FULL_PAY_FROM_ACCOUNT'];
753 }
754
755 if ($isPayFromUserBudget === null && array_key_exists('PAY_CURRENT_ACCOUNT', $fields) && $fields['PAY_CURRENT_ACCOUNT'] !== null)
756 {
757 $isPayFromUserBudget = ($fields['PAY_CURRENT_ACCOUNT'] != "Y");
758 }
759
760 if (array_key_exists('PAY_FROM_ACCOUNT_BACK', $fields))
761 {
762 $backToUserBudget = ($fields['PAY_FROM_ACCOUNT_BACK'] == "Y");
763 }
764
765 $paySystemId = null;
766 $paySystemName = null;
767
768 $userId = $order->getUserId();
769 $currency = $order->getCurrency();
770
771 $rawFields = array();
772 $paymentInner = null;
773 $paymentOuter = null;
774 $countPayments = count($paymentCollection);
775
776 $orderPaid = false;
777
778 if ((($countPayments == 0 && $order->getId() == 0)
779 || ($countPayments == 2 && $paymentCollection->isExistsInnerPayment())
780 || ($countPayments == 1 && !$paymentCollection->isExistsInnerPayment())))
781 {
782
783 $needSum = $order->getPrice() - $order->getSumPaid();
784
785 if ($countPayments <= 1)
786 {
787
788 if ($order->getId() == 0)
789 {
790 if (!isset($fields["PAY_SYSTEM_ID"]))
791 $fields["PAY_SYSTEM_ID"] = static::getDefaultPaySystemId($order->getPersonTypeId());
792
793
795 if ($service = Sale\PaySystem\Manager::getObjectById($fields["PAY_SYSTEM_ID"]))
796 {
798 $paymentOuter = $paymentCollection->createItem($service);
799 $paymentOuter->setField('DATE_BILL', new Main\Type\DateTime());
800 $paymentOuter->setField('SUM', $needSum);
801 $paymentOuter->setField('PAY_SYSTEM_NAME', $service->getField('NAME'));
802 $order->setFieldNoDemand('PAY_SYSTEM_ID', $fields["PAY_SYSTEM_ID"]);
803 $countPayments = 1;
804 }
805 }
806 else
807 {
808 $paymentOuter = null;
809
811 foreach ($paymentCollection as $payment)
812 {
813 if ($payment->isInner())
814 continue;
815
816 $paymentOuter = $payment;
817 }
818
819 if (isset($fields["PAY_SYSTEM_ID"])
820 && $paymentOuter !== null
821 && ($paymentOuter->getPaymentSystemId() != (int)$fields["PAY_SYSTEM_ID"])
822 )
823 {
825 if ($service = Sale\PaySystem\Manager::getObjectById($fields["PAY_SYSTEM_ID"]))
826 {
828 $paymentOuter->setField('PAY_SYSTEM_NAME', $service->getField('NAME'));
829 $paymentOuter->setField('PAY_SYSTEM_ID', intval($fields["PAY_SYSTEM_ID"]));
830 $order->setFieldNoDemand('PAY_SYSTEM_ID', intval($fields["PAY_SYSTEM_ID"]));
831 }
832 }
833 }
834 }
835
836 if (isset($fields['PAYED']))
837 {
838 $paidFlag = null;
839
840
841 if ($countPayments > 0)
842 {
844 foreach($paymentCollection as $payment)
845 {
846 if ($paidFlag === null && $payment->isPaid() && $needSum == 0)
847 {
848 $paidFlag = 'Y';
849 }
850
851 if ($payment->isInner())
852 continue;
853
854 $paymentOuter = $payment;
855 }
856 }
857
858 if ($paidFlag === null)
859 {
860 $paidFlag = 'N';
861 }
862
863
864 if ($paidFlag != $fields['PAYED'])
865 {
866 if ($fields['PAYED'] == "Y")
867 {
868 $pay = true;
869 $orderPaid = true;
870
871 if ($isPayFromUserBudget !== null)
872 {
873 if (static::canPayWithUserBudget($needSum, $userId, $currency, $isPayFromUserBudget))
874 {
875 $userBudget = Sale\Internals\UserBudgetPool::getUserBudget($userId, $currency);
876
877 if ($userBudget >= $needSum)
878 {
879 $pay = false;
880 }
881 }
882 }
883
885 $r = static::payFromBudget($order, $pay, $isPayFromUserBudget);
886 if ($r->isSuccess())
887 {
888 $needSum = $order->getPrice() - $order->getSumPaid();
889
890 if (!$pay)
891 {
893 $r = $paymentOuter->setField('SUM', $needSum);
894 }
895
896 if (!$r->isSuccess())
897 {
898 $result->addErrors($r->getErrors());
899 }
900 }
901 else
902 {
903 $result->addErrors($r->getErrors());
904 }
905 }
906 else
907 {
908 //
910 foreach($paymentCollection as $payment)
911 {
912 if ($payment->isPaid())
913 {
914 if ($backToUserBudget && $payment->isInner())
915 {
916 $payment->setReturn('Y');
917 }
918 else
919 {
920 $payment->setPaid('N');
921 }
922 }
923
924 if ($payment->isInner())
925 {
926 $payment->delete();
927 }
928 else
929 {
930 $payment->setField('SUM', $order->getPrice());
931 }
932 }
933
934
935 }
936
937 unset($fields['PAYED']);
938 }
939 elseif ($order->getId() == 0)
940 {
941 if ($isPayFromUserBudget !== null)
942 {
943 if (static::canPayWithUserBudget($needSum, $userId, $currency, $isPayFromUserBudget))
944 {
945 $userBudget = Sale\Internals\UserBudgetPool::getUserBudget($userId, $currency);
946
947 $setSum = $userBudget;
948
950 $r = static::payFromBudget($order, false);
951 if ($r->isSuccess())
952 {
953 $sum -= $setSum;
954 }
955 else
956 {
957 $result->addErrors($r->getErrors());
958 }
959 }
960 }
961 }
962
963 if ($order->getId() > 0)
964 {
965 $payment = null;
966
968 foreach($paymentCollection as $paymentItem)
969 {
970 if ($paymentItem->isInner())
971 {
972 $paymentInner = $paymentItem;
973 if ($payment === null && $paymentItem->isPaid())
974 {
975 $payment = $paymentItem;
976 }
977 }
978 else
979 {
980 $paymentOuter = $paymentItem;
981 if ($payment === null && $paymentItem->isPaid())
982 {
983 $payment = $paymentItem;
984 }
985 }
986 }
987
988 if ($payment === null)
989 {
990 if ($paymentOuter !== null)
991 $payment = $paymentOuter;
992 else
993 $payment = $paymentInner;
994 }
995
996 if ($payment === null)
997 {
998 return $result;
999 }
1000
1001 $paymentFields = static::convertDateFields($fields, static::getPaymentDateFields());
1002
1003
1004 if (!empty($paymentFields['PAY_SYSTEM_ID']) && $paymentFields['PAY_SYSTEM_ID'] != $payment->getPaymentSystemId())
1005 {
1006 if ($payment->isInner())
1007 {
1008 unset($paymentFields['PAY_SYSTEM_ID']);
1009 }
1010 else
1011 {
1012 $paySystemId = (int)$paymentFields['PAY_SYSTEM_ID'];
1013
1015 if ($paysystem = Sale\PaySystem\Manager::getObjectById($paySystemId))
1016 {
1017 $paymentFields['PAY_SYSTEM_NAME'] = $paysystem->getField('NAME');
1018 }
1019 }
1020 }
1021
1022
1023 $paymentFields = static::replaceFields($paymentFields, static::getPaymentReplaceFields());
1024 $paymentFields = static::clearFields($paymentFields, static::getPaymentAvailableFields());
1025
1027 $r = $payment->setFields($paymentFields);
1028 if ($r->isSuccess())
1029 {
1030
1031 static::fillOrderFieldsFromEntity($order, $payment, $fields, static::getPaymentFieldsToConvert());
1032 }
1033 else
1034 {
1035 $result->addErrors($r->getErrors());
1036 }
1037
1038
1039 if ($result->isSuccess() && intval($paySystemId) > 0)
1040 {
1041 $order->setFieldNoDemand('PAY_SYSTEM_ID', $paySystemId);
1042 }
1043 }
1044 }
1045
1046 $paymentOuter = null;
1047 $calcSum = 0;
1049 foreach($paymentCollection as $payment)
1050 {
1051 $calcSum += $payment->getSum();
1052 if ($payment->isInner())
1053 continue;
1054
1055 $paymentOuter = $payment;
1056 }
1057
1058 if ($paymentOuter && !$paymentOuter->isPaid())
1059 {
1060 if ($order->getPrice() != $calcSum)
1061 {
1062 $paymentOuter->setField('SUM', $paymentOuter->getSum() + ($order->getPrice() - $calcSum));
1063 }
1064 }
1065
1066 if (!$paymentOuter)
1067 return $result;
1068
1069 $fieldsFromOrder = array(
1070 'PS_STATUS', 'PS_STATUS_CODE', 'PS_STATUS_DESCRIPTION',
1071 'PS_STATUS_MESSAGE', 'PS_SUM', 'PS_CURRENCY', 'PS_RESPONSE_DATE',
1072 'PAY_VOUCHER_NUM', 'PAY_VOUCHER_DATE', 'DATE_PAY_BEFORE',
1073 'DATE_BILL', 'PAY_SYSTEM_NAME', 'PAY_SYSTEM_ID',
1074 'DATE_PAYED', 'EMP_PAYED_ID', 'CURRENCY'
1075 );
1076
1077 foreach ($fieldsFromOrder as $fieldName)
1078 {
1079 if (isset($fields[$fieldName]))
1080 {
1081 switch ($fieldName)
1082 {
1083 case 'DATE_BILL':
1084 case 'DATE_PAY_BEFORE':
1085 case 'PS_RESPONSE_DATE':
1086 if (!isset($fields[$fieldName]) || strval($fields[$fieldName]) == '')
1087 continue 2;
1088 $value = new Main\Type\DateTime($fields[$fieldName]);
1089 break;
1090 case 'PAY_VOUCHER_DATE':
1091 if (!isset($fields[$fieldName]) || strval($fields[$fieldName]) == '')
1092 continue 2;
1093 $value = new Main\Type\Date($fields[$fieldName]);
1094 break;
1095 case 'DATE_PAYED':
1096 if (!isset($fields[$fieldName]) || strval($fields[$fieldName]) == '')
1097 continue 2;
1098 $fieldName = 'DATE_PAID';
1099 $value = new Main\Type\DateTime($fields['DATE_PAYED']);
1100 break;
1101 case 'EMP_PAYED_ID':
1102 $fieldName = 'EMP_PAID_ID';
1103 $value = $fields['EMP_PAYED_ID'];
1104 break;
1105 default:
1106 $value = $fields[$fieldName];
1107 break;
1108 }
1109 $paymentOuter->setFieldNoDemand($fieldName, $value);
1110 if ($fieldName === 'PAY_SYSTEM_ID')
1111 {
1112 $order->setFieldNoDemand('PAY_SYSTEM_ID', $value);
1113
1115 if ($paysystem = Sale\PaySystem\Manager::getObjectById($value))
1116 $paymentOuter->setFieldNoDemand('PAY_SYSTEM_NAME', $paysystem->getField('NAME'));
1117 }
1118
1119 if ($fieldName == "DATE_PAID")
1120 $fieldName = 'DATE_PAYED';
1121
1122 if (in_array($fieldName, $this->getAvailableFields()))
1123 {
1124 $order->setFieldNoDemand($fieldName, $value);
1125 };
1126 }
1127 elseif (isset($fields['~'.$fieldName]))
1128 {
1129 $rawFields['~'.$fieldName] = $fields['~'.$fieldName];
1130 }
1131 }
1132
1133 if (isset($fields['PAY_SYSTEM_PRICE']))
1134 $paymentOuter->setField('PRICE_COD', $fields['PAY_SYSTEM_PRICE']);
1135
1136 if (!empty($rawFields))
1137 {
1138 $this->parseRawFields(static::ENTITY_PAYMENT, $rawFields);
1139 }
1140 }
1141
1142
1143 if (array_key_exists('SUM_PAID', $fields))
1144 {
1145 if ($orderPaid)
1146 {
1147 if ($fields['SUM_PAID'] == 0)
1148 {
1149 $fields['SUM_PAID'] = $order->getPrice();
1150 }
1151 }
1152
1153 if ($fields['SUM_PAID'] >= 0)
1154 {
1155 $oldSumPaid = $order->getSumPaid();
1156
1157 $deltaSumPaid = floatval($fields['SUM_PAID']) - $oldSumPaid;
1158
1159 if ($deltaSumPaid > 0)
1160 {
1161 $paidPayment = false;
1162
1164 foreach ($paymentCollection as $payment)
1165 {
1166 if ($payment->isPaid() || $payment->isInner())
1167 continue;
1168
1169 if (Sale\PriceMaths::roundPrecision($payment->getSum()) === Sale\PriceMaths::roundPrecision($deltaSumPaid))
1170 {
1171 $paidPayment = true;
1173 $r = $payment->setPaid("Y");
1174 if (!$r->isSuccess())
1175 {
1176 $result->addErrors($r->getErrors());
1177 }
1178 break;
1179 }
1180 }
1181
1182 if (!$paidPayment)
1183 {
1184 $service = null;
1185 $paymentSystemId = null;
1186
1187 if (count($paymentCollection) > 0)
1188 {
1190 if ($firstPayment = $paymentCollection->rewind())
1191 {
1192 $paymentSystemId = $firstPayment->getPaymentSystemId();
1193 if ($paymentSystemId > 0)
1194 {
1195 $service = Sale\PaySystem\Manager::getObjectById($paymentSystemId);
1196 }
1197 }
1198 }
1199
1200 if (!$service)
1201 {
1202 $paymentSystemId = static::getDefaultPaySystemId($order->getPersonTypeId());
1203 $service = Sale\PaySystem\Manager::getObjectById($paymentSystemId);
1204 }
1205
1207 if ($service)
1208 {
1210 $payment = $paymentCollection->createItem($service);
1211 $payment->setField('DATE_BILL', new Main\Type\DateTime());
1212 $payment->setField('SUM', $deltaSumPaid);
1213 $payment->setField('PAY_SYSTEM_NAME', $service->getField('NAME'));
1214 $order->setFieldNoDemand('PAY_SYSTEM_ID', $paymentSystemId);
1215
1217 $r = $payment->setPaid("Y");
1218 if (!$r->isSuccess())
1219 {
1220 $result->addErrors($r->getErrors());
1221 }
1222 }
1223 }
1224 }
1225 }
1226 }
1227
1228 return $result;
1229 }
1230
1231 private static function getDefaultPaySystemId($personTypeId)
1232 {
1233 $personTypeId = intval($personTypeId);
1234
1235 static $defaultPaySystemId = array();
1236 if (isset($defaultPaySystemId[$personTypeId]))
1237 return $defaultPaySystemId[$personTypeId];
1238
1239 $defaultPaySystemId[$personTypeId] = intval(Main\Config\Option::get('sale', '1C_IMPORT_DEFAULT_PS', 0));
1240 if (isset($defaultPaySystemId[$personTypeId]) && ($defaultPaySystemId[$personTypeId] > 0))
1241 return $defaultPaySystemId[$personTypeId];
1242
1243 if ($personTypeId > 0)
1244 {
1245 $dbPaySystem = Sale\PaySystem\Manager::getList(
1246 array(
1247 'select' => array("ID"),
1248 'filter' => array(
1249 '=ACTIVE' => 'Y',
1250 '=PERSON_TYPE_ID' => $personTypeId,
1251 '=ENTITY_REGISTRY_TYPE' => static::getRegistryType()
1252 ),
1253 'order' => array('SORT'),
1254 'limit' => 1
1255 )
1256 );
1257 if ($paySystem = $dbPaySystem->fetch())
1258 $defaultPaySystemId[$personTypeId] = intval($paySystem['ID']);
1259
1260 if (isset($defaultPaySystemId[$personTypeId]) && ($defaultPaySystemId[$personTypeId] > 0))
1261 return $defaultPaySystemId[$personTypeId];
1262 }
1263
1264 $dbPaySystem = Sale\PaySystem\Manager::getList(
1265 array(
1266 'select' => array("ID"),
1267 'filter' => array(
1268 '=ACTIVE' => 'Y',
1269 '=ENTITY_REGISTRY_TYPE' => static::getRegistryType()
1270 ),
1271 'order' => array('SORT'),
1272 'limit' => 1
1273 )
1274 );
1275 if ($paySystem = $dbPaySystem->fetch())
1276 $defaultPaySystemId[$personTypeId] = intval($paySystem['ID']);
1277
1278 return $defaultPaySystemId[$personTypeId];
1279 }
1280
1288 public function fillTaxFromRequest(Sale\Tax $tax, array $fields)
1289 {
1290 if (!empty($fields['COUNT_DELIVERY_TAX']))
1291 {
1292 $tax->setDeliveryCalculate(($fields['COUNT_DELIVERY_TAX'] == "Y"));
1293 }
1294
1295 if (!empty($fields['TAX_LIST']) && is_array($fields['TAX_LIST']))
1296 {
1297 $tax->initTaxList($fields['TAX_LIST']);
1298 }
1299 elseif (!empty($tax->getTaxList()))
1300 {
1302 if ($order = $this->getOrder())
1303 {
1304 $order->refreshVat();
1305 if ($tax = $order->getTax())
1306 {
1307 $tax->resetTaxList();
1308 }
1309 }
1310 }
1311
1312 if (array_key_exists('TAX_VALUE', $fields))
1313 {
1314 $order = $this->getOrder();
1315 $order->setFieldNoDemand('TAX_VALUE', floatval($fields['TAX_VALUE']));
1316 }
1317
1318 return new Sale\Result();
1319 }
1320
1330 public static function shipment($id, $value, array $storeData = array() )
1331 {
1332 global $USER;
1333
1334 $result = new Sale\Result();
1335
1336 $registry = Sale\Registry::getInstance(static::getRegistryType());
1337
1339 $orderClassName = $registry->getOrderClassName();
1340 if ($order = $orderClassName::load($id))
1341 {
1343 if (!$basket = $order->getBasket())
1344 {
1345 throw new Main\ObjectNotFoundException('Entity "Basket" not found');
1346 }
1347
1349 if(!$shipmentCollection = $order->getShipmentCollection())
1350 {
1351 throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1352 }
1353
1355 foreach ($shipmentCollection as $shipment)
1356 {
1357 if ($shipment->isSystem())
1358 continue;
1359
1361 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
1362 {
1363 throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
1364 }
1365
1367 $r = static::fillShipmentItemCollectionFromRequest($shipmentItemCollection, $storeData, $basket);
1368 if (!$r->isSuccess())
1369 {
1370 $result->addErrors($r->getErrors());
1371 return $result;
1372 }
1373
1375 $r = $shipment->setField('DEDUCTED', $value === true ? 'Y' : 'N');
1376 if (!$r->isSuccess())
1377 {
1378 $result->addErrors($r->getErrors());
1379 continue;
1380 }
1381 }
1382 }
1383
1384
1386 $r = $order->save();
1387 if (!$r->isSuccess())
1388 {
1389 $result->addErrors($r->getErrors());
1390 }
1391
1392 return $result;
1393 }
1394
1405 public static function fillShipmentItemCollectionFromRequest(Sale\ShipmentItemCollection $shipmentItemCollection, array $storeData, Sale\Basket $basket = null)
1406 {
1407 $result = new Sale\Result();
1408
1409
1410 if ($basket === null)
1411 {
1412
1414 if (!$shipment = $shipmentItemCollection->getShipment())
1415 {
1416 throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
1417 }
1418
1419
1421 if(!$shipmentCollection = $shipment->getCollection())
1422 {
1423 throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1424 }
1425
1427 if (!$order = $shipmentCollection->getOrder())
1428 {
1429 throw new Main\ObjectNotFoundException('Entity "Order" not found');
1430 }
1431
1433 if (!$basket = $order->getBasket())
1434 {
1435 throw new Main\ObjectNotFoundException('Entity "Basket" not found');
1436 }
1437 }
1438
1440 foreach ($shipmentItemCollection as $shipmentItem)
1441 {
1443 if (($basketItem = $shipmentItem->getBasketItem()) && $basketItem->getId() == 0)
1444 {
1445 continue;
1446 }
1448 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
1449 if (
1450 !$shipmentItemStoreCollection
1451 && $basketItem->isReservableItem()
1452 )
1453 {
1454 throw new Main\ObjectNotFoundException('Entity "ShipmentItemStoreCollection" not found');
1455 }
1456
1457 $barcodeList = array();
1459 foreach ($shipmentItemStoreCollection as $shipmentItemStore)
1460 {
1461 $storeId = $shipmentItemStore->getField('STORE_ID');
1462 $basketId = $shipmentItemStore->getField('BASKET_ID');
1463 $barcodeList[$basketId][$storeId][] = array(
1464 'ID' => $shipmentItemStore->getId(),
1465 'QUANTITY' => $shipmentItemStore->getQuantity(),
1466 'BARCODE' => $shipmentItemStore->getBarcode(),
1467 );
1468 }
1469
1470 $baseBarcode = null;
1471
1472 foreach($storeData as $basketId => $barcodeDataList)
1473 {
1474 if ((intval($basketId) != $basketId)
1475 || ($basketItem->getId() != $basketId))
1476 continue;
1477
1478 foreach ($barcodeDataList as $barcodeData)
1479 {
1480
1482 if (!$basketItem = $basket->getItemById($basketId))
1483 {
1484 throw new Main\ObjectNotFoundException('Entity "BasketItem" not found');
1485 }
1486
1487
1488 $saveBarcodeList = array();
1489
1490 if ($basketItem->isBarcodeMulti() && is_array($barcodeData['BARCODE']))
1491 {
1492 $barcodeQuantity = $barcodeData['QUANTITY'] / count($barcodeData['BARCODE']);
1493
1494 foreach ($barcodeData['BARCODE'] as $barcodeId => $barcodeValue)
1495 {
1496 $barcodeFields = array(
1497 'QUANTITY' => $barcodeQuantity,
1498 'BARCODE' => $barcodeValue,
1499 );
1500
1501 if (intval($barcodeId) > 0)
1502 {
1503 $barcodeFields['ID'] = intval($barcodeId);
1504 }
1505
1506 $saveBarcodeList[] = $barcodeFields;
1507 }
1508 }
1509 else
1510 {
1511 if (strval($barcodeData['BARCODE']) != '')
1512 {
1513 $baseBarcode = trim($barcodeData['BARCODE']);
1514 }
1515 elseif (!empty($baseBarcode))
1516 {
1517 $barcodeData['BARCODE'] = $baseBarcode;
1518 }
1519
1520 $barcodeFields = array(
1521 'QUANTITY' => $barcodeData['QUANTITY'],
1522 'BARCODE' => $barcodeData['BARCODE'],
1523 );
1524
1525 if (!empty($barcodeList[$basketId]) && !empty($barcodeList[$basketId][$barcodeData['STORE_ID']]))
1526 {
1527
1528 foreach ($barcodeList[$basketId][$barcodeData['STORE_ID']] as $existBarcodeData)
1529 {
1530 if ($existBarcodeData['BARCODE'] == $barcodeData['BARCODE'] && !empty($existBarcodeData['ID']))
1531 {
1532 if ($shipmentItemStoreCollection->getItemById($existBarcodeData['ID']))
1533 {
1534 $barcodeFields['ID'] = $existBarcodeData['ID'];
1535 }
1536 }
1537 }
1538 }
1539
1540 $saveBarcodeList = array(
1541 $barcodeFields
1542 );
1543 }
1544
1545 foreach ($saveBarcodeList as $saveBarcodeData)
1546 {
1547 $barcodeFields = array(
1548 'QUANTITY' => $saveBarcodeData['QUANTITY'],
1549 'BARCODE' => $saveBarcodeData['BARCODE'],
1550 );
1551
1553 $shipmentItemStore = $shipmentItemStoreCollection->getItemByBarcode($saveBarcodeData['BARCODE']);
1554
1555 if (!$shipmentItemStore)
1556 {
1557 $barcodeFields['STORE_ID'] = intval($barcodeData['STORE_ID']);
1558 $shipmentItemStore = $shipmentItemStoreCollection->createItem($basketItem);
1559 }
1560
1562 $r = $shipmentItemStore->setFields($barcodeFields);
1563 if (!$r->isSuccess())
1564 {
1565 $result->addErrors($r->getErrors());
1566 }
1567 }
1568 }
1569 }
1570 }
1571
1572 return $result;
1573 }
1574
1583 public static function allowDelivery($id, $value)
1584 {
1585 $result = new Sale\Result();
1586
1587 $registry = Sale\Registry::getInstance(static::getRegistryType());
1588
1590 $orderClassName = $registry->getOrderClassName();
1591 if ($order = $orderClassName::load($id))
1592 {
1594 if(!$shipmentCollection = $order->getShipmentCollection())
1595 {
1596 throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1597 }
1598
1600 foreach ($shipmentCollection as $shipment)
1601 {
1602 if ($shipment->isSystem())
1603 continue;
1604
1606 $r = $shipment->setField('ALLOW_DELIVERY', $value === true ? 'Y' : 'N');
1607 if (!$r->isSuccess())
1608 {
1609 $result->addErrors($r->getErrors());
1610 return $result;
1611 }
1612 }
1613 }
1614
1615
1617 $r = $order->save();
1618 if (!$r->isSuccess())
1619 {
1620 $result->addErrors($r->getErrors());
1621 }
1622
1623 return $result;
1624 }
1625
1631 public static function add(array $fields)
1632 {
1633 return static::modifyOrder(static::ORDER_COMPAT_ACTION_ADD, $fields);
1634 }
1635
1643 public static function update($id, array $fields, $dateUpdate = false)
1644 {
1645 $result = new Sale\Result();
1646
1647 $id = (int)$id;
1648
1649 if ($id <= 0)
1650 {
1651 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_ID_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_ID_NOT_FOUND'));
1652 return $result;
1653 }
1654
1655 $fields['ID'] = $id;
1656
1657 if (!$dateUpdate)
1658 {
1659 $fields['DATE_UPDATE'] = null;
1660 }
1661
1663 return static::modifyOrder(static::ORDER_COMPAT_ACTION_UPDATE, $fields);
1664 }
1665
1675 public static function modifyOrder($action, array $fields)
1676 {
1677 $result = new Sale\Result();
1678
1679 try
1680 {
1681 $adminSection = (defined('ADMIN_SECTION') && ADMIN_SECTION === true);
1682
1684 $orderCompatibility = static::create($fields);
1685
1687 $order = $orderCompatibility->getOrder();
1688
1690 $propCollection = $order->getPropertyCollection();
1691
1692 if (!empty($fields['ORDER_PROP']) && is_array($fields['ORDER_PROP']))
1693 {
1694 $fields['PROPERTIES'] = $fields['ORDER_PROP'];
1695 }
1696
1697 if (!isset($fields['PROPERTIES']) || !is_array($fields['PROPERTIES']))
1698 {
1699 $fields['PROPERTIES'] = array();
1700 }
1701
1702 // compatibility to prevent setting default values for empty properties
1704 foreach ($propCollection as $propertyValue)
1705 {
1706 $propertyFields = $propertyValue->getProperty();
1707 $key = isset($propertyFields['ID']) ? $propertyFields['ID'] : 'n'.$propertyValue->getId();
1708
1709 if ($propertyValue->getId() <=0
1710 && !array_key_exists($key, $fields['PROPERTIES'])
1711 )
1712 {
1713 $propertyValue->delete();
1714 }
1715 }
1716
1718 $r = $propCollection->setValuesFromPost($fields, $_FILES);
1719 if (!$r->isSuccess())
1720 {
1721 $result->addErrors($r->getErrors());
1722 return $result;
1723 }
1724
1725
1726 $oldPrice = $order->getPrice();
1727
1728// $isStartField = $order->isStartField();
1729
1730
1732 $basket = $order->getBasket();
1733
1734 if (!$basket && $action == static::ORDER_COMPAT_ACTION_SAVE)
1735 {
1736 $fUserId = null;
1737
1738
1739 if (!empty($fields['BASKET_ITEMS']) && is_array($fields['BASKET_ITEMS']))
1740 {
1741 foreach ($fields['BASKET_ITEMS'] as $basketItemData)
1742 {
1743 if (!empty($basketItemData['FUSER_ID']) && intval($basketItemData['FUSER_ID']) > 0)
1744 {
1745 $fUserId = intval($basketItemData['FUSER_ID']);
1746 break;
1747 }
1748 }
1749 }
1750
1751
1752 if (intval($fUserId) <= 0 && !$adminSection)
1753 {
1754 $fUserId = static::getDefaultFuserId();
1755 }
1756
1757 $userId = $order->getUserId();
1758 if ($userId > 0)
1759 {
1760 $fUserIdByUserId = Sale\Fuser::getIdByUserId($userId);
1761 if (intval($fUserId) > 0 && intval($fUserIdByUserId) > 0
1762 && intval($fUserId) != intval($fUserIdByUserId))
1763 {
1764 // TODO: ... [SALE_BASKET_001] - the call of old method of the basket
1765 \CSaleBasket::TransferBasket($fUserId, $fUserIdByUserId);
1766 }
1767
1768 $fUserId = $fUserIdByUserId;
1769 }
1770
1771 if (intval($fUserId) <= 0)
1772 {
1773 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_FUSERID_NOT_FOUND'), "SALE_COMPATIBLE_ORDER_FUSERID_NOT_FOUND"));
1774 return $result;
1775 }
1776
1777
1778 $registry = Sale\Registry::getInstance(static::getRegistryType());
1780 $basketClassName = $registry->getBasketClassName();
1781
1782 if (!$adminSection)
1783 {
1784 $siteId = !empty($fields["SITE_ID"]) ? $fields["SITE_ID"] : (!empty($fields["LID"]) ? $fields['LID']: null);
1785 $allBasket = $basketClassName::loadItemsForFUser($fUserId, $siteId);
1786
1787 if ($allBasket)
1788 {
1789 $basket = $allBasket->getOrderableItems();
1790 }
1791 }
1792
1793
1794 if (!$basket)
1795 {
1796 $basket = $basketClassName::create($order->getSiteId());
1797 $basket->setFUserId($fUserId);
1798 }
1799 }
1800
1801 $isStartField = $order->isStartField(true);
1802
1803
1804 if ($basket)
1805 {
1807 $basketCompatibilityClassName = static::getBasketCompatibilityClassName();
1808 $basketCompatibility = $basketCompatibilityClassName::create($orderCompatibility);
1809
1811 $r = $basketCompatibility->fillBasket($basket, $fields);
1812 if (!$r->isSuccess())
1813 {
1814 $result->addErrors($r->getErrors());
1815 return $result;
1816 }
1817
1818 if ($action == static::ORDER_COMPAT_ACTION_SAVE && $order->getId() == 0 && count($basket) > 0)
1819 {
1820 $order->setMathActionOnly(true);
1821 $order->setBasket($basket);
1822 $order->setMathActionOnly(false);
1823 }
1824
1825 if ($orderCompatibility->isExistPrice() && $oldPrice == $order->getPrice())
1826 {
1827 $order->setFieldNoDemand('PRICE', $orderCompatibility->externalPrice);
1828 }
1829
1830 }
1831
1833 $r = $orderCompatibility->fillTaxFromRequest($order->getTax(), $fields);
1834 if (!$r->isSuccess())
1835 {
1836 $result->addErrors($r->getErrors());
1837 return $result;
1838 }
1839
1841 $r = $orderCompatibility->fillShipmentCollectionFromRequest( $order->getShipmentCollection(), $fields);
1842 if (!$r->isSuccess())
1843 {
1844 $result->addErrors($r->getErrors());
1845 return $result;
1846 }
1847
1848 if ($isStartField)
1849 {
1850 $hasMeaningfulFields = $order->hasMeaningfulField();
1851
1853 $r = $order->doFinalAction($hasMeaningfulFields);
1854 if (!$r->isSuccess())
1855 {
1856 $result->addErrors($r->getErrors());
1857 return $result;
1858 }
1859 }
1860
1861 $order->setMathActionOnly(false);
1862
1864 $r = $orderCompatibility->fillPaymentCollectionFromRequest($fields);
1865 if (!$r->isSuccess())
1866 {
1867 $result->addErrors($r->getErrors());
1868 return $result;
1869 }
1870
1872 $r = static::fillOrderFromRequest($order, $fields);
1873 if (!$r->isSuccess())
1874 {
1875 $result->addErrors($r->getErrors());
1876 return $result;
1877 }
1878
1879 }
1880 catch(Sale\UserMessageException $e)
1881 {
1882 $result->addError(new Sale\ResultError($e->getMessage(), $e->getCode()));
1883 return $result;
1884 }
1885
1886 static::transformationLocation($order);
1887
1889 $r = $order->save();
1890 if ($r->isSuccess())
1891 {
1892 if ($orderData = $r->getData())
1893 $result->setData($orderData);
1894
1895 if ($orderId = $r->getId())
1896 $result->setId($orderId);
1897
1899 $r = $orderCompatibility->saveRawFields($order, static::ENTITY_ORDER);
1900 }
1901
1902 if (!$r->isSuccess())
1903 {
1904 $result->addErrors($r->getErrors());
1905 }
1906 else
1907 {
1908 $oldFields = static::convertDateFieldsToOldFormat($order->getFieldValues());
1909 $oldFields = $oldFields + $orderCompatibility->rawFields;
1910
1912 if ($paymentCollection = $order->getPaymentCollection())
1913 {
1915 foreach ($paymentCollection as $payment)
1916 {
1917 if ($payment->getId() <= 0)
1918 {
1919 continue;
1920 }
1921
1923 $r = $orderCompatibility->saveRawFields($payment, static::ENTITY_PAYMENT);
1924 if (!$r->isSuccess())
1925 {
1926 $result->addErrors($r->getErrors());
1927 }
1928 }
1929 }
1930
1931 $result->setData(array(
1932 'OLD_FIELDS' => $oldFields
1933 ));
1934 }
1935
1936 return $result;
1937 }
1938
1946 public static function reserve($orderId, $value)
1947 {
1948 $result = new Sale\Result();
1949
1950 $registry = Sale\Registry::getInstance(static::getRegistryType());
1951
1953 $orderClassName = $registry->getOrderClassName();
1954 if (!$order = $orderClassName::load($orderId))
1955 {
1956 $result->addError( new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_NOT_FOUND') );
1957 return $result;
1958 }
1959
1961 if (!$shipmentCollection = $order->getShipmentCollection())
1962 {
1963 throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1964 }
1965
1967 foreach ($shipmentCollection as $shipment)
1968 {
1969 if ($shipment->isSystem())
1970 continue;
1971
1972 if ($value == "Y")
1973 {
1975 $r = $shipment->tryReserve();
1976 if (!$r->isSuccess())
1977 {
1978 $registry = Sale\Registry::getInstance(static::getRegistryType());
1979
1981 $entityMarkerClassName = $registry->getEntityMarkerClassName();
1982 $entityMarkerClassName::addMarker($order, $shipment, $r);
1983 if (!$shipment->isSystem())
1984 {
1985 $shipment->setField('MARKED', 'Y');
1986 }
1987
1988 $result->addErrors($r->getErrors());
1989 }
1990 }
1991 else
1992 {
1993 if (!$shipment->isShipped())
1994 {
1996 $r = $shipment->tryUnreserve();
1997 if (!$r->isSuccess())
1998 {
1999 $registry = Sale\Registry::getInstance(static::getRegistryType());
2000
2002 $entityMarkerClassName = $registry->getEntityMarkerClassName();
2003 $entityMarkerClassName::addMarker($order, $shipment, $r);
2004 if (!$shipment->isSystem())
2005 {
2006 $shipment->setField('MARKED', 'Y');
2007 }
2008 $result->addErrors($r->getErrors());
2009 }
2010 }
2011 }
2012 }
2013
2015 $r = $order->save();
2016 if (!$r->isSuccess())
2017 {
2018 $result->addErrors($r->getErrors());
2019 }
2020
2021 return $result;
2022 }
2023
2033 public static function pay($orderId, array $values, $withdraw = false, $pay = false)
2034 {
2035 $result = new Sale\Result();
2036
2037 $paid = null;
2038 if (isset($values['PAYED']) && strval($values['PAYED']) != '')
2039 {
2040 $values['PAID'] = $values['PAYED'];
2041 }
2042
2043 if (intval($orderId) <= 0)
2044 {
2045 $result->addError( new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_ID_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_ID_NOT_FOUND') );
2046 return $result;
2047 }
2048
2049 $registry = Sale\Registry::getInstance(static::getRegistryType());
2051 $orderClassName = $registry->getOrderClassName();
2052 if (!$order = $orderClassName::load($orderId))
2053 {
2054 $result->addError( new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_NOT_FOUND') );
2055 return $result;
2056 }
2057
2058 if ($order->isCanceled())
2059 {
2061 $r = $order->setField('CANCELED', 'N');
2062 if (!$r->isSuccess())
2063 {
2064 $result->addErrors($r->getErrors());
2065 return $result;
2066 }
2067 }
2068
2070 if (!$paymentCollection = $order->getPaymentCollection())
2071 {
2072 throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
2073 }
2074
2075 $paidFormUserBudget = false;
2076
2077 if ($withdraw)
2078 {
2080 $r = static::payFromBudget($order, $pay);
2081 if (!$r->isSuccess())
2082 {
2083 $result->addErrors($r->getErrors());
2084 }
2085 else
2086 {
2087 $payBudgetData = $r->getData();
2088 if (array_key_exists('PAID_FROM_BUDGET', $payBudgetData))
2089 {
2090 $paidFormUserBudget = $payBudgetData['PAID_FROM_BUDGET'];
2091 }
2092 }
2093
2094 }
2095
2096
2097 if (!$paidFormUserBudget)
2098 {
2100 foreach ($paymentCollection as $payment)
2101 {
2102 if (empty($fields))
2103 {
2104 if (isset($values['=DATE_PAYED']))
2105 {
2106 $values['DATE_PAID'] = $values['=DATE_PAYED'];
2107 unset($values['=DATE_PAYED']);
2108 }
2109
2110 $values = static::convertDateFields($values, static::getPaymentDateFields());
2111 $fields = static::clearFields($values, $payment->getAvailableFields());
2112
2113 }
2114
2115 if ($values['PAID'] == "N" && !$payment->isPaid())
2116 continue;
2117
2118 if ($withdraw && $values['PAID'] == "N" && $payment->isInner())
2119 {
2121 $r = $payment->setReturn('Y');
2122 if (!$r->isSuccess())
2123 {
2124 $result->addErrors($r->getErrors());
2125 }
2126 }
2127 else
2128 {
2129 $oldPaid = $payment->isPaid();
2131 $r = $payment->setPaid($values['PAID']);
2132 if (!$r->isSuccess())
2133 {
2134 $result->addErrors($r->getErrors());
2135 }
2136
2137 if ($payment->isInner() && !$oldPaid && $payment->isPaid())
2138 {
2139 Sale\Internals\UserBudgetPool::addPoolItem($order, ( $payment->getSum() * -1 ), Sale\Internals\UserBudgetPool::BUDGET_TYPE_ORDER_UNPAY, $payment);
2140 if (!$r->isSuccess())
2141 {
2142 $result->addErrors($r->getErrors());
2143 }
2144 }
2145
2146 }
2147
2148 if (isset($fields['PAID']))
2149 {
2150 unset($fields['PAID']);
2151 }
2152
2153 $r = $payment->setFields($fields);
2154 if (!$r->isSuccess())
2155 {
2156 $result->addErrors($r->getErrors());
2157 }
2158 }
2159 }
2160
2161 if (!$result->isSuccess())
2162 {
2163 return $result;
2164 }
2165
2167 $r = $order->save();
2168 if (!$r->isSuccess())
2169 {
2170 $result->addErrors($r->getErrors());
2171 }
2172 return $result;
2173 }
2174
2186 public static function payFromBudget(Sale\Order $order, $pay, $paidFormUserBudget = null)
2187 {
2188 $result = new Sale\Result();
2189
2191 $paymentInner = null;
2192
2194 $paymentOuter = null;
2195
2197 if (!$paymentCollection = $order->getPaymentCollection())
2198 {
2199 throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
2200 }
2201
2202 if (count($paymentCollection) > 2)
2203 return $result;
2204
2205 $needSum = $order->getPrice() - $order->getSumPaid();
2206
2207 if ($needSum > 0)
2208 {
2209
2211 foreach ($paymentCollection as $payment)
2212 {
2213 if (!$payment->isInner())
2214 {
2215 $paymentOuter = $payment;
2216 break;
2217 }
2218 }
2219
2220 if (!$pay || ($pay && $paidFormUserBudget === false))
2221 {
2223 $paymentInner = $paymentCollection->getInnerPayment();
2224 if (!$paymentInner)
2225 {
2226 $paymentInner = $paymentCollection->createInnerPayment();
2227 }
2228
2229 if (!$paymentInner)
2230 {
2231 throw new Main\ObjectNotFoundException('Entity inner "Payment" not found');
2232 }
2233
2234 $userBudget = Sale\Internals\UserBudgetPool::getUserBudget($order->getUserId(), $order->getCurrency());
2235
2236 $setSum = $userBudget;
2237 if ($userBudget >= $needSum)
2238 {
2239 $setSum = $needSum;
2240 }
2241
2242 if ($paymentInner->getId() == 0)
2243 {
2244 $paymentInnerFields = array(
2245 'SUM' => $setSum,
2246 'CURRENCY' => $order->getCurrency(),
2247 'DATE_BILL' => new Main\Type\DateTime(),
2248 );
2249
2250 $r = $paymentInner->setFields($paymentInnerFields);
2251 if (!$r->isSuccess())
2252 {
2253 $result->addErrors($r->getErrors());
2254 }
2255 }
2256 else
2257 {
2258 if ($paymentInner->getSum() < $needSum)
2259 {
2260 $paymentInner->setField('SUM', $needSum - $paymentInner->getSum());
2261 }
2262 }
2263
2264 if ($pay && $paidFormUserBudget === false)
2265 {
2266 $paymentOuter->setField('SUM', $needSum - $setSum);
2267 }
2268
2269 $payment = $paymentInner;
2270
2271 }
2272 else
2273 {
2274 $payment = $paymentOuter;
2275 }
2276
2277 if ($pay)
2278 {
2279
2280 if ($payment === null)
2281 {
2282 $paySystemId = static::getDefaultPaySystemId($order->getPersonTypeId());
2283
2285 if ($paySystem = Sale\PaySystem\Manager::getObjectById($paySystemId))
2286 {
2287 $registry = Sale\Registry::getInstance(static::getRegistryType());
2289 $paymentClassName = $registry->getPaymentClassName();
2290
2291 $payment = $paymentClassName::create($paymentCollection, $paySystem);
2292 $payment->setField('SUM', $needSum);
2293 $payment->setField('DATE_BILL', new Main\Type\DateTime());
2294 $paymentCollection->addItem($payment);
2295 }
2296 }
2297
2298 $operationPayment = $payment;
2299 if ($paidFormUserBudget === false)
2300 {
2301 $operationPayment = $paymentOuter;
2302 }
2303
2304 $service = Sale\PaySystem\Manager::getObjectById($operationPayment->getPaymentSystemId());
2305 if ($service)
2306 {
2307 $r = $service->creditNoDemand($operationPayment);
2308 if (!$r->isSuccess())
2309 $result->addErrors($r->getErrors());
2310 }
2311 else
2312 {
2313 $result->addError(new Main\Entity\EntityError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_PAYSYSTEM_NOT_FOUND')));
2314 return $result;
2315 }
2316 }
2317
2318 if ($payment->isReturn() && $payment->isInner())
2319 {
2320 $r = $payment->setPaid('Y');
2321 }
2322 else
2323 {
2325 $r = $payment->setPaid('Y');
2326
2327 if ($r->isSuccess())
2328 {
2329 if ($pay)
2330 {
2331 $operationPayment = $payment;
2332 if ($paidFormUserBudget === false)
2333 {
2334 $operationPayment = $paymentOuter;
2335
2337 $resultPayment = $paymentOuter->setPaid('Y');
2338 if (!$resultPayment->isSuccess())
2339 {
2340 $result->addErrors($resultPayment->getErrors());
2341 }
2342 }
2343
2344 $service = Sale\PaySystem\Manager::getObjectById($operationPayment->getPaymentSystemId());
2345 if ($service)
2346 {
2347 $r = $service->creditNoDemand($operationPayment);
2348 if (!$r->isSuccess())
2349 $result->addErrors($r->getErrors());
2350 }
2351 else
2352 {
2353 $result->addError(new Main\Entity\EntityError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_PAYSYSTEM_NOT_FOUND')));
2354 return $result;
2355 }
2356 }
2357
2358 }
2359 else
2360 {
2361 $result->addErrors($r->getErrors());
2362 }
2363 }
2364
2365 if (!$r->isSuccess())
2366 {
2367 $result->addErrors($r->getErrors());
2368 }
2369
2370 }
2371
2372 $result->setData(array('PAID_FROM_BUDGET' => $paidFormUserBudget));
2373
2374 return $result;
2375 }
2376
2386 public static function cancel($orderId, $value, $comment = false)
2387 {
2388 $result = new Sale\Result();
2389
2390 if (intval($orderId) <= 0)
2391 {
2392 $result->addError( new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_ID_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_ID_NOT_FOUND') );
2393 return $result;
2394 }
2395
2396 $registry = Sale\Registry::getInstance(static::getRegistryType());
2398 $orderClassName = $registry->getOrderClassName();
2399 if (!$order = $orderClassName::load($orderId))
2400 {
2401 $result->addError( new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_NOT_FOUND') );
2402 return $result;
2403 }
2404
2405 if ($value === 'N')
2406 {
2407 if ($order->isCanceled())
2408 {
2409 $r = $order->setField('CANCELED', 'N');
2410 if (!$r->isSuccess())
2411 {
2412 return $result->addErrors($r->getErrors());
2413 }
2414
2415 $r = $order->save();
2416 if (!$r->isSuccess())
2417 {
2418 return $result->addErrors($r->getErrors());
2419 }
2420 }
2421
2422 return $result;
2423 }
2424
2425 if ($order->isCanceled())
2426 {
2427 return $result;
2428 }
2429
2430 $paymentCollection = $order->getPaymentCollection();
2432 foreach ($paymentCollection as $payment)
2433 {
2434 if ($payment->isPaid())
2435 $payment->setReturn('Y');
2436 }
2437
2438 $shipmentCollection = $order->getShipmentCollection();
2440 foreach ($shipmentCollection as $shipment)
2441 {
2442 if ($shipment->isSystem())
2443 continue;
2444
2445 if ($shipment->isShipped())
2446 $shipment->setField('DEDUCTED', 'N');
2447
2448 if ($shipment->isAllowDelivery())
2449 $shipment->disallowDelivery();
2450 }
2451
2452 $r = $order->setField('CANCELED', 'Y');
2453 if (!$r->isSuccess())
2454 {
2455 return $result->addErrors($r->getErrors());
2456 }
2457
2458 if (!empty($comment) && strval($comment) != '')
2459 {
2460 $r = $order->setField('REASON_CANCELED', $comment);
2461 if (!$r->isSuccess())
2462 {
2463 return $result->addErrors($r->getErrors());
2464 }
2465 }
2466
2467 return $order->save();
2468 }
2469
2476 public static function delete($id)
2477 {
2478 $result = new Sale\Result();
2479
2480 if (intval($id) <= 0)
2481 {
2482 $result->addError( new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_ID_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_ID_NOT_FOUND') );
2483 return $result;
2484 }
2485
2486 $registry = Sale\Registry::getInstance(static::getRegistryType());
2488 $orderClassName = $registry->getOrderClassName();
2489 if (!$order = $orderClassName::load($id))
2490 {
2491 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_ORDER_ENTITY_NOT_FOUND'), 'SALE_ORDER_ENTITY_NOT_FOUND'));
2492 return $result;
2493 }
2494
2496 foreach ($order->getPaymentCollection() as $payment)
2497 {
2498 if ($payment->isPaid())
2499 {
2500 $payment->setPaid('N');
2501 }
2502 }
2503
2505 foreach ($order->getShipmentCollection() as $shipment)
2506 {
2507 if ($shipment->isShipped())
2508 {
2509 $shipment->setField('DEDUCTED', 'N');
2510 }
2511 }
2512
2513 $r = $order->save();
2514 if (!$r->isSuccess())
2515 {
2516 $result->addErrors($r->getErrors());
2517 return $result;
2518 }
2519
2520 try
2521 {
2522 $r = $orderClassName::delete($id);
2523 }
2524 catch (\Exception $exception)
2525 {
2526 $r = $orderClassName::deleteNoDemand($id);
2527 }
2528
2529 if (!$r->isSuccess())
2530 {
2531 $result->addErrors($r->getErrors());
2532 }
2533
2534 return $result;
2535 }
2536
2544 public static function canPayWithUserBudget($needSum, $userId, $currency, $fullPay = true)
2545 {
2547 if ($fullPay === false && $budget > 0)
2548 return true;
2549
2550 if ($fullPay === true && $budget >= $needSum)
2551 return true;
2552
2553 return false;
2554 }
2555
2556
2563 public static function createShipmentFromShipmentSystem(Sale\ShipmentCollection $shipmentCollection)
2564 {
2565
2566 $shipment = null;
2567
2569 $systemShipment = $shipmentCollection->getSystemShipment();
2570
2571 if ($systemShipment->getDeliveryId() > 0)
2572 {
2574 $shipment = static::getShipmentByDeliveryId($shipmentCollection, $systemShipment->getDeliveryId());
2575
2576 if (!$shipment)
2577 {
2578 if ($service = Sale\Delivery\Services\Manager::getObjectById($systemShipment->getDeliveryId()))
2579 {
2581 $shipment = $shipmentCollection->createItem($service);
2582 $shipment->setField('DELIVERY_NAME', $service->getName());
2583 }
2584 }
2585 }
2586
2587 return $shipment;
2588 }
2589
2590
2598 public static function getShipmentByDeliveryId(Sale\ShipmentCollection $shipmentCollection, $deliveryId)
2599 {
2601 foreach($shipmentCollection as $shipment)
2602 {
2603 if ($shipment->isSystem())
2604 continue;
2605
2606 if ($shipment->getDeliveryId() == $deliveryId)
2607 {
2608 return $shipment;
2609 }
2610 }
2611
2612 return false;
2613 }
2614
2618 protected static function transformationLocation(Sale\Order $order)
2619 {
2621 if ($propertyCollection = $order->getPropertyCollection())
2622 {
2624 foreach ($propertyCollection as $valueItem)
2625 {
2626 if ($valueItem->getValue() != '')
2627 {
2628 $setValue = $valueItem->getValue();
2629
2630 $prop = $valueItem->getPropertyObject();
2631 if ($prop->getType() == 'LOCATION')
2632 {
2633 $setValue = \CSaleLocation::tryTranslateIDToCode($setValue);
2634 }
2635
2636 $valueItem->setField('VALUE', $setValue);
2637 }
2638
2639 }
2640 }
2641
2642 }
2643
2650 public static function getById($id)
2651 {
2652 $compatibility = new static();
2653
2654 $select = array('*');
2655
2656 $registry = Sale\Registry::getInstance(static::getRegistryType());
2658 $orderClassName = $registry->getOrderClassName();
2659
2660 if ($order = $orderClassName::load($id))
2661 {
2663 if ($paymentCollection = $order->getPaymentCollection())
2664 {
2665 if (count($paymentCollection) == 1)
2666 {
2667 $select = array_merge($select, array_keys(static::getAliasPaymentFields()));
2668 }
2669 }
2670
2672 if ($shipmentCollection = $order->getShipmentCollection())
2673 {
2674 if (count($shipmentCollection) == 1
2675 || (count($shipmentCollection) == 2 && $shipmentCollection->isExistsSystemShipment()))
2676 {
2677 $select = array_merge($select, array_keys(static::getAliasShipmentFields()));
2678 }
2679 }
2680 }
2681
2682 return static::setGetListParameters($compatibility, array(), array("ID" => $id), null, array(), $select);
2683 }
2684
2689 public static function getAliasFields()
2690 {
2691 $fields = array(
2692 'NAME_SEARCH' => array(
2693 'NAME' => 'USER.NAME',
2694 'LAST_NAME' => 'USER.LAST_NAME',
2695 'SECOND_NAME' => 'USER.SECOND_NAME',
2696 'EMAIL' => 'USER.EMAIL',
2697 'LOGIN' => 'USER.LOGIN',
2698 'NAME_SEARCH' => 'USER.ID',
2699 ),
2700 'USER_ID' => 'USER.ID',
2701 'USER_LOGIN' => 'USER.LOGIN',
2702 'USER_NAME' => 'USER.NAME',
2703 'USER_LAST_NAME' => 'USER.LAST_NAME',
2704 'USER_EMAIL' => 'USER.EMAIL',
2705 'RESPONSIBLE_ID' => 'RESPONSIBLE.ID',
2706 'RESPONSIBLE_LOGIN' => 'RESPONSIBLE.LOGIN',
2707 'RESPONSIBLE_NAME' => 'RESPONSIBLE.NAME',
2708 'RESPONSIBLE_LAST_NAME' => 'RESPONSIBLE.LAST_NAME',
2709 'RESPONSIBLE_SECOND_NAME' => 'RESPONSIBLE.SECOND_NAME',
2710 'RESPONSIBLE_EMAIL' => 'RESPONSIBLE.EMAIL',
2711 'RESPONSIBLE_WORK_POSITION' => 'RESPONSIBLE.WORK_POSITION',
2712 'RESPONSIBLE_PERSONAL_PHOTO' => 'RESPONSIBLE.PERSONAL_PHOTO',
2713
2714 'PROPERTY_ID' => 'PROPERTY.ID',
2715 'PROPERTY_ORDER_PROPS_ID' => 'PROPERTY.ORDER_PROPS_ID',
2716 'PROPERTY_NAME' => 'PROPERTY.NAME',
2717 'PROPERTY_VALUE' => 'PROPERTY.VALUE',
2718 'PROPERTY_CODE' => 'PROPERTY.CODE',
2719 'PROPERTY_VAL_BY_CODE' => 'PROPERTY.VALUE',
2720// 'COMPLETE_ORDERS' => 'PROPERTY.ORDER_PROPS_ID',
2721
2722 );
2723 return array_merge(
2724 $fields,
2725 static::getAliasPaymentFields(),
2726 static::getAliasShipmentFields(),
2727 static::getAliasBasketFields()
2728 );
2729 }
2730
2734 protected static function getAliasPaymentFields()
2735 {
2736 return array(
2737 'PAY_SYSTEM_ID' => 'PAYMENT.PAY_SYSTEM_ID',
2738 'PAYED' => 'PAYMENT.PAID',
2739
2740 'DATE_PAYED' => 'PAYMENT.DATE_PAID',
2741 'EMP_PAYED_ID' => 'PAYMENT.EMP_PAID_ID',
2742
2743 'PS_STATUS' => 'PAYMENT.PS_STATUS',
2744 'PS_STATUS_CODE' => 'PAYMENT.PS_STATUS_CODE',
2745 'PS_STATUS_DESCRIPTION' => 'PAYMENT.PS_STATUS_DESCRIPTION',
2746 'PS_STATUS_MESSAGE' => 'PAYMENT.PS_STATUS_MESSAGE',
2747 'PS_SUM' => 'PAYMENT.PS_SUM',
2748 'PS_CURRENCY' => 'PAYMENT.PS_CURRENCY',
2749 'PS_RESPONSE_DATE' => 'PAYMENT.PS_RESPONSE_DATE',
2750 );
2751 }
2752
2756 protected static function getAliasShipmentFields()
2757 {
2758 return array(
2759 'DELIVERY_ID' => 'SHIPMENT.DELIVERY.CODE',
2760 //'DELIVERY_ID' => 'SHIPMENT.DELIVERY_ID',
2761 'PRICE_DELIVERY' => 'SHIPMENT.PRICE_DELIVERY',
2762 'ALLOW_DELIVERY' => 'SHIPMENT.ALLOW_DELIVERY',
2763 'DATE_ALLOW_DELIVERY' => 'SHIPMENT.DATE_ALLOW_DELIVERY',
2764 'EMP_ALLOW_DELIVERY_ID' => 'SHIPMENT.EMP_ALLOW_DELIVERY_ID',
2765
2766 'DATE_DEDUCTED' => 'SHIPMENT.DATE_DEDUCTED',
2767 'EMP_DEDUCTED_ID' => 'SHIPMENT.EMP_DEDUCTED_ID',
2768 'REASON_UNDO_DEDUCTED' => 'SHIPMENT.REASON_UNDO_DEDUCTED',
2769
2770 'TRACKING_NUMBER' => 'SHIPMENT.TRACKING_NUMBER',
2771 'DELIVERY_DOC_NUM' => 'SHIPMENT.DELIVERY_DOC_NUM',
2772 'DELIVERY_DOC_DATE' => 'SHIPMENT.DELIVERY_DOC_DATE',
2773 );
2774 }
2775
2779 protected static function getAliasBasketFields()
2780 {
2781 return array(
2782 'BASKET_ID' => 'BASKET.ID',
2783 'BASKET_PRODUCT_ID' => 'BASKET.PRODUCT_ID',
2784 'BASKET_PRODUCT_XML_ID' => 'BASKET.PRODUCT_XML_ID',
2785 'BASKET_MODULE' => 'BASKET.MODULE',
2786 'BASKET_NAME' => 'BASKET.NAME',
2787 'BASKET_QUANTITY' => 'BASKET.QUANTITY',
2788 'BASKET_PRICE' => 'BASKET.PRICE',
2789 'BASKET_CURRENCY' => 'BASKET.CURRENCY',
2790 'BASKET_VAT_RATE' => 'BASKET.VAT_RATE',
2791 'BASKET_RECOMMENDATION' => 'BASKET.RECOMMENDATION',
2792 'BASKET_DISCOUNT_PRICE' => 'BASKET.DISCOUNT_PRICE',
2793 'BASKET_DISCOUNT_NAME' => 'BASKET.DISCOUNT_NAME',
2794 'BASKET_DISCOUNT_VALUE' => 'BASKET.DISCOUNT_VALUE',
2795 );
2796 }
2797
2801 protected static function getSelectFields()
2802 {
2803 $fields = array_keys(static::getEntity()->getScalarFields());
2804
2805 return array_merge($fields, array(
2806 'DATE_INSERT_FORMAT',
2807 'DATE_UPDATE_SHORT',
2808 'DATE_STATUS_SHORT',
2809 'DATE_CANCELED_SHORT',
2810 'BY_RECOMMENDATION',
2811
2812 'LOCK_STATUS',
2813 'LOCK_USER_NAME',
2814 'DATE_INSERT_FORMAT',
2815
2816 "RESPONSIBLE_ID",
2817 "RESPONSIBLE_LOGIN",
2818 "RESPONSIBLE_NAME",
2819 "RESPONSIBLE_LAST_NAME",
2820 "RESPONSIBLE_SECOND_NAME",
2821 "RESPONSIBLE_EMAIL",
2822 "RESPONSIBLE_WORK_POSITION",
2823 "RESPONSIBLE_PERSONAL_PHOTO",
2824 "RESPONSIBLE_GROUP_ID",
2825
2826 "PAY_SYSTEM_ID",
2827 "DELIVERY_ID",
2828 "DEDUCTED",
2829 "RESERVED",
2830 "PRICE_DELIVERY",
2831 "ALLOW_DELIVERY",
2832 "DATE_ALLOW_DELIVERY",
2833 "EMP_ALLOW_DELIVERY_ID",
2834 "DELIVERY_DOC_NUM",
2835 "DELIVERY_DOC_DATE",
2836 "PAYED",
2837 "DATE_PAYED",
2838 "EMP_PAYED_ID",
2839 "STATUS_ID",
2840 "DATE_STATUS",
2841 "EMP_STATUS_ID",
2842 "DATE_INSERT_FORMAT",
2843 "USER_LOGIN",
2844 "USER_NAME",
2845 "USER_LAST_NAME",
2846 "USER_EMAIL",
2847 "DATE_PAY_BEFORE",
2848 "DATE_BILL",
2849 "ACCOUNT_NUMBER",
2850 "TRACKING_NUMBER",
2851
2852
2853 ));
2854
2855 }
2856
2861 public static function getAvailableFields()
2862 {
2863 $registry = Sale\Registry::getInstance(static::getRegistryType());
2865 $orderClassName = $registry->getOrderClassName();
2866
2867 return array_merge($orderClassName::getAvailableFields(),
2868 array('PRICE_DELIVERY', "PAY_VOUCHER_DATE", "PAY_VOUCHER_NUM", "DATE_ALLOW_DELIVERY", "DATE_PAYED")
2869 );
2870 }
2871
2872
2876 protected static function getShipmentClearFields()
2877 {
2878 return array(
2879 'STATUS_ID',
2880 'ACCOUNT_NUMBER',
2881 'DATE_INSERT',
2882 'MARKED',
2883 'EMP_MARKED_ID',
2884 'DATE_MARKED',
2885 'REASON_MARKED',
2886 'DATE_CANCELED',
2887 'EMP_CANCELED_ID',
2888 );
2889 }
2890
2891
2895 protected static function getPaymentClearFields()
2896 {
2897 return array(
2898 'ACCOUNT_NUMBER',
2899 'MARKED',
2900 'EMP_MARKED_ID',
2901 'DATE_MARKED',
2902 'REASON_MARKED',
2903 );
2904 }
2905
2909 protected static function getPaymentAvailableFields()
2910 {
2911 $registry = Sale\Registry::getInstance(static::getRegistryType());
2913 $paymentClassName = $registry->getPaymentClassName();
2914
2915 return static::clearAvailableFields($paymentClassName::getAvailableFields(), static::getPaymentClearFields());
2916 }
2917
2921 protected static function getShipmentAvailableFields()
2922 {
2923 $registry = Sale\Registry::getInstance(static::getRegistryType());
2925 $shipmentClassName = $registry->getShipmentClassName();
2926
2927 return static::clearAvailableFields($shipmentClassName::getAvailableFields(), static::getShipmentClearFields());
2928 }
2929
2930
2931 protected function getWhiteListFields()
2932 {
2933 return array_merge(parent::getWhiteListFields(), array_keys(static::getAliasFields()));
2934 }
2935
2936
2943 protected static function clearAvailableFields(array $fields, array $clearFields = array())
2944 {
2945 $result = array();
2946 if (!empty($clearFields))
2947 {
2948 foreach ($fields as $field)
2949 {
2950 if (!in_array($field, $clearFields))
2951 {
2952 $result[] = $field;
2953 }
2954 }
2955 }
2956
2957 return $result;
2958 }
2959
2963 protected static function getFieldsFromOtherEntities()
2964 {
2965 return array_merge(
2966 static::getShipmentFieldsToConvert(),
2967 static::getPaymentFieldsToConvert()
2968 );
2969 }
2970
2975 public static function getOrderDateFields()
2976 {
2977 return array(
2978 'DATE_INSERT' => 'datetime',
2979 'DATE_UPDATE' => 'datetime',
2980 'DATE_PAYED' => 'datetime',
2981 'DATE_STATUS' => 'datetime',
2982 'DATE_LOCK' => 'datetime',
2983 'DATE_PAY_BEFORE' => 'date',
2984 'DATE_BILL' => 'date',
2985 'DATE_MARKED' => 'datetime',
2986 'DATE_CANCELED' => 'datetime',
2987 );
2988 }
2989
2993 protected static function getShipmentFieldsToConvert()
2994 {
2995 return array(
2996// 'ALLOW_DELIVERY',
2997 'DATE_ALLOW_DELIVERY',
2998 'EMP_ALLOW_DELIVERY_ID',
2999
3000 'DELIVERY_DOC_NUM',
3001 'DELIVERY_DOC_DATE',
3002 'TRACKING_NUMBER',
3003
3004// 'DEDUCTED',
3005 'DATE_DEDUCTED',
3006 'EMP_DEDUCTED_ID',
3007 'REASON_UNDO_DEDUCTED',
3008
3009 'RESERVED',
3010 );
3011 }
3012
3016 protected static function getPaymentFieldsToConvert()
3017 {
3018 return array(
3019// 'PAYED',
3020 'DATE_PAYED',
3021 'EMP_PAYED_ID',
3022 'PAY_VOUCHER_NUM',
3023 'PAY_VOUCHER_DATE',
3024 'PAY_SYSTEM_ID',
3025 );
3026 }
3027
3032 {
3033 if ($entity instanceof Sale\Shipment)
3034 {
3035 return array(
3036 'DATE_ALLOW_DELIVERY' => 'datetime',
3037 'DELIVERY_DOC_DATE' => 'date',
3038 'DATE_DEDUCTED' => 'datetime',
3039 'DATE_MARKED' => 'datetime',
3040 'DATE_RESPONSIBLE_ID' => 'date',
3041 'DATE_INSERT' => 'datetime',
3042 'TRACKING_LAST_CHECK' => 'datetime',
3043 'TRACKING_LAST_CHANGE' => 'datetime',
3044 );
3045 }
3046 elseif ($entity instanceof Sale\Payment)
3047 {
3048 return array(
3049 'DATE_PAYED' => 'datetime',
3050 'DATE_PAID' => 'datetime',
3051 'PAY_VOUCHER_DATE' => 'date',
3052 'PS_RESPONSE_DATE' => 'datetime',
3053 );
3054 }
3055 }
3056
3060 protected static function getPaymentDateFields()
3061 {
3062 return array(
3063 'DATE_PAYED' => 'datetime',
3064 'DATE_PAID' => 'datetime',
3065 'PAY_VOUCHER_DATE' => 'date',
3066 'PS_RESPONSE_DATE' => 'datetime',
3067 );
3068 }
3069
3070
3074 protected static function getPaymentReplaceFields()
3075 {
3076 return array(
3077 'PAYED' => 'PAID',
3078 'DATE_PAYED' => 'DATE_PAID',
3079 'EMP_PAYED_ID' => 'EMP_PAID_ID',
3080 );
3081 }
3082
3086 protected static function getOrderReplaceFields()
3087 {
3088 return array();
3089 }
3090
3091
3100 public function resetOrderPrice(Sale\Basket $basket, array $requestFields)
3101 {
3102
3103 if (empty($requestFields['BASKET_ITEMS']))
3104 return false;
3105
3106 $resetPrice = false;
3107 $resetPriceDelivery = false;
3108
3110 $order = $this->getOrder();
3111
3112 if ($order->getId() == 0)
3113 {
3114 $order->resetData(array('PRICE_DELIVERY'));
3115 $resetPriceDelivery = true;
3116 }
3117
3118 foreach ($requestFields['BASKET_ITEMS'] as $basketData)
3119 {
3120 if (!isset($basketData['ID']) || intval($basketData['ID']) <= 0)
3121 continue;
3122
3124 if (!$basketItem = $basket->getItemById($basketData['ID']))
3125 continue;
3126
3127 if ($resetPriceDelivery === false)
3128 {
3129 if ($order->getId() == 0 || isset($basketData['PRICE'])
3130 && floatval($basketData['PRICE']) != $basketItem->getPrice())
3131 {
3132
3133 $order->resetData(array('PRICE'));
3134 $resetPrice = true;
3135 }
3136 }
3137
3138
3139// if ($resetPriceDelivery === false)
3140// {
3141// if ($order->getId() == 0 || isset($basketData['QUANTITY'])
3142// && floatval($basketData['QUANTITY']) != $basketItem->getQuantity())
3143// {
3144// $order->resetData(array('PRICE_DELIVERY'));
3145// $resetPriceDelivery = true;
3146// }
3147// }
3148
3149 if ($resetPriceDelivery && $resetPrice)
3150 return true;
3151 }
3152
3153
3154 //
3155 return false;
3156 }
3157
3163 public static function getOrderFields(Sale\Order $order)
3164 {
3165 $result = new Sale\Result();
3166
3167 $fields = array(
3168 "SITE_ID" => $order->getSiteId(),
3169 "LID" => $order->getSiteId(),
3170 "PERSON_TYPE_ID" => $order->getPersonTypeId(),
3171 "PRICE" => $order->getPrice(),
3172 "CURRENCY" => $order->getCurrency(),
3173 "USER_ID" => $order->getUserId(),
3174 "PAY_SYSTEM_ID" => (int)$order->getField('PAY_SYSTEM_ID'),
3175 "PRICE_DELIVERY" => $order->getDeliveryPrice(),
3176 "DELIVERY_ID" => (int)$order->getField('DELIVERY_ID'),
3177 "DISCOUNT_VALUE" => $order->getDiscountPrice(),
3178 "TAX_VALUE" => $order->getTaxValue(),
3179 "TRACKING_NUMBER" => $order->getField('TRACKING_NUMBER'),
3180 "PAYED" => $order->getField('PAYED'),
3181 "CANCELED" => $order->getField('CANCELED'),
3182 "STATUS_ID" => $order->getField('STATUS_ID'),
3183 "RESERVED" => $order->getField('RESERVED'),
3184 );
3185
3186 $orderFields = static::convertOrderToArray($order);
3187 if (is_array($orderFields))
3188 {
3189 $orderFields = $fields + $orderFields;
3190 $orderFields = static::convertDateFieldsToOldFormat($orderFields);
3191 }
3192
3193 $result->setData([
3194 'FIELDS' => $fields,
3195 'ORDER_FIELDS' => $orderFields,
3196 ]);
3197
3198 return $result;
3199 }
3200
3206 public static function convertOrderToArray(Sale\Order $order)
3207 {
3208 $fields = $order->getFieldValues();
3209
3210 //getWeight
3211 $fields = array_merge(
3212 $fields,
3213 [
3214 'ORDER_WEIGHT' => 0,
3215 'BASKET_ITEMS' => [],
3216 'ORDER_PROP' => [],
3217 'DISCOUNT_LIST' => [],
3218 'TAX_LIST' => [],
3219 'VAT_RATE' => $order->getVatRate(),
3220 'VAT_SUM' => $order->getVatSum(),
3221 ]
3222 );
3223
3225 if ($basket = $order->getBasket())
3226 {
3228 foreach ($basket as $basketItem)
3229 {
3231 $basketCompatibilityClassName = static::getBasketCompatibilityClassName();
3232
3233 $fields['BASKET_ITEMS'][] = $basketCompatibilityClassName::convertBasketItemToArray($basketItem);
3234 }
3235
3236 $fields['ORDER_WEIGHT'] = $basket->getWeight();
3237 }
3238
3240 if ($propertyCollection = $order->getPropertyCollection())
3241 {
3243 foreach ($propertyCollection as $property)
3244 {
3245// $propertyValue = $property->getValue();
3246 $fields['ORDER_PROP'][$property->getPropertyId()] = $property->getValue();
3247 }
3248 }
3249
3250
3251 if ($propProfileName = $propertyCollection->getProfileName())
3252 $fields['PROFILE_NAME'] = $propProfileName->getValue();
3253
3254 if ($propPayerName = $propertyCollection->getPayerName())
3255 $fields['PAYER_NAME'] = $propPayerName->getValue();
3256
3257 if ($propUserEmail = $propertyCollection->getUserEmail())
3258 $fields['USER_EMAIL'] = $propUserEmail->getValue();
3259
3260 if ($propDeliveryLocationZip = $propertyCollection->getDeliveryLocationZip())
3261 $fields['DELIVERY_LOCATION_ZIP'] = $propDeliveryLocationZip->getValue();
3262
3263 if ($propDeliveryLocation = $propertyCollection->getDeliveryLocation())
3264 $fields['DELIVERY_LOCATION'] = $propDeliveryLocation->getValue();
3265
3266 if ($propTaxLocation = $propertyCollection->getTaxLocation())
3267 $fields['TAX_LOCATION'] = $propTaxLocation->getValue();
3268
3270
3272 if ($tax = $order->getTax())
3273 {
3274 $fields['TAX_LIST'] = $tax->getTaxList();
3275 }
3276
3277 return $fields;
3278 }
3279
3280 public function isExistPrice()
3281 {
3282 return ($this->externalPrice !== null);
3283 }
3284
3290 public function parseField($key)
3291 {
3292 $output = null;
3293 $locationPropInfo = \CSaleOrder::getLocationPropertyInfo();
3294
3295 static $propIndex = 0;
3296
3297 $propIDTmp = false;
3298 if (mb_strpos($key, "PROPERTY_ID_") === 0)
3299 {
3300 $propIndex++;
3301 $this->addPropertyRuntime($propIndex);
3302 if (!($propRuntimeName = $this->getPropertyRuntimeName($propIndex)))
3303 {
3304 return null;
3305 }
3306
3307 $propIDTmp = intval(mb_substr($key, mb_strlen("PROPERTY_ID_")));
3308
3309 $this->query->addFilter('='.$propRuntimeName.'.ORDER_PROPS_ID', $propIDTmp);
3310 if(isset($locationPropInfo['ID'][$propIDTmp]))
3311 {
3312 $this->addQueryAlias('PROPERTY_ID_'.$propIDTmp, 'LOCATION.ID');
3313 }
3314 else
3315 {
3316 $this->addQueryAlias('PROPERTY_ID_'.$propIDTmp, $propRuntimeName.'.ID');
3317 }
3318
3319 $output = 'PROPERTY_ID_'.$propIDTmp;
3320
3321 }
3322 elseif (mb_strpos($key, "PROPERTY_ORDER_PROPS_ID_") === 0)
3323 {
3324 $propIndex++;
3325 $this->addPropertyRuntime($propIndex);
3326 if (!($propRuntimeName = $this->getPropertyRuntimeName($propIndex)))
3327 {
3328 return null;
3329 }
3330
3331 $propIDTmp = intval(mb_substr($key, mb_strlen("PROPERTY_ORDER_PROPS_ID_")));
3332
3333 $this->query->addFilter('='.$propRuntimeName.'.ORDER_PROPS_ID', $propIDTmp);
3334 if(isset($locationPropInfo['ID'][$propIDTmp]))
3335 {
3336 $this->addQueryAlias('PROPERTY_ORDER_PROPS_ID_'.$propIDTmp, 'LOCATION.ID');
3337 }
3338 else
3339 {
3340 $this->addQueryAlias('PROPERTY_ORDER_PROPS_ID_'.$propIDTmp, $propRuntimeName.'.ORDER_PROPS_ID');
3341 }
3342
3343 $output = 'PROPERTY_ORDER_PROPS_ID_'.$propIDTmp;
3344 }
3345 elseif (mb_strpos($key, "PROPERTY_NAME_") === 0)
3346 {
3347 $propIndex++;
3348 $this->addPropertyRuntime($propIndex);
3349 if (!($propRuntimeName = $this->getPropertyRuntimeName($propIndex)))
3350 {
3351 return null;
3352 }
3353
3354 $propIDTmp = intval(mb_substr($key, mb_strlen("PROPERTY_NAME_")));
3355
3356 $this->addQueryAlias('PROPERTY_NAME_'.$propIDTmp, $propRuntimeName.'.NAME');
3357 $this->query->addFilter('='.$propRuntimeName.'.ORDER_PROPS_ID', $propIDTmp);
3358
3359 $output = 'PROPERTY_NAME_'.$propIDTmp;
3360 }
3361 elseif (mb_strpos($key, "PROPERTY_VALUE_") === 0)
3362 {
3363 $propIndex++;
3364 $this->addPropertyRuntime($propIndex);
3365 if (!($propRuntimeName = $this->getPropertyRuntimeName($propIndex)))
3366 {
3367 return null;
3368 }
3369
3370 $propIDTmp = intval(mb_substr($key, mb_strlen("PROPERTY_VALUE_")));
3371
3372 if(isset($locationPropInfo['ID'][$propIDTmp]))
3373 {
3374 $this->addQueryAlias('PROPERTY_ID_'.$propIDTmp, 'LOCATION.ID');
3375 }
3376 else
3377 {
3378 $this->addQueryAlias('PROPERTY_ID_'.$propIDTmp, $propRuntimeName.'.VALUE');
3379 }
3380
3381 $output = 'PROPERTY_ID_'.$propIDTmp;
3382 }
3383 elseif (mb_strpos($key, "PROPERTY_CODE_") === 0)
3384 {
3385 $propIndex++;
3386 $this->addPropertyRuntime($propIndex);
3387 if (!($propRuntimeName = $this->getPropertyRuntimeName($propIndex)))
3388 {
3389 return null;
3390 }
3391
3392 $propIDTmp = intval(mb_substr($key, mb_strlen("PROPERTY_CODE_")));
3393 $this->addQueryAlias('PROPERTY_CODE_'.$propIDTmp, $propRuntimeName.'.CODE');
3394 $this->query->addFilter('='.$propRuntimeName.'.ORDER_PROPS_ID', $propIDTmp);
3395
3396 $output = 'PROPERTY_CODE_'.$propIDTmp;
3397 }
3398 elseif (mb_strpos($key, "PROPERTY_VAL_BY_CODE_") === 0)
3399 {
3400 $propIndex++;
3401 $this->addPropertyRuntime($propIndex);
3402 if (!($propRuntimeName = $this->getPropertyRuntimeName($propIndex)))
3403 {
3404 return null;
3405 }
3406
3407 $propIDTmp = preg_replace("/[^a-zA-Z0-9_-]/is", "", trim(mb_substr($key, mb_strlen("PROPERTY_VAL_BY_CODE_"))));
3408
3409 $this->addQueryAlias('PROPERTY_VAL_BY_CODE_'.$propIDTmp, $propRuntimeName.'.VALUE');
3410 if(isset($locationPropInfo['CODE'][$propIDTmp]))
3411 {
3412 $this->addQueryAlias('PROPERTY_VAL_BY_CODE_'.$propIDTmp, 'LOCATION.ID');
3413 }
3414 else
3415 {
3416 $this->addQueryAlias('PROPERTY_VAL_BY_CODE_'.$propIDTmp, $propRuntimeName.'.VALUE');
3417 }
3418
3419 $this->query->addFilter('='.$propRuntimeName.'.CODE', $propIDTmp);
3420
3421 $output = 'PROPERTY_VAL_BY_CODE_'.$propIDTmp;
3422 }
3423 elseif (mb_strpos($key, "BASKET_") === 0)
3424 {
3425 $output = static::addBasketRuntime($key);
3426 }
3427
3428 if(isset($locationPropInfo['ID'][$propIDTmp]))
3429 {
3430 $this->query->registerRuntimeField(
3431 'LOCATION',
3432 array(
3433 'data_type' => '\Bitrix\Sale\Location\LocationTable',
3434 'reference' => array(
3435 '=this.PROPERTY.VALUE' => 'ref.CODE'
3436 ),
3437 'join_type' => 'inner'
3438 )
3439 );
3440 }
3441
3442 return $output;
3443 }
3444
3445
3449 protected function addPropertyRuntime($index)
3450 {
3451 if ($this->getPropertyRuntimeName($index))
3452 return;
3453
3454 $this->query->registerRuntimeField(
3455 'PROPERTY_'.$index,
3456 array(
3457 'data_type' => '\Bitrix\Sale\Internals\OrderPropsValueTable',
3458 'reference' => array(
3459 'ref.ORDER_ID' => 'this.ID',
3460 ),
3461 'join_type' => 'inner'
3462 )
3463 );
3464
3465 $this->runtimeFields[] = 'PROPERTY_'.$index;
3466 $this->propertyRuntimeList[$index] = 'PROPERTY_'.$index;
3467 }
3468
3469
3470 protected function getPropertyRuntimeName($index)
3471 {
3472 return (!empty($this->propertyRuntimeList[$index]) ? $this->propertyRuntimeList[$index] : null);
3473 }
3474
3475
3483 protected function addBasketRuntime($key)
3484 {
3485 $output = null;
3486
3487 if ($key == "BASKET_DISCOUNT_COUPON")
3488 {
3489 if (!in_array('COUPONS', $this->runtimeFields))
3490 {
3491 $this->query->registerRuntimeField(
3492 'COUPONS',
3493 array(
3494 'data_type' => '\Bitrix\Sale\Internals\OrderCouponsTable',
3495 'reference' => array(
3496 '=ref.ORDER_ID' => 'this.ID'
3497 ),
3498 )
3499 );
3500 $this->runtimeFields[] = "COUPONS";
3501 }
3502
3503 $this->addQueryAlias('BASKET_DISCOUNT_COUPON', 'COUPONS.COUPON');
3504 $output = 'BASKET_DISCOUNT_COUPON';
3505
3506 }
3507 elseif ($key == "BASKET_DISCOUNT_NAME")
3508 {
3509 if (!in_array('DISCOUNT_ORDER_RULES', $this->runtimeFields))
3510 {
3511 $this->query->registerRuntimeField(
3512 'DISCOUNT_ORDER_RULES',
3513 array(
3514 'data_type' => '\Bitrix\Sale\Internals\OrderRulesTable',
3515 'reference' => array(
3516 '=ref.ORDER_ID' => 'this.ID',
3517 ),
3518 )
3519 );
3520 $this->runtimeFields[] = "DISCOUNT_ORDER_RULES";
3521 }
3522
3523 if (!in_array('DISCOUNT', $this->runtimeFields))
3524 {
3525 $this->query->registerRuntimeField(
3526 'DISCOUNT',
3527 array(
3528 'data_type' => '\Bitrix\Sale\Internals\OrderDiscountTable',
3529 'reference' => array(
3530 '=ref.ID' => 'this.DISCOUNT_ORDER_RULES.ORDER_DISCOUNT_ID'
3531 ),
3532 )
3533 );
3534
3535 $this->runtimeFields[] = "DISCOUNT";
3536 }
3537
3538 $this->addQueryAlias('BASKET_DISCOUNT_NAME', 'DISCOUNT.NAME');
3539 $output = 'BASKET_DISCOUNT_NAME';
3540 }
3541
3542 return $output;
3543 }
3544
3545 protected static function getDefaultFuserId()
3546 {
3547 return Sale\Fuser::getId();
3548 }
3549}
3550
3552{
3553
3554 protected static function getMoneyFields()
3555 {
3556 return array(
3557 "PRICE_DELIVERY",
3558 "PRICE",
3559 "DISCOUNT_VALUE",
3560 "DISCOUNT_ALL",
3561 "BASKET_PRICE_TOTAL",
3562 "PS_SUM",
3563 "PRICE_DELIVERY",
3564 );
3565 }
3566
3571 public function adapt(array $row)
3572 {
3574 return static::convertRowData($data);
3575 }
3576
3582 public static function convertRowData(array $data)
3583 {
3584 if (isset($data['DELIVERY_ID']) && intval($data['DELIVERY_ID']) > 0)
3585 {
3586 $data['DELIVERY_ID'] = \CSaleDelivery::getCodeById($data['DELIVERY_ID']);
3587 }
3588
3589 if (isset($data['CURRENCY']) && !empty($data['CURRENCY']))
3590 {
3591 foreach (static::getMoneyFields() as $field)
3592 {
3593 if (array_key_exists($field, $data))
3594 {
3595 $data[$field] = SaleFormatCurrency($data[$field], $data["CURRENCY"], false, true);
3596 }
3597 }
3598 }
3599
3600 return $data;
3601 }
3602}
$sum
Определения checkout.php:6
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static loadMessages($file)
Определения loc.php:65
static setShipment($order, $shipment)
Определения discountcompatibility.php:301
static convertDateFieldsToOldFormat(array $fields)
Определения entitycompatibility.php:632
parseRawFields($entityName, array $fields, array $availableFields=array())
Определения entitycompatibility.php:743
static getOrderFields(Sale\Order $order)
Определения ordercompatibility.php:3163
static clearAvailableFields(array $fields, array $clearFields=array())
Определения ordercompatibility.php:2943
setBasketCompatibility(BasketCompatibility $basketCompatibility)
Определения ordercompatibility.php:96
static canPayWithUserBudget($needSum, $userId, $currency, $fullPay=true)
Определения ordercompatibility.php:2544
static createShipmentFromRequest(Sale\ShipmentCollection $shipmentCollection, $deliveryId, array $requestFields)
Определения ordercompatibility.php:567
static add(array $fields)
Определения ordercompatibility.php:1631
static getEntityDateFields(Sale\Internals\CollectableEntity $entity)
Определения ordercompatibility.php:3031
static convertRowData(array $data)
Определения ordercompatibility.php:3582
static getId($skipCreate=false)
Определения fuser.php:33
static getUserBudget($userId, $currency)
Определения userbudgetpool.php:305
Определения payment.php:19
static roundPrecision($value)
Определения pricemaths.php:16
static getInstance($type)
Определения registry.php:183
const REGISTRY_TYPE_ORDER
Определения registry.php:16
static getCodeById($id)
Определения delivery.php:1655
static CanUserCancelOrder($ID, $arUserGroups=false, $userID=0)
Определения order.php:538
static getLocationPropertyInfo()
Определения order.php:3247
$data['IS_AVAILABLE']
Определения .description.php:13
$orderId
Определения payment.php:5
</td ></tr ></table ></td ></tr ><?endif?><? $propertyIndex=0;foreach( $arGlobalProperties as $propertyCode=> $propertyValue
Определения file_new.php:729
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$entity
$output
Определения options.php:436
global $USER
Определения csv_new_run.php:40
$siteId
Определения ajax.php:8
$value
Определения Param.php:39
Order
Определения order.php:6
$payment
Определения payment.php:14
$order
Определения payment.php:8
$paymentCollection
Определения payment.php:11
$service
Определения payment.php:18
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
const ADMIN_SECTION
Определения rss.php:2
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$comment
Определения template.php:15
$currency
Определения template.php:266
SaleFormatCurrency($fSum, $strCurrency, $OnlyValue=false, $withoutFormat=false)
Определения include.php:142
$action
Определения file_dialog.php:21
$propertyFields
Определения yandex_run.php:558
$fields
Определения yandex_run.php:501