server_thirdparty.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. 'region_code' => array('name'=>'region_code', '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', 'barcode'=>'xsd:string', 'profid1'=>'xsd:string', 'profid2'=>'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. * @param string $barcode barcode
  264. * @param string $profid1 profid1
  265. * @param string $profid2 profid2
  266. * @return array Array result
  267. */
  268. function getThirdParty($authentication, $id = '', $ref = '', $ref_ext = '', $barcode = '', $profid1 = '', $profid2 = '')
  269. {
  270. global $db, $conf;
  271. dol_syslog("Function: getThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext." barcode=".$barcode." profid1=".$profid1." profid2=".$profid2);
  272. if ($authentication['entity']) {
  273. $conf->entity = $authentication['entity'];
  274. }
  275. // Init and check authentication
  276. $objectresp = array();
  277. $errorcode = '';
  278. $errorlabel = '';
  279. $error = 0;
  280. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  281. // Check parameters
  282. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  283. $error++;
  284. $errorcode = 'BAD_PARAMETERS';
  285. $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  286. }
  287. if (!$error) {
  288. $fuser->getrights();
  289. if ($fuser->rights->societe->lire) {
  290. $thirdparty = new Societe($db);
  291. $result = $thirdparty->fetch($id, $ref, $ref_ext, $barcode, $profid1, $profid2);
  292. if ($result > 0) {
  293. $thirdparty_result_fields = array(
  294. 'id' => $thirdparty->id,
  295. 'ref' => $thirdparty->name,
  296. 'ref_ext' => $thirdparty->ref_ext,
  297. 'status' => $thirdparty->status,
  298. 'client' => $thirdparty->client,
  299. 'supplier' => $thirdparty->fournisseur,
  300. 'customer_code' => $thirdparty->code_client,
  301. 'supplier_code' => $thirdparty->code_fournisseur,
  302. 'customer_code_accountancy' => $thirdparty->code_compta,
  303. 'supplier_code_accountancy' => $thirdparty->code_compta_fournisseur,
  304. 'user_creation' => $thirdparty->user_creation,
  305. 'date_creation' => dol_print_date($thirdparty->date_creation, 'dayhourrfc'),
  306. 'user_modification' => $thirdparty->user_modification,
  307. 'date_modification' => dol_print_date($thirdparty->date_modification, 'dayhourrfc'),
  308. 'address' => $thirdparty->address,
  309. 'zip' => $thirdparty->zip,
  310. 'town' => $thirdparty->town,
  311. 'region_code' => $thirdparty->region_code,
  312. 'country_id' => $thirdparty->country_id,
  313. 'country_code' => $thirdparty->country_code,
  314. 'country' => $thirdparty->country,
  315. 'phone' => $thirdparty->phone,
  316. 'fax' => $thirdparty->fax,
  317. 'email' => $thirdparty->email,
  318. 'url' => $thirdparty->url,
  319. 'profid1' => $thirdparty->idprof1,
  320. 'profid2' => $thirdparty->idprof2,
  321. 'profid3' => $thirdparty->idprof3,
  322. 'profid4' => $thirdparty->idprof4,
  323. 'profid5' => $thirdparty->idprof5,
  324. 'profid6' => $thirdparty->idprof6,
  325. 'capital' => $thirdparty->capital,
  326. 'barcode' => $thirdparty->barcode,
  327. 'vat_used' => $thirdparty->tva_assuj,
  328. 'vat_number' => $thirdparty->tva_intra,
  329. 'note_private' => $thirdparty->note_private,
  330. 'note_public' => $thirdparty->note_public);
  331. $elementtype = 'societe';
  332. // Retrieve all extrafields for thirdsparty
  333. // fetch optionals attributes and labels
  334. $extrafields = new ExtraFields($db);
  335. $extrafields->fetch_name_optionals_label($elementtype, true);
  336. //Get extrafield values
  337. $thirdparty->fetch_optionals();
  338. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  339. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  340. if (isset($thirdparty->array_options['options_'.$key])) {
  341. $thirdparty_result_fields = array_merge($thirdparty_result_fields, array('options_'.$key => $thirdparty->array_options['options_'.$key]));
  342. }
  343. }
  344. }
  345. // Create
  346. $objectresp = array(
  347. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  348. 'thirdparty'=>$thirdparty_result_fields);
  349. } elseif ($result == -2) {
  350. $error++;
  351. $errorcode = 'DUPLICATE_FOUND'; $errorlabel = 'Object found several times for id='.$id.' or ref='.$ref.' or ref_ext='.$ref_ext;
  352. } else {
  353. $error++;
  354. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  355. }
  356. } else {
  357. $error++;
  358. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  359. }
  360. }
  361. if ($error) {
  362. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  363. }
  364. return $objectresp;
  365. }
  366. /**
  367. * Create a thirdparty
  368. *
  369. * @param array $authentication Array of authentication information
  370. * @param Societe $thirdparty Thirdparty
  371. * @return array Array result
  372. */
  373. function createThirdParty($authentication, $thirdparty)
  374. {
  375. global $db, $conf;
  376. $now = dol_now();
  377. dol_syslog("Function: createThirdParty login=".$authentication['login']);
  378. if ($authentication['entity']) {
  379. $conf->entity = $authentication['entity'];
  380. }
  381. // Init and check authentication
  382. $objectresp = array();
  383. $errorcode = ''; $errorlabel = '';
  384. $error = 0;
  385. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  386. // Check parameters
  387. if (empty($thirdparty['ref'])) {
  388. $error++; $errorcode = 'KO'; $errorlabel = "Name is mandatory.";
  389. }
  390. if (!$error) {
  391. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  392. $newobject = new Societe($db);
  393. $newobject->ref = $thirdparty['ref'];
  394. $newobject->name = $thirdparty['ref'];
  395. $newobject->ref_ext = $thirdparty['ref_ext'];
  396. $newobject->status = $thirdparty['status'];
  397. $newobject->client = $thirdparty['client'];
  398. $newobject->fournisseur = $thirdparty['supplier'];
  399. $newobject->code_client = $thirdparty['customer_code'];
  400. $newobject->code_fournisseur = $thirdparty['supplier_code'];
  401. $newobject->code_compta = $thirdparty['customer_code_accountancy'];
  402. $newobject->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
  403. $newobject->date_creation = $now;
  404. $newobject->note_private = $thirdparty['note_private'];
  405. $newobject->note_public = $thirdparty['note_public'];
  406. $newobject->address = $thirdparty['address'];
  407. $newobject->zip = $thirdparty['zip'];
  408. $newobject->town = $thirdparty['town'];
  409. $newobject->country_id = $thirdparty['country_id'];
  410. if ($thirdparty['country_code']) {
  411. $newobject->country_id = getCountry($thirdparty['country_code'], 3);
  412. }
  413. $newobject->region_code = empty($thirdparty['region_code']) ? '' : $thirdparty['region_code'];
  414. //if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
  415. $newobject->phone = $thirdparty['phone'];
  416. $newobject->fax = $thirdparty['fax'];
  417. $newobject->email = $thirdparty['email'];
  418. $newobject->url = $thirdparty['url'];
  419. $newobject->idprof1 = $thirdparty['profid1'];
  420. $newobject->idprof2 = $thirdparty['profid2'];
  421. $newobject->idprof3 = $thirdparty['profid3'];
  422. $newobject->idprof4 = $thirdparty['profid4'];
  423. $newobject->idprof5 = $thirdparty['profid5'];
  424. $newobject->idprof6 = $thirdparty['profid6'];
  425. $newobject->capital = $thirdparty['capital'];
  426. $newobject->barcode = empty($thirdparty['barcode']) ? '' : $thirdparty['barcode'];
  427. $newobject->tva_assuj = empty($thirdparty['vat_used']) ? 0 : $thirdparty['vat_used'];
  428. $newobject->tva_intra = empty($thirdparty['vat_number']) ? '' : $thirdparty['vat_number'];
  429. $newobject->canvas = empty($thirdparty['canvas']) ? '' : $thirdparty['canvas'];
  430. $newobject->particulier = empty($thirdparty['individual']) ? 0 : $thirdparty['individual'];
  431. $elementtype = 'societe';
  432. // Retrieve all extrafields for thirdsparty
  433. // fetch optionals attributes and labels
  434. $extrafields = new ExtraFields($db);
  435. $extrafields->fetch_name_optionals_label($elementtype, true);
  436. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  437. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  438. $key = 'options_'.$key;
  439. if (isset($thirdparty[$key])) {
  440. $newobject->array_options[$key] = $thirdparty[$key];
  441. }
  442. }
  443. }
  444. $db->begin();
  445. $result = $newobject->create($fuser);
  446. if ($newobject->particulier && $result > 0) {
  447. $newobject->firstname = $thirdparty['firstname'];
  448. $newobject->name_bis = $thirdparty['lastname'];
  449. $result = $newobject->create_individual($fuser);
  450. }
  451. if ($result <= 0) {
  452. $error++;
  453. }
  454. if (!$error) {
  455. $db->commit();
  456. // Patch to add capability to associate (one) sale representative
  457. if (!empty($thirdparty['commid']) && $thirdparty['commid'] > 0) {
  458. $newobject->add_commercial($fuser, $thirdparty["commid"]);
  459. }
  460. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
  461. } else {
  462. $db->rollback();
  463. $error++;
  464. $errorcode = 'KO';
  465. $errorlabel = $newobject->error;
  466. }
  467. }
  468. if ($error) {
  469. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  470. }
  471. return $objectresp;
  472. }
  473. /**
  474. * Update a thirdparty
  475. *
  476. * @param array $authentication Array of authentication information
  477. * @param Societe $thirdparty Thirdparty
  478. * @return array Array result
  479. */
  480. function updateThirdParty($authentication, $thirdparty)
  481. {
  482. global $db, $conf;
  483. $now = dol_now();
  484. dol_syslog("Function: updateThirdParty login=".$authentication['login']);
  485. if ($authentication['entity']) {
  486. $conf->entity = $authentication['entity'];
  487. }
  488. // Init and check authentication
  489. $objectresp = array();
  490. $errorcode = ''; $errorlabel = '';
  491. $error = 0;
  492. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  493. // Check parameters
  494. if (empty($thirdparty['id'])) {
  495. $error++; $errorcode = 'KO'; $errorlabel = "Thirdparty id is mandatory.";
  496. }
  497. if (!$error) {
  498. $objectfound = false;
  499. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  500. $object = new Societe($db);
  501. $result = $object->fetch($thirdparty['id']);
  502. if (!empty($object->id)) {
  503. $objectfound = true;
  504. $object->ref = $thirdparty['ref'];
  505. $object->name = $thirdparty['ref'];
  506. $object->ref_ext = $thirdparty['ref_ext'];
  507. $object->status = $thirdparty['status'];
  508. $object->client = $thirdparty['client'];
  509. $object->fournisseur = $thirdparty['supplier'];
  510. $object->code_client = $thirdparty['customer_code'];
  511. $object->code_fournisseur = $thirdparty['supplier_code'];
  512. $object->code_compta = $thirdparty['customer_code_accountancy'];
  513. $object->code_compta_fournisseur = $thirdparty['supplier_code_accountancy'];
  514. $object->date_creation = $now;
  515. $object->note_private = $thirdparty['note_private'];
  516. $object->note_public = $thirdparty['note_public'];
  517. $object->address = $thirdparty['address'];
  518. $object->zip = $thirdparty['zip'];
  519. $object->town = $thirdparty['town'];
  520. $object->country_id = $thirdparty['country_id'];
  521. if ($thirdparty['country_code']) {
  522. $object->country_id = getCountry($thirdparty['country_code'], 3);
  523. }
  524. $object->region_code = $thirdparty['region_code'];
  525. //if ($thirdparty['province_code']) $newobject->province_code=getCountry($thirdparty['province_code'],3);
  526. $object->phone = $thirdparty['phone'];
  527. $object->fax = $thirdparty['fax'];
  528. $object->email = $thirdparty['email'];
  529. $object->url = $thirdparty['url'];
  530. $object->idprof1 = $thirdparty['profid1'];
  531. $object->idprof2 = $thirdparty['profid2'];
  532. $object->idprof3 = $thirdparty['profid3'];
  533. $object->idprof4 = $thirdparty['profid4'];
  534. $object->idprof5 = $thirdparty['profid5'];
  535. $object->idprof6 = $thirdparty['profid6'];
  536. $object->capital = $thirdparty['capital'];
  537. $object->barcode = $thirdparty['barcode'];
  538. $object->tva_assuj = $thirdparty['vat_used'];
  539. $object->tva_intra = $thirdparty['vat_number'];
  540. $object->canvas = $thirdparty['canvas'];
  541. $elementtype = 'societe';
  542. // Retrieve all extrafields for thirdsparty
  543. // fetch optionals attributes and labels
  544. $extrafields = new ExtraFields($db);
  545. $extrafields->fetch_name_optionals_label($elementtype, true);
  546. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  547. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  548. $key = 'options_'.$key;
  549. if (isset($thirdparty[$key])) {
  550. $object->array_options[$key] = $thirdparty[$key];
  551. }
  552. }
  553. }
  554. $db->begin();
  555. $result = $object->update($thirdparty['id'], $fuser);
  556. if ($result <= 0) {
  557. $error++;
  558. }
  559. }
  560. if ((!$error) && ($objectfound)) {
  561. $db->commit();
  562. $objectresp = array(
  563. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  564. 'id'=>$object->id
  565. );
  566. } elseif ($objectfound) {
  567. $db->rollback();
  568. $error++;
  569. $errorcode = 'KO';
  570. $errorlabel = $object->error;
  571. } else {
  572. $error++;
  573. $errorcode = 'NOT_FOUND';
  574. $errorlabel = 'Thirdparty id='.$thirdparty['id'].' cannot be found';
  575. }
  576. }
  577. if ($error) {
  578. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  579. }
  580. return $objectresp;
  581. }
  582. /**
  583. * getListOfThirdParties
  584. *
  585. * @param array $authentication Array of authentication information
  586. * @param array $filterthirdparty Filter fields (key=>value to filer on. For example 'client'=>2, 'supplier'=>1, 'category'=>idcateg, 'name'=>'searchstring', ...)
  587. * @return array Array result
  588. */
  589. function getListOfThirdParties($authentication, $filterthirdparty)
  590. {
  591. global $db, $conf;
  592. dol_syslog("Function: getListOfThirdParties login=".$authentication['login']);
  593. if ($authentication['entity']) {
  594. $conf->entity = $authentication['entity'];
  595. }
  596. // Init and check authentication
  597. $objectresp = array();
  598. $arraythirdparties = array();
  599. $errorcode = ''; $errorlabel = '';
  600. $error = 0;
  601. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  602. // Check parameters
  603. if (!$error) {
  604. $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.*";
  605. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  606. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON s.fk_pays = c.rowid";
  607. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as extra ON s.rowid=fk_object";
  608. $sql .= " WHERE entity=".$conf->entity;
  609. foreach ($filterthirdparty as $key => $val) {
  610. if ($key == 'name' && $val != '') {
  611. $sql .= " AND s.name LIKE '%".$db->escape($val)."%'";
  612. }
  613. if ($key == 'client' && (int) $val > 0) {
  614. $sql .= " AND s.client = ".((int) $val);
  615. }
  616. if ($key == 'supplier' && (int) $val > 0) {
  617. $sql .= " AND s.fournisseur = ".((int) $val);
  618. }
  619. if ($key == 'category' && (int) $val > 0) {
  620. $sql .= " AND s.rowid IN (SELECT fk_soc FROM ".MAIN_DB_PREFIX."categorie_societe WHERE fk_categorie = ".((int) $val).") ";
  621. }
  622. }
  623. dol_syslog("Function: getListOfThirdParties", LOG_DEBUG);
  624. $elementtype = 'societe';
  625. $extrafields = new ExtraFields($db);
  626. $extrafields->fetch_name_optionals_label($elementtype, true);
  627. $resql = $db->query($sql);
  628. if ($resql) {
  629. $num = $db->num_rows($resql);
  630. $i = 0;
  631. while ($i < $num) {
  632. $extrafieldsOptions = array();
  633. $obj = $db->fetch_object($resql);
  634. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  635. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  636. if (isset($obj->{$key})) {
  637. $extrafieldsOptions['options_'.$key] = $obj->{$key};
  638. }
  639. }
  640. }
  641. $arraythirdparties[] = array('id'=>$obj->socRowid,
  642. 'ref'=>$obj->ref,
  643. 'ref_ext'=>$obj->ref_ext,
  644. 'adress'=>$obj->adress,
  645. 'zip'=>$obj->zip,
  646. 'town'=>$obj->town,
  647. 'country'=>$obj->country,
  648. 'phone'=>$obj->phone,
  649. 'fax'=>$obj->fax,
  650. 'url'=>$obj->url
  651. );
  652. $arraythirdparties[$i] = array_merge($arraythirdparties[$i], $extrafieldsOptions);
  653. $i++;
  654. }
  655. } else {
  656. $error++;
  657. $errorcode = $db->lasterrno();
  658. $errorlabel = $db->lasterror();
  659. }
  660. }
  661. if ($error) {
  662. $objectresp = array(
  663. 'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
  664. 'thirdparties'=>$arraythirdparties
  665. );
  666. } else {
  667. $objectresp = array(
  668. 'result'=>array('result_code' => 'OK', 'result_label' => ''),
  669. 'thirdparties'=>$arraythirdparties
  670. );
  671. }
  672. return $objectresp;
  673. }
  674. /**
  675. * Delete a thirdparty
  676. *
  677. * @param array $authentication Array of authentication information
  678. * @param string $id internal id
  679. * @param string $ref internal reference
  680. * @param string $ref_ext external reference
  681. * @return array Array result
  682. */
  683. function deleteThirdParty($authentication, $id = '', $ref = '', $ref_ext = '')
  684. {
  685. global $db, $conf;
  686. dol_syslog("Function: deleteThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
  687. if ($authentication['entity']) {
  688. $conf->entity = $authentication['entity'];
  689. }
  690. // Init and check authentication
  691. $objectresp = array();
  692. $errorcode = ''; $errorlabel = '';
  693. $error = 0;
  694. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  695. // Check parameters
  696. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  697. dol_syslog("Function: deleteThirdParty checkparam");
  698. $error++;
  699. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  700. }
  701. dol_syslog("Function: deleteThirdParty 1");
  702. if (!$error) {
  703. $fuser->getrights();
  704. if ($fuser->rights->societe->lire && $fuser->rights->societe->supprimer) {
  705. $thirdparty = new Societe($db);
  706. $result = $thirdparty->fetch($id, $ref, $ref_ext);
  707. if ($result > 0) {
  708. $db->begin();
  709. $result = $thirdparty->delete($thirdparty->id, $fuser);
  710. if ($result > 0) {
  711. $db->commit();
  712. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''));
  713. } else {
  714. $db->rollback();
  715. $error++;
  716. $errorcode = 'KO';
  717. $errorlabel = $thirdparty->error;
  718. dol_syslog("Function: deleteThirdParty cant delete");
  719. }
  720. } else {
  721. $error++;
  722. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  723. }
  724. } else {
  725. $error++;
  726. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  727. }
  728. }
  729. if ($error) {
  730. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  731. }
  732. return $objectresp;
  733. }
  734. // Return the results.
  735. $server->service(file_get_contents("php://input"));