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