/** * (non-PHPdoc) * @see BaseEntityAbstract::preSave() */ public function preSave() { if (trim($this->getMageOrderId()) === '') { $this->setMageOrderId('0'); } //when brandnew, calculate margin if (trim($this->getUnitCost()) === '') { $this->reCalMarginFromProduct(); } if (trim($this->getId()) === '') { if (trim($this->getItemDescription()) === '') { $this->setItemDescription($this->getProduct()->getName()); } } else { if (intval($this->getActive()) === 1) { //if the isPicked changed $product = $this->getProduct(); $kitCount = 0; //for picked if (self::countByCriteria('id = ? and isPicked != ?', array($this->getId(), $this->getIsPicked())) > 0) { $kitCount = $kitCount === 0 ? SellingItem::countByCriteria('orderItemId = ? and kitId is not null and active = 1 ', array($this->getId())) : $kitCount; if (intval($product->getIsKit()) === 1 && intval($kitCount) !== intval($this->getQtyOrdered())) { throw new EntityException($this->getQtyOrdered() . ' Kit(s) needs to be scanned to this OrderItem(SKU=' . $this->getProduct()->getSku() . ', unitPrice=' . StringUtilsAbstract::getCurrency($this->getUnitPrice()) . ', qty=' . $this->getQtyOrdered() . ') before it can be marked as PICKED, but got:' . $kitCount); } //we are picking this product if (intval($this->getIsPicked()) === 1) { $product->picked($this->getQtyOrdered(), '', $this); $this->addLog('This item is now marked as picked', Log::TYPE_SYSTEM, 'Auto Log', __CLASS__ . '::' . __FUNCTION__); } else { $product->picked(0 - $this->getQtyOrdered(), '', $this); $this->addLog('This item is now Un-marked as picked', Log::TYPE_SYSTEM, 'Auto Log', __CLASS__ . '::' . __FUNCTION__); } } //for shipped if (self::countByCriteria('id = ? and isShipped != ?', array($this->getId(), $this->getIsShipped())) > 0) { $kitCount = $kitCount === 0 ? SellingItem::countByCriteria('orderItemId = ? and kitId is not null and active = 1 ', array($this->getId())) : $kitCount; if (intval($product->getIsKit()) === 1 && intval($kitCount) !== intval($this->getQtyOrdered())) { throw new EntityException($this->getQtyOrdered() . ' Kit(s) needs to be scanned to this OrderItem(ID=' . $this->getId() . ') before it can be marked as SHIPPED, but got:' . $kitCount); } //we are picking this product if (intval($this->getIsShipped()) === 1) { $product->shipped($this->getQtyOrdered(), '', $this); $this->addLog('This item is now marked as SHIPPED', Log::TYPE_SYSTEM, 'Auto Log', __CLASS__ . '::' . __FUNCTION__); } else { $product->shipped(0 - $this->getQtyOrdered(), '', $this); $this->addLog('This item is now Un-marked as SHIPPED', Log::TYPE_SYSTEM, 'Auto Log', __CLASS__ . '::' . __FUNCTION__); } } } else { //if the orderitem is been deactivated if (self::countByCriteria('id = ? and active != ?', array($this->getId(), $this->getActive())) > 0) { if (self::countByCriteria('id = ? and isShipped = 1', array($this->getId())) > 0) { $msg = 'This item is now Un-marked as SHIPPED, as OrderItem(Order: ' . $this->getOrder()->getOrderNo() . ' SKU: ' . $this->getProduct()->getSku() . ', Qty: ' . $this->getQtyOrdered() . ') is now Deactivated'; $product->shipped(0 - $this->getQtyOrdered(), $msg, $this); $this->addLog($msg, Log::TYPE_SYSTEM, 'Auto Log', __CLASS__ . '::' . __FUNCTION__); } if (self::countByCriteria('id = ? and isPicked = 1', array($this->getId())) > 0) { $msg = 'This item is now Un-PICKED, as OrderItem(Order: ' . $this->getOrder()->getOrderNo() . ' SKU: ' . $this->getProduct()->getSku() . ', Qty: ' . $this->getQtyOrdered() . ') is now Deactivated'; $product->shipped(0 - $this->getQtyOrdered(), $msg, $this); $this->addLog($msg, Log::TYPE_SYSTEM, 'Auto Log', __CLASS__ . '::' . __FUNCTION__); } foreach (SellingItem::getAllByCriteria('orderItemId = ?', array($this->getId())) as $sellingItem) { $sellingItem->setActive(false)->save(); } } } } }