/**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField("reception_id");
     $is_new = !$this->_id;
     if ($is_new && $this->cancelled === null) {
         $this->cancelled = 0;
     }
     if ($is_new) {
         $this->loadRefOrderItem();
         $this->_ref_order_item->loadOrder();
     }
     if ($is_new && !$this->reception_id) {
         $order = $this->_ref_order_item->_ref_order;
         $reception = new CProductReception();
         $reception->date = CMbDT::dateTime();
         $reception->societe_id = $order->societe_id;
         $reception->group_id = CProductStockGroup::getHostGroup();
         // Recherche de receptions ayant un numero de reception similaire pour gerer l'increment
         if ($order->order_number) {
             $where = array("reference" => "LIKE '{$order->order_number}%'");
             $number = $reception->countList($where) + 1;
             $reception->reference = "{$order->order_number}-{$number}";
         }
         if ($msg = $reception->store()) {
             return $msg;
         }
         $this->reception_id = $reception->_id;
     }
     if ($is_new) {
         $this->_ref_order_item->loadRefsFwd();
         $this->_ref_order_item->_ref_reference->loadRefsFwd();
         $this->_ref_order_item->_ref_reference->_ref_product->loadRefStock();
         $product =& $this->_ref_order_item->_ref_reference->_ref_product;
         $product->updateFormFields();
         if ($product->loadRefStock()) {
             $stock = $product->_ref_stock_group;
             $stock->quantity += $this->quantity;
         } else {
             $qty = $this->quantity;
             $stock = new CProductStockGroup();
             $stock->product_id = $product->_id;
             $stock->group_id = CProductStockGroup::getHostGroup();
             $stock->quantity = $qty;
             $stock->order_threshold_min = $qty;
             CAppUI::setMsg("Un nouveau stock a été créé", UI_MSG_OK);
             //CAppUI::setMsg("Un nouveau stock pour [%s] a été créé", UI_MSG_OK, $product->_view);
         }
         if ($msg = $stock->store()) {
             return $msg;
         }
     }
     if ($msg = parent::store()) {
         return $msg;
     }
     // If the order is received, we set the flag
     if ($is_new) {
         $order = $this->_ref_order_item->_ref_order;
         if (!$order->received) {
             $count_renewed = $order->countRenewedItems();
             $count_received = $order->countReceivedItems() - (count($order->_ref_order_items) - $count_renewed);
             if ($count_renewed && $count_received >= $count_renewed) {
                 $order->received = 1;
                 $order->store();
             }
         }
     }
     return null;
 }
 /**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField("order_id", "reference_id", "renewal", "septic");
     if (!$this->_id) {
         if ($this->renewal === null) {
             $this->renewal = "1";
         }
         if ($this->septic === null) {
             $this->septic = "0";
         }
     }
     if ($this->order_id && $this->reference_id && !$this->_id) {
         $this->loadRefsFwd();
         $where = array('order_id' => "= '{$this->order_id}'", 'reference_id' => "= '{$this->reference_id}'", 'renewal' => "= '{$this->renewal}'", 'septic' => "= '{$this->septic}'");
         if ($this->lot_id) {
             $where['lot_id'] = "= '{$this->lot_id}'";
         }
         $duplicateKey = new CProductOrderItem();
         if ($duplicateKey->loadObject($where)) {
             $duplicateKey->loadRefsFwd();
             $this->_id = $duplicateKey->_id;
             $this->quantity += $duplicateKey->quantity;
             $this->unit_price = $duplicateKey->unit_price;
             $this->tva = $duplicateKey->tva;
         } else {
             $this->unit_price = $this->_ref_reference->price;
             $this->tva = $this->_ref_reference->tva;
         }
     }
     if ($this->_id && $this->_update_reference) {
         $ref = $this->loadReference();
         $ref->price = $this->unit_price;
         if ($msg = $ref->store()) {
             CAppUI::setMsg($msg, UI_MSG_WARNING);
         } else {
             CAppUI::setMsg('Prix de la référence mis à jour', UI_MSG_OK);
         }
         $this->_update_reference = null;
     }
     /*if (!$this->_id && ($stock = $this->getStock())) {
         $stock->loadRefOrders();
         if ($stock->_zone_future > 2) {
           CAppUI::setMsg("Attention : le stock optimum risque d'être dépassé", UI_MSG_WARNING);
         }
       }*/
     return parent::store();
 }