Пример #1
0
 /**
  * @return Entity\AddResult|Entity\UpdateResult
  * @throws Main\ArgumentOutOfRangeException
  * @throws Main\ObjectNotFoundException
  * @throws \Exception
  */
 public function save()
 {
     $id = $this->getId();
     $fields = $this->fields->getValues();
     if ($id > 0) {
         $fields = $this->fields->getChangedValues();
         if (!empty($fields) && is_array($fields)) {
             //				$fields['DATE_UPDATE'] = new Main\Type\DateTime();
             $r = Internals\ShipmentTable::update($id, $fields);
             if (!$r->isSuccess()) {
                 return $r;
             }
         }
         $result = new Entity\UpdateResult();
         if (!empty($fields['TRACKING_NUMBER'])) {
             $oldEntityValues = $this->fields->getOriginalValues();
             /** @var Main\Event $event */
             $event = new Main\Event('sale', EventActions::EVENT_ON_SHIPMENT_TRACKING_NUMBER_CHANGE, array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
             $event->send();
         }
     } else {
         $fields['ORDER_ID'] = $this->getParentOrderId();
         $fields['DATE_INSERT'] = new Main\Type\DateTime();
         $fields['SYSTEM'] = $fields['SYSTEM'] ? 'Y' : 'N';
         $r = Internals\ShipmentTable::add($fields);
         if (!$r->isSuccess()) {
             return $r;
         }
         $id = $r->getId();
         $this->setFieldNoDemand('ID', $id);
         $result = new Entity\AddResult();
         /** @var ShipmentItemCollection $shipmentItemCollection */
         if (!($shipmentItemCollection = $this->getShipmentItemCollection())) {
             throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
         }
         /** @var Shipment $shipment */
         if (!($shipment = $shipmentItemCollection->getShipment())) {
             throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
         }
         /** @var ShipmentCollection $shipmentCollection */
         if (!($shipmentCollection = $shipment->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
         }
         /** @var Order $order */
         if (!($order = $shipmentCollection->getOrder())) {
             throw new Main\ObjectNotFoundException('Entity "Order" not found');
         }
         if ($order->getId() > 0 && !$this->isSystem()) {
             OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_ADDED', $id, $this);
         }
     }
     if ($result->isSuccess() && !$this->isSystem()) {
         $this->saveExtraServices();
         $this->saveStoreId();
     }
     /** @var ShipmentItemCollection $shipmentItemCollection */
     if (!($shipmentItemCollection = $this->getShipmentItemCollection())) {
         throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
     }
     $r = $shipmentItemCollection->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     if ($result->isSuccess()) {
         /** @var Shipment $shipment */
         if (!($shipment = $shipmentItemCollection->getShipment())) {
             throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
         }
         /** @var ShipmentCollection $shipmentCollection */
         if (!($shipmentCollection = $shipment->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
         }
         /** @var Order $order */
         if (!($order = $shipmentCollection->getOrder())) {
             throw new Main\ObjectNotFoundException('Entity "Order" not found');
         }
         if (!$this->isSystem()) {
             OrderHistory::collectEntityFields('SHIPMENT', $order->getId(), $id);
         }
     }
     return $result;
 }
Пример #2
0
 /**
  * @return Entity\AddResult|Entity\UpdateResult|Result|mixed
  * @throws Main\ArgumentOutOfRangeException
  */
 public function save()
 {
     global $USER;
     $result = new Result();
     $id = $this->getId();
     $this->isNew = $id == 0;
     /** @var array $oldEntityValues */
     $oldEntityValues = $this->fields->getOriginalValues();
     /** @var Main\Entity\Event $event */
     $event = new Main\Event('sale', EventActions::EVENT_ON_ORDER_BEFORE_SAVED, array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
     $event->send();
     if ($event->getResults()) {
         $result = new Result();
         /** @var Main\EventResult $eventResult */
         foreach ($event->getResults() as $eventResult) {
             if ($eventResult->getType() == Main\EventResult::ERROR) {
                 $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR'), 'SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR');
                 if ($eventResultData = $eventResult->getParameters()) {
                     if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) {
                         $errorMsg = $eventResultData['ERROR'];
                     }
                 }
                 $result->addError($errorMsg);
             }
         }
         if (!$result->isSuccess()) {
             return $result;
         }
     }
     $r = Provider::onOrderSave($this);
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
         return $result;
     }
     $fields = $this->fields->getValues();
     if ($id > 0) {
         $fields = $this->fields->getChangedValues();
         if ($this->isChanged()) {
             $fields['DATE_UPDATE'] = new Type\DateTime();
             $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']);
             $fields['VERSION'] = intval($this->getField('VERSION')) + 1;
             $this->setFieldNoDemand('VERSION', $fields['VERSION']);
             $fields['UPDATED_1C'] = 'N';
             $this->setFieldNoDemand('UPDATED_1C', $fields['UPDATED_1C']);
         }
         if (!empty($fields) && is_array($fields)) {
             $result = Internals\OrderTable::update($id, $fields);
             if (!$result->isSuccess()) {
                 return $result;
             }
         } else {
             $result = new Entity\UpdateResult();
         }
     } else {
         $fields['DATE_UPDATE'] = $fields['DATE_INSERT'] = new Type\DateTime();
         $this->setFieldNoDemand('DATE_INSERT', $fields['DATE_INSERT']);
         $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']);
         if ($USER->isAuthorized()) {
             $fields['CREATED_BY'] = $USER->getID();
             $this->setFieldNoDemand('CREATED_BY', $fields['CREATED_BY']);
         }
         if (!isset($fields['STATUS_ID']) || strval($fields['STATUS_ID']) == '') {
             $orderStatus = OrderStatus::getInitialStatus();
             if (!empty($orderStatus) && !is_array($orderStatus)) {
                 $fields['STATUS_ID'] = $orderStatus;
                 $this->setFieldNoDemand('STATUS_ID', $fields['STATUS_ID']);
             }
         }
         if (isset($fields['STATUS_ID']) && strval($fields['STATUS_ID']) != '') {
             if (!isset($fields['DATE_STATUS']) || strval($fields['DATE_STATUS']) == '') {
                 $fields['DATE_STATUS'] = new Type\DateTime();
                 $this->setFieldNoDemand('DATE_STATUS', $fields['DATE_STATUS']);
             }
             if ((!isset($fields['EMP_STATUS_ID']) || (int) $fields['EMP_STATUS_ID'] <= 0) && $USER->isAuthorized()) {
                 $fields['EMP_STATUS_ID'] = $USER->getID();
                 $this->setFieldNoDemand('EMP_STATUS_ID', $fields['EMP_STATUS_ID']);
             }
         }
         $result = Internals\OrderTable::add($fields);
         if (!$result->isSuccess()) {
             return $result;
         }
         $id = $result->getId();
         $this->setFieldNoDemand('ID', $id);
         /** @var Result $r */
         $r = static::setAccountNumber($id);
         if ($r->isSuccess()) {
             if ($accountData = $r->getData()) {
                 if (array_key_exists('ACCOUNT_NUMBER', $accountData)) {
                     $this->setField('ACCOUNT_NUMBER', $accountData['ACCOUNT_NUMBER']);
                 }
             }
         }
         OrderHistory::addAction('ORDER', $id, 'ORDER_ADDED', $id, $this);
     }
     if ($eventName = static::getEntityEventName()) {
         $oldEntityValues = $this->fields->getOriginalValues();
         if (!empty($oldEntityValues)) {
             /** @var Main\Event $event */
             $event = new Main\Event('sale', 'On' . $eventName . 'EntitySaved', array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
             $event->send();
         }
     }
     OrderHistory::collectEntityFields('ORDER', $id, $id);
     $this->fields->clearChanged();
     /** @var Basket $basket */
     $basket = $this->getBasket();
     /** @var Result $r */
     $r = $basket->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     /** @var PaymentCollection $paymentCollection */
     $paymentCollection = $this->getPaymentCollection();
     /** @var Result $r */
     $r = $paymentCollection->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     // user budget
     Internals\UserBudgetPool::onUserBudgetSave($this->getUserId());
     /** @var ShipmentCollection $shipmentCollection */
     $shipmentCollection = $this->getShipmentCollection();
     /** @var Result $r */
     $r = $shipmentCollection->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     /** @var Tax $tax */
     $tax = $this->getTax();
     /** @var Result $r */
     $r = $tax->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     /** @var PropertyValueCollection $propertyCollection */
     $propertyCollection = $this->getPropertyCollection();
     /** @var Result $r */
     $r = $propertyCollection->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     /** @var Discount $discount */
     $discount = $this->getDiscount();
     /** @var Result $r */
     $r = $discount->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     /** @var array $oldEntityValues */
     $oldEntityValues = $this->fields->getOriginalValues();
     $event = new Main\Event('sale', EventActions::EVENT_ON_ORDER_SAVED, array('ENTITY' => $this, 'IS_NEW' => $this->isNew, 'VALUES' => $oldEntityValues));
     $event->send();
     if (($eventList = Internals\EventsPool::getEvents($this)) && !empty($eventList) && is_array($eventList)) {
         foreach ($eventList as $eventName => $eventData) {
             $event = new Main\Event('sale', $eventName, $eventData);
             $event->send();
         }
         Internals\EventsPool::resetEvents($this);
     }
     $this->isNew = false;
     return $result;
 }
Пример #3
0
 /**
  * @return Entity\Result|bool
  * @throws \Bitrix\Main\ArgumentException
  * @throws \Bitrix\Main\ArgumentNullException
  */
 public function save()
 {
     $result = new Result();
     /** @var Order $order */
     $order = $this->getOrder();
     $itemsFromDb = array();
     $filter = array();
     if (!$order) {
         /** @var Main\Entity\Event $event */
         $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_BEFORE_SAVED, array('ENTITY' => $this));
         $event->send();
         if ($event->getResults()) {
             $result = new Result();
             /** @var Main\EventResult $eventResult */
             foreach ($event->getResults() as $eventResult) {
                 if ($eventResult->getType() == Main\EventResult::ERROR) {
                     $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BEFORE_BASKET_SAVED'), 'SALE_EVENT_ON_BEFORE_BASKET_SAVED');
                     if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) {
                         $errorMsg = $eventResultData['ERROR'];
                     }
                     $result->addError($errorMsg);
                 }
             }
             if (!$result->isSuccess()) {
                 return $result;
             }
         }
     }
     $isNew = $order && $order->isNew() ? true : false;
     if ($order && !$isNew) {
         $filter['ORDER_ID'] = $order->getId();
     } else {
         if ($this->isLoadForFuserId()) {
             $filter = array('FUSER_ID' => $this->getFUserId(), 'ORDER_ID' => null, 'LID' => $this->getSiteId());
         }
         if ($isNew) {
             $fUserId = $this->getFUserId(true);
             if ($fUserId <= 0) {
                 $userId = $order->getUserId();
                 if (intval($userId) > 0) {
                     $fUserId = Fuser::getIdByUserId($userId);
                     if ($fUserId > 0) {
                         $this->setFUserId($fUserId);
                     }
                 }
             }
         }
     }
     if (!empty($filter)) {
         $itemsFromDbList = Internals\BasketTable::getList(array("filter" => $filter, "select" => array("ID", 'TYPE', 'SET_PARENT_ID', 'PRODUCT_ID', 'NAME', 'QUANTITY')));
         while ($itemsFromDbItem = $itemsFromDbList->fetch()) {
             if (intval($itemsFromDbItem['SET_PARENT_ID']) > 0 && intval($itemsFromDbItem['SET_PARENT_ID']) != $itemsFromDbItem['ID']) {
                 continue;
             }
             $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
         }
     }
     /** @var BasketItem $basketItem */
     foreach ($this->collection as $index => $basketItem) {
         $r = $basketItem->save();
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
         if (isset($itemsFromDb[$basketItem->getId()]) && $basketItem->getQuantity() > 0) {
             unset($itemsFromDb[$basketItem->getId()]);
         }
     }
     if (!empty($filter)) {
         foreach ($itemsFromDb as $k => $v) {
             if ($v['TYPE'] == static::TYPE_SET) {
                 Internals\BasketTable::deleteBundle($k);
             } else {
                 Internals\BasketTable::deleteWithItems($k);
             }
             /** @var Order $order */
             if ($order && $order->getId() > 0) {
                 OrderHistory::addAction('BASKET', $order->getId(), 'BASKET_REMOVED', $k, null, array('NAME' => $v['NAME'], 'QUANTITY' => $v['QUANTITY'], 'PRODUCT_ID' => $v['PRODUCT_ID']));
             }
         }
     }
     if ($order && $order->getId() > 0) {
         OrderHistory::collectEntityFields('BASKET', $order->getId());
     }
     if (!$order) {
         /** @var Main\Entity\Event $event */
         $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_SAVED, array('ENTITY' => $this));
         $event->send();
         if ($event->getResults()) {
             $result = new Result();
             /** @var Main\EventResult $eventResult */
             foreach ($event->getResults() as $eventResult) {
                 if ($eventResult->getType() == Main\EventResult::ERROR) {
                     $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BASKET_SAVED'), 'SALE_EVENT_ON_BASKET_SAVED');
                     if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) {
                         $errorMsg = $eventResultData['ERROR'];
                     }
                     $result->addError($errorMsg);
                 }
             }
             if (!$result->isSuccess()) {
                 return $result;
             }
         }
     }
     return $result;
 }
