dolibarr 18.0.6
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 (empty($conf->global->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 'remise' => array('name'=>'remise', 'type'=>'xsd:string'),
218 'remise_percent' => array('name'=>'remise_percent', 'type'=>'xsd:string'),
219 'remise_absolue' => array('name'=>'remise_absolue', 'type'=>'xsd:string'),
220 'source' => array('name'=>'source', 'type'=>'xsd:string'),
221 'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
222 'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
223 'project_id' => array('name'=>'project_id', 'type'=>'xsd:string'),
224
225 'mode_reglement_id' => array('name'=>'mode_reglement_id', 'type'=>'xsd:string'),
226 'mode_reglement_code' => array('name'=>'mode_reglement_code', 'type'=>'xsd:string'),
227 'mode_reglement' => array('name'=>'mode_reglement', 'type'=>'xsd:string'),
228 'cond_reglement_id' => array('name'=>'cond_reglement_id', 'type'=>'xsd:string'),
229 'cond_reglement_code' => array('name'=>'cond_reglement_code', 'type'=>'xsd:string'),
230 'cond_reglement' => array('name'=>'cond_reglement', 'type'=>'xsd:string'),
231 'cond_reglement_doc' => array('name'=>'cond_reglement_doc', 'type'=>'xsd:string'),
232
233 'date_livraison' => array('name'=>'date_livraison', 'type'=>'xsd:date'),
234 'demand_reason_id' => array('name'=>'demand_reason_id', 'type'=>'xsd:string'),
235
236 'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray2')
237);
238
239$elementtype = 'commande';
240
241//Retrieve all extrafield for thirdsparty
242// fetch optionals attributes and labels
243$extrafields = new ExtraFields($db);
244$extrafields->fetch_name_optionals_label($elementtype, true);
245$extrafield_array = null;
246if (is_array($extrafields) && count($extrafields) > 0) {
247 $extrafield_array = array();
248}
249if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
250 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
251 //$value=$object->array_options["options_".$key];
252 $type = $extrafields->attributes[$elementtype]['type'][$key];
253 if ($type == 'date' || $type == 'datetime') {
254 $type = 'xsd:dateTime';
255 } else {
256 $type = 'xsd:string';
257 }
258 $extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
259 }
260}
261if (is_array($extrafield_array)) {
262 $order_fields = array_merge($order_fields, $extrafield_array);
263}
264
265$server->wsdl->addComplexType(
266 'order',
267 'complexType',
268 'struct',
269 'all',
270 '',
271 $order_fields
272);
273
274/*
275$server->wsdl->addComplexType(
276 'OrdersArray',
277 'complexType',
278 'array',
279 '',
280 'SOAP-ENC:Array',
281 array(),
282 array(
283 array(
284 'ref'=>'SOAP-ENC:arrayType',
285 'wsdl:arrayType'=>'tns:order[]'
286 )
287 ),
288 'tns:order'
289);*/
290$server->wsdl->addComplexType(
291 'OrdersArray2',
292 'complexType',
293 'array',
294 'sequence',
295 '',
296 array(
297 'order' => array(
298 'name' => 'order',
299 'type' => 'tns:order',
300 'minOccurs' => '0',
301 'maxOccurs' => 'unbounded'
302 )
303 )
304);
305
306
307
308// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
309// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
310// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
311$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
312$styleuse = 'encoded'; // encoded/literal/literal wrapped
313// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
314
315// Register WSDL
316$server->register(
317 'getOrder',
318 array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Entry values
319 array('result'=>'tns:result', 'order'=>'tns:order'), // Exit values
320 $ns,
321 $ns.'#getOrder',
322 $styledoc,
323 $styleuse,
324 'WS to get a particular invoice'
325);
326
327$server->register(
328 'getOrdersForThirdParty',
329 array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'), // Entry values
330 array('result'=>'tns:result', 'orders'=>'tns:OrdersArray2'), // Exit values
331 $ns,
332 $ns.'#getOrdersForThirdParty',
333 $styledoc,
334 $styleuse,
335 'WS to get all orders of a third party'
336);
337
338$server->register(
339 'createOrder',
340 array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
341 array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string'), // Exit values
342 $ns,
343 $ns.'#createOrder',
344 $styledoc,
345 $styleuse,
346 'WS to create an order'
347);
348
349$server->register(
350 'updateOrder',
351 array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
352 array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Exit values
353 $ns,
354 $ns.'#updateOrder',
355 $styledoc,
356 $styleuse,
357 'WS to update an order'
358);
359
360$server->register(
361 'validOrder',
362 array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'id_warehouse'=>'xsd:string'), // Entry values
363 array('result'=>'tns:result'), // Exit values
364 $ns,
365 $ns.'#validOrder',
366 $styledoc,
367 $styleuse,
368 'WS to valid an order'
369);
370
380function getOrder($authentication, $id = '', $ref = '', $ref_ext = '')
381{
382 global $db, $conf;
383
384 dol_syslog("Function: getOrder login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
385
386 if ($authentication['entity']) {
387 $conf->entity = $authentication['entity'];
388 }
389
390 // Init and check authentication
391 $objectresp = array();
392 $errorcode = ''; $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'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
406 }
407
408 if (!$error) {
409 $fuser->getrights();
410
411 if ($fuser->rights->commande->lire) {
412 $order = new Commande($db);
413 $result = $order->fetch($id, $ref, $ref_ext);
414 if ($result > 0) {
415 // Security for external user
416 if ($socid && $socid != $order->socid) {
417 $error++;
418 $errorcode = 'PERMISSION_DENIED'; $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 'remise' => $order->remise,
475 'remise_percent' => $order->remise_percent,
476 'remise_absolue' => $order->remise_absolue,
477
478 'source' => $order->source,
479 'billed' => $order->billed,
480 'note_private' => $order->note_private,
481 'note_public' => $order->note_public,
482 'cond_reglement_id' => $order->cond_reglement_id,
483 'cond_reglement_code' => $order->cond_reglement_code,
484 'cond_reglement' => $order->cond_reglement,
485 'mode_reglement_id' => $order->mode_reglement_id,
486 'mode_reglement_code' => $order->mode_reglement_code,
487 'mode_reglement' => $order->mode_reglement,
488
489 'date_livraison' => $order->delivery_date,
490
491 'demand_reason_id' => $order->demand_reason_id,
492 'demand_reason_code' => $order->demand_reason_code,
493
494 'lines' => $linesresp
495 ));
496 }
497 } else {
498 $error++;
499 $errorcode = 'NOT_FOUND';
500 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
501 }
502 } else {
503 $error++;
504 $errorcode = 'PERMISSION_DENIED';
505 $errorlabel = 'User does not have permission for this request';
506 }
507 }
508
509 if ($error) {
510 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
511 }
512
513 return $objectresp;
514}
515
516
524function getOrdersForThirdParty($authentication, $idthirdparty)
525{
526 global $db, $conf;
527
528 dol_syslog("Function: getOrdersForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
529
530 if ($authentication['entity']) {
531 $conf->entity = $authentication['entity'];
532 }
533
534 // Init and check authentication
535 $objectresp = array();
536 $errorcode = ''; $errorlabel = '';
537 $error = 0;
538 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
539
540 if ($fuser->socid) {
541 $socid = $fuser->socid;
542 }
543
544 // Check parameters
545 if (!$error && empty($idthirdparty)) {
546 $error++;
547 $errorcode = 'BAD_PARAMETERS'; $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 'remise' => $order->remise,
626 'remise_percent' => $order->remise_percent,
627 'remise_absolue' => $order->remise_absolue,
628
629 'source' => $order->source,
630 'billed' => $order->billed,
631 'note_private' => $order->note_private,
632 'note_public' => $order->note_public,
633 'cond_reglement_id' => $order->cond_reglement_id,
634 'cond_reglement' => $order->cond_reglement,
635 'cond_reglement_doc' => $order->cond_reglement_doc,
636 'cond_reglement_code' => $order->cond_reglement_code,
637 'mode_reglement_id' => $order->mode_reglement_id,
638 'mode_reglement' => $order->mode_reglement,
639 'mode_reglement_code' => $order->mode_reglement_code,
640
641 'date_livraison' => $order->delivery_date,
642
643 'demand_reason_id' => $order->demand_reason_id,
644 'demand_reason_code' => $order->demand_reason_code,
645
646 'lines' => $linesresp
647 );
648 }
649 $i++;
650 }
651
652 $objectresp = array(
653 'result'=>array('result_code'=>'OK', 'result_label'=>''),
654 'orders'=>$linesorders
655
656 );
657 } else {
658 $error++;
659 $errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
660 }
661 }
662
663 if ($error) {
664 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
665 }
666
667 return $objectresp;
668}
669
670
678function createOrder($authentication, $order)
679{
680 global $db, $conf, $langs;
681
682 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
683
684 $now = dol_now();
685
686 dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
687
688 if ($authentication['entity']) {
689 $conf->entity = $authentication['entity'];
690 }
691
692 // Init and check authentication
693 $objectresp = array();
694 $errorcode = '';
695 $errorlabel = '';
696 $error = 0;
697 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
698
699 // Check parameters
700
701
702 if (!$error) {
703 $newobject = new Commande($db);
704 $newobject->socid = $order['thirdparty_id'];
705 $newobject->type = $order['type'];
706 $newobject->ref_ext = $order['ref_ext'];
707 $newobject->date = dol_stringtotime($order['date'], 'dayrfc');
708 $newobject->date_lim_reglement = dol_stringtotime($order['date_due'], 'dayrfc');
709 $newobject->note_private = $order['note_private'];
710 $newobject->note_public = $order['note_public'];
711 $newobject->statut = Commande::STATUS_DRAFT; // We start with status draft
712 $newobject->billed = $order['billed'];
713 $newobject->fk_project = $order['project_id'];
714 $newobject->cond_reglement_id = $order['cond_reglement_id'];
715 $newobject->demand_reason_id = $order['demand_reason_id'];
716 $newobject->date_creation = $now;
717
718 $elementtype = 'commande';
719
720 // Retrieve all extrafield for order
721 // fetch optionals attributes and labels
722 $extrafields = new ExtraFields($db);
723 $extrafields->fetch_name_optionals_label($elementtype, true);
724 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
725 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
726 $key = 'options_'.$key;
727 $newobject->array_options[$key] = $order[$key];
728 }
729 }
730
731 // Trick because nusoap does not store data with same structure if there is one or several lines
732 $arrayoflines = array();
733 if (isset($order['lines']['line'][0])) {
734 $arrayoflines = $order['lines']['line'];
735 } else {
736 $arrayoflines = $order['lines'];
737 }
738
739 foreach ($arrayoflines as $key => $line) {
740 // $key can be 'line' or '0','1',...
741 $newline = new OrderLine($db);
742
743 $newline->type = $line['type'];
744 $newline->desc = $line['desc'];
745 $newline->fk_product = $line['product_id'];
746 $newline->tva_tx = $line['vat_rate'];
747 $newline->qty = $line['qty'];
748 $newline->price = $line['price'];
749 $newline->subprice = $line['unitprice'];
750 $newline->total_ht = $line['total_net'];
751 $newline->total_tva = $line['total_vat'];
752 $newline->total_ttc = $line['total'];
753 $newline->date_start = $line['date_start'];
754 $newline->date_end = $line['date_end'];
755
756 $elementtype = 'commandedet';
757
758 // Retrieve all extrafield for lines
759 // fetch optionals attributes and labels
760 $extrafields = new ExtraFields($db);
761 $extrafields->fetch_name_optionals_label($elementtype, true);
762 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
763 foreach ($extrafields->attributes[$elementtype]['label'] as $tmpkey => $tmplabel) {
764 $tmpkey = 'options_'.$tmpkey;
765 $newline->array_options[$tmpkey] = $line[$tmpkey];
766 }
767 }
768
769 $newobject->lines[] = $newline;
770 }
771
772
773 $db->begin();
774 dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
775 $result = $newobject->create($fuser);
776 dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
777 if ($result < 0) {
778 dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
779 $error++;
780 }
781
782 if ($order['status'] == 1) { // We want order to have status validated
783 dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
784 $result = $newobject->valid($fuser);
785 if ($result < 0) {
786 dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
787 $error++;
788 }
789 }
790
791 if ($result >= 0) {
792 dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
793 $db->commit();
794 $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
795 } else {
796 dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
797 $db->rollback();
798 $error++;
799 $errorcode = 'KO';
800 $errorlabel = $newobject->error;
801 }
802 }
803
804 if ($error) {
805 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
806 }
807
808 return $objectresp;
809}
810
811
820function validOrder($authentication, $id = '', $id_warehouse = 0)
821{
822 global $db, $conf, $langs;
823
824 dol_syslog("Function: validOrder login=".$authentication['login']." id=".$id." id_warehouse=".$id_warehouse);
825
826 // Init and check authentication
827 $objectresp = array();
828 $errorcode = '';
829 $errorlabel = '';
830 $error = 0;
831 if ($authentication['entity']) {
832 $conf->entity = $authentication['entity'];
833 }
834 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
835
836 if (!$error) {
837 $fuser->getrights();
838
839 if ($fuser->rights->commande->lire) {
840 $order = new Commande($db);
841 $result = $order->fetch($id);
842
843 $order->fetch_thirdparty();
844 $db->begin();
845 if ($result > 0) {
846 $result = $order->valid($fuser, $id_warehouse);
847
848 if ($result >= 0) {
849 // Define output language
850 $outputlangs = $langs;
851 $order->generateDocument($order->model_pdf, $outputlangs);
852 } else {
853 $db->rollback();
854 $error++;
855 $errorcode = 'KO';
856 $errorlabel = $order->error;
857 }
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 }
871
872 if ($error) {
873 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
874 } else {
875 $db->commit();
876 $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
877 }
878
879 return $objectresp;
880}
881
889function updateOrder($authentication, $order)
890{
891 global $db, $conf, $langs;
892
893 dol_syslog("Function: updateOrder login=".$authentication['login']);
894
895 if ($authentication['entity']) {
896 $conf->entity = $authentication['entity'];
897 }
898
899 // Init and check authentication
900 $objectresp = array();
901 $errorcode = ''; $errorlabel = '';
902 $error = 0;
903 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
904 // Check parameters
905 if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext'])) {
906 $error++; $errorcode = 'KO'; $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"));
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:409
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
createOrder($authentication, $order)
Create order.
validOrder($authentication, $id='', $id_warehouse=0)
Valid an order.
getOrder($authentication, $id='', $ref='', $ref_ext='')
Get order from id, ref or ref_ext.
updateOrder($authentication, $order)
Update 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