Passed
Branch develop (07a336)
by
unknown
39:19
created

updateThirdParty()   F

Complexity

Conditions 16
Paths 224

Size

Total Lines 125
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 80
c 1
b 0
f 0
nc 224
nop 2
dl 0
loc 125
rs 3.983

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
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *       \file       htdocs/webservices/server_thirdparty.php
20
 *       \brief      File that is entry point to call Dolibarr WebServices
21
 */
22
23
if (!defined("NOCSRFCHECK"))    define("NOCSRFCHECK", '1');
24
25
require_once '../master.inc.php';
26
require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
27
require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
28
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
29
30
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
31
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.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('WebServicesDolibarrThirdParty', $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
86
$thirdparty_fields = array(
87
		'id' => array('name'=>'id', 'type'=>'xsd:string'),
88
		'ref' => array('name'=>'name', 'type'=>'xsd:string'),
89
		'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
90
		'fk_user_author' => array('name'=>'fk_user_author', 'type'=>'xsd:string'),
91
		'status' => array('name'=>'status', 'type'=>'xsd:string'),
92
		'client' => array('name'=>'client', 'type'=>'xsd:string'),
93
		'supplier' => array('name'=>'supplier', 'type'=>'xsd:string'),
94
		'customer_code' => array('name'=>'customer_code', 'type'=>'xsd:string'),
95
		'supplier_code' => array('name'=>'supplier_code', 'type'=>'xsd:string'),
96
		'customer_code_accountancy' => array('name'=>'customer_code_accountancy', 'type'=>'xsd:string'),
97
		'supplier_code_accountancy' => array('name'=>'supplier_code_accountancy', 'type'=>'xsd:string'),
98
		'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
99
		'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
100
		'note_private' => array('name'=>'note_private', 'type'=>'xsd:string'),
101
		'note_public' => array('name'=>'note_public', 'type'=>'xsd:string'),
102
		'address' => array('name'=>'address', 'type'=>'xsd:string'),
103
		'zip' => array('name'=>'zip', 'type'=>'xsd:string'),
104
		'town' => array('name'=>'town', 'type'=>'xsd:string'),
105
		'province_id' => array('name'=>'province_id', 'type'=>'xsd:string'),
106
		'country_id' => array('name'=>'country_id', 'type'=>'xsd:string'),
107
		'country_code' => array('name'=>'country_code', 'type'=>'xsd:string'),
108
		'country' => array('name'=>'country', 'type'=>'xsd:string'),
109
		'phone' => array('name'=>'phone', 'type'=>'xsd:string'),
110
		'fax' => array('name'=>'fax', 'type'=>'xsd:string'),
111
		'email' => array('name'=>'email', 'type'=>'xsd:string'),
112
		'url' => array('name'=>'url', 'type'=>'xsd:string'),
113
		'profid1' => array('name'=>'profid1', 'type'=>'xsd:string'),
114
		'profid2' => array('name'=>'profid2', 'type'=>'xsd:string'),
115
		'profid3' => array('name'=>'profid3', 'type'=>'xsd:string'),
116
		'profid4' => array('name'=>'profid4', 'type'=>'xsd:string'),
117
		'profid5' => array('name'=>'profid5', 'type'=>'xsd:string'),
118
		'profid6' => array('name'=>'profid6', 'type'=>'xsd:string'),
119
		'capital' => array('name'=>'capital', 'type'=>'xsd:string'),
120
		'vat_used' => array('name'=>'vat_used', 'type'=>'xsd:string'),
121
		'vat_number' => array('name'=>'vat_number', 'type'=>'xsd:string'));
122
123
$elementtype = 'societe';
124
125
// Retrieve all extrafields for thirdsparty
126
// fetch optionals attributes and labels
127
$extrafields = new ExtraFields($db);
128
$extrafields->fetch_name_optionals_label($elementtype, true);
129
$extrafield_array = null;
130
if (is_array($extrafields) && count($extrafields) > 0) {
0 ignored issues
show
introduced by
The condition is_array($extrafields) is always false.
Loading history...
131
	$extrafield_array = array();
132
}
133
if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
134
{
135
	foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label)
136
	{
137
		//$value=$object->array_options["options_".$key];
138
		$type = $extrafields->attributes[$elementtype]['type'][$key];
139
		if ($type == 'date' || $type == 'datetime') {$type = 'xsd:dateTime'; }
140
		else {$type = 'xsd:string'; }
141
142
		$extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
143
	}
144
}
145
146
if (is_array($extrafield_array)) $thirdparty_fields = array_merge($thirdparty_fields, $extrafield_array);
0 ignored issues
show
introduced by
The condition is_array($extrafield_array) is always false.
Loading history...
147
148
// Define other specific objects
149
$server->wsdl->addComplexType(
150
	'thirdparty',
151
	'complexType',
152
	'struct',
153
	'all',
154
	'',
155
	$thirdparty_fields
156
);
157
158
// Define other specific objects
159
$server->wsdl->addComplexType(
160
	'filterthirdparty',
161
	'complexType',
162
	'struct',
163
	'all',
164
	'',
165
	array(
166
		//'limit' => array('name'=>'limit','type'=>'xsd:string'),
167
		'client' => array('name'=>'client', 'type'=>'xsd:string'),
168
		'supplier' => array('name'=>'supplier', 'type'=>'xsd:string'),
169
		'category' => array('name'=>'category', 'type'=>'xsd:string')
170
	)
171
);
172
173
$server->wsdl->addComplexType(
174
	'ThirdPartiesArray',
175
	'complexType',
176
	'array',
177
	'',
178
	'SOAP-ENC:Array',
179
	array(),
180
	array(
181
		array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:thirdparty[]')
182
	),
183
	'tns:thirdparty'
184
);
185
$server->wsdl->addComplexType(
186
	'ThirdPartiesArray2',
187
	'complexType',
188
	'array',
189
	'sequence',
190
	'',
191
	array(
192
		'thirdparty' => array(
193
			'name' => 'thirdparty',
194
			'type' => 'tns:thirdparty',
195
			'minOccurs' => '0',
196
			'maxOccurs' => 'unbounded'
197
		)
198
	)
199
);
200
201
202
// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
203
// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
204
// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
205
$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
206
$styleuse = 'encoded'; // encoded/literal/literal wrapped
207
// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
208
209
// Register WSDL
210
$server->register(
211
	'getThirdParty',
212
	// Entry values
213
	array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
214
	// Exit values
215
	array('result'=>'tns:result', 'thirdparty'=>'tns:thirdparty'),
216
	$ns,
217
	$ns.'#getThirdParty',
218
	$styledoc,
219
	$styleuse,
220
	'WS to get a thirdparty from its id, ref or ref_ext'
221
);
222
223
// Register WSDL
224
$server->register(
225
	'createThirdParty',
226
	// Entry values
227
	array('authentication'=>'tns:authentication', 'thirdparty'=>'tns:thirdparty'),
228
	// Exit values
229
	array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string'),
230
	$ns,
231
	$ns.'#createThirdParty',
232
	$styledoc,
233
	$styleuse,
234
	'WS to create a thirdparty'
235
);
236
237
// Register WSDL
238
$server->register(
239
	'updateThirdParty',
240
	// Entry values
241
	array('authentication'=>'tns:authentication', 'thirdparty'=>'tns:thirdparty'),
242
	// Exit values
243
	array('result'=>'tns:result', 'id'=>'xsd:string'),
244
	$ns,
245
	$ns.'#updateThirdParty',
246
	$styledoc,
247
	$styleuse,
248
	'WS to update a thirdparty'
249
);
250
251
252
// Register WSDL
253
$server->register(
254
	'getListOfThirdParties',
255
	// Entry values
256
	array('authentication'=>'tns:authentication', 'filterthirdparty'=>'tns:filterthirdparty'),
257
	// Exit values
258
	array('result'=>'tns:result', 'thirdparties'=>'tns:ThirdPartiesArray2'),
259
	$ns,
260
	$ns.'#getListOfThirdParties',
261
	$styledoc,
262
	$styleuse,
263
	'WS to get list of thirdparties id and ref'
264
);
265
266
// Register WSDL
267
$server->register(
268
		'deleteThirdParty',
269
		// Entry values
270
		array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
271
		// Exit values
272
		array('result'=>'tns:result', 'id'=>'xsd:string'),
273
		$ns,
274
		$ns.'#deleteThirdParty',
275
		$styledoc,
276
		$styleuse,
277
		'WS to delete a thirdparty from its id, ref or ref_ext'
278
);
279
280
281
// Full methods code
282
/**
283
 * Get a thirdparty
284
 *
285
 * @param	array		$authentication		Array of authentication information
286
 * @param	string		$id		    		internal id
287
 * @param	string		$ref		    	internal reference
288
 * @param	string		$ref_ext	   		external reference
289
 * @return	array							Array result
290
 */
