diff --git a/htdocs/commande/commande.class.php b/htdocs/commande/commande.class.php index 44261206bb0..6f3f526ecda 100644 --- a/htdocs/commande/commande.class.php +++ b/htdocs/commande/commande.class.php @@ -485,18 +485,42 @@ class Commande $price = $pu - $remise; } - $sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice, total_ht, total_tva, total_ttc)"; - $sql.= " VALUES "; - $sql.= " (".$this->id.", "; - if ($fk_product) { $sql.= "'$fk_product',"; } - else { $sql.='0,'; } - $sql.= " '". $qty."','". price2num($price)."','".$txtva."','".addslashes($desc)."','".price2num($remise_percent)."', '".price2num($subprice)."',"; - $sql.= " '".price2num($total_ht) ."',"; - $sql.= " '".price2num($total_tva)."',"; - $sql.= " '".price2num($total_ttc)."'"; - $sql.= ")"; + // Récupère rang max de la commande dans $rangmax + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'commandedet'; + $sql.= ' WHERE fk_commande ='.$commandeid; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $rangmax = $row[0]; + } + else + { + dolibarr_print_error($this->db); + $this->db->rollback(); + return -1; + } - if ($this->db->query($sql)) + // Insertion ligne + $ligne=new CommandeLigne($this->db); + + $ligne->fk_commande=$commandeid; + $ligne->desc=$desc; + $ligne->price=$price; + $ligne->qty=$qty; + $ligne->txtva=$txtva; + $ligne->fk_product=$fk_product; + $ligne->remise_percent=$remise_percent; + $ligne->subprice=$subprice; + $ligne->remise=$remise; + $ligne->rang=($rangmax+1); + $ligne->info_bits=$info_bits; + $ligne->total_ht=$total_ht; + $ligne->total_tva=$total_tva; + $ligne->total_ttc=$total_ttc; + + $result=$ligne->insert(); + if ($result > 0) { // Mise a jour informations denormalisees au niveau de la facture meme $result=$this->update_price($this->id); @@ -516,8 +540,7 @@ class Commande } else { - $this->error=$this->db->error(); - dolibarr_syslog("Error sql=$sql, error=".$this->error); + $this->error=$ligne->error; $this->db->rollback(); return -2; } @@ -732,6 +755,7 @@ class Commande $this->mode_reglement_id = $obj->fk_mode_reglement; $this->date_livraison = $obj->date_livraison; $this->adresse_livraison_id = $obj->fk_adresse_livraison; + if ($this->statut == 0) $this->brouillon = 1; $this->db->free(); @@ -767,57 +791,8 @@ class Commande } } - if ($this->statut == 0) $this->brouillon = 1; - // \todo Utiliser la classe CommandeLigne au lieu de ce code - $this->lignes = array(); - $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice, l.coef,'; - $sql.= ' p.label, p.description as product_desc, p.ref, p.fk_product_type, p.rowid as prodid'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product=p.rowid'; - $sql.= ' WHERE l.fk_commande = '.$this->id; - $sql.= ' ORDER BY l.rang'; - $result = $this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows($result); - $i = 0; - - while ($i < $num) - { - $objp = $this->db->fetch_object($result); - - $ligne = new CommandeLigne($this->db); - - $ligne->desc = $objp->description; // Description ligne - $ligne->qty = $objp->qty; - $ligne->tva_tx = $objp->tva_tx; - $ligne->subprice = $objp->subprice; - $ligne->remise_percent = $objp->remise_percent; - $ligne->price = $objp->price; - $ligne->product_id = $objp->fk_product; - $ligne->coef = $objp->coef; - - $ligne->libelle = $objp->label; // Label produit - $ligne->product_desc = $objp->product_desc; // Description produit - $ligne->ref = $objp->ref; - - $this->lignes[$i] = $ligne; - //dolibarr_syslog("1 ".$ligne->desc); - //dolibarr_syslog("2 ".$ligne->product_desc); - $i++; - } - $this->db->free($result); - } - else - { - $this->error=$this->db->error(); - dolibarr_syslog("Commande::Fetch Erreur sql=$sql, ".$this->error); - return -1; - } - - - // -------- exp pdf // + $this->lignes = $this->fetch_lignes(); /* * Propale associée @@ -879,28 +854,22 @@ class Commande } } - /** - * - * - */ + /** + * \brief Reinitialise le tableau lignes + * \param only_product Ne renvoie que ligne liées à des produits physiques + * \return array Tableau de CommandeLigne + */ function fetch_lignes($only_product=0) { $this->lignes = array(); - $sql = 'SELECT l.fk_product, l.fk_commande, l.description, l.price, l.qty, l.rowid, l.tva_tx, l.remise_percent, l.subprice'; - if ($only_product==1) - { - $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; - $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product)'; - $sql .= ' WHERE l.fk_commande = '.$this->id; - $sql .= ' AND p.fk_product_type <> 1'; - $sql .= ' ORDER BY l.rowid'; - } - else - { - $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; - $sql .= ' WHERE l.fk_commande = '.$this->id; - $sql .= ' ORDER BY l.rowid'; - } + $sql = 'SELECT l.fk_product, l.fk_commande, l.description, l.price, l.qty, l.rowid, l.tva_tx,'; + $sql.= ' l.remise_percent, l.subprice, l.rang, l.coef'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product)'; + $sql.= ' WHERE l.fk_commande = '.$this->id; + if ($only_product==1) $sql .= ' AND p.fk_product_type = 0'; + $sql .= ' ORDER BY l.rang'; + $result = $this->db->query($sql); if ($result) { @@ -908,17 +877,28 @@ class Commande $i = 0; while ($i < $num) { - $ligne = new CommandeLigne(); $objp = $this->db->fetch_object($result); - $ligne->id = $objp->rowid; - $ligne->commande_id = $objp->fk_commande; + + $ligne = new CommandeLigne($this->db); + $ligne->rowid = $objp->rowid; + $ligne->id = $objp->rowid; // \deprecated + $ligne->fk_commande = $objp->fk_commande; + $ligne->commande_id = $objp->fk_commande; // \deprecated + $ligne->desc = $objp->description; // Description ligne $ligne->qty = $objp->qty; - $ligne->price = $objp->price; $ligne->tva_tx = $objp->tva_tx; $ligne->subprice = $objp->subprice; $ligne->remise_percent = $objp->remise_percent; + $ligne->price = $objp->price; $ligne->product_id = $objp->fk_product; - $ligne->description = stripslashes($objp->description); + $ligne->coef = $objp->coef; + $ligne->rang = $objp->rang; + + $ligne->libelle = $objp->label; // Label produit + $ligne->product_desc = $objp->product_desc; // Description produit + $ligne->ref = $objp->ref; + + $this->lignes[$i] = $ligne; $i++; } @@ -2111,20 +2091,34 @@ class Commande class CommandeLigne { - // From llx_commandedet + var $db; + var $error; + + // From llx_commandedet + var $rowid; + var $fk_facture; + var $desc; // Description ligne + var $product_id; // Id produit prédéfini + var $qty; var $tva_tx; var $subprice; + var $remise; var $remise_percent; var $price; - var $product_id; // Id produit prédéfini - var $desc; // Description ligne + var $rang; var $coef; + + var $info_bits; // Bit 0: 0 si TVA normal - 1 si TVA NPR + var $total_ht; // Total HT de la ligne toute quantité et incluant la remise ligne + var $total_tva; // Total TVA de la ligne toute quantité et incluant la remise ligne + var $total_ttc; // Total TTC de la ligne toute quantité et incluant la remise ligne // From llx_product - var $libelle; // Label produit - var $product_desc; // Description produit - var $ref; // Reference produit + var $ref; // Reference produit + var $libelle; // Label produit + var $product_desc; // Description produit + /** * \brief Constructeur d'objets ligne de commande @@ -2173,7 +2167,105 @@ class CommandeLigne { dolibarr_print_error($this->db); } - } + } + + /** + * \brief Insère l'objet ligne de commande en base + * \return int <0 si ko, >0 si ok + */ + function insert() + { + $this->db->begin(); + + // Insertion dans base de la ligne + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet'; + $sql.= ' (fk_commande, description, price, qty, tva_tx,'; + $sql.= ' fk_product, remise_percent, subprice, remise, fk_remise_except, '; + $sql.= ' rang, coef,'; + $sql.= ' info_bits, total_ht, total_tva, total_ttc)'; + $sql.= " VALUES (".$this->fk_commande.","; + $sql.= " '".addslashes($this->desc)."',"; + $sql.= " '".price2num($this->price)."',"; + $sql.= " '".price2num($this->qty)."',"; + $sql.= " '".price2num($this->txtva)."',"; + if ($this->fk_product) { $sql.= "'".$this->fk_product."',"; } + else { $sql.='0,'; } + $sql.= " '".price2num($this->remise_percent)."',"; + $sql.= " '".price2num($this->subprice)."',"; + $sql.= " '".price2num($this->remise)."',"; + if ($this->fk_remise_except) $sql.= $this->fk_remise_except.","; + else $sql.= 'null,'; + $sql.= ' '.$this->rang.','; + if (isset($this->coef)) $sql.= ' '.$this->coef.','; + else $sql.= ' null,'; + $sql.= " '".$this->info_bits."',"; + $sql.= " '".price2num($this->total_ht)."',"; + $sql.= " '".price2num($this->total_tva)."',"; + $sql.= " '".price2num($this->total_ttc)."'"; + $sql.= ')'; + + dolibarr_syslog("CommandeLigne::insert sql=$sql"); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("CommandeLigne::insert Error ".$this->error); + $this->db->rollback(); + return -2; + } + } + + + /** + * \brief Mise a jour de l'objet ligne de commande en base + * \return int <0 si ko, >0 si ok + */ + function update() + { + $this->db->begin(); + + // Mise a jour ligne en base + $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET"; + $sql.= " description='".addslashes($this->desc)."'"; + $sql.= ",price='".price2num($this->price)."'"; + $sql.= ",subprice='".price2num($this->subprice)."'"; + $sql.= ",remise='".price2num($this->remise)."'"; + $sql.= ",remise_percent='".price2num($this->remise_percent)."'"; + if ($fk_remise_except) $sql.= ",fk_remise_except=".$this->fk_remise_except; + else $sql.= ",fk_remise_except=null"; + $sql.= ",tva_taux='".price2num($this->txtva)."'"; + $sql.= ",qty='".price2num($this->qty)."'"; + $sql.= ",rang='".$this->rang."'"; + $sql.= ",coef='".$this->coef."'"; + $sql.= ",info_bits='".$this->info_bits."'"; + $sql.= ",total_ht='".price2num($this->total_ht)."'"; + $sql.= ",total_tva='".price2num($this->total_tva)."'"; + $sql.= ",total_ttc='".price2num($this->total_ttc)."'"; + $sql.= " WHERE rowid = ".$this->rowid; + + dolibarr_syslog("CommandeLigne::update sql=$sql"); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("CommandeLigne::update Error ".$this->error); + $this->db->rollback(); + return -2; + } + } + } ?> diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index 4aa5010f99a..2bfd8daee3e 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -1054,7 +1054,6 @@ class Facture // Récupère rang max de la facture dans $rangmax $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'facturedet'; $sql.= ' WHERE fk_facture ='.$facid; - $resql = $this->db->query($sql); if ($resql) { @@ -1068,36 +1067,32 @@ class Facture return -1; } - // Insertion dans base de la ligne - $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facturedet'; - $sql.= ' (fk_facture, description, price, qty, tva_taux,'; - $sql.= ' fk_product, remise_percent, subprice, remise, date_start, date_end, fk_code_ventilation, rang,'; - $sql.= ' info_bits, total_ht, total_tva, total_ttc)'; - $sql.= " VALUES (".$facid.", '".addslashes($desc)."',"; - $sql.= "'".price2num($price)."',"; - $sql.= "'".price2num($qty)."',"; - $sql.= "'".price2num($txtva)."',"; - if ($fk_product) { $sql.= "'$fk_product',"; } - else { $sql.='0,'; } - $sql.= " '".price2num($remise_percent)."',"; - $sql.= " '".price2num($subprice)."',"; - $sql.= " '".price2num($remise)."',"; - if ($datestart) { $sql.= "'$datestart',"; } - else { $sql.='null,'; } - if ($dateend) { $sql.= "'$dateend',"; } - else { $sql.='null,'; } - $sql.= ' '.$ventil.','; - $sql.= ' '.($rangmax + 1).','; - $sql.= " '".$info_bits."',"; - $sql.= " '".price2num($total_ht)."',"; - $sql.= " '".price2num($total_tva)."',"; - $sql.= " '".price2num($total_ttc)."'"; - $sql.= ')'; - if ( $this->db->query($sql) ) + // Insertion ligne + $ligne=new FactureLigne($this->db); + + $ligne->fk_facture=$facid; + $ligne->desc=$desc; + $ligne->price=$price; + $ligne->qty=$qty; + $ligne->txtva=$txtva; + $ligne->fk_product=$fk_product; + $ligne->remise_percent=$remise_percent; + $ligne->subprice=$subprice; + $ligne->remise=$remise; + $ligne->datestart=$datestart; + $ligne->dateend=$dateend; + $ligne->ventil=$ventil; + $ligne->rang=($rangmax+1); + $ligne->info_bits=$info_bits; + $ligne->total_ht=$total_ht; + $ligne->total_tva=$total_tva; + $ligne->total_ttc=$total_ttc; + + $result=$ligne->insert(); + if ($result > 0) { // Mise a jour informations denormalisees au niveau de la facture meme $result=$this->update_price($facid); - if ($result > 0) { $this->db->commit(); @@ -1113,8 +1108,7 @@ class Facture } else { - $this->error=$this->db->error(); - dolibarr_syslog("Error sql=$sql, error=".$this->error); + $this->error=$ligne->error; $this->db->rollback(); return -2; } @@ -1171,25 +1165,24 @@ class Facture $subprice = price2num($subprice); // Mise a jour ligne en base - $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET"; - $sql.= " description='".addslashes($desc)."'"; - $sql.= ",price='".price2num($price)."'"; - $sql.= ",subprice='".price2num($subprice)."'"; - $sql.= ",remise='".price2num($remise)."'"; - $sql.= ",remise_percent='".price2num($remise_percent)."'"; - $sql.= ",tva_taux='".price2num($txtva)."'"; - $sql.= ",qty='".price2num($qty)."'"; - if ($datestart) { $sql.= ",date_start='$datestart'"; } - else { $sql.=',date_start=null'; } - if ($dateend) { $sql.= ",date_end='$dateend'"; } - else { $sql.=',date_end=null'; } - //$sql.= " info_bits=".$info_bits.","; - $sql.= ",total_ht='".price2num($total_ht)."'"; - $sql.= ",total_tva='".price2num($total_tva)."'"; - $sql.= ",total_ttc='".price2num($total_ttc)."'"; - $sql.= " WHERE rowid = ".$rowid; + $ligne=new FactureLigne($this->db); + $ligne->rowid=$rowid; + $ligne->fetch($rowid); + + $ligne->desc=$desc; + $ligne->price=$price; + $ligne->qty=$qty; + $ligne->txtva=$txtva; + $ligne->remise_percent=$remise_percent; + $ligne->subprice=$subprice; + $ligne->remise=$remise; + $ligne->datestart=$datestart; + $ligne->dateend=$dateend; + $ligne->total_ht=$total_ht; + $ligne->total_tva=$total_tva; + $ligne->total_ttc=$total_ttc; - $result = $this->db->query( $sql); + $result=$ligne->update(); if ($result > 0) { // Mise a jour info denormalisees au niveau facture @@ -1199,7 +1192,6 @@ class Facture } else { - $this->error=$this->db->error(); $this->db->rollback(); return -1; } @@ -2376,29 +2368,38 @@ class Facture class FactureLigne { var $db; + var $error; + // From llx_facturedet var $rowid; - var $desc; - var $produit_id; + var $fk_facture; + var $desc; // Description ligne + var $product_id; // Id produit prédéfini var $qty; // Quantité (exemple 2) var $subprice; // P.U. HT (exemple 100) + var $remise; // Montant calculé de la remise % sur PU HT (exemple 20) + // subprice = price + remise var $remise_percent; // % de la remise ligne (exemple 20%) var $price; // P.U. HT apres remise % de ligne (exemple 80) var $tva_taux; // Taux tva produit/service (exemple 19.6) - var $remise; // Montant calculé de la remise % sur PU HT (exemple 20) - // subprice = price + remise + var $fk_code_ventilation = 0; + var $fk_export_compta = 0; + var $rang = 0; var $date_start; var $date_end; - var $info_bits; // Bit 0: 0 si TVA normal - 1 si TVA NPR + var $info_bits = 0; // Bit 0: 0 si TVA normal - 1 si TVA NPR var $total_ht; // Total HT de la ligne toute quantité et incluant la remise ligne var $total_tva; // Total TVA de la ligne toute quantité et incluant la remise ligne var $total_ttc; // Total TTC de la ligne toute quantité et incluant la remise ligne - var $error; - + // From llx_product + var $ref; // Reference produit + var $libelle; // Label produit + var $product_desc; // Description produit + /** * \brief Constructeur d'objets ligne de facture @@ -2416,12 +2417,15 @@ class FactureLigne */ function fetch($rowid) { - $sql = 'SELECT fk_facture, fk_product, description, price, qty, rowid, tva_taux,'; - $sql.= ' remise, remise_percent, fk_remise_except, subprice,'; - $sql.= ' '.$this->db->pdate('date_start').' as date_start,'.$this->db->pdate('date_end').' as date_end,'; - $sql.= ' info_bits, total_ht, total_tva, total_ttc, rang,'; - $sql.= ' fk_code_ventilation, fk_export_compta'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid; + $sql = 'SELECT fd.rowid, fd.fk_facture, fd.fk_product, fd.description, fd.price, fd.qty, fd.tva_taux,'; + $sql.= ' fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice,'; + $sql.= ' '.$this->db->pdate('fd.date_start').' as date_start,'.$this->db->pdate('fd.date_end').' as date_end,'; + $sql.= ' fd.info_bits, fd.total_ht, fd.total_tva, fd.total_ttc, fd.rang,'; + $sql.= ' fd.fk_code_ventilation, fd.fk_export_compta,'; + $sql.= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as fd'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON fd.fk_product = p.rowid'; + $sql.= ' WHERE fd.rowid = '.$rowid; $result = $this->db->query($sql); if ($result) { @@ -2445,6 +2449,11 @@ class FactureLigne $this->fk_code_ventilation = $objp->fk_code_ventilation; $this->fk_export_compta = $objp->fk_export_compta; $this->rang = $objp->rang; + + $this->ref = $objp->product_ref; + $this->libelle = $objp->product_libelle; + $this->product_desc = $objp->product_desc; + $this->db->free($result); } else @@ -2453,6 +2462,111 @@ class FactureLigne } } + + /** + * \brief Insère l'objet ligne de facture en base + * \return int <0 si ko, >0 si ok + */ + function insert() + { + $this->db->begin(); + + // Insertion dans base de la ligne + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facturedet'; + $sql.= ' (fk_facture, description, price, qty, tva_taux,'; + $sql.= ' fk_product, remise_percent, subprice, remise, fk_remise_except,'; + $sql.= ' date_start, date_end, fk_code_ventilation, fk_export_compta, '; + $sql.= ' rang,'; + $sql.= ' info_bits, total_ht, total_tva, total_ttc)'; + $sql.= " VALUES (".$this->fk_facture.","; + $sql.= " '".addslashes($this->desc)."',"; + $sql.= " '".price2num($this->price)."',"; + $sql.= " '".price2num($this->qty)."',"; + $sql.= " '".price2num($this->txtva)."',"; + if ($this->fk_product) { $sql.= "'".$this->fk_product."',"; } + else { $sql.='0,'; } + $sql.= " '".price2num($this->remise_percent)."',"; + $sql.= " '".price2num($this->subprice)."',"; + $sql.= " '".price2num($this->remise)."',"; + if ($this->fk_remise_except) $sql.= $this->fk_remise_except.","; + else $sql.= 'null,'; + if ($this->datestart) { $sql.= "'".$this->datestart."',"; } + else { $sql.='null,'; } + if ($this->dateend) { $sql.= "'".$this->dateend."',"; } + else { $sql.='null,'; } + $sql.= ' '.$this->fk_code_ventilation.','; + $sql.= ' '.$this->fk_export_compta.','; + $sql.= ' '.$this->rang.','; + $sql.= " '".$this->info_bits."',"; + $sql.= " '".price2num($this->total_ht)."',"; + $sql.= " '".price2num($this->total_tva)."',"; + $sql.= " '".price2num($this->total_ttc)."'"; + $sql.= ')'; + + dolibarr_syslog("FactureLigne::insert sql=$sql"); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("FactureLigne::insert Error ".$this->error); + $this->db->rollback(); + return -2; + } + } + + + /** + * \brief Mise a jour de l'objet ligne de facture en base + * \return int <0 si ko, >0 si ok + */ + function update() + { + $this->db->begin(); + + // Mise a jour ligne en base + $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET"; + $sql.= " description='".addslashes($this->desc)."'"; + $sql.= ",price='".price2num($this->price)."'"; + $sql.= ",subprice='".price2num($this->subprice)."'"; + $sql.= ",remise='".price2num($this->remise)."'"; + $sql.= ",remise_percent='".price2num($this->remise_percent)."'"; + if ($fk_remise_except) $sql.= ",fk_remise_except=".$this->fk_remise_except; + else $sql.= ",fk_remise_except=null"; + $sql.= ",tva_taux='".price2num($this->txtva)."'"; + $sql.= ",qty='".price2num($this->qty)."'"; + if ($this->datestart) { $sql.= ",date_start='".$this->datestart."'"; } + else { $sql.=',date_start=null'; } + if ($this->dateend) { $sql.= ",date_end='".$this->dateend."'"; } + else { $sql.=',date_end=null'; } + $sql.= ",rang='".$this->rang."'"; + $sql.= ",info_bits='".$this->info_bits."'"; + $sql.= ",total_ht='".price2num($this->total_ht)."'"; + $sql.= ",total_tva='".price2num($this->total_tva)."'"; + $sql.= ",total_ttc='".price2num($this->total_ttc)."'"; + $sql.= " WHERE rowid = ".$this->rowid; + + dolibarr_syslog("FactureLigne::update sql=$sql"); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("FactureLigne::update Error ".$this->error); + $this->db->rollback(); + return -2; + } + } } ?> diff --git a/htdocs/propal.class.php b/htdocs/propal.class.php index 7c68bb9cb3c..05e09c37c2f 100644 --- a/htdocs/propal.class.php +++ b/htdocs/propal.class.php @@ -206,18 +206,42 @@ class Propal $price = $pu - $remise; } - $sql = "INSERT INTO ".MAIN_DB_PREFIX."propaldet (fk_propal, fk_product, qty, price, tva_tx, description, remise_percent, subprice, total_ht, total_tva, total_ttc)"; - $sql.= " VALUES "; - $sql.= " (".$this->id.", "; - if ($fk_product) { $sql.= "'$fk_product',"; } - else { $sql.='0,'; } - $sql.= " '". $qty."','". price2num($price)."','".$txtva."','".addslashes($desc)."','".price2num($remise_percent)."', '".price2num($subprice)."',"; - $sql.= " '".price2num($total_ht) ."',"; - $sql.= " '".price2num($total_tva)."',"; - $sql.= " '".price2num($total_ttc)."'"; - $sql.= ")"; + // Récupère rang max de la propale dans $rangmax + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'propaldet'; + $sql.= ' WHERE fk_propal ='.$propalid; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + $rangmax = $row[0]; + } + else + { + dolibarr_print_error($this->db); + $this->db->rollback(); + return -1; + } - if ($this->db->query($sql)) + // Insertion ligne + $ligne=new PropaleLigne($this->db); + + $ligne->fk_propal=$propalid; + $ligne->desc=$desc; + $ligne->price=$price; + $ligne->qty=$qty; + $ligne->txtva=$txtva; + $ligne->fk_product=$fk_product; + $ligne->remise_percent=$remise_percent; + $ligne->subprice=$subprice; + $ligne->remise=$remise; + $ligne->rang=($rangmax+1); + $ligne->info_bits=$info_bits; + $ligne->total_ht=$total_ht; + $ligne->total_tva=$total_tva; + $ligne->total_ttc=$total_ttc; + + $result=$ligne->insert(); + if ($result > 0) { // Mise a jour informations denormalisees au niveau de la facture meme $result=$this->update_price($facid); @@ -237,8 +261,7 @@ class Propal } else { - $this->error=$this->db->error(); - dolibarr_syslog("Error sql=$sql, error=".$this->error); + $this->error=$ligne->error; $this->db->rollback(); return -2; } @@ -2311,19 +2334,33 @@ class Propal class PropaleLigne { + var $db; + var $error; + // From llx_propaldet + var $rowid; + var $fk_propal; + var $desc; // Description ligne + var $product_id; // Id produit prédéfini + var $qty; var $tva_tx; var $subprice; + var $remise; var $remise_percent; var $price; - var $desc; // Description ligne - var $product_id; // Id produit prédéfini + var $rang; + var $coef; + + var $info_bits; // Bit 0: 0 si TVA normal - 1 si TVA NPR + var $total_ht; // Total HT de la ligne toute quantité et incluant la remise ligne + var $total_tva; // Total TVA de la ligne toute quantité et incluant la remise ligne + var $total_ttc; // Total TTC de la ligne toute quantité et incluant la remise ligne // From llx_product - var $libelle; // Label produit - var $product_desc; // Description produit - var $ref; // Reference produit + var $ref; // Reference produit + var $libelle; // Label produit + var $product_desc; // Description produit /** @@ -2371,7 +2408,105 @@ class PropaleLigne { dolibarr_print_error($this->db); } - } + } + + /** + * \brief Insère l'objet ligne de propal en base + * \return int <0 si ko, >0 si ok + */ + function insert() + { + $this->db->begin(); + + // Insertion dans base de la ligne + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'propaldet'; + $sql.= ' (fk_propal, description, price, qty, tva_tx,'; + $sql.= ' fk_product, remise_percent, subprice, remise, fk_remise_except, '; + $sql.= ' rang, coef,'; + $sql.= ' info_bits, total_ht, total_tva, total_ttc)'; + $sql.= " VALUES (".$this->fk_propal.","; + $sql.= " '".addslashes($this->desc)."',"; + $sql.= " '".price2num($this->price)."',"; + $sql.= " '".price2num($this->qty)."',"; + $sql.= " '".price2num($this->txtva)."',"; + if ($this->fk_product) { $sql.= "'".$this->fk_product."',"; } + else { $sql.='0,'; } + $sql.= " '".price2num($this->remise_percent)."',"; + $sql.= " '".price2num($this->subprice)."',"; + $sql.= " '".price2num($this->remise)."',"; + if ($this->fk_remise_except) $sql.= $this->fk_remise_except.","; + else $sql.= 'null,'; + $sql.= ' '.$this->rang.','; + if (isset($this->coef)) $sql.= ' '.$this->coef.','; + else $sql.= ' null,'; + $sql.= " '".$this->info_bits."',"; + $sql.= " '".price2num($this->total_ht)."',"; + $sql.= " '".price2num($this->total_tva)."',"; + $sql.= " '".price2num($this->total_ttc)."'"; + $sql.= ')'; + + dolibarr_syslog("PropaleLigne::insert sql=$sql"); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("PropaleLigne::insert Error ".$this->error); + $this->db->rollback(); + return -2; + } + } + + + /** + * \brief Mise a jour de l'objet ligne de propale en base + * \return int <0 si ko, >0 si ok + */ + function update() + { + $this->db->begin(); + + // Mise a jour ligne en base + $sql = "UPDATE ".MAIN_DB_PREFIX."propaledet SET"; + $sql.= " description='".addslashes($this->desc)."'"; + $sql.= ",price='".price2num($this->price)."'"; + $sql.= ",subprice='".price2num($this->subprice)."'"; + $sql.= ",remise='".price2num($this->remise)."'"; + $sql.= ",remise_percent='".price2num($this->remise_percent)."'"; + if ($fk_remise_except) $sql.= ",fk_remise_except=".$this->fk_remise_except; + else $sql.= ",fk_remise_except=null"; + $sql.= ",tva_taux='".price2num($this->txtva)."'"; + $sql.= ",qty='".price2num($this->qty)."'"; + $sql.= ",rang='".$this->rang."'"; + $sql.= ",coef='".$this->coef."'"; + $sql.= ",info_bits='".$this->info_bits."'"; + $sql.= ",total_ht='".price2num($this->total_ht)."'"; + $sql.= ",total_tva='".price2num($this->total_tva)."'"; + $sql.= ",total_ttc='".price2num($this->total_ttc)."'"; + $sql.= " WHERE rowid = ".$this->rowid; + + dolibarr_syslog("PropaleLigne::update sql=$sql"); + + $resql=$this->db->query($sql); + if ($resql) + { + $this->db->commit(); + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("PropaleLigne::update Error ".$this->error); + $this->db->rollback(); + return -2; + } + } + } ?>