/** * (non-PHPdoc) * @see BaseEntityAbstract::preSave() */ public function preSave() { if (trim($this->getSerialNo()) === '') { throw new Exception('You can NOT save a selling Item without a serial number.'); } if (trim($this->getId()) !== '') { if (intval($this->getActive()) === 0 && self::countByCriteria('id = ? and active != ?', array($this->getId(), $this->getActive())) > 0) { //trying to deactivated $this->_clearKit(); } else { if (intval($this->getActive()) === 1 && self::countByCriteria('id = ? and serialNo != ?', array($this->getId(), trim($this->getSerialNo()))) > 0) { //trying to changed serialno $this->_clearKit()->setKit(null); } } } if ($this->getOrderItem() instanceof OrderItem) { $this->setProduct($this->getOrderItem()->getProduct())->setOrder($this->getOrderItem()->getOrder()); } if (!$this->getKit() instanceof Kit && strpos(trim($this->getSerialNo()), Kit::BARCODE_PREFIX) === 0) { if (($kit = Kit::getByBarcode(trim($this->getSerialNo()))) instanceof Kit) { $this->setKit($kit); } } if ($this->getKit() instanceof Kit) { $kitProduct = $this->getKit()->getProduct(); $orderItemProduct = $this->getOrderItem() instanceof OrderItem ? $this->getOrderItem()->getProduct() : null; if (!$kitProduct instanceof Product && $orderItemProduct instanceof Product || $kitProduct instanceof Product && !$orderItemProduct instanceof Product || $kitProduct instanceof Product && $orderItemProduct instanceof Product && $kitProduct->getId() !== $orderItemProduct->getId()) { throw new Exception('The Kit [' . $this->getKit()->getBarcode() . ', SKU: ' . ($kitProduct instanceof Product ? $kitProduct->getSku() : '') . '] is not the same product on this OrderItem[SKU:' . ($orderItemProduct instanceof Product ? $orderItemProduct->getSku() : '') . '].'); } } if ($this->getProduct() instanceof Product && intval($this->getProduct()->getIsKit()) === 1) { if (!$this->getKit() instanceof Kit) { throw new Exception('The Product(SKU: ' . $this->getProduct()->getSku() . ') is a KIT, but no valid Kit barcode provided(Provided: ' . $this->getSerialNo() . ').'); } if ($this->getOrderItem()->getOrder() instanceof Order) { $where = array('kitId = :kitId and orderId = :orderId and active = 1'); $params = array('kitId' => $this->getKit()->getId(), 'orderId' => $this->getOrderItem()->getOrder()->getId()); if (($id = trim($this->getId())) !== '') { $where[] = 'id != :id'; $params['id'] = $id; } if (self::countByCriteria(implode(' AND ', $where), $params) > 0) { throw new Exception('The KIT[' . $this->getKit()->getBarcode() . '] has been scanned onto this Order(' . $this->getOrderItem()->getOrder()->getOrderNo() . ') already!'); } } } }