invoice.php 63 KB

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