split.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /* Copyright (C) 2021 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/split.php
  19. * \ingroup takepos
  20. * \brief Page with the content of the popup to split sale
  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. $langs->loadLangs(array("main", "bills", "cashdesk", "banks"));
  42. $action = GETPOST('action', 'aZ09');
  43. $place = (GETPOST('place', 'aZ09') ? GETPOST('place', 'aZ09') : 0);
  44. if (!$user->hasRight('takepos', 'run')) {
  45. accessforbidden();
  46. }
  47. /*
  48. * Actions
  49. */
  50. if ($action=="split") {
  51. $line = GETPOST('line', 'int');
  52. $split = GETPOST('split', 'int');
  53. if ($split==1) { // Split line
  54. $invoice = new Facture($db);
  55. $ret = $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-SPLIT)');
  56. if ($ret > 0) {
  57. $placeid = $invoice->id;
  58. } else {
  59. $constforcompanyid = 'CASHDESK_ID_THIRDPARTY'.$_SESSION["takeposterminal"];
  60. $invoice->socid =getDolGlobalInt($constforcompanyid);
  61. $invoice->date = dol_now();
  62. $invoice->module_source = 'takepos';
  63. $invoice->pos_source = $_SESSION["takeposterminal"];
  64. $invoice->entity = !empty($_SESSION["takeposinvoiceentity"]) ? $_SESSION["takeposinvoiceentity"] : $conf->entity;
  65. if ($invoice->socid <= 0) {
  66. $langs->load('errors');
  67. dol_htmloutput_errors($langs->trans("ErrorModuleSetupNotComplete", "TakePos"), null, 1);
  68. } else {
  69. $placeid = $invoice->create($user);
  70. if ($placeid < 0) {
  71. dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
  72. }
  73. $sql = "UPDATE ".MAIN_DB_PREFIX."facture set ref='(PROV-POS".$_SESSION["takeposterminal"]."-SPLIT)' where rowid=".$placeid;
  74. $db->query($sql);
  75. }
  76. }
  77. $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set fk_facture=".$placeid." where rowid=".$line;
  78. $db->query($sql);
  79. } elseif ($split==0) { // Unsplit line
  80. $invoice = new Facture($db);
  81. if ($place=="SPLIT") {
  82. $place="0";
  83. } // Avoid move line to the same place (from SPLIT to SPLIT place)
  84. $ret = $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')');
  85. if ($ret > 0) {
  86. $placeid = $invoice->id;
  87. } else {
  88. $constforcompanyid = 'CASHDESK_ID_THIRDPARTY'.$_SESSION["takeposterminal"];
  89. $invoice->socid = getDolGlobalInt($constforcompanyid);
  90. $invoice->date = dol_now();
  91. $invoice->module_source = 'takepos';
  92. $invoice->pos_source = $_SESSION["takeposterminal"];
  93. $invoice->entity = !empty($_SESSION["takeposinvoiceentity"]) ? $_SESSION["takeposinvoiceentity"] : $conf->entity;
  94. if ($invoice->socid <= 0) {
  95. $langs->load('errors');
  96. dol_htmloutput_errors($langs->trans("ErrorModuleSetupNotComplete", "TakePos"), null, 1);
  97. } else {
  98. $placeid = $invoice->create($user);
  99. if ($placeid < 0) {
  100. dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
  101. }
  102. $sql = "UPDATE ".MAIN_DB_PREFIX."facture set ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")' where rowid=".$placeid;
  103. $db->query($sql);
  104. }
  105. }
  106. $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set fk_facture=".$placeid." where rowid=".$line;
  107. $db->query($sql);
  108. }
  109. $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-SPLIT)');
  110. $invoice->update_price();
  111. $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')');
  112. $invoice->update_price();
  113. }
  114. /*
  115. * View
  116. */
  117. $invoice = new Facture($db);
  118. if (isset($invoiceid) && $invoiceid > 0) {
  119. $invoice->fetch($invoiceid);
  120. } else {
  121. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture where ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")'";
  122. $resql = $db->query($sql);
  123. $obj = $db->fetch_object($resql);
  124. if ($obj) {
  125. $invoiceid = $obj->rowid;
  126. }
  127. if (!isset($invoiceid)) {
  128. $invoiceid = 0; // Invoice does not exist yet
  129. } else {
  130. $invoice->fetch($invoiceid);
  131. }
  132. }
  133. $arrayofcss = array('/takepos/css/pos.css.php');
  134. if (getDolGlobalInt('TAKEPOS_COLOR_THEME') == 1) {
  135. $arrayofcss[] = '/takepos/css/colorful.css';
  136. }
  137. $arrayofjs = array();
  138. $head = '';
  139. $title = '';
  140. $disablejs = 0;
  141. $disablehead = 0;
  142. top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss);
  143. // Define list of possible payments
  144. $arrayOfValidPaymentModes = array();
  145. $arrayOfValidBankAccount = array();
  146. ?>
  147. <body class="takepossplitphp">
  148. <script>
  149. function Split(selectedline, split) {
  150. $.ajax({
  151. url: "split.php?action=split&token=<?php echo newToken(); ?>&line="+selectedline+"&split="+split+"&place=<?php echo $place;?>",
  152. context: document.body
  153. }).done(function() {
  154. $("#currentplace").load("invoice.php?place="+parent.place+"&invoiceid="+parent.invoiceid, function() {
  155. $('#currentplace').find('.posinvoiceline').click(function(){
  156. Split(this.id, 1);
  157. });
  158. });
  159. $("#splitplace").load("invoice.php?place=SPLIT", function() {
  160. $('#splitplace').find('.posinvoiceline').click(function(){
  161. Split(this.id, 0);
  162. });
  163. });
  164. });
  165. }
  166. $( document ).ready(function() {
  167. if (parent.place=='SPLIT') {
  168. parent.place=0;
  169. parent.invoiceid=0;
  170. parent.Refresh();
  171. }
  172. $("#currentplace").load("invoice.php?place="+parent.place+"&invoiceid="+parent.invoiceid, function() {
  173. $('#currentplace').find('.posinvoiceline')
  174. .click(function(){
  175. Split(this.id, 1);
  176. });
  177. });
  178. $("#splitplace").load("invoice.php?place=SPLIT", function() {
  179. $('#splitplace').find('.posinvoiceline').click(function(){
  180. Split(this.id, 0);
  181. });
  182. });
  183. $("#headersplit1").html("<?php echo $langs->trans("Place");?> "+parent.place);
  184. $("#headersplit2").html("<?php echo $langs->trans("SplitSale");?>");
  185. });
  186. </script>
  187. <div class="headersplit">
  188. <a href="#" onclick="top.location.href='index.php?place='+parent.place"><div class="headercontent" id="headersplit1"></div></a>
  189. </div>
  190. <div class="rowsplit">
  191. <div class="splitsale" id="currentplace"></div>
  192. </div>
  193. <div class="headersplit">
  194. <a href="#" onclick="top.location.href='index.php?place=SPLIT'"><div class="headercontent" id="headersplit2"></div></a>
  195. </div>
  196. <div class="rowsplit">
  197. <div class="splitsale" id="splitplace"></div>
  198. </div>
  199. </body>
  200. </html>