Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
converterdocumentorder.php
1<?php
3
13use Bitrix\Sale;
14
21{
25 protected function getFieldsInfo()
26 {
28 }
29
35 public function resolveParams($documentImport)
36 {
37 if(!($documentImport instanceof DocumentBase))
38 throw new ArgumentException("Document must be instanceof DocumentBase");
39
40 $result = array();
41
42 $params = $documentImport->getFieldValues();
43
44 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
46 $orderClass = $registry->getOrderClassName();
47
48 $availableFields = $orderClass::getAvailableFields();
49
50 foreach ($availableFields as $k)
51 {
52 switch($k)
53 {
54 case 'ID_1C':
55 case 'VERSION_1C':
56 if(isset($params[$k]))
57 $fields[$k] = $params[$k];
58 break;
59 case 'COMMENTS':
60 if(isset($params['COMMENT']))
61 $fields[$k] = $params['COMMENT'];
62 break;
63 case 'CANCELED':
64 $value='';
65 if(isset($params['CANCELED']))
66 $value = $params['CANCELED'];
67
68 if($value == 'Y')
69 {
70 $fields[$k] = 'Y';
71 }
72 else
73
74 {
75 $v='';
76 if(isset($params['REK_VALUES']['CANCEL']))
77 {
78 $v = $params['REK_VALUES']['CANCEL'];
79 }
80
81 if($v == 'Y')
82 {
83 $fields[$k] = 'Y';
84 }
85 else
86 {
87 $fields[$k] = 'N';
88 }
89 }
90 break;
91 case 'DATE_INSERT':
92 if(isset($params['1C_TIME']) && $params['1C_TIME'] instanceof DateTime)
93 $fields[$k] = $params['1C_TIME'];
94 break;
95 case 'STATUS_ID':
96 if(isset($params['REK_VALUES']['1C_STATUS_ID']))
97 {
99 $settings = $this->getSettings();
100 if($settings->changeStatusFor($this->getEntityTypeId()) == 'Y')
101 $fields[$k] = $params['REK_VALUES']['1C_STATUS_ID'];
102 }
103 break;
104 case '1C_PAYED_DATE':
105 case '1C_DELIVERY_DATE':
106 if(isset($params['REK_VALUES'][$k]))
107 $fields[$k] = $params['REK_VALUES'][$k];
108 break;
109 }
110 }
111
112 $result['TRAITS'] = isset($fields)? $fields:array();
113 $result['ITEMS'] = isset($params['ITEMS'])? $this->modifyItemIdByItemName($params['ITEMS']):array();
114 $result['TAXES'] = isset($params['TAXES'])? $params['TAXES']:array();
115
116 return $result;
117 }
118
124 static public function sanitizeFields($order=null, array &$fields, ISettings $settings)
125 {
126 if(!empty($order) && !($order instanceof Sale\Order))
127 throw new ArgumentException("Entity must be instanceof Order");
128
129 if(empty($order))
130 {
131 $fields['DATE_STATUS'] = new DateTime() ;
132 $fields['DATE_UPDATE'] = new DateTime();
133 }
134 else
135 {
136 if(isset($fields['DATE_INSERT']))
137 unset($fields['DATE_INSERT']);
138 }
139
140 if(isset($fields['ID']))
141 unset($fields['ID']);
142
143 if(isset($fields['1C_PAYED_DATE']))
144 unset($fields['1C_PAYED_DATE']);
145
146 if(isset($fields['1C_DELIVERY_DATE']))
147 unset($fields['1C_DELIVERY_DATE']);
148 }
149
154 public function externalize(array $fields)
155 {
156 $result = array();
157
158 $traits = $fields['TRAITS'];
159 $items = $fields['ITEMS'];
160 $taxes = $fields['TAXES'];
161 $stories = isset($fields['STORIES']) ? array_unique($fields['STORIES'], SORT_NUMERIC): array();
162 $businessValue = $fields['BUSINESS_VALUE'];
163
164 $availableFields = $this->getFieldsInfo();
165
167 $settings = $this->getSettings();
168
169 foreach ($availableFields as $k=>$v)
170 {
171 $value='';
172 switch ($k)
173 {
174 case 'ID':
175 $value = $traits['ID'];
176 break;
177 case 'NUMBER':
179 $value = $settings->prefixFor($this->getEntityTypeId()).$traits['ACCOUNT_NUMBER'];
180 break;
181 case 'ID_1C':
182 $value = ($traits[$k]<>'' ? $traits[$k]:'');
183 break;
184 case 'DATE':
185 $value = $traits['DATE_INSERT'];
186 break;
187 case 'OPERATION':
188 $value = DocumentBase::resolveDocumentTypeName($this->getDocmentTypeId());
189 break;
190 case 'ROLE':
191 $value = DocumentBase::getLangByCodeField('SELLER');
192 break;
193 case 'CURRENCY':
194 $replaceCurrency = $settings->getReplaceCurrency();
195 $value = mb_substr($replaceCurrency <> ''? $replaceCurrency : $traits[$k], 0, 3);
196 break;
197 case 'CURRENCY_RATE':
198 $value = self::CURRENCY_RATE_DEFAULT;
199 break;
200 case 'AMOUNT':
201 $value = $traits['PRICE'];
202 break;
203 case 'VERSION':
204 $value = $traits['VERSION'];
205 break;
206 case 'TIME':
207 $value = $traits['DATE_INSERT'];
208 break;
209 case 'COMMENT':
210 $value = $traits['COMMENTS'];
211 break;
212 case 'DISCOUNTS':
213 $value = $this->externalizeDiscounts($traits, $v);
214 break;
215 case 'TAXES':
216 if(count($taxes)>0)
217 $value = $this->externalizeTaxes($taxes, $v);
218 break;
219 case 'STORIES':
220 if(count($stories)>0)
221 $value = $this->externalizeStories(current($stories), $v);
222 break;
223 case 'ITEMS':
224 $value = $this->externalizeItems($items, $v);
225 break;
226 case 'REK_VALUES':
227 $value=array();
228 foreach($v['FIELDS'] as $name=>$fieldInfo)
229 {
230 $valueRV='';
231 switch($name)
232 {
233 case 'DATE_PAID':
234 $valueRV = $traits['DATE_PAYED'];
235 break;
236 case 'PAY_NUMBER':
237 $valueRV = $traits['PAY_VOUCHER_NUM'];
238 break;
239 case 'DATE_ALLOW_DELIVERY_LAST':
240 $valueRV = $traits['DATE_ALLOW_DELIVERY'];
241 break;
242 case 'DELIVERY_SERVICE':
243 case 'DELIVERY_ID':
244 case 'PAY_SYSTEM':
245 case 'PAY_SYSTEM_ID':
246 case 'USER_DESCRIPTION':
247 $valueRV = $traits[$name];
248 break;
249 case 'ALLOW_DELIVERY':
250 //??????
251 break;
252 case 'ORDER_PAID':
253 $valueRV = $traits['PAYED'];
254 break;
255 case 'CANCEL':
256 $valueRV = $traits['CANCELED'];
257 break;
258 case 'FINAL_STATUS':
259 $valueRV = ($traits['STATUS_ID']=='F'? 'Y':'N');
260 break;
261 case 'ORDER_STATUS':
262 $valueRV = "[".$traits['STATUS_ID']."] ".static::getStatusNameById($traits['STATUS_ID']);
263 break;
264 case 'ORDER_STATUS_ID':
265 $valueRV = $traits['STATUS_ID'];
266 break;
267 case 'DATE_CANCEL':
268 $valueRV = $traits['DATE_CANCELED'];
269 break;
270 case 'CANCEL_REASON':
271 $valueRV = $traits['REASON_CANCELED'];
272 break;
273 case 'DATE_STATUS':
274 $valueRV = $traits['DATE_STATUS'];
275 break;
276 case 'SITE_NAME':
277 $valueRV = '['.$traits['LID'].'] '.static::getSiteNameByLid($traits['LID']);
278 break;
279 case 'REKV':
280 $value = array_merge($value, $this->externalizeRekv($businessValue[$name], $fieldInfo));
281 break;
282 }
283 if(!in_array($name, array('REKV')))
284 {
285 $value[] = $this->externalizeRekvValue($name, $valueRV, $fieldInfo);
286 }
287 }
288 break;
289 }
290 if(!in_array($k, array('DISCOUNTS', 'TAXES', 'STORES', 'ITEMS', 'REK_VALUES')))
291 {
292 $this->externalizeField($value, $v);
293 }
294
295 $result[$k] = $value;
296 }
297 $result = $this->modifyTrim($result);
298
299 return $result;
300 }
301
308 public function externalizeItems(array $items, array $info)
309 {
310 $result = array();
311 foreach ($items as $rowId=>$item)
312 {
313 foreach($info['FIELDS'] as $name=>$fieldInfo)
314 {
315 $value='';
316 $fieldValues = [];
317 switch ($name)
318 {
319 case 'ID':
320 $value = static::normalizeExternalCode($item['PRODUCT_XML_ID']);
321 break;
322 case 'CATALOG_ID':
323 $value = $item['CATALOG_XML_ID'];
324 break;
325 case 'NAME':
326 $value = $item['NAME'];
327 break;
328 case 'PRICE_PER_UNIT':
329 $value = $item['PRICE'];
330 break;
331 case 'QUANTITY':
332 $value = $item['QUANTITY'];
333 break;
334 case 'SUMM':
335 $value = $item['PRICE']*$item['QUANTITY'];
336 break;
337 case 'KOEF':
338 $value = self::KOEF_DEFAULT;
339 break;
340 case 'ITEM_UNIT':
341 $code = (intval($item['MEASURE_CODE'])>0 ? $item['MEASURE_CODE']:self::MEASURE_CODE_DEFAULT);
342 foreach($fieldInfo['FIELDS'] as $unitFieldName=>$unitFieldInfo)
343 {
344 $unitValue = '';
345 switch ($unitFieldName)
346 {
347 case 'ITEM_UNIT_CODE':
348 $unitValue = $code;
349 break;
350 case 'ITEM_UNIT_NAME':
351 $unitValue = static::getCatalogMeasureByCode($code);
352 break;
353 }
354 $this->externalizeField($unitValue, $unitFieldInfo);
355 $fieldValues[$unitFieldName] = $unitValue;
356 }
357 break;
358 case 'DISCOUNTS':
359 $summ = doubleval($item['DISCOUNT_PRICE']);
360 if($summ > 0)
361 {
362 foreach($fieldInfo['FIELDS'] as $discountFieldName=>$discountFieldInfo)
363 {
364 $discountValue = '';
365 switch ($discountFieldName)
366 {
367 case 'NAME':
368 $discountValue = DocumentBase::getLangByCodeField('ITEM_DISCOUNT');
369 break;
370 case 'SUMM':
371 $discountValue = $item['DISCOUNT_PRICE'];
372 break;
373 case 'IN_PRICE':
374 $discountValue = 'Y';
375 break;
376 }
377 $this->externalizeField($discountValue, $discountFieldInfo);
378 $fieldValues[$discountFieldName] = $discountValue;
379 }
380 }
381 break;
382 case 'REK_VALUES':
383 foreach($fieldInfo['FIELDS'] as $rekFieldName=>$rekFieldInfo)
384 {
385 $fieldValue = [];
386 switch ($rekFieldName)
387 {
388 case 'TYPE_NOMENKLATURA':
389 foreach ($rekFieldInfo['FIELDS'] as $nameProp=>$infoProp)
390 {
391 $valueProp='';
392 switch ($nameProp)
393 {
394 case 'NAME':
395 $valueProp = DocumentBase::getLangByCodeField('TYPE_NOMENKLATURA');
396 break;
397 case 'VALUE':
399 break;
400 }
401 $this->externalizeField($valueProp, $infoProp);
402 $fieldValue[$nameProp] = $valueProp;
403 }
404 $fieldValues[] = $fieldValue;
405 break;
406 case 'TYPE_OF_NOMENKLATURA':
407 foreach ($rekFieldInfo['FIELDS'] as $nameProp=>$infoProp)
408 {
409 $valueProp='';
410 switch ($nameProp)
411 {
412 case 'NAME':
413 $valueProp = DocumentBase::getLangByCodeField('TYPE_OF_NOMENKLATURA');
414 break;
415 case 'VALUE':
417 break;
418 }
419 $this->externalizeField($valueProp, $infoProp);
420 $fieldValue[$nameProp] = $valueProp;
421 }
422 $fieldValues[] = $fieldValue;
423 break;
424 case 'BASKET_NUMBER':
425 foreach ($rekFieldInfo['FIELDS'] as $nameProp=>$infoProp)
426 {
427 $valueProp='';
428 switch ($nameProp)
429 {
430 case 'NAME':
431 $valueProp = DocumentBase::getLangByCodeField('BASKET_NUMBER');
432 break;
433 case 'VALUE':
434 $valueProp = \CSaleExport::getNumberBasketPosition($item['ID']);
435 break;
436 }
437 $this->externalizeField($valueProp, $infoProp);
438 $fieldValue[$nameProp] = $valueProp;
439 }
440 $fieldValues[] = $fieldValue;
441 break;
442 case 'PROPERTY_VALUE_BASKET':
443 $attributes = isset($item['ATTRIBUTES'])? $item['ATTRIBUTES']:array();
444 if(count($attributes)>0)
445 {
446 foreach ($attributes as $rowIdAttr=>$attribute)
447 {
448 foreach ($rekFieldInfo['FIELDS'] as $nameProp=>$infoProp)
449 {
450 $valueProp='';
451 switch ($nameProp)
452 {
453 case 'NAME':
454 $valueProp = DocumentBase::getLangByCodeField('PROPERTY_VALUE_BASKET').'#'.($attribute['CODE']<>'' ? $attribute['CODE']:$attribute['NAME']);
455 break;
456 case 'VALUE':
457 $valueProp = $attribute['VALUE'];
458 break;
459 }
460 $this->externalizeField($valueProp, $infoProp);
461 $fieldValues[$rowIdAttr][$nameProp] = $valueProp;
462 }
463 }
464 }
465 break;
466 }
467 }
468 break;
469 case 'TAX_RATES':
470 $rate = doubleval($item['VAT_RATE']);
471 if($rate > 0)
472 {
473 foreach($fieldInfo['FIELDS'] as $rateFieldName=>$rateFieldInfo)
474 {
475 $rateValue = '';
476 switch ($rateFieldName)
477 {
478 case 'NAME':
479 $rateValue = DocumentBase::getLangByCodeField('VAT');
480 break;
481 case 'RATE':
482 $rateValue = $item['VAT_RATE']*100;
483 break;
484 }
485 $this->externalizeField($rekValue, $rateFieldInfo);
486 $fieldValues[$rateFieldName] = $rateValue;
487 }
488 }
489 break;
490 case 'TAXES':
491 $rate = doubleval($item['VAT_RATE']);
492 if($rate > 0)
493 {
494 foreach($fieldInfo['FIELDS'] as $taxFieldName=>$taxFieldInfo)
495 {
496 $taxValue = '';
497 switch ($taxFieldName)
498 {
499 case 'NAME':
500 $taxValue = DocumentBase::getLangByCodeField('VAT');
501 break;
502 case 'TAX_VALUE':
503 $taxValue = (($item["PRICE"] / ($item["VAT_RATE"]+1)) * $item["VAT_RATE"]);
504 break;
505 case 'IN_PRICE':
506 $taxValue = 'Y';
507 break;
508 }
509 $this->externalizeField($taxValue, $taxFieldInfo);
510 $fieldValues[$taxFieldName] = $taxValue;
511 }
512 }
513 break;
514 }
515
516 if($value<>'')
517 {
518 $this->externalizeField($value, $fieldInfo);
519 $result[$rowId][$name] = $value;
520 }
521 elseif (is_array($fieldValues))
522 {
523 $result[$rowId][$name] = $fieldValues;
524 }
525 }
526 }
527 return $result;
528 }
529
536 public function externalizeStories(array $stories, array $info)
537 {
538 $result = array();
539 $converterProfile = new ConverterDocumentProfile();
540
541 foreach ($stories as $store)
542 {
543 $store = static::getStoreById($store['ID']);
544
545 $resultStores=array();
546 foreach($info['FIELDS'] as $name=>$fieldInfo)
547 {
548 $value='';
549 switch ($name)
550 {
551 case 'ID':
552 $value = $store['XML_ID'];
553 break;
554 case 'NAME':
555 $value = $store['TITLE'];
556 break;
557 case 'ADDRESS':
558 if(isset($store['ADDRESS']))
559 $value = $converterProfile->externalizeArrayFields(array('STREET'=>$store['ADDRESS']), $fieldInfo);
560 break;
561 case 'CONTACTS':
562 if(isset($store['PHONE']))
563 $value = $converterProfile->externalizeArrayFields(array('WORK_PHONE_NEW'=>$store['PHONE']), $fieldInfo);
564 break;
565 }
566 if(!is_array($value))
567 $this->externalizeField($value, $fieldInfo);
568 $resultStores[$name] = $value;
569 }
570 $result[] = $resultStores;
571 }
572 return $result;
573 }
574
580 public function externalizeTaxes(array $taxes, array $info)
581 {
582 $result = array();
583 foreach ($taxes as $rowId=>$tax)
584 {
585 foreach($info['FIELDS'] as $name=>$fieldInfo)
586 {
587 $value='';
588 switch($name)
589 {
590 case 'NAME':
591 $value = $tax['TAX_NAME'];
592 break;
593 case 'IN_PRICE':
594 $value = $tax['IS_IN_PRICE'];
595 break;
596 case 'SUMM':
597 $value = $tax['VALUE_MONEY'];
598 break;
599 }
600 $this->externalizeField($value, $fieldInfo);
601 $result[$rowId][$name] = $value;
602 }
603 }
604 return $result;
605 }
606
612 public function externalizeDiscounts(array $discount, array $info)
613 {
614 $result = array();
615
616 if(doubleval($discount['DISCOUNT_VALUE'])>0)
617 {
618 foreach($info['FIELDS'] as $name=>$fieldInfo)
619 {
620 $value='';
621 switch($name)
622 {
623 case 'NAME':
624 $value = DocumentBase::getLangByCodeField('ORDER_DISCOUNT');
625 break;
626 case 'IN_PRICE':
627 $value = 'N';
628 break;
629 case 'AMOUNT':
630 $value = $discount['DISCOUNT_VALUE'];
631 break;
632 }
633 $this->externalizeField($value, $fieldInfo);
634 $result[$name] = $value;
635 }
636 }
637 return $result;
638 }
639
644 static private function getCatalogMeasureByCode($code)
645 {
646 static $measure;
647
648 if($measure[$code] === null)
649 {
650 if(Loader::includeModule("catalog"))
651 {
652 $r = \CCatalogMeasure::getList(
653 array(),
654 array(),
655 false,
656 false,
657 array(
658 "CODE",
659 "MEASURE_TITLE"
660 )
661 );
662 while($res = $r->Fetch())
663 {
664 $measure[$res["CODE"]] = $res["MEASURE_TITLE"];
665 }
666 }
667 if($measure === null)
668 $measure[self::MEASURE_CODE_DEFAULT] = \CSaleExport::getTagName("SALE_EXPORT_SHTUKA");
669 }
670 return $measure[$code];
671 }
672
677 static private function getStoreById($id)
678 {
679 static $stories;
680 if($stories[$id] === null)
681 {
682 if(Loader::includeModule('catalog'))
683 {
684 $res = \CCatalogStore::GetList(
685 array("SORT" => "DESC", "ID" => "ASC"),
686 array("ACTIVE" => "Y", "ISSUING_CENTER" => "Y"),
687 false,
688 false,
689 array("ID", "SORT", "TITLE", "ADDRESS", "DESCRIPTION", "PHONE", "EMAIL", "XML_ID")
690 );
691 while ($store = $res->Fetch())
692 {
693 if($store["XML_ID"] == '')
694 $store["XML_ID"] = $store["ID"];
695
696 $stories[$store["ID"]] = $store;
697 }
698
699 if(!is_array($stories))
700 {
701 $stories = array();
702 }
703 }
704 }
705 return (isset($stories[$id]) ? $stories[$id]:array());
706 }
707
712 static protected function getStatusNameById($id)
713 {
714 static $statuses;
715
716 if($statuses === null)
717 {
718 $res = StatusLangTable::getList(array(
719 'select' => array('*'),
720 'filter' => array('=LID' => LANGUAGE_ID)
721 ));
722 while($status = $res->fetch())
723 {
724 $statuses[$status['STATUS_ID']] = $status['NAME'];
725 }
726
727 if(!is_array($statuses))
728 {
729 $statuses = array();
730 }
731 }
732 return (isset($statuses[$id])?$statuses[$id]:'');
733 }
734}
static sanitizeFields($order=null, array &$fields, ISettings $settings)