$where["fields"] = " LIKE '%quantity%'";
        $destockages[$code_cip]["stock"]->_ref_logs = $log->loadList($where, $order);
        foreach ($destockages[$code_cip]["stock"]->_ref_logs as $log) {
            $log->loadRefsFwd();
        }
    } else {
        $product = new CProduct();
        $product->code = $code_cip;
        $product->category_id = CAppUI::conf('dPmedicament CBcbProduitLivretTherapeutique product_category_id');
        if ($product->loadMatchingObject()) {
            $stock = new CProductStockService();
            $stock->object_id = $service_id;
            $stock->object_class = "CService";
            // XXX
            $stock->product_id = $product->_id;
            $stock->store();
            $destockages[$code_cip]['stock'] = $stock;
        } else {
            $destockages[$code_cip]['stock'] = null;
        }
    }
}
if ($all_stocks) {
    $stock = new CProductStockService();
    $stock->object_id = $service_id;
    $stock->object_class = "CService";
    // XXX
    $list_stocks = $stock->loadMatchingList();
    foreach ($list_stocks as $sto) {
        $sto->loadRefsFwd();
        $already = false;
 /**
  * @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();
 }