From 946c017937c2e5b453ed27f83d48bce532874553 Mon Sep 17 00:00:00 2001 From: Francis Appels Date: Tue, 22 Jan 2019 15:59:47 +0100 Subject: [PATCH 1/4] Fix and clean price log. Key should price id iso product id. --- .../fourn/class/fournisseur.product.class.php | 73 +++++++++++++------ 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index 056070d580d..b0dd9713446 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -71,7 +71,7 @@ class ProductFournisseur extends Product public $fourn_price; // price for quantity public $fourn_remise_percent; // discount for quantity (percent) public $fourn_remise; // discount for quantity (amount) - public $product_fourn_id; // supplier id + public $product_fourn_id; // product id /** * @var int ID availability delay - visible/used if option FOURN_PRODUCT_AVAILABILITY is on (duplicate information compared to delivery delay) @@ -332,7 +332,12 @@ class ProductFournisseur extends Product $result=$this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_UPDATE',$user); if ($result < 0) $error++; // End call triggers - + if (! $error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) { + $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrenc, $multicurrency_code); + if ($result < 0) { + $error++; + } + } if (empty($error)) { $this->db->commit(); @@ -404,24 +409,8 @@ class ProductFournisseur extends Product if (! $error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) { // Add record into log table - $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price_log("; - $sql.= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,"; - $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)"; - $sql .= "values("; - $sql.= (isset($multicurrency_buyprice)?"'".$this->db->escape(price2num($multicurrency_buyprice))."'":'null').","; - $sql.= (isset($multicurrency_unitBuyPrice)?"'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'":'null').","; - $sql.= (isset($multicurrency_tx)?"'".$this->db->escape($multicurrency_tx)."'":'1').","; - $sql.= (isset($fk_multicurrency)?"'".$this->db->escape($fk_multicurrency)."'":'null').","; - $sql.= (isset($multicurrency_code)?"'".$this->db->escape($multicurrency_code)."'":'null').","; - $sql .= " '" . $this->db->idate($now) . "',"; - $sql .= " " . $this->product_fourn_id . ","; - $sql .= " " . $user->id . ","; - $sql .= " " . price2num($buyprice) . ","; - $sql .= " " . $qty; - $sql .= ")"; - - $resql = $this->db->query($sql); - if (! $resql) { + $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrenc, $multicurrency_code); + if ($result < 0) { $error++; } } @@ -887,5 +876,47 @@ class ProductFournisseur extends Product ); return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); - } + } + + /** + * Private function to log price history + * + * @param User $user Object user who adds/changes price + * @param date $datec date create + * @param float $buyprice price for qty + * @param float $qty qty for price + * @param float $multicurrency_buyprice Purchase price for the quantity min in currency + * @param float $multicurrency_unitBuyPrice Unit Purchase price in currency + * @param float $multicurrency_tx Rate currency + * @param int $fk_multicurrency key multi currency + * @param string $multicurrency_code Currency code + * + * @return int < 0 NOK > 0 OK + */ + private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code) + { + // Add record into log table + $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price_log("; + $sql.= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,"; + $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)"; + $sql .= "values("; + $sql.= (isset($multicurrency_buyprice)?"'".$this->db->escape(price2num($multicurrency_buyprice))."'":'null').","; + $sql.= (isset($multicurrency_unitBuyPrice)?"'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'":'null').","; + $sql.= (isset($multicurrency_tx)?"'".$this->db->escape($multicurrency_tx)."'":'1').","; + $sql.= (isset($fk_multicurrency)?"'".$this->db->escape($fk_multicurrency)."'":'null').","; + $sql.= (isset($multicurrency_code)?"'".$this->db->escape($multicurrency_code)."'":'null').","; + $sql .= "values('" . $this->db->idate($datec) . "',"; + $sql .= " " . $this->product_fourn_price_id . ","; + $sql .= " " . $user->id . ","; + $sql .= " " . price2num($buyprice) . ","; + $sql .= " " . $qty; + $sql .= ")"; + + $resql = $this->db->query($sql); + if (! $resql) { + return -1; + } else { + return 1; + } + } } From 7ad48064be6b13ca6e33d928bce530fd67f957af Mon Sep 17 00:00:00 2001 From: Francis Appels Date: Tue, 22 Jan 2019 16:18:25 +0100 Subject: [PATCH 2/4] Add code to show in fourn price tooltip. --- .../fourn/class/fournisseur.product.class.php | 146 ++++++++++++++++++ htdocs/product/fournisseurs.php | 9 +- 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index b0dd9713446..af64236db7f 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -877,6 +877,152 @@ class ProductFournisseur extends Product return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); } + + /** + * List supplier prices log of a supplier price + * + * @param int $fourn_id Id of supplier price + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Limit + * @param int $offset Offset + * @return array Array of Log prices + */ + function listProductFournisseurPriceLog($fourn_id, $sortfield='', $sortorder='', $limit=0, $offset=0) + { + global $conf; + + $sql = "SELECT"; + $sql.= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec, u.lastname,"; + $sql.= " pfpl.price, pfpl.quantity"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl"; + $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; + $sql.= ", ".MAIN_DB_PREFIX."user as u"; + $sql.= " WHERE pfp.entity IN (".getEntity('productprice').")"; + $sql.= " AND pfpl.fk_user = u.rowid"; + $sql.= " AND pfp.rowid = pfpl.fk_product_fournisseur"; + $sql.= " AND pfpl.fk_product_fournisseur = ".$fourn_id; + if (empty($sortfield)) $sql.= " ORDER BY pfpl.datec"; + else $sql.= $this->db->order($sortfield, $sortorder); + $sql.=$this->db->plimit($limit, $offset); + dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG); + + $resql = $this->db->query($sql); + if ($resql) + { + $retarray = array(); + + while ($record = $this->db->fetch_array($resql)) + { + $retarray[]=$record; + } + + $this->db->free($resql); + return $retarray; + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + + /** + * Display log price of product supplier price + * + * @param array $productFournLogList list of ProductFournisseur price log objects + * to display in table format. + * @return string HTML String with supplier price + */ + function displayPriceProductFournisseurLog($productFournLogList=array()) + { + global $langs; + + $out = ''; + $langs->load("suppliers"); + if (count($productFournLogList) > 0) { + $out .= ''; + $out .= ''; + $out .= ''; + //$out .= ''; + $out .= ''; + foreach ($productFournLogList as $productFournLog) { + $out.= ''; + $out.= ''; + //$out.= ''; + $out.= ''; + } + $out .= '
'.$langs->trans("Date").''.$langs->trans("Price").''.$langs->trans("QtyMin").''.$langs->trans("User").'
'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').''.price($productFournLog['price']).''.$productFournLog['quantity'].''.$productFournLog['lastname'].'
'; + } + return $out; + } + + + /** + * Return a link to the object card (with optionaly the picto) + * + * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) + * @param string $option On what the link point to ('nolink', ...) + * @param int $notooltip 1=Disable tooltip + * @param string $morecss Add more css on link + * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking + * @return string String with URL + */ + function getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1) + { + global $db, $conf, $langs; + + if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips + + $result = ''; + $companylink = ''; + + $label = '' . $langs->trans("SupplierRef") . ''; + $label.= '
'; + $label.= '' . $langs->trans('Ref') . ': ' . $this->fourn_ref; + + $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here + if (is_array($logPrices) && count($logPrices) > 0) { + $label.= '
'; + $label.= '' . $langs->trans("History") . ''; + $label.= $this->displayPriceProductFournisseurLog($logPrices); + } + + $url = dol_buildpath('/product/fournisseurs.php',1).'?id='.$this->id.'&action=add_price&socid='.$this->fourn_id.'&rowid='.$this->product_fourn_price_id; + + if ($option != 'nolink') + { + // Add param to save lastsearch_values or not + $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0); + if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1; + if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1'; + } + + $linkclose=''; + if (empty($notooltip)) + { + if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) + { + $label=$langs->trans("SupplierRef"); + $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"'; + } + $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"'; + $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"'; + } + else $linkclose = ($morecss?' class="'.$morecss.'"':''); + + $linkstart = ''; + $linkend=''; + + $result .= $linkstart; + if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1); + if ($withpicto != 2) $result.= $this->fourn_ref; + $result .= $linkend; + //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); + + return $result; + } /** * Private function to log price history diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index 9605137b838..8556160b767 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -796,7 +796,14 @@ SCRIPT; print ''.$productfourn->getSocNomUrl(1,'supplier').''; // Supplier ref - print ''.$productfourn->fourn_ref.''; + if ($user->rights->produit->creer || $user->rights->service->creer) // change required right here + { + print ''.$productfourn->getNomUrl().''; + } + else + { + print ''.$productfourn->fourn_ref.''; + } // Availability if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) From 1d08ccef074b92e5c51da365b894e46e6b2e30e5 Mon Sep 17 00:00:00 2001 From: Francis Appels Date: Tue, 22 Jan 2019 16:39:54 +0100 Subject: [PATCH 3/4] Fix sql error --- htdocs/fourn/class/fournisseur.product.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index af64236db7f..1c1b0a03173 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -1051,7 +1051,7 @@ class ProductFournisseur extends Product $sql.= (isset($multicurrency_tx)?"'".$this->db->escape($multicurrency_tx)."'":'1').","; $sql.= (isset($fk_multicurrency)?"'".$this->db->escape($fk_multicurrency)."'":'null').","; $sql.= (isset($multicurrency_code)?"'".$this->db->escape($multicurrency_code)."'":'null').","; - $sql .= "values('" . $this->db->idate($datec) . "',"; + $sql .= "'" . $this->db->idate($datec) . "',"; $sql .= " " . $this->product_fourn_price_id . ","; $sql .= " " . $user->id . ","; $sql .= " " . price2num($buyprice) . ","; From 4a5d260db56c938957a2949360a76a8da43ee0e8 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 26 Jan 2019 14:07:37 +0100 Subject: [PATCH 4/4] Update fournisseur.product.class.php --- htdocs/fourn/class/fournisseur.product.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index 1c1b0a03173..d721a4a9254 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -71,7 +71,7 @@ class ProductFournisseur extends Product public $fourn_price; // price for quantity public $fourn_remise_percent; // discount for quantity (percent) public $fourn_remise; // discount for quantity (amount) - public $product_fourn_id; // product id + public $product_fourn_id; // product-supplier id /** * @var int ID availability delay - visible/used if option FOURN_PRODUCT_AVAILABILITY is on (duplicate information compared to delivery delay)