Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderbuilderexist.php
1<?
3
10
17{
19 protected $builder = null;
20
26 {
27 $this->builder = $builder;
28 }
29
36 public function createOrder(array $data)
37 {
38 $orderClassName = $this->builder->getRegistry()->getOrderClassName();
39 $currentUserId = 0;
40 $oldUserId = null;
41
42 $dataUserId = (int)($data["USER_ID"] ?? 0);
43 if ($dataUserId > 0)
44 {
45 $currentUserId = $dataUserId;
46 }
47
48 $dataOldUserId = (int)($data["OLD_USER_ID"] ?? 0);
49 if ($dataOldUserId > 0)
50 {
51 $oldUserId = $dataOldUserId;
52 }
53
54 //If buyer changed - discount also can be changed
55 OrderEdit::initCouponsData($currentUserId, $data["ID"], $oldUserId);
56 $order = $orderClassName::load($data["ID"]);
57
58 if(!$order)
59 {
60 $this->builder->getErrorsContainer()->addError(new Error(Loc::getMessage("SALE_HLP_OBE_ORDER_NOT_LOADED")));
61 throw new BuildingException();
62 }
63
64 return $order;
65 }
66
67 public function setUser()
68 {
69 $currentUserId = (int)$this->builder->getOrder()->getUserId();
70
71 $formDataUserId = (int)($this->builder->getFormData()['USER_ID'] ?? 0);
72 $isChanged = ($formDataUserId > 0) && ($currentUserId !== $formDataUserId);
73 if ($currentUserId && $isChanged)
74 {
75 $paymentCollection = $this->builder->getOrder()->getPaymentCollection();
77 foreach ($paymentCollection as $payment)
78 {
79 if ($payment->isPaid())
80 {
81 $this->builder->getErrorsContainer()->addError(new Error(
82 Loc::getMessage("SALE_HLP_OBE_CHANGE_USER_ERROR")
83 , 'SALE_ORDEREDIT_ERROR_CHANGE_USER_WITH_PAID_PAYMENTS'));
84 }
85 }
86 }
87
88 if ($formDataUserId > 0)
89 {
90 $this->builder->getOrder()->setFieldNoDemand(
91 "USER_ID",
92 $this->builder->getUserId()
93 );
94 }
95
96 if ($isChanged)
97 {
98 $personTypeId = (int)$this->builder->getOrder()->getPersonTypeId();
99 $resultLoading = \Bitrix\Sale\OrderUserProperties::loadProfiles($formDataUserId, $personTypeId);
100 if (!$resultLoading->isSuccess())
101 {
102 return;
103 }
104 $profiles = $resultLoading->getData();
105 if (!is_array($profiles[$personTypeId]))
106 {
107 return;
108 }
109 $currentProfile = current($profiles[$personTypeId]);
110 if (empty($currentProfile))
111 {
112 return;
113 }
114 $values = $currentProfile['VALUES'];
115 $propertyCollection = $this->builder->getOrder()->getPropertyCollection();
116 $propertyCollection->setValuesFromPost(
117 ['PROPERTIES' => $values],[]
118 );
119 }
120 }
121
122 public function buildBasket()
123 {
124 if(is_array($this->builder->getFormData('PRODUCT')))
125 {
126 $this->builder->getBasketBuilder()
127 ->initBasket()
128 ->preliminaryDataPreparation()
129 ->removeDeletedItems() //edit only
130 ->itemsDataPreparation()
131 ->basketCodeMap()
132 ->setItemsFields()
133 ->fillFUser()
134 ->finalActions();
135 }
136 }
137
138 public function setShipmentPriceFields(Shipment $shipment, array $fields)
139 {
140 if ($fields['CUSTOM_PRICE_DELIVERY'] !== 'Y' && $shipment->getId() <= 0)
141 {
142 $priceDelivery = $shipment->calculateDelivery()->getPrice();
143 }
144 else
145 {
146 $priceDelivery = $fields['PRICE_DELIVERY'];
147 }
148
149 if ($fields['CUSTOM_PRICE_DELIVERY'] === 'Y' || !isset($fields['BASE_PRICE_DELIVERY']))
150 {
151 $basePriceDelivery = $priceDelivery;
152 }
153 else
154 {
155 $basePriceDelivery = $fields['BASE_PRICE_DELIVERY'];
156 }
157
158 $fields['BASE_PRICE_DELIVERY'] = $basePriceDelivery;
159 $fields['PRICE_DELIVERY'] = $priceDelivery;
160
161 $res = $shipment->setFields($fields);
162
163 if(!$res->isSuccess())
164 {
165 $this->builder->getErrorsContainer()->addErrors($res->getErrors());
166 }
167
168 return $shipment;
169 }
170}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static initCouponsData($newUserId, $orderId=0, $oldUserId=0)
setShipmentPriceFields(Shipment $shipment, array $fields)