Qual: Remove duplicate code

This commit is contained in:
Laurent Destailleur 2008-03-07 10:34:16 +00:00
parent a59aca5b8c
commit c1edb57981
13 changed files with 428 additions and 627 deletions

View File

@ -119,7 +119,7 @@ if ($_REQUEST['action'] == 'confirm_validate' && $_REQUEST['confirm'] == 'yes')
{
$propal = new Propal($db);
$propal->fetch($_GET['propalid']);
$result=$propal->update_price($_GET['propalid']);
$result=$propal->update_price();
if ($_REQUEST['lang_id'])
{
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs",$conf);

View File

@ -633,28 +633,28 @@ class Commande extends CommonObject
dolibarr_syslog("Commande::addline commandeid=$commandeid, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
// Clean parameters
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
if (! $qty) $qty=1;
if (! $info_bits) $info_bits=0;
$pu_ht=price2num($pu_ht);
$pu_ttc=price2num($pu_ttc);
$txtva = price2num($txtva);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
$desc=trim($desc);
if ($this->statut == 0)
{
$this->db->begin();
// Nettoyage param<61>tres
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
if (! $qty) $qty=1;
if (! $info_bits) $info_bits=0;
$pu_ht=price2num($pu_ht);
$pu_ttc=price2num($pu_ttc);
$txtva = price2num($txtva);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
// Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
@ -699,6 +699,7 @@ class Commande extends CommonObject
if ($result > 0)
{
// Mise a jour informations denormalisees au niveau de la commande meme
$this->id=$commandeid; // \TODO A virer
$result=$this->update_price($commandeid);
if ($result > 0)
{
@ -940,7 +941,7 @@ class Commande extends CommonObject
$result=$comligne->insert();
if ($result > 0)
{
$result=$this->update_price($this->id);
$result=$this->update_price();
if ($result > 0)
{
$this->db->commit();
@ -1273,7 +1274,7 @@ class Commande extends CommonObject
if ($this->db->query($sql))
{
$this->remise_percent = $remise;
$this->update_price($this->id);
$this->update_price();
return 1;
}
else
@ -1308,7 +1309,7 @@ class Commande extends CommonObject
if ($this->db->query($sql))
{
$this->remise_absolue = $remise;
$this->update_price($this->id);
$this->update_price();
return 1;
}
else
@ -1319,73 +1320,6 @@ class Commande extends CommonObject
}
}
/**
* \brief Mets <EFBFBD> jour le prix total de la commnde
* \return int <0 si ko, >0 si ok
*/
function update_price()
{
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
$tvas=array();
$err=0;
// Liste des lignes factures a sommer
$sql = "SELECT price, qty, tva_tx, total_ht, total_tva, total_ttc";
$sql.= " FROM ".MAIN_DB_PREFIX."commandedet";
$sql.= " WHERE fk_commande = ".$this->id;
dolibarr_syslog("Commande::update_price sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
$this->total_ht = 0;
$this->total_tva = 0;
$this->total_ttc = 0;
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($result);
$this->total_ht += $obj->total_ht;
$this->total_tva += ($obj->total_ttc - $obj->total_ht);
$this->total_ttc += $obj->total_ttc;
$tvas[$obj->tva_taux] += ($obj->total_ttc - $obj->total_ht);
$i++;
}
$this->db->free($result);
// Met a jour indicateurs
$sql = "UPDATE ".MAIN_DB_PREFIX."commande SET";
$sql .= " total_ht='". price2num($this->total_ht)."',";
$sql .= " tva='". price2num($this->total_tva)."',";
$sql .= " total_ttc='".price2num($this->total_ttc)."'";
$sql .=" WHERE rowid = ".$this->id;
dolibarr_syslog("Facture::update_price sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Commande::update_price error=".$this->error,LOG_ERR);
return -1;
}
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Commande::update_price error=".$this->error,LOG_ERR);
return -1;
}
}
/**
* \brief D<EFBFBD>finit une date de livraison
@ -1704,7 +1638,7 @@ class Commande extends CommonObject
if ($result > 0)
{
// Mise a jour info denormalisees au niveau facture
$this->update_price($this->id);
$this->update_price();
if ($LigneOld->qty <> $qty && $LigneOld->produit_id)
{
@ -2189,7 +2123,7 @@ class CommandeLigne
*/
function CommandeLigne($DB)
{
$this->db= $DB ;
$this->db= $DB;
}
/**

View File

@ -823,6 +823,79 @@ class CommonObject
}
}
/**
* \brief Update total_ht, total_ttc and total_vat for an object (sum of lines)
* \return int <0 si ko, >0 si ok
*/
function update_price()
{
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
$err=0;
// List lines to sum
$fieldtva='total_tva';
if ($this->element == 'facture_fourn') $fieldtva='tva';
$sql = 'SELECT qty, total_ht, '.$fieldtva.' as total_tva, total_ttc';
$sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element_line;
$sql.= ' WHERE '.$this->fk_element.' = '.$this->id;
dolibarr_syslog("CommonObject::update_price sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$this->total_ht = 0;
$this->total_tva = 0;
$this->total_ttc = 0;
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$this->total_ht += $obj->total_ht;
$this->total_tva += ($obj->total_ttc - $obj->total_ht);
$this->total_ttc += $obj->total_ttc;
$i++;
}
$this->db->free($resql);
// Now update field total_ht, total_ttc and tva
$fieldht='total_ht';
if ($this->element == 'facture') $fieldht='total';
$fieldtva='tva';
if ($this->element == 'facture_fourn') $fieldtva='total_tva';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
$sql .= " ".$fieldht."='".price2num($this->total_ht)."',";
$sql .= " ".$fieldtva."='".price2num($this->total_tva)."',";
$sql .= " total_ttc='".price2num($this->total_ttc)."'";
$sql .= ' WHERE rowid = '.$this->id;
dolibarr_syslog("CommonObject::update_price sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("CommonObject::update_price error=".$this->error,LOG_ERR);
return -1;
}
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("CommonObject::update_price error=".$this->error,LOG_ERR);
return -1;
}
}
}
?>

View File

@ -39,8 +39,11 @@ require_once(DOL_DOCUMENT_ROOT."/facture.class.php");
class FactureRec extends Facture
{
var $db ;
var $element='facture';
var $element='facturerec';
var $table_element='facture_rec';
var $table_element_line='facturedet_rec';
var $fk_element='fk_facture';
var $id ;
var $socid; // Id client
@ -402,7 +405,8 @@ class FactureRec extends Facture
if ( $this->db->query( $sql) )
{
$this->update_price($facid);
$this->id=$facid; // \TODO A virer
$this->update_price();
return 1;
}
else
@ -413,63 +417,6 @@ class FactureRec extends Facture
}
}
/**
* \brief Mise à jour des sommes de la facture et calculs denormalises
* \param facid id de la facture a modifier
* \return int <0 si ko, >0 si ok
*/
function update_price($facid)
{
$tvas=array();
$err=0;
// Liste des lignes factures a sommer (Ne plus utiliser price)
$sql = 'SELECT qty, tva_taux, subprice, remise_percent, price,';
$sql.= ' total_ht, total_tva, total_ttc';
$sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec';
$sql.= ' WHERE fk_facture = '.$facid;
$resql = $this->db->query($sql);
if ($resql)
{
$this->total_ht = 0;
$this->total_tva = 0;
$this->total_ttc = 0;
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$this->total_ht += $obj->total_ht;
$this->total_tva += ($obj->total_ttc - $obj->total_ht);
$this->total_ttc += $obj->total_ttc;
// Ne plus utiliser amount, ni remise
$this->amount_ht += ($obj->price * $obj->qty);
$this->total_remise += 0; // Plus de remise globale (toute remise est sur une ligne)
$tvas[$obj->tva_taux] += ($obj->total_ttc - $obj->total_ht);
$i++;
}
$this->db->free($resql);
// Met a jour indicateurs sur facture
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_rec ';
$sql .= "SET amount ='".price2num($this->amount_ht)."'";
$sql .= ", remise='". price2num($this->total_remise)."'";
$sql .= ", total='". price2num($this->total_ht)."'";
$sql .= ", tva='". price2num($this->total_tva)."'";
$sql .= ", total_ttc='".price2num($this->total_ttc)."'";
$sql .= ' WHERE rowid = '.$facid;
$resql=$this->db->query($sql);
}
else
{
dolibarr_print_error($this->db);
}
}
/**
* \brief Rend la facture automatique

View File

@ -296,7 +296,7 @@ class Facture extends CommonObject
if (! $error)
{
$resql=$this->update_price($this->id);
$resql=$this->update_price();
if ($resql)
{
// Appel des triggers
@ -641,7 +641,7 @@ class Facture extends CommonObject
$lineid=$facligne->insert();
if ($lineid > 0)
{
$result=$this->update_price($this->id);
$result=$this->update_price();
if ($result > 0)
{
// Crée lien entre remise et ligne de facture
@ -713,72 +713,61 @@ class Facture extends CommonObject
$this->db->begin();
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_tva_sum WHERE fk_facture = '.$rowid;
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'fa_pr WHERE fk_facture = '.$rowid;
if ($this->db->query($sql))
{
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'fa_pr WHERE fk_facture = '.$rowid;
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'co_fa WHERE fk_facture = '.$rowid;
if ($this->db->query($sql))
{
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'co_fa WHERE fk_facture = '.$rowid;
// On met a jour le lien des remises
$list_rowid_det=array();
$sql = 'SELECT fd.rowid FROM '.MAIN_DB_PREFIX.'facturedet as fd WHERE fk_facture = '.$rowid;
$resql=$this->db->query($sql);
while ($obj = $this->db->fetch_object($resql))
{
$list_rowid_det[]=$obj->rowid;
}
// On désaffecte de la facture les remises liées
if (sizeof($list_rowid_det))
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except as re';
$sql.= ' SET re.fk_facture = NULL';
$sql.= ' WHERE re.fk_facture in ('.join(',',$list_rowid_det).')';
dolibarr_syslog("Facture.class::delete sql=".$sql);
if (! $this->db->query($sql))
{
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("Facture.class::delete ".$this->error);
$this->db->rollback();
return -5;
}
}
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE fk_facture = '.$rowid;
if ($this->db->query($sql))
{
// On met a jour le lien des remises
$list_rowid_det=array();
$sql = 'SELECT fd.rowid FROM '.MAIN_DB_PREFIX.'facturedet as fd WHERE fk_facture = '.$rowid;
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture WHERE rowid = '.$rowid;
$resql=$this->db->query($sql);
while ($obj = $this->db->fetch_object($resql))
if ($resql)
{
$list_rowid_det[]=$obj->rowid;
}
// On désaffecte de la facture les remises liées
if (sizeof($list_rowid_det))
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except as re';
$sql.= ' SET re.fk_facture = NULL';
$sql.= ' WHERE re.fk_facture in ('.join(',',$list_rowid_det).')';
dolibarr_syslog("Facture.class::delete sql=".$sql);
if (! $this->db->query($sql))
{
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("Facture.class::delete ".$this->error);
$this->db->rollback();
return -5;
}
}
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE fk_facture = '.$rowid;
if ($this->db->query($sql))
{
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture WHERE rowid = '.$rowid;
$resql=$this->db->query($sql);
if ($resql)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('BILL_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("Facture.class::delete ".$this->error);
$this->db->rollback();
return -6;
}
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('BILL_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("Facture.class::delete ".$this->error);
$this->db->rollback();
return -4;
return -6;
}
}
else
@ -786,7 +775,7 @@ class Facture extends CommonObject
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("Facture.class::delete ".$this->error);
$this->db->rollback();
return -3;
return -4;
}
}
else
@ -794,7 +783,7 @@ class Facture extends CommonObject
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("Facture.class::delete ".$this->error);
$this->db->rollback();
return -2;
return -3;
}
}
else
@ -802,7 +791,7 @@ class Facture extends CommonObject
$this->error=$this->db->error()." sql=".$sql;
dolibarr_syslog("Facture.class::delete ".$this->error);
$this->db->rollback();
return -1;
return -2;
}
}
@ -1071,7 +1060,7 @@ class Facture extends CommonObject
$numfa = $this->ref;
}
$this->update_price($this->id);
$this->update_price();
// Validation de la facture
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture ';
@ -1338,6 +1327,7 @@ class Facture extends CommonObject
$remise = round(($pu * $remise_percent / 100),2);
$price = ($pu - $remise);
}
$product_type=0;
if ($fk_product)
{
@ -1375,7 +1365,8 @@ class Facture extends CommonObject
if ($result > 0)
{
// Mise a jour informations denormalisees au niveau de la facture meme
$result=$this->update_price($facid);
$this->id=$facid; // \TODO A virer
$result=$this->update_price();
if ($result > 0)
{
$this->db->commit();
@ -1476,7 +1467,7 @@ class Facture extends CommonObject
if ($result > 0)
{
// Mise a jour info denormalisees au niveau facture
$this->update_price($this->id);
$this->update_price();
$this->db->commit();
return $result;
}
@ -1537,7 +1528,7 @@ class Facture extends CommonObject
return -1;
}
$result=$this->update_price($this->id);
$result=$this->update_price();
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
@ -1551,105 +1542,6 @@ class Facture extends CommonObject
return 1;
}
/**
\brief Mise à jour des sommes de la facture et calculs denormalises
\param facid id de la facture a modifier
\return int <0 si ko, >0 si ok
*/
function update_price($facid)
{
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
$tvas=array();
$err=0;
// Liste des lignes a sommer
$sql = 'SELECT qty, tva_taux, subprice, remise_percent,';
$sql.= ' total_ht, total_tva, total_ttc';
$sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet';
$sql.= ' WHERE fk_facture = '.$facid;
dolibarr_syslog("Facture::update_price sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$this->total_ht = 0;
$this->total_tva = 0;
$this->total_ttc = 0;
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$this->total_ht += $obj->total_ht;
$this->total_tva += ($obj->total_ttc - $obj->total_ht);
$this->total_ttc += $obj->total_ttc;
$tvas[$obj->tva_taux] += ($obj->total_ttc - $obj->total_ht);
$i++;
}
$this->db->free($resql);
// Met a jour indicateurs
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture SET';
$sql .= " total='". price2num($this->total_ht)."',";
$sql .= " tva='". price2num($this->total_tva)."',";
$sql .= " total_ttc='".price2num($this->total_ttc)."'";
$sql .= ' WHERE rowid = '.$facid;
dolibarr_syslog("Facture::update_price sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
// \TODO A supprimer car l'utilisation de facture_tva_sum non utilisable
// dans un context compta propre. On utilisera plutot les lignes.
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_tva_sum WHERE fk_facture='.$this->id;
if ( $this->db->query($sql) )
{
foreach ($tvas as $key => $value)
{
$sql_del = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_tva_sum where fk_facture ='.$this->id;
$this->db->query($sql_del);
$sql = 'INSERT INTO '.MAIN_DB_PREFIX."facture_tva_sum (fk_facture,amount,tva_tx) values ($this->id,'".price2num($tvas[$key])."','".price2num($key)."');";
if (! $this->db->query($sql) )
{
dolibarr_print_error($this->db);
$err++;
}
}
}
else
{
$err++;
}
if ($err == 0)
{
return 1;
}
else
{
return -3;
}
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Facture::update_price error=".$this->error,LOG_ERR);
return -1;
}
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Facture::update_price error=".$this->error,LOG_ERR);
return -1;
}
}
/**
* \brief Applique une remise relative
* \param user User qui positionne la remise

View File

@ -40,6 +40,8 @@ class CommandeFournisseur extends Commande
var $error;
var $element='order_supplier';
var $table_element='commande_fournisseur';
var $table_element_line = 'commande_fournisseurdet';
var $fk_element = 'fk_commande';
var $id ;
var $brouillon;
@ -135,7 +137,7 @@ class CommandeFournisseur extends Commande
{
$objp = $this->db->fetch_object($result);
$ligne = new CommandeFournisseurLigne();
$ligne = new CommandeFournisseurLigne($this->db);
$ligne->desc = $objp->description; // Description ligne
$ligne->qty = $objp->qty;
@ -660,17 +662,30 @@ class CommandeFournisseur extends Commande
* \param price_base_type HT or TTC
* \param int <0 si ko, >0 si ok
*/
function addline($desc, $pu, $qty, $txtva, $fk_product=0, $fk_prod_fourn_price=0, $fourn_ref='', $remise_percent=0, $price_base_type='HT')
function addline($desc, $pu_ht, $qty, $txtva, $fk_product=0, $fk_prod_fourn_price=0, $fourn_ref='', $remise_percent=0, $price_base_type='HT', $pu_ttc=0)
{
global $langs,$mysoc;
// Clean parameters
$qty = price2num($qty);
$pu = price2num($pu);
$desc = trim($desc);
$remise_percent = price2num($remise_percent);
dolibarr_syslog("Fournisseur.Commande::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
// Clean parameters
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
if (! $qty) $qty=1;
if (! $info_bits) $info_bits=0;
$pu_ht=price2num($pu_ht);
$pu_ttc=price2num($pu_ttc);
$txtva = price2num($txtva);
if ($price_base_type=='HT')
{
$pu=$pu_ht;
}
else
{
$pu=$pu_ttc;
}
$desc=trim($desc);
if ($qty < 1 && ! $fk_product)
{
@ -678,7 +693,7 @@ class CommandeFournisseur extends Commande
return -1;
}
if ($this->brouillon)
if ($this->statut == 0)
{
$this->db->begin();
@ -716,9 +731,19 @@ class CommandeFournisseur extends Commande
}
}
// Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, $price_base_type, $info_bits);
$total_ht = $tabprice[0];
$total_tva = $tabprice[1];
$total_ttc = $tabprice[2];
$subprice = price2num($pu,'MU');
// Champ obsolete
// \TODO A virer
// Anciens indicateurs: $price, $remise (a ne plus utiliser)
$remise = 0;
$price = $subprice;
if ($remise_percent > 0)
@ -730,11 +755,17 @@ class CommandeFournisseur extends Commande
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet";
$sql.= " (fk_commande,label, description,";
$sql.= " fk_product,";
$sql.= " price, qty, tva_tx, remise_percent, subprice, remise, ref)";
$sql.= " price, qty, tva_tx, remise_percent, subprice, remise, ref,";
$sql.= " total_ht, total_tva, total_ttc";
$sql.= ")";
$sql.= " VALUES (".$this->id.", '" . addslashes($label) . "','" . addslashes($desc) . "',";
if ($fk_product) { $sql.= $fk_product.","; }
else { $sql.= "null,"; }
$sql.= price2num($price,'MU').", '$qty', $txtva, $remise_percent,'".price2num($subprice,'MU')."','".price2num($remise)."','".$ref."') ;";
$sql.= price2num($price,'MU').", '$qty', $txtva, $remise_percent,'".price2num($subprice,'MU')."','".price2num($remise)."','".$ref."',";
$sql.= "'".price2num($total_ht)."',";
$sql.= "'".price2num($total_tva)."',";
$sql.= "'".price2num($total_ttc)."'";
$sql.= ")";
dolibarr_syslog('Fournisseur.commande::addline sql='.$sql);
$resql=$this->db->query($sql);
//print $sql;
@ -747,6 +778,7 @@ class CommandeFournisseur extends Commande
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -1;
}
@ -861,67 +893,6 @@ class CommandeFournisseur extends Commande
}
}
/**
* Mettre à jour le prix
*
*/
function update_price()
{
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php";
/*
* Liste des produits a ajouter
*/
$sql = "SELECT price, qty, tva_tx ";
$sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet ";
$sql .= " WHERE fk_commande = $this->id";
if ( $this->db->query($sql) )
{
$num = $this->db->num_rows();
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object();
$products[$i][0] = $obj->price;
$products[$i][1] = $obj->qty;
$products[$i][2] = $obj->tva_tx;
$i++;
}
}
$calculs = calcul_price($products, $this->remise_percent);
$totalht = $calculs[0];
$totaltva = $calculs[1];
$totalttc = $calculs[2];
$total_remise = $calculs[3];
$this->remise = $total_remise;
$this->total_ht = $totalht;
$this->total_tva = $totaltva;
$this->total_ttc = $totalttc;
/*
*
*/
$sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur set";
$sql .= " amount_ht ='".price2num($totalht)."'";
$sql .= ", total_ht ='".price2num($totalht)."'";
$sql .= ", tva ='".price2num($totaltva)."'";
$sql .= ", total_ttc ='".price2num($totalttc)."'";
$sql .= ", remise ='".price2num($total_remise)."'";
$sql .= " WHERE rowid = $this->id";
if ( $this->db->query($sql) )
{
return 1;
}
else
{
print "Erreur mise à jour du prix<p>".$sql;
return -1;
}
}
/**
* \brief Supprime la commande
*
@ -1288,7 +1259,7 @@ class CommandeFournisseur extends Commande
if ($result > 0)
{
// Mise a jour info denormalisees au niveau facture
$this->update_price($this->id);
$this->update_price();
$this->db->commit();
return $result;
}
@ -1393,26 +1364,102 @@ class CommandeFournisseur extends Commande
*/
class CommandeFournisseurLigne extends CommandeLigne
{
// From llx_commandedet
var $qty;
var $tva_tx;
var $subprice;
var $remise_percent;
var $price;
var $fk_product;
var $desc; // Description ligne
// From llx_product
var $libelle; // Label produit
var $product_desc; // Description produit
// From llx_product_fournisseur
var $ref_fourn; // Référence fournisseur
// From llx_commandedet
var $qty;
var $tva_tx;
var $subprice;
var $remise_percent;
var $price;
var $fk_product;
var $desc; // Description ligne
function CommandeFournisseurLigne()
{
// From llx_product
var $libelle; // Label produit
var $product_desc; // Description produit
// From llx_product_fournisseur
var $ref_fourn; // Référence fournisseur
function CommandeFournisseurLigne($DB)
{
$this->db= $DB;
}
/**
* \brief Load line order
* \param rowid id line order
*/
function fetch($rowid)
{
$sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.description, cd.price, cd.qty, cd.tva_tx,';
$sql.= ' cd.remise, cd.remise_percent, cd.subprice,';
$sql.= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
$sql.= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc';
$sql.= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
$sql.= ' WHERE cd.rowid = '.$rowid;
$result = $this->db->query($sql);
if ($result)
{
$objp = $this->db->fetch_object($result);
$this->rowid = $objp->rowid;
$this->fk_commande = $objp->fk_commande;
$this->desc = $objp->description;
$this->qty = $objp->qty;
$this->price = $objp->price;
$this->subprice = $objp->subprice;
$this->tva_tx = $objp->tva_tx;
$this->remise = $objp->remise;
$this->remise_percent = $objp->remise_percent;
$this->produit_id = $objp->fk_product;
$this->info_bits = $objp->info_bits;
$this->total_ht = $objp->total_ht;
$this->total_tva = $objp->total_tva;
$this->total_ttc = $objp->total_ttc;
$this->ref = $objp->product_ref;
$this->product_libelle = $objp->product_libelle;
$this->product_desc = $objp->product_desc;
$this->db->free($result);
}
else
{
dolibarr_print_error($this->db);
}
}
/**
* \brief Mise a jour de l'objet ligne de commande en base
* \return int <0 si ko, >0 si ok
*/
function update_total()
{
$this->db->begin();
}
// Mise a jour ligne en base
$sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseurdet SET";
$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("CommandeFournisseurLigne.class.php::update_total sql=$sql");
$resql=$this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("CommandeFournisseurLigne.class.php::update_total Error ".$this->error);
$this->db->rollback();
return -2;
}
}
}
?>

