Fix: [ bug #1206 ] PMP price is bad calculated

This commit is contained in:
Laurent Destailleur 2014-03-01 14:49:30 +01:00
parent a084887442
commit ea773531b5
3 changed files with 35 additions and 19 deletions

View File

@ -224,7 +224,8 @@ class CommandeFournisseur extends CommonOrder
$line->tva_tx = $objp->tva_tx; $line->tva_tx = $objp->tva_tx;
$line->localtax1_tx = $objp->localtax1_tx; $line->localtax1_tx = $objp->localtax1_tx;
$line->localtax2_tx = $objp->localtax2_tx; $line->localtax2_tx = $objp->localtax2_tx;
$line->subprice = $objp->subprice; $line->subprice = $objp->subprice; // deprecated
$line->pu_ht = $objp->subprice; // Unit price HT
$line->remise_percent = $objp->remise_percent; $line->remise_percent = $objp->remise_percent;
$line->total_ht = $objp->total_ht; $line->total_ht = $objp->total_ht;
$line->total_tva = $objp->total_tva; $line->total_tva = $objp->total_tva;
@ -462,7 +463,9 @@ class CommandeFournisseur extends CommonOrder
{ {
$mouvP = new MouvementStock($this->db); $mouvP = new MouvementStock($this->db);
// We increment stock of product (and sub-products) // We increment stock of product (and sub-products)
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderBackToDraftInDolibarr",$this->ref)); $up_ht_disc=$this->lines[$i]->subprice;
if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderBackToDraftInDolibarr",$this->ref));
if ($result < 0) { $error++; } if ($result < 0) { $error++; }
} }
} }
@ -715,7 +718,9 @@ class CommandeFournisseur extends CommonOrder
{ {
$mouvP = new MouvementStock($this->db); $mouvP = new MouvementStock($this->db);
// We decrement stock of product (and sub-products) // We decrement stock of product (and sub-products)
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderApprovedInDolibarr",$this->ref)); $up_ht_disc=$this->lines[$i]->subprice;
if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderApprovedInDolibarr",$this->ref));
if ($result < 0) { $error++; } if ($result < 0) { $error++; }
} }
} }
@ -1308,7 +1313,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $product Id of product to dispatch * @param int $product Id of product to dispatch
* @param double $qty Qty to dispatch * @param double $qty Qty to dispatch
* @param int $entrepot Id of warehouse to add product * @param int $entrepot Id of warehouse to add product
* @param double $price Price for PMP value calculation * @param double $price Unit Price for PMP value calculation (Unit price without Tax and taking into account discount)
* @param string $comment Comment for stock movement * @param string $comment Comment for stock movement
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
@ -1364,6 +1369,7 @@ class CommandeFournisseur extends CommonOrder
$mouv = new MouvementStock($this->db); $mouv = new MouvementStock($this->db);
if ($product > 0) if ($product > 0)
{ {
// $price should take into account discount (except if option STOCK_EXCLUDE_DISCOUNT_FOR_PMP is on)
$result=$mouv->reception($user, $product, $entrepot, $qty, $price, $comment); $result=$mouv->reception($user, $product, $entrepot, $qty, $price, $comment);
if ($result < 0) if ($result < 0)
{ {

View File

@ -883,7 +883,9 @@ class FactureFournisseur extends CommonInvoice
{ {
$mouvP = new MouvementStock($this->db); $mouvP = new MouvementStock($this->db);
// We increase stock for product // We increase stock for product
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->pu_ht, $langs->trans("InvoiceValidatedInDolibarr",$num)); $up_ht_disc=$this->lines[$i]->pu_ht;
if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("InvoiceValidatedInDolibarr",$num));
if ($result < 0) { $error++; } if ($result < 0) { $error++; }
} }
} }

View File

