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