291
function getThirdParty($authentication, $id = '', $ref = '', $ref_ext = '')
292
{
293
	global $db, $conf;
294
295
	dol_syslog("Function: getThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
296
297
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
298
299
	// Init and check authentication
300
	$objectresp = array();
301
	$errorcode = ''; $errorlabel = '';
302
	$error = 0;
303
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
304
	// Check parameters
305
	if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
306
	{
307
		$error++;
308
		$errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
309
	}
310
311
	if (!$error)
312
	{
313
		$fuser->getrights();
314
315
		if ($fuser->rights->societe->lire)
316
		{
317
			$thirdparty = new Societe($db);
318
			$result = $thirdparty->fetch($id, $ref, $ref_ext);
319
			if ($result > 0)
320
			{
321
				$thirdparty_result_fields = array(
322
						'id' => $thirdparty->id,
323
			   			'ref' => $thirdparty->name,
324
			   			'ref_ext' => $thirdparty->ref_ext,
325
			   			'status' => $thirdparty->status,
326
						'client' => $thirdparty->client,
327
						'supplier' => $thirdparty->fournisseur,
328
						'customer_code' => $thirdparty->code_client,
329
						'supplier_code' => $thirdparty->code_fournisseur,
330
						'customer_code_accountancy' => $thirdparty->code_compta,
331
						'supplier_code_accountancy' => $thirdparty->code_compta_fournisseur,
332
						'user_creation' => $thirdparty->user_creation,
333
						'date_creation' => dol_print_date($thirdparty->date_creation, 'dayhourrfc'),
334
						'user_modification' => $thirdparty->user_modification,
335
						'date_modification' => dol_print_date($thirdparty->date_modification, 'dayhourrfc'),
336
						'address' => $thirdparty->address,
337
						'zip' => $thirdparty->zip,
338
						'town' => $thirdparty->town,
339
						'province_id' => $thirdparty->state_id,
340
						'country_id' => $thirdparty->country_id,
341
						'country_code' => $thirdparty->country_code,
342
						'country' => $thirdparty->country,
343
						'phone' => $thirdparty->phone,
344
						'fax' => $thirdparty->fax,
345
						'email' => $thirdparty->email,
346
						'url' => $thirdparty->url,
347
						'profid1' => $thirdparty->idprof1,
348
						'profid2' => $thirdparty->idprof2,
349
						'profid3' => $thirdparty->idprof3,
350
						'profid4' => $thirdparty->idprof4,
351
						'profid5' => $thirdparty->idprof5,
352
						'profid6' => $thirdparty->idprof6,
353
						'capital' => $thirdparty->capital,
354
			   			'barcode' => $thirdparty->barcode,
355
						'vat_used' => $thirdparty->tva_assuj,
356
						'vat_number' => $thirdparty->tva_intra,
357
						'note_private' => $thirdparty->note_private,
358
						'note_public' => $thirdparty->note_public);
359
360
				$elementtype = 'societe';
361
362
				// Retrieve all extrafields for thirdsparty
363
				// fetch optionals attributes and labels
364
				$extrafields = new ExtraFields($db);
365
				$extrafields->fetch_name_optionals_label($elementtype, true);
366
				//Get extrafield values
367
				$thirdparty->fetch_optionals();
368
369
				if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
370
				{
371
					foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label)
372
					{
373
						if (isset($thirdparty->array_options['options_'.$key])) {
374
							$thirdparty_result_fields = array_merge($thirdparty_result_fields, array('options_'.$key => $thirdparty->array_options['options_'.$key]));
375
						}
376
					}
377
				}
378
379
				// Create
380
				$objectresp = array(
381
					'result'=>array('result_code'=>'OK', 'result_label'=>''),
382
					'thirdparty'=>$thirdparty_result_fields);
383
			}
384
			else {
385
				$error++;
386
				$errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
387
			}
388
		}
389
		else {
390
			$error++;
391
			$errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
392
		}
393
	}
394
395
	if ($error)
396
	{
397
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
398
	}
399
400
	return $objectresp;
401
}
402
403
404
405
/**
406
 * Create a thirdparty
407
 *
408
 * @param	array		$authentication		Array of authentication information
409
 * @param	Societe		$thirdparty		    Thirdparty
410
 * @return	array							Array result
411
 */
