Issues (2811)

public/htdocs/takepos/phone.php (2 issues)

Labels
Severity
1
<?php
2
3
/* Copyright (C) 2018       Andreu Bisquerra            <[email protected]>
4
 * Copyright (C) 2024       Rafael San José             <[email protected]>
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
20
use Dolibarr\Code\Categories\Classes\Categorie;
21
use Dolibarr\Code\Compta\Classes\Facture;
22
use Dolibarr\Code\Product\Classes\Product;
23
use Dolibarr\Lib\ViewMain;
24
25
/**
26
 *  \file       htdocs/takepos/phone.php
27
 *  \ingroup    takepos
28
 *  \brief      TakePOS Phone screen
29
 */
30
31
//if (! defined('NOREQUIREUSER'))   define('NOREQUIREUSER','1');    // Not disabled cause need to load personalized language
32
//if (! defined('NOREQUIREDB'))     define('NOREQUIREDB','1');      // Not disabled cause need to load personalized language
33
//if (! defined('NOREQUIRESOC'))        define('NOREQUIRESOC','1');
34
//if (! defined('NOREQUIRETRAN'))       define('NOREQUIRETRAN','1');
35
if (!defined('NOTOKENRENEWAL')) {
36
    define('NOTOKENRENEWAL', '1');
37
}
38
if (!defined('NOREQUIREMENU')) {
39
    define('NOREQUIREMENU', '1');
40
}
41
if (!defined('NOREQUIREHTML')) {
42
    define('NOREQUIREHTML', '1');
43
}
44
if (!defined('NOREQUIREAJAX')) {
45
    define('NOREQUIREAJAX', '1');
46
}
47
48
if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
49
    require constant('DOL_DOCUMENT_ROOT') . '/main.inc.php';
50
}
51
52
if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
53
    // Decode place if it is an order from a customer phone
54
    $place = GETPOSTISSET("key") ? dol_decode(GETPOST('key')) : GETPOST('place', 'aZ09');
55
} else {
56
    $place = (GETPOST('place', 'aZ09') ? GETPOST('place', 'aZ09') : 0); // $place is id of table for Ba or Restaurant
57
}
58
$action = GETPOST('action', 'aZ09');
59
$setterminal = GETPOSTINT('setterminal');
60
$idproduct = GETPOSTINT('idproduct');
61
$mobilepage = GETPOST('mobilepage', 'alphanohtml'); // Set when page is loaded by a js .load()
62
63
if ($setterminal > 0) {
64
    $_SESSION["takeposterminal"] = $setterminal;
65
}
66
67
$langs->loadLangs(array("bills", "orders", "commercial", "cashdesk", "receiptprinter"));
68
69
if (!$user->hasRight('takepos', 'run') && !defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
70
    accessforbidden('No permission to run the takepos');
71
}
72
73
74
/*
75
 * View
76
 */
