/** * @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; }
/** * @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; }