Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
importonecpackagecrm.php
1<?php
2
4
5
6use Bitrix\Crm\History\InvoiceStatusHistoryEntry;
7use Bitrix\Crm\Statistics\InvoiceSumStatisticEntry;
16
18{
23 protected function checkDocuments(array $documents)
24 {
25 $result = new Result();
26
27 if(!$this->hasDocumentByTypeId(Exchange\OneC\DocumentType::ORDER, $documents))
28 {
29 $result->addError(new Error(GetMessage('CRM_PACKAGE_NOT_FOUND_ORDER'), 'CRM_PACKAGE_NOT_FOUND_ORDER'));
30 }
31 else
32 {
33 $documentOrder = $this->getDocumentByTypeId(Exchange\OneC\DocumentType::ORDER, $documents);
34
35 if(!$this->hasDocumentByTypeId(Exchange\OneC\DocumentType::PAYMENT_CASH, $documents) &&
36 !$this->hasDocumentByTypeId(Exchange\OneC\DocumentType::PAYMENT_CASH_LESS, $documents) &&
37 !$this->hasDocumentByTypeId(Exchange\OneC\DocumentType::PAYMENT_CARD_TRANSACTION, $documents))
38 {
39 $result->addError(new Error(GetMessage('CRM_PACKAGE_NOT_FOUND_PAYMENT', array('#XML_1C_DOCUMENT_ID#'=>$documentOrder->getExternalId())), 'CRM_PACKAGE_NOT_FOUND_PAYMENT'));
40 }
41
42 $countShipment = 0;
43 foreach ($documents as $document)
44 {
45 if($document->getTypeId() == Exchange\OneC\DocumentType::SHIPMENT)
46 {
47 $countShipment++;
48 }
49 }
50
51 if($countShipment>=2)
52 {
53 $result->addError(new Error(GetMessage('CRM_PACKAGE_PARTIAL_SHIPMENT_NOT_SUPPORTED', array('#XML_1C_DOCUMENT_ID#'=>$documentOrder->getExternalId())), 'CRM_PACKAGE_NOT_FOUND_PAYMENT'));
54 }
55 }
56
57 return $result;
58 }
59
64 protected function modifyEntity($item)
65 {
66 $result = new Result();
67
68 if($item instanceof OrderImport)
69 {
70 if($item->getId()>0)
71 {
72 $traits = $item->getField('TRAITS');
73
74 $invoice = new \CCrmInvoice(false);
75 if (!$invoice->SetStatus($item->getId(), $traits['STATUS_ID']))
76 {
77 $result->addError(new Error('Status error!'));
78 }
79 }
80 }
81
82 if($result->isSuccess())
83 $result = parent::modifyEntity($item);
84
85
86 return $result;
87 }
88
94 protected function save(Exchange\Entity\OrderImport $orderImport, $items)
95 {
96 $isNew = !($orderImport->getId()>0);
97
98 $result = parent::save($orderImport, $items);
99
100 if($result->isSuccess())
101 {
102 if($orderImport->getId()>0)
103 {
104 InvoiceStatusHistoryEntry::register($orderImport->getId(), null, array('IS_NEW' => $isNew));
105 InvoiceSumStatisticEntry::register($orderImport->getId(), null);
106 }
107 }
108 return $result;
109 }
110
115 protected function converterFactoryCreate($typeId)
116 {
117 return ConverterFactory::create($typeId);
118 }
123 protected function convert(array $documents)
124 {
125 $result = $this->checkDocuments($documents);
126
127 if($result->isSuccess())
128 {
129 $documentShipment = $this->getDocumentByTypeId(Exchange\OneC\DocumentType::SHIPMENT, $documents);
130
131 if($documentShipment !== null)
132 {
133 $entityTypeId = $this->resolveOwnerEntityTypeId($documentShipment->getTypeId());
134 $settings = ManagerImport::getSettingsByType($entityTypeId);
135
136 $convertor = Exchange\OneC\ConverterFactory::create($entityTypeId);
137 $convertor->init(
138 $settings,
139 $entityTypeId,
140 $documentShipment->getTypeId()
141 );
142
143 $fields = $convertor->resolveParams($documentShipment);
144
145 $shipmentPrice = $fields['TRAITS']['BASE_PRICE_DELIVERY'];
146
147 if($shipmentPrice>0)
148 {
149 $documentOrder = $this->getDocumentByTypeId(Exchange\OneC\DocumentType::ORDER, $documents);
150 $fieldsOrder = $documentOrder->getFieldValues();
151 $items = $this->getProductsItems($fieldsOrder);
152
153 if(!$this->deliveryServiceExists($items))
154 {
155 $fieldsOrder['ITEMS'][][self::DELIVERY_SERVICE_XMLID] = array(
156 'ID' => self::DELIVERY_SERVICE_XMLID,
157 'NAME' => GetMessage('CRM_PACKAGE_DELIVERY_SERVICE_ITEM'),
158 'PRICE' => $shipmentPrice,
159 'PRICE_ONE' => $shipmentPrice,
160 'QUANTITY' => 1,
161 'TYPE' => ImportBase::ITEM_ITEM,
162 'MEASURE_CODE' => 796,
163 'MEASURE_NAME' => GetMessage('CRM_PACKAGE_DELIVERY_SERVICE_ITEM_MEASURE_796'),
164 );
165
166 $documentOrder->setFields($fieldsOrder);
167 }
168 }
169
170 foreach ($documents as $k=>$document)
171 {
172 if($document->getTypeId() == Exchange\OneC\DocumentType::SHIPMENT)
173 {
174 unset($documents[$k]);
175 }
176 }
177 }
178
179 $result = parent::convert($documents);
180
181 if($result->isSuccess())
182 {
183 $personTypeId = 0;
184 $paySystemId = 0;
185 $personTypes = \CCrmPaySystem::getPersonTypeIDs();
186
188 $entityItems = $result->getData();
189 foreach ($entityItems as $entityItem)
190 {
191 if($entityItem->getOwnerTypeId() == EntityType::USER_PROFILE)
192 {
194 $personTypeId = $entityItem->isFiz()? (int)$personTypes['CONTACT']:(int)$personTypes['COMPANY'];
195 break;
196 }
197 }
198
199 if($personTypeId>0)
200 {
201 $billList = \CCrmPaySystem::GetPaySystemsListItems($personTypeId);
202 foreach($billList as $billId => $billName)
203 {
204 $paySystemId = $billId;
205 break;
206 }
207 }
208
209 if($paySystemId>0)
210 {
211 $list = [];
212 foreach ($entityItems as $entityItem)
213 {
214 if(
215 $entityItem->getOwnerTypeId() == EntityType::INVOICE_PAYMENT_CASH ||
216 $entityItem->getOwnerTypeId() == EntityType::INVOICE_PAYMENT_CASH_LESS ||
217 $entityItem->getOwnerTypeId() == EntityType::INVOICE_PAYMENT_CARD_TRANSACTION)
218 {
219 $traits = $entityItem->getFieldValues()['TRAITS'];
220 $traits['PAY_SYSTEM_ID'] = $paySystemId;
221 $entityItem->setField('TRAITS', $traits);
222 }
223 $list[] = $entityItem;
224 }
225 $result->setData($list);
226 }
227 }
228 }
229
230 return $result;
231 }
232
233 public static function configuration()
234 {
235 ManagerImport::registerInstance(static::getShipmentEntityTypeId(), OneC\ImportSettings::getCurrent(), new OneC\CollisionShipment(), new OneC\CriterionShipmentInvoice());
236
237 parent::configuration();
238 }
239
240 protected function resolveEntityTypeId(\Bitrix\Sale\Internals\Entity $entity)
241 {
242 $typeId = EntityType::UNDEFINED;
243
244 if($entity instanceof Order)
245 $typeId = Exchange\Entity\Invoice::resolveEntityTypeId($entity);
246 elseif ($entity instanceof Payment)
247 $typeId = Exchange\Entity\PaymentInvoiceBase::resolveEntityTypeId($entity);
248 elseif ($entity instanceof Shipment)
249 $typeId = Exchange\Entity\ShipmentInvoice::resolveEntityTypeId($entity);
250
251 return $typeId;
252 }
253
254 static protected function getParentEntityTypeId()
255 {
256 return EntityType::INVOICE;
257 }
258
259 static protected function getShipmentEntityTypeId()
260 {
262 }
263
264 static protected function getPaymentCardEntityTypeId()
265 {
267 }
268
269 static protected function getPaymentCashEntityTypeId()
270 {
272 }
273
274 static protected function getPaymentCashLessEntityTypeId()
275 {
277 }
278}
resolveEntityTypeId(\Bitrix\Sale\Internals\Entity $entity)
save(Exchange\Entity\OrderImport $orderImport, $items)
static registerInstance($typeId, ISettingsImport $settings, ICollision $collision=null, ICriterion $criterion=null)