@ -42,7 +42,7 @@ $langs->load('products');
$langs->load('stocks'); $langs->load('stocks');
// Security check // Security check
$id = isset($_GET["id"])?$_GET["id"]:''; $id = GETPOST("id",'int');
if ($user->societe_id) $socid=$user->societe_id; if ($user->societe_id) $socid=$user->societe_id;
$result = restrictedArea($user, 'fournisseur', $id, '', 'commande'); $result = restrictedArea($user, 'fournisseur', $id, '', 'commande');
@ -64,7 +64,9 @@ $mesg='';
if ($_POST["action"] == 'dispatch' && $user->rights->fournisseur->commande->receptionner) if ($_POST["action"] == 'dispatch' && $user->rights->fournisseur->commande->receptionner)
{ {
$commande = new CommandeFournisseur($db); $commande = new CommandeFournisseur($db);
$commande->fetch($_GET["id"]); $commande->fetch($id);
$db->begin();
foreach($_POST as $key => $value) foreach($_POST as $key => $value)
{ {
@ -73,7 +75,7 @@ if ($_POST["action"] == 'dispatch' && $user->rights->fournisseur->commande->rece
$prod = "product_".$reg[1]; $prod = "product_".$reg[1];
$qty = "qty_".$reg[1]; $qty = "qty_".$reg[1];
$ent = "entrepot_".$reg[1]; $ent = "entrepot_".$reg[1];
$pu = "pu_".$reg[1]; $pu = "pu_".$reg[1]; // This is unit price including discount
if ($_POST[$ent] > 0) if ($_POST[$ent] > 0)
{ {
$result = $commande->DispatchProduct($user, $_POST[$prod], $_POST[$qty], $_POST[$ent], $_POST[$pu], $_POST["comment"]); $result = $commande->DispatchProduct($user, $_POST[$prod], $_POST[$qty], $_POST[$ent], $_POST[$pu], $_POST["comment"]);
@ -96,17 +98,19 @@ if ($_POST["action"] == 'dispatch' && $user->rights->fournisseur->commande->rece
$result_trigger=$interface->run_triggers('ORDER_SUPPLIER_DISPATCH',$commande,$user,$langs,$conf); $result_trigger=$interface->run_triggers('ORDER_SUPPLIER_DISPATCH',$commande,$user,$langs,$conf);
if ($result_trigger < 0) { $error++; $commande->errors=$interface->errors; } if ($result_trigger < 0) { $error++; $commande->errors=$interface->errors; }
// Fin appel triggers // Fin appel triggers
$db->commit();
} }
if ($result > 0) if ($result > 0)
{ {
$db->commit();
header("Location: dispatch.php?id=".$_GET["id"]); header("Location: dispatch.php?id=".$_GET["id"]);
exit; exit;
} }
else else
{ {
$db->rollback();
$mesg='<div class="error">'.$langs->trans($commande->error).'</div>'; $mesg='<div class="error">'.$langs->trans($commande->error).'</div>';
} }
} }
@ -232,12 +236,12 @@ if ($id > 0 || ! empty($ref))
$db->free($resql); $db->free($resql);
} }
$sql = "SELECT l.fk_product, l.subprice, SUM(l.qty) as qty,"; $sql = "SELECT l.fk_product, l.subprice, l.remise_percent, SUM(l.qty) as qty,";
$sql.= " p.ref, p.label"; $sql.= " p.ref, p.label";
$sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l"; $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product=p.rowid"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product=p.rowid";
$sql.= " WHERE l.fk_commande = ".$commande->id; $sql.= " WHERE l.fk_commande = ".$commande->id;
$sql.= " GROUP BY p.ref, p.label, l.fk_product, l.subprice"; // Calculation of amount dispatched is done per fk_product so we must group by fk_product $sql.= " GROUP BY p.ref, p.label, l.fk_product, l.subprice, l.remise_percent"; // Calculation of amount dispatched is done per fk_product so we must group by fk_product
$sql.= " ORDER BY p.ref, p.label"; $sql.= " ORDER BY p.ref, p.label";
$resql = $db->query($sql); $resql = $db->query($sql);
@ -278,17 +282,21 @@ if ($id > 0 || ! empty($ref))
if ($remaintodispatch) if ($remaintodispatch)
{ {
$nbproduct++; $nbproduct++;
$var=!$var; $var=!$var;
print "<tr ".$bc[$var].">"; print "<tr ".$bc[$var].">";
print '<td>'; print '<td>';
print '<a href="'.DOL_URL_ROOT.'/product/fournisseurs.php?id='.$objp->fk_product.'">'.img_object($langs->trans("ShowProduct"),'product').' '.$objp->ref.'</a>'; print '<a href="'.DOL_URL_ROOT.'/product/fournisseurs.php?id='.$objp->fk_product.'">'.img_object($langs->trans("ShowProduct"),'product').' '.$objp->ref.'</a>';
print ' - '.$objp->label; print ' - '.$objp->label."\n";
// To show detail cref and description value, we must make calculation by cref // To show detail cref and description value, we must make calculation by cref
//print ($objp->cref?' ('.$objp->cref.')':''); //print ($objp->cref?' ('.$objp->cref.')':'');
//if ($objp->description) print '<br>'.nl2br($objp->description); //if ($objp->description) print '<br>'.nl2br($objp->description);
print '<input name="product_'.$i.'" type="hidden" value="'.$objp->fk_product.'">'; print '<input name="product_'.$i.'" type="hidden" value="'.$objp->fk_product.'">'."\n";
print '<input name="pu_'.$i.'" type="hidden" value="'.$objp->subprice.'">';
$up_ht_disc=$objp->subprice;
if (! empty($objp->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $objp->remise_percent) / 100, 'MU');
print '<input name="pu_'.$i.'" type="hidden" value="'.$up_ht_disc.'">'."<!-- This is a up including discount -->\n";
print "</td>\n"; print "</td>\n";
print '<td align="right">'.$objp->qty.'</td>'; print '<td align="right">'.$objp->qty.'</td>';
@ -346,7 +354,7 @@ if ($id > 0 || ! empty($ref))
print '</form>'; print '</form>';
} }
dol_fiche_end(); dol_fiche_end();
// List of already dispatching // List of already dispatching
@ -369,9 +377,9 @@ if ($id > 0 || ! empty($ref))
if ($num > 0) if ($num > 0)
{ {
print "<br/>\n"; print "<br/>\n";
print_titre($langs->trans("ReceivingForSameOrder")); print_titre($langs->trans("ReceivingForSameOrder"));
print '<table class="noborder" width="100%">'; print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">'; print '<tr class="liste_titre">';