dolibarr 21.0.0-beta
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 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 dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
689
690 if ($authentication['entity']) {
691 $conf->entity = $authentication['entity'];
692 }
693
694 // Init and check authentication
695 $objectresp = array();
696 $errorcode = '';
697 $errorlabel = '';
698 $error = 0;
699 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
700 $newobject = null;
701
702 // Check parameters
703
704
705 if (!$error) {
706 $newobject = new Commande($db);
707 $newobject->socid = $order['thirdparty_id'];
708 $newobject->type = $order['type'];
709 $newobject->ref_ext = $order['ref_ext'];
710 $newobject->date = dol_stringtotime($order['date'], 'dayrfc');
711 $newobject->date_lim_reglement = dol_stringtotime($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->billed = (int) $order['billed'];
716 $newobject->fk_project = (int) $order['project_id'];
717 $newobject->cond_reglement_id = (int) $order['cond_reglement_id'];
718 $newobject->demand_reason_id = (int) $order['demand_reason_id'];
719 $newobject->date_creation = $now;
720
721 $elementtype = 'commande';
722
723 // Retrieve all extrafield for order
724 // fetch optionals attributes and labels
725 $extrafields = new ExtraFields($db);
726 $extrafields->fetch_name_optionals_label($elementtype, true);
727 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
728 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
729 $key = 'options_'.$key;
730 $newobject->array_options[$key] = $order[$key];
731 }
732 }
733
734 // Trick because nusoap does not store data with same structure if there is one or several lines
735 $arrayoflines = array();
736 if (isset($order['lines']['line'][0])) {
737 $arrayoflines = $order['lines']['line']; // @phan-suppress-current-line PhanTypeInvalidDimOffset
738 } else {
739 $arrayoflines = $order['lines'];
740 }
741 if (!is_array($arrayoflines)) {
742 $arrayoflines = array();
743 }
744
745 foreach ($arrayoflines as $key => $line) {
746 // $key can be 'line' or '0','1',...
747 $newline = new OrderLine($db);
748
749 $newline->type = $line['type'];
750 $newline->desc = $line['desc'];
751 $newline->fk_product = $line['product_id'];
752 $newline->tva_tx = $line['vat_rate'];
753 $newline->qty = $line['qty'];
754 $newline->price = $line['price'];
755 $newline->subprice = $line['unitprice'];
756 $newline->total_ht = $line['total_net'];
757 $newline->total_tva = $line['total_vat'];
758 $newline->total_ttc = $line['total'];
759 $newline->date_start = $line['date_start'];
760 $newline->date_end = $line['date_end'];
761
762 $elementtype = 'commandedet';
763
764 // Retrieve all extrafield for lines
765 // fetch optionals attributes and labels
766 $extrafields = new ExtraFields($db);
767 $extrafields->fetch_name_optionals_label($elementtype, true);
768 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
769 foreach ($extrafields->attributes[$elementtype]['label'] as $tmpkey => $tmplabel) {
770 $tmpkey = 'options_'.$tmpkey;
771 $newline->array_options[$tmpkey] = $line[$tmpkey];
772 }
773 }
774
775 $newobject->lines[] = $newline;
776 }
777
778
779 $db->begin();
780 dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
781 $result = $newobject->create($fuser);
782 dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
783 if ($result < 0) {
784 dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
785 $error++;
786 }
787
788 if ($order['status'] == 1) { // We want order to have status validated
789 dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
790 $result = $newobject->valid($fuser);
791 if ($result < 0) {
792 dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
793 $error++;
794 }
795 }
796
797 if ($result >= 0) {
798 dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
799 $db->commit();
800 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref);
801 } else {
802 dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
803 $db->rollback();
804 $error++;
805 $errorcode = 'KO';
806 $errorlabel = $newobject->error;
807 }
808 }
809
810 if ($error) {
811 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
812 }
813
814 return $objectresp;
815}
816
817
826function validOrder($authentication, $id = 0, $id_warehouse = 0)
827{
828 global $db, $conf, $langs;
829
830 dol_syslog("Function: validOrder login=".$authentication['login']." id=".$id." id_warehouse=".$id_warehouse);
831
832 // Init and check authentication
833 $objectresp = array();
834 $errorcode = '';
835 $errorlabel = '';
836 $error = 0;
837 if ($authentication['entity']) {
838 $conf->entity = $authentication['entity'];
839 }
840 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
841
842 if (!$error) {
843 $fuser->loadRights();
844
845 if ($fuser->hasRight('commande', 'lire')) {
846 $order = new Commande($db);
847 $result = $order->fetch($id);
848
849 $order->fetch_thirdparty();
850 $db->begin();
851 if ($result > 0) {
852 $result = $order->valid($fuser, $id_warehouse);
853
854 if ($result >= 0) {
855 // Define output language
856 $outputlangs = $langs;
857 $order->generateDocument($order->model_pdf, $outputlangs);
858 } else {
859 $db->rollback();
860 $error++;
861 $errorcode = 'KO';
862 $errorlabel = $order->error;
863 }
864 } else {
865 $db->rollback();
866 $error++;
867 $errorcode = 'KO';
868 $errorlabel = $order->error;
869 }
870 } else {
871 $db->rollback();
872 $error++;
873 $errorcode = 'KO';
874 $errorlabel = 'Bad permission';
875 }
876 }
877
878 if ($error) {
879 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
880 } else {
881 $db->commit();
882 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''));
883 }
884
885 return $objectresp;
886}
887
895function updateOrder($authentication, $order)
896{
897 global $db, $conf, $langs;
898
899 dol_syslog("Function: updateOrder login=".$authentication['login']);
900
901 if ($authentication['entity']) {
902 $conf->entity = $authentication['entity'];
903 }
904
905 // Init and check authentication
906 $objectresp = array();
907 $errorcode = '';
908 $errorlabel = '';
909 $error = 0;
910 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
911 // Check parameters
912 if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext'])) {
913 $error++;
914 $errorcode = 'KO';
915 $errorlabel = "Order id or ref or ref_ext is mandatory.";
916 }
917
918 if (!$error) {
919 $objectfound = false;
920
921 include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
922
923 $object = new Commande($db);
924 $result = $object->fetch($order['id'], (empty($order['id']) ? $order['ref'] : ''), (empty($order['id']) && empty($order['ref']) ? $order['ref_ext'] : ''));
925
926 if (!empty($object->id)) {
927 $objectfound = true;
928
929 $db->begin();
930
931 if (isset($order['status'])) {
932 if ($order['status'] == -1) {
933 $result = $object->cancel($fuser);
934 }
935 if ($order['status'] == 1) {
936 $result = $object->valid($fuser);
937 if ($result >= 0) {
938 // Define output language
939 $outputlangs = $langs;
940 $object->generateDocument($order->model_pdf, $outputlangs);
941 }
942 }
943 if ($order['status'] == 0) {
944 $result = $object->set_reopen($fuser);
945 }
946 if ($order['status'] == 3) {
947 $result = $object->cloture($fuser);
948 }
949 }
950
951 if (isset($order['billed'])) {
952 if ($order['billed']) {
953 $result = $object->classifyBilled($fuser);
954 }
955 if (!$order['billed']) {
956 $result = $object->classifyUnBilled($fuser);
957 }
958 }
959
960 $elementtype = 'commande';
961
962 //Retrieve all extrafield for object
963 // fetch optionals attributes and labels
964 $extrafields = new ExtraFields($db);
965 $extrafields->fetch_name_optionals_label($elementtype, true);
966 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
967 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
968 $key = 'options_'.$key;
969 if (isset($order[$key])) {
970 $result = $object->setValueFrom($key, $order[$key], 'commande_extrafields');
971 }
972 }
973 }
974
975 if ($result <= 0) {
976 $error++;
977 }
978 }
979
980 if ((!$error) && ($objectfound)) {
981 $db->commit();
982 $objectresp = array(
983 'result' => array('result_code' => 'OK', 'result_label' => ''),
984 'id' => $object->id,
985 'ref' => $object->ref,
986 'ref_ext' => $object->ref_ext
987 );
988 } elseif ($objectfound) {
989 $db->rollback();
990 $error++;
991 $errorcode = 'KO';
992 $errorlabel = $object->error;
993 } else {
994 $error++;
995 $errorcode = 'NOT_FOUND';
996 $errorlabel = 'Order id='.$order['id'].' ref='.$order['ref'].' ref_ext='.$order['ref_ext'].' cannot be found';
997 }
998 }
999
1000 if ($error) {
1001 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
1002 }
1003
1004 return $objectresp;
1005}
1006
1007
1008// Return the results.
1009$server->service(file_get_contents("php://input"));
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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