/** * @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; }
/** * @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; }
public static function createLink($id, $parentId, $type = self::LINK_TYPE_FINISH_START, array $behaviour = array()) { $id = Assert::expectIntegerPositive($id, '$id'); $parentId = Assert::expectIntegerPositive($parentId, '$parentId'); $type = Assert::expectEnumerationMember($type, array(self::LINK_TYPE_START_START, self::LINK_TYPE_START_FINISH, self::LINK_TYPE_FINISH_START, self::LINK_TYPE_FINISH_FINISH), '$type'); $exceptionInfo = array('AUX' => array('MESSAGE' => array('FROM_TASK_ID' => $parentId, 'TASK_ID' => $id, 'LINK_TYPE' => $type))); $result = new AddResult(); if (is_array($behaviour['TASK_DATA']) && !empty($behaviour['TASK_DATA'])) { $toTask = $behaviour['TASK_DATA']; } else { $toTask = TaskTable::getById($id)->fetch(); } if (empty($toTask)) { throw new ActionFailedException('Task not found', $exceptionInfo); } if (is_array($behaviour['PARENT_TASK_DATA']) && !empty($behaviour['PARENT_TASK_DATA'])) { $fromTask = $behaviour['PARENT_TASK_DATA']; } else { $fromTask = TaskTable::getById($parentId)->fetch(); } if (empty($fromTask)) { throw new ActionFailedException('Parent task not found', $exceptionInfo); } if ((string) $toTask['CREATED_DATE'] == '') { $result->addError(new Error(Loc::getMessage('DEPENDENCE_ENTITY_CANT_ADD_LINK_CREATED_DATE_NOT_SET'))); } if ((string) $toTask['END_DATE_PLAN'] == '') { $result->addError(new Error(Loc::getMessage('DEPENDENCE_ENTITY_CANT_ADD_LINK_END_DATE_PLAN_NOT_SET'))); } if ((string) $fromTask['CREATED_DATE'] == '') { $result->addError(new Error(Loc::getMessage('DEPENDENCE_ENTITY_CANT_ADD_LINK_CREATED_DATE_NOT_SET_PARENT_TASK'))); } if ((string) $fromTask['END_DATE_PLAN'] == '') { $result->addError(new Error(Loc::getMessage('DEPENDENCE_ENTITY_CANT_ADD_LINK_END_DATE_PLAN_NOT_SET_PARENT_TASK'))); } if (!$result->isSuccess()) { return $result; } else { return parent::createLink($id, $parentId, array('LINK_DATA' => array('TYPE' => $type))); } }
/** * @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; }
/** * @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; }