dolibarr 21.0.0-alpha
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 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25if (!defined('NOCSRFCHECK')) {
26 define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
27}
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
39}
40if (!defined("NOLOGIN")) {
41 define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
42}
43if (!defined("NOSESSION")) {
44 define("NOSESSION", '1');
45}
46
47require '../main.inc.php';
48require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
49require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
50require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
51require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
52
53require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
54require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
55
56
57dol_syslog("Call Dolibarr webservices interfaces");
58
59$langs->load("main");
60
61// Enable and test if module web services is enabled
62if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
63 $langs->load("admin");
64 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
65 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
66 print $langs->trans("ToActivateModule");
67 exit;
68}
69
70// Create the soap Object
71$server = new nusoap_server();
72$server->soap_defencoding = 'UTF-8';
73$server->decode_utf8 = false;
74$ns = 'http://www.dolibarr.org/ns/';
75$server->configureWSDL('WebServicesDolibarrInvoice', $ns);
76$server->wsdl->schemaTargetNamespace = $ns;
77
78
79// Define WSDL Authentication object
80$server->wsdl->addComplexType(
81 'authentication',
82 'complexType',
83 'struct',
84 'all',
85 '',
86 array(
87 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
88 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
89 'login' => array('name' => 'login', 'type' => 'xsd:string'),
90 'password' => array('name' => 'password', 'type' => 'xsd:string'),
91 'entity' => array('name' => 'entity', 'type' => 'xsd:string')
92 )
93);
94// Define WSDL Return object
95$server->wsdl->addComplexType(
96 'result',
97 'complexType',
98 'struct',
99 'all',
100 '',
101 array(
102 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
103 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
104 )
105);
106
107// Define other specific objects
108$server->wsdl->addComplexType(
109 'line',
110 'complexType',
111 'struct',
112 'all',
113 '',
114 array(
115 'id' => array('name' => 'id', 'type' => 'xsd:string'),
116 'type' => array('name' => 'type', 'type' => 'xsd:int'),
117 'desc' => array('name' => 'desc', 'type' => 'xsd:string'),
118 'vat_rate' => array('name' => 'vat_rate', 'type' => 'xsd:double'),
119 'qty' => array('name' => 'qty', 'type' => 'xsd:double'),
120 'unitprice' => array('name' => 'unitprice', 'type' => 'xsd:double'),
121 'total_net' => array('name' => 'total_net', 'type' => 'xsd:double'),
122 'total_vat' => array('name' => 'total_vat', 'type' => 'xsd:double'),
123 'total' => array('name' => 'total', 'type' => 'xsd:double'),
124 'date_start' => array('name' => 'date_start', 'type' => 'xsd:date'),
125 'date_end' => array('name' => 'date_end', 'type' => 'xsd:date'),
126 // From product
127 'product_id' => array('name' => 'product_id', 'type' => 'xsd:int'),
128 'product_ref' => array('name' => 'product_ref', 'type' => 'xsd:string'),
129 'product_label' => array('name' => 'product_label', 'type' => 'xsd:string'),
130 'product_desc' => array('name' => 'product_desc', 'type' => 'xsd:string')
131 )
132);
133
134/*$server->wsdl->addComplexType(
135 'LinesArray',
136 'complexType',
137 'array',
138 '',
139 'SOAP-ENC:Array',
140 array(),
141 array(
142 array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:line[]')
143 ),
144 'tns:line'
145);*/
146$server->wsdl->addComplexType(
147 'LinesArray2',
148 'complexType',
149 'array',
150 'sequence',
151 '',
152 array(
153 'line' => array(
154 'name' => 'line',
155 'type' => 'tns:line',
156 'minOccurs' => '0',
157 'maxOccurs' => 'unbounded'
158 )
159 ),
160 null,
161 'tns:line'
162);
163
164
165$server->wsdl->addComplexType(
166 'invoice',
167 'complexType',
168 'struct',
169 'all',
170 '',
171 array(
172 'id' => array('name' => 'id', 'type' => 'xsd:string'),
173 'ref' => array('name' => 'ref', 'type' => 'xsd:string'),
174 'ref_ext' => array('name' => 'ref_ext', 'type' => 'xsd:string'),
175 'thirdparty_id' => array('name' => 'thirdparty_id', 'type' => 'xsd:int'),
176 'fk_user_author' => array('name' => 'fk_user_author', 'type' => 'xsd:string'),
177 'fk_user_valid' => array('name' => 'fk_user_valid', 'type' => 'xsd:string'),
178 'date' => array('name' => 'date', 'type' => 'xsd:date'),
179 'date_due' => array('name' => 'date_due', 'type' => 'xsd:date'),
180 'date_creation' => array('name' => 'date_creation', 'type' => 'xsd:dateTime'),
181 'date_validation' => array('name' => 'date_validation', 'type' => 'xsd:dateTime'),
182 'date_modification' => array('name' => 'date_modification', 'type' => 'xsd:dateTime'),
183 'payment_mode_id' => array('name' => 'payment_mode_id', 'type' => 'xsd:string'),
184 'type' => array('name' => 'type', 'type' => 'xsd:int'),
185 'total_net' => array('name' => 'type', 'type' => 'xsd:double'),
186 'total_vat' => array('name' => 'type', 'type' => 'xsd:double'),
187 'total' => array('name' => 'type', 'type' => 'xsd:double'),
188 'note_private' => array('name' => 'note_private', 'type' => 'xsd:string'),
189 'note_public' => array('name' => 'note_public', 'type' => 'xsd:string'),
190 'status' => array('name' => 'status', 'type' => 'xsd:int'),
191 'close_code' => array('name' => 'close_code', 'type' => 'xsd:string'),
192 'close_note' => array('name' => 'close_note', 'type' => 'xsd:string'),
193 'project_id' => array('name' => 'project_id', 'type' => 'xsd:string'),
194 'lines' => array('name' => 'lines', 'type' => 'tns:LinesArray2')
195 )
196);
197/*
198$server->wsdl->addComplexType(
199 'InvoicesArray',
200 'complexType',
201 'array',
202 '',
203 'SOAP-ENC:Array',
204 array(),
205 array(
206 array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:invoice[]')
207 ),
208 'tns:invoice'
209);*/
210$server->wsdl->addComplexType(
211 'InvoicesArray2',
212 'complexType',
213 'array',
214 'sequence',
215 '',
216 array(
217 'invoice' => array(
218 'name' => 'invoice',
219 'type' => 'tns:invoice',
220 'minOccurs' => '0',
221 'maxOccurs' => 'unbounded'
222 )
223 ),
224 null,
225 'tns:invoice'
226);
227
228
229
230// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
231// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
232// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
233$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
234$styleuse = 'encoded'; // encoded/literal/literal wrapped
235// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
236
237// Register WSDL
238$server->register(
239 'getInvoice',
240 // Entry values
241 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
242 // Exit values
243 array('result' => 'tns:result', 'invoice' => 'tns:invoice'),
244 $ns,
245 $ns.'#getInvoice',
246 $styledoc,
247 $styleuse,
248 'WS to get a particular invoice'
249);
250$server->register(
251 'getInvoicesForThirdParty',
252 // Entry values
253 array('authentication' => 'tns:authentication', 'idthirdparty' => 'xsd:string'),
254 // Exit values
255 array('result' => 'tns:result', 'invoices' => 'tns:InvoicesArray2'),
256 $ns,
257 $ns.'#getInvoicesForThirdParty',
258 $styledoc,
259 $styleuse,
260 'WS to get all invoices of a third party'
261);
262$server->register(
263 'createInvoice',
264 // Entry values
265 array('authentication' => 'tns:authentication', 'invoice' => 'tns:invoice'),
266 // Exit values
267 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
268 $ns,
269 $ns.'#createInvoice',
270 $styledoc,
271 $styleuse,
272 'WS to create an invoice'
273);
274$server->register(
275 'createInvoiceFromOrder',
276 // Entry values
277 array('authentication' => 'tns:authentication', 'id_order' => 'xsd:string', 'ref_order' => 'xsd:string', 'ref_ext_order' => 'xsd:string'),
278 // Exit values
279 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
280 $ns,
281 $ns.'#createInvoiceFromOrder',
282 $styledoc,
283 $styleuse,
284 'WS to create an invoice from an order'
285);
286$server->register(
287 'updateInvoice',
288 // Entry values
289 array('authentication' => 'tns:authentication', 'invoice' => 'tns:invoice'),
290 // Exit values
291 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
292 $ns,
293 $ns.'#updateInvoice',
294 $styledoc,
295 $styleuse,
296 'WS to update an invoice'
297);
298
299
309function getInvoice($authentication, $id = 0, $ref = '', $ref_ext = '')
310{
311 global $db, $conf;
312
313 dol_syslog("Function: getInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
314
315 if ($authentication['entity']) {
316 $conf->entity = $authentication['entity'];
317 }
318
319 // Init and check authentication
320 $objectresp = array();
321 $errorcode = '';
322 $errorlabel = '';
323 $error = 0;
324 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
325 // Check parameters
326 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
327 $error++;
328 $errorcode = 'BAD_PARAMETERS';
329 $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
330 }
331
332 if (!$error) {
333 $fuser->loadRights();
334
335 if ($fuser->hasRight('facture', 'lire')) {
336 $invoice = new Facture($db);
337 $result = $invoice->fetch($id, $ref, $ref_ext);
338 if ($result > 0) {
339 $linesresp = array();
340 $i = 0;
341 foreach ($invoice->lines as $line) {
342 //var_dump($line); exit;
343 $linesresp[] = array(
344 'id' => $line->id,
345 'type' => $line->product_type,
346 'desc' => dol_htmlcleanlastbr($line->desc),
347 'total_net' => $line->total_ht,
348 'total_vat' => $line->total_tva,
349 'total' => $line->total_ttc,
350 'vat_rate' => $line->tva_tx,
351 'qty' => $line->qty,
352 'unitprice' => $line->subprice,
353 'date_start' => $line->date_start ? dol_print_date($line->date_start, 'dayrfc') : '',
354 'date_end' => $line->date_end ? dol_print_date($line->date_end, 'dayrfc') : '',
355 'product_id' => $line->fk_product,
356 'product_ref' => $line->product_ref,
357 'product_label' => $line->product_label,
358 'product_desc' => $line->product_desc,
359 );
360 $i++;
361 }
362
363 // Create invoice
364 $objectresp = array(
365 'result' => array('result_code' => 'OK', 'result_label' => ''),
366 'invoice' => array(
367 'id' => $invoice->id,
368 'ref' => $invoice->ref,
369 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
370 'thirdparty_id' => $invoice->socid,
371 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author : '',
372 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id : '',
373 'date' => $invoice->date ? dol_print_date($invoice->date, 'dayrfc') : '',
374 'date_due' => $invoice->date_lim_reglement ? dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
375 'date_creation' => $invoice->date_creation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
376 'date_validation' => $invoice->date_validation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
377 'date_modification' => $invoice->datem ? dol_print_date($invoice->datem, 'dayhourrfc') : '',
378 'type' => $invoice->type,
379 'total_net' => $invoice->total_ht,
380 'total_vat' => $invoice->total_tva,
381 'total' => $invoice->total_ttc,
382 'note_private' => $invoice->note_private ? $invoice->note_private : '',
383 'note_public' => $invoice->note_public ? $invoice->note_public : '',
384 'status' => $invoice->statut,
385 'project_id' => $invoice->fk_project,
386 'close_code' => $invoice->close_code ? $invoice->close_code : '',
387 'close_note' => $invoice->close_note ? $invoice->close_note : '',
388 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
389 'lines' => $linesresp
390 ));
391 } else {
392 $error++;
393 $errorcode = 'NOT_FOUND';
394 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
395 }
396 } else {
397 $error++;
398 $errorcode = 'PERMISSION_DENIED';
399 $errorlabel = 'User does not have permission for this request';
400 }
401 }
402
403 if ($error) {
404 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
405 }
406
407 return $objectresp;
408}
409
410
418function getInvoicesForThirdParty($authentication, $idthirdparty)
419{
420 global $db, $conf;
421
422 dol_syslog("Function: getInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
423
424 if ($authentication['entity']) {
425 $conf->entity = $authentication['entity'];
426 }
427
428 // Init and check authentication
429 $objectresp = array();
430 $errorcode = '';
431 $errorlabel = '';
432 $error = 0;
433 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
434
435 if ($fuser->socid) {
436 $socid = $fuser->socid;
437 }
438
439 // Check parameters
440 if (!$error && empty($idthirdparty)) {
441 $error++;
442 $errorcode = 'BAD_PARAMETERS';
443 $errorlabel = 'Parameter idthirdparty is not provided';
444 }
445
446 if (!$error) {
447 $linesinvoice = array();
448
449 $sql = 'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
450 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f';
451 $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
452 if ($idthirdparty != 'all') {
453 $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
454 }
455
456 $resql = $db->query($sql);
457 if ($resql) {
458 $num = $db->num_rows($resql);
459 $i = 0;
460 while ($i < $num) {
461 // En attendant remplissage par boucle
462 $obj = $db->fetch_object($resql);
463
464 $invoice = new Facture($db);
465 $invoice->fetch($obj->facid);
466
467 // Sécurité pour utilisateur externe
468 if ($socid && ($socid != $invoice->socid)) {
469 $error++;
470 $errorcode = 'PERMISSION_DENIED';
471 $errorlabel = $invoice->socid.' User does not have permission for this request';
472 }
473
474 if (!$error) {
475 // Define lines of invoice
476 $linesresp = array();
477 foreach ($invoice->lines as $line) {
478 $linesresp[] = array(
479 'id' => $line->id,
480 'type' => $line->product_type,
481 'total_net' => $line->total_ht,
482 'total_vat' => $line->total_tva,
483 'total' => $line->total_ttc,
484 'vat_rate' => $line->tva_tx,
485 'qty' => $line->qty,
486 'unitprice' => $line->subprice,
487 'date_start' => $line->date_start ? dol_print_date($line->date_start, 'dayrfc') : '',
488 'date_end' => $line->date_end ? dol_print_date($line->date_end, 'dayrfc') : '',
489 'product_id' => $line->fk_product,
490 'product_ref' => $line->product_ref,
491 'product_label' => $line->product_label,
492 'product_desc' => $line->product_desc,
493 );
494 }
495
496 // Now define invoice
497 $linesinvoice[] = array(
498 'id' => $invoice->id,
499 'ref' => $invoice->ref,
500 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
501 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author : '',
502 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id : '',
503 'date' => $invoice->date ? dol_print_date($invoice->date, 'dayrfc') : '',
504 'date_due' => $invoice->date_lim_reglement ? dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
505 'date_creation' => $invoice->date_creation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
506 'date_validation' => $invoice->date_validation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
507 'date_modification' => $invoice->datem ? dol_print_date($invoice->datem, 'dayhourrfc') : '',
508 'type' => $invoice->type,
509 'total_net' => $invoice->total_ht,
510 'total_vat' => $invoice->total_tva,
511 'total' => $invoice->total_ttc,
512 'note_private' => $invoice->note_private ? $invoice->note_private : '',
513 'note_public' => $invoice->note_public ? $invoice->note_public : '',
514 'status' => $invoice->statut,
515 'project_id' => $invoice->fk_project,
516 'close_code' => $invoice->close_code ? $invoice->close_code : '',
517 'close_note' => $invoice->close_note ? $invoice->close_note : '',
518 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
519 'lines' => $linesresp
520 );
521 }
522
523 $i++;
524 }
525
526 $objectresp = array(
527 'result' => array('result_code' => 'OK', 'result_label' => ''),
528 'invoices' => $linesinvoice
529
530 );
531 } else {
532 $error++;
533 $errorcode = $db->lasterrno();
534 $errorlabel = $db->lasterror();
535 }
536 }
537
538 if ($error) {
539 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
540 }
541
542 return $objectresp;
543}
544
545
553function createInvoice($authentication, $invoice)
554{
555 global $db, $conf;
556
557 $now = dol_now();
558
559 dol_syslog("Function: createInvoice login=".$authentication['login']." id=".$invoice['id'].", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
560
561 if ($authentication['entity']) {
562 $conf->entity = $authentication['entity'];
563 }
564
565 // Init and check authentication
566 $objectresp = array();
567 $errorcode = '';
568 $errorlabel = '';
569 $error = 0;
570 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
571
572 // Check parameters
573 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
574 $error++;
575 $errorcode = 'KO';
576 $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
577 }
578
579 if (!$error) {
580 $new_invoice = new Facture($db);
581 $new_invoice->socid = $invoice['thirdparty_id'];
582 $new_invoice->type = $invoice['type'];
583 $new_invoice->ref_ext = $invoice['ref_ext'];
584 $new_invoice->date = dol_stringtotime($invoice['date'], 'dayrfc');
585 $new_invoice->note_private = $invoice['note_private'];
586 $new_invoice->note_public = $invoice['note_public'];
587 $new_invoice->statut = Facture::STATUS_DRAFT; // We start with status draft
588 $new_invoice->fk_project = $invoice['project_id'];
589 $new_invoice->date_creation = $now;
590
591 //take mode_reglement and cond_reglement from thirdparty
592 $soc = new Societe($db);
593 $res = $soc->fetch($new_invoice->socid);
594 if ($res > 0) {
595 $new_invoice->mode_reglement_id = !empty($invoice['payment_mode_id']) ? $invoice['payment_mode_id'] : $soc->mode_reglement_id;
596 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
597 } else {
598 $new_invoice->mode_reglement_id = $invoice['payment_mode_id'];
599 }
600
601 // Trick because nusoap does not store data with same structure if there is one or several lines
602 $arrayoflines = array();
603 if (isset($invoice['lines']['line'][0])) {
604 $arrayoflines = $invoice['lines']['line'];
605 } else {
606 $arrayoflines = $invoice['lines'];
607 }
608
609 foreach ($arrayoflines as $line) {
610 // $key can be 'line' or '0','1',...
611 $newline = new FactureLigne($db);
612 $newline->product_type = $line['type'];
613 $newline->desc = $line['desc'];
614 $newline->fk_product = $line['product_id'];
615 $newline->tva_tx = isset($line['vat_rate']) ? $line['vat_rate'] : 0;
616 $newline->qty = $line['qty'];
617 $newline->subprice = isset($line['unitprice']) ? $line['unitprice'] : null;
618 $newline->total_ht = $line['total_net'];
619 $newline->total_tva = $line['total_vat'];
620 $newline->total_ttc = $line['total'];
621 $newline->date_start = dol_stringtotime($line['date_start']);
622 $newline->date_end = dol_stringtotime($line['date_end']);
623
624 $new_invoice->lines[] = $newline;
625 }
626 //var_dump($newobject->date_lim_reglement); exit;
627 //var_dump($invoice['lines'][0]['type']);
628
629 $db->begin();
630
631 $result = $new_invoice->create($fuser, 0, dol_stringtotime($invoice['date_due'], 'dayrfc'));
632 if ($result < 0) {
633 $error++;
634 }
635
636 if (!$error && $invoice['status'] == Facture::STATUS_VALIDATED) { // We want invoice to have status validated
637 $result = $new_invoice->validate($fuser);
638 if ($result < 0) {
639 $error++;
640 }
641 }
642
643 if (!$error) {
644 $db->commit();
645 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $new_invoice->id,
646 'ref' => $new_invoice->ref, 'ref_ext' => $new_invoice->ref_ext);
647 } else {
648 $db->rollback();
649 $error++;
650 $errorcode = 'KO';
651 $errorlabel = $new_invoice->error;
652 dol_syslog("Function: createInvoice error while creating".$errorlabel);
653 }
654 }
655
656 if ($error) {
657 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
658 }
659
660 return $objectresp;
661}
662
672function createInvoiceFromOrder($authentication, $id_order = '', $ref_order = '', $ref_ext_order = '')
673{
674 global $db, $conf;
675
676 dol_syslog("Function: createInvoiceFromOrder login=".$authentication['login']." id=".$id_order.", ref=".$ref_order.", ref_ext=".$ref_ext_order);
677
678 if ($authentication['entity']) {
679 $conf->entity = $authentication['entity'];
680 }
681
682 // Init and check authentication
683 $objectresp = array();
684 $errorcode = '';
685 $errorlabel = '';
686 $error = 0;
687 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
688 if ($fuser->socid) {
689 $socid = $fuser->socid;
690 }
691
692 // Check parameters
693 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
694 $error++;
695 $errorcode = 'KO';
696 $errorlabel = "order id or ref or ref_ext is mandatory.";
697 }
698
700 if (!$error) {
701 $fuser->loadRights();
702
703 if ($fuser->hasRight('commande', 'lire')) {
704 $order = new Commande($db);
705 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
706 if ($result > 0) {
707 // Security for external user
708 if ($socid && ($socid != $order->socid)) {
709 $error++;
710 $errorcode = 'PERMISSION_DENIED';
711 $errorlabel = $order->socid.'User does not have permission for this request';
712 }
713
714 if (!$error) {
715 $newobject = new Facture($db);
716 $result = $newobject->createFromOrder($order, $fuser);
717
718 if ($result < 0) {
719 $error++;
720 dol_syslog("Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
721 }
722 }
723 } else {
724 $error++;
725 $errorcode = 'NOT_FOUND';
726 $errorlabel = 'Object not found for id='.$id_order.' nor ref='.$ref_order.' nor ref_ext='.$ref_ext_order;
727 }
728 } else {
729 $error++;
730 $errorcode = 'PERMISSION_DENIED';
731 $errorlabel = 'User does not have permission for this request';
732 }
733 }
734
735 if ($error) {
736 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
737 } else {
738 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref, 'ref_ext' => $newobject->ref_ext);
739 }
740
741 return $objectresp;
742}
743
751function updateInvoice($authentication, $invoice)
752{
753 global $db, $conf, $langs;
754
755 dol_syslog("Function: updateInvoice login=".$authentication['login']." id=".$invoice['id'].
756 ", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
757
758 if ($authentication['entity']) {
759 $conf->entity = $authentication['entity'];
760 }
761
762 // Init and check authentication
763 $objectresp = array();
764 $errorcode = '';
765 $errorlabel = '';
766 $error = 0;
767 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
768
769 // Check parameters
770 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
771 $error++;
772 $errorcode = 'KO';
773 $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
774 }
775
776 if (!$error) {
777 $objectfound = false;
778
779 $object = new Facture($db);
780 $result = $object->fetch($invoice['id'], $invoice['ref'], $invoice['ref_ext'], '');
781
782 if (!empty($object->id)) {
783 $objectfound = true;
784
785 $db->begin();
786
787 if (isset($invoice['status'])) {
788 if ($invoice['status'] == Facture::STATUS_DRAFT) {
789 $result = $object->setDraft($fuser);
790 }
791 if ($invoice['status'] == Facture::STATUS_VALIDATED) {
792 $result = $object->validate($fuser);
793
794 if ($result >= 0) {
795 // Define output language
796 $outputlangs = $langs;
797 $object->generateDocument($object->model_pdf, $outputlangs);
798 }
799 }
800 if ($invoice['status'] == Facture::STATUS_CLOSED) {
801 $result = $object->setPaid($fuser, $invoice['close_code'], $invoice['close_note']);
802 }
803 if ($invoice['status'] == Facture::STATUS_ABANDONED) {
804 $result = $object->setCanceled($fuser, $invoice['close_code'], $invoice['close_note']);
805 }
806 }
807 }
808
809 if ((!$error) && ($objectfound)) {
810 $db->commit();
811 $objectresp = array(
812 'result' => array('result_code' => 'OK', 'result_label' => ''),
813 'id' => $object->id,
814 'ref' => $object->ref,
815 'ref_ext' => $object->ref_ext
816 );
817 } elseif ($objectfound) {
818 $db->rollback();
819 $error++;
820 $errorcode = 'KO';
821 $errorlabel = $object->error;
822 } else {
823 $error++;
824 $errorcode = 'NOT_FOUND';
825 $errorlabel = 'Invoice id='.$invoice['id'].' ref='.$invoice['ref'].' ref_ext='.$invoice['ref_ext'].' cannot be found';
826 }
827 }
828
829 if ($error) {
830 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
831 }
832
833 return $objectresp;
834}
835
836// Return the results.
837$server->service(file_get_contents("php://input"));
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:427
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.
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.
Definition ws.lib.php:37