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