dolibarr 24.0.0-beta
server_invoice.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26if (!defined('NOCSRFCHECK')) {
27 define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
28}
29if (!defined('NOTOKENRENEWAL')) {
30 define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
31}
32if (!defined('NOREQUIREMENU')) {
33 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
34}
35if (!defined('NOREQUIREHTML')) {
36 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
37}
38if (!defined('NOREQUIREAJAX')) {
39 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
40}
41if (!defined("NOLOGIN")) {
42 define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
43}
44if (!defined("NOSESSION")) {
45 define("NOSESSION", '1');
46}
47
48require '../main.inc.php';
49require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
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';
53
54require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
55require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
56
62dol_syslog("Call Dolibarr webservices interfaces");
63
64$langs->load("main");
65
66// Enable and test if module web services is enabled
67if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
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");
72 exit;
73}
74
75// Create the soap Object
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// @phan-suppress-next-line PhanUndeclaredProperty
82$server->wsdl->schemaTargetNamespace = $ns;
83
84
85// Define WSDL Authentication object
86$server->wsdl->addComplexType(
87 'authentication',
88 'complexType',
89 'struct',
90 'all',
91 '',
92 array(
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')
98 )
99);
100// Define WSDL Return object
101$server->wsdl->addComplexType(
102 'result',
103 'complexType',
104 'struct',
105 'all',
106 '',
107 array(
108 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
109 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
110 )
111);
112
113// Define other specific objects
114$server->wsdl->addComplexType(
115 'line',
116 'complexType',
117 'struct',
118 'all',
119 '',
120 array(
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'),
132 // From product
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')
137 )
138);
139
140/*$server->wsdl->addComplexType(
141 'LinesArray',
142 'complexType',
143 'array',
144 '',
145 'SOAP-ENC:Array',
146 array(),
147 array(
148 array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:line[]')
149 ),
150 'tns:line'
151);*/
152$server->wsdl->addComplexType(
153 'LinesArray2',
154 'complexType',
155 'array',
156 'sequence',
157 '',
158 array(
159 'line' => array(
160 'name' => 'line',
161 'type' => 'tns:line',
162 'minOccurs' => '0',
163 'maxOccurs' => 'unbounded'
164 )
165 ),
166 array(),
167 'tns:line'
168);
169
170
171$server->wsdl->addComplexType(
172 'invoice',
173 'complexType',
174 'struct',
175 'all',
176 '',
177 array(
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')
201 )
202);
203/*
204$server->wsdl->addComplexType(
205 'InvoicesArray',
206 'complexType',
207 'array',
208 '',
209 'SOAP-ENC:Array',
210 array(),
211 array(
212 array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:invoice[]')
213 ),
214 'tns:invoice'
215);*/
216$server->wsdl->addComplexType(
217 'InvoicesArray2',
218 'complexType',
219 'array',
220 'sequence',
221 '',
222 array(
223 'invoice' => array(
224 'name' => 'invoice',
225 'type' => 'tns:invoice',
226 'minOccurs' => '0',
227 'maxOccurs' => 'unbounded'
228 )
229 ),
230 array(),
231 'tns:invoice'
232);
233
234
235
236// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
237// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
238// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
239$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
240$styleuse = 'encoded'; // encoded/literal/literal wrapped
241// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
242
243// Register WSDL
244$server->register(
245 'getInvoice',
246 // Entry values
247 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
248 // Exit values
249 array('result' => 'tns:result', 'invoice' => 'tns:invoice'),
250 $ns,
251 $ns.'#getInvoice',
252 $styledoc,
253 $styleuse,
254 'WS to get a particular invoice'
255);
256$server->register(
257 'getInvoicesForThirdParty',
258 // Entry values
259 array('authentication' => 'tns:authentication', 'idthirdparty' => 'xsd:string'),
260 // Exit values
261 array('result' => 'tns:result', 'invoices' => 'tns:InvoicesArray2'),
262 $ns,
263 $ns.'#getInvoicesForThirdParty',
264 $styledoc,
265 $styleuse,
266 'WS to get all invoices of a third party'
267);
268$server->register(
269 'createInvoice',
270 // Entry values
271 array('authentication' => 'tns:authentication', 'invoice' => 'tns:invoice'),
272 // Exit values
273 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
274 $ns,
275 $ns.'#createInvoice',
276 $styledoc,
277 $styleuse,
278 'WS to create an invoice'
279);
280$server->register(
281 'createInvoiceFromOrder',
282 // Entry values
283 array('authentication' => 'tns:authentication', 'id_order' => 'xsd:string', 'ref_order' => 'xsd:string', 'ref_ext_order' => 'xsd:string'),
284 // Exit values
285 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
286 $ns,
287 $ns.'#createInvoiceFromOrder',
288 $styledoc,
289 $styleuse,
290 'WS to create an invoice from an order'
291);
292$server->register(
293 'updateInvoice',
294 // Entry values
295 array('authentication' => 'tns:authentication', 'invoice' => 'tns:invoice'),
296 // Exit values
297 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
298 $ns,
299 $ns.'#updateInvoice',
300 $styledoc,
301 $styleuse,
302 'WS to update an invoice'
303);
304
305
315function getInvoice($authentication, $id = 0, $ref = '', $ref_ext = '')
316{
317 global $db, $conf;
318
319 dol_syslog("Function: getInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
320
321 if ($authentication['entity']) {
322 $conf->entity = $authentication['entity'];
323 }
324
325 // Init and check authentication
326 $objectresp = array();
327 $errorcode = '';
328 $errorlabel = '';
329 $error = 0;
330 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
331 // Check parameters
332 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
333 $error++;
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.";
336 }
337
338 if (!$error) {
339 $fuser->loadRights();
340
341 if ($fuser->hasRight('facture', 'lire')) {
342 $invoice = new Facture($db);
343 $result = $invoice->fetch($id, $ref, $ref_ext);
344 if ($result > 0) {
345 $linesresp = array();
346 $i = 0;
347 foreach ($invoice->lines as $line) {
348 //var_dump($line); exit;
349 $linesresp[] = array(
350 'id' => $line->id,
351 'type' => $line->product_type,
352 'desc' => dol_htmlcleanlastbr($line->desc),
353 'total_net' => $line->total_ht,
354 'total_vat' => $line->total_tva,
355 'total' => $line->total_ttc,
356 'vat_rate' => $line->tva_tx,
357 'qty' => $line->qty,
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,
365 );
366 $i++;
367 }
368
369 // Create invoice
370 $objectresp = array(
371 'result' => array('result_code' => 'OK', 'result_label' => ''),
372 'invoice' => array(
373 'id' => $invoice->id,
374 'ref' => $invoice->ref,
375 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
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
396 ));
397 } else {
398 $error++;
399 $errorcode = 'NOT_FOUND';
400 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
401 }
402 } else {
403 $error++;
404 $errorcode = 'PERMISSION_DENIED';
405 $errorlabel = 'User does not have permission for this request';
406 }
407 }
408
409 if ($error) {
410 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
411 }
412
413 return $objectresp;
414}
415
416
424function getInvoicesForThirdParty($authentication, $idthirdparty)
425{
426 global $db, $conf;
427
428 dol_syslog("Function: getInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
429
430 if ($authentication['entity']) {
431 $conf->entity = $authentication['entity'];
432 }
433
434 // Init and check authentication
435 $objectresp = array();
436 $errorcode = '';
437 $errorlabel = '';
438 $error = 0;
439 $socid = 0;
440 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
441
442 if ($fuser->socid) {
443 $socid = $fuser->socid;
444 }
445
446 // Check parameters
447 if (!$error && empty($idthirdparty)) {
448 $error++;
449 $errorcode = 'BAD_PARAMETERS';
450 $errorlabel = 'Parameter idthirdparty is not provided';
451 }
452
453 if (!$error) {
454 $linesinvoice = array();
455
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);
461 }
462
463 $resql = $db->query($sql);
464 if ($resql) {
465 $num = $db->num_rows($resql);
466 $i = 0;
467 while ($i < $num) {
468 // En attendant remplissage par boucle
469 $obj = $db->fetch_object($resql);
470
471 $invoice = new Facture($db);
472 $invoice->fetch($obj->facid);
473
474 // Security check for external user
475 if ($socid && ($socid != $invoice->socid)) {
476 $error++;
477 $errorcode = 'PERMISSION_DENIED';
478 $errorlabel = $invoice->socid.' User does not have permission for this request';
479 }
480
481 if (!$error) {
482 // Define lines of invoice
483 $linesresp = array();
484 foreach ($invoice->lines as $line) {
485 $linesresp[] = array(
486 'id' => $line->id,
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,
492 'qty' => $line->qty,
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,
500 );
501 }
502
503 // Now define invoice
504 $linesinvoice[] = array(
505 'id' => $invoice->id,
506 'ref' => $invoice->ref,
507 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
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
527 );
528 }
529
530 $i++;
531 }
532
533 $objectresp = array(
534 'result' => array('result_code' => 'OK', 'result_label' => ''),
535 'invoices' => $linesinvoice
536
537 );
538 } else {
539 $error++;
540 $errorcode = $db->lasterrno();
541 $errorlabel = $db->lasterror();
542 }
543 }
544
545 if ($error) {
546 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
547 }
548
549 return $objectresp;
550}
551
552
560function createInvoice($authentication, $invoice)
561{
562 global $db, $conf;
563
564 $now = dol_now();
565
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'];
569 }
570
571 // Init and check authentication
572 $objectresp = array();
573 $errorcode = '';
574 $errorlabel = '';
575 $error = 0;
576 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
577
578 // Check parameters
579 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
580 $error++;
581 $errorcode = 'KO';
582 $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
583 }
584
585 if (!$error) {
586 $new_invoice = new Facture($db);
587 $new_invoice->socid = $invoice['thirdparty_id'];
588 $new_invoice->type = $invoice['type'];
589 $new_invoice->ref_ext = $invoice['ref_ext'];
590 $new_invoice->date = dol_stringtotime($invoice['date'], 'dayrfc');
591 $new_invoice->note_private = $invoice['note_private'];
592 $new_invoice->note_public = $invoice['note_public'];
593 $new_invoice->status = Facture::STATUS_DRAFT; // We start with status draft
594 $new_invoice->fk_project = (int) $invoice['project_id'];
595 $new_invoice->date_creation = $now;
596
597 //take mode_reglement and cond_reglement from thirdparty
598 $soc = new Societe($db);
599 $res = $soc->fetch($new_invoice->socid);
600 if ($res > 0) {
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;
603 } else {
604 $new_invoice->mode_reglement_id = (int) $invoice['payment_mode_id'];
605 }
606
607 // Trick because nusoap does not store data with same structure if there is one or several lines
608 $arrayoflines = array();
609 if (isset($invoice['lines']['line'][0])) {
610 $arrayoflines = $invoice['lines']['line']; // @phan-suppress-current-line PhanTypeInvalidDimOffset
611 } else {
612 $arrayoflines = $invoice['lines'];
613 }
614 if (!is_array($arrayoflines)) {
615 $arrayoflines = array();
616 }
617
618 foreach ($arrayoflines as $line) {
619 // $key can be 'line' or '0','1',...
620 $newline = new FactureLigne($db);
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'];
630 $newline->date_start = dol_stringtotime($line['date_start']);
631 $newline->date_end = dol_stringtotime($line['date_end']);
632
633 $new_invoice->lines[] = $newline;
634 }
635 //var_dump($newobject->date_lim_reglement); exit;
636 //var_dump($invoice['lines'][0]['type']);
637
638 $db->begin();
639
640 $result = $new_invoice->create($fuser, 0, dol_stringtotime($invoice['date_due'], 'dayrfc'));
641 if ($result < 0) {
642 $error++;
643 }
644
645 if (!$error && $invoice['status'] == Facture::STATUS_VALIDATED) { // We want invoice to have status validated
646 $result = $new_invoice->validate($fuser);
647 if ($result < 0) {
648 $error++;
649 }
650 }
651
652 if (!$error) {
653 $db->commit();
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);
656 } else {
657 $db->rollback();
658 $error++;
659 $errorcode = 'KO';
660 $errorlabel = $new_invoice->error;
661 dol_syslog("Function: createInvoice error while creating".$errorlabel);
662 }
663 }
664
665 if ($error) {
666 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
667 }
668
669 return $objectresp;
670}
671
681function createInvoiceFromOrder($authentication, $id_order = 0, $ref_order = '', $ref_ext_order = '')
682{
683 global $db, $conf;
684
685 dol_syslog("Function: createInvoiceFromOrder login=".$authentication['login']." id=".$id_order.", ref=".$ref_order.", ref_ext=".$ref_ext_order);
686
687 if ($authentication['entity']) {
688 $conf->entity = $authentication['entity'];
689 }
690
691 // Init and check authentication
692 $objectresp = array();
693 $errorcode = '';
694 $errorlabel = '';
695 $error = 0;
696 $newobject = null;
697 $socid = 0;
698 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
699 if ($fuser->socid) {
700 $socid = $fuser->socid;
701 }
702
703 // Check parameters
704 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
705 $error++;
706 $errorcode = 'KO';
707 $errorlabel = "order id or ref or ref_ext is mandatory.";
708 }
709
711 if (!$error) {
712 $fuser->loadRights();
713
714 if ($fuser->hasRight('commande', 'lire')) {
715 $order = new Commande($db);
716 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
717 if ($result > 0) {
718 // Security for external user
719 if ($socid && ($socid != $order->socid)) {
720 $error++;
721 $errorcode = 'PERMISSION_DENIED';
722 $errorlabel = $order->socid.'User does not have permission for this request';
723 }
724
725 if (!$error) {
726 $newobject = new Facture($db);
727 $result = $newobject->createFromOrder($order, $fuser);
728
729 if ($result < 0) {
730 $error++;
731 dol_syslog("Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
732 }
733 }
734 } else {
735 $error++;
736 $errorcode = 'NOT_FOUND';
737 $errorlabel = 'Object not found for id='.$id_order.' nor ref='.$ref_order.' nor ref_ext='.$ref_ext_order;
738 }
739 } else {
740 $error++;
741 $errorcode = 'PERMISSION_DENIED';
742 $errorlabel = 'User does not have permission for this request';
743 }
744 }
745
746 if ($error || $newobject === null) {
747 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
748 } else {
749 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref, 'ref_ext' => $newobject->ref_ext);
750 }
751
752 return $objectresp;
753}
754
762function updateInvoice($authentication, $invoice)
763{
764 global $db, $conf, $langs;
765
766 dol_syslog("Function: updateInvoice login=".$authentication['login']." id=".$invoice['id'].
767 ", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
768
769 if ($authentication['entity']) {
770 $conf->entity = $authentication['entity'];
771 }
772
773 // Init and check authentication
774 $objectresp = array();
775 $errorcode = '';
776 $errorlabel = '';
777 $error = 0;
778 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
779
780 // Check parameters
781 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
782 $error++;
783 $errorcode = 'KO';
784 $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
785 }
786
787 if (!$error) {
788 $objectfound = false;
789
790 $object = new Facture($db);
791 $result = $object->fetch((int) $invoice['id'], $invoice['ref'], $invoice['ref_ext'], 0);
792
793 if (!empty($object->id)) {
794 $objectfound = true;
795
796 $db->begin();
797
798 if (isset($invoice['status'])) {
799 if ($invoice['status'] == Facture::STATUS_DRAFT) {
800 $result = $object->setDraft($fuser);
801 }
802 if ($invoice['status'] == Facture::STATUS_VALIDATED) {
803 $result = $object->validate($fuser);
804
805 if ($result >= 0) {
806 // Define output language
807 $outputlangs = $langs;
808 $object->generateDocument($object->model_pdf, $outputlangs);
809 }
810 }
811 if ($invoice['status'] == Facture::STATUS_CLOSED) {
812 $result = $object->setPaid($fuser, (string) $invoice['close_code'], (string) $invoice['close_note']);
813 }
814 if ($invoice['status'] == Facture::STATUS_ABANDONED) {
815 $result = $object->setCanceled($fuser, (string) $invoice['close_code'], (string) $invoice['close_note']);
816 }
817 }
818 }
819
820 if ((!$error) && ($objectfound)) {
821 $db->commit();
822 $objectresp = array(
823 'result' => array('result_code' => 'OK', 'result_label' => ''),
824 'id' => $object->id,
825 'ref' => $object->ref,
826 'ref_ext' => $object->ref_ext
827 );
828 } elseif ($objectfound) {
829 $db->rollback();
830 $error++;
831 $errorcode = 'KO';
832 $errorlabel = $object->error;
833 } else {
834 $error++;
835 $errorcode = 'NOT_FOUND';
836 $errorlabel = 'Invoice id='.$invoice['id'].' ref='.$invoice['ref'].' ref_ext='.$invoice['ref_ext'].' cannot be found';
837 }
838 }
839
840 if ($error) {
841 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
842 }
843
844 return $objectresp;
845}
846
847// Return the results.
848$server->service(file_get_contents("php://input"));
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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...
Definition date.lib.php:435
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
Definition ws.lib.php:37