24 if (!defined(
"NOCSRFCHECK")) define(
"NOCSRFCHECK",
'1');
26 require
'../master.inc.php';
27 require_once NUSOAP_PATH.
'/nusoap.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/ws.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
36 dol_syslog(
"Call Dolibarr webservices interfaces");
41 if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
43 $langs->load(
"admin");
44 dol_syslog(
"Call Dolibarr webservices interfaces with module webservices disabled");
45 print $langs->trans(
"WarningModuleNotActive",
'WebServices').
'.<br><br>';
46 print $langs->trans(
"ToActivateModule");
51 $server =
new nusoap_server();
52 $server->soap_defencoding =
'UTF-8';
53 $server->decode_utf8 =
false;
54 $ns =
'http://www.dolibarr.org/ns/';
55 $server->configureWSDL(
'WebServicesDolibarrInvoice', $ns);
56 $server->wsdl->schemaTargetNamespace = $ns;
60 $server->wsdl->addComplexType(
67 'dolibarrkey' => array(
'name'=>
'dolibarrkey',
'type'=>
'xsd:string'),
68 'sourceapplication' => array(
'name'=>
'sourceapplication',
'type'=>
'xsd:string'),
69 'login' => array(
'name'=>
'login',
'type'=>
'xsd:string'),
70 'password' => array(
'name'=>
'password',
'type'=>
'xsd:string'),
71 'entity' => array(
'name'=>
'entity',
'type'=>
'xsd:string')
75 $server->wsdl->addComplexType(
82 'result_code' => array(
'name'=>
'result_code',
'type'=>
'xsd:string'),
83 'result_label' => array(
'name'=>
'result_label',
'type'=>
'xsd:string'),
88 $server->wsdl->addComplexType(
95 'id' => array(
'name'=>
'id',
'type'=>
'xsd:string'),
96 'type' => array(
'name'=>
'type',
'type'=>
'xsd:int'),
97 'desc' => array(
'name'=>
'desc',
'type'=>
'xsd:string'),
98 'vat_rate' => array(
'name'=>
'vat_rate',
'type'=>
'xsd:double'),
99 'qty' => array(
'name'=>
'qty',
'type'=>
'xsd:double'),
100 'unitprice' => array(
'name'=>
'unitprice',
'type'=>
'xsd:double'),
101 'total_net' => array(
'name'=>
'total_net',
'type'=>
'xsd:double'),
102 'total_vat' => array(
'name'=>
'total_vat',
'type'=>
'xsd:double'),
103 'total' => array(
'name'=>
'total',
'type'=>
'xsd:double'),
104 'date_start' => array(
'name'=>
'date_start',
'type'=>
'xsd:date'),
105 'date_end' => array(
'name'=>
'date_end',
'type'=>
'xsd:date'),
107 'product_id' => array(
'name'=>
'product_id',
'type'=>
'xsd:int'),
108 'product_ref' => array(
'name'=>
'product_ref',
'type'=>
'xsd:string'),
109 'product_label' => array(
'name'=>
'product_label',
'type'=>
'xsd:string'),
110 'product_desc' => array(
'name'=>
'product_desc',
'type'=>
'xsd:string')
126 $server->wsdl->addComplexType(
135 'type' =>
'tns:line',
137 'maxOccurs' =>
'unbounded'
145 $server->wsdl->addComplexType(
152 'id' => array(
'name'=>
'id',
'type'=>
'xsd:string'),
153 'ref' => array(
'name'=>
'ref',
'type'=>
'xsd:string'),
154 'ref_ext' => array(
'name'=>
'ref_ext',
'type'=>
'xsd:string'),
155 'thirdparty_id' => array(
'name'=>
'thirdparty_id',
'type'=>
'xsd:int'),
156 'fk_user_author' => array(
'name'=>
'fk_user_author',
'type'=>
'xsd:string'),
157 'fk_user_valid' => array(
'name'=>
'fk_user_valid',
'type'=>
'xsd:string'),
158 'date' => array(
'name'=>
'date',
'type'=>
'xsd:date'),
159 'date_due' => array(
'name'=>
'date_due',
'type'=>
'xsd:date'),
160 'date_creation' => array(
'name'=>
'date_creation',
'type'=>
'xsd:dateTime'),
161 'date_validation' => array(
'name'=>
'date_validation',
'type'=>
'xsd:dateTime'),
162 'date_modification' => array(
'name'=>
'date_modification',
'type'=>
'xsd:dateTime'),
163 'payment_mode_id' => array(
'name'=>
'payment_mode_id',
'type'=>
'xsd:string'),
164 'type' => array(
'name'=>
'type',
'type'=>
'xsd:int'),
165 'total_net' => array(
'name'=>
'type',
'type'=>
'xsd:double'),
166 'total_vat' => array(
'name'=>
'type',
'type'=>
'xsd:double'),
167 'total' => array(
'name'=>
'type',
'type'=>
'xsd:double'),
168 'note_private' => array(
'name'=>
'note_private',
'type'=>
'xsd:string'),
169 'note_public' => array(
'name'=>
'note_public',
'type'=>
'xsd:string'),
170 'status' => array(
'name'=>
'status',
'type'=>
'xsd:int'),
171 'close_code' => array(
'name'=>
'close_code',
'type'=>
'xsd:string'),
172 'close_note' => array(
'name'=>
'close_note',
'type'=>
'xsd:string'),
173 'project_id' => array(
'name'=>
'project_id',
'type'=>
'xsd:string'),
174 'lines' => array(
'name'=>
'lines',
'type'=>
'tns:LinesArray2')
190 $server->wsdl->addComplexType(
199 'type' =>
'tns:invoice',
201 'maxOccurs' =>
'unbounded'
214 $styleuse =
'encoded';
221 array(
'authentication'=>
'tns:authentication',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
223 array(
'result'=>
'tns:result',
'invoice'=>
'tns:invoice'),
228 'WS to get a particular invoice'
231 'getInvoicesForThirdParty',
233 array(
'authentication'=>
'tns:authentication',
'idthirdparty'=>
'xsd:string'),
235 array(
'result'=>
'tns:result',
'invoices'=>
'tns:InvoicesArray2'),
237 $ns.
'#getInvoicesForThirdParty',
240 'WS to get all invoices of a third party'
245 array(
'authentication'=>
'tns:authentication',
'invoice'=>
'tns:invoice'),
247 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
249 $ns.
'#createInvoice',
252 'WS to create an invoice'
255 'createInvoiceFromOrder',
257 array(
'authentication'=>
'tns:authentication',
'id_order'=>
'xsd:string',
'ref_order'=>
'xsd:string',
'ref_ext_order'=>
'xsd:string'),
259 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
261 $ns.
'#createInvoiceFromOrder',
264 'WS to create an invoice from an order'
269 array(
'authentication'=>
'tns:authentication',
'invoice'=>
'tns:invoice'),
271 array(
'result'=>
'tns:result',
'id'=>
'xsd:string',
'ref'=>
'xsd:string',
'ref_ext'=>
'xsd:string'),
273 $ns.
'#updateInvoice',
276 'WS to update an invoice'
289 function getInvoice($authentication, $id =
'', $ref =
'', $ref_ext =
'')
293 dol_syslog(
"Function: getInvoice login=".$authentication[
'login'].
" id=".$id.
" ref=".$ref.
" ref_ext=".$ref_ext);
295 if ($authentication[
'entity']) $conf->entity = $authentication[
'entity'];
298 $objectresp = array();
299 $errorcode =
''; $errorlabel =
'';
303 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
306 $errorcode =
'BAD_PARAMETERS'; $errorlabel =
"Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
313 if ($fuser->rights->facture->lire)
316 $result = $invoice->fetch($id, $ref, $ref_ext);
319 $linesresp = array();
321 foreach ($invoice->lines as $line)
324 $linesresp[] = array(
326 'type'=>$line->product_type,
328 'total_net'=>$line->total_ht,
329 'total_vat'=>$line->total_tva,
330 'total'=>$line->total_ttc,
331 'vat_rate'=>$line->tva_tx,
333 'unitprice'=> $line->subprice,
334 'date_start'=> $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
335 'date_end'=> $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
336 'product_id'=>$line->fk_product,
337 'product_ref'=>$line->product_ref,
338 'product_label'=>$line->product_label,
339 'product_desc'=>$line->product_desc,
346 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
348 'id' => $invoice->id,
349 'ref' => $invoice->ref,
350 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
351 'thirdparty_id' => $invoice->socid,
352 'fk_user_author' => $invoice->user_author ? $invoice->user_author :
'',
353 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid :
'',
354 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
355 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
356 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
357 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
358 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
359 'type' => $invoice->type,
360 'total_net' => $invoice->total_ht,
361 'total_vat' => $invoice->total_tva,
362 'total' => $invoice->total_ttc,
363 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
364 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
365 'status' => $invoice->statut,
366 'project_id' => $invoice->fk_project,
367 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
368 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
369 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
370 'lines' => $linesresp
375 $errorcode =
'NOT_FOUND'; $errorlabel =
'Object not found for id='.$id.
' nor ref='.$ref.
' nor ref_ext='.$ref_ext;
380 $errorcode =
'PERMISSION_DENIED'; $errorlabel =
'User does not have permission for this request';
386 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
404 dol_syslog(
"Function: getInvoicesForThirdParty login=".$authentication[
'login'].
" idthirdparty=".$idthirdparty);
406 if ($authentication[
'entity']) $conf->entity = $authentication[
'entity'];
409 $objectresp = array();
410 $errorcode =
''; $errorlabel =
'';
414 if ($fuser->socid) $socid = $fuser->socid;
417 if (!$error && empty($idthirdparty))
420 $errorcode =
'BAD_PARAMETERS'; $errorlabel =
'Parameter idthirdparty is not provided';
425 $linesinvoice = array();
427 $sql =
'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
428 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
429 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
430 if ($idthirdparty !=
'all') $sql .=
" AND f.fk_soc = ".$db->escape($idthirdparty);
432 $resql = $db->query($sql);
435 $num = $db->num_rows(
$resql);
440 $obj = $db->fetch_object(
$resql);
443 $invoice->fetch($obj->facid);
446 if ($socid && ($socid != $invoice->socid))
449 $errorcode =
'PERMISSION_DENIED'; $errorlabel = $invoice->socid.
' User does not have permission for this request';
455 $linesresp = array();
456 foreach ($invoice->lines as $line)
458 $linesresp[] = array(
460 'type'=>$line->product_type,
461 'total_net'=>$line->total_ht,
462 'total_vat'=>$line->total_tva,
463 'total'=>$line->total_ttc,
464 'vat_rate'=>$line->tva_tx,
466 'unitprice'=> $line->subprice,
467 'date_start'=> $line->date_start ?
dol_print_date($line->date_start,
'dayrfc') :
'',
468 'date_end'=> $line->date_end ?
dol_print_date($line->date_end,
'dayrfc') :
'',
469 'product_id'=>$line->fk_product,
470 'product_ref'=>$line->product_ref,
471 'product_label'=>$line->product_label,
472 'product_desc'=>$line->product_desc,
477 $linesinvoice[] = array(
478 'id' => $invoice->id,
479 'ref' => $invoice->ref,
480 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext :
'',
481 'fk_user_author' => $invoice->user_author ? $invoice->user_author :
'',
482 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid :
'',
483 'date' => $invoice->date ?
dol_print_date($invoice->date,
'dayrfc') :
'',
484 'date_due' => $invoice->date_lim_reglement ?
dol_print_date($invoice->date_lim_reglement,
'dayrfc') :
'',
485 'date_creation' => $invoice->date_creation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
486 'date_validation' => $invoice->date_validation ?
dol_print_date($invoice->date_creation,
'dayhourrfc') :
'',
487 'date_modification' => $invoice->datem ?
dol_print_date($invoice->datem,
'dayhourrfc') :
'',
488 'type' => $invoice->type,
489 'total_net' => $invoice->total_ht,
490 'total_vat' => $invoice->total_tva,
491 'total' => $invoice->total_ttc,
492 'note_private' => $invoice->note_private ? $invoice->note_private :
'',
493 'note_public' => $invoice->note_public ? $invoice->note_public :
'',
494 'status'=> $invoice->statut,
495 'project_id' => $invoice->fk_project,
496 'close_code' => $invoice->close_code ? $invoice->close_code :
'',
497 'close_note' => $invoice->close_note ? $invoice->close_note :
'',
498 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id :
'',
499 'lines' => $linesresp
507 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
508 'invoices'=>$linesinvoice
514 $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
520 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
540 dol_syslog(
"Function: createInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
542 if ($authentication[
'entity']) $conf->entity = $authentication[
'entity'];
545 $objectresp = array();
546 $errorcode =
''; $errorlabel =
'';
551 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
552 $error++; $errorcode =
'KO'; $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
557 $new_invoice =
new Facture($db);
558 $new_invoice->socid = $invoice[
'thirdparty_id'];
559 $new_invoice->type = $invoice[
'type'];
560 $new_invoice->ref_ext = $invoice[
'ref_ext'];
562 $new_invoice->note_private = $invoice[
'note_private'];
563 $new_invoice->note_public = $invoice[
'note_public'];
565 $new_invoice->fk_project = $invoice[
'project_id'];
566 $new_invoice->date_creation = $now;
570 $res = $soc->fetch($new_invoice->socid);
572 $new_invoice->mode_reglement_id = !empty($invoice[
'payment_mode_id']) ? $invoice[
'payment_mode_id'] : $soc->mode_reglement_id;
573 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
575 else $new_invoice->mode_reglement_id = $invoice[
'payment_mode_id'];
578 $arrayoflines = array();
579 if (isset($invoice[
'lines'][
'line'][0])) $arrayoflines = $invoice[
'lines'][
'line'];
580 else $arrayoflines = $invoice[
'lines'];
582 foreach ($arrayoflines as $line)
586 $newline->product_type = $line[
'type'];
587 $newline->desc = $line[
'desc'];
588 $newline->fk_product = $line[
'product_id'];
589 $newline->tva_tx = isset($line[
'vat_rate']) ? $line[
'vat_rate'] : 0;
590 $newline->qty = $line[
'qty'];
591 $newline->subprice = isset($line[
'unitprice']) ? $line[
'unitprice'] : null;
592 $newline->total_ht = $line[
'total_net'];
593 $newline->total_tva = $line[
'total_vat'];
594 $newline->total_ttc = $line[
'total'];
598 $new_invoice->lines[] = $newline;
605 $result = $new_invoice->create($fuser, 0,
dol_stringtotime($invoice[
'date_due'],
'dayrfc'));
613 $result = $new_invoice->validate($fuser);
623 $objectresp = array(
'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
'id'=>$new_invoice->id,
624 'ref'=>$new_invoice->ref,
'ref_ext'=>$new_invoice->ref_ext);
630 $errorlabel = $new_invoice->error;
631 dol_syslog(
"Function: createInvoice error while creating".$errorlabel);
637 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
656 dol_syslog(
"Function: createInvoiceFromOrder login=".$authentication[
'login'].
" id=".$id_order.
", ref=".$ref_order.
", ref_ext=".$ref_ext_order);
658 if ($authentication[
'entity']) $conf->entity = $authentication[
'entity'];
661 $objectresp = array();
662 $errorcode =
''; $errorlabel =
'';
665 if ($fuser->socid) $socid = $fuser->socid;
668 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
669 $error++; $errorcode =
'KO'; $errorlabel =
"order id or ref or ref_ext is mandatory.";
677 if ($fuser->rights->commande->lire)
680 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
684 if ($socid && ($socid != $order->socid))
687 $errorcode =
'PERMISSION_DENIED'; $errorlabel = $order->socid.
'User does not have permission for this request';
693 $result = $newobject->createFromOrder($order, $fuser);
698 dol_syslog(
"Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
704 $errorcode =
'NOT_FOUND'; $errorlabel =
'Object not found for id='.$id_order.
' nor ref='.$ref_order.
' nor ref_ext='.$ref_ext_order;
709 $errorcode =
'PERMISSION_DENIED'; $errorlabel =
'User does not have permission for this request';
715 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
718 $objectresp = array(
'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
'id'=>$newobject->id,
'ref'=>$newobject->ref,
'ref_ext'=>$newobject->ref_ext);
733 global $db, $conf, $langs;
735 dol_syslog(
"Function: updateInvoice login=".$authentication[
'login'].
" id=".$invoice[
'id'].
736 ", ref=".$invoice[
'ref'].
", ref_ext=".$invoice[
'ref_ext']);
738 if ($authentication[
'entity']) $conf->entity = $authentication[
'entity'];
741 $objectresp = array();
742 $errorcode =
''; $errorlabel =
'';
747 if (empty($invoice[
'id']) && empty($invoice[
'ref']) && empty($invoice[
'ref_ext'])) {
748 $error++; $errorcode =
'KO'; $errorlabel =
"Invoice id or ref or ref_ext is mandatory.";
753 $objectfound =
false;
756 $result = $object->fetch($invoice[
'id'], $invoice[
'ref'], $invoice[
'ref_ext'],
'');
758 if (!empty($object->id)) {
763 if (isset($invoice[
'status']))
767 $result = $object->setDraft($fuser);
771 $result = $object->validate($fuser);
776 $outputlangs = $langs;
777 $object->generateDocument($object->model_pdf, $outputlangs);
781 $result = $object->set_paid($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
784 $result = $object->set_canceled($fuser, $invoice[
'close_code'], $invoice[
'close_note']);
788 if ((!$error) && ($objectfound))
792 'result'=>array(
'result_code'=>
'OK',
'result_label'=>
''),
795 'ref_ext'=>$object->ref_ext
798 elseif ($objectfound)
803 $errorlabel = $object->error;
806 $errorcode =
'NOT_FOUND';
807 $errorlabel =
'Invoice id='.$invoice[
'id'].
' ref='.$invoice[
'ref'].
' ref_ext='.$invoice[
'ref_ext'].
' cannot be found';
813 $objectresp = array(
'result'=>array(
'result_code' => $errorcode,
'result_label' => $errorlabel));
820 $server->service(file_get_contents(
"php://input"));
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
dol_now($mode= 'auto')
Return date for now.
createInvoice($authentication, $invoice)
Create an invoice.
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...
createInvoiceFromOrder($authentication, $id_order= '', $ref_order= '', $ref_ext_order= '')
Create an invoice from an order.
Class to manage third parties objects (customers, suppliers, prospects...)
updateInvoice($authentication, $invoice)
Uddate an invoice, only change the state of an invoice.
const STATUS_VALIDATED
Validated (need to be paid)
Class to manage customers orders.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
const STATUS_DRAFT
Draft status.
const STATUS_CLOSED
Classified paid.
const STATUS_ABANDONED
Classified abandoned and no payment done.
getInvoicesForThirdParty($authentication, $idthirdparty)
Get list of invoices for third party.
print
Draft customers invoices.
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_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Class to manage invoices.
Class to manage invoice lines.
getInvoice($authentication, $id= '', $ref= '', $ref_ext= '')
Get invoice from id, ref or ref_ext.