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