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