reduction.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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('NOCSRFCHECK')) {
  27. define('NOCSRFCHECK', '1');
  28. }
  29. if (!defined('NOTOKENRENEWAL')) {
  30. define('NOTOKENRENEWAL', '1');
  31. }
  32. if (!defined('NOREQUIREMENU')) {
  33. define('NOREQUIREMENU', '1');
  34. }
  35. if (!defined('NOREQUIREHTML')) {
  36. define('NOREQUIREHTML', '1');
  37. }
  38. if (!defined('NOREQUIREAJAX')) {
  39. define('NOREQUIREAJAX', '1');
  40. }
  41. require '../main.inc.php'; // Load $user and permissions
  42. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  43. $place = (GETPOST('place', 'aZ09') ? GETPOST('place', 'aZ09') : 0); // $place is id of table for Ba or Restaurant
  44. $invoiceid = GETPOST('invoiceid', 'int');
  45. if (empty($user->rights->takepos->run)) {
  46. accessforbidden();
  47. }
  48. /*
  49. * View
  50. */
  51. $invoice = new Facture($db);
  52. if ($invoiceid > 0) {
  53. $invoice->fetch($invoiceid);
  54. } else {
  55. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture where ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")'";
  56. $resql = $db->query($sql);
  57. $obj = $db->fetch_object($resql);
  58. if ($obj) {
  59. $invoiceid = $obj->rowid;
  60. }
  61. if (!$invoiceid) {
  62. $invoiceid = 0; // Invoice does not exist yet
  63. } else {
  64. $invoice->fetch($invoiceid);
  65. }
  66. }
  67. $arrayofcss = array('/takepos/css/pos.css.php');
  68. $arrayofjs = array();
  69. top_htmlhead($head, '', 0, 0, $arrayofjs, $arrayofcss);
  70. $langs->loadLangs(array('main', 'bills', 'cashdesk'));
  71. if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
  72. $htmlReductionPercent = '<span class="fa fa-2x fa-percent"></span>';
  73. $htmlReductionAmount = '<span class="fa fa-2x fa-money"></span><br>'.$langs->trans('Amount');
  74. } else {
  75. $htmlReductionPercent = $langs->trans('ReductionShort').'<br>%';
  76. $htmlReductionAmount = $langs->trans('ReductionShort').'<br>'.$langs->trans('Amount');
  77. }
  78. ?>
  79. <link rel="stylesheet" href="css/pos.css.php">
  80. </head>
  81. <body>
  82. <script>
  83. var reductionType ='';
  84. var reductionTotal = '';
  85. var editAction = '';
  86. var editNumber = '';
  87. var htmlBtnOK = '<span style="font-size: 14pt;">OK</span>';
  88. var htmlReductionPercent = '<?php echo dol_escape_js($htmlReductionPercent); ?>';
  89. var htmlReductionAmount = '<?php echo dol_escape_js($htmlReductionAmount); ?>';
  90. /**
  91. * Reset values
  92. */
  93. function Reset()
  94. {
  95. reductionType = '';
  96. reductionTotal = '';
  97. editAction = '';
  98. editNumber = '';
  99. jQuery('#reduction_total').val(reductionTotal);
  100. jQuery("#reduction_type_percent").html(htmlReductionPercent);
  101. jQuery('#reduction_type_amount').html(htmlReductionAmount);
  102. }
  103. /**
  104. * Edit action
  105. *
  106. * @param {string} number Number pressed
  107. */
  108. function Edit(number)
  109. {
  110. console.log('Edit ' + number);
  111. if (number === 'p') {
  112. if (editAction === 'p' && reductionType === 'percent'){
  113. ValidateReduction();
  114. } else {
  115. editAction = 'p';
  116. }
  117. reductionType = 'percent';
  118. } else if (number === 'a') {
  119. if (editAction === 'a' && reductionType === 'amount'){
  120. ValidateReduction();
  121. } else {
  122. editAction = 'a';
  123. }
  124. reductionType = 'amount';
  125. }
  126. if (editAction === 'p'){
  127. jQuery('#reduction_type_percent').html(htmlBtnOK);
  128. jQuery('#reduction_type_amount').html(htmlReductionAmount);
  129. } else if (editAction === 'a'){
  130. jQuery('#reduction_type_amount').html(htmlBtnOK);
  131. jQuery("#reduction_type_percent").html(htmlReductionPercent);
  132. } else {
  133. jQuery('#reduction_type_percent').html(htmlReductionPercent);
  134. jQuery('#reduction_type_amount').html(htmlReductionAmount);
  135. }
  136. }
  137. /**
  138. * Add a number in reduction input
  139. *
  140. * @param {string} reductionNumber Number pressed
  141. */
  142. function AddReduction(reductionNumber)
  143. {
  144. console.log('AddReduction ' + reductionNumber);
  145. reductionTotal += String(reductionNumber);
  146. jQuery('#reduction_total').val(reductionTotal);
  147. }
  148. /**
  149. * Validate a reduction
  150. */
  151. function ValidateReduction()
  152. {
  153. console.log('ValidateReduction');
  154. if (reductionTotal.length <= 0) {
  155. console.error('Error no reduction');
  156. return;
  157. }
  158. var reductionNumber = parseFloat(reductionTotal);
  159. if (isNaN(reductionNumber)) {
  160. console.error('Error not a valid number :', reductionNumber);
  161. return;
  162. }
  163. if (reductionType === 'percent') {
  164. var invoiceid = <?php echo ($invoiceid > 0 ? $invoiceid : 0); ?>;
  165. parent.$("#poslines").load("invoice.php?action=update_reduction_global&place=<?php echo $place; ?>&number="+reductionNumber+"&invoiceid="+invoiceid, function() {
  166. Reset();
  167. parent.$.colorbox.close();
  168. });
  169. } else if (reductionType === 'amount') {
  170. var desc = "<?php echo dol_escape_js($langs->transnoentities('Reduction')); ?>";
  171. parent.$("#poslines").load("invoice.php?action=freezone&place=<?php echo $place; ?>&number=-"+reductionNumber+"&desc="+desc, function() {
  172. Reset();
  173. parent.$.colorbox.close();
  174. });
  175. } else {
  176. console.error('Error bad reduction type :', reductionType);
  177. }
  178. }
  179. </script>
  180. <div style="position:absolute; top:2%; left:5%; width:91%;">
  181. <center>
  182. <?php
  183. print '<input type="text" class="takepospay" id="reduction_total" name="reduction_total" style="width: 50%;" placeholder="'.$langs->trans('Reduction').'">';
  184. ?>
  185. </center>
  186. </div>
  187. <div style="position:absolute; top:33%; left:5%; height:52%; width:92%;">
  188. <?php
  189. print '<button type="button" class="calcbutton" onclick="AddReduction(7);">7</button>';
  190. print '<button type="button" class="calcbutton" onclick="AddReduction(8);">8</button>';
  191. print '<button type="button" class="calcbutton" onclick="AddReduction(9);">9</button>';
  192. print '<button type="button" class="calcbutton2" id="reduction_type_percent" onclick="Edit(\'p\');">'.$htmlReductionPercent.'</button>';
  193. print '<button type="button" class="calcbutton" onclick="AddReduction(4);">4</button>';
  194. print '<button type="button" class="calcbutton" onclick="AddReduction(5);">5</button>';
  195. print '<button type="button" class="calcbutton" onclick="AddReduction(6);">6</button>';
  196. print '<button type="button" class="calcbutton2" id="reduction_type_amount" onclick="Edit(\'a\');">'.$htmlReductionAmount.'</button>';
  197. print '<button type="button" class="calcbutton" onclick="AddReduction(1);">1</button>';
  198. print '<button type="button" class="calcbutton" onclick="AddReduction(2);">2</button>';
  199. print '<button type="button" class="calcbutton" onclick="AddReduction(3);">3</button>';
  200. print '<button type="button" class="calcbutton3 poscolorblue" onclick="Reset();"><span id="printtext" style="font-weight: bold; font-size: 18pt;">C</span></button>';
  201. print '<button type="button" class="calcbutton" onclick="AddReduction(0);">0</button>';
  202. print '<button type="button" class="calcbutton" onclick="AddReduction(\'.\');">.</button>';
  203. print '<button type="button" class="calcbutton">&nbsp;</button>';
  204. print '<button type="button" class="calcbutton3 poscolordelete" onclick="parent.$.colorbox.close();"><span id="printtext" style="font-weight: bold; font-size: 18pt;">X</span></button>';
  205. ?>
  206. </div>
  207. </body>
  208. </html>