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");
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 = 0, $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();
325 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
327 $errorcode =
'BAD_PARAMETERS';
328 $errorlabel =
"Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
334 if ($fuser->hasRight(
'facture',
'lire')) {
336 $result = $invoice->fetch($id, $ref, $ref_ext);
338 $linesresp = array();
340 foreach ($invoice->lines as $line) {
342 $linesresp[] = array(
344 'type'=>$line->product_type,
346 'total_net'=>$line->total_ht,
347 'total_vat'=>$line->total_tva,
348 'total'=>$line->total_ttc,
349 'vat_rate'=>$line->tva_tx,
351 'unitprice'=> $line->subprice,
352 'date_start'=> $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
353 'date_end'=> $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
354 'product_id'=>$line->fk_product,
355 'product_ref'=>$line->product_ref,
356 'product_label'=>$line->product_label,
357 'product_desc'=>$line->product_desc,
364 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
366 'id' => $invoice->id,
367 'ref' => $invoice->ref,
368 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
369 'thirdparty_id' => $invoice->socid,
370 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author :
'',
371 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id :
'',
372 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
373 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
374 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
375 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
376 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
377 'type' => $invoice->type,
378 'total_net' => $invoice->total_ht,
379 'total_vat' => $invoice->total_tva,
380 'total' => $invoice->total_ttc,
381 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
382 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
383 'status' => $invoice->statut,
384 'project_id' => $invoice->fk_project,
385 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
386 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
387 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
388 'lines' => $linesresp
392 $errorcode =
'NOT_FOUND';
393 $errorlabel =
'Object not found for id='.$id.
' nor ref='.$ref.
' nor ref_ext='.$ref_ext;
397 $errorcode =
'PERMISSION_DENIED';
398 $errorlabel =
'User does not have permission for this request';
403 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
421 dol_syslog(
"Function: getInvoicesForThirdParty login=".$authentication[
'login'].
" idthirdparty=".$idthirdparty);
423 if ($authentication[
'entity']) {
424 $conf->entity = $authentication[
'entity'];
428 $objectresp = array();
435 $socid = $fuser->socid;
439 if (!$error && empty($idthirdparty)) {
441 $errorcode =
'BAD_PARAMETERS';
442 $errorlabel =
'Parameter idthirdparty is not provided';
446 $linesinvoice = array();
448 $sql =
'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
449 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
450 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
451 if ($idthirdparty !=
'all') {
452 $sql .=
" AND f.fk_soc = ".((int) $idthirdparty);
455 $resql = $db->query($sql);
457 $num = $db->num_rows($resql);
461 $obj = $db->fetch_object($resql);
464 $invoice->fetch($obj->facid);
467 if ($socid && ($socid != $invoice->socid)) {
469 $errorcode =
'PERMISSION_DENIED';
470 $errorlabel = $invoice->socid.
' User does not have permission for this request';
475 $linesresp = array();
476 foreach ($invoice->lines as $line) {
477 $linesresp[] = array(
479 'type'=>$line->product_type,
480 'total_net'=>$line->total_ht,
481 'total_vat'=>$line->total_tva,
482 'total'=>$line->total_ttc,
483 'vat_rate'=>$line->tva_tx,
485 'unitprice'=> $line->subprice,
486 'date_start'=> $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
487 'date_end'=> $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
488 'product_id'=>$line->fk_product,
489 'product_ref'=>$line->product_ref,
490 'product_label'=>$line->product_label,
491 'product_desc'=>$line->product_desc,
496 $linesinvoice[] = array(
497 'id' => $invoice->id,
498 'ref' => $invoice->ref,
499 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
500 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author :
'',
501 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id :
'',
502 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
503 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
504 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
505 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
506 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
507 'type' => $invoice->type,
508 'total_net' => $invoice->total_ht,
509 'total_vat' => $invoice->total_tva,
510 'total' => $invoice->total_ttc,
511 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
512 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
513 'status'=> $invoice->statut,
514 'project_id' => $invoice->fk_project,
515 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
516 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
517 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
518 'lines' => $linesresp
526 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
527 'invoices'=>$linesinvoice
532 $errorcode = $db->lasterrno();
533 $errorlabel = $db->lasterror();
538 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
558 dol_syslog(
"Function: createInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
560 if ($authentication[
'entity']) {
561 $conf->entity = $authentication[
'entity'];
565 $objectresp = array();
572 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
575 $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
579 $new_invoice =
new Facture($db);
580 $new_invoice->socid = $invoice[
'thirdparty_id'];
581 $new_invoice->type = $invoice[
'type'];
582 $new_invoice->ref_ext = $invoice[
'ref_ext'];
584 $new_invoice->note_private = $invoice[
'note_private'];
585 $new_invoice->note_public = $invoice[
'note_public'];
587 $new_invoice->fk_project = $invoice[
'project_id'];
588 $new_invoice->date_creation = $now;
592 $res = $soc->fetch($new_invoice->socid);
594 $new_invoice->mode_reglement_id = !empty($invoice[
'payment_mode_id']) ? $invoice[
'payment_mode_id'] : $soc->mode_reglement_id;
595 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
597 $new_invoice->mode_reglement_id = $invoice[
'payment_mode_id'];
601 $arrayoflines = array();
602 if (isset($invoice[
'lines'][
'line'][0])) {
603 $arrayoflines = $invoice[
'lines'][
'line'];
605 $arrayoflines = $invoice[
'lines'];
608 foreach ($arrayoflines as $line) {
611 $newline->product_type = $line[
'type'];
612 $newline->desc = $line[
'desc'];
613 $newline->fk_product = $line[
'product_id'];
614 $newline->tva_tx = isset($line[
'vat_rate']) ? $line[
'vat_rate'] : 0;
615 $newline->qty = $line[
'qty'];
616 $newline->subprice = isset($line[
'unitprice']) ? $line[
'unitprice'] :
null;
617 $newline->total_ht = $line[
'total_net'];
618 $newline->total_tva = $line[
'total_vat'];
619 $newline->total_ttc = $line[
'total'];
623 $new_invoice->lines[] = $newline;
630 $result = $new_invoice->create($fuser, 0,
dol_stringtotime($invoice[
'date_due'],
'dayrfc'));
636 $result = $new_invoice->validate($fuser);
644 $objectresp = array(
'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
'id'=>$new_invoice->id,
645 'ref'=>$new_invoice->ref,
'ref_ext'=>$new_invoice->ref_ext);
650 $errorlabel = $new_invoice->error;
651 dol_syslog(
"Function: createInvoice error while creating".$errorlabel);
656 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
675 dol_syslog(
"Function: createInvoiceFromOrder login=".$authentication[
'login'].
" id=".$id_order.
", ref=".$ref_order.
", ref_ext=".$ref_ext_order);
677 if ($authentication[
'entity']) {
678 $conf->entity = $authentication[
'entity'];
682 $objectresp = array();
688 $socid = $fuser->socid;
692 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
695 $errorlabel =
"order id or ref or ref_ext is mandatory.";
702 if ($fuser->hasRight(
'commande',
'lire')) {
704 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
707 if ($socid && ($socid != $order->socid)) {
709 $errorcode =
'PERMISSION_DENIED';
710 $errorlabel = $order->socid.
'User does not have permission for this request';
715 $result = $newobject->createFromOrder($order, $fuser);
719 dol_syslog(
"Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
724 $errorcode =
'NOT_FOUND';
725 $errorlabel =
'Object not found for id='.$id_order.
' nor ref='.$ref_order.
' nor ref_ext='.$ref_ext_order;
729 $errorcode =
'PERMISSION_DENIED';
730 $errorlabel =
'User does not have permission for this request';
735 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
737 $objectresp = array(
'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
'id'=>$newobject->id,
'ref'=>$newobject->ref,
'ref_ext'=>$newobject->ref_ext);
752 global $db, $conf, $langs;
754 dol_syslog(
"Function: updateInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
755 ", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
757 if ($authentication[
'entity']) {
758 $conf->entity = $authentication[
'entity'];
762 $objectresp = array();
769 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
772 $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
776 $objectfound =
false;
779 $result = $object->fetch($invoice[
'id'], $invoice[
'ref'], $invoice[
'ref_ext'],
'');
781 if (!empty($object->id)) {
786 if (isset($invoice[
'status'])) {
788 $result = $object->setDraft($fuser);
791 $result = $object->validate($fuser);
795 $outputlangs = $langs;
796 $object->generateDocument($object->model_pdf, $outputlangs);
800 $result = $object->setPaid($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
803 $result = $object->setCanceled($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
808 if ((!$error) && ($objectfound)) {
811 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
814 'ref_ext'=>$object->ref_ext
816 } elseif ($objectfound) {
820 $errorlabel = $object->error;
823 $errorcode =
'NOT_FOUND';
824 $errorlabel =
'Invoice id='.$invoice[
'id'].
' ref='.$invoice[
'ref'].
' ref_ext='.$invoice[
'ref_ext'].
' cannot be found';
829 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
836$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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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.
getInvoice($authentication, $id=0, $ref='', $ref_ext='')
Get invoice from id, ref or ref_ext.
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.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.