server_thirdparty.php 27 KB

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