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