dolibarr 24.0.0-beta
server_supplier_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) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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/ws.lib.php';
50require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
51require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
52
58dol_syslog("Call Dolibarr webservices interfaces");
59
60$langs->load("main");
61
62// Enable and test if module web services is enabled
63if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
64 $langs->load("admin");
65 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
66 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
67 print $langs->trans("ToActivateModule");
68 exit;
69}
70
71// Create the soap Object
72$server = new nusoap_server();
73$server->soap_defencoding = 'UTF-8';
74$server->decode_utf8 = false;
75$ns = 'http://www.dolibarr.org/ns/';
76$server->configureWSDL('WebServicesDolibarrSupplierInvoice', $ns);
77// @phan-suppress-next-line PhanUndeclaredProperty
78$server->wsdl->schemaTargetNamespace = $ns;
79
80
81// Define WSDL Authentication object
82$server->wsdl->addComplexType(
83 'authentication',
84 'complexType',
85 'struct',
86 'all',
87 '',
88 array(
89 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
90 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
91 'login' => array('name' => 'login', 'type' => 'xsd:string'),
92 'password' => array('name' => 'password', 'type' => 'xsd:string'),
93 'entity' => array('name' => 'entity', 'type' => 'xsd:string'),
94 )
95);
96// Define WSDL Return object
97$server->wsdl->addComplexType(
98 'result',
99 'complexType',
100 'struct',
101 'all',
102 '',
103 array(
104 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
105 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
106 )
107);
108
109// Define other specific objects
110$server->wsdl->addComplexType(
111 'line',
112 'element',
113 'struct',
114 'all',
115 '',
116 array(
117 'id' => array('name' => 'id', 'type' => 'xsd:string'),
118 'type' => array('name' => 'type', 'type' => 'xsd:int'),
119 'desc' => array('name' => 'desc', 'type' => 'xsd:string'),
120 'fk_product' => array('name' => 'fk_product', 'type' => 'xsd:int'),
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 'vat_rate' => array('name' => 'vat_rate', 'type' => 'xsd:double'),
125 'qty' => array('name' => 'qty', 'type' => 'xsd:double'),
126 'date_start' => array('name' => 'date_start', 'type' => 'xsd:date'),
127 'date_end' => array('name' => 'date_end', 'type' => 'xsd:date'),
128 // From product
129 'product_ref' => array('name' => 'product_ref', 'type' => 'xsd:string'),
130 'product_label' => array('name' => 'product_label', 'type' => 'xsd:string'),
131 'product_desc' => array('name' => 'product_desc', 'type' => 'xsd:string')
132 )
133);
134
135$server->wsdl->addComplexType(
136 'LinesArray',
137 'complexType',
138 'array',
139 '',
140 'SOAP-ENC:Array',
141 array(),
142 array(
143 array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:line[]')
144 ),
145 'tns:line'
146);
147
148$server->wsdl->addComplexType(
149 'invoice',
150 'element', // If we put element here instead of complexType to have tag called invoice in getInvoicesForThirdParty we brek getInvoice
151 'struct',
152 'all',
153 '',
154 array(
155 'id' => array('name' => 'id', 'type' => 'xsd:string'),
156 'ref' => array('name' => 'ref', 'type' => 'xsd:string'),
157 'ref_ext' => array('name' => 'ref_ext', 'type' => 'xsd:string'),
158 'ref_supplier' => array('name' => 'ref_supplier', 'type' => 'xsd:string'),
159 'fk_user_author' => array('name' => 'fk_user_author', 'type' => 'xsd:int'),
160 'fk_user_valid' => array('name' => 'fk_user_valid', 'type' => 'xsd:int'),
161 'fk_user_modif' => array('name' => 'fk_user_modif', 'type' => 'xsd:int'),
162 'fk_thirdparty' => array('name' => 'fk_thirdparty', 'type' => 'xsd:int'),
163 'date_creation' => array('name' => 'date_creation', 'type' => 'xsd:dateTime'),
164 'date_validation' => array('name' => 'date_validation', 'type' => 'xsd:dateTime'),
165 'date_modification' => array('name' => 'date_modification', 'type' => 'xsd:dateTime'),
166 'date_invoice' => array('name' => 'date_invoice', 'type' => 'xsd:date'),
167 'date_term' => array('name' => 'date_modification', 'type' => 'xsd:date'),
168 'label' => array('name' => 'label', 'type' => 'xsd:date'),
169 'type' => array('name' => 'type', 'type' => 'xsd:int'),
170 'total_net' => array('name' => 'type', 'type' => 'xsd:double'),
171 'total_vat' => array('name' => 'type', 'type' => 'xsd:double'),
172 'total' => array('name' => 'type', 'type' => 'xsd:double'),
173 'note_private' => array('name' => 'note_private', 'type' => 'xsd:string'),
174 'note_public' => array('name' => 'note_public', 'type' => 'xsd:string'),
175 'status' => array('name' => 'status', 'type' => 'xsd:int'),
176 'close_code' => array('name' => 'close_code', 'type' => 'xsd:string'),
177 'close_note' => array('name' => 'close_note', 'type' => 'xsd:string'),
178 'lines' => array('name' => 'lines', 'type' => 'tns:LinesArray')
179 )
180);
181
182$server->wsdl->addComplexType(
183 'InvoicesArray',
184 'complexType',
185 'array',
186 '',
187 'SOAP-ENC:Array',
188 array(),
189 array(
190 array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:invoice[]')
191 ),
192 'tns:invoice'
193);
194
195$server->wsdl->addComplexType(
196 'invoices',
197 'complexType',
198 'array',
199 '',
200 'SOAP-ENC:Array',
201 array(),
202 array(
203 array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:invoice[]')
204 ),
205 'tns:invoice'
206);
207
208
209
210// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
211// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
212// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
213$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
214$styleuse = 'encoded'; // encoded/literal/literal wrapped
215// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
216
217// Register WSDL
218$server->register(
219 'getSupplierInvoice',
220 // Entry values
221 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
222 // Exit values
223 array('result' => 'tns:result', 'invoice' => 'tns:invoice'),
224 $ns,
225 $ns.'#getSupplierInvoice',
226 $styledoc,
227 $styleuse,
228 'WS to get SupplierInvoice'
229);
230$server->register(
231 'getSupplierInvoicesForThirdParty',
232 // Entry values
233 array('authentication' => 'tns:authentication', 'idthirdparty' => 'xsd:string'),
234 // Exit values
235 array('result' => 'tns:result', 'invoices' => 'tns:invoices'),
236 $ns,
237 $ns.'#getSupplierInvoicesForThirdParty',
238 $styledoc,
239 $styleuse,
240 'WS to get SupplierInvoicesForThirdParty'
241);
242
243
253function getSupplierInvoice($authentication, $id = 0, $ref = '', $ref_ext = '')
254{
255 global $db, $conf;
256
257 dol_syslog("Function: getSupplierInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
258
259 if ($authentication['entity']) {
260 $conf->entity = $authentication['entity'];
261 }
262
263 // Init and check authentication
264 $objectresp = array();
265 $errorcode = '';
266 $errorlabel = '';
267 $error = 0;
268 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
269 // Check parameters
270 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
271 $error++;
272 $errorcode = 'BAD_PARAMETERS';
273 $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
274 }
275
276 if (!$error) {
277 $fuser->loadRights();
278
279 if ($fuser->hasRight('fournisseur', 'facture', 'lire')) {
280 $invoice = new FactureFournisseur($db);
281 $result = $invoice->fetch($id, $ref, $ref_ext);
282 if ($result > 0) {
283 $linesresp = array();
284 $i = 0;
285 foreach ($invoice->lines as $line) {
286 //var_dump($line); exit;
287 $linesresp[] = array(
288 'id' => $line->id,
289 'type' => $line->product_type,
290 'total_net' => $line->total_ht,
291 'total_vat' => $line->total_tva,
292 'total' => $line->total_ttc,
293 'vat_rate' => $line->tva_tx,
294 'qty' => $line->qty
295 );
296 $i++;
297 }
298
299 // Create invoice
300 $objectresp = array(
301 'result' => array('result_code' => 'OK', 'result_label' => ''),
302 'invoice' => array(
303 'id' => $invoice->id,
304 'ref' => $invoice->ref,
305 'ref_supplier' => $invoice->ref_supplier,
306 'ref_ext' => $invoice->ref_ext,
307 'fk_user_author' => $invoice->user_creation_id,
308 'fk_user_valid' => $invoice->user_validation_id,
309 'fk_user_modif' => $invoice->user_modification_id,
310 'fk_thirdparty' => $invoice->socid,
311 'type' => $invoice->type,
312 'status' => $invoice->status,
313 'total_net' => $invoice->total_ht,
314 'total_vat' => $invoice->total_tva,
315 'total' => $invoice->total_ttc,
316 'date_creation' => dol_print_date($invoice->date_creation, 'dayhourrfc'),
317 'date_modification' => dol_print_date($invoice->date_modification, 'dayhourrfc'),
318 'date_invoice' => dol_print_date($invoice->date, 'dayhourrfc'),
319 'date_term' => dol_print_date($invoice->date_echeance, 'dayhourrfc'),
320 'label' => $invoice->label,
321 'paid' => $invoice->paid,
322 'note_private' => $invoice->note_private,
323 'note_public' => $invoice->note_public,
324 'close_code' => $invoice->close_code,
325 'close_note' => $invoice->close_note,
326
327 'lines' => $linesresp,
328 // 'lines' => array('0'=>array('id'=>222,'type'=>1),
329 // '1'=>array('id'=>333,'type'=>1)),
330
331 ));
332 } else {
333 $error++;
334 $errorcode = 'NOT_FOUND';
335 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
336 }
337 } else {
338 $error++;
339 $errorcode = 'PERMISSION_DENIED';
340 $errorlabel = 'User does not have permission for this request';
341 }
342 }
343
344 if ($error) {
345 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
346 }
347
348 return $objectresp;
349}
350
351
359function getSupplierInvoicesForThirdParty($authentication, $idthirdparty)
360{
361 global $db, $conf;
362
363 dol_syslog("Function: getSupplierInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
364
365 if ($authentication['entity']) {
366 $conf->entity = $authentication['entity'];
367 }
368
369 // Init and check authentication
370 $objectresp = array();
371 $errorcode = '';
372 $errorlabel = '';
373 $error = 0;
374 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
375
376 // Check parameters
377 if (!$error && empty($idthirdparty)) {
378 $error++;
379 $errorcode = 'BAD_PARAMETERS';
380 $errorlabel = 'Parameter id is not provided';
381 }
382
383 if (!$error) {
384 $linesinvoice = array();
385
386 $sql = "SELECT f.rowid as facid";
387 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
388 $sql .= " WHERE f.entity = ".((int) $conf->entity);
389 if ($idthirdparty != 'all') {
390 $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
391 }
392
393 $resql = $db->query($sql);
394 if ($resql) {
395 $num = $db->num_rows($resql);
396 $i = 0;
397 while ($i < $num) {
398 // En attendant remplissage par boucle
399 $obj = $db->fetch_object($resql);
400
401 $invoice = new FactureFournisseur($db);
402 $result = $invoice->fetch($obj->facid);
403 if ($result < 0) {
404 $error++;
405 $errorcode = $result;
406 $errorlabel = $invoice->error;
407 break;
408 }
409
410 // Define lines of invoice
411 $linesresp = array();
412 foreach ($invoice->lines as $line) {
413 $linesresp[] = array(
414 'id' => $line->rowid,
415 'type' => $line->product_type,
416 'desc' => dol_htmlcleanlastbr($line->desc),
417 'total_net' => $line->total_ht,
418 'total_vat' => $line->total_tva,
419 'total' => $line->total_ttc,
420 'vat_rate' => $line->tva_tx,
421 'qty' => $line->qty,
422 'product_ref' => $line->product_ref,
423 'product_label' => $line->product_label,
424 'product_desc' => $line->product_desc,
425 );
426 }
427
428 // Now define invoice
429 $linesinvoice[] = array(
430 'id' => $invoice->id,
431 'ref' => $invoice->ref,
432 'ref_supplier' => $invoice->ref_supplier,
433 'ref_ext' => $invoice->ref_ext,
434 'fk_user_author' => $invoice->user_creation_id,
435 'fk_user_valid' => $invoice->user_validation_id,
436 'fk_thirdparty' => $invoice->socid,
437 'type' => $invoice->type,
438 'status' => $invoice->status,
439 'total_net' => $invoice->total_ht,
440 'total_vat' => $invoice->total_tva,
441 'total' => $invoice->total_ttc,
442 'date_creation' => dol_print_date($invoice->date_creation, 'dayhourrfc'),
443 'date_modification' => dol_print_date($invoice->date_modification, 'dayhourrfc'),
444 'date_invoice' => dol_print_date($invoice->date, 'dayhourrfc'),
445 'date_term' => dol_print_date($invoice->date_echeance, 'dayhourrfc'),
446 'label' => $invoice->label,
447 'paid' => $invoice->paid,
448 'note_private' => $invoice->note_private,
449 'note_public' => $invoice->note_public,
450 'close_code' => $invoice->close_code,
451 'close_note' => $invoice->close_note,
452
453 'lines' => $linesresp
454 );
455
456 $i++;
457 }
458
459 $objectresp = array(
460 'result' => array('result_code' => 'OK', 'result_label' => ''),
461 'invoices' => $linesinvoice
462
463 );
464 } else {
465 $error++;
466 $errorcode = $db->lasterrno();
467 $errorlabel = $db->lasterror();
468 }
469 }
470
471 if ($error) {
472 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
473 }
474
475 return $objectresp;
476}
477
478// Return the results.
479$server->service(file_get_contents("php://input"));
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class to manage suppliers invoices.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getSupplierInvoicesForThirdParty($authentication, $idthirdparty)
Get list of invoices for third party.
getSupplierInvoice($authentication, $id=0, $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:37