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