Пример #4
0
 /**
  * @return Entity\AddResult|Entity\UpdateResult|Result|mixed
  * @throws Main\ArgumentOutOfRangeException
  */
 public function save()
 {
     global $USER, $CACHE_MANAGER;
     $result = new Result();
     $id = $this->getId();
     $this->isNew = $id == 0;
     if ($this->isNew) {
         $fields = $this->fields->getChangedValues();
         if (empty($fields['STATUS_ID'])) {
             /** @var Result $r */
             $r = $this->setField("STATUS_ID", OrderStatus::getInitialStatus());
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     /** @var array $oldEntityValues */
     $oldEntityValues = $this->fields->getOriginalValues();
     /** @var Main\Entity\Event $event */
     $event = new Main\Event('sale', EventActions::EVENT_ON_ORDER_BEFORE_SAVED, array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
     $event->send();
     if ($event->getResults()) {
         /** @var Main\EventResult $eventResult */
         foreach ($event->getResults() as $eventResult) {
             if ($eventResult->getType() == Main\EventResult::ERROR) {
                 $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR'), 'SALE_EVENT_ON_BEFORE_ORDER_SAVED_ERROR');
                 if ($eventResultData = $eventResult->getParameters()) {
                     if (isset($eventResultData) && $eventResultData instanceof ResultError) {
                         /** @var ResultError $errorMsg */
                         $errorMsg = $eventResultData;
                     }
                 }
                 $result->addError($errorMsg);
             }
         }
     }
     if (!$result->isSuccess()) {
         return $result;
     }
     $r = $this->verify();
     if (!$r->isSuccess()) {
         $oldErrorText = $this->getField('REASON_MARKED');
         /** @var ResultError $error */
         foreach ($r->getErrors() as $error) {
             if ($error instanceof ResultNotice) {
                 continue;
             } elseif ($error instanceof ResultWarning) {
                 $oldErrorText .= (strval($oldErrorText) != '' ? "\n" : "") . $error->getMessage();
                 continue;
             } else {
                 $result->addError($error);
             }
         }
         if (strval(trim($oldErrorText)) != '') {
             $this->setField('MARKED', "Y");
             $this->setField('REASON_MARKED', $oldErrorText);
         }
         if (!$result->isSuccess()) {
             return $result;
         }
     }
     $r = Provider::onOrderSave($this);
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
         return $result;
     }
     $fields = $this->fields->getValues();
     if ($id > 0) {
         $fields = $this->fields->getChangedValues();
         $isChanged = (bool) (!empty($fields));
         if ($this->isChanged()) {
             if (!isset($fields['DATE_UPDATE']) || strval($fields['DATE_UPDATE']) == '') {
                 $fields['DATE_UPDATE'] = new Type\DateTime();
                 $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']);
             }
             $fields['VERSION'] = intval($this->getField('VERSION')) + 1;
             $this->setFieldNoDemand('VERSION', $fields['VERSION']);
             if (array_key_exists('REASON_MARKED', $fields) && strlen($fields['REASON_MARKED']) > 255) {
                 $fields['REASON_MARKED'] = substr($fields['REASON_MARKED'], 0, 255);
             }
         }
         if (!empty($fields) && is_array($fields)) {
             $r = Internals\OrderTable::update($id, $fields);
             if (!$r->isSuccess()) {
                 OrderHistory::addAction('ORDER', $id, 'ORDER_UPDATE_ERROR', $id, $this, array("ERROR" => $r->getErrorMessages()));
                 $result->addWarnings($r->getErrors());
                 return $result;
             }
             if ($resultData = $r->getData()) {
                 $result->setData($resultData);
             }
             OrderHistory::addAction('ORDER', $id, 'ORDER_UPDATED', $id, $this);
         }
     } else {
         $isChanged = true;
         if (!isset($fields['DATE_INSERT']) || strval($fields['DATE_INSERT']) == '') {
             $fields['DATE_INSERT'] = new Type\DateTime();
             $this->setFieldNoDemand('DATE_INSERT', $fields['DATE_INSERT']);
         }
         if (!isset($fields['DATE_UPDATE']) || strval($fields['DATE_UPDATE']) == '') {
             $fields['DATE_UPDATE'] = new Type\DateTime();
             $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']);
         }
         if ($USER->isAuthorized()) {
             $fields['CREATED_BY'] = $USER->getID();
             $this->setFieldNoDemand('CREATED_BY', $fields['CREATED_BY']);
         }
         if (!isset($fields['STATUS_ID']) || strval($fields['STATUS_ID']) == '') {
             $orderStatus = OrderStatus::getInitialStatus();
             if (!empty($orderStatus) && !is_array($orderStatus)) {
                 $fields['STATUS_ID'] = $orderStatus;
                 $this->setFieldNoDemand('STATUS_ID', $fields['STATUS_ID']);
             }
         }
         if (isset($fields['STATUS_ID']) && strval($fields['STATUS_ID']) != '') {
             if (!isset($fields['DATE_STATUS']) || strval($fields['DATE_STATUS']) == '') {
                 $fields['DATE_STATUS'] = new Type\DateTime();
                 $this->setFieldNoDemand('DATE_STATUS', $fields['DATE_STATUS']);
             }
             if ((!isset($fields['EMP_STATUS_ID']) || (int) $fields['EMP_STATUS_ID'] <= 0) && $USER->isAuthorized()) {
                 $fields['EMP_STATUS_ID'] = $USER->getID();
                 $this->setFieldNoDemand('EMP_STATUS_ID', $fields['EMP_STATUS_ID']);
             }
         }
         if (array_key_exists('REASON_MARKED', $fields) && strlen($fields['REASON_MARKED']) > 255) {
             $fields['REASON_MARKED'] = substr($fields['REASON_MARKED'], 0, 255);
         }
         $r = Internals\OrderTable::add($fields);
         if (!$r->isSuccess()) {
             $result->addWarnings($r->getErrors());
             return $result;
         }
         if ($resultData = $r->getData()) {
             $result->setData($resultData);
         }
         $id = $r->getId();
         $this->setFieldNoDemand('ID', $id);
         /** @var Result $r */
         $r = static::setAccountNumber($id);
         if ($r->isSuccess()) {
             if ($accountData = $r->getData()) {
                 if (array_key_exists('ACCOUNT_NUMBER', $accountData)) {
                     $this->setField('ACCOUNT_NUMBER', $accountData['ACCOUNT_NUMBER']);
                 }
             }
         }
         OrderHistory::addAction('ORDER', $id, 'ORDER_ADDED', $id, $this);
     }
     if ($id > 0) {
         $result->setId($id);
     }
     if ($eventName = static::getEntityEventName()) {
         $oldEntityValues = $this->fields->getOriginalValues();
         if (!empty($oldEntityValues)) {
             /** @var Main\Event $event */
             $event = new Main\Event('sale', 'On' . $eventName . 'EntitySaved', array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
             $event->send();
         }
     }
     $changeMeaningfulFields = array("PERSON_TYPE_ID", "CANCELED", "STATUS_ID", "MARKED", "PRICE", "SUM_PAID", "USER_ID", "EXTERNAL_ORDER");
     if ($isChanged) {
         $logFields = array();
         if (!$this->isNew) {
             $fields = $this->getFields();
             $originalValues = $fields->getOriginalValues();
             foreach ($originalValues as $originalFieldName => $originalFieldValue) {
                 if (in_array($originalFieldName, $changeMeaningfulFields) && $this->getField($originalFieldName) != $originalFieldValue) {
                     $logFields[$originalFieldName] = $this->getField($originalFieldName);
                     $logFields['OLD_' . $originalFieldName] = $originalFieldValue;
                 }
             }
             OrderHistory::addLog('ORDER', $id, "ORDER_UPDATE", $id, $this, $logFields, OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1);
         }
     }
     OrderHistory::collectEntityFields('ORDER', $id, $id);
     /** @var Basket $basket */
     $basket = $this->getBasket();
     /** @var Result $r */
     $r = $basket->save();
     if (!$r->isSuccess()) {
         $result->addWarnings($r->getErrors());
     }
     /** @var PaymentCollection $paymentCollection */
     $paymentCollection = $this->getPaymentCollection();
     /** @var Result $r */
     $r = $paymentCollection->save();
     if (!$r->isSuccess()) {
         $result->addWarnings($r->getErrors());
     }
     // user budget
     Internals\UserBudgetPool::onUserBudgetSave($this->getUserId());
     /** @var ShipmentCollection $shipmentCollection */
     $shipmentCollection = $this->getShipmentCollection();
     /** @var Result $r */
     $r = $shipmentCollection->save();
     if (!$r->isSuccess()) {
         $result->addWarnings($r->getErrors());
     }
     /** @var Tax $tax */
     $tax = $this->getTax();
     /** @var Result $r */
     $r = $tax->save();
     if (!$r->isSuccess()) {
         $result->addWarnings($r->getErrors());
     }
     /** @var PropertyValueCollection $propertyCollection */
     $propertyCollection = $this->getPropertyCollection();
     /** @var Result $r */
     $r = $propertyCollection->save();
     if (!$r->isSuccess()) {
         $result->addWarnings($r->getErrors());
     }
     /** @var Discount $discount */
     $discount = $this->getDiscount();
     /** @var Result $r */
     $r = $discount->save();
     if (!$r->isSuccess()) {
         $result->addWarnings($r->getErrors());
     }
     /** @var array $oldEntityValues */
     $oldEntityValues = $this->fields->getOriginalValues();
     OrderHistory::addLog('ORDER', $this->getId(), 'ORDER_EVENT_ON_ORDER_SAVED', null, null, array(), OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1);
     $event = new Main\Event('sale', EventActions::EVENT_ON_ORDER_SAVED, array('ENTITY' => $this, 'IS_NEW' => $this->isNew, 'VALUES' => $oldEntityValues));
     $event->send();
     if (($eventList = Internals\EventsPool::getEvents($this)) && !empty($eventList) && is_array($eventList)) {
         foreach ($eventList as $eventName => $eventData) {
             $event = new Main\Event('sale', $eventName, $eventData);
             $event->send();
             Notify::callNotify($this, $eventName);
         }
         Internals\EventsPool::resetEvents($this);
     }
     Notify::callNotify($this, EventActions::EVENT_ON_ORDER_SAVED);
     if (!$result->isSuccess()) {
         $errorMsg = $this->getField('REASON_MARKED');
         $errorMsg .= (strval($errorMsg) != "" ? "\n" : "") . join("\n", $result->getErrors());
         $updateFields = array('MARKED' => 'Y', 'DATE_MARKED' => new Type\DateTime(), 'EMP_MARKED_ID' => $USER->getId(), 'REASON_MARKED' => $errorMsg);
         Internals\OrderTable::update($id, $updateFields);
         OrderHistory::addLog('ORDER', $this->getId(), 'ORDER_EVENT_ON_ORDER_SAVED_ERROR', null, null, array("ERROR" => $errorMsg), OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1);
     } else {
         if (defined("CACHED_b_sale_order") && ($this->isNew || $this->isChanged() && $fields["UPDATED_1C"] != "Y")) {
             $CACHE_MANAGER->Read(CACHED_b_sale_order, "sale_orders");
             $CACHE_MANAGER->SetImmediate("sale_orders", true);
         }
     }
     OrderHistory::collectEntityFields('ORDER', $id, $id);
     $this->fields->clearChanged();
     $this->isNew = false;
     return $result;
 }
Пример #5
0
 /**
  * @return Entity\AddResult|Entity\UpdateResult
  * @throws Main\ArgumentOutOfRangeException
  * @throws Main\ObjectNotFoundException
  * @throws \Exception
  */
 public function save()
 {
     $id = $this->getId();
     $fields = $this->fields->getValues();
     /** @var ShipmentItemCollection $shipmentItemCollection */
     if (!($shipmentItemCollection = $this->getCollection())) {
         throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
     }
     /** @var Shipment $shipment */
     if (!($shipment = $shipmentItemCollection->getShipment())) {
         throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
     }
     /** @var ShipmentCollection $shipmentCollection */
     if (!($shipmentCollection = $shipment->getCollection())) {
         throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
     }
     /** @var Order $order */
     if (!($order = $shipmentCollection->getOrder())) {
         throw new Main\ObjectNotFoundException('Entity "Order" not found');
     }
     if ($id > 0) {
         $fields = $this->fields->getChangedValues();
         if (!empty($fields) && is_array($fields)) {
             /** @var ShipmentItemCollection $shipmentItemCollection */
             if (!($shipmentItemCollection = $this->getCollection())) {
                 throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
             }
             /** @var Shipment $shipment */
             if (!($shipment = $shipmentItemCollection->getShipment())) {
                 throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
             }
             if (!$shipment->isSystem()) {
                 if (isset($fields["QUANTITY"]) && floatval($fields["QUANTITY"]) == 0) {
                     return new Entity\UpdateResult();
                 }
             }
             //$fields['DATE_UPDATE'] = new Main\Type\DateTime();
             $r = Internals\ShipmentItemTable::update($id, $fields);
             if (!$r->isSuccess()) {
                 return $r;
             }
         }
         $result = new Entity\UpdateResult();
         if ($order && $order->getId() > 0) {
             OrderHistory::collectEntityFields('SHIPMENT_ITEM_STORE', $order->getId(), $id);
         }
     } else {
         $fields['ORDER_DELIVERY_ID'] = $this->getParentShipmentId();
         $fields['DATE_INSERT'] = new Main\Type\DateTime();
         $fields["BASKET_ID"] = $this->basketItem->getId();
         if (!isset($fields["QUANTITY"]) || floatval($fields["QUANTITY"]) == 0) {
             return new Entity\UpdateResult();
         }
         if (!isset($fields['RESERVED_QUANTITY'])) {
             $fields['RESERVED_QUANTITY'] = $this->getReservedQuantity() === null ? 0 : $this->getReservedQuantity();
         }
         $r = Internals\ShipmentItemTable::add($fields);
         if (!$r->isSuccess()) {
             return $r;
         }
         $id = $r->getId();
         $this->setFieldNoDemand('ID', $id);
         $result = new Entity\AddResult();
         if (!$shipment->isSystem()) {
             OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_ITEM_BASKET_ADDED', $shipment->getId(), $this->basketItem, array('QUANTITY' => $this->getQuantity()));
         }
     }
     if ($eventName = static::getEntityEventName()) {
         $oldEntityValues = $this->fields->getOriginalValues();
         if (!empty($oldEntityValues)) {
             /** @var Main\Event $event */
             $event = new Main\Event('sale', 'On' . $eventName . 'EntitySaved', array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
             $event->send();
         }
     }
     $shipmentItemStoreCollection = $this->getShipmentItemStoreCollection();
     $r = $shipmentItemStoreCollection->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     if ($result->isSuccess()) {
         OrderHistory::collectEntityFields('SHIPMENT_ITEM', $order->getId(), $id);
     }
     $this->fields->clearChanged();
     return $result;
 }
Пример #6
0
 /**
  * @return Entity\AddResult|Entity\UpdateResult
  * @throws Main\ArgumentOutOfRangeException
  * @throws Main\ObjectNotFoundException
  * @throws \Exception
  */
 public function save()
 {
     $id = $this->getId();
     $fields = $this->fields->getValues();
     if ($id > 0) {
         $fields = $this->fields->getChangedValues();
         if (!empty($fields) && is_array($fields)) {
             //$fields['DATE_UPDATE'] = new Main\Type\DateTime();
             $r = Internals\PaymentTable::update($id, $fields);
             if (!$r->isSuccess()) {
                 return $r;
             }
         }
         $result = new Entity\UpdateResult();
     } else {
         $fields['ORDER_ID'] = $this->getParentOrderId();
         /** @var PaymentCollection $paymentCollection */
         if (!($paymentCollection = $this->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
         }
         /** @var Order $order */
         if (!($order = $paymentCollection->getOrder())) {
             throw new Main\ObjectNotFoundException('Entity "Order" not found');
         }
         if (!isset($fields['CURRENCY']) || strval($fields['CURRENCY']) == "") {
             $fields['CURRENCY'] = $order->getCurrency();
         }
         //$fields['DATE_INSERT'] = new Main\Type\DateTime();
         $r = Internals\PaymentTable::add($fields);
         if (!$r->isSuccess()) {
             return $r;
         }
         $id = $r->getId();
         $this->setFieldNoDemand('ID', $id);
         $result = new Entity\AddResult();
         if ($order->getId() > 0) {
             OrderHistory::addAction('PAYMENT', $order->getId(), 'PAYMENT_ADDED', $id, array('PAY_SYSTEM' => $this->getPaymentSystemName(), 'SUM' => $this->getSum()));
         }
     }
     if ($result->isSuccess()) {
         /** @var PaymentCollection $paymentCollection */
         if (!($paymentCollection = $this->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
         }
         /** @var Order $order */
         if (!($order = $paymentCollection->getOrder())) {
             throw new Main\ObjectNotFoundException('Entity "Order" not found');
         }
         OrderHistory::collectEntityFields('PAYMENT', $order->getId(), $id);
     }
     return $result;
 }
Пример #7
0
 /**
  * @return Entity\Result
  * @throws Main\ArgumentException
  * @throws Main\ArgumentNullException
  * @throws Main\ObjectNotFoundException
  */
 public function save()
 {
     $result = new Entity\Result();
     $itemsFromDb = array();
     if ($this->getOrder()->getId() > 0) {
         $itemsFromDbList = Internals\ShipmentTable::getList(array("filter" => array("ORDER_ID" => $this->getOrder()->getId()), "select" => array("ID", "DELIVERY_NAME")));
         while ($itemsFromDbItem = $itemsFromDbList->fetch()) {
             $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
         }
     }
     /** @var Shipment $shipment */
     foreach ($this->collection as $shipment) {
         if ($shipment->isSystem()) {
             continue;
         }
         if (($systemShipment = $this->getSystemShipment()) && $systemShipment->getId() == 0) {
             /** @var Result $r */
             $r = $this->cloneShipment($shipment, $systemShipment);
             if ($r->isSuccess()) {
                 break;
             } else {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     /** @var Shipment $shipment */
     foreach ($this->collection as $shipment) {
         $r = $shipment->save();
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
         if (isset($itemsFromDb[$shipment->getId()])) {
             unset($itemsFromDb[$shipment->getId()]);
         }
     }
     foreach ($itemsFromDb as $k => $v) {
         Internals\ShipmentTable::deleteWithItems($k);
         /** @var Order $order */
         if (!($order = $this->getOrder())) {
             throw new Main\ObjectNotFoundException('Entity "Order" not found');
         }
         if ($order->getId() > 0) {
             OrderHistory::addAction('SHIPMENT', $order->getId(), 'SHIPMENT_REMOVED', $k, null, array('ID' => $k, 'DELIVERY_NAME' => $v['DELIVERY_NAME']));
         }
     }
     if ($this->getOrder()->getId() > 0) {
         OrderHistory::collectEntityFields('SHIPMENT', $this->getOrder()->getId());
     }
     return $result;
 }
Пример #8
0
 /**
  * @return Entity\AddResult|Entity\UpdateResult
  * @throws Main\ArgumentOutOfRangeException
  * @throws Main\ObjectNotFoundException
  * @throws \Exception
  */
 public function save()
 {
     $id = $this->getId();
     $fields = $this->fields->getValues();
     if ($id > 0) {
         $fields = $this->fields->getChangedValues();
         if (!empty($fields) && is_array($fields)) {
             //$fields['DATE_UPDATE'] = new Main\Type\DateTime();
             $r = Internals\PaymentTable::update($id, $fields);
             if (!$r->isSuccess()) {
                 return $r;
             }
         }
         $result = new Entity\UpdateResult();
     } else {
         $fields['ORDER_ID'] = $this->getParentOrderId();
         /** @var PaymentCollection $paymentCollection */
         if (!($paymentCollection = $this->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
         }
         /** @var Order $order */
         if (!($order = $paymentCollection->getOrder())) {
             throw new Main\ObjectNotFoundException('Entity "Order" not found');
         }
         if (!isset($fields['CURRENCY']) || strval($fields['CURRENCY']) == "") {
             $fields['CURRENCY'] = $order->getCurrency();
         }
         //$fields['DATE_INSERT'] = new Main\Type\DateTime();
         $r = Internals\PaymentTable::add($fields);
         if (!$r->isSuccess()) {
             return $r;
         }
         $id = $r->getId();
         $this->setFieldNoDemand('ID', $id);
         $result = new Entity\AddResult();
         if ($order->getId() > 0) {
             OrderHistory::addAction('PAYMENT', $order->getId(), 'PAYMENT_ADDED', $id, $this);
         }
     }
     if ($result->isSuccess()) {
         /** @var PaymentCollection $paymentCollection */
         if (!($paymentCollection = $this->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
         }
         /** @var Order $order */
         if (!($order = $paymentCollection->getOrder())) {
             throw new Main\ObjectNotFoundException('Entity "Order" not found');
         }
         OrderHistory::collectEntityFields('PAYMENT', $order->getId(), $id);
         if ($eventName = static::getEntityEventName()) {
             $oldEntityValues = $this->fields->getOriginalValues();
             if (!empty($oldEntityValues)) {
                 /** @var Main\Event $event */
                 $event = new Main\Event('sale', 'On' . $eventName . 'EntitySaved', array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
                 $event->send();
             }
         }
     }
     $this->fields->clearChanged();
     return $result;
 }
Пример #9
0
 /**
  * @return Entity\Result|bool
  * @throws \Bitrix\Main\ArgumentException
  * @throws \Bitrix\Main\ArgumentNullException
  */
 public function save()
 {
     $result = new Result();
     /** @var Order $order */
     $order = $this->getOrder();
     $itemsFromDb = array();
     $filter = array();
     if (!$order) {
         /** @var Main\Entity\Event $event */
         $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_BEFORE_SAVED, array('ENTITY' => $this));
         $event->send();
         if ($event->getResults()) {
             /** @var Main\EventResult $eventResult */
             foreach ($event->getResults() as $eventResult) {
                 if ($eventResult->getType() == Main\EventResult::ERROR) {
                     $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BEFORE_BASKET_SAVED'), 'SALE_EVENT_ON_BEFORE_BASKET_SAVED');
                     if ($eventResultData = $eventResult->getParameters()) {
                         if (isset($eventResultData) && $eventResultData instanceof ResultError) {
                             /** @var ResultError $errorMsg */
                             $errorMsg = $eventResultData;
                         }
                     }
                     $result->addError($errorMsg);
                 }
             }
             if (!$result->isSuccess()) {
                 return $result;
             }
         }
     }
     $isNew = $order && $order->isNew() ? true : false;
     if ($order && !$isNew) {
         $filter['ORDER_ID'] = $order->getId();
     } else {
         if ($this->isLoadForFuserId()) {
             $filter = array('FUSER_ID' => $this->getFUserId(), 'ORDER_ID' => null, 'LID' => $this->getSiteId());
         }
         if ($isNew) {
             $fUserId = $this->getFUserId(true);
             if ($fUserId <= 0) {
                 $userId = $order->getUserId();
                 if (intval($userId) > 0) {
                     $fUserId = Fuser::getIdByUserId($userId);
                     if ($fUserId > 0) {
                         $this->setFUserId($fUserId);
                     }
                 }
             }
         }
     }
     if (!empty($filter)) {
         $itemsFromDbList = Internals\BasketTable::getList(array("filter" => $filter, "select" => array("ID", 'TYPE', 'SET_PARENT_ID', 'PRODUCT_ID', 'NAME', 'QUANTITY', 'FUSER_ID', 'ORDER_ID')));
         while ($itemsFromDbItem = $itemsFromDbList->fetch()) {
             if (intval($itemsFromDbItem['SET_PARENT_ID']) > 0 && intval($itemsFromDbItem['SET_PARENT_ID']) != $itemsFromDbItem['ID']) {
                 continue;
             }
             $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
         }
     }
     if ($order && ($order->getId() > 0 && $this->getOrderId() == 0)) {
         $this->orderId = $order->getId();
     }
     $changeMeaningfulFields = array("PRODUCT_ID", "QUANTITY", "PRICE", "DISCOUNT_VALUE", "VAT_RATE", "NAME");
     /** @var BasketItem $basketItem */
     foreach ($this->collection as $index => $basketItem) {
         $isNew = (bool) ($basketItem->getId() <= 0);
         $isChanged = $basketItem->isChanged();
         if ($order && $order->getId() > 0 && $isChanged) {
             $logFields = array();
             $fields = $basketItem->getFields();
             $originalValues = $fields->getOriginalValues();
             foreach ($originalValues as $originalFieldName => $originalFieldValue) {
                 if (in_array($originalFieldName, $changeMeaningfulFields) && $basketItem->getField($originalFieldName) != $originalFieldValue) {
                     $logFields[$originalFieldName] = $basketItem->getField($originalFieldName);
                     $logFields['OLD_' . $originalFieldName] = $originalFieldValue;
                 }
             }
         }
         $r = $basketItem->save();
         if ($r->isSuccess()) {
             if ($order && $order->getId() > 0) {
                 if ($isChanged) {
                     OrderHistory::addLog('BASKET', $order->getId(), $isNew ? "BASKET_ITEM_ADD" : "BASKET_ITEM_UPDATE", $basketItem->getId(), $basketItem, $logFields, OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1);
                     OrderHistory::addAction('BASKET', $order->getId(), "BASKET_SAVED", $basketItem->getId(), $basketItem);
                 }
             }
         } else {
             $result->addErrors($r->getErrors());
         }
         if (isset($itemsFromDb[$basketItem->getId()]) && $basketItem->getQuantity() > 0) {
             unset($itemsFromDb[$basketItem->getId()]);
         }
     }
     if (!empty($filter)) {
         $itemEventName = BasketItem::getEntityEventName();
         foreach ($itemsFromDb as $k => $v) {
             /** @var Main\Event $event */
             $event = new Main\Event('sale', "OnBefore" . $itemEventName . "Deleted", array('VALUES' => $v));
             $event->send();
             if ($v['TYPE'] == static::TYPE_SET) {
                 if ($order && $order->getId() > 0) {
                     OrderHistory::addLog('BASKET', $order->getId(), 'BASKET_ITEM_DELETE_BUNDLE', $k, null, array("PRODUCT_ID" => $v["PRODUCT_ID"], "NAME" => $v["NAME"], "QUANTITY" => $v["QUANTITY"]), OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1);
                 }
                 Internals\BasketTable::deleteBundle($k);
             } else {
                 if ($order && $order->getId() > 0) {
                     OrderHistory::addLog('BASKET', $order->getId(), 'BASKET_ITEM_DELETED', $k, null, array("PRODUCT_ID" => $v["PRODUCT_ID"], "NAME" => $v["NAME"], "QUANTITY" => $v["QUANTITY"]), OrderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1);
                 }
                 Internals\BasketTable::deleteWithItems($k);
             }
             /** @var Main\Event $event */
             $event = new Main\Event('sale', "On" . $itemEventName . "Deleted", array('VALUES' => $v));
             $event->send();
             /** @var Order $order */
             if ($order && $order->getId() > 0) {
                 OrderHistory::addAction('BASKET', $order->getId(), 'BASKET_REMOVED', $k, null, array('NAME' => $v['NAME'], 'QUANTITY' => $v['QUANTITY'], 'PRODUCT_ID' => $v['PRODUCT_ID']));
             }
         }
     }
     if ($order && $order->getId() > 0) {
         OrderHistory::collectEntityFields('BASKET', $order->getId());
     }
     if (!$order) {
         /** @var Main\Entity\Event $event */
         $event = new Main\Event('sale', EventActions::EVENT_ON_BASKET_SAVED, array('ENTITY' => $this));
         $event->send();
         if ($event->getResults()) {
             /** @var Main\EventResult $eventResult */
             foreach ($event->getResults() as $eventResult) {
                 if ($eventResult->getType() == Main\EventResult::ERROR) {
                     $errorMsg = new ResultError(Main\Localization\Loc::getMessage('SALE_EVENT_ON_BASKET_SAVED'), 'SALE_EVENT_ON_BASKET_SAVED');
                     if ($eventResultData = $eventResult->getParameters()) {
                         if (isset($eventResultData) && $eventResultData instanceof ResultError) {
                             /** @var ResultError $errorMsg */
                             $errorMsg = $eventResultData;
                         }
                     }
                     $result->addError($errorMsg);
                 }
             }
             if (!$result->isSuccess()) {
                 return $result;
             }
         }
     }
     return $result;
 }