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