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