reduction.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /* Copyright (C) 2018 Andreu Bisquerra <jove@bisquerra.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/takepos/reduction.php
  19. * \ingroup takepos
  20. * \brief Page with the content of the popup to enter reductions
  21. */
  22. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Not disabled cause need to load personalized language
  23. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Not disabled cause need to load personalized language
  24. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
  25. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
  26. if (!defined('NOTOKENRENEWAL')) {
  27. define('NOTOKENRENEWAL', '1');
  28. }
  29. if (!defined('NOREQUIREMENU')) {
  30. define('NOREQUIREMENU', '1');
  31. }
  32. if (!defined('NOREQUIREHTML')) {
  33. define('NOREQUIREHTML', '1');
  34. }
  35. if (!defined('NOREQUIREAJAX')) {
  36. define('NOREQUIREAJAX', '1');
  37. }
  38. // Load Dolibarr environment
  39. require '../main.inc.php'; // Load $user and permissions
  40. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  41. $place = (GETPOST('place', 'aZ09') ? GETPOST('place', 'aZ09') : 0); // $place is id of table for Ba or Restaurant
  42. $invoiceid = GETPOST('invoiceid', 'int');
  43. if (!$user->hasRight('takepos', 'run')) {
  44. accessforbidden();
  45. }
  46. /*
  47. * View
  48. */
  49. $invoice = new Facture($db);
  50. if ($invoiceid > 0) {
  51. $invoice->fetch($invoiceid);
  52. } else {
  53. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture where ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")'";
  54. $resql = $db->query($sql);
  55. $obj = $db->fetch_object($resql);
  56. if ($obj) {
  57. $invoiceid = $obj->rowid;
  58. }
  59. if (!$invoiceid) {
  60. $invoiceid = 0; // Invoice does not exist yet
  61. } else {
  62. $invoice->fetch($invoiceid);
  63. }
  64. }
  65. $head = '';
  66. $arrayofcss = array('/takepos/css/pos.css.php');
  67. $arrayofjs = array();
  68. top_htmlhead($head, '', 0, 0, $arrayofjs, $arrayofcss);
  69. $langs->loadLangs(array('main', 'bills', 'cashdesk'));
  70. if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
  71. $htmlReductionPercent = '<span class="fa fa-2x fa-percent"></span>';
  72. $htmlReductionAmount = '<span class="fa fa-2x fa-money"></span><br>'.$langs->trans('Amount');
  73. } else {
  74. $htmlReductionPercent = $langs->trans('ReductionShort').'<br>%';
  75. $htmlReductionAmount = $langs->trans('ReductionShort').'<br>'.$langs->trans('Amount');
  76. }
  77. ?>
  78. <link rel="stylesheet" href="css/pos.css.php">
  79. </head>
  80. <body>
  81. <script>
  82. var reductionType ='';
  83. var reductionTotal = '';
  84. var editAction = '';
  85. var editNumber = '';
  86. var htmlBtnOK = '<span style="font-size: 14pt;">OK</span>';
  87. var htmlReductionPercent = '<?php echo dol_escape_js($htmlReductionPercent); ?>';
  88. var htmlReductionAmount = '<?php echo dol_escape_js($htmlReductionAmount); ?>';
  89. /**
  90. * Reset values
  91. */
  92. function Reset()
  93. {
  94. reductionType = '';
  95. reductionTotal = '';
  96. editAction = '';
  97. editNumber = '';
  98. jQuery('#reduction_total').val(reductionTotal);
  99. jQuery("#reduction_type_percent").html(htmlReductionPercent);
  100. jQuery('#reduction_type_amount').html(htmlReductionAmount);
  101. }
  102. /**
  103. * Edit action
  104. *
  105. * @param {string} number Number pressed
  106. */
  107. function Edit(number)
  108. {
  109. console.log('Edit ' + number);
  110. if (number === 'p') {
  111. if (editAction === 'p' && reductionType === 'percent'){
  112. ValidateReduction();
  113. } else {
  114. editAction = 'p';
  115. }
  116. reductionType = 'percent';
  117. } else if (number === 'a') {
  118. if (editAction === 'a' && reductionType === 'amount'){
  119. ValidateReduction();
  120. } else {
  121. editAction = 'a';
  122. }
  123. reductionType = 'amount';
  124. }
  125. if (editAction === 'p'){
  126. jQuery('#reduction_type_percent').html(htmlBtnOK);
  127. jQuery('#reduction_type_amount').html(htmlReductionAmount);
  128. } else if (editAction === 'a'){
  129. jQuery('#reduction_type_amount').html(htmlBtnOK);
  130. jQuery("#reduction_type_percent").html(htmlReductionPercent);
  131. } else {
  132. jQuery('#reduction_type_percent').html(htmlReductionPercent);
  133. jQuery('#reduction_type_amount').html(htmlReductionAmount);
  134. }
  135. }
  136. /**
  137. * Add a number in reduction input
  138. *
  139. * @param {string} reductionNumber Number pressed
  140. */
  141. function AddReduction(reductionNumber)
  142. {
  143. console.log('AddReduction ' + reductionNumber);
  144. reductionTotal += String(reductionNumber);
  145. jQuery('#reduction_total').val(reductionTotal);
  146. }
  147. /**
  148. * Validate a reduction
  149. */
  150. function ValidateReduction()
  151. {
  152. console.log('ValidateReduction');
  153. if (reductionTotal.length <= 0) {
  154. console.error('Error no reduction');
  155. return;
  156. }
  157. var reductionNumber = parseFloat(reductionTotal);
  158. if (isNaN(reductionNumber)) {
  159. console.error('Error not a valid number :', reductionNumber);
  160. return;
  161. }
  162. if (reductionType === 'percent') {
  163. var invoiceid = <?php echo ($invoiceid > 0 ? $invoiceid : 0); ?>;
  164. parent.$("#poslines").load("invoice.php?action=update_reduction_global&token=<?php echo newToken(); ?>&place=<?php echo $place; ?>&number="+reductionNumber+"&invoiceid="+invoiceid, function() {
  165. Reset();
  166. parent.$.colorbox.close();
  167. });
  168. } else if (reductionType === 'amount') {
  169. var desc = "<?php echo dol_escape_js($langs->transnoentities('Reduction')); ?>";
  170. parent.$("#poslines").load("invoice.php?action=freezone&token=<?php echo newToken(); ?>&place=<?php echo $place; ?>&number=-"+reductionNumber+"&desc="+desc, function() {
  171. Reset();
  172. parent.$.colorbox.close();
  173. });
  174. } else {
  175. console.error('Error bad reduction type :', reductionType);
  176. }
  177. }
  178. </script>
  179. <div style="position:absolute; top:2%; left:5%; width:91%;">
  180. <center>
  181. <?php
  182. print '<input type="text" class="takepospay" id="reduction_total" name="reduction_total" style="width: 50%;" placeholder="'.$langs->trans('Reduction').'">';
  183. ?>
  184. </center>
  185. </div>
  186. <div style="position:absolute; top:33%; left:5%; height:52%; width:92%;">
  187. <?php
  188. print '<button type="button" class="calcbutton" onclick="AddReduction(\'7\');">7</button>';
  189. print '<button type="button" class="calcbutton" onclick="AddReduction(\'8\');">8</button>';
  190. print '<button type="button" class="calcbutton" onclick="AddReduction(\'9\');">9</button>';
  191. print '<button type="button" class="calcbutton2" id="reduction_type_percent" onclick="Edit(\'p\');">'.$htmlReductionPercent.'</button>';
  192. print '<button type="button" class="calcbutton" onclick="AddReduction(\'4\');">4</button>';
  193. print '<button type="button" class="calcbutton" onclick="AddReduction(\'5\');">5</button>';
  194. print '<button type="button" class="calcbutton" onclick="AddReduction(\'6\');">6</button>';
  195. print '<button type="button" class="calcbutton2" id="reduction_type_amount" onclick="Edit(\'a\');">'.$htmlReductionAmount.'</button>';
  196. print '<button type="button" class="calcbutton" onclick="AddReduction(\'1\');">1</button>';
  197. print '<button type="button" class="calcbutton" onclick="AddReduction(\'2\');">2</button>';
  198. print '<button type="button" class="calcbutton" onclick="AddReduction(\'3\');">3</button>';
  199. print '<button type="button" class="calcbutton3 poscolorblue" onclick="Reset();"><span id="printtext" style="font-weight: bold; font-size: 18pt;">C</span></button>';
  200. print '<button type="button" class="calcbutton" onclick="AddReduction(\'0\');">0</button>';
  201. print '<button type="button" class="calcbutton" onclick="AddReduction(\'.\');">.</button>';
  202. print '<button type="button" class="calcbutton">&nbsp;</button>';
  203. print '<button type="button" class="calcbutton3 poscolordelete" onclick="parent.$.colorbox.close();"><span id="printtext" style="font-weight: bold; font-size: 18pt;">X</span></button>';
  204. ?>
  205. </div>
  206. </body>
  207. </html>