dolibarr  7.0.0-beta
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  //print "c=".$invoice->cond_reglement_id." m=".$invoice->mode_reglement_id; exit;
219 
220  // Si paiement differe ...
221  if ( $obj_facturation->getSetPaymentMode() == 'DIF' )
222  {
223  $resultcreate=$invoice->create($user,0,dol_stringtotime($obj_facturation->paiementLe()));
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 
229  $resultvalid=$invoice->validate($user, $obj_facturation->numInvoice(), 0);
230 
231  if ($warehouseidtodecrease > 0)
232  {
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  {
240  if ($invoice->lines[$i]->fk_product > 0)
241  {
242  $mouvP = new MouvementStock($db);
243  $mouvP->origin = &$invoice;
244  // We decrease stock for product
245  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));
246  else $result=$mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
247  if ($result < 0) {
248  $error++;
249  }
250  }
251  }
252  }
253  }
254  else
255  {
256  setEventMessage($invoice->error, $invoice->errors, 'errors');
257  $error++;
258  }
259 
260  $id = $invoice->id;
261  }
262  else
263  {
264  $resultcreate=$invoice->create($user,0,0);
265  if ($resultcreate > 0)
266  {
267  $warehouseidtodecrease=(isset($_SESSION["CASHDESK_ID_WAREHOUSE"])?$_SESSION["CASHDESK_ID_WAREHOUSE"]:0);
268  if (! empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease=0; // If a particular stock is defined, we disable choice
269 
270  $resultvalid=$invoice->validate($user, $obj_facturation->numInvoice(), 0);
271 
272  if ($warehouseidtodecrease > 0)
273  {
274  // Decrease
275  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
276  $langs->load("agenda");
277  // Loop on each line
278  $cpt=count($invoice->lines);
279  for ($i = 0; $i < $cpt; $i++)
280  {
281  if ($invoice->lines[$i]->fk_product > 0)
282  {
283  $mouvP = new MouvementStock($db);
284  $mouvP->origin = &$invoice;
285  // We decrease stock for product
286  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));
287  else $result=$mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos",$invoice->newref));
288  if ($result < 0) {
289  setEventMessages($mouvP->error, $mouvP->errors, 'errors');
290  $error++;
291  }
292  }
293  }
294  }
295 
296  $id = $invoice->id;
297 
298  // Add the payment
299  $payment=new Paiement($db);
300  $payment->datepaye=$now;
301  $payment->bank_account=$conf_fkaccount;
302  $payment->amounts[$invoice->id]=$obj_facturation->prixTotalTtc();
303  $payment->note=$langs->trans("Payment").' '.$langs->trans("Invoice").' '.$obj_facturation->numInvoice();
304  $payment->paiementid=$invoice->mode_reglement_id;
305  $payment->num_paiement='';
306 
307  $paiement_id = $payment->create($user);
308  if ($paiement_id > 0)
309  {
310  if (! $error)
311  {
312  $result=$payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccountid, '', '');
313  if (! $result > 0)
314  {
315  $errmsg=$paiement->error;
316  $error++;
317  }
318  }
319 
320  if (! $error)
321  {
322  if ($invoice->total_ttc == $obj_facturation->prixTotalTtc()
323  && $obj_facturation->getSetPaymentMode() != 'DIFF')
324  {
325  // We set status to payed
326  $result=$invoice->set_paid($user);
327  //print 'eeeee';exit;
328  }
329 
330  }
331  }
332  else
333  {
334  setEventMessages($invoice->error, $invoice->errors, 'errors');
335  $error++;
336  }
337  }
338  else
339  {
340  setEventMessages($invoice->error, $invoice->errors, 'errors');
341  $error++;
342  }
343  }
344 
345 
346  if (! $error)
347  {
348  $db->commit();
349  $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
350  }
351  else
352  {
353  $db->rollback();
354  $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
355  }
356  break;
357 
358  // End of case: valide_facture
359 }
360 
361 unset ($_SESSION['serObjFacturation']);
362 
363 $_SESSION['serObjFacturation'] = serialize($obj_facturation);
364 
365 header('Location: '.$redirection);
366 exit;
Class to manage stock movements.
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:286
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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).
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
Class to manage invoices.
Class to manage invoice lines.