24 if (!defined(
"NOCSRFCHECK")) {
25 define(
"NOCSRFCHECK",
'1');
28 require
'../master.inc.php';
29 require_once NUSOAP_PATH.
'/nusoap.php';
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';
34 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
38 dol_syslog(
"Call Dolibarr webservices interfaces");
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");
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;
61 $server->wsdl->addComplexType(
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')
76 $server->wsdl->addComplexType(
83 'result_code' => array(
'name'=>
'result_code',
'type'=>
'xsd:string'),
84 'result_label' => array(
'name'=>
'result_label',
'type'=>
'xsd:string'),
89 $server->wsdl->addComplexType(
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'),
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')
127 $server->wsdl->addComplexType(
136 'type' =>
'tns:line',
138 'maxOccurs' =>
'unbounded'
146 $server->wsdl->addComplexType(
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')
191 $server->wsdl->addComplexType(
200 'type' =>
'tns:invoice',
202 'maxOccurs' =>
'unbounded'
215 $styleuse =
'encoded';
222 array(
'authentication'=>
'tns:authentication',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
224 array(
'result'=>
'tns:result',
'invoice'=>
'tns:invoice'),
229 'WS to get a particular invoice'
232 'getInvoicesForThirdParty',
234 array(
'authentication'=>
'tns:authentication',
'idthirdparty'=>
'xsd:string'),
236 array(
'result'=>
'tns:result',
'invoices'=>
'tns:InvoicesArray2'),
238 $ns.
'#getInvoicesForThirdParty',
241 'WS to get all invoices of a third party'
246 array(
'authentication'=>
'tns:authentication',
'invoice'=>
'tns:invoice'),
248 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
250 $ns.
'#createInvoice',
253 'WS to create an invoice'
256 'createInvoiceFromOrder',
258 array(
'authentication'=>
'tns:authentication',
'id_order'=>
'xsd:string',
'ref_order'=>
'xsd:string',
'ref_ext_order'=>
'xsd:string'),
260 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
262 $ns.
'#createInvoiceFromOrder',
265 'WS to create an invoice from an order'
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.
'#updateInvoice',
277 'WS to update an invoice'
290 function getInvoice($authentication, $id =
'', $ref =
'', $ref_ext =
'')
294 dol_syslog(
"Function: getInvoice login=".$authentication[
'login'].
" id=".$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
296 if ($authentication[
'entity']) {
297 $conf->entity = $authentication[
'entity'];
301 $objectresp = array();
302 $errorcode =
''; $errorlabel =
'';
306 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
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.";
314 if ($fuser->rights->facture->lire) {
316 $result = $invoice->fetch($id, $ref, $ref_ext);
318 $linesresp = array();
320 foreach ($invoice->lines as $line) {
322 $linesresp[] = array(
324 'type'=>$line->product_type,
326 'total_net'=>$line->total_ht,
327 'total_vat'=>$line->total_tva,
328 'total'=>$line->total_ttc,
329 'vat_rate'=>$line->tva_tx,
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,
344 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
346 'id' => $invoice->id,
347 'ref' => $invoice->ref,
348 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
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
372 $errorcode =
'NOT_FOUND'; $errorlabel =
'Object not found for id='.$id.
' nor ref='.$ref.
' nor ref_ext='.$ref_ext;
376 $errorcode =
'PERMISSION_DENIED'; $errorlabel =
'User does not have permission for this request';
381 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
399 dol_syslog(
"Function: getInvoicesForThirdParty login=".$authentication[
'login'].
" idthirdparty=".$idthirdparty);
401 if ($authentication[
'entity']) {
402 $conf->entity = $authentication[
'entity'];
406 $objectresp = array();
407 $errorcode =
''; $errorlabel =
'';
412 $socid = $fuser->socid;
416 if (!$error && empty($idthirdparty)) {
418 $errorcode =
'BAD_PARAMETERS'; $errorlabel =
'Parameter idthirdparty is not provided';
422 $linesinvoice = array();
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);
431 $resql = $db->query($sql);
433 $num = $db->num_rows(
$resql);
437 $obj = $db->fetch_object(
$resql);
440 $invoice->fetch($obj->facid);
443 if ($socid && ($socid != $invoice->socid)) {
445 $errorcode =
'PERMISSION_DENIED'; $errorlabel = $invoice->socid.
' User does not have permission for this request';
450 $linesresp = array();
451 foreach ($invoice->lines as $line) {
452 $linesresp[] = array(
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,
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,
471 $linesinvoice[] = array(
472 'id' => $invoice->id,
473 'ref' => $invoice->ref,
474 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
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
501 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
502 'invoices'=>$linesinvoice
507 $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
512 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
532 dol_syslog(
"Function: createInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
534 if ($authentication[
'entity']) {
535 $conf->entity = $authentication[
'entity'];
539 $objectresp = array();
540 $errorcode =
''; $errorlabel =
'';
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.";
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'];
555 $new_invoice->note_private = $invoice[
'note_private'];
556 $new_invoice->note_public = $invoice[
'note_public'];
558 $new_invoice->fk_project = $invoice[
'project_id'];
559 $new_invoice->date_creation = $now;
563 $res = $soc->fetch($new_invoice->socid);
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;
568 $new_invoice->mode_reglement_id = $invoice[
'payment_mode_id'];
572 $arrayoflines = array();
573 if (isset($invoice[
'lines'][
'line'][0])) {
574 $arrayoflines = $invoice[
'lines'][
'line'];
576 $arrayoflines = $invoice[
'lines'];
579 foreach ($arrayoflines as $line) {
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'];
594 $new_invoice->lines[] = $newline;
601 $result = $new_invoice->create($fuser, 0,
dol_stringtotime($invoice[
'date_due'],
'dayrfc'));
607 $result = $new_invoice->validate($fuser);
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);
621 $errorlabel = $new_invoice->error;
622 dol_syslog(
"Function: createInvoice error while creating".$errorlabel);
627 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
646 dol_syslog(
"Function: createInvoiceFromOrder login=".$authentication[
'login'].
" id=".$id_order.
", ref=".$ref_order.
", ref_ext=".$ref_ext_order);
648 if ($authentication[
'entity']) {
649 $conf->entity = $authentication[
'entity'];
653 $objectresp = array();
654 $errorcode =
''; $errorlabel =
'';
658 $socid = $fuser->socid;
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.";
670 if ($fuser->rights->commande->lire) {
672 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
675 if ($socid && ($socid != $order->socid)) {
677 $errorcode =
'PERMISSION_DENIED'; $errorlabel = $order->socid.
'User does not have permission for this request';
682 $result = $newobject->createFromOrder($order, $fuser);
686 dol_syslog(
"Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
691 $errorcode =
'NOT_FOUND'; $errorlabel =
'Object not found for id='.$id_order.
' nor ref='.$ref_order.
' nor ref_ext='.$ref_ext_order;
695 $errorcode =
'PERMISSION_DENIED'; $errorlabel =
'User does not have permission for this request';
700 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
702 $objectresp = array(
'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
'id'=>$newobject->id,
'ref'=>$newobject->ref,
'ref_ext'=>$newobject->ref_ext);
717 global $db, $conf, $langs;
719 dol_syslog(
"Function: updateInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
720 ", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
722 if ($authentication[
'entity']) {
723 $conf->entity = $authentication[
'entity'];
727 $objectresp = array();
728 $errorcode =
''; $errorlabel =
'';
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.";
738 $objectfound =
false;
741 $result = $object->fetch($invoice[
'id'], $invoice[
'ref'], $invoice[
'ref_ext'],
'');
743 if (!empty($object->id)) {
748 if (isset($invoice[
'status'])) {
750 $result = $object->setDraft($fuser);
753 $result = $object->validate($fuser);
757 $outputlangs = $langs;
758 $object->generateDocument($object->model_pdf, $outputlangs);
762 $result = $object->setPaid($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
765 $result = $object->setCanceled($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
770 if ((!$error) && ($objectfound)) {
773 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
776 'ref_ext'=>$object->ref_ext
778 } elseif ($objectfound) {
782 $errorlabel = $object->error;
785 $errorcode =
'NOT_FOUND';
786 $errorlabel =
'Invoice id='.$invoice[
'id'].
' ref='.$invoice[
'ref'].
' ref_ext='.$invoice[
'ref_ext'].
' cannot be found';
791 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
798 $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...)
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
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.