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

This commit is contained in:
Laurent Destailleur 2006-06-16 23:55:20 +00:00
parent 4a814b6b81
commit 9666db6cb7
20 changed files with 743 additions and 819 deletions

View File

@ -121,8 +121,7 @@ while ($i < GEN_NUMBER_PROPAL)
$soc = new Societe($db); $soc = new Societe($db);
$obj = $conf->global->PROPALE_ADDON;
$obj = PROPALE_ADDON;
$modPropale = new $obj; $modPropale = new $obj;
$numpr = $modPropale->propale_get_num($soc); $numpr = $modPropale->propale_get_num($soc);
@ -141,7 +140,7 @@ while ($i < GEN_NUMBER_PROPAL)
while ($xnbp < $nbp) while ($xnbp < $nbp)
{ {
$prodid = rand(1, $num_prods); $prodid = rand(1, $num_prods);
$propal->insert_product($prodids[$prodid], rand(1,5)); $propal->addline($propa->id, 'Description '.$xnbp, '100', rand(1,5), '19.6', $prodids[$prodid], 0);
$xnbp++; $xnbp++;
} }

View File

@ -96,7 +96,9 @@ if ($_POST['action'] == 'confirm_deleteproductline' && $_POST['confirm'] == 'yes
$propal = new Propal($db); $propal = new Propal($db);
$propal->fetch($_GET['propalid']); $propal->fetch($_GET['propalid']);
$propal->delete_product($_GET['ligne']); $propal->delete_product($_GET['ligne']);
propale_pdf_create($db, $_GET['propalid'], $propal->modelpdf); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs);
} }
Header('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET['propalid']); Header('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET['propalid']);
exit; exit;
@ -109,7 +111,9 @@ if ($_POST['action'] == 'confirm_validate' && $_POST['confirm'] == 'yes')
$propal = new Propal($db); $propal = new Propal($db);
$propal->fetch($_GET['propalid']); $propal->fetch($_GET['propalid']);
$result=$propal->update_price($_GET['propalid']); $result=$propal->update_price($_GET['propalid']);
propale_pdf_create($db, $_GET['propalid'], $propal->modelpdf); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs);
$result=$propal->valid($user); $result=$propal->valid($user);
} }
Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET['propalid']); Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET['propalid']);
@ -147,13 +151,14 @@ if ($_POST['action'] == 'set_ref_client' && $user->rights->propale->creer)
$propal->set_ref_client($user, $_POST['ref_client']); $propal->set_ref_client($user, $_POST['ref_client']);
} }
/*
* Creation propale
*/
if ($_POST['action'] == 'add') if ($_POST['action'] == 'add')
{ {
$propal = new Propal($db, $_POST['socidp']); $propal = new Propal($db, $_POST['socidp']);
/* // Si on a selectionné une propal à copier, on réalise la copie
* Si on a selectionné une propal à copier, on réalise la copie
*/
if($_POST['createmode']=='copy' && $_POST['copie_propal']) if($_POST['createmode']=='copy' && $_POST['copie_propal'])
{ {
if($propal->load_from($_POST['copie_propal']) == -1) if($propal->load_from($_POST['copie_propal']) == -1)
@ -216,7 +221,9 @@ if ($_POST['action'] == 'add')
*/ */
if ($id > 0) if ($id > 0)
{ {
propale_pdf_create($db, $id, $_POST['model']); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
propale_pdf_create($db, $id, $_POST['model'], $outputlangs);
Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$id); Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$id);
exit; exit;
@ -398,36 +405,75 @@ if ($_POST['action'] == "addligne" && $user->rights->propale->creer)
$propal = new Propal($db); $propal = new Propal($db);
$ret=$propal->fetch($_POST['propalid']); $ret=$propal->fetch($_POST['propalid']);
if (isset($_POST['np_tva_tx'])) // Ecrase $pu par celui du produit
// Ecrase $desc par celui du produit
// Ecrase $txtva par celui du produit
if ($_POST['idprod'])
{ {
$propal->insert_product_generic( $prod = new Product($db, $_POST['idprod']);
$_POST['np_desc'], $prod->fetch($_POST['idprod']);
$_POST['np_price'],
$_POST['qty'], // multiprix
$_POST['np_tva_tx'], if ($conf->global->PRODUIT_MULTIPRICES == 1)
$_POST['np_remise']); {
$pu = $prod->multiprices[$soc->price_level];
} }
else else
{ {
$propal->insert_product( $pu=$prod->price;
$_POST['idprod'],
$_POST['qty'],
$_POST['remise'],
$_POST['np_desc']);
} }
propale_pdf_create($db, $_POST['propalid'], $propal->modelpdf);
// La description de la ligne est celle saisie ou
// celle du produit si (non saisi + PRODUIT_CHANGE_PROD_DESC défini)
$desc=$_POST['np_desc'];
if (! $desc && $conf->global->PRODUIT_CHANGE_PROD_DESC)
{
$desc = $prod->description;
}
$tva_tx = get_default_tva($mysoc,$soc,$prod->tva_tx);
}
else
{
$pu=$_POST['np_price'];
$tva_tx=$_POST['np_tva_tx'];
$desc=$_POST['np_desc'];
}
$propal->addline(
$_POST['propalid'],
$desc,
$pu,
$_POST['qty'],
$tva_tx,
$_POST['idprod'],
$_POST['remise_percent']
);
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs);
} }
} }
if ($_POST['action'] == 'updateligne' && $user->rights->propale->creer && $_POST["save"] == $langs->trans("Save")) /*
{
/*
* Mise à jour d'une ligne dans la propale * Mise à jour d'une ligne dans la propale
*/ */
if ($_POST['action'] == 'updateligne' && $user->rights->propale->creer && $_POST["save"] == $langs->trans("Save"))
{
$propal = new Propal($db); $propal = new Propal($db);
$propal->fetch($_GET['propalid']); if (! $propal->fetch($_POST['propalid']) > 0) dolibarr_print_error($db);
$propal->UpdateLigne($_POST['ligne'], $_POST['subprice'], $_POST['qty'], $_POST['remise_percent'], $_POST['tva_tx'], $_POST['desc']);
propale_pdf_create($db, $_GET['propalid'], $propal->modelpdf); $result = $propal->updateline($_POST['ligne'],
$_POST['subprice'],
$_POST['qty'],
$_POST['remise_percent'],
$_POST['tva_tx'],
$_POST['desc']);
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs);
} }
/* /*
@ -435,12 +481,12 @@ if ($_POST['action'] == 'updateligne' && $user->rights->propale->creer && $_POST
*/ */
if ($_REQUEST['action'] == 'builddoc' && $user->rights->propale->creer) if ($_REQUEST['action'] == 'builddoc' && $user->rights->propale->creer)
{ {
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
$outputlangs->setDefaultLang($_REQUEST['lang_id']);
$propal = new Propal($db); $propal = new Propal($db);
$propal->fetch($_GET['propalid']); $propal->fetch($_GET['propalid']);
if ($_POST['model']) $propal->set_pdf_model($user, $_POST['model']); if ($_POST['model']) $propal->set_pdf_model($user, $_POST['model']);
propale_pdf_create($db, $_GET['propalid'], $propal->modelpdf, $outputlangs); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs);
} }
@ -452,7 +498,9 @@ if ($_GET['action'] == 'del_ligne' && $user->rights->propale->creer && !$conf->g
$propal = new Propal($db); $propal = new Propal($db);
$propal->fetch($_GET['propalid']); $propal->fetch($_GET['propalid']);
$propal->delete_product($_GET['ligne']); $propal->delete_product($_GET['ligne']);
propale_pdf_create($db, $_GET['propalid'], $propal->modelpdf); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs);
} }
if ($_POST['action'] == 'set_project') if ($_POST['action'] == 'set_project')
@ -1197,7 +1245,7 @@ if ($_GET['propalid'] > 0)
print "</td>\n"; print "</td>\n";
print '<td align="right"><input type="text" size="5" name="np_price"></td>'; print '<td align="right"><input type="text" size="5" name="np_price"></td>';
print '<td align="right"><input type="text" size="2" value="1" name="qty"></td>'; print '<td align="right"><input type="text" size="2" value="1" name="qty"></td>';
print '<td align="right" nowrap><input type="text" size="1" value="'.$societe->remise_client.'" name="np_remise">%</td>'; print '<td align="right" nowrap><input type="text" size="1" value="'.$societe->remise_client.'" name="remise_percent">%</td>';
print '<td align="center" valign="middle" colspan="4"><input type="submit" class="button" value="'.$langs->trans('Add').'" name="addligne"></td>'; print '<td align="center" valign="middle" colspan="4"><input type="submit" class="button" value="'.$langs->trans('Add').'" name="addligne"></td>';
print '</tr>'; print '</tr>';
@ -1228,7 +1276,7 @@ if ($_GET['propalid'] > 0)
print '</td>'; print '</td>';
print '<td>&nbsp;</td>'; print '<td>&nbsp;</td>';
print '<td align="right"><input type="text" size="2" name="qty" value="1"></td>'; print '<td align="right"><input type="text" size="2" name="qty" value="1"></td>';
print '<td align="right" nowrap><input type="text" size="1" name="remise" value="'.$societe->remise_client.'">%</td>'; print '<td align="right" nowrap><input type="text" size="1" name="remise_percent" value="'.$societe->remise_client.'">%</td>';
print '<td align="center" valign="middle" colspan="4"><input type="submit" class="button" value="'.$langs->trans("Add").'" name="addligne">'; print '<td align="center" valign="middle" colspan="4"><input type="submit" class="button" value="'.$langs->trans("Add").'" name="addligne">';
print '</td></tr>'."\n"; print '</td></tr>'."\n";

