split.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 (empty($user->rights->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 = $conf->global->$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") $place="0"; // Avoid move line to the same place (from SPLIT to SPLIT place)
  82. $ret = $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')');
  83. if ($ret > 0) {
  84. $placeid = $invoice->id;
  85. } else {
  86. $constforcompanyid = 'CASHDESK_ID_THIRDPARTY'.$_SESSION["takeposterminal"];
  87. $invoice->socid = $conf->global->$constforcompanyid;
  88. $invoice->date = dol_now();
  89. $invoice->module_source = 'takepos';
  90. $invoice->pos_source = $_SESSION["takeposterminal"];
  91. $invoice->entity = !empty($_SESSION["takeposinvoiceentity"]) ? $_SESSION["takeposinvoiceentity"] : $conf->entity;
  92. if ($invoice->socid <= 0) {
  93. $langs->load('errors');
  94. dol_htmloutput_errors($langs->trans("ErrorModuleSetupNotComplete", "TakePos"), null, 1);
  95. } else {
  96. $placeid = $invoice->create($user);
  97. if ($placeid < 0) {
  98. dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
  99. }
  100. $sql = "UPDATE ".MAIN_DB_PREFIX."facture set ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")' where rowid=".$placeid;
  101. $db->query($sql);
  102. }
  103. }
  104. $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set fk_facture=".$placeid." where rowid=".$line;
  105. $db->query($sql);
  106. }
  107. $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-SPLIT)');
  108. $invoice->update_price();
  109. $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')');
  110. $invoice->update_price();
  111. }
  112. /*
  113. * View
  114. */
  115. $invoice = new Facture($db);
  116. if (isset($invoiceid) && $invoiceid > 0) {
  117. $invoice->fetch($invoiceid);
  118. } else {
  119. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture where ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")'";
  120. $resql = $db->query($sql);
  121. $obj = $db->fetch_object($resql);
  122. if ($obj) {
  123. $invoiceid = $obj->rowid;
  124. }
  125. if (!isset($invoiceid)) {
  126. $invoiceid = 0; // Invoice does not exist yet
  127. } else {
  128. $invoice->fetch($invoiceid);
  129. }
  130. }
  131. $arrayofcss = array('/takepos/css/pos.css.php');
  132. if (getDolGlobalInt('TAKEPOS_COLOR_THEME') == 1) {
  133. $arrayofcss[] = '/takepos/css/colorful.css';
  134. }
  135. $arrayofjs = array();
  136. $head = '';
  137. $title = '';
  138. $disablejs = 0;
  139. $disablehead = 0;
  140. top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss);
  141. // Define list of possible payments
  142. $arrayOfValidPaymentModes = array();
  143. $arrayOfValidBankAccount = array();
  144. ?>
  145. <body class="takepossplitphp">
  146. <script>
  147. function Split(selectedline, split) {
  148. $.ajax({
  149. url: "split.php?action=split&token=<?php echo newToken(); ?>&line="+selectedline+"&split="+split+"&place=<?php echo $place;?>",
  150. context: document.body
  151. }).done(function() {
  152. $("#currentplace").load("invoice.php?place="+parent.place+"&invoiceid="+parent.invoiceid, function() {
  153. $('#currentplace').find('.posinvoiceline').click(function(){
  154. Split(this.id, 1);
  155. });
  156. });
  157. $("#splitplace").load("invoice.php?place=SPLIT", function() {
  158. $('#splitplace').find('.posinvoiceline').click(function(){
  159. Split(this.id, 0);
  160. });
  161. });
  162. });
  163. }
  164. $( document ).ready(function() {
  165. if (parent.place=='SPLIT') {
  166. parent.place=0;
  167. parent.invoiceid=0;
  168. parent.Refresh();
  169. }
  170. $("#currentplace").load("invoice.php?place="+parent.place+"&invoiceid="+parent.invoiceid, function() {
  171. $('#currentplace').find('.posinvoiceline')
  172. .click(function(){
  173. Split(this.id, 1);
  174. });
  175. });
  176. $("#splitplace").load("invoice.php?place=SPLIT", function() {
  177. $('#splitplace').find('.posinvoiceline').click(function(){
  178. Split(this.id, 0);
  179. });
  180. });
  181. $("#headersplit1").html("<?php echo $langs->trans("Place");?> "+parent.place);
  182. $("#headersplit2").html("<?php echo $langs->trans("SplitSale");?>");
  183. });
  184. </script>
  185. <div class="headersplit">
  186. <a href="#" onclick="top.location.href='index.php?place='+parent.place"><div class="headercontent" id="headersplit1"></div></a>
  187. </div>
  188. <div class="rowsplit">
  189. <div class="splitsale" id="currentplace"></div>
  190. </div>
  191. <div class="headersplit">
  192. <a href="#" onclick="top.location.href='index.php?place=SPLIT'"><div class="headercontent" id="headersplit2"></div></a>
  193. </div>
  194. <div class="rowsplit">
  195. <div class="splitsale" id="splitplace"></div>
  196. </div>
  197. </body>
  198. </html>