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