View File

@ -321,7 +321,10 @@ class Commande
*/ */
function create($user) function create($user)
{ {
global $conf,$langs; global $conf,$langs,$mysoc;
// Nettoyage parametres
$this->brouillon = 1; // On positionne en mode brouillon la commande
// Vérification paramètres // Vérification paramètres
if ($this->source < 0) if ($this->source < 0)
@ -329,19 +332,15 @@ class Commande
$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Source")); $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Source"));
return -1; return -1;
} }
if (! $remise) $remise=0;
if (! $this->projetid) $this->projetid = 0;
// On positionne en mode brouillon la commande dolibarr_syslog("Commande.class.php::create");
$this->brouillon = 1;
if (! $remise) $soc = new Societe($this->db);
{ $soc->fetch($this->socidp);
$remise = 0 ;
}
if (! $this->projetid) $this->db->begin();
{
$this->projetid = 0;
}
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commande ('; $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commande (';
$sql.= 'fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note, ref_client,'; $sql.= 'fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note, ref_client,';
@ -357,10 +356,13 @@ class Commande
$sql.= " '".$this->remise_absolue."',"; $sql.= " '".$this->remise_absolue."',";
$sql.= " '".$this->remise_percent."')"; $sql.= " '".$this->remise_percent."')";
dolibarr_syslog("Commande.class.php::create sql=$sql"); $resql=$this->db->query($sql);
if ( $this->db->query($sql) ) if ($resql)
{ {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande'); $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande');
if ($this->id)
{
/* /*
* Insertion des produits dans la base * Insertion des produits dans la base
*/ */
@ -369,38 +371,33 @@ class Commande
$prod = new Product($this->db, $this->products[$i]); $prod = new Product($this->db, $this->products[$i]);
if ($prod->fetch($this->products[$i])) if ($prod->fetch($this->products[$i]))
{ {
$this->soc_id; $tva_tx = get_default_tva($mysoc,$soc,$prod->tva_tx);
$client = new Societe($this->db);
$client->fetch($this->soc_id);
if($client->tva_assuj == "0")
$tva_tx ="0";
else
$tva_tx=$prod->tva_tx;
// multiprix // multiprix
if($conf->global->PRODUIT_MULTIPRICES == 1) if($conf->global->PRODUIT_MULTIPRICES == 1)
{ $price = $prod->multiprices[$soc->price_level];
//$prod->multiprices[$client->price_level]
$this->insert_product_generic($prod->libelle,
$prod->description,
$prod->multiprices[$client->price_level],
$this->products_qty[$i],
$tva_tx,
$this->products[$i],
$this->products_remise_percent[$i]);
}
else else
{ $price = $prod->price;
$this->insert_product_generic($prod->libelle,
$resql = $this->addline(
$this->id,
$prod->description, $prod->description,
$prod->price, $price,
$this->products_qty[$i], $this->products_qty[$i],
$tva_tx, $tva_tx,
$this->products[$i], $this->products[$i],
$this->products_remise_percent[$i]); $this->products_remise_percent[$i]
);
if ($resql < 0)
{
$this->error=$this->db->error;
dolibarr_print_error($this->db);
break;
} }
} }
} }
// Mise a jour ref
$sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id; $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id;
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
@ -409,25 +406,7 @@ class Commande
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'co_pr (fk_commande, fk_propale) VALUES ('.$this->id.','.$this->propale_id.')'; $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'co_pr (fk_commande, fk_propale) VALUES ('.$this->id.','.$this->propale_id.')';
$this->db->query($sql); $this->db->query($sql);
} }
/*
* Produits
*
*/
for ($i = 0 ; $i < sizeof($this->lines) ; $i++)
{
$result_insert = $this->insert_product_generic(
$this->lines[$i]->libelle,
$this->lines[$i]->description,
$this->lines[$i]->price,
$this->lines[$i]->qty,
$this->lines[$i]->tva_tx,
$this->lines[$i]->product_id,
$this->lines[$i]->remise_percent);
if ( $result_insert < 0)
{
dolibarr_print_error($this->db);
}
}
return $this->id; return $this->id;
} }
else else
@ -435,197 +414,103 @@ class Commande
return -1; return -1;
} }
} }
}
else else
{ {
dolibarr_print_error($this->db); dolibarr_print_error($this->db);
return 0; return 0;
} }
} }
/** /**
* Ajoute un produit * \brief Ajout d'un produit dans la commande, en base
* * \param commandeid id de la commande
* \param desc description de la ligne
* \param pu prix unitaire
* \param qty quantité
* \param txtva taux de tva forcé, sinon -1
* \param fk_product id du produit/service predéfini
* \param remise_percent pourcentage de remise de la ligne
* \return int >0 si ok, <0 si ko
* \see add_product
* \remarks Les parametres sont deja censé etre juste et avec valeurs finales a l'appel
* de cette methode. Aussi, pour le taux tva, il doit deja avoir ete défini
* par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,taux_produit)
* et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue)
*/ */
function insert_product_generic($p_desc, $p_product_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0) function addline($commandeid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0)
{ {
global $conf; dolibarr_syslog("commande.class.php::addline $commandeid, $desc, $pu, $qty, $txtva, $fk_product, $remise_percent");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
if ($this->statut == 0) if ($this->statut == 0)
{ {
if (strlen(trim($p_qty)) == 0) $this->db->begin();
{
$p_qty = 1;
}
$p_price = ereg_replace(',','.',$p_price); // Nettoyage paramètres
$remise_percent=price2num($remise_percent);
$price = $p_price; $qty=price2num($qty);
$subprice = $p_price; if (! $qty) $qty=1;
if ($remise_percent > 0)
{
$remise = round(($p_price * $remise_percent / 100), 2);
$price = $p_price - $remise;
}
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet (fk_commande, fk_product, qty, price, tva_tx, label, description, remise_percent, subprice) VALUES ';
$sql .= " ('".$this->id."', '$p_product_id','". $p_qty."','".price2num($price)."','".$p_tva_tx."','".addslashes($p_desc)."','".addslashes($p_product_desc)."', '$remise_percent', '$subprice') ; ";
// Bugfix
/*
if ($conf->global->PRODUIT_CHANGE_PROD_DESC)
{
$sql .= " ('".$this->id."', '$p_product_id','". $p_qty."','".price2num($price)."','".$p_tva_tx."','".addslashes($p_desc)."','".addslashes($p_product_desc)."', '$remise_percent', '$subprice') ; ";
}
else
{
$sql .= " ('".$this->id."', '$p_product_id','". $p_qty."','".price2num($price)."','".$p_tva_tx."','".addslashes($p_desc)."','".addslashes($p_product_desc)."', '$remise_percent', '$subprice') ; ";
}
*/
if ($this->db->query($sql) )
{
if ($this->update_price() > 0)
{
return 1;
}
else
{
return -1;
}
}
else
{
dolibarr_print_error($this->db);
return -2;
}
}
}
/**
* Ajoute une ligne de commande
*
*/
function addline($desc, $product_desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0)
{
global $conf;
// Nettoyage parametres
$qty = price2num($qty);
$pu = price2num($pu); $pu = price2num($pu);
$desc=trim($desc); $txtva = price2num($txtva);
$product_desc=trim($product_desc);
if (strlen(trim($qty))==0)
{
$qty=1;
}
// Verifs // Calcul du total TTC et de la TVA pour la ligne a partir de
if (! $this->brouillon) return -1; // 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);
$total_ht = $tabprice[0];
$total_tva = $tabprice[1];
$total_ttc = $tabprice[2];
$this->db->begin(); // Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser)
$price = $pu;
if ($fk_product > 0) $subprice = $pu;
{ if ($remise_percent > 0)
$prod = new Product($this->db, $fk_product);
if ($prod->fetch($fk_product) > 0)
{
$desc = $desc?$desc:$prod->libelle;
$product_desc = $prod->description;
$client = new Societe($this->db);
$client->fetch($this->soc_id);
if($client->tva_assuj == "0")
$txtva ="0";
else
$txtva=$prod->tva_tx;
// multiprix
if($conf->global->PRODUIT_MULTIPRICES == 1)
{
$pu = $prod->multiprices[$client->price_level];
}
else
$pu = $prod->price;
}
}
$remise = 0;
$price = round(price2num($pu), 2);
$subprice = $price;
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet (fk_commande,label,description,fk_product, price,qty,tva_tx, remise_percent, subprice, remise)';
if ($conf->global->PRODUIT_CHANGE_PROD_DESC)
{
$sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($product_desc) . "',$fk_product,".price2num($price).", '$qty', $txtva, $remise_percent,'".price2num($subprice)."','".price2num( $remise)."') ;";
}
else
{
$sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "',$fk_product,".price2num($price).", '$qty', $txtva, $remise_percent,'".price2num($subprice)."','".price2num( $remise)."') ;";
}
if ( $this->db->query( $sql) )
{
$this->update_price();
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -1;
}
}
/**
* Ajoute une ligne de commande libre
*
*/
function addline_libre($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0)
{
global $conf;
// Nettoyage parametres
$qty = ereg_replace(',','.',$qty);
$pu = ereg_replace(',','.',$pu);
$desc=trim($desc);
if (strlen(trim($qty))==0)
{
$qty=1;
}
// Verifs
if (! $this->brouillon) return -1;
$this->db->begin();
$remise = 0;
$price = round(ereg_replace(',','.',$pu), 2);
$subprice = $price;
// appliquait la remise 2 fois : sur la ligne et le HT
/*
if (trim(strlen($remise_percent)) > 0)
{ {
$remise = round(($pu * $remise_percent / 100), 2); $remise = round(($pu * $remise_percent / 100), 2);
$price = $pu - $remise; $price = $pu - $remise;
} }
*/
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet (fk_commande,label,description,fk_product, price,qty,tva_tx, remise_percent, subprice, remise)';
$sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "','$fk_product',".price2num($price).", '$qty', '$txtva', $remise_percent,'".price2num($subprice)."','".price2num( $remise)."') ;";
if ( $this->db->query( $sql) ) $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.= ")";
if ($this->db->query($sql))
{
// Mise a jour informations denormalisees au niveau de la facture meme
$result=$this->update_price($this->id);
if ($result > 0)
{ {
$this->update_price();
$this->db->commit(); $this->db->commit();
return 1; return 1;
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
} }
else
{
$this->error=$this->db->error();
dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback();
return -2;
}
}
}
/** /**
@ -806,7 +691,7 @@ class Commande
$result = $this->db->query($sql) ; $result = $this->db->query($sql) ;
if ( $result ) if ( $result )
{ {
$obj = $this->db->fetch_object(); $obj = $this->db->fetch_object($result);
$this->id = $obj->rowid; $this->id = $obj->rowid;
$this->ref = $obj->ref; $this->ref = $obj->ref;
$this->ref_client = $obj->ref_client; $this->ref_client = $obj->ref_client;
@ -886,7 +771,7 @@ class Commande
{ {
$objp = $this->db->fetch_object($result); $objp = $this->db->fetch_object($result);
$ligne = new CommandeLigne(); $ligne = new CommandeLigne($this->db);
$ligne->desc = $objp->description; // Description ligne $ligne->desc = $objp->description; // Description ligne
$ligne->qty = $objp->qty; $ligne->qty = $objp->qty;
@ -1227,49 +1112,64 @@ class Commande
} }
/** /**
* \brief Mets à jour le prix total de la proposition * \brief Mets à jour le prix total de la commnde
* \return int <0 si ko, >0 si ok * \return int <0 si ko, >0 si ok
*/ */
function update_price() function update_price()
{ {
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
/* // Liste des lignes factures a sommer
* Liste des produits a ajouter $sql = "SELECT price, qty, tva_tx, total_ht, total_tva, total_ttc";
*/ $sql.= " FROM ".MAIN_DB_PREFIX."commandedet";
$sql = "SELECT price, qty, tva_tx FROM ".MAIN_DB_PREFIX."commandedet";
$sql.= " WHERE fk_commande = ".$this->id; $sql.= " WHERE fk_commande = ".$this->id;
if ( $this->db->query($sql) ) $result = $this->db->query($sql);
if ($result)
{ {
$num = $this->db->num_rows(); $this->total_ht = 0;
$i = 0; $this->total_tva = 0;
$this->total_ttc = 0;
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num) while ($i < $num)
{ {
$obj = $this->db->fetch_object(); $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;
// Anciens indicateurs
$this->amount_ht += $obj->price * $obj->qty;
$this->total_remise += 0; // Plus de remise globale (toute remise est sur une ligne)
/* \deprecated car simplifie par les 3 indicateurs total_ht, total_tva et total_ttc sur lignes
$products[$i][0] = $obj->price; $products[$i][0] = $obj->price;
$products[$i][1] = $obj->qty; $products[$i][1] = $obj->qty;
$products[$i][2] = $obj->tva_tx; $products[$i][2] = $obj->tva_tx;
*/
$i++; $i++;
} }
}
$calculs = calcul_price($products, $this->remise_percent, $this->remise_absolue);
$this->db->free($result);
}
/* \deprecated car simplifie par les 3 indicateurs total_ht, total_tva et total_ttc sur lignes
$calculs = calcul_price($products, $this->remise_percent, $this->remise_absolue);
$this->total_remise = $calculs[3]; $this->total_remise = $calculs[3];
$this->amount_ht = $calculs[4]; $this->amount_ht = $calculs[4];
$this->total_ht = $calculs[0]; $this->total_ht = $calculs[0];
$this->total_tva = $calculs[1]; $this->total_tva = $calculs[1];
$this->total_ttc = $calculs[2]; $this->total_ttc = $calculs[2];
$tvas = $calculs[5]; $tvas = $calculs[5];
*/
// Met a jour en base // Met a jour en base
$sql = "UPDATE ".MAIN_DB_PREFIX."commande SET"; $sql = "UPDATE ".MAIN_DB_PREFIX."commande SET";
$sql .= " total_ht='". price2num($this->total_ht)."'"; $sql .= " amount_ht='".price2num($this->total_ht)."'";
$sql .= ", total_ht='". price2num($this->total_ht)."'";
$sql .= ", tva='". price2num($this->total_tva)."'"; $sql .= ", tva='". price2num($this->total_tva)."'";
$sql .= ", total_ttc='".price2num($this->total_ttc)."'"; $sql .= ", total_ttc='".price2num($this->total_ttc)."'";
$sql .= ", remise='".price2num($this->total_remise)."'"; $sql .= ", remise='".price2num($this->total_remise)."'";
$sql .=" WHERE rowid = ".$this->id; $sql .=" WHERE rowid = ".$this->id;
if ( $this->db->query($sql) ) if ( $this->db->query($sql) )
{ {
return 1; return 1;
@ -1277,6 +1177,7 @@ class Commande
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
dolibarr_syslog("Commande::update_price error=".$this->error);
return -1; return -1;
} }
} }
@ -1579,51 +1480,80 @@ class Commande
* \param tva_tx Taux TVA * \param tva_tx Taux TVA
* \return int < 0 si erreur, > 0 si ok * \return int < 0 si erreur, > 0 si ok
*/ */
function update_line($rowid, $desc, $pu, $qty, $remise_percent=0, $tva_tx) function update_line($rowid, $desc, $pu, $qty, $remise_percent=0, $txtva)
{ {
dolibarr_syslog("Commande::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent=0, $tva_tx"); dolibarr_syslog("Commande::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $txtva");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
if ($this->brouillon) if ($this->brouillon)
{ {
$this->db->begin(); $this->db->begin();
// Nettoyage paramètres // Nettoyage paramètres
$pu=price2num($pu); $remise_percent=price2num($remise_percent);
if (strlen(trim($qty))==0) $qty=1; $qty=price2num($qty);
$remise = 0; if (! $qty) $qty=1;
$pu = price2num($pu);
$txtva=price2num($txtva);
// 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);
$total_ht = $tabprice[0];
$total_tva = $tabprice[1];
$total_ttc = $tabprice[2];
// Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser)
$price = $pu; $price = $pu;
$subprice = $price; $subprice = $pu;
$remise_percent=trim($remise_percent); $remise = 0;
if ($remise_percent > 0) if ($remise_percent > 0)
{ {
$remise = round(($pu * $remise_percent / 100), 2); $remise = round(($pu * $remise_percent / 100),2);
$price = $pu - $remise; $price = ($pu - $remise);
}
else
{
$remise_percent=0;
} }
$price = price2num($price);
$subprice = price2num($subprice);
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commandedet'; // Mise a jour ligne en base
$sql.= " SET description='".addslashes($desc)."',price='".price2num($price)."',subprice='".$subprice."',"; $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
$sql.= " remise='".$remise."',remise_percent='".$remise_percent."',qty='".$qty."',tva_tx='".$tva_tx."'"; $sql.= " description='".addslashes($desc)."'";
$sql.= " WHERE rowid = '".$rowid."'"; $sql.= ",price='".price2num($price)."'";
$sql.= ",subprice='".price2num($subprice)."'";
$sql.= ",remise='".price2num($remise)."'";
$sql.= ",remise_percent='".price2num($remise_percent)."'";
$sql.= ",tva_tx='".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); $result = $this->db->query( $sql);
if ( $result ) if ($result > 0)
{ {
// Mise a jour info denormalisees au niveau facture
$this->update_price($this->id); $this->update_price($this->id);
$this->db->commit(); $this->db->commit();
return $result; return $result;
} }
else else
{ {
$this->error=$this->db->error();
$this->db->rollback(); $this->db->rollback();
dolibarr_print_error($this->db);
return -1; return -1;
} }
} }
else else
{ {
$this->error="Commande::updateline Order status makes operation forbidden";
return -2; return -2;
} }
} }

