dolibarr 19.0.4
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 (!getDolGlobalString('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 = 0, $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 = '';
258 $errorlabel = '';
259 $error = 0;
260 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
261 // Check parameters
262 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
263 $error++;
264 $errorcode = 'BAD_PARAMETERS';
265 $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
266 }
267
268 if (!$error) {
269 $fuser->getrights();
270
271 if ($fuser->hasRight('fournisseur', 'facture', 'lire')) {
272 $invoice = new FactureFournisseur($db);
273 $result = $invoice->fetch($id, $ref, $ref_ext);
274 if ($result > 0) {
275 $linesresp = array();
276 $i = 0;
277 foreach ($invoice->lines as $line) {
278 //var_dump($line); exit;
279 $linesresp[] = array(
280 'id'=>$line->rowid,
281 'type'=>$line->product_type,
282 'total_net'=>$line->total_ht,
283 'total_vat'=>$line->total_tva,
284 'total'=>$line->total_ttc,
285 'vat_rate'=>$line->tva_tx,
286 'qty'=>$line->qty
287 );
288 $i++;
289 }
290
291 // Create invoice
292 $objectresp = array(
293 'result'=>array('result_code'=>'OK', 'result_label'=>''),
294 'invoice'=>array(
295 'id' => $invoice->id,
296 'ref' => $invoice->ref,
297 'ref_supplier'=>$invoice->ref_supplier,
298 'ref_ext' => $invoice->ref_ext,
299 'fk_user_author' => $invoice->fk_user_author,
300 'fk_user_valid' => $invoice->fk_user_valid,
301 'fk_thirdparty' => $invoice->fk_soc,
302 'type'=>$invoice->type,
303 'status'=>$invoice->statut,
304 'total_net'=>$invoice->total_ht,
305 'total_vat'=>$invoice->total_tva,
306 'total'=>$invoice->total_ttc,
307 'date_creation'=>dol_print_date($invoice->datec, 'dayhourrfc'),
308 'date_modification'=>dol_print_date($invoice->tms, 'dayhourrfc'),
309 'date_invoice'=>dol_print_date($invoice->date, 'dayhourrfc'),
310 'date_term'=>dol_print_date($invoice->date_echeance, 'dayhourrfc'),
311 'label'=>$invoice->label,
312 'paid'=>$invoice->paid,
313 'note_private'=>$invoice->note_private,
314 'note_public'=>$invoice->note_public,
315 'close_code'=>$invoice->close_code,
316 'close_note'=>$invoice->close_note,
317
318 'lines' => $linesresp,
319 // 'lines' => array('0'=>array('id'=>222,'type'=>1),
320 // '1'=>array('id'=>333,'type'=>1)),
321
322 ));
323 } else {
324 $error++;
325 $errorcode = 'NOT_FOUND';
326 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
327 }
328 } else {
329 $error++;
330 $errorcode = 'PERMISSION_DENIED';
331 $errorlabel = 'User does not have permission for this request';
332 }
333 }
334
335 if ($error) {
336 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
337 }
338
339 return $objectresp;
340}
341
342
350function getSupplierInvoicesForThirdParty($authentication, $idthirdparty)
351{
352 global $db, $conf;
353
354 dol_syslog("Function: getSupplierInvoicesForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
355
356 if ($authentication['entity']) {
357 $conf->entity = $authentication['entity'];
358 }
359
360 // Init and check authentication
361 $objectresp = array();
362 $errorcode = '';
363 $errorlabel = '';
364 $error = 0;
365 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
366
367 // Check parameters
368 if (!$error && empty($idthirdparty)) {
369 $error++;
370 $errorcode = 'BAD_PARAMETERS';
371 $errorlabel = 'Parameter id is not provided';
372 }
373
374 if (!$error) {
375 $linesinvoice = array();
376
377 $sql = "SELECT f.rowid as facid";
378 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
379 $sql .= " WHERE f.entity = ".((int) $conf->entity);
380 if ($idthirdparty != 'all') {
381 $sql .= " AND f.fk_soc = ".((int) $idthirdparty);
382 }
383
384 $resql = $db->query($sql);
385 if ($resql) {
386 $num = $db->num_rows($resql);
387 $i = 0;
388 while ($i < $num) {
389 // En attendant remplissage par boucle
390 $obj = $db->fetch_object($resql);
391
392 $invoice = new FactureFournisseur($db);
393 $result = $invoice->fetch($obj->facid);
394 if ($result < 0) {
395 $error++;
396 $errorcode = $result;
397 $errorlabel = $invoice->error;
398 break;
399 }
400
401 // Define lines of invoice
402 $linesresp = array();
403 foreach ($invoice->lines as $line) {
404 $linesresp[] = array(
405 'id'=>$line->rowid,
406 'type'=>$line->product_type,
407 'desc'=>dol_htmlcleanlastbr($line->description),
408 'total_net'=>$line->total_ht,
409 'total_vat'=>$line->total_tva,
410 'total'=>$line->total_ttc,
411 'vat_rate'=>$line->tva_tx,
412 'qty'=>$line->qty,
413 'product_ref'=>$line->product_ref,
414 'product_label'=>$line->product_label,
415 'product_desc'=>$line->product_desc,
416 );
417 }
418
419 // Now define invoice
420 $linesinvoice[] = array(
421 'id'=>$invoice->id,
422 'ref'=>$invoice->ref,
423 'ref_supplier'=>$invoice->ref_supplier,
424 'ref_ext'=>$invoice->ref_ext,
425 'fk_user_author' => $invoice->user_creation_id,
426 'fk_user_valid' => $invoice->user_validation_id,
427 'fk_thirdparty' => $invoice->socid,
428 'type'=>$invoice->type,
429 'status'=>$invoice->status,
430 'total_net'=>$invoice->total_ht,
431 'total_vat'=>$invoice->total_tva,
432 'total'=>$invoice->total_ttc,
433 'date_creation'=>dol_print_date($invoice->datec, 'dayhourrfc'),
434 'date_modification'=>dol_print_date($invoice->tms, 'dayhourrfc'),
435 'date_invoice'=>dol_print_date($invoice->date, 'dayhourrfc'),
436 'date_term'=>dol_print_date($invoice->date_echeance, 'dayhourrfc'),
437 'label'=>$invoice->label,
438 'paid'=>$invoice->paid,
439 'note_private'=>$invoice->note_private,
440 'note_public'=>$invoice->note_public,
441 'close_code'=>$invoice->close_code,
442 'close_note'=>$invoice->close_note,
443
444 'lines' => $linesresp
445 );
446
447 $i++;
448 }
449
450 $objectresp = array(
451 'result'=>array('result_code'=>'OK', 'result_label'=>''),
452 'invoices'=>$linesinvoice
453
454 );
455 } else {
456 $error++;
457 $errorcode = $db->lasterrno();
458 $errorlabel = $db->lasterror();
459 }
460 }
461
462 if ($error) {
463 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
464 }
465
466 return $objectresp;
467}
468
469// Return the results.
470$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.
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.
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:35