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