server_thirdparty.php 29 KB

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