412
function createThirdParty($authentication, $thirdparty)
413
{
414
	global $db, $conf;
415
416
	$now = dol_now();
417
418
	dol_syslog("Function: createThirdParty login=".$authentication['login']);
419
420
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
421
422
	// Init and check authentication
423
	$objectresp = array();
424
	$errorcode = ''; $errorlabel = '';
425
	$error = 0;
426
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
427
	// Check parameters
428
	if (empty($thirdparty['ref']))
429
	{
430
		$error++; $errorcode = 'KO'; $errorlabel = "Name is mandatory.";
431
	}
432
433
434
	if (!$error)
435
	{
436
		include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
437
438
		$newobject = new Societe($db);
439
		$newobject->ref = $thirdparty['ref'];
440
		$newobject->name = $thirdparty['ref'];
441
		$newobject->ref_ext = $thirdparty['ref_ext'];
442
		$newobject->status = $thirdparty['status'];
443
		$newobject->client = $thirdparty['client'];
444
		$newobject->fournisseur = $thirdparty['supplier'];
445
		$newobject->code_client = $thirdparty['customer_code'];
446
		$newobject->code_fournisseur = $thirdparty['supplier_code'];
447
		$newobject->code_compta = $thirdparty['customer_code_accountancy'];
448
		$newobject->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
449
		$newobject->date_creation = $now;
450
		$newobject->note_private = $thirdparty['note_private'];
451
		$newobject->note_public = $thirdparty['note_public'];
452
		$newobject->address = $thirdparty['address'];
453
		$newobject->zip = $thirdparty['zip'];
454
		$newobject->town = $thirdparty['town'];
455
456
		$newobject->country_id = $thirdparty['country_id'];
457
		if ($thirdparty['country_code']) $newobject->country_id = getCountry($thirdparty['country_code'], 3);
458
		$newobject->province_id = $thirdparty['province_id'];
0 ignored issues
show
Bug introduced by
The property province_id does not seem to exist on Societe.
Loading history...
459
		//if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
460
461
		$newobject->phone = $thirdparty['phone'];
462
		$newobject->fax = $thirdparty['fax'];
463
		$newobject->email = $thirdparty['email'];
464
		$newobject->url = $thirdparty['url'];
465
		$newobject->idprof1 = $thirdparty['profid1'];
466
		$newobject->idprof2 = $thirdparty['profid2'];
467
		$newobject->idprof3 = $thirdparty['profid3'];
468
		$newobject->idprof4 = $thirdparty['profid4'];
469
		$newobject->idprof5 = $thirdparty['profid5'];
470
		$newobject->idprof6 = $thirdparty['profid6'];
471
472
		$newobject->capital = $thirdparty['capital'];
473
474
		$newobject->barcode = empty($thirdparty['barcode']) ? '' : $thirdparty['barcode'];
475
		$newobject->tva_assuj = empty($thirdparty['vat_used']) ? 0 : $thirdparty['vat_used'];
476
		$newobject->tva_intra = empty($thirdparty['vat_number']) ? '' : $thirdparty['vat_number'];
477
478
		$newobject->canvas = empty($thirdparty['canvas']) ? '' : $thirdparty['canvas'];
479
		$newobject->particulier = empty($thirdparty['individual']) ? 0 : $thirdparty['individual'];
480
481
		$elementtype = 'societe';
482
483
		// Retrieve all extrafields for thirdsparty
484
		// fetch optionals attributes and labels
485
		$extrafields = new ExtraFields($db);
486
		$extrafields->fetch_name_optionals_label($elementtype, true);
487
		if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
488
		{
489
			foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label)
490
			{
491
				$key = 'options_'.$key;
492
				if (isset($thirdparty[$key])) {
493
					$newobject->array_options[$key] = $thirdparty[$key];
494
				}
495
			}
496
		}
497
498
		$db->begin();
499
500
		$result = $newobject->create($fuser);
501
		if ($newobject->particulier && $result > 0) {
502
			$newobject->firstname = $thirdparty['firstname'];
503
			$newobject->name_bis = $thirdparty['lastname'];
504
			$result = $newobject->create_individual($fuser);
505
		}
506
		if ($result <= 0)
507
		{
508
			$error++;
509
		}
510
511
		if (!$error)
512
		{
513
			$db->commit();
514
515
			// Patch to add capability to associate (one) sale representative
516
			if (!empty($thirdparty['commid']) && $thirdparty['commid'] > 0)
517
				$newobject->add_commercial($fuser, $thirdparty["commid"]);
518
519
			$objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
520
		}
521
		else {
522
			$db->rollback();
523
			$error++;
524
			$errorcode = 'KO';
525
			$errorlabel = $newobject->error;
526
		}
527
	}
