dolibarr  16.0.5
server_order.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2012 JF FERRY <jfefe@aternatik.fr>
4  * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19 
25 if (!defined("NOCSRFCHECK")) {
26  define("NOCSRFCHECK", '1');
27 }
28 
29 require '../master.inc.php';
30 require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
33 require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";
34 
35 
36 dol_syslog("Call Dolibarr webservices interfaces");
37 
38 $langs->load("main");
39 
40 // Enable and test if module web services is enabled
41 if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
42  $langs->load("admin");
43  dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
44  print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
45  print $langs->trans("ToActivateModule");
46  exit;
47 }
48 
49 // Create the soap Object
50 $server = new nusoap_server();
51 $server->soap_defencoding = 'UTF-8';
52 $server->decode_utf8 = false;
53 $ns = 'http://www.dolibarr.org/ns/';
54 $server->configureWSDL('WebServicesDolibarrOrder', $ns);
55 $server->wsdl->schemaTargetNamespace = $ns;
56 
57 
58 // Define WSDL Authentication object
59 $server->wsdl->addComplexType(
60  'authentication',
61  'complexType',
62  'struct',
63  'all',
64  '',
65  array(
66  'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
67  'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
68  'login' => array('name'=>'login', 'type'=>'xsd:string'),
69  'password' => array('name'=>'password', 'type'=>'xsd:string'),
70  'entity' => array('name'=>'entity', 'type'=>'xsd:string')
71  )
72 );
73 // Define WSDL Return object
74 $server->wsdl->addComplexType(
75  'result',
76  'complexType',
77  'struct',
78  'all',
79  '',
80  array(
81  'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
82  'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
83  )
84 );
85 
86 $line_fields = array(
87  'id' => array('name'=>'id', 'type'=>'xsd:string'),
88  'type' => array('name'=>'type', 'type'=>'xsd:int'),
89  'fk_commande' => array('name'=>'fk_commande', 'type'=>'xsd:int'),
90  'fk_parent_line' => array('name'=>'fk_parent_line', 'type'=>'xsd:int'),
91  'desc' => array('name'=>'desc', 'type'=>'xsd:string'),
92  'qty' => array('name'=>'qty', 'type'=>'xsd:double'),
93  'price' => array('name'=>'price', 'type'=>'xsd:double'),
94  'unitprice' => array('name'=>'unitprice', 'type'=>'xsd:double'),
95  'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:double'),
96 
97  'remise' => array('name'=>'remise', 'type'=>'xsd:double'),
98  'remise_percent' => array('name'=>'remise_percent', 'type'=>'xsd:double'),
99 
100  'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
101  'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
102  'total' => array('name'=>'total', 'type'=>'xsd:double'),
103 
104  'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
105  'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
106 
107  // From product
108  'product_id' => array('name'=>'product_id', 'type'=>'xsd:int'),
109  'product_ref' => array('name'=>'product_ref', 'type'=>'xsd:string'),
110  'product_label' => array('name'=>'product_label', 'type'=>'xsd:string'),
111  'product_desc' => array('name'=>'product_desc', 'type'=>'xsd:string')
112 );
113 
114 $elementtype = 'commandedet';
115 
116 //Retrieve all extrafield for thirdsparty
117 // fetch optionals attributes and labels
118 $extrafields = new ExtraFields($db);
119 $extrafields->fetch_name_optionals_label($elementtype, true);
120 $extrafield_line_array = null;
121 if (is_array($extrafields) && count($extrafields) > 0) {
122  $extrafield_line_array = array();
123 }
124 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
125  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
126  //$value=$object->array_options["options_".$key];
127  $type = $extrafields->attributes[$elementtype]['type'][$key];
128  if ($type == 'date' || $type == 'datetime') {
129  $type = 'xsd:dateTime';
130  } else {
131  $type = 'xsd:string';
132  }
133  $extrafield_line_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
134  }
135 }
136 if (is_array($extrafield_line_array)) {
137  $line_fields = array_merge($line_fields, $extrafield_line_array);
138 }
139 
140 // Define other specific objects
141 $server->wsdl->addComplexType(
142  'line',
143  'complexType',
144  'struct',
145  'all',
146  '',
147  $line_fields
148 );
149 
150 /*$server->wsdl->addComplexType(
151  'LinesArray',
152  'complexType',
153  'array',
154  '',
155  'SOAP-ENC:Array',
156  array(),
157  array(
158  array(
159  'ref'=>'SOAP-ENC:arrayType',
160  'wsdl:arrayType'=>'tns:line[]'
161  )
162  ),
163  'tns:line'
164 );*/
165 $server->wsdl->addComplexType(
166  'LinesArray2',
167  'complexType',
168  'array',
169  'sequence',
170  '',
171  array(
172  'line' => array(
173  'name' => 'line',
174  'type' => 'tns:line',
175  'minOccurs' => '0',
176  'maxOccurs' => 'unbounded'
177  )
178  )
179 );
180 
181 $order_fields = array(
182  'id' => array('name'=>'id', 'type'=>'xsd:string'),
183  'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
184  'ref_client' => array('name'=>'ref_client', 'type'=>'xsd:string'),
185  'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
186  'ref_int' => array('name'=>'ref_int', 'type'=>'xsd:string'),
187  'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
188  'status' => array('name'=>'status', 'type'=>'xsd:int'),
189  'billed' => array('name'=>'billed', 'type'=>'xsd:string'),
190  'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
191  'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
192  'total_localtax1' => array('name'=>'total_localtax1', 'type'=>'xsd:double'),
193  'total_localtax2' => array('name'=>'total_localtax2', 'type'=>'xsd:double'),
194  'total' => array('name'=>'total', 'type'=>'xsd:double'),
195  'date' => array('name'=>'date', 'type'=>'xsd:date'),
196  'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
197  'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
198  'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
199  'remise' => array('name'=>'remise', 'type'=>'xsd:string'),
200  'remise_percent' => array('name'=>'remise_percent', 'type'=>'xsd:string'),
201  'remise_absolue' => array('name'=>'remise_absolue', 'type'=>'xsd:string'),
202  'source' => array('name'=>'source', 'type'=>'xsd:string'),
203  'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
204  'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
205  'project_id' => array('name'=>'project_id', 'type'=>'xsd:string'),
206 
207  'mode_reglement_id' => array('name'=>'mode_reglement_id', 'type'=>'xsd:string'),
208  'mode_reglement_code' => array('name'=>'mode_reglement_code', 'type'=>'xsd:string'),
209  'mode_reglement' => array('name'=>'mode_reglement', 'type'=>'xsd:string'),
210  'cond_reglement_id' => array('name'=>'cond_reglement_id', 'type'=>'xsd:string'),
211  'cond_reglement_code' => array('name'=>'cond_reglement_code', 'type'=>'xsd:string'),
212  'cond_reglement' => array('name'=>'cond_reglement', 'type'=>'xsd:string'),
213  'cond_reglement_doc' => array('name'=>'cond_reglement_doc', 'type'=>'xsd:string'),
214 
215  'date_livraison' => array('name'=>'date_livraison', 'type'=>'xsd:date'),
216  'demand_reason_id' => array('name'=>'demand_reason_id', 'type'=>'xsd:string'),
217 
218  'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray2')
219 );
220 
221 $elementtype = 'commande';
222 
223 //Retrieve all extrafield for thirdsparty
224 // fetch optionals attributes and labels
225 $extrafields = new ExtraFields($db);
226 $extrafields->fetch_name_optionals_label($elementtype, true);
227 $extrafield_array = null;
228 if (is_array($extrafields) && count($extrafields) > 0) {
229  $extrafield_array = array();
230 }
231 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
232  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
233  //$value=$object->array_options["options_".$key];
234  $type = $extrafields->attributes[$elementtype]['type'][$key];
235  if ($type == 'date' || $type == 'datetime') {
236  $type = 'xsd:dateTime';
237  } else {
238  $type = 'xsd:string';
239  }
240  $extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
241  }
242 }
243 if (is_array($extrafield_array)) {
244  $order_fields = array_merge($order_fields, $extrafield_array);
245 }
246 
247 $server->wsdl->addComplexType(
248  'order',
249  'complexType',
250  'struct',
251  'all',
252  '',
253  $order_fields
254 );
255 
256 /*
257 $server->wsdl->addComplexType(
258  'OrdersArray',
259  'complexType',
260  'array',
261  '',
262  'SOAP-ENC:Array',
263  array(),
264  array(
265  array(
266  'ref'=>'SOAP-ENC:arrayType',
267  'wsdl:arrayType'=>'tns:order[]'
268  )
269  ),
270  'tns:order'
271 );*/
272 $server->wsdl->addComplexType(
273  'OrdersArray2',
274  'complexType',
275  'array',
276  'sequence',
277  '',
278  array(
279  'order' => array(
280  'name' => 'order',
281  'type' => 'tns:order',
282  'minOccurs' => '0',
283  'maxOccurs' => 'unbounded'
284  )
285  )
286 );
287 
288 
289 
290 // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
291 // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
292 // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
293 $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
294 $styleuse = 'encoded'; // encoded/literal/literal wrapped
295 // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
296 
297 // Register WSDL
298 $server->register(
299  'getOrder',
300  array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Entry values
301  array('result'=>'tns:result', 'order'=>'tns:order'), // Exit values
302  $ns,
303  $ns.'#getOrder',
304  $styledoc,
305  $styleuse,
306  'WS to get a particular invoice'
307 );
308 
309 $server->register(
310  'getOrdersForThirdParty',
311  array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'), // Entry values
312  array('result'=>'tns:result', 'orders'=>'tns:OrdersArray2'), // Exit values
313  $ns,
314  $ns.'#getOrdersForThirdParty',
315  $styledoc,
316  $styleuse,
317  'WS to get all orders of a third party'
318 );
319 
320 $server->register(
321  'createOrder',
322  array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
323  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string'), // Exit values
324  $ns,
325  $ns.'#createOrder',
326  $styledoc,
327  $styleuse,
328  'WS to create an order'
329 );
330 
331 $server->register(
332  'updateOrder',
333  array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
334  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Exit values
335  $ns,
336  $ns.'#updateOrder',
337  $styledoc,
338  $styleuse,
339  'WS to update an order'
340 );
341 
342 $server->register(
343  'validOrder',
344  array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'id_warehouse'=>'xsd:string'), // Entry values
345  array('result'=>'tns:result'), // Exit values
346  $ns,
347  $ns.'#validOrder',
348  $styledoc,
349  $styleuse,
350  'WS to valid an order'
351 );
352 
362 function getOrder($authentication, $id = '', $ref = '', $ref_ext = '')
363 {
364  global $db, $conf;
365 
366  dol_syslog("Function: getOrder login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
367 
368  if ($authentication['entity']) {
369  $conf->entity = $authentication['entity'];
370  }
371 
372  // Init and check authentication
373  $objectresp = array();
374  $errorcode = ''; $errorlabel = '';
375  $error = 0;
376  $socid = 0;
377 
378  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
379 
380  if ($fuser->socid) {
381  $socid = $fuser->socid;
382  }
383 
384  // Check parameters
385  if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
386  $error++;
387  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
388  }
389 
390  if (!$error) {
391  $fuser->getrights();
392 
393  if ($fuser->rights->commande->lire) {
394  $order = new Commande($db);
395  $result = $order->fetch($id, $ref, $ref_ext);
396  if ($result > 0) {
397  // Security for external user
398  if ($socid && $socid != $order->socid) {
399  $error++;
400  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
401  }
402 
403  if (!$error) {
404  $linesresp = array();
405  $i = 0;
406  foreach ($order->lines as $line) {
407  //var_dump($line); exit;
408  $linesresp[] = array(
409  'id'=>$line->rowid,
410  'fk_commande'=>$line->fk_commande,
411  'fk_parent_line'=>$line->fk_parent_line,
412  'desc'=>$line->desc,
413  'qty'=>$line->qty,
414  'price'=>$line->price,
415  'unitprice'=>$line->subprice,
416  'vat_rate'=>$line->tva_tx,
417  'remise'=>$line->remise,
418  'remise_percent'=>$line->remise_percent,
419  'product_id'=>$line->fk_product,
420  'product_type'=>$line->product_type,
421  'total_net'=>$line->total_ht,
422  'total_vat'=>$line->total_tva,
423  'total'=>$line->total_ttc,
424  'date_start'=>$line->date_start,
425  'date_end'=>$line->date_end,
426  'product_ref'=>$line->product_ref,
427  'product_label'=>$line->product_label,
428  'product_desc'=>$line->product_desc
429  );
430  $i++;
431  }
432 
433  // Create order
434  $objectresp = array(
435  'result'=>array('result_code'=>'OK', 'result_label'=>''),
436  'order'=>array(
437  'id' => $order->id,
438  'ref' => $order->ref,
439  'ref_client' => $order->ref_client,
440  'ref_ext' => $order->ref_ext,
441  'ref_int' => $order->ref_int,
442  'thirdparty_id' => $order->socid,
443  'status' => $order->statut,
444 
445  'total_net' => $order->total_ht,
446  'total_vat' => $order->total_tva,
447  'total_localtax1' => $order->total_localtax1,
448  'total_localtax2' => $order->total_localtax2,
449  'total' => $order->total_ttc,
450  'project_id' => $order->fk_project,
451 
452  'date' => $order->date ?dol_print_date($order->date, 'dayrfc') : '',
453  'date_creation' => $order->date_creation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
454  'date_validation' => $order->date_validation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
455  'date_modification' => $order->date_modification ?dol_print_date($order->date_modification, 'dayhourrfc') : '',
456 
457  'remise' => $order->remise,
458  'remise_percent' => $order->remise_percent,
459  'remise_absolue' => $order->remise_absolue,
460 
461  'source' => $order->source,
462  'billed' => $order->billed,
463  'note_private' => $order->note_private,
464  'note_public' => $order->note_public,
465  'cond_reglement_id' => $order->cond_reglement_id,
466  'cond_reglement_code' => $order->cond_reglement_code,
467  'cond_reglement' => $order->cond_reglement,
468  'mode_reglement_id' => $order->mode_reglement_id,
469  'mode_reglement_code' => $order->mode_reglement_code,
470  'mode_reglement' => $order->mode_reglement,
471 
472  'date_livraison' => $order->delivery_date,
473 
474  'demand_reason_id' => $order->demand_reason_id,
475  'demand_reason_code' => $order->demand_reason_code,
476 
477  'lines' => $linesresp
478  ));
479  }
480  } else {
481  $error++;
482  $errorcode = 'NOT_FOUND';
483  $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
484  }
485  } else {
486  $error++;
487  $errorcode = 'PERMISSION_DENIED';
488  $errorlabel = 'User does not have permission for this request';
489  }
490  }
491 
492  if ($error) {
493  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
494  }
495 
496  return $objectresp;
497 }
498 
499 
507 function getOrdersForThirdParty($authentication, $idthirdparty)
508 {
509  global $db, $conf;
510 
511  dol_syslog("Function: getOrdersForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
512 
513  if ($authentication['entity']) {
514  $conf->entity = $authentication['entity'];
515  }
516 
517  // Init and check authentication
518  $objectresp = array();
519  $errorcode = ''; $errorlabel = '';
520  $error = 0;
521  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
522 
523  if ($fuser->socid) {
524  $socid = $fuser->socid;
525  }
526 
527  // Check parameters
528  if (!$error && empty($idthirdparty)) {
529  $error++;
530  $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter id is not provided';
531  }
532 
533  if (!$error) {
534  $linesorders = array();
535 
536  $sql = 'SELECT c.rowid as orderid';
537  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c';
538  $sql .= " WHERE c.entity = ".$conf->entity;
539  if ($idthirdparty != 'all') {
540  $sql .= " AND c.fk_soc = ".((int) $idthirdparty);
541  }
542 
543 
544  $resql = $db->query($sql);
545  if ($resql) {
546  $num = $db->num_rows($resql);
547  $i = 0;
548  while ($i < $num) {
549  // En attendant remplissage par boucle
550  $obj = $db->fetch_object($resql);
551 
552  $order = new Commande($db);
553  $order->fetch($obj->orderid);
554 
555  // Sécurité pour utilisateur externe
556  if ($socid && ($socid != $order->socid)) {
557  $error++;
558  $errorcode = 'PERMISSION_DENIED';
559  $errorlabel = $order->socid.' User does not have permission for this request';
560  }
561 
562  if (!$error) {
563  // Define lines of invoice
564  $linesresp = array();
565  foreach ($order->lines as $line) {
566  $linesresp[] = array(
567  'id'=>$line->rowid,
568  'type'=>$line->product_type,
569  'fk_commande'=>$line->fk_commande,
570  'fk_parent_line'=>$line->fk_parent_line,
571  'desc'=>$line->desc,
572  'qty'=>$line->qty,
573  'price'=>$line->price,
574  'unitprice'=>$line->subprice,
575  'tva_tx'=>$line->tva_tx,
576  'remise'=>$line->remise,
577  'remise_percent'=>$line->remise_percent,
578  'total_net'=>$line->total_ht,
579  'total_vat'=>$line->total_tva,
580  'total'=>$line->total_ttc,
581  'date_start'=>$line->date_start,
582  'date_end'=>$line->date_end,
583  'product_id'=>$line->fk_product,
584  'product_ref'=>$line->product_ref,
585  'product_label'=>$line->product_label,
586  'product_desc'=>$line->product_desc
587  );
588  }
589 
590  // Now define invoice
591  $linesorders[] = array(
592  'id' => $order->id,
593  'ref' => $order->ref,
594  'ref_client' => $order->ref_client,
595  'ref_ext' => $order->ref_ext,
596  'ref_int' => $order->ref_int,
597  'socid' => $order->socid,
598  'status' => $order->statut,
599 
600  'total_net' => $order->total_ht,
601  'total_vat' => $order->total_tva,
602  'total_localtax1' => $order->total_localtax1,
603  'total_localtax2' => $order->total_localtax2,
604  'total' => $order->total_ttc,
605  'project_id' => $order->fk_project,
606 
607  'date' => $order->date_commande ?dol_print_date($order->date_commande, 'dayrfc') : '',
608 
609  'remise' => $order->remise,
610  'remise_percent' => $order->remise_percent,
611  'remise_absolue' => $order->remise_absolue,
612 
613  'source' => $order->source,
614  'billed' => $order->billed,
615  'note_private' => $order->note_private,
616  'note_public' => $order->note_public,
617  'cond_reglement_id' => $order->cond_reglement_id,
618  'cond_reglement' => $order->cond_reglement,
619  'cond_reglement_doc' => $order->cond_reglement_doc,
620  'cond_reglement_code' => $order->cond_reglement_code,
621  'mode_reglement_id' => $order->mode_reglement_id,
622  'mode_reglement' => $order->mode_reglement,
623  'mode_reglement_code' => $order->mode_reglement_code,
624 
625  'date_livraison' => $order->delivery_date,
626 
627  'demand_reason_id' => $order->demand_reason_id,
628  'demand_reason_code' => $order->demand_reason_code,
629 
630  'lines' => $linesresp
631  );
632  }
633  $i++;
634  }
635 
636  $objectresp = array(
637  'result'=>array('result_code'=>'OK', 'result_label'=>''),
638  'orders'=>$linesorders
639 
640  );
641  } else {
642  $error++;
643  $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
644  }
645  }
646 
647  if ($error) {
648  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
649  }
650 
651  return $objectresp;
652 }
653 
654 
662 function createOrder($authentication, $order)
663 {
664  global $db, $conf, $langs;
665 
666  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
667 
668  $now = dol_now();
669 
670  dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
671 
672  if ($authentication['entity']) {
673  $conf->entity = $authentication['entity'];
674  }
675 
676  // Init and check authentication
677  $objectresp = array();
678  $errorcode = '';
679  $errorlabel = '';
680  $error = 0;
681  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
682 
683  // Check parameters
684 
685 
686  if (!$error) {
687  $newobject = new Commande($db);
688  $newobject->socid = $order['thirdparty_id'];
689  $newobject->type = $order['type'];
690  $newobject->ref_ext = $order['ref_ext'];
691  $newobject->date = dol_stringtotime($order['date'], 'dayrfc');
692  $newobject->date_lim_reglement = dol_stringtotime($order['date_due'], 'dayrfc');
693  $newobject->note_private = $order['note_private'];
694  $newobject->note_public = $order['note_public'];
695  $newobject->statut = Commande::STATUS_DRAFT; // We start with status draft
696  $newobject->billed = $order['billed'];
697  $newobject->fk_project = $order['project_id'];
698  $newobject->cond_reglement_id = $order['cond_reglement_id'];
699  $newobject->demand_reason_id = $order['demand_reason_id'];
700  $newobject->date_creation = $now;
701 
702  $elementtype = 'commande';
703 
704  // Retrieve all extrafield for order
705  // fetch optionals attributes and labels
706  $extrafields = new ExtraFields($db);
707  $extrafields->fetch_name_optionals_label($elementtype, true);
708  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
709  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
710  $key = 'options_'.$key;
711  $newobject->array_options[$key] = $order[$key];
712  }
713  }
714 
715  // Trick because nusoap does not store data with same structure if there is one or several lines
716  $arrayoflines = array();
717  if (isset($order['lines']['line'][0])) {
718  $arrayoflines = $order['lines']['line'];
719  } else {
720  $arrayoflines = $order['lines'];
721  }
722 
723  foreach ($arrayoflines as $key => $line) {
724  // $key can be 'line' or '0','1',...
725  $newline = new OrderLine($db);
726 
727  $newline->type = $line['type'];
728  $newline->desc = $line['desc'];
729  $newline->fk_product = $line['product_id'];
730  $newline->tva_tx = $line['vat_rate'];
731  $newline->qty = $line['qty'];
732  $newline->price = $line['price'];
733  $newline->subprice = $line['unitprice'];
734  $newline->total_ht = $line['total_net'];
735  $newline->total_tva = $line['total_vat'];
736  $newline->total_ttc = $line['total'];
737  $newline->date_start = $line['date_start'];
738  $newline->date_end = $line['date_end'];
739 
740  $elementtype = 'commandedet';
741 
742  // Retrieve all extrafield for lines
743  // fetch optionals attributes and labels
744  $extrafields = new ExtraFields($db);
745  $extrafields->fetch_name_optionals_label($elementtype, true);
746  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
747  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
748  $key = 'options_'.$key;
749  $newline->array_options[$key] = $line[$key];
750  }
751  }
752 
753  $newobject->lines[] = $newline;
754  }
755 
756 
757  $db->begin();
758  dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
759  $result = $newobject->create($fuser);
760  dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
761  if ($result < 0) {
762  dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
763  $error++;
764  }
765 
766  if ($order['status'] == 1) { // We want order to have status validated
767  dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
768  $result = $newobject->valid($fuser);
769  if ($result < 0) {
770  dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
771  $error++;
772  }
773  }
774 
775  if ($result >= 0) {
776  dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
777  $db->commit();
778  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
779  } else {
780  dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
781  $db->rollback();
782  $error++;
783  $errorcode = 'KO';
784  $errorlabel = $newobject->error;
785  }
786  }
787 
788  if ($error) {
789  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
790  }
791 
792  return $objectresp;
793 }
794 
795 
804 function validOrder($authentication, $id = '', $id_warehouse = 0)
805 {
806  global $db, $conf, $langs;
807 
808  dol_syslog("Function: validOrder login=".$authentication['login']." id=".$id." id_warehouse=".$id_warehouse);
809 
810  // Init and check authentication
811  $objectresp = array();
812  $errorcode = '';
813  $errorlabel = '';
814  $error = 0;
815  if ($authentication['entity']) {
816  $conf->entity = $authentication['entity'];
817  }
818  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
819 
820  if (!$error) {
821  $fuser->getrights();
822 
823  if ($fuser->rights->commande->lire) {
824  $order = new Commande($db);
825  $result = $order->fetch($id);
826 
827  $order->fetch_thirdparty();
828  $db->begin();
829  if ($result > 0) {
830  $result = $order->valid($fuser, $id_warehouse);
831 
832  if ($result >= 0) {
833  // Define output language
834  $outputlangs = $langs;
835  $order->generateDocument($order->model_pdf, $outputlangs);
836  } else {
837  $db->rollback();
838  $error++;
839  $errorcode = 'KO';
840  $errorlabel = $order->error;
841  }
842  } else {
843  $db->rollback();
844  $error++;
845  $errorcode = 'KO';
846  $errorlabel = $order->error;
847  }
848  } else {
849  $db->rollback();
850  $error++;
851  $errorcode = 'KO';
852  $errorlabel = $order->error;
853  }
854  }
855 
856  if ($error) {
857  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
858  } else {
859  $db->commit();
860  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
861  }
862 
863  return $objectresp;
864 }
865 
873 function updateOrder($authentication, $order)
874 {
875  global $db, $conf, $langs;
876 
877  dol_syslog("Function: updateOrder login=".$authentication['login']);
878 
879  if ($authentication['entity']) {
880  $conf->entity = $authentication['entity'];
881  }
882 
883  // Init and check authentication
884  $objectresp = array();
885  $errorcode = ''; $errorlabel = '';
886  $error = 0;
887  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
888  // Check parameters
889  if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext'])) {
890  $error++; $errorcode = 'KO'; $errorlabel = "Order id or ref or ref_ext is mandatory.";
891  }
892 
893  if (!$error) {
894  $objectfound = false;
895 
896  include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
897 
898  $object = new Commande($db);
899  $result = $object->fetch($order['id'], (empty($order['id']) ? $order['ref'] : ''), (empty($order['id']) && empty($order['ref']) ? $order['ref_ext'] : ''));
900 
901  if (!empty($object->id)) {
902  $objectfound = true;
903 
904  $db->begin();
905 
906  if (isset($order['status'])) {
907  if ($order['status'] == -1) {
908  $result = $object->cancel($fuser);
909  }
910  if ($order['status'] == 1) {
911  $result = $object->valid($fuser);
912  if ($result >= 0) {
913  // Define output language
914  $outputlangs = $langs;
915  $object->generateDocument($order->model_pdf, $outputlangs);
916  }
917  }
918  if ($order['status'] == 0) {
919  $result = $object->set_reopen($fuser);
920  }
921  if ($order['status'] == 3) {
922  $result = $object->cloture($fuser);
923  }
924  }
925 
926  if (isset($order['billed'])) {
927  if ($order['billed']) {
928  $result = $object->classifyBilled($fuser);
929  }
930  if (!$order['billed']) {
931  $result = $object->classifyUnBilled($fuser);
932  }
933  }
934 
935  $elementtype = 'commande';
936 
937  //Retrieve all extrafield for object
938  // fetch optionals attributes and labels
939  $extrafields = new ExtraFields($db);
940  $extrafields->fetch_name_optionals_label($elementtype, true);
941  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
942  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
943  $key = 'options_'.$key;
944  if (isset($order[$key])) {
945  $result = $object->setValueFrom($key, $order[$key], 'commande_extrafields');
946  }
947  }
948  }
949 
950  if ($result <= 0) {
951  $error++;
952  }
953  }
954 
955  if ((!$error) && ($objectfound)) {
956  $db->commit();
957  $objectresp = array(
958  'result'=>array('result_code'=>'OK', 'result_label'=>''),
959  'id'=>$object->id,
960  'ref'=>$object->ref,
961  'ref_ext'=>$object->ref_ext
962  );
963  } elseif ($objectfound) {
964  $db->rollback();
965  $error++;
966  $errorcode = 'KO';
967  $errorlabel = $object->error;
968  } else {
969  $error++;
970  $errorcode = 'NOT_FOUND';
971  $errorlabel = 'Order id='.$order['id'].' ref='.$order['ref'].' ref_ext='.$order['ref_ext'].' cannot be found';
972  }
973  }
974 
975  if ($error) {
976  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
977  }
978 
979  return $objectresp;
980 }
981 
982 
983 // Return the results.
984 $server->service(file_get_contents("php://input"));
getOrdersForThirdParty
getOrdersForThirdParty($authentication, $idthirdparty)
Get list of orders for third party.
Definition: server_order.php:507
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
validOrder
validOrder($authentication, $id='', $id_warehouse=0)
Valid an order.
Definition: server_order.php:804
updateOrder
updateOrder($authentication, $order)
Update an order.
Definition: server_order.php:873
OrderLine
Class to manage order lines.
Definition: commande.class.php:4146
getOrder
getOrder($authentication, $id='', $ref='', $ref_ext='')
Get order from id, ref or ref_ext.
Definition: server_order.php:362
Commande
Class to manage customers orders.
Definition: commande.class.php:46
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Commande\STATUS_DRAFT
const STATUS_DRAFT
Draft status.
Definition: commande.class.php:381
ExtraFields
Class to manage standard extra fields.
Definition: extrafields.class.php:39
createOrder
createOrder($authentication, $order)
Create order.
Definition: server_order.php:662
check_authentication
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition: ws.lib.php:35
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$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
dol_stringtotime
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:383