dolibarr 24.0.0-beta
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26if (!defined('NOTOKENRENEWAL')) {
27 define('NOTOKENRENEWAL', '1'); // Disables token renewal
28}
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', '1');
31}
32if (!defined('NOREQUIREAJAX')) {
33 define('NOREQUIREAJAX', '1');
34}
35if (!defined('NOREQUIRESOC')) {
36 define('NOREQUIRESOC', '1');
37}
38
39// Load Dolibarr environment
40require '../../main.inc.php';
41require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
50// init getSupplierPrices hook
51$hookmanager->initHooks(array('ajaxGetSupplierPrices'));
52
53$idprod = GETPOSTINT('idprod');
54
55$prices = array();
56
57// Load translation files required by the page
58$langs->loadLangs(array("stocks", "margins", "products"));
59
60if (!isModEnabled('product') && !isModEnabled('service')) {
61 accessforbidden("Not allowed");
62}
63if (!$user->hasRight('fournisseur', 'lire')) {
64 accessforbidden("Not allowed");
65}
66
68
69if ($idprod > 0) {
70 $object->fetch($idprod);
71
72 if ($object->type == $object::TYPE_PRODUCT) {
73 restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
74 }
75 if ($object->type == $object::TYPE_SERVICE) {
76 restrictedArea($user, 'service', $object->id, 'product&product', '', '');
77 }
78} else {
79 restrictedArea($user, 'produit|service', 0, 'product&product', '', '', 'rowid');
80}
81
82
83
84/*
85 * View
86 */
87
88top_httphead('application/json');
89
90//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
91
92if ($idprod > 0) {
93 $sorttouse = 's.nom, pfp.quantity, pfp.price';
94 if (GETPOST('bestpricefirst')) {
95 $sorttouse = 'pfp.unitprice, s.nom, pfp.quantity, pfp.price';
96 }
97
98 $productSupplierArray = $object->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.
99 if (is_array($productSupplierArray)) {
100 foreach ($productSupplierArray as $productSupplier) {
101 if (getDolGlobalInt("DISABLE_BAD_REPUTATION_PRODUCT_PRICE") && $productSupplier->supplier_reputation == "DONOTORDER") {
102 continue;
103 }
104
105 $price = $productSupplier->fourn_price * (1 - $productSupplier->fourn_remise_percent / 100);
106 $unitprice = $productSupplier->fourn_unitprice * (1 - $productSupplier->fourn_remise_percent / 100);
107
108 $title = $productSupplier->fourn_name.' - '.$productSupplier->ref_supplier.' - ';
109
110 if ($productSupplier->fourn_qty == 1) {
111 $title .= price($price, 0, $langs, 0, 0, -1, $conf->currency)."/";
112 }
113 $title .= $productSupplier->fourn_qty.' '.($productSupplier->fourn_qty == 1 ? $langs->trans("Unit") : $langs->trans("Units"));
114
115 if ($productSupplier->fourn_qty > 1) {
116 $title .= " - ";
117 $title .= price($unitprice, 0, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit");
118 $price = $unitprice;
119 }
120
121 $label = price($price, 0, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit");
122 if ($productSupplier->ref_supplier) {
123 $label .= ' ('.$productSupplier->ref_supplier.')';
124 }
125
126 $prices[] = array(
127 "id" => $productSupplier->product_fourn_price_id,
128 "price" => price2num($price, '', 0), // For price field, we must use price2num(), for label or title, price()
129 "label" => $label,
130 "title" => $title,
131
132 // New data to allow js UX to build more interesting stuff
133 "supplierData" => [
134 'price' => (float) $productSupplier->fourn_price,
135 'unitPrice' => (float) $productSupplier->fourn_price,
136 'discountPercent' => (float) $productSupplier->fourn_remise_percent,
137 'qty' => (float) $productSupplier->fourn_qty,
138 'finalUnitPrice' => (float) $unitprice,
139 'finalPrice' => (float) $price,
140 'socName' => $productSupplier->fourn_name,
141 'ref' => $productSupplier->ref_supplier,
142 'reputation' => $productSupplier->supplier_reputation,
143 'dateCreation' => $productSupplier->fourn_date_creation,
144 'deliveryTimeDays' => $productSupplier->delivery_time_days,
145 // Carry the product's default unit so the line form can preselect
146 // #units like the customer side already does for idprod (see
147 // issues #34610 for the customer side and #38636 for the
148 // supplier side).
149 "fk_unit" => $productSupplier->fk_unit
150 ],
151 );
152 }
153 }
154
155 // After best supplier prices and before costprice
156 if (isModEnabled('stock')) {
157 // Add price for pmp
158 $price = $object->pmp;
159 if (empty($price) && getDolGlobalString('PRODUCT_USE_SUB_COST_PRICES_IF_COST_PRICE_EMPTY')) {
160 // get pmp for subproducts if any
161 $object->get_sousproduits_arbo();
162 $prods_arbo = $object->get_arbo_each_prod();
163 if (!empty($prods_arbo)) {
164 $price = 0;
165 foreach ($prods_arbo as $child) {
166 $sousprod = new Product($db);
167 $sousprod->fetch($child['id']);
168 $price += $sousprod->pmp;
169 }
170 }
171 }
172
173 $prices[] = array("id" => 'pmpprice', "price" => price2num($price, 'MU'), "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()
174 }
175
176 // Add price for costprice (at end)
177 $price = $object->cost_price;
178 if (empty($price) && getDolGlobalString('PRODUCT_USE_SUB_COST_PRICES_IF_COST_PRICE_EMPTY')) {
179 // get costprice for subproducts if any
180 $object->get_sousproduits_arbo();
181 $prods_arbo = $object->get_arbo_each_prod();
182 if (!empty($prods_arbo)) {
183 $price = 0;
184 foreach ($prods_arbo as $child) {
185 $sousprod = new Product($db);
186 $sousprod->fetch($child['id']);
187 $price += $sousprod->cost_price;
188 }
189 }
190 }
191
192 $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()
193
194 $parameters = array(
195 'prices' => &$prices,
196 'idprod' => $idprod,
197 'bestpricefirst' => GETPOST('bestpricefirst')
198 );
199
200 $hookmanager->executeHooks('afterGetSupplierPrices', $parameters, $producttmp);
201}
202
203echo json_encode($prices);
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage predefined suppliers products.
Class to manage products or services.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.