528
529
	if ($error)
530
	{
531
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
532
	}
533
534
	return $objectresp;
535
}
536
537
/**
538
 * Update a thirdparty
539
 *
540
 * @param	array		$authentication		Array of authentication information
541
 * @param	Societe		$thirdparty		    Thirdparty
542
 * @return	array							Array result
543
 */
544
function updateThirdParty($authentication, $thirdparty)
545
{
546
	global $db, $conf;
547
548
	$now = dol_now();
549
550
	dol_syslog("Function: updateThirdParty login=".$authentication['login']);
551
552
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
553
554
	// Init and check authentication
555
	$objectresp = array();
556
	$errorcode = ''; $errorlabel = '';
557
	$error = 0;
558
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
559
	// Check parameters
560
	if (empty($thirdparty['id'])) {
561
		$error++; $errorcode = 'KO'; $errorlabel = "Thirdparty id is mandatory.";
562
	}
563
564
	if (!$error)
565
	{
566
		$objectfound = false;
567
568
		include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
569
570
		$object = new Societe($db);
571
		$result = $object->fetch($thirdparty['id']);
572
573
		if (!empty($object->id)) {
574
			$objectfound = true;
575
576
			$object->ref = $thirdparty['ref'];
577
			$object->name = $thirdparty['ref'];
578
			$object->ref_ext = $thirdparty['ref_ext'];
579
			$object->status = $thirdparty['status'];
580
			$object->client = $thirdparty['client'];
581
			$object->fournisseur = $thirdparty['supplier'];
582
			$object->code_client = $thirdparty['customer_code'];
583
			$object->code_fournisseur = $thirdparty['supplier_code'];
584
			$object->code_compta = $thirdparty['customer_code_accountancy'];
585
			$object->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
586
			$object->date_creation = $now;
587
			$object->note_private = $thirdparty['note_private'];
588
			$object->note_public = $thirdparty['note_public'];
589
			$object->address = $thirdparty['address'];
590
			$object->zip = $thirdparty['zip'];
591
			$object->town = $thirdparty['town'];
592
593
			$object->country_id = $thirdparty['country_id'];
594
			if ($thirdparty['country_code']) $object->country_id = getCountry($thirdparty['country_code'], 3);
595
			$object->province_id = $thirdparty['province_id'];
0 ignored issues
show
Bug introduced by
The property province_id does not seem to exist on Societe.
Loading history...
596
			//if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
597
598
			$object->phone = $thirdparty['phone'];
599
			$object->fax = $thirdparty['fax'];
600
			$object->email = $thirdparty['email'];
601
			$object->url = $thirdparty['url'];
602
			$object->idprof1 = $thirdparty['profid1'];
603
			$object->idprof2 = $thirdparty['profid2'];
604
			$object->idprof3 = $thirdparty['profid3'];
605
			$object->idprof4 = $thirdparty['profid4'];
606
			$object->idprof5 = $thirdparty['profid5'];
607
			$object->idprof6 = $thirdparty['profid6'];
608
609
			$object->capital = $thirdparty['capital'];
610
611
			$object->barcode = $thirdparty['barcode'];
612
			$object->tva_assuj = $thirdparty['vat_used'];
613
			$object->tva_intra = $thirdparty['vat_number'];
614
615
			$object->canvas = $thirdparty['canvas'];
616
617
			$elementtype = 'societe';
618
619
			// Retrieve all extrafields for thirdsparty
620
			// fetch optionals attributes and labels
621
			$extrafields = new ExtraFields($db);
622
			$extrafields->fetch_name_optionals_label($elementtype, true);
623
			if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
624
			{
625
				foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label)
626
				{
627
					$key = 'options_'.$key;
628
					if (isset($thirdparty[$key])) {
629
						$object->array_options[$key] = $thirdparty[$key];
630
					}
631
				}
632
			}
633
634
			$db->begin();
635
636
			$result = $object->update($thirdparty['id'], $fuser);
637
			if ($result <= 0) {
638
				$error++;
639
			}
640
		}
641
642
		if ((!$error) && ($objectfound))
643
		{
644
			$db->commit();
645
			$objectresp = array(
646
					'result'=>array('result_code'=>'OK', 'result_label'=>''),
647
					'id'=>$object->id
648
			);
649
		}
650
		elseif ($objectfound)
651
		{
652
			$db->rollback();
653
			$error++;
654
			$errorcode = 'KO';
655
			$errorlabel = $object->error;
656
		} else {
657
			$error++;
658
			$errorcode = 'NOT_FOUND';
659
			$errorlabel = 'Thirdparty id='.$thirdparty['id'].' cannot be found';
660
		}
661
	}
