Passed
Branch develop (f6c1a1)
by
unknown
28:54
created

updateOrder()   F

Complexity

Conditions 28
Paths 12032

Size

Total Lines 109
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 59
nc 12032
nop 2
dl 0
loc 109
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2006-2016	Laurent Destailleur	<[email protected]>
3
 * Copyright (C) 2012		JF FERRY			<[email protected]>
4
 * Copyright (C) 2012		Regis Houssin		<[email protected]>
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
20
/**
21
 *       \file       htdocs/webservices/server_order.php
22
 *       \brief      File that is entry point to call Dolibarr WebServices
23
 */
24
25
if (!defined("NOCSRFCHECK")) define("NOCSRFCHECK", '1');
26
27
require '../master.inc.php';
28
require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
29
require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
30
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
31
require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";
32
33
34
dol_syslog("Call Dolibarr webservices interfaces");
35
36
$langs->load("main");
37
38
// Enable and test if module web services is enabled
39
if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
40
{
41
	$langs->load("admin");
42
	dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
43
	print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
44
	print $langs->trans("ToActivateModule");
45
	exit;
46
}
47
48
// Create the soap Object
49
$server = new nusoap_server();
50
$server->soap_defencoding = 'UTF-8';
51
$server->decode_utf8 = false;
52
$ns = 'http://www.dolibarr.org/ns/';
53
$server->configureWSDL('WebServicesDolibarrOrder', $ns);
54
$server->wsdl->schemaTargetNamespace = $ns;
0 ignored issues
show
Bug introduced by
The property schemaTargetNamespace does not seem to exist on wsdl.
Loading history...
55
56
57
// Define WSDL Authentication object
58
$server->wsdl->addComplexType(
59
		'authentication',
60
		'complexType',
61
		'struct',
62
		'all',
63
		'',
64
		array(
65
				'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
66
				'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
67
				'login' => array('name'=>'login', 'type'=>'xsd:string'),
68
				'password' => array('name'=>'password', 'type'=>'xsd:string'),
69
				'entity' => array('name'=>'entity', 'type'=>'xsd:string')
70
		)
71
);
72
// Define WSDL Return object
73
$server->wsdl->addComplexType(
74
		'result',
75
		'complexType',
76
		'struct',
77
		'all',
78
		'',
79
		array(
80
				'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
81
				'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
82
		)
83
);
84
85
$line_fields = array(
86
	'id' => array('name'=>'id', 'type'=>'xsd:string'),
87
	'type' => array('name'=>'type', 'type'=>'xsd:int'),
88
	'fk_commande' => array('name'=>'fk_commande', 'type'=>'xsd:int'),
89
	'fk_parent_line' => array('name'=>'fk_parent_line', 'type'=>'xsd:int'),
90
	'desc' => array('name'=>'desc', 'type'=>'xsd:string'),
91
	'qty' => array('name'=>'qty', 'type'=>'xsd:double'),
92
	'price' => array('name'=>'price', 'type'=>'xsd:double'),
93
	'unitprice' => array('name'=>'unitprice', 'type'=>'xsd:double'),
94
	'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:double'),
95
96
	'remise' => array('name'=>'remise', 'type'=>'xsd:double'),
97
	'remise_percent' => array('name'=>'remise_percent', 'type'=>'xsd:double'),
98
99
	'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
100
	'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
101
	'total' => array('name'=>'total', 'type'=>'xsd:double'),
102
103
	'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
104
	'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
105
106
	// From product
107
	'product_id' => array('name'=>'product_id', 'type'=>'xsd:int'),
108
	'product_ref' => array('name'=>'product_ref', 'type'=>'xsd:string'),
109
	'product_label' => array('name'=>'product_label', 'type'=>'xsd:string'),
110
	'product_desc' => array('name'=>'product_desc', 'type'=>'xsd:string')