77
78
$title = 'TakePOS - Dolibarr ' . DOL_VERSION;
79
if (getDolGlobalString('MAIN_APPLICATION_TITLE')) {
80
    $title = 'TakePOS - ' . getDolGlobalString('MAIN_APPLICATION_TITLE');
81
}
82
83
// llxHeader
84
if (empty($mobilepage) && (empty($action) || ((getDolGlobalString('TAKEPOS_PHONE_BASIC_LAYOUT') == 1 && $conf->browser->layout == 'phone') || defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')))) {
85
    $head = '<meta name="apple-mobile-web-app-title" content="TakePOS"/>
86
	<meta name="apple-mobile-web-app-capable" content="yes">
87
	<meta name="mobile-web-app-capable" content="yes">
88
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>';
89
    $arrayofcss = array(
90
        '/takepos/css/phone.css',
91
    );
92
    $arrayofjs = array('/takepos/js/jquery.colorbox-min.js');
93
    $disablejs = 0;
94
    $disablehead = 0;
95
    ViewMain::topHtmlHead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss);
96
97
    print '<body style="background-color:#D1D1D1;">' . "\n";
98
} else {
99
    ViewMain::topHtmlHead('', $title);
100
101
    print '<body>' . "\n";
102
}
103
104
105
if ($action == "productinfo") {
106
    $prod = new Product($db);
107
    $prod->fetch($idproduct);
108
    print '<button type="button" class="publicphonebutton2 phoneblue total" onclick="AddProductConfirm(place, ' . $idproduct . ')">' . $langs->trans('Add') . '</button>';
109
    print "<br><b>" . $prod->label . "</b><br>";
110
    print '<img class="imgwrapper" width="60%" src="' . constant('BASE_URL') . '/takepos/public/auto_order.php?genimg=pro&query=pro&id=' . $idproduct . '">';
111
    print "<br>" . $prod->description;
112
    print "<br><b>" . price($prod->price_ttc, 1, $langs, 1, -1, -1, $conf->currency) . "</b>";
113
    print '<br>';
114
} elseif ($action == "publicpreorder") {
115
    print '<button type="button" class="publicphonebutton2 phoneblue total" onclick="TakeposPrintingOrder();">' . $langs->trans('Confirm') . '</button>';
116
    print "<br><br>";
117
    print '<div class="comment">
118
            <textarea class="textinput " placeholder="' . $langs->trans('Note') . '"></textarea>
119
			</div>';
120
    print '<br>';
121
} elseif ($action == "publicpayment") {
122
    $langs->loadLangs(array("orders"));
123
    print '<h1>' . $langs->trans('Ordered') . '</h1>';
124
    print '<button type="button" class="publicphonebutton2 phoneblue total" onclick="CheckPlease();">' . $langs->trans('Payment') . '</button>';
125
    print '<br>';
126
} elseif ($action == "checkplease") {
127
    if (GETPOSTISSET("payment")) {
128
        print '<h1>' . $langs->trans('Ordered') . '</h1>';
129
        $printer = new dolReceiptPrinter($db);
130
        $printer->initPrinter(getDolGlobalString('TAKEPOS_PRINTER_TO_USE' . $_SESSION["takeposterminal"]));
131
        if ($printer->getPrintConnector()) {
132
            if (!is_null($printer->printer)) {
133
                $printer->printer->feed();
134
                $printer->printer->feed();
135
                $printer->printer->text($langs->trans('IM'));
136
                $printer->printer->feed();
137
                $printer->printer->text($langs->trans('Place') . ": " . $place);
138
                $printer->printer->feed();
139
                $printer->printer->text($langs->trans('Payment') . ": " . $langs->trans(GETPOST('payment', 'alpha')));
140
                $printer->printer->feed();
141
                $printer->printer->feed();
142
                $printer->printer->feed();
143
                $printer->printer->feed();
144
                $printer->printer->feed();
145
            }
146
            $printer->close();
147
        }
148
    } else {
149
        print '<button type="button" class="publicphonebutton2 phoneblue total marginbottomonly" onclick="CheckPlease(\'Cash\')">' . $langs->trans('Cash') . '</button>';
150
        print '<button type="button" class="publicphonebutton2 phoneblue total marginbottomonly" onclick="CheckPlease(\'CreditCard\')">' . $langs->trans('CreditCard') . '</button>';
151
        print '<br>';
152
    }
153
} elseif ($action == "editline") {
154
    $placeid = GETPOSTINT('placeid');
155
    $selectedline = GETPOSTINT('selectedline');
156
    $invoice = new Facture($db);
157
    $invoice->fetch($placeid);
158
    foreach ($invoice->lines as $line) {
159
        if ($line->id == $selectedline) {
160
            $prod = new Product($db);
161
            $prod->fetch($line->fk_product);
162
            print "<b>" . $prod->label . "</b><br>";
163
            print '<img class="imgwrapper" width="60%" src="' . constant('BASE_URL') . '/takepos/public/auto_order.php?genimg=pro&query=pro&id=' . $line->fk_product . '">';
164
            print "<br>" . $prod->description;
165
            print "<br><b>" . price($prod->price_ttc, 1, $langs, 1, -1, -1, $conf->currency) . "</b>";
166
            print '<br>';
167
            print '<button type="button" class="publicphonebutton2 phonered width24" onclick="SetQty(place, ' . $selectedline . ', ' . ($line->qty - 1) . ')">-</button>';
168
            print '<button type="button" class="publicphonebutton2 phonegreen width24" onclick="SetQty(place, ' . $selectedline . ', ' . ($line->qty + 1) . ')">+</button>';
169
            print '<button type="button" class="publicphonebutton2 phoneblue width24" onclick="SetNote(place, ' . $selectedline . ')">' . $langs->trans('Note') . '</button>';
170
        }
171
    }
172
} else {
173
    ?>
174
    <div class="container">
175
        <div class="phonebuttonsrow center">
176
            <?php
177
            if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
178
                print '<button type="button" class="phonebutton uppercase" onclick="LoadPlacesList();">' . dol_trunc($langs->trans('Floors'), 5, 'right', 'UTF-8') . '</button>';
179
                print '<button type="button" class="phonebutton uppercase" onclick="LoadCats();">' . dol_trunc($langs->trans('Categories'), 5, 'right', 'UTF-8') . '</button>';
180
                print '<button type="button" class="phonebutton uppercase" onclick="TakeposPrintingOrder();">' . dol_trunc($langs->trans('Order'), 5, 'right', 'UTF-8') . '</button>';
181
                print '<button type="button" class="phonebutton uppercase" onclick="CheckPlease();">' . dol_trunc($langs->trans('Payment'), 5, 'right', 'UTF-8') . '</button>';
182
                print '<button type="button" class="phonebutton uppercase" onclick="Exit();">' . dol_trunc($langs->trans('Logout'), 5, 'right', 'UTF-8') . '</button>';
183
            } else {
184
                print '<button type="button" class="publicphonebutton phoneblue uppercase" onclick="LoadCats();">' . dol_trunc($langs->trans('Categories'), 8, 'right', 'UTF-8') . '</button>';
185
                print '<button type="button" class="publicphonebutton phoneorange uppercase" onclick="PublicPreOrder();">' . dol_trunc($langs->trans('Order'), 8, 'right', 'UTF-8') . '</button>';
186
                // Do not show the payment link when order done from public page (a customer must not be able to set its order to paid himself)
187
                //print '<button type="button" class="publicphonebutton phonegreen uppercase" onclick="CheckPlease();">'.dol_trunc($langs->trans('Payment'), 8, 'right', 'UTF-8').'</button>';
188
            }
189
            ?>
190
        </div>
191
        <div class="phonerow2">
192
            <div id="phonediv2" class="phonediv2"></div>
193
        </div>
194
        <div class="phonerow1">
195
            <div id="phonediv1" class="phonediv1"></div>
196
        </div>
197
    </div>
198
199
    <script type="text/javascript">
200
        <?php
201
        $categorie = new Categorie($db);
202
        $categories = $categorie->get_full_arbo('product', ((getDolGlobalInt('TAKEPOS_ROOT_CATEGORY_ID') > 0) ? $conf->global->TAKEPOS_ROOT_CATEGORY_ID : 0), 1);
203
204
        // Search root category to know its level
205
        //$conf->global->TAKEPOS_ROOT_CATEGORY_ID=0;
206
        $levelofrootcategory = 0;
207
        if (getDolGlobalInt('TAKEPOS_ROOT_CATEGORY_ID') > 0) {
208
            foreach ($categories as $key => $categorycursor) {
0 ignored issues
show
The expression $categories of type integer is not traversable.
Loading history...
209
                if ($categorycursor['id'] == getDolGlobalInt('TAKEPOS_ROOT_CATEGORY_ID')) {
210
                    $levelofrootcategory = $categorycursor['level'];
211
                    break;
212
                }
213
            }
214
        }
215
        $levelofmaincategories = $levelofrootcategory + 1;
216
217
        $maincategories = array();
218
        $subcategories = array();
219
        foreach ($categories as $key => $categorycursor) {
0 ignored issues
show
The expression $categories of type integer is not traversable.
Loading history...
220
            if ($categorycursor['level'] == $levelofmaincategories) {
221
                $maincategories[$key] = $categorycursor;
222
            } else {
223
                $subcategories[$key] = $categorycursor;
224
            }
225
        }
226
227
        sort($maincategories);
228
        sort($subcategories);
229
        ?>
230
231
        var categories = <?php echo json_encode($maincategories); ?>;
232
        var subcategories = <?php echo json_encode($subcategories); ?>;
233
234
        var currentcat;
235
        var pageproducts = 0;
236
        var pagecategories = 0;
237
        var pageactions = 0;
238
        var place = "<?php echo $place; ?>";
239
        var editaction = "qty";
240
        var editnumber = "";
241
242
243
        $(document).ready(function () {
244
            console.log("Refresh");
245
            LoadPlace(place);
246
        });
247
248
        function LoadPlace(placeid) {
249
            place = placeid;
250
            <?php
251
            if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
252
                echo '$("#phonediv2").load("' . constant('BASE_URL') . '/takepos/public/auto_order.php?mobilepage=invoice&place="+place+" #tablelines", function() {
253
			});';
