Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderbuildernew.php
1<?
3
9
11{
12 protected $builder = null;
13
15 {
16 $this->builder = $builder;
17 }
18
19 public function createOrder(array $data)
20 {
21 $siteId = '';
22
23 if(isset($data['SITE_ID']))
24 {
25 $siteId = $data['SITE_ID'];
26 }
27 elseif($data['LID'])
28 {
29 $siteId = $data['LID'];
30 }
31
32 if($siteId == '')
33 {
34 $this->builder->getErrorsContainer()->addError(new Error(Loc::getMessage("SALE_HLP_OBN_SITEID_ABSENT")));
35 throw new BuildingException();
36 }
37
38 $currentUserId = 0;
39 $oldUserId = null;
40
41 if (isset($data['USER_ID']))
42 {
43 $currentUserId = (int)$data['USER_ID'];
44 }
45
46 if (isset($data['OLD_USER_ID']))
47 {
48 $oldUserId = (int)$data['OLD_USER_ID'];
49 }
50
51 $currency = null;
52 if (isset($data['CURRENCY']))
53 {
54 $currency = $data['CURRENCY'];
55 }
56
57 //If buyer changed - discount also can be changed
58 OrderEdit::initCouponsData($currentUserId, $data['ID'], $oldUserId);
59 $orderClassName = $this->builder->getRegistry()->getOrderClassName();
60 $order = $orderClassName::create($siteId, $currentUserId, $currency);
61
62 if(!$order)
63 {
64 $this->builder->getErrorsContainer()->addError(new Error(Loc::getMessage("SALE_HLP_OBE_ORDER_NOT_CREATED")));
65 throw new BuildingException();
66 }
67
68 return $order;
69 }
70
71 public function buildBasket()
72 {
73 if(is_array($this->builder->getFormData('PRODUCT')))
74 {
75 $this->builder->getBasketBuilder()
76 ->initBasket()
77 ->preliminaryDataPreparation()
78 ->itemsDataPreparation()
79 ->basketCodeMap()
80 ->setItemsFields()
81 ->fillFUser()
82 ->finalActions();
83 }
84 else
85 {
86 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
87
89 $basketClass = $registry->getBasketClassName();
90
91 if($basket = $basketClass::create($this->builder->getOrder()->getSiteId()))
92 {
93 $this->builder->getOrder()->setBasket($basket);
94 }
95 else
96 {
97 $this->builder->getErrorsContainer()->addError(new Error('Can\'t create basket'));
98 throw new BuildingException();
99 }
100 }
101 }
102
103 public function setUser()
104 {
105 $this->builder->getOrder()->setFieldNoDemand(
106 'USER_ID',
107 $this->builder->getUserId()
108 );
109
110 $currentUserId = (int)$this->builder->getOrder()->getUserId();
111 $oldFormDataUserId = (int)$this->builder->getFormData('OLD_USER_ID');
112
113 $currentPersonTypeId = (int)$this->builder->getOrder()->getPersonTypeId();
114 $oldPersonTypeId = (int)$this->builder->getFormData('OLD_PERSON_TYPE_ID');
115
116 $reloadProfile = $oldFormDataUserId > 0 && $currentUserId !== $oldFormDataUserId;
117 if (!$reloadProfile && $oldPersonTypeId > 0 && $oldPersonTypeId !== $currentPersonTypeId)
118 {
119 $reloadProfile = true;
120 }
121
122 if ($reloadProfile)
123 {
124 $resultLoading = \Bitrix\Sale\OrderUserProperties::loadProfiles($currentUserId, $currentPersonTypeId);
125 if (!$resultLoading->isSuccess())
126 {
127 return;
128 }
129 $profiles = $resultLoading->getData();
130 if (empty($profiles) || !is_array($profiles))
131 {
132 return;
133 }
134 $currentProfile = current($profiles[$currentPersonTypeId]);
135 if (empty($currentProfile))
136 {
137 return;
138 }
139 $values = $currentProfile['VALUES'];
140 $propertyCollection = $this->builder->getOrder()->getPropertyCollection();
141 $propertyCollection->setValuesFromPost(
142 ['PROPERTIES' => $values],[]
143 );
144 }
145 }
146
147 public function setShipmentPriceFields(Shipment $shipment, array $fields)
148 {
149 if ($fields['CUSTOM_PRICE_DELIVERY'] !== 'Y')
150 {
151 $fields['PRICE_DELIVERY'] = $shipment->calculateDelivery()->getPrice();
152 }
153 $fields['BASE_PRICE_DELIVERY'] = $fields['PRICE_DELIVERY'];
154
155 $res = $shipment->setFields($fields);
156
157 if(!$res->isSuccess())
158 {
159 $this->builder->getErrorsContainer()->addErrors($res->getErrors());
160 }
161
162 return $shipment;
163 }
164}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static initCouponsData($newUserId, $orderId=0, $oldUserId=0)
setShipmentPriceFields(Shipment $shipment, array $fields)