dolibarr 18.0.6
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 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
23if (!defined('NOCSRFCHECK')) {
24 define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
25}
26if (!defined('NOTOKENRENEWAL')) {
27 define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
28}
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
31}
32if (!defined('NOREQUIREHTML')) {
33 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
34}
35if (!defined('NOREQUIREAJAX')) {
36 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
37}
38if (!defined("NOLOGIN")) {
39 define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
40}
41if (!defined("NOSESSION")) {
42 define("NOSESSION", '1');
43}
44
45require '../main.inc.php';
46require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
47require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
48require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
49require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
50
51
52dol_syslog("Call Dolibarr webservices interfaces");
53
54$langs->load("main");
55
56// Enable and test if module web services is enabled
57if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
58 $langs->load("admin");
59 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
60 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
61 print $langs->trans("ToActivateModule");
62 exit;
63}
64
65// Create the soap Object
66$server = new nusoap_server();
67$server->soap_defencoding = 'UTF-8';
68$server->decode_utf8 = false;
69$ns = 'http://www.dolibarr.org/ns/';
70$server->configureWSDL('WebServicesDolibarrSupplierInvoice', $ns);
71$server->wsdl->schemaTargetNamespace = $ns;
72
73
74// Define WSDL Authentication object
75$server->wsdl->addComplexType(
76 'authentication',
77 'complexType',
78 'struct',
79 'all',
80 '',
81 array(
82 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
83 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
84 'login' => array('name'=>'login', 'type'=>'xsd:string'),
85 'password' => array('name'=>'password', 'type'=>'xsd:string'),
86 'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
87 )
88);
89// Define WSDL Return object
90$server->wsdl->addComplexType(
91 'result',
92 'complexType',
93 'struct',
94 'all',
95 '',
96 array(
97 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
98 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
99 )
100);
101
102// Define other specific objects
103$server->wsdl->addComplexType(
104 'line',
105 'element',
106 'struct',
107 'all',
108 '',
109 array(
110 'id' => array('name'=>'id', 'type'=>'xsd:string'),
111 'type' => array('name'=>'type', 'type'=>'xsd:int'),
112 'desc' => array('name'=>'desc', 'type'=>'xsd:string'),
113 'fk_product' => array('name'=>'fk_product', 'type'=>'xsd:int'),
114 'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
115 'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
116 'total' => array('name'=>'total', 'type'=>'xsd:double'),
117 'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:double'),
118 'qty' => array('name'=>'qty', 'type'=>'xsd:double'),
119 'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
120 'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
121 // From product
122 'product_ref' => array('name'=>'product_ref', 'type'=>'xsd:string'),
123 'product_label' => array('name'=>'product_label', 'type'=>'xsd:string'),
124 'product_desc' => array('name'=>'product_desc', 'type'=>'xsd:string')
125 )
126);
127
128$server->wsdl->addComplexType(
129 'LinesArray',
130 'complexType',
131 'array',
132 '',
133 'SOAP-ENC:Array',
134 array(),
135 array(
136 array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:line[]')
137 ),
138 'tns:line'
139);
140
141$server->wsdl->addComplexType(
142 'invoice',
143 'element', // If we put element here instead of complexType to have tag called invoice in getInvoicesForThirdParty we brek getInvoice
144 'struct',
145 'all',
146 '',
147 array(
148 'id' => array('name'=>'id', 'type'=>'xsd:string'),
149 'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
150 'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
151 'ref_supplier' => array('name'=>'ref_supplier', 'type'=>'xsd:string'),
152 'fk_user_author' => array('name'=>'fk_user_author', 'type'=>'xsd:int'),
153 'fk_user_valid' => array('name'=>'fk_user_valid', 'type'=>'xsd:int'),
154 'fk_thirdparty' => array('name'=>'fk_thirdparty', 'type'=>'xsd:int'),
155 'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
156 'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
157 'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
158 'date_invoice' => array('name'=>'date_invoice', 'type'=>'xsd:date'),
159 'date_term' => array('name'=>'date_modification', 'type'=>'xsd:date'),
160 'label' => array('name'=>'label', 'type'=>'xsd:date'),
161 'type' => array('name'=>'type', 'type'=>'xsd:int'),
162 'total_net' => array('name'=>'type', 'type'=>'xsd:double'),
163 'total_vat' => array('name'=>'type', 'type'=>'xsd:double'),
164 'total' => array('name'=>'type', 'type'=>'xsd:double'),
165 'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
166 'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
167 'status' => array('name'=>'status', 'type'=>'xsd:int'),
168 'close_code' => array('name'=>'close_code', 'type'=>'xsd:string'),
169 'close_note' => array('name'=>'close_note', 'type'=>'xsd:string'),
170 'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray')
171 )
172);
173
174$server->wsdl->addComplexType(
175 'InvoicesArray',
176 'complexType',
177 'array',
178 '',
179 'SOAP-ENC:Array',
180 array(),
181 array(
182 array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:invoice[]')
183 ),
184 'tns:invoice'
185);
186
187$server->wsdl->addComplexType(
188 'invoices',
189 'complexType',
190 'array',
191 '',
192 'SOAP-ENC:Array',
193 array(),
194 array(
195 array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:invoice[]')
196 ),
197 'tns:invoice'
198);
199
200
201
202// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
203// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
204// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
205$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
206$styleuse = 'encoded'; // encoded/literal/literal wrapped
207// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
208
209// Register WSDL
210$server->register(
211 'getSupplierInvoice',
212 // Entry values
213 array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
214 // Exit values
215 array('result'=>'tns:result', 'invoice'=>'tns:invoice'),
216 $ns,
217 $ns.'#getSupplierInvoice',
218 $styledoc,
219 $styleuse,
220 'WS to get SupplierInvoice'
221);
222$server->register(
223 'getSupplierInvoicesForThirdParty',
224 // Entry values
225 array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'),
226 // Exit values
227 array('result'=>'tns:result', 'invoices'=>'tns:invoices'),
228 $ns,
229 $ns.'#getSupplierInvoicesForThirdParty',
230 $styledoc,
231 $styleuse,
232 'WS to get SupplierInvoicesForThirdParty'
233);
234
235
245function getSupplierInvoice($authentication, $id = '', $ref = '', $ref_ext = '')
246{
247 global $db, $conf;
248
249 dol_syslog("Function: getSupplierInvoice login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
250
251 if ($authentication['entity']) {
252 $conf->entity = $authentication['entity'];
253 }
254
255 // Init and check authentication
256 $objectresp = array();
257 $errorcode = ''; $errorlabel = '';
258 $error = 0;
259 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
260 // Check parameters
261 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
262 $error++;
263 $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
264 }
265
266 if (!$error) {
267 $fuser->getrights();
268
269 if ($fuser->rights->fournisseur->facture->lire) {
270 $invoice = new FactureFournisseur($db);
271 $result = $invoice->fetch($id, $ref, $ref_ext);
272 if ($result > 0) {
273 $linesresp = array();
274 $i = 0;
275 foreach ($invoice->lines as $line) {
276 //var_dump($line); exit;
277 $linesresp[] = array(
278 'id'=>$line->rowid,
279 'type'=>$line->product_type,
280 'total_net'=>$line->total_ht,
281 'total_vat'=>$line->total_tva,
282 'total'=>$line->total_ttc,
283 'vat_rate'=>$line->tva_tx,
284 'qty'=>$line->qty
285 );
286 $i++;
287 }
288
289 // Create invoice
290 $objectresp = array(
291 'result'=>array('result_code'=>'OK', 'result_label'=>''),
292 'invoice'=>array(
293 'id' => $invoice->id,
294 'ref' => $invoice->ref,
295 'ref_supplier'=>$invoice->ref_supplier,
296 'ref_ext' => $invoice->ref_ext,
297 'fk_user_author' => $invoice->fk_user_author,
298 'fk_user_valid' => $invoice->fk_user_valid,
299 'fk_thirdparty' => $invoice->fk_soc,
300 'type'=>$invoice->type,
301 'status'=>$invoice->statut,
302 'total_net'=>$invoice->total_ht,
303 'total_vat'=>$invoice->total_tva,
304 'total'=>$invoice->total_ttc,
305 'date_creation'=>dol_print_date($invoice->datec, 'dayhourrfc'),
306 'date_modification'=>dol_print_date($invoice->tms, 'dayhourrfc'),
307 'date_invoice'=>dol_print_date($invoice->date, 'dayhourrfc'),
308 'date_term'=>dol_print_date($invoice->date_echeance, 'dayhourrfc'),
309 'label'=>$invoice->label,
310 'paid'=>$invoice->paid,
311 'note_private'=>$invoice->note_private,
312 'note_public'=>$invoice->note_public,
313 'close_code'=>$invoice->close_code,
314 'close_note'=>$invoice->close_note,
315
316 'lines' => $linesresp,
317 // 'lines' => array('0'=>array('id'=>222,'type'=>1),
318 // '1'=>array('id'=>333,'type'=>1)),
319
320 ));
321 } else {
322 $error++;
323 $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
324 }
325 } else {
326 $error++;
327 $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
328 }
329 }
330
331 if ($error) {
332 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
333 }
334
335 return $objectresp;
336}
337
338
346function getSupplierInvoicesForThirdParty($authentication, $idthirdparty)
347{
348 global $db, $conf;
349
350 dol_syslog("Function: getSupplierInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
351
352 if ($authentication['entity']) {
353 $conf->entity = $authentication['entity'];
354 }
355
356 // Init and check authentication
357 $objectresp = array();
358 $errorcode = ''; $errorlabel = '';
359 $error = 0;
360 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
361
362 // Check parameters
363 if (!$error && empty($idthirdparty)) {
364 $error++;
365 $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter id is not provided';
366 }
367
368 if (!$error) {
369 $linesinvoice = array();
370
371 $sql = "SELECT f.rowid as facid";
372 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
373 $sql .= " WHERE f.entity = ".((int) $conf->entity);
374 if ($idthirdparty != 'all') {
375 $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
376 }
377
378 $resql = $db->query($sql);
379 if ($resql) {
380 $num = $db->num_rows($resql);
381 $i = 0;
382 while ($i < $num) {
383 // En attendant remplissage par boucle
384 $obj = $db->fetch_object($resql);
385
386 $invoice = new FactureFournisseur($db);
387 $result = $invoice->fetch($obj->facid);
388 if ($result < 0) {
389 $error++;
390 $errorcode = $result; $errorlabel = $invoice->error;
391 break;
392 }
393
394 // Define lines of invoice
395 $linesresp = array();
396 foreach ($invoice->lines as $line) {
397 $linesresp[] = array(
398 'id'=>$line->rowid,
399 'type'=>$line->product_type,
400 'desc'=>dol_htmlcleanlastbr($line->description),
401 'total_net'=>$line->total_ht,
402 'total_vat'=>$line->total_tva,
403 'total'=>$line->total_ttc,
404 'vat_rate'=>$line->tva_tx,
405 'qty'=>$line->qty,
406 'product_ref'=>$line->product_ref,
407 'product_label'=>$line->product_label,
408 'product_desc'=>$line->product_desc,
409 );
410 }
411
412 // Now define invoice
413 $linesinvoice[] = array(
414 'id'=>$invoice->id,
415 'ref'=>$invoice->ref,
416 'ref_supplier'=>$invoice->ref_supplier,
417 'ref_ext'=>$invoice->ref_ext,
418 'fk_user_author' => $invoice->fk_user_author,
419 'fk_user_valid' => $invoice->fk_user_valid,
420 'fk_thirdparty' => $invoice->fk_soc,
421 'type'=>$invoice->type,
422 'status'=>$invoice->statut,
423 'total_net'=>$invoice->total_ht,
424 'total_vat'=>$invoice->total_tva,
425 'total'=>$invoice->total_ttc,
426 'date_creation'=>dol_print_date($invoice->datec, 'dayhourrfc'),
427 'date_modification'=>dol_print_date($invoice->tms, 'dayhourrfc'),
428 'date_invoice'=>dol_print_date($invoice->date, 'dayhourrfc'),
429 'date_term'=>dol_print_date($invoice->date_echeance, 'dayhourrfc'),
430 'label'=>$invoice->label,
431 'paid'=>$invoice->paid,
432 'note_private'=>$invoice->note_private,
433 'note_public'=>$invoice->note_public,
434 'close_code'=>$invoice->close_code,
435 'close_note'=>$invoice->close_note,
436
437 'lines' => $linesresp
438 );
439
440 $i++;
441 }
442
443 $objectresp = array(
444 'result'=>array('result_code'=>'OK', 'result_label'=>''),
445 'invoices'=>$linesinvoice
446
447 );
448 } else {
449 $error++;
450 $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
451 }
452 }
453
454 if ($error) {
455 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
456 }
457
458 return $objectresp;
459}
460
461// Return the results.
462$server->service(file_get_contents("php://input"));
Class to manage suppliers invoices.
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_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.
getSupplierInvoicesForThirdParty($authentication, $idthirdparty)
Get list of invoices for third party.
getSupplierInvoice($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