dolibarr 24.0.2
split.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2021 Andreu Bisquerra <jove@bisquerra.com>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Not disabled cause need to load personalized language
27//if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Not disabled cause need to load personalized language
28//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
29//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
30if (!defined('NOTOKENRENEWAL')) {
31 define('NOTOKENRENEWAL', '1');
32}
33if (!defined('NOREQUIREMENU')) {
34 define('NOREQUIREMENU', '1');
35}
36if (!defined('NOREQUIREHTML')) {
37 define('NOREQUIREHTML', '1');
38}
39if (!defined('NOREQUIREAJAX')) {
40 define('NOREQUIREAJAX', '1');
41}
42
43// Load Dolibarr environment
44require '../main.inc.php'; // Load $user and permissions
45require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
52$langs->loadLangs(array("main", "bills", "cashdesk", "banks"));
53
54$action = GETPOST('action', 'aZ09');
55$place = (GETPOST('place', 'aZ09') ? GETPOST('place', 'aZ09') : 0);
56
57if (!$user->hasRight('takepos', 'run')) {
59}
60
61
62/*
63 * Actions
64 */
65
66if ($action == "split" && $user->hasRight('takepos', 'run')) {
67 $line = GETPOSTINT('line');
68 $split = GETPOSTINT('split');
69 $invoice = null;
70 $placeid = 0;
71 // The line can only be taken from the draft invoice of this terminal it is moved from
72 $sourceref = '(PROV-POS'.$_SESSION["takeposterminal"].'-'.($split == 1 ? $place : 'SPLIT').')';
73 if ($split == 1) { // Split line
74 $invoice = new Facture($db);
75 $ret = $invoice->fetch(0, '(PROV-POS'.$_SESSION["takeposterminal"].'-SPLIT)');
76 if ($ret > 0) {
77 $placeid = $invoice->id;
78 } else {
79 $constforcompanyid = 'CASHDESK_ID_THIRDPARTY'.$_SESSION["takeposterminal"];
80 $invoice->socid = getDolGlobalInt($constforcompanyid);
81 $invoice->date = dol_now();
82 $invoice->module_source = 'takepos';
83 $invoice->pos_source = $_SESSION["takeposterminal"];
84 $invoice->entity = !empty($_SESSION["takeposinvoiceentity"]) ? $_SESSION["takeposinvoiceentity"] : $conf->entity;
85 if ($invoice->socid <= 0) {
86 $langs->load('errors');
87 dol_htmloutput_errors($langs->trans("ErrorModuleSetupNotComplete", "TakePos"), [], 1);
88 } else {
89 $placeid = $invoice->create($user);
90 if ($placeid < 0) {
91 dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
92 }
93
94 $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET ref='(PROV-POS".$_SESSION["takeposterminal"]."-SPLIT)'";
95 $sql .= " WHERE rowid = ".((int) $placeid);
96 $db->query($sql);
97 }
98 }
99 if ($placeid > 0) {
100 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET fk_facture = ".((int) $placeid)." WHERE rowid = ".((int) $line);
101 $sql .= " AND fk_facture IN (SELECT f.rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.ref = '".$db->escape($sourceref)."'";
102 $sql .= " AND f.fk_statut = ".Facture::STATUS_DRAFT." AND f.entity IN (".getEntity('invoice')."))";
103 $db->query($sql);
104 }
105 } elseif ($split == 0) { // Unsplit line
106 $invoice = new Facture($db);
107 if ($place == "SPLIT") {
108 $place = "0";
109 } // Avoid move line to the same place (from SPLIT to SPLIT place)
110 $ret = $invoice->fetch(0, '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')');
111 if ($ret > 0) {
112 $placeid = $invoice->id;
113 } else {
114 $constforcompanyid = 'CASHDESK_ID_THIRDPARTY'.$_SESSION["takeposterminal"];
115 $invoice->socid = getDolGlobalInt($constforcompanyid);
116 $invoice->date = dol_now();
117 $invoice->module_source = 'takepos';
118 $invoice->pos_source = $_SESSION["takeposterminal"];
119 $invoice->entity = !empty($_SESSION["takeposinvoiceentity"]) ? $_SESSION["takeposinvoiceentity"] : $conf->entity;
120 if ($invoice->socid <= 0) {
121 $langs->load('errors');
122 dol_htmloutput_errors($langs->trans("ErrorModuleSetupNotComplete", "TakePos"), [], 1);
123 } else {
124 $placeid = $invoice->create($user);
125 if ($placeid < 0) {
126 dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
127 }
128
129 $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")'";
130 $sql .= " WHERE rowid = ".((int) $placeid);
131 $db->query($sql);
132 }
133 }
134 if ($placeid > 0) {
135 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet SET fk_facture = ".((int) $placeid)." WHERE rowid = ".((int) $line);
136 $sql .= " AND fk_facture IN (SELECT f.rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.ref = '".$db->escape($sourceref)."'";
137 $sql .= " AND f.fk_statut = ".Facture::STATUS_DRAFT." AND f.entity IN (".getEntity('invoice')."))";
138 $db->query($sql);
139 }
140 }
141 if ($invoice !== null) {
142 $invoice->fetch(0, '(PROV-POS'.$_SESSION["takeposterminal"].'-SPLIT)');
143 $invoice->update_price();
144
145 $invoice->fetch(0, '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')');
146 $invoice->update_price();
147 }
148}
149
150
151/*
152 * View
153 */
154
155$invoice = new Facture($db);
156if (isset($invoiceid) && $invoiceid > 0) {
157 $invoice->fetch($invoiceid);
158} else {
159 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture where ref = '".$db->escape("(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")")."'";
160 $resql = $db->query($sql);
161 $obj = $db->fetch_object($resql);
162 if ($obj) {
163 $invoiceid = $obj->rowid;
164 }
165 if (!isset($invoiceid)) {
166 $invoiceid = 0; // Invoice does not exist yet
167 } else {
168 $invoice->fetch($invoiceid);
169 }
170}
171
172$arrayofcss = array('/takepos/css/pos.css.php');
173if (getDolGlobalInt('TAKEPOS_COLOR_THEME') == 1) {
174 $arrayofcss[] = '/takepos/css/colorful.css';
175}
176$arrayofjs = array();
177
178$head = '';
179$title = '';
180$disablejs = 0;
181$disablehead = 0;
182
183top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss);
184
185// Define list of possible payments
186$arrayOfValidPaymentModes = array();
187$arrayOfValidBankAccount = array();
188
189?>
190<body class="takepossplitphp">
191
192<script>
193function Split(selectedline, split) {
194 $.ajax({
195 url: "split.php?action=split&token=<?php echo newToken(); ?>&line="+selectedline+"&split="+split+"&place=<?php echo $place;?>",
196 context: document.body
197 }).done(function() {
198 $("#currentplace").load("invoice.php?place="+parent.place+"&invoiceid="+parent.invoiceid, function() {
199 $('#currentplace').find('.posinvoiceline').click(function(){
200 Split(this.id, 1);
201 });
202 });
203 $("#splitplace").load("invoice.php?place=SPLIT", function() {
204 $('#splitplace').find('.posinvoiceline').click(function(){
205 Split(this.id, 0);
206 });
207 });
208 });
209}
210
211$( document ).ready(function() {
212 if (parent.place=='SPLIT') {
213 parent.place=0;
214 parent.invoiceid=0;
215 parent.Refresh();
216 }
217 $("#currentplace").load("invoice.php?place="+parent.place+"&invoiceid="+parent.invoiceid, function() {
218 $('#currentplace').find('.posinvoiceline')
219 .click(function(){
220 Split(this.id, 1);
221 });
222 });
223
224 $("#splitplace").load("invoice.php?place=SPLIT", function() {
225 $('#splitplace').find('.posinvoiceline').click(function(){
226 Split(this.id, 0);
227 });
228 });
229
230
231
232 $("#headersplit1").html("<?php echo $langs->trans("Place");?> "+parent.place);
233 $("#headersplit2").html("<?php echo $langs->trans("SplitSale");?>");
234
235});
236</script>
237
238<div class="headersplit">
239 <a href="#" onclick="top.location.href='index.php?place='+parent.place"><div class="headercontent" id="headersplit1"></div></a>
240</div>
241
242<div class="rowsplit">
243 <div class="splitsale" id="currentplace"></div>
244</div>
245
246<div class="headersplit">
247 <a href="#" onclick="top.location.href='index.php?place=SPLIT'"><div class="headercontent" id="headersplit2"></div></a>
248</div>
249
250<div class="rowsplit">
251 <div class="splitsale" id="splitplace"></div>
252</div>
253
254</body>
255</html>
Class to manage invoices.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_htmloutput_errors($mesgstring='', $mesgarray=array(), $keepembedded=0)
Print formatted error messages to output (Used to show messages on html output).
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
top_htmlhead($head, $title='', $disablejs=0, $disablehead=0, $arrayofjs=array(), $arrayofcss=array(), $disableforlogin=0, $disablenofollow=0, $disablenoindex=0)
Output html header of a page.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.