Passed
Pull Request — master (#2)
by
unknown
26:19
created

updateOrder()   F

Complexity

Conditions 26
Paths 6032

Size

Total Lines 105
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 57
nc 6032
nop 2
dl 0
loc 105
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 <http://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;
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
114
//Retreive all extrafield for thirdsparty
115
// fetch optionals attributes and labels
116
$extrafields=new ExtraFields($db);
117
$extralabels=$extrafields->fetch_name_optionals_label('commandedet',true);
118
$extrafield_line_array=null;
119
if (is_array($extrafields) && count($extrafields)>0) {
120
	$extrafield_line_array = array();
121
}
122
foreach($extrafields->attribute_label as $key=>$label)
123
{
124
	//$value=$object->array_options["options_".$key];
125
	$type =$extrafields->attribute_type[$key];
126
	if ($type=='date' || $type=='datetime') {$type='xsd:dateTime';}
127
	else {$type='xsd:string';}
128
	$extrafield_line_array['options_'.$key]=array('name'=>'options_'.$key,'type'=>$type);
129
}
130
if (is_array($extrafield_line_array)) $line_fields=array_merge($line_fields,$extrafield_line_array);
131
132
// Define other specific objects
133
$server->wsdl->addComplexType(
134
		'line',
135
		'complexType',
136
		'struct',
137
		'all',
138
		'',
139
		$line_fields
140
);
141
142
/*$server->wsdl->addComplexType(
143
		'LinesArray',
144
		'complexType',
145
		'array',
146
		'',
147
		'SOAP-ENC:Array',
148
		array(),
149
		array(
150
				array(
151
						'ref'=>'SOAP-ENC:arrayType',
152
						'wsdl:arrayType'=>'tns:line[]'
153
				)
154
		),
155
		'tns:line'
156
);*/
157
$server->wsdl->addComplexType(
158
		'LinesArray2',
159
		'complexType',
160
		'array',
161
		'sequence',
162
		'',
163
		array(
164
				'line' => array(
165
						'name' => 'line',
166
						'type' => 'tns:line',
167
						'minOccurs' => '0',
168
						'maxOccurs' => 'unbounded'
169
				)
170
		)
171
);
172
173
$order_fields = array(
174
	'id' => array('name'=>'id','type'=>'xsd:string'),
175
	'ref' => array('name'=>'ref','type'=>'xsd:string'),
176
	'ref_client' => array('name'=>'ref_client','type'=>'xsd:string'),
177
	'ref_ext' => array('name'=>'ref_ext','type'=>'xsd:string'),
178
	'ref_int' => array('name'=>'ref_int','type'=>'xsd:string'),
179
	'thirdparty_id' => array('name'=>'thirdparty_id','type'=>'xsd:int'),
180
	'status' => array('name'=>'status','type'=>'xsd:int'),
181
	'billed' => array('name'=>'billed','type'=>'xsd:string'),
182
	'total_net' => array('name'=>'total_net','type'=>'xsd:double'),
183
	'total_vat' => array('name'=>'total_vat','type'=>'xsd:double'),
184
	'total_localtax1' => array('name'=>'total_localtax1','type'=>'xsd:double'),
185
	'total_localtax2' => array('name'=>'total_localtax2','type'=>'xsd:double'),
186
	'total' => array('name'=>'total','type'=>'xsd:double'),
187
	'date' => array('name'=>'date','type'=>'xsd:date'),
188
	'date_creation' => array('name'=>'date_creation','type'=>'xsd:dateTime'),
189
	'date_validation' => array('name'=>'date_validation','type'=>'xsd:dateTime'),
190
	'date_modification' => array('name'=>'date_modification','type'=>'xsd:dateTime'),
191
	'remise' => array('name'=>'remise','type'=>'xsd:string'),
192
	'remise_percent' => array('name'=>'remise_percent','type'=>'xsd:string'),
193
	'remise_absolue' => array('name'=>'remise_absolue','type'=>'xsd:string'),
194
	'source' => array('name'=>'source','type'=>'xsd:string'),
195
	'note_private' => array('name'=>'note_private','type'=>'xsd:string'),
196
	'note_public' => array('name'=>'note_public','type'=>'xsd:string'),
197
	'project_id' => array('name'=>'project_id','type'=>'xsd:string'),
198
199
	'mode_reglement_id' => array('name'=>'mode_reglement_id','type'=>'xsd:string'),
200
	'mode_reglement_code' => array('name'=>'mode_reglement_code','type'=>'xsd:string'),
201
	'mode_reglement' => array('name'=>'mode_reglement','type'=>'xsd:string'),
202
	'cond_reglement_id' => array('name'=>'cond_reglement_id','type'=>'xsd:string'),
203
	'cond_reglement_code' => array('name'=>'cond_reglement_code','type'=>'xsd:string'),
204
	'cond_reglement' => array('name'=>'cond_reglement','type'=>'xsd:string'),
205
	'cond_reglement_doc' => array('name'=>'cond_reglement_doc','type'=>'xsd:string'),
206
207
	'date_livraison' => array('name'=>'date_livraison','type'=>'xsd:date'),
208
	'fk_delivery_address' => array('name'=>'fk_delivery_address','type'=>'xsd:int'),
209
	'demand_reason_id' => array('name'=>'demand_reason_id','type'=>'xsd:string'),
210
211
	'lines' => array('name'=>'lines','type'=>'tns:LinesArray2')
212
);
213
214
//Retreive all extrafield for thirdsparty
215
// fetch optionals attributes and labels
216
$extrafields=new ExtraFields($db);
217
$extralabels=$extrafields->fetch_name_optionals_label('commande',true);
218
$extrafield_array=null;
219
if (is_array($extrafields) && count($extrafields)>0) {
220
	$extrafield_array = array();
221
}
222
foreach($extrafields->attribute_label as $key=>$label)
223
{
224
	//$value=$object->array_options["options_".$key];
225
	$type =$extrafields->attribute_type[$key];
226
	if ($type=='date' || $type=='datetime') {$type='xsd:dateTime';}
227
	else {$type='xsd:string';}
228
	$extrafield_array['options_'.$key]=array('name'=>'options_'.$key,'type'=>$type);
229
}
230
if (is_array($extrafield_array)) $order_fields=array_merge($order_fields,$extrafield_array);
231
232
$server->wsdl->addComplexType(
233
		'order',
234
		'complexType',
235
		'struct',
236
		'all',
237
		'',
238
		$order_fields
239
);
240
241
/*
242
$server->wsdl->addComplexType(
243
		'OrdersArray',
244
		'complexType',
245
		'array',
246
		'',
247
		'SOAP-ENC:Array',
248
		array(),
249
		array(
250
				array(
251
						'ref'=>'SOAP-ENC:arrayType',
252
						'wsdl:arrayType'=>'tns:order[]'
253
				)
254
		),
255
		'tns:order'
256
);*/
257
$server->wsdl->addComplexType(
258
		'OrdersArray2',
259
		'complexType',
260
		'array',
261
		'sequence',
262
		'',
263
		array(
264
				'order' => array(
265
						'name' => 'order',
266
						'type' => 'tns:order',
267
						'minOccurs' => '0',
268
						'maxOccurs' => 'unbounded'
269
				)
270
		)
271
);
272
273
274
275
// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
276
// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
277
// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
278
$styledoc='rpc';       // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
279
$styleuse='encoded';   // encoded/literal/literal wrapped
280
// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
281
282
// Register WSDL
283
$server->register(
284
		'getOrder',
285
		array('authentication'=>'tns:authentication','id'=>'xsd:string','ref'=>'xsd:string','ref_ext'=>'xsd:string'), // Entry values
286
		array('result'=>'tns:result','order'=>'tns:order'),	// Exit values
287
		$ns,
288
		$ns.'#getOrder',
289
		$styledoc,
290
		$styleuse,
291
		'WS to get a particular invoice'
292
);
293
294
$server->register(
295
		'getOrdersForThirdParty',
296
		array('authentication'=>'tns:authentication','idthirdparty'=>'xsd:string'),	// Entry values
297
		array('result'=>'tns:result','orders'=>'tns:OrdersArray2'),	// Exit values
298
		$ns,
299
		$ns.'#getOrdersForThirdParty',
300
		$styledoc,
301
		$styleuse,
302
		'WS to get all orders of a third party'
303
);
304
305
$server->register(
306
		'createOrder',
307
		array('authentication'=>'tns:authentication','order'=>'tns:order'),	// Entry values
308
		array('result'=>'tns:result','id'=>'xsd:string','ref'=>'xsd:string'),	// Exit values
309
		$ns,
310
		$ns.'#createOrder',
311
		$styledoc,
312
		$styleuse,
313
		'WS to create an order'
314
);
315
316
$server->register(
317
		'updateOrder',
318
		array('authentication'=>'tns:authentication','order'=>'tns:order'),	// Entry values
319
		array('result'=>'tns:result','id'=>'xsd:string','ref'=>'xsd:string','ref_ext'=>'xsd:string'),	// Exit values
320
		$ns,
321
		$ns.'#updateOrder',
322
		$styledoc,
323
		$styleuse,
324
		'WS to update an order'
325
);
326
327
$server->register(
328
		'validOrder',
329
		array('authentication'=>'tns:authentication','id'=>'xsd:string','id_warehouse'=>'xsd:string'),  // Entry values
330
		array('result'=>'tns:result'),	// Exit values
331
		$ns,
332
		$ns.'#validOrder',
333
		$styledoc,
334
		$styleuse,
335
		'WS to valid an order'
336
);
337
338
/**
339
 * Get order from id, ref or ref_ext.
340
 *
341
 * @param	array		$authentication		Array of authentication information
342
 * @param	int			$id					Id
343
 * @param	string		$ref				Ref
344
 * @param	string		$ref_ext			Ref_ext
345
 * @return	array							Array result
346
 */
347
function getOrder($authentication,$id='',$ref='',$ref_ext='')
348
{
349
	global $db,$conf,$langs;
350
351
	dol_syslog("Function: getOrder login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
352
353
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
354
355
	// Init and check authentication
356
	$objectresp=array();
357
	$errorcode='';$errorlabel='';
358
	$error=0;
359
360
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
361
362
	if ($fuser->societe_id) $socid=$fuser->societe_id;
363
364
	// Check parameters
365
	if (! $error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
366
	{
367
		$error++;
368
		$errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
369
	}
370
371
	if (! $error)
372
	{
373
		$fuser->getrights();
374
375
		if ($fuser->rights->commande->lire)
376
		{
377
			$order=new Commande($db);
378
			$result=$order->fetch($id,$ref,$ref_ext);
379
			if ($result > 0)
380
			{
381
				// Security for external user
382
				if( $socid && ( $socid != $order->socid) )
383
				{
384
					$error++;
385
					$errorcode='PERMISSION_DENIED'; $errorlabel=$order->socid.'User does not have permission for this request';
386
				}
387
388
				if(!$error)
389
				{
390
391
					$linesresp=array();
392
					$i=0;
393
					foreach($order->lines as $line)
394
					{
395
						//var_dump($line); exit;
396
						$linesresp[]=array(
397
						'id'=>$line->rowid,
0 ignored issues
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

397
						'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...
398
						'fk_commande'=>$line->fk_commande,
399
						'fk_parent_line'=>$line->fk_parent_line,
400
						'desc'=>$line->desc,
401
						'qty'=>$line->qty,
402
						'price'=>$line->price,
403
						'unitprice'=>$line->subprice,
404
						'vat_rate'=>$line->tva_tx,
405
						'remise'=>$line->remise,
406
						'remise_percent'=>$line->remise_percent,
407
						'product_id'=>$line->fk_product,
408
						'product_type'=>$line->product_type,
409
						'total_net'=>$line->total_ht,
410
						'total_vat'=>$line->total_tva,
411
						'total'=>$line->total_ttc,
412
						'date_start'=>$line->date_start,
413
						'date_end'=>$line->date_end,
414
						'product_ref'=>$line->product_ref,
415
						'product_label'=>$line->product_label,
416
						'product_desc'=>$line->product_desc
417
						);
418
						$i++;
419
					}
420
421
					// Create order
422
					$objectresp = array(
423
					'result'=>array('result_code'=>'OK', 'result_label'=>''),
424
					'order'=>array(
425
					'id' => $order->id,
426
					'ref' => $order->ref,
427
					'ref_client' => $order->ref_client,
428
					'ref_ext' => $order->ref_ext,
429
					'ref_int' => $order->ref_int,
430
					'thirdparty_id' => $order->socid,
431
					'status' => $order->statut,
432
433
					'total_net' => $order->total_ht,
434
					'total_vat' => $order->total_tva,
435
					'total_localtax1' => $order->total_localtax1,
436
					'total_localtax2' => $order->total_localtax2,
437
					'total' => $order->total_ttc,
438
					'project_id' => $order->fk_project,
439
440
					'date' => $order->date_commande?dol_print_date($order->date_commande,'dayrfc'):'',
441
					'date_creation' => $invoice->date_creation?dol_print_date($invoice->date_creation,'dayhourrfc'):'',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $invoice seems to be never defined.
Loading history...
442
					'date_validation' => $invoice->date_validation?dol_print_date($invoice->date_creation,'dayhourrfc'):'',
443
					'date_modification' => $invoice->datem?dol_print_date($invoice->datem,'dayhourrfc'):'',
444
445
					'remise' => $order->remise,
446
					'remise_percent' => $order->remise_percent,
447
					'remise_absolue' => $order->remise_absolue,
448
449
					'source' => $order->source,
450
					'billed' => $order->billed,
451
					'note_private' => $order->note_private,
452
					'note_public' => $order->note_public,
453
					'cond_reglement_id' => $order->cond_reglement_id,
454
					'cond_reglement_code' => $order->cond_reglement_code,
455
					'cond_reglement' => $order->cond_reglement,
0 ignored issues
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

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

560
						'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...
561
						'type'=>$line->product_type,
562
						'fk_commande'=>$line->fk_commande,
563
						'fk_parent_line'=>$line->fk_parent_line,
564
						'desc'=>$line->desc,
565
						'qty'=>$line->qty,
566
						'price'=>$line->price,
567
						'unitprice'=>$line->subprice,
568
						'tva_tx'=>$line->tva_tx,
569
						'remise'=>$line->remise,
570
						'remise_percent'=>$line->remise_percent,
571
						'total_net'=>$line->total_ht,
572
						'total_vat'=>$line->total_tva,
573
						'total'=>$line->total_ttc,
574
						'date_start'=>$line->date_start,
575
						'date_end'=>$line->date_end,
576
						'product_id'=>$line->fk_product,
577
						'product_ref'=>$line->product_ref,
578
						'product_label'=>$line->product_label,
579
						'product_desc'=>$line->product_desc
580
						);
581
					}
582
583
					// Now define invoice
584
					$linesorders[]=array(
585
					'id' => $order->id,
586
					'ref' => $order->ref,
587
					'ref_client' => $order->ref_client,
588
					'ref_ext' => $order->ref_ext,
589
					'ref_int' => $order->ref_int,
590
					'socid' => $order->socid,
591
					'status' => $order->statut,
592
593
					'total_net' => $order->total_ht,
594
					'total_vat' => $order->total_tva,
595
					'total_localtax1' => $order->total_localtax1,
596
					'total_localtax2' => $order->total_localtax2,
597
					'total' => $order->total_ttc,
598
					'project_id' => $order->fk_project,
599
600
					'date' => $order->date_commande?dol_print_date($order->date_commande,'dayrfc'):'',
601
602
					'remise' => $order->remise,
603
					'remise_percent' => $order->remise_percent,
604
					'remise_absolue' => $order->remise_absolue,
605
606
					'source' => $order->source,
607
					'billed' => $order->billed,
608
					'note_private' => $order->note_private,
609
					'note_public' => $order->note_public,
610
					'cond_reglement_id' => $order->cond_reglement_id,
611
					'cond_reglement' => $order->cond_reglement,
0 ignored issues
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

611
					'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...
612
					'cond_reglement_doc' => $order->cond_reglement_doc,
613
					'cond_reglement_code' => $order->cond_reglement_code,
614
					'mode_reglement_id' => $order->mode_reglement_id,
615
					'mode_reglement' => $order->mode_reglement,
616
					'mode_reglement_code' => $order->mode_reglement_code,
617
618
					'date_livraison' => $order->date_livraison,
619
620
					'demand_reason_id' => $order->demand_reason_id,
621
					'demand_reason_code' => $order->demand_reason_code,
622
623
					'lines' => $linesresp
624
					);
625
				}
626
				$i++;
627
			}
628
629
			$objectresp=array(
630
			'result'=>array('result_code'=>'OK', 'result_label'=>''),
631
			'orders'=>$linesorders
632
633
			);
634
		}
635
		else
636
		{
637
			$error++;
638
			$errorcode=$db->lasterrno(); $errorlabel=$db->lasterror();
639
		}
640
	}
641
642
	if ($error)
643
	{
644
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
645
	}
646
647
	return $objectresp;
648
}
649
650
651
/**
652
 * Create order
653
 *
654
 * @param	array		$authentication		Array of authentication information
655
 * @param	array		$order				Order info
656
 * @return	int								Id of new order
657
 */
658
function createOrder($authentication,$order)
659
{
660
	global $db,$conf,$langs;
661
662
	include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
663
664
	$now=dol_now();
665
666
	dol_syslog("Function: createOrder login=".$authentication['login']." socid :".$order['socid']);
667
668
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
669
670
	// Init and check authentication
671
	$objectresp=array();
672
	$errorcode='';$errorlabel='';
673
	$error=0;
674
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
675
676
	// Check parameters
677
678
679
	if (! $error)
680
	{
681
		$newobject=new Commande($db);
682
		$newobject->socid=$order['thirdparty_id'];
683
		$newobject->type=$order['type'];
684
		$newobject->ref_ext=$order['ref_ext'];
685
		$newobject->date=dol_stringtotime($order['date'],'dayrfc');
686
		$newobject->date_lim_reglement=dol_stringtotime($order['date_due'],'dayrfc');
687
		$newobject->note_private=$order['note_private'];
688
		$newobject->note_public=$order['note_public'];
689
		$newobject->statut=Commande::STATUS_DRAFT;	// We start with status draft
690
		$newobject->billed=$order['billed'];
691
		$newobject->fk_project=$order['project_id'];
692
		$newobject->fk_delivery_address=$order['fk_delivery_address'];
693
		$newobject->cond_reglement_id=$order['cond_reglement_id'];
694
		$newobject->demand_reason_id=$order['demand_reason_id'];
695
		$newobject->date_creation=$now;
696
697
		// Retrieve all extrafield for order
698
		// fetch optionals attributes and labels
699
		$extrafields=new ExtraFields($db);
700
		$extralabels=$extrafields->fetch_name_optionals_label('commandet',true);
701
		foreach($extrafields->attribute_label as $key=>$label)
702
		{
703
			$key='options_'.$key;
704
			$newobject->array_options[$key]=$order[$key];
705
		}
706
707
		// Trick because nusoap does not store data with same structure if there is one or several lines
708
		$arrayoflines=array();
709
		if (isset($order['lines']['line'][0])) $arrayoflines=$order['lines']['line'];
710
		else $arrayoflines=$order['lines'];
711
712
		foreach($arrayoflines as $key => $line)
713
		{
714
			// $key can be 'line' or '0','1',...
715
			$newline=new OrderLine($db);
716
717
			$newline->type=$line['type'];
718
			$newline->desc=$line['desc'];
719
			$newline->fk_product=$line['product_id'];
720
			$newline->tva_tx=$line['vat_rate'];
721
			$newline->qty=$line['qty'];
722
			$newline->price=$line['price'];
723
			$newline->subprice=$line['unitprice'];
724
			$newline->total_ht=$line['total_net'];
725
			$newline->total_tva=$line['total_vat'];
726
			$newline->total_ttc=$line['total'];
727
			$newline->date_start=$line['date_start'];
728
			$newline->date_end=$line['date_end'];
729
730
			// Retrieve all extrafield for lines
731
			// fetch optionals attributes and labels
732
			$extrafields=new ExtraFields($db);
733
			$extralabels=$extrafields->fetch_name_optionals_label('commandedet',true);
734
			foreach($extrafields->attribute_label as $key=>$label)
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
735
			{
736
				$key='options_'.$key;
737
				$newline->array_options[$key]=$line[$key];
738
			}
739
740
			$newobject->lines[]=$newline;
741
		}
742
743
744
		$db->begin();
745
		dol_syslog("Webservice server_order:: order creation start", LOG_DEBUG);
746
		$result=$newobject->create($fuser);
747
		dol_syslog('Webservice server_order:: order creation done with $result='.$result, LOG_DEBUG);
748
		if ($result < 0)
749
		{
750
			dol_syslog("Webservice server_order:: order creation failed", LOG_ERR);
751
			$error++;
752
		}
753
754
		if ($order['status'] == 1)   // We want order to have status validated
755
		{
756
			dol_syslog("Webservice server_order:: order validation start", LOG_DEBUG);
757
			$result=$newobject->valid($fuser);
758
			if ($result < 0)
759
			{
760
				dol_syslog("Webservice server_order:: order validation failed", LOG_ERR);
761
				$error++;
762
			}
763
		}
764
765
		if ($result >= 0)
766
		{
767
			dol_syslog("Webservice server_order:: order creation & validation succeeded, commit", LOG_DEBUG);
768
			$db->commit();
769
			$objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''),'id'=>$newobject->id,'ref'=>$newobject->ref);
770
		}
771
		else
772
		{
773
			dol_syslog("Webservice server_order:: order creation or validation failed, rollback", LOG_ERR);
774
			$db->rollback();
775
			$error++;
776
			$errorcode='KO';
777
			$errorlabel=$newobject->error;
778
		}
779
	}
780
781
	if ($error)
782
	{
783
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
784
	}
785
786
	return $objectresp;
787
}
788
789
790
/**
791
 * Valid an order
792
 *
793
 * @param	array		$authentication		Array of authentication information
794
 * @param	int			$id					Id of order to validate
795
 * @param	int			$id_warehouse		Id of warehouse to use for stock decrease
796
 * @return	array							Array result
797
 */
798
function validOrder($authentication,$id='',$id_warehouse=0)
799
{
800
	global $db,$conf,$langs;
801
802
	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...
803
804
	// Init and check authentication
805
	$objectresp=array();
806
	$errorcode='';$errorlabel='';
807
	$error=0;
808
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
809
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
810
811
	if (! $error)
812
	{
813
		$fuser->getrights();
814
815
		if ($fuser->rights->commande->lire)
816
		{
817
			$order=new Commande($db);
818
			$result=$order->fetch($id,$ref,$ref_ext);
819
820
			$order->fetch_thirdparty();
821
			$db->begin();
822
			if ($result > 0)
823
			{
824
825
				$result=$order->valid($fuser,$id_warehouse);
826
827
				if ($result	>= 0)
828
				{
829
					// Define output language
830
					$outputlangs = $langs;
831
					$order->generateDocument($order->modelpdf, $outputlangs);
832
				}
833
				else
834
				{
835
					$db->rollback();
836
					$error++;
837
					$errorcode='KO';
838
					$errorlabel=$newobject->error;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newobject seems to be never defined.
Loading history...
839
				}
840
			}
841
			else
842
			{
843
				$db->rollback();
844
				$error++;
845
				$errorcode='KO';
846
				$errorlabel=$newobject->error;
847
			}
848
		}
849
		else
850
		{
851
			$db->rollback();
852
			$error++;
853
			$errorcode='KO';
854
			$errorlabel=$newobject->error;
855
		}
856
	}
857
858
	if ($error)
859
	{
860
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
861
	}
862
	else
863
	{
864
		$db->commit();
865
		$objectresp= array('result'=>array('result_code'=>'OK', 'result_label'=>''));
866
	}
867
868
	return $objectresp;
869
}
870
871
/**
872
 * Update an order
873
 *
874
 * @param	array		$authentication		Array of authentication information
875
 * @param	array		$order				Order info
876
 * @return	array							Array result
877
 */
878
function updateOrder($authentication,$order)
879
{
880
	global $db,$conf,$langs;
881
882
	$now=dol_now();
883
884
	dol_syslog("Function: updateOrder login=".$authentication['login']);
885
886
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
887
888
	// Init and check authentication
889
	$objectresp=array();
890
	$errorcode='';$errorlabel='';
891
	$error=0;
892
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
893
	// Check parameters
894
	if (empty($order['id']) && empty($order['ref']) && empty($order['ref_ext']))	{
895
		$error++; $errorcode='KO'; $errorlabel="Order id or ref or ref_ext is mandatory.";
896
	}
897
898
	if (! $error)
899
	{
900
		$objectfound=false;
901
902
		include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
903
904
		$object=new Commande($db);
905
		$result=$object->fetch($order['id'],(empty($order['id'])?$order['ref']:''),(empty($order['id']) && empty($order['ref'])?$order['ref_ext']:''));
906
907
		if (!empty($object->id)) {
908
909
			$objectfound=true;
910
911
			$db->begin();
912
913
			if (isset($order['status']))
914
			{
915
				if ($order['status'] == -1) $result=$object->cancel($fuser);
916
				if ($order['status'] == 1)
917
				{
918
					$result=$object->valid($fuser);
919
					if ($result	>= 0)
920
					{
921
						// Define output language
922
						$outputlangs = $langs;
923
						$object->generateDocument($order->modelpdf, $outputlangs);
924
					}
925
				}
926
				if ($order['status'] == 0)  $result=$object->set_reopen($fuser);
927
				if ($order['status'] == 3)  $result=$object->cloture($fuser);
928
			}
929
930
			if (isset($order['billed']))
931
			{
932
				if ($order['billed'])   $result=$object->classifyBilled($fuser);
933
				if (! $order['billed']) $result=$object->classifyUnBilled($fuser);
934
			}
935
936
			//Retreive all extrafield for object
937
			// fetch optionals attributes and labels
938
			$extrafields=new ExtraFields($db);
939
			$extralabels=$extrafields->fetch_name_optionals_label('commande',true);
940
			foreach($extrafields->attribute_label as $key=>$label)
941
			{
942
				$key='options_'.$key;
943
				if (isset($order[$key]))
944
				{
945
					$result=$object->setValueFrom($key, $order[$key], 'commande_extrafields');
946
				}
947
			}
948
949
			if ($result <= 0) {
950
				$error++;
951
			}
952
		}
953
954
		if ((! $error) && ($objectfound))
955
		{
956
			$db->commit();
957
			$objectresp=array(
958
					'result'=>array('result_code'=>'OK', 'result_label'=>''),
959
					'id'=>$object->id,
960
					'ref'=>$object->ref,
961
					'ref_ext'=>$object->ref_ext
962
			);
963
		}
964
		elseif ($objectfound)
965
		{
966
			$db->rollback();
967
			$error++;
968
			$errorcode='KO';
969
			$errorlabel=$object->error;
970
		} else {
971
			$error++;
972
			$errorcode='NOT_FOUND';
973
			$errorlabel='Order id='.$order['id'].' ref='.$order['ref'].' ref_ext='.$order['ref_ext'].' cannot be found';
974
		}
975
	}
976
977
	if ($error)
978
	{
979
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
980
	}
981
982
	return $objectresp;
983
}
984
985
986
// Return the results.
987
$server->service(file_get_contents("php://input"));
988