19 private static $FIELD_INFOS =
null;
23 if($parentEntityContext instanceof Internals\
Entity)
28 parent::__construct(
null);
39 protected function createEntity(array $fileds)
43 $orderClass = $registry->getOrderClassName();
45 return $orderClass::create($this->settings->getSiteId(), $fileds[
'USER_ID'], $this->settings->getCurrency());
71 public function save()
80 if(!static::$FIELD_INFOS)
82 static::$FIELD_INFOS = array(
93 return static::$FIELD_INFOS;
101 public function add(array $params)
106 $basketItems = $params[
'ITEMS'];
107 $taxes = $params[
'TAXES'];
110 $personalTypeId =
$fields[
'PERSON_TYPE_ID'];
112 $propertyFields =
'';
113 if(isset(
$fields[
'ORDER_PROP']))
115 $propertyFields =
$fields[
'ORDER_PROP'];
119 if(empty($personalTypeId))
121 $result->addError(
new Main\
Error(
'Person type is not load'));
126 $result->addError(
new Main\
Error(
'User id is not load'));
128 if(!$result->isSuccess())
134 $entity = $this->createEntity([
'USER_ID'=>$userId]);
135 $entity->setPersonTypeId($personalTypeId);
137 $result = $this->fillProperty(
$entity, $propertyFields);
138 if(!$result->isSuccess())
143 $registry = Sale\Registry::getInstance($entity::getRegistryType());
144 $basketClassName = $registry->getBasketClassName();
146 $basket = $basketClassName::create(
$entity->getSiteId());
148 $result = $this->fillBasket($basket, $basketItems);
149 if($result->isSuccess())
152 $items = $result->getData();
153 $this->fillTax(
$entity, $taxes, $items[
'modifyTaxList']);
163 $result =
$entity->doFinalAction(
true);
164 if ($result->isSuccess())
177 public function update(array $params)
179 $result =
new Sale\Result();
181 $criterion = $this->getCurrentCriterion($this->
getEntity());
187 $basketItems = $params[
'ITEMS'];
188 $taxes = $params[
'TAXES'];
190 $propertyFields =
'';
191 if(isset(
$fields[
'ORDER_PROP']))
193 $propertyFields =
$fields[
'ORDER_PROP'];
197 if($criterion->equals(
$fields))
199 $result = $this->fillProperty($order, $propertyFields);
200 if(!$result->isSuccess())
205 $basket = $order->getBasket();
207 $result = $this->fillBasket($basket, $basketItems);
208 if($result->isSuccess())
210 $items = $result->getData();
211 $this->fillTax($order, $taxes, $items[
'modifyTaxList']);
218 $result = $order->doFinalAction(
true);
219 if(!$result->isSuccess())
224 foreach (
$fields as $k =>$field)
226 if(!in_array($k, $order::getAvailableFields()))
230 $result = $order->setFields(
$fields);
240 public function delete(array $params =
null)
250 return 'EXTERNAL_ORDER';
261 if($result->isSuccess())
265 $order = $this->loadParentEntity(
$fields);
285 public function markedEntityCollisions(
$collisions)
292 $entity->setField(
'MARKED',
'Y');
295 $result =
new Sale\Result();
296 $result->addWarning(
new Sale\ResultError(
EntityCollisionType::getDescription($collision->getTypeId()).($collision->getMessage() !=
null ?
" ".$collision->getMessage():
'' ), $collision->getTypeName()));
302 private static function prepareFieldsBasketProperty($item)
306 if(!empty($item[
'ATTRIBUTES']))
308 foreach($item[
'ATTRIBUTES'] as $id => $value)
320 private function prepareFieldsBasketItem($productXML_ID, $item)
326 $product = $code<>
'' ? static::getProduct($code):array();
329 $product = static::getProduct($productXML_ID);
334 "PRODUCT_ID" => $product[
"ID"],
335 "NAME" => $product[
"NAME"],
336 "MODULE" =>
"catalog",
337 "PRODUCT_PROVIDER_CLASS" =>
'\Bitrix\Catalog\Product\CatalogProvider',
338 "CATALOG_XML_ID" => $product[
"IBLOCK_XML_ID"],
339 "DETAIL_PAGE_URL" => $product[
"DETAIL_PAGE_URL"],
340 "WEIGHT" => $product[
"WEIGHT"],
341 "NOTES" => $product[
"CATALOG_GROUP_NAME"]
346 $ri =
new Main\Type\RandomSequence($productXML_ID);
348 "PRODUCT_ID" => $ri->rand(1000000, 9999999),
349 "NAME" => $item[
"NAME"],
350 "MODULE" =>
"1c_exchange",
351 "PRODUCT_PROVIDER_CLASS" =>
false,
352 "CATALOG_XML_ID" =>
"1c_exchange",
353 "DISCOUNT_PRICE" => $item[
'DISCOUNT'][
'PRICE'],
354 "MEASURE_CODE" => $item[
"MEASURE_CODE"],
355 "MEASURE_NAME" => $item[
"MEASURE_NAME"]
359 $result[
"CURRENCY"] =
$settings->getCurrency();
361 $result[
"QUANTITY"] = $item[
"QUANTITY"];
362 $result[
"DELAY"] =
"N";
363 $result[
"CAN_BUY"] =
"Y";
364 $result[
"IGNORE_CALLBACK_FUNC"] =
"Y";
365 $result[
"PRODUCT_XML_ID"] = $productXML_ID;
366 $result[
"MARKING_CODE_GROUP"] = $item[
'MARKING_GROUP'];
372 private function prepareFieldsTax(
$fields)
379 "IS_IN_PRICE" =>
$fields[
"IN_PRICE"],
380 "VALUE_MONEY" =>
$fields[
"SUMM"],
382 "APPLY_ORDER" =>
"100"
395 if(mb_strpos($code,
'#') !==
false)
397 $code = explode(
'#', $code);
403 private static function getProduct($code)
407 $r = \CIBlockElement::GetList(array(),
408 array(
"=XML_ID" => $code,
"ACTIVE" =>
"Y",
"CHECK_PERMISSIONS" =>
"Y"),
411 array(
"ID",
"IBLOCK_ID",
"XML_ID",
"NAME",
"DETAIL_PAGE_URL")
413 if($ar = $r->GetNext())
416 $product = \CCatalogProduct::GetByID($ar[
"ID"]);
418 $result[
"WEIGHT"] = $product[
"WEIGHT"];
419 $result[
"CATALOG_GROUP_NAME"] = $product[
"CATALOG_GROUP_NAME"];
421 $productIBlock = static::getIBlockProduct($ar[
"IBLOCK_ID"]);
422 $result[
"IBLOCK_XML_ID"] = $productIBlock[$ar[
"IBLOCK_ID"]][
"XML_ID"];
433 static public function getBasketItemByItem(
Sale\
BasketBase $basket, array $item)
435 foreach($basket as $basketItem)
438 if($item[
'ID'] == $basketItem->getField(
'PRODUCT_XML_ID'))
440 $fieldsBasketProperty = static::prepareFieldsBasketProperty($item);
442 $propertyBasketItem = array();
444 if($basketPropertyCollection = $basketItem->getPropertyCollection())
445 $propertyBasketItem = $basketPropertyCollection->getPropertyValues();
447 if(!empty($fieldsBasketProperty) && is_array($fieldsBasketProperty))
449 if($basketPropertyCollection->isPropertyAlreadyExists($fieldsBasketProperty))
454 elseif(count($propertyBasketItem)<=0)
477 foreach($items as $productXML_ID => $item)
484 $result[$k][$productXML_ID] = $item;
498 private function fillBasket(
Sale\
BasketBase $basket, array $basketItems)
502 $taxListModify = array();
503 $basketItemsIndexList = array();
506 foreach ($basket as $basketItem)
508 $basketItemsIndexList[$basketItem->getId()] = $basketItem->getQuantity();
511 $basketItems = static::getGroupItemsBasketFields($basketItems);
513 if(!empty($basketItems))
516 foreach($basketItems as $items)
518 foreach($items as $productXML_ID => $item)
520 $fieldsBasket = array();
521 if($basketItem = static::getBasketItemByItem($basket, $item))
524 $criterionBasketItems = $this->getCurrentCriterion($basket->getOrder());
526 if($criterionBasketItems->equalsBasketItem($basketItem, $item))
528 if($item[
'MARKING_GROUP'] != $basketItem->getMarkingCodeGroup())
529 $fieldsBasket[
'MARKING_CODE_GROUP'] = $item[
'MARKING_GROUP'];
531 if($item[
'PRICE'] != $basketItem->getPrice())
532 $basketItem->setPrice($item[
'PRICE'],
true);
534 if($item[
'QUANTITY'] != $basketItem->getQuantity())
535 $fieldsBasket[
'QUANTITY'] = $item[
'QUANTITY'];
538 $criterionBasketItemsTax = $this->getCurrentCriterion($basket->getOrder());
540 if($criterionBasketItemsTax->equalsBasketItemTax($basketItem, $item))
542 $taxListModify[$basketItem->getBasketCode()] = $item[
'TAX'];
546 $criterionBasketItemsDiscount = $this->getCurrentCriterion($basket->getOrder());
548 if($criterionBasketItemsDiscount->equalsBasketItemDiscount($basketItem, $item))
550 $fieldsBasket[
'DISCOUNT_PRICE'] = $item[
'DISCOUNT'][
'PRICE'];
554 if (isset($basketItemsIndexList[$basketItem->getId()]))
555 unset($basketItemsIndexList[$basketItem->getId()]);
560 $fieldsBasket = $this->prepareFieldsBasketItem($productXML_ID, $item);
561 $fieldsCurrency = $this->convertCurrency($item);
563 $fieldsBasket[
'CURRENCY'] = $fieldsCurrency[
'CURRENCY'];
564 $fieldsBasket[
'SORT'] = $sort;
567 $basketItem = $basket->createItem($fieldsBasket[
'MODULE'], $fieldsBasket[
'PRODUCT_ID']);
569 $basketItem->setPrice($fieldsCurrency[
'PRICE'],
true);
571 unset($fieldsBasket[
'MODULE'], $fieldsBasket[
'PRODUCT_ID']);
573 $taxListModify[$basketItem->getBasketCode()] = $item[
'TAX'];
576 if(!empty($fieldsBasket))
578 $r = $basketItem->setFields($fieldsBasket);
581 $fieldsBasketProperty = static::prepareFieldsBasketProperty($item);
582 if(!empty($fieldsBasketProperty))
585 if ($propertyCollection = $basketItem->getPropertyCollection())
587 $propertyCollection->setProperty($fieldsBasketProperty);
593 $result->addErrors($r->getErrors());
600 if($result->isSuccess())
602 $result->setData(array(
'modifyTaxList'=>$taxListModify));
604 $r = $this->synchronizeQuantityBasketItems($basketItemsIndexList);
607 if(!empty($basketItemsIndexList) && is_array($basketItemsIndexList))
609 foreach ($basketItemsIndexList as $basketIndexId => $basketIndexValue)
612 if ($foundedBasketItem = $basket->getItemById($basketIndexId))
614 $resultDelete = $foundedBasketItem->delete();
615 if($resultDelete->isSuccess() ==
false)
617 $result->addErrors($resultDelete->getErrors());
625 $result->addErrors($r->getErrors());
636 protected function convertCurrency(array $item)
639 $result[
'CURRENCY'] = $this->settings->getCurrency();
644 if($this->getEntityId()>0 && $order->getCurrency() <> $this->settings->getCurrency())
646 $item[
'PRICE'] = \CCurrencyRates::ConvertCurrency($item[
'PRICE'], $this->settings->getCurrency(), $order->getCurrency());
648 $result[
'CURRENCY'] = $order->getCurrency();
651 $result[
'PRICE'] = $item[
'PRICE'];
661 private static function getProductsVatRate(
Sale\Basket $basket)
664 static $vatFields =
null;
666 foreach($basket as $basketItem)
668 if($provider = $basketItem->getProvider())
671 if($vatFields[$basketItem->getProductId()] ===
null)
673 $rsVAT = \CCatalogProduct::GetVATInfo($basketItem->getProductId());
674 if ($arVAT = $rsVAT->Fetch())
675 $vatFields[$basketItem->getProductId()] = $arVAT[
'RATE'];
679 if (isset($vatFields[$basketItem->getProductId()]))
680 $vatRate = (float)$vatFields[$basketItem->getProductId()] * 0.01;
682 $result[$basketItem->getBasketCode()] = array(
'VAT_RATE'=>$vatRate);
697 private function fillTax(Order $order, array
$fields, $modifyTaxList)
699 if(isset($modifyTaxList))
702 $tax = $order->getTax();
703 $tax->resetTaxList();
706 $basket = $order->getBasket();
707 $productVatData = static::getProductsVatRate($basket);
710 foreach($basket as $basketItem)
712 $code = $basketItem->getBasketCode();
713 if(isset($modifyTaxList[$code]))
721 $productVatFields = $productVatData[$basketItem->getBasketCode()];
722 if(!empty($productVatFields))
724 if($productVatFields[
'VAT_RATE'] <> $modifyTaxList[$code][
'VAT_RATE'])
731 $basketItem->setField(
'VAT_RATE', $modifyTaxList[$code][
'VAT_RATE']);
732 $basketItem->setField(
'VAT_INCLUDED', $modifyTaxList[$code][
'VAT_INCLUDED']);
736 $tax->initTaxList($this->prepareFieldsTax(
$fields));
737 $order->refreshVat();
741 private function fillProperty(Order $order, $fieldsOrderProperty)
743 $result =
new Sale\Result();
746 $propCollection = $order->getPropertyCollection();
748 if (!empty($fieldsOrderProperty) && is_array($fieldsOrderProperty))
750 $fields[
'PROPERTIES'] = $fieldsOrderProperty;
753 $r = $propCollection->setValuesFromPost(
$fields, $_FILES);
754 if (!$r->isSuccess())
756 $result->addErrors($r->getErrors());
774 if(!($order instanceof
Order))
780 private static function getIBlockProduct($iblockId)
782 static $iblock_fields =
null;
784 if($iblock_fields[$iblockId] ==
null)
786 $r = \CIBlock::GetList(array(), array(
"ID" => $iblockId));
787 if ($ar = $r->Fetch())
788 $iblock_fields[$iblockId] = $ar;
790 return $iblock_fields;
799 public function calculateDeltaQuantity(
Sale\
Basket $basket, array $basketItems)
801 $basketItemsIndexQuantityList = array();
804 foreach ($basket as $basketItem)
806 $basketItemsIndexQuantityList[$basketItem->getId()] = $basketItem->getQuantity();
809 if(!empty($basketItems) && is_array($basketItems))
811 foreach($basketItems as $items)
813 foreach($items as $productXML_ID => $item)
815 if($basketItem = static::getBasketItemByItem($basket, $item))
817 if(isset($basketItemsIndexQuantityList[$basketItem->getId()]))
819 if($basketItemsIndexQuantityList[$basketItem->getId()] <= $item[
'QUANTITY'])
821 unset($basketItemsIndexQuantityList[$basketItem->getId()]);
825 $basketItemsIndexQuantityList[$basketItem->getId()] -= $item[
'QUANTITY'];
833 return $basketItemsIndexQuantityList;
843 public function synchronizeQuantityBasketItems(array $basketItemsIndex)
845 $result =
new Sale\Result();
852 $basket = $order->getBasket();
854 $shipmentCollection = $order->getShipmentCollection();
856 $systemShipment = $shipmentCollection->getSystemShipment();
858 if(!empty($basketItemsIndex) && is_array($basketItemsIndex))
860 foreach ($basketItemsIndex as $basketIndexId => $basketIndexQuantity)
863 if ($foundedBasketItem = $basket->getItemById($basketIndexId))
865 $systemBasketQuantity = $systemShipment->getBasketItemQuantity($foundedBasketItem);
867 if($basketIndexQuantity>$systemBasketQuantity)
869 $needQuantity = $basketIndexQuantity-$systemBasketQuantity;
873 Exchange\ManagerImport::configure($shipmentImport);
874 $shipmentImport->setParentEntity($order);
876 $r = $shipmentImport->synchronizeQuantityShipmentItems($foundedBasketItem, $needQuantity);
884 $result->addErrors($r->getErrors());
916 'ITEMS'=>$this->getFieldsItems(),
927 protected function getFieldsItems()
931 if($order instanceof
Order)
934 $basketItems = $order->getBasket();
935 foreach ($basketItems as $basket)
937 $attributes = array();
938 $attributeFields = static::getAttributesItem($basket);
939 if(count($attributeFields)>0)
940 $attributes[
'ATTRIBUTES'] = $attributeFields;
942 $result[] = array_merge($basket->getFieldValues(), $attributes);
953 static public function getAttributesItem(
Sale\BasketItem $basket)
957 $propertyItems = $basket->getPropertyCollection();
958 foreach ($propertyItems as $property)
960 $result[] = $property->getFieldValues();
973 if($order instanceof
Order)
975 $res = \CSaleOrderTax::GetList(
977 array(
"ORDER_ID" => $order->getId()),
980 array(
"ID",
"TAX_NAME",
"VALUE",
"VALUE_MONEY",
"CODE",
"IS_IN_PRICE")
982 while ($tax = $res->Fetch())
996 $cashBoxOneCId = \Bitrix\Sale\Cashbox\Cashbox1C::getId();
998 if($order instanceof
Order)
1000 if($cashBoxOneCId>0)
1002 $result = \Bitrix\Sale\Cashbox\CheckManager::getPrintableChecks(array($cashBoxOneCId), array($order->getId()));