invoice.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /* Copyright (C) 2018 Andreu Bisquerra <jove@bisquerra.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
  18. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  19. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  20. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  21. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  22. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  23. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  24. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  25. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  26. require '../main.inc.php'; // Load $user and permissions
  27. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  29. $langs->loadLangs(array("bills", "cashdesk"));
  30. $id = GETPOST('id','int');
  31. $action = GETPOST('action','alpha');
  32. $idproduct = GETPOST('idproduct','int');
  33. $place = GETPOST('place','int');
  34. $number = GETPOST('number');
  35. $idline = GETPOST('idline');
  36. $desc = GETPOST('desc','alpha');
  37. $pay = GETPOST('pay');
  38. $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."facture where facnumber='(PROV-POS-".$place.")'";
  39. $resql = $db->query($sql);
  40. $row = $db->fetch_array ($resql);
  41. $placeid=$row[0];
  42. if (! $placeid) $placeid=0; // not necesary
  43. else{
  44. $invoice = new Facture($db);
  45. $invoice->fetch($placeid);
  46. }
  47. /*
  48. * Actions
  49. */
  50. if ($action == 'valid' && $user->rights->facture->creer){
  51. if ($pay=="cash") $bankaccount=$conf->global->CASHDESK_ID_BANKACCOUNT_CASH;
  52. else if ($pay=="card") $bankaccount=$conf->global->CASHDESK_ID_BANKACCOUNT_CB;
  53. else if ($pay=="cheque") $bankaccount=$conf->global->CASHDESK_ID_BANKACCOUNT_CHEQUE;
  54. $now=dol_now();
  55. $invoice = new Facture($db);
  56. $invoice->fetch($placeid);
  57. if (! empty($conf->stock->enabled) and $conf->global->CASHDESK_NO_DECREASE_STOCK!="1") $invoice->validate($user, '', $conf->global->CASHDESK_ID_WAREHOUSE);
  58. else $invoice->validate($user);
  59. // Add the payment
  60. $payment=new Paiement($db);
  61. $payment->datepaye=$now;
  62. $payment->bank_account=$bankaccount;
  63. $payment->amounts[$invoice->id]=$invoice->total_ttc;
  64. if ($pay=="cash") $payment->paiementid=4;
  65. else if ($pay=="card") $payment->paiementid=6;
  66. else if ($pay=="cheque") $payment->paiementid=7;
  67. $payment->num_paiement=$invoice->facnumber;
  68. $payment->create($user);
  69. $payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccount, '', '');
  70. $invoice->set_paid($user);
  71. }
  72. if (($action=="addline" || $action=="freezone") and $placeid==0)
  73. {
  74. // $place is id of POS, $placeid is id of invoice
  75. if ($placeid==0) {
  76. $invoice = new Facture($db);
  77. $invoice->socid=$conf->global->CASHDESK_ID_THIRDPARTY;
  78. $invoice->date=dol_now();
  79. $invoice->ref="(PROV-POS)";
  80. $invoice->module_source = 'takepos';
  81. $invoice->pos_source = (string) (empty($place)?'0':$place);
  82. $placeid=$invoice->create($user);
  83. $sql="UPDATE ".MAIN_DB_PREFIX."facture set facnumber='(PROV-POS-".$place.")' where rowid=".$placeid;
  84. $db->query($sql);
  85. }
  86. }
  87. if ($action=="addline"){
  88. $prod = new Product($db);
  89. $prod->fetch($idproduct);
  90. $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);
  91. $invoice->fetch($placeid);
  92. }
  93. if ($action=="freezone"){
  94. $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);
  95. $invoice->fetch($placeid);
  96. }
  97. if ($action=="deleteline"){
  98. if ($idline>0 and $placeid>0){ //If exist invoice and line, to avoid errors if deleted from other device or no line selected
  99. $invoice->deleteline($idline);
  100. $invoice->fetch($placeid);
  101. }
  102. else if ($placeid>0){ //If exist invoice, but no line selected, proced to delete last line
  103. $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."facturedet where fk_facture='$placeid' order by rowid DESC";
  104. $resql = $db->query($sql);
  105. $row = $db->fetch_array ($resql);
  106. $deletelineid=$row[0];
  107. $invoice->deleteline($deletelineid);
  108. $invoice->fetch($placeid);
  109. }
  110. }
  111. if ($action=="updateqty"){
  112. foreach ($invoice->lines as $line){
  113. if ($line->id==$idline) $result = $invoice->updateline($line->id, $line->desc, $line->subprice, $number, $line->remise_percent,
  114. $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type,
  115. $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent,
  116. $line->fk_unit);
  117. }
  118. $invoice->fetch($placeid);
  119. }
  120. if ($action=="updateprice"){
  121. foreach ($invoice->lines as $line){
  122. if ($line->id==$idline) $result = $invoice->updateline($line->id, $line->desc, $number, $line->qty, $line->remise_percent,
  123. $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type,
  124. $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent,
  125. $line->fk_unit);
  126. }
  127. $invoice->fetch($placeid);
  128. }
  129. if ($action=="updatereduction"){
  130. foreach ($invoice->lines as $line){
  131. if ($line->id==$idline) $result = $invoice->updateline($line->id, $line->desc, $line->subprice, $line->qty, $number,
  132. $line->date_start, $line->date_end, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 'HT', $line->info_bits, $line->product_type,
  133. $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->special_code, $line->array_options, $line->situation_percent,
  134. $line->fk_unit);
  135. }
  136. $invoice->fetch($placeid);
  137. }
  138. if ($action=="order" and $placeid!=0){
  139. require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  140. $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>';
  141. $footerorder='</tbody></table>'.dol_print_date(dol_now(), 'dayhour').'<br></html>';
  142. $order_receipt_printer1="";
  143. $order_receipt_printer2="";
  144. $catsprinter1 = explode(';',$conf->global->TAKEPOS_PRINTED_CATEGORIES_1);
  145. $catsprinter2 = explode(';',$conf->global->TAKEPOS_PRINTED_CATEGORIES_2);
  146. foreach ($invoice->lines as $line){
  147. if ($line->special_code=="3") continue;
  148. $c = new Categorie($db);
  149. $existing = $c->containing($line->fk_product, Categorie::TYPE_PRODUCT, 'id');
  150. $result = array_intersect($catsprinter1, $existing);
  151. $count=count($result);
  152. if ($count>0){
  153. $sql="UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='3' where rowid=$line->rowid";
  154. $db->query($sql);
  155. $order_receipt_printer1.='<tr>'.$line->product_label.'<td align="right">'.$line->qty.'</td></tr>';
  156. }
  157. }
  158. foreach ($invoice->lines as $line){
  159. if ($line->special_code=="3") continue;
  160. $c = new Categorie($db);
  161. $existing = $c->containing($line->fk_product, Categorie::TYPE_PRODUCT, 'id');
  162. $result = array_intersect($catsprinter2, $existing);
  163. $count=count($result);
  164. if ($count>0){
  165. $sql="UPDATE ".MAIN_DB_PREFIX."facturedet set special_code='3' where rowid=$line->rowid";
  166. $db->query($sql);
  167. $order_receipt_printer2.='<tr>'.$line->product_label.'<td align="right">'.$line->qty.'</td></tr>';
  168. }
  169. }
  170. $invoice->fetch($placeid);
  171. }
  172. ?>
  173. <style>
  174. .selected {
  175. color: red;
  176. }
  177. .order {
  178. color: limegreen;
  179. }
  180. </style>
  181. <script language="javascript">
  182. var selectedline=0;
  183. var selectedtext="";
  184. $(document).ready(function(){
  185. $('table tbody tr').click(function(){
  186. $('table tbody tr').removeClass("selected");
  187. $(this).addClass("selected");
  188. if (selectedline==this.id) return; // If is already selected
  189. else selectedline=this.id;
  190. selectedtext=$('#'+selectedline).find("td:first").html();
  191. });
  192. <?php if ($action=="order" and $order_receipt_printer1!=""){
  193. ?>
  194. $.ajax({
  195. type: "POST",
  196. url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER;?>:8111/print',
  197. data: '<?php print $headerorder.$order_receipt_printer1.$footerorder; ?>'
  198. });
  199. <?php
  200. }
  201. if ($action=="order" and $order_receipt_printer2!=""){
  202. ?>
  203. $.ajax({
  204. type: "POST",
  205. url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER;?>:8111/print2',
  206. data: '<?php print $headerorder.$order_receipt_printer2.$footerorder; ?>'
  207. });
  208. <?php
  209. }
  210. if ($action=="search"){
  211. ?>
  212. $('#search').focus();
  213. <?php
  214. }
  215. ?>
  216. });
  217. function Print(id){
  218. $.colorbox({href:"receipt.php?facid="+id, width:"40%", height:"90%", transition:"none", iframe:"true", title:"<?php echo $langs->trans("PrintTicket");?>"});
  219. }
  220. function TakeposPrinting(id){
  221. var receipt;
  222. $.get("receipt.php?facid="+id, function(data, status){
  223. receipt=data.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '');
  224. $.ajax({
  225. type: "POST",
  226. url: 'http://<?php print $conf->global->TAKEPOS_PRINT_SERVER;?>:8111/print',
  227. data: receipt
  228. });
  229. });
  230. }
  231. </script>
  232. <?php
  233. print '<div class="div-table-responsive-no-min">';
  234. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  235. print '<tr class="liste_titre nodrag nodrop">';
  236. print '<td class="linecoldescription">'.$langs->trans('Description').'</td>';
  237. print '<td class="linecolqty" align="right">'.$langs->trans('Qty').'</td>';
  238. print '<td class="linecolht" align="right">'.$langs->trans('TotalHTShort').'</td>';
  239. print "</tr>\n";
  240. if ($placeid>0) foreach ($invoice->lines as $line)
  241. {
  242. print '<tr class="drag drop oddeven';
  243. if ($line->special_code=="3") print ' order';
  244. print '" id="'.$line->rowid.'">';
  245. print '<td>'.$line->product_label.$line->desc.'</td>';
  246. print '<td align="right">'.$line->qty.'</td>';
  247. print '<td align="right">'.price($line->total_ttc).'</td>';
  248. print '</tr>';
  249. }
  250. print '</table>';
  251. print '<p style="font-size:120%;" align="right"><b>'.$langs->trans('TotalTTC');
  252. if($conf->global->TAKEPOS_BAR_RESTAURANT) print " ".$langs->trans('Place')." ".$place;
  253. print ': '.price($invoice->total_ttc, 1, '', 1, - 1, - 1, $conf->currency).'&nbsp;</b></p>';
  254. //if ($invoice->socid != $conf->global->CASHDESK_ID_THIRDPARTY){
  255. $soc = new Societe($db);
  256. if ($invoice->socid > 0) $soc->fetch($invoice->socid);
  257. else $soc->fetch($conf->global->CASHDESK_ID_THIRDPARTY);
  258. print '<p style="font-size:120%;" align="right">';
  259. print $langs->trans("Customer").': '.$soc->name;
  260. print '</p>';
  261. //}
  262. if ($action=="valid"){
  263. print '<p style="font-size:120%;" align="center"><b>'.$invoice->facnumber." ".$langs->trans('BillShortStatusValidated').'</b></p>';
  264. if ($conf->global->TAKEPOSCONNECTOR) print '<center><button type="button" onclick="TakeposPrinting('.$placeid.');">'.$langs->trans('PrintTicket').'</button><center>';
  265. else print '<center><button type="button" onclick="Print('.$placeid.');">'.$langs->trans('PrintTicket').'</button><center>';
  266. }
  267. if ($action=="search"){
  268. print '<center>
  269. <input type="text" id="search" name="search" onkeyup="Search2();" name="search" style="width:80%;font-size: 150%;" placeholder='.$langs->trans('Search').'
  270. </center>';
  271. }
  272. print '</div>';