FIX Autodetect buy price for invoices autogenerated with templates.

This commit is contained in:
Laurent Destailleur 2019-03-06 21:39:09 +01:00
parent 31c296f44b
commit b15e00cae7
3 changed files with 33 additions and 7 deletions

View File

@ -1129,7 +1129,7 @@ if (empty($reshook))
// Source facture
$object->fac_rec = GETPOST('fac_rec', 'int');
$id = $object->create($user); // This include recopy of links from recurring invoice and invoice lines
$id = $object->create($user); // This include recopy of links from recurring invoice and recurring invoice lines
}
}

View File

@ -723,7 +723,7 @@ class Facture extends CommonInvoice
}
/*
* Insert lines of predefined invoices
* Insert lines of template invoices
*/
if (! $error && $this->fac_rec > 0)
{
@ -749,6 +749,31 @@ class Facture extends CommonInvoice
$localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
$localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
$fk_product_fournisseur_price = empty($_facrec->lines[$i]->fk_product_fournisseur_price)?null:$_facrec->lines[$i]->fk_product_fournisseur_price;
$buyprice = empty($_facrec->lines[$i]->buyprice)?0:$_facrec->lines[$i]->buyprice;
// If buyprice not defined from template invoice, we try to guess the best value
if (! $buyprice && $_facrec->lines[$i]->fk_product > 0)
{
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
$producttmp = new ProductFournisseur($this->db);
$producttmp->fetch($_facrec->lines[$i]->fk_product);
// If margin module defined on costprice, we try the costprice
// If not defined or if module margin defined and pmp and stock module enabled, we try pmp price
// else we get the best supplier price
if ($conf->global->MARGIN_TYPE == 'costprice' && ! empty($producttmp->cost_price)) $buyprice = $producttmp->cost_price;
elseif (! empty($conf->stock->enabled) && ($conf->global->MARGIN_TYPE == 'costprice' || $conf->global->MARGIN_TYPE == 'pmp') && ! empty($producttmp->pmp)) $buyprice = $producttmp->pmp;
else {
if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0)
{
if ($producttmp->product_fourn_price_id > 0)
{
$buyprice = price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent/100) + $producttmp->fourn_remise, 'MU');
}
}
}
}
$result_insert = $this->addline(
$_facrec->lines[$i]->desc,
$_facrec->lines[$i]->subprice,
@ -771,8 +796,8 @@ class Facture extends CommonInvoice
'',
0,
0,
null,
0,
$fk_product_fournisseur_price,
$buyprice,
$_facrec->lines[$i]->label,
empty($_facrec->lines[$i]->array_options)?null:$_facrec->lines[$i]->array_options,
$_facrec->lines[$i]->situation_percent,

View File

@ -1528,8 +1528,8 @@ class Product extends CommonObject
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
* Read price used by a provider.
* We enter as input couple prodfournprice/qty or triplet qty/product_id/fourn_ref.
* Read price used by a provider.
* We enter as input couple prodfournprice/qty or triplet qty/product_id/fourn_ref.
* This also set some properties on product like ->buyprice, ->fourn_pu, ...
*
* @param int $prodfournprice Id du tarif = rowid table product_fournisseur_price
@ -1537,7 +1537,8 @@ class Product extends CommonObject
* @param int $product_id Filter on a particular product id
* @param string $fourn_ref Filter on a supplier price ref. 'none' to exclude ref in search.
* @param int $fk_soc If of supplier
* @return int <-1 if KO, -1 if qty not enough, 0 if OK but nothing found, id_product if OK and found. May also initialize some properties like (->ref_supplier, buyprice, fourn_pu, vatrate_supplier...)
* @return int <-1 if KO, -1 if qty not enough, 0 if OK but nothing found, id_product if OK and found. May also initialize some properties like (->ref_supplier, buyprice, fourn_pu, vatrate_supplier...)
* @see find_min_price_product_fournisseur()
*/
function get_buyprice($prodfournprice, $qty, $product_id=0, $fourn_ref='', $fk_soc=0)
{