View File

@ -42,6 +42,8 @@ class FactureFournisseur extends Facture
var $socid;
var $element='facture_fourn';
var $table_element='facture_fourn';
var $table_element_line='facture_fourn_det';
var $fk_element='fk_facture_fourn';
//! 0=brouillon,
//! 1=validée,
@ -136,7 +138,7 @@ class FactureFournisseur extends Facture
}
}
// Mise à jour prix
if ($this->update_price($this->id) > 0)
if ($this->update_price() > 0)
{
$this->db->commit();
return $this->id;
@ -514,7 +516,7 @@ class FactureFournisseur extends Facture
if ($resql)
{
// Mise a jour prix total facture
return $this->update_price($this->id);
return $this->update_price();
}
else
{
@ -539,66 +541,10 @@ class FactureFournisseur extends Facture
dolibarr_print_error($this->db);
}
// Mise a jour prix facture
$this->update_price($this->id);
$this->update_price();
return 1;
}
/**
* \brief Mise à jour des sommes de la facture
* \param facid id de la facture a modifier
* \return int <0 si ko, >0 si ok
*/
function update_price($facid)
{
global $conf;
$total_ht = 0;
$total_tva = 0;
$total_ttc = 0;
$sql = 'SELECT sum(total_ht), sum(tva), sum(total_ttc) FROM '.MAIN_DB_PREFIX.'facture_fourn_det';
$sql .= ' WHERE fk_facture_fourn = '.$facid.';';
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
if ($num)
{
$row = $this->db->fetch_row();
$total_ht = $row[0];
$total_tva = $row[1];
$total_ttc = $row[2];
}
$this->db->free($resql);
$total_ht = $total_ht != '' ? $total_ht : 0;
$total_tva = $total_tva != '' ? $total_tva : 0;
$total_ttc = $total_ttc != '' ? $total_ttc : 0;
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn SET';
$sql .= ' total_ht = '. price2num($total_ht,'MT');
$sql .= ',total_tva = '.price2num($total_tva,'MT');
$sql .= ',total_ttc = '.price2num($total_ttc,'MT');
$sql .= ' WHERE rowid = '.$facid.';';
dolibarr_syslog("Fournisseur.facture::update_price sql=".$sql);
$resql2 = $this->db->query($sql);
if ($resql2)
{
return 1;
}
else
{
$this->error=$this->db->error();
return -2;
}
}
else
{
dolibarr_print_error($this->db);
return -1;
}
}
/**
* \brief Charge les informations d'ordre info dans l'objet facture

View File

@ -16,14 +16,12 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
/**
\file htdocs/install/upgrade2.php
\brief Effectue la migration de donnees diverses
\version $Revision$
\version $Id$
*/
include_once('./inc.php');
@ -33,6 +31,7 @@ require_once($dolibarr_main_document_root . '/facture.class.php');
require_once($dolibarr_main_document_root . '/propal.class.php');
require_once($dolibarr_main_document_root . '/contrat/contrat.class.php');
require_once($dolibarr_main_document_root . '/commande/commande.class.php');
require_once($dolibarr_main_document_root . '/fourn/fournisseur.commande.class.php');
require_once($dolibarr_main_document_root . '/lib/price.lib.php');
require_once($dolibarr_main_document_root . '/lib/menubase.class.php');
@ -161,6 +160,8 @@ if (isset($_POST['action']) && $_POST['action'] == 'upgrade')
migrate_price_commande($db,$langs,$conf);
migrate_price_commande_fournisseur($db,$langs,$conf);
migrate_price_facture($db,$langs,$conf);
migrate_price_contrat($db,$langs,$conf);
@ -998,7 +999,7 @@ function migrate_price_facture($db,$langs,$conf)
$facligne->total_ttc = $total_ttc;
dolibarr_install_syslog("upgrade2: Line $rowid: facid=$obj->facid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent remise_global=$remise_percent_global -> $total_ht, $total_tva, $total_ttc");
print ". ";
print ".";
$facligne->update_total();
@ -1010,9 +1011,9 @@ function migrate_price_facture($db,$langs,$conf)
if ( $facture->fetch($facture->id) >= 0)
{
if ( $facture->update_price($facture->id) > 0 )
if ( $facture->update_price() > 0 )
{
print "X ";
//print $facture->id;
}
else
{
@ -1026,6 +1027,7 @@ function migrate_price_facture($db,$langs,$conf)
$err++;
}
}
print " ";
$i++;
}
@ -1113,7 +1115,7 @@ function migrate_price_propal($db,$langs,$conf)
$propal->id=$obj->rowid;
if ( $propal->fetch($propal->id) >= 0 )
{
if ( $propal->update_price($propal->id) > 0 )
if ( $propal->update_price() > 0 )
{
print ". ";
}
@ -1218,7 +1220,7 @@ function migrate_price_contrat($db,$langs,$conf)
$propal->id=$obj->rowid;
if ( $propal->fetch($propal->id) >= 0 )
{
if ( $propal->update_price($propal->id) > 0 )
if ( $propal->update_price() > 0 )
{
print ". ";
}
@ -1320,7 +1322,7 @@ function migrate_price_commande($db,$langs,$conf)
$commande->id = $obj->rowid;
if ( $commande->fetch($commande->id) >= 0 )
{
if ( $commande->update_price($commande->id) > 0 )
if ( $commande->update_price() > 0 )
{
print ". ";
}
@ -1370,6 +1372,116 @@ function migrate_price_commande($db,$langs,$conf)
}
/*
* Mise a jour des totaux lignes de commande fournisseur
*/
function migrate_price_commande_fournisseur($db,$langs,$conf)
{
$db->begin();
dolibarr_install_syslog("upgrade2: Upgrade data for supplier order");
print '<tr><td colspan="4">';
print '<br>';
print '<b>'.$langs->trans('MigrationSupplierOrder')."</b><br>\n";
// Liste des lignes commande non a jour
$sql = "SELECT cd.rowid, cd.qty, cd.subprice, cd.remise_percent, cd.tva_tx as tva_taux, ";
$sql.= " c.rowid as commandeid, c.remise_percent as remise_percent_global";
$sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd, ".MAIN_DB_PREFIX."commande_fournisseur as c";
$sql.= " WHERE cd.fk_commande = c.rowid";
$sql.= " AND ((cd.total_ttc = 0 AND cd.remise_percent != 100) or cd.total_ttc IS NULL)";
$resql=$db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
if ($num)
{
while ($i < $num)
{
$obj = $db->fetch_object($resql);
$rowid = $obj->rowid;
$qty = $obj->qty;
$pu = $obj->subprice;
$txtva = $obj->tva_taux;
$remise_percent = $obj->remise_percent;
$remise_percent_global = $obj->remise_percent_global;
// On met a jour les 3 nouveaux champs
$commandeligne= new CommandeFournisseurLigne($db);
$commandeligne->fetch($rowid);
$result=calcul_price_total($qty,$pu,$remise_percent,$txtva,$remise_percent_global,'HT',$info_bits);
$total_ht = $result[0];
$total_tva = $result[1];
$total_ttc = $result[2];
$commandeligne->total_ht = $total_ht;
$commandeligne->total_tva = $total_tva;
$commandeligne->total_ttc = $total_ttc;
dolibarr_install_syslog("upgrade2: Line $rowid: commandeid=$obj->rowid pu=$pu qty=$qty tva_taux=$txtva remise_percent=$remise_percent remise_global=$remise_percent_global -> $total_ht, $total_tva, $total_ttc");
print ". ";
$commandeligne->update_total($rowid);
/* On touche pas a facture mere
$commande = new Commande($db);
$commande->id = $obj->rowid;
if ( $commande->fetch($commande->id) >= 0 )
{
if ( $commande->update_price() > 0 )
{
print ". ";
}
else
{
print "Error id=".$commande->id;
$err++;
}
}
else
{
print "Error #3";
$err++;
}
*/
$i++;
}
}
else
{
print $langs->trans("AlreadyDone");
}
$db->free($resql);
$sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet";
$sql.= " WHERE price = 0 and total_ttc = 0 and total_tva = 0 and total_ht = 0";
$resql=$db->query($sql);
if (! $resql)
{
dolibarr_print_error($db);
}
$db->commit();
}
else
{
print "Error #1 ".$db->error();
$err++;
$db->rollback();
}
print '<br>';
print '</td></tr>';
}
/*
* Mise a jour des modeles selectionnes
*/

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2006-2007 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -78,102 +78,3 @@ function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $remise_pe
return $result;
}
/**
\brief Permet de calculer un prix.
\param products
\param remise_percent
\param remise_absolue
\return result[0] total_ht
result[1] total_tva
result[2] total_ttc
result[5] tableau des totaux par tva
\deprecated
*/
function calcul_price($products, $remise_percent, $remise_absolue=0)
{
$total_ht = 0;
$amount_ht = 0;
$total_tva = 0;
$total_ttc = 0;
$total_remise = 0;
$result[5] = array();
if ( sizeof( $products ) )
{
foreach ($products as $product)
{
$prod_price = $product[0]; // Prix unitaire HT apres remise % de ligne
$prod_qty = $product[1];
$prod_txtva = $product[2];
// montant total HT de la ligne
$line_price_ht = $prod_qty * $prod_price;
// incrémentation montant HT hors remise de l'ensemble
$amount_ht += $line_price_ht;
// si une remise relative est consentie sur l'ensemble
if ($remise_percent > 0)
{
// calcul de la remise sur la ligne
$line_remise = ($line_price_ht * $remise_percent / 100);
// soustraction de cette remise au montant HT de la ligne
$line_price_ht -= $line_remise;
// incrémentation du montant total de remise sur l'ensemble
$total_remise += $line_remise;
}
// incrémentation du montant HT remisé de l'ensemble
$total_ht += $line_price_ht;
// calcul de la TVA sur la ligne
$line_tva = ($line_price_ht * (abs($prod_txtva) / 100));
// incrémentation du montant TTC de la valeur HT, on traite la TVA ensuite
$total_ttc += $line_price_ht;
// traitement de la tva non perçue récupérable
if ( $prod_txtva >= 0 )
{
// ce n'est pas une TVA non perçue récupérable,
// donc on incrémente le total TTC de l'ensemble, de la valeur de TVA de la ligne
$total_ttc += $line_tva;
}
// dans tous les cas, on incrémente le total de TVA
$total_tva += $line_tva;
// on incrémente le tableau de différentiation des taux de TVA
// s'il faut rassembler les tva facturables ou non, du même taux
// dans un même ligne du tableau, remplacer la ligne suivante par :
// $result[5][abs($prod_txtva)] += $line_tva;
$result[5][$prod_txtva] += $line_tva;
$i++;
}
}
/*
* Si remise absolue, on la retire
*/
$total_ht -= $remise_absolue;
/*
* Gestion des arrondis sur total des prix
*/
$total_ht = round($total_ht, 2);
$total_tva = round($total_tva, 2);
$total_ttc = $total_ht + $total_tva;
// Renvoi réponse
$result[0] = $total_ht;
$result[1] = $total_tva;
$result[2] = $total_ttc;
$result[3] = $total_remise;
$result[4] = $amount_ht;
return $result;
}

