dolibarr  16.0.5
price_rules.php
1 <?php
22 require '../../main.inc.php';
23 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
24 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
25 
26 // Load translation files required by the page
27 $langs->loadLangs(array('admin', 'products'));
28 
29 $action = GETPOST('action', 'aZ09');
30 
31 // Security check
32 if (!$user->admin || (empty($conf->product->enabled) && empty($conf->service->enabled))) {
34 }
35 
36 $error = 0;
37 
38 
43 if ($action == 'update') {
44  $var_percent = GETPOST('var_percent', 'array');
45  $var_min_percent = GETPOST('var_min_percent', 'array');
46  $fk_level = GETPOST('fk_level', 'array');
47 
48  for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
49  $check = isset($var_min_percent[$i]);
50 
51  if ($i != 1) {
52  $check = $check && isset($var_percent[$i]) && isset($fk_level[$i]);
53  }
54 
55  if (!$check) {
56  continue;
57  }
58 
59  $i_var_percent = 0;
60 
61  // Set $i_var_percent, the percent of price for level compared to an other level
62  if ($i != 1) {
63  $i_var_percent = (float) price2num($var_percent[$i]);
64  }
65 
66  $i_var_min_percent = (float) price2num($var_min_percent[$i]);
67  $i_fk_level = (int) $fk_level[$i];
68 
69  if ($i == 1) {
70  $check1 = true;
71  $check2 = $i_var_min_percent;
72  } else {
73  $check1 = $i_fk_level >= 1 && $i_fk_level <= $conf->global->PRODUIT_MULTIPRICES_LIMIT;
74  $check2 = $i_var_percent && ($i_var_min_percent || (string) $i_var_min_percent === '0');
75  }
76 
77  if (empty($i_var_percent) && empty($i_var_min_percent)) {
78  //If the level is between range but percent fields are empty, then we ensure it does not exist in DB
79  $db->query("DELETE FROM ".MAIN_DB_PREFIX."product_pricerules WHERE level = ".((int) $i));
80  continue;
81  }
82 
83  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_pricerules (level, fk_level, var_percent, var_min_percent) VALUES (";
84  $sql .= ((int) $i).", ".$db->escape($i_fk_level).", ".$i_var_percent.", ".$i_var_min_percent.")";
85 
86  if (!$db->query($sql)) {
87  //If we could not create, then we try updating
88  $sql = "UPDATE ".MAIN_DB_PREFIX."product_pricerules";
89  $sql .= " SET fk_level = ".$db->escape($i_fk_level).", var_percent = ".$i_var_percent.", var_min_percent = ".$i_var_min_percent." WHERE level = ".((int) $i);
90 
91  if (!$db->query($sql)) {
92  setEventMessages($langs->trans('ErrorSavingChanges'), null, 'errors');
93  $error++;
94  }
95  }
96  }
97 
98  if (!$error) {
99  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
100  }
101 }
102 
103 
104 /*
105  * View
106  */
107 
108 $sql = "SELECT rowid, level, fk_level, var_percent, var_min_percent";
109 $sql .= " FROM ".MAIN_DB_PREFIX."product_pricerules";
110 $query = $db->query($sql);
111 
112 $rules = array();
113 
114 while ($result = $db->fetch_object($query)) {
115  $rules[$result->level] = $result;
116 }
117 
118 $title = $langs->trans('ProductServiceSetup');
119 $tab = $langs->trans("ProductsAndServices");
120 
121 if (empty($conf->product->enabled)) {
122  $title = $langs->trans('ServiceSetup');
123  $tab = $langs->trans('Services');
124 } elseif (empty($conf->service->enabled)) {
125  $title = $langs->trans('ProductSetup');
126  $tab = $langs->trans('Products');
127 }
128 
129 llxHeader('', $langs->trans('MultipriceRules'));
130 
131 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
132 print load_fiche_titre($title, $linkback, 'title_setup');
133 
134 
135 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
136 print '<input type="hidden" name="token" value="'.newToken().'">';
137 print '<input type="hidden" name="action" value="update">';
138 
140 print dol_get_fiche_head($head, 'generator', $tab, -1, 'product');
141 
142 print '<span class="opacitymedium">'.$langs->trans("MultiPriceRuleDesc").'</span><br><br>';
143 
144 // Array that contains the number of prices available
145 $price_options = array();
146 
147 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
148  $price_options[$i] = $langs->trans('SellingPrice').' '.$i;
149 }
150 ?>
151 
152  <table class="noborder centpercent">
153  <tr class="liste_titre">
154  <td style="text-align: center"><?php echo $langs->trans('PriceLevel') ?></td>
155  <td style="text-align: center"><?php echo $langs->trans('Price') ?></td>
156  <td style="text-align: center"><?php echo $langs->trans('MinPrice') ?></td></tr>
157  <tr>
158  <td class="fieldrequired" style="text-align: center"><?php echo $langs->trans('SellingPrice') ?> 1</td>
159  <td></td>
160  <td style="text-align: center"><input type="text" style="text-align: right" name="var_min_percent[1]" size="5" value="<?php echo price(isset($rules[1]) ? $rules[1]->var_min_percent : 0, 2) ?>"> <?php echo $langs->trans('PercentDiscountOver', $langs->trans('SellingPrice').' 1') ?></td>
161  </tr>
162  <?php for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) : ?>
163  <tr>
164  <td class="fieldrequired" style="text-align: center"><?php
165  echo $langs->trans('SellingPrice').' '.$i;
166  // Label of price
167  $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i;
168  if (!empty($conf->global->$keyforlabel)) {
169  print ' - '.$langs->trans($conf->global->$keyforlabel);
170  }
171  ?>
172  </td>
173  <td style="text-align: center">
174  <input type="text" style="text-align: right" name="var_percent[<?php echo $i ?>]" size="5" value="<?php echo price(isset($rules[$i]) ? $rules[$i]->var_percent : 0, 2) ?>">
175  <?php
176  $return = array();
177  for ($j = 1; $j < $i; $j++) {
178  $return[$j] = $price_options[$j];
179  }
180  $texttoshow = $langs->trans('PercentVariationOver', '{s1}');
181  $texttoshow = str_replace('{s1}', Form::selectarray("fk_level[$i]", $return, (isset($rules[$i]) ? $rules[$i]->fk_level : null)), $texttoshow);
182  print $texttoshow;
183  ?>
184  </td>
185  <td style="text-align: center">
186  <input type="text" style="text-align: right" name="var_min_percent[<?php echo $i ?>]" size="5" value="<?php echo price(isset($rules[$i]) ? $rules[$i]->var_min_percent : 0, 2) ?>">
187  <?php echo $langs->trans('PercentDiscountOver', $langs->transnoentitiesnoconv('SellingPrice').' '.$i) ?>
188  </td>
189  </tr>
190  <?php endfor ?>
191  </table>
192 
193 <?php
194 
195 print dol_get_fiche_end();
196 
197 print '<div style="text-align: center">
198  <input type="submit" value="'.$langs->trans("Save").'" class="button button-save">
199  </div>';
200 
201 print '</form>';
202 
203 // End of page
204 llxFooter();
205 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
name
$conf db name
Definition: repair.php:122
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
product_admin_prepare_head
product_admin_prepare_head()
Return array head with list of tabs to view object informations.
Definition: product.lib.php:275
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
Form\selectarray
static selectarray($htmlname, $array, $id='', $show_empty=0, $key_in_label=0, $value_as_key=0, $moreparam='', $translate=0, $maxlen=0, $disabled=0, $sort='', $morecss='', $addjscombo=1, $moreparamonempty='', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
Definition: html.form.class.php:7879
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
float
div float
Buy price without taxes.
Definition: style.css.php:809