dolibarr  17.0.4
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  'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
187  'status' => array('name'=>'status', 'type'=>'xsd:int'),
188  'billed' => array('name'=>'billed', 'type'=>'xsd:string'),
189  'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
190  'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
191  'total_localtax1' => array('name'=>'total_localtax1', 'type'=>'xsd:double'),
192  'total_localtax2' => array('name'=>'total_localtax2', 'type'=>'xsd:double'),
193  'total' => array('name'=>'total', 'type'=>'xsd:double'),
194  'date' => array('name'=>'date', 'type'=>'xsd:date'),
195  'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
196  'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
197  'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
198  'remise' => array('name'=>'remise', 'type'=>'xsd:string'),
199  'remise_percent' => array('name'=>'remise_percent', 'type'=>'xsd:string'),
200  'remise_absolue' => array('name'=>'remise_absolue', 'type'=>'xsd:string'),
201  'source' => array('name'=>'source', 'type'=>'xsd:string'),
202  'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
203  'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
204  'project_id' => array('name'=>'project_id', 'type'=>'xsd:string'),
205 
206  'mode_reglement_id' => array('name'=>'mode_reglement_id', 'type'=>'xsd:string'),
207  'mode_reglement_code' => array('name'=>'mode_reglement_code', 'type'=>'xsd:string'),
208  'mode_reglement' => array('name'=>'mode_reglement', 'type'=>'xsd:string'),
209  'cond_reglement_id' => array('name'=>'cond_reglement_id', 'type'=>'xsd:string'),
210  'cond_reglement_code' => array('name'=>'cond_reglement_code', 'type'=>'xsd:string'),
211  'cond_reglement' => array('name'=>'cond_reglement', 'type'=>'xsd:string'),
212  'cond_reglement_doc' => array('name'=>'cond_reglement_doc', 'type'=>'xsd:string'),
213 
214  'date_livraison' => array('name'=>'date_livraison', 'type'=>'xsd:date'),
215  'demand_reason_id' => array('name'=>'demand_reason_id', 'type'=>'xsd:string'),
216 
217  'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray2')
218 );
219 
220 $elementtype = 'commande';
221 
222 //Retrieve all extrafield for thirdsparty
223 // fetch optionals attributes and labels
224 $extrafields = new ExtraFields($db);
225 $extrafields->fetch_name_optionals_label($elementtype, true);
226 $extrafield_array = null;
227 if (is_array($extrafields) && count($extrafields) > 0) {
228  $extrafield_array = array();
229 }
230 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
231  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
232  //$value=$object->array_options["options_".$key];
233  $type = $extrafields->attributes[$elementtype]['type'][$key];
234  if ($type == 'date' || $type == 'datetime') {
235  $type = 'xsd:dateTime';
236  } else {
237  $type = 'xsd:string';
238  }
239  $extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
240  }
241 }
242 if (is_array($extrafield_array)) {
243  $order_fields = array_merge($order_fields, $extrafield_array);
244 }
245 
246 $server->wsdl->addComplexType(
247  'order',
248  'complexType',
249  'struct',
250  'all',
251  '',
252  $order_fields
253 );
254 
255 /*
256 $server->wsdl->addComplexType(
257  'OrdersArray',
258  'complexType',
259  'array',
260  '',
261  'SOAP-ENC:Array',
262  array(),
263  array(
264  array(
265  'ref'=>'SOAP-ENC:arrayType',
266  'wsdl:arrayType'=>'tns:order[]'
267  )
268  ),
269  'tns:order'
270 );*/
271 $server->wsdl->addComplexType(
272  'OrdersArray2',
273  'complexType',
274  'array',
275  'sequence',
276  '',
277  array(
278  'order' => array(
279  'name' => 'order',
280  'type' => 'tns:order',
281  'minOccurs' => '0',
282  'maxOccurs' => 'unbounded'
283  )
284  )
285 );
286 
287 
288 
289 // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
290 // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
291 // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
292 $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
293 $styleuse = 'encoded'; // encoded/literal/literal wrapped
294 // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
295 
296 // Register WSDL
297 $server->register(
298  'getOrder',
299  array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Entry values
300  array('result'=>'tns:result', 'order'=>'tns:order'), // Exit values
301  $ns,
302  $ns.'#getOrder',
303  $styledoc,
304  $styleuse,
305  'WS to get a particular invoice'
306 );
307 
308 $server->register(
309  'getOrdersForThirdParty',
310  array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'), // Entry values
311  array('result'=>'tns:result', 'orders'=>'tns:OrdersArray2'), // Exit values
312  $ns,
313  $ns.'#getOrdersForThirdParty',
314  $styledoc,
315  $styleuse,
316  'WS to get all orders of a third party'
317 );
318 
319 $server->register(
320  'createOrder',
321  array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
322  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string'), // Exit values
323  $ns,
324  $ns.'#createOrder',
325  $styledoc,
326  $styleuse,
327  'WS to create an order'
328 );
329 
330 $server->register(
331  'updateOrder',
332  array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
333  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Exit values
334  $ns,
335  $ns.'#updateOrder',
336  $styledoc,
337  $styleuse,
338  'WS to update an order'
339 );
340 
341 $server->register(
342  'validOrder',
343  array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'id_warehouse'=>'xsd:string'), // Entry values
344  array('result'=>'tns:result'), // Exit values
345  $ns,
346  $ns.'#validOrder',
347  $styledoc,
348  $styleuse,
349  'WS to valid an order'
350 );
351 
361 function getOrder($authentication, $id = '', $ref = '', $ref_ext = '')
362 {
363  global $db, $conf;
364 
365  dol_syslog("Function: getOrder login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
366 
367  if ($authentication['entity']) {
368  $conf->entity = $authentication['entity'];
369  }
370 
371  // Init and check authentication
372  $objectresp = array();
373  $errorcode = ''; $errorlabel = '';
374  $error = 0;
375  $socid = 0;
376 
377  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
378 
379  if ($fuser->socid) {
380  $socid = $fuser->socid;
381  }
382 
383  // Check parameters
384  if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
385  $error++;
386  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
387  }
388 
389  if (!$error) {
390  $fuser->getrights();
391 
392  if ($fuser->rights->commande->lire) {
393  $order = new Commande($db);
394  $result = $order->fetch($id, $ref, $ref_ext);
395  if ($result > 0) {
396  // Security for external user
397  if ($socid && $socid != $order->socid) {
398  $error++;
399  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
400  }
401 
402  if (!$error) {
403  $linesresp = array();
404  $i = 0;
405  foreach ($order->lines as $line) {
406  //var_dump($line); exit;
407  $linesresp[] = array(
408  'id'=>$line->rowid,
409  'fk_commande'=>$line->fk_commande,
410  'fk_parent_line'=>$line->fk_parent_line,
411  'desc'=>$line->desc,
412  'qty'=>$line->qty,
413  'price'=>$line->price,
414  'unitprice'=>$line->subprice,
415  'vat_rate'=>$line->tva_tx,
416  'remise'=>$line->remise,
417  'remise_percent'=>$line->remise_percent,
418  'product_id'=>$line->fk_product,
419  'product_type'=>$line->product_type,
420  'total_net'=>$line->total_ht,
421  'total_vat'=>$line->total_tva,
422  'total'=>$line->total_ttc,
423  'date_start'=>$line->date_start,
424  'date_end'=>$line->date_end,
425  'product_ref'=>$line->product_ref,
426  'product_label'=>$line->product_label,
427  'product_desc'=>$line->product_desc
428  );
429  $i++;
430  }
431 
432  // Create order
433  $objectresp = array(
434  'result'=>array('result_code'=>'OK', 'result_label'=>''),
435  'order'=>array(
436  'id' => $order->id,
437  'ref' => $order->ref,
438  'ref_client' => $order->ref_client,
439  'ref_ext' => $order->ref_ext,
440  'thirdparty_id' => $order->socid,
441  'status' => $order->statut,
442 
443  'total_net' => $order->total_ht,
444  'total_vat' => $order->total_tva,
445  'total_localtax1' => $order->total_localtax1,
446  'total_localtax2' => $order->total_localtax2,
447  'total' => $order->total_ttc,
448  'project_id' => $order->fk_project,
449 
450  'date' => $order->date ?dol_print_date($order->date, 'dayrfc') : '',
451  'date_creation' => $order->date_creation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
452  'date_validation' => $order->date_validation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
453  'date_modification' => $order->date_modification ?dol_print_date($order->date_modification, 'dayhourrfc') : '',
454 
455  'remise' => $order->remise,
456  'remise_percent' => $order->remise_percent,
457  'remise_absolue' => $order->remise_absolue,
458 
459  'source' => $order->source,
460  'billed' => $order->billed,
461  'note_private' => $order->note_private,
462  'note_public' => $order->note_public,
463  'cond_reglement_id' => $order->cond_reglement_id,
464  'cond_reglement_code' => $order->cond_reglement_code,
465  'cond_reglement' => $order->cond_reglement,
466  'mode_reglement_id' => $order->mode_reglement_id,
467  'mode_reglement_code' => $order->mode_reglement_code,
468  'mode_reglement' => $order->mode_reglement,
469 
470  'date_livraison' => $order->delivery_date,
471 
472  'demand_reason_id' => $order->demand_reason_id,
473  'demand_reason_code' => $order->demand_reason_code,
474 
475  'lines' => $linesresp
476  ));
477  }
478  } else {
479  $error++;
480  $errorcode = 'NOT_FOUND';
481  $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
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  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
492  }
493 
494  return $objectresp;
495 }
496 
497 
505 function getOrdersForThirdParty($authentication, $idthirdparty)
506 {
507  global $db, $conf;
508 
509  dol_syslog("Function: getOrdersForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
510 
511  if ($authentication['entity']) {
512  $conf->entity = $authentication['entity'];
513  }
514 
515  // Init and check authentication
516  $objectresp = array();
517  $errorcode = ''; $errorlabel = '';
518  $error = 0;
519  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
520 
521  if ($fuser->socid) {
522  $socid = $fuser->socid;
523  }
524 
525  // Check parameters
526  if (!$error && empty($idthirdparty)) {
527  $error++;
528  $errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter id is not provided';
529  }
530 
531  if (!$error) {
532  $linesorders = array();
533 
534  $sql = 'SELECT c.rowid as orderid';
535  $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c';
536  $sql .= " WHERE c.entity = ".$conf->entity;
537  if ($idthirdparty != 'all') {
538  $sql .= " AND c.fk_soc = ".((int) $idthirdparty);
539  }
540 
541 
542  $resql = $db->query($sql);
543  if ($resql) {
544  $num = $db->num_rows($resql);
545  $i = 0;
546  while ($i < $num) {
547  // En attendant remplissage par boucle
548  $obj = $db->fetch_object($resql);
549 
550  $order = new Commande($db);
551  $order->fetch($obj->orderid);
552 
553  // Sécurité pour utilisateur externe
554  if ($socid && ($socid != $order->socid)) {
555  $error++;
556  $errorcode = 'PERMISSION_DENIED';
557  $errorlabel = $order->socid.' User does not have permission for this request';
558  }
559 
560  if (!$error) {
561  // Define lines of invoice
562  $linesresp = array();
563  foreach ($order->lines as $line) {
564  $linesresp[] = array(
565  'id'=>$line->rowid,
566  'type'=>$line->product_type,
567  'fk_commande'=>$line->fk_commande,
568  'fk_parent_line'=>$line->fk_parent_line,
569  'desc'=>$line->desc,
570  'qty'=>$line->qty,
571  'price'=>$line->price,
572  'unitprice'=>$line->subprice,
573  'tva_tx'=>$line->tva_tx,
574  'remise'=>$line->remise,
575  'remise_percent'=>$line->remise_percent,
576  'total_net'=>$line->total_ht,
577  'total_vat'=>$line->total_tva,
578  'total'=>$line->total_ttc,
579  'date_start'=>$line->date_start,
580  'date_end'=>$line->date_end,
581  'product_id'=>$line->fk_product,
582  'product_ref'=>$line->product_ref,
583  'product_label'=>$line->product_label,
584  'product_desc'=>$line->product_desc
585  );
586  }
587 
588  // Now define invoice
589  $linesorders[] = array(
590  'id' => $order->id,
591  'ref' => $order->ref,
592  'ref_client' => $order->ref_client,
593  'ref_ext' => $order->ref_ext,
594  'socid' => $order->socid,
595  'status' => $order->statut,
596 
597  'total_net' => $order->total_ht,
598  'total_vat' => $order->total_tva,
599  'total_localtax1' => $order->total_localtax1,
600  'total_localtax2' => $order->total_localtax2,
601  'total' => $order->total_ttc,
602  'project_id' => $order->fk_project,
603 
604  'date' => $order->date_commande ?dol_print_date($order->date_commande, 'dayrfc') : '',
605 
606  'remise' => $order->remise,
607  'remise_percent' => $order->remise_percent,
608  'remise_absolue' => $order->remise_absolue,
609 
610  'source' => $order->source,
611  'billed' => $order->billed,
612  'note_private' => $order->note_private,
613  'note_public' => $order->note_public,
614  'cond_reglement_id' => $order->cond_reglement_id,
615  'cond_reglement' => $order->cond_reglement,
616  'cond_reglement_doc' => $order->cond_reglement_doc,
617  'cond_reglement_code' => $order->cond_reglement_code,
618  'mode_reglement_id' => $order->mode_reglement_id,
619  'mode_reglement' => $order->mode_reglement,
620  'mode_reglement_code' => $order->mode_reglement_code,
621 
622  'date_livraison' => $order->delivery_date,
623 
624  'demand_reason_id' => $order->demand_reason_id,
625  'demand_reason_code' => $order->demand_reason_code,
626 
627  'lines' => $linesresp
628  );
629  }
630  $i++;
631  }
632 
633  $objectresp = array(
634  'result'=>array('result_code'=>'OK', 'result_label'=>''),
635  'orders'=>$linesorders
636 
637  );
638  } else {
639  $error++;
640  $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
641  }
642  }
643 
644  if ($error) {
645  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
646  }
647 
648  return $objectresp;
649 }
650 
651 
659 function createOrder($authentication, $order)
660 {
661  global $db, $conf, $langs;
662 
663  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
664 
665  $now = dol_now();
666 
667  dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
668 
669  if ($authentication['entity']) {
670  $conf->entity = $authentication['entity'];
671  }
672 
673  // Init and check authentication
674  $objectresp = array();
675  $errorcode = '';
676  $errorlabel = '';
677  $error = 0;
678  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
679 
680  // Check parameters
681 
682 
683  if (!$error) {
684  $newobject = new Commande($db);
685  $newobject->socid = $order['thirdparty_id'];
686  $newobject->type = $order['type'];
687  $newobject->ref_ext = $order['ref_ext'];
688  $newobject->date = dol_stringtotime($order['date'], 'dayrfc');
689  $newobject->date_lim_reglement = dol_stringtotime($order['date_due'], 'dayrfc');
690  $newobject->note_private = $order['note_private'];
691  $newobject->note_public = $order['note_public'];
692  $newobject->statut = Commande::STATUS_DRAFT; // We start with status draft
693  $newobject->billed = $order['billed'];
694  $newobject->fk_project = $order['project_id'];
695  $newobject->cond_reglement_id = $order['cond_reglement_id'];
696  $newobject->demand_reason_id = $order['demand_reason_id'];
697  $newobject->date_creation = $now;
698 
699  $elementtype = 'commande';
700 
701  // Retrieve all extrafield for order
702  // fetch optionals attributes and labels
703  $extrafields = new ExtraFields($db);
704  $extrafields->fetch_name_optionals_label($elementtype, true);
705  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
706  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
707  $key = 'options_'.$key;
708  $newobject->array_options[$key] = $order[$key];
709  }
710  }
711 
712  // Trick because nusoap does not store data with same structure if there is one or several lines
713  $arrayoflines = array();
714  if (isset($order['lines']['line'][0])) {
715  $arrayoflines = $order['lines']['line'];
716  } else {
717  $arrayoflines = $order['lines'];
718  }
719 
720  foreach ($arrayoflines as $key => $line) {
721  // $key can be 'line' or '0','1',...
722  $newline = new OrderLine($db);
723 
724  $newline->type = $line['type'];
725  $newline->desc = $line['desc'];
726  $newline->fk_product = $line['product_id'];
727  $newline->tva_tx = $line['vat_rate'];
728  $newline->qty = $line['qty'];
729  $newline->price = $line['price'];
730  $newline->subprice = $line['unitprice'];
731  $newline->total_ht = $line['total_net'];
732  $newline->total_tva = $line['total_vat'];
733  $newline->total_ttc = $line['total'];
734  $newline->date_start = $line['date_start'];
735  $newline->date_end = $line['date_end'];
736 
737  $elementtype = 'commandedet';
738 
739  // Retrieve all extrafield for lines
740  // fetch optionals attributes and labels
741  $extrafields = new ExtraFields($db);
742  $extrafields->fetch_name_optionals_label($elementtype, true);
743  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
744  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
745  $key = 'options_'.$key;
746  $newline->array_options[$key] = $line[$key];
747  }
748  }
749 
750  $newobject->lines[] = $newline;
751  }
752 
753 
754  $db->begin();
755  dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
756  $result = $newobject->create($fuser);
757  dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
758  if ($result < 0) {
759  dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
760  $error++;
761  }
762 
763  if ($order['status'] == 1) { // We want order to have status validated
764  dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
765  $result = $newobject->valid($fuser);
766  if ($result < 0) {
767  dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
768  $error++;
769  }
770  }
771 
772  if ($result >= 0) {
773  dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
774  $db->commit();
775  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
776  } else {
777  dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
778  $db->rollback();
779  $error++;
780  $errorcode = 'KO';
781  $errorlabel = $newobject->error;
782  }
783  }
784 
785  if ($error) {
786  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
787  }
788 
789  return $objectresp;
790 }
791 
792 
801 function validOrder($authentication, $id = '', $id_warehouse = 0)
802 {
803  global $db, $conf, $langs;
804 
805  dol_syslog("Function: validOrder login=".$authentication['login']." id=".$id." id_warehouse=".$id_warehouse);
806 
807  // Init and check authentication
808  $objectresp = array();
809  $errorcode = '';
810  $errorlabel = '';
811  $error = 0;
812  if ($authentication['entity']) {
813  $conf->entity = $authentication['entity'];
814  }
815  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
816 
817  if (!$error) {
818  $fuser->getrights();
819 
820  if ($fuser->rights->commande->lire) {
821  $order = new Commande($db);
822  $result = $order->fetch($id);
823 
824  $order->fetch_thirdparty();
825  $db->begin();
826  if ($result > 0) {
827  $result = $order->valid($fuser, $id_warehouse);
828 
829  if ($result >= 0) {
830  // Define output language
831  $outputlangs = $langs;
832  $order->generateDocument($order->model_pdf, $outputlangs);
833  } else {
834  $db->rollback();
835  $error++;
836  $errorcode = 'KO';
837  $errorlabel = $order->error;
838  }
839  } else {
840  $db->rollback();
841  $error++;
842  $errorcode = 'KO';
843  $errorlabel = $order->error;
844  }
845  } else {
846  $db->rollback();
847  $error++;
848  $errorcode = 'KO';
849  $errorlabel = $order->error;
850  }
851  }
852 
853  if ($error) {
854  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
855  } else {
856  $db->commit();
857  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
858  }
859 
860  return $objectresp;
861 }
862 
870 function updateOrder($authentication, $order)
871 {
872  global $db, $conf, $langs;
873 
874  dol_syslog("Function: updateOrder login=".$authentication['login']);
875 
876  if ($authentication['entity']) {
877  $conf->entity = $authentication['entity'];
878  }
879 
880  // Init and check authentication
881  $objectresp = array();
882  $errorcode = ''; $errorlabel = '';
883  $error = 0;
884  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
885  // Check parameters
886  if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext'])) {
887  $error++; $errorcode = 'KO'; $errorlabel = "Order id or ref or ref_ext is mandatory.";
888  }
889 
890  if (!$error) {
891  $objectfound = false;
892 
893  include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
894 
895  $object = new Commande($db);
896  $result = $object->fetch($order['id'], (empty($order['id']) ? $order['ref'] : ''), (empty($order['id']) && empty($order['ref']) ? $order['ref_ext'] : ''));
897 
898  if (!empty($object->id)) {
899  $objectfound = true;
900 
901  $db->begin();
902 
903  if (isset($order['status'])) {
904  if ($order['status'] == -1) {
905  $result = $object->cancel($fuser);
906  }
907  if ($order['status'] == 1) {
908  $result = $object->valid($fuser);
909  if ($result >= 0) {
910  // Define output language
911  $outputlangs = $langs;
912  $object->generateDocument($order->model_pdf, $outputlangs);
913  }
914  }
915  if ($order['status'] == 0) {
916  $result = $object->set_reopen($fuser);
917  }
918  if ($order['status'] == 3) {
919  $result = $object->cloture($fuser);
920  }
921  }
922 
923  if (isset($order['billed'])) {
924  if ($order['billed']) {
925  $result = $object->classifyBilled($fuser);
926  }
927  if (!$order['billed']) {
928  $result = $object->classifyUnBilled($fuser);
929  }
930  }
931 
932  $elementtype = 'commande';
933 
934  //Retrieve all extrafield for object
935  // fetch optionals attributes and labels
936  $extrafields = new ExtraFields($db);
937  $extrafields->fetch_name_optionals_label($elementtype, true);
938  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
939  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
940  $key = 'options_'.$key;
941  if (isset($order[$key])) {
942  $result = $object->setValueFrom($key, $order[$key], 'commande_extrafields');
943  }
944  }
945  }
946 
947  if ($result <= 0) {
948  $error++;
949  }
950  }
951 
952  if ((!$error) && ($objectfound)) {
953  $db->commit();
954  $objectresp = array(
955  'result'=>array('result_code'=>'OK', 'result_label'=>''),
956  'id'=>$object->id,
957  'ref'=>$object->ref,
958  'ref_ext'=>$object->ref_ext
959  );
960  } elseif ($objectfound) {
961  $db->rollback();
962  $error++;
963  $errorcode = 'KO';
964  $errorlabel = $object->error;
965  } else {
966  $error++;
967  $errorcode = 'NOT_FOUND';
968  $errorlabel = 'Order id='.$order['id'].' ref='.$order['ref'].' ref_ext='.$order['ref_ext'].' cannot be found';
969  }
970  }
971 
972  if ($error) {
973  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
974  }
975 
976  return $objectresp;
977 }
978 
979 
980 // Return the results.
981 $server->service(file_get_contents("php://input"));
Class to manage customers orders.
const STATUS_DRAFT
Draft status.
Class to manage standard extra fields.
Class to manage order lines.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
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:407
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_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
createOrder($authentication, $order)
Create order.
validOrder($authentication, $id='', $id_warehouse=0)
Valid an order.
getOrder($authentication, $id='', $ref='', $ref_ext='')
Get order from id, ref or ref_ext.
updateOrder($authentication, $order)
Update an order.
getOrdersForThirdParty($authentication, $idthirdparty)
Get list of orders for third party.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition: ws.lib.php:35