diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index 03ff0ff1eb2173c18f04c79bde371a482aa256db..117a54f89c340d0e9e056be4d4a584fc19897f3b 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -504,39 +504,41 @@ class FactureFournisseur extends CommonInvoice { $obj = $this->db->fetch_object($resql_rows); - $this->lines[$i] = new SupplierInvoiceLine($this->db); - - $this->lines[$i]->id = $obj->rowid; - $this->lines[$i]->rowid = $obj->rowid; - $this->lines[$i]->description = $obj->description; - $this->lines[$i]->product_ref = $obj->product_ref; // Internal reference - $this->lines[$i]->ref = $obj->product_ref; // deprecated. - $this->lines[$i]->ref_supplier = $obj->ref_supplier; // Reference product supplier TODO Rename field ref to ref_supplier into table llx_facture_fourn_det and llx_commande_fournisseurdet and update fields it into updateline - $this->lines[$i]->libelle = $obj->label; // Deprecated - $this->lines[$i]->label = $obj->label; // This field may contains label of product (when invoice create from order) - $this->lines[$i]->product_desc = $obj->product_desc; // Description du produit - $this->lines[$i]->subprice = $obj->pu_ht; - $this->lines[$i]->pu_ht = $obj->pu_ht; - $this->lines[$i]->pu_ttc = $obj->pu_ttc; - $this->lines[$i]->tva_tx = $obj->tva_tx; - $this->lines[$i]->localtax1_tx = $obj->localtax1_tx; - $this->lines[$i]->localtax2_tx = $obj->localtax2_tx; - $this->lines[$i]->qty = $obj->qty; - $this->lines[$i]->remise_percent = $obj->remise_percent; - $this->lines[$i]->tva = $obj->total_tva; - $this->lines[$i]->total_ht = $obj->total_ht; - $this->lines[$i]->total_tva = $obj->total_tva; - $this->lines[$i]->total_localtax1 = $obj->total_localtax1; - $this->lines[$i]->total_localtax2 = $obj->total_localtax2; - $this->lines[$i]->total_ttc = $obj->total_ttc; - $this->lines[$i]->fk_product = $obj->fk_product; - $this->lines[$i]->product_type = $obj->product_type; - $this->lines[$i]->product_label = $obj->label; - $this->lines[$i]->info_bits = $obj->info_bits; - $this->lines[$i]->fk_parent_line = $obj->fk_parent_line; - $this->lines[$i]->special_code = $obj->special_code; - $this->lines[$i]->rang = $obj->rang; - $this->lines[$i]->fk_unit = $obj->fk_unit; + $line = new SupplierInvoiceLine($this->db); + + $line->id = $obj->rowid; + $line->rowid = $obj->rowid; + $line->description = $obj->description; + $line->product_ref = $obj->product_ref; + $line->ref = $obj->product_ref; + $line->ref_supplier = $obj->ref_supplier; + $line->libelle = $obj->label; + $line->label = $obj->label; + $line->product_desc = $obj->product_desc; + $line->subprice = $obj->pu_ht; + $line->pu_ht = $obj->pu_ht; + $line->pu_ttc = $obj->pu_ttc; + $line->tva_tx = $obj->tva_tx; + $line->localtax1_tx = $obj->localtax1_tx; + $line->localtax2_tx = $obj->localtax2_tx; + $line->qty = $obj->qty; + $line->remise_percent = $obj->remise_percent; + $line->tva = $obj->total_tva; + $line->total_ht = $obj->total_ht; + $line->total_tva = $obj->total_tva; + $line->total_localtax1 = $obj->total_localtax1; + $line->total_localtax2 = $obj->total_localtax2; + $line->total_ttc = $obj->total_ttc; + $line->fk_product = $obj->fk_product; + $line->product_type = $obj->product_type; + $line->product_label = $obj->label; + $line->info_bits = $obj->info_bits; + $line->fk_parent_line = $obj->fk_parent_line; + $line->special_code = $obj->special_code; + $line->rang = $obj->rang; + $line->fk_unit = $obj->fk_unit; + + $this->lines[$i] = $line; $i++; } @@ -1368,53 +1370,25 @@ class FactureFournisseur extends CommonInvoice */ function deleteline($rowid, $notrigger=0) { - global $user, $langs, $conf; + if (!$rowid) { + $rowid = $this->id; + } - if (! $rowid) $rowid=$this->id; + $line = new SupplierInvoiceLine($this->db); - dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG); + if ($line->fetch($rowid) < 1) { + return -1; + } - $error=0; - $this->db->begin(); + $res = $line->delete($notrigger); - if (! $error && ! $notrigger) - { - // Call trigger - $result=$this->call_trigger('LINEBILL_SUPPLIER_DELETE',$user); - if ($result < 0) $error++; - // End call triggers - } + if ($res < 1) { + $this->errors[] = $line->error; + } else { + $res = $this->update_price(); + } - if (! $error) - { - // Supprime ligne - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det '; - $sql.= ' WHERE rowid = '.$rowid; - dol_syslog(get_class($this)."::delete", LOG_DEBUG); - $resql = $this->db->query($sql); - if (! $resql) - { - $error++; - $this->error=$this->db->lasterror(); - } - } - - if (! $error) - { - // Mise a jour prix facture - $result=$this->update_price(); - } - - if (! $error) - { - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - return -1; - } + return $res; } @@ -1905,13 +1879,26 @@ class SupplierInvoiceLine extends CommonObjectLine * @see product_ref */ public $ref; + /** + * Internal ref + * @var string + */ public $product_ref; + /** + * Reference product supplier + * TODO Rename field ref to ref_supplier into table llx_facture_fourn_det and llx_commande_fournisseurdet and update fields it into updateline + * @var string + */ public $ref_supplier; /** * @deprecated * @see label */ public $libelle; + /** + * Product description + * @var string + */ public $product_desc; /** @@ -1946,6 +1933,7 @@ class SupplierInvoiceLine extends CommonObjectLine /** * Product label + * This field may contains label of product (when invoice create from order) * @var string */ var $label; @@ -1968,6 +1956,24 @@ class SupplierInvoiceLine extends CommonObjectLine */ public $fk_prev_id; + public $tva_tx; + public $localtax1_tx; + public $localtax2_tx; + public $qty; + public $remise_percent; + public $total_ht; + public $total_ttc; + public $total_localtax1; + public $total_localtax2; + public $fk_product; + public $product_type; + public $product_label; + public $info_bits; + public $fk_parent_line; + public $special_code; + public $rang; + + /** * Constructor * @@ -1978,6 +1984,116 @@ class SupplierInvoiceLine extends CommonObjectLine $this->db= $db; } + /** + * Retrieves a supplier invoice line + * + * @param int $rowid Line id + * @return int <0 KO; 0 NOT FOUND; 1 OK + */ + public function fetch($rowid) + { + $sql = 'SELECT f.rowid, f.ref as ref_supplier, f.description, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.tva_tx'; + $sql.= ', f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2 '; + $sql.= ', f.total_ht, f.tva as total_tva, f.total_ttc, f.fk_product, f.product_type, f.info_bits, f.rang, f.special_code, f.fk_parent_line, f.fk_unit'; + $sql.= ', p.rowid as product_id, p.ref as product_ref, p.label as label, p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_det as f'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON f.fk_product = p.rowid'; + $sql.= ' WHERE rowid = '.$rowid; + $sql.= ' ORDER BY f.rang, f.rowid'; + + $query = $this->db->query($sql); + + if (!$query) { + $this->errors[] = $this->db->error(); + return -1; + } + + if (!$this->db->num_rows($query)) { + return 0; + } + + $obj = $this->db->fetch_object($query); + + $this->id = $obj->rowid; + $this->rowid = $obj->rowid; + $this->description = $obj->description; + $this->product_ref = $obj->product_ref; + $this->ref = $obj->product_ref; + $this->ref_supplier = $obj->ref_supplier; + $this->libelle = $obj->label; + $this->label = $obj->label; + $this->product_desc = $obj->product_desc; + $this->subprice = $obj->pu_ht; + $this->pu_ht = $obj->pu_ht; + $this->pu_ttc = $obj->pu_ttc; + $this->tva_tx = $obj->tva_tx; + $this->localtax1_tx = $obj->localtax1_tx; + $this->localtax2_tx = $obj->localtax2_tx; + $this->qty = $obj->qty; + $this->remise_percent = $obj->remise_percent; + $this->tva = $obj->total_tva; + $this->total_ht = $obj->total_ht; + $this->total_tva = $obj->total_tva; + $this->total_localtax1 = $obj->total_localtax1; + $this->total_localtax2 = $obj->total_localtax2; + $this->total_ttc = $obj->total_ttc; + $this->fk_product = $obj->fk_product; + $this->product_type = $obj->product_type; + $this->product_label = $obj->label; + $this->info_bits = $obj->info_bits; + $this->fk_parent_line = $obj->fk_parent_line; + $this->special_code = $obj->special_code; + $this->rang = $obj->rang; + $this->fk_unit = $obj->fk_unit; + + return 1; + } + + /** + * Deletes a line + * + * @param bool|int $notrigger + * @return int -1 KO; 1 OK + */ + public function delete($notrigger = 0) + { + global $user; + + dol_syslog(get_class($this)."::deleteline rowid=".$this->id, LOG_DEBUG); + + $error = 0; + + $this->db->begin(); + + if (!$notrigger) { + if ($this->call_trigger('LINEBILL_SUPPLIER_DELETE',$user) < 0) { + $error++; + } + } + + if (!$error) { + // Supprime ligne + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det '; + $sql .= ' WHERE rowid = '.$this->id; + dol_syslog(get_class($this)."::delete", LOG_DEBUG); + $resql = $this->db->query($sql); + if (!$resql) { + $error++; + $this->error = $this->db->lasterror(); + } + } + + if (! $error) + { + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -1; + } + } }