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