Beispiel #1
0
 /**
  * @param $id
  * @return Main\Entity\DeleteResult
  * @throws Main\ArgumentException
  * @throws Main\ArgumentNullException
  */
 public static function deleteWithItems($id)
 {
     $id = intval($id);
     if ($id <= 0) {
         throw new Main\ArgumentNullException("id");
     }
     $itemsList = BasketPropertyTable::getList(array("filter" => array("BASKET_ID" => $id), "select" => array("ID")));
     while ($item = $itemsList->fetch()) {
         BasketPropertyTable::delete($item["ID"]);
     }
     return BasketTable::delete($id);
 }
Beispiel #2
0
 /**
  * @return Entity\AddResult|Entity\UpdateResult
  * @throws ArgumentException
  * @throws ArgumentNullException
  * @throws \Exception
  */
 public function save()
 {
     $id = $this->getId();
     $changedFields = $this->fields->getChangedValues();
     $isNew = $id <= 0;
     if (!empty($changedFields)) {
         /** @var array $oldEntityValues */
         $oldEntityValues = $this->fields->getOriginalValues();
         /** @var Event $event */
         $event = new Event('sale', EventActions::EVENT_ON_BASKET_ITEM_BEFORE_SAVED, array('ENTITY' => $this, 'IS_NEW' => $isNew, 'VALUES' => $oldEntityValues));
         $event->send();
         if ($event->getResults()) {
             $result = new Result();
             /** @var EventResult $eventResult */
             foreach ($event->getResults() as $eventResult) {
                 if ($eventResult->getType() == EventResult::ERROR) {
                     $errorMsg = new ResultError(Loc::getMessage('SALE_EVENT_ON_BEFORE_BASKET_ITEM_SAVED'), 'SALE_EVENT_ON_BEFORE_BASKET_ITEM_SAVED');
                     if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) {
                         $errorMsg = $eventResultData['ERROR'];
                     }
                     $result->addError($errorMsg);
                 }
             }
             if (!$result->isSuccess()) {
                 return $result;
             }
         }
     }
     $fields = $this->fields->getValues();
     if ($this->isBundleParent()) {
         $bundleBasketCollection = $this->getBundleCollection();
     }
     if ($id > 0) {
         $fields = $changedFields;
         if (!isset($fields["ORDER_ID"]) || intval($fields["ORDER_ID"]) == 0) {
             $orderId = null;
             if ($this->getParentOrderId() > 0) {
                 $orderId = $this->getParentOrderId();
             }
             if ($this->isBundleChild() && $orderId === null) {
                 /** @var BasketItem $parentBasket */
                 if (!($parentBasket = $this->getParentBasketItem())) {
                     throw new ObjectNotFoundException('Entity parent "BasketItem" not found');
                 }
                 $orderId = $parentBasket->getParentOrderId();
             }
             if (intval($orderId) > 0 && $this->getField('ORDER_ID') != $orderId) {
                 $fields['ORDER_ID'] = $orderId;
             }
         }
         if (!empty($fields) && is_array($fields)) {
             if (isset($fields["QUANTITY"]) && floatval($fields["QUANTITY"]) == 0) {
                 return new Entity\UpdateResult();
             }
             $fields['DATE_UPDATE'] = new DateTime();
             $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']);
             $r = Internals\BasketTable::update($id, $fields);
             if (!$r->isSuccess()) {
                 return $r;
             }
         }
         $result = new Entity\UpdateResult();
     } else {
         $fields['ORDER_ID'] = $this->getParentOrderId();
         $fields['DATE_INSERT'] = new DateTime();
         $fields['DATE_UPDATE'] = new DateTime();
         $this->setFieldNoDemand('DATE_INSERT', $fields['DATE_INSERT']);
         $this->setFieldNoDemand('DATE_UPDATE', $fields['DATE_UPDATE']);
         if (!$this->isBundleChild() && (!isset($fields["FUSER_ID"]) || intval($fields["FUSER_ID"]) <= 0)) {
             /** @var Basket $basket */
             if (!($basket = $this->getCollection())) {
                 throw new ObjectNotFoundException('Entity "Basket" not found');
             }
             $fields["FUSER_ID"] = intval($basket->getFUserId());
         }
         /** @var Basket $basket */
         if (!($basket = $this->getCollection())) {
             throw new ObjectNotFoundException('Entity "Basket" not found');
         }
         /** @var Order $order */
         if ($order = $basket->getOrder()) {
             if (!isset($fields["LID"]) || strval($fields["LID"]) == '') {
                 $fields['LID'] = $order->getField('LID');
             }
         } else {
             if ($siteId = $basket->getSiteId()) {
                 $fields['LID'] = $siteId;
             }
         }
         if ($this->isBundleChild()) {
             if (!($parentBasketItem = $this->getParentBasketItem())) {
                 throw new ObjectNotFoundException('Entity parent "BasketItem" not found');
             }
             $fields['LID'] = $parentBasketItem->getField('LID');
             if (!isset($fields["FUSER_ID"]) || intval($fields["FUSER_ID"]) <= 0) {
                 $fields['FUSER_ID'] = intval($parentBasketItem->getField('FUSER_ID'));
             }
         }
         if (!isset($fields["LID"]) || strval(trim($fields["LID"])) == '') {
             throw new ArgumentNullException('lid');
         }
         if ($this->isBundleChild() && (!isset($fields["SET_PARENT_ID"]) || intval($fields["QUANTITY"]) <= 0)) {
             $fields["SET_PARENT_ID"] = $this->getParentBasketItemId();
             $this->setFieldNoDemand('SET_PARENT_ID', $fields['SET_PARENT_ID']);
         }
         if (!isset($fields["QUANTITY"]) || floatval($fields["QUANTITY"]) == 0) {
             return new Entity\AddResult();
         }
         if (!isset($fields["CURRENCY"]) || strval(trim($fields["CURRENCY"])) == '') {
             throw new ArgumentNullException('currency');
         }
         $r = Internals\BasketTable::add($fields);
         if (!$r->isSuccess()) {
             return $r;
         }
         $id = $r->getId();
         $this->setFieldNoDemand('ID', $id);
         $this->setFieldNoDemand('LID', $fields['LID']);
         $this->setFieldNoDemand('FUSER_ID', $fields['FUSER_ID']);
         if ($basket->getOrder() && $basket->getOrderId() > 0) {
             OrderHistory::addAction('BASKET', $order->getId(), 'BASKET_ADDED', $id, $this);
         }
         $result = new Entity\AddResult();
     }
     if ($isNew || !empty($changedFields)) {
         /** @var array $oldEntityValues */
         $oldEntityValues = $this->fields->getOriginalValues();
         /** @var Event $event */
         $event = new Event('sale', EventActions::EVENT_ON_BASKET_ITEM_SAVED, array('ENTITY' => $this, 'IS_NEW' => $isNew, 'VALUES' => $oldEntityValues));
         $event->send();
         if ($event->getResults()) {
             $result = new Result();
             /** @var EventResult $eventResult */
             foreach ($event->getResults() as $eventResult) {
                 if ($eventResult->getType() == EventResult::ERROR) {
                     $errorMsg = new ResultError(Loc::getMessage('SALE_EVENT_ON_BEFORE_BASKET_ITEM_SAVED'), 'SALE_EVENT_ON_BEFORE_BASKET_ITEM_SAVED');
                     if (isset($eventResultData['ERROR']) && $eventResultData['ERROR'] instanceof ResultError) {
                         $errorMsg = $eventResultData['ERROR'];
                     }
                     $result->addError($errorMsg);
                 }
             }
             if (!$result->isSuccess()) {
                 return $result;
             }
         }
     }
     if ($eventName = static::getEntityEventName()) {
         /** @var array $oldEntityValues */
         $oldEntityValues = $this->fields->getOriginalValues();
         if (!empty($oldEntityValues)) {
             /** @var Event $event */
             $event = new Event('sale', 'On' . $eventName . 'EntitySaved', array('ENTITY' => $this, 'VALUES' => $oldEntityValues));
             $event->send();
         }
     }
     $this->fields->clearChanged();
     // bundle
     if ($this->isBundleParent()) {
         if (!empty($bundleBasketCollection)) {
             if (!($order = $bundleBasketCollection->getOrder())) {
                 /** @var Basket $basketCollection */
                 $basketCollection = $this->getCollection();
                 if ($order = $basketCollection->getOrder()) {
                     $bundleBasketCollection->setOrder($order);
                 }
             }
             $itemsFromDb = array();
             if (!$isNew) {
                 $itemsFromDbList = Internals\BasketTable::getList(array("filter" => array("SET_PARENT_ID" => $id), "select" => array("ID")));
                 while ($itemsFromDbItem = $itemsFromDbList->fetch()) {
                     if ($itemsFromDbItem["ID"] == $id) {
                         continue;
                     }
                     $itemsFromDb[$itemsFromDbItem["ID"]] = true;
                 }
             }
             /** @var BasketItem $bundleBasketItem */
             foreach ($bundleBasketCollection as $bundleBasketItem) {
                 $r = $bundleBasketItem->save();
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                 }
                 if (isset($itemsFromDb[$bundleBasketItem->getId()])) {
                     unset($itemsFromDb[$bundleBasketItem->getId()]);
                 }
             }
             foreach ($itemsFromDb as $k => $v) {
                 Internals\BasketTable::delete($k);
             }
         }
     }
     /** @var BasketPropertiesCollection $basketPropertyCollection */
     $basketPropertyCollection = $this->getPropertyCollection();
     $r = $basketPropertyCollection->save();
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     return $result;
 }