invoice.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. // if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
  19. // if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  20. // if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  21. // if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  22. if (!defined('NOCSRFCHECK')) { define('NOCSRFCHECK', '1'); }
  23. if (!defined('NOTOKENRENEWAL')) { define('NOTOKENRENEWAL', '1'); }
  24. if (!defined('NOREQUIREMENU')) { define('NOREQUIREMENU', '1'); }
  25. if (!defined('NOREQUIREHTML')) { define('NOREQUIREHTML', '1'); }
  26. if (!defined('NOREQUIREAJAX')) { define('NOREQUIREAJAX', '1'); }
  27. require '../main.inc.php';
  28. // Load $user and permissions
  29. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
  31. $langs->loadLangs(
  32. array(
  33. "bills",
  34. "cashdesk"
  35. )
  36. );
  37. $id = GETPOST('id', 'int');
  38. $action = GETPOST('action', 'alpha');
  39. $idproduct = GETPOST('idproduct', 'int');
  40. $place = GETPOST('place', 'int');
  41. $number = GETPOST('number');
  42. $idline = GETPOST('idline');
  43. $desc = GETPOST('desc', 'alpha');
  44. $pay = GETPOST('pay');
  45. $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."facture where ref='(PROV-POS-".$place.")'";
  46. $resql = $db->query($sql);
  47. $row = $db->fetch_array($resql);
  48. $placeid = $row[0];
  49. if (!$placeid) { $placeid = 0; // not necesary
  50. } else
  51. {
  52. $invoice = new Facture($db);
  53. $invoice->fetch($placeid);
  54. }
  55. /*
  56. * Actions
  57. */
  58. if ($action == 'valid' && $user->rights->facture->creer)
  59. {
  60. if ($pay=="cash") $bankaccount=$conf->global->CASHDESK_ID_BANKACCOUNT_CASH;
  61. elseif ($pay=="card") $bankaccount=$conf->global->CASHDESK_ID_BANKACCOUNT_CB;
  62. elseif ($pay=="cheque") $bankaccount=$conf->global->CASHDESK_ID_BANKACCOUNT_CHEQUE;
  63. $now=dol_now();
  64. $invoice = new Facture($db);
  65. $invoice->fetch($placeid);
  66. if (! empty($conf->stock->enabled) and $conf->global->CASHDESK_NO_DECREASE_STOCK!="1") $invoice->validate($user, '', $conf->global->CASHDESK_ID_WAREHOUSE);
  67. else $invoice->validate($user);
  68. // Add the payment
  69. $payment=new Paiement($db);
  70. $payment->datepaye=$now;
  71. $payment->bank_account=$bankaccount;
  72. $payment->amounts[$invoice->id]=$invoice->total_ttc;
  73. if ($pay=="cash") $payment->paiementid=4;
  74. elseif ($pay=="card") $payment->paiementid=6;
  75. elseif ($pay=="cheque") $payment->paiementid=7;
  76. $payment->num_paiement=$invoice->ref;
  77. $payment->create($user);
  78. $payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccount, '', '');
  79. $invoice->set_paid($user);
  80. }
  81. if (($action=="addline" || $action=="freezone") && $placeid==0)
  82. {
  83. // $place is id of POS, $placeid is id of invoice
  84. if ($placeid==0)
  85. {
  86. $invoice = new Facture($db);
  87. $invoice->socid=$conf->global->CASHDESK_ID_THIRDPARTY;
  88. $invoice->date=dol_now();
  89. $invoice->ref="(PROV-POS)";
  90. $invoice->module_source = 'takepos';
  91. $invoice->pos_source = (string) (empty($place)?'0':$place);
  92. $placeid=$invoice->create($user);
  93. $sql="UPDATE ".MAIN_DB_PREFIX."facture set ref='(PROV-POS-".$place.")' where rowid=".$placeid;
  94. $db->query($sql);
  95. }
  96. }
  97. if ($action == "addline") {
  98. $prod = new Product($db);
  99. $prod->fetch($idproduct);
  100. $invoice->addline($prod->description, $prod->price, 1, $prod->tva_tx, $prod->localtax1_tx, $prod->localtax2_tx, $idproduct, $prod->remise_percent, '', 0, 0, 0, '', $prod->price_base_type, $prod->price_ttc, $prod->type, -1, 0, '', 0, 0, null, 0, '', 0, 100, '', null, 0);
  101. $invoice->fetch($placeid);
  102. }
  103. if ($action == "freezone") {
  104. $invoice->addline($desc, $number, 1, $conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS, 0, 0, 0, 0, '', 0, 0, 0, '', 'TTC', $number, 0, -1, 0, '', 0, 0, null, 0, '', 0, 100, '', null, 0);
  105. $invoice->fetch($placeid);
  106. }
  107. if ($action == "deleteline") {
  108. if ($idline > 0 and $placeid > 0) { //If exist invoice and line, to avoid errors if deleted from other device or no line selected
  109. $invoice->deleteline($idline);
  110. $invoice->fetch($placeid);
  111. }
  112. elseif ($placeid > 0) { //If exist invoice, but no line selected, proced to delete last line
  113. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "facturedet where fk_facture='$placeid' order by rowid DESC";
  114. $resql = $db->query($sql);
  115. $row = $db->fetch_array($resql);
  116. $deletelineid = $row[0];
  117. $invoice->deleteline($deletelineid);
  118. $invoice->fetch($placeid);
  119. }
  120. }
  121. if ($action == "updateqty") {
  122. foreach($invoice->lines as $line)
  123. {
  124. if ($line->id == $idline) { $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);
  125. }
  126. }
  127. $invoice->fetch($placeid);
  128. }
  129. if ($action == "updateprice") {
  130. foreach($invoice->lines as $line)
  131. {
  132. if ($line->id == $idline) { $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, '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);
  133. }
  134. }
  135. $invoice->fetch($placeid);
  136. }
  137. if ($action == "updatereduction") {
  138. foreach($invoice->lines as $line)
  139. {
  140. if ($line->id == $idline) { $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);
  141. }
  142. }
  143. $invoice->fetch($placeid);
  144. }
  145. if ($action == "order" and $placeid != 0) {
  146. include_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  147. $headerorder = '<html><br><b>' . $langs->trans('Place') . ' ' . $place . '<br><table width="65%"><thead><tr><th align="left">' . $langs->trans("Label") . '</th><th align="right">' . $langs->trans("Qty") . '</th></tr></thead><tbody>';
  148. $footerorder = '</tbody></table>' . dol_print_date(dol_now(), 'dayhour') . '<br></html>';
  149. $order_receipt_printer1 = "";
  150. $order_receipt_printer2 = "";
  151. $catsprinter1 = explode(';', $conf->global->TAKEPOS_PRINTED_CATEGORIES_1);
  152. $catsprinter2 = explode(';', $conf->global->TAKEPOS_PRINTED_CATEGORIES_2);
  153. foreach($invoice->lines as $line)
  154. {
  155. if ($line->special_code == "3") { continue;
  156. }
  157. $c = new Categorie($db);
  158. $existing = $c->containing($line->fk_product, Categorie::TYPE_PRODUCT, 'id');
  159. $result = array_intersect($catsprinter1, $existing);
  160. $count = count($result);
  161. if ($count > 0) {
  162. $sql = "UPDATE " . MAIN_DB_PREFIX . "facturedet set special_code='3' where rowid=$line->rowid";
  163. $db->query($sql);
  164. $order_receipt_printer1.= '<tr>' . $line->product_label . '<td align="right">' . $line->qty . '</td></tr>';
  165. }
  166. }
  167. foreach($invoice->lines as $line)
  168. {
  169. if ($line->special_code == "3") { continue;
  170. }
  171. $c = new Categorie($db);
  172. $existing = $c->containing($line->fk_product, Categorie::TYPE_PRODUCT, 'id');
  173. $result = array_intersect($catsprinter2, $existing);
  174. $count = count($result);
  175. if ($count > 0) {
  176. $sql = "UPDATE " . MAIN_DB_PREFIX . "facturedet set special_code='3' where rowid=$line->rowid";
  177. $db->query($sql);
  178. $order_receipt_printer2.= '<tr>' . $line->product_label . '<td align="right">' . $line->qty . '</td></tr>';
  179. }
  180. }
  181. $invoice->fetch($placeid);
  182. }
  183. ?>
  184. <style>
  185. .selected {
  186. font-weight: bold;
  187. }
  188. .order {
  189. color: limegreen;
  190. }
  191. </style>
  192. <script language="javascript">
  193. var selectedline=0;
  194. var selectedtext="";
  195. var placeid=<?php echo $placeid;?>;
  196. $(document).ready(function(){
  197. $('table tbody tr').click(function(){
  198. $('table tbody tr').removeClass("selected");
  199. $(this).addClass("selected");
  200. if (selectedline==this.id) return; // If is already selected
  201. else selectedline=this.id;
  202. selectedtext=$('#'+selectedline).find("td:first").html();
  203. });
  204. <?php
  205. if ($action == "order" and $order_receipt_printer1 != "") {
  206. ?>
  207. $.ajax({
  208. type: "POST",
  209. url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print',
  210. data: '<?php
  211. print $headerorder . $order_receipt_printer1 . $footerorder; ?>'
  212. });
  213. <?php
  214. }
  215. if ($action == "order" and $order_receipt_printer2 != "") {
  216. ?>
  217. $.ajax({
  218. type: "POST",
  219. url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print2',
  220. data: '<?php
  221. print $headerorder . $order_receipt_printer2 . $footerorder; ?>'
  222. });
  223. <?php
  224. }
  225. if ($action == "search") {
  226. ?>
  227. $('#search').focus();
  228. <?php
  229. }
  230. ?>
  231. });
  232. $(document).ready(function(){
  233. $('table tbody tr').click(function(){
  234. $('table tbody tr').removeClass("selected");
  235. $(this).addClass("selected");
  236. if (selectedline==this.id) return; // If is already selected
  237. else selectedline=this.id;
  238. selectedtext=$('#'+selectedline).find("td:first").html();
  239. });
  240. <?php
  241. if ($action == "temp" and $ticket_printer1 != "") {
  242. ?>
  243. $.ajax({
  244. type: "POST",
  245. url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print',
  246. data: '<?php
  247. print $header_soc . $header_ticket . $body_ticket . $ticket_printer1 . $ticket_total . $footer_ticket; ?>'
  248. });
  249. <?php
  250. }
  251. if ($action == "search") {
  252. ?>
  253. $('#search').focus();
  254. <?php
  255. }
  256. ?>
  257. });
  258. function Print(id){
  259. $.colorbox({href:"receipt.php?facid="+id, width:"40%", height:"90%", transition:"none", iframe:"true", title:"<?php
  260. echo $langs->trans("PrintTicket"); ?>"});
  261. }
  262. function TakeposPrinting(id){
  263. var receipt;
  264. $.get("receipt.php?facid="+id, function(data, status){
  265. receipt=data.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '');
  266. $.ajax({
  267. type: "POST",
  268. url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER; ?>:8111/print',
  269. data: receipt
  270. });
  271. });
  272. }
  273. </script>
  274. <?php
  275. print '<div class="div-table-responsive-no-min invoice">';
  276. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  277. print '<tr class="liste_titre nodrag nodrop">';
  278. print '<td class="linecoldescription">' . $langs->trans('Description') . '</td>';
  279. print '<td class="linecolqty" align="right">' . $langs->trans('Qty') . '</td>';
  280. print '<td class="linecolht" align="right">' . $langs->trans('TotalHTShort') . '</td>';
  281. print "</tr>\n";
  282. if ($placeid > 0) {
  283. foreach($invoice->lines as $line)
  284. {
  285. print '<tr class="drag drop oddeven';
  286. if ($line->special_code == "3") { print ' order';
  287. }
  288. print '" id="' . $line->rowid . '">';
  289. print '<td align="left">' . $line->product_label . $line->desc . '</td>';
  290. print '<td align="right">' . $line->qty . '</td>';
  291. print '<td align="right">' . price($line->total_ttc) . '</td>';
  292. print '</tr>';
  293. }
  294. }
  295. print '</table>';
  296. print '<p style="font-size:120%;" align="right"><b>'.$langs->trans('TotalTTC');
  297. if($conf->global->TAKEPOS_BAR_RESTAURANT) print " ".$langs->trans('Place')." ".$place;
  298. print ': '.price($invoice->total_ttc, 1, '', 1, - 1, - 1, $conf->currency).'&nbsp;</b></p>';
  299. if ($invoice->socid != $conf->global->CASHDESK_ID_THIRDPARTY)
  300. {
  301. $soc = new Societe($db);
  302. if ($invoice->socid > 0) $soc->fetch($invoice->socid);
  303. else $soc->fetch($conf->global->CASHDESK_ID_THIRDPARTY);
  304. print '<p style="font-size:120%;" align="right">';
  305. print $langs->trans("Customer").': '.$soc->name;
  306. print '</p>';
  307. }
  308. if ($action=="valid")
  309. {
  310. print '<p style="font-size:120%;" align="center"><b>'.$invoice->ref." ".$langs->trans('BillShortStatusValidated').'</b></p>';
  311. if ($conf->global->TAKEPOSCONNECTOR) print '<center><button type="button" onclick="TakeposPrinting('.$placeid.');">'.$langs->trans('PrintTicket').'</button><center>';
  312. else print '<center><button type="button" onclick="Print('.$placeid.');">'.$langs->trans('PrintTicket').'</button><center>';
  313. }
  314. if ($action == "search")
  315. {
  316. print '<center>
  317. <input type="text" id="search" name="search" onkeyup="Search2();" name="search" style="width:80%;font-size: 150%;" placeholder=' . $langs->trans('Search') . '
  318. </center>';
  319. }
  320. print '</div>';