server_productorservice.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 JF FERRY <jfefe@aternatik.fr>
  4. * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * Path to WSDL is: http://localhost/dolibarr/webservices/server_productorservice.php?wsdl
  20. */
  21. /**
  22. * \file htdocs/webservices/server_productorservice.php
  23. * \brief File that is entry point to call Dolibarr WebServices
  24. */
  25. if (!defined("NOCSRFCHECK")) {
  26. define("NOCSRFCHECK", '1');
  27. }
  28. require '../master.inc.php';
  29. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  34. require_once DOL_DOCUMENT_ROOT."/categories/class/categorie.class.php";
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  36. dol_syslog("Call Dolibarr webservices interfaces");
  37. $langs->load("main");
  38. // Enable and test if module web services is enabled
  39. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  40. $langs->load("admin");
  41. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  42. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  43. print $langs->trans("ToActivateModule");
  44. exit;
  45. }
  46. // Create the soap Object
  47. $server = new nusoap_server();
  48. $server->soap_defencoding = 'UTF-8';
  49. $server->decode_utf8 = false;
  50. $ns = 'http://www.dolibarr.org/ns/';
  51. $server->configureWSDL('WebServicesDolibarrProductOrService', $ns);
  52. $server->wsdl->schemaTargetNamespace = $ns;
  53. // Define WSDL Authentication object
  54. $server->wsdl->addComplexType(
  55. 'authentication',
  56. 'complexType',
  57. 'struct',
  58. 'all',
  59. '',
  60. array(
  61. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  62. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  63. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  64. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  65. 'entity' => array('name'=>'entity', 'type'=>'xsd:string')
  66. )
  67. );
  68. // Define WSDL Return object
  69. $server->wsdl->addComplexType(
  70. 'result',
  71. 'complexType',
  72. 'struct',
  73. 'all',
  74. '',
  75. array(
  76. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  77. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string')
  78. )
  79. );
  80. $productorservice_fields = array(
  81. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  82. 'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
  83. 'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
  84. 'type' => array('name'=>'type', 'type'=>'xsd:string'),
  85. 'label' => array('name'=>'label', 'type'=>'xsd:string'),
  86. 'description' => array('name'=>'description', 'type'=>'xsd:string'),
  87. 'date_creation' => array('name'=>'date_creation', 'type'=>'xsd:dateTime'),
  88. 'date_modification' => array('name'=>'date_modification', 'type'=>'xsd:dateTime'),
  89. 'note' => array('name'=>'note', 'type'=>'xsd:string'),
  90. 'status_tobuy' => array('name'=>'status_tobuy', 'type'=>'xsd:string'),
  91. 'status_tosell' => array('name'=>'status_tosell', 'type'=>'xsd:string'),
  92. 'barcode' => array('name'=>'barcode', 'type'=>'xsd:string'),
  93. 'barcode_type' => array('name'=>'barcode_type', 'type'=>'xsd:string'),
  94. 'country_id' => array('name'=>'country_id', 'type'=>'xsd:string'),
  95. 'country_code' => array('name'=>'country_code', 'type'=>'xsd:string'),
  96. 'customcode' => array('name'=>'customcode', 'type'=>'xsd:string'),
  97. 'price_net' => array('name'=>'price_net', 'type'=>'xsd:string'),
  98. 'price' => array('name'=>'price', 'type'=>'xsd:string'),
  99. 'price_min_net' => array('name'=>'price_min_net', 'type'=>'xsd:string'),
  100. 'price_min' => array('name'=>'price_min', 'type'=>'xsd:string'),
  101. 'price_base_type' => array('name'=>'price_base_type', 'type'=>'xsd:string'),
  102. 'vat_rate' => array('name'=>'vat_rate', 'type'=>'xsd:string'),
  103. 'vat_npr' => array('name'=>'vat_npr', 'type'=>'xsd:string'),
  104. 'localtax1_tx' => array('name'=>'localtax1_tx', 'type'=>'xsd:string'),
  105. 'localtax2_tx' => array('name'=>'localtax2_tx', 'type'=>'xsd:string'),
  106. 'stock_alert' => array('name'=>'stock_alert', 'type'=>'xsd:string'),
  107. 'stock_real' => array('name'=>'stock_real', 'type'=>'xsd:string'),
  108. 'stock_pmp' => array('name'=>'stock_pmp', 'type'=>'xsd:string'),
  109. 'warehouse_ref' => array('name'=>'warehouse_ref', 'type'=>'xsd:string'), // Used only for create or update to set which warehouse to use for stock correction if stock_real differs from database
  110. 'canvas' => array('name'=>'canvas', 'type'=>'xsd:string'),
  111. 'import_key' => array('name'=>'import_key', 'type'=>'xsd:string'),
  112. 'dir' => array('name'=>'dir', 'type'=>'xsd:string'),
  113. 'images' => array('name'=>'images', 'type'=>'tns:ImagesArray')
  114. );
  115. $elementtype = 'product';
  116. //Retrieve all extrafield for product
  117. // fetch optionals attributes and labels
  118. $extrafields = new ExtraFields($db);
  119. $extrafields->fetch_name_optionals_label($elementtype, true);
  120. $extrafield_array = null;
  121. if (is_array($extrafields) && count($extrafields) > 0) {
  122. $extrafield_array = array();
  123. }
  124. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  125. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  126. $type = $extrafields->attributes[$elementtype]['type'][$key];
  127. if ($type == 'date' || $type == 'datetime') {
  128. $type = 'xsd:dateTime';
  129. } else {
  130. $type = 'xsd:string';
  131. }
  132. $extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
  133. }
  134. }
  135. if (!empty($extrafield_array) && is_array($extrafield_array)) {
  136. $productorservice_fields = array_merge($productorservice_fields, $extrafield_array);
  137. }
  138. // Define other specific objects
  139. $server->wsdl->addComplexType(
  140. 'product',
  141. 'complexType',
  142. 'struct',
  143. 'all',
  144. '',
  145. $productorservice_fields
  146. );
  147. /*
  148. * Image of product
  149. */
  150. $server->wsdl->addComplexType(
  151. 'ImagesArray',
  152. 'complexType',
  153. 'array',
  154. 'sequence',
  155. '',
  156. array(
  157. 'image' => array(
  158. 'name' => 'image',
  159. 'type' => 'tns:image',
  160. 'minOccurs' => '0',
  161. 'maxOccurs' => 'unbounded'
  162. )
  163. )
  164. );
  165. /*
  166. * An image
  167. */
  168. $server->wsdl->addComplexType(
  169. 'image',
  170. 'complexType',
  171. 'struct',
  172. 'all',
  173. '',
  174. array(
  175. 'photo' => array('name'=>'photo', 'type'=>'xsd:string'),
  176. 'photo_vignette' => array('name'=>'photo_vignette', 'type'=>'xsd:string'),
  177. 'imgWidth' => array('name'=>'imgWidth', 'type'=>'xsd:string'),
  178. 'imgHeight' => array('name'=>'imgHeight', 'type'=>'xsd:string')
  179. )
  180. );
  181. // Define other specific objects
  182. $server->wsdl->addComplexType(
  183. 'filterproduct',
  184. 'complexType',
  185. 'struct',
  186. 'all',
  187. '',
  188. array(
  189. //'limit' => array('name'=>'limit','type'=>'xsd:string'),
  190. 'type' => array('name'=>'type', 'type'=>'xsd:string'),
  191. 'status_tobuy' => array('name'=>'status_tobuy', 'type'=>'xsd:string'),
  192. 'status_tosell' => array('name'=>'status_tosell', 'type'=>'xsd:string'),
  193. )
  194. );
  195. $server->wsdl->addComplexType(
  196. 'ProductsArray2',
  197. 'complexType',
  198. 'array',
  199. 'sequence',
  200. '',
  201. array(
  202. 'product' => array(
  203. 'name' => 'product',
  204. 'type' => 'tns:product',
  205. 'minOccurs' => '0',
  206. 'maxOccurs' => 'unbounded'
  207. )
  208. )
  209. );
  210. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  211. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  212. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  213. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  214. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  215. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  216. // Register WSDL
  217. $server->register(
  218. 'getProductOrService',
  219. // Entry values
  220. array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string', 'lang'=>'xsd:string'),
  221. // Exit values
  222. array('result'=>'tns:result', 'product'=>'tns:product'),
  223. $ns,
  224. $ns.'#getProductOrService',
  225. $styledoc,
  226. $styleuse,
  227. 'WS to get product or service'
  228. );
  229. // Register WSDL
  230. $server->register(
  231. 'createProductOrService',
  232. // Entry values
  233. array('authentication'=>'tns:authentication', 'product'=>'tns:product'),
  234. // Exit values
  235. array('result'=>'tns:result', 'id'=>'xsd:string'),
  236. $ns,
  237. $ns.'#createProductOrService',
  238. $styledoc,
  239. $styleuse,
  240. 'WS to create a product or service'
  241. );
  242. // Register WSDL
  243. $server->register(
  244. 'updateProductOrService',
  245. // Entry values
  246. array('authentication'=>'tns:authentication', 'product'=>'tns:product'),
  247. // Exit values
  248. array('result'=>'tns:result', 'id'=>'xsd:string'),
  249. $ns,
  250. $ns.'#updateProductOrService',
  251. $styledoc,
  252. $styleuse,
  253. 'WS to update a product or service'
  254. );
  255. // Register WSDL
  256. $server->register(
  257. 'deleteProductOrService',
  258. // Entry values
  259. array('authentication'=>'tns:authentication', 'listofid'=>'xsd:string'),
  260. // Exit values
  261. array('result'=>'tns:result', 'nbdeleted'=>'xsd:int'),
  262. $ns,
  263. $ns.'#deleteProductOrService',
  264. $styledoc,
  265. $styleuse,
  266. 'WS to delete a product or service'
  267. );
  268. // Register WSDL
  269. $server->register(
  270. 'getListOfProductsOrServices',
  271. // Entry values
  272. array('authentication'=>'tns:authentication', 'filterproduct'=>'tns:filterproduct'),
  273. // Exit values
  274. array('result'=>'tns:result', 'products'=>'tns:ProductsArray2'),
  275. $ns,
  276. $ns.'#getListOfProductsOrServices',
  277. $styledoc,
  278. $styleuse,
  279. 'WS to get list of all products or services id and ref'
  280. );
  281. // Register WSDL
  282. $server->register(
  283. 'getProductsForCategory',
  284. // Entry values
  285. array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'lang'=>'xsd:string'),
  286. // Exit values
  287. array('result'=>'tns:result', 'products'=>'tns:ProductsArray2'),
  288. $ns,
  289. $ns.'#getProductsForCategory',
  290. $styledoc,
  291. $styleuse,
  292. 'WS to get list of all products or services for a category'
  293. );
  294. /**
  295. * Get produt or service
  296. *
  297. * @param array $authentication Array of authentication information
  298. * @param int $id Id of object
  299. * @param string $ref Ref of object
  300. * @param string $ref_ext Ref external of object
  301. * @param string $lang Lang to force
  302. * @return mixed
  303. */
  304. function getProductOrService($authentication, $id = '', $ref = '', $ref_ext = '', $lang = '')
  305. {
  306. global $db, $conf, $langs;
  307. dol_syslog("Function: getProductOrService login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
  308. $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
  309. $langs->setDefaultLang($langcode);
  310. if ($authentication['entity']) {
  311. $conf->entity = $authentication['entity'];
  312. }
  313. // Init and check authentication
  314. $objectresp = array();
  315. $errorcode = '';
  316. $errorlabel = '';
  317. $error = 0;
  318. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  319. // Check parameters
  320. if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
  321. $error++;
  322. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  323. }
  324. if (!$error) {
  325. $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
  326. $langs->setDefaultLang($langcode);
  327. $fuser->getrights();
  328. $nbmax = 10;
  329. if ($fuser->rights->produit->lire || $fuser->rights->service->lire) {
  330. $product = new Product($db);
  331. $result = $product->fetch($id, $ref, $ref_ext);
  332. if ($result > 0) {
  333. $product->load_stock();
  334. $dir = (!empty($conf->product->dir_output) ? $conf->product->dir_output : $conf->service->dir_output);
  335. $pdir = get_exdir($product->id, 2, 0, 0, $product, 'product').$product->ref."/";
  336. $dir = $dir.'/'.$pdir;
  337. if (!empty($product->multilangs[$langs->defaultlang]["label"])) {
  338. $product->label = $product->multilangs[$langs->defaultlang]["label"];
  339. }
  340. if (!empty($product->multilangs[$langs->defaultlang]["description"])) {
  341. $product->description = $product->multilangs[$langs->defaultlang]["description"];
  342. }
  343. if (!empty($product->multilangs[$langs->defaultlang]["note"])) {
  344. $product->note = $product->multilangs[$langs->defaultlang]["note"];
  345. }
  346. $productorservice_result_fields = array(
  347. 'id' => $product->id,
  348. 'ref' => $product->ref,
  349. 'ref_ext' => $product->ref_ext,
  350. 'label' => $product->label,
  351. 'description' => $product->description,
  352. 'date_creation' => dol_print_date($product->date_creation, 'dayhourrfc'),
  353. 'date_modification' => dol_print_date($product->date_modification, 'dayhourrfc'),
  354. 'note' => $product->note,
  355. 'status_tosell' => $product->status,
  356. 'status_tobuy' => $product->status_buy,
  357. 'type' => $product->type,
  358. 'barcode' => $product->barcode,
  359. 'barcode_type' => $product->barcode_type,
  360. 'country_id' => $product->country_id > 0 ? $product->country_id : '',
  361. 'country_code' => $product->country_code,
  362. 'custom_code' => $product->customcode,
  363. 'price_net' => $product->price,
  364. 'price' => $product->price_ttc,
  365. 'price_min_net' => $product->price_min,
  366. 'price_min' => $product->price_min_ttc,
  367. 'price_base_type' => $product->price_base_type,
  368. 'vat_rate' => $product->tva_tx,
  369. //! French VAT NPR
  370. 'vat_npr' => $product->tva_npr,
  371. //! Spanish local taxes
  372. 'localtax1_tx' => $product->localtax1_tx,
  373. 'localtax2_tx' => $product->localtax2_tx,
  374. 'stock_real' => $product->stock_reel,
  375. 'stock_virtual' => $product->stock_theorique,
  376. 'stock_alert' => $product->seuil_stock_alerte,
  377. 'pmp' => $product->pmp,
  378. 'import_key' => $product->import_key,
  379. 'dir' => $pdir,
  380. 'images' => $product->liste_photos($dir, $nbmax)
  381. );
  382. $elementtype = 'product';
  383. //Retrieve all extrafield for thirdsparty
  384. // fetch optionals attributes and labels
  385. $extrafields = new ExtraFields($db);
  386. $extrafields->fetch_name_optionals_label($elementtype, true);
  387. //Get extrafield values
  388. $product->fetch_optionals();
  389. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  390. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  391. $productorservice_result_fields = array_merge($productorservice_result_fields, array('options_'.$key => $product->array_options['options_'.$key]));
  392. }
  393. }
  394. // Create
  395. $objectresp = array(
  396. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  397. 'product'=>$productorservice_result_fields
  398. );
  399. } else {
  400. $error++;
  401. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  402. }
  403. } else {
  404. $error++;
  405. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  406. }
  407. }
  408. if ($error) {
  409. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  410. }
  411. //var_dump($objectresp);exit;
  412. return $objectresp;
  413. }
  414. /**
  415. * Create an invoice
  416. *
  417. * @param array $authentication Array of authentication information
  418. * @param Product $product Product
  419. * @return array Array result
  420. */
  421. function createProductOrService($authentication, $product)
  422. {
  423. global $db, $conf;
  424. $now = dol_now();
  425. dol_syslog("Function: createProductOrService login=".$authentication['login']);
  426. if ($authentication['entity']) {
  427. $conf->entity = $authentication['entity'];
  428. }
  429. // Init and check authentication
  430. $objectresp = array();
  431. $errorcode = '';
  432. $errorlabel = '';
  433. $error = 0;
  434. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  435. // Check parameters
  436. if (empty($product['price_base_type'])) {
  437. if (isset($product['price_net']) && $product['price_net'] > 0) {
  438. $product['price_base_type'] = 'HT';
  439. }
  440. if (isset($product['price']) && $product['price'] > 0) {
  441. $product['price_base_type'] = 'TTC';
  442. }
  443. }
  444. if (isset($product['price_net']) && $product['price_net'] > 0 && isset($product['price']) && $product['price'] > 0) {
  445. $error++; $errorcode = 'KO'; $errorlabel = "You must choose between price or price_net to provide price.";
  446. }
  447. if (!empty($product['barcode']) && empty($product['barcode_type'])) {
  448. $error++; $errorcode = 'KO'; $errorlabel = "You must set a barcode type when setting a barcode.";
  449. }
  450. if (!$error) {
  451. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  452. $newobject = new Product($db);
  453. $newobject->ref = $product['ref'];
  454. $newobject->ref_ext = empty($product['ref_ext']) ? '' : $product['ref_ext'];
  455. $newobject->type = empty($product['type']) ? 0 : $product['type'];
  456. $newobject->label = empty($product['label']) ? '' : $product['label'];
  457. $newobject->description = empty($product['description']) ? '' : $product['description'];
  458. $newobject->note_public = empty($product['note_public']) ? '' : $product['note_public'];
  459. $newobject->note_private = empty($product['note_private']) ? '' : $product['note_private'];
  460. $newobject->status = empty($product['status_tosell']) ? 0 : $product['status_tosell'];
  461. $newobject->status_buy = empty($product['status_tobuy']) ? 0 : $product['status_tobuy'];
  462. $newobject->price = isset($product['price_net']) ? $product['price_net'] : 0;
  463. $newobject->price_ttc = isset($product['price']) ? $product['price'] : 0;
  464. $newobject->tva_tx = empty($product['vat_rate']) ? 0 : $product['vat_rate'];
  465. $newobject->price_base_type = $product['price_base_type'];
  466. $newobject->date_creation = $now;
  467. if (!empty($product['barcode'])) {
  468. $newobject->barcode = $product['barcode'];
  469. $newobject->barcode_type = $product['barcode_type'];
  470. }
  471. $newobject->stock_reel = isset($product['stock_real']) ? $product['stock_real'] : null;
  472. $newobject->pmp = isset($product['pmp']) ? $product['pmp'] : null;
  473. $newobject->seuil_stock_alerte = isset($product['stock_alert']) ? $product['stock_alert'] : null;
  474. $newobject->country_id = isset($product['country_id']) ? $product['country_id'] : 0;
  475. if (!empty($product['country_code'])) {
  476. $newobject->country_id = getCountry($product['country_code'], 3);
  477. }
  478. $newobject->customcode = isset($product['customcode']) ? $product['customcode'] : '';
  479. $newobject->canvas = isset($product['canvas']) ? $product['canvas'] : '';
  480. /*foreach($product['lines'] as $line)
  481. {
  482. $newline=new FactureLigne($db);
  483. $newline->type=$line['type'];
  484. $newline->desc=$line['desc'];
  485. $newline->fk_product=$line['fk_product'];
  486. $newline->total_ht=$line['total_net'];
  487. $newline->total_vat=$line['total_vat'];
  488. $newline->total_ttc=$line['total'];
  489. $newline->vat=$line['vat_rate'];
  490. $newline->qty=$line['qty'];
  491. $newline->fk_product=$line['product_id'];
  492. }*/
  493. //var_dump($product['ref_ext']);
  494. //var_dump($product['lines'][0]['type']);
  495. $elementtype = 'product';
  496. $extrafields = new ExtraFields($db);
  497. $extrafields->fetch_name_optionals_label($elementtype, true);
  498. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  499. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  500. $key = 'options_'.$key;
  501. $newobject->array_options[$key] = $product[$key];
  502. }
  503. }
  504. $db->begin();
  505. $result = $newobject->create($fuser, 0);
  506. if ($result <= 0) {
  507. $error++;
  508. }
  509. if (!$error) {
  510. // Update stock if stock count is provided and differs from database after creation or update
  511. if (isset($product['stock_real']) && $product['stock_real'] != '' && !empty($conf->global->stock->enabled)) {
  512. include_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  513. $savstockreal = $newobject->stock_reel;
  514. $newobject->load_stock('novirtual,nobatch'); // This overwrite ->stock_reel, surely 0 because we have just created product
  515. $getstockreal = $newobject->stock_reel;
  516. if ($savstockreal != $getstockreal) {
  517. $warehouse = new Entrepot($db);
  518. $warehouse->fetch(0, $product['warehouse_ref']);
  519. if ($warehouse->id > 0) {
  520. if (($savstockreal - $getstockreal) > 0) {
  521. $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 0, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
  522. }
  523. if (($savstockreal - $getstockreal) > 0) {
  524. $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 1, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
  525. }
  526. if ($result <= 0) {
  527. $error++;
  528. $newobject->error = 'You set a different value for stock, but correction of stock count (before='.$getstockreal.', after='.$savstockreal.') fails with error '.$newobject->error;
  529. }
  530. } else {
  531. $error++;
  532. $newobject->error = 'You set a different value for stock but we failed to find warehouse '.$product['warehouse_ref'].' to make correction.';
  533. }
  534. }
  535. }
  536. }
  537. if (!$error) {
  538. $db->commit();
  539. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
  540. } else {
  541. $db->rollback();
  542. $error++;
  543. $errorcode = 'KO';
  544. $errorlabel = $newobject->error;
  545. }
  546. }
  547. if ($error) {
  548. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  549. }
  550. return $objectresp;
  551. }
  552. /**
  553. * Update a product or service
  554. *
  555. * @param array $authentication Array of authentication information
  556. * @param Product $product Product
  557. * @return array Array result
  558. */
  559. function updateProductOrService($authentication, $product)
  560. {
  561. global $db, $conf;
  562. $now = dol_now();
  563. dol_syslog("Function: updateProductOrService login=".$authentication['login']);
  564. if ($authentication['entity']) {
  565. $conf->entity = $authentication['entity'];
  566. }
  567. // Init and check authentication
  568. $objectresp = array();
  569. $errorcode = '';
  570. $errorlabel = '';
  571. $error = 0;
  572. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  573. // Check parameters
  574. if ($product['price_net'] > 0) {
  575. $product['price_base_type'] = 'HT';
  576. }
  577. if ($product['price'] > 0) {
  578. $product['price_base_type'] = 'TTC';
  579. }
  580. if ($product['price_net'] > 0 && $product['price'] > 0) {
  581. $error++; $errorcode = 'KO'; $errorlabel = "You must choose between price or price_net to provide price.";
  582. }
  583. if ($product['barcode'] && !$product['barcode_type']) {
  584. $error++; $errorcode = 'KO'; $errorlabel = "You must set a barcode type when setting a barcode.";
  585. }
  586. if (!$error) {
  587. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  588. $newobject = new Product($db);
  589. $newobject->fetch($product['id']);
  590. if (isset($product['ref'])) {
  591. $newobject->ref = $product['ref'];
  592. }
  593. if (isset($product['ref_ext'])) {
  594. $newobject->ref_ext = $product['ref_ext'];
  595. }
  596. $newobject->type = $product['type'];
  597. $newobject->label = $product['label'];
  598. $newobject->description = $product['description'];
  599. $newobject->note = $product['note'];
  600. $newobject->status = $product['status_tosell'];
  601. $newobject->status_buy = $product['status_tobuy'];
  602. $newobject->price = $product['price_net'];
  603. $newobject->price_ttc = $product['price'];
  604. $newobject->tva_tx = $product['vat_rate'];
  605. $newobject->price_base_type = $product['price_base_type'];
  606. $newobject->date_creation = $now;
  607. if ($product['barcode']) {
  608. $newobject->barcode = $product['barcode'];
  609. $newobject->barcode_type = $product['barcode_type'];
  610. }
  611. $newobject->stock_reel = isset($product['stock_real']) ? $product['stock_real'] : null;
  612. $newobject->pmp = isset($product['pmp']) ? $product['pmp'] : null;
  613. $newobject->seuil_stock_alerte = isset($product['stock_alert']) ? $product['stock_alert'] : null;
  614. $newobject->country_id = isset($product['country_id']) ? $product['country_id'] : 0;
  615. if (!empty($product['country_code'])) {
  616. $newobject->country_id = getCountry($product['country_code'], 3);
  617. }
  618. $newobject->customcode = isset($product['customcode']) ? $product['customcode'] : '';
  619. $newobject->canvas = isset($product['canvas']) ? $product['canvas'] : '';
  620. $elementtype = 'product';
  621. $extrafields = new ExtraFields($db);
  622. $extrafields->fetch_name_optionals_label($elementtype, true);
  623. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  624. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  625. $key = 'options_'.$key;
  626. $newobject->array_options[$key] = $product[$key];
  627. }
  628. }
  629. $db->begin();
  630. $result = $newobject->update($newobject->id, $fuser);
  631. if ($result <= 0) {
  632. $error++;
  633. } else {
  634. // Update stock if stock count is provided and differs from database after creation or update
  635. if (isset($product['stock_real']) && $product['stock_real'] != '' && !empty($conf->global->stock->enabled)) {
  636. include_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  637. $savstockreal = $newobject->stock_reel;
  638. $newobject->load_stock('novirtual,nobatch'); // This overwrite ->stock_reel
  639. $getstockreal = $newobject->stock_reel;
  640. if ($savstockreal != $getstockreal) {
  641. $warehouse = new Entrepot($db);
  642. $warehouse->fetch(0, $product['warehouse_ref']);
  643. if ($warehouse->id > 0) {
  644. if (($savstockreal - $getstockreal) > 0) {
  645. $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 0, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
  646. }
  647. if (($savstockreal - $getstockreal) > 0) {
  648. $result = $newobject->correct_stock($fuser, $warehouse->id, ($savstockreal - $getstockreal), 1, 'Correction from external call (Web Service)', 0, 'WS'.dol_print_date($now, 'dayhourlog'));
  649. }
  650. if ($result <= 0) {
  651. $error++;
  652. $newobject->error = 'You set a different value for stock, but correction of stock count (before='.$getstockreal.', after='.$savstockreal.') fails with error '.$newobject->error;
  653. }
  654. } else {
  655. $error++;
  656. $newobject->error = 'You set a different value for stock but we failed to find warehouse '.$product['warehouse_ref'].' to make correction.';
  657. }
  658. }
  659. }
  660. }
  661. if (!$error) {
  662. if ($newobject->price_base_type == 'HT') {
  663. $result = $newobject->updatePrice($newobject->price, $newobject->price_base_type, $fuser);
  664. if ($result <= 0) {
  665. $error++;
  666. }
  667. } elseif ($newobject->price_base_type == 'TTC') {
  668. $result = $newobject->updatePrice($newobject->price_ttc, $newobject->price_base_type, $fuser);
  669. if ($result <= 0) {
  670. $error++;
  671. }
  672. }
  673. }
  674. if (!$error) {
  675. $db->commit();
  676. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
  677. } else {
  678. $db->rollback();
  679. $error++;
  680. $errorcode = 'KO';
  681. $errorlabel = $newobject->error;
  682. }
  683. }
  684. if ($error) {
  685. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  686. }
  687. return $objectresp;
  688. }
  689. /**
  690. * Delete a product or service
  691. *
  692. * @param array $authentication Array of authentication information
  693. * @param string $listofidstring List of id with comma
  694. * @return array Array result
  695. */
  696. function deleteProductOrService($authentication, $listofidstring)
  697. {
  698. global $db, $conf;
  699. dol_syslog("Function: deleteProductOrService login=".$authentication['login']);
  700. if ($authentication['entity']) {
  701. $conf->entity = $authentication['entity'];
  702. }
  703. // Init and check authentication
  704. $objectresp = array();
  705. $errorcode = '';
  706. $errorlabel = '';
  707. $error = 0;
  708. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  709. // User must be defined to user authenticated
  710. global $user;
  711. $user = $fuser;
  712. $listofid = explode(',', trim($listofidstring));
  713. $listofiddeleted = array();
  714. // Check parameters
  715. if (count($listofid) == 0 || empty($listofid[0])) {
  716. $error++; $errorcode = 'KO'; $errorlabel = "List of Id of products or services to delete are required.";
  717. }
  718. if (!$error) {
  719. $firsterror = '';
  720. $db->begin();
  721. foreach ($listofid as $id) {
  722. $newobject = new Product($db);
  723. $result = $newobject->fetch($id);
  724. if ($result == 0) {
  725. $error++;
  726. $firsterror = 'Product or service with id '.$id.' not found';
  727. break;
  728. } else {
  729. $result = $newobject->delete($user);
  730. if ($result <= 0) {
  731. $error++;
  732. $firsterror = $newobject->error;
  733. break;
  734. }
  735. $listofiddeleted[] = $id;
  736. }
  737. }
  738. if (!$error) {
  739. $db->commit();
  740. //$objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'listofid'=>$listofiddeleted);
  741. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'nbdeleted'=>count($listofiddeleted));
  742. } else {
  743. $db->rollback();
  744. $error++;
  745. $errorcode = 'KO';
  746. $errorlabel = $firsterror;
  747. }
  748. }
  749. if ($error) {
  750. //$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel), 'listofid'=>$listofiddeleted);
  751. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel), 'nbdeleted'=>0);
  752. } elseif (count($listofiddeleted) == 0) {
  753. //$objectresp=array('result'=>array('result_code'=>'NOT_FOUND', 'result_label'=>'No product or service with id '.join(',',$listofid).' found'), 'listofid'=>$listofiddeleted);
  754. $objectresp = array('result'=>array('result_code'=>'NOT_FOUND', 'result_label'=>'No product or service with id '.join(',', $listofid).' found'), 'nbdeleted'=>0);
  755. }
  756. return $objectresp;
  757. }
  758. /**
  759. * getListOfProductsOrServices
  760. *
  761. * @param array $authentication Array of authentication information
  762. * @param array $filterproduct Filter fields
  763. * @return array Array result
  764. */
  765. function getListOfProductsOrServices($authentication, $filterproduct)
  766. {
  767. global $db, $conf;
  768. dol_syslog("Function: getListOfProductsOrServices login=".$authentication['login']);
  769. if ($authentication['entity']) {
  770. $conf->entity = $authentication['entity'];
  771. }
  772. // Init and check authentication
  773. $objectresp = array();
  774. $arrayproducts = array();
  775. $errorcode = '';
  776. $errorlabel = '';
  777. $error = 0;
  778. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  779. // Check parameters
  780. if (!$error) {
  781. $sql = "SELECT rowid, ref, ref_ext";
  782. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  783. $sql .= " WHERE entity=".$conf->entity;
  784. foreach ($filterproduct as $key => $val) {
  785. if ($key == 'type' && $val >= 0) {
  786. $sql .= " AND fk_product_type = ".((int) $val);
  787. }
  788. if ($key == 'status_tosell') {
  789. $sql .= " AND tosell = ".((int) $val);
  790. }
  791. if ($key == 'status_tobuy') {
  792. $sql .= " AND tobuy = ".((int) $val);
  793. }
  794. }
  795. $resql = $db->query($sql);
  796. if ($resql) {
  797. $num = $db->num_rows($resql);
  798. $i = 0;
  799. while ($i < $num) {
  800. $obj = $db->fetch_object($resql);
  801. $arrayproducts[] = array('id'=>$obj->rowid, 'ref'=>$obj->ref, 'ref_ext'=>$obj->ref_ext);
  802. $i++;
  803. }
  804. } else {
  805. $error++;
  806. $errorcode = $db->lasterrno();
  807. $errorlabel = $db->lasterror();
  808. }
  809. }
  810. if ($error) {
  811. $objectresp = array(
  812. 'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
  813. 'products'=>$arrayproducts
  814. );
  815. } else {
  816. $objectresp = array(
  817. 'result'=>array('result_code' => 'OK', 'result_label' => ''),
  818. 'products'=>$arrayproducts
  819. );
  820. }
  821. return $objectresp;
  822. }
  823. /**
  824. * Get list of products for a category
  825. *
  826. * @param array $authentication Array of authentication information
  827. * @param array $id Category id
  828. * @param Translate $lang Force lang
  829. * @return array Array result
  830. */
  831. function getProductsForCategory($authentication, $id, $lang = '')
  832. {
  833. global $db, $conf, $langs;
  834. $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
  835. $langs->setDefaultLang($langcode);
  836. dol_syslog("Function: getProductsForCategory login=".$authentication['login']." id=".$id);
  837. if ($authentication['entity']) {
  838. $conf->entity = $authentication['entity'];
  839. }
  840. $objectresp = array();
  841. $errorcode = ''; $errorlabel = '';
  842. $error = 0;
  843. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  844. if (!$error && !$id) {
  845. $error++;
  846. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id must be provided.";
  847. }
  848. if (!$error) {
  849. $langcode = ($lang ? $lang : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
  850. $langs->setDefaultLang($langcode);
  851. $fuser->getrights();
  852. $nbmax = 10;
  853. if ($fuser->rights->produit->lire) {
  854. $categorie = new Categorie($db);
  855. $result = $categorie->fetch($id);
  856. if ($result > 0) {
  857. $table = "product";
  858. $field = "product";
  859. $sql = "SELECT fk_".$field." FROM ".MAIN_DB_PREFIX."categorie_".$table;
  860. $sql .= " WHERE fk_categorie = ".((int) $id);
  861. $sql .= " ORDER BY fk_".$field." ASC";
  862. dol_syslog("getProductsForCategory get id of product into category", LOG_DEBUG);
  863. $res = $db->query($sql);
  864. if ($res) {
  865. $iProduct = 0;
  866. $tmpproduct = new Product($db);
  867. $products = array();
  868. while ($rec = $db->fetch_array($res)) {
  869. $tmpproduct->fetch($rec['fk_'.$field]);
  870. if ($tmpproduct->status > 0) {
  871. $dir = (!empty($conf->product->dir_output) ? $conf->product->dir_output : $conf->service->dir_output);
  872. $pdir = get_exdir($tmpproduct->id, 2, 0, 0, $tmpproduct, 'product').$tmpproduct->id."/photos/";
  873. $dir = $dir.'/'.$pdir;
  874. $products[] = array(
  875. 'id' => $tmpproduct->id,
  876. 'ref' => $tmpproduct->ref,
  877. 'ref_ext' => $tmpproduct->ref_ext,
  878. 'label' => !empty($tmpproduct->multilangs[$langs->defaultlang]["label"]) ? $tmpproduct->multilangs[$langs->defaultlang]["label"] : $tmpproduct->label,
  879. 'description' => !empty($tmpproduct->multilangs[$langs->defaultlang]["description"]) ? $tmpproduct->multilangs[$langs->defaultlang]["description"] : $tmpproduct->description,
  880. 'date_creation' => dol_print_date($tmpproduct->date_creation, 'dayhourrfc'),
  881. 'date_modification' => dol_print_date($tmpproduct->date_modification, 'dayhourrfc'),
  882. 'note' => !empty($tmpproduct->multilangs[$langs->defaultlang]["note"]) ? $tmpproduct->multilangs[$langs->defaultlang]["note"] : $tmpproduct->note,
  883. 'status_tosell' => $tmpproduct->status,
  884. 'status_tobuy' => $tmpproduct->status_buy,
  885. 'type' => $tmpproduct->type,
  886. 'barcode' => $tmpproduct->barcode,
  887. 'barcode_type' => $tmpproduct->barcode_type,
  888. 'country_id' => $tmpproduct->country_id > 0 ? $tmpproduct->country_id : '',
  889. 'country_code' => $tmpproduct->country_code,
  890. 'custom_code' => $tmpproduct->customcode,
  891. 'price_net' => $tmpproduct->price,
  892. 'price' => $tmpproduct->price_ttc,
  893. 'vat_rate' => $tmpproduct->tva_tx,
  894. 'price_base_type' => $tmpproduct->price_base_type,
  895. 'stock_real' => $tmpproduct->stock_reel,
  896. 'stock_alert' => $tmpproduct->seuil_stock_alerte,
  897. 'pmp' => $tmpproduct->pmp,
  898. 'import_key' => $tmpproduct->import_key,
  899. 'dir' => $pdir,
  900. 'images' => $tmpproduct->liste_photos($dir, $nbmax)
  901. );
  902. $elementtype = 'product';
  903. //Retrieve all extrafield for thirdsparty
  904. // fetch optionals attributes and labels
  905. $extrafields = new ExtraFields($db);
  906. $extrafields->fetch_name_optionals_label($elementtype, true);
  907. //Get extrafield values
  908. $tmpproduct->fetch_optionals();
  909. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  910. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  911. $products[$iProduct] = array_merge($products[$iProduct], array('options_'.$key => $tmpproduct->array_options['options_'.$key]));
  912. }
  913. }
  914. $iProduct++;
  915. }
  916. }
  917. // Retour
  918. $objectresp = array(
  919. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  920. 'products'=> $products
  921. );
  922. } else {
  923. $errorcode = 'NORECORDS_FOR_ASSOCIATION'; $errorlabel = 'No products associated'.$sql;
  924. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  925. dol_syslog("getProductsForCategory:: ".$errorcode, LOG_DEBUG);
  926. }
  927. } else {
  928. $error++;
  929. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id;
  930. }
  931. } else {
  932. $error++;
  933. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  934. }
  935. }
  936. if ($error) {
  937. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  938. }
  939. return $objectresp;
  940. }
  941. // Return the results.
  942. $server->service(file_get_contents("php://input"));