Qual: Uniformisation du code php pour gérer les lignes de propal, facture et commande.

This commit is contained in:
Laurent Destailleur 2006-06-17 16:52:30 +00:00
parent f62589158b
commit 99bca7259c
3 changed files with 516 additions and 175 deletions

View File

@ -485,18 +485,42 @@ class Commande
$price = $pu - $remise; $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)"; // Récupère rang max de la commande dans $rangmax
$sql.= " VALUES "; $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'commandedet';
$sql.= " (".$this->id.", "; $sql.= ' WHERE fk_commande ='.$commandeid;
if ($fk_product) { $sql.= "'$fk_product',"; } $resql = $this->db->query($sql);
else { $sql.='0,'; } if ($resql)
$sql.= " '". $qty."','". price2num($price)."','".$txtva."','".addslashes($desc)."','".price2num($remise_percent)."', '".price2num($subprice)."',"; {
$sql.= " '".price2num($total_ht) ."',"; $row = $this->db->fetch_row($resql);
$sql.= " '".price2num($total_tva)."',"; $rangmax = $row[0];
$sql.= " '".price2num($total_ttc)."'"; }
$sql.= ")"; 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 // Mise a jour informations denormalisees au niveau de la facture meme
$result=$this->update_price($this->id); $result=$this->update_price($this->id);
@ -516,8 +540,7 @@ class Commande
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$ligne->error;
dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback(); $this->db->rollback();
return -2; return -2;
} }
@ -732,6 +755,7 @@ class Commande
$this->mode_reglement_id = $obj->fk_mode_reglement; $this->mode_reglement_id = $obj->fk_mode_reglement;
$this->date_livraison = $obj->date_livraison; $this->date_livraison = $obj->date_livraison;
$this->adresse_livraison_id = $obj->fk_adresse_livraison; $this->adresse_livraison_id = $obj->fk_adresse_livraison;
if ($this->statut == 0) $this->brouillon = 1;
$this->db->free(); $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 = $this->fetch_lignes();
$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 //
/* /*
* Propale associée * Propale associée
@ -880,27 +855,21 @@ 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) function fetch_lignes($only_product=0)
{ {
$this->lignes = array(); $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'; $sql = 'SELECT l.fk_product, l.fk_commande, l.description, l.price, l.qty, l.rowid, l.tva_tx,';
if ($only_product==1) $sql.= ' l.remise_percent, l.subprice, l.rang, l.coef';
{ $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l';
$sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product)';
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (p.rowid = l.fk_product)'; $sql.= ' WHERE l.fk_commande = '.$this->id;
$sql .= ' WHERE l.fk_commande = '.$this->id; if ($only_product==1) $sql .= ' AND p.fk_product_type = 0';
$sql .= ' AND p.fk_product_type <> 1'; $sql .= ' ORDER BY l.rang';
$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';
}
$result = $this->db->query($sql); $result = $this->db->query($sql);
if ($result) if ($result)
{ {
@ -908,17 +877,28 @@ class Commande
$i = 0; $i = 0;
while ($i < $num) while ($i < $num)
{ {
$ligne = new CommandeLigne();
$objp = $this->db->fetch_object($result); $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->qty = $objp->qty;
$ligne->price = $objp->price;
$ligne->tva_tx = $objp->tva_tx; $ligne->tva_tx = $objp->tva_tx;
$ligne->subprice = $objp->subprice; $ligne->subprice = $objp->subprice;
$ligne->remise_percent = $objp->remise_percent; $ligne->remise_percent = $objp->remise_percent;
$ligne->price = $objp->price;
$ligne->product_id = $objp->fk_product; $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; $this->lignes[$i] = $ligne;
$i++; $i++;
} }
@ -2111,20 +2091,34 @@ class Commande
class CommandeLigne class CommandeLigne
{ {
var $db;
var $error;
// From llx_commandedet // From llx_commandedet
var $rowid;
var $fk_facture;
var $desc; // Description ligne
var $product_id; // Id produit prédéfini
var $qty; var $qty;
var $tva_tx; var $tva_tx;
var $subprice; var $subprice;
var $remise;
var $remise_percent; var $remise_percent;
var $price; var $price;
var $product_id; // Id produit prédéfini var $rang;
var $desc; // Description ligne
var $coef; 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 // From llx_product
var $ref; // Reference produit
var $libelle; // Label produit var $libelle; // Label produit
var $product_desc; // Description produit var $product_desc; // Description produit
var $ref; // Reference produit
/** /**
* \brief Constructeur d'objets ligne de commande * \brief Constructeur d'objets ligne de commande
@ -2174,6 +2168,104 @@ class CommandeLigne
dolibarr_print_error($this->db); 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;
}
}
} }
?> ?>

View File

@ -1054,7 +1054,6 @@ class Facture
// Récupère rang max de la facture dans $rangmax // Récupère rang max de la facture dans $rangmax
$sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'facturedet'; $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'facturedet';
$sql.= ' WHERE fk_facture ='.$facid; $sql.= ' WHERE fk_facture ='.$facid;
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -1068,36 +1067,32 @@ class Facture
return -1; return -1;
} }
// Insertion dans base de la ligne // Insertion ligne
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facturedet'; $ligne=new FactureLigne($this->db);
$sql.= ' (fk_facture, description, price, qty, tva_taux,';
$sql.= ' fk_product, remise_percent, subprice, remise, date_start, date_end, fk_code_ventilation, rang,'; $ligne->fk_facture=$facid;
$sql.= ' info_bits, total_ht, total_tva, total_ttc)'; $ligne->desc=$desc;
$sql.= " VALUES (".$facid.", '".addslashes($desc)."',"; $ligne->price=$price;
$sql.= "'".price2num($price)."',"; $ligne->qty=$qty;
$sql.= "'".price2num($qty)."',"; $ligne->txtva=$txtva;
$sql.= "'".price2num($txtva)."',"; $ligne->fk_product=$fk_product;
if ($fk_product) { $sql.= "'$fk_product',"; } $ligne->remise_percent=$remise_percent;
else { $sql.='0,'; } $ligne->subprice=$subprice;
$sql.= " '".price2num($remise_percent)."',"; $ligne->remise=$remise;
$sql.= " '".price2num($subprice)."',"; $ligne->datestart=$datestart;
$sql.= " '".price2num($remise)."',"; $ligne->dateend=$dateend;
if ($datestart) { $sql.= "'$datestart',"; } $ligne->ventil=$ventil;
else { $sql.='null,'; } $ligne->rang=($rangmax+1);
if ($dateend) { $sql.= "'$dateend',"; } $ligne->info_bits=$info_bits;
else { $sql.='null,'; } $ligne->total_ht=$total_ht;
$sql.= ' '.$ventil.','; $ligne->total_tva=$total_tva;
$sql.= ' '.($rangmax + 1).','; $ligne->total_ttc=$total_ttc;
$sql.= " '".$info_bits."',";
$sql.= " '".price2num($total_ht)."',"; $result=$ligne->insert();
$sql.= " '".price2num($total_tva)."',"; if ($result > 0)
$sql.= " '".price2num($total_ttc)."'";
$sql.= ')';
if ( $this->db->query($sql) )
{ {
// Mise a jour informations denormalisees au niveau de la facture meme // Mise a jour informations denormalisees au niveau de la facture meme
$result=$this->update_price($facid); $result=$this->update_price($facid);
if ($result > 0) if ($result > 0)
{ {
$this->db->commit(); $this->db->commit();
@ -1113,8 +1108,7 @@ class Facture
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$ligne->error;
dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback(); $this->db->rollback();
return -2; return -2;
} }
@ -1171,25 +1165,24 @@ class Facture
$subprice = price2num($subprice); $subprice = price2num($subprice);
// Mise a jour ligne en base // Mise a jour ligne en base
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET"; $ligne=new FactureLigne($this->db);
$sql.= " description='".addslashes($desc)."'"; $ligne->rowid=$rowid;
$sql.= ",price='".price2num($price)."'"; $ligne->fetch($rowid);
$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;
$result = $this->db->query( $sql); $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=$ligne->update();
if ($result > 0) if ($result > 0)
{ {
// Mise a jour info denormalisees au niveau facture // Mise a jour info denormalisees au niveau facture
@ -1199,7 +1192,6 @@ class Facture
} }
else else
{ {
$this->error=$this->db->error();
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
@ -2376,28 +2368,37 @@ class Facture
class FactureLigne class FactureLigne
{ {
var $db; var $db;
var $error;
// From llx_facturedet
var $rowid; var $rowid;
var $desc; var $fk_facture;
var $produit_id; var $desc; // Description ligne
var $product_id; // Id produit prédéfini
var $qty; // Quantité (exemple 2) var $qty; // Quantité (exemple 2)
var $subprice; // P.U. HT (exemple 100) 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 $remise_percent; // % de la remise ligne (exemple 20%)
var $price; // P.U. HT apres remise % de ligne (exemple 80) var $price; // P.U. HT apres remise % de ligne (exemple 80)
var $tva_taux; // Taux tva produit/service (exemple 19.6) var $tva_taux; // Taux tva produit/service (exemple 19.6)
var $remise; // Montant calculé de la remise % sur PU HT (exemple 20) var $fk_code_ventilation = 0;
// subprice = price + remise var $fk_export_compta = 0;
var $rang = 0;
var $date_start; var $date_start;
var $date_end; 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_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_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 $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
/** /**
@ -2416,12 +2417,15 @@ class FactureLigne
*/ */
function fetch($rowid) function fetch($rowid)
{ {
$sql = 'SELECT fk_facture, fk_product, description, price, qty, rowid, tva_taux,'; $sql = 'SELECT fd.rowid, fd.fk_facture, fd.fk_product, fd.description, fd.price, fd.qty, fd.tva_taux,';
$sql.= ' remise, remise_percent, fk_remise_except, subprice,'; $sql.= ' fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice,';
$sql.= ' '.$this->db->pdate('date_start').' as date_start,'.$this->db->pdate('date_end').' as date_end,'; $sql.= ' '.$this->db->pdate('fd.date_start').' as date_start,'.$this->db->pdate('fd.date_end').' as date_end,';
$sql.= ' info_bits, total_ht, total_tva, total_ttc, rang,'; $sql.= ' fd.info_bits, fd.total_ht, fd.total_tva, fd.total_ttc, fd.rang,';
$sql.= ' fk_code_ventilation, fk_export_compta'; $sql.= ' fd.fk_code_ventilation, fd.fk_export_compta,';
$sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid; $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); $result = $this->db->query($sql);
if ($result) if ($result)
{ {
@ -2445,6 +2449,11 @@ class FactureLigne
$this->fk_code_ventilation = $objp->fk_code_ventilation; $this->fk_code_ventilation = $objp->fk_code_ventilation;
$this->fk_export_compta = $objp->fk_export_compta; $this->fk_export_compta = $objp->fk_export_compta;
$this->rang = $objp->rang; $this->rang = $objp->rang;
$this->ref = $objp->product_ref;
$this->libelle = $objp->product_libelle;
$this->product_desc = $objp->product_desc;
$this->db->free($result); $this->db->free($result);
} }
else 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;
}
}
} }
?> ?>

