From b15e00cae798c8792a7b5c7aef104bdfa94f665b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 6 Mar 2019 21:39:09 +0100 Subject: [PATCH] FIX Autodetect buy price for invoices autogenerated with templates. --- htdocs/compta/facture/card.php | 2 +- htdocs/compta/facture/class/facture.class.php | 31 +++++++++++++++++-- htdocs/product/class/product.class.php | 7 +++-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php index 1df96583ce3..e37d2c3d2a9 100644 --- a/htdocs/compta/facture/card.php +++ b/htdocs/compta/facture/card.php @@ -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 } } diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index dda877d0a1f..86ee5482261 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -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, diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 316a911d5a3..88b410da7fa 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -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) {