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