server_productorservice.php 38 KB

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