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