server_thirdparty.php 29 KB

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