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/**
19 *	\file       htdocs/takepos/pay.php
20 *	\ingroup	takepos
21 *	\brief      Page with the content of the popup to enter payments
22 */
23
24//if (! defined('NOREQUIREUSER'))	define('NOREQUIREUSER', '1');	// Not disabled cause need to load personalized language
25//if (! defined('NOREQUIREDB'))		define('NOREQUIREDB', '1');		// Not disabled cause need to load personalized language
26//if (! defined('NOREQUIRESOC'))		define('NOREQUIRESOC', '1');
27//if (! defined('NOREQUIRETRAN'))		define('NOREQUIRETRAN', '1');
28if (!defined('NOCSRFCHECK')) {
29	define('NOCSRFCHECK', '1');
30}
31if (!defined('NOTOKENRENEWAL')) {
32	define('NOTOKENRENEWAL', '1');
33}
34if (!defined('NOREQUIREMENU')) {
35	define('NOREQUIREMENU', '1');
36}
37if (!defined('NOREQUIREHTML')) {
38	define('NOREQUIREHTML', '1');
39}
40//if (!defined('NOREQUIREAJAX')) {
41//	define('NOREQUIREAJAX', '1');
42//}
43
44require '../main.inc.php'; // Load $user and permissions
45require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
46
47$langs->loadLangs(array("main", "bills", "cashdesk", "banks"));
48
49$place = (GETPOST('place', 'aZ09') ? GETPOST('place', 'aZ09') : '0'); // $place is id of table for Bar or Restaurant
50
51$invoiceid = GETPOST('invoiceid', 'int');
52
53if (empty($user->rights->takepos->run)) {
54	accessforbidden();
55}
56
57
58/*
59 * View
60 */
61
62$invoice = new Facture($db);
63if ($invoiceid > 0) {
64	$invoice->fetch($invoiceid);
65} else {
66	$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture where ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")'";
67	$resql = $db->query($sql);
68	$obj = $db->fetch_object($resql);
69	if ($obj) {
70		$invoiceid = $obj->rowid;
71	}
72	if (!$invoiceid) {
73		$invoiceid = 0; // Invoice does not exist yet
74	} else {
75		$invoice->fetch($invoiceid);
76	}
77}
78
79$arrayofcss = array('/takepos/css/pos.css.php');
80$arrayofjs = array();
81
82$head = '';
83$title = '';
84$disablejs = 0;
85$disablehead = 0;
86
87top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss);
88
89// Define list of possible payments
90$arrayOfValidPaymentModes = array();
91$arrayOfValidBankAccount = array();
92
93$sql = "SELECT code, libelle as label FROM ".MAIN_DB_PREFIX."c_paiement";
94$sql .= " WHERE entity IN (".getEntity('c_paiement').")";
95$sql .= " AND active = 1";
96$sql .= " ORDER BY libelle";
97$resql = $db->query($sql);
98
99if ($resql) {
100	while ($obj = $db->fetch_object($resql)) {
101		$paycode = $obj->code;
102		if ($paycode == 'LIQ') {
103			$paycode = 'CASH';
104		}
105		if ($paycode == 'CB') {
106			$paycode = 'CB';
107		}
108		if ($paycode == 'CHQ') {
109			$paycode = 'CHEQUE';
110		}
111
112		$accountname = "CASHDESK_ID_BANKACCOUNT_".$paycode.$_SESSION["takeposterminal"];
113		if (!empty($conf->global->$accountname) && $conf->global->$accountname > 0) {
114			$arrayOfValidBankAccount[$conf->global->$accountname] = $conf->global->$accountname;
115			$arrayOfValidPaymentModes[] = $obj;
116		}
117	}
118}
119?>
120<link rel="stylesheet" href="css/pos.css.php">
121<?php
122if ($conf->global->TAKEPOS_COLOR_THEME == 1) {
123	print '<link rel="stylesheet" href="css/colorful.css">';
124}
125?>
126</head>
127<body>
128
129<script>
130<?php
131$remaintopay = 0;
132if ($invoice->id > 0) {
133	$remaintopay = $invoice->getRemainToPay();
134}
135$alreadypayed = (is_object($invoice) ? ($invoice->total_ttc - $remaintopay) : 0);
136
137if ($conf->global->TAKEPOS_NUMPAD == 0) {
138	print "var received='';";
139} else {
140	print "var received=0;";
141}
142
143?>
144	var alreadypayed = <?php echo $alreadypayed ?>;
145
146	function addreceived(price)
147	{
148		<?php
149		if (empty($conf->global->TAKEPOS_NUMPAD)) {
150			print 'received+=String(price);'."\n";
151		} else {
152			print 'received+=parseFloat(price);'."\n";
153		}
154		?>
155		$('.change1').html(pricejs(parseFloat(received), 'MT'));
156		$('.change1').val(parseFloat(received));
157		alreadypaydplusreceived=price2numjs(alreadypayed + parseFloat(received));
158		//console.log("already+received = "+alreadypaydplusreceived);
159		//console.log("total_ttc = "+<?php echo $invoice->total_ttc; ?>);
160		if (alreadypaydplusreceived > <?php echo $invoice->total_ttc; ?>)
161		   {
162			var change=parseFloat(alreadypayed + parseFloat(received) - <?php echo $invoice->total_ttc; ?>);
163			$('.change2').html(pricejs(change, 'MT'));
164			$('.change2').val(change);
165			$('.change1').removeClass('colorred');
166			$('.change1').addClass('colorgreen');
167			$('.change2').removeClass('colorwhite');
168			$('.change2').addClass('colorred');
169		}
170		else
171		{
172			$('.change2').html(pricejs(0, 'MT'));
173			$('.change2').val(0);
174			if (alreadypaydplusreceived == <?php echo $invoice->total_ttc; ?>)
175			{
176				$('.change1').removeClass('colorred');
177				$('.change1').addClass('colorgreen');
178				$('.change2').removeClass('colorred');
179				$('.change2').addClass('colorwhite');
180			}
181			else
182			{
183				$('.change1').removeClass('colorgreen');
184				$('.change1').addClass('colorred');
185				$('.change2').removeClass('colorred');
186				$('.change2').addClass('colorwhite');
187			}
188		}
189	}
190
191	function reset()
192	{
193		received=0;
194		$('.change1').html(pricejs(received, 'MT'));
195		$('.change1').val(price2numjs(received));
196		$('.change2').html(pricejs(received, 'MT'));
197		$('.change2').val(price2numjs(received));
198		$('.change1').removeClass('colorgreen');
199		$('.change1').addClass('colorred');
200		$('.change2').removeClass('colorred');
201		$('.change2').addClass('colorwhite');
202	}
203
204	function Validate(payment)
205	{
206		var invoiceid = <?php echo ($invoiceid > 0 ? $invoiceid : 0); ?>;
207		var accountid = $("#selectaccountid").val();
208		var amountpayed = $("#change1").val();
209		var excess = $("#change2").val();
210		if (amountpayed > <?php echo $invoice->total_ttc; ?>) {
211			amountpayed = <?php echo $invoice->total_ttc; ?>;
212		}
213		console.log("We click on the payment mode to pay amount = "+amountpayed);
214		parent.$("#poslines").load("invoice.php?place=<?php echo $place; ?>&action=valid&pay="+payment+"&amount="+amountpayed+"&excess="+excess+"&invoiceid="+invoiceid+"&accountid="+accountid, function() {
215			if (amountpayed > <?php echo $remaintopay; ?> || amountpayed == <?php echo $remaintopay; ?> || amountpayed==0 ) {
216				console.log("Close popup");
217				parent.$.colorbox.close();
218			}
219			else {
220				console.log("Amount is not comple, so we do NOT close popup and reload it.");
221				location.reload();
222			}
223		});
224	}
225
226	function ValidateSumup() {
227		console.log("Launch ValidateSumup");
228		<?php $_SESSION['SMP_CURRENT_PAYMENT'] = "NEW" ?>
229		var invoiceid = <?php echo($invoiceid > 0 ? $invoiceid : 0); ?>;
230		var amountpayed = $("#change1").val();
231		if (amountpayed > <?php echo $invoice->total_ttc; ?>) {
232			amountpayed = <?php echo $invoice->total_ttc; ?>;
233		}
234
235		// Starting sumup app
236		window.open('sumupmerchant://pay/1.0?affiliate-key=<?php echo $conf->global->TAKEPOS_SUMUP_AFFILIATE ?>&app-id=<?php echo $conf->global->TAKEPOS_SUMUP_APPID ?>&total=' + amountpayed + '&currency=EUR&title=' + invoiceid + '&callback=<?php echo DOL_MAIN_URL_ROOT ?>/takepos/smpcb.php');
237
238		var loop = window.setInterval(function () {
239			$.ajax({
240				method: 'POST',
241				data: { token: '<?php echo currentToken(); ?>' },
242				url: '<?php echo DOL_URL_ROOT ?>/takepos/smpcb.php?status' }).done(function (data) {
243				console.log(data);
244				if (data === "SUCCESS") {
245					parent.$("#poslines").load("invoice.php?place=<?php echo $place; ?>&action=valid&pay=CB&amount=" + amountpayed + "&invoiceid=" + invoiceid, function () {
246						//parent.$("#poslines").scrollTop(parent.$("#poslines")[0].scrollHeight);
247						parent.$.colorbox.close();
248						//parent.setFocusOnSearchField();	// This does not have effect
249					});
250					clearInterval(loop);
251				} else if (data === "FAILED") {
252					parent.$.colorbox.close();
253					clearInterval(loop);
254				}
255			});
256		}, 2500);
257	}
258</script>
259
260<div style="position:relative; padding-top: 20px; left:5%; height:150px; width:90%;">
261	<div class="paymentbordline paymentbordlinetotal center">
262		<span class="takepospay colorwhite"><?php echo $langs->trans('TotalTTC'); ?>: <span id="totaldisplay" class="colorwhite"><?php echo price($invoice->total_ttc, 1, '', 1, -1, -1, $invoice->multicurrency_code); ?></span></span>
263	</div>
264	<?php if ($remaintopay != $invoice->total_ttc) { ?>
265		<div class="paymentbordline paymentbordlineremain center">
266			<span class="takepospay colorwhite"><?php echo $langs->trans('RemainToPay'); ?>: <span id="remaintopaydisplay" class="colorwhite"><?php echo price($remaintopay, 1, '', 1, -1, -1, $invoice->multicurrency_code); ?></span></span>
267		</div>
268	<?php } ?>
269	<div class="paymentbordline paymentbordlinereceived center">
270		<span class="takepospay colorwhite"><?php echo $langs->trans("Received"); ?>: <span class="change1 colorred"><?php echo price(0, 1, '', 1, -1, -1, $invoice->multicurrency_code); ?></span><input type="hidden" id="change1" class="change1" value="0"></span>
271	</div>
272	<div class="paymentbordline paymentbordlinechange center">
273		<span class="takepospay colorwhite"><?php echo $langs->trans("Change"); ?>: <span class="change2 colorwhite"><?php echo price(0, 1, '', 1, -1, -1, $invoice->multicurrency_code); ?></span><input type="hidden" id="change2" class="change2" value="0"></span>
274	</div>
275	<?php
276	if (!empty($conf->global->TAKEPOS_CAN_FORCE_BANK_ACCOUNT_DURING_PAYMENT)) {
277		require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
278		print '<div class="paymentbordline paddingtop paddingbottom center">';
279		$filter = '';
280		$form = new Form($db);
281		print '<span class="takepospay colorwhite">'.$langs->trans("BankAccount").': </span>';
282		$form->select_comptes(0, 'accountid', 0, $filter, 1, '');
283		print ajax_combobox('selectaccountid');
284		print '</div>';
285	}
286	?>
287</div>
288
289<div style="position:absolute; left:5%; height:52%; width:90%;">
290<?php
291$action_buttons = array(
292	array(
293		"function" =>"reset()",
294		"span" => "style='font-size: 150%;'",
295		"text" => "C",
296		"class" => "poscolorblue"
297	),
298	array(
299		"function" => "parent.$.colorbox.close();",
300		"span" => "id='printtext' style='font-weight: bold; font-size: 18pt;'",
301		"text" => "X",
302		"class" => "poscolordelete"
303	),
304);
305$numpad = $conf->global->TAKEPOS_NUMPAD;
306
307print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '7' : '10').');">'.($numpad == 0 ? '7' : '10').'</button>';
308print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '8' : '20').');">'.($numpad == 0 ? '8' : '20').'</button>';
309print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '9' : '50').');">'.($numpad == 0 ? '9' : '50').'</button>';
310?>
311<?php if (count($arrayOfValidPaymentModes) > 0) {
312	$paycode = $arrayOfValidPaymentModes[0]->code;
313	$payIcon = '';
314	if ($paycode == 'LIQ') {
315		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
316			$payIcon = 'coins';
317		}
318	} elseif ($paycode == 'CB') {
319		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
320			$payIcon = 'credit-card';
321		}
322	} elseif ($paycode == 'CHQ') {
323		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
324			$payIcon = 'money-check';
325		}
326	}
327
328	print '<button type="button" class="calcbutton2" onclick="Validate(\''.dol_escape_js($paycode).'\');">'.(!empty($payIcon) ? '<span class="fa fa-2x fa-'.$payIcon.' iconwithlabel"></span><span class="hideonsmartphone"><br>'. $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[0]->code) : $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[0]->code)).'</span></button>';
329} else {
330	print '<button type="button" class="calcbutton2">'.$langs->trans("NoPaimementModesDefined").'</button>';
331}
332
333print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '4' : '1').');">'.($numpad == 0 ? '4' : '1').'</button>';
334print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '5' : '2').');">'.($numpad == 0 ? '5' : '2').'</button>';
335print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '6' : '5').');">'.($numpad == 0 ? '6' : '5').'</button>';
336?>
337<?php if (count($arrayOfValidPaymentModes) > 1) {
338	$paycode = $arrayOfValidPaymentModes[1]->code;
339	$payIcon = '';
340	if ($paycode == 'LIQ') {
341		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
342			$payIcon = 'coins';
343		}
344	} elseif ($paycode == 'CB') {
345		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
346			$payIcon = 'credit-card';
347		}
348	} elseif ($paycode == 'CHQ') {
349		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
350			$payIcon = 'money-check';
351		}
352	}
353
354	print '<button type="button" class="calcbutton2" onclick="Validate(\''.dol_escape_js($paycode).'\');">'.(!empty($payIcon) ? '<span class="fa fa-2x fa-'.$payIcon.' iconwithlabel"></span><br> '. $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[1]->code) : $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[1]->code)).'</button>';
355} else {
356	$button = array_pop($action_buttons);
357	print '<button type="button" class="calcbutton2" onclick="'.$button["function"].'"><span '.$button["span"].'>'.$button["text"].'</span></button>';
358}
359
360print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '1' : '0.10').');">'.($numpad == 0 ? '1' : '0.10').'</button>';
361print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '2' : '0.20').');">'.($numpad == 0 ? '2' : '0.20').'</button>';
362print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '3' : '0.50').');">'.($numpad == 0 ? '3' : '0.50').'</button>';
363?>
364<?php if (count($arrayOfValidPaymentModes) > 2) {
365	$paycode = $arrayOfValidPaymentModes[2]->code;
366	$payIcon = '';
367	if ($paycode == 'LIQ') {
368		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
369			$payIcon = 'coins';
370		}
371	} elseif ($paycode == 'CB') {
372		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
373			$payIcon = 'credit-card';
374		}
375	} elseif ($paycode == 'CHQ') {
376		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
377			$payIcon = 'money-check';
378		}
379	}
380
381	print '<button type="button" class="calcbutton2" onclick="Validate(\''.dol_escape_js($paycode).'\');">'.(!empty($payIcon) ? '<span class="fa fa-2x fa-'.$payIcon.' iconwithlabel"></span><br>'. $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[2]->code) : $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[2]->code)).'</button>';
382} else {
383	$button = array_pop($action_buttons);
384	print '<button type="button" class="calcbutton2" onclick="'.$button["function"].'"><span '.$button["span"].'>'.$button["text"].'</span></button>';
385}
386
387print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '0' : '0.01').');">'.($numpad == 0 ? '0' : '0.01').'</button>';
388print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '\'000\'' : '0.02').');">'.($numpad == 0 ? '000' : '0.02').'</button>';
389print '<button type="button" class="calcbutton" onclick="addreceived('.($numpad == 0 ? '\'.\'' : '0.05').');">'.($numpad == 0 ? '.' : '0.05').'</button>';
390
391$i = 3;
392while ($i < count($arrayOfValidPaymentModes)) {
393	$paycode = $arrayOfValidPaymentModes[$i]->code;
394	$payIcon = '';
395	if ($paycode == 'LIQ') {
396		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
397			$payIcon = 'coins';
398		}
399	} elseif ($paycode == 'CB') {
400		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
401			$payIcon = 'credit-card';
402		}
403	} elseif ($paycode == 'CHQ') {
404		if (!isset($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON) || !empty($conf->global->TAKEPOS_NUMPAD_USE_PAYMENT_ICON)) {
405			$payIcon = 'money-check';
406		}
407	}
408
409	print '<button type="button" class="calcbutton2" onclick="Validate(\''.dol_escape_js($paycode).'\');">'.(!empty($payIcon) ? '<span class="fa fa-2x fa-'.$payIcon.' iconwithlabel"></span><br>'. $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[$i]->code) : $langs->trans("PaymentTypeShort".$arrayOfValidPaymentModes[$i]->code)).'</button>';
410	$i = $i + 1;
411}
412
413$keyforsumupbank = "CASHDESK_ID_BANKACCOUNT_SUMUP".$_SESSION["takeposterminal"];
414if ($conf->global->TAKEPOS_ENABLE_SUMUP) {
415	if (!empty($conf->global->$keyforsumupbank)) {
416		print '<button type="button" class="calcbutton2" onclick="ValidateSumup();">Sumup</button>';
417	} else {
418		$langs->loadLangs(array("errors", "admin"));
419		print '<button type="button" class="calcbutton2 disabled" title="'.$langs->trans("SetupNotComplete").'">Sumup</button>';
420	}
421}
422
423$class = ($i == 3) ? "calcbutton3" : "calcbutton2";
424foreach ($action_buttons as $button) {
425	$newclass = $class.($button["class"] ? " ".$button["class"] : "");
426	print '<button type="button" class="'.$newclass.'" onclick="'.$button["function"].'"><span '.$button["span"].'>'.$button["text"].'</span></button>';
427}
428
429if ($conf->global->TAKEPOS_DELAYED_PAYMENT) {
430	print '<button type="button" class="calcbutton2" onclick="Validate(\'delayed\');">'.$langs->trans("Reported").'</button>';
431}
432?>
433</div>
434
435</body>
436</html>
437