254
            } else {
255
                echo '$("#phonediv2").load("' . constant('BASE_URL') . '/takepos/invoice.php?mobilepage=invoice&place="+place+" #tablelines", function() {
256
			});';
257
            }
258
            ?>
259
            LoadCats();
260
        }
261
262
        function AddProduct(placeid, productid) {
263
            <?php
264
            // If is a public terminal first show product information
265
            if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
266
                print 'place=placeid;
267
			$("#phonediv1").load("' . constant('BASE_URL') . '/takepos/public/auto_order.php?action=productinfo&token=' . newToken() . '&place="+place+"&idproduct="+productid, function() {
268
			});';
269
            } else {
270
                print 'AddProductConfirm(placeid, productid);';
271
            } ?>
272
        }
273
274
        function PublicPreOrder() {
275
            $("#phonediv1").load("'.DOL_URL_ROOT.'/takepos/public/auto_order.php?action=publicpreorder&token=<?php echo newToken(); ?>&place=" + place, function () {
276
            });
277
        }
278
279
        function AddProductConfirm(placeid, productid) {
280
            place = placeid;
281
            <?php
282
            if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
283
                echo '$("#phonediv2").load("' . constant('BASE_URL') . '/takepos/public/auto_order.php?mobilepage=invoice&action=addline&token=' . newToken() . '&place="+place+"&idproduct="+productid, function() {
284
			});';
