dolibarr  16.0.5
getSupplierPrices.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
3  * Copyright (C) 2015 Francis Appels <francis.appels@z-application.com>
4  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
25 if (!defined('NOTOKENRENEWAL')) {
26  define('NOTOKENRENEWAL', '1'); // Disables token renewal
27 }
28 if (!defined('NOREQUIREMENU')) {
29  define('NOREQUIREMENU', '1');
30 }
31 if (!defined('NOREQUIREAJAX')) {
32  define('NOREQUIREAJAX', '1');
33 }
34 if (!defined('NOREQUIRESOC')) {
35  define('NOREQUIRESOC', '1');
36 }
37 
38 require '../../main.inc.php';
39 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
40 
41 $idprod = GETPOST('idprod', 'int');
42 
43 $prices = array();
44 
45 // Load translation files required by the page
46 $langs->loadLangs(array("stocks", "margins", "products"));
47 
48 
49 /*
50  * View
51  */
52 
53 top_httphead();
54 
55 //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
56 
57 if ($idprod > 0) {
58  $producttmp = new ProductFournisseur($db);
59  $producttmp->fetch($idprod);
60 
61  $sorttouse = 's.nom, pfp.quantity, pfp.price';
62  if (GETPOST('bestpricefirst')) {
63  $sorttouse = 'pfp.unitprice, s.nom, pfp.quantity, pfp.price';
64  }
65 
66  $productSupplierArray = $producttmp->list_product_fournisseur_price($idprod, $sorttouse); // We list all price per supplier, and then firstly with the lower quantity. So we can choose first one with enough quantity into list.
67  if (is_array($productSupplierArray)) {
68  foreach ($productSupplierArray as $productSupplier) {
69  $price = $productSupplier->fourn_price * (1 - $productSupplier->fourn_remise_percent / 100);
70  $unitprice = $productSupplier->fourn_unitprice * (1 - $productSupplier->fourn_remise_percent / 100);
71 
72  $title = $productSupplier->fourn_name.' - '.$productSupplier->fourn_ref.' - ';
73 
74  if ($productSupplier->fourn_qty == 1) {
75  $title .= price($price, 0, $langs, 0, 0, -1, $conf->currency)."/";
76  }
77  $title .= $productSupplier->fourn_qty.' '.($productSupplier->fourn_qty == 1 ? $langs->trans("Unit") : $langs->trans("Units"));
78 
79  if ($productSupplier->fourn_qty > 1) {
80  $title .= " - ";
81  $title .= price($unitprice, 0, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit");
82  $price = $unitprice;
83  }
84 
85  $label = price($price, 0, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit");
86  if ($productSupplier->fourn_ref) {
87  $label .= ' ('.$productSupplier->fourn_ref.')';
88  }
89 
90  $prices[] = array("id" => $productSupplier->product_fourn_price_id, "price" => price2num($price, 0, '', 0), "label" => $label, "title" => $title); // For price field, we must use price2num(), for label or title, price()
91  }
92  }
93 
94  // After best supplier prices and before costprice
95  if (!empty($conf->stock->enabled)) {
96  // Add price for pmp
97  $price = $producttmp->pmp;
98  if (empty($price) && !empty($conf->global->PRODUCT_USE_SUB_COST_PRICES_IF_COST_PRICE_EMPTY)) {
99  // get pmp for subproducts if any
100  $producttmp->get_sousproduits_arbo();
101  $prods_arbo=$producttmp->get_arbo_each_prod();
102  if (!empty($prods_arbo)) {
103  $price = 0;
104  foreach ($prods_arbo as $child) {
105  $sousprod = new Product($db);
106  $sousprod->fetch($child['id']);
107  $price += $sousprod->pmp;
108  }
109  }
110  }
111 
112  $prices[] = array("id" => 'pmpprice', "price" => price2num($price), "label" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency)); // For price field, we must use price2num(), for label or title, price()
113  }
114 
115  // Add price for costprice (at end)
116  $price = $producttmp->cost_price;
117  if (empty($price) && ! empty($conf->global->PRODUCT_USE_SUB_COST_PRICES_IF_COST_PRICE_EMPTY)) {
118  // get costprice for subproducts if any
119  $producttmp->get_sousproduits_arbo();
120  $prods_arbo=$producttmp->get_arbo_each_prod();
121  if (!empty($prods_arbo)) {
122  $price = 0;
123  foreach ($prods_arbo as $child) {
124  $sousprod = new Product($db);
125  $sousprod->fetch($child['id']);
126  $price += $sousprod->cost_price;
127  }
128  }
129  }
130 
131  $prices[] = array("id" => 'costprice', "price" => price2num($price), "label" => $langs->trans("CostPrice").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency)); // For price field, we must use price2num(), for label or title, price()
132 }
133 
134 echo json_encode($prices);
ProductFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.product.class.php:41
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
top_httphead
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1407
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Product
Class to manage products or services.
Definition: product.class.php:46
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5541