662
663
	if ($error)
664
	{
665
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
666
	}
667
668
	return $objectresp;
669
}
670
671
672
673
/**
674
 * getListOfThirdParties
675
 *
676
 * @param	array		$authentication		Array of authentication information
677
 * @param	array		$filterthirdparty	Filter fields (key=>value to filer on. For example 'client'=>2, 'supplier'=>1, 'category'=>idcateg, 'name'=>'searchstring', ...)
678
 * @return	array							Array result
679
 */
680
function getListOfThirdParties($authentication, $filterthirdparty)
681
{
682
	global $db, $conf;
683
684
	dol_syslog("Function: getListOfThirdParties login=".$authentication['login']);
685
686
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
687
688
	// Init and check authentication
689
	$objectresp = array();
690
	$arraythirdparties = array();
691
692
	$errorcode = ''; $errorlabel = '';
693
	$error = 0;
694
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
695
	// Check parameters
696
697
	if (!$error)
698
	{
699
		$sql  = "SELECT s.rowid as socRowid, s.nom as ref, s.ref_ext, s.address, s.zip, s.town, c.label as country, s.phone, s.fax, s.url, extra.*";
700
		$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
701
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON s.fk_pays = c.rowid";
702
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as extra ON s.rowid=fk_object";
703
704
		$sql .= " WHERE entity=".$conf->entity;
705
		foreach ($filterthirdparty as $key => $val)
706
		{
707
			if ($key == 'name' && $val != '')  $sql .= " AND s.name LIKE '%".$db->escape($val)."%'";
708
			if ($key == 'client' && (int) $val > 0)  $sql .= " AND s.client = ".$db->escape($val);
709
			if ($key == 'supplier' && (int) $val > 0)  $sql .= " AND s.fournisseur = ".$db->escape($val);
710
			if ($key == 'category' && (int) $val > 0)  $sql .= " AND s.rowid IN (SELECT fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe WHERE fk_categorie=".$db->escape($val).") ";
711
		}
712
		dol_syslog("Function: getListOfThirdParties", LOG_DEBUG);
713
714
		$elementtype = 'societe';
715
716
		$extrafields = new ExtraFields($db);
717
		$extrafields->fetch_name_optionals_label($elementtype, true);
718
719
720
		$resql = $db->query($sql);
721
		if ($resql)
722
		{
723
			$num = $db->num_rows($resql);
724
725
			$i = 0;
726
			while ($i < $num)
727
			{
728
				$extrafieldsOptions = array();
729
				$obj = $db->fetch_object($resql);
730
731
				if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label']))
732
				{
733
					foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label)
734
					{
735
						if (isset($obj->{$key})) {
736
							$extrafieldsOptions['options_'.$key] = $obj->{$key};
737
						}
738
					}
739
				}
740
741
				$arraythirdparties[] = array('id'=>$obj->socRowid,
742
					'ref'=>$obj->ref,
743
					'ref_ext'=>$obj->ref_ext,
744
					'adress'=>$obj->adress,
745
					'zip'=>$obj->zip,
746
					'town'=>$obj->town,
747
					'country'=>$obj->country,
748
					'phone'=>$obj->phone,
749
					'fax'=>$obj->fax,
750
					'url'=>$obj->url
751
				);
752
				$arraythirdparties[$i] = array_merge($arraythirdparties[$i], $extrafieldsOptions);
753
754
				$i++;
755
			}
756
		}
757
		else {
758
			$error++;
759
			$errorcode = $db->lasterrno();
760
			$errorlabel = $db->lasterror();
761
		}
762
	}
