dolibarr  19.0.0-dev
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'); // Do not check anti CSRF attack test
25 }
26 if (!defined('NOTOKENRENEWAL')) {
27  define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
28 }
29 if (!defined('NOREQUIREMENU')) {
30  define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
31 }
32 if (!defined('NOREQUIREHTML')) {
33  define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
34 }
35 if (!defined('NOREQUIREAJAX')) {
36  define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
37 }
38 if (!defined("NOLOGIN")) {
39  define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
40 }
41 if (!defined("NOSESSION")) {
42  define("NOSESSION", '1');
43 }
44 
45 require '../main.inc.php';
46 require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
47 require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
48 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
49 
50 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
51 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
52 
53 
54 dol_syslog("Call Dolibarr webservices interfaces");
55 
56 $langs->load("main");
57 
58 // Enable and test if module web services is enabled
59 if (empty($conf->global->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;
149 if (is_array($extrafields) && count($extrafields) > 0) {
150  $extrafield_array = array();
151 }
152 if (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 
166 if (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
316 function 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->rights->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,
356  'supplier_code_accountancy' => $thirdparty->code_compta_fournisseur,
357  'user_creation' => $thirdparty->user_creation,
358  'date_creation' => dol_print_date($thirdparty->date_creation, 'dayhourrfc'),
359  'user_modification' => $thirdparty->user_modification,
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'; $errorlabel = 'Object found several times for id='.$id.' or ref='.$ref.' or ref_ext='.$ref_ext;
409  } else {
410  $error++;
411  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
412  }
413  } else {
414  $error++;
415  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
416  }
417  }
418 
419  if ($error) {
420  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
421  }
422 
423  return $objectresp;
424 }
425 
426 
427 
435 function createThirdParty($authentication, $thirdparty)
436 {
437  global $db, $conf;
438 
439  $now = dol_now();
440 
441  dol_syslog("Function: createThirdParty login=".$authentication['login']);
442 
443  if ($authentication['entity']) {
444  $conf->entity = $authentication['entity'];
445  }
446 
447  // Init and check authentication
448  $objectresp = array();
449  $errorcode = ''; $errorlabel = '';
450  $error = 0;
451  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
452  // Check parameters
453  if (empty($thirdparty['ref'])) {
454  $error++; $errorcode = 'KO'; $errorlabel = "Name is mandatory.";
455  }
456 
457 
458  if (!$error) {
459  include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
460 
461  $newobject = new Societe($db);
462  $newobject->ref = $thirdparty['ref'];
463  $newobject->name = $thirdparty['ref'];
464  $newobject->ref_ext = $thirdparty['ref_ext'];
465  $newobject->status = $thirdparty['status'];
466  $newobject->client = $thirdparty['client'];
467  $newobject->fournisseur = $thirdparty['supplier'];
468  $newobject->code_client = $thirdparty['customer_code'];
469  $newobject->code_fournisseur = $thirdparty['supplier_code'];
470  $newobject->code_compta = $thirdparty['customer_code_accountancy'];
471  $newobject->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
472  $newobject->date_creation = $now;
473  $newobject->note_private = $thirdparty['note_private'];
474  $newobject->note_public = $thirdparty['note_public'];
475  $newobject->address = $thirdparty['address'];
476  $newobject->zip = $thirdparty['zip'];
477  $newobject->town = $thirdparty['town'];
478 
479  $newobject->country_id = $thirdparty['country_id'];
480  if ($thirdparty['country_code']) {
481  $newobject->country_id = getCountry($thirdparty['country_code'], 3);
482  }
483  $newobject->region_code = empty($thirdparty['region_code']) ? '' : $thirdparty['region_code'];
484  //if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
485 
486  $newobject->phone = $thirdparty['phone'];
487  $newobject->fax = $thirdparty['fax'];
488  $newobject->email = $thirdparty['email'];
489  $newobject->url = $thirdparty['url'];
490  $newobject->idprof1 = $thirdparty['profid1'];
491  $newobject->idprof2 = $thirdparty['profid2'];
492  $newobject->idprof3 = $thirdparty['profid3'];
493  $newobject->idprof4 = $thirdparty['profid4'];
494  $newobject->idprof5 = $thirdparty['profid5'];
495  $newobject->idprof6 = $thirdparty['profid6'];
496 
497  $newobject->capital = $thirdparty['capital'];
498 
499  $newobject->barcode = empty($thirdparty['barcode']) ? '' : $thirdparty['barcode'];
500  $newobject->tva_assuj = empty($thirdparty['vat_used']) ? 0 : $thirdparty['vat_used'];
501  $newobject->tva_intra = empty($thirdparty['vat_number']) ? '' : $thirdparty['vat_number'];
502 
503  $newobject->canvas = empty($thirdparty['canvas']) ? '' : $thirdparty['canvas'];
504  $newobject->particulier = empty($thirdparty['individual']) ? 0 : $thirdparty['individual'];
505 
506  $elementtype = 'societe';
507 
508  // Retrieve all extrafields for thirdsparty
509  // fetch optionals attributes and labels
510  $extrafields = new ExtraFields($db);
511  $extrafields->fetch_name_optionals_label($elementtype, true);
512  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
513  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
514  $key = 'options_'.$key;
515  if (isset($thirdparty[$key])) {
516  $newobject->array_options[$key] = $thirdparty[$key];
517  }
518  }
519  }
520 
521  $db->begin();
522 
523  $result = $newobject->create($fuser);
524  if ($newobject->particulier && $result > 0) {
525  $newobject->firstname = $thirdparty['firstname'];
526  $newobject->name_bis = $thirdparty['lastname'];
527  $result = $newobject->create_individual($fuser);
528  }
529  if ($result <= 0) {
530  $error++;
531  }
532 
533  if (!$error) {
534  $db->commit();
535 
536  // Patch to add capability to associate (one) sale representative
537  if (!empty($thirdparty['commid']) && $thirdparty['commid'] > 0) {
538  $newobject->add_commercial($fuser, $thirdparty["commid"]);
539  }
540 
541  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
542  } else {
543  $db->rollback();
544  $error++;
545  $errorcode = 'KO';
546  $errorlabel = $newobject->error;
547  }
548  }
549 
550  if ($error) {
551  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
552  }
553 
554  return $objectresp;
555 }
556 
564 function updateThirdParty($authentication, $thirdparty)
565 {
566  global $db, $conf;
567 
568  $now = dol_now();
569 
570  dol_syslog("Function: updateThirdParty login=".$authentication['login']);
571 
572  if ($authentication['entity']) {
573  $conf->entity = $authentication['entity'];
574  }
575 
576  // Init and check authentication
577  $objectresp = array();
578  $errorcode = ''; $errorlabel = '';
579  $error = 0;
580  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
581  // Check parameters
582  if (empty($thirdparty['id'])) {
583  $error++; $errorcode = 'KO'; $errorlabel = "Thirdparty id is mandatory.";
584  }
585 
586  if (!$error) {
587  $objectfound = false;
588 
589  include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
590 
591  $object = new Societe($db);
592  $result = $object->fetch($thirdparty['id']);
593 
594  if (!empty($object->id)) {
595  $objectfound = true;
596 
597  $object->ref = $thirdparty['ref'];
598  $object->name = $thirdparty['ref'];
599  $object->ref_ext = $thirdparty['ref_ext'];
600  $object->status = $thirdparty['status'];
601  $object->client = $thirdparty['client'];
602  $object->fournisseur = $thirdparty['supplier'];
603  $object->code_client = $thirdparty['customer_code'];
604  $object->code_fournisseur = $thirdparty['supplier_code'];
605  $object->code_compta = $thirdparty['customer_code_accountancy'];
606  $object->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
607  $object->date_creation = $now;
608  $object->note_private = $thirdparty['note_private'];
609  $object->note_public = $thirdparty['note_public'];
610  $object->address = $thirdparty['address'];
611  $object->zip = $thirdparty['zip'];
612  $object->town = $thirdparty['town'];
613 
614  $object->country_id = $thirdparty['country_id'];
615  if ($thirdparty['country_code']) {
616  $object->country_id = getCountry($thirdparty['country_code'], 3);
617  }
618  $object->region_code = $thirdparty['region_code'];
619  //if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
620 
621  $object->phone = $thirdparty['phone'];
622  $object->fax = $thirdparty['fax'];
623  $object->email = $thirdparty['email'];
624  $object->url = $thirdparty['url'];
625  $object->idprof1 = $thirdparty['profid1'];
626  $object->idprof2 = $thirdparty['profid2'];
627  $object->idprof3 = $thirdparty['profid3'];
628  $object->idprof4 = $thirdparty['profid4'];
629  $object->idprof5 = $thirdparty['profid5'];
630  $object->idprof6 = $thirdparty['profid6'];
631 
632  $object->capital = $thirdparty['capital'];
633 
634  $object->barcode = $thirdparty['barcode'];
635  $object->tva_assuj = $thirdparty['vat_used'];
636  $object->tva_intra = $thirdparty['vat_number'];
637 
638  $object->canvas = $thirdparty['canvas'];
639 
640  $elementtype = 'societe';
641 
642  // Retrieve all extrafields for thirdsparty
643  // fetch optionals attributes and labels
644  $extrafields = new ExtraFields($db);
645  $extrafields->fetch_name_optionals_label($elementtype, true);
646  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
647  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
648  $key = 'options_'.$key;
649  if (isset($thirdparty[$key])) {
650  $object->array_options[$key] = $thirdparty[$key];
651  }
652  }
653  }
654 
655  $db->begin();
656 
657  $result = $object->update($thirdparty['id'], $fuser);
658  if ($result <= 0) {
659  $error++;
660  }
661  }
662 
663  if ((!$error) && ($objectfound)) {
664  $db->commit();
665  $objectresp = array(
666  'result'=>array('result_code'=>'OK', 'result_label'=>''),
667  'id'=>$object->id
668  );
669  } elseif ($objectfound) {
670  $db->rollback();
671  $error++;
672  $errorcode = 'KO';
673  $errorlabel = $object->error;
674  } else {
675  $error++;
676  $errorcode = 'NOT_FOUND';
677  $errorlabel = 'Thirdparty id='.$thirdparty['id'].' cannot be found';
678  }
679  }
680 
681  if ($error) {
682  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
683  }
684 
685  return $objectresp;
686 }
687 
688 
689 
697 function getListOfThirdParties($authentication, $filterthirdparty)
698 {
699  global $db, $conf;
700 
701  dol_syslog("Function: getListOfThirdParties login=".$authentication['login']);
702 
703  if ($authentication['entity']) {
704  $conf->entity = $authentication['entity'];
705  }
706 
707  // Init and check authentication
708  $objectresp = array();
709  $arraythirdparties = array();
710 
711  $errorcode = ''; $errorlabel = '';
712  $error = 0;
713  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
714  // Check parameters
715 
716  if (!$error) {
717  $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.*";
718  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
719  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON s.fk_pays = c.rowid";
720  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as extra ON s.rowid=fk_object";
721 
722  $sql .= " WHERE entity=".$conf->entity;
723  foreach ($filterthirdparty as $key => $val) {
724  if ($key == 'name' && $val != '') {
725  $sql .= " AND s.name LIKE '%".$db->escape($val)."%'";
726  }
727  if ($key == 'client' && (int) $val > 0) {
728  $sql .= " AND s.client = ".((int) $val);
729  }
730  if ($key == 'supplier' && (int) $val > 0) {
731  $sql .= " AND s.fournisseur = ".((int) $val);
732  }
733  if ($key == 'category' && (int) $val > 0) {
734  $sql .= " AND s.rowid IN (SELECT fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe WHERE fk_categorie = ".((int) $val).") ";
735  }
736  }
737  dol_syslog("Function: getListOfThirdParties", LOG_DEBUG);
738 
739  $elementtype = 'societe';
740 
741  $extrafields = new ExtraFields($db);
742  $extrafields->fetch_name_optionals_label($elementtype, true);
743 
744 
745  $resql = $db->query($sql);
746  if ($resql) {
747  $num = $db->num_rows($resql);
748 
749  $i = 0;
750  while ($i < $num) {
751  $extrafieldsOptions = array();
752  $obj = $db->fetch_object($resql);
753 
754  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
755  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
756  if (isset($obj->{$key})) {
757  $extrafieldsOptions['options_'.$key] = $obj->$key;
758  }
759  }
760  }
761 
762  $arraythirdparties[] = array('id'=>$obj->socRowid,
763  'ref'=>$obj->ref,
764  'ref_ext'=>$obj->ref_ext,
765  'adress'=>$obj->adress,
766  'zip'=>$obj->zip,
767  'town'=>$obj->town,
768  'country'=>$obj->country,
769  'phone'=>$obj->phone,
770  'fax'=>$obj->fax,
771  'url'=>$obj->url
772  );
773  $arraythirdparties[$i] = array_merge($arraythirdparties[$i], $extrafieldsOptions);
774 
775  $i++;
776  }
777  } else {
778  $error++;
779  $errorcode = $db->lasterrno();
780  $errorlabel = $db->lasterror();
781  }
782  }
783 
784  if ($error) {
785  $objectresp = array(
786  'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
787  'thirdparties'=>$arraythirdparties
788  );
789  } else {
790  $objectresp = array(
791  'result'=>array('result_code' => 'OK', 'result_label' => ''),
792  'thirdparties'=>$arraythirdparties
793  );
794  }
795 
796  return $objectresp;
797 }
798 
808 function deleteThirdParty($authentication, $id = '', $ref = '', $ref_ext = '')
809 {
810  global $db, $conf;
811 
812  dol_syslog("Function: deleteThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
813 
814  if ($authentication['entity']) {
815  $conf->entity = $authentication['entity'];
816  }
817 
818  // Init and check authentication
819  $objectresp = array();
820  $errorcode = ''; $errorlabel = '';
821  $error = 0;
822  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
823  // Check parameters
824  if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
825  dol_syslog("Function: deleteThirdParty checkparam");
826  $error++;
827  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
828  }
829  dol_syslog("Function: deleteThirdParty 1");
830 
831  if (!$error) {
832  $fuser->getrights();
833 
834  if ($fuser->rights->societe->lire && $fuser->rights->societe->supprimer) {
835  $thirdparty = new Societe($db);
836  $result = $thirdparty->fetch($id, $ref, $ref_ext);
837 
838  if ($result > 0) {
839  $db->begin();
840 
841  $result = $thirdparty->delete($thirdparty->id, $fuser);
842 
843  if ($result > 0) {
844  $db->commit();
845 
846  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
847  } else {
848  $db->rollback();
849  $error++;
850  $errorcode = 'KO';
851  $errorlabel = $thirdparty->error;
852  dol_syslog("Function: deleteThirdParty cant delete");
853  }
854  } else {
855  $error++;
856  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
857  }
858  } else {
859  $error++;
860  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
861  }
862  }
863 
864  if ($error) {
865  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
866  }
867 
868  return $objectresp;
869 }
870 
871 // Return the results.
872 $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.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
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.
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