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);
81$server->wsdl->schemaTargetNamespace = $ns;
85$server->wsdl->addComplexType(
92 'dolibarrkey' => array(
'name' =>
'dolibarrkey',
'type' =>
'xsd:string'),
93 'sourceapplication' => array(
'name' =>
'sourceapplication',
'type' =>
'xsd:string'),
94 'login' => array(
'name' =>
'login',
'type' =>
'xsd:string'),
95 'password' => array(
'name' =>
'password',
'type' =>
'xsd:string'),
96 'entity' => array(
'name' =>
'entity',
'type' =>
'xsd:string')
100$server->wsdl->addComplexType(
107 'result_code' => array(
'name' =>
'result_code',
'type' =>
'xsd:string'),
108 'result_label' => array(
'name' =>
'result_label',
'type' =>
'xsd:string'),
113$server->wsdl->addComplexType(
120 'id' => array(
'name' =>
'id',
'type' =>
'xsd:string'),
121 'type' => array(
'name' =>
'type',
'type' =>
'xsd:int'),
122 'desc' => array(
'name' =>
'desc',
'type' =>
'xsd:string'),
123 'vat_rate' => array(
'name' =>
'vat_rate',
'type' =>
'xsd:double'),
124 'qty' => array(
'name' =>
'qty',
'type' =>
'xsd:double'),
125 'unitprice' => array(
'name' =>
'unitprice',
'type' =>
'xsd:double'),
126 'total_net' => array(
'name' =>
'total_net',
'type' =>
'xsd:double'),
127 'total_vat' => array(
'name' =>
'total_vat',
'type' =>
'xsd:double'),
128 'total' => array(
'name' =>
'total',
'type' =>
'xsd:double'),
129 'date_start' => array(
'name' =>
'date_start',
'type' =>
'xsd:date'),
130 'date_end' => array(
'name' =>
'date_end',
'type' =>
'xsd:date'),
132 'product_id' => array(
'name' =>
'product_id',
'type' =>
'xsd:int'),
133 'product_ref' => array(
'name' =>
'product_ref',
'type' =>
'xsd:string'),
134 'product_label' => array(
'name' =>
'product_label',
'type' =>
'xsd:string'),
135 'product_desc' => array(
'name' =>
'product_desc',
'type' =>
'xsd:string')
151$server->wsdl->addComplexType(
160 'type' =>
'tns:line',
162 'maxOccurs' =>
'unbounded'
170$server->wsdl->addComplexType(
177 'id' => array(
'name' =>
'id',
'type' =>
'xsd:string'),
178 'ref' => array(
'name' =>
'ref',
'type' =>
'xsd:string'),
179 'ref_ext' => array(
'name' =>
'ref_ext',
'type' =>
'xsd:string'),
180 'thirdparty_id' => array(
'name' =>
'thirdparty_id',
'type' =>
'xsd:int'),
181 'fk_user_author' => array(
'name' =>
'fk_user_author',
'type' =>
'xsd:string'),
182 'fk_user_valid' => array(
'name' =>
'fk_user_valid',
'type' =>
'xsd:string'),
183 'date' => array(
'name' =>
'date',
'type' =>
'xsd:date'),
184 'date_due' => array(
'name' =>
'date_due',
'type' =>
'xsd:date'),
185 'date_creation' => array(
'name' =>
'date_creation',
'type' =>
'xsd:dateTime'),
186 'date_validation' => array(
'name' =>
'date_validation',
'type' =>
'xsd:dateTime'),
187 'date_modification' => array(
'name' =>
'date_modification',
'type' =>
'xsd:dateTime'),
188 'payment_mode_id' => array(
'name' =>
'payment_mode_id',
'type' =>
'xsd:string'),
189 'type' => array(
'name' =>
'type',
'type' =>
'xsd:int'),
190 'total_net' => array(
'name' =>
'type',
'type' =>
'xsd:double'),
191 'total_vat' => array(
'name' =>
'type',
'type' =>
'xsd:double'),
192 'total' => array(
'name' =>
'type',
'type' =>
'xsd:double'),
193 'note_private' => array(
'name' =>
'note_private',
'type' =>
'xsd:string'),
194 'note_public' => array(
'name' =>
'note_public',
'type' =>
'xsd:string'),
195 'status' => array(
'name' =>
'status',
'type' =>
'xsd:int'),
196 'close_code' => array(
'name' =>
'close_code',
'type' =>
'xsd:string'),
197 'close_note' => array(
'name' =>
'close_note',
'type' =>
'xsd:string'),
198 'project_id' => array(
'name' =>
'project_id',
'type' =>
'xsd:string'),
199 'lines' => array(
'name' =>
'lines',
'type' =>
'tns:LinesArray2')
215$server->wsdl->addComplexType(
224 'type' =>
'tns:invoice',
226 'maxOccurs' =>
'unbounded'
239$styleuse =
'encoded';
246 array(
'authentication' =>
'tns:authentication',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
248 array(
'result' =>
'tns:result',
'invoice' =>
'tns:invoice'),
253 'WS to get a particular invoice'
256 'getInvoicesForThirdParty',
258 array(
'authentication' =>
'tns:authentication',
'idthirdparty' =>
'xsd:string'),
260 array(
'result' =>
'tns:result',
'invoices' =>
'tns:InvoicesArray2'),
262 $ns.
'#getInvoicesForThirdParty',
265 'WS to get all invoices of a third party'
270 array(
'authentication' =>
'tns:authentication',
'invoice' =>
'tns:invoice'),
272 array(
'result' =>
'tns:result',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
274 $ns.
'#createInvoice',
277 'WS to create an invoice'
280 'createInvoiceFromOrder',
282 array(
'authentication' =>
'tns:authentication',
'id_order' =>
'xsd:string',
'ref_order' =>
'xsd:string',
'ref_ext_order' =>
'xsd:string'),
284 array(
'result' =>
'tns:result',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
286 $ns.
'#createInvoiceFromOrder',
289 'WS to create an invoice from an order'
294 array(
'authentication' =>
'tns:authentication',
'invoice' =>
'tns:invoice'),
296 array(
'result' =>
'tns:result',
'id' =>
'xsd:string',
'ref' =>
'xsd:string',
'ref_ext' =>
'xsd:string'),
298 $ns.
'#updateInvoice',
301 'WS to update an invoice'
318 dol_syslog(
"Function: getInvoice login=".$authentication[
'login'].
" id=".
$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
320 if ($authentication[
'entity']) {
321 $conf->entity = $authentication[
'entity'];
325 $objectresp = array();
331 if (!$error && ((
$id && $ref) || (
$id && $ref_ext) || ($ref && $ref_ext))) {
333 $errorcode =
'BAD_PARAMETERS';
334 $errorlabel =
"Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
338 $fuser->loadRights();
340 if ($fuser->hasRight(
'facture',
'lire')) {
342 $result = $invoice->fetch(
$id, $ref, $ref_ext);
344 $linesresp = array();
346 foreach ($invoice->lines as $line) {
348 $linesresp[] = array(
350 'type' => $line->product_type,
352 'total_net' => $line->total_ht,
353 'total_vat' => $line->total_tva,
354 'total' => $line->total_ttc,
355 'vat_rate' => $line->tva_tx,
357 'unitprice' => $line->subprice,
358 'date_start' => $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
359 'date_end' => $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
360 'product_id' => $line->fk_product,
361 'product_ref' => $line->product_ref,
362 'product_label' => $line->product_label,
363 'product_desc' => $line->product_desc,
370 'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
372 'id' => $invoice->id,
373 'ref' => $invoice->ref,
374 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
375 'thirdparty_id' => $invoice->socid,
376 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author :
'',
377 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id :
'',
378 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
379 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
380 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
381 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
382 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
383 'type' => $invoice->type,
384 'total_net' => $invoice->total_ht,
385 'total_vat' => $invoice->total_tva,
386 'total' => $invoice->total_ttc,
387 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
388 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
389 'status' => $invoice->statut,
390 'project_id' => $invoice->fk_project,
391 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
392 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
393 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
394 'lines' => $linesresp
398 $errorcode =
'NOT_FOUND';
399 $errorlabel =
'Object not found for id='.$id.
' nor ref='.$ref.
' nor ref_ext='.$ref_ext;
403 $errorcode =
'PERMISSION_DENIED';
404 $errorlabel =
'User does not have permission for this request';
409 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
427 dol_syslog(
"Function: getInvoicesForThirdParty login=".$authentication[
'login'].
" idthirdparty=".$idthirdparty);
429 if ($authentication[
'entity']) {
430 $conf->entity = $authentication[
'entity'];
434 $objectresp = array();
442 $socid = $fuser->socid;
446 if (!$error && empty($idthirdparty)) {
448 $errorcode =
'BAD_PARAMETERS';
449 $errorlabel =
'Parameter idthirdparty is not provided';
453 $linesinvoice = array();
455 $sql =
'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
456 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
457 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
458 if ($idthirdparty !=
'all') {
459 $sql .=
" AND f.fk_soc = ".((int) $idthirdparty);
462 $resql = $db->query($sql);
464 $num = $db->num_rows($resql);
468 $obj = $db->fetch_object($resql);
471 $invoice->fetch($obj->facid);
474 if ($socid && ($socid != $invoice->socid)) {
476 $errorcode =
'PERMISSION_DENIED';
477 $errorlabel = $invoice->socid.
' User does not have permission for this request';
482 $linesresp = array();
483 foreach ($invoice->lines as $line) {
484 $linesresp[] = array(
486 'type' => $line->product_type,
487 'total_net' => $line->total_ht,
488 'total_vat' => $line->total_tva,
489 'total' => $line->total_ttc,
490 'vat_rate' => $line->tva_tx,
492 'unitprice' => $line->subprice,
493 'date_start' => $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
494 'date_end' => $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
495 'product_id' => $line->fk_product,
496 'product_ref' => $line->product_ref,
497 'product_label' => $line->product_label,
498 'product_desc' => $line->product_desc,
503 $linesinvoice[] = array(
504 'id' => $invoice->id,
505 'ref' => $invoice->ref,
506 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
507 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author :
'',
508 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id :
'',
509 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
510 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
511 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
512 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
513 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
514 'type' => $invoice->type,
515 'total_net' => $invoice->total_ht,
516 'total_vat' => $invoice->total_tva,
517 'total' => $invoice->total_ttc,
518 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
519 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
520 'status' => $invoice->statut,
521 'project_id' => $invoice->fk_project,
522 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
523 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
524 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
525 'lines' => $linesresp
533 'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
534 'invoices' => $linesinvoice
539 $errorcode = $db->lasterrno();
540 $errorlabel = $db->lasterror();
545 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
565 dol_syslog(
"Function: createInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
566 if ($authentication[
'entity']) {
567 $conf->entity = $authentication[
'entity'];
571 $objectresp = array();
578 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
581 $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
585 $new_invoice =
new Facture($db);
586 $new_invoice->socid = $invoice[
'thirdparty_id'];
587 $new_invoice->type = $invoice[
'type'];
588 $new_invoice->ref_ext = $invoice[
'ref_ext'];
590 $new_invoice->note_private = $invoice[
'note_private'];
591 $new_invoice->note_public = $invoice[
'note_public'];
593 $new_invoice->fk_project = (int) $invoice[
'project_id'];
594 $new_invoice->date_creation = $now;
598 $res = $soc->fetch($new_invoice->socid);
600 $new_invoice->mode_reglement_id = !empty($invoice[
'payment_mode_id']) ? $invoice[
'payment_mode_id'] : $soc->mode_reglement_id;
601 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
603 $new_invoice->mode_reglement_id = (int) $invoice[
'payment_mode_id'];
607 $arrayoflines = array();
608 if (isset($invoice[
'lines'][
'line'][0])) {
609 $arrayoflines = $invoice[
'lines'][
'line'];
611 $arrayoflines = $invoice[
'lines'];
613 if (!is_array($arrayoflines)) {
614 $arrayoflines = array();
617 foreach ($arrayoflines as $line) {
620 $newline->product_type = $line[
'type'];
621 $newline->desc = $line[
'desc'];
622 $newline->fk_product = $line[
'product_id'];
623 $newline->tva_tx = isset($line[
'vat_rate']) ? $line[
'vat_rate'] : 0;
624 $newline->qty = $line[
'qty'];
625 $newline->subprice = isset($line[
'unitprice']) ? $line[
'unitprice'] :
null;
626 $newline->total_ht = $line[
'total_net'];
627 $newline->total_tva = $line[
'total_vat'];
628 $newline->total_ttc = $line[
'total'];
632 $new_invoice->lines[] = $newline;
639 $result = $new_invoice->create($fuser, 0,
dol_stringtotime($invoice[
'date_due'],
'dayrfc'));
645 $result = $new_invoice->validate($fuser);
653 $objectresp = array(
'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
'id' => $new_invoice->id,
654 'ref' => $new_invoice->ref,
'ref_ext' => $new_invoice->ref_ext);
659 $errorlabel = $new_invoice->error;
660 dol_syslog(
"Function: createInvoice error while creating".$errorlabel);
665 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
684 dol_syslog(
"Function: createInvoiceFromOrder login=".$authentication[
'login'].
" id=".$id_order.
", ref=".$ref_order.
", ref_ext=".$ref_ext_order);
686 if ($authentication[
'entity']) {
687 $conf->entity = $authentication[
'entity'];
691 $objectresp = array();
699 $socid = $fuser->socid;
703 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
706 $errorlabel =
"order id or ref or ref_ext is mandatory.";
711 $fuser->loadRights();
713 if ($fuser->hasRight(
'commande',
'lire')) {
715 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
718 if ($socid && ($socid != $order->socid)) {
720 $errorcode =
'PERMISSION_DENIED';
721 $errorlabel = $order->socid.
'User does not have permission for this request';
726 $result = $newobject->createFromOrder($order, $fuser);
730 dol_syslog(
"Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
735 $errorcode =
'NOT_FOUND';
736 $errorlabel =
'Object not found for id='.$id_order.
' nor ref='.$ref_order.
' nor ref_ext='.$ref_ext_order;
740 $errorcode =
'PERMISSION_DENIED';
741 $errorlabel =
'User does not have permission for this request';
745 if ($error || $newobject ===
null) {
746 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
748 $objectresp = array(
'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
'id' => $newobject->id,
'ref' => $newobject->ref,
'ref_ext' => $newobject->ref_ext);
763 global $db,
$conf, $langs;
765 dol_syslog(
"Function: updateInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
766 ", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
768 if ($authentication[
'entity']) {
769 $conf->entity = $authentication[
'entity'];
773 $objectresp = array();
780 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
783 $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
787 $objectfound =
false;
790 $result =
$object->fetch($invoice[
'id'], $invoice[
'ref'], $invoice[
'ref_ext'], 0);
797 if (isset($invoice[
'status'])) {
799 $result =
$object->setDraft($fuser);
802 $result =
$object->validate($fuser);
806 $outputlangs = $langs;
811 $result =
$object->setPaid($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
814 $result =
$object->setCanceled($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
819 if ((!$error) && ($objectfound)) {
822 'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
827 } elseif ($objectfound) {
834 $errorcode =
'NOT_FOUND';
835 $errorlabel =
'Invoice id='.$invoice[
'id'].
' ref='.$invoice[
'ref'].
' ref_ext='.$invoice[
'ref_ext'].
' cannot be found';
840 $objectresp = array(
'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
847$server->service(file_get_contents(
"php://input"));
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $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.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
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)
Update an invoice, only change the state of an invoice.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.