763
764
	if ($error)
765
	{
766
		$objectresp = array(
767
			'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
768
			'thirdparties'=>$arraythirdparties
769
		);
770
	}
771
	else {
772
		$objectresp = array(
773
			'result'=>array('result_code' => 'OK', 'result_label' => ''),
774
			'thirdparties'=>$arraythirdparties
775
		);
776
	}
777
778
	return $objectresp;
779
}
780
781
/**
782
 * Delete a thirdparty
783
 *
784
 * @param	array		$authentication		Array of authentication information
785
 * @param	string		$id		    		internal id
786
 * @param	string		$ref		    	internal reference
787
 * @param	string		$ref_ext	   		external reference
788
 * @return	array							Array result
789
 */
790
function deleteThirdParty($authentication, $id = '', $ref = '', $ref_ext = '')
791
{
792
	global $db, $conf;
793
794
	dol_syslog("Function: deleteThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
795
796
	if ($authentication['entity']) $conf->entity = $authentication['entity'];
797
798
	// Init and check authentication
799
	$objectresp = array();
800
	$errorcode = ''; $errorlabel = '';
801
	$error = 0;
802
	$fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
803
	// Check parameters
804
	if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
805
	{
806
		dol_syslog("Function: deleteThirdParty checkparam");
807
		$error++;
808
		$errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
809
	}
810
	dol_syslog("Function: deleteThirdParty 1");
811
812
	if (!$error)
813
	{
814
		$fuser->getrights();
815
816
		if ($fuser->rights->societe->lire && $fuser->rights->societe->supprimer)
817
		{
818
			$thirdparty = new Societe($db);
819
			$result = $thirdparty->fetch($id, $ref, $ref_ext);
820
821
			if ($result > 0)
822
			{
823
				$db->begin();
824
825
				$result = $thirdparty->delete($thirdparty->id, $fuser);
826
827
				if ($result > 0)
828
				{
829
					$db->commit();
830
831
					$objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
832
				}
833
				else {
834
					$db->rollback();
835
					$error++;
836
					$errorcode = 'KO';
837
					$errorlabel = $thirdparty->error;
838
					dol_syslog("Function: deleteThirdParty cant delete");
839
				}
840
			}
841
			else {
842
				$error++;
843
				$errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
844
			}
845
		}
846
		else {
847
			$error++;
848
			$errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
849
		}
850
	}
851
852
	if ($error)
853
	{
854
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
855
	}
856
857
	return $objectresp;
858
}
859
860
// Return the results.
861
$server->service(file_get_contents("php://input"));
862