dolibarr  13.0.2
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 <https://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 'validate_sell':
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->amountWithTax();
87  } else {
88  $montant = $_POST['txtEncaisse'];
89  }
90 
91  if ($mode_reglement != 'DIF') {
92  $obj_facturation->amountCollected($montant);
93 
94  //Determination de la somme rendue
95  $total = $obj_facturation->amountWithTax();
96  $encaisse = $obj_facturation->amountCollected();
97 
98  $obj_facturation->amountReturned($encaisse - $total);
99  } else {
100  //$txtDatePaiement=$_POST['txtDatePaiement'];
101  $datePaiement = dol_mktime(0, 0, 0, $_POST['txtDatePaiementmonth'], $_POST['txtDatePaiementday'], $_POST['txtDatePaiementyear']);
102  $txtDatePaiement = dol_print_date($datePaiement, 'dayrfc');
103  $obj_facturation->paiementLe($txtDatePaiement);
104  }
105 
106  $redirection = 'affIndex.php?menutpl=validation';
107  break;
108 
109 
110  case 'retour':
111  $redirection = 'affIndex.php?menutpl=facturation';
112  break;
113 
114 
115  case 'validate_invoice':
116  $now = dol_now();
117 
118  // Recuperation de la date et de l'heure
119  $date = dol_print_date($now, 'day');
120  $heure = dol_print_date($now, 'hour');
121 
122  $note = '';
123  if (!is_object($obj_facturation))
124  {
125  dol_print_error('', 'Empty context');
126  exit;
127  }
128 
129  switch ($obj_facturation->getSetPaymentMode())
130  {
131  case 'DIF':
132  $mode_reglement_id = 0;
133  //$cond_reglement_id = dol_getIdFromCode($db,'RECEP','cond_reglement','code','rowid')
134  $cond_reglement_id = 0;
135  break;
136  case 'ESP':
137  $mode_reglement_id = dol_getIdFromCode($db, 'LIQ', 'c_paiement', 'code', 'id', 1);
138  $cond_reglement_id = 0;
139  $note .= $langs->trans("Cash")."\n";
140  $note .= $langs->trans("Received").' : '.$obj_facturation->amountCollected()." ".$conf->currency."\n";
141  $note .= $langs->trans("Rendu").' : '.$obj_facturation->amountReturned()." ".$conf->currency."\n";
142  $note .= "\n";
143  $note .= '--------------------------------------'."\n\n";
144  break;
145  case 'CB':
146  $mode_reglement_id = dol_getIdFromCode($db, 'CB', 'c_paiement', 'code', 'id', 1);
147  $cond_reglement_id = 0;
148  break;
149  case 'CHQ':
150  $mode_reglement_id = dol_getIdFromCode($db, 'CHQ', 'c_paiement', 'code', 'id', 1);
151  $cond_reglement_id = 0;
152  break;
153  }
154  if (empty($mode_reglement_id)) $mode_reglement_id = 0; // If mode_reglement_id not found
155  if (empty($cond_reglement_id)) $cond_reglement_id = 0; // If cond_reglement_id not found
156  $note .= $_POST['txtaNotes'];
157  dol_syslog("obj_facturation->getSetPaymentMode()=".$obj_facturation->getSetPaymentMode()." mode_reglement_id=".$mode_reglement_id." cond_reglement_id=".$cond_reglement_id);
158 
159  $error = 0;
160 
161 
162  $db->begin();
163 
164  $user->fetch($_SESSION['uid']);
165  $user->getrights();
166 
167  $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
168  $societe = new Societe($db);
169  $societe->fetch($thirdpartyid);
170 
171  $invoice = new Facture($db);
172 
173  // Get content of cart
174  $tab_liste = $_SESSION['poscart'];
175 
176  // Loop on each line into cart
177  $tab_liste_size = count($tab_liste);
178  for ($i = 0; $i < $tab_liste_size; $i++)
179  {
180  $tmp = getTaxesFromId($tab_liste[$i]['fk_tva']);
181  $vat_rate = $tmp['rate'];
182  $vat_npr = $tmp['npr'];
183  $vat_src_code = $tmp['code'];
184 
185  $invoiceline = new FactureLigne($db);
186  $invoiceline->fk_product = $tab_liste[$i]['fk_article'];
187  $invoiceline->desc = $tab_liste[$i]['label'];
188  $invoiceline->qty = $tab_liste[$i]['qte'];
189  $invoiceline->remise_percent = $tab_liste[$i]['remise_percent'];
190  $invoiceline->price = $tab_liste[$i]['price'];
191  $invoiceline->subprice = $tab_liste[$i]['price'];
192 
193  $invoiceline->tva_tx = empty($vat_rate) ? 0 : $vat_rate; // works even if vat_rate is ''
194  $invoiceline->info_bits = empty($vat_npr) ? 0 : $vat_npr;
195  $invoiceline->vat_src_code = $vat_src_code;
196 
197  $invoiceline->total_ht = $tab_liste[$i]['total_ht'];
198  $invoiceline->total_ttc = $tab_liste[$i]['total_ttc'];
199  $invoiceline->total_tva = $tab_liste[$i]['total_vat'];
200  $invoiceline->total_localtax1 = $tab_liste[$i]['total_localtax1'];
201  $invoiceline->total_localtax2 = $tab_liste[$i]['total_localtax2'];
202 
203  $invoice->lines[] = $invoiceline;
204  }
205 
206  $invoice->socid = $conf_fksoc;
207  $invoice->date_creation = $now;
208  $invoice->date = $now;
209  $invoice->date_lim_reglement = 0;
210  $invoice->total_ht = $obj_facturation->amountWithoutTax();
211  $invoice->total_tva = $obj_facturation->amountVat();
212  $invoice->total_ttc = $obj_facturation->amountWithTax();
213  $invoice->note_private = $note;
214  $invoice->cond_reglement_id = $cond_reglement_id;
215  $invoice->mode_reglement_id = $mode_reglement_id;
216  $invoice->module_source = 'cashdesk';
217  $invoice->pos_source = '0';
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  } else {
254  setEventMessages($invoice->error, $invoice->errors, 'errors');
255  $error++;
256  }
257 
258  $id = $invoice->id;
259  } else {
260  $resultcreate = $invoice->create($user, 0, 0);
261  if ($resultcreate > 0)
262  {
263  $warehouseidtodecrease = (isset($_SESSION["CASHDESK_ID_WAREHOUSE"]) ? $_SESSION["CASHDESK_ID_WAREHOUSE"] : 0);
264  if (!empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease = 0; // If a particular stock is defined, we disable choice
265 
266  $resultvalid = $invoice->validate($user, $obj_facturation->numInvoice(), 0);
267 
268  if ($warehouseidtodecrease > 0)
269  {
270  // Decrease
271  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
272  $langs->load("agenda");
273  // Loop on each line
274  $cpt = count($invoice->lines);
275  for ($i = 0; $i < $cpt; $i++)
276  {
277  if ($invoice->lines[$i]->fk_product > 0)
278  {
279  $mouvP = new MouvementStock($db);
280  $mouvP->origin = &$invoice;
281  // We decrease stock for product
282  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));
283  else $result = $mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarrFromPos", $invoice->newref));
284  if ($result < 0) {
285  setEventMessages($mouvP->error, $mouvP->errors, 'errors');
286  $error++;
287  }
288  }
289  }
290  }
291 
292  $id = $invoice->id;
293 
294  // Add the payment
295  $payment = new Paiement($db);
296  $payment->datepaye = $now;
297  $payment->amounts[$invoice->id] = $obj_facturation->amountWithTax();
298  $payment->note_public = $langs->trans("Payment").' '.$langs->trans("Invoice").' '.$obj_facturation->numInvoice();
299  $payment->paiementid = $invoice->mode_reglement_id;
300  $payment->num_paiement = '';
301  $payment->num_payment = '';
302 
303  $paiement_id = $payment->create($user);
304  if ($paiement_id > 0)
305  {
306  if (!$error)
307  {
308  $result = $payment->addPaymentToBank($user, 'payment', '(CustomerInvoicePayment)', $bankaccountid, '', '');
309  if (!$result > 0)
310  {
311  $errmsg = $paiement->error;
312  $error++;
313  }
314  }
315 
316  if (!$error)
317  {
318  if ($invoice->total_ttc == $obj_facturation->amountWithTax()
319  && $obj_facturation->getSetPaymentMode() != 'DIFF')
320  {
321  // We set status to paid
322  $result = $invoice->set_paid($user);
323  //print 'set paid';exit;
324  }
325  }
326  } else {
327  setEventMessages($invoice->error, $invoice->errors, 'errors');
328  $error++;
329  }
330  } else {
331  setEventMessages($invoice->error, $invoice->errors, 'errors');
332  $error++;
333  }
334  }
335 
336 
337  if (!$error)
338  {
339  $db->commit();
340  $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
341  } else {
342  $db->rollback();
343  $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
344  }
345  break;
346 
347  // End of case: validate_invoice
348 }
349 
350 unset($_SESSION['serObjFacturation']);
351 
352 $_SESSION['serObjFacturation'] = serialize($obj_facturation);
353 
354 header('Location: '.$redirection);
355 exit;
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage stock movements.
const TYPE_STANDARD
Standard invoice.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_now($mode= 'auto')
Return date for now.
getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
Get tax (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:319
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage payments of customer invoices.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Class to manage invoices.
Class to manage invoice lines.