public function checkByShipment(\Bitrix\Sale\Shipment $shipment, array $restrictionParams, $deliveryId = 0) { if (empty($restrictionParams)) { return true; } $siteId = $shipment->getCollection()->getOrder()->getSiteId(); return $this->check($siteId, $restrictionParams, $deliveryId); }
/** * @param \Bitrix\Sale\Shipment $shipment * @param string $jsObjName * @param string $idPrefix * @throws \Bitrix\Main\ArgumentNullException */ public function __construct($shipment, $jsObjName = "", $idPrefix = "") { self::$useStoreControl = Option::get('catalog', 'default_use_store_control', 'N') == 'Y'; $order = $shipment->getCollection()->getOrder(); parent::__construct($order, $jsObjName, $idPrefix); $this->shipment = $shipment; $this->data = array(); }
protected function calculateConcrete(\Bitrix\Sale\Shipment $shipment = null) { $result = new CalculationResult(); $price = $this->config["MAIN"]["PRICE"]; if ($shipment && \Bitrix\Main\Loader::includeModule('currency')) { $rates = new \CCurrencyRates(); $currency = $this->currency; $shipmentCurrency = $shipment->getCollection()->getOrder()->getCurrency(); $price = $rates->convertCurrency($price, $currency, $shipmentCurrency); } $result->setDeliveryPrice(roundEx($price, SALE_VALUE_PRECISION)); $result->setPeriodDescription($this->getPeriodText()); return $result; }
public function checkByShipment(\Bitrix\Sale\Shipment $shipment, array $restrictionParams, $deliveryId = 0) { if (intval($deliveryId) <= 0) { return true; } /** @var \Bitrix\Sale\Order $order */ $order = $shipment->getCollection()->getOrder(); if (!($props = $order->getPropertyCollection())) { return true; } if (!($locationProp = $props->getDeliveryLocation())) { return true; } if (!($locationCode = $locationProp->getValue())) { return true; } return $this->check($locationCode, $restrictionParams, $deliveryId); }
public function checkByShipment(\Bitrix\Sale\Shipment $shipment, array $restrictionParams, $deliveryId = 0) { if (intval($deliveryId) <= 0) { return true; } $paymentsCount = 0; $paySystemId = 0; /** @var \Bitrix\Sale\Payment $payment */ foreach ($shipment->getCollection()->getOrder()->getPaymentCollection() as $payment) { if ($payment->getId() != PaySystemInner::getId()) { $paymentsCount++; $paySystemId = $payment->getPaymentSystemId(); } } if ($paymentsCount <= 0 || $paymentsCount > 1 || $paySystemId <= 0) { return true; } return $this->check($paySystemId, $restrictionParams, $deliveryId); }
/** * @param \Bitrix\Sale\Shipment|null $shipment * @return CalculationResult * @throws \Bitrix\Main\ArgumentException */ protected function calculateConcrete(\Bitrix\Sale\Shipment $shipment = null) { $result = new CalculationResult(); $result->setDeliveryPrice(roundEx($this->config["MAIN"]["0"], SALE_VALUE_PRECISION)); $order = $shipment->getCollection()->getOrder(); if (!($props = $order->getPropertyCollection())) { return $result; } if (!($locationProp = $props->getDeliveryLocation())) { return $result; } if (!($locationCode = $locationProp->getValue())) { return $result; } $res = \Bitrix\Sale\Location\LocationTable::getList(array('runtime' => array(new \Bitrix\Main\Entity\ReferenceField('PARENT_GROUP', '\\Bitrix\\Sale\\Location\\GroupLocationTable', array('=this.PARENTS.ID' => 'ref.LOCATION_ID'), array("join_type" => 'inner'))), 'filter' => array('=CODE' => $locationCode), 'select' => array('CLOSEST_GROUP_ID' => 'PARENT_GROUP.LOCATION_GROUP_ID'), 'order' => array('PARENTS.DEPTH_LEVEL' => 'desc'), 'limit' => 1)); if ($group = $res->fetch()) { if (isset($this->config["MAIN"][$group['CLOSEST_GROUP_ID']])) { $result->setDeliveryPrice(roundEx(floatval($this->config["MAIN"][$group['CLOSEST_GROUP_ID']]), SALE_VALUE_PRECISION)); } } return $result; }
/** * @param $profileId * @param array $profileConfig * @param \Bitrix\Sale\Shipment $shipment * @return CalculationResult * @throws SystemException * @throws \Bitrix\Main\LoaderException */ public function calculateProfile($profileId, array $profileConfig, \Bitrix\Sale\Shipment $shipment) { global $APPLICATION; $result = new CalculationResult(); $step = 0; $tmp = false; /** @var ShipmentCollection $shipmentCollection */ $shipmentCollection = $shipment->getCollection(); /** @var Order $order */ $order = $shipmentCollection->getOrder(); $shipmentCurrency = $order->getCurrency(); if (!Loader::includeModule('currency')) { throw new SystemException("Can't include module \"Currency\""); } $calculator = $this->getCalcultor(); if ($calculator !== false) { if ($res = call_user_func($calculator, $profileId, $profileConfig["CONFIG"], self::convertNewOrderToOld($shipment), ++$step, $tmp)) { if (is_array($res)) { if ($res["RESULT"] == "OK") { if (isset($res["TEXT"])) { $result->setDescription($res["TEXT"]); } if (isset($res["VALUE"])) { $result->setDeliveryPrice(floatval($res["VALUE"])); } if (isset($res["TRANSIT"])) { $result->setPeriodDescription($res["TRANSIT"]); } } else { if (isset($res["TEXT"]) && strlen($res["TEXT"]) > 0) { $result->addError(new EntityError($res["TEXT"], 'DELIVERY_CALCULATION')); } else { $result->addError(new EntityError(Loc::getMessage('SALE_DLVR_HANDL_AUT_ERROR_CALCULATION'), 'DELIVERY_CALCULATION')); } } } elseif (is_numeric($res)) { $result->setDeliveryPrice(floatval($res)); } } else { if ($ex = $APPLICATION->getException()) { $result->addError(new EntityError($ex->getString(), 'DELIVERY_CALCULATION')); } else { $result->setDeliveryPrice(0); } } if ($result->isSuccess() && $this->currency != $shipmentCurrency) { $result->setDeliveryPrice(\CCurrencyRates::convertCurrency($result->getPrice(), $this->currency, $shipmentCurrency)); } } $result->setDeliveryPrice($result->getPrice() + $this->getMarginPrice($shipment)); return $result; }
/** * 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; }
/** * Calculates delivery price * @param \Bitrix\Sale\Shipment $shipment. * @return \Bitrix\Sale\Delivery\CalculationResult */ public function calculate(\Bitrix\Sale\Shipment $shipment = null) { if ($shipment && !$shipment->getCollection()) { return false; } $result = $this->calculateConcrete($shipment); if ($shipment) { $this->extraServices->setValues($shipment->getExtraServices()); $extraServicePrice = $this->extraServices->getTotalCost(); if (floatval($extraServicePrice) > 0) { $result->setExtraServicesPrice($extraServicePrice); } } $eventParams = array("RESULT" => $result, "SHIPMENT" => $shipment); $event = new Event('sale', self::EVENT_ON_CALCULATE, $eventParams); $event->send(); $resultList = $event->getResults(); if (is_array($resultList) && !empty($resultList)) { foreach ($resultList as &$eventResult) { if ($eventResult->getType() != EventResult::SUCCESS) { continue; } $params = $eventResult->getParameters(); if (isset($params["RESULT"])) { $result = $params["RESULT"]; } } } return $result; }
/** * @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); }
/** * @param Shipment $shipment * @return array * @throws NotSupportedException * @throws ObjectNotFoundException */ public static function syncReservedQuantity(Shipment $shipment) { $result = array(); /** @var ShipmentCollection $shipmentCollection */ $shipmentCollection = $shipment->getCollection(); $systemShipment = $shipmentCollection->getSystemShipment(); $shipmentItemCollection = $shipment->getShipmentItemCollection(); /** @var ShipmentItem $shipmentItem */ foreach ($shipmentItemCollection as $shipmentIndex => $shipmentItem) { $basketCode = $shipmentItem->getBasketCode(); if (!array_key_exists($basketCode, static::$poolProductQuantity)) { continue; } $reserveQuantity = static::$poolProductQuantity[$basketCode]; if ($reserveQuantity >= 0) { $basketCode = $shipmentItem->getBasketCode(); if (floatval($shipmentItem->getQuantity()) == floatval($shipmentItem->getReservedQuantity())) { continue; } $needReserved = floatval($shipmentItem->getQuantity()) - floatval($shipmentItem->getReservedQuantity()); $resultQuantity = static::reserveShipmentItem($shipmentItem, $reserveQuantity - $needReserved > 0 || $reserveQuantity == 0 ? $needReserved : $reserveQuantity); if (!$shipment->isSystem()) { $shipmentReservedQuantity = floatval($shipmentItem->getReservedQuantity()) + $resultQuantity; if (floatval($shipmentReservedQuantity) != floatval($shipmentItem->getQuantity())) { /** @var ShipmentItemCollection $systemShipmentItemCollection */ $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection(); /** @var ShipmentItem $systemShipmentItem */ if ($systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($shipmentItem->getBasketCode())) { $needMoreReserved = $shipmentItem->getQuantity() - $shipmentReservedQuantity; $quantityNeedReserve = $systemShipmentItem->getReservedQuantity() - $needMoreReserved; $quantityNeedReserve = $quantityNeedReserve <= 0 ? $systemShipmentItem->getReservedQuantity() : $needMoreReserved; if (static::unreserveShipmentItem($systemShipmentItem, $quantityNeedReserve)) { $resultNeedQuantity = static::reserveShipmentItem($shipmentItem, $quantityNeedReserve); $resultQuantity = $resultQuantity + $resultNeedQuantity; } } } } } else { $resultQuantity = static::unreserveShipmentItem($shipmentItem, $shipmentItem->getReservedQuantity()); } static::$poolProductQuantity[$basketCode] -= $resultQuantity; $result[$shipmentItem->getBasketCode()] = $resultQuantity; if (static::$poolProductQuantity[$basketCode] == 0) { unset(static::$poolProductQuantity[$basketCode]); } } return $result; }
/** * @param Shipment $shipment * @return array Old order. * @throws \Bitrix\Main\ArgumentNullException */ public static function convertOrderNewToOld(\Bitrix\Sale\Shipment $shipment) { /** @var \Bitrix\Sale\ShipmentCollection $shipmentCollection */ $shipmentCollection = $shipment->getCollection(); /** @var \Bitrix\Sale\Order $newOrder */ $newOrder = $shipmentCollection->getOrder(); $props = $newOrder->getPropertyCollection(); $oldOrder = array(); if (intval($newOrder->getId()) > 0) { $oldOrder["ID"] = $newOrder->getId(); } /** @var \Bitrix\Sale\Basket $basket */ if ($collection = $shipment->getShipmentItemCollection()) { $oldOrder["PRICE"] = $collection->getPrice(); } $oldOrder["WEIGHT"] = 0; $oldOrder["LOCATION_FROM"] = \Bitrix\Main\Config\Option::get('sale', 'location', "", $newOrder->getSiteId()); $oldOrder["SITE_ID"] = $newOrder->getSiteId(); $oldOrder["PERSON_TYPE_ID"] = $newOrder->getPersonTypeId(); $oldOrder["CURRENCY"] = $newOrder->getCurrency(); $loc = $props->getDeliveryLocation(); $oldOrder["LOCATION_TO"] = !!$loc ? $loc->getValue() : ""; $loc = $props->getDeliveryLocationZip(); $oldOrder["LOCATION_ZIP"] = !!$loc ? $loc->getValue() : ""; $oldOrder["ITEMS"] = array(); /** @var \Bitrix\Sale\ShipmentItem $shipmentItem */ foreach ($shipment->getShipmentItemCollection() as $shipmentItem) { $basketItem = $shipmentItem->getBasketItem(); if (!$basketItem) { continue; } $itemFieldValues = $basketItem->getFieldValues(); $itemFieldValues["QUANTITY"] = $shipmentItem->getField("QUANTITY"); if (!empty($itemFieldValues["DIMENSIONS"]) && is_string($itemFieldValues["DIMENSIONS"])) { $itemFieldValues["DIMENSIONS"] = unserialize($itemFieldValues["DIMENSIONS"]); } $oldOrder["ITEMS"][] = $itemFieldValues; $itemWeight = floatval($basketItem->getField("WEIGHT")); if ($itemWeight > 0) { $oldOrder["WEIGHT"] += $itemWeight * floatval($basketItem->getField("QUANTITY")); } } return $oldOrder; }