View File

@ -193,40 +193,70 @@ if ($_REQUEST['action'] == 'setremiseabsolue' && $user->rights->facture->creer)
$_GET['id']=$_REQUEST['id']; $_GET['id']=$_REQUEST['id'];
} }
if ($_POST['action'] == 'addligne' && $user->rights->commande->creer) /*
{
/*
* Ajout d'une ligne produit dans la commande * Ajout d'une ligne produit dans la commande
*/ */
if ($_POST['qty'] && (($_POST['pu'] && $_POST['desc']) || $_POST['p_idprod'])) if ($_POST['action'] == 'addligne' && $user->rights->commande->creer)
{
if ($_POST['qty'] && (($_POST['pu'] && $_POST['desc']) || $_POST['idprod']))
{ {
$commande = new Commande($db); $commande = new Commande($db);
$ret=$commande->fetch($_POST['id']); $ret=$commande->fetch($_POST['id']);
if (isset($_POST['p_idprod'])) // Ecrase $pu par celui du produit
// Ecrase $desc par celui du produit
// Ecrase $txtva par celui du produit
if ($_POST['idprod'])
{ {
$result = $commande->addline( $prod = new Product($db, $_POST['idprod']);
$_POST['np_desc'], $prod->fetch($_POST['idprod']);
$_POST['product_desc'],
$_POST['pu'], // multiprix
$_POST['qty'], if ($conf->global->PRODUIT_MULTIPRICES == 1)
$_POST['tva_tx'], {
$_POST['p_idprod'], $pu = $prod->multiprices[$soc->price_level];
$_POST['remise_percent']);
} }
else else
{ {
$result = $commande->addline_libre( $pu=$prod->price;
$_POST['desc'],
$_POST['pu'],
$_POST['qty'],
$_POST['tva_tx'],
0,
$_POST['remise_percent']);
} }
// La description de la ligne est celle saisie ou
// celle du produit si (non saisi + PRODUIT_CHANGE_PROD_DESC défini)
$desc=$_POST['np_desc'];
if (! $desc && $conf->global->PRODUIT_CHANGE_PROD_DESC)
{
$desc = $prod->description;
}
$tva_tx = get_default_tva($mysoc,$soc,$prod->tva_tx);
}
else
{
$pu=$_POST['pu'];
$tva_tx=$_POST['tva_tx'];
$desc=$_POST['desc'];
}
$commande->addline(
$_POST['id'],
$desc,
$pu,
$_POST['qty'],
$tva_tx,
$_POST['idprod'],
$_POST['remise_percent']
);
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs);
} }
} }
/*
* Mise à jour d'une ligne dans la propale
*/
if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['save'] == $langs->trans('Save')) if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['save'] == $langs->trans('Save'))
{ {
$commande = new Commande($db,'',$_POST['id']); $commande = new Commande($db,'',$_POST['id']);
@ -234,12 +264,24 @@ if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POS
$result = $commande->update_line($_POST['elrowid'], $result = $commande->update_line($_POST['elrowid'],
$_POST['eldesc'], $_POST['eldesc'],
$_POST['elprice'], $_POST['pu'],
$_POST['elqty'], $_POST['qty'],
$_POST['elremise_percent'], $_POST['elremise_percent'],
$_POST['eltva_tx'] $_POST['tva_tx']
); );
if ($result >= 0)
{
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs);
}
else
{
dolibarr_print_error($db,$commande->error);
exit;
}
$_GET['id']=$_POST['id']; // Pour réaffichage de la fiche en cours d'édition $_GET['id']=$_POST['id']; // Pour réaffichage de la fiche en cours d'édition
} }
@ -1190,12 +1232,12 @@ else
print '<textarea name="eldesc" cols="50" rows="1">'.stripslashes($objp->description).'</textarea></td>'; print '<textarea name="eldesc" cols="50" rows="1">'.stripslashes($objp->description).'</textarea></td>';
print '<td align="right">'; print '<td align="right">';
if($soc->tva_assuj == "0") if($soc->tva_assuj == "0")
print '<input type="hidden" name="eltva_tx" value="0">0'; print '<input type="hidden" name="tva_tx" value="0">0';
else else
print $html->select_tva('eltva_tx',$objp->tva_tx,$mysoc,$soc); print $html->select_tva('tva_tx',$objp->tva_tx,$mysoc,$soc);
print '</td>'; print '</td>';
print '<td align="right"><input size="5" type="text" class="flat" name="elprice" value="'.price($objp->subprice).'"></td>'; print '<td align="right"><input size="5" type="text" class="flat" name="pu" value="'.price($objp->subprice).'"></td>';
print '<td align="right"><input size="2" type="text" class="flat" name="elqty" value="'.$objp->qty.'"></td>'; print '<td align="right"><input size="2" type="text" class="flat" name="qty" value="'.$objp->qty.'"></td>';
print '<td align="right" nowrap="nowrap"><input size="1" type="text" class="flat" name="elremise_percent" value="'.$objp->remise_percent.'">%</td>'; print '<td align="right" nowrap="nowrap"><input size="1" type="text" class="flat" name="elremise_percent" value="'.$objp->remise_percent.'">%</td>';
print '<td align="center" colspan="4"><input type="submit" class="button" name="save" value="'.$langs->trans('Save').'">'; print '<td align="center" colspan="4"><input type="submit" class="button" name="save" value="'.$langs->trans('Save').'">';
print '<br /><input type="submit" class="button" name="cancel" value="'.$langs->trans('Cancel').'"></td>'; print '<br /><input type="submit" class="button" name="cancel" value="'.$langs->trans('Cancel').'"></td>';
@ -1378,9 +1420,9 @@ else
print '<td colspan="2">'; print '<td colspan="2">';
// multiprix // multiprix
if($conf->global->PRODUIT_MULTIPRICES == 1) if($conf->global->PRODUIT_MULTIPRICES == 1)
$html->select_produits('','p_idprod','',$conf->produit->limit_size,$soc->price_level); $html->select_produits('','idprod','',$conf->produit->limit_size,$soc->price_level);
else else
$html->select_produits('','p_idprod','',$conf->produit->limit_size); $html->select_produits('','idprod','',$conf->produit->limit_size);
if (! $conf->use_ajax) print '<br>'; if (! $conf->use_ajax) print '<br>';
print '<textarea cols="50" name="np_desc" rows="1"></textarea>'; print '<textarea cols="50" name="np_desc" rows="1"></textarea>';
print '</td>'; print '</td>';

