dolibarr 18.0.6
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 (empty($conf->global->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 = '', $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 = ''; $errorlabel = '';
321 $error = 0;
322 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
323 // Check parameters
324 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
325 $error++;
326 $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
327 }
328
329 if (!$error) {
330 $fuser->getrights();
331
332 if ($fuser->rights->facture->lire) {
333 $invoice = new Facture($db);
334 $result = $invoice->fetch($id, $ref, $ref_ext);
335 if ($result > 0) {
336 $linesresp = array();
337 $i = 0;
338 foreach ($invoice->lines as $line) {
339 //var_dump($line); exit;
340 $linesresp[] = array(
341 'id'=>$line->id,
342 'type'=>$line->product_type,
343 'desc'=>dol_htmlcleanlastbr($line->desc),
344 'total_net'=>$line->total_ht,
345 'total_vat'=>$line->total_tva,
346 'total'=>$line->total_ttc,
347 'vat_rate'=>$line->tva_tx,
348 'qty'=>$line->qty,
349 'unitprice'=> $line->subprice,
350 'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
351 'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
352 'product_id'=>$line->fk_product,
353 'product_ref'=>$line->product_ref,
354 'product_label'=>$line->product_label,
355 'product_desc'=>$line->product_desc,
356 );
357 $i++;
358 }
359
360 // Create invoice
361 $objectresp = array(
362 'result'=>array('result_code'=>'OK', 'result_label'=>''),
363 'invoice'=>array(
364 'id' => $invoice->id,
365 'ref' => $invoice->ref,
366 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
367 'thirdparty_id' => $invoice->socid,
368 'fk_user_author' => $invoice->user_author ? $invoice->user_author : '',
369 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid : '',
370 'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
371 'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
372 'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
373 'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
374 'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
375 'type' => $invoice->type,
376 'total_net' => $invoice->total_ht,
377 'total_vat' => $invoice->total_tva,
378 'total' => $invoice->total_ttc,
379 'note_private' => $invoice->note_private ? $invoice->note_private : '',
380 'note_public' => $invoice->note_public ? $invoice->note_public : '',
381 'status' => $invoice->statut,
382 'project_id' => $invoice->fk_project,
383 'close_code' => $invoice->close_code ? $invoice->close_code : '',
384 'close_note' => $invoice->close_note ? $invoice->close_note : '',
385 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
386 'lines' => $linesresp
387 ));
388 } else {
389 $error++;
390 $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
391 }
392 } else {
393 $error++;
394 $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
395 }
396 }
397
398 if ($error) {
399 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
400 }
401
402 return $objectresp;
403}
404
405
413function getInvoicesForThirdParty($authentication, $idthirdparty)
414{
415 global $db, $conf;
416
417 dol_syslog("Function: getInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
418
419 if ($authentication['entity']) {
420 $conf->entity = $authentication['entity'];
421 }
422
423 // Init and check authentication
424 $objectresp = array();
425 $errorcode = ''; $errorlabel = '';
426 $error = 0;
427 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
428
429 if ($fuser->socid) {
430 $socid = $fuser->socid;
431 }
432
433 // Check parameters
434 if (!$error && empty($idthirdparty)) {
435 $error++;
436 $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter idthirdparty is not provided';
437 }
438
439 if (!$error) {
440 $linesinvoice = array();
441
442 $sql = 'SELECT f.rowid as facid, ref as ref, ref_ext, type, fk_statut as status, total_ttc, total, tva';
443 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f';
444 $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
445 if ($idthirdparty != 'all') {
446 $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
447 }
448
449 $resql = $db->query($sql);
450 if ($resql) {
451 $num = $db->num_rows($resql);
452 $i = 0;
453 while ($i < $num) {
454 // En attendant remplissage par boucle
455 $obj = $db->fetch_object($resql);
456
457 $invoice = new Facture($db);
458 $invoice->fetch($obj->facid);
459
460 // Sécurité pour utilisateur externe
461 if ($socid && ($socid != $invoice->socid)) {
462 $error++;
463 $errorcode = 'PERMISSION_DENIED'; $errorlabel = $invoice->socid.' User does not have permission for this request';
464 }
465
466 if (!$error) {
467 // Define lines of invoice
468 $linesresp = array();
469 foreach ($invoice->lines as $line) {
470 $linesresp[] = array(
471 'id'=>$line->id,
472 'type'=>$line->product_type,
473 'total_net'=>$line->total_ht,
474 'total_vat'=>$line->total_tva,
475 'total'=>$line->total_ttc,
476 'vat_rate'=>$line->tva_tx,
477 'qty'=>$line->qty,
478 'unitprice'=> $line->subprice,
479 'date_start'=> $line->date_start ?dol_print_date($line->date_start, 'dayrfc') : '',
480 'date_end'=> $line->date_end ?dol_print_date($line->date_end, 'dayrfc') : '',
481 'product_id'=>$line->fk_product,
482 'product_ref'=>$line->product_ref,
483 'product_label'=>$line->product_label,
484 'product_desc'=>$line->product_desc,
485 );
486 }
487
488 // Now define invoice
489 $linesinvoice[] = array(
490 'id' => $invoice->id,
491 'ref' => $invoice->ref,
492 'ref_ext' => $invoice->ref_ext ? $invoice->ref_ext : '', // If not defined, field is not added into soap
493 'fk_user_author' => $invoice->user_author ? $invoice->user_author : '',
494 'fk_user_valid' => $invoice->user_valid ? $invoice->user_valid : '',
495 'date' => $invoice->date ?dol_print_date($invoice->date, 'dayrfc') : '',
496 'date_due' => $invoice->date_lim_reglement ?dol_print_date($invoice->date_lim_reglement, 'dayrfc') : '',
497 'date_creation' => $invoice->date_creation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
498 'date_validation' => $invoice->date_validation ?dol_print_date($invoice->date_creation, 'dayhourrfc') : '',
499 'date_modification' => $invoice->datem ?dol_print_date($invoice->datem, 'dayhourrfc') : '',
500 'type' => $invoice->type,
501 'total_net' => $invoice->total_ht,
502 'total_vat' => $invoice->total_tva,
503 'total' => $invoice->total_ttc,
504 'note_private' => $invoice->note_private ? $invoice->note_private : '',
505 'note_public' => $invoice->note_public ? $invoice->note_public : '',
506 'status'=> $invoice->statut,
507 'project_id' => $invoice->fk_project,
508 'close_code' => $invoice->close_code ? $invoice->close_code : '',
509 'close_note' => $invoice->close_note ? $invoice->close_note : '',
510 'payment_mode_id' => $invoice->mode_reglement_id ? $invoice->mode_reglement_id : '',
511 'lines' => $linesresp
512 );
513 }
514
515 $i++;
516 }
517
518 $objectresp = array(
519 'result'=>array('result_code'=>'OK', 'result_label'=>''),
520 'invoices'=>$linesinvoice
521
522 );
523 } else {
524 $error++;
525 $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
526 }
527 }
528
529 if ($error) {
530 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
531 }
532
533 return $objectresp;
534}
535
536
544function createInvoice($authentication, $invoice)
545{
546 global $db, $conf;
547
548 $now = dol_now();
549
550 dol_syslog("Function: createInvoice login=".$authentication['login']." id=".$invoice['id'].", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
551
552 if ($authentication['entity']) {
553 $conf->entity = $authentication['entity'];
554 }
555
556 // Init and check authentication
557 $objectresp = array();
558 $errorcode = ''; $errorlabel = '';
559 $error = 0;
560 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
561
562 // Check parameters
563 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
564 $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
565 }
566
567 if (!$error) {
568 $new_invoice = new Facture($db);
569 $new_invoice->socid = $invoice['thirdparty_id'];
570 $new_invoice->type = $invoice['type'];
571 $new_invoice->ref_ext = $invoice['ref_ext'];
572 $new_invoice->date = dol_stringtotime($invoice['date'], 'dayrfc');
573 $new_invoice->note_private = $invoice['note_private'];
574 $new_invoice->note_public = $invoice['note_public'];
575 $new_invoice->statut = Facture::STATUS_DRAFT; // We start with status draft
576 $new_invoice->fk_project = $invoice['project_id'];
577 $new_invoice->date_creation = $now;
578
579 //take mode_reglement and cond_reglement from thirdparty
580 $soc = new Societe($db);
581 $res = $soc->fetch($new_invoice->socid);
582 if ($res > 0) {
583 $new_invoice->mode_reglement_id = !empty($invoice['payment_mode_id']) ? $invoice['payment_mode_id'] : $soc->mode_reglement_id;
584 $new_invoice->cond_reglement_id = $soc->cond_reglement_id;
585 } else {
586 $new_invoice->mode_reglement_id = $invoice['payment_mode_id'];
587 }
588
589 // Trick because nusoap does not store data with same structure if there is one or several lines
590 $arrayoflines = array();
591 if (isset($invoice['lines']['line'][0])) {
592 $arrayoflines = $invoice['lines']['line'];
593 } else {
594 $arrayoflines = $invoice['lines'];
595 }
596
597 foreach ($arrayoflines as $line) {
598 // $key can be 'line' or '0','1',...
599 $newline = new FactureLigne($db);
600 $newline->product_type = $line['type'];
601 $newline->desc = $line['desc'];
602 $newline->fk_product = $line['product_id'];
603 $newline->tva_tx = isset($line['vat_rate']) ? $line['vat_rate'] : 0;
604 $newline->qty = $line['qty'];
605 $newline->subprice = isset($line['unitprice']) ? $line['unitprice'] : null;
606 $newline->total_ht = $line['total_net'];
607 $newline->total_tva = $line['total_vat'];
608 $newline->total_ttc = $line['total'];
609 $newline->date_start = dol_stringtotime($line['date_start']);
610 $newline->date_end = dol_stringtotime($line['date_end']);
611
612 $new_invoice->lines[] = $newline;
613 }
614 //var_dump($newobject->date_lim_reglement); exit;
615 //var_dump($invoice['lines'][0]['type']);
616
617 $db->begin();
618
619 $result = $new_invoice->create($fuser, 0, dol_stringtotime($invoice['date_due'], 'dayrfc'));
620 if ($result < 0) {
621 $error++;
622 }
623
624 if (!$error && $invoice['status'] == Facture::STATUS_VALIDATED) { // We want invoice to have status validated
625 $result = $new_invoice->validate($fuser);
626 if ($result < 0) {
627 $error++;
628 }
629 }
630
631 if (!$error) {
632 $db->commit();
633 $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$new_invoice->id,
634 'ref'=>$new_invoice->ref, 'ref_ext'=>$new_invoice->ref_ext);
635 } else {
636 $db->rollback();
637 $error++;
638 $errorcode = 'KO';
639 $errorlabel = $new_invoice->error;
640 dol_syslog("Function: createInvoice error while creating".$errorlabel);
641 }
642 }
643
644 if ($error) {
645 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
646 }
647
648 return $objectresp;
649}
650
660function createInvoiceFromOrder($authentication, $id_order = '', $ref_order = '', $ref_ext_order = '')
661{
662 global $db, $conf;
663
664 dol_syslog("Function: createInvoiceFromOrder login=".$authentication['login']." id=".$id_order.", ref=".$ref_order.", ref_ext=".$ref_ext_order);
665
666 if ($authentication['entity']) {
667 $conf->entity = $authentication['entity'];
668 }
669
670 // Init and check authentication
671 $objectresp = array();
672 $errorcode = ''; $errorlabel = '';
673 $error = 0;
674 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
675 if ($fuser->socid) {
676 $socid = $fuser->socid;
677 }
678
679 // Check parameters
680 if (empty($id_order) && empty($ref_order) && empty($ref_ext_order)) {
681 $error++; $errorcode = 'KO'; $errorlabel = "order id or ref or ref_ext is mandatory.";
682 }
683
685 if (!$error) {
686 $fuser->getrights();
687
688 if ($fuser->rights->commande->lire) {
689 $order = new Commande($db);
690 $result = $order->fetch($id_order, $ref_order, $ref_ext_order);
691 if ($result > 0) {
692 // Security for external user
693 if ($socid && ($socid != $order->socid)) {
694 $error++;
695 $errorcode = 'PERMISSION_DENIED'; $errorlabel = $order->socid.'User does not have permission for this request';
696 }
697
698 if (!$error) {
699 $newobject = new Facture($db);
700 $result = $newobject->createFromOrder($order, $fuser);
701
702 if ($result < 0) {
703 $error++;
704 dol_syslog("Webservice server_invoice:: invoice creation from order failed", LOG_ERR);
705 }
706 }
707 } else {
708 $error++;
709 $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id_order.' nor ref='.$ref_order.' nor ref_ext='.$ref_ext_order;
710 }
711 } else {
712 $error++;
713 $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
714 }
715 }
716
717 if ($error) {
718 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
719 } else {
720 $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref, 'ref_ext'=>$newobject->ref_ext);
721 }
722
723 return $objectresp;
724}
725
733function updateInvoice($authentication, $invoice)
734{
735 global $db, $conf, $langs;
736
737 dol_syslog("Function: updateInvoice login=".$authentication['login']." id=".$invoice['id'].
738 ", ref=".$invoice['ref'].", ref_ext=".$invoice['ref_ext']);
739
740 if ($authentication['entity']) {
741 $conf->entity = $authentication['entity'];
742 }
743
744 // Init and check authentication
745 $objectresp = array();
746 $errorcode = ''; $errorlabel = '';
747 $error = 0;
748 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
749
750 // Check parameters
751 if (empty($invoice['id']) && empty($invoice['ref']) && empty($invoice['ref_ext'])) {
752 $error++; $errorcode = 'KO'; $errorlabel = "Invoice id or ref or ref_ext is mandatory.";
753 }
754
755 if (!$error) {
756 $objectfound = false;
757
758 $object = new Facture($db);
759 $result = $object->fetch($invoice['id'], $invoice['ref'], $invoice['ref_ext'], '');
760
761 if (!empty($object->id)) {
762 $objectfound = true;
763
764 $db->begin();
765
766 if (isset($invoice['status'])) {
767 if ($invoice['status'] == Facture::STATUS_DRAFT) {
768 $result = $object->setDraft($fuser);
769 }
770 if ($invoice['status'] == Facture::STATUS_VALIDATED) {
771 $result = $object->validate($fuser);
772
773 if ($result >= 0) {
774 // Define output language
775 $outputlangs = $langs;
776 $object->generateDocument($object->model_pdf, $outputlangs);
777 }
778 }
779 if ($invoice['status'] == Facture::STATUS_CLOSED) {
780 $result = $object->setPaid($fuser, $invoice['close_code'], $invoice['close_note']);
781 }
782 if ($invoice['status'] == Facture::STATUS_ABANDONED) {
783 $result = $object->setCanceled($fuser, $invoice['close_code'], $invoice['close_note']);
784 }
785 }
786 }
787
788 if ((!$error) && ($objectfound)) {
789 $db->commit();
790 $objectresp = array(
791 'result'=>array('result_code'=>'OK', 'result_label'=>''),
792 'id'=>$object->id,
793 'ref'=>$object->ref,
794 'ref_ext'=>$object->ref_ext
795 );
796 } elseif ($objectfound) {
797 $db->rollback();
798 $error++;
799 $errorcode = 'KO';
800 $errorlabel = $object->error;
801 } else {
802 $error++;
803 $errorcode = 'NOT_FOUND';
804 $errorlabel = 'Invoice id='.$invoice['id'].' ref='.$invoice['ref'].' ref_ext='.$invoice['ref_ext'].' cannot be found';
805 }
806 }
807
808 if ($error) {
809 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
810 }
811
812 return $objectresp;
813}
814
815// Return the results.
816$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:409
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.
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.
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.
getInvoice($authentication, $id='', $ref='', $ref_ext='')
Get invoice from id, ref or ref_ext.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition ws.lib.php:35