price_rules.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * Page to set how to autocalculate price for each level when option PRODUCT_MULTIPRICE is on.
  19. * This page is a tab in the setup of module Product if option PRODUIT_MULTIPRICES_ALLOW_AUTOCALC_PRICELEVEL is set.
  20. */
  21. // Load Dolibarr environment
  22. require '../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  25. // Load translation files required by the page
  26. $langs->loadLangs(array('admin', 'products'));
  27. $action = GETPOST('action', 'aZ09');
  28. // Security check
  29. if (!$user->admin || (empty($conf->product->enabled) && empty($conf->service->enabled))) {
  30. accessforbidden();
  31. }
  32. $error = 0;
  33. /**
  34. * Actions
  35. */
  36. if ($action == 'update') {
  37. $var_percent = GETPOST('var_percent', 'array');
  38. $var_min_percent = GETPOST('var_min_percent', 'array');
  39. $fk_level = GETPOST('fk_level', 'array');
  40. for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
  41. $check = isset($var_min_percent[$i]);
  42. if ($i != 1) {
  43. $check = $check && isset($var_percent[$i]) && isset($fk_level[$i]);
  44. }
  45. if (!$check) {
  46. continue;
  47. }
  48. $i_var_percent = 0;
  49. // Set $i_var_percent, the percent of price for level compared to an other level
  50. if ($i != 1) {
  51. $i_var_percent = (float) price2num($var_percent[$i]);
  52. }
  53. $i_var_min_percent = (float) price2num($var_min_percent[$i]);
  54. $i_fk_level = (int) $fk_level[$i];
  55. if ($i == 1) {
  56. $check1 = true;
  57. $check2 = $i_var_min_percent;
  58. } else {
  59. $check1 = $i_fk_level >= 1 && $i_fk_level <= $conf->global->PRODUIT_MULTIPRICES_LIMIT;
  60. $check2 = $i_var_percent && ($i_var_min_percent || (string) $i_var_min_percent === '0');
  61. }
  62. if (empty($i_var_percent) && empty($i_var_min_percent)) {
  63. //If the level is between range but percent fields are empty, then we ensure it does not exist in DB
  64. $db->query("DELETE FROM ".MAIN_DB_PREFIX."product_pricerules WHERE level = ".((int) $i));
  65. continue;
  66. }
  67. $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_pricerules (level, fk_level, var_percent, var_min_percent) VALUES (";
  68. $sql .= ((int) $i).", ".$db->escape($i_fk_level).", ".$i_var_percent.", ".$i_var_min_percent.")";
  69. if (!$db->query($sql)) {
  70. //If we could not create, then we try updating
  71. $sql = "UPDATE ".MAIN_DB_PREFIX."product_pricerules";
  72. $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);
  73. if (!$db->query($sql)) {
  74. setEventMessages($langs->trans('ErrorSavingChanges'), null, 'errors');
  75. $error++;
  76. }
  77. }
  78. }
  79. if (!$error) {
  80. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  81. }
  82. }
  83. /*
  84. * View
  85. */
  86. $sql = "SELECT rowid, level, fk_level, var_percent, var_min_percent";
  87. $sql .= " FROM ".MAIN_DB_PREFIX."product_pricerules";
  88. $query = $db->query($sql);
  89. $rules = array();
  90. while ($result = $db->fetch_object($query)) {
  91. $rules[$result->level] = $result;
  92. }
  93. $title = $langs->trans('ProductServiceSetup');
  94. $tab = $langs->trans("ProductsAndServices");
  95. if (empty($conf->product->enabled)) {
  96. $title = $langs->trans('ServiceSetup');
  97. $tab = $langs->trans('Services');
  98. } elseif (empty($conf->service->enabled)) {
  99. $title = $langs->trans('ProductSetup');
  100. $tab = $langs->trans('Products');
  101. }
  102. llxHeader('', $langs->trans('MultipriceRules'));
  103. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  104. print load_fiche_titre($title, $linkback, 'title_setup');
  105. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  106. print '<input type="hidden" name="token" value="'.newToken().'">';
  107. print '<input type="hidden" name="action" value="update">';
  108. $head = product_admin_prepare_head();
  109. print dol_get_fiche_head($head, 'generator', $tab, -1, 'product');
  110. print '<span class="opacitymedium">'.$langs->trans("MultiPriceRuleDesc").'</span><br><br>';
  111. // Array that contains the number of prices available
  112. $price_options = array();
  113. for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
  114. $price_options[$i] = $langs->trans('SellingPrice').' '.$i;
  115. }
  116. ?>
  117. <table class="noborder centpercent">
  118. <tr class="liste_titre">
  119. <td style="text-align: center"><?php echo $langs->trans('PriceLevel') ?></td>
  120. <td style="text-align: center"><?php echo $langs->trans('Price') ?></td>
  121. <td style="text-align: center"><?php echo $langs->trans('MinPrice') ?></td></tr>
  122. <tr>
  123. <td class="fieldrequired" style="text-align: center"><?php echo $langs->trans('SellingPrice') ?> 1</td>
  124. <td></td>
  125. <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, 2) ?>"> <?php echo $langs->trans('PercentDiscountOver', $langs->trans('SellingPrice').' 1') ?></td>
  126. </tr>
  127. <?php for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) : ?>
  128. <tr>
  129. <td class="fieldrequired" style="text-align: center"><?php
  130. echo $langs->trans('SellingPrice').' '.$i;
  131. // Label of price
  132. $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i;
  133. if (!empty($conf->global->$keyforlabel)) {
  134. print ' - '.$langs->trans($conf->global->$keyforlabel);
  135. }
  136. ?>
  137. </td>
  138. <td style="text-align: center">
  139. <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, 2) ?>">
  140. <?php
  141. $return = array();
  142. for ($j = 1; $j < $i; $j++) {
  143. $return[$j] = $price_options[$j];
  144. }
  145. $texttoshow = $langs->trans('PercentVariationOver', '{s1}');
  146. $texttoshow = str_replace('{s1}', Form::selectarray("fk_level[$i]", $return, (isset($rules[$i]) ? $rules[$i]->fk_level : null)), $texttoshow);
  147. print $texttoshow;
  148. ?>
  149. </td>
  150. <td style="text-align: center">
  151. <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, 2) ?>">
  152. <?php echo $langs->trans('PercentDiscountOver', $langs->transnoentitiesnoconv('SellingPrice').' '.$i) ?>
  153. </td>
  154. </tr>
  155. <?php endfor ?>
  156. </table>
  157. <?php
  158. print dol_get_fiche_end();
  159. print '<div style="text-align: center">
  160. <input type="submit" value="'.$langs->trans("Save").'" class="button button-save">
  161. </div>';
  162. print '</form>';
  163. // End of page
  164. llxFooter();
  165. $db->close();