1<?php
2/**
3 * Copyright (C) 2018    Andreu Bisquerra    <jove@bisquerra.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19/**
20 *	\file       htdocs/takepos/invoice.php
21 *	\ingroup    takepos
22 *	\brief      Page to generate section with list of lines
23 */
24
25// if (! defined('NOREQUIREUSER'))    define('NOREQUIREUSER', '1');    // Not disabled cause need to load personalized language
26// if (! defined('NOREQUIREDB'))        define('NOREQUIREDB', '1');        // Not disabled cause need to load personalized language
27// if (! defined('NOREQUIRESOC'))        define('NOREQUIRESOC', '1');
28// if (! defined('NOREQUIRETRAN'))        define('NOREQUIRETRAN', '1');
29if (!defined('NOCSRFCHECK')) { define('NOCSRFCHECK', '1'); }
30if (!defined('NOTOKENRENEWAL')) { define('NOTOKENRENEWAL', '1'); }
31if (!defined('NOREQUIREMENU')) { define('NOREQUIREMENU', '1'); }
32if (!defined('NOREQUIREHTML')) { define('NOREQUIREHTML', '1'); }
33if (!defined('NOREQUIREAJAX')) { define('NOREQUIREAJAX', '1'); }
34
35if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) require '../main.inc.php';
36require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
37require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
38require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
39
40global $mysoc;
41
42$langs->loadLangs(array("companies", "commercial", "bills", "cashdesk", "stocks", "banks"));
43
44$id = GETPOST('id', 'int');
45$action = GETPOST('action', 'aZ09');
46$idproduct = GETPOST('idproduct', 'int');
47$place = (GETPOST('place', 'aZ09') ? GETPOST('place', 'aZ09') : 0); // $place is id of table for Bar or Restaurant
48$placeid = 0; // $placeid is ID of invoice
49// Terminal is stored into $_SESSION["takeposterminal"];
50
51if (empty($user->rights->takepos->run) && !defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
52	accessforbidden();
53}
54
55
56/*
57 * View
58 */
59
60if (($conf->global->TAKEPOS_PHONE_BASIC_LAYOUT == 1 && $conf->browser->layout == 'phone') || defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE'))
61{
62	// DIRECT LINK TO THIS PAGE FROM MOBILE AND NO TERMINAL SELECTED
63	if ($_SESSION["takeposterminal"] == "")
64	{
65		if ($conf->global->TAKEPOS_NUM_TERMINALS == "1") $_SESSION["takeposterminal"] = 1;
66		else {
67			header("Location: ".DOL_URL_ROOT."/takepos/index.php");
68			exit;
69		}
70	}
71	$mobilepage = GETPOST('mobilepage', 'alpha');
72	$title = 'TakePOS - Dolibarr '.DOL_VERSION;
73	if (!empty($conf->global->MAIN_APPLICATION_TITLE)) $title = 'TakePOS - '.$conf->global->MAIN_APPLICATION_TITLE;
74	$head = '<meta name="apple-mobile-web-app-title" content="TakePOS"/>
75	<meta name="apple-mobile-web-app-capable" content="yes">
76	<meta name="mobile-web-app-capable" content="yes">
77	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>';
78	$arrayofcss = array(
79	'/takepos/css/pos.css.php',
80	'/takepos/js/jquery.colorbox-min.js'
81	);
82	$arrayofjs = array('/takepos/js/jquery.colorbox-min.js');
83	top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss);
84}
85
86/**
87 * Abort invoice creationg with a given error message
88 *
89 * @param   string  $message        Message explaining the error to the user
90 * @return	void
91 */
92function fail($message)
93{
94	header($_SERVER['SERVER_PROTOCOL'].' 500 Internal Server Error', true, 500);
95	die($message);
96}
97
98
99
100$number = GETPOST('number', 'alpha');
101$idline = GETPOST('idline', 'int');
102$selectedline = GETPOST('selectedline', 'int');
103$desc = GETPOST('desc', 'alphanohtml');
104$pay = GETPOST('pay', 'aZ09');
105$amountofpayment = price2num(GETPOST('amount', 'alpha'));
106
107$invoiceid = GETPOST('invoiceid', 'int');
108
109$paycode = $pay;
110if ($pay == 'cash')   $paycode = 'LIQ'; // For backward compatibility
111if ($pay == 'card')   $paycode = 'CB'; // For backward compatibility
112if ($pay == 'cheque') $paycode = 'CHQ'; // For backward compatibility
113
114// Retrieve paiementid
115$sql = "SELECT id FROM ".MAIN_DB_PREFIX."c_paiement";
116$sql .= " WHERE entity IN (".getEntity('c_paiement').")";
117$sql .= " AND code = '".$db->escape($paycode)."'";
118$resql = $db->query($sql);
119$obj = $db->fetch_object($resql);
120$paiementid = $obj->id;
121
122$invoice = new Facture($db);
123if ($invoiceid > 0)
124{
125	$ret = $invoice->fetch($invoiceid);
126} else {
127	$ret = $invoice->fetch('', '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')');
128}
129if ($ret > 0)
130{
131	$placeid = $invoice->id;
132}
133
134$constforcompanyid = 'CASHDESK_ID_THIRDPARTY'.$_SESSION["takeposterminal"];
135
136$soc = new Societe($db);
137if ($invoice->socid > 0) $soc->fetch($invoice->socid);
138else $soc->fetch($conf->global->$constforcompanyid);
139
140
141/*
142 * Actions
143 */
144
145// Action to record a payment on a TakePOS invoice
146if ($action == 'valid' && $user->rights->facture->creer)
147{
148	$bankaccount = 0;
149	$error = 0;
150
151	if (!empty($conf->global->TAKEPOS_CAN_FORCE_BANK_ACCOUNT_DURING_PAYMENT)) {
152		$bankaccount = GETPOST('accountid', 'int');
153	} else {
154		if ($pay == "cash") $bankaccount = $conf->global->{'CASHDESK_ID_BANKACCOUNT_CASH'.$_SESSION["takeposterminal"]};            // For backward compatibility
155		elseif ($pay == "card") $bankaccount = $conf->global->{'CASHDESK_ID_BANKACCOUNT_CB'.$_SESSION["takeposterminal"]};          // For backward compatibility
156		elseif ($pay == "cheque") $bankaccount = $conf->global->{'CASHDESK_ID_BANKACCOUNT_CHEQUE'.$_SESSION["takeposterminal"]};    // For backward compatibility
157		else {
158			$accountname = "CASHDESK_ID_BANKACCOUNT_".$pay.$_SESSION["takeposterminal"];
159			$bankaccount = $conf->global->$accountname;
160		}
161	}
162
163	if ($bankaccount <= 0 && $pay != "delayed") {
164		$errormsg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount"));
165		$error++;
166	}
167
168	$now = dol_now();
169	$res = 0;
170
171	$invoice = new Facture($db);
172	$invoice->fetch($placeid);
173
174	if ($invoice->total_ttc < 0) {
175		$invoice->type = $invoice::TYPE_CREDIT_NOTE;
176
177		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture WHERE";
178		$sql .= " fk_soc = ".((int) $invoice->socid);
179		$sql .= " AND type <> ".Facture::TYPE_CREDIT_NOTE;
180		$sql .= " AND fk_statut >= ".$invoice::STATUS_VALIDATED;
181		$sql .= " ORDER BY rowid DESC";
182
183		$resql = $db->query($sql);
184		if ($resql) {
185			$obj = $db->fetch_object($resql);
186			$fk_source = $obj->rowid;
187			if ($fk_source == null) {
188				fail($langs->transnoentitiesnoconv("NoPreviousBillForCustomer"));
189			}
190		} else {
191			fail($langs->transnoentitiesnoconv("NoPreviousBillForCustomer"));
192		}
193		$invoice->fk_facture_source = $fk_source;
194		$invoice->update($user);
195	}
196
197	//$sav_FACTURE_ADDON = '';
198	//if (!empty($conf->global->TAKEPOS_ADDON)) {
199	//	$sav_FACTURE_ADDON = $conf->global->FACTURE_ADDON;
200	//	if ($conf->global->TAKEPOS_ADDON == "terminal") $conf->global->FACTURE_ADDON = $conf->global->{'TAKEPOS_ADDON'.$_SESSION["takeposterminal"]};
201	//	else $conf->global->FACTURE_ADDON = $conf->global->TAKEPOS_ADDON;
202	//}
203
204	$constantforkey = 'CASHDESK_NO_DECREASE_STOCK'.$_SESSION["takeposterminal"];
205	if ($error) {
206		dol_htmloutput_errors($errormsg, null, 1);
207	} elseif ($invoice->statut != Facture::STATUS_DRAFT) {
208		//If invoice is validated but it is not fully paid is not error and make the payment
209		if ($invoice->getRemainToPay() > 0) {
210			$res = 1;
211		} else {
212			dol_syslog("Sale already validated");
213			dol_htmloutput_errors($langs->trans("InvoiceIsAlreadyValidated", "TakePos"), null, 1);
214		}
215	} elseif (count($invoice->lines) == 0) {
216		$error++;
217		dol_syslog('Sale without lines');
218		dol_htmloutput_errors($langs->trans("NoLinesToBill", "TakePos"), null, 1);
219	} elseif (!empty($conf->stock->enabled) && $conf->global->$constantforkey != "1") {
220		$savconst = $conf->global->STOCK_CALCULATE_ON_BILL;
221		$conf->global->STOCK_CALCULATE_ON_BILL = 1;
222
223		$constantforkey = 'CASHDESK_ID_WAREHOUSE'.$_SESSION["takeposterminal"];
224		dol_syslog("Validate invoice with stock change into warehouse defined into constant ".$constantforkey." = ".$conf->global->$constantforkey);
225		$batch_rule = 0;
226		if (!empty($conf->productbatch->enabled) && !empty($conf->global->CASHDESK_FORCE_DECREASE_STOCK)) {
227			require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
228			$batch_rule = Productbatch::BATCH_RULE_SELLBY_EATBY_DATES_FIRST;
229		}
230		$res = $invoice->validate($user, '', $conf->global->$constantforkey, 0, $batch_rule);
231
232		$conf->global->STOCK_CALCULATE_ON_BILL = $savconst;
233	} else {
234		$res = $invoice->validate($user);
235		if ($res < 0) {
236			$error++;
237			dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
238		}
239	}
240
241	// Restore save values
242	//if (!empty($sav_FACTURE_ADDON))
243	//{
244	//	$conf->global->FACTURE_ADDON = $sav_FACTURE_ADDON;
245	//}
246
247	// Add the payment
248	if (!$error && $res >= 0) {
249		$remaintopay = $invoice->getRemainToPay();
250		if ($remaintopay > 0) {
251			$payment = new Paiement($db);
252			$payment->datepaye = $now;
253			$payment->fk_account = $bankaccount;
254			$payment->amounts[$invoice->id] = $amountofpayment;
255			if ($pay == 'cash') $payment->pos_change = price2num(GETPOST('excess', 'alpha'));
256
257			// If user has not used change control, add total invoice payment
258			// Or if user has used change control and the amount of payment is higher than remain to pay, add the remain to pay
259			if ($amountofpayment == 0 || $amountofpayment > $remaintopay) $payment->amounts[$invoice->id] = $remaintopay;
260
261			$payment->paiementid = $paiementid;
262			$payment->num_payment = $invoice->ref;
263
264			if ($pay != "delayed") {
265				$payment->create($user);
266				$payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccount, '', '');
267				$remaintopay = $invoice->getRemainToPay(); // Recalculate remain to pay after the payment is recorded
268			}
269		}
270
271		if ($remaintopay == 0) {
272			dol_syslog("Invoice is paid, so we set it to status Paid");
273			$result = $invoice->set_paid($user);
274			if ($result > 0) $invoice->paye = 1;
275			// set payment method
276			$invoice->setPaymentMethods($paiementid);
277		} else {
278			dol_syslog("Invoice is not paid, remain to pay = ".$remaintopay);
279		}
280	} else {
281		dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
282	}
283}
284
285if ($action == 'creditnote')
286{
287	$creditnote = new Facture($db);
288	$creditnote->socid = $invoice->socid;
289	$creditnote->date = dol_now();
290	$creditnote->type = Facture::TYPE_CREDIT_NOTE;
291	$creditnote->fk_facture_source = $placeid;
292	$creditnote->remise_absolue = $invoice->remise_absolue;
293	$creditnote->remise_percent = $invoice->remise_percent;
294	$creditnote->create($user);
295
296	foreach ($invoice->lines as $line)
297	{
298		// Extrafields
299		if (method_exists($line, 'fetch_optionals')) {
300			// load extrafields
301			$line->fetch_optionals();
302		}
303		// Reset fk_parent_line for no child products and special product
304		if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
305			$fk_parent_line = 0;
306		}
307		if ($invoice->type == Facture::TYPE_SITUATION)
308		{
309			$source_fk_prev_id = $line->fk_prev_id; // temporary storing situation invoice fk_prev_id
310			$line->fk_prev_id  = $line->id; // The new line of the new credit note we are creating must be linked to the situation invoice line it is created from
311			if (!empty($invoice->tab_previous_situation_invoice))
312			{
313				// search the last standard invoice in cycle and the possible credit note between this last and invoice
314				// TODO Move this out of loop of $invoice->lines
315				$tab_jumped_credit_notes = array();
316				$lineIndex = count($invoice->tab_previous_situation_invoice) - 1;
317				$searchPreviousInvoice = true;
318				while ($searchPreviousInvoice)
319				{
320					if ($invoice->tab_previous_situation_invoice[$lineIndex]->type == Facture::TYPE_SITUATION || $lineIndex < 1)
321					{
322						$searchPreviousInvoice = false; // find, exit;
323						break;
324					} else {
325						if ($invoice->tab_previous_situation_invoice[$lineIndex]->type == Facture::TYPE_CREDIT_NOTE) {
326							$tab_jumped_credit_notes[$lineIndex] = $invoice->tab_previous_situation_invoice[$lineIndex]->id;
327						}
328						$lineIndex--; // go to previous invoice in cycle
329					}
330				}
331
332				$maxPrevSituationPercent = 0;
333				foreach ($invoice->tab_previous_situation_invoice[$lineIndex]->lines as $prevLine)
334				{
335					if ($prevLine->id == $source_fk_prev_id)
336					{
337						$maxPrevSituationPercent = max($maxPrevSituationPercent, $prevLine->situation_percent);
338
339						//$line->subprice  = $line->subprice - $prevLine->subprice;
340						$line->total_ht  = $line->total_ht - $prevLine->total_ht;
341						$line->total_tva = $line->total_tva - $prevLine->total_tva;
342						$line->total_ttc = $line->total_ttc - $prevLine->total_ttc;
343						$line->total_localtax1 = $line->total_localtax1 - $prevLine->total_localtax1;
344						$line->total_localtax2 = $line->total_localtax2 - $prevLine->total_localtax2;
345
346						$line->multicurrency_subprice  = $line->multicurrency_subprice - $prevLine->multicurrency_subprice;
347						$line->multicurrency_total_ht  = $line->multicurrency_total_ht - $prevLine->multicurrency_total_ht;
348						$line->multicurrency_total_tva = $line->multicurrency_total_tva - $prevLine->multicurrency_total_tva;
349						$line->multicurrency_total_ttc = $line->multicurrency_total_ttc - $prevLine->multicurrency_total_ttc;
350					}
351				}
352
353				// prorata
354				$line->situation_percent = $maxPrevSituationPercent - $line->situation_percent;
355
356				//print 'New line based on invoice id '.$invoice->tab_previous_situation_invoice[$lineIndex]->id.' fk_prev_id='.$source_fk_prev_id.' will be fk_prev_id='.$line->fk_prev_id.' '.$line->total_ht.' '.$line->situation_percent.'<br>';
357
358				// If there is some credit note between last situation invoice and invoice used for credit note generation (note: credit notes are stored as delta)
359				$maxPrevSituationPercent = 0;
360				foreach ($tab_jumped_credit_notes as $index => $creditnoteid) {
361					foreach ($invoice->tab_previous_situation_invoice[$index]->lines as $prevLine)
362					{
363						if ($prevLine->fk_prev_id == $source_fk_prev_id)
364						{
365							$maxPrevSituationPercent = $prevLine->situation_percent;
366
367							$line->total_ht  -= $prevLine->total_ht;
368							$line->total_tva -= $prevLine->total_tva;
369							$line->total_ttc -= $prevLine->total_ttc;
370							$line->total_localtax1 -= $prevLine->total_localtax1;
371							$line->total_localtax2 -= $prevLine->total_localtax2;
372
373							$line->multicurrency_subprice  -= $prevLine->multicurrency_subprice;
374							$line->multicurrency_total_ht  -= $prevLine->multicurrency_total_ht;
375							$line->multicurrency_total_tva -= $prevLine->multicurrency_total_tva;
376							$line->multicurrency_total_ttc -= $prevLine->multicurrency_total_ttc;
377						}
378					}
379				}
380
381				// prorata
382				$line->situation_percent += $maxPrevSituationPercent;
383
384				//print 'New line based on invoice id '.$invoice->tab_previous_situation_invoice[$lineIndex]->id.' fk_prev_id='.$source_fk_prev_id.' will be fk_prev_id='.$line->fk_prev_id.' '.$line->total_ht.' '.$line->situation_percent.'<br>';
385			}
386		}
387
388		$line->fk_facture = $creditnote->id;
389		$line->fk_parent_line = $fk_parent_line;
390
391		$line->subprice = -$line->subprice; // invert price for object
392		$line->pa_ht = $line->pa_ht; // we choosed to have buy/cost price always positive, so no revert of sign here
393		$line->total_ht = -$line->total_ht;
394		$line->total_tva = -$line->total_tva;
395		$line->total_ttc = -$line->total_ttc;
396		$line->total_localtax1 = -$line->total_localtax1;
397		$line->total_localtax2 = -$line->total_localtax2;
398
399		$line->multicurrency_subprice = -$line->multicurrency_subprice;
400		$line->multicurrency_total_ht = -$line->multicurrency_total_ht;
401		$line->multicurrency_total_tva = -$line->multicurrency_total_tva;
402		$line->multicurrency_total_ttc = -$line->multicurrency_total_ttc;
403
404		$result = $line->insert(0, 1); // When creating credit note with same lines than source, we must ignore error if discount alreayd linked
405
406		$creditnote->lines[] = $line; // insert new line in current object
407
408		// Defined the new fk_parent_line
409		if ($result > 0 && $line->product_type == 9) {
410			$fk_parent_line = $result;
411		}
412	}
413	$creditnote->update_price(1);
414
415	$constantforkey = 'CASHDESK_NO_DECREASE_STOCK'.$_SESSION["takeposterminal"];
416	if (!empty($conf->stock->enabled) && $conf->global->$constantforkey != "1") {
417		$savconst = $conf->global->STOCK_CALCULATE_ON_BILL;
418		$conf->global->STOCK_CALCULATE_ON_BILL = 1;
419		$constantforkey = 'CASHDESK_ID_WAREHOUSE'.$_SESSION["takeposterminal"];
420		dol_syslog("Validate invoice with stock change into warehouse defined into constant ".$constantforkey." = ".$conf->global->$constantforkey);
421		$batch_rule = 0;
422		if (!empty($conf->productbatch->enabled) && !empty($conf->global->CASHDESK_FORCE_DECREASE_STOCK)) {
423			require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
424			$batch_rule = Productbatch::BATCH_RULE_SELLBY_EATBY_DATES_FIRST;
425		}
426		$res = $creditnote->validate($user, '', $conf->global->$constantforkey, 0, $batch_rule);
427		$conf->global->STOCK_CALCULATE_ON_BILL = $savconst;
428	} else {
429		$res = $creditnote->validate($user);
430	}
431}
432
433if ($action == 'history' || $action == 'creditnote')
434{
435	if ($action == 'creditnote') $placeid = $creditnote->id;
436	else $placeid = (int) GETPOST('placeid', 'int');
437	$invoice = new Facture($db);
438	$invoice->fetch($placeid);
439}
440
441if (!empty($conf->multicurrency->enabled) && $_SESSION["takeposcustomercurrency"] != "") {
442	$invoice->setMulticurrencyCode($_SESSION["takeposcustomercurrency"]);
443}
444
445if (($action == "addline" || $action == "freezone") && $placeid == 0)
446{
447	$invoice->socid = $conf->global->$constforcompanyid;
448	$invoice->date = dol_now();
449	$invoice->module_source = 'takepos';
450	$invoice->pos_source = $_SESSION["takeposterminal"];
451	$invoice->entity = !empty($_SESSION["takeposinvoiceentity"]) ? $_SESSION["takeposinvoiceentity"] : $conf->entity;
452
453	if ($invoice->socid <= 0)
454	{
455		$langs->load('errors');
456		dol_htmloutput_errors($langs->trans("ErrorModuleSetupNotComplete", "TakePos"), null, 1);
457	} else {
458		$placeid = $invoice->create($user);
459		if ($placeid < 0)
460		{
461			dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
462		}
463		$sql = "UPDATE ".MAIN_DB_PREFIX."facture set ref='(PROV-POS".$_SESSION["takeposterminal"]."-".$place.")' where rowid=".$placeid;
464		$db->query($sql);
465	}
466}
467
468if ($action == "addline")
469{
470	$prod = new Product($db);
471	$prod->fetch($idproduct);
472
473	$customer = new Societe($db);
474	$customer->fetch($invoice->socid);
475
476	$datapriceofproduct = $prod->getSellPrice($mysoc, $customer, 0);
477
478	$price = $datapriceofproduct['pu_ht'];
479	$price_ttc = $datapriceofproduct['pu_ttc'];
480	//$price_min = $datapriceofproduct['price_min'];
481	$price_base_type = $datapriceofproduct['price_base_type'];
482	$tva_tx = $datapriceofproduct['tva_tx'];
483	$tva_npr = $datapriceofproduct['tva_npr'];
484
485	// Local Taxes
486	$localtax1_tx = get_localtax($tva_tx, 1, $customer, $mysoc, $tva_npr);
487	$localtax2_tx = get_localtax($tva_tx, 2, $customer, $mysoc, $tva_npr);
488
489	if (!empty($conf->global->TAKEPOS_SUPPLEMENTS))
490	{
491		require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
492		$cat = new Categorie($db);
493		$categories = $cat->containing($idproduct, 'product');
494		$found = (array_search($conf->global->TAKEPOS_SUPPLEMENTS_CATEGORY, array_column($categories, 'id')));
495		if ($found !== false) // If this product is a supplement
496		{
497			$sql = "SELECT fk_parent_line FROM ".MAIN_DB_PREFIX."facturedet where rowid=$selectedline";
498			$resql = $db->query($sql);
499			$row = $db->fetch_array($resql);
500			if ($row[0] == null) $parent_line = $selectedline;
501			else $parent_line = $row[0]; //If the parent line is already a supplement, add the supplement to the main  product
502		}
503	}
504
505	$idoflineadded = 0;
506	if (!empty($conf->global->TAKEPOS_GROUP_SAME_PRODUCT)) {
507		foreach ($invoice->lines as $line) {
508			if ($line->product_ref == $prod->ref) {
509				$result = $invoice->updateline($line->id, $line->desc, $line->subprice, $line->qty + 1, $line->remise_percent, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit);
510				if ($result < 0) {
511					dol_htmloutput_errors($invoice->error, $invoice->errors, 1);
512				} else {
513					$idoflineadded = $line->id;
514				}
515				break;
516			}
517		}
518	}
519	if ($idoflineadded <= 0) {
520		$invoice->fetch_thirdparty();
521		$idoflineadded = $invoice->addline($prod->description, $price, 1, $tva_tx, $localtax1_tx, $localtax2_tx, $idproduct, $customer->remise_percent, '', 0, 0, 0, '', $price_base_type, $price_ttc, $prod->type, -1, 0, '', 0, $parent_line, null, '', '', 0, 100, '', null, 0);
522	}
523
524	$invoice->fetch($placeid);
525}
526
527if ($action == "freezone") {
528	$customer = new Societe($db);
529	$customer->fetch($invoice->socid);
530
531	$tva_tx = GETPOST('tva_tx', 'alpha');
532	if ($tva_tx != '') {
533		if (!preg_match('/\((.*)\)/', $tva_tx)) {
534			$tva_tx = price2num($tva_tx);
535		}
536	} else {
537		$tva_tx = get_default_tva($mysoc, $customer);
538	}
539
540	// Local Taxes
541	$localtax1_tx = get_localtax($tva_tx, 1, $customer, $mysoc, $tva_npr);
542	$localtax2_tx = get_localtax($tva_tx, 2, $customer, $mysoc, $tva_npr);
543
544	$invoice->addline($desc, $number, 1, $tva_tx, $localtax1_tx, $localtax2_tx, 0, 0, '', 0, 0, 0, '', 'TTC', $number, 0, -1, 0, '', 0, 0, null, '', '', 0, 100, '', null, 0);
545	$invoice->fetch($placeid);
546}
547
548if ($action == "addnote") {
549	foreach ($invoice->lines as $line)
550	{
551		if ($line->id == $number)
552		{
553			$line->array_options['order_notes'] = $desc;
554			$result = $invoice->updateline($line->id, $line->desc, $line->subprice, $line->qty, $line->remise_percent, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit);
555		}
556	}
557	$invoice->fetch($placeid);
558}
559
560if ($action == "deleteline") {
561	if ($idline > 0 and $placeid > 0) { // If invoice exists and line selected. To avoid errors if deleted from another device or no line selected.
562		$invoice->deleteline($idline);
563		$invoice->fetch($placeid);
564	} elseif ($placeid > 0) {             // If invoice exists but no line selected, proceed to delete last line.
565		$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facturedet where fk_facture = ".((int) $placeid)." ORDER BY rowid DESC";
566		$resql = $db->query($sql);
567		$row = $db->fetch_array($resql);
568		$deletelineid = $row[0];
569		$invoice->deleteline($deletelineid);
570		$invoice->fetch($placeid);
571	}
572	if (count($invoice->lines) == 0) {
573		$invoice->delete($user);
574		header("Location: ".DOL_URL_ROOT."/takepos/invoice.php");
575		exit;
576	}
577}
578
579// Action to delete or discard an invoice
580if ($action == "delete") {
581	// $placeid is the invoice id (it differs from place) and is defined if the place is set and the ref of invoice is '(PROV-POS'.$_SESSION["takeposterminal"].'-'.$place.')', so the fetch at begining of page works.
582	if ($placeid > 0) {
583		$result = $invoice->fetch($placeid);
584
585		if ($result > 0 && $invoice->statut == Facture::STATUS_DRAFT)
586		{
587			$db->begin();
588
589			// We delete the lines
590			$resdeletelines = 1;
591			foreach ($invoice->lines as $line) {
592				$tmpres = $invoice->deleteline($line->id);
593				if ($tmpres < 0) {
594					$resdeletelines = 0;
595					break;
596				}
597			}
598
599			$sql = "UPDATE ".MAIN_DB_PREFIX."facture";
600			$sql .= " SET fk_soc=".$conf->global->{'CASHDESK_ID_THIRDPARTY'.$_SESSION["takeposterminal"]}.", ";
601			$sql .= " datec = '".$db->idate(dol_now())."'";
602			$sql .= " WHERE ref='(PROV-POS".$db->escape($_SESSION["takeposterminal"]."-".$place).")'";
603			$resql1 = $db->query($sql);
604
605			if ($resdeletelines && $resql1) {
606            	$db->commit();
607            } else {
608            	$db->rollback();
609            }
610
611			$invoice->fetch($placeid);
612		}
613	}
614}
615
616if ($action == "updateqty")
617{
618	foreach ($invoice->lines as $line)
619	{
620		if ($line->id == $idline)
621		{
622			$result = $invoice->updateline($line->id, $line->desc, $line->subprice, $number, $line->remise_percent, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit);
623		}
624	}
625
626	$invoice->fetch($placeid);
627}
628
629if ($action == "updateprice")
630{
631	foreach ($invoice->lines as $line)
632	{
633		if ($line->id == $idline)
634		{
635			$prod = new Product($db);
636			$prod->fetch($line->fk_product);
637			$customer = new Societe($db);
638			$customer->fetch($invoice->socid);
639			$datapriceofproduct = $prod->getSellPrice($mysoc, $customer, 0);
640			$price_min = $datapriceofproduct['price_min'];
641			$usercanproductignorepricemin = ((!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->produit->ignore_price_min_advance)) || empty($conf->global->MAIN_USE_ADVANCED_PERMS));
642			$pu_ht = price2num($number / (1 + ($line->tva_tx / 100)), 'MU');
643			//Check min price
644			if ($usercanproductignorepricemin && (!empty($price_min) && (price2num($pu_ht) * (1 - price2num($line->remise_percent) / 100) < price2num($price_min))))
645			{
646				echo $langs->trans("CantBeLessThanMinPrice");
647			} else $result = $invoice->updateline($line->id, $line->desc, $number, $line->qty, $line->remise_percent, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'TTC', $line->info_bits, $line->product_type, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit);
648		}
649	}
650	$invoice->fetch($placeid);
651}
652
653if ($action == "updatereduction")
654{
655	foreach ($invoice->lines as $line)
656	{
657		if ($line->id == $idline)
658		{
659			$prod = new Product($db);
660			$prod->fetch($line->fk_product);
661			$customer = new Societe($db);
662			$customer->fetch($invoice->socid);
663			$datapriceofproduct = $prod->getSellPrice($mysoc, $customer, 0);
664			$price_min = $datapriceofproduct['price_min'];
665			$usercanproductignorepricemin = ((!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->produit->ignore_price_min_advance)) || empty($conf->global->MAIN_USE_ADVANCED_PERMS));
666			$pu_ht = price2num($line->multicurrency_subprice / (1 + ($line->tva_tx / 100)), 'MU');
667			//Check min price
668			if ($usercanproductignorepricemin && (!empty($price_min) && (price2num($line->multicurrency_subprice) * (1 - price2num($number) / 100) < price2num($price_min))))
669			{
670				echo $langs->trans("CantBeLessThanMinPrice");
671			} else $result = $invoice->updateline($line->id, $line->desc, $line->multicurrency_subprice, $line->qty, $number, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit);
672		}
673	}
674	$invoice->fetch($placeid);
675} elseif ($action == 'update_reduction_global') {
676	foreach ($invoice->lines as $line) {
677		$result = $invoice->updateline($line->id, $line->desc, $line->subprice, $line->qty, $number, $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent, $line->fk_unit);
678	}
679
680	$invoice->fetch($placeid);
681}
682
683if ($action == "order" and $placeid != 0)
684{
685	include_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
686	if ($conf->global->TAKEPOS_PRINT_METHOD == "receiptprinter" || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
687		require_once DOL_DOCUMENT_ROOT.'/core/class/dolreceiptprinter.class.php';
688		$printer = new dolReceiptPrinter($db);
689	}
690
691	$sql = "SELECT label FROM ".MAIN_DB_PREFIX."takepos_floor_tables where rowid=".((int) $place);
692	$resql = $db->query($sql);
693	$row = $db->fetch_object($resql);
694	$headerorder = '<html><br><b>'.$langs->trans('Place').' '.$row->label.'<br><table width="65%"><thead><tr><th class="left">'.$langs->trans("Label").'</th><th class="right">'.$langs->trans("Qty").'</th></tr></thead><tbody>';
695	$footerorder = '</tbody></table>'.dol_print_date(dol_now(), 'dayhour').'<br></html>';
696	$order_receipt_printer1 = "";
697	$order_receipt_printer2 = "";
698	$order_receipt_printer3 = "";
699	$catsprinter1 = explode(';', $conf->global->TAKEPOS_PRINTED_CATEGORIES_1);
700	$catsprinter2 = explode(';', $conf->global->TAKEPOS_PRINTED_CATEGORIES_2);
701	$catsprinter3 = explode(';', $conf->global->TAKEPOS_PRINTED_CATEGORIES_3);
702	foreach ($invoice->lines as $line)
703	{
704		if ($line->special_code == "4") {
705			continue;
706		}
707		$c = new Categorie($db);
708		$existing = $c->containing($line->fk_product, Categorie::TYPE_PRODUCT, 'id');
709		$result = array_intersect($catsprinter1, $existing);
710		$count = count($result);
711		if (!$line->fk_product) $count++; // Print Free-text item (Unassigned printer) to Printer 1
712		if ($count > 0) {
713			$linestoprint++;
714			$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='1' where rowid=".$line->id; //Set to print on printer 1
715			$db->query($sql);
716			$order_receipt_printer1 .= '<tr><td class="left">';
717			if ($line->fk_product) $order_receipt_printer1 .= $line->product_label;
718			else $order_receipt_printer1 .= $line->description;
719			$order_receipt_printer1 .= '</td><td class="right">'.$line->qty;
720			if (!empty($line->array_options['options_order_notes'])) $order_receipt_printer1 .= "<br>(".$line->array_options['options_order_notes'].")";
721			$order_receipt_printer1 .= '</td></tr>';
722		}
723	}
724	if (($conf->global->TAKEPOS_PRINT_METHOD == "receiptprinter" || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") && $linestoprint > 0) {
725		$invoice->fetch($placeid); //Reload object before send to printer
726		$printer->orderprinter = 1;
727		echo "<script>";
728		echo "var orderprinter1esc='";
729		$ret = $printer->sendToPrinter($invoice, $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_ORDERS'.$_SESSION["takeposterminal"]}, $conf->global->{'TAKEPOS_ORDER_PRINTER1_TO_USE'.$_SESSION["takeposterminal"]}); // PRINT TO PRINTER 1
730		echo "';</script>";
731	}
732	$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='4' where special_code='1' and fk_facture=".$invoice->id; // Set as printed
733	$db->query($sql);
734	$invoice->fetch($placeid); //Reload object after set lines as printed
735	$linestoprint = 0;
736
737	foreach ($invoice->lines as $line)
738	{
739		if ($line->special_code == "4") {
740			continue;
741		}
742		$c = new Categorie($db);
743		$existing = $c->containing($line->fk_product, Categorie::TYPE_PRODUCT, 'id');
744		$result = array_intersect($catsprinter2, $existing);
745		$count = count($result);
746		if ($count > 0) {
747			$linestoprint++;
748			$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='2' where rowid=".$line->id; //Set to print on printer 2
749			$db->query($sql);
750			$order_receipt_printer2 .= '<tr>'.$line->product_label.'<td class="right">'.$line->qty;
751			if (!empty($line->array_options['options_order_notes'])) $order_receipt_printer2 .= "<br>(".$line->array_options['options_order_notes'].")";
752			$order_receipt_printer2 .= '</td></tr>';
753		}
754	}
755	if (($conf->global->TAKEPOS_PRINT_METHOD == "receiptprinter" || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") && $linestoprint > 0) {
756		$invoice->fetch($placeid); //Reload object before send to printer
757		$printer->orderprinter = 2;
758		echo "<script>";
759		echo "var orderprinter2esc='";
760		$ret = $printer->sendToPrinter($invoice, $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_ORDERS'.$_SESSION["takeposterminal"]}, $conf->global->{'TAKEPOS_ORDER_PRINTER2_TO_USE'.$_SESSION["takeposterminal"]}); // PRINT TO PRINTER 2
761		echo "';</script>";
762	}
763	$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='4' where special_code='2' and fk_facture=".$invoice->id; // Set as printed
764	$db->query($sql);
765	$invoice->fetch($placeid); //Reload object after set lines as printed
766	$linestoprint = 0;
767
768	foreach ($invoice->lines as $line)
769	{
770		if ($line->special_code == "4") {
771			continue;
772		}
773		$c = new Categorie($db);
774		$existing = $c->containing($line->fk_product, Categorie::TYPE_PRODUCT, 'id');
775		$result = array_intersect($catsprinter3, $existing);
776		$count = count($result);
777		if ($count > 0) {
778			$linestoprint++;
779			$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='3' where rowid=".$line->id; //Set to print on printer 3
780			$db->query($sql);
781			$order_receipt_printer3 .= '<tr>'.$line->product_label.'<td class="right">'.$line->qty;
782			if (!empty($line->array_options['options_order_notes'])) $order_receipt_printer3 .= "<br>(".$line->array_options['options_order_notes'].")";
783			$order_receipt_printer3 .= '</td></tr>';
784		}
785	}
786	if (($conf->global->TAKEPOS_PRINT_METHOD == "receiptprinter" || $conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") && $linestoprint > 0) {
787		$invoice->fetch($placeid); //Reload object before send to printer
788		$printer->orderprinter = 3;
789		echo "<script>";
790		echo "var orderprinter3esc='";
791		$ret = $printer->sendToPrinter($invoice, $conf->global->{'TAKEPOS_TEMPLATE_TO_USE_FOR_ORDERS'.$_SESSION["takeposterminal"]}, $conf->global->{'TAKEPOS_ORDER_PRINTER3_TO_USE'.$_SESSION["takeposterminal"]}); // PRINT TO PRINTER 3
792		echo "';</script>";
793	}
794	$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='4' where special_code='3' and fk_facture=".$invoice->id; // Set as printed
795	$db->query($sql);
796	$invoice->fetch($placeid); //Reload object after set lines as printed
797}
798
799$sectionwithinvoicelink = '';
800if ($action == "valid" || $action == "history" || $action == 'creditnote')
801{
802	$sectionwithinvoicelink .= '<!-- Section with invoice link -->'."\n";
803	$sectionwithinvoicelink .= '<span style="font-size:120%;" class="center">';
804	$sectionwithinvoicelink .= $invoice->getNomUrl(1, '', 0, 0, '', 0, 0, -1, '_backoffice')." - ";
805	$remaintopay = $invoice->getRemainToPay();
806	if ($remaintopay > 0)
807	{
808		$sectionwithinvoicelink .= $langs->trans('RemainToPay').': <span class="amountremaintopay" style="font-size: unset">'.price($remaintopay, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
809	} else {
810		if ($invoice->paye) $sectionwithinvoicelink .= '<span class="amountpaymentcomplete" style="font-size: unset">'.$langs->trans("Paid").'</span>';
811		else $sectionwithinvoicelink .= $langs->trans('BillShortStatusValidated');
812	}
813	$sectionwithinvoicelink .= '</span><br>';
814	if ($conf->global->TAKEPOS_PRINT_METHOD == "takeposconnector") {
815		if (filter_var($conf->global->TAKEPOS_PRINT_SERVER, FILTER_VALIDATE_URL) == true) $sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="TakeposConnector('.$placeid.');">'.$langs->trans('PrintTicket').'</button>';
816		else $sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="TakeposPrinting('.$placeid.');">'.$langs->trans('PrintTicket').'</button>';
817	} elseif ($conf->global->TAKEPOS_PRINT_METHOD == "receiptprinter") {
818		$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="DolibarrTakeposPrinting('.$placeid.');">'.$langs->trans('PrintTicket').'</button>';
819	} else {
820		$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="Print('.$placeid.');">'.$langs->trans('PrintTicket').'</button>';
821		if ($conf->global->TAKEPOS_GIFT_RECEIPT) {
822			$sectionwithinvoicelink .= ' <button id="buttonprint" type="button" onclick="Print('.$placeid.', 1);">'.$langs->trans('GiftReceipt').'</button>';
823		}
824	}
825	if ($conf->global->TAKEPOS_EMAIL_TEMPLATE_INVOICE > 0)
826	{
827		$sectionwithinvoicelink .= ' <button id="buttonsend" type="button" onclick="SendTicket('.$placeid.');">'.$langs->trans('SendTicket').'</button>';
828	}
829
830	if ($remaintopay <= 0 && $conf->global->TAKEPOS_AUTO_PRINT_TICKETS) $sectionwithinvoicelink .= '<script language="javascript">$("#buttonprint").click();</script>';
831}
832
833/*
834 * View
835 */
836
837$form = new Form($db);
838
839?>
840<script language="javascript">
841var selectedline=0;
842var selectedtext="";
843var placeid=<?php echo ($placeid > 0 ? $placeid : 0); ?>;
844$(document).ready(function() {
845	var idoflineadded = <?php echo ($idoflineadded ? $idoflineadded : 0); ?>;
846
847    $('.posinvoiceline').click(function(){
848    	console.log("Click done on "+this.id);
849        $('.posinvoiceline').removeClass("selected");
850        $(this).addClass("selected");
851        if (selectedline==this.id) return; // If is already selected
852        else selectedline=this.id;
853        selectedtext=$('#'+selectedline).find("td:first").html();
854		<?php
855		if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
856			print '$("#phonediv1").load("auto_order.php?action=editline&placeid="+placeid+"&selectedline="+selectedline, function() {
857			});';
858		}
859		?>
860    });
861
862    /* Autoselect the line */
863    if (idoflineadded > 0)
864    {
865        console.log("Auto select "+idoflineadded);
866        $('.posinvoiceline#'+idoflineadded).click();
867    }
868<?php
869
870if ($action == "order" and $order_receipt_printer1 != "") {
871	if (filter_var($conf->global->TAKEPOS_PRINT_SERVER, FILTER_VALIDATE_URL) == true) {
872		?>
873		$.ajax({
874			type: "POST",
875			url: '<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>/printer/index.php',
876			data: 'invoice='+orderprinter1esc
877		});
878		<?php
879	}
880	else {
881		?>
882		$.ajax({
883			type: "POST",
884			url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print',
885			data: '<?php
886			print $headerorder.$order_receipt_printer1.$footerorder; ?>'
887		});
888		<?php
889	}
890}
891
892if ($action == "order" and $order_receipt_printer2 != "") {
893	if (filter_var($conf->global->TAKEPOS_PRINT_SERVER, FILTER_VALIDATE_URL) == true) {
894		?>
895		$.ajax({
896			type: "POST",
897			url: '<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>/printer/index.php?printer=2',
898			data: 'invoice='+orderprinter2esc
899		});
900		<?php
901	}
902	else {
903		?>
904		$.ajax({
905			type: "POST",
906			url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print2',
907			data: '<?php
908			print $headerorder.$order_receipt_printer2.$footerorder; ?>'
909		});
910    	<?php
911	}
912}
913
914if ($action == "order" and $order_receipt_printer3 != "") {
915	if (filter_var($conf->global->TAKEPOS_PRINT_SERVER, FILTER_VALIDATE_URL) == true) {
916		?>
917		$.ajax({
918			type: "POST",
919			url: '<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>/printer/index.php?printer=3',
920			data: 'invoice='+orderprinter3esc
921		});
922		<?php
923	}
924}
925
926// Set focus to search field
927if ($action == "search" || $action == "valid") {
928	?>
929	parent.setFocusOnSearchField();
930    <?php
931}
932
933
934if ($action == "temp" and $ticket_printer1 != "") {
935	?>
936    $.ajax({
937        type: "POST",
938        url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print',
939        data: '<?php
940		print $header_soc.$header_ticket.$body_ticket.$ticket_printer1.$ticket_total.$footer_ticket; ?>'
941    });
942    <?php
943}
944
945if ($action == "search") {
946	?>
947    $('#search').focus();
948    <?php
949}
950
951?>
952
953});
954
955function SendTicket(id)
956{
957    console.log("Open box to select the Print/Send form");
958    $.colorbox({href:"send.php?facid="+id, width:"70%", height:"30%", transition:"none", iframe:"true", title:"<?php echo $langs->trans("SendTicket"); ?>"});
959}
960
961function Print(id, gift){
962    $.colorbox({href:"receipt.php?facid="+id+"&gift="+gift, width:"40%", height:"90%", transition:"none", iframe:"true", title:"<?php
963	echo $langs->trans("PrintTicket"); ?>"});
964}
965
966function TakeposPrinting(id){
967    var receipt;
968	console.log("TakeposPrinting" + id);
969    $.get("receipt.php?facid="+id, function(data, status){
970        receipt=data.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '');
971        $.ajax({
972            type: "POST",
973            url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print',
974            data: receipt
975        });
976    });
977}
978
979function TakeposConnector(id){
980	console.log("TakeposConnector" + id);
981	$.get("ajax/ajax.php?action=printinvoiceticket&term=<?php echo $_SESSION["takeposterminal"]; ?>&id="+id, function(data, status){
982        $.ajax({
983			type: "POST",
984			url: '<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>/printer/index.php',
985			data: 'invoice='+data
986		});
987    });
988}
989
990function DolibarrTakeposPrinting(id) {
991    console.log("DolibarrTakeposPrinting Printing invoice ticket " + id)
992    $.ajax({
993        type: "GET",
994        url: "<?php print dol_buildpath('/takepos/ajax/ajax.php', 1).'?action=printinvoiceticket&term='.$_SESSION["takeposterminal"].'&id='; ?>" + id,
995    });
996}
997
998function CreditNote() {
999	$("#poslines").load("invoice.php?action=creditnote&invoiceid="+placeid, function() {
1000	});
1001}
1002
1003
1004$( document ).ready(function() {
1005	console.log("Set customer info and sales in header placeid=<?php echo $placeid; ?> status=<?php echo $invoice->statut; ?>");
1006
1007    <?php
1008	$s = $langs->trans("Customer");
1009	if ($invoice->id > 0 && ($invoice->socid != $conf->global->$constforcompanyid)) {
1010		$s = $soc->name;
1011	}
1012	?>
1013
1014    $("#customerandsales").html('');
1015
1016	$("#customerandsales").append('<a class="valignmiddle tdoverflowmax100 minwidth100" id="customer" onclick="Customer();" title="<?php print dol_escape_js($s); ?>"><span class="fas fa-building paddingrightonly"></span><?php print dol_escape_js($s); ?></a>');
1017
1018	<?php
1019	$sql = "SELECT rowid, datec, ref FROM ".MAIN_DB_PREFIX."facture";
1020	if (empty($conf->global->TAKEPOS_CAN_EDIT_IF_ALREADY_VALIDATED)) {
1021		// By default, only invoices with a ref not already defined can in list of open invoice we can edit.
1022		$sql .= " WHERE ref LIKE '(PROV-POS".$db->escape($_SESSION["takeposterminal"])."-0%' AND entity IN (".getEntity('invoice').")";
1023	} else {
1024		// If TAKEPOS_CAN_EDIT_IF_ALREADY_VALIDATED set, we show also draft invoice that already has a reference defined
1025		$sql .= " WHERE pos_source = '".$db->escape($_SESSION["takeposterminal"])."'";
1026		$sql .= " AND module_source = 'takepos'";
1027		$sql .= " AND entity IN (".getEntity('invoice').")";
1028	}
1029
1030	$sql .= $db->order('datec', 'ASC');
1031	$resql = $db->query($sql);
1032	if ($resql) {
1033		while ($obj = $db->fetch_object($resql)) {
1034			echo '$("#customerandsales").append(\'';
1035			echo '<a class="valignmiddle" title="'.dol_escape_js($langs->trans("SaleStartedAt", dol_print_date($db->jdate($obj->datec), '%H:%M', 'tzuser'))).'" onclick="place=\\\'';
1036			$num_sale = str_replace(")", "", str_replace("(PROV-POS".$_SESSION["takeposterminal"]."-", "", $obj->ref));
1037			echo $num_sale;
1038			if (str_replace("-", "", $num_sale) > $max_sale) $max_sale = str_replace("-", "", $num_sale);
1039			echo '\\\'; invoiceid=\\\'';
1040			echo $obj->rowid;
1041			echo '\\\'; Refresh();">';
1042			if ($placeid == $obj->rowid) echo "<b>";
1043			echo dol_print_date($db->jdate($obj->datec), '%H:%M', 'tzuser');
1044			if ($placeid == $obj->rowid) echo "</b>";
1045			echo '</a>\');';
1046		}
1047		echo '$("#customerandsales").append(\'<a onclick="place=\\\'0-';
1048		echo $max_sale + 1;
1049		echo '\\\'; invoiceid=0; Refresh();"><span class="fa fa-plus-square" title="'.dol_escape_htmltag($langs->trans("StartAParallelSale")).'"></a>\');';
1050	} else {
1051		dol_print_error($db);
1052	}
1053
1054	$s = '';
1055
1056	$constantforkey = 'CASHDESK_NO_DECREASE_STOCK'.$_SESSION["takeposterminal"];
1057	if (!empty($conf->stock->enabled) && $conf->global->$constantforkey != "1")
1058	{
1059		$s = '<span class="small">';
1060		$constantforkey = 'CASHDESK_ID_WAREHOUSE'.$_SESSION["takeposterminal"];
1061		$warehouse = new Entrepot($db);
1062		$warehouse->fetch($conf->global->$constantforkey);
1063		$s .= $langs->trans("Warehouse").'<br>'.$warehouse->ref;
1064		$s .= '</span>';
1065	}
1066	?>
1067
1068    $("#infowarehouse").html('<?php print dol_escape_js($s); ?>');
1069
1070	<?php
1071	// Module Adherent
1072	$s = '';
1073	if (!empty($conf->adherent->enabled) && $invoice->socid > 0 && $invoice->socid != $conf->global->$constforcompanyid)
1074	{
1075		$s = '<span class="small">';
1076		require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
1077		$langs->load("members");
1078		$s .= $langs->trans("Member").': ';
1079		$adh = new Adherent($db);
1080		$result = $adh->fetch('', '', $invoice->socid);
1081		if ($result > 0)
1082		{
1083			$adh->ref = $adh->getFullName($langs);
1084			if (empty($adh->statut)) { $s .= "<s>"; }
1085			$s .= $adh->getFullName($langs);
1086			$s .= ' - '.$adh->type;
1087			if ($adh->datefin)
1088			{
1089				$s .= '<br>'.$langs->trans("SubscriptionEndDate").': '.dol_print_date($adh->datefin, 'day');
1090				if ($adh->hasDelay()) {
1091					$s .= " ".img_warning($langs->trans("Late"));
1092				}
1093			} else {
1094				$s .= '<br>'.$langs->trans("SubscriptionNotReceived");
1095				if ($adh->statut > 0) $s .= " ".img_warning($langs->trans("Late")); // displays delay Pictogram only if not a draft and not terminated
1096			}
1097	  		if (empty($adh->statut)) { $s .= "</s>"; }
1098		} else {
1099			$s .= '<br>'.$langs->trans("ThirdpartyNotLinkedToMember");
1100		}
1101		$s .= '</span>';
1102	}
1103	?>
1104	$("#moreinfo").html('<?php print dol_escape_js($s); ?>');
1105
1106});
1107
1108</script>
1109
1110<?php
1111// Add again js for footer because this content is injected into index.php page so all init
1112// for tooltip and other js beautifiers must be reexecuted too.
1113if (!empty($conf->use_javascript_ajax))
1114{
1115	print "\n".'<!-- Includes JS Footer of Dolibarr -->'."\n";
1116	print '<script src="'.DOL_URL_ROOT.'/core/js/lib_foot.js.php?lang='.$langs->defaultlang.($ext ? '&'.$ext : '').'"></script>'."\n";
1117}
1118
1119print '<!-- invoice.php place='.(int) $place.' invoice='.$invoice->ref.' mobilepage='.$mobilepage.' $_SESSION["basiclayout"]='.$_SESSION["basiclayout"].' conf->global->TAKEPOS_BAR_RESTAURANT='.$conf->global->TAKEPOS_BAR_RESTAURANT.' -->'."\n";
1120print '<div class="div-table-responsive-no-min invoice">';
1121print '<table id="tablelines" class="noborder noshadow postablelines" width="100%">';
1122if ($sectionwithinvoicelink && ($mobilepage == "invoice" || $mobilepage == "")) {
1123	print '<tr><td colspan="4">'.$sectionwithinvoicelink.'</td></tr>';
1124}
1125print '<tr class="liste_titre nodrag nodrop">';
1126print '<td class="linecoldescription">';
1127// In phone version only show when it is invoice page
1128if ($mobilepage == "invoice" || $mobilepage == "") {
1129	print '<input type="hidden" name="invoiceid" id="invoiceid" value="'.$invoice->id.'">';
1130}
1131if ($conf->global->TAKEPOS_BAR_RESTAURANT)
1132{
1133	$sql = "SELECT floor, label FROM ".MAIN_DB_PREFIX."takepos_floor_tables where rowid=".((int) $place);
1134	$resql = $db->query($sql);
1135	$obj = $db->fetch_object($resql);
1136	if ($obj)
1137	{
1138		$label = $obj->label;
1139		$floor = $obj->floor;
1140	}
1141	// In phone version only show when is invoice page
1142	if ($mobilepage == "invoice" || $mobilepage == "") {
1143		print '<span class="opacitymedium">'.$langs->trans('Place')."</span> <b>".$label."</b><br>";
1144		print '<span class="opacitymedium">'.$langs->trans('Floor')."</span> <b>".$floor."</b>";
1145	}
1146	elseif (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) print $mysoc->name;
1147	elseif ($mobilepage == "cats") print $langs->trans('Category');
1148	elseif ($mobilepage == "products") print $langs->trans('Label');
1149} else {
1150	print $langs->trans("Products");
1151}
1152print '</td>';
1153if ($_SESSION["basiclayout"] != 1)
1154{
1155	print '<td class="linecolqty right">'.$langs->trans('ReductionShort').'</td>';
1156	print '<td class="linecolqty right">'.$langs->trans('Qty').'</td>';
1157	print '<td class="linecolht right nowraponall">';
1158	print '<span class="opacitymedium small">'.$langs->trans('TotalTTCShort').'</span><br>';
1159	// In phone version only show when it is invoice page
1160	if ($mobilepage == "invoice" || $mobilepage == "") {
1161		print '<span id="linecolht-span-total" style="font-size:1.3em; font-weight: bold;">'.price($invoice->total_ttc, 1, '', 1, -1, -1, $conf->currency).'</span>';
1162		if (!empty($conf->multicurrency->enabled) && $_SESSION["takeposcustomercurrency"] != "" && $conf->currency != $_SESSION["takeposcustomercurrency"]) {
1163			//Only show customer currency if multicurrency module is enabled, if currency selected and if this currency selected is not the same as main currency
1164			include_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
1165			$multicurrency = new MultiCurrency($db);
1166			$multicurrency->fetch(0, $_SESSION["takeposcustomercurrency"]);
1167			print '<br><span id="linecolht-span-total" style="font-size:0.9em; font-style:italic;">('.price($invoice->total_ttc * $multicurrency->rate->rate).' '.$_SESSION["takeposcustomercurrency"].')</span>';
1168		}
1169		print '</td>';
1170	}
1171	print '</td>';
1172}
1173elseif ($mobilepage == "invoice") print '<td class="linecolqty right">'.$langs->trans('Qty').'</td>';
1174print "</tr>\n";
1175
1176
1177if ($_SESSION["basiclayout"] == 1)
1178{
1179	if ($mobilepage == "cats")
1180	{
1181		require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1182		$categorie = new Categorie($db);
1183		$categories = $categorie->get_full_arbo('product');
1184		$htmlforlines = '';
1185		foreach ($categories as $row) {
1186			if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) $htmlforlines .= '<div class="leftcat';
1187			else $htmlforlines .= '<tr class="drag drop oddeven posinvoiceline';
1188			$htmlforlines .= '" onclick="LoadProducts('.$row['id'].');">';
1189			if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) $htmlforlines .= '<img class="imgwrapper" width="33%" src="'.DOL_URL_ROOT.'/takepos/public/auto_order.php?genimg=cat&query=cat&id='.$row['id'].'"><br>';
1190			else $htmlforlines .= '<td class="left">';
1191			$htmlforlines .= $row['label'];
1192			if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) $htmlforlines .= '</div>'."\n";
1193			else $htmlforlines .= '</td></tr>'."\n";
1194		}
1195		$htmlforlines .= '</table>';
1196		$htmlforlines .= '</table>';
1197		print $htmlforlines;
1198	}
1199
1200	if ($mobilepage == "products")
1201	{
1202		require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1203		$object = new Categorie($db);
1204		$catid = GETPOST('catid', 'int');
1205		$result = $object->fetch($catid);
1206		$prods = $object->getObjectsInCateg("product");
1207		$htmlforlines = '';
1208		foreach ($prods as $row) {
1209			if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) $htmlforlines .= '<div class="leftcat';
1210			else $htmlforlines .= '<tr class="drag drop oddeven posinvoiceline';
1211			$htmlforlines .= '" onclick="AddProduct(\''.$place.'\', '.$row->id.')">';
1212			if (defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
1213				$htmlforlines .= '<img class="imgwrapper" width="33%" src="'.DOL_URL_ROOT.'/takepos/public/auto_order.php?genimg=pro&query=pro&id='.$row->id.'"><br>';
1214				$htmlforlines .= $row->label.''.price($row->price_ttc, 1, $langs, 1, -1, -1, $conf->currency);
1215				$htmlforlines .= '</div>'."\n";
1216			}
1217			else {
1218				$htmlforlines .= '<td class="left">';
1219				$htmlforlines .= $row->label;
1220				$htmlforlines .= '<div class="right">'.price($row->price_ttc, 1, $langs, 1, -1, -1, $conf->currency).'</div>';
1221				$htmlforlines .= '</tr>'."\n";
1222			}
1223		}
1224		$htmlforlines .= '</table>';
1225		print $htmlforlines;
1226	}
1227
1228	if ($mobilepage == "places")
1229	{
1230		$sql = "SELECT rowid, entity, label, leftpos, toppos, floor FROM ".MAIN_DB_PREFIX."takepos_floor_tables";
1231		$resql = $db->query($sql);
1232		$rows = array();
1233		$htmlforlines = '';
1234		while ($row = $db->fetch_array($resql)) {
1235			$rows[] = $row;
1236			$htmlforlines .= '<tr class="drag drop oddeven posinvoiceline';
1237			$htmlforlines .= '" onclick="LoadPlace(\''.$row['label'].'\')">';
1238			$htmlforlines .= '<td class="left">';
1239			$htmlforlines .= $row['label'];
1240			$htmlforlines .= '</td>';
1241			$htmlforlines .= '</tr>'."\n";
1242		}
1243		$htmlforlines .= '</table>';
1244		print $htmlforlines;
1245	}
1246}
1247
1248if ($placeid > 0)
1249{
1250	//In Phone basic layout hide some content depends situation
1251	if ($_SESSION["basiclayout"] == 1 && $mobilepage != "invoice" && $action != "order") return;
1252
1253	if (is_array($invoice->lines) && count($invoice->lines))
1254	{
1255		print '<!-- invoice.php show lines of invoices -->'."\n";
1256		$tmplines = array_reverse($invoice->lines);
1257		foreach ($tmplines as $line)
1258		{
1259			if ($line->fk_parent_line != false)
1260			{
1261				$htmlsupplements[$line->fk_parent_line] .= '<tr class="drag drop oddeven posinvoiceline';
1262				if ($line->special_code == "4") $htmlsupplements[$line->fk_parent_line] .= ' order';
1263				$htmlsupplements[$line->fk_parent_line] .= '" id="'.$line->id.'">';
1264				$htmlsupplements[$line->fk_parent_line] .= '<td class="left">';
1265				$htmlsupplements[$line->fk_parent_line] .= img_picto('', 'rightarrow');
1266				if ($line->product_label) $htmlsupplements[$line->fk_parent_line] .= $line->product_label;
1267				if ($line->product_label && $line->desc) $htmlsupplements[$line->fk_parent_line] .= '<br>';
1268				if ($line->product_label != $line->desc)
1269				{
1270					$firstline = dolGetFirstLineOfText($line->desc);
1271					if ($firstline != $line->desc)
1272					{
1273						$htmlsupplements[$line->fk_parent_line] .= $form->textwithpicto(dolGetFirstLineOfText($line->desc), $line->desc);
1274					} else {
1275						$htmlsupplements[$line->fk_parent_line] .= $line->desc;
1276					}
1277				}
1278				$htmlsupplements[$line->fk_parent_line] .= '</td>';
1279				if ($_SESSION["basiclayout"] != 1)
1280				{
1281					$htmlsupplements[$line->fk_parent_line] .= '<td class="right">'.vatrate($line->remise_percent, true).'</td>';
1282					$htmlsupplements[$line->fk_parent_line] .= '<td class="right">'.$line->qty.'</td>';
1283					$htmlsupplements[$line->fk_parent_line] .= '<td class="right">'.price($line->total_ttc).'</td>';
1284				}
1285				$htmlsupplements[$line->fk_parent_line] .= '</tr>'."\n";
1286				continue;
1287			}
1288			$htmlforlines = '';
1289
1290			$htmlforlines .= '<tr class="drag drop oddeven posinvoiceline';
1291			if ($line->special_code == "4") {
1292				$htmlforlines .= ' order';
1293			}
1294			$htmlforlines .= '" id="'.$line->id.'">';
1295			$htmlforlines .= '<td class="left">';
1296			if ($_SESSION["basiclayout"] == 1) $htmlforlines .= '<span class="phoneqty">'.$line->qty."</span> x ";
1297			if (isset($line->product_type))
1298			{
1299				if (empty($line->product_type)) $htmlforlines .= img_object('', 'product').' ';
1300				else $htmlforlines .= img_object('', 'service').' ';
1301			}
1302			if (empty($conf->global->TAKEPOS_SHOW_N_FIRST_LINES)) {
1303				$tooltiptext = '';
1304				if ($line->product_ref) {
1305					$tooltiptext .= '<b>'.$langs->trans("Ref").'</b> : '.$line->product_ref.'<br>';
1306					$tooltiptext .= '<b>'.$langs->trans("Label").'</b> : '.$line->product_label.'<br>';
1307					if ($line->product_label != $line->desc) {
1308						if ($line->desc) $tooltiptext .= '<br>';
1309						$tooltiptext .= $line->desc;
1310					}
1311				}
1312				$htmlforlines .= $form->textwithpicto($line->product_label ? $line->product_label : ($line->product_ref ? $line->product_ref : dolGetFirstLineOfText($line->desc, 1)), $tooltiptext);
1313			} else {
1314				if ($line->product_label) $htmlforlines .= $line->product_label;
1315				if ($line->product_label != $line->desc)
1316				{
1317					if ($line->product_label && $line->desc) $htmlforlines .= '<br>';
1318					$firstline = dolGetFirstLineOfText($line->desc, $conf->global->TAKEPOS_SHOW_N_FIRST_LINES);
1319					if ($firstline != $line->desc)
1320					{
1321						$htmlforlines .= $form->textwithpicto(dolGetFirstLineOfText($line->desc), $line->desc);
1322					} else {
1323						$htmlforlines .= $line->desc;
1324					}
1325				}
1326			}
1327			if (!empty($line->array_options['options_order_notes'])) $htmlforlines .= "<br>(".$line->array_options['options_order_notes'].")";
1328			if ($_SESSION["basiclayout"] == 1) {
1329				$htmlforlines .= '</td><td class="right phonetable"><button type="button" onclick="SetQty(place, '.$line->rowid.', '.($line->qty - 1).');" class="publicphonebutton2 phonered">-</button>&nbsp;&nbsp;<button type="button" onclick="SetQty(place, '.$line->rowid.', '.($line->qty + 1).');" class="publicphonebutton2 phonegreen">+</button>';
1330			}
1331			if ($_SESSION["basiclayout"] != 1)
1332			{
1333				$moreinfo = '';
1334				$moreinfo .= $langs->transcountry("TotalHT", $mysoc->country_code).': '.price($line->total_ht);
1335				if ($line->vat_src_code) $moreinfo .= '<br>'.$langs->trans("VATCode").': '.$line->vat_src_code;
1336				$moreinfo .= '<br>'.$langs->transcountry("TotalVAT", $mysoc->country_code).': '.price($line->total_tva);
1337				$moreinfo .= '<br>'.$langs->transcountry("TotalLT1", $mysoc->country_code).': '.price($line->total_localtax1);
1338				$moreinfo .= '<br>'.$langs->transcountry("TotalLT2", $mysoc->country_code).': '.price($line->total_localtax2);
1339				$moreinfo .= '<br>'.$langs->transcountry("TotalTTC", $mysoc->country_code).': '.price($line->total_ttc);
1340				//$moreinfo .= $langs->trans("TotalHT").': '.$line->total_ht;
1341				if ($line->date_start || $line->date_end) $htmlforlines .= '<br><div class="clearboth nowraponall">'.get_date_range($line->date_start, $line->date_end).'</div>';
1342				$htmlforlines .= '</td>';
1343				$htmlforlines .= '<td class="right">'.vatrate($line->remise_percent, true).'</td>';
1344				$htmlforlines .= '<td class="right">';
1345				if (!empty($conf->stock->enabled) && !empty($user->rights->stock->mouvement->lire))
1346				{
1347					$constantforkey = 'CASHDESK_ID_WAREHOUSE'.$_SESSION["takeposterminal"];
1348					if (!empty($conf->global->$constantforkey) && $line->fk_product > 0) {
1349						$sql = "SELECT e.rowid, e.ref, e.lieu, e.fk_parent, e.statut, ps.reel, ps.rowid as product_stock_id, p.pmp";
1350						$sql .= " FROM ".MAIN_DB_PREFIX."entrepot as e,";
1351						$sql .= " ".MAIN_DB_PREFIX."product_stock as ps";
1352						$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = ps.fk_product";
1353						$sql .= " WHERE ps.reel != 0";
1354						$sql .= " AND ps.fk_entrepot = ".$conf->global->$constantforkey;
1355						$sql .= " AND e.entity IN (".getEntity('stock').")";
1356						$sql .= " AND ps.fk_product = ".$line->fk_product;
1357						$resql = $db->query($sql);
1358						if ($resql) {
1359							$obj = $db->fetch_object($resql);
1360							$stock_real = price2num($obj->reel, 'MS');
1361							$htmlforlines .= $line->qty;
1362							if ($line->qty && $line->qty > $stock_real) $htmlforlines .= '<span style="color: var(--amountremaintopaycolor)">';
1363							$htmlforlines .= ' <span class="posstocktoolow">('.$langs->trans("Stock").' '.$stock_real.')</span>';
1364							if ($line->qty && $line->qty > $stock_real) $htmlforlines .= "</span>";
1365						} else {
1366							dol_print_error($db);
1367						}
1368					} else {
1369						$htmlforlines .= $line->qty;
1370					}
1371				} else {
1372					$htmlforlines .= $line->qty;
1373				}
1374
1375				$htmlforlines .= '</td>';
1376				$htmlforlines .= '<td class="right classfortooltip" title="'.$moreinfo.'">';
1377				$htmlforlines .= price($line->total_ttc, 1, '', 1, -1, -1, $conf->currency);
1378				if (!empty($conf->multicurrency->enabled) && $_SESSION["takeposcustomercurrency"] != "" && $conf->currency != $_SESSION["takeposcustomercurrency"]) {
1379					//Only show customer currency if multicurrency module is enabled, if currency selected and if this currency selected is not the same as main currency
1380					include_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
1381					$multicurrency = new MultiCurrency($db);
1382					$multicurrency->fetch(0, $_SESSION["takeposcustomercurrency"]);
1383					$htmlforlines .= '<br><span id="linecolht-span-total" style="font-size:0.9em; font-style:italic;">('.price($line->total_ttc * $multicurrency->rate->rate).' '.$_SESSION["takeposcustomercurrency"].')</span>';
1384				}
1385				$htmlforlines .= '</td>';
1386			}
1387			$htmlforlines .= '</tr>'."\n";
1388			$htmlforlines .= $htmlsupplements[$line->id];
1389
1390			print $htmlforlines;
1391		}
1392	} else {
1393		print '<tr class="drag drop oddeven"><td class="left"><span class="opacitymedium">'.$langs->trans("Empty").'</span></td><td></td><td></td><td></td></tr>';
1394	}
1395} else {      // No invoice generated yet
1396	print '<tr class="drag drop oddeven"><td class="left"><span class="opacitymedium">'.$langs->trans("Empty").'</span></td><td></td><td></td><td></td></tr>';
1397}
1398
1399print '</table>';
1400
1401if (($action == "valid" || $action == "history") && $invoice->type != Facture::TYPE_CREDIT_NOTE) {
1402	print '<button id="buttonprint" type="button" onclick="ModalBox(\'ModalCreditNote\')">'.$langs->trans('CreateCreditNote').'</button>';
1403}
1404
1405
1406if ($action == "search")
1407{
1408	print '<center>
1409	<input type="text" id="search" name="search" onkeyup="Search2();" name="search" style="width:80%;font-size: 150%;" placeholder=' . $langs->trans('Search').'
1410	</center>';
1411}
1412
1413print '</div>';
1414