server_thirdparty.php 27 KB

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