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