285
            } else {
286
                echo '$("#phonediv2").load("' . constant('BASE_URL') . '/takepos/invoice.php?mobilepage=invoice&action=addline&token=' . newToken() . '&place="+place+"&idproduct="+productid, function() {
287
			});';
288
            } ?>
289
290
            return true;
291
        }
292
293
        function SetQty(place, selectedline, qty) {
294
            console.log("We click on SetQty()");
295
            <?php
296
            if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
297
            ?>
298
            if (qty == 0) {
299
                $("#phonediv2").load("<?php echo DOL_URL_ROOT ?>/takepos/public/auto_order.php?mobilepage=invoice&action=deleteline&token=<?php echo newToken(); ?>&place=" + place + "&idline=" + selectedline, function () {
300
                });
301
            } else {
302
                $("#phonediv2").load("<?php echo DOL_URL_ROOT ?>/takepos/public/auto_order.php?mobilepage=invoice&action=updateqty&token=<?php echo newToken(); ?>&place=" + place + "&idline=" + selectedline + "&number=" + qty, function () {
303
                });
304
            }
305
            <?php
306
            } else {
307
            ?>
308
            if (qty == 0) {
309
                $("#phonediv2").load("<?php echo DOL_URL_ROOT ?>/takepos/invoice.php?mobilepage=invoice&action=deleteline&token=<?php echo newToken(); ?>&place=" + place + "&idline=" + selectedline, function () {
310
                });
311
            } else {
312
                $("#phonediv2").load("<?php echo DOL_URL_ROOT ?>/takepos/invoice.php?mobilepage=invoice&action=updateqty&token=<?php echo newToken(); ?>&place=" + place + "&idline=" + selectedline + "&number=" + qty, function () {
313
                });
314
            }
315
            <?php
316
            } ?>
