/**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField("code", 'quantity', 'unit_quantity');
     if (!$this->quantity) {
         $this->quantity = 1;
     }
     if ($this->unit_quantity == 0) {
         $this->unit_quantity = '';
     }
     if ($this->code !== null && (!$this->_id || $this->fieldModified("code"))) {
         $this->code_canonical = preg_replace("/[^0-9a-z]/i", "", $this->code);
     }
     $cc = trim($this->classe_comptable, "0\n\r\t ");
     if (preg_match('/^\\d+$/', $cc)) {
         $this->classe_comptable = str_pad($cc, 9, "0", STR_PAD_RIGHT);
     } else {
         $this->classe_comptable = "";
     }
     if ($this->fieldModified("cancelled", 1)) {
         $references = $this->loadRefsReferences();
         foreach ($references as $_ref) {
             $_ref->cancelled = 1;
             $_ref->store();
         }
     }
     $create_stock_quantity = $this->_create_stock_quantity;
     if ($msg = parent::store()) {
         return $msg;
     }
     if ($create_stock_quantity) {
         $stock = $this->loadRefStock();
         $stock->quantity = $create_stock_quantity;
         $stock->order_threshold_min = $stock->quantity;
         $group = CGroups::loadCurrent();
         $stock->location_id = CProductStockLocation::getDefaultLocation($group, $this)->_id;
         if ($msg = $stock->store()) {
             CAppUI::setMsg($msg, UI_MSG_WARNING);
         }
         $this->_create_stock_quantity = null;
     }
     return null;
 }
 /**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField('delivery_id', 'quantity');
     if (!$this->_id && !$this->preparateur_id) {
         $this->preparateur_id = CMediusers::get()->_id;
     }
     $this->loadRefsFwd();
     $stock = $this->getStock();
     $stock->loadRefsFwd();
     $infinite_group_stock = CAppUI::conf('dPstock CProductStockGroup infinite_quantity') == '1';
     $negative_allowed = CAppUI::conf('dPstock CProductStockGroup negative_allowed') == '1';
     $stock_service = new CProductStockService();
     $stock_service->product_id = $stock->product_id;
     $stock_service->object_id = $this->_ref_delivery->service_id;
     $stock_service->object_class = "CService";
     // XXX
     //$stock_service->location_id = $this->target_location_id;
     $stock_service->loadMatchingObject();
     if ($this->date_delivery && !$negative_allowed && !$infinite_group_stock && ($this->quantity == 0 || $stock->quantity < $this->quantity)) {
         $unit = $stock->_ref_product->_unit_title ? $stock->_ref_product->_unit_title : $stock->_ref_product->_view;
         return "Impossible de délivrer ce nombre de {$unit}";
     }
     // Un-deliver
     if ($this->_undeliver) {
         $this->_undeliver = null;
         // If we can't delete (it has a back ref or something else: interop, etc)
         if ($msg = $this->delete()) {
             $this->quantity = 0;
             return parent::store();
         }
         return null;
     }
     // If we want to deliver, just provide a delivery date
     if ($this->date_delivery && !$infinite_group_stock) {
         $stock->quantity -= $this->quantity;
         if ($msg = $stock->store()) {
             return $msg;
         }
     } else {
         if ($this->_unreceive) {
             if ($stock_service->_id) {
                 $stock_service->quantity -= $this->quantity;
             }
             $this->_unreceive = null;
             $this->date_reception = '';
         }
     }
     // If we want to receive, just provide a reception date
     // if not dispensation nominative
     if ($this->date_reception && !$this->_ref_delivery->patient_id) {
         /*if (!$this->_ref_delivery->patient_id && (
             !$this->_id && $this->date_reception || $this->fieldModified("date_reception")
           )) {*/
         // If a stock already exist, its quantity is updated
         if ($stock_service->_id) {
             $stock_service->quantity += $this->quantity;
         } else {
             $stock_service->order_threshold_min = abs($this->quantity) + 1;
             $stock_service->order_threshold_max = $stock_service->order_threshold_min * 2;
             $stock_service->quantity = $this->quantity;
             $default_location = CProductStockLocation::getDefaultLocation($this->_ref_delivery->loadRefService(), $stock->_ref_product);
             $stock_service->location_id = $default_location->_id;
         }
         if ($stock_service->object_id && $stock_service->object_class) {
             if ($msg = $stock_service->store()) {
                 return $msg;
             }
         }
     }
     // dispensation nominative
     if (!$this->_ref_delivery->patient_id) {
         if (!$stock_service->_id) {
             $stock_service->quantity = $this->quantity;
             $stock_service->order_threshold_min = 0;
         }
         if ($this->_ref_delivery->service_id) {
             if ($msg = $stock_service->store()) {
                 return $msg;
             }
         }
     }
     // Calcul du stock du sejour
     if ($this->_ref_delivery->sejour_id && $this->_code_cis && $this->_code_cip && $this->_datetime_min && CModule::getActive("pharmacie")) {
         $stock_sejour = CStockSejour::getFromCIS($this->_code_cis, $this->_ref_delivery->sejour_id);
         // Mise a jour de la quantité du stock en quantité d'administration
         $ratio = CMedicamentArticle::get($this->_code_cip)->_ratio_cis_cip;
         $quantity = $this->quantity / $ratio;
         // Mise à jour du stock
         if ($stock_sejour->_id) {
             $count_quantity = $stock_sejour->countQuantityForDates($this->_datetime_min);
             $quantite = $stock_sejour->quantite + $quantity - $count_quantity;
         } else {
             $quantite = $quantity;
         }
         $stock_sejour->quantite = round($quantite, 4);
         $stock_sejour->datetime = $this->_datetime_min;
         if ($msg = $stock_sejour->store()) {
             return $msg;
         }
     }
     return parent::store();
 }
 /**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField("location_id");
     if (!$this->location_id) {
         $location = CProductStockLocation::getDefaultLocation($this->loadRefHost(), $this->loadRefProduct());
         $this->location_id = $location->_id;
     }
     return parent::store();
 }
 /**
  * @see parent::store()
  */
 function store()
 {
     $is_new = !$this->_id;
     if ($is_new) {
         if (!$this->datetime_min) {
             $this->datetime_min = $this->date_dispensation;
         }
         if (!$this->datetime_max) {
             $this->datetime_max = $this->date_dispensation;
         }
         if (!$this->preparateur_id) {
             $this->preparateur_id = CMediusers::get()->_id;
         }
     }
     $order_to_0 = $this->fieldModified("order", "0");
     if ($msg = parent::store()) {
         return $msg;
     }
     if ($this->_prises) {
         $prises = json_decode(stripslashes($this->_prises), true);
         foreach ($prises as $_prise) {
             $prise_dispensation = new CPriseDispensation();
             $prise_dispensation->delivery_id = $this->_id;
             $prise_dispensation->datetime = $_prise["datetime"];
             $prise_dispensation->quantite_adm = $_prise["quantite_adm"];
             $prise_dispensation->unite_adm = utf8_decode($_prise["unite_adm"]);
             $prise_dispensation->quantite_disp = $_prise["quantite_disp"];
             $prise_dispensation->object_id = $_prise["object_id"];
             $prise_dispensation->object_class = $_prise["object_class"];
             $prise_dispensation->store();
         }
         $this->_prises = null;
     }
     // Sortie manuelle ou autotrace et passage de "commande" à "pas commande"
     if ($is_new && $this->manual || ($is_new || $order_to_0) && $this->_auto_trace) {
         $delivery_trace = new CProductDeliveryTrace();
         $delivery_trace->delivery_id = $this->_id;
         $delivery_trace->quantity = $this->quantity;
         $delivery_trace->date_delivery = $this->date_delivery ? $this->date_delivery : CMbDT::dateTime();
         $delivery_trace->_code_cis = $this->_code_cis;
         $delivery_trace->_code_cip = $this->_code_cip;
         $delivery_trace->_datetime_min = $this->datetime_min;
         if ($this->manual) {
             $delivery_trace->date_reception = $delivery_trace->date_delivery;
         }
         $product = $this->loadRefStock()->loadRefProduct();
         $location = CProductStockLocation::getDefaultLocation($this->loadRefService(), $product);
         $delivery_trace->target_location_id = $location->_id;
         if ($msg = $delivery_trace->store()) {
             CAppUI::setMsg("La commande a été validée, mais n'a pas pu etre délivrée automatiquement pour la raison suivante: <br />{$msg}", UI_MSG_WARNING);
         } else {
             CAppUI::setMsg("CProductDeliveryTrace-msg-create");
         }
     }
     if (!$is_new) {
         return null;
     }
     $this->loadRefStock()->loadRefsFwd();
     if ($this->_auto_deliver || $this->_ref_stock->_ref_product->auto_dispensed || $this->_auto_trace) {
         $this->date_dispensation = CMbDT::dateTime();
         $this->order = 0;
         return parent::store();
     }
     return null;
 }