dolibarr  13.0.2
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 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-2020 Frédéric France <frederic.france@netlogic.fr>
15  * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <https://www.gnu.org/licenses/>.
29  */
30 
37 require '../main.inc.php';
38 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
39 require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
40 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
41 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
42 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
43 
44 if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
45  require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
46 
47  $prodcustprice = new Productcustomerprice($db);
48 }
49 
50 // Load translation files required by the page
51 $langs->loadLangs(array('products', 'bills', 'companies', 'other'));
52 
53 $mesg = '';
54 $error = 0;
55 $errors = array();
56 
57 $id = GETPOST('id', 'int');
58 $ref = GETPOST('ref', 'alpha');
59 $action = GETPOST('action', 'aZ09');
60 $cancel = GETPOST('cancel', 'alpha');
61 $eid = GETPOST('eid', 'int');
62 
63 $search_soc = GETPOST('search_soc');
64 
65 // Security check
66 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
67 $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
68 if ($user->socid) $socid = $user->socid;
69 $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
70 
71 if ($id > 0 || !empty($ref))
72 {
73  $object = new Product($db);
74  $object->fetch($id, $ref);
75 }
76 
77 // Clean param
78 if ((!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) && empty($conf->global->PRODUIT_MULTIPRICES_LIMIT)) $conf->global->PRODUIT_MULTIPRICES_LIMIT = 5;
79 
80 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
81 $hookmanager->initHooks(array('productpricecard', 'globalcard'));
82 
83 
84 /*
85  * Actions
86  */
87 
88 if ($cancel) $action = '';
89 
90 $parameters = array('id'=>$id, 'ref'=>$ref);
91 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
92 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
93 
94 if (empty($reshook))
95 {
96  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
97  {
98  $search_soc = '';
99  }
100 
101  if ($action == 'setlabelsellingprice' && $user->admin)
102  {
103  require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
104  $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.GETPOST('pricelevel');
105  dolibarr_set_const($db, $keyforlabel, GETPOST('labelsellingprice', 'alpha'), 'chaine', 0, '', $conf->entity);
106  $action = '';
107  }
108 
109  if (($action == 'update_vat') && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer))
110  {
111  $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)'
112 
113  // We must define tva_tx, npr and local taxes
114  $tva_tx = $tva_tx_txt;
115  $reg = array();
116  $vatratecode = '';
117  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
118  {
119  $vat_src_code = $reg[1];
120  $tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
121  }
122 
123  $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
124  $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
125  $localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
126  // If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
127  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
128  {
129  // 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.
130  $vatratecode = $reg[1];
131  // Get record from code
132  $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
133  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
134  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
135  $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
136  $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
137  $resql = $db->query($sql);
138  if ($resql)
139  {
140  $obj = $db->fetch_object($resql);
141  $npr = $obj->recuperableonly;
142  $localtax1 = $obj->localtax1;
143  $localtax2 = $obj->localtax2;
144  $localtax1_type = $obj->localtax1_type;
145  $localtax2_type = $obj->localtax2_type;
146  }
147  }
148 
149  $object->default_vat_code = $vatratecode;
150  $object->tva_tx = $tva_tx;
151  $object->tva_npr = $npr;
152  $object->localtax1_tx = $localtax1;
153  $object->localtax2_tx = $localtax2;
154  $object->localtax1_type = $localtax1_type;
155  $object->localtax2_type = $localtax2_type;
156 
157  $db->begin();
158 
159  $resql = $object->update($object->id, $user);
160  if ($resql <= 0)
161  {
162  $error++;
163  setEventMessages($object->error, $object->errors, 'errors');
164  }
165 
166  if ($error)
167  {
168  //$localtaxarray=array('0'=>$localtax1_type,'1'=>$localtax1,'2'=>$localtax2_type,'3'=>$localtax2);
169  $localtaxarray = array(); // We do not store localtaxes into product, we will use instead the "vat code" to retrieve them.
170  $object->updatePrice(0, $object->price_base_type, $user, $tva_tx, '', 0, $npr, 0, 0, $localtaxarray, $vatratecode);
171  }
172 
173  if (!$error)
174  {
175  $db->commit();
176  } else {
177  $db->rollback();
178  }
179 
180  $action = '';
181  }
182 
183  if (($action == 'update_price') && !$cancel && $object->getRights()->creer)
184  {
185  $error = 0;
186  $pricestoupdate = array();
187 
188  $psq = GETPOST('psqflag');
189  $psq = empty($newpsq) ? 0 : $newpsq;
190  $maxpricesupplier = $object->min_recommended_price();
191 
192  if (!empty($conf->dynamicprices->enabled)) {
193  $object->fk_price_expression = empty($eid) ? 0 : $eid; //0 discards expression
194 
195  if ($object->fk_price_expression != 0) {
196  //Check the expression validity by parsing it
197  $priceparser = new PriceParser($db);
198 
199  if ($priceparser->parseProduct($object) < 0) {
200  $error++;
201  setEventMessages($priceparser->translatedError(), null, 'errors');
202  }
203  }
204  }
205 
206  // Multiprices
207  if (!$error && (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))) {
208  $newprice = GETPOST('price', 'array');
209  $newprice_min = GETPOST('price_min', 'array');
210  $newpricebase = GETPOST('multiprices_base_type', 'array');
211  $newvattx = GETPOST('tva_tx', 'array');
212  $newvatnpr = GETPOST('tva_npr', 'array');
213  $newlocaltax1_tx = GETPOST('localtax1_tx', 'array');
214  $newlocaltax1_type = GETPOST('localtax1_type', 'array');
215  $newlocaltax2_tx = GETPOST('localtax2_tx', 'array');
216  $newlocaltax2_type = GETPOST('localtax2_type', 'array');
217 
218  //Shall we generate prices using price rules?
219  $object->price_autogen = GETPOST('usePriceRules') == 'on';
220 
221  for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
222  {
223  if (!isset($newprice[$i])) {
224  continue;
225  }
226 
227  $tva_tx_txt = $newvattx[$i];
228 
229  $tva_tx = $tva_tx_txt;
230  $vatratecode = '';
231  $reg = array();
232  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
233  {
234  $vat_src_code = $reg[1];
235  $tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
236  }
237  $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
238 
239  $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
240  $localtax1 = $newlocaltax1_tx[$i];
241  $localtax1_type = $newlocaltax1_type[$i];
242  $localtax2 = $newlocaltax2_tx[$i];
243  $localtax2_type = $newlocaltax2_type[$i];
244  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
245  {
246  // We look into database using code
247  $vatratecode = $reg[1];
248  // Get record from code
249  $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
250  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
251  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
252  $sql .= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
253  $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
254  $resql = $db->query($sql);
255  if ($resql)
256  {
257  $obj = $db->fetch_object($resql);
258  $npr = $obj->recuperableonly;
259  $localtax1 = $obj->localtax1;
260  $localtax2 = $obj->localtax2;
261  $localtax1_type = $obj->localtax1_type;
262  $localtax2_type = $obj->localtax2_type;
263  }
264  }
265 
266  $pricestoupdate[$i] = array(
267  'price' => price2num($newprice[$i], '', 2),
268  'price_min' => price2num($newprice_min[$i], '', 2),
269  'price_base_type' => $newpricebase[$i],
270  'default_vat_code' => $vatratecode,
271  'vat_tx' => $tva_tx, // default_vat_code should be used in priority in a future
272  'npr' => $npr, // default_vat_code should be used in priority in a future
273  'localtaxes_array' => array('0'=>$localtax1_type, '1'=>$localtax1, '2'=>$localtax2_type, '3'=>$localtax2) // default_vat_code should be used in priority in a future
274  );
275 
276  //If autogeneration is enabled, then we only set the first level
277  if ($object->price_autogen) {
278  break;
279  }
280  }
281  } elseif (!$error)
282  {
283  $newprice = price2num(GETPOST('price', 'alpha'), '', 2);
284  $newprice_min = price2num(GETPOST('price_min', 'alpha'), '', 2);
285  $newpricebase = GETPOST('price_base_type', 'alpha');
286  $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)'
287 
288  $tva_tx = $tva_tx_txt;
289  $vatratecode = '';
290  $reg = array();
291  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
292  {
293  $vat_src_code = $reg[1];
294  $tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
295  }
296  $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
297 
298  $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
299  $localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
300  // If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
301  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
302  {
303  // We look into database using code
304  $vatratecode = $reg[1];
305  // Get record from code
306  $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
307  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
308  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
309  $sql .= " AND t.taux = ".$tva_tx." AND t.active = 1";
310  $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
311  $resql = $db->query($sql);
312  if ($resql)
313  {
314  $obj = $db->fetch_object($resql);
315  $npr = $obj->recuperableonly;
316  $localtax1 = $obj->localtax1;
317  $localtax2 = $obj->localtax2;
318  $localtax1_type = $obj->localtax1_type;
319  $localtax2_type = $obj->localtax2_type;
320 
321  // If spain, we don't use the localtax found into tax record in database with same code, but using the get_localtax rule
322  if (in_array($mysoc->country_code, array('ES')))
323  {
324  $localtax1 = get_localtax($tva_tx, 1);
325  $localtax2 = get_localtax($tva_tx, 2);
326  }
327  }
328  }
329  $pricestoupdate[0] = array(
330  'price' => $newprice,
331  'price_min' => $newprice_min,
332  'price_base_type' => $newpricebase,
333  'default_vat_code' => $vatratecode,
334  'vat_tx' => $tva_tx, // default_vat_code should be used in priority in a future
335  'npr' => $npr, // default_vat_code should be used in priority in a future
336  'localtaxes_array' => array('0'=>$localtax1_type, '1'=>$localtax1, '2'=>$localtax2_type, '3'=>$localtax2) // default_vat_code should be used in priority in a future
337  );
338  }
339 
340  if (!$error) {
341  $db->begin();
342 
343  foreach ($pricestoupdate as $key => $val) {
344  $newprice = $val['price'];
345 
346  if ($val['price'] < $val['price_min'] && !empty($object->fk_price_expression)) {
347  $newprice = $val['price_min']; //Set price same as min, the user will not see the
348  }
349 
350  $newprice = price2num($newprice, 'MU');
351  $newprice_min = price2num($val['price_min'], 'MU');
352 
353  if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE) && $newprice_min < $maxpricesupplier) {
354  setEventMessages($langs->trans("MinimumPriceLimit", price($maxpricesupplier, 0, '', 1, - 1, - 1, 'auto')), null, 'errors');
355  $error++;
356  break;
357  }
358 
359  if ($object->multiprices[$key] != $newprice || $object->multiprices_min[$key] != $newprice_min || $object->multiprices_base_type[$key] != $val['price_base_type'])
360  $res = $object->updatePrice($newprice, $val['price_base_type'], $user, $val['vat_tx'], $newprice_min, $key, $val['npr'], $psq, 0, $val['localtaxes_array'], $val['default_vat_code']);
361  else $res = 0;
362 
363 
364  if ($res < 0) {
365  $error++;
366  setEventMessages($object->error, $object->errors, 'errors');
367  break;
368  }
369  }
370  }
371 
372  if (!$error && $object->update($object->id, $user) < 0) {
373  $error++;
374  setEventMessages($object->error, $object->errors, 'errors');
375  }
376 
377  if (empty($error)) {
378  $action = '';
379  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
380  $db->commit();
381  } else {
382  $action = 'edit_price';
383  $db->rollback();
384  }
385  }
386 
387 
388  if ($action == 'delete' && $user->rights->produit->supprimer)
389  {
390  $result = $object->log_price_delete($user, GETPOST('lineid', 'int'));
391  if ($result < 0) {
392  setEventMessages($object->error, $object->errors, 'errors');
393  }
394  }
395 
396  // Set Price by quantity
397  if ($action == 'activate_price_by_qty')
398  {
399  // Activating product price by quantity add a new price line with price_by_qty set to 1
400  $level = GETPOST('level', 'int');
401  $object->updatePrice(0, $object->price_base_type, $user, $object->tva_tx, 0, $level, $object->tva_npr, 1);
402  }
403  // Unset Price by quantity
404  if ($action == 'disable_price_by_qty')
405  {
406  // Disabling product price by quantity add a new price line with price_by_qty set to 0
407  $level = GETPOST('level', 'int');
408  $object->updatePrice(0, $object->price_base_type, $user, $object->tva_tx, 0, $level, $object->tva_npr, 0);
409  }
410 
411  if ($action == 'edit_price_by_qty')
412  { // Edition d'un prix par quantité
413  $rowid = GETPOST('rowid', 'int');
414  }
415 
416  // Add or update price by quantity
417  if ($action == 'update_price_by_qty')
418  {
419  // Récupération des variables
420  $rowid = GETPOST('rowid', 'int');
421  $priceid = GETPOST('priceid', 'int');
422  $newprice = price2num(GETPOST("price", 'alpha'), 'MU');
423  // $newminprice=price2num(GETPOST("price_min"),'MU'); // TODO : Add min price management
424  $quantity = GETPOST('quantity', 'int');
425  $remise_percent = price2num(GETPOST('remise_percent', 'alpha'));
426  $remise = 0; // TODO : allow discount by amount when available on documents
427 
428  if (empty($quantity)) {
429  $error++;
430  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Qty")), null, 'errors');
431  }
432  if (empty($newprice)) {
433  $error++;
434  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Price")), null, 'errors');
435  }
436  if (!$error) {
437  // Calcul du prix HT et du prix unitaire
438  if ($object->price_base_type == 'TTC') {
439  $price = price2num($newprice) / (1 + ($object->tva_tx / 100));
440  }
441 
442  $price = price2num($newprice, 'MU');
443  $unitPrice = price2num($price / $quantity, 'MU');
444 
445  // Ajout / mise à jour
446  if ($rowid > 0) {
447  $sql = "UPDATE ".MAIN_DB_PREFIX."product_price_by_qty SET";
448  $sql .= " price='".$db->escape($price)."',";
449  $sql .= " unitprice=".$unitPrice.",";
450  $sql .= " quantity=".$quantity.",";
451  $sql .= " remise_percent=".$remise_percent.",";
452  $sql .= " remise=".$remise;
453  $sql .= " WHERE rowid = ".$rowid;
454 
455  $result = $db->query($sql);
456  if (!$result) dol_print_error($db);
457  } else {
458  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price_by_qty (fk_product_price,price,unitprice,quantity,remise_percent,remise) values (";
459  $sql .= $priceid.','.$price.','.$unitPrice.','.$quantity.','.$remise_percent.','.$remise.')';
460 
461  $result = $db->query($sql);
462  if (!$result) {
463  if ($db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
464  setEventMessages($langs->trans("DuplicateRecord"), null, 'errors');
465  } else {
466  dol_print_error($db);
467  }
468  }
469  }
470  }
471  }
472 
473  if ($action == 'delete_price_by_qty')
474  {
475  $rowid = GETPOST('rowid', 'int');
476  if (!empty($rowid)) {
477  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price_by_qty";
478  $sql .= " WHERE rowid = ".$rowid;
479 
480  $result = $db->query($sql);
481  } else {
482  setEventMessages(('delete_price_by_qty'.$langs->transnoentities('MissingIds')), null, 'errors');
483  }
484  }
485 
486  if ($action == 'delete_all_price_by_qty') {
487  $priceid = GETPOST('priceid', 'int');
488  if (!empty($rowid)) {
489  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price_by_qty";
490  $sql .= " WHERE fk_product_price = ".$priceid;
491 
492  $result = $db->query($sql);
493  } else {
494  setEventMessages(('delete_price_by_qty'.$langs->transnoentities('MissingIds')), null, 'errors');
495  }
496  }
497 
503  if ($action == 'add_customer_price_confirm' && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer)) {
504  $maxpricesupplier = $object->min_recommended_price();
505 
506  $update_child_soc = GETPOST('updatechildprice', 'int');
507 
508  // add price by customer
509  $prodcustprice->fk_soc = GETPOST('socid', 'int');
510  $prodcustprice->fk_product = $object->id;
511  $prodcustprice->price = price2num(GETPOST("price"), 'MU');
512  $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU');
513  $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha');
514 
515  $tva_tx_txt = GETPOST("tva_tx", 'alpha');
516 
517  $tva_tx = $tva_tx_txt;
518  $vatratecode = '';
519  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
520  {
521  $vat_src_code = $reg[1];
522  $tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
523  }
524  $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
525 
526  $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
527  $localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
528  // If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
529  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
530  {
531  // We look into database using code
532  $vatratecode = $reg[1];
533  // Get record from code
534  $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
535  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
536  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
537  $sql .= " AND t.taux = ".$tva_tx." AND t.active = 1";
538  $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
539  $resql = $db->query($sql);
540  if ($resql)
541  {
542  $obj = $db->fetch_object($resql);
543  $npr = $obj->recuperableonly;
544  $localtax1 = $obj->localtax1;
545  $localtax2 = $obj->localtax2;
546  $localtax1_type = $obj->localtax1_type;
547  $localtax2_type = $obj->localtax2_type;
548  }
549  }
550 
551  $prodcustprice->default_vat_code = $vatratecode;
552  $prodcustprice->tva_tx = $tva_tx;
553  $prodcustprice->recuperableonly = $npr;
554  $prodcustprice->localtax1_tx = $localtax1;
555  $prodcustprice->localtax2_tx = $localtax2;
556  $prodcustprice->localtax1_type = $localtax1_type;
557  $prodcustprice->localtax2_type = $localtax2_type;
558 
559  if (!($prodcustprice->fk_soc > 0))
560  {
561  $langs->load("errors");
562  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ThirdParty")), null, 'errors');
563  $error++;
564  $action = 'add_customer_price';
565  }
566  if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE) && $prodcustprice->price_min < $maxpricesupplier)
567  {
568  $langs->load("errors");
569  setEventMessages($langs->trans("MinimumPriceLimit", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')), null, 'errors');
570  $error++;
571  $action = 'add_customer_price';
572  }
573 
574  if (!$error)
575  {
576  $result = $prodcustprice->create($user, 0, $update_child_soc);
577 
578  if ($result < 0) {
579  setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
580  } else {
581  setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
582  }
583 
584  $action = '';
585  }
586  }
587 
588  if ($action == 'delete_customer_price' && ($user->rights->produit->supprimer || $user->rights->service->supprimer))
589  {
590  // Delete price by customer
591  $prodcustprice->id = GETPOST('lineid');
592  $result = $prodcustprice->delete($user);
593 
594  if ($result < 0) {
595  setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
596  } else {
597  setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
598  }
599  $action = '';
600  }
601 
602  if ($action == 'update_customer_price_confirm' && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer))
603  {
604  $maxpricesupplier = $object->min_recommended_price();
605 
606  $update_child_soc = GETPOST('updatechildprice', 'int');
607 
608  $prodcustprice->fetch(GETPOST('lineid', 'int'));
609 
610  // update price by customer
611  $prodcustprice->price = price2num(GETPOST("price"), 'MU');
612  $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU');
613  $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha');
614 
615  $tva_tx_txt = GETPOST("tva_tx");
616 
617  $tva_tx = $tva_tx_txt;
618  $vatratecode = '';
619  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
620  {
621  $vat_src_code = $reg[1];
622  $tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
623  }
624  $tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
625 
626  $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
627  $localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
628  // If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
629  if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
630  {
631  // We look into database using code
632  $vatratecode = $reg[1];
633  // Get record from code
634  $sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
635  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
636  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$db->escape($mysoc->country_code)."'";
637  $sql .= " AND t.taux = ".$tva_tx." AND t.active = 1";
638  $sql .= " AND t.code ='".$db->escape($vatratecode)."'";
639  $resql = $db->query($sql);
640  if ($resql)
641  {
642  $obj = $db->fetch_object($resql);
643  $npr = $obj->recuperableonly;
644  $localtax1 = $obj->localtax1;
645  $localtax2 = $obj->localtax2;
646  $localtax1_type = $obj->localtax1_type;
647  $localtax2_type = $obj->localtax2_type;
648  }
649  }
650 
651  $prodcustprice->default_vat_code = $vatratecode;
652  $prodcustprice->tva_tx = $tva_tx;
653  $prodcustprice->recuperableonly = $npr;
654  $prodcustprice->localtax1_tx = $localtax1;
655  $prodcustprice->localtax2_tx = $localtax2;
656  $prodcustprice->localtax1_type = $localtax1_type;
657  $prodcustprice->localtax2_type = $localtax2_type;
658 
659  if ($prodcustprice->price_min < $maxpricesupplier && !empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE))
660  {
661  setEventMessages($langs->trans("MinimumPriceLimit", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')), null, 'errors');
662  $error++;
663  $action = 'update_customer_price';
664  }
665 
666  if (!$error)
667  {
668  $result = $prodcustprice->update($user, 0, $update_child_soc);
669 
670  if ($result < 0) {
671  setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
672  } else {
673  setEventMessages($langs->trans("Save"), null, 'mesgs');
674  }
675 
676  $action = '';
677  }
678  }
679 }
680 
681 
682 /*
683  * View
684  */
685 
686 $form = new Form($db);
687 
688 if (!empty($id) || !empty($ref))
689 {
690  // fetch updated prices
691  $object->fetch($id, $ref);
692 }
693 
694 $title = $langs->trans('ProductServiceCard');
695 $helpurl = '';
696 $shortlabel = dol_trunc($object->label, 16);
697 if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT))
698 {
699  $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('SellingPrices');
700  $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
701 }
702 if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE))
703 {
704  $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('SellingPrices');
705  $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
706 }
707 
708 llxHeader('', $title, $helpurl, '', 0, 0, '', '', '', 'classforhorizontalscrolloftabs');
709 
710 $head = product_prepare_head($object);
711 $titre = $langs->trans("CardProduct".$object->type);
712 $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
713 
714 print dol_get_fiche_head($head, 'price', $titre, -1, $picto);
715 
716 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
717 $object->next_prev_filter = " fk_product_type = ".$object->type;
718 
719 $shownav = 1;
720 if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) $shownav = 0;
721 
722 dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
723 
724 
725 print '<div class="fichecenter">';
726 
727 print '<div class="underbanner clearboth"></div>';
728 print '<table class="border tableforfield" width="100%">';
729 
730 // Price per customer segment/level
731 if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))
732 {
733  // Price and min price are variable (depends on level of company).
734  if (!empty($socid))
735  {
736  $soc = new Societe($db);
737  $soc->id = $socid;
738  $soc->fetch($socid);
739 
740  // Selling price
741  print '<tr><td class="titlefield">';
742  print $langs->trans("SellingPrice");
743  print '</td>';
744  print '<td colspan="2">';
745  if ($object->multiprices_base_type[$soc->price_level] == 'TTC') {
746  print price($object->multiprices_ttc[$soc->price_level]);
747  } else {
748  print price($object->multiprices[$soc->price_level]);
749  }
750  if ($object->multiprices_base_type[$soc->price_level]) {
751  print ' '.$langs->trans($object->multiprices_base_type[$soc->price_level]);
752  } else {
753  print ' '.$langs->trans($object->price_base_type);
754  }
755  print '</td></tr>';
756 
757  // Price min
758  print '<tr><td>'.$langs->trans("MinPrice").'</td><td colspan="2">';
759  if ($object->multiprices_base_type[$soc->price_level] == 'TTC')
760  {
761  print price($object->multiprices_min_ttc[$soc->price_level]).' '.$langs->trans($object->multiprices_base_type[$soc->price_level]);
762  } else {
763  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]);
764  }
765  print '</td></tr>';
766 
767  if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
768  {
769  // TVA
770  print '<tr><td>'.$langs->trans("DefaultTaxRate").'</td><td colspan="2">';
771 
772  $positiverates = '';
773  if (price2num($object->multiprices_tva_tx[$soc->price_level])) $positiverates .= ($positiverates ? '/' : '').price2num($object->multiprices_tva_tx[$soc->price_level]);
774  if (price2num($object->multiprices_localtax1_type[$soc->price_level])) $positiverates .= ($positiverates ? '/' : '').price2num($object->multiprices_localtax1_tx[$soc->price_level]);
775  if (price2num($object->multiprices_localtax2_type[$soc->price_level])) $positiverates .= ($positiverates ? '/' : '').price2num($object->multiprices_localtax2_tx[$soc->price_level]);
776  if (empty($positiverates)) $positiverates = '0';
777  echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), '%', $object->tva_npr);
778  //print vatrate($object->multiprices_tva_tx[$soc->price_level], true);
779  print '</td></tr>';
780  } else {
781  // TVA
782  print '<tr><td>'.$langs->trans("DefaultTaxRate").'</td><td>';
783 
784  $positiverates = '';
785  if (price2num($object->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
786  if (price2num($object->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
787  if (price2num($object->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
788  if (empty($positiverates)) $positiverates = '0';
789  echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), '%', $object->tva_npr);
790  /*
791  if ($object->default_vat_code)
792  {
793  print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
794  }
795  else print vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true);*/
796  print '</td></tr>';
797  }
798  } else {
799  if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
800  {
801  // We show only vat for level 1
802  print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td>';
803  print '<td colspan="2">'.vatrate($object->multiprices_tva_tx[1], true).'</td>';
804  print '</tr>';
805  } else {
806  // TVA
807  print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td><td>';
808 
809  $positiverates = '';
810  if (price2num($object->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
811  if (price2num($object->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
812  if (price2num($object->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
813  if (empty($positiverates)) $positiverates = '0';
814  echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), '%', $object->tva_npr);
815  /*
816  if ($object->default_vat_code)
817  {
818  print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
819  }
820  else print vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true);*/
821  print '</td></tr>';
822  }
823  print '</table>';
824 
825  print '<br>';
826 
827  print '<table class="noborder tableforfield" width="100%">';
828  print '<tr class="liste_titre"><td>';
829  print $langs->trans("PriceLevel");
830  if ($user->admin) print ' <a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editlabelsellingprice&amp;pricelevel='.$i.'&amp;id='.$object->id.'">'.img_edit($langs->trans('EditSellingPriceLabel'), 0).'</a>';
831  print '</td>';
832  print '<td style="text-align: right">'.$langs->trans("SellingPrice").'</td>';
833  print '<td style="text-align: right">'.$langs->trans("MinPrice").'</td>';
834  print '</tr>';
835 
836  for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
837  {
838  print '<tr class="oddeven">';
839 
840  // Label of price
841  print '<td>';
842  $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i;
843  if (preg_match('/editlabelsellingprice/', $action))
844  {
845  print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">';
846  print '<input type="hidden" name="token" value="'.newToken().'">';
847  print '<input type="hidden" name="action" value="setlabelsellingprice">';
848  print '<input type="hidden" name="pricelevel" value="'.$i.'">';
849  print $langs->trans("SellingPrice").' '.$i.' - ';
850  print '<input class="maxwidthonsmartphone" type="text" name="labelsellingprice" value="'.$conf->global->$keyforlabel.'">';
851  print '&nbsp;<input type="submit" class="button smallpaddingimp" value="'.$langs->trans("Modify").'">';
852  print '</form>';
853  } else {
854  print $langs->trans("SellingPrice").' '.$i;
855  if (!empty($conf->global->$keyforlabel)) print ' - '.$langs->trans($conf->global->$keyforlabel);
856  }
857  print '</td>';
858 
859  if ($object->multiprices_base_type [$i] == 'TTC') {
860  print '<td style="text-align: right">'.price($object->multiprices_ttc[$i]);
861  } else {
862  print '<td style="text-align: right">'.price($object->multiprices[$i]);
863  }
864 
865  if ($object->multiprices_base_type[$i]) {
866  print ' '.$langs->trans($object->multiprices_base_type [$i]).'</td>';
867  } else {
868  print ' '.$langs->trans($object->price_base_type).'</td>';
869  }
870 
871  // Prix min
872  print '<td style="text-align: right">';
873  if (empty($object->multiprices_base_type[$i])) $object->multiprices_base_type[$i] = "HT";
874  if ($object->multiprices_base_type[$i] == 'TTC')
875  {
876  print price($object->multiprices_min_ttc[$i]).' '.$langs->trans($object->multiprices_base_type[$i]);
877  } else {
878  print price($object->multiprices_min[$i]).' '.$langs->trans($object->multiprices_base_type[$i]);
879  }
880  print '</td></tr>';
881 
882  // Price by quantity
883  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) // TODO Fix the form included into a tr instead of a td
884  {
885  print '<tr><td>'.$langs->trans("PriceByQuantity").' '.$i;
886  if (!empty($conf->global->$keyforlabel)) print ' - '.$langs->trans($conf->global->$keyforlabel);
887  print '</td><td colspan="2">';
888 
889  if ($object->prices_by_qty[$i] == 1) {
890  print '<table width="50%" class="border" summary="List of quantities">';
891 
892  print '<tr class="liste_titre">';
893  print '<td>'.$langs->trans("PriceByQuantityRange").' '.$i.'</td>';
894  print '<td class="right">'.$langs->trans("HT").'</td>';
895  print '<td class="right">'.$langs->trans("UnitPrice").'</td>';
896  print '<td class="right">'.$langs->trans("Discount").'</td>';
897  print '<td>&nbsp;</td>';
898  print '</tr>';
899  foreach ($object->prices_by_qty_list[$i] as $ii => $prices)
900  {
901  if ($action == 'edit_price_by_qty' && $rowid == $prices['rowid'] && ($user->rights->produit->creer || $user->rights->service->creer)) {
902  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
903  print '<input type="hidden" name="token" value="'.newToken().'">';
904  print '<input type="hidden" name="action" value="update_price_by_qty">';
905  print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[$i].'">';
906  print '<input type="hidden" value="'.$prices['rowid'].'" name="rowid">';
907  print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
908  print '<td><input size="5" type="text" value="'.$prices['quantity'].'" name="quantity"></td>';
909  print '<td class="right" colspan="2"><input size="10" type="text" value="'.price2num($prices['price'], 'MU').'" name="price">&nbsp;'.$object->price_base_type.'</td>';
910  print '<td class="right nowraponall"><input size="5" type="text" value="'.$prices['remise_percent'].'" name="remise_percent"> %</td>';
911  print '<td class="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>';
912  print '</tr>';
913  print '</form>';
914  } else {
915  print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
916  print '<td>'.$prices['quantity'].'</td>';
917  print '<td class="right">'.price($prices['price']).'</td>';
918  print '<td class="right">'.price($prices['unitprice']).'</td>';
919  print '<td class="right">'.price($prices['remise_percent']).' %</td>';
920  print '<td class="center">';
921  if (($user->rights->produit->creer || $user->rights->service->creer)) {
922  print '<a class="editfielda marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit_price_by_qty&amp;rowid='.$prices["rowid"].'">';
923  print img_edit().'</a>';
924  print '<a class="marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete_price_by_qty&amp;token='.newToken().'&amp;rowid='.$prices["rowid"].'">';
925  print img_delete().'</a>';
926  } else {
927  print '&nbsp;';
928  }
929  print '</td>';
930  print '</tr>';
931  }
932  }
933  if ($action != 'edit_price_by_qty' && ($user->rights->produit->creer || $user->rights->service->creer)) {
934  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
935  print '<input type="hidden" name="token" value="'.newToken().'">';
936  print '<input type="hidden" name="action" value="update_price_by_qty">';
937  print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[$i].'">'; // id in product_price
938  print '<input type="hidden" value="0" name="rowid">'; // id in product_price
939  print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
940  print '<td><input size="5" type="text" value="1" name="quantity"></td>';
941  print '<td class="right" class="nowrap"><input size="10" type="text" value="0" name="price">&nbsp;'.$object->price_base_type.'</td>';
942  print '<td class="right">&nbsp;</td>';
943  print '<td class="right" class="nowraponall"><input size="5" type="text" value="0" name="remise_percent"> %</td>';
944  print '<td class="center"><input type="submit" value="'.$langs->trans("Add").'" class="button"></td>';
945  print '</tr>';
946  print '</form>';
947  }
948 
949  print '</table>';
950  print '<a class="editfielda marginleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=disable_price_by_qty&level='.$i.'">('.$langs->trans("DisablePriceByQty").')</a>';
951  } else {
952  print $langs->trans("No");
953  print '&nbsp; <a class="marginleftonly marginrightonly" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=activate_price_by_qty&level='.$i.'">('.$langs->trans("Activate").')</a>';
954  }
955  print '</td></tr>';
956  }
957  }
958  }
959 } else {
960  // TVA
961  print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td><td>';
962 
963  $positiverates = '';
964  if (price2num($object->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
965  if (price2num($object->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
966  if (price2num($object->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
967  if (empty($positiverates)) $positiverates = '0';
968  echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), '%', $object->tva_npr);
969  /*
970  if ($object->default_vat_code)
971  {
972  print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
973  }
974  else print vatrate($object->tva_tx, true, $object->tva_npr, true);*/
975  print '</td></tr>';
976 
977  // Price
978  print '<tr><td>'.$langs->trans("SellingPrice").'</td><td>';
979  if ($object->price_base_type == 'TTC') {
980  print price($object->price_ttc).' '.$langs->trans($object->price_base_type);
981  } else {
982  print price($object->price).' '.$langs->trans($object->price_base_type);
983  }
984  print '</td></tr>';
985 
986  // Price minimum
987  print '<tr><td>'.$langs->trans("MinPrice").'</td><td>';
988  if ($object->price_base_type == 'TTC') {
989  print price($object->price_min_ttc).' '.$langs->trans($object->price_base_type);
990  } else {
991  print price($object->price_min).' '.$langs->trans($object->price_base_type);
992  }
993  print '</td></tr>';
994 
995  // Price by quantity
996  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) // TODO Fix the form inside tr instead of td
997  {
998  print '<tr><td>'.$langs->trans("PriceByQuantity");
999  if ($object->prices_by_qty[0] == 0) {
1000  print '&nbsp; <a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=activate_price_by_qty&level=1">('.$langs->trans("Activate").')';
1001  } else {
1002  print '&nbsp; <a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=disable_price_by_qty&level=1">('.$langs->trans("DisablePriceByQty").')';
1003  }
1004  print '</td><td>';
1005 
1006  if ($object->prices_by_qty[0] == 1)
1007  {
1008  print '<table width="50%" class="border" summary="List of quantities">';
1009  print '<tr class="liste_titre">';
1010  //print '<td>' . $langs->trans("PriceByQuantityRange") . '</td>';
1011  print '<td>'.$langs->trans("Quantity").'</td>';
1012  print '<td class="right">'.$langs->trans("Price").'</td>';
1013  print '<td class="right"></td>';
1014  print '<td class="right">'.$langs->trans("UnitPrice").'</td>';
1015  print '<td class="right">'.$langs->trans("Discount").'</td>';
1016  print '<td>&nbsp;</td>';
1017  print '</tr>';
1018  if ($action != 'edit_price_by_qty')
1019  {
1020  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">'; // FIXME a form into a table is not allowed
1021  print '<input type="hidden" name="token" value="'.newToken().'">';
1022  print '<input type="hidden" name="action" value="update_price_by_qty">';
1023  print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[0].'">'; // id in product_price
1024  print '<input type="hidden" value="0" name="rowid">'; // id in product_price_by_qty
1025 
1026  print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1027  print '<td><input size="5" type="text" value="1" name="quantity"></td>';
1028  print '<td class="right"><input class="width50 right" type="text" value="0" name="price"></td>';
1029  print '<td>';
1030  //print $object->price_base_type;
1031  print '</td>';
1032  print '<td class="right">&nbsp;</td>';
1033  print '<td class="right nowraponall"><input type="text" class="width50 right" value="0" name="remise_percent"> %</td>';
1034  print '<td class="center"><input type="submit" value="'.$langs->trans("Add").'" class="button"></td>';
1035  print '</tr>';
1036 
1037  print '</form>';
1038  }
1039  foreach ($object->prices_by_qty_list[0] as $ii => $prices)
1040  {
1041  if ($action == 'edit_price_by_qty' && $rowid == $prices['rowid'] && ($user->rights->produit->creer || $user->rights->service->creer))
1042  {
1043  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1044  print '<input type="hidden" name="token" value="'.newToken().'">';
1045  print '<input type="hidden" name="action" value="update_price_by_qty">';
1046  print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[0].'">'; // id in product_price
1047  print '<input type="hidden" value="'.$prices['rowid'].'" name="rowid">'; // id in product_price_by_qty
1048  print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1049  print '<td><input size="5" type="text" value="'.$prices['quantity'].'" name="quantity"></td>';
1050  print '<td class="right"><input class="width50 right" type="text" value="'.price2num($prices['price'], 'MU').'" name="price"></td>';
1051  print '<td class="right">';
1052  //print $object->price_base_type;
1053  print $prices['price_base_type'];
1054  print '</td>';
1055  print '<td class="right">&nbsp;</td>';
1056  print '<td class="right nowraponall"><input class="width50 right" type="text" value="'.$prices['remise_percent'].'" name="remise_percent"> %</td>';
1057  print '<td class="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>';
1058  print '</tr>';
1059  print '</form>';
1060  } else {
1061  print '<tr class="'.($ii % 2 == 0 ? 'pair' : 'impair').'">';
1062  print '<td>'.$prices['quantity'].'</td>';
1063  print '<td class="right">'.price($prices['price']).'</td>';
1064  print '<td class="right">';
1065  //print $object->price_base_type;
1066  print $prices['price_base_type'];
1067  print '</td>';
1068  print '<td class="right">'.price($prices['unitprice']).'</td>';
1069  print '<td class="right">'.price($prices['remise_percent']).' %</td>';
1070  print '<td class="center">';
1071  if (($user->rights->produit->creer || $user->rights->service->creer))
1072  {
1073  print '<a class="editfielda marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit_price_by_qty&amp;rowid='.$prices["rowid"].'">';
1074  print img_edit().'</a>';
1075  print '<a class="marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete_price_by_qty&amp;token='.newToken().'&amp;rowid='.$prices["rowid"].'">';
1076  print img_delete().'</a>';
1077  } else {
1078  print '&nbsp;';
1079  }
1080  print '</td>';
1081  print '</tr>';
1082  }
1083  }
1084  print '</table>';
1085  } else {
1086  print $langs->trans("No");
1087  }
1088  print '</td></tr>';
1089  }
1090 }
1091 
1092 print "</table>\n";
1093 
1094 print '</div>';
1095 print '<div style="clear:both"></div>';
1096 
1097 
1098 print dol_get_fiche_end();
1099 
1100 
1101 
1102 /* ************************************************************************** */
1103 /* */
1104 /* Barre d'action */
1105 /* */
1106 /* ************************************************************************** */
1107 
1108 if (!$action || $action == 'delete' || $action == 'showlog_customer_price' || $action == 'showlog_default_price' || $action == 'add_customer_price'
1109  || $action == 'activate_price_by_qty' || $action == 'disable_price_by_qty')
1110 {
1111  print "\n".'<div class="tabsAction">'."\n";
1112 
1113  if ($object->isVariant()) {
1114  if ($user->rights->produit->creer || $user->rights->service->creer) {
1115  print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NoEditVariants")).'">'.$langs->trans("UpdateDefaultPrice").'</a></div>';
1116  }
1117  } else {
1118  if (empty($conf->global->PRODUIT_MULTIPRICES) && empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1119  if ($user->rights->produit->creer || $user->rights->service->creer) {
1120  print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit_price&amp;id='.$object->id.'">'.$langs->trans("UpdateDefaultPrice").'</a></div>';
1121  }
1122  }
1123 
1124  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
1125  if ($user->rights->produit->creer || $user->rights->service->creer) {
1126  print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=add_customer_price&amp;id='.$object->id.'">'.$langs->trans("AddCustomerPrice").'</a></div>';
1127  }
1128  }
1129 
1130  if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1131  if ($user->rights->produit->creer || $user->rights->service->creer) {
1132  print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit_vat&amp;id='.$object->id.'">'.$langs->trans("UpdateVAT").'</a></div>';
1133  }
1134 
1135  if ($user->rights->produit->creer || $user->rights->service->creer) {
1136  print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit_price&amp;id='.$object->id.'">'.$langs->trans("UpdateLevelPrices").'</a></div>';
1137  }
1138  }
1139  }
1140 
1141  print "\n</div>\n";
1142 }
1143 
1144 
1145 
1146 /*
1147  * Edit price area
1148  */
1149 
1150 if ($action == 'edit_vat' && ($user->rights->produit->creer || $user->rights->service->creer))
1151 {
1152  print load_fiche_titre($langs->trans("UpdateVAT"), '');
1153 
1154  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1155  print '<input type="hidden" name="token" value="'.newToken().'">';
1156  print '<input type="hidden" name="action" value="update_vat">';
1157  print '<input type="hidden" name="id" value="'.$object->id.'">';
1158 
1159  print dol_get_fiche_head('');
1160 
1161  print '<table class="border centpercent">';
1162 
1163  // VAT
1164  print '<tr><td>'.$langs->trans("DefaultTaxRate").'</td><td>';
1165  print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
1166  print '</td></tr>';
1167 
1168  print '</table>';
1169 
1170  print dol_get_fiche_end();
1171 
1172  print '<div class="center">';
1173  print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
1174  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
1175  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
1176  print '</div>';
1177 
1178  print '<br></form><br>';
1179 }
1180 
1181 if ($action == 'edit_price' && $object->getRights()->creer)
1182 {
1183  print load_fiche_titre($langs->trans("NewPrice"), '');
1184 
1185  if (empty($conf->global->PRODUIT_MULTIPRICES) && empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))
1186  {
1187  print '<!-- Edit price -->'."\n";
1188  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1189  print '<input type="hidden" name="token" value="'.newToken().'">';
1190  print '<input type="hidden" name="action" value="update_price">';
1191  print '<input type="hidden" name="id" value="'.$object->id.'">';
1192 
1193  print dol_get_fiche_head('');
1194 
1195  print '<div class="div-table-responsive-no-min">';
1196  print '<table class="border centpercent">';
1197 
1198  // VAT
1199  print '<tr><td class="titlefield">'.$langs->trans("DefaultTaxRate").'</td><td>';
1200  print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
1201  print '</td></tr>';
1202 
1203  // Price base
1204  print '<tr><td>';
1205  print $langs->trans('PriceBase');
1206  print '</td>';
1207  print '<td>';
1208  print $form->selectPriceBaseType($object->price_base_type, "price_base_type");
1209  print '</td>';
1210  print '</tr>';
1211 
1212  // Only show price mode and expression selector if module is enabled
1213  if (!empty($conf->dynamicprices->enabled)) {
1214  // Price mode selector
1215  print '<tr><td>'.$langs->trans("PriceMode").'</td><td>';
1216  $price_expression = new PriceExpression($db);
1217  $price_expression_list = array(0 => $langs->trans("PriceNumeric")); //Put the numeric mode as first option
1218  foreach ($price_expression->list_price_expression() as $entry) {
1219  $price_expression_list[$entry->id] = $entry->title;
1220  }
1221  $price_expression_preselection = GETPOST('eid') ? GETPOST('eid') : ($object->fk_price_expression ? $object->fk_price_expression : '0');
1222  print $form->selectarray('eid', $price_expression_list, $price_expression_preselection);
1223  print '&nbsp; <div id="expression_editor" class="button">'.$langs->trans("PriceExpressionEditor").'</div>';
1224  print '</td></tr>';
1225 
1226  // This code hides the numeric price input if is not selected, loads the editor page if editor button is pressed
1227  ?>
1228 
1229  <script type="text/javascript">
1230  jQuery(document).ready(function() {
1231  jQuery("#expression_editor").click(function() {
1232  window.location = "<?php echo DOL_URL_ROOT ?>/product/dynamic_price/editor.php?id=<?php echo $id ?>&tab=price&eid=" + $("#eid").val();
1233  });
1234  jQuery("#eid").change(on_change);
1235  on_change();
1236  });
1237  function on_change() {
1238  if ($("#eid").val() == 0) {
1239  jQuery("#price_numeric").show();
1240  } else {
1241  jQuery("#price_numeric").hide();
1242  }
1243  }
1244  </script>
1245  <?php
1246  }
1247 
1248  // Price
1249  $product = new Product($db);
1250  $product->fetch($id, $ref, '', 1); //Ignore the math expression when getting the price
1251  print '<tr id="price_numeric"><td>';
1252  $text = $langs->trans('SellingPrice');
1253  print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
1254  print '</td><td>';
1255  if ($object->price_base_type == 'TTC') {
1256  print '<input name="price" size="10" value="'.price($product->price_ttc).'">';
1257  } else {
1258  print '<input name="price" size="10" value="'.price($product->price).'">';
1259  }
1260  print '</td></tr>';
1261 
1262  // Price minimum
1263  print '<tr><td>';
1264  $text = $langs->trans('MinPrice');
1265  print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
1266  print '</td><td>';
1267  if ($object->price_base_type == 'TTC') {
1268  print '<input name="price_min" size="10" value="'.price($object->price_min_ttc).'">';
1269  } else {
1270  print '<input name="price_min" size="10" value="'.price($object->price_min).'">';
1271  }
1272  if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE))
1273  {
1274  print ' &nbsp; '.$langs->trans("MinimumRecommendedPrice", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
1275  }
1276  print '</td>';
1277  print '</tr>';
1278 
1279  $parameters = array();
1280  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
1281 
1282  print '</table>';
1283  print '</div>';
1284 
1285  print dol_get_fiche_end();
1286 
1287  print '<div class="center">';
1288  print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
1289  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
1290  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
1291  print '</div>';
1292 
1293  print '<br></form>';
1294  } else {
1295  print '<!-- Edit price per level -->'."\n";
1296  ?>
1297  <script>
1298 
1299  var showHidePriceRules = function () {
1300  var otherPrices = $('div.fiche form table tbody tr:not(:first)');
1301  var minPrice1 = $('div.fiche form input[name="price_min[1]"]');
1302 
1303  if (jQuery('input#usePriceRules').prop('checked')) {
1304  otherPrices.hide();
1305  minPrice1.hide();
1306  } else {
1307  otherPrices.show();
1308  minPrice1.show();
1309  }
1310  };
1311 
1312  jQuery(document).ready(function () {
1313  showHidePriceRules();
1314 
1315  jQuery('input#usePriceRules').click(showHidePriceRules);
1316  });
1317  </script>
1318  <?php
1319 
1320  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1321  print '<input type="hidden" name="token" value="'.newToken().'">';
1322  print '<input type="hidden" name="action" value="update_price">';
1323  print '<input type="hidden" name="id" value="'.$object->id.'">';
1324 
1325  //print dol_get_fiche_head('', '', '', -1);
1326 
1327  if ((!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) && !empty($conf->global->PRODUIT_MULTIPRICES_ALLOW_AUTOCALC_PRICELEVEL)) {
1328  print $langs->trans('UseMultipriceRules').' <input type="checkbox" id="usePriceRules" name="usePriceRules" '.($object->price_autogen ? 'checked' : '').'><br><br>';
1329  }
1330 
1331  print '<div class="div-table-responsive-no-min">';
1332  print '<table class="noborder">';
1333  print '<thead><tr class="liste_titre">';
1334 
1335  print '<td>'.$langs->trans("PriceLevel").'</td>';
1336 
1337  if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) print '<td style="text-align: center">'.$langs->trans("DefaultTaxRate").'</td>';
1338  else print '<td></td>';
1339 
1340  print '<td class="center">'.$langs->trans("SellingPrice").'</td>';
1341 
1342  print '<td class="center">'.$langs->trans("MinPrice").'</td>';
1343 
1344  if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE)) {
1345  print '<td></td>';
1346  }
1347  print '</tr></thead>';
1348 
1349  print '<tbody>';
1350 
1351  for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
1352  {
1353  print '<tr class="oddeven">';
1354  print '<td>';
1355  $text = $langs->trans('SellingPrice').' '.$i;
1356  print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
1357  print '</td>';
1358 
1359  // VAT
1360  if (empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
1361  print '<td>';
1362  print '<input type="hidden" name="tva_tx['.$i.']" value="'.($object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx).'">';
1363  print '<input type="hidden" name="tva_npr['.$i.']" value="'.$object->tva_npr.'">';
1364  print '<input type="hidden" name="localtax1_tx['.$i.']" value="'.$object->localtax1_tx.'">';
1365  print '<input type="hidden" name="localtax1_type['.$i.']" value="'.$object->localtax1_type.'">';
1366  print '<input type="hidden" name="localtax2_tx['.$i.']" value="'.$object->localtax2_tx.'">';
1367  print '<input type="hidden" name="localtax2_type['.$i.']" value="'.$object->localtax2_type.'">';
1368  print '</td>';
1369  } else {
1370  // This option is kept for backward compatibility but has no sense
1371  print '<td style="text-align: center">';
1372  print $form->load_tva("tva_tx[".$i.']', $object->multiprices_tva_tx[$i], $mysoc, '', $object->id, false, $object->type, false, 1);
1373  print '</td>';
1374  }
1375 
1376  // Selling price
1377  print '<td style="text-align: center">';
1378  if ($object->multiprices_base_type [$i] == 'TTC') {
1379  print '<input name="price['.$i.']" size="10" value="'.price($object->multiprices_ttc [$i]).'">';
1380  } else {
1381  print '<input name="price['.$i.']" size="10" value="'.price($object->multiprices [$i]).'">';
1382  }
1383  print '&nbsp;'.$form->selectPriceBaseType($object->multiprices_base_type [$i], "multiprices_base_type[".$i."]");
1384  print '</td>';
1385 
1386  // Min price
1387  print '<td style="text-align: center">';
1388  if ($object->multiprices_base_type [$i] == 'TTC') {
1389  print '<input name="price_min['.$i.']" size="10" value="'.price($object->multiprices_min_ttc [$i]).'">';
1390  } else {
1391  print '<input name="price_min['.$i.']" size="10" value="'.price($object->multiprices_min [$i]).'">';
1392  }
1393  if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE))
1394  {
1395  print '<td class="left">'.$langs->trans("MinimumRecommendedPrice", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
1396  }
1397  print '</td>';
1398 
1399  print '</tr>';
1400  }
1401 
1402  print '</tbody>';
1403 
1404  print '</table>';
1405  print '</div>';
1406 
1407  //print dol_get_fiche_end();
1408 
1409  print '<div style="text-align: center">';
1410  print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
1411  print '&nbsp;&nbsp;&nbsp;';
1412  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'"></div>';
1413  print '</form>';
1414  }
1415 }
1416 
1417 
1418 // List of price changes - log historic (ordered by descending date)
1419 
1420 if ((empty($conf->global->PRODUIT_CUSTOMER_PRICES) || $action == 'showlog_default_price') && !in_array($action, array('edit_price', 'edit_vat')))
1421 {
1422  $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,";
1423  $sql .= " p.price_level, p.price_min, p.price_min_ttc,p.price_by_qty,";
1424  $sql .= " p.date_price as dp, p.fk_price_expression, u.rowid as user_id, u.login";
1425  $sql .= " FROM ".MAIN_DB_PREFIX."product_price as p,";
1426  $sql .= " ".MAIN_DB_PREFIX."user as u";
1427  $sql .= " WHERE fk_product = ".$object->id;
1428  $sql .= " AND p.entity IN (".getEntity('productprice').")";
1429  $sql .= " AND p.fk_user_author = u.rowid";
1430  if (!empty($socid) && !empty($conf->global->PRODUIT_MULTIPRICES)) $sql .= " AND p.price_level = ".$soc->price_level;
1431  $sql .= " ORDER BY p.date_price DESC, p.rowid DESC, p.price_level ASC";
1432  // $sql .= $db->plimit();
1433 
1434  $result = $db->query($sql);
1435  if ($result)
1436  {
1437  print '<div class="divlogofpreviouscustomerprice">';
1438 
1439  $num = $db->num_rows($result);
1440 
1441  if (!$num)
1442  {
1443  $db->free($result);
1444 
1445  // Il doit au moins y avoir la ligne de prix initial.
1446  // On l'ajoute donc pour remettre a niveau (pb vieilles versions)
1447  // We emulate the change of the price from interface with the same value than the one into table llx_product
1448  if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
1449  $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);
1450  } else {
1451  $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));
1452  }
1453 
1454  $result = $db->query($sql);
1455  $num = $db->num_rows($result);
1456  }
1457 
1458  if ($num > 0) {
1459  // Default prices or
1460  // Log of previous customer prices
1461  $backbutton = '<a class="justalink" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">'.$langs->trans("Back").'</a>';
1462 
1463  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) print_barre_liste($langs->trans("DefaultPriceLog"), 0, $_SERVER["PHP_SELF"], '', '', '', $backbutton, 0, $num, 'title_accountancy.png');
1464  else print_barre_liste($langs->trans("PriceByCustomerLog"), 0, $_SERVER["PHP_SELF"], '', '', '', '', 0, $num, 'title_accountancy.png');
1465 
1466  print '<div class="div-table-responsive">';
1467  print '<table class="liste centpercent">';
1468 
1469  print '<tr class="liste_titre">';
1470  print '<td>'.$langs->trans("AppliedPricesFrom").'</td>';
1471 
1472  if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1473  print '<td class="center">'.$langs->trans("PriceLevel").'</td>';
1474  }
1475  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1476  print '<td class="center">'.$langs->trans("Type").'</td>';
1477  }
1478 
1479  print '<td class="center">'.$langs->trans("PriceBase").'</td>';
1480  print $conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL;
1481  if (empty($conf->global->PRODUIT_MULTIPRICES) && empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) print '<td class="right">'.$langs->trans("DefaultTaxRate").'</td>';
1482  print '<td class="right">'.$langs->trans("HT").'</td>';
1483  print '<td class="right">'.$langs->trans("TTC").'</td>';
1484  if (!empty($conf->dynamicprices->enabled)) {
1485  print '<td class="right">'.$langs->trans("PriceExpressionSelected").'</td>';
1486  }
1487  print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("HT").'</td>';
1488  print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("TTC").'</td>';
1489  print '<td class="right">'.$langs->trans("ChangedBy").'</td>';
1490  if ($user->rights->produit->supprimer)
1491  print '<td class="right">&nbsp;</td>';
1492  print '</tr>';
1493 
1494  $notfirstlineforlevel = array();
1495 
1496  $i = 0;
1497  while ($i < $num)
1498  {
1499  $objp = $db->fetch_object($result);
1500 
1501  print '<tr class="oddeven">';
1502  // Date
1503  print "<td>".dol_print_date($db->jdate($objp->dp), "dayhour", 'tzuserrel')."</td>";
1504 
1505  // Price level
1506  if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
1507  print '<td class="center">'.$objp->price_level."</td>";
1508  }
1509  // Price by quantity
1510  if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))
1511  {
1512  $type = ($objp->price_by_qty == 1) ? 'PriceByQuantity' : 'Standard';
1513  print '<td class="center">'.$langs->trans($type)."</td>";
1514  }
1515 
1516  print '<td class="center">';
1517  if (empty($objp->price_by_qty)) {
1518  print $langs->trans($objp->price_base_type);
1519  }
1520  print "</td>";
1521 
1522  if (empty($conf->global->PRODUIT_MULTIPRICES) && empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))
1523  {
1524  print '<td class="right">';
1525 
1526  if (empty($objp->price_by_qty)) {
1527  $positiverates = '';
1528  if (price2num($objp->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($objp->tva_tx);
1529  if (price2num($objp->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($objp->localtax1_tx);
1530  if (price2num($objp->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($objp->localtax2_tx);
1531  if (empty($positiverates)) $positiverates = '0';
1532  echo vatrate($positiverates.($objp->default_vat_code ? ' ('.$objp->default_vat_code.')' : ''), '%', $objp->tva_npr);
1533  /*
1534  if ($objp->default_vat_code)
1535  {
1536  print vatrate($objp->tva_tx, true) . ' ('.$objp->default_vat_code.')';
1537  }
1538  else print vatrate($objp->tva_tx, true, $objp->recuperableonly);*/
1539  }
1540 
1541  print "</td>";
1542  }
1543 
1544  // Price
1545  if (!empty($objp->fk_price_expression) && !empty($conf->dynamicprices->enabled))
1546  {
1547  $price_expression = new PriceExpression($db);
1548  $res = $price_expression->fetch($objp->fk_price_expression);
1549  $title = $price_expression->title;
1550  print '<td class="right"></td>';
1551  print '<td class="right"></td>';
1552  print '<td class="right">'.$title."</td>";
1553  } else {
1554  print '<td class="right">';
1555  if (empty($objp->price_by_qty)) {
1556  print price($objp->price);
1557  }
1558  print "</td>";
1559  print '<td class="right">';
1560  if (empty($objp->price_by_qty)) {
1561  print price($objp->price_ttc);
1562  }
1563  print "</td>";
1564  if (!empty($conf->dynamicprices->enabled)) { //Only if module is enabled
1565  print '<td class="right"></td>';
1566  }
1567  }
1568 
1569  print '<td class="right">';
1570  if (empty($objp->price_by_qty)) {
1571  print price($objp->price_min);
1572  }
1573  print '</td>';
1574 
1575  print '<td class="right">';
1576  if (empty($objp->price_by_qty)) {
1577  print price($objp->price_min_ttc);
1578  }
1579  print '</td>';
1580 
1581  // User
1582  print '<td class="right">';
1583  if ($objp->user_id > 0) {
1584  $userstatic = new User($db);
1585  $userstatic->fetch($objp->user_id);
1586  print $userstatic->getNomUrl(1, '', 0, 0, 24, 0, 'login');
1587  }
1588  print '</td>';
1589 
1590  // Action
1591  if ($user->rights->produit->supprimer)
1592  {
1593  $candelete = 0;
1594  if (!empty($conf->global->PRODUIT_MULTIPRICES) || !empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES))
1595  {
1596  if (empty($notfirstlineforlevel[$objp->price_level])) $notfirstlineforlevel[$objp->price_level] = 1;
1597  else $candelete = 1;
1598  } elseif ($i > 0) $candelete = 1;
1599 
1600  print '<td class="right">';
1601  if ($candelete)
1602  {
1603  print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete&amp;token='.newToken().'&amp;id='.$object->id.'&amp;lineid='.$objp->rowid.'">';
1604  print img_delete();
1605  print '</a>';
1606  } else print '&nbsp;'; // Can not delete last price (it's current price)
1607  print '</td>';
1608  }
1609 
1610  print "</tr>\n";
1611  $i++;
1612  }
1613 
1614  $db->free($result);
1615  print "</table>";
1616  print '</div>';
1617  print "<br>";
1618  }
1619 
1620  print '</div>';
1621  } else {
1622  dol_print_error($db);
1623  }
1624 }
1625 
1626 
1627 // Add area to show/add/edit a price for a dedicated customer
1628 if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES))
1629 {
1630  $prodcustprice = new Productcustomerprice($db);
1631 
1632  $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
1633  $sortfield = GETPOST("sortfield", 'alpha');
1634  $sortorder = GETPOST("sortorder", 'alpha');
1635  $page = (GETPOST("page", 'int') ?GETPOST("page", 'int') : 0);
1636  if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
1637  $offset = $limit * $page;
1638  $pageprev = $page - 1;
1639  $pagenext = $page + 1;
1640  if (!$sortorder)
1641  $sortorder = "ASC";
1642  if (!$sortfield)
1643  $sortfield = "soc.nom";
1644 
1645  // Build filter to diplay only concerned lines
1646  $filter = array('t.fk_product' => $object->id);
1647 
1648  if (!empty($search_soc)) {
1649  $filter['soc.nom'] = $search_soc;
1650  }
1651 
1652  if ($action == 'add_customer_price') {
1653  // Form to add a new customer price
1654  $maxpricesupplier = $object->min_recommended_price();
1655 
1656  print load_fiche_titre($langs->trans('PriceByCustomer'));
1657 
1658  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1659  print '<input type="hidden" name="token" value="'.newToken().'">';
1660  print '<input type="hidden" name="action" value="add_customer_price_confirm">';
1661  print '<input type="hidden" name="id" value="'.$object->id.'">';
1662 
1663  print '<table class="liste centpercent">';
1664  print '<tr>';
1665  print '<td class="fieldrequired">'.$langs->trans('ThirdParty').'</td>';
1666  print '<td>';
1667  print img_picto('', 'company').$form->select_company('', 'socid', 's.client IN (1,2,3)', 'SelectThirdParty', 0, 0, array(), 0, 'minwidth300');
1668  print '</td>';
1669  print '</tr>';
1670 
1671  // VAT
1672  print '<tr><td class="fieldrequired">'.$langs->trans("DefaultTaxRate").'</td><td>';
1673  print $form->load_tva("tva_tx", $object->default_vat_code ? $object->tva_tx.' ('.$object->default_vat_code.')' : $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
1674  print '</td></tr>';
1675 
1676  // Price base
1677  print '<tr><td class="fieldrequired">';
1678  print $langs->trans('PriceBase');
1679  print '</td>';
1680  print '<td>';
1681  print $form->selectPriceBaseType($object->price_base_type, "price_base_type");
1682  print '</td>';
1683  print '</tr>';
1684 
1685  // Price
1686  print '<tr><td class="fieldrequired">';
1687  $text = $langs->trans('SellingPrice');
1688  print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
1689  print '</td><td>';
1690  if ($object->price_base_type == 'TTC') {
1691  print '<input name="price" size="10" value="'.price($object->price_ttc).'">';
1692  } else {
1693  print '<input name="price" size="10" value="'.price($object->price).'">';
1694  }
1695  print '</td></tr>';
1696 
1697  // Price minimum
1698  print '<tr><td>';
1699  $text = $langs->trans('MinPrice');
1700  print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
1701  if ($object->price_base_type == 'TTC') {
1702  print '<td><input name="price_min" size="10" value="'.price($object->price_min_ttc).'">';
1703  } else {
1704  print '<td><input name="price_min" size="10" value="'.price($object->price_min).'">';
1705  }
1706  if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE))
1707  {
1708  print '<td class="left">'.$langs->trans("MinimumRecommendedPrice", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
1709  }
1710  print '</td></tr>';
1711 
1712  print '</table>';
1713 
1714 
1715  print '<div class="center">';
1716 
1717  // Update all child soc
1718  print '<div class="marginbottomonly">';
1719  print '<input type="checkbox" name="updatechildprice" id="updatechildprice" value="1"> ';
1720  print '<label for="updatechildprice">'.$langs->trans('ForceUpdateChildPriceSoc').'</label>';
1721  print '</div>';
1722 
1723  print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
1724  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
1725  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
1726  print '</div>';
1727 
1728  print '</form>';
1729  } elseif ($action == 'edit_customer_price')
1730  {
1731  // Edit mode
1732  $maxpricesupplier = $object->min_recommended_price();
1733 
1734  print load_fiche_titre($langs->trans('PriceByCustomer'));
1735 
1736  $result = $prodcustprice->fetch(GETPOST('lineid', 'int'));
1737  if ($result < 0) {
1738  setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
1739  }
1740 
1741  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1742  print '<input type="hidden" name="token" value="'.newToken().'">';
1743  print '<input type="hidden" name="action" value="update_customer_price_confirm">';
1744  print '<input type="hidden" name="lineid" value="'.$prodcustprice->id.'">';
1745 
1746  print '<table class="liste centpercent">';
1747  print '<tr>';
1748  print '<td class="titlefield fieldrequired">'.$langs->trans('ThirdParty').'</td>';
1749  $staticsoc = new Societe($db);
1750  $staticsoc->fetch($prodcustprice->fk_soc);
1751  print "<td>".$staticsoc->getNomUrl(1)."</td>";
1752  print '</tr>';
1753 
1754  // VAT
1755  print '<tr><td class="fieldrequired">'.$langs->trans("DefaultTaxRate").'</td><td>';
1756  print $form->load_tva("tva_tx", $prodcustprice->default_vat_code ? $prodcustprice->tva_tx.' ('.$prodcustprice->default_vat_code.')' : $prodcustprice->tva_tx, $mysoc, '', $object->id, $prodcustprice->recuperableonly, $object->type, false, 1);
1757  print '</td></tr>';
1758 
1759  // Price base
1760  print '<tr><td class="fieldrequired">';
1761  print $langs->trans('PriceBase');
1762  print '</td>';
1763  print '<td>';
1764  print $form->selectPriceBaseType($prodcustprice->price_base_type, "price_base_type");
1765  print '</td>';
1766  print '</tr>';
1767 
1768  // Price
1769  print '<tr><td class="fieldrequired">';
1770  $text = $langs->trans('SellingPrice');
1771  print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
1772  print '</td><td>';
1773  if ($prodcustprice->price_base_type == 'TTC') {
1774  print '<input name="price" size="10" value="'.price($prodcustprice->price_ttc).'">';
1775  } else {
1776  print '<input name="price" size="10" value="'.price($prodcustprice->price).'">';
1777  }
1778  print '</td></tr>';
1779 
1780  // Price minimum
1781  print '<tr><td>';
1782  $text = $langs->trans('MinPrice');
1783  print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
1784  print '</td><td>';
1785  if ($prodcustprice->price_base_type == 'TTC') {
1786  print '<input name="price_min" size="10" value="'.price($prodcustprice->price_min_ttc).'">';
1787  } else {
1788  print '<input name="price_min" size="10" value="'.price($prodcustprice->price_min).'">';
1789  }
1790  print '</td>';
1791  if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE))
1792  {
1793  print '<td class="left">'.$langs->trans("MinimumRecommendedPrice", price($maxpricesupplier, 0, '', 1, -1, -1, 'auto')).' '.img_warning().'</td>';
1794  }
1795  print '</tr>';
1796 
1797  print '</table>';
1798 
1799 
1800  print '<div class="center">';
1801  print '<div class="marginbottomonly">';
1802  print '<input type="checkbox" name="updatechildprice" id="updatechildprice" value="1"> ';
1803  print '<label for="updatechildprice">'.$langs->trans('ForceUpdateChildPriceSoc').'</label>';
1804  print "</div>";
1805 
1806  print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
1807  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
1808  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
1809  print '</div>';
1810 
1811  print '<br></form>';
1812  } elseif ($action == 'showlog_customer_price')
1813  {
1814  // List of all log of prices by customers
1815  print '<!-- list of all log of prices per customer -->'."\n";
1816 
1817  $filter = array('t.fk_product' => $object->id, 't.fk_soc' => GETPOST('socid', 'int'));
1818 
1819  // Count total nb of records
1820  $nbtotalofrecords = '';
1821  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
1822  $nbtotalofrecords = $prodcustprice->fetch_all_log($sortorder, $sortfield, $conf->liste_limit, $offset, $filter);
1823  }
1824 
1825  $result = $prodcustprice->fetch_all_log($sortorder, $sortfield, $conf->liste_limit, $offset, $filter);
1826  if ($result < 0) {
1827  setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
1828  }
1829 
1830  $option = '&socid='.GETPOST('socid', 'int').'&id='.$object->id;
1831 
1832  $staticsoc = new Societe($db);
1833  $staticsoc->fetch(GETPOST('socid', 'int'));
1834 
1835  $title = $langs->trans('PriceByCustomerLog');
1836  $title .= ' - '.$staticsoc->getNomUrl(1);
1837 
1838  $backbutton = '<a class="justalink" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">'.$langs->trans("Back").'</a>';
1839 
1840  print_barre_liste($title, $page, $_SERVER['PHP_SELF'], $option, $sortfield, $sortorder, $backbutton, count($prodcustprice->lines), $nbtotalofrecords, 'title_accountancy.png');
1841 
1842  if (count($prodcustprice->lines) > 0)
1843  {
1844  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1845  print '<input type="hidden" name="token" value="'.newToken().'">';
1846  print '<input type="hidden" name="id" value="'.$object->id.'">';
1847 
1848  print '<div class="div-table-responsive-no-min">';
1849  print '<table class="liste centpercent">';
1850 
1851  print '<tr class="liste_titre">';
1852  print '<td>'.$langs->trans("ThirdParty").'</td>';
1853  print '<td>'.$langs->trans("AppliedPricesFrom").'</td>';
1854  print '<td class="center">'.$langs->trans("PriceBase").'</td>';
1855  print '<td class="right">'.$langs->trans("DefaultTaxRate").'</td>';
1856  print '<td class="right">'.$langs->trans("HT").'</td>';
1857  if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1")
1858  {
1859  //print '<td class="right">' . $langs->trans("INCVATONLY") . '</td>';
1860  print '<td class="right">'.$langs->trans("INCT").'</td>';
1861  } else {
1862  print '<td class="right">'.$langs->trans("TTC").'</td>';
1863  }
1864  print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("HT").'</td>';
1865  print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("TTC").'</td>';
1866  print '<td class="right">'.$langs->trans("ChangedBy").'</td>';
1867  print '<td>&nbsp;</td>';
1868  print '</tr>';
1869 
1870  foreach ($prodcustprice->lines as $line)
1871  {
1872  // Date
1873  $staticsoc = new Societe($db);
1874  $staticsoc->fetch($line->fk_soc);
1875 
1876  $tva_tx = $line->default_vat_code ? $line->tva_tx.' ('.$line->default_vat_code.')' : $line->tva_tx;
1877 
1878  // Line for default price
1879  if ($line->price_base_type == 'HT')
1880  {
1881  $pu = $line->price;
1882  } else {
1883  $pu = $line->price_ttc;
1884  }
1885 
1886  // Local tax is not saved into table of product. We use value linked to VAT code.
1887  $localtaxarray = getLocalTaxesFromRate($line->tva_tx.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), 0, $staticsoc, $mysoc);
1888  // Define part of HT, VAT, TTC
1889  $resultarray = calcul_price_total(1, $pu, 0, $line->tva_tx, 1, 1, 0, $line->price_base_type, $line->recuperableonly, $object->type, $mysoc, $localtaxarray);
1890  // Calcul du total ht sans remise
1891  $total_ht = $resultarray[0];
1892  $total_vat = $resultarray[1];
1893  $total_localtax1 = $resultarray[9];
1894  $total_localtax2 = $resultarray[10];
1895  $total_ttc = $resultarray[2];
1896 
1897  print '<tr class="oddeven">';
1898 
1899  print "<td>".$staticsoc->getNomUrl(1)."</td>";
1900  print "<td>".dol_print_date($line->datec, "dayhour", 'tzuserrel')."</td>";
1901  print '<td class="center">'.$langs->trans($line->price_base_type)."</td>";
1902  print '<td class="right">';
1903 
1904  $positiverates = '';
1905  if (price2num($line->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($line->tva_tx);
1906  if (price2num($line->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax1_tx);
1907  if (price2num($line->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax2_tx);
1908  if (empty($positiverates)) $positiverates = '0';
1909 
1910  echo vatrate($positiverates.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), '%', ($line->tva_npr ? $line->tva_npr : $line->recuperableonly));
1911 
1912  //. vatrate($tva_tx, true, $line->recuperableonly) .
1913  print "</td>";
1914  print '<td class="right">'.price($line->price)."</td>";
1915 
1916  if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1")
1917  {
1918  //print '<td class="right">' . price($line->price_ttc) . "</td>";
1919  print '<td class="right">'.price($resultarray[2]).'</td>';
1920  } else {
1921  print '<td class="right">'.price($line->price_ttc)."</td>";
1922  }
1923 
1924  print '<td class="right">'.price($line->price_min).'</td>';
1925  print '<td class="right">'.price($line->price_min_ttc).'</td>';
1926 
1927  // User
1928  $userstatic = new User($db);
1929  $userstatic->fetch($line->fk_user);
1930  print '<td class="right">';
1931  print $userstatic->getNomUrl(1, '', 0, 0, 24, 0, 'login');
1932  //print $userstatic->getLoginUrl(1);
1933  print '</td>';
1934  print '</tr>';
1935  }
1936  print "</table>";
1937  print '</div>';
1938  } else {
1939  print $langs->trans('None');
1940  }
1941  } elseif ($action != 'showlog_default_price' && $action != 'edit_price')
1942  {
1943  // List of all prices by customers
1944  print '<!-- list of all prices per customer -->'."\n";
1945 
1946  // Count total nb of records
1947  $nbtotalofrecords = '';
1948  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
1949  $nbtotalofrecords = $prodcustprice->fetch_all($sortorder, $sortfield, 0, 0, $filter);
1950  }
1951 
1952  $result = $prodcustprice->fetch_all($sortorder, $sortfield, $conf->liste_limit, $offset, $filter);
1953  if ($result < 0) {
1954  setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
1955  }
1956 
1957  $option = '&search_soc='.$search_soc.'&id='.$object->id;
1958 
1959  print_barre_liste($langs->trans('PriceByCustomer'), $page, $_SERVER ['PHP_SELF'], $option, $sortfield, $sortorder, '', count($prodcustprice->lines), $nbtotalofrecords, 'title_accountancy.png');
1960 
1961  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
1962  print '<input type="hidden" name="token" value="'.newToken().'">';
1963  print '<input type="hidden" name="id" value="'.$object->id.'">';
1964 
1965  print '<div class="div-table-responsive-no-min">';
1966  print '<table class="liste centpercent">';
1967 
1968  if (count($prodcustprice->lines) > 0 || $search_soc)
1969  {
1970  $colspan = 8;
1971  //if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") $colspan++;
1972 
1973  print '<tr class="liste_titre">';
1974  print '<td class="liste_titre"><input type="text" class="flat" name="search_soc" value="'.$search_soc.'" size="20"></td>';
1975  print '<td class="liste_titre" colspan="'.$colspan.'">&nbsp;</td>';
1976  // Print the search button
1977  print '<td class="liste_titre maxwidthsearch">';
1978  $searchpicto = $form->showFilterAndCheckAddButtons(0);
1979  print $searchpicto;
1980  print '</td>';
1981  print '</tr>';
1982  }
1983 
1984  print '<tr class="liste_titre">';
1985  print '<td>'.$langs->trans("ThirdParty").'</td>';
1986  print '<td>'.$langs->trans("AppliedPricesFrom").'</td>';
1987  print '<td class="center">'.$langs->trans("PriceBase").'</td>';
1988  print '<td class="right">'.$langs->trans("DefaultTaxRate").'</td>';
1989  print '<td class="right">'.$langs->trans("HT").'</td>';
1990  if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1")
1991  {
1992  //print '<td class="right">' . $langs->trans("INCVATONLY") . '</td>';
1993  print '<td class="right">'.$langs->trans("INCT").'</td>';
1994  } else {
1995  print '<td class="right">'.$langs->trans("TTC").'</td>';
1996  }
1997 
1998  print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("HT").'</td>';
1999  print '<td class="right">'.$langs->trans("MinPrice").' '.$langs->trans("TTC").'</td>';
2000  print '<td class="right">'.$langs->trans("ChangedBy").'</td>';
2001  print '<td></td>';
2002  print '</tr>';
2003 
2004  // Line for default price
2005  if ($object->price_base_type == 'HT')
2006  {
2007  $pu = $object->price;
2008  } else {
2009  $pu = $object->price_ttc;
2010  }
2011 
2012  // Local tax is not saved into table of product. We use value linked to VAT code.
2013  $localtaxarray = getLocalTaxesFromRate($object->tva_tx.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), 0, $mysoc, $mysoc);
2014  // Define part of HT, VAT, TTC
2015  $resultarray = calcul_price_total(1, $pu, 0, $object->tva_tx, 1, 1, 0, $object->price_base_type, $object->recuperableonly, $object->type, $mysoc, $localtaxarray);
2016  // Calcul du total ht sans remise
2017  $total_ht = $resultarray[0];
2018  $total_vat = $resultarray[1];
2019  $total_localtax1 = $resultarray[9];
2020  $total_localtax2 = $resultarray[10];
2021  $total_ttc = $resultarray[2];
2022 
2023  print '<tr class="oddeven">';
2024  print "<td>".$langs->trans("Default")."</td>";
2025  print "<td></td>";
2026 
2027  print '<td class="center">'.$langs->trans($object->price_base_type)."</td>";
2028  print '<td class="right">';
2029 
2030  $positiverates = '';
2031  if (price2num($object->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
2032  if (price2num($object->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
2033  if (price2num($object->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
2034  if (empty($positiverates)) $positiverates = '0';
2035  echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), '%', $object->tva_npr);
2036 
2037  //print vatrate($object->tva_tx, true, $object->tva_npr);
2038  //print $object->default_vat_code?' ('.$object->default_vat_code.')':'';
2039  print "</td>";
2040 
2041  print '<td class="right">'.price($object->price)."</td>";
2042 
2043  if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1")
2044  {
2045  //print '<td class="right">' . price($object->price_ttc) . "</td>";
2046  print '<td class="right">'.price($resultarray[2]).'</td>';
2047  } else {
2048  print '<td class="right">'.price($object->price_ttc)."</td>";
2049  }
2050 
2051 
2052  print '<td class="right">'.price($object->price_min).'</td>';
2053  print '<td class="right">'.price($object->price_min_ttc).'</td>';
2054  print '<td class="right">';
2055  print '</td>';
2056  if ($user->rights->produit->supprimer || $user->rights->service->supprimer)
2057  {
2058  print '<td class="nowraponall">';
2059  print '<a class="marginleftonly marginrightonly" href="'.$_SERVER["PHP_SELF"].'?action=showlog_default_price&amp;id='.$object->id.'">';
2060  print img_info($langs->trans('PriceByCustomerLog'));
2061  print '</a>';
2062  print ' ';
2063  print '<a class="marginleftonly marginrightonly editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit_price&amp;id='.$object->id.'">';
2064  print img_edit('default', 0, 'style="vertical-align: middle;"');
2065  print '</a>';
2066  print '</td>';
2067  }
2068  print "</tr>\n";
2069 
2070  if (count($prodcustprice->lines) > 0)
2071  {
2072  foreach ($prodcustprice->lines as $line)
2073  {
2074  // Date
2075  $staticsoc = new Societe($db);
2076  $staticsoc->fetch($line->fk_soc);
2077 
2078  $tva_tx = $line->default_vat_code ? $line->tva_tx.' ('.$line->default_vat_code.')' : $line->tva_tx;
2079 
2080  // Line for default price
2081  if ($line->price_base_type == 'HT')
2082  {
2083  $pu = $line->price;
2084  } else {
2085  $pu = $line->price_ttc;
2086  }
2087 
2088  // Local tax is not saved into table of product. We use value linked to VAT code.
2089  $localtaxarray = getLocalTaxesFromRate($line->tva_tx.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), 0, $staticsoc, $mysoc);
2090  // Define part of HT, VAT, TTC
2091  $resultarray = calcul_price_total(1, $pu, 0, $line->tva_tx, 1, 1, 0, $line->price_base_type, $line->recuperableonly, $object->type, $mysoc, $localtaxarray);
2092  // Calcul du total ht sans remise
2093  $total_ht = $resultarray[0];
2094  $total_vat = $resultarray[1];
2095  $total_localtax1 = $resultarray[9];
2096  $total_localtax2 = $resultarray[10];
2097  $total_ttc = $resultarray[2];
2098 
2099  print '<tr class="oddeven">';
2100 
2101  print "<td>".$staticsoc->getNomUrl(1)."</td>";
2102  print "<td>".dol_print_date($line->datec, "dayhour", 'tzuserrel')."</td>";
2103  print '<td class="center">'.$langs->trans($line->price_base_type)."</td>";
2104  print '<td class="right">';
2105 
2106  $positiverates = '';
2107  if (price2num($line->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($line->tva_tx);
2108  if (price2num($line->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax1_tx);
2109  if (price2num($line->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($line->localtax2_tx);
2110  if (empty($positiverates)) $positiverates = '0';
2111 
2112  echo vatrate($positiverates.($line->default_vat_code ? ' ('.$line->default_vat_code.')' : ''), '%', ($line->tva_npr ? $line->tva_npr : $line->recuperableonly));
2113 
2114  print "</td>";
2115  print '<td class="right">'.price($line->price)."</td>";
2116 
2117  if ($mysoc->localtax1_assuj == "1" || $mysoc->localtax2_assuj == "1") {
2118  //print '<td class="right">' . price($line->price_ttc) . "</td>";
2119  print '<td class="right">'.price($resultarray[2]).'</td>';
2120  } else {
2121  print '<td class="right">'.price($line->price_ttc)."</td>";
2122  }
2123 
2124  print '<td class="right">'.price($line->price_min).'</td>';
2125  print '<td class="right">'.price($line->price_min_ttc).'</td>';
2126 
2127  // User
2128  $userstatic = new User($db);
2129  $userstatic->fetch($line->fk_user);
2130  print '<td class="right">';
2131  print $userstatic->getNomUrl(1, '', 0, 0, 24, 0, 'login');
2132  print '</td>';
2133 
2134  // Todo Edit or delete button
2135  // Action
2136  if ($user->rights->produit->supprimer || $user->rights->service->supprimer)
2137  {
2138  print '<td class="right nowraponall">';
2139  print '<a href="'.$_SERVER["PHP_SELF"].'?action=showlog_customer_price&amp;id='.$object->id.'&amp;socid='.$line->fk_soc.'">';
2140  print img_info($langs->trans('PriceByCustomerLog'));
2141  print '</a>';
2142  print ' ';
2143  print '<a class="marginleftonly editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit_customer_price&amp;id='.$object->id.'&amp;lineid='.$line->id.'">';
2144  print img_edit('default', 0, 'style="vertical-align: middle;"');
2145  print '</a>';
2146  print ' ';
2147  print '<a class="marginleftonly" href="'.$_SERVER["PHP_SELF"].'?action=delete_customer_price&amp;token='.newToken().'&amp;id='.$object->id.'&amp;lineid='.$line->id.'">';
2148  print img_delete('default', 'style="vertical-align: middle;"');
2149  print '</a>';
2150  print '</td>';
2151  }
2152 
2153  print "</tr>\n";
2154  }
2155  }
2156 
2157  print "</table>";
2158  print '</div>';
2159 
2160  print "</form>";
2161  }
2162 }
2163 
2164 // End of page
2165 llxFooter();
2166 $db->close();
File of class to manage predefined price products or services by customer.
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
img_edit($titlealt= 'default', $float=0, $other= '')
Show logo editer/modifier fiche.
Class to parse product price expressions.
Class to manage products or services.
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).
Definition: admin.lib.php:575
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller= '', $localtaxes_array= '', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code= '')
Calculate totals (net, vat, ...) of a line.
Definition: price.lib.php:86
Class to manage Dolibarr users.
Definition: user.class.php:44
const TYPE_SERVICE
Service.
const TYPE_PRODUCT
Regular product.
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate, from a $thirdparty_buyer to a $thirdparty_seller Note: This function applies same rules than get_default_tva.
img_warning($titlealt= 'default', $moreatt= '', $morecss= 'pictowarning')
Show warning logo.
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...
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
print_barre_liste($titre, $page, $file, $options= '', $sortfield= '', $sortorder= '', $morehtmlcenter= '', $num=-1, $totalnboflines= '', $picto= 'generic', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow= '')
Print a title with navigation controls for pagination.
Class to manage generation of HTML components Only common components must be here.
Class to manage third parties objects (customers, suppliers, prospects...)
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Class for accesing price expression table.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0)
Return a string with VAT rate label formated for view output Used into pdf and HTML pages...
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_info($titlealt= 'default')
Show info logo.
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
dol_banner_tab($object, $paramid, $morehtml= '', $shownav=1, $fieldid= 'rowid', $fieldref= 'ref', $morehtmlref= '', $moreparam= '', $nodbprefix=0, $morehtmlleft= '', $morehtmlstatus= '', $onlybanner=0, $morehtmlright= '')
Show tab footer of a card.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
llxFooter()
Empty footer.
Definition: wrapper.php:59
img_delete($titlealt= 'default', $other= 'class="pictodelete"', $morecss= '')
Show delete logo.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35