View File

@ -206,18 +206,42 @@ class Propal
$price = $pu - $remise; $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)"; // Récupère rang max de la propale dans $rangmax
$sql.= " VALUES "; $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'propaldet';
$sql.= " (".$this->id.", "; $sql.= ' WHERE fk_propal ='.$propalid;
if ($fk_product) { $sql.= "'$fk_product',"; } $resql = $this->db->query($sql);
else { $sql.='0,'; } if ($resql)
$sql.= " '". $qty."','". price2num($price)."','".$txtva."','".addslashes($desc)."','".price2num($remise_percent)."', '".price2num($subprice)."',"; {
$sql.= " '".price2num($total_ht) ."',"; $row = $this->db->fetch_row($resql);
$sql.= " '".price2num($total_tva)."',"; $rangmax = $row[0];
$sql.= " '".price2num($total_ttc)."'"; }
$sql.= ")"; 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 // Mise a jour informations denormalisees au niveau de la facture meme
$result=$this->update_price($facid); $result=$this->update_price($facid);
@ -237,8 +261,7 @@ class Propal
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$ligne->error;
dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback(); $this->db->rollback();
return -2; return -2;
} }
@ -2311,19 +2334,33 @@ class Propal
class PropaleLigne class PropaleLigne
{ {
var $db;
var $error;
// From llx_propaldet // From llx_propaldet
var $qty; var $rowid;
var $tva_tx; var $fk_propal;
var $subprice;
var $remise_percent;
var $price;
var $desc; // Description ligne var $desc; // Description ligne
var $product_id; // Id produit prédéfini var $product_id; // Id produit prédéfini
var $qty;
var $tva_tx;
var $subprice;
var $remise;
var $remise_percent;
var $price;
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 // From llx_product
var $ref; // Reference produit
var $libelle; // Label produit var $libelle; // Label produit
var $product_desc; // Description produit var $product_desc; // Description produit
var $ref; // Reference produit
/** /**
@ -2372,6 +2409,104 @@ class PropaleLigne
dolibarr_print_error($this->db); 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;
}
}
} }
?> ?>