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