317
            LoadCats();
318
319
            return true;
320
        }
321
322
        function SetNote(place, selectedline) {
323
            console.log("We click on SetNote()");
324
            var note = prompt("<?php dol_escape_js($langs->trans('Note')); ?>", "");
325
            $("#phonediv2").load("<?php echo DOL_URL_ROOT ?>/takepos/public/auto_order.php?mobilepage=invoice&action=updateqty&token=<?php echo newToken(); ?>&place=" + place + "&idline=" + selectedline + "&number=" + qty, function () {
326
            });
327
            LoadCats();
328
        }
329
330
        function LoadCats() {
331
            console.log("We click on LoadCats()");
332
            <?php
333
            if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
334
                // Load invoice.php to get categories by using auto_order so it will define INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE
335
                echo '$("#phonediv1").load("' . constant('BASE_URL') . '/takepos/public/auto_order.php?format=div&mobilepage=cats&place="+place+" #tablelines", function() {
336
			});';
337
            } else {
338
                echo '$("#phonediv1").load("' . constant('BASE_URL') . '/takepos/invoice.php?mobilepage=cats&place="+place+"", function() {
339
			});';
340
            }
341
            ?>
342
        }
343
344
        function LoadProducts(idcat) {
345
            console.log("We click on LoadProducts()");
346
            <?php
347
            if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
348
                echo '$("#phonediv1").load("' . constant('BASE_URL') . '/takepos/public/auto_order.php?format=div&mobilepage=products&catid="+idcat+"&place="+place+"", function() {
349
			});';
350
            } else {
351
                echo '$("#phonediv1").load("' . constant('BASE_URL') . '/takepos/invoice.php?mobilepage=products&catid="+idcat+"&place="+place+"", function() {
352
			});';
353
            } ?>
354
        }
355
356
        function LoadPlacesList() {
357
            $("#phonediv1").load("invoice.php?mobilepage=places", function () {
358
            });
359
        }
360
361
        function TakeposPrintingOrder() {
362
            console.log("TakeposPrintingOrder");
363
            <?php
364
            if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
365
                echo '$("#phonediv2").load("' . constant('BASE_URL') . '/takepos/public/auto_order.php?action=order&token=' . newToken() . '&mobilepage=order&place="+place, function() {
366
			});';
367
                echo '$("#phonediv1").load("' . constant('BASE_URL') . '/takepos/public/auto_order.php?action=publicpayment&token=' . newToken() . '&place="+place, function() {
368
			});';
369
            } else {
370
                echo '$("#phonediv2").load("' . constant('BASE_URL') . '/takepos/invoice.php?action=order&token=' . newToken() . '&place="+place, function() {
371
			});';
372
            } ?>
373
        }
374
375
        function Exit() {
376
            console.log("Click on Exit");
377
            window.location.href = '<?php echo DOL_URL_ROOT ?>/user/logout.php?token=<?php echo newToken(); ?>';
378
        }
379
380
        function CheckPlease(payment) {
381
            console.log("Click on CheckPlease");
382
            if (payment == undefined) {
383
                $("#phonediv1").load("<?php echo DOL_URL_ROOT ?>/takepos/public/auto_order.php?action=checkplease&token=<?php echo newToken(); ?>&place=" + place, function () {
384
                });
385
            } else {
386
                console.log("Request the check to the waiter");
387
                $("#phonediv1").load("<?php echo DOL_URL_ROOT ?>/takepos/public/auto_order.php?action=checkplease&token=<?php echo newToken(); ?>&place=<?php echo urlencode($place); ?>&payment=" + payment, function () {
388
                });
389
            }
390
        }
391
392
    </script>
393
394
395
    <?php
396
    if (getDolGlobalString('TAKEPOS_NUM_TERMINALS') != "1" && $_SESSION["takeposterminal"] == "") {
397
        print '<div class="dialog-info-takepos-terminal" id="dialog-info" title="TakePOS">' . $langs->trans('TerminalSelect') . '</div>';
398
    }
399
}
400
401
print '</body>';
402
print '</html>';
403
404
405
$db->close();
406