dolibarr 19.0.3
limits.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2022 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2009-2018 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
30
31// Load translation files required by the page
32$langs->loadLangs(array('companies', 'products', 'admin'));
33
34$action = GETPOST('action', 'aZ09');
35$cancel = GETPOST('cancel', 'aZ09');
36$currencycode = GETPOST('currencycode', 'alpha');
37
38if (isModEnabled('multicompany') && getDolGlobalString('MULTICURRENCY_USE_LIMIT_BY_CURRENCY')) {
39 // When MULTICURRENCY_USE_LIMIT_BY_CURRENCY is on, we use always a defined currency code instead of '' even for default.
40 $currencycode = (!empty($currencycode) ? $currencycode : $conf->currency);
41}
42
43$mainmaxdecimalsunit = 'MAIN_MAX_DECIMALS_UNIT'.(!empty($currencycode) ? '_'.$currencycode : '');
44$mainmaxdecimalstot = 'MAIN_MAX_DECIMALS_TOT'.(!empty($currencycode) ? '_'.$currencycode : '');
45$mainmaxdecimalsshown = 'MAIN_MAX_DECIMALS_SHOWN'.(!empty($currencycode) ? '_'.$currencycode : '');
46$mainroundingruletot = 'MAIN_ROUNDING_RULE_TOT'.(!empty($currencycode) ? '_'.$currencycode : '');
47
48$valmainmaxdecimalsunit = GETPOST($mainmaxdecimalsunit, 'int');
49$valmainmaxdecimalstot = GETPOST($mainmaxdecimalstot, 'int');
50$valmainmaxdecimalsshown = GETPOST($mainmaxdecimalsshown, 'alpha'); // Can be 'x.y' but also 'x...'
51$valmainroundingruletot = price2num(GETPOST($mainroundingruletot, 'alphanohtml'), '', 2);
52
53if (!$user->admin) {
55}
56
57
58/*
59 * Actions
60 */
61
62if ($action == 'update' && !$cancel) {
63 $error = 0;
64 $MAXDEC = 8;
65 if ($valmainmaxdecimalsunit > $MAXDEC
66 || $valmainmaxdecimalstot > $MAXDEC
67 || $valmainmaxdecimalsshown > $MAXDEC) {
68 $error++;
69 setEventMessages($langs->trans("ErrorDecimalLargerThanAreForbidden", $MAXDEC), null, 'errors');
70 $action = 'edit';
71 }
72
73 if ($valmainmaxdecimalsunit < 0
74 || $valmainmaxdecimalstot < 0
75 || $valmainmaxdecimalsshown < 0) {
76 $langs->load("errors");
77 $error++;
78 setEventMessages($langs->trans("ErrorNegativeValueNotAllowed"), null, 'errors');
79 $action = 'edit';
80 }
81
82 if ($valmainroundingruletot) {
83 if ($valmainroundingruletot * pow(10, $valmainmaxdecimalstot) < 1) {
84 $langs->load("errors");
85 $error++;
86 setEventMessages($langs->trans("ErrorMAIN_ROUNDING_RULE_TOTCanMAIN_MAX_DECIMALS_TOT"), null, 'errors');
87 $action = 'edit';
88 }
89 }
90
91 if ((float) $valmainmaxdecimalsshown == 0) {
92 $langs->load("errors");
93 $error++;
94 setEventMessages($langs->trans("ErrorValueCantBeNull", dol_trunc(dol_string_nohtmltag($langs->transnoentitiesnoconv("MAIN_MAX_DECIMALS_SHOWN")), 40)), null, 'errors');
95 $action = 'edit';
96 }
97 if (! $error && ((float) $valmainmaxdecimalsshown < $valmainmaxdecimalsunit || (float) $valmainmaxdecimalsshown < $valmainmaxdecimalstot)) {
98 $langs->load("errors");
99 $error++;
100 setEventMessages($langs->trans("ErrorValueForTooLow", dol_trunc(dol_string_nohtmltag($langs->transnoentitiesnoconv("MAIN_MAX_DECIMALS_SHOWN")), 40)), null, 'errors');
101 $action = 'edit';
102 }
103
104 if (!$error) {
105 dolibarr_set_const($db, $mainmaxdecimalsunit, $valmainmaxdecimalsunit, 'chaine', 0, '', $conf->entity);
106 dolibarr_set_const($db, $mainmaxdecimalstot, $valmainmaxdecimalstot, 'chaine', 0, '', $conf->entity);
107 dolibarr_set_const($db, $mainmaxdecimalsshown, $valmainmaxdecimalsshown, 'chaine', 0, '', $conf->entity);
108
109 dolibarr_set_const($db, $mainroundingruletot, $valmainroundingruletot, 'chaine', 0, '', $conf->entity);
110
111 header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home&leftmenu=setup".(!empty($currencycode) ? '&currencycode='.$currencycode : ''));
112 exit;
113 }
114}
115
116
117/*
118 * View
119 */
120
121$form = new Form($db);
122
123$title = $langs->trans("LimitsSetup");
124$help_url = '';
125
126llxHeader('', $title, $help_url);
127
128print load_fiche_titre($title, '', 'title_setup');
129
130$aCurrencies = array($conf->currency); // Default currency always first position
131
132if (isModEnabled('multicompany') && getDolGlobalString('MULTICURRENCY_USE_LIMIT_BY_CURRENCY')) {
133 require_once DOL_DOCUMENT_ROOT . '/core/lib/multicurrency.lib.php';
134
135 $sql = "SELECT rowid, code FROM " . MAIN_DB_PREFIX . "multicurrency";
136 $sql .= " WHERE entity = " . ((int) $conf->entity);
137 $sql .= " AND code <> '" . $db->escape($conf->currency) . "'"; // Default currency always first position
138 $resql = $db->query($sql);
139 if ($resql) {
140 while ($obj = $db->fetch_object($resql)) {
141 $aCurrencies[] = $obj->code;
142 }
143 }
144
145 if (!empty($aCurrencies) && count($aCurrencies) > 1) {
146 $head = multicurrencyLimitPrepareHead($aCurrencies);
147
148 print dol_get_fiche_head($head, $currencycode, '', -1, '');
149 }
150}
151
152print '<span class="opacitymedium">'.$langs->trans("LimitsDesc")."</span><br>\n";
153print "<br>\n";
154
155if ($action == 'edit') {
156 print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
157 print '<input type="hidden" name="token" value="' . newToken() . '">';
158 print '<input type="hidden" name="action" value="update">';
159 if (isModEnabled('multicompany') && getDolGlobalString('MULTICURRENCY_USE_LIMIT_BY_CURRENCY')) {
160 print '<input type="hidden" name="currencycode" value="' . $currencycode . '">';
161 }
162
163 clearstatcache();
164
165 print '<table class="noborder centpercent">';
166 print '<tr class="liste_titre"><td>'.$langs->trans("Parameters").'</td><td>'.$langs->trans("Value").'</td></tr>';
167
168 print '<tr class="oddeven"><td>';
169 print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_UNIT"), $langs->trans("ParameterActiveForNextInputOnly"));
170 print '</td><td><input class="flat right" name="'.$mainmaxdecimalsunit.'" size="3" value="'.(GETPOSTISSET($mainmaxdecimalsunit) ? GETPOST($mainmaxdecimalsunit) : getDolGlobalInt('MAIN_MAX_DECIMALS_UNIT', 0)).'"></td></tr>';
171
172 print '<tr class="oddeven"><td>';
173 print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
174 print '</td><td><input class="flat right" name="'.$mainmaxdecimalstot.'" size="3" value="'.(GETPOSTISSET($mainmaxdecimalstot) ? GETPOST($mainmaxdecimalstot) : getDolGlobalInt('MAIN_MAX_DECIMALS_TOT', 0)).'"></td></tr>';
175
176 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").'</td>';
177 print '<td><input class="flat right" name="'.$mainmaxdecimalsshown.'" size="3" value="'.(GETPOSTISSET($mainmaxdecimalsshown) ? GETPOST($mainmaxdecimalsshown) : getDolGlobalString('MAIN_MAX_DECIMALS_SHOWN')).'"></td></tr>';
178
179 print '<tr class="oddeven"><td>';
180 print $form->textwithpicto($langs->trans("MAIN_ROUNDING_RULE_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
181 print '</td><td><input class="flat right" name="'.$mainroundingruletot.'" size="3" value="'.(GETPOSTISSET($mainroundingruletot) ? GETPOST($mainroundingruletot) : getDolGlobalString('MAIN_ROUNDING_RULE_TOT')).'"></td></tr>';
182
183 print '</table>';
184
185 print '<div class="center">';
186 print '<input class="button button-save" type="submit" name="save" value="'.$langs->trans("Save").'">';
187 print ' &nbsp; ';
188 print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
189 print '</div>';
190 print '<br>';
191
192 print '</form>';
193 print '<br>';
194} else {
195 print '<div class="div-table-responsive-no-min">';
196 print '<table class="noborder centpercent">';
197 print '<tr class="liste_titre"><td>'.$langs->trans("Parameter").'</td><td class="right">'.$langs->trans("Value").'</td></tr>';
198
199 print '<tr class="oddeven"><td>';
200 print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_UNIT"), $langs->trans("ParameterActiveForNextInputOnly"));
201 print '</td><td align="right">'.(isset($conf->global->$mainmaxdecimalsunit) ? $conf->global->$mainmaxdecimalsunit : $conf->global->MAIN_MAX_DECIMALS_UNIT).'</td></tr>';
202
203 print '<tr class="oddeven"><td>';
204 print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
205 print '</td><td align="right">'.(isset($conf->global->$mainmaxdecimalstot) ? $conf->global->$mainmaxdecimalstot : $conf->global->MAIN_MAX_DECIMALS_TOT).'</td></tr>';
206
207 print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").'</td>';
208 print '<td align="right">'.(isset($conf->global->$mainmaxdecimalsshown) ? $conf->global->$mainmaxdecimalsshown : $conf->global->MAIN_MAX_DECIMALS_SHOWN).'</td></tr>';
209
210 print '<tr class="oddeven"><td>';
211 print $form->textwithpicto($langs->trans("MAIN_ROUNDING_RULE_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
212 print '</td><td align="right">'.(isset($conf->global->$mainroundingruletot) ? $conf->global->$mainroundingruletot : (getDolGlobalString('MAIN_ROUNDING_RULE_TOT') ? $conf->global->MAIN_ROUNDING_RULE_TOT : '')).'</td></tr>';
213
214 print '</table>';
215 print '</div>';
216
217 print '<div class="tabsAction">';
218 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().(!empty($currencycode) ? '&currencycode='.$currencycode : '').'">'.$langs->trans("Modify").'</a>';
219 print '</div>';
220}
221
222if (isModEnabled('multicompany') && getDolGlobalString('MULTICURRENCY_USE_LIMIT_BY_CURRENCY')) {
223 if (!empty($aCurrencies) && count($aCurrencies) > 1) {
224 print dol_get_fiche_end();
225 }
226}
227
228if (empty($mysoc->country_code)) {
229 $langs->load("errors");
230 $warnpicto = img_warning($langs->trans("WarningMandatorySetupNotComplete"));
231 print '<br><a href="'.DOL_URL_ROOT.'/admin/company.php?mainmenu=home">'.$warnpicto.' '.$langs->trans("WarningMandatorySetupNotComplete").'</a>';
232} else {
233 // Show examples
234 print load_fiche_titre($langs->trans("ExamplesWithCurrentSetup"), '', '');
235
236 print '<span class="opacitymedium">'.$langs->trans("Format").':</span> '.price(price2num(1234.56789, 'MT'), 0, $langs, 1, -1, -1, $currencycode)."<br>\n";
237
238 // Always show vat rates with vat 0
239 $s = 2 / 3;
240 $qty = 1;
241 $vat = 0;
242 $tmparray = calcul_price_total(1, $qty * price2num($s, 'MU'), 0, $vat, 0, 0, 0, 'HT', 0, 0, $mysoc);
243 print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
244 print ' x <span class="opacitymedium">'.$langs->trans("Quantity").":</span> ".$qty;
245 print ' - <span class="opacitymedium">'.$langs->trans("VAT").":</span> ".$vat.'%';
246 print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
247
248 $s = 10 / 3;
249 $qty = 1;
250 $vat = 0;
251 $tmparray = calcul_price_total(1, $qty * price2num($s, 'MU'), 0, $vat, 0, 0, 0, 'HT', 0, 0, $mysoc);
252 print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
253 print ' x <span class="opacitymedium">'.$langs->trans("Quantity").":</span> ".$qty;
254 print ' - <span class="opacitymedium">'.$langs->trans("VAT").":</span> ".$vat.'%';
255 print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
256
257 $s = 10 / 3;
258 $qty = 2;
259 $vat = 0;
260 $tmparray = calcul_price_total(1, $qty * price2num($s, 'MU'), 0, $vat, 0, 0, 0, 'HT', 0, 0, $mysoc);
261 print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
262 print ' x <span class="opacitymedium">'.$langs->trans("Quantity").":</span> ".$qty;
263 print ' - <span class="opacitymedium">'.$langs->trans("VAT").":</span> ".$vat.'%';
264 print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
265
266 // Add vat rates examples specific to country
267 $vat_rates = array();
268
269 $sql = "SELECT taux as vat_rate, t.code as vat_code, t.localtax1 as localtax_rate1, t.localtax2 as localtax_rate2";
270 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
271 $sql .= " WHERE t.active=1 AND t.fk_pays = c.rowid AND c.code='".$db->escape($mysoc->country_code)."' AND (t.taux <> 0 OR t.localtax1 <> '0' OR t.localtax2 <> '0')";
272 $sql .= " AND t.entity IN (".getEntity('c_tva').")";
273 $sql .= " ORDER BY t.taux ASC";
274 $resql = $db->query($sql);
275 if ($resql) {
276 $num = $db->num_rows($resql);
277 if ($num) {
278 for ($i = 0; $i < $num; $i++) {
279 $obj = $db->fetch_object($resql);
280 $vat_rates[] = array('vat_rate'=>$obj->vat_rate, 'code'=>$obj->vat_code, 'localtax_rate1'=>$obj->localtax_rate1, 'locltax_rate2'=>$obj->localtax_rate2);
281 }
282 }
283 } else {
284 dol_print_error($db);
285 }
286
287 if (count($vat_rates)) {
288 foreach ($vat_rates as $vatarray) {
289 $vat = $vatarray['vat_rate'];
290 for ($qty = 1; $qty <= 2; $qty++) {
291 $vattxt = $vat.($vatarray['code'] ? ' ('.$vatarray['code'].')' : '');
292
293 $localtax_array = getLocalTaxesFromRate($vattxt, 0, $mysoc, $mysoc);
294
295 $s = 10 / 3;
296 $tmparray = calcul_price_total($qty, price2num($s, 'MU'), 0, $vat, -1, -1, 0, 'HT', 0, 0, $mysoc, $localtax_array);
297 print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
298 print ' x <span class="opacitymedium">'.$langs->trans("Quantity").":</span> ".$qty;
299 print ' - <span class="opacitymedium">'.$langs->trans("VAT").':</span> '.$vat.'%';
300 print($vatarray['code'] ? ' ('.$vatarray['code'].')' : '');
301 print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ";
302 print $tmparray[0].' / '.$tmparray[1].($tmparray[9] ? '+'.$tmparray[9] : '').($tmparray[10] ? '+'.$tmparray[10] : '').' / '.$tmparray[2];
303 print "<br>\n";
304 }
305 }
306 } else {
307 // More examples if not specific vat rate found
308 // This example must be kept for test purpose with current value because value used (2/7, 10/3, and vat 0, 10)
309 // were calculated to show all possible cases of rounding. If we change this, examples becomes useless or show the same rounding rule.
310
311 $localtax_array = array();
312
313 $s = 10 / 3;
314 $qty = 1;
315 $vat = 10;
316 $tmparray = calcul_price_total($qty, price2num($s, 'MU'), 0, $vat, -1, -1, 0, 'HT', 0, 0, $mysoc, $localtax_array);
317 print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
318 print ' x <span class="opacitymedium">'.$langs->trans("Quantity").":</span> ".$qty;
319 print ' - <span class="opacitymedium">'.$langs->trans("VAT").":</span> ".$vat.'%';
320 print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
321
322 $s = 10 / 3;
323 $qty = 2;
324 $vat = 10;
325 $tmparray = calcul_price_total($qty, price2num($s, 'MU'), 0, $vat, -1, -1, 0, 'HT', 0, 0, $mysoc, $localtax_array);
326 print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
327 print ' x <span class="opacitymedium">'.$langs->trans("Quantity").":</span> ".$qty;
328 print ' - <span class="opacitymedium">'.$langs->trans("VAT").":</span> ".$vat.'%';
329 print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
330 }
331}
332
333// End of page
334llxFooter();
335$db->close();
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).
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
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.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
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.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
multicurrencyLimitPrepareHead($aCurrencies)
Prepare array with list of currency tabs.
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
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.