dolibarr 23.0.3
server_thirdparty.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25if (!defined('NOCSRFCHECK')) {
26 define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
27}
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
39}
40if (!defined("NOLOGIN")) {
41 define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
42}
43if (!defined("NOSESSION")) {
44 define("NOSESSION", '1');
45}
46
47require '../main.inc.php';
48require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
49require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
50require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
51
52require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
53require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
54
60dol_syslog("Call Dolibarr webservices interfaces");
61
62$langs->load("main");
63
64// Enable and test if module web services is enabled
65if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
66 $langs->load("admin");
67 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
68 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
69 print $langs->trans("ToActivateModule");
70 exit;
71}
72
73// Create the soap Object
74$server = new nusoap_server();
75$server->soap_defencoding = 'UTF-8';
76$server->decode_utf8 = false;
77$ns = 'http://www.dolibarr.org/ns/';
78$server->configureWSDL('WebServicesDolibarrThirdParty', $ns);
79// @phan-suppress-next-line PhanUndeclaredProperty
80$server->wsdl->schemaTargetNamespace = $ns;
81
82
83// Define WSDL Authentication object
84$server->wsdl->addComplexType(
85 'authentication',
86 'complexType',
87 'struct',
88 'all',
89 '',
90 array(
91 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
92 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
93 'login' => array('name' => 'login', 'type' => 'xsd:string'),
94 'password' => array('name' => 'password', 'type' => 'xsd:string'),
95 'entity' => array('name' => 'entity', 'type' => 'xsd:string'),
96 )
97);
98// Define WSDL Return object
99$server->wsdl->addComplexType(
100 'result',
101 'complexType',
102 'struct',
103 'all',
104 '',
105 array(
106 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
107 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
108 )
109);
110
111
112$thirdparty_fields = array(
113 'id' => array('name' => 'id', 'type' => 'xsd:string'),
114 'ref' => array('name' => 'name', 'type' => 'xsd:string'),
115 'ref_ext' => array('name' => 'ref_ext', 'type' => 'xsd:string'),
116 'fk_user_author' => array('name' => 'fk_user_author', 'type' => 'xsd:string'),
117 'status' => array('name' => 'status', 'type' => 'xsd:string'),
118 'client' => array('name' => 'client', 'type' => 'xsd:string'),
119 'supplier' => array('name' => 'supplier', 'type' => 'xsd:string'),
120 'customer_code' => array('name' => 'customer_code', 'type' => 'xsd:string'),
121 'supplier_code' => array('name' => 'supplier_code', 'type' => 'xsd:string'),
122 'customer_code_accountancy' => array('name' => 'customer_code_accountancy', 'type' => 'xsd:string'),
123 'supplier_code_accountancy' => array('name' => 'supplier_code_accountancy', 'type' => 'xsd:string'),
124 'date_creation' => array('name' => 'date_creation', 'type' => 'xsd:dateTime'),
125 'date_modification' => array('name' => 'date_modification', 'type' => 'xsd:dateTime'),
126 'note_private' => array('name' => 'note_private', 'type' => 'xsd:string'),
127 'note_public' => array('name' => 'note_public', 'type' => 'xsd:string'),
128 'address' => array('name' => 'address', 'type' => 'xsd:string'),
129 'zip' => array('name' => 'zip', 'type' => 'xsd:string'),
130 'town' => array('name' => 'town', 'type' => 'xsd:string'),
131 'region_code' => array('name' => 'region_code', 'type' => 'xsd:string'),
132 'country_id' => array('name' => 'country_id', 'type' => 'xsd:string'),
133 'country_code' => array('name' => 'country_code', 'type' => 'xsd:string'),
134 'country' => array('name' => 'country', 'type' => 'xsd:string'),
135 'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
136 'fax' => array('name' => 'fax', 'type' => 'xsd:string'),
137 'email' => array('name' => 'email', 'type' => 'xsd:string'),
138 'url' => array('name' => 'url', 'type' => 'xsd:string'),
139 'profid1' => array('name' => 'profid1', 'type' => 'xsd:string'),
140 'profid2' => array('name' => 'profid2', 'type' => 'xsd:string'),
141 'profid3' => array('name' => 'profid3', 'type' => 'xsd:string'),
142 'profid4' => array('name' => 'profid4', 'type' => 'xsd:string'),
143 'profid5' => array('name' => 'profid5', 'type' => 'xsd:string'),
144 'profid6' => array('name' => 'profid6', 'type' => 'xsd:string'),
145 'capital' => array('name' => 'capital', 'type' => 'xsd:string'),
146 'vat_used' => array('name' => 'vat_used', 'type' => 'xsd:string'),
147 'vat_number' => array('name' => 'vat_number', 'type' => 'xsd:string'));
148
149$elementtype = 'societe';
150
151// Retrieve all extrafields for thirdparties
152// fetch optionals attributes and labels
153$extrafields = new ExtraFields($db);
154$extrafields->fetch_name_optionals_label($elementtype, true);
155$extrafield_array = null;
156if (is_array($extrafields->attributes) && $extrafields->attributes[$elementtype]['count'] > 0) {
157 $extrafield_array = array();
158}
159if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
160 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
161 //$value=$object->array_options["options_".$key];
162 $type = $extrafields->attributes[$elementtype]['type'][$key];
163 if ($type == 'date' || $type == 'datetime') {
164 $type = 'xsd:dateTime';
165 } else {
166 $type = 'xsd:string';
167 }
168
169 $extrafield_array['options_'.$key] = array('name' => 'options_'.$key, 'type' => $type);
170 }
171}
172
173if (is_array($extrafield_array)) {
174 $thirdparty_fields = array_merge($thirdparty_fields, $extrafield_array);
175}
176
177// Define other specific objects
178$server->wsdl->addComplexType(
179 'thirdparty',
180 'complexType',
181 'struct',
182 'all',
183 '',
184 $thirdparty_fields
185);
186
187// Define other specific objects
188$server->wsdl->addComplexType(
189 'filterthirdparty',
190 'complexType',
191 'struct',
192 'all',
193 '',
194 array(
195 //'limit' => array('name'=>'limit','type'=>'xsd:string'),
196 'client' => array('name' => 'client', 'type' => 'xsd:string'),
197 'supplier' => array('name' => 'supplier', 'type' => 'xsd:string'),
198 'category' => array('name' => 'category', 'type' => 'xsd:string')
199 )
200);
201
202$server->wsdl->addComplexType(
203 'ThirdPartiesArray',
204 'complexType',
205 'array',
206 '',
207 'SOAP-ENC:Array',
208 array(),
209 array(
210 array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:thirdparty[]')
211 ),
212 'tns:thirdparty'
213);
214$server->wsdl->addComplexType(
215 'ThirdPartiesArray2',
216 'complexType',
217 'array',
218 'sequence',
219 '',
220 array(
221 'thirdparty' => array(
222 'name' => 'thirdparty',
223 'type' => 'tns:thirdparty',
224 'minOccurs' => '0',
225 'maxOccurs' => 'unbounded'
226 )
227 )
228);
229
230
231// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
232// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
233// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
234$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
235$styleuse = 'encoded'; // encoded/literal/literal wrapped
236// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
237
238// Register WSDL
239$server->register(
240 'getThirdParty',
241 // Entry values
242 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string', 'barcode' => 'xsd:string', 'profid1' => 'xsd:string', 'profid2' => 'xsd:string'),
243 // Exit values
244 array('result' => 'tns:result', 'thirdparty' => 'tns:thirdparty'),
245 $ns,
246 $ns.'#getThirdParty',
247 $styledoc,
248 $styleuse,
249 'WS to get a thirdparty from its id, ref or ref_ext'
250);
251
252// Register WSDL
253$server->register(
254 'createThirdParty',
255 // Entry values
256 array('authentication' => 'tns:authentication', 'thirdparty' => 'tns:thirdparty'),
257 // Exit values
258 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string'),
259 $ns,
260 $ns.'#createThirdParty',
261 $styledoc,
262 $styleuse,
263 'WS to create a thirdparty'
264);
265
266// Register WSDL
267$server->register(
268 'updateThirdParty',
269 // Entry values
270 array('authentication' => 'tns:authentication', 'thirdparty' => 'tns:thirdparty'),
271 // Exit values
272 array('result' => 'tns:result', 'id' => 'xsd:string'),
273 $ns,
274 $ns.'#updateThirdParty',
275 $styledoc,
276 $styleuse,
277 'WS to update a thirdparty'
278);
279
280
281// Register WSDL
282$server->register(
283 'getListOfThirdParties',
284 // Entry values
285 array('authentication' => 'tns:authentication', 'filterthirdparty' => 'tns:filterthirdparty'),
286 // Exit values
287 array('result' => 'tns:result', 'thirdparties' => 'tns:ThirdPartiesArray2'),
288 $ns,
289 $ns.'#getListOfThirdParties',
290 $styledoc,
291 $styleuse,
292 'WS to get list of thirdparties id and ref'
293);
294
295// Register WSDL
296$server->register(
297 'deleteThirdParty',
298 // Entry values
299 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
300 // Exit values
301 array('result' => 'tns:result', 'id' => 'xsd:string'),
302 $ns,
303 $ns.'#deleteThirdParty',
304 $styledoc,
305 $styleuse,
306 'WS to delete a thirdparty from its id, ref or ref_ext'
307);
308
309
310// Full methods code
323function getThirdParty($authentication, $id = '', $ref = '', $ref_ext = '', $barcode = '', $profid1 = '', $profid2 = '')
324{
325 global $db, $conf;
326
327 dol_syslog("Function: getThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext." barcode=".$barcode." profid1=".$profid1." profid2=".$profid2);
328
329 if ($authentication['entity']) {
330 $conf->entity = $authentication['entity'];
331 }
332
333 // Init and check authentication
334 $objectresp = array();
335 $errorcode = '';
336 $errorlabel = '';
337 $error = 0;
338 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
339 // Check parameters
340 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
341 $error++;
342 $errorcode = 'BAD_PARAMETERS';
343 $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
344 }
345
346 if (!$error) {
347 $fuser->loadRights();
348
349 if ($fuser->hasRight('societe', 'lire')) {
350 $thirdparty = new Societe($db);
351 $result = $thirdparty->fetch((int) $id, $ref, $ref_ext, $barcode, $profid1, $profid2);
352 if ($result > 0) {
353 $thirdparty_result_fields = array(
354 'id' => $thirdparty->id,
355 'ref' => $thirdparty->name,
356 'ref_ext' => $thirdparty->ref_ext,
357 'status' => $thirdparty->status,
358 'client' => $thirdparty->client,
359 'supplier' => $thirdparty->fournisseur,
360 'customer_code' => $thirdparty->code_client,
361 'supplier_code' => $thirdparty->code_fournisseur,
362 'customer_code_accountancy' => $thirdparty->code_compta_client,
363 'supplier_code_accountancy' => $thirdparty->code_compta_fournisseur,
364 'user_creation_id' => $thirdparty->user_creation_id,
365 'date_creation' => dol_print_date($thirdparty->date_creation, 'dayhourrfc'),
366 'user_modification_id' => $thirdparty->user_modification_id,
367 'date_modification' => dol_print_date($thirdparty->date_modification, 'dayhourrfc'),
368 'address' => $thirdparty->address,
369 'zip' => $thirdparty->zip,
370 'town' => $thirdparty->town,
371 'region_code' => $thirdparty->region_code,
372 'country_id' => $thirdparty->country_id,
373 'country_code' => $thirdparty->country_code,
374 'country' => $thirdparty->country,
375 'phone' => $thirdparty->phone,
376 'fax' => $thirdparty->fax,
377 'email' => $thirdparty->email,
378 'url' => $thirdparty->url,
379 'profid1' => $thirdparty->idprof1,
380 'profid2' => $thirdparty->idprof2,
381 'profid3' => $thirdparty->idprof3,
382 'profid4' => $thirdparty->idprof4,
383 'profid5' => $thirdparty->idprof5,
384 'profid6' => $thirdparty->idprof6,
385 'capital' => $thirdparty->capital,
386 'barcode' => $thirdparty->barcode,
387 'vat_used' => $thirdparty->tva_assuj,
388 'vat_number' => $thirdparty->tva_intra,
389 'note_private' => $thirdparty->note_private,
390 'note_public' => $thirdparty->note_public);
391
392 $elementtype = 'societe';
393
394 // Retrieve all extrafields for thirdsparty
395 // fetch optionals attributes and labels
396 $extrafields = new ExtraFields($db);
397 $extrafields->fetch_name_optionals_label($elementtype, true);
398 //Get extrafield values
399 $thirdparty->fetch_optionals();
400
401 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
402 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
403 if (isset($thirdparty->array_options['options_'.$key])) {
404 $thirdparty_result_fields = array_merge($thirdparty_result_fields, array('options_'.$key => $thirdparty->array_options['options_'.$key]));
405 }
406 }
407 }
408
409 // Create
410 $objectresp = array(
411 'result' => array('result_code' => 'OK', 'result_label' => ''),
412 'thirdparty' => $thirdparty_result_fields);
413 } elseif ($result == -2) {
414 $error++;
415 $errorcode = 'DUPLICATE_FOUND';
416 $errorlabel = 'Object found several times for id='.$id.' or ref='.$ref.' or ref_ext='.$ref_ext;
417 } else {
418 $error++;
419 $errorcode = 'NOT_FOUND';
420 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
421 }
422 } else {
423 $error++;
424 $errorcode = 'PERMISSION_DENIED';
425 $errorlabel = 'User does not have permission for this request';
426 }
427 }
428
429 if ($error) {
430 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
431 }
432
433 return $objectresp;
434}
435
436
437
445function createThirdParty($authentication, $thirdparty)
446{
447 global $db, $conf;
448
449 $now = dol_now();
450
451 dol_syslog("Function: createThirdParty login=".$authentication['login']);
452
453 if ($authentication['entity']) {
454 $conf->entity = $authentication['entity'];
455 }
456
457 // Init and check authentication
458 $objectresp = array();
459 $errorcode = '';
460 $errorlabel = '';
461 $error = 0;
462 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
463 // Check parameters
464 if (empty($thirdparty['ref'])) {
465 $error++;
466 $errorcode = 'KO';
467 $errorlabel = "Name is mandatory.";
468 }
469
470
471 if (!$error) {
472 include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
473
474 $newobject = new Societe($db);
475 $newobject->ref = $thirdparty['ref'];
476 $newobject->name = $thirdparty['ref'];
477 $newobject->ref_ext = $thirdparty['ref_ext'];
478 $newobject->status = (int) $thirdparty['status'];
479 $newobject->client = (int) $thirdparty['client'];
480 $newobject->fournisseur = (int) $thirdparty['supplier'];
481 $newobject->code_client = $thirdparty['customer_code'];
482 $newobject->code_fournisseur = $thirdparty['supplier_code'];
483 $newobject->code_compta = $thirdparty['customer_code_accountancy'];
484 $newobject->code_compta_client = $thirdparty['customer_code_accountancy'];
485 $newobject->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
486 $newobject->date_creation = $now;
487 $newobject->note_private = $thirdparty['note_private'];
488 $newobject->note_public = $thirdparty['note_public'];
489 $newobject->address = $thirdparty['address'];
490 $newobject->zip = $thirdparty['zip'];
491 $newobject->town = $thirdparty['town'];
492
493 $newobject->country_id = (int) $thirdparty['country_id'];
494 if ($thirdparty['country_code']) {
495 $newobject->country_id = getCountry($thirdparty['country_code'], '3');
496 }
497 $newobject->region_code = empty($thirdparty['region_code']) ? '' : $thirdparty['region_code'];
498 //if ($thirdparty['province_code']) $newobject->province_code = getCountry($thirdparty['province_code'], '3');
499
500 $newobject->phone = $thirdparty['phone'];
501 $newobject->fax = $thirdparty['fax'];
502 $newobject->email = $thirdparty['email'];
503 $newobject->url = $thirdparty['url'];
504 $newobject->idprof1 = $thirdparty['profid1'];
505 $newobject->idprof2 = $thirdparty['profid2'];
506 $newobject->idprof3 = $thirdparty['profid3'];
507 $newobject->idprof4 = $thirdparty['profid4'];
508 $newobject->idprof5 = $thirdparty['profid5'];
509 $newobject->idprof6 = $thirdparty['profid6'];
510
511 $newobject->capital = (float) $thirdparty['capital'];
512
513 $newobject->barcode = !isset($thirdparty['barcode']) ? '' : $thirdparty['barcode'];
514 $newobject->tva_assuj = empty($thirdparty['vat_used']) ? 0 : $thirdparty['vat_used'];
515 $newobject->tva_intra = empty($thirdparty['vat_number']) ? '' : $thirdparty['vat_number'];
516
517 $newobject->canvas = !isset($thirdparty['canvas']) ? '' : $thirdparty['canvas'];
518 $newobject->particulier = empty($thirdparty['individual']) ? 0 : $thirdparty['individual'];
519
520 $elementtype = 'societe';
521
522 // Retrieve all extrafields for thirdsparty
523 // fetch optionals attributes and labels
524 $extrafields = new ExtraFields($db);
525 $extrafields->fetch_name_optionals_label($elementtype, true);
526 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
527 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
528 $key = 'options_'.$key;
529 if (isset($thirdparty[$key])) {
530 $newobject->array_options[$key] = $thirdparty[$key];
531 }
532 }
533 }
534
535 $db->begin();
536
537 $result = $newobject->create($fuser);
538 if ($newobject->particulier && $result > 0) {
539 $newobject->firstname = isset($thirdparty['firstname']) ? $thirdparty['firstname'] : '';
540 $newobject->name_bis = isset($thirdparty['lastname']) ? $thirdparty['lastname'] : '';
541 $result = $newobject->create_individual($fuser);
542 }
543 if ($result <= 0) {
544 $error++;
545 }
546
547 if (!$error) {
548 $db->commit();
549
550 // Patch to add capability to associate (one) sales representative
551 if (isset($thirdparty['commid']) && $thirdparty['commid'] > 0) { // @phan-suppress-current-line PhanTypeInvalidDimOffset
552 $newobject->add_commercial($fuser, $thirdparty['commid']);
553 }
554
555 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref);
556 } else {
557 $db->rollback();
558 $error++;
559 $errorcode = 'KO';
560 $errorlabel = $newobject->error;
561 }
562 }
563
564 if ($error) {
565 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
566 }
567
568 return $objectresp;
569}
570
578function updateThirdParty($authentication, $thirdparty)
579{
580 global $db, $conf;
581
582 $now = dol_now();
583
584 dol_syslog("Function: updateThirdParty login=".$authentication['login']);
585
586 if ($authentication['entity']) {
587 $conf->entity = $authentication['entity'];
588 }
589
590 // Init and check authentication
591 $objectresp = array();
592 $errorcode = '';
593 $errorlabel = '';
594 $error = 0;
595 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
596 // Check parameters
597 if (empty($thirdparty['id'])) {
598 $error++;
599 $errorcode = 'KO';
600 $errorlabel = "Thirdparty id is mandatory.";
601 }
602
603 if (!$error) {
604 $objectfound = false;
605
606 include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
607
608 $object = new Societe($db);
609 $result = $object->fetch((int) $thirdparty['id']);
610
611 if (!empty($object->id)) {
612 $objectfound = true;
613
614 $object->ref = $thirdparty['ref'];
615 $object->name = $thirdparty['ref'];
616 $object->ref_ext = $thirdparty['ref_ext'];
617 $object->status = (int) $thirdparty['status'];
618 $object->client = (int) $thirdparty['client'];
619 $object->fournisseur = (int) $thirdparty['supplier'];
620 $object->code_client = $thirdparty['customer_code'];
621 $object->code_fournisseur = $thirdparty['supplier_code'];
622 $object->code_compta = $thirdparty['customer_code_accountancy'];
623 $object->code_compta_client = $thirdparty['customer_code_accountancy'];
624 $object->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
625 $object->date_creation = $now;
626 $object->note_private = $thirdparty['note_private'];
627 $object->note_public = $thirdparty['note_public'];
628 $object->address = $thirdparty['address'];
629 $object->zip = $thirdparty['zip'];
630 $object->town = $thirdparty['town'];
631
632 $object->country_id = (int) $thirdparty['country_id'];
633 if ($thirdparty['country_code']) {
634 $object->country_id = getCountry($thirdparty['country_code'], '3');
635 }
636 $object->region_code = $thirdparty['region_code'];
637 //if ($thirdparty['province_code']) $newobject->province_code = getCountry($thirdparty['province_code'], '3');
638
639 $object->phone = $thirdparty['phone'];
640 $object->fax = $thirdparty['fax'];
641 $object->email = $thirdparty['email'];
642 $object->url = $thirdparty['url'];
643 $object->idprof1 = $thirdparty['profid1'];
644 $object->idprof2 = $thirdparty['profid2'];
645 $object->idprof3 = $thirdparty['profid3'];
646 $object->idprof4 = $thirdparty['profid4'];
647 $object->idprof5 = $thirdparty['profid5'];
648 $object->idprof6 = $thirdparty['profid6'];
649
650 $object->capital = (float) $thirdparty['capital'];
651
652 $object->barcode = !isset($thirdparty['barcode']) ? '' : $thirdparty['barcode'];
653 $object->tva_assuj = (int) $thirdparty['vat_used'];
654 $object->tva_intra = $thirdparty['vat_number'];
655
656 $object->canvas = !isset($thirdparty['canvas']) ? '' : $thirdparty['canvas'];
657
658 $elementtype = 'societe';
659
660 // Retrieve all extrafields for thirdsparty
661 // fetch optionals attributes and labels
662 $extrafields = new ExtraFields($db);
663 $extrafields->fetch_name_optionals_label($elementtype, true);
664 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
665 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
666 $key = 'options_'.$key;
667 if (isset($thirdparty[$key])) {
668 $object->array_options[$key] = $thirdparty[$key];
669 }
670 }
671 }
672
673 $db->begin();
674
675 $result = $object->update((int) $thirdparty['id'], $fuser);
676 if ($result <= 0) {
677 $error++;
678 }
679 }
680
681 '@phan-var-force array{id:string} $thirdparty';
682
683 if ((!$error) && ($objectfound)) {
684 $db->commit();
685 $objectresp = array(
686 'result' => array('result_code' => 'OK', 'result_label' => ''),
687 'id' => $object->id
688 );
689 } elseif ($objectfound) {
690 $db->rollback();
691 $error++;
692 $errorcode = 'KO';
693 $errorlabel = $object->error;
694 } else {
695 $error++;
696 $errorcode = 'NOT_FOUND';
697 $errorlabel = 'Thirdparty id='.$thirdparty['id'].' cannot be found';
698 }
699 }
700
701 if ($error) {
702 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
703 }
704
705 return $objectresp;
706}
707
708
709
717function getListOfThirdParties($authentication, $filterthirdparty)
718{
719 global $db, $conf;
720
721 dol_syslog("Function: getListOfThirdParties login=".$authentication['login']);
722
723 if ($authentication['entity']) {
724 $conf->entity = $authentication['entity'];
725 }
726
727 // Init and check authentication
728 $objectresp = array();
729 $arraythirdparties = array();
730
731 $errorcode = '';
732 $errorlabel = '';
733 $error = 0;
734 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
735 // Check parameters
736
737 if (!$error) {
738 $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.*";
739 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
740 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON s.fk_pays = c.rowid";
741 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as extra ON s.rowid=fk_object";
742
743 $sql .= " WHERE entity=".$conf->entity;
744 foreach ($filterthirdparty as $key => $val) {
745 if ($key == 'name' && $val != '') {
746 $sql .= " AND s.name LIKE '%".$db->escape($val)."%'";
747 }
748 if ($key == 'client' && (int) $val > 0) {
749 $sql .= " AND s.client = ".((int) $val);
750 }
751 if ($key == 'supplier' && (int) $val > 0) {
752 $sql .= " AND s.fournisseur = ".((int) $val);
753 }
754 if ($key == 'category' && (int) $val > 0) {
755 $sql .= " AND s.rowid IN (SELECT fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe WHERE fk_categorie = ".((int) $val).") ";
756 }
757 }
758 dol_syslog("Function: getListOfThirdParties", LOG_DEBUG);
759
760 $elementtype = 'societe';
761
762 $extrafields = new ExtraFields($db);
763 $extrafields->fetch_name_optionals_label($elementtype, true);
764
765
766 $resql = $db->query($sql);
767 if ($resql) {
768 $num = $db->num_rows($resql);
769
770 $i = 0;
771 while ($i < $num) {
772 $extrafieldsOptions = array();
773 $obj = $db->fetch_object($resql);
774
775 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
776 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
777 if (isset($obj->{$key})) {
778 $extrafieldsOptions['options_'.$key] = $obj->$key;
779 }
780 }
781 }
782
783 $arraythirdparties[] = array('id' => $obj->socRowid,
784 'ref' => $obj->ref,
785 'ref_ext' => $obj->ref_ext,
786 'address' => $obj->address,
787 'zip' => $obj->zip,
788 'town' => $obj->town,
789 'country' => $obj->country,
790 'phone' => $obj->phone,
791 'fax' => $obj->fax,
792 'url' => $obj->url
793 );
794 $arraythirdparties[$i] = array_merge($arraythirdparties[$i], $extrafieldsOptions);
795
796 $i++;
797 }
798 } else {
799 $error++;
800 $errorcode = $db->lasterrno();
801 $errorlabel = $db->lasterror();
802 }
803 }
804
805 if ($error) {
806 $objectresp = array(
807 'result' => array('result_code' => $errorcode, 'result_label' => $errorlabel),
808 'thirdparties' => $arraythirdparties
809 );
810 } else {
811 $objectresp = array(
812 'result' => array('result_code' => 'OK', 'result_label' => ''),
813 'thirdparties' => $arraythirdparties
814 );
815 }
816
817 return $objectresp;
818}
819
829function deleteThirdParty($authentication, $id = '', $ref = '', $ref_ext = '')
830{
831 global $db, $conf;
832
833 dol_syslog("Function: deleteThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
834
835 if ($authentication['entity']) {
836 $conf->entity = $authentication['entity'];
837 }
838
839 // Init and check authentication
840 $objectresp = array();
841 $errorcode = '';
842 $errorlabel = '';
843 $error = 0;
844 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
845 // Check parameters
846 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
847 dol_syslog("Function: deleteThirdParty checkparam");
848 $error++;
849 $errorcode = 'BAD_PARAMETERS';
850 $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
851 }
852 dol_syslog("Function: deleteThirdParty 1");
853
854 if (!$error) {
855 $fuser->loadRights();
856
857 if ($fuser->hasRight('societe', 'lire') && $fuser->hasRight('societe', 'supprimer')) {
858 $thirdparty = new Societe($db);
859 $result = $thirdparty->fetch((int) $id, $ref, $ref_ext);
860
861 if ($result > 0) {
862 $db->begin();
863
864 $result = $thirdparty->delete($thirdparty->id, $fuser);
865
866 if ($result > 0) {
867 $db->commit();
868
869 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''));
870 } else {
871 $db->rollback();
872 $error++;
873 $errorcode = 'KO';
874 $errorlabel = $thirdparty->error;
875 dol_syslog("Function: deleteThirdParty can't delete");
876 }
877 } else {
878 $error++;
879 $errorcode = 'NOT_FOUND';
880 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
881 }
882 } else {
883 $error++;
884 $errorcode = 'PERMISSION_DENIED';
885 $errorlabel = 'User does not have permission for this request';
886 }
887 }
888
889 if ($error) {
890 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
891 }
892
893 return $objectresp;
894}
895
896// Return the results.
897$server->service(file_get_contents("php://input"));
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage standard extra fields.
Class to manage third parties objects (customers, suppliers, prospects...)
getCountry($searchkey, $withcode='', $dbtouse=null, $outputlangs=null, $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getThirdParty($authentication, $id='', $ref='', $ref_ext='', $barcode='', $profid1='', $profid2='')
Get a thirdparty.
deleteThirdParty($authentication, $id='', $ref='', $ref_ext='')
Delete a thirdparty.
updateThirdParty($authentication, $thirdparty)
Update a thirdparty.
getListOfThirdParties($authentication, $filterthirdparty)
getListOfThirdParties
createThirdParty($authentication, $thirdparty)
Create a thirdparty.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition ws.lib.php:37