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