View File

@ -142,7 +142,9 @@ if ($_POST['action'] == 'confirm_valid' && $_POST['confirm'] == 'yes' && $user->
$result = $fac->set_valid($fac->id, $user, $soc); $result = $fac->set_valid($fac->id, $user, $soc);
if ($result) if ($result)
{ {
facture_pdf_create($db, $fac->id); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
facture_pdf_create($db, $fac->id, '', $_REQUEST['model'], $outputlangs);
} }
} }
@ -360,14 +362,17 @@ if ($_POST['action'] == 'add')
} }
} }
/*
* Ajout d'une ligne produit dans la facture
*/
if (($_POST['action'] == 'addligne' || $_POST['action'] == 'addligne_predef') && $user->rights->facture->creer) if (($_POST['action'] == 'addligne' || $_POST['action'] == 'addligne_predef') && $user->rights->facture->creer)
{ {
if ($_POST['qty'] && ( $_POST['desc'] || $_POST['idprod'])) if ($_POST['qty'] && (($_POST['pu']!=0 && $_POST['desc']) || $_POST['idprod']))
{ {
$fac = new Facture($db); $fac = new Facture($db);
$fac->fetch($_POST['facid']); $ret=$fac->fetch($_POST['facid']);
$soc = new Societe($db); $soc = new Societe($db);
$soc->fetch($fac->socidp); $ret=$soc->fetch($fac->socidp);
$datestart=''; $datestart='';
$dateend=''; $dateend='';
@ -414,16 +419,10 @@ if (($_POST['action'] == 'addligne' || $_POST['action'] == 'addligne_predef') &&
$pu=$prod->price; $pu=$prod->price;
} }
// La description de la ligne est celle saisie ou
// celle du produit si (non saisi + PRODUIT_CHANGE_PROD_DESC défini)
$desc=$_POST['desc']; $desc=$_POST['desc'];
if (! $desc && $conf->global->PRODUIT_CHANGE_PROD_DESC)
if (! $desc)
{
$desc = $prod->description;
}
// ceci n'a plus d'intérêt si on rajoute la description produit
// à chaque fois ???
if ($conf->global->PRODUIT_CHANGE_PROD_DESC)
{ {
$desc = $prod->description; $desc = $prod->description;
} }
@ -457,7 +456,7 @@ if (($_POST['action'] == 'addligne' || $_POST['action'] == 'addligne_predef') &&
if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['save'] == $langs->trans('Save')) if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['save'] == $langs->trans('Save'))
{ {
$fac = new Facture($db,'',$_POST['facid']); $fac = new Facture($db,'',$_POST['facid']);
$fac->fetch($_POST['facid']); if (! $fac->fetch($_POST['facid']) > 0) dolibarr_print_error($db);
$datestart=''; $datestart='';
$dateend=''; $dateend='';
@ -478,6 +477,10 @@ if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST
$_POST['tva_tx'] $_POST['tva_tx']
); );
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs);
$_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition $_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition
} }
@ -676,12 +679,12 @@ if ($_POST['action'] == 'send' || $_POST['action'] == 'relance')
*/ */
if ($_REQUEST['action'] == 'builddoc') // En get ou en post if ($_REQUEST['action'] == 'builddoc') // En get ou en post
{ {
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
$outputlangs->setDefaultLang($_REQUEST['lang_id']);
$facture = new Facture($db, 0, $_GET['facid']); $facture = new Facture($db, 0, $_GET['facid']);
$facture->fetch($_GET['facid']); $facture->fetch($_GET['facid']);
if ($_REQUEST['model']) $facture->set_pdf_model($user, $_REQUEST['model']); if ($_REQUEST['model']) $facture->set_pdf_model($user, $_REQUEST['model']);
$result=facture_pdf_create($db, $_REQUEST['facid'], '', $facture->modelpdf, $outputlangs); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
$result=facture_pdf_create($db, $facture->id, '', $facture->modelpdf, $outputlangs);
if ($result <= 0) if ($result <= 0)
{ {
dolibarr_print_error($db,$result); dolibarr_print_error($db,$result);
@ -2268,8 +2271,8 @@ else
if (! is_readable($file)) if (! is_readable($file))
{ {
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs"); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
$outputlangs->setDefaultLang($_REQUEST['lang_id']); if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
$result=facture_pdf_create($db, $_REQUEST['facid'], '', $_REQUEST['model'], $outputlangs); $result=facture_pdf_create($db, $fac->id, '', $_REQUEST['model'], $outputlangs);
if ($result <= 0) if ($result <= 0)
{ {
dolibarr_print_error($db,$result); dolibarr_print_error($db,$result);
@ -2319,8 +2322,8 @@ else
if (! is_readable($file)) if (! is_readable($file))
{ {
$outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs"); $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs");
$outputlangs->setDefaultLang($_REQUEST['lang_id']); if ($_REQUEST['lang_id']) $outputlangs->setDefaultLang($_REQUEST['lang_id']);
$result=facture_pdf_create($db, $_REQUEST['facid'], '', $_REQUEST['model'], $outputlangs); $result=facture_pdf_create($db, $fac->id, '', $_REQUEST['model'], $outputlangs);
if ($result <= 0) if ($result <= 0)
{ {
dolibarr_print_error($db,$result); dolibarr_print_error($db,$result);

View File

@ -447,7 +447,7 @@ class FactureRec extends Facture
if ( $this->db->query( $sql) ) if ( $this->db->query( $sql) )
{ {
$this->updateprice($facid); $this->update_price($facid);
return 1; return 1;
} }
else else
@ -492,7 +492,7 @@ class FactureRec extends Facture
$result = $this->db->query( $sql); $result = $this->db->query( $sql);
$this->updateprice($this->id); $this->update_price($this->id);
} }
} }
@ -506,14 +506,14 @@ class FactureRec extends Facture
$sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = $rowid;"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = $rowid;";
$result = $this->db->query( $sql); $result = $this->db->query( $sql);
$this->updateprice($this->id); $this->update_price($this->id);
} }
} }
/** /**
* Mise à jour des sommes de la facture * Mise à jour des sommes de la facture
*/ */
function updateprice($facid) function update_price($facid)
{ {
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php";
$err=0; $err=0;
@ -595,7 +595,7 @@ class FactureRec extends Facture
if ($this->db->query($sql) ) if ($this->db->query($sql) )
{ {
$this->updateprice($this->id); $this->update_price($this->id);
return 1; return 1;
} }
else else

View File

@ -151,7 +151,7 @@ if ($_GET["propalid"] > 0)
print '</td>'; print '</td>';
print '</tr>'; print '</tr>';
$rowspan=9; $rowspan=8;
// Société // Société
print '<tr><td>'.$langs->trans('Company').'</td><td colspan="5">'.$societe->getNomUrl(1).'</td></tr>'; print '<tr><td>'.$langs->trans('Company').'</td><td colspan="5">'.$societe->getNomUrl(1).'</td></tr>';
@ -231,6 +231,7 @@ if ($_GET["propalid"] > 0)
print '</td></tr>'; print '</td></tr>';
// Destinataire // Destinataire
/* Remplacé par contacts de propal
$langs->load('mails'); $langs->load('mails');
print '<tr>'; print '<tr>';
print '<td>'.$langs->trans('MailTo').'</td>'; print '<td>'.$langs->trans('MailTo').'</td>';
@ -253,6 +254,7 @@ if ($_GET["propalid"] > 0)
print '<td colspan="3">&nbsp;</td>'; print '<td colspan="3">&nbsp;</td>';
} }
} }
*/
// Projet // Projet
if ($conf->projet->enabled) if ($conf->projet->enabled)

View File

@ -397,53 +397,6 @@ class Expedition
} }
/**
* Ajoute un produit
*
*/
function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0)
{
if ($this->statut == 0)
{
if (strlen(trim($p_qty)) == 0)
{
$p_qty = 1;
}
$p_price = price2num($p_price);
$price = $p_price;
$subprice = $p_price;
if ($remise_percent > 0)
{
$remise = round(($p_price * $remise_percent / 100), 2);
$price = $p_price - $remise;
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
$sql .= " (".$this->id.", $p_product_id,". $p_qty.",". $price.",".$p_tva_tx.",'". addslashes($p_desc) ."',$remise_percent, $subprice) ; ";
if ($this->db->query($sql) )
{
if ($this->update_price() > 0)
{
return 1;
}
else
{
return -1;
}
}
else
{
print $this->db->error();
print "<br>".$sql;
return -2;
}
}
}
/** /**
* \brief Crée un bon de livraison à partir de l'expédition * \brief Crée un bon de livraison à partir de l'expédition
* \param user Utilisateur * \param user Utilisateur

View File

@ -109,8 +109,6 @@ class Facture
{ {
global $langs,$conf,$mysoc; global $langs,$conf,$mysoc;
$this->db->begin();
// Nettoyage paramètres // Nettoyage paramètres
$this->note=trim($this->note); $this->note=trim($this->note);
$this->note_public=trim($this->note_public); $this->note_public=trim($this->note_public);
@ -122,6 +120,11 @@ class Facture
dolibarr_syslog("Facture::create"); dolibarr_syslog("Facture::create");
$soc = new Societe($this->db);
$soc->fetch($this->socidp);
$this->db->begin();
// Facture récurrente // Facture récurrente
if ($this->fac_rec > 0) if ($this->fac_rec > 0)
{ {
@ -196,8 +199,7 @@ class Facture
{ {
$prod = new Product($this->db, $this->products[$i]); $prod = new Product($this->db, $this->products[$i]);
$res=$prod->fetch($this->products[$i]); $res=$prod->fetch($this->products[$i]);
$soc = new Societe($this->db);
$soc->fetch($this->socidp);
$tva_tx = get_default_tva($mysoc,$soc,$prod->tva_tx); $tva_tx = get_default_tva($mysoc,$soc,$prod->tva_tx);
// multiprix // multiprix
if($conf->global->PRODUIT_MULTIPRICES == 1) if($conf->global->PRODUIT_MULTIPRICES == 1)
@ -258,7 +260,7 @@ class Facture
if ($resql) if ($resql)
{ {
$resql=$this->updateprice($this->id); $resql=$this->update_price($this->id);
if ($resql) if ($resql)
{ {
// Appel des triggers // Appel des triggers
@ -789,7 +791,7 @@ class Facture
$numfa = $this->getNextNumRef($soc); $numfa = $this->getNextNumRef($soc);
} }
$this->updateprice($this->id); $this->update_price($this->id);
// Validation de la facture // Validation de la facture
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture '; $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture ';
@ -1014,7 +1016,7 @@ class Facture
function addline($facid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart='', $dateend='', $ventil = 0) function addline($facid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart='', $dateend='', $ventil = 0)
{ {
global $conf; global $conf;
dolibarr_syslog("facture.class.php::addline($facid,$desc,$product_desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$datestart,$dateend,$ventil)"); dolibarr_syslog("facture.class.php::addline($facid,$desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$datestart,$dateend,$ventil)");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
if ($this->brouillon) if ($this->brouillon)
@ -1048,8 +1050,6 @@ class Facture
$remise = round(($pu * $remise_percent / 100),2); $remise = round(($pu * $remise_percent / 100),2);
$price = ($pu - $remise); $price = ($pu - $remise);
} }
$price = price2num($price);
$subprice = price2num($subprice);
// 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';
@ -1096,7 +1096,7 @@ class Facture
if ( $this->db->query($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->updateprice($facid); $result=$this->update_price($facid);
if ($result > 0) if ($result > 0)
{ {
@ -1105,14 +1105,18 @@ class Facture
} }
else else
{ {
$this->error=$this->db->error();
dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
} }
else else
{ {
dolibarr_print_error($this->db); $this->error=$this->db->error();
$this->db->commit(); dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback();
return -2;
} }
} }
} }
@ -1167,8 +1171,8 @@ 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"; $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET";
$sql.= " set description='".addslashes($desc)."'"; $sql.= " description='".addslashes($desc)."'";
$sql.= ",price='".price2num($price)."'"; $sql.= ",price='".price2num($price)."'";
$sql.= ",subprice='".price2num($subprice)."'"; $sql.= ",subprice='".price2num($subprice)."'";
$sql.= ",remise='".price2num($remise)."'"; $sql.= ",remise='".price2num($remise)."'";
@ -1186,10 +1190,10 @@ class Facture
$sql.= " WHERE rowid = ".$rowid; $sql.= " WHERE rowid = ".$rowid;
$result = $this->db->query( $sql); $result = $this->db->query( $sql);
if ($result) if ($result > 0)
{ {
// Mise a jour info denormalisees au niveau facture // Mise a jour info denormalisees au niveau facture
$this->updateprice($this->id); $this->update_price($this->id);
$this->db->commit(); $this->db->commit();
return $result; return $result;
} }
@ -1202,7 +1206,7 @@ class Facture
} }
else else
{ {
$this->error="Invoice statut makes operation forbidden"; $this->error="Facture::UpdateLine Invoice statut makes operation forbidden";
return -2; return -2;
} }
} }
@ -1217,7 +1221,7 @@ class Facture
{ {
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid; $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid;
$result = $this->db->query( $sql); $result = $this->db->query( $sql);
$this->updateprice($this->id); $this->update_price($this->id);
} }
} }
@ -1226,12 +1230,12 @@ class Facture
* \param facid id de la facture a modifier * \param facid id de la facture a modifier
* \return int <0 si ko, >0 si ok * \return int <0 si ko, >0 si ok
*/ */
function updateprice($facid) function update_price($facid)
{ {
$tvas=array(); $tvas=array();
$err=0; $err=0;
// Lit les lignes detail // Liste des lignes factures a sommer
$sql = 'SELECT qty, tva_taux, subprice, remise_percent, price, total_ht, total_tva, total_ttc'; $sql = 'SELECT qty, tva_taux, subprice, remise_percent, price, total_ht, total_tva, total_ttc';
$sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet'; $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet';
$sql.= ' WHERE fk_facture = '.$facid; $sql.= ' WHERE fk_facture = '.$facid;
@ -1350,7 +1354,7 @@ class Facture
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
$this->remise_percent = $remise; $this->remise_percent = $remise;
$this->updateprice($this->id); $this->update_price($this->id);
return 1; return 1;
} }
else else
@ -1385,7 +1389,7 @@ class Facture
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
$this->remise_absolue = $remise; $this->remise_absolue = $remise;
$this->updateprice($this->id); $this->update_price($this->id);
return 1; return 1;
} }
else else

View File

@ -662,51 +662,6 @@ class CommandeFournisseur extends Commande
} }
} }
/**
* Ajoute un produit
*
*/
function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0)
{
if ($this->statut == 0)
{
if (strlen(trim($p_qty)) == 0)
{
$p_qty = 1;
}
$p_price = price2num($p_price);
$price = $p_price;
$subprice = $p_price;
if ($remise_percent > 0)
{
$remise = round(($p_price * $remise_percent / 100), 2);
$price = $p_price - $remise;
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
$sql .= " ('".$this->id."', '$p_product_id','". $p_qty."','". $price."','".$p_tva_tx."','".addslashes($p_desc)."','$remise_percent', '$subprice') ; ";
if ($this->db->query($sql) )
{
if ($this->update_price() > 0)
{
return 1;
}
else
{
return -1;
}
}
else
{
dolibarr_print_error($this->db);
return -2;
}
}
}
/** /**
* Supprime une ligne de la commande * Supprime une ligne de la commande
* *

View File

@ -125,7 +125,7 @@ class FactureFournisseur extends Facture
} }
} }
// Mise à jour prix // Mise à jour prix
if ($this->updateprice($this->id) > 0) if ($this->update_price($this->id) > 0)
{ {
$this->db->commit(); $this->db->commit();
return $this->id; return $this->id;
@ -340,7 +340,7 @@ class FactureFournisseur extends Facture
dolibarr_print_error($this->db); dolibarr_print_error($this->db);
} }
// Mise a jour prix facture // Mise a jour prix facture
$this->updateprice($this->id); $this->update_price($this->id);
} }
/** /**
@ -378,7 +378,7 @@ class FactureFournisseur extends Facture
if ($resql) if ($resql)
{ {
// Mise a jour prix facture // Mise a jour prix facture
return $this->updateprice($this->id); return $this->update_price($this->id);
} }
else else
{ {
@ -403,7 +403,7 @@ class FactureFournisseur extends Facture
dolibarr_print_error($this->db); dolibarr_print_error($this->db);
} }
// Mise a jour prix facture // Mise a jour prix facture
$this->updateprice($this->id); $this->update_price($this->id);
return 1; return 1;
} }
@ -412,7 +412,7 @@ class FactureFournisseur extends Facture
* \param facid id de la facture a modifier * \param facid id de la facture a modifier
* \return int <0 si ko, >0 si ok * \return int <0 si ko, >0 si ok
*/ */
function updateprice($facid) function update_price($facid)
{ {
$total_ht = 0; $total_ht = 0;
$total_tva = 0; $total_tva = 0;

View File

@ -163,6 +163,8 @@ if (isset($_POST['action']) && $_POST['action'] == 'upgrade')
migrate_modeles($db,$langs,$conf); migrate_modeles($db,$langs,$conf);
migrate_price_facture($db,$langs,$conf);
// On commit dans tous les cas. // On commit dans tous les cas.
// La procédure etant conçue pour pouvoir passer plusieurs fois quelquesoit la situation. // La procédure etant conçue pour pouvoir passer plusieurs fois quelquesoit la situation.
@ -583,6 +585,52 @@ function migrate_paiementfourn_facturefourn($db,$langs,$conf)
} }
/*
* Mise a jour des totaux facture
*/
function migrate_price_facture($db,$langs,$conf)
{
if ($conf->facture->enabled)
{
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture";
if ($db->query($sql))
{
$num = $db->num_rows();
$i = 0;
while ($i < $num)
{
$row = $db->fetch_row($i);
$facture = new Facture($db);
if ( $facture->fetch($row[0]) )
{
if ( $facture->update_price($row[0]) > 0 )
{
print "(ok $row[0]) ";
}
else
{
print "Erreur #2";
$err++;
}
}
else
{
print "Erreur #3";
$err++;
}
$i++;
}
$db->free();
}
else
{
print "Erreur #1";
$err++;
}
}
}
/* /*
* Mise a jour des modeles selectionnes * Mise a jour des modeles selectionnes
*/ */

View File

@ -439,52 +439,7 @@ class Livraison
return $this->create($user); return $this->create($user);
} }
/**
* Ajoute un produit
*
*/
function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx=19.6, $p_product_id=0, $remise_percent=0)
{
if ($this->statut == 0)
{
if (strlen(trim($p_qty)) == 0)
{
$p_qty = 1;
}
$p_price = price2num($p_price);
$price = $p_price;
$subprice = $p_price;
if ($remise_percent > 0)
{
$remise = round(($p_price * $remise_percent / 100), 2);
$price = $p_price - $remise;
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commandedet (fk_commande, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
$sql .= " (".$this->id.", $p_product_id,". $p_qty.",". $price.",".$p_tva_tx.",'". addslashes($p_desc) ."',$remise_percent, $subprice) ; ";
if ($this->db->query($sql) )
{
if ($this->update_price() > 0)
{
return 1;
}
else
{
return -1;
}
}
else
{
print $this->db->error();
print "<br>".$sql;
return -2;
}
}
}
/** /**
* Ajoute une ligne * Ajoute une ligne
* *

View File

@ -780,7 +780,7 @@ class Product
if (! $id && ! $ref) if (! $id && ! $ref)
{ {
$this->error=$langs->trans('ErrorWrongParameters'); $this->error=$langs->trans('ErrorWrongParameters');
dolibarr_error("Product::fetch ".$this->error); dolibarr_print_error("Product::fetch ".$this->error);
return -1; return -1;
} }

View File

@ -228,16 +228,41 @@ if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes' && $user-
if ($_POST["action"] == 'addinpropal') if ($_POST["action"] == 'addinpropal')
{ {
$propal = New Propal($db); $propal = New Propal($db);
$propal->fetch($_POST["propalid"]); $result=$propal->fetch($_POST["propalid"]);
if ($result <= 0)
$result = $propal->insert_product($_GET["id"], $_POST["qty"], $_POST["remise_percent"]);
if ( $result < 0)
{ {
$mesg = $langs->trans("ErrorUnknown").": $result"; dolibarr_print_error($db,$propal->error);
exit;
} }
Header("Location: ../comm/propal.php?propalid=".$propal->id); $prod = new Product($db, $_GET['id']);
$result=$prod->fetch($_GET['id']);
if ($result <= 0)
{
dolibarr_print_error($db,$prod->error);
exit; exit;
}
// multiprix
if ($conf->global->PRODUIT_MULTIPRICES == 1)
{
$pu = $prod->multiprices[$soc->price_level];
}
else
{
$pu=$prod->price;
}
$desc = $prod->desc;
$tva_tx = get_default_tva($mysoc,$soc,$prod->tva_tx);
$result = $propal->addline($propal->id, $desc, $pu, $_POST["qty"], $tva_tx, $prod->id, $_POST["remise_percent"]);
if ($result > 0)
{
Header("Location: ../comm/propal.php?propalid=".$propal->id);
return;
}
$mesg = $langs->trans("ErrorUnknown").": $result";
} }
/* /*

View File

@ -123,7 +123,6 @@ class Propal
* \param qty Quantité * \param qty Quantité
* \param remise_percent Remise relative effectuée sur le produit * \param remise_percent Remise relative effectuée sur le produit
* \return void * \return void
* \see insert_product
*/ */
function add_product($idproduct, $qty, $remise_percent=0) function add_product($idproduct, $qty, $remise_percent=0)
{ {
@ -157,10 +156,13 @@ class Propal
/** /**
* \brief Ajout d'un produit dans la proposition, en base * \brief Ajout d'un produit dans la proposition, en base
* \param idproduct Id du produit à ajouter * \param propalid id de la propale
* \param qty Quantité * \param desc description de la ligne
* \param remise_percent Remise relative effectuée sur le produit * \param pu prix unitaire
* \param desc Descriptif optionnel * \param qty quantité
* \param txtva taux de tva forcé, sinon -1
* \param fk_product id du produit/service predéfini
* \param remise_percent pourcentage de remise de la ligne
* \return int >0 si ok, <0 si ko * \return int >0 si ok, <0 si ko
* \see add_product * \see add_product
* \remarks Les parametres sont deja censé etre juste et avec valeurs finales a l'appel * \remarks Les parametres sont deja censé etre juste et avec valeurs finales a l'appel
@ -168,37 +170,21 @@ class Propal
* par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,taux_produit) * par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,taux_produit)
* et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue) * et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue)
*/ */
function insert_product($idproduct, $qty, $remise_percent=0, $desc='') function addline($propalid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0)
{ {
global $conf,$mysoc; dolibarr_syslog("propal.class.php::addline $propalid, $desc, $pu, $qty, $txtva, $fk_product, $remise_percent");
dolibarr_syslog("propal.class.php::insert_product $idproduct, $qty, $remise_percent, $desc");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
if ($this->statut == 0) if ($this->statut == 0)
{ {
// Nettoyage parametres $this->db->begin();
// Nettoyage paramètres
$remise_percent=price2num($remise_percent); $remise_percent=price2num($remise_percent);
$qty=price2num($qty); $qty=price2num($qty);
if (! $qty) $qty=1;
if ($idproduct) $pu = price2num($pu);
{ $txtva = price2num($txtva);
$prod = new Product($this->db, $idproduct);
if ($prod->fetch($idproduct) > 0)
{
$this->fetch_client();
// multiprix
if ($conf->global->PRODUIT_MULTIPRICES == 1)
{
$pu = price2num($prod->multiprices[$this->client->price_level]);
}
else
{
$pu = price2num($prod->price);
}
$txtva = get_default_tva($mysoc,$this->client,$prod->tva_tx);
if (! $desc) $desc = $prod->description;
if ($conf->global->PRODUIT_CHANGE_PROD_DESC) $desc=$prod->description;
// Calcul du total TTC et de la TVA pour la ligne a partir de // Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva // qty, pu, remise_percent et txtva
@ -211,89 +197,6 @@ class Propal
// Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser) // Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser)
$price = $pu; $price = $pu;
$subprice = $pu;
$remise = 0;
if ($remise_percent > 0)
{
$remise = round(($prod->price * $remise_percent / 100), 2);
$price = $prod->price - $remise;
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propaldet (fk_propal, fk_product, qty, price, tva_tx, description, remise_percent, subprice,";
$sql.= " total_ht, total_tva, total_ttc)";
$sql.= " VALUES ";
$sql.= " (".$this->id.",". $idproduct.",'". $qty."','". price2num($price) ."','".$txtva."','".addslashes($desc?$desc:$prod->label)."','".price2num($remise_percent)."','".price2num($subprice)."',";
$sql.= " '".price2num($total_ht) ."',";
$sql.= " '".price2num($total_tva)."',";
$sql.= " '".price2num($total_ttc)."'";
$sql.= ")";
if ($this->db->query($sql))
{
$this->update_price();
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Error sql=$sql, error=".$this->error);
return -1;
}
}
else
{
$this->error=$this->db->error();
return -2;
}
}
}
else
{
return -3;
}
}
/**
* \brief Ajout d'un produit dans la proposition, en base
* \param desc Descriptif optionnel
* \param pu Prix
* \param qty Quantité
* \param tva_tx Taux tva
* \param remise_percent Remise effectuée sur le produit
* \return int >0 si ok, <0 si ko
* \see add_product
* \remarks Les parametres sont deja censé etre juste et avec valeurs finales a l'appel
* de cette methode. Aussi, pour le taux tva, il doit deja avoir ete défini
* par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,taux_produit)
* et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue)
*/
function insert_product_generic($desc, $pu, $qty, $tva_tx, $remise_percent=0)
{
global $conf,$mysoc;
dolibarr_syslog("propal.class.php::insert_product_generic $desc, $pu, $qty, $tva_tx, $remise_percent");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
if ($this->statut == 0)
{
// Nettoyage paramètres
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
if (! $qty) $qty=1;
$pu = price2num($pu);
$tva_tx = price2num($tva_tx);
// 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, $tva_tx);
$total_ht = $tabprice[0];
$total_tva = $tabprice[1];
$total_ttc = $tabprice[2];
// Anciens indicateurs: $price, $subprice, $remise (a ne plus utiliser)
$price = $pu;
$subprice = $pu; $subprice = $pu;
if ($remise_percent > 0) if ($remise_percent > 0)
{ {
@ -303,7 +206,10 @@ class Propal
$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 = "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.= " VALUES ";
$sql.= " (".$this->id.", 0,'". $qty."','". price2num($price)."','".$tva_tx."','".addslashes($desc)."','$remise_percent', '".price2num($subprice)."',"; $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_ht) ."',";
$sql.= " '".price2num($total_tva)."',"; $sql.= " '".price2num($total_tva)."',";
$sql.= " '".price2num($total_ttc)."'"; $sql.= " '".price2num($total_ttc)."'";
@ -311,13 +217,19 @@ class Propal
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
// Mise a jour informations denormalisees au niveau de la facture meme
$result=$this->update_price($facid);
if ($this->update_price() > 0) if ($result > 0)
{ {
$this->db->commit();
return 1; return 1;
} }
else else
{ {
$this->error=$this->db->error();
dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback();
return -1; return -1;
} }
} }
@ -325,6 +237,7 @@ class Propal
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
dolibarr_syslog("Error sql=$sql, error=".$this->error); dolibarr_syslog("Error sql=$sql, error=".$this->error);
$this->db->rollback();
return -2; return -2;
} }
} }
@ -341,26 +254,27 @@ class Propal
* \param desc Description * \param desc Description
* \return int 0 en cas de succès * \return int 0 en cas de succès
*/ */
function UpdateLigne($id, $pu, $qty, $remise_percent=0, $tva_tx, $desc='') function updateline($rowid, $pu, $qty, $remise_percent=0, $txtva, $desc='')
{ {
global $conf,$mysoc; dolibarr_syslog("propal.class.php::updateligne $rowid, $pu, $qty, $remise_percent, $txtva, $desc");
dolibarr_syslog("propal.class.php::updateligne $id, $pu, $qty, $remise_percent, $tva_tx, $desc");
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
if ($this->statut == 0) if ($this->statut == 0)
{ {
$this->db->begin();
// Nettoyage paramètres // Nettoyage paramètres
$remise_percent=price2num($remise_percent); $remise_percent=price2num($remise_percent);
$qty=price2num($qty); $qty=price2num($qty);
if (! $qty) $qty=1; if (! $qty) $qty=1;
$pu = price2num($pu); $pu = price2num($pu);
$tva_tx = price2num($tva_tx); $txtva = price2num($txtva);
// Calcul du total TTC et de la TVA pour la ligne a partir de // Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva // qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // 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. // 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, $tva_tx); $tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva);
$total_ht = $tabprice[0]; $total_ht = $tabprice[0];
$total_tva = $tabprice[1]; $total_tva = $tabprice[1];
$total_ttc = $tabprice[2]; $total_ttc = $tabprice[2];
@ -379,21 +293,24 @@ class Propal
$sql.= " , price='". price2num($price)."'"; $sql.= " , price='". price2num($price)."'";
$sql.= " , remise_percent='".$remise_percent."'"; $sql.= " , remise_percent='".$remise_percent."'";
$sql.= " , subprice='".$subprice."'"; $sql.= " , subprice='".$subprice."'";
$sql.= " , tva_tx='".$tva_tx."'"; $sql.= " , tva_tx='".$txtva."'";
$sql.= " , description='".addslashes($desc)."'"; $sql.= " , description='".addslashes($desc)."'";
$sql.= " , total_ht='".price2num($total_ht)."'"; $sql.= " , total_ht='".price2num($total_ht)."'";
$sql.= " , total_tva='".price2num($total_tva)."'"; $sql.= " , total_tva='".price2num($total_tva)."'";
$sql.= " , total_ttc='".price2num($total_ttc)."'"; $sql.= " , total_ttc='".price2num($total_ttc)."'";
$sql.= " WHERE rowid = '".$id."';"; $sql.= " WHERE rowid = '".$rowid."';";
if ($this->db->query($sql)) $result=$this->db->query($sql);
if ($result > 0)
{ {
$this->update_price(); $this->update_price();
$this->db->commit();
return 0; return 0;
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
$this->db->rollback();
dolibarr_syslog("Propal::UpdateLigne Erreur sql=$sql, error=".$this->error); dolibarr_syslog("Propal::UpdateLigne Erreur sql=$sql, error=".$this->error);
return -1; return -1;
} }
@ -744,15 +661,18 @@ class Propal
* \brief Crée une propal * \brief Crée une propal
* \return int <0 si ko, >=0 si ok * \return int <0 si ko, >=0 si ok
*/ */
function create() function create($user='')
{ {
global $langs,$conf; global $langs,$conf,$mysoc;
// Nettoyage/définition paramètres // Nettoyage/définition paramètres
$this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600); $this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600);
dolibarr_syslog("Propal::create ref=".$this->ref); dolibarr_syslog("Propal::create ref=".$this->ref);
$soc = new Societe($this->db);
$soc->fetch($this->socidp);
$this->db->begin(); $this->db->begin();
// Insertion dans la base // Insertion dans la base
@ -760,7 +680,7 @@ class Propal
$sql.= " tva, total, datep, datec, ref, fk_user_author, note, note_public, model_pdf, fin_validite,"; $sql.= " tva, total, datep, datec, ref, fk_user_author, note, note_public, model_pdf, fin_validite,";
$sql.= " fk_cond_reglement, fk_mode_reglement, date_livraison, ref_client) "; $sql.= " fk_cond_reglement, fk_mode_reglement, date_livraison, ref_client) ";
$sql.= " VALUES ($this->socidp, $this->contactid, 0, $this->remise, $this->remise_percent, $this->remise_absolue,"; $sql.= " VALUES ($this->socidp, $this->contactid, 0, $this->remise, $this->remise_percent, $this->remise_absolue,";
$sql.= " 0,0,".$this->db->idate($this->datep).", now(), '$this->ref', $this->author,"; $sql.= " 0,0,".$this->db->idate($this->datep).", now(), '".$this->ref."', ".$this->author.",";
$sql.= "'".addslashes($this->note)."',"; $sql.= "'".addslashes($this->note)."',";
$sql.= "'".addslashes($this->note_public)."',"; $sql.= "'".addslashes($this->note_public)."',";
$sql.= "'$this->modelpdf',".$this->db->idate($this->fin_validite).","; $sql.= "'$this->modelpdf',".$this->db->idate($this->fin_validite).",";
@ -780,28 +700,54 @@ class Propal
for ($i = 0 ; $i < sizeof($this->products) ; $i++) for ($i = 0 ; $i < sizeof($this->products) ; $i++)
{ {
$prod = new Product($this->db, $this->products[$i]); $prod = new Product($this->db, $this->products[$i]);
$result=$prod->fetch($this->products[$i]); if ($prod->fetch($this->products[$i]))
{
$tva_tx = get_default_tva($mysoc,$soc,$prod->tva_tx);
// multiprix
if($conf->global->PRODUIT_MULTIPRICES == 1)
$price = $prod->multiprices[$soc->price_level];
else
$price = $prod->price;
$result=$this->insert_product($this->products[$i], $resql = $this->addline(
$this->id,
$prod->libelle,
$prod->description,
$price,
$this->products_qty[$i], $this->products_qty[$i],
$this->products_remise_percent[$i]); $tva_tx,
$this->products[$i],
$this->products_remise_percent[$i],
$this->products_date_start[$i],
$this->products_date_end[$i]
);
if ($resql < 0)
{
$this->error=$this->db->error;
dolibarr_print_error($this->db);
break;
}
}
} }
// Affectation au projet // Affectation au projet
if ($this->projetidp) if ($resql && $this->projetidp)
{ {
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref'"; $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref'";
$result=$this->db->query($sql); $result=$this->db->query($sql);
} }
// Affectation de l'adresse de livraison // Affectation de l'adresse de livraison
if ($this->adresse_livraison_id) if ($resql && $this->adresse_livraison_id)
{ {
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_adresse_livraison=$this->adresse_livraison_id WHERE ref='$this->ref'"; $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_adresse_livraison=$this->adresse_livraison_id WHERE ref='$this->ref'";
$result=$this->db->query($sql); $result=$this->db->query($sql);
} }
// Mise a journ infos dénormalisés if ($resql)
{
// Mise a jour infos dénormalisés
$resql=$this->update_price(); $resql=$this->update_price();
if ($resql) if ($resql)
{ {
@ -823,6 +769,7 @@ class Propal
} }
} }
} }
}
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
@ -842,9 +789,7 @@ class Propal
{ {
include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php'); include_once(DOL_DOCUMENT_ROOT.'/lib/price.lib.php');
/* // Liste des lignes factures a sommer
* Liste des produits a ajouter
*/
$sql = "SELECT price, qty, tva_tx, total_ht, total_tva, total_ttc"; $sql = "SELECT price, qty, tva_tx, total_ht, total_tva, total_ttc";
$sql.= " FROM ".MAIN_DB_PREFIX."propaldet"; $sql.= " FROM ".MAIN_DB_PREFIX."propaldet";
$sql.= " WHERE fk_propal = ".$this->id; $sql.= " WHERE fk_propal = ".$this->id;
@ -2221,16 +2166,22 @@ class Propal
*/ */
foreach($this->lignes as $ligne) foreach($this->lignes as $ligne)
{ {
if($ligne->product_id != 0) $resql = $this->addline(
{ $this->id,
$result=$this->insert_product($ligne->product_id, $prod->libelle,
$prod->description,
$price,
$ligne->qty, $ligne->qty,
$ligne->remise_percent, $ligne->product_desc); $tva_tx,
} $ligne->product_id,
else $ligne->remise_percent
);
if ($resql < 0)
{ {
$this->insert_product_generic($ligne->desc, $ligne->price, $this->error=$this->db->error;
$ligne->qty, $ligne->tva_tx, $ligne->remise_percent); dolibarr_print_error($this->db);
break;
} }
} }

View File

@ -120,6 +120,11 @@ ALTER TABLE llx_propaldet ADD COLUMN total_tva real AFTER total_ht;
ALTER TABLE llx_propaldet ADD COLUMN total_ttc real AFTER total_tva; ALTER TABLE llx_propaldet ADD COLUMN total_ttc real AFTER total_tva;
ALTER TABLE llx_propaldet ADD COLUMN info_bits integer DEFAULT 0 AFTER total_ttc; ALTER TABLE llx_propaldet ADD COLUMN info_bits integer DEFAULT 0 AFTER total_ttc;
ALTER TABLE llx_commandedet ADD COLUMN total_ht real AFTER price;
ALTER TABLE llx_commandedet ADD COLUMN total_tva real AFTER total_ht;
ALTER TABLE llx_commandedet ADD COLUMN total_ttc real AFTER total_tva;
ALTER TABLE llx_commandedet ADD COLUMN info_bits integer DEFAULT 0 AFTER total_ttc;
ALTER TABLE llx_commande ADD INDEX idx_commande_fk_soc (fk_soc); ALTER TABLE llx_commande ADD INDEX idx_commande_fk_soc (fk_soc);
ALTER TABLE llx_commande ADD CONSTRAINT fk_commande_societe FOREIGN KEY (fk_soc) REFERENCES llx_societe (idp); ALTER TABLE llx_commande ADD CONSTRAINT fk_commande_societe FOREIGN KEY (fk_soc) REFERENCES llx_societe (idp);

View File

@ -26,13 +26,17 @@ create table llx_commandedet
fk_product integer, fk_product integer,
label varchar(255), label varchar(255),
description text, description text,
tva_tx real DEFAULT 19.6, -- taux tva tva_tx real, -- taux tva
qty real, -- quantité qty real, -- quantité
remise_percent real DEFAULT 0, -- pourcentage de remise remise_percent real DEFAULT 0, -- pourcentage de remise
remise real DEFAULT 0, -- montant de la remise remise real DEFAULT 0, -- montant de la remise
fk_remise_except integer NULL, -- Lien vers table des remises fixes fk_remise_except integer NULL, -- Lien vers table des remises fixes
subprice real, -- prix avant remise subprice real, -- prix avant remise
price real, -- prix final price real, -- prix final
total_ht real, -- Total HT de la ligne toute quantité et incluant remise ligne et globale
total_tva real, -- Total TVA de la ligne toute quantité et incluant remise ligne et globale
total_ttc real, -- Total TTC de la ligne toute quantité et incluant remise ligne et globale
info_bits integer DEFAULT 0, -- TVA NPR ou non
coef real, -- coefficient de marge coef real, -- coefficient de marge
rang integer DEFAULT 0 rang integer DEFAULT 0
)type=innodb; )type=innodb;

View File

@ -26,7 +26,7 @@ create table llx_facturedet
fk_facture integer NOT NULL, fk_facture integer NOT NULL,
fk_product integer NULL, -- Doit pouvoir etre nul pour ligne detail sans produits fk_product integer NULL, -- Doit pouvoir etre nul pour ligne detail sans produits
description text, description text,
tva_taux real DEFAULT 19.6, -- Taux tva produit/service (exemple 19.6) tva_taux real, -- Taux tva produit/service (exemple 19.6)
qty real, -- Quantité (exemple 2) qty real, -- Quantité (exemple 2)
remise_percent real DEFAULT 0, -- % de la remise ligne (exemple 20%) remise_percent real DEFAULT 0, -- % de la remise ligne (exemple 20%)
remise real DEFAULT 0, -- Montant calculé de la remise % sur PU HT (exemple 20) remise real DEFAULT 0, -- Montant calculé de la remise % sur PU HT (exemple 20)

View File

@ -25,7 +25,7 @@ create table llx_propaldet
fk_propal integer, fk_propal integer,
fk_product integer, fk_product integer,
description text, description text,
tva_tx real DEFAULT 19.6, -- taux tva tva_tx real, -- taux tva
qty real, -- quantité qty real, -- quantité
remise_percent real DEFAULT 0, -- pourcentage de remise remise_percent real DEFAULT 0, -- pourcentage de remise
remise real DEFAULT 0, -- montant de la remise remise real DEFAULT 0, -- montant de la remise