24if (!defined(
'NOCSRFCHECK')) {
25 define(
'NOCSRFCHECK',
'1');
27if (!defined(
'NOTOKENRENEWAL')) {
28 define(
'NOTOKENRENEWAL',
'1');
30if (!defined(
'NOREQUIREMENU')) {
31 define(
'NOREQUIREMENU',
'1');
33if (!defined(
'NOREQUIREHTML')) {
34 define(
'NOREQUIREHTML',
'1');
36if (!defined(
'NOREQUIREAJAX')) {
37 define(
'NOREQUIREAJAX',
'1');
39if (!defined(
"NOLOGIN")) {
40 define(
"NOLOGIN",
'1');
42if (!defined(
"NOSESSION")) {
43 define(
"NOSESSION",
'1');
46require
'../main.inc.php';
47require_once NUSOAP_PATH.
'/nusoap.php';
48require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
49require_once DOL_DOCUMENT_ROOT.
'/core/lib/ws.lib.php';
50require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
52require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
53require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
56dol_syslog(
"Call Dolibarr webservices interfaces");
61if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
62 $langs->load(
"admin");
63 dol_syslog(
"Call Dolibarr webservices interfaces with module webservices disabled");
64 print $langs->trans(
"WarningModuleNotActive",
'WebServices').
'.<br><br>';
65 print $langs->trans(
"ToActivateModule");
70$server =
new nusoap_server();
71$server->soap_defencoding =
'UTF-8';
72$server->decode_utf8 =
false;
73$ns =
'http://www.dolibarr.org/ns/';
74$server->configureWSDL(
'WebServicesDolibarrInvoice', $ns);
75$server->wsdl->schemaTargetNamespace = $ns;
79$server->wsdl->addComplexType(
86 'dolibarrkey' => array(
'name'=>
'dolibarrkey',
'type'=>
'xsd:string'),
87 'sourceapplication' => array(
'name'=>
'sourceapplication',
'type'=>
'xsd:string'),
88 'login' => array(
'name'=>
'login',
'type'=>
'xsd:string'),
89 'password' => array(
'name'=>
'password',
'type'=>
'xsd:string'),
90 'entity' => array(
'name'=>
'entity',
'type'=>
'xsd:string')
94$server->wsdl->addComplexType(
101 'result_code' => array(
'name'=>
'result_code',
'type'=>
'xsd:string'),
102 'result_label' => array(
'name'=>
'result_label',
'type'=>
'xsd:string'),
107$server->wsdl->addComplexType(
114 'id' => array(
'name'=>
'id',
'type'=>
'xsd:string'),
115 'type' => array(
'name'=>
'type',
'type'=>
'xsd:int'),
116 'desc' => array(
'name'=>
'desc',
'type'=>
'xsd:string'),
117 'vat_rate' => array(
'name'=>
'vat_rate',
'type'=>
'xsd:double'),
118 'qty' => array(
'name'=>
'qty',
'type'=>
'xsd:double'),
119 'unitprice' => array(
'name'=>
'unitprice',
'type'=>
'xsd:double'),
120 'total_net' => array(
'name'=>
'total_net',
'type'=>
'xsd:double'),
121 'total_vat' => array(
'name'=>
'total_vat',
'type'=>
'xsd:double'),
122 'total' => array(
'name'=>
'total',
'type'=>
'xsd:double'),
123 'date_start' => array(
'name'=>
'date_start',
'type'=>
'xsd:date'),
124 'date_end' => array(
'name'=>
'date_end',
'type'=>
'xsd:date'),
126 'product_id' => array(
'name'=>
'product_id',
'type'=>
'xsd:int'),
127 'product_ref' => array(
'name'=>
'product_ref',
'type'=>
'xsd:string'),
128 'product_label' => array(
'name'=>
'product_label',
'type'=>
'xsd:string'),
129 'product_desc' => array(
'name'=>
'product_desc',
'type'=>
'xsd:string')
145$server->wsdl->addComplexType(
154 'type' =>
'tns:line',
156 'maxOccurs' =>
'unbounded'
164$server->wsdl->addComplexType(
171 'id' => array(
'name'=>
'id',
'type'=>
'xsd:string'),
172 'ref' => array(
'name'=>
'ref',
'type'=>
'xsd:string'),
173 'ref_ext' => array(
'name'=>
'ref_ext',
'type'=>
'xsd:string'),
174 'thirdparty_id' => array(
'name'=>
'thirdparty_id',
'type'=>
'xsd:int'),
175 'fk_user_author' => array(
'name'=>
'fk_user_author',
'type'=>
'xsd:string'),
176 'fk_user_valid' => array(
'name'=>
'fk_user_valid',
'type'=>
'xsd:string'),
177 'date' => array(
'name'=>
'date',
'type'=>
'xsd:date'),
178 'date_due' => array(
'name'=>
'date_due',
'type'=>
'xsd:date'),
179 'date_creation' => array(
'name'=>
'date_creation',
'type'=>
'xsd:dateTime'),
180 'date_validation' => array(
'name'=>
'date_validation',
'type'=>
'xsd:dateTime'),
181 'date_modification' => array(
'name'=>
'date_modification',
'type'=>
'xsd:dateTime'),
182 'payment_mode_id' => array(
'name'=>
'payment_mode_id',
'type'=>
'xsd:string'),
183 'type' => array(
'name'=>
'type',
'type'=>
'xsd:int'),
184 'total_net' => array(
'name'=>
'type',
'type'=>
'xsd:double'),
185 'total_vat' => array(
'name'=>
'type',
'type'=>
'xsd:double'),
186 'total' => array(
'name'=>
'type',
'type'=>
'xsd:double'),
187 'note_private' => array(
'name'=>
'note_private',
'type'=>
'xsd:string'),
188 'note_public' => array(
'name'=>
'note_public',
'type'=>
'xsd:string'),
189 'status' => array(
'name'=>
'status',
'type'=>
'xsd:int'),
190 'close_code' => array(
'name'=>
'close_code',
'type'=>
'xsd:string'),
191 'close_note' => array(
'name'=>
'close_note',
'type'=>
'xsd:string'),
192 'project_id' => array(
'name'=>
'project_id',
'type'=>
'xsd:string'),
193 'lines' => array(
'name'=>
'lines',
'type'=>
'tns:LinesArray2')
209$server->wsdl->addComplexType(
218 'type' =>
'tns:invoice',
220 'maxOccurs' =>
'unbounded'
233$styleuse =
'encoded';
240 array(
'authentication'=>
'tns:authentication',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
242 array(
'result'=>
'tns:result',
'invoice'=>
'tns:invoice'),
247 'WS to get a particular invoice'
250 'getInvoicesForThirdParty',
252 array(
'authentication'=>
'tns:authentication',
'idthirdparty'=>
'xsd:string'),
254 array(
'result'=>
'tns:result',
'invoices'=>
'tns:InvoicesArray2'),
256 $ns.
'#getInvoicesForThirdParty',
259 'WS to get all invoices of a third party'
264 array(
'authentication'=>
'tns:authentication',
'invoice'=>
'tns:invoice'),
266 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
268 $ns.
'#createInvoice',
271 'WS to create an invoice'
274 'createInvoiceFromOrder',
276 array(
'authentication'=>
'tns:authentication',
'id_order'=>
'xsd:string',
'ref_order'=>
'xsd:string',
'ref_ext_order'=>
'xsd:string'),
278 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
280 $ns.
'#createInvoiceFromOrder',
283 'WS to create an invoice from an order'
288 array(
'authentication'=>
'tns:authentication',
'invoice'=>
'tns:invoice'),
290 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
292 $ns.
'#updateInvoice',
295 'WS to update an invoice'
308function getInvoice($authentication, $id =
'', $ref =
'', $ref_ext =
'')
312 dol_syslog(
"Function: getInvoice login=".$authentication[
'login'].
" id=".$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
314 if ($authentication[
'entity']) {
315 $conf->entity = $authentication[
'entity'];
319 $objectresp = array();
320 $errorcode =
''; $errorlabel =
'';
324 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
326 $errorcode =
'BAD_PARAMETERS'; $errorlabel =
"Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
332 if ($fuser->rights->facture->lire) {
334 $result = $invoice->fetch($id, $ref, $ref_ext);
336 $linesresp = array();
338 foreach ($invoice->lines as $line) {
340 $linesresp[] = array(
342 'type'=>$line->product_type,
344 'total_net'=>$line->total_ht,
345 'total_vat'=>$line->total_tva,
346 'total'=>$line->total_ttc,
347 'vat_rate'=>$line->tva_tx,
349 'unitprice'=> $line->subprice,
350 'date_start'=> $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
351 'date_end'=> $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
352 'product_id'=>$line->fk_product,
353 'product_ref'=>$line->product_ref,
354 'product_label'=>$line->product_label,
355 'product_desc'=>$line->product_desc,
362 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
364 'id' => $invoice->id,
365 'ref' => $invoice->ref,
366 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
367 'thirdparty_id' => $invoice->socid,
368 'fk_user_author' => $invoice->user_author ? $invoice->user_author :
'',
369 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid :
'',
370 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
371 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
372 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
373 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
374 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
375 'type' => $invoice->type,
376 'total_net' => $invoice->total_ht,
377 'total_vat' => $invoice->total_tva,
378 'total' => $invoice->total_ttc,
379 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
380 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
381 'status' => $invoice->statut,
382 'project_id' => $invoice->fk_project,
383 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
384 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
385 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
386 'lines' => $linesresp
390 $errorcode =
'NOT_FOUND'; $errorlabel =
'Object not found for id='.$id.
' nor ref='.$ref.
' nor ref_ext='.$ref_ext;
394 $errorcode =
'PERMISSION_DENIED'; $errorlabel =
'User does not have permission for this request';
399 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
417 dol_syslog(
"Function: getInvoicesForThirdParty login=".$authentication[
'login'].
" idthirdparty=".$idthirdparty);
419 if ($authentication[
'entity']) {
420 $conf->entity = $authentication[
'entity'];
424 $objectresp = array();
425 $errorcode =
''; $errorlabel =
'';
430 $socid = $fuser->socid;
434 if (!$error && empty($idthirdparty)) {
436 $errorcode =
'BAD_PARAMETERS'; $errorlabel =
'Parameter idthirdparty is not provided';
440 $linesinvoice = array();
442 $sql =
'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
443 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
444 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
445 if ($idthirdparty !=
'all') {
446 $sql .=
" AND f.fk_soc = ".((int) $idthirdparty);
449 $resql = $db->query($sql);
451 $num = $db->num_rows($resql);
455 $obj = $db->fetch_object($resql);
458 $invoice->fetch($obj->facid);
461 if ($socid && ($socid != $invoice->socid)) {
463 $errorcode =
'PERMISSION_DENIED'; $errorlabel = $invoice->socid.
' User does not have permission for this request';
468 $linesresp = array();
469 foreach ($invoice->lines as $line) {
470 $linesresp[] = array(
472 'type'=>$line->product_type,
473 'total_net'=>$line->total_ht,
474 'total_vat'=>$line->total_tva,
475 'total'=>$line->total_ttc,
476 'vat_rate'=>$line->tva_tx,
478 'unitprice'=> $line->subprice,
479 'date_start'=> $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
480 'date_end'=> $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
481 'product_id'=>$line->fk_product,
482 'product_ref'=>$line->product_ref,
483 'product_label'=>$line->product_label,
484 'product_desc'=>$line->product_desc,
489 $linesinvoice[] = array(
490 'id' => $invoice->id,
491 'ref' => $invoice->ref,
492 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
493 'fk_user_author' => $invoice->user_author ? $invoice->user_author :
'',
494 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid :
'',
495 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
496 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
497 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
498 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
499 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
500 'type' => $invoice->type,
501 'total_net' => $invoice->total_ht,
502 'total_vat' => $invoice->total_tva,
503 'total' => $invoice->total_ttc,
504 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
505 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
506 'status'=> $invoice->statut,
507 'project_id' => $invoice->fk_project,
508 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
509 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
510 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
511 'lines' => $linesresp
519 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
520 'invoices'=>$linesinvoice
525 $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
530 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
550 dol_syslog(
"Function: createInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
552 if ($authentication[
'entity']) {
553 $conf->entity = $authentication[
'entity'];
557 $objectresp = array();
558 $errorcode =
''; $errorlabel =
'';
563 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
564 $error++; $errorcode =
'KO'; $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
568 $new_invoice =
new Facture($db);
569 $new_invoice->socid = $invoice[
'thirdparty_id'];
570 $new_invoice->type = $invoice[
'type'];
571 $new_invoice->ref_ext = $invoice[
'ref_ext'];
573 $new_invoice->note_private = $invoice[
'note_private'];
574 $new_invoice->note_public = $invoice[
'note_public'];
576 $new_invoice->fk_project = $invoice[
'project_id'];
577 $new_invoice->date_creation = $now;
581 $res = $soc->fetch($new_invoice->socid);
583 $new_invoice->mode_reglement_id = !empty($invoice[
'payment_mode_id']) ? $invoice[
'payment_mode_id'] : $soc->mode_reglement_id;
584 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
586 $new_invoice->mode_reglement_id = $invoice[
'payment_mode_id'];
590 $arrayoflines = array();
591 if (isset($invoice[
'lines'][
'line'][0])) {
592 $arrayoflines = $invoice[
'lines'][
'line'];
594 $arrayoflines = $invoice[
'lines'];
597 foreach ($arrayoflines as $line) {
600 $newline->product_type = $line[
'type'];
601 $newline->desc = $line[
'desc'];
602 $newline->fk_product = $line[
'product_id'];
603 $newline->tva_tx = isset($line[
'vat_rate']) ? $line[
'vat_rate'] : 0;
604 $newline->qty = $line[
'qty'];
605 $newline->subprice = isset($line[
'unitprice']) ? $line[
'unitprice'] :
null;
606 $newline->total_ht = $line[
'total_net'];
607 $newline->total_tva = $line[
'total_vat'];
608 $newline->total_ttc = $line[
'total'];
612 $new_invoice->lines[] = $newline;
619 $result = $new_invoice->create($fuser, 0,
dol_stringtotime($invoice[
'date_due'],
'dayrfc'));
625 $result = $new_invoice->validate($fuser);
633 $objectresp = array(
'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
'id'=>$new_invoice->id,
634 'ref'=>$new_invoice->ref,
'ref_ext'=>$new_invoice->ref_ext);
639 $errorlabel = $new_invoice->error;
640 dol_syslog(
"Function: createInvoice error while creating".$errorlabel);
645 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
664 dol_syslog(
"Function: createInvoiceFromOrder login=".$authentication[
'login'].
" id=".$id_order.
", ref=".$ref_order.
", ref_ext=".$ref_ext_order);
666 if ($authentication[
'entity']) {
667 $conf->entity = $authentication[
'entity'];
671 $objectresp = array();
672 $errorcode =
''; $errorlabel =
'';
676 $socid = $fuser->socid;
680 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
681 $error++; $errorcode =
'KO'; $errorlabel =
"order id or ref or ref_ext is mandatory.";
688 if ($fuser->rights->commande->lire) {
690 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
693 if ($socid && ($socid != $order->socid)) {
695 $errorcode =
'PERMISSION_DENIED'; $errorlabel = $order->socid.
'User does not have permission for this request';
700 $result = $newobject->createFromOrder($order, $fuser);
704 dol_syslog(
"Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
709 $errorcode =
'NOT_FOUND'; $errorlabel =
'Object not found for id='.$id_order.
' nor ref='.$ref_order.
' nor ref_ext='.$ref_ext_order;
713 $errorcode =
'PERMISSION_DENIED'; $errorlabel =
'User does not have permission for this request';
718 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
720 $objectresp = array(
'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
'id'=>$newobject->id,
'ref'=>$newobject->ref,
'ref_ext'=>$newobject->ref_ext);
735 global $db, $conf, $langs;
737 dol_syslog(
"Function: updateInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
738 ", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
740 if ($authentication[
'entity']) {
741 $conf->entity = $authentication[
'entity'];
745 $objectresp = array();
746 $errorcode =
''; $errorlabel =
'';
751 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
752 $error++; $errorcode =
'KO'; $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
756 $objectfound =
false;
759 $result = $object->fetch($invoice[
'id'], $invoice[
'ref'], $invoice[
'ref_ext'],
'');
761 if (!empty($object->id)) {
766 if (isset($invoice[
'status'])) {
768 $result = $object->setDraft($fuser);
771 $result = $object->validate($fuser);
775 $outputlangs = $langs;
776 $object->generateDocument($object->model_pdf, $outputlangs);
780 $result = $object->setPaid($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
783 $result = $object->setCanceled($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
788 if ((!$error) && ($objectfound)) {
791 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
794 'ref_ext'=>$object->ref_ext
796 } elseif ($objectfound) {
800 $errorlabel = $object->error;
803 $errorcode =
'NOT_FOUND';
804 $errorlabel =
'Invoice id='.$invoice[
'id'].
' ref='.$invoice[
'ref'].
' ref_ext='.$invoice[
'ref_ext'].
' cannot be found';
809 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
816$server->service(file_get_contents(
"php://input"));
Class to manage customers orders.
Class to manage invoices.
const STATUS_DRAFT
Draft status.
const STATUS_VALIDATED
Validated (need to be paid)
const STATUS_ABANDONED
Classified abandoned and no payment done.
const STATUS_CLOSED
Classified paid.
Class to manage invoice lines.
Class to manage third parties objects (customers, suppliers, prospects...)
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...
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_now($mode='auto')
Return date for now.
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
createInvoiceFromOrder($authentication, $id_order='', $ref_order='', $ref_ext_order='')
Create an invoice from an order.
createInvoice($authentication, $invoice)
Create an invoice.
getInvoicesForThirdParty($authentication, $idthirdparty)
Get list of invoices for third party.
updateInvoice($authentication, $invoice)
Uddate an invoice, only change the state of an invoice.
getInvoice($authentication, $id='', $ref='', $ref_ext='')
Get invoice from id, ref or ref_ext.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.