View File

@ -46,8 +46,8 @@ class Propal extends CommonObject
var $error;
var $element='propal';
var $table_element='propal';
var $fk_element='fk_propal';
var $table_element_line='propaldet';
var $fk_element='fk_propal';
var $id;
@ -342,7 +342,7 @@ class Propal extends CommonObject
if ($result > 0)
{
// Mise a jour informations denormalisees au niveau de la propale meme
$result=$this->update_price($propalid);
$result=$this->update_price();
if ($result > 0)
{

View File

@ -5,6 +5,8 @@
-- Ce fichier doit être chargé sur une version 2.2.0
--
drop table llx_facture_tva_sum;
delete from llx_const where name='MAIN_GRAPH_LIBRARY' and (value like 'phplot%' or value like 'artichow%');
ALTER TABLE llx_societe_adresse_livraison ADD COLUMN tel varchar(20) after fk_pays;

View File

@ -1,27 +0,0 @@
-- ===================================================================
-- Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
--
-- $Id$
-- ===================================================================
-- Supprimme orhpelins pour permettre montée de la clé
-- V4 DELETE llx_facture_tva_sum FROM llx_facture_tva_sum LEFT JOIN llx_facture ON llx_facture_tva_sum.fk_facture = llx_facture.rowid WHERE llx_facture.rowid IS NULL;
ALTER TABLE llx_facture_tva_sum ADD INDEX idx_facture_tva_sum_fk_facture (fk_facture);
ALTER TABLE llx_facture_tva_sum ADD CONSTRAINT fk_facture_tva_sum_fk_facture FOREIGN KEY (fk_facture) REFERENCES llx_facture (rowid);

View File

@ -1,26 +0,0 @@
-- ===================================================================
-- Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
--
-- $Id$
-- ===================================================================
create table llx_facture_tva_sum
(
fk_facture integer NOT NULL,
amount real NOT NULL,
tva_tx real NOT NULL
)type=innodb;