dolibarr  9.0.0
validation_verif.php
Go to the documentation of this file.
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 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/cashdesk/include/environnement.php';
28 require_once DOL_DOCUMENT_ROOT.'/cashdesk/class/Facturation.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
33 
34 $obj_facturation = unserialize($_SESSION['serObjFacturation']);
35 
36 $action =GETPOST('action','aZ09');
37 $bankaccountid=GETPOST('cashdeskbank');
38 
39 switch ($action)
40 {
41  default:
42  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=validation';
43  break;
44 
45  case 'valide_achat':
46  $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
47 
48  $company=new Societe($db);
49  $company->fetch($thirdpartyid);
50 
51  $invoice=new Facture($db);
52  $invoice->date=dol_now();
53  $invoice->type= Facture::TYPE_STANDARD;
54 
55  // To use a specific numbering module for POS, reset $conf->global->FACTURE_ADDON and other vars here
56  // and restore values just after
57  $sav_FACTURE_ADDON='';
58  if (! empty($conf->global->POS_ADDON))
59  {
60  $sav_FACTURE_ADDON = $conf->global->FACTURE_ADDON;
61  $conf->global->FACTURE_ADDON = $conf->global->POS_ADDON;
62 
63  // To force prefix only for POS with terre module
64  if (! empty($conf->global->POS_NUMBERING_TERRE_FORCE_PREFIX)) $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX = $conf->global->POS_NUMBERING_TERRE_FORCE_PREFIX;
65  // To force prefix only for POS with mars module
66  if (! empty($conf->global->POS_NUMBERING_MARS_FORCE_PREFIX)) $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX = $conf->global->POS_NUMBERING_MARS_FORCE_PREFIX;
67  // To force rule only for POS with mercure
68  //...
69  }
70 
71  $num=$invoice->getNextNumRef($company);
72 
73  // Restore save values
74  if (! empty($sav_FACTURE_ADDON))
75  {
76  $conf->global->FACTURE_ADDON = $sav_FACTURE_ADDON;
77  }
78 
79  $obj_facturation->numInvoice($num);
80 
81  $obj_facturation->getSetPaymentMode($_POST['hdnChoix']);
82 
83  // Si paiement autre qu'en especes, montant encaisse = prix total
84  $mode_reglement = $obj_facturation->getSetPaymentMode();
85  if ( $mode_reglement != 'ESP' ) {
86  $montant = $obj_facturation->prixTotalTtc();
87  } else {
88  $montant = $_POST['txtEncaisse'];
89  }
90 
91  if ( $mode_reglement != 'DIF') {
92  $obj_facturation->montantEncaisse($montant);
93 
94  //Determination de la somme rendue
95  $total = $obj_facturation->prixTotalTtc();
96  $encaisse = $obj_facturation->montantEncaisse();
97 
98  $obj_facturation->montantRendu($encaisse - $total);
99  }
100  else
101  {
102  //$txtDatePaiement=$_POST['txtDatePaiement'];
103  $datePaiement=dol_mktime(0,0,0,$_POST['txtDatePaiementmonth'],$_POST['txtDatePaiementday'],$_POST['txtDatePaiementyear']);
104  $txtDatePaiement=dol_print_date($datePaiement,'dayrfc');
105  $obj_facturation->paiementLe($txtDatePaiement);
106  }
107 
108  $redirection = 'affIndex.php?menutpl=validation';
109  break;
110 
111 
112  case 'retour':
113  $redirection = 'affIndex.php?menutpl=facturation';
114  break;
115 
116 
117  case 'valide_facture':
118  $now=dol_now();
119 
120  // Recuperation de la date et de l'heure
121  $date = dol_print_date($now,'day');
122  $heure = dol_print_date($now,'hour');
123 
124  $note = '';
125  if (! is_object($obj_facturation))
126  {
127  dol_print_error('','Empty context');
128  exit;
129  }
130 
131  switch ($obj_facturation->getSetPaymentMode() )
132  {
133  case 'DIF':
134  $mode_reglement_id = 0;
135  //$cond_reglement_id = dol_getIdFromCode($db,'RECEP','cond_reglement','code','rowid')
136  $cond_reglement_id = 0;
137  break;
138  case 'ESP':
139  $mode_reglement_id = dol_getIdFromCode($db,'LIQ','c_paiement','code','id',1);
140  $cond_reglement_id = 0;
141  $note .= $langs->trans("Cash")."\n";
142  $note .= $langs->trans("Received").' : '.$obj_facturation->montantEncaisse()." ".$conf->currency."\n";
143  $note .= $langs->trans("Rendu").' : '.$obj_facturation->montantRendu()." ".$conf->currency."\n";
144  $note .= "\n";
145  $note .= '--------------------------------------'."\n\n";
146  break;
147  case 'CB':
148  $mode_reglement_id = dol_getIdFromCode($db,'CB','c_paiement','code','id',1);
149  $cond_reglement_id = 0;
150  break;
151  case 'CHQ':
152  $mode_reglement_id = dol_getIdFromCode($db,'CHQ','c_paiement','code','id',1);
153  $cond_reglement_id = 0;
154  break;
155  }
156  if (empty($mode_reglement_id)) $mode_reglement_id=0; // If mode_reglement_id not found
157  if (empty($cond_reglement_id)) $cond_reglement_id=0; // If cond_reglement_id not found
158  $note .= $_POST['txtaNotes'];
159  dol_syslog("obj_facturation->getSetPaymentMode()=".$obj_facturation->getSetPaymentMode()." mode_reglement_id=".$mode_reglement_id." cond_reglement_id=".$cond_reglement_id);
160 
161  $error=0;
162 
163 
164  $db->begin();
165 
166  $user->fetch($_SESSION['uid']);
167  $user->getrights();
168 
169  $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
170  $societe = new Societe($db);
171  $societe->fetch($thirdpartyid);
172 
173  $invoice=new Facture($db);
174 
175  // Get content of cart
176  $tab_liste = $_SESSION['poscart'];
177 
178  // Loop on each line into cart
179  $tab_liste_size=count($tab_liste);
180  for ($i=0; $i < $tab_liste_size; $i++)
181  {
182  $tmp = getTaxesFromId($tab_liste[$i]['fk_tva']);
183  $vat_rate = $tmp['rate'];
184  $vat_npr = $tmp['npr'];
185  $vat_src_code = $tmp['code'];
186 
187  $invoiceline=new FactureLigne($db);
188  $invoiceline->fk_product=$tab_liste[$i]['fk_article'];
189  $invoiceline->desc=$tab_liste[$i]['label'];
190  $invoiceline->qty=$tab_liste[$i]['qte'];
191  $invoiceline->remise_percent=$tab_liste[$i]['remise_percent'];
192  $invoiceline->price=$tab_liste[$i]['price'];
193  $invoiceline->subprice=$tab_liste[$i]['price'];
194 
195  $invoiceline->tva_tx=empty($vat_rate)?0:$vat_rate; // works even if vat_rate is ''
196  $invoiceline->info_bits=empty($vat_npr)?0:$vat_npr;
197  $invoiceline->vat_src_code=$vat_src_code;
198 
199  $invoiceline->total_ht=$tab_liste[$i]['total_ht'];
200  $invoiceline->total_ttc=$tab_liste[$i]['total_ttc'];
201  $invoiceline->total_tva=$tab_liste[$i]['total_vat'];
202  $invoiceline->total_localtax1=$tab_liste[$i]['total_localtax1'];
203  $invoiceline->total_localtax2=$tab_liste[$i]['total_localtax2'];
204 
205  $invoice->lines[]=$invoiceline;
206  }
207 
208  $invoice->socid=$conf_fksoc;
209  $invoice->date_creation=$now;
210  $invoice->date=$now;
211  $invoice->date_lim_reglement=0;
212  $invoice->total_ht=$obj_facturation->prixTotalHt();
213  $invoice->total_tva=$obj_facturation->montantTva();
214  $invoice->total_ttc=$obj_facturation->prixTotalTtc();
215  $invoice->note_private=$note;
216  $invoice->cond_reglement_id=$cond_reglement_id;
217  $invoice->mode_reglement_id=$mode_reglement_id;
218  $invoice->module_source = 'cashdesk';
219  $invoice->pos_source = '0';
220  //print "c=".$invoice->cond_reglement_id." m=".$invoice->mode_reglement_id; exit;
221 
222  // Si paiement differe ...
223  if ( $obj_facturation->getSetPaymentMode() == 'DIF' )
224  {
225  $resultcreate=$invoice->create($user,0,dol_stringtotime($obj_facturation->paiementLe()));
226  if ($resultcreate > 0)
227  {
228  $warehouseidtodecrease=(isset($_SESSION["CASHDESK_ID_WAREHOUSE"])?$_SESSION["CASHDESK_ID_WAREHOUSE"]:0);
229  if (! empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease=0; // If a particular stock is defined, we disable choice
230 
231  $resultvalid=$invoice->validate($user, $obj_facturation->numInvoice(), 0);
232 
233  if ($warehouseidtodecrease > 0)
234  {
235  // Decrease
236  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
237  $langs->load("agenda");
238  // Loop on each line
239  $cpt=count($invoice->lines);
240  for ($i = 0; $i < $cpt; $i++)
241  {
242  if ($invoice->lines[$i]->fk_product > 0)
243  {
244  $mouvP = new MouvementStock($db);
245  $mouvP->origin = &$invoice;
246  // We decrease stock for product
247  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));
248  else $result=$mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
249  if ($result < 0) {
250  $error++;
251  }
252  }
253  }
254  }
255  }
256  else
257  {
258  setEventMessages($invoice->error, $invoice->errors, 'errors');
259  $error++;
260  }
261 
262  $id = $invoice->id;
263  }
264  else
265  {
266  $resultcreate=$invoice->create($user,0,0);
267  if ($resultcreate > 0)
268  {
269  $warehouseidtodecrease=(isset($_SESSION["CASHDESK_ID_WAREHOUSE"])?$_SESSION["CASHDESK_ID_WAREHOUSE"]:0);
270  if (! empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease=0; // If a particular stock is defined, we disable choice
271 
272  $resultvalid=$invoice->validate($user, $obj_facturation->numInvoice(), 0);
273 
274  if ($warehouseidtodecrease > 0)
275  {
276  // Decrease
277  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
278  $langs->load("agenda");
279  // Loop on each line
280  $cpt=count($invoice->lines);
281  for ($i = 0; $i < $cpt; $i++)
282  {
283  if ($invoice->lines[$i]->fk_product > 0)
284  {
285  $mouvP = new MouvementStock($db);
286  $mouvP->origin = &$invoice;
287  // We decrease stock for product
288  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));
289  else $result=$mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
290  if ($result < 0) {
291  setEventMessages($mouvP->error, $mouvP->errors, 'errors');
292  $error++;
293  }
294  }
295  }
296  }
297 
298  $id = $invoice->id;
299 
300  // Add the payment
301  $payment=new Paiement($db);
302  $payment->datepaye=$now;
303  $payment->bank_account=$conf_fkaccount;
304  $payment->amounts[$invoice->id]=$obj_facturation->prixTotalTtc();
305  $payment->note=$langs->trans("Payment").' '.$langs->trans("Invoice").' '.$obj_facturation->numInvoice();
306  $payment->paiementid=$invoice->mode_reglement_id;
307  $payment->num_paiement='';
308 
309  $paiement_id = $payment->create($user);
310  if ($paiement_id > 0)
311  {
312  if (! $error)
313  {
314  $result=$payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccountid, '', '');
315  if (! $result > 0)
316  {
317  $errmsg=$paiement->error;
318  $error++;
319  }
320  }
321 
322  if (! $error)
323  {
324  if ($invoice->total_ttc == $obj_facturation->prixTotalTtc()
325  && $obj_facturation->getSetPaymentMode() != 'DIFF')
326  {
327  // We set status to payed
328  $result=$invoice->set_paid($user);
329  //print 'set paid';exit;
330  }
331  }
332  }
333  else
334  {
335  setEventMessages($invoice->error, $invoice->errors, 'errors');
336  $error++;
337  }
338  }
339  else
340  {
341  setEventMessages($invoice->error, $invoice->errors, 'errors');
342  $error++;
343  }
344  }
345 
346 
347  if (! $error)
348  {
349  $db->commit();
350  $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
351  }
352  else
353  {
354  $db->rollback();
355  $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
356  }
357  break;
358 
359  // End of case: valide_facture
360 }
361 
362 unset ($_SESSION['serObjFacturation']);
363 
364 $_SESSION['serObjFacturation'] = serialize($obj_facturation);
365 
366 header('Location: '.$redirection);
367 exit;
Class to manage stock movements.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
const TYPE_STANDARD
Standard invoice.
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
Return an id or code from a code or id.
getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
Get vat main information from Id.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:295
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage payments of customer invoices.
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class to manage invoices.
Class to manage invoice lines.