dolibarr  13.0.2
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 $error = 0;
36 
37 
42 if ($action == 'update') {
43  $var_percent = GETPOST('var_percent', 'array');
44  $var_min_percent = GETPOST('var_min_percent', 'array');
45  $fk_level = GETPOST('fk_level', 'array');
46 
47  for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
48  $check = isset($var_min_percent[$i]);
49 
50  if ($i != 1) {
51  $check = $check && isset($var_percent[$i]) && isset($fk_level[$i]);
52  }
53 
54  if (!$check) {
55  continue;
56  }
57 
58  $i_var_percent = 0;
59 
60  // Set $i_var_percent, the percent of price for level compared to an other level
61  if ($i != 1) {
62  $i_var_percent = (float) price2num($var_percent[$i]);
63  }
64 
65  $i_var_min_percent = (float) price2num($var_min_percent[$i]);
66  $i_fk_level = (int) $fk_level[$i];
67 
68  if ($i == 1) {
69  $check1 = true;
70  $check2 = $i_var_min_percent;
71  } else {
72  $check1 = $i_fk_level >= 1 && $i_fk_level <= $conf->global->PRODUIT_MULTIPRICES_LIMIT;
73  $check2 = $i_var_percent && ($i_var_min_percent || (string) $i_var_min_percent === '0');
74  }
75 
76  if (empty($i_var_percent) && empty($i_var_min_percent)) {
77  //If the level is between range but percent fields are empty, then we ensure it does not exist in DB
78  $db->query("DELETE FROM ".MAIN_DB_PREFIX."product_pricerules WHERE level = ".((int) $i));
79  continue;
80  }
81 
82  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_pricerules (level, fk_level, var_percent, var_min_percent) VALUES (";
83  $sql .= ((int) $i).", ".$db->escape($i_fk_level).", ".$i_var_percent.", ".$i_var_min_percent.")";
84 
85  if (!$db->query($sql)) {
86  //If we could not create, then we try updating
87  $sql = "UPDATE ".MAIN_DB_PREFIX."product_pricerules";
88  $sql .= " SET fk_level = ".$db->escape($i_fk_level).", var_percent = ".$i_var_percent.", var_min_percent = ".$i_var_min_percent." WHERE level = ".$i;
89 
90  if (!$db->query($sql)) {
91  setEventMessages($langs->trans('ErrorSavingChanges'), null, 'errors');
92  $error++;
93  }
94  }
95  }
96 
97  if (!$error) {
98  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
99  }
100 }
101 
102 
103 /*
104  * View
105  */
106 
107 $sql = "SELECT rowid, level, fk_level, var_percent, var_min_percent";
108 $sql .= " FROM ".MAIN_DB_PREFIX."product_pricerules";
109 $query = $db->query($sql);
110 
111 $rules = array();
112 
113 while ($result = $db->fetch_object($query)) {
114  $rules[$result->level] = $result;
115 }
116 
117 $title = $langs->trans('ProductServiceSetup');
118 $tab = $langs->trans("ProductsAndServices");
119 
120 if (empty($conf->product->enabled)) {
121  $title = $langs->trans('ServiceSetup');
122  $tab = $langs->trans('Services');
123 } elseif (empty($conf->service->enabled)) {
124  $title = $langs->trans('ProductSetup');
125  $tab = $langs->trans('Products');
126 }
127 
128 llxHeader('', $langs->trans('MultipriceRules'));
129 
130 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
131 print load_fiche_titre($title, $linkback, 'title_setup');
132 
133 
134 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
135 print '<input type="hidden" name="token" value="'.newToken().'">';
136 print '<input type="hidden" name="action" value="update">';
137 
139 print dol_get_fiche_head($head, 'generator', $tab, -1, 'product');
140 
141 print '<span class="opacitymedium">'.$langs->trans("MultiPriceRuleDesc").'</span><br><br>';
142 
143 // Array that contains the number of prices available
144 $price_options = array();
145 
146 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
147  $price_options[$i] = $langs->trans('SellingPrice').' '.$i;
148 }
149 ?>
150 
151  <table class="noborder centpercent">
152  <tr class="liste_titre">
153  <td style="text-align: center"><?php echo $langs->trans('PriceLevel') ?></td>
154  <td style="text-align: center"><?php echo $langs->trans('Price') ?></td>
155  <td style="text-align: center"><?php echo $langs->trans('MinPrice') ?></td></tr>
156  <tr>
157  <td class="fieldrequired" style="text-align: center"><?php echo $langs->trans('SellingPrice') ?> 1</td>
158  <td></td>
159  <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>
160  </tr>
161  <?php for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++): ?>
162  <tr>
163  <td class="fieldrequired" style="text-align: center"><?php
164  echo $langs->trans('SellingPrice').' '.$i;
165  // Label of price
166  $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i;
167  if (!empty($conf->global->$keyforlabel)) {
168  print ' - '.$langs->trans($conf->global->$keyforlabel);
169  }
170  ?>
171  </td>
172  <td style="text-align: center">
173  <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) ?>">
174  <?php
175  $return = array();
176  for ($j = 1; $j < $i; $j++) {
177  $return[$j] = $price_options[$j];
178  }
179  $texttoshow = $langs->trans('PercentVariationOver', '{s1}');
180  $texttoshow = str_replace('{s1}', Form::selectarray("fk_level[$i]", $return, (isset($rules[$i]) ? $rules[$i]->fk_level : null)), $texttoshow);
181  print $texttoshow;
182  ?>
183  </td>
184  <td style="text-align: center">
185  <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) ?>">
186  <?php echo $langs->trans('PercentDiscountOver', $langs->transnoentitiesnoconv('SellingPrice').' '.$i) ?>
187  </td>
188  </tr>
189  <?php endfor ?>
190  </table>
191 
192 <?php
193 
195 
196 print '<div style="text-align: center">
197  <input type="submit" value="'.$langs->trans("Save").'" class="button button-save">
198  </div>';
199 
200 print '</form>';
201 
202 // End of page
203 llxFooter();
204 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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=0, $moreparamonempty= '', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:108
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
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;...
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 ...
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
div float
Buy price without taxes.
Definition: style.css.php:650
print
Draft customers invoices.
Definition: index.php:89
product_admin_prepare_head()
Return array head with list of tabs to view object informations.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
llxFooter()
Empty footer.
Definition: wrapper.php:59
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105