예제 #1
0
 /**
  * @param \Bitrix\Sale\Shipment $shipment
  * @throws Main\NotSupportedException
  */
 public static function setShipmentByDefaultValues(&$shipment)
 {
     /** @var \Bitrix\Sale\ShipmentCollection $shipmentCollection */
     $shipmentCollection = $shipment->getCollection();
     $systemShipment = $shipmentCollection->getSystemShipment();
     $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
     /** @var \Bitrix\Sale\ShipmentItemCollection $shipmentItemCollection */
     $shipmentItemCollection = $shipment->getShipmentItemCollection();
     /** @var \Bitrix\Sale\ShipmentItem $systemShipmentItem */
     foreach ($systemShipmentItemCollection as $systemShipmentItem) {
         if ($systemShipmentItem->getQuantity() <= 0) {
             continue;
         }
         $basketItem = $systemShipmentItem->getBasketItem();
         $shipmentItem = $shipmentItemCollection->createItem($basketItem);
         $shipmentItem->setField('QUANTITY', $systemShipmentItem->getQuantity());
     }
     $shipment->setField('CUSTOM_PRICE_DELIVERY', 'N');
     $shipment->setField('DELIVERY_ID', $systemShipment->getField('DELIVERY_ID'));
     $shipment->setField('COMPANY_ID', $systemShipment->getField('COMPANY_ID'));
     $shipment->setField('DELIVERY_NAME', $systemShipment->getField('DELIVERY_NAME'));
     $shipment->setExtraServices($systemShipment->getExtraServices());
     $shipment->setStoreId($systemShipment->getStoreId());
     $price = \Bitrix\Sale\Helpers\Admin\Blocks\OrderShipment::getDeliveryPrice($shipment);
     $shipment->setField('BASE_PRICE_DELIVERY', $price);
 }
