26if (!defined(
'NOCSRFCHECK')) {
27 define(
'NOCSRFCHECK',
'1');
29if (!defined(
'NOTOKENRENEWAL')) {
30 define(
'NOTOKENRENEWAL',
'1');
32if (!defined(
'NOREQUIREMENU')) {
33 define(
'NOREQUIREMENU',
'1');
35if (!defined(
'NOREQUIREHTML')) {
36 define(
'NOREQUIREHTML',
'1');
38if (!defined(
'NOREQUIREAJAX')) {
39 define(
'NOREQUIREAJAX',
'1');
41if (!defined(
"NOLOGIN")) {
42 define(
"NOLOGIN",
'1');
44if (!defined(
"NOSESSION")) {
45 define(
"NOSESSION",
'1');
48require
'../main.inc.php';
49require_once NUSOAP_PATH.
'/nusoap.php';
50require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
51require_once DOL_DOCUMENT_ROOT.
'/core/lib/ws.lib.php';
52require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
54require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
55require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
62dol_syslog(
"Call Dolibarr webservices interfaces");
68 $langs->load(
"admin");
69 dol_syslog(
"Call Dolibarr webservices interfaces with module webservices disabled");
70 print $langs->trans(
"WarningModuleNotActive",
'WebServices').
'.<br><br>';
71 print $langs->trans(
"ToActivateModule");
76$server =
new nusoap_server();
77$server->soap_defencoding =
'UTF-8';
78$server->decode_utf8 =
false;
79$ns =
'http://www.dolibarr.org/ns/';
80$server->configureWSDL(
'WebServicesDolibarrInvoice', $ns);
82$server->wsdl->schemaTargetNamespace = $ns;
86$server->wsdl->addComplexType(
93 'dolibarrkey' => array(
'name' =>
'dolibarrkey',
'type' =>
'xsd:string'),
94 'sourceapplication' => array(
'name' =>
'sourceapplication',
'type' =>
'xsd:string'),
95 'login' => array(
'name' =>
'login',
'type' =>
'xsd:string'),
96 'password' => array(
'name' =>
'password',
'type' =>
'xsd:string'),
97 'entity' => array(
'name' =>
'entity',
'type' =>
'xsd:string')
101$server->wsdl->addComplexType(
108 'result_code' => array(
'name' =>
'result_code',
'type' =>
'xsd:string'),
109 'result_label' => array(
'name' =>
'result_label',
'type' =>
'xsd:string'),
114$server->wsdl->addComplexType(
121 'id' => array(
'name' =>
'id',
'type' =>
'xsd:string'),
122 'type' => array(
'name' =>
'type',
'type' =>
'xsd:int'),
123 'desc' => array(
'name' =>
'desc',
'type' =>
'xsd:string'),
124 'vat_rate' => array(
'name' =>
'vat_rate',
'type' =>
'xsd:double'),
125 'qty' => array(
'name' =>
'qty',
'type' =>
'xsd:double'),
126 'unitprice' => array(
'name' =>
'unitprice',
'type' =>
'xsd:double'),
127 'total_net' => array(
'name' =>
'total_net',
'type' =>
'xsd:double'),
128 'total_vat' => array(
'name' =>
'total_vat',
'type' =>
'xsd:double'),
129 'total' => array(
'name' =>
'total',
'type' =>
'xsd:double'),
130 'date_start' => array(
'name' =>
'date_start',
'type' =>
'xsd:date'),
131 'date_end' => array(
'name' =>
'date_end',
'type' =>
'xsd:date'),
133 'product_id' => array(
'name' =>
'product_id',
'type' =>
'xsd:int'),
134 'product_ref' => array(
'name' =>
'product_ref',
'type' =>
'xsd:string'),
135 'product_label' => array(
'name' =>
'product_label',
'type' =>
'xsd:string'),
136 'product_desc' => array(
'name' =>
'product_desc',
'type' =>
'xsd:string')
152$server->wsdl->addComplexType(
161 'type' =>
'tns:line',
163 'maxOccurs' =>
'unbounded'
171$server->wsdl->addComplexType(
178 'id' => array(
'name' =>
'id',
'type' =>
'xsd:string'),
179 'ref' => array(
'name' =>
'ref',
'type' =>
'xsd:string'),
180 'ref_ext' => array(
'name' =>
'ref_ext',
'type' =>
'xsd:string'),
181 'thirdparty_id' => array(
'name' =>
'thirdparty_id',
'type' =>
'xsd:int'),
182 'fk_user_author' => array(
'name' =>
'fk_user_author',
'type' =>
'xsd:string'),
183 'fk_user_valid' => array(
'name' =>
'fk_user_valid',
'type' =>
'xsd:string'),
184 'date' => array(
'name' =>
'date',
'type' =>
'xsd:date'),
185 'date_due' => array(
'name' =>
'date_due',
'type' =>
'xsd:date'),
186 'date_creation' => array(
'name' =>
'date_creation',
'type' =>
'xsd:dateTime'),
187 'date_validation' => array(
'name' =>
'date_validation',
'type' =>
'xsd:dateTime'),
188 'date_modification' => array(
'name' =>
'date_modification',
'type' =>
'xsd:dateTime'),
189 'payment_mode_id' => array(
'name' =>
'payment_mode_id',
'type' =>
'xsd:string'),
190 'type' => array(
'name' =>
'type',
'type' =>
'xsd:int'),
191 'total_net' => array(
'name' =>
'type',
'type' =>
'xsd:double'),
192 'total_vat' => array(
'name' =>
'type',
'type' =>
'xsd:double'),
193 'total' => array(
'name' =>
'type',
'type' =>
'xsd:double'),
194 'note_private' => array(
'name' =>
'note_private',
'type' =>
'xsd:string'),
195 'note_public' => array(
'name' =>
'note_public',
'type' =>
'xsd:string'),
196 'status' => array(
'name' =>
'status',
'type' =>
'xsd:int'),
197 'close_code' => array(
'name' =>
'close_code',
'type' =>
'xsd:string'),
198 'close_note' => array(
'name' =>
'close_note',
'type' =>
'xsd:string'),
199 'project_id' => array(
'name' =>
'project_id',
'type' =>
'xsd:string'),
200 'lines' => array(
'name' =>
'lines',
'type' =>
'tns:LinesArray2')
216$server->wsdl->addComplexType(
225 'type' =>
'tns:invoice',
227 'maxOccurs' =>
'unbounded'
240$styleuse =
'encoded';
247 array(
'authentication' =>
'tns:authentication',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
249 array(
'result' =>
'tns:result',
'invoice' =>
'tns:invoice'),
254 'WS to get a particular invoice'
257 'getInvoicesForThirdParty',
259 array(
'authentication' =>
'tns:authentication',
'idthirdparty' =>
'xsd:string'),
261 array(
'result' =>
'tns:result',
'invoices' =>
'tns:InvoicesArray2'),
263 $ns.
'#getInvoicesForThirdParty',
266 'WS to get all invoices of a third party'
271 array(
'authentication' =>
'tns:authentication',
'invoice' =>
'tns:invoice'),
273 array(
'result' =>
'tns:result',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
275 $ns.
'#createInvoice',
278 'WS to create an invoice'
281 'createInvoiceFromOrder',
283 array(
'authentication' =>
'tns:authentication',
'id_order' =>
'xsd:string',
'ref_order' =>
'xsd:string',
'ref_ext_order' =>
'xsd:string'),
285 array(
'result' =>
'tns:result',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
287 $ns.
'#createInvoiceFromOrder',
290 'WS to create an invoice from an order'
295 array(
'authentication' =>
'tns:authentication',
'invoice' =>
'tns:invoice'),
297 array(
'result' =>
'tns:result',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
299 $ns.
'#updateInvoice',
302 'WS to update an invoice'
319 dol_syslog(
"Function: getInvoice login=".$authentication[
'login'].
" id=".
$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
321 if ($authentication[
'entity']) {
322 $conf->entity = $authentication[
'entity'];
326 $objectresp = array();
332 if (!$error && ((
$id && $ref) || (
$id && $ref_ext) || ($ref && $ref_ext))) {
334 $errorcode =
'BAD_PARAMETERS';
335 $errorlabel =
"Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
339 $fuser->loadRights();
341 if ($fuser->hasRight(
'facture',
'lire')) {
343 $result = $invoice->fetch(
$id, $ref, $ref_ext);
345 $linesresp = array();
347 foreach ($invoice->lines as $line) {
349 $linesresp[] = array(
351 'type' => $line->product_type,
353 'total_net' => $line->total_ht,
354 'total_vat' => $line->total_tva,
355 'total' => $line->total_ttc,
356 'vat_rate' => $line->tva_tx,
358 'unitprice' => $line->subprice,
359 'date_start' => $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
360 'date_end' => $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
361 'product_id' => $line->fk_product,
362 'product_ref' => $line->product_ref,
363 'product_label' => $line->product_label,
364 'product_desc' => $line->product_desc,
371 'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
373 'id' => $invoice->id,
374 'ref' => $invoice->ref,
375 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
376 'thirdparty_id' => $invoice->socid,
377 'fk_user_author' => $invoice->user_creation_id ? $invoice->user_creation_id :
'',
378 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id :
'',
379 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
380 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
381 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
382 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
383 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
384 'type' => $invoice->type,
385 'total_net' => $invoice->total_ht,
386 'total_vat' => $invoice->total_tva,
387 'total' => $invoice->total_ttc,
388 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
389 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
390 'status' => $invoice->status,
391 'project_id' => $invoice->fk_project,
392 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
393 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
394 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
395 'lines' => $linesresp
399 $errorcode =
'NOT_FOUND';
400 $errorlabel =
'Object not found for id='.$id.
' nor ref='.$ref.
' nor ref_ext='.$ref_ext;
404 $errorcode =
'PERMISSION_DENIED';
405 $errorlabel =
'User does not have permission for this request';
410 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
428 dol_syslog(
"Function: getInvoicesForThirdParty login=".$authentication[
'login'].
" idthirdparty=".$idthirdparty);
430 if ($authentication[
'entity']) {
431 $conf->entity = $authentication[
'entity'];
435 $objectresp = array();
443 $socid = $fuser->socid;
447 if (!$error && empty($idthirdparty)) {
449 $errorcode =
'BAD_PARAMETERS';
450 $errorlabel =
'Parameter idthirdparty is not provided';
454 $linesinvoice = array();
456 $sql =
'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
457 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
458 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
459 if ($idthirdparty !=
'all') {
460 $sql .=
" AND f.fk_soc = ".((int) $idthirdparty);
463 $resql =
$db->query($sql);
465 $num =
$db->num_rows($resql);
469 $obj =
$db->fetch_object($resql);
472 $invoice->fetch($obj->facid);
475 if ($socid && ($socid != $invoice->socid)) {
477 $errorcode =
'PERMISSION_DENIED';
478 $errorlabel = $invoice->socid.
' User does not have permission for this request';
483 $linesresp = array();
484 foreach ($invoice->lines as $line) {
485 $linesresp[] = array(
487 'type' => $line->product_type,
488 'total_net' => $line->total_ht,
489 'total_vat' => $line->total_tva,
490 'total' => $line->total_ttc,
491 'vat_rate' => $line->tva_tx,
493 'unitprice' => $line->subprice,
494 'date_start' => $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
495 'date_end' => $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
496 'product_id' => $line->fk_product,
497 'product_ref' => $line->product_ref,
498 'product_label' => $line->product_label,
499 'product_desc' => $line->product_desc,
504 $linesinvoice[] = array(
505 'id' => $invoice->id,
506 'ref' => $invoice->ref,
507 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
508 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author :
'',
509 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id :
'',
510 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
511 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
512 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
513 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
514 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
515 'type' => $invoice->type,
516 'total_net' => $invoice->total_ht,
517 'total_vat' => $invoice->total_tva,
518 'total' => $invoice->total_ttc,
519 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
520 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
521 'status' => $invoice->status,
522 'project_id' => $invoice->fk_project,
523 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
524 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
525 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
526 'lines' => $linesresp
534 'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
535 'invoices' => $linesinvoice
540 $errorcode =
$db->lasterrno();
541 $errorlabel =
$db->lasterror();
546 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
566 dol_syslog(
"Function: createInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
567 if ($authentication[
'entity']) {
568 $conf->entity = $authentication[
'entity'];
572 $objectresp = array();
579 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
582 $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
587 $new_invoice->socid = $invoice[
'thirdparty_id'];
588 $new_invoice->type = $invoice[
'type'];
589 $new_invoice->ref_ext = $invoice[
'ref_ext'];
591 $new_invoice->note_private = $invoice[
'note_private'];
592 $new_invoice->note_public = $invoice[
'note_public'];
594 $new_invoice->fk_project = (int) $invoice[
'project_id'];
595 $new_invoice->date_creation = $now;
599 $res = $soc->fetch($new_invoice->socid);
601 $new_invoice->mode_reglement_id = !empty($invoice[
'payment_mode_id']) ? $invoice[
'payment_mode_id'] : $soc->mode_reglement_id;
602 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
604 $new_invoice->mode_reglement_id = (int) $invoice[
'payment_mode_id'];
608 $arrayoflines = array();
609 if (isset($invoice[
'lines'][
'line'][0])) {
610 $arrayoflines = $invoice[
'lines'][
'line'];
612 $arrayoflines = $invoice[
'lines'];
614 if (!is_array($arrayoflines)) {
615 $arrayoflines = array();
618 foreach ($arrayoflines as $line) {
621 $newline->product_type = (int) $line[
'type'];
622 $newline->desc = $line[
'desc'];
623 $newline->fk_product = (int) $line[
'product_id'];
624 $newline->tva_tx = isset($line[
'vat_rate']) ? $line[
'vat_rate'] : 0;
625 $newline->qty = (float) $line[
'qty'];
626 $newline->subprice = isset($line[
'unitprice']) ? $line[
'unitprice'] :
null;
627 $newline->total_ht = (float) $line[
'total_net'];
628 $newline->total_tva = (float) $line[
'total_vat'];
629 $newline->total_ttc = (float) $line[
'total'];
633 $new_invoice->lines[] = $newline;
640 $result = $new_invoice->create($fuser, 0,
dol_stringtotime($invoice[
'date_due'],
'dayrfc'));
646 $result = $new_invoice->validate($fuser);
654 $objectresp = array(
'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
'id' => $new_invoice->id,
655 'ref' => $new_invoice->ref,
'ref_ext' => $new_invoice->ref_ext);
660 $errorlabel = $new_invoice->error;
661 dol_syslog(
"Function: createInvoice error while creating".$errorlabel);
666 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
685 dol_syslog(
"Function: createInvoiceFromOrder login=".$authentication[
'login'].
" id=".$id_order.
", ref=".$ref_order.
", ref_ext=".$ref_ext_order);
687 if ($authentication[
'entity']) {
688 $conf->entity = $authentication[
'entity'];
692 $objectresp = array();
700 $socid = $fuser->socid;
704 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
707 $errorlabel =
"order id or ref or ref_ext is mandatory.";
712 $fuser->loadRights();
714 if ($fuser->hasRight(
'commande',
'lire')) {
716 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
719 if ($socid && ($socid != $order->socid)) {
721 $errorcode =
'PERMISSION_DENIED';
722 $errorlabel = $order->socid.
'User does not have permission for this request';
727 $result = $newobject->createFromOrder($order, $fuser);
731 dol_syslog(
"Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
736 $errorcode =
'NOT_FOUND';
737 $errorlabel =
'Object not found for id='.$id_order.
' nor ref='.$ref_order.
' nor ref_ext='.$ref_ext_order;
741 $errorcode =
'PERMISSION_DENIED';
742 $errorlabel =
'User does not have permission for this request';
746 if ($error || $newobject ===
null) {
747 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
749 $objectresp = array(
'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
'id' => $newobject->id,
'ref' => $newobject->ref,
'ref_ext' => $newobject->ref_ext);
766 dol_syslog(
"Function: updateInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
767 ", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
769 if ($authentication[
'entity']) {
770 $conf->entity = $authentication[
'entity'];
774 $objectresp = array();
781 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
784 $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
788 $objectfound =
false;
791 $result =
$object->fetch((
int) $invoice[
'id'], $invoice[
'ref'], $invoice[
'ref_ext'], 0);
798 if (isset($invoice[
'status'])) {
800 $result =
$object->setDraft($fuser);
803 $result =
$object->validate($fuser);
807 $outputlangs = $langs;
812 $result =
$object->setPaid($fuser, (
string) $invoice[
'close_code'], (
string) $invoice[
'close_note']);
815 $result =
$object->setCanceled($fuser, (
string) $invoice[
'close_code'], (
string) $invoice[
'close_note']);
820 if ((!$error) && ($objectfound)) {
823 'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
828 } elseif ($objectfound) {
835 $errorcode =
'NOT_FOUND';
836 $errorlabel =
'Invoice id='.$invoice[
'id'].
' ref='.$invoice[
'ref'].
' ref_ext='.$invoice[
'ref_ext'].
' cannot be found';
841 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
848$server->service(file_get_contents(
"php://input"));
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
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_now($mode='gmt')
Return date for now.
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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)
Update an invoice, only change the state of an invoice.
createInvoiceFromOrder($authentication, $id_order=0, $ref_order='', $ref_ext_order='')
Create an invoice from an order.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.