dolibarr 21.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 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 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$server->wsdl->schemaTargetNamespace = $ns;
82
83
84// Define WSDL Authentication object
85$server->wsdl->addComplexType(
86 'authentication',
87 'complexType',
88 'struct',
89 'all',
90 '',
91 array(
92 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
93 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
94 'login' => array('name' => 'login', 'type' => 'xsd:string'),
95 'password' => array('name' => 'password', 'type' => 'xsd:string'),
96 'entity' => array('name' => 'entity', 'type' => 'xsd:string')
97 )
98);
99// Define WSDL Return object
100$server->wsdl->addComplexType(
101 'result',
102 'complexType',
103 'struct',
104 'all',
105 '',
106 array(
107 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
108 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
109 )
110);
111
112// Define other specific objects
113$server->wsdl->addComplexType(
114 'line',
115 'complexType',
116 'struct',
117 'all',
118 '',
119 array(
120 'id' => array('name' => 'id', 'type' => 'xsd:string'),
121 'type' => array('name' => 'type', 'type' => 'xsd:int'),
122 'desc' => array('name' => 'desc', 'type' => 'xsd:string'),
123 'vat_rate' => array('name' => 'vat_rate', 'type' => 'xsd:double'),
124 'qty' => array('name' => 'qty', 'type' => 'xsd:double'),
125 'unitprice' => array('name' => 'unitprice', 'type' => 'xsd:double'),
126 'total_net' => array('name' => 'total_net', 'type' => 'xsd:double'),
127 'total_vat' => array('name' => 'total_vat', 'type' => 'xsd:double'),
128 'total' => array('name' => 'total', 'type' => 'xsd:double'),
129 'date_start' => array('name' => 'date_start', 'type' => 'xsd:date'),
130 'date_end' => array('name' => 'date_end', 'type' => 'xsd:date'),
131 // From product
132 'product_id' => array('name' => 'product_id', 'type' => 'xsd:int'),
133 'product_ref' => array('name' => 'product_ref', 'type' => 'xsd:string'),
134 'product_label' => array('name' => 'product_label', 'type' => 'xsd:string'),
135 'product_desc' => array('name' => 'product_desc', 'type' => 'xsd:string')
136 )
137);
138
139/*$server->wsdl->addComplexType(
140 'LinesArray',
141 'complexType',
142 'array',
143 '',
144 'SOAP-ENC:Array',
145 array(),
146 array(
147 array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:line[]')
148 ),
149 'tns:line'
150);*/
151$server->wsdl->addComplexType(
152 'LinesArray2',
153 'complexType',
154 'array',
155 'sequence',
156 '',
157 array(
158 'line' => array(
159 'name' => 'line',
160 'type' => 'tns:line',
161 'minOccurs' => '0',
162 'maxOccurs' => 'unbounded'
163 )
164 ),
165 array(),
166 'tns:line'
167);
168
169
170$server->wsdl->addComplexType(
171 'invoice',
172 'complexType',
173 'struct',
174 'all',
175 '',
176 array(
177 'id' => array('name' => 'id', 'type' => 'xsd:string'),
178 'ref' => array('name' => 'ref', 'type' => 'xsd:string'),
179 'ref_ext' => array('name' => 'ref_ext', 'type' => 'xsd:string'),
180 'thirdparty_id' => array('name' => 'thirdparty_id', 'type' => 'xsd:int'),
181 'fk_user_author' => array('name' => 'fk_user_author', 'type' => 'xsd:string'),
182 'fk_user_valid' => array('name' => 'fk_user_valid', 'type' => 'xsd:string'),
183 'date' => array('name' => 'date', 'type' => 'xsd:date'),
184 'date_due' => array('name' => 'date_due', 'type' => 'xsd:date'),
185 'date_creation' => array('name' => 'date_creation', 'type' => 'xsd:dateTime'),
186 'date_validation' => array('name' => 'date_validation', 'type' => 'xsd:dateTime'),
187 'date_modification' => array('name' => 'date_modification', 'type' => 'xsd:dateTime'),
188 'payment_mode_id' => array('name' => 'payment_mode_id', 'type' => 'xsd:string'),
189 'type' => array('name' => 'type', 'type' => 'xsd:int'),
190 'total_net' => array('name' => 'type', 'type' => 'xsd:double'),
191 'total_vat' => array('name' => 'type', 'type' => 'xsd:double'),
192 'total' => array('name' => 'type', 'type' => 'xsd:double'),
193 'note_private' => array('name' => 'note_private', 'type' => 'xsd:string'),
194 'note_public' => array('name' => 'note_public', 'type' => 'xsd:string'),
195 'status' => array('name' => 'status', 'type' => 'xsd:int'),
196 'close_code' => array('name' => 'close_code', 'type' => 'xsd:string'),
197 'close_note' => array('name' => 'close_note', 'type' => 'xsd:string'),
198 'project_id' => array('name' => 'project_id', 'type' => 'xsd:string'),
199 'lines' => array('name' => 'lines', 'type' => 'tns:LinesArray2')
200 )
201);
202/*
203$server->wsdl->addComplexType(
204 'InvoicesArray',
205 'complexType',
206 'array',
207 '',
208 'SOAP-ENC:Array',
209 array(),
210 array(
211 array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:invoice[]')
212 ),
213 'tns:invoice'
214);*/
215$server->wsdl->addComplexType(
216 'InvoicesArray2',
217 'complexType',
218 'array',
219 'sequence',
220 '',
221 array(
222 'invoice' => array(
223 'name' => 'invoice',
224 'type' => 'tns:invoice',
225 'minOccurs' => '0',
226 'maxOccurs' => 'unbounded'
227 )
228 ),
229 array(),
230 'tns:invoice'
231);
232
233
234
235// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
236// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
237// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
238$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
239$styleuse = 'encoded'; // encoded/literal/literal wrapped
240// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
241
242// Register WSDL
243$server->register(
244 'getInvoice',
245 // Entry values
246 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
247 // Exit values
248 array('result' => 'tns:result', 'invoice' => 'tns:invoice'),
249 $ns,
250 $ns.'#getInvoice',
251 $styledoc,
252 $styleuse,
253 'WS to get a particular invoice'
254);
255$server->register(
256 'getInvoicesForThirdParty',
257 // Entry values
258 array('authentication' => 'tns:authentication', 'idthirdparty' => 'xsd:string'),
259 // Exit values
260 array('result' => 'tns:result', 'invoices' => 'tns:InvoicesArray2'),
261 $ns,
262 $ns.'#getInvoicesForThirdParty',
263 $styledoc,
264 $styleuse,
265 'WS to get all invoices of a third party'
266);
267$server->register(
268 'createInvoice',
269 // Entry values
270 array('authentication' => 'tns:authentication', 'invoice' => 'tns:invoice'),
271 // Exit values
272 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
273 $ns,
274 $ns.'#createInvoice',
275 $styledoc,
276 $styleuse,
277 'WS to create an invoice'
278);
279$server->register(
280 'createInvoiceFromOrder',
281 // Entry values
282 array('authentication' => 'tns:authentication', 'id_order' => 'xsd:string', 'ref_order' => 'xsd:string', 'ref_ext_order' => 'xsd:string'),
283 // Exit values
284 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
285 $ns,
286 $ns.'#createInvoiceFromOrder',
287 $styledoc,
288 $styleuse,
289 'WS to create an invoice from an order'
290);
291$server->register(
292 'updateInvoice',
293 // Entry values
294 array('authentication' => 'tns:authentication', 'invoice' => 'tns:invoice'),
295 // Exit values
296 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
297 $ns,
298 $ns.'#updateInvoice',
299 $styledoc,
300 $styleuse,
301 'WS to update an invoice'
302);
303
304
314function getInvoice($authentication, $id = 0, $ref = '', $ref_ext = '')
315{
316 global $db, $conf;
317
318 dol_syslog("Function: getInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
319
320 if ($authentication['entity']) {
321 $conf->entity = $authentication['entity'];
322 }
323
324 // Init and check authentication
325 $objectresp = array();
326 $errorcode = '';
327 $errorlabel = '';
328 $error = 0;
329 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
330 // Check parameters
331 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
332 $error++;
333 $errorcode = 'BAD_PARAMETERS';
334 $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
335 }
336
337 if (!$error) {
338 $fuser->loadRights();
339
340 if ($fuser->hasRight('facture', 'lire')) {
341 $invoice = new Facture($db);
342 $result = $invoice->fetch($id, $ref, $ref_ext);
343 if ($result > 0) {
344 $linesresp = array();
345 $i = 0;
346 foreach ($invoice->lines as $line) {
347 //var_dump($line); exit;
348 $linesresp[] = array(
349 'id' => $line->id,
350 'type' => $line->product_type,
351 'desc' => dol_htmlcleanlastbr($line->desc),
352 'total_net' => $line->total_ht,
353 'total_vat' => $line->total_tva,
354 'total' => $line->total_ttc,
355 'vat_rate' => $line->tva_tx,
356 'qty' => $line->qty,
357 'unitprice' => $line->subprice,
358 'date_start' => $line->date_start ? dol_print_date($line->date_start, 'dayrfc') : '',
359 'date_end' => $line->date_end ? dol_print_date($line->date_end, 'dayrfc') : '',
360 'product_id' => $line->fk_product,
361 'product_ref' => $line->product_ref,
362 'product_label' => $line->product_label,
363 'product_desc' => $line->product_desc,
364 );
365 $i++;
366 }
367
368 // Create invoice
369 $objectresp = array(
370 'result' => array('result_code' => 'OK', 'result_label' => ''),
371 'invoice' => array(
372 'id' => $invoice->id,
373 'ref' => $invoice->ref,
374 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
375 'thirdparty_id' => $invoice->socid,
376 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author : '',
377 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id : '',
378 'date' => $invoice->date ? dol_print_date($invoice->date, 'dayrfc') : '',
379 'date_due' => $invoice->date_lim_reglement ? dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
380 'date_creation' => $invoice->date_creation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
381 'date_validation' => $invoice->date_validation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
382 'date_modification' => $invoice->datem ? dol_print_date($invoice->datem, 'dayhourrfc') : '',
383 'type' => $invoice->type,
384 'total_net' => $invoice->total_ht,
385 'total_vat' => $invoice->total_tva,
386 'total' => $invoice->total_ttc,
387 'note_private' => $invoice->note_private ? $invoice->note_private : '',
388 'note_public' => $invoice->note_public ? $invoice->note_public : '',
389 'status' => $invoice->statut,
390 'project_id' => $invoice->fk_project,
391 'close_code' => $invoice->close_code ? $invoice->close_code : '',
392 'close_note' => $invoice->close_note ? $invoice->close_note : '',
393 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
394 'lines' => $linesresp
395 ));
396 } else {
397 $error++;
398 $errorcode = 'NOT_FOUND';
399 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
400 }
401 } else {
402 $error++;
403 $errorcode = 'PERMISSION_DENIED';
404 $errorlabel = 'User does not have permission for this request';
405 }
406 }
407
408 if ($error) {
409 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
410 }
411
412 return $objectresp;
413}
414
415
423function getInvoicesForThirdParty($authentication, $idthirdparty)
424{
425 global $db, $conf;
426
427 dol_syslog("Function: getInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
428
429 if ($authentication['entity']) {
430 $conf->entity = $authentication['entity'];
431 }
432
433 // Init and check authentication
434 $objectresp = array();
435 $errorcode = '';
436 $errorlabel = '';
437 $error = 0;
438 $socid = 0;
439 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
440
441 if ($fuser->socid) {
442 $socid = $fuser->socid;
443 }
444
445 // Check parameters
446 if (!$error && empty($idthirdparty)) {
447 $error++;
448 $errorcode = 'BAD_PARAMETERS';
449 $errorlabel = 'Parameter idthirdparty is not provided';
450 }
451
452 if (!$error) {
453 $linesinvoice = array();
454
455 $sql = 'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
456 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f';
457 $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
458 if ($idthirdparty != 'all') {
459 $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
460 }
461
462 $resql = $db->query($sql);
463 if ($resql) {
464 $num = $db->num_rows($resql);
465 $i = 0;
466 while ($i < $num) {
467 // En attendant remplissage par boucle
468 $obj = $db->fetch_object($resql);
469
470 $invoice = new Facture($db);
471 $invoice->fetch($obj->facid);
472
473 // Sécurité pour utilisateur externe
474 if ($socid && ($socid != $invoice->socid)) {
475 $error++;
476 $errorcode = 'PERMISSION_DENIED';
477 $errorlabel = $invoice->socid.' User does not have permission for this request';
478 }
479
480 if (!$error) {
481 // Define lines of invoice
482 $linesresp = array();
483 foreach ($invoice->lines as $line) {
484 $linesresp[] = array(
485 'id' => $line->id,
486 'type' => $line->product_type,
487 'total_net' => $line->total_ht,
488 'total_vat' => $line->total_tva,
489 'total' => $line->total_ttc,
490 'vat_rate' => $line->tva_tx,
491 'qty' => $line->qty,
492 'unitprice' => $line->subprice,
493 'date_start' => $line->date_start ? dol_print_date($line->date_start, 'dayrfc') : '',
494 'date_end' => $line->date_end ? dol_print_date($line->date_end, 'dayrfc') : '',
495 'product_id' => $line->fk_product,
496 'product_ref' => $line->product_ref,
497 'product_label' => $line->product_label,
498 'product_desc' => $line->product_desc,
499 );
500 }
501
502 // Now define invoice
503 $linesinvoice[] = array(
504 'id' => $invoice->id,
505 'ref' => $invoice->ref,
506 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
507 'fk_user_author' => $invoice->fk_user_author ? $invoice->fk_user_author : '',
508 'fk_user_valid' => $invoice->user_validation_id ? $invoice->user_validation_id : '',
509 'date' => $invoice->date ? dol_print_date($invoice->date, 'dayrfc') : '',
510 'date_due' => $invoice->date_lim_reglement ? dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
511 'date_creation' => $invoice->date_creation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
512 'date_validation' => $invoice->date_validation ? dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
513 'date_modification' => $invoice->datem ? dol_print_date($invoice->datem, 'dayhourrfc') : '',
514 'type' => $invoice->type,
515 'total_net' => $invoice->total_ht,
516 'total_vat' => $invoice->total_tva,
517 'total' => $invoice->total_ttc,
518 'note_private' => $invoice->note_private ? $invoice->note_private : '',
519 'note_public' => $invoice->note_public ? $invoice->note_public : '',
520 'status' => $invoice->statut,
521 'project_id' => $invoice->fk_project,
522 'close_code' => $invoice->close_code ? $invoice->close_code : '',
523 'close_note' => $invoice->close_note ? $invoice->close_note : '',
524 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
525 'lines' => $linesresp
526 );
527 }
528
529 $i++;
530 }
531
532 $objectresp = array(
533 'result' => array('result_code' => 'OK', 'result_label' => ''),
534 'invoices' => $linesinvoice
535
536 );
537 } else {
538 $error++;
539 $errorcode = $db->lasterrno();
540 $errorlabel = $db->lasterror();
541 }
542 }
543
544 if ($error) {
545 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
546 }
547
548 return $objectresp;
549}
550
551
559function createInvoice($authentication, $invoice)
560{
561 global $db, $conf;
562
563 $now = dol_now();
564
565 dol_syslog("Function: createInvoice login=".$authentication['login']." id=".$invoice['id'].", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
566 if ($authentication['entity']) {
567 $conf->entity = $authentication['entity'];
568 }
569
570 // Init and check authentication
571 $objectresp = array();
572 $errorcode = '';
573 $errorlabel = '';
574 $error = 0;
575 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
576
577 // Check parameters
578 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
579 $error++;
580 $errorcode = 'KO';
581 $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
582 }
583
584 if (!$error) {
585 $new_invoice = new Facture($db);
586 $new_invoice->socid = $invoice['thirdparty_id'];
587 $new_invoice->type = $invoice['type'];
588 $new_invoice->ref_ext = $invoice['ref_ext'];
589 $new_invoice->date = dol_stringtotime($invoice['date'], 'dayrfc');
590 $new_invoice->note_private = $invoice['note_private'];
591 $new_invoice->note_public = $invoice['note_public'];
592 $new_invoice->statut = Facture::STATUS_DRAFT; // We start with status draft
593 $new_invoice->fk_project = (int) $invoice['project_id'];
594 $new_invoice->date_creation = $now;
595
596 //take mode_reglement and cond_reglement from thirdparty
597 $soc = new Societe($db);
598 $res = $soc->fetch($new_invoice->socid);
599 if ($res > 0) {
600 $new_invoice->mode_reglement_id = !empty($invoice['payment_mode_id']) ? $invoice['payment_mode_id'] : $soc->mode_reglement_id;
601 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
602 } else {
603 $new_invoice->mode_reglement_id = (int) $invoice['payment_mode_id'];
604 }
605
606 // Trick because nusoap does not store data with same structure if there is one or several lines
607 $arrayoflines = array();
608 if (isset($invoice['lines']['line'][0])) {
609 $arrayoflines = $invoice['lines']['line']; // @phan-suppress-current-line PhanTypeInvalidDimOffset
610 } else {
611 $arrayoflines = $invoice['lines'];
612 }
613 if (!is_array($arrayoflines)) {
614 $arrayoflines = array();
615 }
616
617 foreach ($arrayoflines as $line) {
618 // $key can be 'line' or '0','1',...
619 $newline = new FactureLigne($db);
620 $newline->product_type = $line['type'];
621 $newline->desc = $line['desc'];
622 $newline->fk_product = $line['product_id'];
623 $newline->tva_tx = isset($line['vat_rate']) ? $line['vat_rate'] : 0;
624 $newline->qty = $line['qty'];
625 $newline->subprice = isset($line['unitprice']) ? $line['unitprice'] : null;
626 $newline->total_ht = $line['total_net'];
627 $newline->total_tva = $line['total_vat'];
628 $newline->total_ttc = $line['total'];
629 $newline->date_start = dol_stringtotime($line['date_start']);
630 $newline->date_end = dol_stringtotime($line['date_end']);
631
632 $new_invoice->lines[] = $newline;
633 }
634 //var_dump($newobject->date_lim_reglement); exit;
635 //var_dump($invoice['lines'][0]['type']);
636
637 $db->begin();
638
639 $result = $new_invoice->create($fuser, 0, dol_stringtotime($invoice['date_due'], 'dayrfc'));
640 if ($result < 0) {
641 $error++;
642 }
643
644 if (!$error && $invoice['status'] == Facture::STATUS_VALIDATED) { // We want invoice to have status validated
645 $result = $new_invoice->validate($fuser);
646 if ($result < 0) {
647 $error++;
648 }
649 }
650
651 if (!$error) {
652 $db->commit();
653 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $new_invoice->id,
654 'ref' => $new_invoice->ref, 'ref_ext' => $new_invoice->ref_ext);
655 } else {
656 $db->rollback();
657 $error++;
658 $errorcode = 'KO';
659 $errorlabel = $new_invoice->error;
660 dol_syslog("Function: createInvoice error while creating".$errorlabel);
661 }
662 }
663
664 if ($error) {
665 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
666 }
667
668 return $objectresp;
669}
670
680function createInvoiceFromOrder($authentication, $id_order = '', $ref_order = '', $ref_ext_order = '')
681{
682 global $db, $conf;
683
684 dol_syslog("Function: createInvoiceFromOrder login=".$authentication['login']." id=".$id_order.", ref=".$ref_order.", ref_ext=".$ref_ext_order);
685
686 if ($authentication['entity']) {
687 $conf->entity = $authentication['entity'];
688 }
689
690 // Init and check authentication
691 $objectresp = array();
692 $errorcode = '';
693 $errorlabel = '';
694 $error = 0;
695 $newobject = null;
696 $socid = 0;
697 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
698 if ($fuser->socid) {
699 $socid = $fuser->socid;
700 }
701
702 // Check parameters
703 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
704 $error++;
705 $errorcode = 'KO';
706 $errorlabel = "order id or ref or ref_ext is mandatory.";
707 }
708
710 if (!$error) {
711 $fuser->loadRights();
712
713 if ($fuser->hasRight('commande', 'lire')) {
714 $order = new Commande($db);
715 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
716 if ($result > 0) {
717 // Security for external user
718 if ($socid && ($socid != $order->socid)) {
719 $error++;
720 $errorcode = 'PERMISSION_DENIED';
721 $errorlabel = $order->socid.'User does not have permission for this request';
722 }
723
724 if (!$error) {
725 $newobject = new Facture($db);
726 $result = $newobject->createFromOrder($order, $fuser);
727
728 if ($result < 0) {
729 $error++;
730 dol_syslog("Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
731 }
732 }
733 } else {
734 $error++;
735 $errorcode = 'NOT_FOUND';
736 $errorlabel = 'Object not found for id='.$id_order.' nor ref='.$ref_order.' nor ref_ext='.$ref_ext_order;
737 }
738 } else {
739 $error++;
740 $errorcode = 'PERMISSION_DENIED';
741 $errorlabel = 'User does not have permission for this request';
742 }
743 }
744
745 if ($error || $newobject === null) {
746 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
747 } else {
748 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref, 'ref_ext' => $newobject->ref_ext);
749 }
750
751 return $objectresp;
752}
753
761function updateInvoice($authentication, $invoice)
762{
763 global $db, $conf, $langs;
764
765 dol_syslog("Function: updateInvoice login=".$authentication['login']." id=".$invoice['id'].
766 ", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
767
768 if ($authentication['entity']) {
769 $conf->entity = $authentication['entity'];
770 }
771
772 // Init and check authentication
773 $objectresp = array();
774 $errorcode = '';
775 $errorlabel = '';
776 $error = 0;
777 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
778
779 // Check parameters
780 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
781 $error++;
782 $errorcode = 'KO';
783 $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
784 }
785
786 if (!$error) {
787 $objectfound = false;
788
789 $object = new Facture($db);
790 $result = $object->fetch($invoice['id'], $invoice['ref'], $invoice['ref_ext'], 0);
791
792 if (!empty($object->id)) {
793 $objectfound = true;
794
795 $db->begin();
796
797 if (isset($invoice['status'])) {
798 if ($invoice['status'] == Facture::STATUS_DRAFT) {
799 $result = $object->setDraft($fuser);
800 }
801 if ($invoice['status'] == Facture::STATUS_VALIDATED) {
802 $result = $object->validate($fuser);
803
804 if ($result >= 0) {
805 // Define output language
806 $outputlangs = $langs;
807 $object->generateDocument($object->model_pdf, $outputlangs);
808 }
809 }
810 if ($invoice['status'] == Facture::STATUS_CLOSED) {
811 $result = $object->setPaid($fuser, $invoice['close_code'], $invoice['close_note']);
812 }
813 if ($invoice['status'] == Facture::STATUS_ABANDONED) {
814 $result = $object->setCanceled($fuser, $invoice['close_code'], $invoice['close_note']);
815 }
816 }
817 }
818
819 if ((!$error) && ($objectfound)) {
820 $db->commit();
821 $objectresp = array(
822 'result' => array('result_code' => 'OK', 'result_label' => ''),
823 'id' => $object->id,
824 'ref' => $object->ref,
825 'ref_ext' => $object->ref_ext
826 );
827 } elseif ($objectfound) {
828 $db->rollback();
829 $error++;
830 $errorcode = 'KO';
831 $errorlabel = $object->error;
832 } else {
833 $error++;
834 $errorcode = 'NOT_FOUND';
835 $errorlabel = 'Invoice id='.$invoice['id'].' ref='.$invoice['ref'].' ref_ext='.$invoice['ref_ext'].' cannot be found';
836 }
837 }
838
839 if ($error) {
840 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
841 }
842
843 return $objectresp;
844}
845
846// Return the results.
847$server->service(file_get_contents("php://input"));
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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:431
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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