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