validation_verif.php 13 KB

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