limits.php 15 KB

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