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