* @package    Mediboard
 * @subpackage Stock
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 */
// If it is a return to the group stock
if (isset($_POST['_return']) && isset($_POST['_code'])) {
    $stock_service = CProductStockService::getFromCode($_POST['_code']);
    $stock_group = CProductStockGroup::getFromCode($_POST['_code']);
    $_POST['quantity'] = -abs($_POST['quantity']);
    $_POST['_code'] = null;
    $_POST['_return'] = null;
    if ($stock_service && $stock_group) {
        $delivery = new CProductDelivery();
        $where = array('stock_id' => "= '{$stock_group->_id}'", 'stock_class' => "= 'CProductStockGroup'", 'service_id' => "= '{$stock_service->service_id}'", 'quantity' => "< 0");
        if (!$delivery->loadObject($where)) {
            $delivery->stock_id = $stock_group->_id;
            $delivery->stock_class = $stock_group->_class;
            $delivery->service_id = $stock_service->service_id;
        }
        $delivery->quantity += $_POST['quantity'];
        $delivery->date_dispensation = CMbDT::dateTime();
        if ($msg = $delivery->store()) {
            CAppUI::setMsg($msg, UI_MSG_ERROR);
        }
        $_POST['delivery_id'] = $delivery->_id;
    }
}
$do = new CDoObjectAddEdit('CProductDeliveryTrace');
$do->doIt();
 /**
  * @see parent::delete()
  */
 function delete()
 {
     $this->completeField('delivery_id', 'quantity', 'date_delivery', 'date_reception');
     $this->loadRefsFwd();
     $stock = $this->getStock();
     $stock->loadRefsFwd();
     $infinite_group_stock = CAppUI::conf('dPstock CProductStockGroup infinite_quantity') == '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->loadMatchingObject();
     if (!$infinite_group_stock && $this->date_delivery) {
         $stock->quantity += $this->quantity;
         if ($msg = $stock->store()) {
             return $msg;
         }
     }
     if ($stock_service->_id && $this->date_reception) {
         $stock_service->quantity -= $this->quantity;
         if ($msg = $stock_service->store()) {
             return $msg;
         }
     }
     // Calcul du stock du sejour
     if ($this->_code_cis && $this->_ref_delivery->sejour_id && CModule::getActive("pharmacie")) {
         $stock_sejour = CStockSejour::getFromCIS($this->_code_cis, $this->_ref_delivery->sejour_id);
         if ($stock_sejour->_id) {
             $codes_cip = CMedicamentProduit::getArticleCodes($this->_code_cis);
             $ds = $this->getDS();
             $where = array();
             $where["product.code"] = $ds->prepareIn($codes_cip);
             $where["product_delivery.sejour_id"] = $ds->prepare("= '{$this->_ref_delivery->sejour_id}'");
             $ljoin = array();
             $ljoin["product_stock_group"] = "product_stock_group.stock_id = product_delivery.stock_id\r\n                                           AND product_delivery.stock_class = 'CProductStockGroup'";
             $ljoin["product"] = "product.product_id = product_stock_group.product_id";
             $delivery = new CProductDelivery();
             $delivery->loadObject($where, "product_delivery.date_dispensation DESC", null, $ljoin);
             // Si la delivrance actuelle est la derniere pour ce sejour et ce CIS
             if ($delivery->_id == $this->delivery_id) {
                 // Mise a jour de la quantité du stock en quantité d'administration
                 $code_cip = $this->_ref_delivery->loadRefStock()->loadRefProduct()->code;
                 $product = CMedicamentArticle::get($code_cip);
                 if ($product->getId()) {
                     $ratio = $product->_ratio_cis_cip;
                     // Mise à jour du stock
                     $stock_sejour->datetime = $this->_ref_delivery->datetime_min;
                     $stock_sejour->quantite -= $this->quantity / $ratio;
                     $stock_sejour->quantite = round($stock_sejour->quantite, 4);
                     if ($msg = $stock_sejour->store()) {
                         return $msg;
                     }
                 }
             }
         }
     }
     return parent::delete();
 }