limits.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /**
  20. * \file htdocs/admin/limits.php
  21. * \brief Page to setup limits
  22. */
  23. // Load Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  27. // Load translation files required by the page
  28. $langs->loadLangs(array('companies', 'products', 'admin'));
  29. $action = GETPOST('action', 'aZ09');
  30. $cancel = GETPOST('cancel', 'aZ09');
  31. $currencycode = GETPOST('currencycode', 'alpha');
  32. if (isModEnabled('multicompany') && !empty($conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY)) {
  33. // When MULTICURRENCY_USE_LIMIT_BY_CURRENCY is on, we use always a defined currency code instead of '' even for default.
  34. $currencycode = (!empty($currencycode) ? $currencycode : $conf->currency);
  35. }
  36. $mainmaxdecimalsunit = 'MAIN_MAX_DECIMALS_UNIT'.(!empty($currencycode) ? '_'.$currencycode : '');
  37. $mainmaxdecimalstot = 'MAIN_MAX_DECIMALS_TOT'.(!empty($currencycode) ? '_'.$currencycode : '');
  38. $mainmaxdecimalsshown = 'MAIN_MAX_DECIMALS_SHOWN'.(!empty($currencycode) ? '_'.$currencycode : '');
  39. $mainroundingruletot = 'MAIN_ROUNDING_RULE_TOT'.(!empty($currencycode) ? '_'.$currencycode : '');
  40. $valmainmaxdecimalsunit = GETPOST($mainmaxdecimalsunit, 'int');
  41. $valmainmaxdecimalstot = GETPOST($mainmaxdecimalstot, 'int');
  42. $valmainmaxdecimalsshown = GETPOST($mainmaxdecimalsshown, 'alpha'); // Can be 'x.y' but also 'x...'
  43. $valmainroundingruletot = price2num(GETPOST($mainroundingruletot, 'alphanohtml'), '', 2);
  44. if (!$user->admin) {
  45. accessforbidden();
  46. }
  47. /*
  48. * Actions
  49. */
  50. if ($action == 'update' && !$cancel) {
  51. $error = 0;
  52. $MAXDEC = 8;
  53. if ($valmainmaxdecimalsunit > $MAXDEC
  54. || $valmainmaxdecimalstot > $MAXDEC
  55. || $valmainmaxdecimalsshown > $MAXDEC) {
  56. $error++;
  57. setEventMessages($langs->trans("ErrorDecimalLargerThanAreForbidden", $MAXDEC), null, 'errors');
  58. $action = 'edit';
  59. }
  60. if ($valmainmaxdecimalsunit < 0
  61. || $valmainmaxdecimalstot < 0
  62. || $valmainmaxdecimalsshown < 0) {
  63. $langs->load("errors");
  64. $error++;
  65. setEventMessages($langs->trans("ErrorNegativeValueNotAllowed"), null, 'errors');
  66. $action = 'edit';
  67. }
  68. if ($valmainroundingruletot) {
  69. if ($valmainroundingruletot * pow(10, $valmainmaxdecimalstot) < 1) {
  70. $langs->load("errors");
  71. $error++;
  72. setEventMessages($langs->trans("ErrorMAIN_ROUNDING_RULE_TOTCanMAIN_MAX_DECIMALS_TOT"), null, 'errors');
  73. $action = 'edit';
  74. }
  75. }
  76. if ((float) $valmainmaxdecimalsshown == 0) {
  77. $langs->load("errors");
  78. $error++;
  79. setEventMessages($langs->trans("ErrorValueCantBeNull", dol_trunc(dol_string_nohtmltag($langs->transnoentitiesnoconv("MAIN_MAX_DECIMALS_SHOWN")), 40)), null, 'errors');
  80. $action = 'edit';
  81. }
  82. if (! $error && ((float) $valmainmaxdecimalsshown < $valmainmaxdecimalsunit || (float) $valmainmaxdecimalsshown < $valmainmaxdecimalstot)) {
  83. $langs->load("errors");
  84. $error++;
  85. setEventMessages($langs->trans("ErrorValueForTooLow", dol_trunc(dol_string_nohtmltag($langs->transnoentitiesnoconv("MAIN_MAX_DECIMALS_SHOWN")), 40)), null, 'errors');
  86. $action = 'edit';
  87. }
  88. if (!$error) {
  89. dolibarr_set_const($db, $mainmaxdecimalsunit, $valmainmaxdecimalsunit, 'chaine', 0, '', $conf->entity);
  90. dolibarr_set_const($db, $mainmaxdecimalstot, $valmainmaxdecimalstot, 'chaine', 0, '', $conf->entity);
  91. dolibarr_set_const($db, $mainmaxdecimalsshown, $valmainmaxdecimalsshown, 'chaine', 0, '', $conf->entity);
  92. dolibarr_set_const($db, $mainroundingruletot, $valmainroundingruletot, 'chaine', 0, '', $conf->entity);
  93. header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home&leftmenu=setup".(!empty($currencycode) ? '&currencycode='.$currencycode : ''));
  94. exit;
  95. }
  96. }
  97. /*
  98. * View
  99. */
  100. $form = new Form($db);
  101. $title = $langs->trans("LimitsSetup");
  102. $help_url = '';
  103. llxHeader('', $title, $help_url);
  104. print load_fiche_titre($title, '', 'title_setup');
  105. $aCurrencies = array($conf->currency); // Default currency always first position
  106. if (isModEnabled('multicompany') && !empty($conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY)) {
  107. require_once DOL_DOCUMENT_ROOT . '/core/lib/multicurrency.lib.php';
  108. $sql = "SELECT rowid, code FROM " . MAIN_DB_PREFIX . "multicurrency";
  109. $sql .= " WHERE entity = " . ((int) $conf->entity);
  110. $sql .= " AND code <> '" . $db->escape($conf->currency) . "'"; // Default currency always first position
  111. $resql = $db->query($sql);
  112. if ($resql) {
  113. while ($obj = $db->fetch_object($resql)) {
  114. $aCurrencies[] = $obj->code;
  115. }
  116. }
  117. if (!empty($aCurrencies) && count($aCurrencies) > 1) {
  118. $head = multicurrencyLimitPrepareHead($aCurrencies);
  119. print dol_get_fiche_head($head, $currencycode, '', -1, '');
  120. }
  121. }
  122. print '<span class="opacitymedium">'.$langs->trans("LimitsDesc")."</span><br>\n";
  123. print "<br>\n";
  124. if ($action == 'edit') {
  125. print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
  126. print '<input type="hidden" name="token" value="' . newToken() . '">';
  127. print '<input type="hidden" name="action" value="update">';
  128. if (isModEnabled('multicompany') && !empty($conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY)) {
  129. print '<input type="hidden" name="currencycode" value="' . $currencycode . '">';
  130. }
  131. clearstatcache();
  132. print '<table class="noborder centpercent">';
  133. print '<tr class="liste_titre"><td>'.$langs->trans("Parameters").'</td><td>'.$langs->trans("Value").'</td></tr>';
  134. print '<tr class="oddeven"><td>';
  135. print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_UNIT"), $langs->trans("ParameterActiveForNextInputOnly"));
  136. print '</td><td><input class="flat right" name="'.$mainmaxdecimalsunit.'" size="3" value="'.(GETPOSTISSET($mainmaxdecimalsunit) ? GETPOST($mainmaxdecimalsunit) : getDolGlobalInt('MAIN_MAX_DECIMALS_UNIT', 0)).'"></td></tr>';
  137. print '<tr class="oddeven"><td>';
  138. print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
  139. print '</td><td><input class="flat right" name="'.$mainmaxdecimalstot.'" size="3" value="'.(GETPOSTISSET($mainmaxdecimalstot) ? GETPOST($mainmaxdecimalstot) : getDolGlobalInt('MAIN_MAX_DECIMALS_TOT', 0)).'"></td></tr>';
  140. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").'</td>';
  141. print '<td><input class="flat right" name="'.$mainmaxdecimalsshown.'" size="3" value="'.(GETPOSTISSET($mainmaxdecimalsshown) ? GETPOST($mainmaxdecimalsshown) : getDolGlobalString('MAIN_MAX_DECIMALS_SHOWN')).'"></td></tr>';
  142. print '<tr class="oddeven"><td>';
  143. print $form->textwithpicto($langs->trans("MAIN_ROUNDING_RULE_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
  144. print '</td><td><input class="flat right" name="'.$mainroundingruletot.'" size="3" value="'.(GETPOSTISSET($mainroundingruletot) ? GETPOST($mainroundingruletot) : getDolGlobalString('MAIN_ROUNDING_RULE_TOT')).'"></td></tr>';
  145. print '</table>';
  146. print '<div class="center">';
  147. print '<input class="button button-save" type="submit" name="save" value="'.$langs->trans("Save").'">';
  148. print ' &nbsp; ';
  149. print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
  150. print '</div>';
  151. print '<br>';
  152. print '</form>';
  153. print '<br>';
  154. } else {
  155. print '<div class="div-table-responsive-no-min">';
  156. print '<table class="noborder centpercent">';
  157. print '<tr class="liste_titre"><td>'.$langs->trans("Parameter").'</td><td class="right">'.$langs->trans("Value").'</td></tr>';
  158. print '<tr class="oddeven"><td>';
  159. print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_UNIT"), $langs->trans("ParameterActiveForNextInputOnly"));
  160. print '</td><td align="right">'.(isset($conf->global->$mainmaxdecimalsunit) ? $conf->global->$mainmaxdecimalsunit : $conf->global->MAIN_MAX_DECIMALS_UNIT).'</td></tr>';
  161. print '<tr class="oddeven"><td>';
  162. print $form->textwithpicto($langs->trans("MAIN_MAX_DECIMALS_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
  163. print '</td><td align="right">'.(isset($conf->global->$mainmaxdecimalstot) ? $conf->global->$mainmaxdecimalstot : $conf->global->MAIN_MAX_DECIMALS_TOT).'</td></tr>';
  164. print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAX_DECIMALS_SHOWN").'</td>';
  165. print '<td align="right">'.(isset($conf->global->$mainmaxdecimalsshown) ? $conf->global->$mainmaxdecimalsshown : $conf->global->MAIN_MAX_DECIMALS_SHOWN).'</td></tr>';
  166. print '<tr class="oddeven"><td>';
  167. print $form->textwithpicto($langs->trans("MAIN_ROUNDING_RULE_TOT"), $langs->trans("ParameterActiveForNextInputOnly"));
  168. 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>';
  169. print '</table>';
  170. print '</div>';
  171. print '<div class="tabsAction">';
  172. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().(!empty($currencycode) ? '&currencycode='.$currencycode : '').'">'.$langs->trans("Modify").'</a>';
  173. print '</div>';
  174. }
  175. if (isModEnabled('multicompany') && !empty($conf->global->MULTICURRENCY_USE_LIMIT_BY_CURRENCY)) {
  176. if (!empty($aCurrencies) && count($aCurrencies) > 1) {
  177. print dol_get_fiche_end();
  178. }
  179. }
  180. if (empty($mysoc->country_code)) {
  181. $langs->load("errors");
  182. $warnpicto = img_warning($langs->trans("WarningMandatorySetupNotComplete"));
  183. print '<br><a href="'.DOL_URL_ROOT.'/admin/company.php?mainmenu=home">'.$warnpicto.' '.$langs->trans("WarningMandatorySetupNotComplete").'</a>';
  184. } else {
  185. // Show examples
  186. print load_fiche_titre($langs->trans("ExamplesWithCurrentSetup"), '', '');
  187. print '<span class="opacitymedium">'.$langs->trans("Format").':</span> '.price(price2num(1234.56789, 'MT'), 0, $langs, 1, -1, -1, $currencycode)."<br>\n";
  188. // Always show vat rates with vat 0
  189. $s = 2 / 3; $qty = 1; $vat = 0;
  190. $tmparray = calcul_price_total(1, $qty * price2num($s, 'MU'), 0, $vat, 0, 0, 0, 'HT', 0, 0, $mysoc);
  191. print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
  192. print " x ".$langs->trans("Quantity").": ".$qty;
  193. print " - ".$langs->trans("VAT").": ".$vat.'%';
  194. print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
  195. $s = 10 / 3; $qty = 1; $vat = 0;
  196. $tmparray = calcul_price_total(1, $qty * price2num($s, 'MU'), 0, $vat, 0, 0, 0, 'HT', 0, 0, $mysoc);
  197. print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
  198. print " x ".$langs->trans("Quantity").": ".$qty;
  199. print " - ".$langs->trans("VAT").": ".$vat.'%';
  200. print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
  201. $s = 10 / 3; $qty = 2; $vat = 0;
  202. $tmparray = calcul_price_total(1, $qty * price2num($s, 'MU'), 0, $vat, 0, 0, 0, 'HT', 0, 0, $mysoc);
  203. print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
  204. print " x ".$langs->trans("Quantity").": ".$qty;
  205. print " - ".$langs->trans("VAT").": ".$vat.'%';
  206. print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
  207. // Add vat rates examples specific to country
  208. $vat_rates = array();
  209. $sql = "SELECT taux as vat_rate, t.code as vat_code, t.localtax1 as localtax_rate1, t.localtax2 as localtax_rate2";
  210. $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
  211. $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')";
  212. $sql .= " ORDER BY t.taux ASC";
  213. $resql = $db->query($sql);
  214. if ($resql) {
  215. $num = $db->num_rows($resql);
  216. if ($num) {
  217. for ($i = 0; $i < $num; $i++) {
  218. $obj = $db->fetch_object($resql);
  219. $vat_rates[] = array('vat_rate'=>$obj->vat_rate, 'code'=>$obj->vat_code, 'localtax_rate1'=>$obj->localtax_rate1, 'locltax_rate2'=>$obj->localtax_rate2);
  220. }
  221. }
  222. } else {
  223. dol_print_error($db);
  224. }
  225. if (count($vat_rates)) {
  226. foreach ($vat_rates as $vatarray) {
  227. $vat = $vatarray['vat_rate'];
  228. for ($qty = 1; $qty <= 2; $qty++) {
  229. $vattxt = $vat.($vatarray['code'] ? ' ('.$vatarray['code'].')' : '');
  230. $localtax_array = getLocalTaxesFromRate($vattxt, 0, $mysoc, $mysoc);
  231. $s = 10 / 3;
  232. $tmparray = calcul_price_total($qty, price2num($s, 'MU'), 0, $vat, -1, -1, 0, 'HT', 0, 0, $mysoc, $localtax_array);
  233. print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
  234. print " x ".$langs->trans("Quantity").": ".$qty;
  235. print " - ".$langs->trans("VAT").": ".$vat.'%';
  236. print ($vatarray['code'] ? ' ('.$vatarray['code'].')' : '');
  237. print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ";
  238. print $tmparray[0].' / '.$tmparray[1].($tmparray[9] ? '+'.$tmparray[9] : '').($tmparray[10] ? '+'.$tmparray[10] : '').' / '.$tmparray[2];
  239. print "<br>\n";
  240. }
  241. }
  242. } else {
  243. // More examples if not specific vat rate found
  244. // This example must be kept for test purpose with current value because value used (2/7, 10/3, and vat 0, 10)
  245. // were calculated to show all possible cases of rounding. If we change this, examples becomes useless or show the same rounding rule.
  246. $localtax_array = array();
  247. $s = 10 / 3; $qty = 1; $vat = 10;
  248. $tmparray = calcul_price_total($qty, price2num($s, 'MU'), 0, $vat, -1, -1, 0, 'HT', 0, 0, $mysoc, $localtax_array);
  249. print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
  250. print " x ".$langs->trans("Quantity").": ".$qty;
  251. print " - ".$langs->trans("VAT").": ".$vat.'%';
  252. print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
  253. $s = 10 / 3; $qty = 2; $vat = 10;
  254. $tmparray = calcul_price_total($qty, price2num($s, 'MU'), 0, $vat, -1, -1, 0, 'HT', 0, 0, $mysoc, $localtax_array);
  255. print '<span class="opacitymedium">'.$langs->trans("UnitPriceOfProduct").":</span> ".price2num($s, 'MU');
  256. print " x ".$langs->trans("Quantity").": ".$qty;
  257. print " - ".$langs->trans("VAT").": ".$vat.'%';
  258. print ' &nbsp; -> &nbsp; <span class="opacitymedium">'.$langs->trans("TotalPriceAfterRounding").":</span> ".$tmparray[0].' / '.$tmparray[1].' / '.$tmparray[2]."<br>\n";
  259. }
  260. }
  261. // End of page
  262. llxFooter();
  263. $db->close();