dolibarr 25.0.0-alpha
price.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
5 * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
7 * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2014-2018 Juanjo Menent <jmenent@2byte.es>
9 * Copyright (C) 2014-2019 Philippe Grand <philippe.grand@atoo-net.com>
10 * Copyright (C) 2014 Ion agorria <ion@agorria.com>
11 * Copyright (C) 2015-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
12 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
13 * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
14 * Copyright (C) 2018-2026 Frédéric France <frederic.france@free.fr>
15 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
16 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
17 * Copyright (C) 2025 Mélina Joum <melina.joum@altairis.fr>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 */
32
39// Load Dolibarr environment
40require '../main.inc.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
42require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
43require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
44require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
45require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
46require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
47
57$prodcustprice = null;
58if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
59 require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
60
61 $prodcustprice = new ProductCustomerPrice($db);
62}
63
64// Load translation files required by the page
65$langs->loadLangs(array('products', 'bills', 'companies', 'other'));
66
67$error = 0;
68$errors = array();
69$rowid = 0;
70
71$id = GETPOSTINT('id');
72$ref = GETPOST('ref', 'alpha');
73$action = GETPOST('action', 'aZ09');
74$cancel = GETPOST('cancel', 'alpha');
75$eid = GETPOSTINT('eid');
76
77$search_soc = GETPOST('search_soc');
78
79// Security check
80$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
81$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
82if ($user->socid) {
83 $socid = $user->socid;
84}
85
86$object = new Product($db);
87if ($id > 0 || !empty($ref)) {
88 $object->fetch($id, $ref);
89}
90
91$extrafields = new ExtraFields($db);
92
93// Clean param
94if ((getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) && !getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT')) {
95 $conf->global->PRODUIT_MULTIPRICES_LIMIT = 5;
96}
97
98// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
99$hookmanager->initHooks(array('productpricecard', 'globalcard'));
100
101if ($object->id > 0) {
102 if ($object->type == $object::TYPE_PRODUCT) {
103 restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
104 }
105 if ($object->type == $object::TYPE_SERVICE) {
106 restrictedArea($user, 'service', $object->id, 'product&product', '', '');
107 }
108} else {
109 restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
110}
111
112$maxpricesupplier = 0;
113
114$permissiontoadd = 0;
115if ($object->id > 0) {
116 if ($object->type == $object::TYPE_PRODUCT) {
117 $permissiontoadd = $user->hasRight('produit', 'creer');
118 }
119 if ($object->type == $object::TYPE_SERVICE) {
120 $permissiontoadd = $user->hasRight('service', 'creer');
121 }
122} else {
123 $permissiontoadd = ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'));
124}
125
126
127/*
128 * Actions
129 */
130
131if ($cancel) {
132 $action = '';
133}
134
135$parameters = array('id' => $id, 'ref' => $ref);
136$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
137if ($reshook < 0) {
138 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
139}
140
141if (empty($reshook)) {
142 if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
143 $search_soc = '';
144 }
145
146 if ($action == 'setlabelsellingprice' && $user->admin) {
147 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
148 $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.GETPOST('pricelevel');
149 dolibarr_set_const($db, $keyforlabel, GETPOST('labelsellingprice', 'alpha'), 'chaine', 0, '', $conf->entity);
150 $action = '';
151 }
152
153 if (($action == 'update_vat') && !$cancel && $permissiontoadd) {
154 $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)'
155
156 $price_label = GETPOST('price_label', 'alpha');
157
158 // We must define tva_tx, npr and local taxes
159 $tva_tx = $tva_tx_txt;
160 $reg = array();
161 $vatratecode = '';
162 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
163 $vatratecode = $reg[1];
164 $tva_tx = preg_replace('/\s*\‍(.*\‍)/', '', $tva_tx_txt); // Remove code into vatrate.
165 }
166
167 $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
168 $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
169 $localtax1 = 0;
170 $localtax2 = 0;
171 $localtax1_type = '0';
172 $localtax2_type = '0';
173 // If value contains the unique code of vat line (new recommended method), we use it to find npr and local taxes
174
175 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
176 // We look into database using code (we can't use get_localtax() because it depends on buyer that is not known). Same in create product.
177 $vatratecode = $reg[1];
178 // Get record from code
179 $sql = "SELECT t.rowid, t.type_vat, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
180 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
181 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
182 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
183 $sql .= " AND t.code = '".$db->escape($vatratecode)."'";
184 $sql .= " AND t.type_vat IN (0, 1)"; // Use only VAT rates type all or i.e. the sales type VAT rates.
185 $sql .= " AND t.entity IN (".getEntity('c_tva').")";
186 $resql = $db->query($sql);
187 if ($resql) {
188 $obj = $db->fetch_object($resql);
189 if ($obj) {
190 $npr = $obj->recuperableonly;
191 $localtax1 = $obj->localtax1;
192 $localtax2 = $obj->localtax2;
193 $localtax1_type = $obj->localtax1_type;
194 $localtax2_type = $obj->localtax2_type;
195 }
196 }
197 } else {
198 // Get record with empty code
199 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
200 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
201 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
202 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
203 $sql .= " AND t.code = ''";
204 $sql .= " AND t.entity IN (".getEntity('c_tva').")";
205 $resql = $db->query($sql);
206 if ($resql) {
207 $obj = $db->fetch_object($resql);
208 if ($obj) {
209 $npr = $obj->recuperableonly;
210 $localtax1 = $obj->localtax1;
211 $localtax2 = $obj->localtax2;
212 $localtax1_type = $obj->localtax1_type;
213 $localtax2_type = $obj->localtax2_type;
214 }
215 }
216 }
217
218 $object->default_vat_code = $vatratecode;
219 $object->tva_tx = $tva_tx;
220 $object->tva_npr = $npr;
221 $object->localtax1_tx = $localtax1;
222 $object->localtax2_tx = $localtax2;
223 $object->localtax1_type = $localtax1_type;
224 $object->localtax2_type = $localtax2_type;
225 $object->price_label = $price_label;
226
227 $db->begin();
228
229 $resql = $object->update($object->id, $user);
230 if ($resql <= 0) {
231 $error++;
232 setEventMessages($object->error, $object->errors, 'errors');
233 }
234
235 if (!$error) {
236 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
237 $produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT');
238 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
239 // Force the update of the price of the product using the new VAT
240 if ($object->multiprices_base_type[$i] == 'HT') {
241 $oldprice = $object->multiprices[$i];
242 $oldminprice = $object->multiprices_min[$i];
243 } else {
244 $oldprice = $object->multiprices_ttc[$i];
245 $oldminprice = $object->multiprices_min_ttc[$i];
246 }
247 $oldpricebasetype = $object->multiprices_base_type[$i];
248 $oldnpr = $object->multiprices_recuperableonly[$i];
249
250 //$localtaxarray=array('0'=>$localtax1_type,'1'=>$localtax1,'2'=>$localtax2_type,'3'=>$localtax2);
251 $localtaxarray = array(); // We do not store localtaxes into product, we will use instead the "vat code" to retrieve them.
252 $level = $i;
253 $ret = $object->updatePrice($oldprice, $oldpricebasetype, $user, (float) $tva_tx, $oldminprice, $level, $oldnpr, 0, 0, $localtaxarray, $vatratecode, $price_label);
254
255 if ($ret < 0) {
256 $error++;
257 setEventMessages($object->error, $object->errors, 'errors');
258 }
259 }
260 } else {
261 // Force the update of the price of the product using the new VAT
262 if ($object->price_base_type == 'HT') {
263 $oldprice = $object->price;
264 $oldminprice = $object->price_min;
265 } else {
266 $oldprice = $object->price_ttc;
267 $oldminprice = $object->price_min_ttc;
268 }
269 $oldpricebasetype = $object->price_base_type;
270 $oldnpr = $object->tva_npr;
271
272 //$localtaxarray=array('0'=>$localtax1_type,'1'=>$localtax1,'2'=>$localtax2_type,'3'=>$localtax2);
273 $localtaxarray = array(); // We do not store localtaxes into product, we will use instead the "vat code" to retrieve them when required.
274 $level = 0;
275 $ret = $object->updatePrice($oldprice, $oldpricebasetype, $user, (float) $tva_tx, $oldminprice, $level, $oldnpr, 0, 0, $localtaxarray, $vatratecode, $price_label);
276
277 if ($ret < 0) {
278 $error++;
279 setEventMessages($object->error, $object->errors, 'errors');
280 }
281 }
282 }
283
284 if (!$error) {
285 $db->commit();
286 } else {
287 $db->rollback();
288 }
289
290 $action = '';
291 }
292
293 $maxpricesupplier = 0;
294
295 if (($action == 'update_price' || $action == 'update_level_price') && !$cancel && $permissiontoadd) {
296 $error = 0;
297 $pricestoupdate = array();
298 $object->oldcopy = dol_clone($object, 1); // when calling ->update later we need to call method on ->oldcopy so we clone using param 1
299
300 $psq = GETPOSTINT('psqflag');
301
302 $maxpricesupplier = $object->min_recommended_price();
303
304 // Packaging
305 $packaging = getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING') ? price2num(GETPOST('packaging', 'alpha'), 'MS') : null;
306
307 if (isModEnabled('dynamicprices')) {
308 $object->fk_price_expression = empty($eid) ? 0 : $eid; //0 discards expression
309
310 if ($object->fk_price_expression != 0) {
311 //Check the expression validity by parsing it
312 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
313 $priceparser = new PriceParser($db);
314
315 if ($priceparser->parseProduct($object) < 0) {
316 $error++;
317 setEventMessages($priceparser->translatedError(), null, 'errors');
318 }
319 }
320 }
321
322 // Multiprices
323 if (!$error && (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')
324 || ($action == 'update_level_price' && getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')))) { // Test on permission already done
325 $newprice = GETPOST('price', 'array');
326 $newprice_min = GETPOST('price_min', 'array');
327 $newpricebase = GETPOST('multiprices_base_type', 'array');
328 $newvattx = GETPOST('tva_tx', 'array');
329 $newvatnpr = GETPOST('tva_npr', 'array');
330 $newlocaltax1_tx = GETPOST('localtax1_tx', 'array');
331 $newlocaltax1_type = GETPOST('localtax1_type', 'array');
332 $newlocaltax2_tx = GETPOST('localtax2_tx', 'array');
333 $newlocaltax2_type = GETPOST('localtax2_type', 'array');
334
335 //Shall we generate prices using price rules?
336 $object->price_autogen = (int) (GETPOST('usePriceRules') == 'on');
337
338 $produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT');
339 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
340 if (!isset($newprice[$i])) {
341 continue;
342 }
343
344 $tva_tx_txt = $newvattx[$i];
345
346 $tva_tx = $tva_tx_txt;
347 $vatratecode = '';
348 $reg = array();
349 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
350 $vat_src_code = $reg[1];
351 $tva_tx = preg_replace('/\s*\‍(.*\‍)/', '', $tva_tx_txt); // Remove code into vatrate.
352 }
353 $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
354
355 $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
356 $localtax1 = $newlocaltax1_tx[$i];
357 $localtax1_type = $newlocaltax1_type[$i];
358 $localtax2 = $newlocaltax2_tx[$i];
359 $localtax2_type = $newlocaltax2_type[$i];
360 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
361 // We look into database using code
362 $vatratecode = $reg[1];
363 // Get record from code
364 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
365 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
366 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
367 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
368 $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
369 $sql .= " AND t.entity IN (".getEntity('c_tva').")";
370 $resql = $db->query($sql);
371 if ($resql) {
372 $obj = $db->fetch_object($resql);
373 if ($obj) {
374 $npr = $obj->recuperableonly;
375 $localtax1 = $obj->localtax1;
376 $localtax2 = $obj->localtax2;
377 $localtax1_type = $obj->localtax1_type;
378 $localtax2_type = $obj->localtax2_type;
379 }
380
381 // If spain, we don't use the localtax found into tax record in database with same code, but using the get_localtax rule.
382 if (in_array($mysoc->country_code, array('ES'))) {
383 $localtax1 = get_localtax($tva_tx, 1);
384 $localtax2 = get_localtax($tva_tx, 2);
385 }
386 }
387 } else {
388 // Get record with empty code
389 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
390 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
391 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
392 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
393 $sql .= " AND t.code = ''";
394 $resql = $db->query($sql);
395 if ($resql) {
396 $obj = $db->fetch_object($resql);
397 if ($obj) {
398 $npr = $obj->recuperableonly;
399 $localtax1 = $obj->localtax1;
400 $localtax2 = $obj->localtax2;
401 $localtax1_type = $obj->localtax1_type;
402 $localtax2_type = $obj->localtax2_type;
403 }
404 }
405 }
406
407 $pricestoupdate[$i] = array(
408 'price_label' => '', // no change so empty value
409 'price' => price2num($newprice[$i], '', 2),
410 'price_min' => price2num($newprice_min[$i], '', 2),
411 'price_base_type' => $newpricebase[$i],
412 'default_vat_code' => $vatratecode,
413 'vat_tx' => $tva_tx, // default_vat_code should be used in priority in a future
414 'npr' => $npr, // default_vat_code should be used in priority in a future
415 'localtaxes_array' => array('0' => $localtax1_type, '1' => $localtax1, '2' => $localtax2_type, '3' => $localtax2) // default_vat_code should be used in priority in a future
416 );
417
418 //If autogeneration is enabled, then we only set the first level
419 if ($object->price_autogen) {
420 break;
421 }
422 }
423 } elseif (!$error) {
424 $newprice = price2num(GETPOST('price', 'alpha'), '', 2);
425 $newprice_min = price2num(GETPOST('price_min', 'alpha'), '', 2);
426 $newpricebase = GETPOST('price_base_type', 'alpha');
427 $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)'
428 $price_label = GETPOST('price_label', 'alpha');
429
430 $tva_tx = $tva_tx_txt;
431 $vatratecode = '';
432 $reg = array();
433 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
434 $vat_src_code = $reg[1];
435 $tva_tx = preg_replace('/\s*\‍(.*\‍)/', '', $tva_tx_txt); // Remove code into vatrate.
436 }
437 $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
438
439 $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
440 $localtax1 = 0;
441 $localtax2 = 0;
442 $localtax1_type = '0';
443 $localtax2_type = '0';
444 // If value contains the unique code of vat line (new recommended method), we use it to find npr and local taxes
445 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
446 // We look into database using code
447 $vatratecode = $reg[1];
448 // Get record from code
449 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
450 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
451 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
452 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
453 $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
454 $sql .= " AND t.entity IN (".getEntity('c_tva').")";
455 $resql = $db->query($sql);
456 if ($resql) {
457 $obj = $db->fetch_object($resql);
458 if ($obj) {
459 $npr = $obj->recuperableonly;
460 $localtax1 = $obj->localtax1;
461 $localtax2 = $obj->localtax2;
462 $localtax1_type = $obj->localtax1_type;
463 $localtax2_type = $obj->localtax2_type;
464 }
465
466 // If spain, we don't use the localtax found into tax record in database with same code, but using the get_localtax rule.
467 if (in_array($mysoc->country_code, array('ES'))) {
468 $localtax1 = get_localtax($tva_tx, 1);
469 $localtax2 = get_localtax($tva_tx, 2);
470 }
471 }
472 } else {
473 // Get record with empty code
474 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
475 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
476 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
477 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
478 $sql .= " AND t.code = ''";
479 $resql = $db->query($sql);
480 if ($resql) {
481 $obj = $db->fetch_object($resql);
482 if ($obj) {
483 $npr = $obj->recuperableonly;
484 $localtax1 = $obj->localtax1;
485 $localtax2 = $obj->localtax2;
486 $localtax1_type = $obj->localtax1_type;
487 $localtax2_type = $obj->localtax2_type;
488 }
489 }
490 }
491
492 $pricestoupdate[0] = array(
493 'price' => $newprice,
494 'price_label' => $price_label,
495 'price_min' => $newprice_min,
496 'price_base_type' => $newpricebase,
497 'default_vat_code' => $vatratecode,
498 'vat_tx' => $tva_tx, // default_vat_code should be used in priority in a future
499 'npr' => $npr, // default_vat_code should be used in priority in a future
500 'localtaxes_array' => array('0' => $localtax1_type, '1' => $localtax1, '2' => $localtax2_type, '3' => $localtax2) // default_vat_code should be used in priority in a future
501 );
502 }
503
504 if (!$error) {
505 $db->begin();
506
507 // Packaging
508 if (getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING')) {
509 $object->packaging = (float) $packaging;
510 }
511
512 foreach ($pricestoupdate as $key => $val) {
513 $newprice = $val['price'];
514
515 if ($val['price'] < $val['price_min'] && !empty($object->fk_price_expression)) {
516 $newprice = $val['price_min']; //Set price same as min, the user will not see the
517 }
518
519 $newprice = price2num($newprice, 'MU');
520 $newprice_min = price2num($val['price_min'], 'MU');
521 $newvattx = price2num($val['vat_tx']);
522
523 if (getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE') && $newprice_min < $maxpricesupplier) {
524 setEventMessages($langs->trans("MinimumPriceLimit", price($maxpricesupplier, 0, '', 1, - 1, - 1, 'auto')), null, 'errors');
525 $error++;
526 break;
527 }
528 // If price has changed, we update it
529 if (!array_key_exists($key, $object->multiprices) || $object->multiprices[$key] != $newprice || $object->multiprices_min[$key] != $newprice_min || $object->multiprices_base_type[$key] != $val['price_base_type'] || $object->multiprices_tva_tx[$key] != $newvattx) {
530 $res = $object->updatePrice((float) $newprice, $val['price_base_type'], $user, (float) $val['vat_tx'], (float) $newprice_min, $key, $val['npr'], $psq, 0, $val['localtaxes_array'], $val['default_vat_code'], $val['price_label']);
531 } else {
532 $res = 0;
533 }
534
535 if ($res < 0) {
536 $error++;
537 setEventMessages($object->error, $object->errors, 'errors');
538 break;
539 }
540
541 // Update level price extrafields
542 $price_extralabels = $extrafields->fetch_name_optionals_label("product_price");
543 if ((getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) && !empty($price_extralabels)) {
544 $sql = "SELECT rowid";
545 $sql .= " FROM ".$object->db->prefix()."product_price";
546 $sql .= " WHERE entity IN (".getEntity('productprice').")";
547 $sql .= " AND price_level=".((int) $key); // $i
548 $sql .= " AND fk_product = ".((int) $object->id);
549 $sql .= " ORDER BY date_price DESC, rowid DESC";
550 $sql .= " LIMIT 1";
551 $resql = $object->db->query($sql);
552 if ($resql) {
553 $lineid = $object->db->fetch_object($resql);
554 $db->free($resql);
555 }
556 if (!empty($lineid->rowid)) {
557 require_once DOL_DOCUMENT_ROOT . '/core/class/genericobject.class.php';
558 $genericObject = new GenericObject($db);
559 // We need to force table to update product_price extrafields
560 $genericObject->id = $lineid->rowid;
561 $genericObject->table_element = 'product_price';
562 foreach ($price_extralabels as $code => $label) {
563 $extrafield_values = $extrafields->getOptionalsFromPost('product_price', (string) $key);
564 $genericObject->array_options['options_'.$code] = $extrafield_values['options_'.$code];
565 }
566 $result = $genericObject->insertExtraFields();
567
568 if ($result < 0) {
569 setEventMessages($genericObject->error, $genericObject->errors, 'errors');
570 $error++;
571 }
572 }
573 }
574 }
575 }
576
577 if (!$error && $object->update($object->id, $user) < 0) {
578 $error++;
579 setEventMessages($object->error, $object->errors, 'errors');
580 }
581
582 if (empty($error)) {
583 $action = '';
584 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
585 $db->commit();
586 } else {
587 $action = 'edit_price';
588 $db->rollback();
589 }
590 }
591
592
593 if ($action == 'delete' && $user->hasRight('produit', 'supprimer')) {
594 $result = $object->log_price_delete($user, GETPOSTINT('lineid'));
595 if ($result < 0) {
596 setEventMessages($object->error, $object->errors, 'errors');
597 }
598 }
599
600 // Set Price by quantity
601 if ($action == 'activate_price_by_qty' && $permissiontoadd) {
602 // Activating product price by quantity add a new price line with price_by_qty set to 1
603 $level = GETPOSTINT('level');
604 $basePriceType = $object->price_base_type;
605 $basePrice = ($basePriceType == 'HT') ? $object->price : $object->price_ttc;
606 $basePriceMin = ($basePriceType == 'HT') ? $object->price_min : $object->price_min_ttc;
607 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') && $level > 0 && isset($object->multiprices[$level])) {
608 // With a price per level, the price to keep is the price of the level, not the default price of the product
609 $basePriceType = (empty($object->multiprices_base_type[$level]) ? 'HT' : $object->multiprices_base_type[$level]);
610 $basePrice = ($basePriceType == 'HT') ? $object->multiprices[$level] : $object->multiprices_ttc[$level];
611 $basePriceMin = ($basePriceType == 'HT') ? $object->multiprices_min[$level] : $object->multiprices_min_ttc[$level];
612 }
613 $ret = $object->updatePrice($basePrice, $basePriceType, $user, $object->tva_tx, $basePriceMin, $level, $object->tva_npr, 1, 0, array(), $object->default_vat_code);
614
615 if ($ret < 0) {
616 setEventMessages($object->error, $object->errors, 'errors');
617 }
618 }
619 // Unset Price by quantity
620 if ($action == 'disable_price_by_qty' && $permissiontoadd) {
621 // Disabling product price by quantity add a new price line with price_by_qty set to 0
622 $level = GETPOSTINT('level');
623 $basePriceType = $object->price_base_type;
624 $basePrice = ($basePriceType == 'HT') ? $object->price : $object->price_ttc;
625 $basePriceMin = ($basePriceType == 'HT') ? $object->price_min : $object->price_min_ttc;
626 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') && $level > 0 && isset($object->multiprices[$level])) {
627 // With a price per level, the price to keep is the price of the level, not the default price of the product
628 $basePriceType = (empty($object->multiprices_base_type[$level]) ? 'HT' : $object->multiprices_base_type[$level]);
629 $basePrice = ($basePriceType == 'HT') ? $object->multiprices[$level] : $object->multiprices_ttc[$level];
630 $basePriceMin = ($basePriceType == 'HT') ? $object->multiprices_min[$level] : $object->multiprices_min_ttc[$level];
631 }
632 $ret = $object->updatePrice($basePrice, $basePriceType, $user, $object->tva_tx, $basePriceMin, $level, $object->tva_npr, -1, 0, array(), $object->default_vat_code);
633
634 if ($ret < 0) {
635 setEventMessages($object->error, $object->errors, 'errors');
636 }
637 }
638
639 if ($action == 'edit_price_by_qty') { // Test on permission not required
640 $rowid = GETPOSTINT('rowid');
641 }
642
643 // Add or update price by quantity
644 if ($action == 'update_price_by_qty' && $permissiontoadd) {
645 // Get the variables
646 $rowid = GETPOSTINT('rowid');
647 $priceid = GETPOSTINT('priceid');
648 $newprice = price2num(GETPOST("price"), 'MU', 2);
649 // $newminprice=price2num(GETPOST("price_min"),'MU'); // TODO : Add min price management
650 $quantity = price2num(GETPOST('quantity'), 'MS', 2);
651 $remise_percent = price2num(GETPOST('remise_percent'), '', 2);
652 $remise = 0; // TODO : allow discount by amount when available on documents
653
654 if (empty($quantity)) {
655 $error++;
656 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Qty")), null, 'errors');
657 }
658 if (empty($newprice)) {
659 $error++;
660 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Price")), null, 'errors');
661 }
662 if (!$error && !($rowid > 0)) {
663 // A new line can only be attached to a price line of the product being edited
664 $sql = "SELECT pp.rowid FROM ".MAIN_DB_PREFIX."product_price as pp";
665 $sql .= " WHERE pp.rowid = ".((int) $priceid)." AND pp.fk_product = ".((int) $object->id);
666 $resql = $db->query($sql);
667 if (!$resql || !$db->num_rows($resql)) {
668 $error++;
669 setEventMessages($langs->trans("ErrorRecordNotFound"), null, 'errors');
670 }
671 }
672 if (!$error) {
673 // Calcul du prix HT et du prix unitaire
674 if ($object->price_base_type == 'TTC') {
675 $price = (float) price2num($newprice) / (1 + ($object->tva_tx / 100));
676 }
677
678 $price = price2num($newprice, 'MU');
679 $unitPrice = price2num((float) $price / (float) $quantity, 'MU');
680
681 // Update or add
682 if ($rowid > 0) {
683 $sql = "UPDATE ".MAIN_DB_PREFIX."product_price_by_qty SET";
684 $sql .= " price=".((float) $price).",";
685 $sql .= " unitprice=".((float) $unitPrice).",";
686 $sql .= " quantity=".((float) $quantity).",";
687 $sql .= " remise_percent=".((float) $remise_percent).",";
688 $sql .= " remise=".((float) $remise);
689 $sql .= " WHERE rowid = ".((int) $rowid);
690 $sql .= " AND fk_product_price IN (SELECT pp.rowid FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = ".((int) $object->id).")";
691
692 $result = $db->query($sql);
693 if (!$result) {
694 dol_print_error($db);
695 }
696 } else {
697 $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price_by_qty (fk_product_price,price,unitprice,quantity,remise_percent,remise) values (";
698 $sql .= ((int) $priceid).','.((float) $price).','.((float) $unitPrice).','.((float) $quantity).','.((float) $remise_percent).','.((float) $remise).')';
699
700 $result = $db->query($sql);
701 if (!$result) {
702 if ($db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
703 setEventMessages($langs->trans("DuplicateRecord"), null, 'errors');
704 } else {
705 dol_print_error($db);
706 }
707 }
708 }
709 }
710 }
711
712 if ($action == 'delete_price_by_qty' && $permissiontoadd) {
713 $rowid = GETPOSTINT('rowid');
714 if (!empty($rowid)) {
715 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price_by_qty";
716 $sql .= " WHERE rowid = ".((int) $rowid);
717 $sql .= " AND fk_product_price IN (SELECT pp.rowid FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = ".((int) $object->id).")";
718
719 $result = $db->query($sql);
720 } else {
721 setEventMessages(('delete_price_by_qty'.$langs->transnoentities('MissingIds')), null, 'errors');
722 }
723 }
724
725 if ($action == 'delete_all_price_by_qty' && $permissiontoadd) {
726 $priceid = GETPOSTINT('priceid');
727 if (!empty($priceid)) {
728 $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price_by_qty";
729 $sql .= " WHERE fk_product_price = ".((int) $priceid);
730 $sql .= " AND fk_product_price IN (SELECT pp.rowid FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = ".((int) $object->id).")";
731
732 $result = $db->query($sql);
733 } else {
734 setEventMessages(('delete_price_by_qty'.$langs->transnoentities('MissingIds')), null, 'errors');
735 }
736 }
737
743 if ($action == 'add_customer_price_confirm' && !$cancel && $prodcustprice !== null && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
744 $maxpricesupplier = $object->min_recommended_price();
745
746 $update_child_soc = GETPOSTINT('updatechildprice');
747
748 // add price by customer
749 $prodcustprice->fk_soc = GETPOSTINT('socid');
750 $prodcustprice->ref_customer = GETPOST('ref_customer', 'alpha');
751 $prodcustprice->fk_product = $object->id;
752 $prodcustprice->price = price2num(GETPOST("price"), 'MU');
753 $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU');
754 $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha');
755 $prodcustprice->price_label = GETPOST("price_label", 'alpha');
756 $prodcustprice->discount_percent = price2num(GETPOST("discount_percent"));
757 $prodcustprice->date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server;
758 $prodcustprice->date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server
759
760 $extralabels = $extrafields->fetch_name_optionals_label("product_customer_price");
761 $extrafield_values = $extrafields->getOptionalsFromPost("product_customer_price");
762
763 $tva_tx_txt = GETPOST("tva_tx", 'alpha');
764
765 $tva_tx = $tva_tx_txt;
766 $vatratecode = '';
767 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
768 $vat_src_code = $reg[1];
769 $tva_tx = preg_replace('/\s*\‍(.*\‍)/', '', $tva_tx_txt); // Remove code into vatrate.
770 }
771 $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
772
773 $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
774 $localtax1 = 0;
775 $localtax2 = 0;
776 $localtax1_type = '0';
777 $localtax2_type = '0';
778 // If value contains the unique code of vat line (new recommended method), we use it to find npr and local taxes
779 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
780 // We look into database using code
781 $vatratecode = $reg[1];
782 // Get record from code
783 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
784 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
785 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
786 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
787 $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
788 $sql .= " AND t.entity IN (".getEntity('c_tva').")";
789 $resql = $db->query($sql);
790 if ($resql) {
791 $obj = $db->fetch_object($resql);
792 if ($obj) {
793 $npr = $obj->recuperableonly;
794 $localtax1 = $obj->localtax1;
795 $localtax2 = $obj->localtax2;
796 $localtax1_type = $obj->localtax1_type;
797 $localtax2_type = $obj->localtax2_type;
798 }
799
800 // If spain, we don't use the localtax found into tax record in database with same code, but using the get_localtax rule.
801 if (in_array($mysoc->country_code, array('ES'))) {
802 $localtax1 = get_localtax($tva_tx, 1);
803 $localtax2 = get_localtax($tva_tx, 2);
804 }
805 }
806 } else {
807 // Get record with empty code
808 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
809 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
810 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
811 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
812 $sql .= " AND t.code = ''";
813 $resql = $db->query($sql);
814 if ($resql) {
815 $obj = $db->fetch_object($resql);
816 if ($obj) {
817 $npr = $obj->recuperableonly;
818 $localtax1 = $obj->localtax1;
819 $localtax2 = $obj->localtax2;
820 $localtax1_type = $obj->localtax1_type;
821 $localtax2_type = $obj->localtax2_type;
822 }
823 }
824 }
825
826 $prodcustprice->default_vat_code = $vatratecode;
827 $prodcustprice->tva_tx = $tva_tx;
828 $prodcustprice->recuperableonly = $npr;
829 $prodcustprice->localtax1_tx = $localtax1;
830 $prodcustprice->localtax2_tx = $localtax2;
831 $prodcustprice->localtax1_type = $localtax1_type;
832 $prodcustprice->localtax2_type = $localtax2_type;
833
834 if (!($prodcustprice->fk_soc > 0)) {
835 $langs->load("errors");
836 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ThirdParty")), null, 'errors');
837 $error++;
838 $action = 'add_customer_price';
839 }
840 if (getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE') && $prodcustprice->price_min < (float) $maxpricesupplier) {
841 $langs->load("errors");
842 setEventMessages($langs->trans("MinimumPriceLimit", price((float) $maxpricesupplier, 0, '', 1, -1, -1, 'auto')), null, 'errors');
843 $error++;
844 $action = 'add_customer_price';
845 }
846
847 if (!$error) {
848 // Insert price
849 $result = $prodcustprice->create($user, 0, $update_child_soc);
850 if ($result > 0) {
851 if (!empty($extrafield_values) && is_array($extrafield_values)) {
852 $productcustomerprice = new ProductCustomerPrice($db);
853 $res = $productcustomerprice->fetch($prodcustprice->id);
854 if ($res > 0) {
855 foreach ($extrafield_values as $key => $value) {
856 $productcustomerprice->array_options[$key] = $value;
857 }
858 $result2 = $productcustomerprice->insertExtraFields();
859 if ($result2 < 0) {
860 $prodcustprice->error = $productcustomerprice->error;
861 $prodcustprice->errors = $productcustomerprice->errors;
862 $error++;
863 }
864 }
865 }
866 }
867
868 if ($result < 0) {
869 setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
870 $action = 'add_customer_price';
871 } else {
872 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
873 $action = '';
874 }
875 }
876 }
877
878 // A customer price line can only be removed or updated from the page of its own product
879 if (in_array($action, ['confirm_remove_customer_price', 'update_customer_price_confirm']) && $prodcustprice !== null) {
880 $prodcustprice->fetch(GETPOSTINT('lineid'));
881 if ((int) $prodcustprice->fk_product !== (int) $object->id) {
882 setEventMessages($langs->trans("ErrorRecordNotFound"), null, 'errors');
883 $action = '';
884 }
885 }
886
887 if ($action == 'confirm_remove_customer_price' && $prodcustprice !== null && ($user->hasRight('produit', 'supprimer') || $user->hasRight('service', 'supprimer'))) {
888 // Delete price by customer
889 $update_child_soc = GETPOSTINT('updatechildprice');
890
891 $prodcustprice->fetch(GETPOSTINT('lineid'));
892 $result = $prodcustprice->delete($user, 0, $update_child_soc);
893
894 if ($result > 0) {
895 $db->query("DELETE FROM ".MAIN_DB_PREFIX."product_customer_price_extrafields WHERE fk_object = ".((int) $prodcustprice->id));
896 setEventMessages($langs->trans("PriceRemoved"), null, 'mesgs');
897 } else {
898 $error++;
899 setEventMessages($object->error, $object->errors, 'errors');
900 }
901
902 if ($result < 0) {
903 setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
904 } else {
905 setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
906 }
907 $action = '';
908 }
909
910 if ($action == 'update_customer_price_confirm' && !$cancel && $prodcustprice !== null && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
911 $maxpricesupplier = $object->min_recommended_price();
912
913 $update_child_soc = GETPOSTINT('updatechildprice');
914
915 $prodcustprice->fetch(GETPOSTINT('lineid'));
916
917 // update price by customer
918 $prodcustprice->ref_customer = GETPOST('ref_customer', 'alpha');
919 $prodcustprice->price = price2num(GETPOST("price"), 'MU');
920 $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU');
921 $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha');
922 $prodcustprice->price_label = GETPOST("price_label", 'alpha');
923 $prodcustprice->discount_percent = price2num(GETPOST("discount_percent"));
924 $prodcustprice->date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server;
925 $prodcustprice->date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server
926
927 $extralabels = $extrafields->fetch_name_optionals_label("product_customer_price");
928 $extrafield_values = $extrafields->getOptionalsFromPost("product_customer_price");
929
930 $tva_tx_txt = GETPOST("tva_tx");
931
932 $tva_tx = $tva_tx_txt;
933 $vatratecode = '';
934 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
935 $vat_src_code = $reg[1];
936 $tva_tx = preg_replace('/\s*\‍(.*\‍)/', '', $tva_tx_txt); // Remove code into vatrate.
937 }
938 $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
939
940 $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
941 $localtax1 = 0;
942 $localtax2 = 0;
943 $localtax1_type = '0';
944 $localtax2_type = '0';
945 // If value contains the unique code of vat line (new recommended method), we use it to find npr and local taxes
946 if (preg_match('/\‍((.*)\‍)/', $tva_tx_txt, $reg)) {
947 // We look into database using code
948 $vatratecode = $reg[1];
949 // Get record from code
950 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
951 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
952 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
953 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
954 $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
955 $sql .= " AND t.entity IN (".getEntity('c_tva').")";
956 $resql = $db->query($sql);
957 if ($resql) {
958 $obj = $db->fetch_object($resql);
959 if ($obj) {
960 $npr = $obj->recuperableonly;
961 $localtax1 = $obj->localtax1;
962 $localtax2 = $obj->localtax2;
963 $localtax1_type = $obj->localtax1_type;
964 $localtax2_type = $obj->localtax2_type;
965 }
966
967 // If spain, we don't use the localtax found into tax record in database with same code, but using the get_localtax rule.
968 if (in_array($mysoc->country_code, array('ES'))) {
969 $localtax1 = get_localtax($tva_tx, 1);
970 $localtax2 = get_localtax($tva_tx, 2);
971 }
972 }
973 } else {
974 // Get record with empty code
975 $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
976 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
977 $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
978 $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
979 $sql .= " AND t.code = ''";
980 $resql = $db->query($sql);
981 if ($resql) {
982 $obj = $db->fetch_object($resql);
983 if ($obj) {
984 $npr = $obj->recuperableonly;
985 $localtax1 = $obj->localtax1;
986 $localtax2 = $obj->localtax2;
987 $localtax1_type = $obj->localtax1_type;
988 $localtax2_type = $obj->localtax2_type;
989 }
990 }
991 }
992
993 $prodcustprice->default_vat_code = $vatratecode;
994 $prodcustprice->tva_tx = $tva_tx;
995 $prodcustprice->recuperableonly = $npr;
996 $prodcustprice->localtax1_tx = $localtax1;
997 $prodcustprice->localtax2_tx = $localtax2;
998 $prodcustprice->localtax1_type = $localtax1_type;
999 $prodcustprice->localtax2_type = $localtax2_type;
1000
1001 if ($prodcustprice->price_min < $maxpricesupplier && getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE')) {
1002 setEventMessages($langs->trans("MinimumPriceLimit", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')), null, 'errors');
1003 $error++;
1004 $action = 'update_customer_price';
1005 }
1006
1007 if (!$error) {
1008 $result = $prodcustprice->update($user, 0, $update_child_soc);
1009 if ($result > 0) {
1010 if (!empty($extrafield_values) && is_array($extrafield_values)) {
1011 $productcustomerprice = new ProductCustomerPrice($db);
1012 $res = $productcustomerprice->fetch($prodcustprice->id);
1013 if ($res > 0) {
1014 foreach ($extrafield_values as $key => $value) {
1015 $productcustomerprice->array_options[$key] = $value;
1016 }
1017 $result2 = $productcustomerprice->insertExtraFields();
1018 if ($result2 < 0) {
1019 $prodcustprice->error = $productcustomerprice->error;
1020 $prodcustprice->errors = $productcustomerprice->errors;
1021 $error++;
1022 }
1023 }
1024 }
1025 }
1026
1027 if ($result < 0) {
1028 setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
1029 $action = 'update_customer_price';
1030 } else {
1031 setEventMessages($langs->trans("Save"), null, 'mesgs');
1032 $action = '';
1033 }
1034 }
1035 }
1036}
1037
1038
1039/*
1040 * View
1041 */
1042
1043$form = new Form($db);
1044
1045if (!empty($id) || !empty($ref)) {
1046 // fetch updated prices
1047 $object->fetch($id, $ref);
1048}
1049
1050$title = $langs->trans('ProductServiceCard');
1051$helpurl = '';
1052$shortlabel = dol_trunc($object->label, 16);
1053if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
1054 $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('SellingPrices');
1055 $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
1056}
1057if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
1058 $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('SellingPrices');
1059 $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
1060}
1061
1062llxHeader('', $title, $helpurl, '', 0, 0, '', '', '', 'classforhorizontalscrolloftabs mod-product page-price');
1063
1064if ($action == 'ask_remove_customer_price' && ($user->hasRight('produit', 'supprimer') || $user->hasRight('service', 'supprimer'))) {
1065 $formquestion = array(
1066 array('type' => 'checkbox', 'name' => 'updatechildprice', 'label' => $langs->trans('ForceDeleteChildPriceSoc'), 'value' => 0),
1067 );
1068 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.GETPOSTINT('lineid'), $langs->trans('DeleteCustomerPrice'), $langs->trans('ConfirmDeleteCustomerPrice'), 'confirm_remove_customer_price', $formquestion, 0, 1);
1069 print $formconfirm;
1070}
1071
1072$head = product_prepare_head($object);
1073$titre = $langs->trans("CardProduct".$object->type);
1074$picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
1075
1076print dol_get_fiche_head($head, 'price', $titre, -1, $picto);
1077
1078$linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1&type='.$object->type.'">'.$langs->trans("BackToList").'</a>';
1079$object->next_prev_filter = "(te.fk_product_type:=:".((int) $object->type).")";
1080
1081$shownav = 1;
1082if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
1083 $shownav = 0;
1084}
1085
1086dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
1087
1088
1089print '<div class="fichecenter">';
1090
1091print '<div class="underbanner clearboth"></div>';
1092print '<table class="border tableforfield centpercent">';
1093
1094$soc = null;
1095
1096// Price per customer segment/level
1097if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
1098 // Price and min price are variable (depends on level of company).
1099 if (!empty($socid)) {
1100 $soc = new Societe($db);
1101 $soc->id = $socid;
1102 $soc->fetch($socid);
1103
1104 // Type
1105 if (isModEnabled("product") && isModEnabled("service")) {
1106 $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
1107 print '<tr><td class="">';
1108 print (!getDolGlobalString('PRODUCT_DENY_CHANGE_PRODUCT_TYPE')) ? $form->editfieldkey("Type", 'fk_product_type', (string) $object->type, $object, 0, $typeformat) : $langs->trans('Type');
1109 print '</td><td>';
1110 print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
1111 print '</td></tr>';
1112 }
1113
1114 // Selling price
1115 print '<tr><td class="titlefieldcreate">';
1116 print $langs->trans("SellingPrice");
1117 print '</td>';
1118 print '<td colspan="2">';
1119 if ($object->multiprices_base_type[$soc->price_level] == 'TTC') {
1120 print '<span class="amount">'.price($object->multiprices_ttc[$soc->price_level]).'</span>';
1121 } else {
1122 print '<span class="amount">'.price($object->multiprices[$soc->price_level]).'</span>';
1123 }
1124 if ($object->multiprices_base_type[$soc->price_level]) {
1125 print ' '.$langs->trans($object->multiprices_base_type[$soc->price_level]);
1126 } else {
1127 print ' '.$langs->trans($object->price_base_type);
1128 }
1129 print '</td></tr>';
1130
1131 // Price min
1132 print '<tr><td>'.$langs->trans("MinPrice").'</td><td colspan="2">';
1133 if ($object->multiprices_base_type[$soc->price_level] == 'TTC') {
1134 print price($object->multiprices_min_ttc[$soc->price_level]).' '.$langs->trans($object->multiprices_base_type[$soc->price_level]);
1135 } else {
1136 print price($object->multiprices_min[$soc->price_level]).' '.$langs->trans(empty($object->multiprices_base_type[$soc->price_level]) ? 'HT' : $object->multiprices_base_type[$soc->price_level]);
1137 }
1138 print '</td></tr>';
1139
1140 if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) { // using this option is a bug. kept for backward compatibility
1141 // TVA
1142 print '<tr><td>'.$langs->trans("DefaultTaxRate").'</td><td colspan="2">';
1143
1144 $positiverates = '';
1145 if (price2num($object->multiprices_tva_tx[$soc->price_level])) {
1146 $positiverates .= ($positiverates ? '/' : '').price2num($object->multiprices_tva_tx[$soc->price_level]);
1147 }
1148 if (empty($positiverates)) {
1149 $positiverates = '0';
1150 }
1151 echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), true, $object->tva_npr);
1152
1153 print '</td></tr>';
1154 } else {
1155 // TVA
1156 print '<tr><td>'.$langs->trans("DefaultTaxRate").'</td><td>';
1157
1158 $positiverates = '';
1159 if (price2num($object->tva_tx)) {
1160 $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
1161 }
1162 if (price2num($object->localtax1_type)) {
1163 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
1164 }
1165 if (price2num($object->localtax2_type)) {
1166 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
1167 }
1168 if (empty($positiverates)) {
1169 $positiverates = '0';
1170 }
1171 echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), true, $object->tva_npr);
1172
1173 print '</td></tr>';
1174 }
1175 } else {
1176 if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) { // using this option is a bug. kept for backward compatibility
1177 // Type
1178 if (isModEnabled("product") && isModEnabled("service")) {
1179 $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
1180 print '<tr><td class="">';
1181 print (!getDolGlobalString('PRODUCT_DENY_CHANGE_PRODUCT_TYPE')) ? $form->editfieldkey("Type", 'fk_product_type', (string) $object->type, $object, 0, $typeformat) : $langs->trans('Type');
1182 print '</td><td>';
1183 print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
1184 print '</td></tr>';
1185 }
1186
1187 // We show only vat for level 1
1188 print '<tr><td class="titlefieldcreate">'.$langs->trans("DefaultTaxRate").'</td>';
1189 print '<td colspan="2">'.vatrate($object->multiprices_tva_tx[1], true).'</td>';
1190 print '</tr>';
1191 } else {
1192 // Type
1193 if (isModEnabled("product") && isModEnabled("service")) {
1194 $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
1195 print '<tr><td class="">';
1196 print (!getDolGlobalString('PRODUCT_DENY_CHANGE_PRODUCT_TYPE')) ? $form->editfieldkey("Type", 'fk_product_type', (string) $object->type, $object, 0, $typeformat) : $langs->trans('Type');
1197 print '</td><td>';
1198 print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
1199 print '</td></tr>';
1200 }
1201
1202 // TVA
1203 print '<!-- Default VAT Rate -->';
1204 print '<tr><td class="titlefieldcreate">'.$langs->trans("DefaultTaxRate").'</td><td>';
1205
1206 // TODO We show localtax from $object, but this properties may not be correct. Only value $object->default_vat_code is guaranteed.
1207 $positiverates = '';
1208 if (price2num($object->tva_tx)) {
1209 $positiverates .= ($positiverates ? '<span class="opacitymedium">/</span>' : '').price2num($object->tva_tx);
1210 }
1211 if (price2num($object->localtax1_type)) {
1212 $positiverates .= ($positiverates ? '<span class="opacitymedium">/</span>' : '').price2num($object->localtax1_tx);
1213 }
1214 if (price2num($object->localtax2_type)) {
1215 $positiverates .= ($positiverates ? '<span class="opacitymedium">/</span>' : '').price2num($object->localtax2_tx);
1216 }
1217 if (empty($positiverates)) {
1218 $positiverates = '0';
1219 }
1220
1221 print vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), true, $object->tva_npr, 1);
1222 /*
1223 if ($object->default_vat_code)
1224 {
1225 print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
1226 }
1227 else print vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true);*/
1228 print '</td></tr>';
1229 }
1230 print '</table>';
1231
1232 print '<br>';
1233
1234 print '<table class="liste centpercent">';
1235 print '<tr class="liste_titre"><td>';
1236 print $langs->trans("PriceLevel");
1237 if ($user->admin) {
1238 print ' <a class="editfielda" href="'.dolBuildUrl($_SERVER["PHP_SELF"], ['action' => 'editlabelsellingprice', 'id' => $object->id], true).'">'.img_edit($langs->trans('EditSellingPriceLabel'), 0).'</a>';
1239 }
1240 print '</td>';
1241 print '<td style="text-align: right">'.$langs->trans("SellingPrice").'</td>';
1242 print '<td style="text-align: right">'.$langs->trans("MinPrice").'</td>';
1243 // fetch optionals attributes and labels
1244 $extrafields->fetch_name_optionals_label("product_price");
1245 if ($extrafields->attributes["product_price"] && array_key_exists('label', $extrafields->attributes["product_price"])) {
1246 $extralabels = $extrafields->attributes["product_price"]['label'];
1247 if (!empty($extralabels)) {
1248 foreach ($extralabels as $key => $value) {
1249 // Show field if not hidden
1250 if (!empty($extrafields->attributes["product_price"]['list'][$key]) && $extrafields->attributes["product_price"]['list'][$key] != 3) {
1251 if (!empty($extrafields->attributes["product_price"]['langfile'][$key])) {
1252 $langs->load($extrafields->attributes["product_price"]['langfile'][$key]);
1253 }
1254 if (!empty($extrafields->attributes["product_price"]['help'][$key])) {
1255 $extratitle = $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_price"]['help'][$key]));
1256 } else {
1257 $extratitle = $langs->trans($value);
1258 }
1259 $field = 'ef.' . $key;
1260 print '<td style="text-align: right">'.$extratitle.'</td>';
1261 }
1262 }
1263 }
1264 }
1265 print '</tr>';
1266
1267 $produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT');
1268 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
1269 print '<tr class="oddeven">';
1270
1271 // Label of price
1272 print '<td>';
1273 $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i;
1274 if (preg_match('/editlabelsellingprice/', $action)) {
1275 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">';
1276 print '<input type="hidden" name="token" value="'.newToken().'">';
1277 print '<input type="hidden" name="action" value="setlabelsellingprice">';
1278 print '<input type="hidden" name="pricelevel" value="'.$i.'">';
1279 print $langs->trans("SellingPrice").' '.$i.' - ';
1280 print '<input class="maxwidthonsmartphone" type="text" name="labelsellingprice" value="' . getDolGlobalString($keyforlabel).'">';
1281 print '&nbsp;<input type="submit" class="button smallpaddingimp" value="'.$langs->trans("Modify").'">';
1282 print '</form>';
1283 } else {
1284 print $langs->trans("SellingPrice").' '.$i;
1285 if (getDolGlobalString($keyforlabel)) {
1286 print ' - '.$langs->trans(getDolGlobalString($keyforlabel));
1287 }
1288 }
1289 print '</td>';
1290
1291 if ($object->multiprices_base_type [$i] == 'TTC') {
1292 print '<td class="right"><span class="amount">'.price($object->multiprices_ttc[$i]);
1293 } else {
1294 print '<td class="right"><span class="amount">'.price($object->multiprices[$i]);
1295 }
1296
1297 if ($object->multiprices_base_type[$i]) {
1298 print ' '.$langs->trans($object->multiprices_base_type [$i]).'</span></td>';
1299 } else {
1300 print ' '.$langs->trans($object->price_base_type).'</span></td>';
1301 }
1302
1303 // Prix min
1304 print '<td style="text-align: right">';
1305 if (empty($object->multiprices_base_type[$i])) {
1306 $object->multiprices_base_type[$i] = "HT";
1307 }
1308 if ($object->multiprices_base_type[$i] == 'TTC') {
1309 print price($object->multiprices_min_ttc[$i]).' '.$langs->trans($object->multiprices_base_type[$i]);
1310 } else {
1311 print price($object->multiprices_min[$i]).' '.$langs->trans($object->multiprices_base_type[$i]);
1312 }
1313 print '</td>';
1314 if (!empty($extralabels)) {
1315 require_once DOL_DOCUMENT_ROOT . '/core/class/genericobject.class.php';
1316 $genericObject = new GenericObject($db);
1317 // We need to force table to fetch optionals product_price extrafields
1318 $genericObject->table_element = 'product_price';
1319 $sql1 = "SELECT rowid";
1320 $sql1 .= " FROM ".$object->db->prefix()."product_price";
1321 $sql1 .= " WHERE entity IN (".getEntity('productprice').")";
1322 $sql1 .= " AND price_level=".((int) $i);
1323 $sql1 .= " AND fk_product = ".((int) $object->id);
1324 $sql1 .= " ORDER BY date_price DESC, rowid DESC";
1325 $sql1 .= " LIMIT 1";
1326 $resql1 = $object->db->query($sql1);
1327 if ($resql1 && $lineid = $object->db->fetch_object($resql1)) {
1328 $genericObject->id = $lineid->rowid;
1329 $genericObject->fetch_optionals();
1330 }
1331 foreach ($extralabels as $key => $value) {
1332 if (!empty($extrafields->attributes["product_price"]['list'][$key]) && $extrafields->attributes["product_price"]['list'][$key] != 3) {
1333 $extravalue = $genericObject->array_options['options_' . $key];
1334 // If field is a computed field, we make computation to get value
1335 if (!empty($extrafields->attributes["product_price"]['computed'][$key])) {
1336 $genericObject->price = $object->multiprices[$i];
1337 $genericObject->price_ttc = $object->multiprices_ttc[$i];
1338 $genericObject->price_base_type = $object->multiprices_base_type[$i];
1339 $genericObject->price_min = $object->multiprices_min[$i];
1340 $genericObject->price_min_ttc = $object->multiprices_min_ttc[$i];
1341 $genericObject->tva_tx = $object->multiprices_tva_tx[$i];
1342
1343 $objectoffield = $genericObject; // For compatibility with the computed formula. $objectoffield is exported by dol_eval().
1344 $extravalue = dol_eval((string) $extrafields->attributes["product_price"]['computed'][$key], 1, 1, '2');
1345 }
1346 print '<td align="right">'.$extrafields->showOutputField($key, $extravalue, '', 'product_price')."</td>";
1347 }
1348 }
1349 $db->free($resql1);
1350 }
1351 print '</tr>';
1352
1353 // Price by quantity
1354 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) { // TODO Fix the form included into a tr instead of a td
1355 print '<tr><td>'.$langs->trans("PriceByQuantity").' '.$i;
1356 if (getDolGlobalString($keyforlabel)) {
1357 print ' - '.$langs->trans(getDolGlobalString($keyforlabel));
1358 }
1359 print '</td><td colspan="2">';
1360
1361 if ($object->prices_by_qty[$i] == 1) {
1362 print '<table width="50%" class="border" summary="List of quantities">';
1363
1364 print '<tr class="liste_titre">';
1365 print '<td>'.$langs->trans("PriceByQuantityRange").' '.$i.'</td>';
1366 print '<td class="right">'.$langs->trans("HT").'</td>';
1367 print '<td class="right">'.$langs->trans("UnitPrice").'</td>';
1368 print '<td class="right">'.$langs->trans("Discount").'</td>';
1369 print '<td>&nbsp;</td>';
1370 print '</tr>';
1371 $ii = 0;
1372 foreach ($object->prices_by_qty_list[$i] as $ii => $prices) {
1373 if ($action == 'edit_price_by_qty' && $rowid == $prices['rowid'] && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
1374 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1375 print '<input type="hidden" name="token" value="'.newToken().'">';
1376 print '<input type="hidden" name="action" value="update_price_by_qty">';
1377 print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[$i].'">';
1378 print '<input type="hidden" value="'.$prices['rowid'].'" name="rowid">';
1379 print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1380 print '<td><input size="5" type="text" value="'.$prices['quantity'].'" name="quantity"></td>';
1381 print '<td class="right" colspan="2"><input size="10" type="text" value="'.price2num($prices['price'], 'MU').'" name="price">&nbsp;'.$object->price_base_type.'</td>';
1382 print '<td class="right nowraponall"><input size="5" type="text" value="'.$prices['remise_percent'].'" name="remise_percent"> %</td>';
1383 print '<td class="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>';
1384 print '</tr>';
1385 print '</form>';
1386 } else {
1387 print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1388 print '<td>'.$prices['quantity'].'</td>';
1389 print '<td class="right">'.price($prices['price']).'</td>';
1390 print '<td class="right">'.price($prices['unitprice']).'</td>';
1391 print '<td class="right">'.price($prices['remise_percent']).' %</td>';
1392 print '<td class="center">';
1393 if (($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
1394 print '<a class="editfielda marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=edit_price_by_qty&token='.newToken().'&rowid='.$prices["rowid"].'">';
1395 print img_edit().'</a>';
1396 print '<a class="marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete_price_by_qty&token='.newToken().'&rowid='.$prices["rowid"].'">';
1397 print img_delete().'</a>';
1398 } else {
1399 print '&nbsp;';
1400 }
1401 print '</td>';
1402 print '</tr>';
1403 }
1404 }
1405 if ($action != 'edit_price_by_qty' && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
1406 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1407 print '<input type="hidden" name="token" value="'.newToken().'">';
1408 print '<input type="hidden" name="action" value="update_price_by_qty">';
1409 print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[$i].'">'; // id in product_price
1410 print '<input type="hidden" value="0" name="rowid">'; // id in product_price
1411 print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1412 print '<td><input size="5" type="text" value="1" name="quantity"></td>';
1413 print '<td class="right" class="nowrap"><input size="10" type="text" value="0" name="price">&nbsp;'.$object->price_base_type.'</td>';
1414 print '<td class="right">&nbsp;</td>';
1415 print '<td class="right" class="nowraponall"><input size="5" type="text" value="0" name="remise_percent"> %</td>';
1416 print '<td class="center"><input type="submit" value="'.$langs->trans("Add").'" class="button"></td>';
1417 print '</tr>';
1418 print '</form>';
1419 }
1420
1421 print '</table>';
1422 print '<a class="editfielda marginleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=disable_price_by_qty&level='.$i.'&token='.newToken().'">('.$langs->trans("DisablePriceByQty").')</a>';
1423 } else {
1424 print $langs->trans("No");
1425 print '&nbsp; <a class="marginleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=activate_price_by_qty&level='.$i.'&token='.newToken().'">('.$langs->trans("Activate").')</a>';
1426 }
1427 print '</td></tr>';
1428 }
1429 }
1430 }
1431} else {
1432 // Type
1433 if (isModEnabled("product") && isModEnabled("service")) {
1434 $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
1435 print '<tr><td class="">';
1436 print (!getDolGlobalString('PRODUCT_DENY_CHANGE_PRODUCT_TYPE')) ? $form->editfieldkey("Type", 'fk_product_type', (string) $object->type, $object, 0, $typeformat) : $langs->trans('Type');
1437 print '</td><td>';
1438 print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, 0, $typeformat);
1439 print '</td></tr>';
1440 }
1441
1442 // TVA
1443 print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td><td>';
1444
1445 $positiverates = '';
1446 if (price2num($object->tva_tx)) {
1447 $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
1448 }
1449 if (price2num($object->localtax1_type)) {
1450 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
1451 }
1452 if (price2num($object->localtax2_type)) {
1453 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
1454 }
1455 if (empty($positiverates)) {
1456 $positiverates = '0';
1457 }
1458 echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), true, $object->tva_npr, 0, 1);
1459 /*
1460 if ($object->default_vat_code)
1461 {
1462 print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
1463 }
1464 else print vatrate($object->tva_tx, true, $object->tva_npr, true);*/
1465 print '</td></tr>';
1466
1467 // Price
1468 print '<tr class="field_selling_price"><td>'.$langs->trans("SellingPrice").'</td><td>';
1469 if ($object->price_base_type == 'TTC') {
1470 print price($object->price_ttc).' '.$langs->trans($object->price_base_type);
1471 } else {
1472 print price($object->price).' '.$langs->trans($object->price_base_type);
1473 if (getDolGlobalString('PRODUCT_DISPLAY_VAT_INCL_PRICES') && !empty($object->price_ttc)) {
1474 print '<i class="opacitymedium"> - ' . price($object->price_ttc).' '.$langs->trans('TTC') . '</i>';
1475 }
1476 }
1477
1478 print '</td></tr>';
1479
1480 // Price minimum
1481 print '<tr class="field_min_price"><td>'.$langs->trans("MinPrice").'</td><td>';
1482 if ($object->price_base_type == 'TTC') {
1483 print price($object->price_min_ttc).' '.$langs->trans($object->price_base_type);
1484 } else {
1485 print price($object->price_min).' '.$langs->trans($object->price_base_type);
1486 if (getDolGlobalString('PRODUCT_DISPLAY_VAT_INCL_PRICES') && !empty($object->price_min_ttc)) {
1487 print '<i class="opacitymedium"> - ' . price($object->price_min_ttc).' '.$langs->trans('TTC') . '</i>';
1488 }
1489 }
1490 print '</td></tr>';
1491
1492 // Packaging
1493 if (getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING')) {
1494 print '<tr class="field_price_label"><td>'.$form->textwithpicto($langs->trans("PackagingForThisProduct"), $langs->trans("PackagingForThisProductDesc")).'</td><td>';
1495 print $object->packaging;
1496 print '</td></tr>';
1497 }
1498
1499 // Price Label
1500 print '<tr class="field_price_label"><td>'.$langs->trans("PriceLabel").'</td><td>';
1501 print $object->price_label;
1502 print '</td></tr>';
1503
1504 $ii = 0;
1505 // Price by quantity
1506 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) { // TODO Fix the form inside tr instead of td
1507 print '<tr><td>'.$langs->trans("PriceByQuantity");
1508 if ($object->prices_by_qty[0] == 0) {
1509 print '&nbsp; <a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=activate_price_by_qty&level=1&token='.newToken().'">('.$langs->trans("Activate").')';
1510 } else {
1511 print '&nbsp; <a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=disable_price_by_qty&level=1&token='.newToken().'">('.$langs->trans("DisablePriceByQty").')';
1512 }
1513 print '</td><td>';
1514
1515 if ($object->prices_by_qty[0] == 1) {
1516 print '<table width="50%" class="border" summary="List of quantities">';
1517 print '<tr class="liste_titre">';
1518 //print '<td>' . $langs->trans("PriceByQuantityRange") . '</td>';
1519 print '<td>'.$langs->trans("Quantity").'</td>';
1520 print '<td class="right">'.$langs->trans("Price").'</td>';
1521 print '<td class="right"></td>';
1522 print '<td class="right">'.$langs->trans("UnitPrice").'</td>';
1523 print '<td class="right">'.$langs->trans("Discount").'</td>';
1524 print '<td>&nbsp;</td>';
1525 print '</tr>';
1526 if ($action != 'edit_price_by_qty') {
1527 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">'; // FIXME a form into a table is not allowed
1528 print '<input type="hidden" name="token" value="'.newToken().'">';
1529 print '<input type="hidden" name="action" value="update_price_by_qty">';
1530 print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[0].'">'; // id in product_price
1531 print '<input type="hidden" value="0" name="rowid">'; // id in product_price_by_qty
1532
1533 print '<tr class="'.(($ii % 2) == 0 ? 'pair' : 'impair').'">'; // @phpstan-ignore-line
1534 print '<td><input size="5" type="text" value="1" name="quantity"></td>';
1535 print '<td class="right"><input class="width50 right" type="text" value="0" name="price"></td>';
1536 print '<td>';
1537 //print $object->price_base_type;
1538 print '</td>';
1539 print '<td class="right">&nbsp;</td>';
1540 print '<td class="right nowraponall"><input type="text" class="width50 right" value="0" name="remise_percent"> %</td>';
1541 print '<td class="center"><input type="submit" value="'.$langs->trans("Add").'" class="button"></td>';
1542 print '</tr>';
1543
1544 print '</form>';
1545 }
1546 foreach ($object->prices_by_qty_list[0] as $ii => $prices) {
1547 if ($action == 'edit_price_by_qty' && $rowid == $prices['rowid'] && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
1548 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1549 print '<input type="hidden" name="token" value="'.newToken().'">';
1550 print '<input type="hidden" name="action" value="update_price_by_qty">';
1551 print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[0].'">'; // id in product_price
1552 print '<input type="hidden" value="'.$prices['rowid'].'" name="rowid">'; // id in product_price_by_qty
1553 print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1554 print '<td><input size="5" type="text" value="'.$prices['quantity'].'" name="quantity"></td>';
1555 print '<td class="right"><input class="width50 right" type="text" value="'.price2num($prices['price'], 'MU').'" name="price"></td>';
1556 print '<td class="right">';
1557 //print $object->price_base_type;
1558 print $prices['price_base_type'];
1559 print '</td>';
1560 print '<td class="right">&nbsp;</td>';
1561 print '<td class="right nowraponall"><input class="width50 right" type="text" value="'.$prices['remise_percent'].'" name="remise_percent"> %</td>';
1562 print '<td class="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>';
1563 print '</tr>';
1564 print '</form>';
1565 } else {
1566 print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1567 print '<td>'.$prices['quantity'].'</td>';
1568 print '<td class="right">'.price($prices['price']).'</td>';
1569 print '<td class="right">';
1570 //print $object->price_base_type;
1571 print $prices['price_base_type'];
1572 print '</td>';
1573 print '<td class="right">'.price($prices['unitprice']).'</td>';
1574 print '<td class="right">'.price($prices['remise_percent']).' %</td>';
1575 print '<td class="center">';
1576 if (($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
1577 print '<a class="editfielda marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=edit_price_by_qty&token='.newToken().'&rowid='.$prices["rowid"].'">';
1578 print img_edit().'</a>';
1579 print '<a class="marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete_price_by_qty&token='.newToken().'&rowid='.$prices["rowid"].'">';
1580 print img_delete().'</a>';
1581 } else {
1582 print '&nbsp;';
1583 }
1584 print '</td>';
1585 print '</tr>';
1586 }
1587 }
1588 print '</table>';
1589 } else {
1590 print $langs->trans("No");
1591 }
1592 print '</td></tr>';
1593 }
1594}
1595
1596print "</table>\n";
1597
1598print '</div>';
1599print '<div class="clearboth"></div>';
1600
1601
1602print dol_get_fiche_end();
1603
1604
1605
1606// Button for actions
1607
1608if (!$action || $action == 'delete' || $action == 'showlog_customer_price' || $action == 'showlog_default_price' || $action == 'add_customer_price'
1609 || $action == 'activate_price_by_qty' || $action == 'disable_price_by_qty') {
1610 print "\n".'<div class="tabsAction">'."\n";
1611
1612 $parameters = array();
1613 $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
1614 if (empty($reshook)) {
1615 if ($object->isVariant()) {
1616 //if ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer')) {
1617 print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#" title="' . dol_escape_htmltag($langs->trans("NoEditVariants")) . '">' . $langs->trans("UpdateDefaultPrice") . '</a></div>';
1618 //}
1619 } else {
1620 if (!getDolGlobalString('PRODUIT_MULTIPRICES') && !getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') && !getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
1621 if ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer')) {
1622 print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?action=edit_price&token='.newToken().'&id=' . $object->id . '">' . $langs->trans("UpdateDefaultPrice") . '</a></div>';
1623 } else {
1624 print '<div class="inline-block divButAction"><span class="butActionRefused" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">' . $langs->trans("UpdateDefaultPrice") . '</span></div>';
1625 }
1626 }
1627
1628 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
1629 if ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer')) {
1630 print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?action=add_customer_price&token='.newToken().'&id=' . $object->id . '">' . $langs->trans("AddCustomerPrice") . '</a></div>';
1631 } else {
1632 print '<div class="inline-block divButAction"><span class="butActionRefused" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">' . $langs->trans("AddCustomerPrice") . '</span></div>';
1633 }
1634 }
1635
1636 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
1637 if ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer')) {
1638 print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?action=edit_vat&token='.newToken().'&id=' . $object->id . '">' . $langs->trans("UpdateVAT") . '</a></div>';
1639 } else {
1640 print '<div class="inline-block divButAction"><span class="butActionRefused" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">' . $langs->trans("UpdateVAT") . '</span></div>';
1641 }
1642
1643 if ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer')) {
1644 print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?action=edit_level_price&token='.newToken().'&id=' . $object->id . '">' . $langs->trans("UpdateLevelPrices") . '</a></div>';
1645 } else {
1646 print '<div class="inline-block divButAction"><span class="butActionRefused" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">' . $langs->trans("UpdateLevelPrices") . '</span></div>';
1647 }
1648 }
1649 }
1650 }
1651
1652 print "\n</div>\n";
1653}
1654
1655
1656
1657/*
1658 * Edit price area
1659 */
1660
1661if ($action == 'edit_vat' && ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer'))) {
1662 print load_fiche_titre($langs->trans("UpdateVAT"), '');
1663
1664 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1665 print '<input type="hidden" name="token" value="'.newToken().'">';
1666 print '<input type="hidden" name="action" value="update_vat">';
1667 print '<input type="hidden" name="id" value="'.$object->id.'">';
1668
1669 print dol_get_fiche_head();
1670
1671 print '<table class="border centpercent">';
1672
1673 // VAT
1674 print '<tr><td>'.$langs->trans("DefaultTaxRate").'</td><td>';
1675
1676 print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, null, $object->id, $object->tva_npr, $object->type, false, 1, 1);
1677
1678 print '</td></tr>';
1679
1680 print '</table>';
1681
1682 print dol_get_fiche_end();
1683
1684 print $form->buttonsSaveCancel();
1685
1686 print '<br></form><br>';
1687}
1688
1689if (($action == 'edit_price' || $action == 'edit_level_price') && $permissiontoadd) {
1690 print '<br>';
1691 print load_fiche_titre($langs->trans("NewPrice"), '');
1692
1693 if (!getDolGlobalString('PRODUIT_MULTIPRICES') && $action != 'edit_level_price') {
1694 print '<!-- Edit price -->'."\n";
1695 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1696 print '<input type="hidden" name="token" value="'.newToken().'">';
1697 print '<input type="hidden" name="action" value="update_price">';
1698 print '<input type="hidden" name="id" value="'.$object->id.'">';
1699
1700 print dol_get_fiche_head();
1701
1702 print '<div class="div-table-responsive-no-min">';
1703 print '<table class="border centpercent">';
1704
1705 // VAT
1706 print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td><td>';
1707 print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, null, $object->id, $object->tva_npr, $object->type, false, 1, 1);
1708 print '</td></tr>';
1709
1710 // Price base
1711 print '<tr><td>';
1712 print $langs->trans('PriceBase');
1713 print '</td>';
1714 print '<td>';
1715 print $form->selectPriceBaseType($object->price_base_type, "price_base_type");
1716 print '</td>';
1717 print '</tr>';
1718
1719 // Only show price mode and expression selector if module is enabled
1720 if (isModEnabled('dynamicprices')) {
1721 // Price mode selector
1722 print '<!-- Show price mode of dynamicprices editor -->'."\n";
1723 print '<tr><td>'.$langs->trans("PriceMode").'</td><td>';
1724 print img_picto('', 'dynamicprice', 'class="pictofixedwidth"');
1725 $price_expression = new PriceExpression($db);
1726 $price_expression_list = array(0 => $langs->trans("Numeric").' <span class="opacitymedium">('.$langs->trans("NoDynamicPrice").')</span>'); //Put the numeric mode as first option
1727 foreach ($price_expression->list_price_expression() as $entry) {
1728 $price_expression_list[$entry->id] = $entry->title;
1729 }
1730 $price_expression_preselection = GETPOST('eid') ? GETPOST('eid') : ($object->fk_price_expression ? $object->fk_price_expression : '0');
1731 print $form->selectarray('eid', $price_expression_list, $price_expression_preselection);
1732 print '&nbsp; <a id="expression_editor" class="classlink">'.$langs->trans("PriceExpressionEditor").'</a>';
1733 print '</td></tr>';
1734
1735 // This code hides the numeric price input if is not selected, loads the editor page if editor button is pressed
1736 ?>
1737
1738 <script type="text/javascript">
1739 jQuery(document).ready(function() {
1740 jQuery("#expression_editor").click(function() {
1741 window.location = "<?php echo DOL_URL_ROOT ?>/product/dynamic_price/editor.php?id=<?php echo $id ?>&tab=price&eid=" + $("#eid").val();
1742 });
1743 jQuery("#eid").change(on_change);
1744 on_change();
1745 });
1746 function on_change() {
1747 if ($("#eid").val() == 0) {
1748 jQuery("#price_numeric").show();
1749 } else {
1750 jQuery("#price_numeric").hide();
1751 }
1752 }
1753 </script>
1754 <?php
1755 }
1756
1757 // Price
1758 $product = new Product($db);
1759 $product->fetch($id, $ref, '', '1'); //Ignore the math expression when getting the price
1760 print '<tr id="price_numeric"><td>';
1761 $text = $langs->trans('SellingPrice');
1762 print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", getDolGlobalString('MAIN_MAX_DECIMALS_UNIT')), 1, 'help');
1763 print '</td><td>';
1764 if ($object->price_base_type == 'TTC') {
1765 print '<input name="price" size="10" value="'.price($product->price_ttc).'">';
1766 } else {
1767 print '<input name="price" size="10" value="'.price($product->price).'">';
1768 }
1769 print '</td></tr>';
1770
1771 // Price minimum
1772 print '<tr><td>';
1773 $text = $langs->trans('MinPrice');
1774 print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", getDolGlobalString('MAIN_MAX_DECIMALS_UNIT')), 1, 'help');
1775 print '</td><td>';
1776 if ($object->price_base_type == 'TTC') {
1777 print '<input name="price_min" size="10" value="'.($object->price_min_ttc ? price($object->price_min_ttc) : '').'">';
1778 } else {
1779 print '<input name="price_min" size="10" value="'.($object->price_min ? price($object->price_min) : '').'">';
1780 }
1781 if (getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE')) {
1782 print ' &nbsp; '.$langs->trans("MinimumRecommendedPrice", price((float) $maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
1783 }
1784 print '</td>';
1785 print '</tr>';
1786
1787 // Packaging
1788 if (getDolGlobalString('PRODUCT_USE_CUSTOMER_PACKAGING')) {
1789 print '<tr><td>';
1790 print $form->textwithpicto($langs->trans("PackagingForThisProduct"), $langs->trans("PackagingForThisProductDesc"));
1791 print '</td><td>';
1792 $packaging = $object->packaging;
1793 print '<input class="flat" name="packaging" size="5" value="' . ($packaging ? price($packaging, 0, '', 1, -1, 2) : '').'">';
1794 print '</td>';
1795 print '</tr>';
1796 }
1797
1798 // Price Label
1799 print '<tr><td>';
1800 print $langs->trans('PriceLabel');
1801 print '</td><td>';
1802 print '<input name="price_label" maxlength="255" class="minwidth300 maxwidth400onsmartphone" value="'.$object->price_label.'">';
1803 print '</td>';
1804 print '</tr>';
1805
1806 $parameters = array();
1807 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
1808
1809 print '</table>';
1810 print '</div>';
1811
1812 print dol_get_fiche_end();
1813
1814 print $form->buttonsSaveCancel();
1815
1816 print '</form>';
1817 } elseif ($action == 'edit_level_price' && $permissiontoadd) {
1818 print '<!-- Edit price per level -->'."\n"; ?>
1819 <script>
1820
1821 var showHidePriceRules = function () {
1822 var otherPrices = $('div.fiche form table tbody tr:not(:first)');
1823 var minPrice1 = $('div.fiche form input[name="price_min[1]"]');
1824
1825 if (jQuery('input#usePriceRules').prop('checked')) {
1826 otherPrices.hide();
1827 minPrice1.hide();
1828 } else {
1829 otherPrices.show();
1830 minPrice1.show();
1831 }
1832 }
1833
1834 jQuery(document).ready(function () {
1835 showHidePriceRules();
1836
1837 jQuery('input#usePriceRules').click(showHidePriceRules);
1838 });
1839 </script>
1840 <?php
1841
1842 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1843 print '<input type="hidden" name="token" value="'.newToken().'">';
1844 print '<input type="hidden" name="action" value="update_level_price">';
1845 print '<input type="hidden" name="id" value="'.$object->id.'">';
1846
1847 //print dol_get_fiche_head(array(), '', '', -1);
1848
1849 if ((getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) && getDolGlobalString('PRODUIT_MULTIPRICES_ALLOW_AUTOCALC_PRICELEVEL')) {
1850 print $langs->trans('UseMultipriceRules').' <input type="checkbox" id="usePriceRules" name="usePriceRules" '.($object->price_autogen ? 'checked' : '').'><br><br>';
1851 }
1852
1853 print '<div class="div-table-responsive-no-min">';
1854 print '<table class="noborder">';
1855 print '<thead><tr class="liste_titre">';
1856
1857 print '<td>'.$langs->trans("PriceLevel").'</td>';
1858
1859 if (getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) {
1860 print '<td style="text-align: center">'.$langs->trans("DefaultTaxRate").'</td>';
1861 } else {
1862 print '<td></td>';
1863 }
1864
1865 print '<td class="center">'.$langs->trans("SellingPrice").'</td>';
1866
1867 print '<td class="center">'.$langs->trans("MinPrice").'</td>';
1868
1869 if (getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE')) {
1870 print '<td></td>';
1871 }
1872
1873 // fetch optionals attributes and labels
1874 $extrafields->fetch_name_optionals_label("product_price");
1875 if ($extrafields->attributes["product_price"] && array_key_exists('label', $extrafields->attributes["product_price"])) {
1876 $extralabels = $extrafields->attributes["product_price"]['label'];
1877 if (!empty($extralabels)) {
1878 foreach ($extralabels as $key => $value) {
1879 // Show field if not hidden
1880 if (!empty($extrafields->attributes["product_price"]['list'][$key]) && $extrafields->attributes["product_price"]['list'][$key] != 3) {
1881 if (!empty($extrafields->attributes["product_price"]['langfile'][$key])) {
1882 $langs->load($extrafields->attributes["product_price"]['langfile'][$key]);
1883 }
1884 if (!empty($extrafields->attributes["product_price"]['help'][$key])) {
1885 $extratitle = $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_price"]['help'][$key]));
1886 } else {
1887 $extratitle = $langs->trans($value);
1888 }
1889 print '<td style="text-align: right">'.$extratitle.'</td>';
1890 }
1891 }
1892 }
1893 }
1894 print '</tr></thead>';
1895
1896 print '<tbody>';
1897
1898 $produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT');
1899 for ($i = 1; $i <= $produit_multiprices_limit; $i++) {
1900 print '<tr class="oddeven">';
1901 print '<td>';
1902 $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i;
1903 $text = $langs->trans('SellingPrice').' '.$i.' - '.getDolGlobalString($keyforlabel);
1904 print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", getDolGlobalString('MAIN_MAX_DECIMALS_UNIT')), 1, 'help');
1905 print '</td>';
1906
1907 // VAT
1908 if (!getDolGlobalString('PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL')) {
1909 print '<td>';
1910 print '<input type="hidden" name="tva_tx['.$i.']" value="'.($object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx).'">';
1911 print '<input type="hidden" name="tva_npr['.$i.']" value="'.$object->tva_npr.'">';
1912 print '<input type="hidden" name="localtax1_tx['.$i.']" value="'.$object->localtax1_tx.'">';
1913 print '<input type="hidden" name="localtax1_type['.$i.']" value="'.$object->localtax1_type.'">';
1914 print '<input type="hidden" name="localtax2_tx['.$i.']" value="'.$object->localtax2_tx.'">';
1915 print '<input type="hidden" name="localtax2_type['.$i.']" value="'.$object->localtax2_type.'">';
1916 print '</td>';
1917 } else {
1918 // This option is kept for backward compatibility but has no sense
1919 print '<td style="text-align: center">';
1920 print $form->load_tva("tva_tx[".$i.']', $object->multiprices_tva_tx[$i], $mysoc, null, $object->id, 0, $object->type, false, 1, 1);
1921 print '</td>';
1922 }
1923
1924 // Selling price
1925 print '<td style="text-align: center">';
1926 if ($object->multiprices_base_type [$i] == 'TTC') {
1927 print '<input name="price['.$i.']" size="10" value="'.price($object->multiprices_ttc [$i]).'">';
1928 } else {
1929 print '<input name="price['.$i.']" size="10" value="'.price($object->multiprices [$i]).'">';
1930 }
1931 print '&nbsp;'.$form->selectPriceBaseType($object->multiprices_base_type [$i], "multiprices_base_type[".$i."]");
1932 print '</td>';
1933
1934 // Min price
1935 print '<td style="text-align: center">';
1936 if ($object->multiprices_base_type [$i] == 'TTC') {
1937 print '<input name="price_min['.$i.']" size="10" value="'.price($object->multiprices_min_ttc [$i]).'">';
1938 } else {
1939 print '<input name="price_min['.$i.']" size="10" value="'.price($object->multiprices_min [$i]).'">';
1940 }
1941 if (getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE')) {
1942 print '<td class="left">'.$langs->trans("MinimumRecommendedPrice", price((float) $maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
1943 }
1944 print '</td>';
1945
1946 if (!empty($extralabels)) {
1947 require_once DOL_DOCUMENT_ROOT . '/core/class/genericobject.class.php';
1948 $genericObject = new GenericObject($db);
1949 // We need to force table to fetch optionals product_price extrafields
1950 $genericObject->table_element = 'product_price';
1951 $sql1 = "SELECT rowid";
1952 $sql1 .= " FROM ".$object->db->prefix()."product_price";
1953 $sql1 .= " WHERE entity IN (".getEntity('productprice').")";
1954 $sql1 .= " AND price_level=".((int) $i);
1955 $sql1 .= " AND fk_product = ".((int) $object->id);
1956 $sql1 .= " ORDER BY date_price DESC, rowid DESC";
1957 $sql1 .= " LIMIT 1";
1958 $resql1 = $object->db->query($sql1);
1959 if ($resql1 && $lineid = $object->db->fetch_object($resql1)) {
1960 $genericObject->id = $lineid->rowid;
1961 $genericObject->fetch_optionals();
1962 }
1963 foreach ($extralabels as $key => $value) {
1964 if (!empty($extrafields->attributes["product_price"]['list'][$key]) && ($extrafields->attributes["product_price"]['list'][$key] == 1 || $extrafields->attributes["product_price"]['list'][$key] == 3 || ($action == "edit_level_price" && $extrafields->attributes["product_price"]['list'][$key] == 4))) {
1965 if (!empty($extrafields->attributes["product_price"]['langfile'][$key])) {
1966 $langs->load($extrafields->attributes["product_price"]['langfile'][$key]);
1967 }
1968
1969 // $extravalue = (GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : $genericObject->array_options['options_' . $key] ?? '');
1970 print '<td align="center">';
1971 print $extrafields->showInputField($key, $genericObject->array_options['options_' . $key], '', (string) $i, '', '', $genericObject, 'product_price');
1972 print '</td>';
1973 }
1974 }
1975 $db->free($resql1);
1976 }
1977 print '</tr>';
1978 }
1979
1980 print '</tbody>';
1981
1982 print '</table>';
1983 print '</div>';
1984
1985 //print dol_get_fiche_end();
1986
1987 print $form->buttonsSaveCancel();
1988
1989 print '</form>';
1990 }
1991}
1992
1993// Add area to show/add/edit a price for a dedicated customer
1994if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
1995 $prodcustprice = new ProductCustomerPrice($db);
1996
1997 $limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
1998 $sortfield = GETPOST('sortfield', 'aZ09comma');
1999 $sortorder = GETPOST('sortorder', 'aZ09comma');
2000 $page = (GETPOSTINT("page") ? GETPOSTINT("page") : 0);
2001 if (empty($page) || $page == -1) {
2002 $page = 0;
2003 } // If $page is not defined, or '' or -1
2004 $offset = $limit * $page;
2005 $pageprev = $page - 1;
2006 $pagenext = $page + 1;
2007 if (!$sortorder) {
2008 $sortorder = "ASC,ASC";
2009 }
2010 if (!$sortfield) {
2011 $sortfield = "soc.nom,t.date_begin";
2012 }
2013
2014 // Build filter to display only concerned lines
2015 $filter = array('t.fk_product' => (string) $object->id);
2016
2017 if (!empty($search_soc)) {
2018 $filter['soc.nom'] = (string) $search_soc;
2019 }
2020
2021 if ($action == 'add_customer_price') {
2022 // Form to add a new customer price
2023 $maxpricesupplier = $object->min_recommended_price();
2024
2025 print '<!-- add_customer_price -->';
2026 print load_fiche_titre($langs->trans('AddCustomerPrice'));
2027
2028 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
2029 print '<input type="hidden" name="token" value="'.newToken().'">';
2030 print '<input type="hidden" name="action" value="add_customer_price_confirm">';
2031 print '<input type="hidden" name="id" value="'.$object->id.'">';
2032
2033 print '<div class="tabBar tabBarWithBottom">';
2034
2035 print '<table class="border centpercent">';
2036 print '<tr>';
2037 print '<td class="fieldrequired">'.$langs->trans('ThirdParty').'</td>';
2038 print '<td>';
2039 $filter = '(s.client:IN:1,2,3)';
2040 print img_picto('', 'company').$form->select_company(GETPOSTINT('socid'), 'socid', $filter, 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300');
2041 print '</td>';
2042 print '</tr>';
2043
2044 // Ref. Customer
2045 print '<tr><td>' . $langs->trans('RefCustomer') . '</td>';
2046 print '<td><input name="ref_customer" size="12"></td></tr>';
2047
2048 // Applied Prices From
2049 $date_begin = dol_mktime(0, 0, 0, GETPOSTINT('date_beginmonth'), GETPOSTINT('date_beginday'), GETPOSTINT('date_beginyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server;
2050 print '<tr><td>'.$langs->trans("AppliedPricesFrom").'</td><td>';
2051 print $form->selectDate(!empty($date_begin) ? $date_begin : dol_now(), "date_begin", 0, 0, 1, "date_begin");
2052 print '</td></tr>';
2053
2054 // Applied Prices To
2055 $date_end = dol_mktime(0, 0, 0, GETPOSTINT('date_endmonth'), GETPOSTINT('date_endday'), GETPOSTINT('date_endyear'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server
2056 print '<tr><td>'.$langs->trans("AppliedPricesTo").'</td><td>';
2057 print $form->selectDate($date_end, "date_end", 0, 0, 1, "date_end");
2058 print '</td></tr>';
2059
2060 // VAT
2061 print '<tr><td class="fieldrequired">'.$langs->trans("DefaultTaxRate").'</td><td>';
2062 print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, null, $object->id, $object->tva_npr, $object->type, false, 1, 1);
2063 print '</td></tr>';
2064
2065 // Price base
2066 print '<tr><td class="fieldrequired">';
2067 print $langs->trans('PriceBase');
2068 print '</td>';
2069 print '<td>';
2070 print $form->selectPriceBaseType($object->price_base_type, "price_base_type");
2071 print '</td>';
2072 print '</tr>';
2073
2074 // Price
2075 print '<tr><td class="fieldrequired">';
2076 $text = $langs->trans('SellingPrice');
2077 print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", getDolGlobalString('MAIN_MAX_DECIMALS_UNIT')), 1, 'help');
2078 print '</td><td>';
2079 if ($object->price_base_type == 'TTC') {
2080 print '<input name="price" size="10" value="'.price($object->price_ttc).'">';
2081 } else {
2082 print '<input name="price" size="10" value="'.price($object->price).'">';
2083 }
2084 print '</td></tr>';
2085
2086 // Price minimum
2087 print '<tr><td>';
2088 $text = $langs->trans('MinPrice');
2089 print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", getDolGlobalString('MAIN_MAX_DECIMALS_UNIT')), 1, 'help');
2090 if ($object->price_base_type == 'TTC') {
2091 print '<td><input name="price_min" size="10" value="'.price($object->price_min_ttc).'">';
2092 } else {
2093 print '<td><input name="price_min" size="10" value="'.price($object->price_min).'">';
2094 }
2095 if (getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE')) {
2096 print '<td class="left">'.$langs->trans("MinimumRecommendedPrice", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
2097 }
2098 print '</td></tr>';
2099
2100 // Price Label
2101 print '<tr><td>';
2102 print $langs->trans('PriceLabel');
2103 print '</td><td>';
2104 print '<input name="price_label" maxlength="255" class="minwidth300 maxwidth400onsmartphone" value="'.$object->price_label.'">';
2105 print '</td>';
2106 print '</tr>';
2107
2108 // Discount
2109 $discount_percent = price2num(GETPOST("discount_percent"));
2110 print '<tr><td>'.$langs->trans("Discount").'</td><td>';
2111 print '<input name="discount_percent" size="10" value="'.price($discount_percent).'">';
2112 print '</td></tr>';
2113
2114 // Extrafields
2115 $extrafields->fetch_name_optionals_label("product_customer_price");
2116 $extralabels = !empty($extrafields->attributes["product_customer_price"]['label']) ? $extrafields->attributes["product_customer_price"]['label'] : '';
2117 $extrafield_values = $extrafields->getOptionalsFromPost("product_customer_price");
2118 if (!empty($extralabels)) {
2119 if (empty($prodcustprice->id)) {
2120 foreach ($extralabels as $key => $value) {
2121 if (!empty($extrafields->attributes["product_customer_price"]['list'][$key]) && ($extrafields->attributes["product_customer_price"]['list'][$key] == 1 || $extrafields->attributes["product_customer_price"]['list'][$key] == 3 || ($action == "add_customer_price" && $extrafields->attributes["product_customer_price"]['list'][$key] == 4))) {
2122 if (!empty($extrafields->attributes["product_customer_price"]['langfile'][$key])) {
2123 $langs->load($extrafields->attributes["product_customer_price"]['langfile'][$key]);
2124 }
2125
2126 print '<tr><td'.($extrafields->attributes["product_customer_price"]['required'][$key] ? ' class="fieldrequired"' : '').'>';
2127 if (!empty($extrafields->attributes["product_customer_price"]['help'][$key])) {
2128 print $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_customer_price"]['help'][$key]));
2129 } else {
2130 print $langs->trans($value);
2131 }
2132 print '</td><td>'.$extrafields->showInputField($key, GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : '', '', '', '', '', 0, 'product_customer_price').'</td></tr>';
2133 }
2134 }
2135 }
2136 }
2137
2138 print '</table>';
2139
2140 print '</div>';
2141
2142
2143 print '<div class="center">';
2144
2145 // Update all child soc
2146 print '<div class="marginbottomonly">';
2147 print '<input type="checkbox" name="updatechildprice" id="updatechildprice" value="1"> ';
2148 print '<label for="updatechildprice">'.$langs->trans('ForceUpdateChildPriceSoc').'</label>';
2149 print '</div>';
2150
2151 print $form->buttonsSaveCancel();
2152
2153 print '</form>';
2154 } elseif ($action == 'edit_customer_price') {
2155 // Edit mode
2156 $maxpricesupplier = $object->min_recommended_price();
2157
2158 print '<!-- edit_customer_price -->';
2159 print load_fiche_titre($langs->trans('PriceByCustomer'));
2160
2161 $result = $prodcustprice->fetch(GETPOSTINT('lineid'));
2162 if ($result < 0) {
2163 setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
2164 }
2165
2166 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
2167 print '<input type="hidden" name="token" value="'.newToken().'">';
2168 print '<input type="hidden" name="action" value="update_customer_price_confirm">';
2169 print '<input type="hidden" name="lineid" value="'.$prodcustprice->id.'">';
2170
2171 print '<table class="liste centpercent">';
2172 print '<tr>';
2173 print '<td class="titlefield fieldrequired">'.$langs->trans('ThirdParty').'</td>';
2174 $staticsoc = new Societe($db);
2175 $staticsoc->fetch($prodcustprice->fk_soc);
2176 print "<td>".$staticsoc->getNomUrl(1)."</td>";
2177 print '</tr>';
2178
2179 // Ref. Customer
2180 print '<tr><td>' . $langs->trans('RefCustomer') . '</td>';
2181 print '<td><input name="ref_customer" size="12" value="' . dol_escape_htmltag($prodcustprice->ref_customer) . '"></td></tr>';
2182
2183 // Applied Prices From
2184 print '<tr><td>'.$langs->trans("AppliedPricesFrom").'</td><td>';
2185 print $form->selectDate($prodcustprice->date_begin, "date_begin", 0, 0, 1, "date_begin");
2186 print '</td></tr>';
2187
2188 // Applied Prices To
2189 print '<tr><td>'.$langs->trans("AppliedPricesTo").'</td><td>';
2190 print $form->selectDate($prodcustprice->date_end, "date_end", 0, 0, 1, "date_end");
2191 print '</td></tr>';
2192
2193 // VAT
2194 print '<tr><td class="fieldrequired">'.$langs->trans("DefaultTaxRate").'</td><td>';
2195 print $form->load_tva("tva_tx", $prodcustprice->default_vat_code ? $prodcustprice->tva_tx.' ('.$prodcustprice->default_vat_code.')' : $prodcustprice->tva_tx, $mysoc, null, $object->id, $prodcustprice->recuperableonly, $object->type, false, 1, 1);
2196 print '</td></tr>';
2197
2198 // Price base
2199 print '<tr><td class="fieldrequired">';
2200 print $langs->trans('PriceBase');
2201 print '</td>';
2202 print '<td>';
2203 print $form->selectPriceBaseType($prodcustprice->price_base_type, "price_base_type");
2204 print '</td>';
2205 print '</tr>';
2206
2207 // Price
2208 print '<tr><td class="fieldrequired">';
2209 $text = $langs->trans('SellingPrice');
2210 print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", getDolGlobalString('MAIN_MAX_DECIMALS_UNIT')), 1, 'help');
2211 print '</td><td>';
2212 if ($prodcustprice->price_base_type == 'TTC') {
2213 print '<input name="price" size="10" value="'.price($prodcustprice->price_ttc).'">';
2214 } else {
2215 print '<input name="price" size="10" value="'.price($prodcustprice->price).'">';
2216 }
2217 print '</td></tr>';
2218
2219 // Price minimum
2220 print '<tr><td>';
2221 $text = $langs->trans('MinPrice');
2222 print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", getDolGlobalString('MAIN_MAX_DECIMALS_UNIT')), 1, 'help');
2223 print '</td><td>';
2224 if ($prodcustprice->price_base_type == 'TTC') {
2225 print '<input name="price_min" size="10" value="'.price($prodcustprice->price_min_ttc).'">';
2226 } else {
2227 print '<input name="price_min" size="10" value="'.price($prodcustprice->price_min).'">';
2228 }
2229 print '</td>';
2230 if (getDolGlobalString('PRODUCT_MINIMUM_RECOMMENDED_PRICE')) {
2231 print '<td class="left">'.$langs->trans("MinimumRecommendedPrice", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
2232 }
2233 print '</tr>';
2234
2235 // Price Label
2236 print '<tr><td>';
2237 print $langs->trans('PriceLabel');
2238 print '</td><td>';
2239 print '<input name="price_label" maxlength="255" class="minwidth300 maxwidth400onsmartphone" value="'.$prodcustprice->price_label.'">';
2240 print '</td>';
2241 print '</tr>';
2242
2243 // Discount
2244 print '<tr><td>'.$langs->trans("Discount").'</td><td>';
2245 print '<input name="discount_percent" size="10" value="'.price($prodcustprice->discount_percent).'">';
2246 print '</td></tr>';
2247
2248 // Extrafields
2249 $extrafields->fetch_name_optionals_label("product_customer_price");
2250 $extralabels = !empty($extrafields->attributes["product_customer_price"]['label']) ? $extrafields->attributes["product_customer_price"]['label'] : '';
2251 $extrafield_values = $extrafields->getOptionalsFromPost("product_customer_price");
2252 if (!empty($extralabels)) {
2253 if (empty($object->id)) {
2254 foreach ($extralabels as $key => $value) {
2255 if (!empty($extrafields->attributes["product_customer_price"]['list'][$key]) && ($extrafields->attributes["product_customer_price"]['list'][$key] == 1 || $extrafields->attributes["product_customer_price"]['list'][$key] == 3 || ($action == "edit_customer_price" && $extrafields->attributes["product_customer_price"]['list'][$key] == 4))) {
2256 if (!empty($extrafields->attributes["product_customer_price"]['langfile'][$key])) {
2257 $langs->load($extrafields->attributes["product_customer_price"]['langfile'][$key]);
2258 }
2259
2260 print '<tr><td'.($extrafields->attributes["product_customer_price"]['required'][$key] ? ' class="fieldrequired"' : '').'>';
2261 if (!empty($extrafields->attributes["product_customer_price"]['help'][$key])) {
2262 print $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_customer_price"]['help'][$key]));
2263 } else {
2264 print $langs->trans($value);
2265 }
2266 print '</td><td>'.$extrafields->showInputField($key, GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : '', '', '', '', '', 0, 'product_customer_price').'</td></tr>';
2267 }
2268 }
2269 } else {
2270 $sql = "SELECT";
2271 $sql .= " fk_object";
2272 foreach ($extralabels as $key => $value) {
2273 $sql .= ", ".$db->sanitize($key);
2274 }
2275 $sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price_extrafields";
2276 $sql .= " WHERE fk_object = ".((int) $prodcustprice->id);
2277 $resql = $db->query($sql);
2278 if ($resql) {
2279 $obj = $db->fetch_object($resql);
2280 foreach ($extralabels as $key => $value) {
2281 if (!empty($extrafields->attributes["product_customer_price"]['list'][$key]) && ($extrafields->attributes["product_customer_price"]['list'][$key] == 1 || $extrafields->attributes["product_customer_price"]['list'][$key] == 3 || ($action == "edit_customer_price" && $extrafields->attributes["product_customer_price"]['list'][$key] == 4))) {
2282 if (!empty($extrafields->attributes["product_customer_price"]['langfile'][$key])) {
2283 $langs->load($extrafields->attributes["product_customer_price"]['langfile'][$key]);
2284 }
2285
2286 print '<tr><td'.($extrafields->attributes["product_customer_price"]['required'][$key] ? ' class="fieldrequired"' : '').'>';
2287 if (!empty($extrafields->attributes["product_customer_price"]['help'][$key])) {
2288 print $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_customer_price"]['help'][$key]));
2289 } else {
2290 print $langs->trans($value);
2291 }
2292 print '</td><td>'.$extrafields->showInputField($key, GETPOSTISSET('options_'.$key) ? $extrafield_values['options_'.$key] : $obj->{$key}, '', '', '', '', 0, 'product_customer_price');
2293
2294 print '</td></tr>';
2295 }
2296 }
2297 $db->free($resql);
2298 }
2299 }
2300 }
2301
2302 print '</table>';
2303
2304 print '<div class="center">';
2305 print '<div class="marginbottomonly">';
2306 print '<input type="checkbox" name="updatechildprice" id="updatechildprice" value="1"> ';
2307 print '<label for="updatechildprice">'.$langs->trans('ForceUpdateChildPriceSoc').'</label>';
2308 print "</div>";
2309
2310 print $form->buttonsSaveCancel();
2311
2312 print '<br></form>';
2313 } elseif ($action == 'showlog_customer_price') {
2314 // List of all log of prices by customers
2315 print '<!-- list of all log of prices per customer -->'."\n";
2316
2317 $sortfield = 't.datec';
2318 $filter = array('t.fk_product' => (string) $object->id, 't.fk_soc' => (string) GETPOSTINT('socid'));
2319
2320 // Count total nb of records
2321 $nbtotalofrecords = '';
2322 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
2323 $nbtotalofrecords = $prodcustprice->fetchAllLog($sortorder, $sortfield, $conf->liste_limit, $offset, $filter);
2324 }
2325
2326 $result = $prodcustprice->fetchAllLog($sortorder, $sortfield, $conf->liste_limit, $offset, $filter);
2327 if ($result < 0) {
2328 setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
2329 }
2330
2331 $option = '&socid='.GETPOSTINT('socid').'&id='.$object->id;
2332
2333 $staticsoc = new Societe($db);
2334 $staticsoc->fetch(GETPOSTINT('socid'));
2335
2336 $title = $langs->trans('PriceByCustomerLog');
2337 $title .= ' - '.$staticsoc->getNomUrl(1);
2338
2339 $backbutton = '<a class="justalink" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">'.$langs->trans("GoBack").'</a>';
2340
2341 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
2342 print_barre_liste($title, $page, $_SERVER['PHP_SELF'], $option, $sortfield, $sortorder, $backbutton, count($prodcustprice->lines), $nbtotalofrecords, 'title_accountancy.png');
2343
2344 if (count($prodcustprice->lines) > 0) {
2345 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
2346 print '<input type="hidden" name="token" value="'.newToken().'">';
2347 print '<input type="hidden" name="id" value="'.$object->id.'">';
2348
2349 print '<div class="div-table-responsive-no-min">';
2350 print '<table class="liste centpercent">';
2351
2352 print '<tr class="liste_titre">';
2353 print '<td>'.$langs->trans("ThirdParty").'</td>';
2354 print '<td>'.$langs->trans('RefCustomer').'</td>';
2355 print '<td>'.$langs->trans("AppliedPricesFrom").'</td>';
2356 print '<td>'.$langs->trans("AppliedPricesTo").'</td>';
2357 print '<td class="center">'.$langs->trans("PriceBase").'</td>';
2358 print '<td class="right">'.$langs->trans("DefaultTaxRate").'</td>';
2359 print '<td class="right">'.$langs->trans("HT").'</td>';
2360 print '<td class="right">'.$langs->trans("TTC").'</td>';
2361 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2362 print '<td class="right">'.$langs->trans("INCT").'</td>';
2363 }
2364 print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("HT").'</td>';
2365 print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("TTC").'</td>';
2366 print '<td class="right">'.$langs->trans("PriceLabel").'</td>';
2367 print '<td class="right">'.$langs->trans("Discount").'</td>';
2368 print '<td class="right">'.$langs->trans("ChangedBy").'</td>';
2369 print '<td>'.$langs->trans("DateCreation").'</td>';
2370 print '</tr>';
2371
2372 foreach ($prodcustprice->lines as $line) {
2373 // Date
2374 $staticsoc = new Societe($db);
2375 $staticsoc->fetch($line->fk_soc);
2376
2377 $tva_tx = $line->default_vat_code ? $line->tva_tx.' ('.$line->default_vat_code.')' : $line->tva_tx;
2378
2379 // Line for default price
2380 if ($line->price_base_type == 'HT') {
2381 $pu = $line->price;
2382 } else {
2383 $pu = $line->price_ttc;
2384 }
2385
2386 // Local tax is not saved into table of product. We use value linked to VAT code.
2387 $localtaxarray = getLocalTaxesFromRate($line->tva_tx.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), 0, $staticsoc, $mysoc);
2388 // Define part of HT, VAT, TTC
2389 $resultarray = calcul_price_total(1, $pu, 0, (float) $line->tva_tx, 1, 1, 0, $line->price_base_type, $line->recuperableonly, $object->type, $mysoc, $localtaxarray);
2390 // Calcul du total ht sans remise
2391 $total_ht = $resultarray[0];
2392 $total_vat = $resultarray[1];
2393 $total_localtax1 = $resultarray[9];
2394 $total_localtax2 = $resultarray[10];
2395 $total_ttc = $resultarray[2];
2396
2397 print '<tr class="oddeven">';
2398
2399 print '<td class="tdoverflowmax125">'.$staticsoc->getNomUrl(1)."</td>";
2400 print '<td>'.$line->ref_customer.'</td>';
2401 print "<td>".dol_print_date($line->date_begin, "day", 'tzuserrel')."</td>";
2402 print "<td>".dol_print_date($line->date_end, "day", 'tzuserrel')."</td>";
2403 print '<td class="center">'.$langs->trans($line->price_base_type)."</td>";
2404 print '<td class="right nowraponall">';
2405
2406 $positiverates = '';
2407 if (price2num($line->tva_tx)) {
2408 $positiverates .= ($positiverates ? '/' : '').price2num($line->tva_tx);
2409 }
2410 if (price2num($line->localtax1_type)) {
2411 $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax1_tx);
2412 }
2413 if (price2num($line->localtax2_type)) {
2414 $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax2_tx);
2415 }
2416 if (empty($positiverates)) {
2417 $positiverates = '0';
2418 }
2419
2420 echo vatrate($positiverates.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), true, $line->recuperableonly);
2421
2422 //. vatrate($tva_tx, true, $line->recuperableonly) .
2423 print "</td>";
2424 print '<td class="right"><span class="amount">'.price($line->price)."</span></td>";
2425
2426 print '<td class="right"><span class="amount">'.price($line->price_ttc)."</span></td>";
2427 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2428 print '<td class="right">'.price($resultarray[2]).'</td>';
2429 }
2430
2431 print '<td class="right">'.price($line->price_min).'</td>';
2432 print '<td class="right">'.price($line->price_min_ttc).'</td>';
2433 print '<td class="right">'.dolPrintHTML($line->price_label).'</td>';
2434 print '<td class="right">'.price($line->discount_percent).'</td>';
2435
2436 // User
2437 print '<td class="tdoverflowmax125">';
2438 // @TODO Add a cache on $userstatic
2439 if ($line->fk_user > 0) {
2440 $userstatic = new User($db);
2441 $userstatic->fetch($line->fk_user);
2442 print $userstatic->getNomUrl(1, '', 0, 0, 24, 0, 'login');
2443 }
2444 print '</td>';
2445
2446 print "<td>".dol_print_date($line->datec, "dayhour", 'tzuserrel')."</td>";
2447 print '</tr>';
2448 }
2449 print "</table>";
2450 print '</div>';
2451 } else {
2452 print $langs->trans('None');
2453 }
2454 } elseif ($action != 'showlog_default_price' && $action != 'edit_price' && $action != 'edit_level_price') {
2455 // List of all prices by customers
2456 print '<!-- list of all prices per customer -->'."\n";
2457
2458 // Count total nb of records
2459 $nbtotalofrecords = '';
2460 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
2461 $nbtotalofrecords = $prodcustprice->fetchAll($sortorder, $sortfield, 0, 0, $filter);
2462 }
2463
2464 $result = $prodcustprice->fetchAll($sortorder, $sortfield, $conf->liste_limit, $offset, $filter);
2465 if ($result < 0) {
2466 setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
2467 }
2468
2469 $option = '&search_soc='.$search_soc.'&id='.$object->id;
2470
2471 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
2472 print_barre_liste($langs->trans('PriceByCustomer'), $page, $_SERVER ['PHP_SELF'], $option, $sortfield, $sortorder, '', count($prodcustprice->lines), $nbtotalofrecords, 'title_accountancy.png');
2473
2474 print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
2475 print '<input type="hidden" name="token" value="'.newToken().'">';
2476 print '<input type="hidden" name="id" value="'.$object->id.'">';
2477
2478 print '<!-- List of prices per customer -->'."\n";
2479 print '<div class="div-table-responsive-no-min">'."\n";
2480 print '<table class="liste centpercent">'."\n";
2481
2482 if (count($prodcustprice->lines) > 0 || $search_soc) {
2483 $extrafields->fetch_name_optionals_label("product_customer_price");
2484 $custom_price_extralabels = !empty($extrafields->attributes["product_customer_price"]['label']) ? $extrafields->attributes["product_customer_price"]['label'] : '';
2485 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
2486 $colspan = 11;
2487 } else {
2488 $colspan = 12;
2489 }
2490 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2491 $colspan++;
2492 }
2493 if (!empty($custom_price_extralabels) && is_array($custom_price_extralabels)) {
2494 $colspan += count($custom_price_extralabels);
2495 }
2496
2497 print '<tr class="liste_titre">';
2498 print '<td class="liste_titre"><input type="text" class="flat maxwidth125" name="search_soc" value="'.$search_soc.'"></td>';
2499 print '<td class="liste_titre" colspan="'.$colspan.'">&nbsp;</td>';
2500 // Print the search button
2501 print '<td class="liste_titre maxwidthsearch">';
2502 $searchpicto = $form->showFilterAndCheckAddButtons(0);
2503 print $searchpicto;
2504 print '</td>';
2505 print '</tr>';
2506 }
2507
2508 print '<tr class="liste_titre">';
2509 print '<td>'.$langs->trans("ThirdParty").'</td>';
2510 print '<td>'.$langs->trans('RefCustomer').'</td>';
2511 print '<td>'.$langs->trans("AppliedPricesFrom").'</td>';
2512 print '<td>'.$langs->trans("AppliedPricesTo").'</td>';
2513 print '<td class="center">'.$langs->trans("PriceBase").'</td>';
2514 print '<td class="right">'.$langs->trans("DefaultTaxRate").'</td>';
2515 print '<td class="right">'.$langs->trans("HT").'</td>';
2516 print '<td class="right">'.$langs->trans("TTC").'</td>';
2517 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2518 print '<td class="right">'.$langs->trans("INCT").'</td>';
2519 }
2520 print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("HT").'</td>';
2521 print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("TTC").'</td>';
2522 print '<td class="right">'.$langs->trans("PriceLabel").'</td>';
2523 print '<td class="right">'.$langs->trans("Discount").'</td>';
2524 // fetch optionals attributes and labels
2525 $extrafields->fetch_name_optionals_label("product_customer_price");
2526 if ($extrafields->attributes["product_customer_price"] && array_key_exists('label', $extrafields->attributes["product_customer_price"])) {
2527 $extralabels = $extrafields->attributes["product_customer_price"]['label'];
2528 if (!empty($extralabels)) {
2529 foreach ($extralabels as $key => $value) {
2530 // Show field if not hidden
2531 if (!empty($extrafields->attributes["product_customer_price"]['list'][$key]) && $extrafields->attributes["product_customer_price"]['list'][$key] != 3) {
2532 if (!empty($extrafields->attributes["product_customer_price"]['langfile'][$key])) {
2533 $langs->load($extrafields->attributes["product_customer_price"]['langfile'][$key]);
2534 }
2535 if (!empty($extrafields->attributes["product_customer_price"]['help'][$key])) {
2536 $extratitle = $form->textwithpicto($langs->trans($value), $langs->trans($extrafields->attributes["product_customer_price"]['help'][$key]));
2537 } else {
2538 $extratitle = $langs->trans($value);
2539 }
2540 print '<td style="text-align: right">'.$extratitle.'</td>';
2541 }
2542 }
2543 }
2544 }
2545 print '<td>'.$langs->trans("ChangedBy").'</td>';
2546 print '<td></td>';
2547 print '</tr>';
2548
2549 // Line for default price
2550 if ($object->price_base_type == 'HT') {
2551 $pu = $object->price;
2552 } else {
2553 $pu = $object->price_ttc;
2554 }
2555
2556 // Local tax was not saved into table llx_product on old versions. So we will use the value linked to the VAT code.
2557 $localtaxarray = getLocalTaxesFromRate($object->tva_tx.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), 0, $mysoc, $mysoc);
2558 // Define part of HT, VAT, TTC
2559 $resultarray = calcul_price_total(1, $pu, 0, $object->tva_tx, 1, 1, 0, $object->price_base_type, 0, $object->type, $mysoc, $localtaxarray);
2560 // Calcul du total ht sans remise
2561 $total_ht = $resultarray[0];
2562 $total_vat = $resultarray[1];
2563 $total_localtax1 = $resultarray[9];
2564 $total_localtax2 = $resultarray[10];
2565 $total_ttc = $resultarray[2];
2566
2567 if (!getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
2568 print '<!-- PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES -->'."\n";
2569 print '<tr class="oddeven">';
2570 print '<td colspan="4">' . $langs->trans('Default') . '</td>';
2571
2572 print '<td class="center">'.$langs->trans($object->price_base_type)."</td>";
2573
2574 // VAT Rate
2575 print '<td class="right nowraponall">';
2576
2577 $positiverates = '';
2578 if (price2num($object->tva_tx)) {
2579 $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
2580 }
2581 if (price2num($object->localtax1_type)) {
2582 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
2583 }
2584 if (price2num($object->localtax2_type)) {
2585 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
2586 }
2587 if (empty($positiverates)) {
2588 $positiverates = '0';
2589 }
2590 echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), true, $object->tva_npr);
2591
2592 print "</td>";
2593
2594 print '<td class="right"><span class="amount">'.price($object->price)."</span></td>";
2595
2596 print '<td class="right"><span class="amount">'.price($object->price_ttc)."</span></td>";
2597 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2598 //print '<td class="right">' . price($object->price_ttc) . "</td>";
2599 print '<td class="right"><span class="amount">'.price($resultarray[2]).'</span></td>';
2600 }
2601
2602 print '<td class="right">'.price($object->price_min).'</td>';
2603 print '<td class="right">'.price($object->price_min_ttc).'</td>';
2604 print '<td class="right">'.dolPrintHTML($object->price_label).'</td>';
2605 print '<td class="right"></td>';
2606 print '<td class="right"></td>';
2607 if (!empty($extralabels)) {
2608 foreach ($extralabels as $key) {
2609 // Show field if not hidden
2610 if (!empty($extrafields->attributes["product_customer_price"]['list'][$key]) && $extrafields->attributes["product_customer_price"]['list'][$key] != 3) {
2611 print '<td class="right"></td>';
2612 }
2613 }
2614 }
2615 if ($user->hasRight('produit', 'supprimer') || $user->hasRight('service', 'supprimer')) {
2616 print '<td class="nowraponall">';
2617 print '<a class="marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?action=showlog_default_price&token='.newToken().'&id='.$object->id.'">';
2618 print img_info($langs->trans('PriceByCustomerLog'));
2619 print '</a>';
2620 print ' ';
2621 print '<a class="marginleftonly marginrightonly editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit_price&token='.newToken().'&id='.$object->id.'">';
2622 print img_edit('default', 0, 'style="vertical-align: middle;"');
2623 print '</a>';
2624 print '</td>';
2625 }
2626 print "</tr>\n";
2627 }
2628
2629 if (count($prodcustprice->lines) > 0) {
2630 foreach ($prodcustprice->lines as $line) {
2631 // Date
2632 $staticsoc = new Societe($db);
2633 $staticsoc->fetch($line->fk_soc);
2634
2635 $tva_tx = $line->default_vat_code ? $line->tva_tx.' ('.$line->default_vat_code.')' : $line->tva_tx;
2636
2637 // Line for default price
2638 if ($line->price_base_type == 'HT') {
2639 $pu = $line->price;
2640 } else {
2641 $pu = $line->price_ttc;
2642 }
2643
2644 // Local tax is not saved into table of product. We use value linked to VAT code.
2645 $localtaxarray = getLocalTaxesFromRate($line->tva_tx.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), 0, $staticsoc, $mysoc);
2646 // Define part of HT, VAT, TTC
2647 $resultarray = calcul_price_total(1, $pu, 0, (float) $line->tva_tx, 1, 1, 0, $line->price_base_type, $line->recuperableonly, $object->type, $mysoc, $localtaxarray);
2648 // Calcul du total ht sans remise
2649 $total_ht = $resultarray[0];
2650 $total_vat = $resultarray[1];
2651 $total_localtax1 = $resultarray[9];
2652 $total_localtax2 = $resultarray[10];
2653 $total_ttc = $resultarray[2];
2654
2655 print '<tr class="oddeven">';
2656
2657 print '<td class="tdoverflowmax125">'.$staticsoc->getNomUrl(1)."</td>";
2658 print '<td>'.dolPrintHTML($line->ref_customer).'</td>';
2659 print "<td>".dol_print_date($line->date_begin, "day", 'tzuserrel')."</td>";
2660 print "<td>".dol_print_date($line->date_end, "day", 'tzuserrel')."</td>";
2661 print '<td class="center">'.$langs->trans($line->price_base_type)."</td>";
2662 // VAT Rate
2663 print '<td class="right nowraponall">';
2664
2665 $positiverates = '';
2666 if (price2num($line->tva_tx)) {
2667 $positiverates .= ($positiverates ? '/' : '').price2num($line->tva_tx);
2668 }
2669 if (price2num($line->localtax1_type)) {
2670 $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax1_tx);
2671 }
2672 if (price2num($line->localtax2_type)) {
2673 $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax2_tx);
2674 }
2675 if (empty($positiverates)) {
2676 $positiverates = '0';
2677 }
2678
2679 echo vatrate($positiverates.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), true, $line->recuperableonly);
2680
2681 print "</td>";
2682
2683 print '<td class="right"><span class="amount">'.price($line->price)."</span></td>";
2684
2685 print '<td class="right"><span class="amount">'.price($line->price_ttc)."</span></td>";
2686 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2687 //print '<td class="right">' . price($line->price_ttc) . "</td>";
2688 print '<td class="right"><span class="amount">'.price($resultarray[2]).'</span></td>';
2689 }
2690
2691 print '<td class="right">'.price($line->price_min).'</td>';
2692 print '<td class="right">'.price($line->price_min_ttc).'</td>';
2693 print '<td class="right">'.dolPrintHTMLForAttribute($line->price_label).'</td>';
2694 print '<td class="right">'.price($line->discount_percent).'</td>';
2695
2696 // Extrafields
2697 $extrafields->fetch_name_optionals_label("product_customer_price");
2698 $extralabels = $extrafields->attributes["product_customer_price"]['label'] ?? array();
2699 if (!empty($extralabels)) {
2700 $sql = "SELECT";
2701 $sql .= " fk_object";
2702 foreach ($extralabels as $key => $value) {
2703 $sql .= ", ".$db->sanitize($key);
2704 }
2705 $sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price_extrafields";
2706 $sql .= " WHERE fk_object = ".((int) $line->id);
2707 $resql = $db->query($sql);
2708 if ($resql) {
2709 // Row may not exist yet (e.g. if the only extrafield configured is a computed one, no row is ever inserted)
2710 $obj = ($db->num_rows($resql) == 1) ? $db->fetch_object($resql) : null;
2711 foreach ($extralabels as $key => $value) {
2712 if (!empty($extrafields->attributes["product_customer_price"]['list'][$key]) && $extrafields->attributes["product_customer_price"]['list'][$key] != 3) {
2713 // If field is a computed field, we make computation to get value, whether or not a stored row exists
2714 if (!empty($extrafields->attributes["product_customer_price"]['computed'][$key])) {
2715 $objectoffield = $line; // For compatibility with the computed formula. $objectoffield is exported by dol_eval().
2716 $extravalue = dol_eval((string) $extrafields->attributes["product_customer_price"]['computed'][$key], 1, 1, '2');
2717 print '<td align="right">'.$extrafields->showOutputField($key, $extravalue, '', 'product_customer_price')."</td>";
2718 } elseif ($obj) {
2719 print '<td align="right">'.$extrafields->showOutputField($key, $obj->{$key}, '', 'product_customer_price')."</td>";
2720 } else {
2721 print "<td></td>";
2722 }
2723 }
2724 }
2725 $db->free($resql);
2726 }
2727 }
2728
2729 // User
2730 print '<td class="tdoverflowmax125">';
2731 if ($line->fk_user > 0) {
2732 $userstatic = new User($db);
2733 $userstatic->fetch($line->fk_user);
2734 // @TODO Add a cache on $userstatic object
2735 print $userstatic->getNomUrl(-1, '', 0, 0, 24, 0, 'login');
2736 }
2737 print '</td>';
2738
2739 // Todo Edit or delete button
2740 // Action
2741 print '<td class="right nowraponall">';
2742 if ($user->hasRight('produit', 'creer') || $user->hasRight('service', 'creer')) {
2743 print '<a href="'.$_SERVER["PHP_SELF"].'?action=showlog_customer_price&token='.newToken().'&id='.$object->id.'&socid='.$line->fk_soc.'">';
2744 print img_info($langs->trans('PriceByCustomerLog'));
2745 print '</a>';
2746 print ' ';
2747 print '<a class="marginleftonly editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit_customer_price&token='.newToken().'&id='.$object->id.'&lineid='.$line->id.'">';
2748 print img_edit('default', 0, 'style="vertical-align: middle;"');
2749 print '</a>';
2750 print ' ';
2751 }
2752 if ($user->hasRight('produit', 'supprimer') || $user->hasRight('service', 'supprimer')) {
2753 print '<a class="marginleftonly" href="'.$_SERVER["PHP_SELF"].'?action=ask_remove_customer_price&token='.newToken().'&id='.$object->id.'&lineid='.$line->id.'">';
2754 print img_delete('default', 'style="vertical-align: middle;"');
2755 print '</a>';
2756 }
2757 print '</td>';
2758
2759 print "</tr>\n";
2760 }
2761 }
2762
2763 print "</table>";
2764 print '</div>';
2765
2766 print "</form>";
2767 }
2768}
2769
2770// List of price changes - log historic (ordered by descending date)
2771
2772if ((!getDolGlobalString('PRODUIT_CUSTOMER_PRICES') || $action == 'showlog_default_price') && !in_array($action, array('edit_price', 'edit_level_price', 'edit_vat'))) {
2773 $sql = "SELECT p.rowid, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.default_vat_code, p.recuperableonly, p.localtax1_tx, p.localtax1_type, p.localtax2_tx, p.localtax2_type,";
2774 $sql .= " p.price_level, p.price_min, p.price_min_ttc,p.price_by_qty,";
2775 $sql .= " p.date_price as dp, p.fk_price_expression, u.rowid as user_id, u.login";
2776 $sql .= " ,p.price_label";
2777 $sql .= " FROM ".MAIN_DB_PREFIX."product_price as p,";
2778 $sql .= " ".MAIN_DB_PREFIX."user as u";
2779 $sql .= " WHERE fk_product = ".((int) $object->id);
2780 $sql .= " AND p.entity IN (".getEntity('productprice').")";
2781 $sql .= " AND p.fk_user_author = u.rowid";
2782 if (!empty($socid) && (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) && $soc !== null) {
2783 $sql .= " AND p.price_level = ".((int) $soc->price_level);
2784 }
2785 $sql .= " ORDER BY p.date_price DESC, p.rowid DESC, p.price_level ASC";
2786 // $sql .= $db->plimit();
2787 //print $sql;
2788
2789 $result = $db->query($sql);
2790 if ($result) {
2791 print '<div class="divlogofpreviouscustomerprice">';
2792
2793 $num = $db->num_rows($result);
2794
2795 if (!$num) {
2796 $db->free($result);
2797
2798 // We must have at least one initial line
2799 // We add it to fix this if not (trouble with old versions)
2800 // We emulate the change of the price from interface with the same value than the one into table llx_product
2801 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
2802 $ret = $object->updatePrice(($object->multiprices_base_type[1] == 'TTC' ? $object->multiprices_ttc[1] : $object->multiprices[1]), $object->multiprices_base_type[1], $user, (empty($object->multiprices_tva_tx[1]) ? 0 : $object->multiprices_tva_tx[1]), ($object->multiprices_base_type[1] == 'TTC' ? $object->multiprices_min_ttc[1] : $object->multiprices_min[1]), 1, 0, 0, 0, array(), $object->default_vat_code);
2803 } else {
2804 $ret = $object->updatePrice(($object->price_base_type == 'TTC' ? $object->price_ttc : $object->price), $object->price_base_type, $user, $object->tva_tx, ($object->price_base_type == 'TTC' ? $object->price_min_ttc : $object->price_min), 0, $object->tva_npr, 0, 0, array(), $object->default_vat_code);
2805 }
2806
2807 if ($ret < 0) {
2808 dol_print_error($db, $object->error, $object->errors);
2809 } else {
2810 $result = $db->query($sql);
2811 $num = $db->num_rows($result);
2812 }
2813 }
2814
2815 if ($num > 0) {
2816 // Default prices or
2817 // Log of previous customer prices
2818 $backbutton = '<a class="justalink" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">'.$langs->trans("GoBack").'</a>';
2819
2820 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES')) {
2821 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition, PhanPluginSuspiciousParamOrder
2822 print_barre_liste($langs->trans("DefaultPriceLog"), 0, $_SERVER["PHP_SELF"], '', '', '', $backbutton, 0, $num, 'title_accountancy.png');
2823 } else {
2824 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition, PhanPluginSuspiciousParamOrder
2825 print_barre_liste($langs->trans("PriceByCustomerLog"), 0, $_SERVER["PHP_SELF"], '', '', '', '', 0, $num, 'title_accountancy.png');
2826 }
2827
2828 print '<!-- List of log prices -->'."\n";
2829 print '<div class="div-table-responsive">'."\n";
2830 print '<table class="liste centpercent noborder">'."\n";
2831
2832 print '<tr class="liste_titre">';
2833 print '<th>'.$langs->trans("AppliedPricesFrom").'</tg>';
2834
2835 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
2836 print '<th class="center">'.$langs->trans("PriceLevel").'</th>';
2837 }
2838 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
2839 print '<th class="center">'.$langs->trans("Type").'</th>';
2840 }
2841
2842 print '<th class="center">'.$langs->trans("PriceBase").'</th>';
2843 if (!getDolGlobalString('PRODUIT_MULTIPRICES') && !getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
2844 print '<th class="right">'.$langs->trans("DefaultTaxRate").'</th>';
2845 }
2846 print '<th class="right">'.$langs->trans("HT").'</th>';
2847 print '<th class="right">'.$langs->trans("TTC").'</th>';
2848 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2849 print '<th class="right">'.$langs->trans("INCT").'</th>';
2850 }
2851 if (isModEnabled('dynamicprices')) {
2852 print '<th class="right">'.$langs->trans("PriceExpressionSelected").'</th>';
2853 }
2854 print '<th class="right">'.$langs->trans("MinPrice").' '.$langs->trans("HT").'</th>';
2855 print '<th class="right">'.$langs->trans("MinPrice").' '.$langs->trans("TTC").'</th>';
2856 print '<th class="right">'.$langs->trans("Label").'</th>';
2857 print '<th>'.$langs->trans("ChangedBy").'</th>';
2858 if ($user->hasRight('produit', 'supprimer')) {
2859 print '<th class="right">&nbsp;</th>';
2860 }
2861 print '</tr>';
2862
2863 $notfirstlineforlevel = array();
2864
2865 $i = 0;
2866 while ($i < $num) {
2867 $objp = $db->fetch_object($result);
2868
2869 print '<tr class="oddeven">';
2870 // Date
2871 print "<td>".dol_print_date($db->jdate($objp->dp), "dayhour", 'tzuserrel')."</td>";
2872
2873 // Price level
2874 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
2875 print '<td class="center">'.$objp->price_level."</td>";
2876 }
2877 // Price by quantity
2878 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
2879 $type = ($objp->price_by_qty == 1) ? 'PriceByQuantity' : 'Standard';
2880 print '<td class="center">'.$langs->trans($type)."</td>";
2881 }
2882
2883 print '<td class="center">';
2884 if (empty($objp->price_by_qty)) {
2885 print $langs->trans($objp->price_base_type);
2886 }
2887 print "</td>";
2888
2889 if (!getDolGlobalString('PRODUIT_MULTIPRICES') && !getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
2890 print '<td class="right">';
2891
2892 if (empty($objp->price_by_qty)) {
2893 $positiverates = '';
2894 if (price2num($objp->tva_tx)) {
2895 $positiverates .= ($positiverates ? '/' : '').price2num($objp->tva_tx);
2896 }
2897 if (price2num($objp->localtax1_type)) {
2898 $positiverates .= ($positiverates ? '/' : '').price2num($objp->localtax1_tx);
2899 }
2900 if (price2num($objp->localtax2_type)) {
2901 $positiverates .= ($positiverates ? '/' : '').price2num($objp->localtax2_tx);
2902 }
2903 if (empty($positiverates)) {
2904 $positiverates = '0';
2905 }
2906 echo vatrate($positiverates.($objp->default_vat_code ? ' ('.$objp->default_vat_code.')' : ''), true, !empty($objp->tva_npr) ? $objp->tva_npr : 0);
2907 /*
2908 if ($objp->default_vat_code)
2909 {
2910 print vatrate($objp->tva_tx, true) . ' ('.$objp->default_vat_code.')';
2911 }
2912 else print vatrate($objp->tva_tx, true, $objp->recuperableonly);*/
2913 }
2914
2915 print "</td>";
2916 }
2917
2918 // Line for default price
2919 if ($objp->price_base_type == 'HT') {
2920 $pu = $objp->price;
2921 } else {
2922 $pu = $objp->price_ttc;
2923 }
2924
2925 // Local tax was not saved into table llx_product on old version. So we will use value linked to VAT code.
2926 $localtaxarray = getLocalTaxesFromRate($objp->tva_tx.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), 0, $mysoc, $mysoc);
2927 // Define part of HT, VAT, TTC
2928 $resultarray = calcul_price_total(1, $pu, 0, $objp->tva_tx, 1, 1, 0, $objp->price_base_type, $objp->recuperableonly, $object->type, $mysoc, $localtaxarray);
2929 // Calcul du total ht sans remise
2930 $total_ht = $resultarray[0];
2931 $total_vat = $resultarray[1];
2932 $total_localtax1 = $resultarray[9];
2933 $total_localtax2 = $resultarray[10];
2934 $total_ttc = $resultarray[2];
2935
2936 // Price
2937 if (!empty($objp->fk_price_expression) && !empty($conf->dynamicprices->enabled)) {
2938 $price_expression = new PriceExpression($db);
2939 $res = $price_expression->fetch($objp->fk_price_expression);
2940 $title = $price_expression->title;
2941 print '<td class="right"></td>';
2942 print '<td class="right"></td>';
2943 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2944 print '<td class="right"></td>';
2945 }
2946 print '<td class="right">'.$title."</td>";
2947 } else {
2948 // Price HT
2949 print '<td class="right">';
2950 if (empty($objp->price_by_qty)) {
2951 print '<span class="amount">'.price($objp->price).'</span>';
2952 }
2953 print "</td>";
2954 // Price TTC
2955 print '<td class="right">';
2956 if (empty($objp->price_by_qty)) {
2957 $price_ttc = $objp->price_ttc;
2958 print '<span class="amount">'.price($price_ttc).'<span>';
2959 }
2960 print "</td>";
2961 if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2962 print '<td class="right">';
2963 print $resultarray[2];
2964 print '</td>';
2965 }
2966 if (isModEnabled('dynamicprices')) { // Only if module is enabled
2967 print '<td class="right"></td>';
2968 }
2969 }
2970
2971 // Price min
2972 print '<td class="right">';
2973 if (empty($objp->price_by_qty)) {
2974 print price($objp->price_min);
2975 }
2976 print '</td>';
2977
2978 // Price min inc tax
2979 print '<td class="right">';
2980 if (empty($objp->price_by_qty)) {
2981 $price_min_ttc = $objp->price_min_ttc;
2982 print price($price_min_ttc);
2983 }
2984 print '</td>';
2985
2986 // Price Label
2987 print '<td>';
2988 print dolPrintHTML($objp->price_label);
2989 print '</td>';
2990
2991 // User
2992 print '<td class="tdoverflowmax125">';
2993 if ($objp->user_id > 0) {
2994 // @TODO Add a cache on $userstatic
2995 $userstatic = new User($db);
2996 $userstatic->fetch($objp->user_id);
2997 print $userstatic->getNomUrl(-1, '', 0, 0, 24, 0, 'login');
2998 }
2999 print '</td>';
3000
3001 // Action
3002 if ($user->hasRight('produit', 'supprimer')) {
3003 $candelete = 0;
3004 if (getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
3005 if (empty($notfirstlineforlevel[$objp->price_level])) {
3006 $notfirstlineforlevel[$objp->price_level] = 1;
3007 } else {
3008 $candelete = 1;
3009 }
3010 } elseif ($i > 0) {
3011 $candelete = 1;
3012 }
3013
3014 print '<td class="right">';
3015 if ($candelete || ($db->jdate($objp->dp) >= dol_now())) { // Test on date is to be able to delete a corrupted record with a date in future
3016 print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&id='.$object->id.'&lineid='.$objp->rowid.'">';
3017 print img_delete();
3018 print '</a>';
3019 } else {
3020 print '&nbsp;'; // Can not delete last price (it's current price)
3021 }
3022 print '</td>';
3023 }
3024
3025 print "</tr>\n";
3026 $i++;
3027 }
3028
3029 $db->free($result);
3030 print "</table>";
3031 print '</div>';
3032 print "<br>";
3033 }
3034
3035 print '</div>';
3036 } else {
3037 dol_print_error($db);
3038 }
3039}
3040
3041// End of page
3042llxFooter();
3043$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:281
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage standard extra fields.
Class to manage generation of HTML components Only common components must be here.
Class of a generic business object.
Class for accessing price expression table.
Class to parse product price expressions.
File of class to manage predefined price products or services by customer.
Class to manage products or services.
const TYPE_PRODUCT
Regular product.
const TYPE_SERVICE
Service.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
global $mysoc
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0, $html=0)
Return a string with VAT rate label formatted for view output Used into pdf and HTML pages.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dol_eval($s, $returnvalue=1, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
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.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
get_localtax($vatrate, $local, $thirdparty_buyer=null, $thirdparty_seller=null, $vatnpr=0)
Return localtax rate for a particular VAT rate, when selling a product with vat $vatrate,...
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:542
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:740
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $notused, $price_base_type, $info_bits, $type, $seller=null, $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition price.lib.php:90
product_prepare_head($object)
Prepare array with list of tabs.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.