dolibarr  13.0.2
server_invoice.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 if (!defined("NOCSRFCHECK")) define("NOCSRFCHECK", '1');
25 
26 require '../master.inc.php';
27 require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
31 
32 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
34 
35 
36 dol_syslog("Call Dolibarr webservices interfaces");
37 
38 $langs->load("main");
39 
40 // Enable and test if module web services is enabled
41 if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
42 {
43  $langs->load("admin");
44  dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
45  print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
46  print $langs->trans("ToActivateModule");
47  exit;
48 }
49 
50 // Create the soap Object
51 $server = new nusoap_server();
52 $server->soap_defencoding = 'UTF-8';
53 $server->decode_utf8 = false;
54 $ns = 'http://www.dolibarr.org/ns/';
55 $server->configureWSDL('WebServicesDolibarrInvoice', $ns);
56 $server->wsdl->schemaTargetNamespace = $ns;
57 
58 
59 // Define WSDL Authentication object
60 $server->wsdl->addComplexType(
61  'authentication',
62  'complexType',
63  'struct',
64  'all',
65  '',
66  array(
67  'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
68  'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
69  'login' => array('name'=>'login', 'type'=>'xsd:string'),
70  'password' => array('name'=>'password', 'type'=>'xsd:string'),
71  'entity' => array('name'=>'entity', 'type'=>'xsd:string')
72  )
73 );
74 // Define WSDL Return object
75 $server->wsdl->addComplexType(
76  'result',
77  'complexType',
78  'struct',
79  'all',
80  '',
81  array(
82  'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
83  'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
84  )
85 );
86 
87 // Define other specific objects
88 $server->wsdl->addComplexType(
89  'line',
90  'complexType',
91  'struct',
92  'all',
93  '',
94  array(
95  'id' => array('name'=>'id', 'type'=>'xsd:string'),
96  'type' => array('name'=>'type', 'type'=>'xsd:int'),
97  'desc' => array('name'=>'desc', 'type'=>'xsd:string'),
98  'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:double'),
99  'qty' => array('name'=>'qty', 'type'=>'xsd:double'),
100  'unitprice' => array('name'=>'unitprice', 'type'=>'xsd:double'),
101  'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
102  'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
103  'total' => array('name'=>'total', 'type'=>'xsd:double'),
104  'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
105  'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
106  // From product
107  'product_id' => array('name'=>'product_id', 'type'=>'xsd:int'),
108  'product_ref' => array('name'=>'product_ref', 'type'=>'xsd:string'),
109  'product_label' => array('name'=>'product_label', 'type'=>'xsd:string'),
110  'product_desc' => array('name'=>'product_desc', 'type'=>'xsd:string')
111  )
112 );
113 
114 /*$server->wsdl->addComplexType(
115  'LinesArray',
116  'complexType',
117  'array',
118  '',
119  'SOAP-ENC:Array',
120  array(),
121  array(
122  array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:line[]')
123  ),
124  'tns:line'
125 );*/
126 $server->wsdl->addComplexType(
127  'LinesArray2',
128  'complexType',
129  'array',
130  'sequence',
131  '',
132  array(
133  'line' => array(
134  'name' => 'line',
135  'type' => 'tns:line',
136  'minOccurs' => '0',
137  'maxOccurs' => 'unbounded'
138  )
139  ),
140  null,
141  'tns:line'
142 );
143 
144 
145 $server->wsdl->addComplexType(
146  'invoice',
147  'complexType',
148  'struct',
149  'all',
150  '',
151  array(
152  'id' => array('name'=>'id', 'type'=>'xsd:string'),
153  'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
154  'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
155  'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
156  'fk_user_author' => array('name'=>'fk_user_author', 'type'=>'xsd:string'),
157  'fk_user_valid' => array('name'=>'fk_user_valid', 'type'=>'xsd:string'),
158  'date' => array('name'=>'date', 'type'=>'xsd:date'),
159  'date_due' => array('name'=>'date_due', 'type'=>'xsd:date'),
160  'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
161  'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
162  'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
163  'payment_mode_id' => array('name'=>'payment_mode_id', 'type'=>'xsd:string'),
164  'type' => array('name'=>'type', 'type'=>'xsd:int'),
165  'total_net' => array('name'=>'type', 'type'=>'xsd:double'),
166  'total_vat' => array('name'=>'type', 'type'=>'xsd:double'),
167  'total' => array('name'=>'type', 'type'=>'xsd:double'),
168  'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
169  'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
170  'status' => array('name'=>'status', 'type'=>'xsd:int'),
171  'close_code' => array('name'=>'close_code', 'type'=>'xsd:string'),
172  'close_note' => array('name'=>'close_note', 'type'=>'xsd:string'),
173  'project_id' => array('name'=>'project_id', 'type'=>'xsd:string'),
174  'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray2')
175  )
176 );
177 /*
178 $server->wsdl->addComplexType(
179  'InvoicesArray',
180  'complexType',
181  'array',
182  '',
183  'SOAP-ENC:Array',
184  array(),
185  array(
186  array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:invoice[]')
187  ),
188  'tns:invoice'
189 );*/
190 $server->wsdl->addComplexType(
191  'InvoicesArray2',
192  'complexType',
193  'array',
194  'sequence',
195  '',
196  array(
197  'invoice' => array(
198  'name' => 'invoice',
199  'type' => 'tns:invoice',
200  'minOccurs' => '0',
201  'maxOccurs' => 'unbounded'
202  )
203  ),
204  null,
205  'tns:invoice'
206 );
207 
208 
209 
210 // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
211 // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
212 // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
213 $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
214 $styleuse = 'encoded'; // encoded/literal/literal wrapped
215 // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
216 
217 // Register WSDL
218 $server->register(
219  'getInvoice',
220  // Entry values
221  array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
222  // Exit values
223  array('result'=>'tns:result', 'invoice'=>'tns:invoice'),
224  $ns,
225  $ns.'#getInvoice',
226  $styledoc,
227  $styleuse,
228  'WS to get a particular invoice'
229 );
230 $server->register(
231  'getInvoicesForThirdParty',
232  // Entry values
233  array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'),
234  // Exit values
235  array('result'=>'tns:result', 'invoices'=>'tns:InvoicesArray2'),
236  $ns,
237  $ns.'#getInvoicesForThirdParty',
238  $styledoc,
239  $styleuse,
240  'WS to get all invoices of a third party'
241 );
242 $server->register(
243  'createInvoice',
244  // Entry values
245  array('authentication'=>'tns:authentication', 'invoice'=>'tns:invoice'),
246  // Exit values
247  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
248  $ns,
249  $ns.'#createInvoice',
250  $styledoc,
251  $styleuse,
252  'WS to create an invoice'
253 );
254 $server->register(
255  'createInvoiceFromOrder',
256  // Entry values
257  array('authentication'=>'tns:authentication', 'id_order'=>'xsd:string', 'ref_order'=>'xsd:string', 'ref_ext_order'=>'xsd:string'),
258  // Exit values
259  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
260  $ns,
261  $ns.'#createInvoiceFromOrder',
262  $styledoc,
263  $styleuse,
264  'WS to create an invoice from an order'
265 );
266 $server->register(
267  'updateInvoice',
268  // Entry values
269  array('authentication'=>'tns:authentication', 'invoice'=>'tns:invoice'),
270  // Exit values
271  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
272  $ns,
273  $ns.'#updateInvoice',
274  $styledoc,
275  $styleuse,
276  'WS to update an invoice'
277 );
278 
279 
289 function getInvoice($authentication, $id = '', $ref = '', $ref_ext = '')
290 {
291  global $db, $conf;
292 
293  dol_syslog("Function: getInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
294 
295  if ($authentication['entity']) $conf->entity = $authentication['entity'];
296 
297  // Init and check authentication
298  $objectresp = array();
299  $errorcode = ''; $errorlabel = '';
300  $error = 0;
301  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
302  // Check parameters
303  if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
304  {
305  $error++;
306  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
307  }
308 
309  if (!$error)
310  {
311  $fuser->getrights();
312 
313  if ($fuser->rights->facture->lire)
314  {
315  $invoice = new Facture($db);
316  $result = $invoice->fetch($id, $ref, $ref_ext);
317  if ($result > 0)
318  {
319  $linesresp = array();
320  $i = 0;
321  foreach ($invoice->lines as $line)
322  {
323  //var_dump($line); exit;
324  $linesresp[] = array(
325  'id'=>$line->id,
326  'type'=>$line->product_type,
327  'desc'=>dol_htmlcleanlastbr($line->desc),
328  'total_net'=>$line->total_ht,
329  'total_vat'=>$line->total_tva,
330  'total'=>$line->total_ttc,
331  'vat_rate'=>$line->tva_tx,
332  'qty'=>$line->qty,
333  'unitprice'=> $line->subprice,
334  'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
335  'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
336  'product_id'=>$line->fk_product,
337  'product_ref'=>$line->product_ref,
338  'product_label'=>$line->product_label,
339  'product_desc'=>$line->product_desc,
340  );
341  $i++;
342  }
343 
344  // Create invoice
345  $objectresp = array(
346  'result'=>array('result_code'=>'OK', 'result_label'=>''),
347  'invoice'=>array(
348  'id' => $invoice->id,
349  'ref' => $invoice->ref,
350  'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
351  'thirdparty_id' => $invoice->socid,
352  'fk_user_author' => $invoice->user_author ? $invoice->user_author : '',
353  'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid : '',
354  'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
355  'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
356  'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
357  'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
358  'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
359  'type' => $invoice->type,
360  'total_net' => $invoice->total_ht,
361  'total_vat' => $invoice->total_tva,
362  'total' => $invoice->total_ttc,
363  'note_private' => $invoice->note_private ? $invoice->note_private : '',
364  'note_public' => $invoice->note_public ? $invoice->note_public : '',
365  'status' => $invoice->statut,
366  'project_id' => $invoice->fk_project,
367  'close_code' => $invoice->close_code ? $invoice->close_code : '',
368  'close_note' => $invoice->close_note ? $invoice->close_note : '',
369  'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
370  'lines' => $linesresp
371  ));
372  }
373  else {
374  $error++;
375  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
376  }
377  }
378  else {
379  $error++;
380  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
381  }
382  }
383 
384  if ($error)
385  {
386  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
387  }
388 
389  return $objectresp;
390 }
391 
392 
400 function getInvoicesForThirdParty($authentication, $idthirdparty)
401 {
402  global $db, $conf;
403 
404  dol_syslog("Function: getInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
405 
406  if ($authentication['entity']) $conf->entity = $authentication['entity'];
407 
408  // Init and check authentication
409  $objectresp = array();
410  $errorcode = ''; $errorlabel = '';
411  $error = 0;
412  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
413 
414  if ($fuser->socid) $socid = $fuser->socid;
415 
416  // Check parameters
417  if (!$error && empty($idthirdparty))
418  {
419  $error++;
420  $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter idthirdparty is not provided';
421  }
422 
423  if (!$error)
424  {
425  $linesinvoice = array();
426 
427  $sql = 'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
428  $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f';
429  $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
430  if ($idthirdparty != 'all') $sql .= " AND f.fk_soc = ".$db->escape($idthirdparty);
431 
432  $resql = $db->query($sql);
433  if ($resql)
434  {
435  $num = $db->num_rows($resql);
436  $i = 0;
437  while ($i < $num)
438  {
439  // En attendant remplissage par boucle
440  $obj = $db->fetch_object($resql);
441 
442  $invoice = new Facture($db);
443  $invoice->fetch($obj->facid);
444 
445  // Sécurité pour utilisateur externe
446  if ($socid && ($socid != $invoice->socid))
447  {
448  $error++;
449  $errorcode = 'PERMISSION_DENIED'; $errorlabel = $invoice->socid.' User does not have permission for this request';
450  }
451 
452  if (!$error)
453  {
454  // Define lines of invoice
455  $linesresp = array();
456  foreach ($invoice->lines as $line)
457  {
458  $linesresp[] = array(
459  'id'=>$line->id,
460  'type'=>$line->product_type,
461  'total_net'=>$line->total_ht,
462  'total_vat'=>$line->total_tva,
463  'total'=>$line->total_ttc,
464  'vat_rate'=>$line->tva_tx,
465  'qty'=>$line->qty,
466  'unitprice'=> $line->subprice,
467  'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
468  'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
469  'product_id'=>$line->fk_product,
470  'product_ref'=>$line->product_ref,
471  'product_label'=>$line->product_label,
472  'product_desc'=>$line->product_desc,
473  );
474  }
475 
476  // Now define invoice
477  $linesinvoice[] = array(
478  'id' => $invoice->id,
479  'ref' => $invoice->ref,
480  'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
481  'fk_user_author' => $invoice->user_author ? $invoice->user_author : '',
482  'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid : '',
483  'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
484  'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
485  'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
486  'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
487  'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
488  'type' => $invoice->type,
489  'total_net' => $invoice->total_ht,
490  'total_vat' => $invoice->total_tva,
491  'total' => $invoice->total_ttc,
492  'note_private' => $invoice->note_private ? $invoice->note_private : '',
493  'note_public' => $invoice->note_public ? $invoice->note_public : '',
494  'status'=> $invoice->statut,
495  'project_id' => $invoice->fk_project,
496  'close_code' => $invoice->close_code ? $invoice->close_code : '',
497  'close_note' => $invoice->close_note ? $invoice->close_note : '',
498  'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
499  'lines' => $linesresp
500  );
501  }
502 
503  $i++;
504  }
505 
506  $objectresp = array(
507  'result'=>array('result_code'=>'OK', 'result_label'=>''),
508  'invoices'=>$linesinvoice
509 
510  );
511  }
512  else {
513  $error++;
514  $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
515  }
516  }
517 
518  if ($error)
519  {
520  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
521  }
522 
523  return $objectresp;
524 }
525 
526 
534 function createInvoice($authentication, $invoice)
535 {
536  global $db, $conf;
537 
538  $now = dol_now();
539 
540  dol_syslog("Function: createInvoice login=".$authentication['login']." id=".$invoice['id'].", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
541 
542  if ($authentication['entity']) $conf->entity = $authentication['entity'];
543 
544  // Init and check authentication
545  $objectresp = array();
546  $errorcode = ''; $errorlabel = '';
547  $error = 0;
548  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
549 
550  // Check parameters
551  if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
552  $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
553  }
554 
555  if (!$error)
556  {
557  $new_invoice = new Facture($db);
558  $new_invoice->socid = $invoice['thirdparty_id'];
559  $new_invoice->type = $invoice['type'];
560  $new_invoice->ref_ext = $invoice['ref_ext'];
561  $new_invoice->date = dol_stringtotime($invoice['date'], 'dayrfc');
562  $new_invoice->note_private = $invoice['note_private'];
563  $new_invoice->note_public = $invoice['note_public'];
564  $new_invoice->statut = Facture::STATUS_DRAFT; // We start with status draft
565  $new_invoice->fk_project = $invoice['project_id'];
566  $new_invoice->date_creation = $now;
567 
568  //take mode_reglement and cond_reglement from thirdparty
569  $soc = new Societe($db);
570  $res = $soc->fetch($new_invoice->socid);
571  if ($res > 0) {
572  $new_invoice->mode_reglement_id = !empty($invoice['payment_mode_id']) ? $invoice['payment_mode_id'] : $soc->mode_reglement_id;
573  $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
574  }
575  else $new_invoice->mode_reglement_id = $invoice['payment_mode_id'];
576 
577  // Trick because nusoap does not store data with same structure if there is one or several lines
578  $arrayoflines = array();
579  if (isset($invoice['lines']['line'][0])) $arrayoflines = $invoice['lines']['line'];
580  else $arrayoflines = $invoice['lines'];
581 
582  foreach ($arrayoflines as $line)
583  {
584  // $key can be 'line' or '0','1',...
585  $newline = new FactureLigne($db);
586  $newline->product_type = $line['type'];
587  $newline->desc = $line['desc'];
588  $newline->fk_product = $line['product_id'];
589  $newline->tva_tx = isset($line['vat_rate']) ? $line['vat_rate'] : 0;
590  $newline->qty = $line['qty'];
591  $newline->subprice = isset($line['unitprice']) ? $line['unitprice'] : null;
592  $newline->total_ht = $line['total_net'];
593  $newline->total_tva = $line['total_vat'];
594  $newline->total_ttc = $line['total'];
595  $newline->date_start = dol_stringtotime($line['date_start']);
596  $newline->date_end = dol_stringtotime($line['date_end']);
597 
598  $new_invoice->lines[] = $newline;
599  }
600  //var_dump($newobject->date_lim_reglement); exit;
601  //var_dump($invoice['lines'][0]['type']);
602 
603  $db->begin();
604 
605  $result = $new_invoice->create($fuser, 0, dol_stringtotime($invoice['date_due'], 'dayrfc'));
606  if ($result < 0)
607  {
608  $error++;
609  }
610 
611  if (!$error && $invoice['status'] == Facture::STATUS_VALIDATED) // We want invoice to have status validated
612  {
613  $result = $new_invoice->validate($fuser);
614  if ($result < 0)
615  {
616  $error++;
617  }
618  }
619 
620  if (!$error)
621  {
622  $db->commit();
623  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$new_invoice->id,
624  'ref'=>$new_invoice->ref, 'ref_ext'=>$new_invoice->ref_ext);
625  }
626  else {
627  $db->rollback();
628  $error++;
629  $errorcode = 'KO';
630  $errorlabel = $new_invoice->error;
631  dol_syslog("Function: createInvoice error while creating".$errorlabel);
632  }
633  }
634 
635  if ($error)
636  {
637  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
638  }
639 
640  return $objectresp;
641 }
642 
652 function createInvoiceFromOrder($authentication, $id_order = '', $ref_order = '', $ref_ext_order = '')
653 {
654  global $db, $conf;
655 
656  dol_syslog("Function: createInvoiceFromOrder login=".$authentication['login']." id=".$id_order.", ref=".$ref_order.", ref_ext=".$ref_ext_order);
657 
658  if ($authentication['entity']) $conf->entity = $authentication['entity'];
659 
660  // Init and check authentication
661  $objectresp = array();
662  $errorcode = ''; $errorlabel = '';
663  $error = 0;
664  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
665  if ($fuser->socid) $socid = $fuser->socid;
666 
667  // Check parameters
668  if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
669  $error++; $errorcode = 'KO'; $errorlabel = "order id or ref or ref_ext is mandatory.";
670  }
671 
673  if (!$error)
674  {
675  $fuser->getrights();
676 
677  if ($fuser->rights->commande->lire)
678  {
679  $order = new Commande($db);
680  $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
681  if ($result > 0)
682  {
683  // Security for external user
684  if ($socid && ($socid != $order->socid))
685  {
686  $error++;
687  $errorcode = 'PERMISSION_DENIED'; $errorlabel = $order->socid.'User does not have permission for this request';
688  }
689 
690  if (!$error)
691  {
692  $newobject = new Facture($db);
693  $result = $newobject->createFromOrder($order, $fuser);
694 
695  if ($result < 0)
696  {
697  $error++;
698  dol_syslog("Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
699  }
700  }
701  }
702  else {
703  $error++;
704  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id_order.' nor ref='.$ref_order.' nor ref_ext='.$ref_ext_order;
705  }
706  }
707  else {
708  $error++;
709  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
710  }
711  }
712 
713  if ($error)
714  {
715  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
716  }
717  else {
718  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref, 'ref_ext'=>$newobject->ref_ext);
719  }
720 
721  return $objectresp;
722 }
723 
731 function updateInvoice($authentication, $invoice)
732 {
733  global $db, $conf, $langs;
734 
735  dol_syslog("Function: updateInvoice login=".$authentication['login']." id=".$invoice['id'].
736  ", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
737 
738  if ($authentication['entity']) $conf->entity = $authentication['entity'];
739 
740  // Init and check authentication
741  $objectresp = array();
742  $errorcode = ''; $errorlabel = '';
743  $error = 0;
744  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
745 
746  // Check parameters
747  if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
748  $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
749  }
750 
751  if (!$error)
752  {
753  $objectfound = false;
754 
755  $object = new Facture($db);
756  $result = $object->fetch($invoice['id'], $invoice['ref'], $invoice['ref_ext'], '');
757 
758  if (!empty($object->id)) {
759  $objectfound = true;
760 
761  $db->begin();
762 
763  if (isset($invoice['status']))
764  {
765  if ($invoice['status'] == Facture::STATUS_DRAFT)
766  {
767  $result = $object->setDraft($fuser);
768  }
769  if ($invoice['status'] == Facture::STATUS_VALIDATED)
770  {
771  $result = $object->validate($fuser);
772 
773  if ($result >= 0)
774  {
775  // Define output language
776  $outputlangs = $langs;
777  $object->generateDocument($object->model_pdf, $outputlangs);
778  }
779  }
780  if ($invoice['status'] == Facture::STATUS_CLOSED) {
781  $result = $object->set_paid($fuser, $invoice['close_code'], $invoice['close_note']);
782  }
783  if ($invoice['status'] == Facture::STATUS_ABANDONED)
784  $result = $object->set_canceled($fuser, $invoice['close_code'], $invoice['close_note']);
785  }
786  }
787 
788  if ((!$error) && ($objectfound))
789  {
790  $db->commit();
791  $objectresp = array(
792  'result'=>array('result_code'=>'OK', 'result_label'=>''),
793  'id'=>$object->id,
794  'ref'=>$object->ref,
795  'ref_ext'=>$object->ref_ext
796  );
797  }
798  elseif ($objectfound)
799  {
800  $db->rollback();
801  $error++;
802  $errorcode = 'KO';
803  $errorlabel = $object->error;
804  } else {
805  $error++;
806  $errorcode = 'NOT_FOUND';
807  $errorlabel = 'Invoice id='.$invoice['id'].' ref='.$invoice['ref'].' ref_ext='.$invoice['ref_ext'].' cannot be found';
808  }
809  }
810 
811  if ($error)
812  {
813  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
814  }
815 
816  return $objectresp;
817 }
818 
819 // Return the results.
820 $server->service(file_get_contents("php://input"));
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition: ws.lib.php:35
dol_now($mode= 'auto')
Return date for now.
createInvoice($authentication, $invoice)
Create an invoice.
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
createInvoiceFromOrder($authentication, $id_order= '', $ref_order= '', $ref_ext_order= '')
Create an invoice from an order.
Class to manage third parties objects (customers, suppliers, prospects...)
updateInvoice($authentication, $invoice)
Uddate an invoice, only change the state of an invoice.
const STATUS_VALIDATED
Validated (need to be paid)
Class to manage customers orders.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
const STATUS_DRAFT
Draft status.
const STATUS_CLOSED
Classified paid.
const STATUS_ABANDONED
Classified abandoned and no payment done.
getInvoicesForThirdParty($authentication, $idthirdparty)
Get list of invoices for third party.
print
Draft customers invoices.
Definition: index.php:89
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_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Class to manage invoices.
Class to manage invoice lines.
getInvoice($authentication, $id= '', $ref= '', $ref_ext= '')
Get invoice from id, ref or ref_ext.