validation_verif.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /* Copyright (C) 2007-2008 Jeremie Ollivier <jeremie.o@laposte.net>
  3. * Copyright (C) 2008-2009 Laurent Destailleur <eldy@uers.sourceforge.net>
  4. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/cashdesk/validation_verif.php
  21. * \ingroup cashdesk
  22. * \brief validation_verif.php
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/cashdesk/include/environnement.php';
  26. require_once DOL_DOCUMENT_ROOT.'/cashdesk/class/Facturation.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  31. $obj_facturation = unserialize($_SESSION['serObjFacturation']);
  32. $action =GETPOST('action','aZ09');
  33. $bankaccountid=GETPOST('cashdeskbank');
  34. switch ($action)
  35. {
  36. default:
  37. $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=validation';
  38. break;
  39. case 'valide_achat':
  40. $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
  41. $company=new Societe($db);
  42. $company->fetch($thirdpartyid);
  43. $invoice=new Facture($db);
  44. $invoice->date=dol_now();
  45. $invoice->type= Facture::TYPE_STANDARD;
  46. // To use a specific numbering module for POS, reset $conf->global->FACTURE_ADDON and other vars here
  47. // and restore values just after
  48. $sav_FACTURE_ADDON='';
  49. if (! empty($conf->global->POS_ADDON))
  50. {
  51. $sav_FACTURE_ADDON = $conf->global->FACTURE_ADDON;
  52. $conf->global->FACTURE_ADDON = $conf->global->POS_ADDON;
  53. // To force prefix only for POS with terre module
  54. if (! empty($conf->global->POS_NUMBERING_TERRE_FORCE_PREFIX)) $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX = $conf->global->POS_NUMBERING_TERRE_FORCE_PREFIX;
  55. // To force prefix only for POS with mars module
  56. if (! empty($conf->global->POS_NUMBERING_MARS_FORCE_PREFIX)) $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX = $conf->global->POS_NUMBERING_MARS_FORCE_PREFIX;
  57. // To force rule only for POS with mercure
  58. //...
  59. }
  60. $num=$invoice->getNextNumRef($company);
  61. // Restore save values
  62. if (! empty($sav_FACTURE_ADDON))
  63. {
  64. $conf->global->FACTURE_ADDON = $sav_FACTURE_ADDON;
  65. }
  66. $obj_facturation->numInvoice($num);
  67. $obj_facturation->getSetPaymentMode($_POST['hdnChoix']);
  68. // Si paiement autre qu'en especes, montant encaisse = prix total
  69. $mode_reglement = $obj_facturation->getSetPaymentMode();
  70. if ( $mode_reglement != 'ESP' ) {
  71. $montant = $obj_facturation->prixTotalTtc();
  72. } else {
  73. $montant = $_POST['txtEncaisse'];
  74. }
  75. if ( $mode_reglement != 'DIF') {
  76. $obj_facturation->montantEncaisse($montant);
  77. //Determination de la somme rendue
  78. $total = $obj_facturation->prixTotalTtc();
  79. $encaisse = $obj_facturation->montantEncaisse();
  80. $obj_facturation->montantRendu($encaisse - $total);
  81. }
  82. else
  83. {
  84. //$txtDatePaiement=$_POST['txtDatePaiement'];
  85. $datePaiement=dol_mktime(0,0,0,$_POST['txtDatePaiementmonth'],$_POST['txtDatePaiementday'],$_POST['txtDatePaiementyear']);
  86. $txtDatePaiement=dol_print_date($datePaiement,'dayrfc');
  87. $obj_facturation->paiementLe($txtDatePaiement);
  88. }
  89. $redirection = 'affIndex.php?menutpl=validation';
  90. break;
  91. case 'retour':
  92. $redirection = 'affIndex.php?menutpl=facturation';
  93. break;
  94. case 'valide_facture':
  95. $now=dol_now();
  96. // Recuperation de la date et de l'heure
  97. $date = dol_print_date($now,'day');
  98. $heure = dol_print_date($now,'hour');
  99. $note = '';
  100. if (! is_object($obj_facturation))
  101. {
  102. dol_print_error('','Empty context');
  103. exit;
  104. }
  105. switch ($obj_facturation->getSetPaymentMode() )
  106. {
  107. case 'DIF':
  108. $mode_reglement_id = 0;
  109. //$cond_reglement_id = dol_getIdFromCode($db,'RECEP','cond_reglement','code','rowid')
  110. $cond_reglement_id = 0;
  111. break;
  112. case 'ESP':
  113. $mode_reglement_id = dol_getIdFromCode($db,'LIQ','c_paiement','code','id',1);
  114. $cond_reglement_id = 0;
  115. $note .= $langs->trans("Cash")."\n";
  116. $note .= $langs->trans("Received").' : '.$obj_facturation->montantEncaisse()." ".$conf->currency."\n";
  117. $note .= $langs->trans("Rendu").' : '.$obj_facturation->montantRendu()." ".$conf->currency."\n";
  118. $note .= "\n";
  119. $note .= '--------------------------------------'."\n\n";
  120. break;
  121. case 'CB':
  122. $mode_reglement_id = dol_getIdFromCode($db,'CB','c_paiement','code','id',1);
  123. $cond_reglement_id = 0;
  124. break;
  125. case 'CHQ':
  126. $mode_reglement_id = dol_getIdFromCode($db,'CHQ','c_paiement','code','id',1);
  127. $cond_reglement_id = 0;
  128. break;
  129. }
  130. if (empty($mode_reglement_id)) $mode_reglement_id=0; // If mode_reglement_id not found
  131. if (empty($cond_reglement_id)) $cond_reglement_id=0; // If cond_reglement_id not found
  132. $note .= $_POST['txtaNotes'];
  133. dol_syslog("obj_facturation->getSetPaymentMode()=".$obj_facturation->getSetPaymentMode()." mode_reglement_id=".$mode_reglement_id." cond_reglement_id=".$cond_reglement_id);
  134. $error=0;
  135. $db->begin();
  136. $user->fetch($_SESSION['uid']);
  137. $user->getrights();
  138. $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
  139. $societe = new Societe($db);
  140. $societe->fetch($thirdpartyid);
  141. $invoice=new Facture($db);
  142. // Get content of cart
  143. $tab_liste = $_SESSION['poscart'];
  144. // Loop on each line into cart
  145. $tab_liste_size=count($tab_liste);
  146. for ($i=0; $i < $tab_liste_size; $i++)
  147. {
  148. $tmp = getTaxesFromId($tab_liste[$i]['fk_tva']);
  149. $vat_rate = $tmp['rate'];
  150. $vat_npr = $tmp['npr'];
  151. $vat_src_code = $tmp['code'];
  152. $invoiceline=new FactureLigne($db);
  153. $invoiceline->fk_product=$tab_liste[$i]['fk_article'];
  154. $invoiceline->desc=$tab_liste[$i]['label'];
  155. $invoiceline->qty=$tab_liste[$i]['qte'];
  156. $invoiceline->remise_percent=$tab_liste[$i]['remise_percent'];
  157. $invoiceline->price=$tab_liste[$i]['price'];
  158. $invoiceline->subprice=$tab_liste[$i]['price'];
  159. $invoiceline->tva_tx=empty($vat_rate)?0:$vat_rate; // works even if vat_rate is ''
  160. $invoiceline->info_bits=empty($vat_npr)?0:$vat_npr;
  161. $invoiceline->vat_src_code=$vat_src_code;
  162. $invoiceline->total_ht=$tab_liste[$i]['total_ht'];
  163. $invoiceline->total_ttc=$tab_liste[$i]['total_ttc'];
  164. $invoiceline->total_tva=$tab_liste[$i]['total_vat'];
  165. $invoiceline->total_localtax1=$tab_liste[$i]['total_localtax1'];
  166. $invoiceline->total_localtax2=$tab_liste[$i]['total_localtax2'];
  167. $invoice->lines[]=$invoiceline;
  168. }
  169. $invoice->socid=$conf_fksoc;
  170. $invoice->date_creation=$now;
  171. $invoice->date=$now;
  172. $invoice->date_lim_reglement=0;
  173. $invoice->total_ht=$obj_facturation->prixTotalHt();
  174. $invoice->total_tva=$obj_facturation->montantTva();
  175. $invoice->total_ttc=$obj_facturation->prixTotalTtc();
  176. $invoice->note_private=$note;
  177. $invoice->cond_reglement_id=$cond_reglement_id;
  178. $invoice->mode_reglement_id=$mode_reglement_id;
  179. $invoice->module_source = 'cashdesk';
  180. $invoice->pos_source = '0';
  181. //print "c=".$invoice->cond_reglement_id." m=".$invoice->mode_reglement_id; exit;
  182. // Si paiement differe ...
  183. if ( $obj_facturation->getSetPaymentMode() == 'DIF' )
  184. {
  185. $resultcreate=$invoice->create($user,0,dol_stringtotime($obj_facturation->paiementLe()));
  186. if ($resultcreate > 0)
  187. {
  188. $warehouseidtodecrease=(isset($_SESSION["CASHDESK_ID_WAREHOUSE"])?$_SESSION["CASHDESK_ID_WAREHOUSE"]:0);
  189. if (! empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease=0; // If a particular stock is defined, we disable choice
  190. $resultvalid=$invoice->validate($user, $obj_facturation->numInvoice(), 0);
  191. if ($warehouseidtodecrease > 0)
  192. {
  193. // Decrease
  194. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  195. $langs->load("agenda");
  196. // Loop on each line
  197. $cpt=count($invoice->lines);
  198. for ($i = 0; $i < $cpt; $i++)
  199. {
  200. if ($invoice->lines[$i]->fk_product > 0)
  201. {
  202. $mouvP = new MouvementStock($db);
  203. $mouvP->origin = &$invoice;
  204. // We decrease stock for product
  205. if ($invoice->type == $invoice::TYPE_CREDIT_NOTE) $result=$mouvP->reception($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
  206. else $result=$mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
  207. if ($result < 0) {
  208. $error++;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. else
  215. {
  216. setEventMessages($invoice->error, $invoice->errors, 'errors');
  217. $error++;
  218. }
  219. $id = $invoice->id;
  220. }
  221. else
  222. {
  223. $resultcreate=$invoice->create($user,0,0);
  224. if ($resultcreate > 0)
  225. {
  226. $warehouseidtodecrease=(isset($_SESSION["CASHDESK_ID_WAREHOUSE"])?$_SESSION["CASHDESK_ID_WAREHOUSE"]:0);
  227. if (! empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease=0; // If a particular stock is defined, we disable choice
  228. $resultvalid=$invoice->validate($user, $obj_facturation->numInvoice(), 0);
  229. if ($warehouseidtodecrease > 0)
  230. {
  231. // Decrease
  232. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  233. $langs->load("agenda");
  234. // Loop on each line
  235. $cpt=count($invoice->lines);
  236. for ($i = 0; $i < $cpt; $i++)
  237. {
  238. if ($invoice->lines[$i]->fk_product > 0)
  239. {
  240. $mouvP = new MouvementStock($db);
  241. $mouvP->origin = &$invoice;
  242. // We decrease stock for product
  243. if ($invoice->type == $invoice::TYPE_CREDIT_NOTE) $result=$mouvP->reception($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
  244. else $result=$mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
  245. if ($result < 0) {
  246. setEventMessages($mouvP->error, $mouvP->errors, 'errors');
  247. $error++;
  248. }
  249. }
  250. }
  251. }
  252. $id = $invoice->id;
  253. // Add the payment
  254. $payment=new Paiement($db);
  255. $payment->datepaye=$now;
  256. $payment->bank_account=$conf_fkaccount;
  257. $payment->amounts[$invoice->id]=$obj_facturation->prixTotalTtc();
  258. $payment->note=$langs->trans("Payment").' '.$langs->trans("Invoice").' '.$obj_facturation->numInvoice();
  259. $payment->paiementid=$invoice->mode_reglement_id;
  260. $payment->num_paiement='';
  261. $paiement_id = $payment->create($user);
  262. if ($paiement_id > 0)
  263. {
  264. if (! $error)
  265. {
  266. $result=$payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccountid, '', '');
  267. if (! $result > 0)
  268. {
  269. $errmsg=$paiement->error;
  270. $error++;
  271. }
  272. }
  273. if (! $error)
  274. {
  275. if ($invoice->total_ttc == $obj_facturation->prixTotalTtc()
  276. && $obj_facturation->getSetPaymentMode() != 'DIFF')
  277. {
  278. // We set status to payed
  279. $result=$invoice->set_paid($user);
  280. //print 'set paid';exit;
  281. }
  282. }
  283. }
  284. else
  285. {
  286. setEventMessages($invoice->error, $invoice->errors, 'errors');
  287. $error++;
  288. }
  289. }
  290. else
  291. {
  292. setEventMessages($invoice->error, $invoice->errors, 'errors');
  293. $error++;
  294. }
  295. }
  296. if (! $error)
  297. {
  298. $db->commit();
  299. $redirection = 'affIndex.php?menutpl=validation_ok&facid='.$id; // Ajout de l'id de la facture, pour l'inclure dans un lien pointant directement vers celle-ci dans Dolibarr
  300. }
  301. else
  302. {
  303. $db->rollback();
  304. $redirection = 'affIndex.php?facid='.$id.'&error=1&mesg=ErrorFailedToCreateInvoice'; // Ajout de l'id de la facture, pour l'inclure dans un lien pointant directement vers celle-ci dans Dolibarr
  305. }
  306. break;
  307. // End of case: valide_facture
  308. }
  309. unset ($_SESSION['serObjFacturation']);
  310. $_SESSION['serObjFacturation'] = serialize($obj_facturation);
  311. header('Location: '.$redirection);
  312. exit;