dolibarr 20.0.0
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->getrights();
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_fournisseur = $thirdparty['supplier_code_accountancy'];
480 $newobject->date_creation = $now;
481 $newobject->note_private = $thirdparty['note_private'];
482 $newobject->note_public = $thirdparty['note_public'];
483 $newobject->address = $thirdparty['address'];
484 $newobject->zip = $thirdparty['zip'];
485 $newobject->town = $thirdparty['town'];
486
487 $newobject->country_id = $thirdparty['country_id'];
488 if ($thirdparty['country_code']) {
489 $newobject->country_id = getCountry($thirdparty['country_code'], 3);
490 }
491 $newobject->region_code = empty($thirdparty['region_code']) ? '' : $thirdparty['region_code'];
492 //if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
493
494 $newobject->phone = $thirdparty['phone'];
495 $newobject->fax = $thirdparty['fax'];
496 $newobject->email = $thirdparty['email'];
497 $newobject->url = $thirdparty['url'];
498 $newobject->idprof1 = $thirdparty['profid1'];
499 $newobject->idprof2 = $thirdparty['profid2'];
500 $newobject->idprof3 = $thirdparty['profid3'];
501 $newobject->idprof4 = $thirdparty['profid4'];
502 $newobject->idprof5 = $thirdparty['profid5'];
503 $newobject->idprof6 = $thirdparty['profid6'];
504
505 $newobject->capital = $thirdparty['capital'];
506
507 $newobject->barcode = empty($thirdparty['barcode']) ? '' : $thirdparty['barcode'];
508 $newobject->tva_assuj = empty($thirdparty['vat_used']) ? 0 : $thirdparty['vat_used'];
509 $newobject->tva_intra = empty($thirdparty['vat_number']) ? '' : $thirdparty['vat_number'];
510
511 $newobject->canvas = empty($thirdparty['canvas']) ? '' : $thirdparty['canvas'];
512 $newobject->particulier = empty($thirdparty['individual']) ? 0 : $thirdparty['individual'];
513
514 $elementtype = 'societe';
515
516 // Retrieve all extrafields for thirdsparty
517 // fetch optionals attributes and labels
518 $extrafields = new ExtraFields($db);
519 $extrafields->fetch_name_optionals_label($elementtype, true);
520 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
521 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
522 $key = 'options_'.$key;
523 if (isset($thirdparty[$key])) {
524 $newobject->array_options[$key] = $thirdparty[$key];
525 }
526 }
527 }
528
529 $db->begin();
530
531 $result = $newobject->create($fuser);
532 if ($newobject->particulier && $result > 0) {
533 $newobject->firstname = $thirdparty['firstname'];
534 $newobject->name_bis = $thirdparty['lastname'];
535 $result = $newobject->create_individual($fuser);
536 }
537 if ($result <= 0) {
538 $error++;
539 }
540
541 if (!$error) {
542 $db->commit();
543
544 // Patch to add capability to associate (one) sale representative
545 if (!empty($thirdparty['commid']) && $thirdparty['commid'] > 0) {
546 $newobject->add_commercial($fuser, $thirdparty["commid"]);
547 }
548
549 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref);
550 } else {
551 $db->rollback();
552 $error++;
553 $errorcode = 'KO';
554 $errorlabel = $newobject->error;
555 }
556 }
557
558 if ($error) {
559 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
560 }
561
562 return $objectresp;
563}
564
572function updateThirdParty($authentication, $thirdparty)
573{
574 global $db, $conf;
575
576 $now = dol_now();
577
578 dol_syslog("Function: updateThirdParty login=".$authentication['login']);
579
580 if ($authentication['entity']) {
581 $conf->entity = $authentication['entity'];
582 }
583
584 // Init and check authentication
585 $objectresp = array();
586 $errorcode = '';
587 $errorlabel = '';
588 $error = 0;
589 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
590 // Check parameters
591 if (empty($thirdparty['id'])) {
592 $error++;
593 $errorcode = 'KO';
594 $errorlabel = "Thirdparty id is mandatory.";
595 }
596
597 if (!$error) {
598 $objectfound = false;
599
600 include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
601
602 $object = new Societe($db);
603 $result = $object->fetch($thirdparty['id']);
604
605 if (!empty($object->id)) {
606 $objectfound = true;
607
608 $object->ref = $thirdparty['ref'];
609 $object->name = $thirdparty['ref'];
610 $object->ref_ext = $thirdparty['ref_ext'];
611 $object->status = $thirdparty['status'];
612 $object->client = $thirdparty['client'];
613 $object->fournisseur = $thirdparty['supplier'];
614 $object->code_client = $thirdparty['customer_code'];
615 $object->code_fournisseur = $thirdparty['supplier_code'];
616 $object->code_compta = $thirdparty['customer_code_accountancy'];
617 $object->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
618 $object->date_creation = $now;
619 $object->note_private = $thirdparty['note_private'];
620 $object->note_public = $thirdparty['note_public'];
621 $object->address = $thirdparty['address'];
622 $object->zip = $thirdparty['zip'];
623 $object->town = $thirdparty['town'];
624
625 $object->country_id = $thirdparty['country_id'];
626 if ($thirdparty['country_code']) {
627 $object->country_id = getCountry($thirdparty['country_code'], 3);
628 }
629 $object->region_code = $thirdparty['region_code'];
630 //if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
631
632 $object->phone = $thirdparty['phone'];
633 $object->fax = $thirdparty['fax'];
634 $object->email = $thirdparty['email'];
635 $object->url = $thirdparty['url'];
636 $object->idprof1 = $thirdparty['profid1'];
637 $object->idprof2 = $thirdparty['profid2'];
638 $object->idprof3 = $thirdparty['profid3'];
639 $object->idprof4 = $thirdparty['profid4'];
640 $object->idprof5 = $thirdparty['profid5'];
641 $object->idprof6 = $thirdparty['profid6'];
642
643 $object->capital = $thirdparty['capital'];
644
645 $object->barcode = $thirdparty['barcode'];
646 $object->tva_assuj = $thirdparty['vat_used'];
647 $object->tva_intra = $thirdparty['vat_number'];
648
649 $object->canvas = $thirdparty['canvas'];
650
651 $elementtype = 'societe';
652
653 // Retrieve all extrafields for thirdsparty
654 // fetch optionals attributes and labels
655 $extrafields = new ExtraFields($db);
656 $extrafields->fetch_name_optionals_label($elementtype, true);
657 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
658 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
659 $key = 'options_'.$key;
660 if (isset($thirdparty[$key])) {
661 $object->array_options[$key] = $thirdparty[$key];
662 }
663 }
664 }
665
666 $db->begin();
667
668 $result = $object->update($thirdparty['id'], $fuser);
669 if ($result <= 0) {
670 $error++;
671 }
672 }
673
674 '@phan-var-force array{id:string} $thirdparty';
675
676 if ((!$error) && ($objectfound)) {
677 $db->commit();
678 $objectresp = array(
679 'result' => array('result_code' => 'OK', 'result_label' => ''),
680 'id' => $object->id
681 );
682 } elseif ($objectfound) {
683 $db->rollback();
684 $error++;
685 $errorcode = 'KO';
686 $errorlabel = $object->error;
687 } else {
688 $error++;
689 $errorcode = 'NOT_FOUND';
690 $errorlabel = 'Thirdparty id='.$thirdparty['id'].' cannot be found';
691 }
692 }
693
694 if ($error) {
695 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
696 }
697
698 return $objectresp;
699}
700
701
702
710function getListOfThirdParties($authentication, $filterthirdparty)
711{
712 global $db, $conf;
713
714 dol_syslog("Function: getListOfThirdParties login=".$authentication['login']);
715
716 if ($authentication['entity']) {
717 $conf->entity = $authentication['entity'];
718 }
719
720 // Init and check authentication
721 $objectresp = array();
722 $arraythirdparties = array();
723
724 $errorcode = '';
725 $errorlabel = '';
726 $error = 0;
727 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
728 // Check parameters
729
730 if (!$error) {
731 $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.*";
732 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
733 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON s.fk_pays = c.rowid";
734 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as extra ON s.rowid=fk_object";
735
736 $sql .= " WHERE entity=".$conf->entity;
737 foreach ($filterthirdparty as $key => $val) {
738 if ($key == 'name' && $val != '') {
739 $sql .= " AND s.name LIKE '%".$db->escape($val)."%'";
740 }
741 if ($key == 'client' && (int) $val > 0) {
742 $sql .= " AND s.client = ".((int) $val);
743 }
744 if ($key == 'supplier' && (int) $val > 0) {
745 $sql .= " AND s.fournisseur = ".((int) $val);
746 }
747 if ($key == 'category' && (int) $val > 0) {
748 $sql .= " AND s.rowid IN (SELECT fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe WHERE fk_categorie = ".((int) $val).") ";
749 }
750 }
751 dol_syslog("Function: getListOfThirdParties", LOG_DEBUG);
752
753 $elementtype = 'societe';
754
755 $extrafields = new ExtraFields($db);
756 $extrafields->fetch_name_optionals_label($elementtype, true);
757
758
759 $resql = $db->query($sql);
760 if ($resql) {
761 $num = $db->num_rows($resql);
762
763 $i = 0;
764 while ($i < $num) {
765 $extrafieldsOptions = array();
766 $obj = $db->fetch_object($resql);
767
768 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
769 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
770 if (isset($obj->{$key})) {
771 $extrafieldsOptions['options_'.$key] = $obj->$key;
772 }
773 }
774 }
775
776 $arraythirdparties[] = array('id' => $obj->socRowid,
777 'ref' => $obj->ref,
778 'ref_ext' => $obj->ref_ext,
779 'address' => $obj->address,
780 'zip' => $obj->zip,
781 'town' => $obj->town,
782 'country' => $obj->country,
783 'phone' => $obj->phone,
784 'fax' => $obj->fax,
785 'url' => $obj->url
786 );
787 $arraythirdparties[$i] = array_merge($arraythirdparties[$i], $extrafieldsOptions);
788
789 $i++;
790 }
791 } else {
792 $error++;
793 $errorcode = $db->lasterrno();
794 $errorlabel = $db->lasterror();
795 }
796 }
797
798 if ($error) {
799 $objectresp = array(
800 'result' => array('result_code' => $errorcode, 'result_label' => $errorlabel),
801 'thirdparties' => $arraythirdparties
802 );
803 } else {
804 $objectresp = array(
805 'result' => array('result_code' => 'OK', 'result_label' => ''),
806 'thirdparties' => $arraythirdparties
807 );
808 }
809
810 return $objectresp;
811}
812
822function deleteThirdParty($authentication, $id = '', $ref = '', $ref_ext = '')
823{
824 global $db, $conf;
825
826 dol_syslog("Function: deleteThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
827
828 if ($authentication['entity']) {
829 $conf->entity = $authentication['entity'];
830 }
831
832 // Init and check authentication
833 $objectresp = array();
834 $errorcode = '';
835 $errorlabel = '';
836 $error = 0;
837 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
838 // Check parameters
839 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
840 dol_syslog("Function: deleteThirdParty checkparam");
841 $error++;
842 $errorcode = 'BAD_PARAMETERS';
843 $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
844 }
845 dol_syslog("Function: deleteThirdParty 1");
846
847 if (!$error) {
848 $fuser->getrights();
849
850 if ($fuser->hasRight('societe', 'lire') && $fuser->hasRight('societe', 'supprimer')) {
851 $thirdparty = new Societe($db);
852 $result = $thirdparty->fetch($id, $ref, $ref_ext);
853
854 if ($result > 0) {
855 $db->begin();
856
857 $result = $thirdparty->delete($thirdparty->id, $fuser);
858
859 if ($result > 0) {
860 $db->commit();
861
862 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''));
863 } else {
864 $db->rollback();
865 $error++;
866 $errorcode = 'KO';
867 $errorlabel = $thirdparty->error;
868 dol_syslog("Function: deleteThirdParty can't delete");
869 }
870 } else {
871 $error++;
872 $errorcode = 'NOT_FOUND';
873 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
874 }
875 } else {
876 $error++;
877 $errorcode = 'PERMISSION_DENIED';
878 $errorlabel = 'User does not have permission for this request';
879 }
880 }
881
882 if ($error) {
883 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
884 }
885
886 return $objectresp;
887}
888
889// Return the results.
890$server->service(file_get_contents("php://input"));
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 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:36