111
);
112
113
$elementtype = 'commandedet';
114
115
//Retreive all extrafield for thirdsparty
116
// fetch optionals attributes and labels
117
$extrafields = new ExtraFields($db);
118
$extrafields->fetch_name_optionals_label($elementtype, true);
119
$extrafield_line_array = null;
120
if (is_array($extrafields) && count($extrafields) > 0) {
0 ignored issues
show
introduced by
The condition is_array($extrafields) is always false.
Loading history...
121
	$extrafield_line_array = array();
122
}
123
if (is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
124
{
125
	foreach ($extrafields->attributes[$elementtype]['label'] as $key=>$label)
126
	{
127
		//$value=$object->array_options["options_".$key];
128
		$type = $extrafields->attributes[$elementtype]['type'][$key];
129
		if ($type == 'date' || $type == 'datetime') {$type = 'xsd:dateTime'; }
130
		else {$type = 'xsd:string'; }
131
		$extrafield_line_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
132
	}
133
}
134
if (is_array($extrafield_line_array)) $line_fields = array_merge($line_fields, $extrafield_line_array);
0 ignored issues
show
introduced by
The condition is_array($extrafield_line_array) is always false.
Loading history...
135
136
// Define other specific objects
137
$server->wsdl->addComplexType(
138
		'line',
139
		'complexType',
140
		'struct',
141
		'all',
142
		'',
143
		$line_fields
144
);
145
146
/*$server->wsdl->addComplexType(
147
		'LinesArray',
148
		'complexType',
149
		'array',
150
		'',
151
		'SOAP-ENC:Array',
152
		array(),
153
		array(
154
				array(
155
						'ref'=>'SOAP-ENC:arrayType',
156
						'wsdl:arrayType'=>'tns:line[]'
157
				)
158
		),
159
		'tns:line'
160
);*/
161
$server->wsdl->addComplexType(
162
		'LinesArray2',
163
		'complexType',
164
		'array',
165
		'sequence',
166
		'',
167
		array(
168
				'line' => array(
169
						'name' => 'line',
170
						'type' => 'tns:line',
171
						'minOccurs' => '0',
172
						'maxOccurs' => 'unbounded'
173
				)
174
		)
175
);
176
177
$order_fields = array(
178
	'id' => array('name'=>'id', 'type'=>'xsd:string'),
179
	'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
180
	'ref_client' => array('name'=>'ref_client', 'type'=>'xsd:string'),
181
	'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
182
	'ref_int' => array('name'=>'ref_int', 'type'=>'xsd:string'),
183
	'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
184
	'status' => array('name'=>'status', 'type'=>'xsd:int'),
185
	'billed' => array('name'=>'billed', 'type'=>'xsd:string'),
186
	'total_net' => array('name'=>'total_net', 'type'=>'xsd:double'),
187
	'total_vat' => array('name'=>'total_vat', 'type'=>'xsd:double'),
188
	'total_localtax1' => array('name'=>'total_localtax1', 'type'=>'xsd:double'),
189
	'total_localtax2' => array('name'=>'total_localtax2', 'type'=>'xsd:double'),
190
	'total' => array('name'=>'total', 'type'=>'xsd:double'),
191
	'date' => array('name'=>'date', 'type'=>'xsd:date'),
192
	'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
193
	'date_validation' => array('name'=>'date_validation', 'type'=>'xsd:dateTime'),
194
	'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
195
	'remise' => array('name'=>'remise', 'type'=>'xsd:string'),
196
	'remise_percent' => array('name'=>'remise_percent', 'type'=>'xsd:string'),
197
	'remise_absolue' => array('name'=>'remise_absolue', 'type'=>'xsd:string'),
198
	'source' => array('name'=>'source', 'type'=>'xsd:string'),
199
	'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
200
	'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
201
	'project_id' => array('name'=>'project_id', 'type'=>'xsd:string'),
202
203
	'mode_reglement_id' => array('name'=>'mode_reglement_id', 'type'=>'xsd:string'),
204
	'mode_reglement_code' => array('name'=>'mode_reglement_code', 'type'=>'xsd:string'),
205
	'mode_reglement' => array('name'=>'mode_reglement', 'type'=>'xsd:string'),
206
	'cond_reglement_id' => array('name'=>'cond_reglement_id', 'type'=>'xsd:string'),
207
	'cond_reglement_code' => array('name'=>'cond_reglement_code', 'type'=>'xsd:string'),
208
	'cond_reglement' => array('name'=>'cond_reglement', 'type'=>'xsd:string'),
209
	'cond_reglement_doc' => array('name'=>'cond_reglement_doc', 'type'=>'xsd:string'),
210
211
	'date_livraison' => array('name'=>'date_livraison', 'type'=>'xsd:date'),
212
	'demand_reason_id' => array('name'=>'demand_reason_id', 'type'=>'xsd:string'),
213
214
	'lines' => array('name'=>'lines', 'type'=>'tns:LinesArray2')
215
);
216
217
$elementtype = 'commande';
218
219
//Retreive all extrafield for thirdsparty
220
// fetch optionals attributes and labels
221
$extrafields = new ExtraFields($db);
222
$extrafields->fetch_name_optionals_label($elementtype, true);
223
$extrafield_array = null;
224
if (is_array($extrafields) && count($extrafields) > 0) {
0 ignored issues
show
introduced by
The condition is_array($extrafields) is always false.
Loading history...
225
	$extrafield_array = array();
226
}
227
if (is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
228
{
229
	foreach ($extrafields->attributes[$elementtype]['label'] as $key=>$label)
230
	{
231
		//$value=$object->array_options["options_".$key];
232
		$type = $extrafields->attributes[$elementtype]['type'][$key];
233
		if ($type == 'date' || $type == 'datetime') {$type = 'xsd:dateTime'; }
234
		else {$type = 'xsd:string'; }
235
		$extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
236
	}
237
}
238
if (is_array($extrafield_array)) $order_fields = array_merge($order_fields, $extrafield_array);
0 ignored issues
show
introduced by
The condition is_array($extrafield_array) is always false.
Loading history...
239
240
$server->wsdl->addComplexType(
241
		'order',
242
		'complexType',
243
		'struct',
244
		'all',
245
		'',
246
		$order_fields
247
);
248
249
/*
250
$server->wsdl->addComplexType(
251
		'OrdersArray',
252
		'complexType',
253
		'array',
254
		'',
255
		'SOAP-ENC:Array',
256
		array(),
257
		array(
258
				array(
259
						'ref'=>'SOAP-ENC:arrayType',
260
						'wsdl:arrayType'=>'tns:order[]'
261
				)
262
		),
263
		'tns:order'
264
);*/
265
$server->wsdl->addComplexType(
266
		'OrdersArray2',
267
		'complexType',
268
		'array',
269
		'sequence',
270
		'',
271
		array(
272
				'order' => array(
273
						'name' => 'order',
274
						'type' => 'tns:order',
275
						'minOccurs' => '0',
276
						'maxOccurs' => 'unbounded'
277
				)
278
		)
279
);
280
281
282
283
// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
284
// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
285
// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
286
$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
287
$styleuse = 'encoded'; // encoded/literal/literal wrapped
288
// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
289
290
// Register WSDL
291
$server->register(
292
		'getOrder',
293
		array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Entry values
294
		array('result'=>'tns:result', 'order'=>'tns:order'), // Exit values
295
		$ns,
296
		$ns.'#getOrder',
297
		$styledoc,
298
		$styleuse,
299
		'WS to get a particular invoice'
300
);
301
302
$server->register(
303
		'getOrdersForThirdParty',
304
		array('authentication'=>'tns:authentication', 'idthirdparty'=>'xsd:string'), // Entry values
305
		array('result'=>'tns:result', 'orders'=>'tns:OrdersArray2'), // Exit values
306
		$ns,
307
		$ns.'#getOrdersForThirdParty',
308
		$styledoc,
309
		$styleuse,
310
		'WS to get all orders of a third party'
311
);
312
313
$server->register(
314
		'createOrder',
315
		array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
316
		array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string'), // Exit values
317
		$ns,
318
		$ns.'#createOrder',
319
		$styledoc,
320
		$styleuse,
321
		'WS to create an order'
322
);
323
324
$server->register(
325
		'updateOrder',
326
		array('authentication'=>'tns:authentication', 'order'=>'tns:order'), // Entry values
327
		array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'), // Exit values
328
		$ns,
329
		$ns.'#updateOrder',
330
		$styledoc,
331
		$styleuse,
332
		'WS to update an order'
333
);
334
335
$server->register(
336
		'validOrder',
337
		array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'id_warehouse'=>'xsd:string'), // Entry values
338
		array('result'=>'tns:result'), // Exit values
339
		$ns,
340
		$ns.'#validOrder',
341
		$styledoc,
342
		$styleuse,
343
		'WS to valid an order'
344
);
345
346
/**
347
 * Get order from id, ref or ref_ext.
348
 *
349
 * @param	array		$authentication		Array of authentication information
350
 * @param	int			$id					Id
351
 * @param	string		$ref				Ref
352
 * @param	string		$ref_ext			Ref_ext
353
 * @return	array							Array result
354
 */
355
function getOrder($authentication, $id = '', $ref = '', $ref_ext = '')
356
{
357
	global $db, $conf, $langs;
358
359
	dol_syslog("Function: getOrder login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
360
361
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
362
363
	// Init and check authentication
364
	$objectresp = array();
365
	$errorcode = ''; $errorlabel = '';
366
	$error = 0;
367
368
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
369
370
	if ($fuser->societe_id) $socid = $fuser->societe_id;
0 ignored issues
show
Bug introduced by
The property societe_id does not exist on User. Did you mean societe?
Loading history...
371
372
	// Check parameters
373
	if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
374
	{
375
		$error++;
376
		$errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
377
	}
378
379
	if (!$error)
380
	{
381
		$fuser->getrights();
382
383
		if ($fuser->rights->commande->lire)
384
		{
385
			$order = new Commande($db);
386
			$result = $order->fetch($id, $ref, $ref_ext);
387
			if ($result > 0)
388
			{
389
				// Security for external user
390
				if ($socid && ($socid != $order->socid))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $socid does not seem to be defined for all execution paths leading up to this point.
Loading history...
391
				{
392
					$error++;
393
					$errorcode = 'PERMISSION_DENIED'; $errorlabel = $order->socid.'User does not have permission for this request';
394
				}
395
396
				if (!$error)
397
				{
398
					$linesresp = array();
399
					$i = 0;
400
					foreach ($order->lines as $line)
401
					{
402
						//var_dump($line); exit;
403
						$linesresp[] = array(
404
						'id'=>$line->rowid,
1 ignored issue
show
Deprecated Code introduced by
The property CommonObjectLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

404
						'id'=>/** @scrutinizer ignore-deprecated */ $line->rowid,

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
405
						'fk_commande'=>$line->fk_commande,
406
						'fk_parent_line'=>$line->fk_parent_line,
407
						'desc'=>$line->desc,
408
						'qty'=>$line->qty,
409
						'price'=>$line->price,
1 ignored issue
show
Deprecated Code introduced by
The property CommonOrderLine::$price has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

409
						'price'=>/** @scrutinizer ignore-deprecated */ $line->price,
Loading history...
410
						'unitprice'=>$line->subprice,
411
						'vat_rate'=>$line->tva_tx,
412
						'remise'=>$line->remise,
1 ignored issue
show
Deprecated Code introduced by
The property OrderLine::$remise has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

412
						'remise'=>/** @scrutinizer ignore-deprecated */ $line->remise,
Loading history...
413
						'remise_percent'=>$line->remise_percent,
414
						'product_id'=>$line->fk_product,
415
						'product_type'=>$line->product_type,
416
						'total_net'=>$line->total_ht,
417
						'total_vat'=>$line->total_tva,
418
						'total'=>$line->total_ttc,
419
						'date_start'=>$line->date_start,
420
						'date_end'=>$line->date_end,
421
						'product_ref'=>$line->product_ref,
422
						'product_label'=>$line->product_label,
423
						'product_desc'=>$line->product_desc
424
						);
425
						$i++;
426
					}
427
428
					// Create order
429
					$objectresp = array(
430
					'result'=>array('result_code'=>'OK', 'result_label'=>''),
431
					'order'=>array(
432
					'id' => $order->id,
433
					'ref' => $order->ref,
434
					'ref_client' => $order->ref_client,
435
					'ref_ext' => $order->ref_ext,
436
					'ref_int' => $order->ref_int,
437
					'thirdparty_id' => $order->socid,
438
					'status' => $order->statut,
439
440
					'total_net' => $order->total_ht,
441
					'total_vat' => $order->total_tva,
442
					'total_localtax1' => $order->total_localtax1,
443
					'total_localtax2' => $order->total_localtax2,
444
					'total' => $order->total_ttc,
445
					'project_id' => $order->fk_project,
446
447
					'date' => $order->date ?dol_print_date($order->date, 'dayrfc') : '',
448
					'date_creation' => $order->date_creation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
449
					'date_validation' => $order->date_validation ?dol_print_date($order->date_creation, 'dayhourrfc') : '',
450
					'date_modification' => $order->date_modification ?dol_print_date($order->date_modification, 'dayhourrfc') : '',
451
452
					'remise' => $order->remise,
453
					'remise_percent' => $order->remise_percent,
454
					'remise_absolue' => $order->remise_absolue,
455
456
					'source' => $order->source,
457
					'billed' => $order->billed,
458
					'note_private' => $order->note_private,
459
					'note_public' => $order->note_public,
460
					'cond_reglement_id' => $order->cond_reglement_id,
461
					'cond_reglement_code' => $order->cond_reglement_code,
462
					'cond_reglement' => $order->cond_reglement,
1 ignored issue
show
Deprecated Code introduced by
The property CommonObject::$cond_reglement has been deprecated: Kept for compatibility ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

462
					'cond_reglement' => /** @scrutinizer ignore-deprecated */ $order->cond_reglement,

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
463
					'mode_reglement_id' => $order->mode_reglement_id,
464
					'mode_reglement_code' => $order->mode_reglement_code,
465
					'mode_reglement' => $order->mode_reglement,
466
467
					'date_livraison' => $order->date_livraison,
468
469
					'demand_reason_id' => $order->demand_reason_id,
470
					'demand_reason_code' => $order->demand_reason_code,
471
472
					'lines' => $linesresp
473
					));
474
				}
475
			}
476
			else {
477
				$error++;
478
				$errorcode = 'NOT_FOUND';
479
                $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
480
			}
481
		}
482
		else {
483
			$error++;
484
			$errorcode = 'PERMISSION_DENIED';
485
            $errorlabel = 'User does not have permission for this request';
486
		}
487
	}
488
489
	if ($error)
490
	{
491
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
492
	}
493
494
	return $objectresp;
495
}
496
497
498
/**
499
 * Get list of orders for third party
500
 *
501
 * @param	array		$authentication		Array of authentication information
502
 * @param	int			$idthirdparty		Id of thirdparty
503
 * @return	array							Array result
504
 */
505
function getOrdersForThirdParty($authentication, $idthirdparty)
506
{
507
	global $db, $conf, $langs;
508
509
	dol_syslog("Function: getOrdersForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
510
511
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
512
513
	// Init and check authentication
514
	$objectresp = array();
515
	$errorcode = ''; $errorlabel = '';
516
	$error = 0;
517
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
518
519
	if ($fuser->societe_id) $socid = $fuser->societe_id;
0 ignored issues
show
Bug introduced by
The property societe_id does not exist on User. Did you mean societe?
Loading history...
520
521
	// Check parameters
522
	if (!$error && empty($idthirdparty))
523
	{
524
		$error++;
525
		$errorcode = 'BAD_PARAMETERS'; $errorlabel = 'Parameter id is not provided';
526
	}
527
528
	if (!$error)
529
	{
530
		$linesorders = array();
531
532
		$sql = 'SELECT c.rowid as orderid';
533
		$sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c';
534
		$sql .= " WHERE c.entity = ".$conf->entity;
535
		if ($idthirdparty != 'all') $sql .= " AND c.fk_soc = ".$db->escape($idthirdparty);
536
537
538
		$resql = $db->query($sql);
539
		if ($resql)
540
		{
541
			$num = $db->num_rows($resql);
542
			$i = 0;
543
			while ($i < $num)
544
			{
545
				// En attendant remplissage par boucle
546
				$obj = $db->fetch_object($resql);
547
548
				$order = new Commande($db);
549
				$order->fetch($obj->orderid);
550
551
				// Sécurité pour utilisateur externe
552
				if ($socid && ($socid != $order->socid))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $socid does not seem to be defined for all execution paths leading up to this point.
Loading history...
553
				{
554
					$error++;
555
					$errorcode = 'PERMISSION_DENIED';
556
                    $errorlabel = $order->socid.' User does not have permission for this request';
557
				}
558
559
				if (!$error)
560
				{
561
					// Define lines of invoice
562
					$linesresp = array();
563
					foreach ($order->lines as $line)
564
					{
565
						$linesresp[] = array(
566
						'id'=>$line->rowid,
1 ignored issue
show
Deprecated Code introduced by
The property CommonObjectLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

566
						'id'=>/** @scrutinizer ignore-deprecated */ $line->rowid,

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
567
						'type'=>$line->product_type,
568
						'fk_commande'=>$line->fk_commande,
569
						'fk_parent_line'=>$line->fk_parent_line,
570
						'desc'=>$line->desc,
571
						'qty'=>$line->qty,
572
						'price'=>$line->price,
1 ignored issue
show
Deprecated Code introduced by
The property CommonOrderLine::$price has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

572
						'price'=>/** @scrutinizer ignore-deprecated */ $line->price,
Loading history...
573
						'unitprice'=>$line->subprice,
574
						'tva_tx'=>$line->tva_tx,
575
						'remise'=>$line->remise,
1 ignored issue
show
Deprecated Code introduced by
The property OrderLine::$remise has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

575
						'remise'=>/** @scrutinizer ignore-deprecated */ $line->remise,
Loading history...
576
						'remise_percent'=>$line->remise_percent,
577
						'total_net'=>$line->total_ht,
578
						'total_vat'=>$line->total_tva,
579
						'total'=>$line->total_ttc,
580
						'date_start'=>$line->date_start,
581
						'date_end'=>$line->date_end,
582
						'product_id'=>$line->fk_product,
583
						'product_ref'=>$line->product_ref,
584
						'product_label'=>$line->product_label,
585
						'product_desc'=>$line->product_desc
586
						);
587
					}
588
589
					// Now define invoice
590
					$linesorders[] = array(
591
					'id' => $order->id,
592
					'ref' => $order->ref,
593
					'ref_client' => $order->ref_client,
594
					'ref_ext' => $order->ref_ext,
595
					'ref_int' => $order->ref_int,
596
					'socid' => $order->socid,
597
					'status' => $order->statut,
598
599
					'total_net' => $order->total_ht,
600
					'total_vat' => $order->total_tva,
601
					'total_localtax1' => $order->total_localtax1,
602
					'total_localtax2' => $order->total_localtax2,
603
					'total' => $order->total_ttc,
604
					'project_id' => $order->fk_project,
605
606
					'date' => $order->date_commande ?dol_print_date($order->date_commande, 'dayrfc') : '',
1 ignored issue
show
Deprecated Code introduced by
The property Commande::$date_commande has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

606
					'date' => /** @scrutinizer ignore-deprecated */ $order->date_commande ?dol_print_date($order->date_commande, 'dayrfc') : '',
Loading history...
607
608
					'remise' => $order->remise,
609
					'remise_percent' => $order->remise_percent,
610
					'remise_absolue' => $order->remise_absolue,
611
612
					'source' => $order->source,
613
					'billed' => $order->billed,
614
					'note_private' => $order->note_private,
615
					'note_public' => $order->note_public,
616
					'cond_reglement_id' => $order->cond_reglement_id,
617
					'cond_reglement' => $order->cond_reglement,
1 ignored issue
show
Deprecated Code introduced by
The property CommonObject::$cond_reglement has been deprecated: Kept for compatibility ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

617
					'cond_reglement' => /** @scrutinizer ignore-deprecated */ $order->cond_reglement,

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
618
					'cond_reglement_doc' => $order->cond_reglement_doc,
619
					'cond_reglement_code' => $order->cond_reglement_code,
620
					'mode_reglement_id' => $order->mode_reglement_id,
621
					'mode_reglement' => $order->mode_reglement,
622
					'mode_reglement_code' => $order->mode_reglement_code,
623
624
					'date_livraison' => $order->date_livraison,
625
626
					'demand_reason_id' => $order->demand_reason_id,
627
					'demand_reason_code' => $order->demand_reason_code,
628
629
					'lines' => $linesresp
630
					);
631
				}
632
				$i++;
633
			}
634
635
			$objectresp = array(
636
			'result'=>array('result_code'=>'OK', 'result_label'=>''),
637
			'orders'=>$linesorders
638
639
			);
640
		}
641
		else {
642
			$error++;
643
			$errorcode = $db->lasterrno(); $errorlabel = $db->lasterror();
644
		}
645
	}
646
647
	if ($error)
648
	{
649
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
650
	}
651
652
	return $objectresp;
653
}
654
655
656
/**
657
 * Create order
658
 *
659
 * @param	array		$authentication		Array of authentication information
660
 * @param	array		$order				Order info
661
 * @return	int								Id of new order
662
 */
663
function createOrder($authentication, $order)
664
{
665
	global $db, $conf, $langs;
666
667
	include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
668
669
	$now = dol_now();
670
671
	dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
672
673
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
674
675
	// Init and check authentication
676
	$objectresp = array();
677
	$errorcode = '';
678
    $errorlabel = '';
679
	$error = 0;
680
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
681
682
	// Check parameters
683
684
685
	if (!$error)
686
	{
687
		$newobject = new Commande($db);
688
		$newobject->socid = $order['thirdparty_id'];
689
		$newobject->type = $order['type'];
690
		$newobject->ref_ext = $order['ref_ext'];
691
		$newobject->date = dol_stringtotime($order['date'], 'dayrfc');
692
		$newobject->date_lim_reglement = dol_stringtotime($order['date_due'], 'dayrfc');
693
		$newobject->note_private = $order['note_private'];
694
		$newobject->note_public = $order['note_public'];
695
		$newobject->statut = Commande::STATUS_DRAFT; // We start with status draft
696
		$newobject->billed = $order['billed'];
697
		$newobject->fk_project = $order['project_id'];
698
		$newobject->cond_reglement_id = $order['cond_reglement_id'];
699
		$newobject->demand_reason_id = $order['demand_reason_id'];
700
		$newobject->date_creation = $now;
701
702
		$elementtype = 'commande';
703
704
		// Retrieve all extrafield for order
705
		// fetch optionals attributes and labels
706
		$extrafields = new ExtraFields($db);
707
		$extrafields->fetch_name_optionals_label($elementtype, true);
708
		if (is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
709
		{
710
			foreach ($extrafields->attributes[$elementtype]['label'] as $key=>$label)
711
			{
712
				$key = 'options_'.$key;
713
				$newobject->array_options[$key] = $order[$key];
714
			}
715
		}
716
717
		// Trick because nusoap does not store data with same structure if there is one or several lines
718
		$arrayoflines = array();
719
		if (isset($order['lines']['line'][0])) $arrayoflines = $order['lines']['line'];
720
		else $arrayoflines = $order['lines'];
721
722
		foreach ($arrayoflines as $key => $line)
723
		{
724
			// $key can be 'line' or '0','1',...
725
			$newline = new OrderLine($db);
726
727
			$newline->type = $line['type'];
728
			$newline->desc = $line['desc'];
729
			$newline->fk_product = $line['product_id'];
730
			$newline->tva_tx = $line['vat_rate'];
731
			$newline->qty = $line['qty'];
732
			$newline->price = $line['price'];
1 ignored issue
show
Deprecated Code introduced by
The property CommonOrderLine::$price has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

732
			/** @scrutinizer ignore-deprecated */ $newline->price = $line['price'];
Loading history...
733
			$newline->subprice = $line['unitprice'];
734
			$newline->total_ht = $line['total_net'];
735
			$newline->total_tva = $line['total_vat'];
736
			$newline->total_ttc = $line['total'];
737
			$newline->date_start = $line['date_start'];
738
			$newline->date_end = $line['date_end'];
739
740
			$elementtype = 'commandedet';
741
742
			// Retrieve all extrafield for lines
743
			// fetch optionals attributes and labels
744
			$extrafields = new ExtraFields($db);
745
			$extrafields->fetch_name_optionals_label($elementtype, true);
746
			if (is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
747
			{
748
				foreach ($extrafields->attributes[$elementtype]['label'] as $key=>$label)
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
749
				{
750
					$key = 'options_'.$key;
751
					$newline->array_options[$key] = $line[$key];
752
				}
753
			}
754
755
			$newobject->lines[] = $newline;
756
		}
757
758
759
		$db->begin();
760
		dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
761
		$result = $newobject->create($fuser);
762
		dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
763
		if ($result < 0)
764
		{
765
			dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
766
			$error++;
767
		}
768
769
		if ($order['status'] == 1)   // We want order to have status validated
770
		{
771
			dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
772
			$result = $newobject->valid($fuser);
773
			if ($result < 0)
774
			{
775
				dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
776
				$error++;
777
			}
778
		}
779
780
		if ($result >= 0)
781
		{
782
			dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
783
			$db->commit();
784
			$objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
785
		}
786
		else {
787
			dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
788
			$db->rollback();
789
			$error++;
790
			$errorcode = 'KO';
791
			$errorlabel = $newobject->error;
792
		}
793
	}
794
795
	if ($error)
796
	{
797
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
798
	}
799
800
	return $objectresp;
801
}
802
803
804
/**
805
 * Valid an order
806
 *
807
 * @param	array		$authentication		Array of authentication information
808
 * @param	int			$id					Id of order to validate
809
 * @param	int			$id_warehouse		Id of warehouse to use for stock decrease
810
 * @return	array							Array result
811
 */
812
function validOrder($authentication, $id = '', $id_warehouse = 0)
813
{
814
	global $db, $conf, $langs;
815
816
	dol_syslog("Function: validOrder login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ref seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $ref_ext seems to be never defined.
Loading history...
817
818
	// Init and check authentication
819
	$objectresp = array();
820
	$errorcode = '';
821
    $errorlabel = '';
822
	$error = 0;
823
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
824
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
825
826
	if (!$error)
827
	{
828
		$fuser->getrights();
829
830
		if ($fuser->rights->commande->lire)
831
		{
832
			$order = new Commande($db);
833
			$result = $order->fetch($id, $ref, $ref_ext);
834
835
			$order->fetch_thirdparty();
836
			$db->begin();
837
			if ($result > 0)
838
			{
839
				$result = $order->valid($fuser, $id_warehouse);
840
841
				if ($result >= 0)
842
				{
843
					// Define output language
844
					$outputlangs = $langs;
845
					$order->generateDocument($order->modelpdf, $outputlangs);
846
				}
847
				else {
848
					$db->rollback();
849
					$error++;
850
					$errorcode = 'KO';
851
					$errorlabel = $order->error;
852
				}
853
			}
854
			else {
855
				$db->rollback();
856
				$error++;
857
				$errorcode = 'KO';
858
				$errorlabel = $order->error;
859
			}
860
		}
861
		else {
862
			$db->rollback();
863
			$error++;
864
			$errorcode = 'KO';
865
			$errorlabel = $order->error;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $order seems to be never defined.
Loading history...
866
		}
867
	}
868
869
	if ($error)
870
	{
871
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
872
	}
873
	else {
874
		$db->commit();
875
		$objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
876
	}
877
878
	return $objectresp;
879
}
880
881
/**
882
 * Update an order
883
 *
884
 * @param	array		$authentication		Array of authentication information
885
 * @param	array		$order				Order info
886
 * @return	array							Array result
887
 */
888
function updateOrder($authentication, $order)
889
{
890
	global $db, $conf, $langs;
891
892
	$now = dol_now();
893
894
	dol_syslog("Function: updateOrder login=".$authentication['login']);
895
896
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
897
898
	// Init and check authentication
899
	$objectresp = array();
900
	$errorcode = ''; $errorlabel = '';
901
	$error = 0;
902
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
903
	// Check parameters
904
	if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext'])) {
905
		$error++; $errorcode = 'KO'; $errorlabel = "Order id or ref or ref_ext is mandatory.";
906
	}
907
908
	if (!$error)
909
	{
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
			{
924
				if ($order['status'] == -1) $result = $object->cancel($fuser);
925
				if ($order['status'] == 1)
926
				{
927
					$result = $object->valid($fuser);
928
					if ($result >= 0)
929
					{
930
						// Define output language
931
						$outputlangs = $langs;
932
						$object->generateDocument($order->modelpdf, $outputlangs);
933
					}
934
				}
935
				if ($order['status'] == 0)  $result = $object->set_reopen($fuser);
936
				if ($order['status'] == 3)  $result = $object->cloture($fuser);
937
			}
938
939
			if (isset($order['billed']))
940
			{
941
				if ($order['billed'])   $result = $object->classifyBilled($fuser);
942
				if (!$order['billed']) $result = $object->classifyUnBilled($fuser);
943
			}
944
945
			$elementtype = 'commande';
946
947
			//Retreive all extrafield for object
948
			// fetch optionals attributes and labels
949
			$extrafields = new ExtraFields($db);
950
			$extrafields->fetch_name_optionals_label($elementtype, true);
951
			if (is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
952
			{
953
				foreach ($extrafields->attributes[$elementtype]['label'] as $key=>$label)
954
				{
955
					$key = 'options_'.$key;
956
					if (isset($order[$key]))
957
					{
958
						$result = $object->setValueFrom($key, $order[$key], 'commande_extrafields');
959
					}
960
				}
961
			}
962
963
			if ($result <= 0) {
964
				$error++;
965
			}
966
		}
967
968
		if ((!$error) && ($objectfound))
969
		{
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
		}
978
		elseif ($objectfound)
979
		{
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
	{
993
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
994
	}
995
996
	return $objectresp;
997
}
998
999
1000
// Return the results.
1001
$server->service(file_get_contents("php://input"));
1002