Fix bad recalculation of cost price of production of a product

This commit is contained in:
Laurent Destailleur 2022-10-17 16:39:52 +02:00
parent 159628be56
commit ebd7605545
2 changed files with 30 additions and 13 deletions

View File

@ -399,7 +399,7 @@ ActionAvailableOnVariantProductOnly=Action only available on the variant of prod
ProductsPricePerCustomer=Product prices per customers
ProductSupplierExtraFields=Additional Attributes (Supplier Prices)
DeleteLinkedProduct=Delete the child product linked to the combination
AmountUsedToUpdateWAP=Amount to use to update the Weighted Average Price
AmountUsedToUpdateWAP=Unit amount to use to update the Weighted Average Price
PMPValue=Weighted average price
PMPValueShort=WAP
mandatoryperiod=Mandatory periods

View File

@ -802,6 +802,8 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
// Lines to consume
$bomcostupdated = 0; // We will recalculate the unitary cost to produce a product using the real "products to consume into MO"
if (!empty($object->lines)) {
$nblinetoconsume = 0;
foreach ($object->lines as $line) {
@ -820,7 +822,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
$linecost = price2num($tmpproduct->pmp, 'MT');
if ($object->qty > 0) {
// add free consume line cost to bomcost
// add free consume line cost to $bomcostupdated
$costprice = price2num((!empty($tmpproduct->cost_price)) ? $tmpproduct->cost_price : $tmpproduct->pmp);
if (empty($costprice)) {
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
@ -831,12 +833,11 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
$costprice = 0;
}
}
$linecost = price2num(($line->qty * $costprice) / $object->qty, 'MT');
$bomcost += $linecost;
$linecost = price2num(($line->qty * $costprice) / $object->qty, 'MT'); // price for line for all quantities
$bomcostupdated += price2num(($line->qty * $costprice) / $object->qty, 'MU'); // same but with full accuracy
}
$bomcost = price2num($bomcost, 'MU');
$bomcostupdated = price2num($bomcostupdated, 'MU');
$arrayoflines = $object->fetchLinesLinked('consumed', $line->id);
$alreadyconsumed = 0;
foreach ($arrayoflines as $line2) {
@ -1111,7 +1112,7 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print '<td>'.$langs->trans("Product").'</td>';
print '<td class="right">'.$langs->trans("Qty").'</td>';
if ($permissiontoupdatecost) {
if (empty($bomcost)) {
if (empty($bomcostupdated)) {
print '<td class="right">'.$form->textwithpicto($langs->trans("UnitCost"), $langs->trans("AmountUsedToUpdateWAP")).'</td>';
} else {
print '<td class="right">'.$form->textwithpicto($langs->trans("ManufacturingPrice"), $langs->trans("AmountUsedToUpdateWAP")).'</td>';
@ -1199,17 +1200,25 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
if ($permissiontoupdatecost) {
// Defined $manufacturingcost
$manufacturingcost = 0;
if ($object->mrptype == 0) { // If MO is a "Manufacture" type (and not "Disassemble"
$manufacturingcost = $bomcost;
$manufacturingcostsrc = '';
if ($object->mrptype == 0) { // If MO is a "Manufacture" type (and not "Disassemble")
$manufacturingcost = $bomcostupdated;
$manufacturingcostsrc = $langs->trans("CalculatedFromProductsToConsume");
if (empty($manufacturingcost)) {
$manufacturingcost = $bomcost;
$manufacturingcostsrc = $langs->trans("ValueFromBom");
}
if (empty($manufacturingcost)) {
$manufacturingcost = price2num($tmpproduct->cost_price, 'MU');
$manufacturingcostsrc = $langs->trans("CostPrice");
}
if (empty($manufacturingcost)) {
$manufacturingcost = price2num($tmpproduct->pmp, 'MU');
$manufacturingcostsrc = $langs->trans("PMPValue");
}
}
print '<td class="right nowraponall">';
print '<td class="right nowraponall" title="'.dol_escape_htmltag($manufacturingcostsrc).'">';
if ($manufacturingcost) {
print price($manufacturingcost);
}
@ -1306,19 +1315,27 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
if ($permissiontoupdatecost) {
// Defined $manufacturingcost
$manufacturingcost = 0;
if ($object->mrptype == 0) { // If MO is a "Manufacture" type (and not "Disassemble"
$manufacturingcost = $bomcost;
$manufacturingcostsrc = '';
if ($object->mrptype == 0) { // If MO is a "Manufacture" type (and not "Disassemble")
$manufacturingcost = $bomcostupdated;
$manufacturingcostsrc = $langs->trans("CalculatedFromProductsToConsume");
if (empty($manufacturingcost)) {
$manufacturingcost = $bomcost;
$manufacturingcostsrc = $langs->trans("ValueFromBom");
}
if (empty($manufacturingcost)) {
$manufacturingcost = price2num($tmpproduct->cost_price, 'MU');
$manufacturingcostsrc = $langs->trans("CostPrice");
}
if (empty($manufacturingcost)) {
$manufacturingcost = price2num($tmpproduct->pmp, 'MU');
$manufacturingcostsrc = $langs->trans("PMPValue");
}
}
if ($tmpproduct->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
$preselected = (GETPOSTISSET('pricetoproduce-'.$line->id.'-'.$i) ? GETPOST('pricetoproduce-'.$line->id.'-'.$i) : ($manufacturingcost ? price($manufacturingcost) : ''));
print '<td class="right"><input type="text" class="width50 right" name="pricetoproduce-'.$line->id.'-'.$i.'" value="'.$preselected.'"></td>';
print '<td class="right"><input type="text" class="width75 right" name="pricetoproduce-'.$line->id.'-'.$i.'" value="'.$preselected.'"></td>';
} else {
print '<td><input type="hidden" class="width50 right" name="pricetoproduce-'.$line->id.'-'.$i.'" value="'.($manufacturingcost ? $manufacturingcost : '').'"></td>';
}