PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/cashdesk/validation_verif.php

https://github.com/asterix14/dolibarr
PHP | 297 lines | 213 code | 52 blank | 32 comment | 26 complexity | 127c959558c2290df6072f6b9b2f4a33 MD5 | raw file
Possible License(s): LGPL-2.0
  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 2 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. require('../main.inc.php');
  20. require_once(DOL_DOCUMENT_ROOT.'/cashdesk/include/environnement.php');
  21. require_once(DOL_DOCUMENT_ROOT.'/cashdesk/class/Facturation.class.php');
  22. require_once(DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php');
  23. require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php');
  24. require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php');
  25. $obj_facturation = unserialize($_SESSION['serObjFacturation']);
  26. unset ($_SESSION['serObjFacturation']);
  27. $action =GETPOST('action');
  28. $bankaccountid=GETPOST('cashdeskbank');
  29. switch ($action)
  30. {
  31. default:
  32. $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menu=validation';
  33. break;
  34. case 'valide_achat':
  35. $company=new Societe($db);
  36. $company->fetch($conf->global->CASHDESK_ID_THIRDPARTY);
  37. $invoice=new Facture($db);
  38. $invoice->date=dol_now();
  39. $invoice->type=0;
  40. $num=$invoice->getNextNumRef($company);
  41. $obj_facturation->num_facture($num);
  42. $obj_facturation->mode_reglement($_POST['hdnChoix']);
  43. // Si paiement autre qu'en especes, montant encaisse = prix total
  44. $mode_reglement = $obj_facturation->mode_reglement();
  45. if ( $mode_reglement != 'ESP' ) {
  46. $montant = $obj_facturation->prix_total_ttc();
  47. } else {
  48. $montant = $_POST['txtEncaisse'];
  49. }
  50. if ( $mode_reglement != 'DIF') {
  51. $obj_facturation->montant_encaisse($montant);
  52. //Determination de la somme rendue
  53. $total = $obj_facturation->prix_total_ttc();
  54. $encaisse = $obj_facturation->montant_encaisse();
  55. $obj_facturation->montant_rendu($encaisse - $total);
  56. }
  57. else
  58. {
  59. //$txtDatePaiement=$_POST['txtDatePaiement'];
  60. $datePaiement=dol_mktime(0,0,0,$_POST['txtDatePaiementmonth'],$_POST['txtDatePaiementday'],$_POST['txtDatePaiementyear']);
  61. $txtDatePaiement=dol_print_date($datePaiement,'dayrfc');
  62. $obj_facturation->paiement_le($txtDatePaiement);
  63. }
  64. $redirection = 'affIndex.php?menu=validation';
  65. break;
  66. case 'retour':
  67. $redirection = 'affIndex.php?menu=facturation';
  68. break;
  69. case 'valide_facture':
  70. $now=dol_now();
  71. // Recuperation de la date et de l'heure
  72. $date = dol_print_date($now,'day');
  73. $heure = dol_print_date($now,'hour');
  74. $note = '';
  75. if (! is_object($obj_facturation))
  76. {
  77. dol_print_error('','Empty context');
  78. exit;
  79. }
  80. switch ( $obj_facturation->mode_reglement() )
  81. {
  82. case 'DIF':
  83. $mode_reglement_id = 0;
  84. //$cond_reglement_id = dol_getIdFromCode($db,'RECEP','cond_reglement','code','rowid')
  85. $cond_reglement_id = 0;
  86. break;
  87. case 'ESP':
  88. $mode_reglement_id = dol_getIdFromCode($db,'LIQ','c_paiement');
  89. $cond_reglement_id = 0;
  90. $note .= $langs->trans("Cash")."\n";
  91. $note .= $langs->trans("Received").' : '.$obj_facturation->montant_encaisse()." ".$conf->monnaie."\n";
  92. $note .= $langs->trans("Rendu").' : '.$obj_facturation->montant_rendu()." ".$conf->monnaie."\n";
  93. $note .= "\n";
  94. $note .= '--------------------------------------'."\n\n";
  95. break;
  96. case 'CB':
  97. $mode_reglement_id = dol_getIdFromCode($db,'CB','c_paiement');
  98. $cond_reglement_id = 0;
  99. break;
  100. case 'CHQ':
  101. $mode_reglement_id = dol_getIdFromCode($db,'CHQ','c_paiement');
  102. $cond_reglement_id = 0;
  103. break;
  104. }
  105. if (empty($mode_reglement_id)) $mode_reglement_id=0; // If mode_reglement_id not found
  106. if (empty($cond_reglement_id)) $cond_reglement_id=0; // If cond_reglement_id not found
  107. $note .= $_POST['txtaNotes'];
  108. dol_syslog("obj_facturation->mode_reglement()=".$obj_facturation->mode_reglement()." mode_reglement_id=".$mode_reglement_id." cond_reglement_id=".$cond_reglement_id);
  109. $error=0;
  110. $db->begin();
  111. $user->fetch($_SESSION['uid']);
  112. $user->getrights();
  113. $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
  114. $societe = new Societe($db);
  115. $societe->fetch($thirdpartyid);
  116. $invoice=new Facture($db);
  117. // Get content of cart
  118. $tab_liste = $_SESSION['poscart'];
  119. // Loop on each product
  120. $tab_liste_size=count($tab_liste);
  121. for ($i=0;$i < $tab_liste_size;$i++)
  122. {
  123. // Recuperation de l'article
  124. $product = new Product($db);
  125. $product->fetch($tab_liste[$i]['fk_article']);
  126. $ret=array('label'=>$product->label,'tva_tx'=>$product->tva_tx,'price'=>$product->price);
  127. if ($conf->global->PRODUIT_MULTIPRICES)
  128. {
  129. if (isset($product->multiprices[$societe->price_level]))
  130. {
  131. $ret['price'] = $product->multiprices[$societe->price_level];
  132. }
  133. }
  134. $tab_article = $ret;
  135. $res = $db->query('SELECT taux FROM '.MAIN_DB_PREFIX.'c_tva WHERE rowid = '.$tab_liste[$i]['fk_tva']);
  136. $ret=array();
  137. $tab = $db->fetch_array($res);
  138. foreach ( $tab as $cle => $valeur )
  139. {
  140. $ret[$cle] = $valeur;
  141. }
  142. $tab_tva = $ret;
  143. $invoiceline=new FactureLigne($db);
  144. $invoiceline->fk_product=$tab_liste[$i]['fk_article'];
  145. $invoiceline->desc=$tab_article['label'];
  146. $invoiceline->tva_tx=empty($tab_tva['taux'])?0:$tab_tva['taux']; // works even if vat_rate is ''
  147. //$invoiceline->tva_tx=$tab_tva['taux'];
  148. $invoiceline->qty=$tab_liste[$i]['qte'];
  149. $invoiceline->remise_percent=$tab_liste[$i]['remise_percent'];
  150. $invoiceline->price=$tab_article['price'];
  151. $invoiceline->subprice=$tab_article['price'];
  152. $invoiceline->total_ht=$tab_liste[$i]['total_ht'];
  153. $invoiceline->total_ttc=$tab_liste[$i]['total_ttc'];
  154. $invoiceline->total_tva=($tab_liste[$i]['total_ttc']-$tab_liste[$i]['total_ht']);
  155. $invoice->lines[]=$invoiceline;
  156. }
  157. $invoice->socid=$conf_fksoc;
  158. $invoice->date_creation=$now;
  159. $invoice->date=$now;
  160. $invoice->date_lim_reglement=0;
  161. $invoice->total_ht=$obj_facturation->prix_total_ht();
  162. $invoice->total_tva=$obj_facturation->montant_tva();
  163. $invoice->total_ttc=$obj_facturation->prix_total_ttc();
  164. $invoice->note=$note;
  165. $invoice->cond_reglement_id=$cond_reglement_id;
  166. $invoice->mode_reglement_id=$mode_reglement_id;
  167. //print "c=".$invoice->cond_reglement_id." m=".$invoice->mode_reglement_id; exit;
  168. // Si paiement differe ...
  169. if ( $obj_facturation->mode_reglement() == 'DIF' )
  170. {
  171. $resultcreate=$invoice->create($user,0,dol_stringtotime($obj_facturation->paiement_le()));
  172. if ($resultcreate > 0)
  173. {
  174. $resultvalid=$invoice->validate($user,$obj_facturation->num_facture());
  175. }
  176. else
  177. {
  178. $error++;
  179. }
  180. $id = $invoice->id;
  181. }
  182. else
  183. {
  184. $resultcreate=$invoice->create($user,0,0);
  185. if ($resultcreate > 0)
  186. {
  187. $resultvalid=$invoice->validate($user, $obj_facturation->num_facture(), (isset($_SESSION["CASHDESK_ID_WAREHOUSE"])?$_SESSION["CASHDESK_ID_WAREHOUSE"]:0));
  188. $id = $invoice->id;
  189. // Add the payment
  190. $payment=new Paiement($db);
  191. $payment->datepaye=$now;
  192. $payment->bank_account=$conf_fkaccount;
  193. $payment->amounts[$invoice->id]=$obj_facturation->prix_total_ttc();
  194. $payment->note=$langs->trans("Payment").' '.$langs->trans("Invoice").' '.$obj_facturation->num_facture();
  195. $payment->paiementid=$invoice->mode_reglement_id;
  196. $payment->num_paiement='';
  197. $paiement_id = $payment->create($user);
  198. if ($paiement_id > 0)
  199. {
  200. if (! $error)
  201. {
  202. $result=$payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccountid, '', '');
  203. if (! $result > 0)
  204. {
  205. $errmsg=$paiement->error;
  206. $error++;
  207. }
  208. }
  209. if (! $error)
  210. {
  211. if ($invoice->total_ttc == $obj_facturation->prix_total_ttc()
  212. && $obj_facturation->mode_reglement() != 'DIFF')
  213. {
  214. // We set status to payed
  215. $result=$invoice->set_paid($user);
  216. //print 'eeeee';exit;
  217. }
  218. }
  219. }
  220. else
  221. {
  222. $error++;
  223. }
  224. }
  225. else
  226. {
  227. $error++;
  228. }
  229. }
  230. if (! $error)
  231. {
  232. $db->commit();
  233. $redirection = 'affIndex.php?menu=validation_ok&facid='.$id; // Ajout de l'id de la facture, pour l'inclure dans un lien pointant directement vers celle-ci dans Dolibarr
  234. }
  235. else
  236. {
  237. $db->rollback();
  238. $redirection = 'affIndex.php?facid='.$id.'&mesg=ErrorFailedToCreateInvoice'; // Ajout de l'id de la facture, pour l'inclure dans un lien pointant directement vers celle-ci dans Dolibarr
  239. }
  240. break;
  241. // End of case: valide_facture
  242. }
  243. $_SESSION['serObjFacturation'] = serialize($obj_facturation);
  244. header('Location: '.$redirection);
  245. ?>