예제 #2
0
 /**
  * Modify shipment collection.
  *
  * @param string $action				Action code.
  * @param Shipment $shipment			Shipment.
  * @param null|string $name					Field name.
  * @param null|string|int|float $oldValue				Old value.
  * @param null|string|int|float $value					New value.
  * @return bool
  *
  * @throws Main\NotImplementedException
  * @throws \Bitrix\Main\ArgumentException
  * @throws \Bitrix\Main\ArgumentOutOfRangeException
  * @throws \Bitrix\Main\NotSupportedException
  * @throws \Exception
  */
 public function onShipmentCollectionModify($action, Shipment $shipment, $name = null, $oldValue = null, $value = null)
 {
     global $USER;
     $result = new Result();
     if ($action == EventActions::DELETE) {
         if ($this->getField('DELIVERY_ID') == $shipment->getDeliveryId()) {
             /** @var ShipmentCollection $shipmentCollection */
             if (!($shipmentCollection = $shipment->getCollection())) {
                 throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
             }
             $foundShipment = false;
             /** @var Shipment $entityShipment */
             foreach ($shipmentCollection as $entityShipment) {
                 if ($entityShipment->isSystem()) {
                     continue;
                 }
                 if (intval($entityShipment->getField('DELIVERY_ID')) > 0) {
                     $foundShipment = true;
                     $this->setFieldNoDemand('DELIVERY_ID', $entityShipment->getField('DELIVERY_ID'));
                     break;
                 }
             }
             if (!$foundShipment && !$shipment->isSystem()) {
                 /** @var Shipment $systemShipment */
                 if (($systemShipment = $shipmentCollection->getSystemShipment()) && intval($systemShipment->getField('DELIVERY_ID')) > 0) {
                     $this->setFieldNoDemand('DELIVERY_ID', $systemShipment->getField('DELIVERY_ID'));
                 }
             }
         }
     }
     if ($action != EventActions::UPDATE) {
         return $result;
     }
     // PRICE_DELIVERY, ALLOW_DELIVERY, DEDUCTED, MARKED
     // CANCELED, DELIVERY_ID
     if ($name == "ALLOW_DELIVERY") {
         if ($this->isCanceled()) {
             $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'));
             return $result;
         }
         $r = $shipment->deliver();
         if ($r->isSuccess()) {
             $event = new Main\Event('sale', EventActions::EVENT_ON_SHIPMENT_DELIVER, array('ENTITY' => $shipment));
             $event->send();
         } else {
             $result->addErrors($r->getErrors());
         }
         if (Configuration::getProductReservationCondition() == Configuration::RESERVE_ON_ALLOW_DELIVERY) {
             if ($value == "Y") {
                 /** @var Result $r */
                 $r = $shipment->tryReserve();
                 if (!$r->isSuccess()) {
                     $shipment->setField('MARKED', 'Y');
                     if (is_array($r->getErrorMessages())) {
                         $oldErrorText = $shipment->getField('REASON_MARKED');
                         foreach ($r->getErrorMessages() as $error) {
                             $oldErrorText .= (strval($oldErrorText) != '' ? "\n" : "") . $error;
                         }
                         $shipment->setField('REASON_MARKED', $oldErrorText);
                     }
                     $result->addErrors($r->getErrors());
                 }
             } else {
                 if (!$shipment->isShipped()) {
                     /** @var Result $r */
                     $r = $shipment->tryUnreserve();
                     if (!$r->isSuccess()) {
                         $result->addErrors($r->getErrors());
                     }
                 }
             }
             if (!$result->isSuccess()) {
                 return $result;
             }
         }
         if ($oldValue == "N") {
             $orderStatus = Config\Option::get('sale', 'status_on_allow_delivery', '');
             if (strval($orderStatus) != '') {
                 if ($USER && $USER->isAuthorized()) {
                     $statusesList = OrderStatus::getAllowedUserStatuses($USER->getID(), $this->getField('STATUS_ID'));
                 } else {
                     $statusesList = OrderStatus::getAllStatuses();
                 }
                 if ($this->getField('STATUS_ID') != $orderStatus && array_key_exists($orderStatus, $statusesList)) {
                     /** @var Result $r */
                     $r = $this->setField('STATUS_ID', $orderStatus);
                     if (!$r->isSuccess()) {
                         $result->addErrors($r->getErrors());
                         return $result;
                     }
                 }
             }
         }
         if (Configuration::needShipOnAllowDelivery() && $value == "Y") {
             $shipment->setField("DEDUCTED", "Y");
         }
         /** @var ShipmentCollection $shipmentCollection */
         if (!($shipmentCollection = $this->getShipmentCollection())) {
             throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
         }
         if ($shipmentCollection->isAllowDelivery() && $this->getField('ALLOW_DELIVERY') == 'N') {
             $this->setFieldNoDemand('DATE_ALLOW_DELIVERY', new Type\DateTime());
         }
         $this->setFieldNoDemand('ALLOW_DELIVERY', $shipmentCollection->isAllowDelivery() ? "Y" : "N");
     } elseif ($name == "DEDUCTED") {
         if ($this->isCanceled()) {
             $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_SHIPMENT_ORDER_CANCELED'), 'SALE_ORDER_SHIPMENT_ORDER_CANCELED'));
             return $result;
         }
         if (Configuration::getProductReservationCondition() == Configuration::RESERVE_ON_SHIP) {
             if ($value == "Y") {
                 /** @var Result $r */
                 $r = $shipment->tryReserve();
                 if (!$r->isSuccess()) {
                     $shipment->setField('MARKED', 'Y');
                     if (is_array($r->getErrorMessages())) {
                         $oldErrorText = $shipment->getField('REASON_MARKED');
                         foreach ($r->getErrorMessages() as $error) {
                             $oldErrorText .= (strval($oldErrorText) != '' ? "\n" : "") . $error;
                         }
                         $shipment->setField('REASON_MARKED', $oldErrorText);
                     }
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
             } else {
                 $shipment->tryUnreserve();
             }
         }
         if ($value == "Y") {
             /** @var Result $r */
             $r = $shipment->tryShip();
             if (!$r->isSuccess()) {
                 $shipment->setField('MARKED', 'Y');
                 if (is_array($r->getErrorMessages())) {
                     $oldErrorText = $shipment->getField('REASON_MARKED');
                     foreach ($r->getErrorMessages() as $error) {
                         $oldErrorText .= (strval($oldErrorText) != '' ? "\n" : "") . $error;
                     }
                     $shipment->setField('REASON_MARKED', $oldErrorText);
                 }
                 $result->addErrors($r->getErrors());
                 return $result;
             }
         } elseif ($oldValue == 'Y') {
             /** @var Result $r */
             $r = $shipment->tryUnship();
             if (!$r->isSuccess()) {
                 /** @var Result $resultShipment */
                 $resultShipment = $shipment->setField('MARKED', 'Y');
                 if (!$resultShipment->isSuccess()) {
                     $result->addErrors($r->getErrors());
                 }
                 if (is_array($r->getErrorMessages())) {
                     $oldErrorText = $shipment->getField('REASON_MARKED');
                     foreach ($r->getErrorMessages() as $error) {
                         $oldErrorText .= (strval($oldErrorText) != '' ? "\n" : "") . $error;
                     }
                     /** @var Result $resultShipment */
                     $resultShipment = $shipment->setField('REASON_MARKED', $oldErrorText);
                     if (!$resultShipment->isSuccess()) {
                         $result->addErrors($r->getErrors());
                     }
                 }
                 $result->addErrors($r->getErrors());
                 return $result;
             }
         }
         /** @var ShipmentCollection $shipmentCollection */
         $shipmentCollection = $shipment->getCollection();
         $this->setFieldNoDemand('DEDUCTED', $shipmentCollection->isShipped() ? "Y" : "N");
         if ($shipmentCollection->isShipped()) {
             if (strval($shipment->getField('DATE_DEDUCTED')) != '') {
                 $this->setFieldNoDemand('DATE_DEDUCTED', $shipment->getField('DATE_DEDUCTED'));
             }
             if (strval($shipment->getField('EMP_DEDUCTED_ID')) != '') {
                 $this->setFieldNoDemand('EMP_DEDUCTED_ID', $shipment->getField('EMP_DEDUCTED_ID'));
             }
         }
     } elseif ($name == "MARKED") {
         if ($value == "Y") {
             /** @var Result $r */
             $r = $this->setField('MARKED', 'Y');
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     } elseif ($name == "REASON_MARKED") {
         if (!empty($value)) {
             $orderReasonMarked = $this->getField('REASON_MARKED');
             if (is_array($value)) {
                 $newOrderReasonMarked = '';
                 foreach ($value as $err) {
                     $newOrderReasonMarked .= (strval($newOrderReasonMarked) != '' ? "\n" : "") . $err;
                 }
             } else {
                 $newOrderReasonMarked = $value;
             }
             /** @var Result $r */
             $r = $this->setField('REASON_MARKED', $orderReasonMarked . (strval($orderReasonMarked) != '' ? "\n" : "") . $newOrderReasonMarked);
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     } elseif ($name == "BASE_PRICE_DELIVERY") {
         if ($this->isCanceled()) {
             $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
             return $result;
         }
         /** @var ShipmentCollection $shipmentCollection */
         if (!($shipmentCollection = $shipment->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
         }
         $discount = $this->getDiscount();
         $discount->setCalculateShipments($shipment);
         $r = $shipment->setField('PRICE_DELIVERY', $value);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     } elseif ($name == "PRICE_DELIVERY") {
         if ($this->isCanceled()) {
             $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
             return $result;
         }
         /** @var ShipmentCollection $shipmentCollection */
         if (!($shipmentCollection = $shipment->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
         }
         $this->setFieldNoDemand("PRICE_DELIVERY", $this->getField("PRICE_DELIVERY") - $oldValue + $value);
         /** @var Result $r */
         $r = $this->setField("PRICE", $this->getField("PRICE") - $oldValue + $value);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     } elseif ($name == "DELIVERY_ID") {
         if ($shipment->isSystem() || intval($shipment->getField('DELIVERY_ID')) <= 0) {
             return $result;
         }
         $this->setFieldNoDemand('DELIVERY_ID', $shipment->getField('DELIVERY_ID'));
     }
     return $result;
 }
예제 #3
0
 /**
  * @param Shipment $shipment
  * @param array $shippedList
  * @throws NotSupportedException
  * @throws SystemException
  */
 private function reverseShipment(Shipment $shipment, array $shippedList)
 {
     $needShip = $shipment->needShip();
     $correct = null;
     $shipmentItemCollection = $shipment->getShipmentItemCollection();
     $basketList = static::getBasketFromShipmentItemCollection($shipmentItemCollection);
     $bundleIndexList = static::getBundleIndexFromShipmentItemCollection($shipmentItemCollection);
     $basketProviderMap = static::createProviderBasketMap($basketList, array('QUANTITY', 'RESERVED'));
     $basketProviderList = static::redistributeToProviders($basketProviderMap);
     if (Configuration::useStoreControl()) {
         /** @var Result $r */
         $r = static::getStoreDataFromShipmentItemCollection($shipmentItemCollection);
     }
     if (!empty($basketProviderList)) {
         foreach ($basketProviderList as $provider => $providerBasketItemList) {
             if ($provider instanceof Provider) {
                 throw new NotSupportedException('provider not supported');
             } elseif ($provider && array_key_exists("IBXSaleProductProvider", class_implements($provider))) {
                 foreach ($providerBasketItemList as $providerBasketItem) {
                     if ($providerBasketItem['BASKET_ITEM']->isBundleParent()) {
                         continue;
                     }
                     $basketCode = $providerBasketItem['BASKET_CODE'];
                     if (!isset($shippedList[$basketCode])) {
                         continue;
                     }
                     if (!$providerBasketItem['BASKET_ITEM']->isEmptyItem()) {
                         $data = array("BASKET_ITEM" => $providerBasketItem['BASKET_ITEM'], "PRODUCT_ID" => $providerBasketItem['PRODUCT_ID'], "QUANTITY" => $providerBasketItem['QUANTITY'], "PRODUCT_RESERVED" => $providerBasketItem['RESERVED'], 'UNDO_DEDUCTION' => $needShip ? 'Y' : 'N', 'EMULATE' => 'N');
                         if (Configuration::useStoreControl() && !empty($storeData) && is_array($storeData) && isset($storeData[$providerBasketItem['BASKET_CODE']])) {
                             $data['STORE_DATA'] = $storeData[$providerBasketItem['BASKET_CODE']];
                             $barcodeReverseList = array();
                             if (!empty($shippedList[$basketCode]['BARCODE']) && is_array($shippedList[$basketCode]['BARCODE'])) {
                                 foreach ($shippedList[$basketCode]['BARCODE'] as $barcodeValue => $barcodeShipped) {
                                     if ($barcodeShipped === true) {
                                         $barcodeReverseList[] = $barcodeValue;
                                     }
                                 }
                                 foreach ($data['STORE_DATA'] as $storeId => $barcodeData) {
                                     if (!empty($barcodeData['BARCODE']) && is_array($barcodeData['BARCODE'])) {
                                         if (empty($barcodeReverseList)) {
                                             $data['STORE_DATA'][$storeId]['BARCODE'] = array();
                                         } else {
                                             foreach ($barcodeData['BARCODE'] as $barcodeId => $barcodeValue) {
                                                 if (!in_array($barcodeValue, $barcodeReverseList)) {
                                                     unset($data['STORE_DATA'][$storeId]['BARCODE'][$barcodeId]);
                                                     $data['STORE_DATA'][$storeId]['QUANTITY'] -= 1;
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         $resultProductData = $provider::DeductProduct($data);
                     } else {
                         $resultProductData['RESULT'] = true;
                     }
                     $result[$providerBasketItem['BASKET_CODE']] = $resultProductData;
                     if (isset($resultProductData['RESULT']) && $resultProductData['RESULT'] === true) {
                         $correct = true;
                     }
                 }
             }
         }
     }
     if ($correct === true) {
         $shipment->setField('DEDUCTED', $needShip ? 'N' : 'Y');
     }
     if (!empty($result) && !empty($bundleIndexList) && is_array($bundleIndexList)) {
         foreach ($bundleIndexList as $bundleParentBasketCode => $bundleChildList) {
             $tryShipmentBundle = false;
             foreach ($bundleChildList as $bundleChildBasketCode) {
                 if (isset($result[$bundleChildBasketCode]) && $result[$bundleChildBasketCode]['RESULT'] === true) {
                     $tryShipmentBundle = true;
                 } else {
                     $tryShipmentBundle = false;
                     break;
                 }
             }
             $result[$bundleParentBasketCode] = array('RESULT' => $tryShipmentBundle);
         }
     }
 }