PageRenderTime 70ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/admin/AdminStockManagementController.php

https://bitbucket.org/enurkov/prestashop
PHP | 1156 lines | 898 code | 103 blank | 155 comment | 109 complexity | 636f63dc3484eae29e6afea65c85e9ac MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class AdminStockManagementControllerCore extends AdminController
  30. {
  31. public function __construct()
  32. {
  33. $this->context = Context::getContext();
  34. $this->table = 'product';
  35. $this->className = 'Product';
  36. $this->lang = true;
  37. $this->multishop_context = Shop::CONTEXT_ALL;
  38. $this->fields_list = array(
  39. 'reference' => array(
  40. 'title' => $this->l('Product reference'),
  41. 'align' => 'center',
  42. 'filter_key' => 'a!reference',
  43. 'width' => 200
  44. ),
  45. 'ean13' => array(
  46. 'title' => $this->l('EAN13'),
  47. 'align' => 'center',
  48. 'filter_key' => 'a!ean13',
  49. 'width' => 100
  50. ),
  51. 'upc' => array(
  52. 'title' => $this->l('UPC'),
  53. 'align' => 'center',
  54. 'filter_key' => 'a!upc',
  55. 'width' => 100
  56. ),
  57. 'name' => array(
  58. 'title' => $this->l('Name'),
  59. ),
  60. 'stock' => array(
  61. 'title' => $this->l('Quantity'),
  62. 'hint' => $this->l('Sum of quantities for all warehouses'),
  63. 'width' => 100,
  64. 'orderby' => false,
  65. 'filter' => false,
  66. 'search' => false,
  67. ),
  68. );
  69. parent::__construct();
  70. // overrides confirmation messages specifically for this controller
  71. $this->_conf = array(
  72. 1 => $this->l('The product was successfully added to stock'),
  73. 2 => $this->l('The product was successfully removed from the stock'),
  74. 3 => $this->l('The transfer was successfully completed'),
  75. );
  76. }
  77. /**
  78. * AdminController::renderList() override
  79. * @see AdminController::renderList()
  80. */
  81. public function renderList()
  82. {
  83. // sets actions
  84. $this->addRowAction('details');
  85. $this->addRowAction('addstock');
  86. $this->addRowAction('removestock');
  87. $this->addRowAction('transferstock');
  88. // no link on list rows
  89. $this->list_no_link = true;
  90. // inits toolbar
  91. $this->toolbar_btn = array();
  92. // overrides query
  93. $this->_select = 'a.id_product as id, COUNT(pa.id_product_attribute) as variations';
  94. $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.id_product = a.id_product)'.Shop::addSqlAssociation('product_attribute', 'pa', false);
  95. $this->_where = 'AND a.cache_is_pack = 0 AND a.is_virtual = 0';
  96. $this->_group = 'GROUP BY a.id_product';
  97. // displays informations
  98. $this->displayInformation($this->l('This interface allows you to manage the stocks of each of your products and their variations.').'<br />');
  99. $this->displayInformation($this->l('Through this interface, you can increase quantities (add) and decrease quantities (delete) of products for a given warehouse.'));
  100. $this->displayInformation($this->l('Furthermore, you can move quantities of (transfer) products between warehouses, or within one warehouse.').'<br />');
  101. $this->displayInformation($this->l('Note that if you want to increase quantities of multiple products at once, you can use the "Supply orders" page under the "Stock" menu.').'<br />');
  102. $this->displayInformation($this->l('Finally, you will be asked to specify the state of the quantity you will add:'));
  103. $this->displayInformation($this->l('Usable for sale means that this quantity will be available in your shop(s),'));
  104. $this->displayInformation($this->l('otherwise it will be considered reserved (i.e. for other purposes).'));
  105. return parent::renderList();
  106. }
  107. /**
  108. * AdminController::renderForm() override
  109. * @see AdminController::renderForm()
  110. */
  111. public function renderForm()
  112. {
  113. // gets the product
  114. $id_product = (int)Tools::getValue('id_product');
  115. $id_product_attribute = (int)Tools::getValue('id_product_attribute');
  116. // gets warehouses
  117. $warehouses_add = Warehouse::getWarehouses(true);
  118. $warehouses_remove = Warehouse::getWarehousesByProductId($id_product, $id_product_attribute);
  119. // displays warning if no warehouses
  120. if (!$warehouses_add)
  121. $this->displayWarning($this->l('You have to have Warehouses before adding stock. See Stock/Warehouses'));
  122. //get currencies list
  123. $currencies = Currency::getCurrencies();
  124. $id_default_currency = Configuration::get('PS_CURRENCY_DEFAULT');
  125. $default_currency = Currency::getCurrency($id_default_currency);
  126. if ($default_currency)
  127. $currencies = array_merge(array($default_currency, '-'), $currencies);
  128. // switch, in order to display the form corresponding to the current action
  129. switch ($this->display)
  130. {
  131. case 'addstock' :
  132. // gets the last stock mvt for this product, so we can display the last unit price te and the last quantity added
  133. $last_sm_unit_price_te = $this->l('N/A');
  134. $last_sm_quantity = 0;
  135. $last_sm_quantity_is_usable = -1;
  136. $last_sm = StockMvt::getLastPositiveStockMvt($id_product, $id_product_attribute);
  137. // if there is a stock mvt
  138. if ($last_sm != false)
  139. {
  140. $last_sm_currency = new Currency((int)$last_sm['id_currency']);
  141. $last_sm_quantity = (int)$last_sm['physical_quantity'];
  142. $last_sm_quantity_is_usable = (int)$last_sm['is_usable'];
  143. if (Validate::isLoadedObject($last_sm_currency))
  144. $last_sm_unit_price_te = Tools::displayPrice((float)$last_sm['price_te'], $last_sm_currency);
  145. }
  146. $this->displayInformation($this->l('Note that rolling over the quantity and price fields will give you the details of the last stock movement.'));
  147. // fields in the form
  148. $this->fields_form[]['form'] = array(
  149. 'legend' => array(
  150. 'title' => $this->l('Add product to stock'),
  151. 'image' => '../img/admin/add_stock.png'
  152. ),
  153. 'input' => array(
  154. array(
  155. 'type' => 'hidden',
  156. 'name' => 'is_post',
  157. ),
  158. array(
  159. 'type' => 'hidden',
  160. 'name' => 'id_product',
  161. ),
  162. array(
  163. 'type' => 'hidden',
  164. 'name' => 'id_product_attribute',
  165. ),
  166. array(
  167. 'type' => 'hidden',
  168. 'name' => 'check',
  169. ),
  170. array(
  171. 'type' => 'text',
  172. 'label' => $this->l('Product reference:'),
  173. 'name' => 'reference',
  174. 'size' => 30,
  175. 'disabled' => true,
  176. ),
  177. array(
  178. 'type' => 'text',
  179. 'label' => $this->l('EAN13:'),
  180. 'name' => 'ean13',
  181. 'size' => 15,
  182. 'disabled' => true,
  183. ),
  184. array(
  185. 'type' => 'text',
  186. 'label' => $this->l('UPC:'),
  187. 'name' => 'upc',
  188. 'size' => 15,
  189. 'disabled' => true,
  190. ),
  191. array(
  192. 'type' => 'text',
  193. 'label' => $this->l('Name:'),
  194. 'name' => 'name',
  195. 'size' => 75,
  196. 'disabled' => true,
  197. ),
  198. array(
  199. 'type' => 'text',
  200. 'label' => $this->l('Quantity to add:'),
  201. 'name' => 'quantity',
  202. 'size' => 10,
  203. 'maxlength' => 6,
  204. 'required' => true,
  205. 'desc' => $this->l('Physical quantity to add'),
  206. 'hint' => sprintf(
  207. $this->l('Last physical quantity added : %s (%s)'),
  208. ($last_sm_quantity > 0 ? $last_sm_quantity : $this->l('N/A')),
  209. ($last_sm_quantity > 0 ? ($last_sm_quantity_is_usable >= 0 ? $this->l('usable') : $this->l('not usable')) : $this->l('N/A'))),
  210. ),
  211. array(
  212. 'type' => 'radio',
  213. 'label' => $this->l('Usable for sale?'),
  214. 'name' => 'usable',
  215. 'required' => true,
  216. 'class' => 't',
  217. 'is_bool' => true,
  218. 'values' => array(
  219. array(
  220. 'id' => 'active_on',
  221. 'value' => 1,
  222. 'label' => $this->l('Enabled')
  223. ),
  224. array(
  225. 'id' => 'active_off',
  226. 'value' => 0,
  227. 'label' => $this->l('Disabled')
  228. )
  229. ),
  230. 'desc' => $this->l('Is this quantity usable for sale on shops, or reserved in the warehouse for other purposes?')
  231. ),
  232. array(
  233. 'type' => 'select',
  234. 'label' => $this->l('Warehouse:'),
  235. 'name' => 'id_warehouse',
  236. 'required' => true,
  237. 'options' => array(
  238. 'query' => $warehouses_add,
  239. 'id' => 'id_warehouse',
  240. 'name' => 'name'
  241. ),
  242. 'desc' => $this->l('Select the warehouse where you want to add the product into')
  243. ),
  244. array(
  245. 'type' => 'text',
  246. 'label' => $this->l('Unit price (tax excl.):'),
  247. 'name' => 'price',
  248. 'required' => true,
  249. 'size' => 10,
  250. 'maxlength' => 10,
  251. 'desc' => $this->l('Unit purchase price or unit manufacturing cost for this product (tax excl.)'),
  252. 'hint' => sprintf($this->l('Last unit price (tax excl.): %s'), $last_sm_unit_price_te),
  253. ),
  254. array(
  255. 'type' => 'select',
  256. 'label' => $this->l('Currency:'),
  257. 'name' => 'id_currency',
  258. 'required' => true,
  259. 'options' => array(
  260. 'query' => $currencies,
  261. 'id' => 'id_currency',
  262. 'name' => 'name'
  263. ),
  264. 'desc' => $this->l('The currency associated to the product unit price'),
  265. ),
  266. array(
  267. 'type' => 'select',
  268. 'label' => $this->l('Label:'),
  269. 'name' => 'id_stock_mvt_reason',
  270. 'required' => true,
  271. 'options' => array(
  272. 'query' => StockMvtReason::getStockMvtReasonsWithFilter($this->context->language->id,
  273. array(Configuration::get('PS_STOCK_MVT_TRANSFER_TO')),
  274. 1),
  275. 'id' => 'id_stock_mvt_reason',
  276. 'name' => 'name'
  277. ),
  278. 'desc' => $this->l('Label used in stock movements'),
  279. ),
  280. ),
  281. 'submit' => array(
  282. 'title' => $this->l('Add to stock'),
  283. 'class' => 'button'
  284. )
  285. );
  286. $this->fields_value['usable'] = 1;
  287. break;
  288. case 'removestock' :
  289. $this->fields_form[]['form'] = array(
  290. 'legend' => array(
  291. 'title' => $this->l('Remove product from stock'),
  292. 'image' => '../img/admin/remove_stock.png'
  293. ),
  294. 'input' => array(
  295. array(
  296. 'type' => 'hidden',
  297. 'name' => 'is_post',
  298. ),
  299. array(
  300. 'type' => 'hidden',
  301. 'name' => 'id_product',
  302. ),
  303. array(
  304. 'type' => 'hidden',
  305. 'name' => 'id_product_attribute',
  306. ),
  307. array(
  308. 'type' => 'hidden',
  309. 'name' => 'check',
  310. ),
  311. array(
  312. 'type' => 'text',
  313. 'label' => $this->l('Product reference:'),
  314. 'name' => 'reference',
  315. 'size' => 30,
  316. 'disabled' => true,
  317. ),
  318. array(
  319. 'type' => 'text',
  320. 'label' => $this->l('EAN13:'),
  321. 'name' => 'ean13',
  322. 'size' => 15,
  323. 'disabled' => true,
  324. ),
  325. array(
  326. 'type' => 'text',
  327. 'label' => $this->l('Name:'),
  328. 'name' => 'name',
  329. 'size' => 75,
  330. 'disabled' => true,
  331. ),
  332. array(
  333. 'type' => 'text',
  334. 'label' => $this->l('Quantity to remove:'),
  335. 'name' => 'quantity',
  336. 'size' => 10,
  337. 'maxlength' => 6,
  338. 'required' => true,
  339. 'desc' => $this->l('Physical quantity to remove'),
  340. ),
  341. array(
  342. 'type' => 'radio',
  343. 'label' => $this->l('Usable for sale:'),
  344. 'name' => 'usable',
  345. 'required' => true,
  346. 'class' => 't',
  347. 'is_bool' => true,
  348. 'values' => array(
  349. array(
  350. 'id' => 'active_on',
  351. 'value' => 1,
  352. 'label' => $this->l('Enabled')
  353. ),
  354. array(
  355. 'id' => 'active_off',
  356. 'value' => 0,
  357. 'label' => $this->l('Disabled')
  358. )
  359. ),
  360. 'desc' => $this->l('Do you want to remove this quantity from the usable quantity (yes) or the physical quantity (no)?')
  361. ),
  362. array(
  363. 'type' => 'select',
  364. 'label' => $this->l('Warehouse:'),
  365. 'name' => 'id_warehouse',
  366. 'required' => true,
  367. 'options' => array(
  368. 'query' => $warehouses_remove,
  369. 'id' => 'id_warehouse',
  370. 'name' => 'name'
  371. ),
  372. 'desc' => $this->l('Select the warehouse from where you want to remove the product')
  373. ),
  374. array(
  375. 'type' => 'select',
  376. 'label' => $this->l('Label:'),
  377. 'name' => 'id_stock_mvt_reason',
  378. 'required' => true,
  379. 'options' => array(
  380. 'query' => StockMvtReason::getStockMvtReasonsWithFilter($this->context->language->id,
  381. array(Configuration::get('PS_STOCK_MVT_TRANSFER_FROM')),
  382. -1),
  383. 'id' => 'id_stock_mvt_reason',
  384. 'name' => 'name'
  385. ),
  386. 'desc' => $this->l('Label used in stock movements'),
  387. ),
  388. ),
  389. 'submit' => array(
  390. 'title' => $this->l('Remove from stock'),
  391. 'class' => 'button'
  392. )
  393. );
  394. break;
  395. case 'transferstock' :
  396. $this->fields_form[]['form'] = array(
  397. 'legend' => array(
  398. 'title' => $this->l('Transfer product from one warehouse to another'),
  399. 'image' => '../img/admin/transfer_stock.png'
  400. ),
  401. 'input' => array(
  402. array(
  403. 'type' => 'hidden',
  404. 'name' => 'is_post',
  405. ),
  406. array(
  407. 'type' => 'hidden',
  408. 'name' => 'id_product',
  409. ),
  410. array(
  411. 'type' => 'hidden',
  412. 'name' => 'id_product_attribute',
  413. ),
  414. array(
  415. 'type' => 'hidden',
  416. 'name' => 'check',
  417. ),
  418. array(
  419. 'type' => 'text',
  420. 'label' => $this->l('Product reference:'),
  421. 'name' => 'reference',
  422. 'size' => 30,
  423. 'disabled' => true,
  424. ),
  425. array(
  426. 'type' => 'text',
  427. 'label' => $this->l('EAN13:'),
  428. 'name' => 'ean13',
  429. 'size' => 15,
  430. 'disabled' => true,
  431. ),
  432. array(
  433. 'type' => 'text',
  434. 'label' => $this->l('Name:'),
  435. 'name' => 'name',
  436. 'size' => 75,
  437. 'disabled' => true,
  438. ),
  439. array(
  440. 'type' => 'text',
  441. 'label' => $this->l('Quantity to transfer:'),
  442. 'name' => 'quantity',
  443. 'size' => 10,
  444. 'maxlength' => 6,
  445. 'required' => true,
  446. 'desc' => $this->l('Quantity to transfer:')
  447. ),
  448. array(
  449. 'type' => 'select',
  450. 'label' => $this->l('Source Warehouse:'),
  451. 'name' => 'id_warehouse_from',
  452. 'required' => true,
  453. 'options' => array(
  454. 'query' => $warehouses_remove,
  455. 'id' => 'id_warehouse',
  456. 'name' => 'name'
  457. ),
  458. 'desc' => $this->l('Select the warehouse from which you want to transfer the product.')
  459. ),
  460. array(
  461. 'type' => 'radio',
  462. 'label' => $this->l('Usable for sale in source warehouse?'),
  463. 'name' => 'usable_from',
  464. 'required' => true,
  465. 'class' => 't',
  466. 'is_bool' => true,
  467. 'values' => array(
  468. array(
  469. 'id' => 'active_on',
  470. 'value' => 1,
  471. 'label' => $this->l('Enabled')
  472. ),
  473. array(
  474. 'id' => 'active_off',
  475. 'value' => 0,
  476. 'label' => $this->l('Disabled')
  477. )
  478. ),
  479. 'desc' => $this->l('Is this a usable quantity for sale?')
  480. ),
  481. array(
  482. 'type' => 'select',
  483. 'label' => $this->l('Destination Warehouse:'),
  484. 'name' => 'id_warehouse_to',
  485. 'required' => true,
  486. 'options' => array(
  487. 'query' => $warehouses_add,
  488. 'id' => 'id_warehouse',
  489. 'name' => 'name'
  490. ),
  491. 'desc' => $this->l('Select the warehouse to which to transfer the product.')
  492. ),
  493. array(
  494. 'type' => 'radio',
  495. 'label' => $this->l('Usable for sale in destination warehouse?'),
  496. 'name' => 'usable_to',
  497. 'required' => true,
  498. 'class' => 't',
  499. 'is_bool' => true,
  500. 'values' => array(
  501. array(
  502. 'id' => 'active_on',
  503. 'value' => 1,
  504. 'label' => $this->l('Enabled')
  505. ),
  506. array(
  507. 'id' => 'active_off',
  508. 'value' => 0,
  509. 'label' => $this->l('Disabled')
  510. )
  511. ),
  512. 'desc' => $this->l('Do you want it to be usable for sale?')
  513. ),
  514. ),
  515. 'submit' => array(
  516. 'title' => $this->l('Transfer'),
  517. 'class' => 'button'
  518. )
  519. );
  520. break;
  521. }
  522. $this->initToolbar();
  523. }
  524. /**
  525. * AdminController::postProcess() override
  526. * @see AdminController::postProcess()
  527. */
  528. public function postProcess()
  529. {
  530. parent::postProcess();
  531. // Checks access
  532. if (Tools::isSubmit('addStock') && !($this->tabAccess['add'] === '1'))
  533. $this->errors[] = Tools::displayError('You do not have the required permission to add stock.');
  534. if (Tools::isSubmit('removeStock') && !($this->tabAccess['delete'] === '1'))
  535. $this->errors[] = Tools::displayError('You do not have the required permission to delete stock.');
  536. if (Tools::isSubmit('transferStock') && !($this->tabAccess['edit'] === '1'))
  537. $this->errors[] = Tools::displayError('You do not have the required permission to transfer stock.');
  538. if (count($this->errors))
  539. return;
  540. // Global checks when add / remove / transfer product
  541. if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock') || Tools::isSubmit('transferstock') ) && Tools::isSubmit('is_post'))
  542. {
  543. // get product ID
  544. $id_product = (int)Tools::getValue('id_product', 0);
  545. if ($id_product <= 0)
  546. $this->errors[] = Tools::displayError('The selected product is not valid.');
  547. // get product_attribute ID
  548. $id_product_attribute = (int)Tools::getValue('id_product_attribute', 0);
  549. // check the product hash
  550. $check = Tools::getValue('check', '');
  551. $check_valid = md5(_COOKIE_KEY_.$id_product.$id_product_attribute);
  552. if ($check != $check_valid)
  553. $this->errors[] = Tools::displayError('The selected product is not valid.');
  554. // get quantity and check that the post value is really an integer
  555. // If it's not, we have nothing to do
  556. $quantity = Tools::getValue('quantity', 0);
  557. if (!is_numeric($quantity) || (int)$quantity <= 0)
  558. $this->errors[] = Tools::displayError('The quantity value is not valid.');
  559. $quantity = (int)$quantity;
  560. $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
  561. $redirect = self::$currentIndex.'&token='.$token;
  562. }
  563. // Global checks when add / remove product
  564. if ((Tools::isSubmit('addstock') || Tools::isSubmit('removestock') ) && Tools::isSubmit('is_post'))
  565. {
  566. // get warehouse id
  567. $id_warehouse = (int)Tools::getValue('id_warehouse', 0);
  568. if ($id_warehouse <= 0 || !Warehouse::exists($id_warehouse))
  569. $this->errors[] = Tools::displayError('The selected warehouse is not valid.');
  570. // get stock movement reason id
  571. $id_stock_mvt_reason = (int)Tools::getValue('id_stock_mvt_reason', 0);
  572. if ($id_stock_mvt_reason <= 0 || !StockMvtReason::exists($id_stock_mvt_reason))
  573. $this->errors[] = Tools::displayError('The reason is not valid.');
  574. // get usable flag
  575. $usable = Tools::getValue('usable', null);
  576. if (is_null($usable))
  577. $this->errors[] = Tools::displayError('You have to specify if the product quantity is usable for sale on shops.');
  578. $usable = (bool)$usable;
  579. }
  580. if (Tools::isSubmit('addstock') && Tools::isSubmit('is_post'))
  581. {
  582. // get product unit price
  583. $price = str_replace(',', '.', Tools::getValue('price', 0));
  584. if (!is_numeric($price))
  585. $this->errors[] = Tools::displayError('The product price is not valid.');
  586. $price = round(floatval($price), 6);
  587. // get product unit price currency id
  588. $id_currency = (int)Tools::getValue('id_currency', 0);
  589. if ($id_currency <= 0 || ( !($result = Currency::getCurrency($id_currency)) || empty($result) ))
  590. $this->errors[] = Tools::displayError('The selected currency is not valid.');
  591. // if all is ok, add stock
  592. if (count($this->errors) == 0)
  593. {
  594. $warehouse = new Warehouse($id_warehouse);
  595. // convert price to warehouse currency if needed
  596. if ($id_currency != $warehouse->id_currency)
  597. {
  598. // First convert price to the default currency
  599. $price_converted_to_default_currency = Tools::convertPrice($price, $id_currency, false);
  600. // Convert the new price from default currency to needed currency
  601. $price = Tools::convertPrice($price_converted_to_default_currency, $warehouse->id_currency, true);
  602. }
  603. // add stock
  604. $stock_manager = StockManagerFactory::getManager();
  605. if ($stock_manager->addProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $price, $usable))
  606. {
  607. StockAvailable::synchronize($id_product);
  608. if (Tools::isSubmit('addstockAndStay'))
  609. {
  610. $redirect = self::$currentIndex.'&id_product='.(int)$id_product;
  611. if ($id_product_attribute)
  612. $redirect .= '&id_product_attribute='.(int)$id_product_attribute;
  613. $redirect .= '&addstock&token='.$token;
  614. }
  615. Tools::redirectAdmin($redirect.'&conf=1');
  616. }
  617. else
  618. $this->errors[] = Tools::displayError('An error occurred. No stock was added.');
  619. }
  620. }
  621. if (Tools::isSubmit('removestock') && Tools::isSubmit('is_post'))
  622. {
  623. // if all is ok, remove stock
  624. if (count($this->errors) == 0)
  625. {
  626. $warehouse = new Warehouse($id_warehouse);
  627. // remove stock
  628. $stock_manager = StockManagerFactory::getManager();
  629. $removed_products = $stock_manager->removeProduct($id_product, $id_product_attribute, $warehouse, $quantity, $id_stock_mvt_reason, $usable);
  630. if (count($removed_products) > 0)
  631. {
  632. StockAvailable::synchronize($id_product);
  633. Tools::redirectAdmin($redirect.'&conf=2');
  634. }
  635. else
  636. {
  637. $physical_quantity_in_stock = (int)$stock_manager->getProductPhysicalQuantities($id_product, $id_product_attribute, array($warehouse->id), false);
  638. $usable_quantity_in_stock = (int)$stock_manager->getProductPhysicalQuantities($id_product, $id_product_attribute, array($warehouse->id), true);
  639. $not_usable_quantity = ($physical_quantity_in_stock - $usable_quantity_in_stock);
  640. if ($usable_quantity_in_stock < $quantity)
  641. $this->errors[] = sprintf(Tools::displayError('You don\'t have enough usable quantity. Could not remove %d out of %d.'), (int)$quantity, (int)$usable_quantity_in_stock);
  642. else if ($not_usable_quantity < $quantity)
  643. $this->errors[] = sprintf(Tools::displayError('You don\'t have enough quantity (not usable). Could not remove %d out of %d.'), (int)$quantity, (int)$not_usable_quantity);
  644. else
  645. $this->errors[] = Tools::displayError('It is not possible to remove the specified quantity or an error occurred. No stock was removed.');
  646. }
  647. }
  648. }
  649. if (Tools::isSubmit('transferstock') && Tools::isSubmit('is_post'))
  650. {
  651. // get source warehouse id
  652. $id_warehouse_from = (int)Tools::getValue('id_warehouse_from', 0);
  653. if ($id_warehouse_from <= 0 || !Warehouse::exists($id_warehouse_from))
  654. $this->errors[] = Tools::displayError('The source warehouse is not valid.');
  655. // get destination warehouse id
  656. $id_warehouse_to = (int)Tools::getValue('id_warehouse_to', 0);
  657. if ($id_warehouse_to <= 0 || !Warehouse::exists($id_warehouse_to))
  658. $this->errors[] = Tools::displayError('The destination warehouse is not valid.');
  659. // get usable flag for source warehouse
  660. $usable_from = Tools::getValue('usable_from', null);
  661. if (is_null($usable_from))
  662. $this->errors[] = Tools::displayError('You have to specify if the product quantity is usable for sale on shops in source warehouse.');
  663. $usable_from = (bool)$usable_from;
  664. // get usable flag for destination warehouse
  665. $usable_to = Tools::getValue('usable_to', null);
  666. if (is_null($usable_to))
  667. $this->errors[] = Tools::displayError('You have to specify if the product quantity is usable for sale on shops in destination warehouse.');
  668. $usable_to = (bool)$usable_to;
  669. // if we can process stock transfers
  670. if (count($this->errors) == 0)
  671. {
  672. // transfer stock
  673. $stock_manager = StockManagerFactory::getManager();
  674. $is_transfer = $stock_manager->transferBetweenWarehouses(
  675. $id_product,
  676. $id_product_attribute,
  677. $quantity,
  678. $id_warehouse_from,
  679. $id_warehouse_to,
  680. $usable_from,
  681. $usable_to
  682. );
  683. StockAvailable::synchronize($id_product);
  684. if ($is_transfer)
  685. Tools::redirectAdmin($redirect.'&conf=3');
  686. else
  687. $this->errors[] = Tools::displayError('It is not possible to transfer the specified quantity, or an error occurred. No stock was transferred.');
  688. }
  689. }
  690. }
  691. /**
  692. * assign default action in toolbar_btn smarty var, if they are not set.
  693. * uses override to specifically add, modify or remove items
  694. *
  695. */
  696. public function initToolbar()
  697. {
  698. switch ($this->display)
  699. {
  700. case 'addstock':
  701. $this->toolbar_btn['save-and-stay'] = array(
  702. 'short' => 'SaveAndStay',
  703. 'href' => '#',
  704. 'desc' => $this->l('Save and stay'),
  705. );
  706. case 'removestock':
  707. case 'transferstock':
  708. $this->toolbar_btn['save'] = array(
  709. 'href' => '#',
  710. 'desc' => $this->l('Save')
  711. );
  712. // Default cancel button - like old back link
  713. $back = Tools::safeOutput(Tools::getValue('back', ''));
  714. if (empty($back))
  715. $back = self::$currentIndex.'&token='.$this->token;
  716. $this->toolbar_btn['cancel'] = array(
  717. 'href' => $back,
  718. 'desc' => $this->l('Cancel')
  719. );
  720. break;
  721. default:
  722. parent::initToolbar();
  723. }
  724. }
  725. /**
  726. * AdminController::init() override
  727. * @see AdminController::init()
  728. */
  729. public function init()
  730. {
  731. parent::init();
  732. if (Tools::isSubmit('addstock'))
  733. {
  734. $this->display = 'addstock';
  735. $this->toolbar_title = $this->l('Stock: Add product');
  736. }
  737. if (Tools::isSubmit('removestock'))
  738. {
  739. $this->display = 'removestock';
  740. $this->toolbar_title = $this->l('Stock: Remove product');
  741. }
  742. if (Tools::isSubmit('transferstock'))
  743. {
  744. $this->display = 'transferstock';
  745. $this->toolbar_title = $this->l('Stock: Transfer product');
  746. }
  747. }
  748. /**
  749. * method call when ajax request is made with the details row action
  750. * @see AdminController::postProcess()
  751. */
  752. public function ajaxProcess()
  753. {
  754. // test if an id is submit
  755. if (Tools::isSubmit('id'))
  756. {
  757. // override attributes
  758. $this->identifier = 'id_product_attribute';
  759. $this->display = 'list';
  760. $this->lang = false;
  761. $this->addRowAction('addstock');
  762. $this->addRowAction('removestock');
  763. $this->addRowAction('transferstock');
  764. // get current lang id
  765. $lang_id = (int)$this->context->language->id;
  766. // Get product id
  767. $product_id = (int)Tools::getValue('id');
  768. // Load product attributes with sql override
  769. $this->table = 'product_attribute';
  770. $this->_select = 'a.id_product_attribute as id, a.id_product, a.reference, a.ean13, a.upc';
  771. $this->_where = 'AND a.id_product = '.$product_id;
  772. $this->_group = 'GROUP BY a.id_product_attribute';
  773. // get list and force no limit clause in the request
  774. $this->getList($this->context->language->id, null, null, 0, false);
  775. // Render list
  776. $helper = new HelperList();
  777. $helper->bulk_actions = array();
  778. $helper->toolbar_scroll = $this->toolbar_scroll;
  779. $helper->show_toolbar = false;
  780. $helper->actions = $this->actions;
  781. $helper->list_skip_actions = $this->list_skip_actions;
  782. $helper->no_link = true;
  783. $helper->shopLinkType = '';
  784. $helper->identifier = $this->identifier;
  785. // Force render - no filter, form, js, sorting ...
  786. $helper->simple_header = true;
  787. $content = $helper->generateList($this->_list, $this->fields_list);
  788. echo Tools::jsonEncode(array('use_parent_structure' => false, 'data' => $content));
  789. }
  790. die;
  791. }
  792. /**
  793. * AdminController::getList() override
  794. * @see AdminController::getList()
  795. */
  796. public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
  797. {
  798. parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
  799. if ($this->display == 'list')
  800. {
  801. // Check each row to see if there are combinations and get the correct action in consequence
  802. $nb_items = count($this->_list);
  803. for ($i = 0; $i < $nb_items; $i++)
  804. {
  805. $item = &$this->_list[$i];
  806. // if it's an ajax request we have to consider manipulating a product variation
  807. if ($this->ajax == '1')
  808. {
  809. $item['name'] = Product::getProductName($item['id_product'], $item['id']);
  810. // no details for this row
  811. $this->addRowActionSkipList('details', array($item['id']));
  812. // specify actions in function of stock
  813. $this->skipActionByStock($item, true);
  814. }
  815. // If current product has variations
  816. else if ((int)$item['variations'] > 0)
  817. {
  818. // we have to desactivate stock actions on current row
  819. $this->addRowActionSkipList('addstock', array($item['id']));
  820. $this->addRowActionSkipList('removestock', array($item['id']));
  821. $this->addRowActionSkipList('transferstock', array($item['id']));
  822. // does not display these informaions because this product has combinations
  823. $item['reference'] = '--';
  824. $item['ean13'] = '--';
  825. $item['upc'] = '--';
  826. }
  827. else
  828. {
  829. //there are no variations of current product, so we don't want to show details action
  830. $this->addRowActionSkipList('details', array($item['id']));
  831. // specify actions in function of stock
  832. $this->skipActionByStock($item, false);
  833. }
  834. // Checks access
  835. if (!($this->tabAccess['add'] === '1'))
  836. $this->addRowActionSkipList('addstock', array($item['id']));
  837. if (!($this->tabAccess['delete'] === '1'))
  838. $this->addRowActionSkipList('removestock', array($item['id']));
  839. if (!($this->tabAccess['edit'] === '1'))
  840. $this->addRowActionSkipList('transferstock', array($item['id']));
  841. }
  842. }
  843. }
  844. /**
  845. * Check stock for a given product or product attribute
  846. * and manage available actions in consequence
  847. *
  848. * @param array $item reference to the current item
  849. * @param bool $is_product_attribute specify if it's a product or a product variation
  850. */
  851. protected function skipActionByStock(&$item, $is_product_variation = false)
  852. {
  853. $stock_manager = StockManagerFactory::getManager();
  854. //get stocks for this product
  855. if ($is_product_variation)
  856. $stock = $stock_manager->getProductPhysicalQuantities($item['id_product'], $item['id']);
  857. else
  858. $stock = $stock_manager->getProductPhysicalQuantities($item['id'], 0);
  859. //affects stock to the list for display
  860. $item['stock'] = $stock;
  861. if ($stock <= 0)
  862. {
  863. //there is no stock, we can only add stock
  864. $this->addRowActionSkipList('removestock', array($item['id']));
  865. $this->addRowActionSkipList('transferstock', array($item['id']));
  866. }
  867. }
  868. /**
  869. * AdminController::initContent() override
  870. * @see AdminController::initContent()
  871. */
  872. public function initContent()
  873. {
  874. if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
  875. {
  876. $this->warnings[md5('PS_ADVANCED_STOCK_MANAGEMENT')] = $this->l('You need to activate advanced stock management prior to use this feature.');
  877. return false;
  878. }
  879. // Manage the add stock form
  880. if ($this->display == 'addstock' || $this->display == 'removestock' || $this->display == 'transferstock')
  881. {
  882. if (Tools::isSubmit('id_product') || Tools::isSubmit('id_product_attribute'))
  883. {
  884. // get id product and product attribute if possible
  885. $id_product = (int)Tools::getValue('id_product', 0);
  886. $id_product_attribute = (int)Tools::getValue('id_product_attribute', 0);
  887. $product_is_valid = false;
  888. $is_pack = false;
  889. $is_virtual = false;
  890. $lang_id = $this->context->language->id;
  891. // try to load product attribute first
  892. if ($id_product_attribute > 0)
  893. {
  894. // try to load product attribute
  895. $combination = new Combination($id_product_attribute);
  896. if (Validate::isLoadedObject($combination))
  897. {
  898. $product_is_valid = true;
  899. $id_product = $combination->id_product;
  900. $reference = $combination->reference;
  901. $ean13 = $combination->ean13;
  902. $upc = $combination->upc;
  903. $manufacturer_reference = $combination->supplier_reference;
  904. // get the full name for this combination
  905. $query = new DbQuery();
  906. $query->select('IFNULL(CONCAT(pl.`name`, \' : \', GROUP_CONCAT(agl.`name`, \' - \', al.`name` SEPARATOR \', \')),pl.`name`) as name');
  907. $query->from('product_attribute', 'a');
  908. $query->join('INNER JOIN '._DB_PREFIX_.'product_lang pl ON (pl.`id_product` = a.`id_product` AND pl.`id_lang` = '.(int)$lang_id.')
  909. LEFT JOIN '._DB_PREFIX_.'product_attribute_combination pac ON (pac.`id_product_attribute` = a.`id_product_attribute`)
  910. LEFT JOIN '._DB_PREFIX_.'attribute atr ON (atr.`id_attribute` = pac.`id_attribute`)
  911. LEFT JOIN '._DB_PREFIX_.'attribute_lang al ON (al.`id_attribute` = atr.`id_attribute` AND al.`id_lang` = '.(int)$lang_id.')
  912. LEFT JOIN '._DB_PREFIX_.'attribute_group_lang agl ON (agl.`id_attribute_group` = atr.`id_attribute_group` AND agl.`id_lang` = '.(int)$lang_id.')'
  913. );
  914. $query->where('a.`id_product_attribute` = '.$id_product_attribute);
  915. $name = Db::getInstance()->getValue($query);
  916. }
  917. }
  918. // try to load a simple product
  919. else
  920. {
  921. $product = new Product($id_product, false, $lang_id);
  922. if (is_int($product->id))
  923. {
  924. $product_is_valid = true;
  925. $reference = $product->reference;
  926. $ean13 = $product->ean13;
  927. $upc = $product->upc;
  928. $name = $product->name;
  929. $manufacturer_reference = $product->supplier_reference;
  930. $is_pack = $product->cache_is_pack;
  931. $is_virtual = $product->is_virtual;
  932. }
  933. }
  934. if ($product_is_valid === true && $is_pack == false && $is_virtual == false)
  935. {
  936. // init form
  937. $this->renderForm();
  938. $this->getlanguages();
  939. $helper = new HelperForm();
  940. // Check if form template has been overriden
  941. if (file_exists($this->context->smarty->getTemplateDir(0).'/'.$this->tpl_folder.'form.tpl'))
  942. $helper->tpl = $this->tpl_folder.'form.tpl';
  943. $this->setHelperDisplay($helper);
  944. $helper->submit_action = $this->display;
  945. $helper->id = null; // no display standard hidden field in the form
  946. $helper->languages = $this->_languages;
  947. $helper->default_form_language = $this->default_form_language;
  948. $helper->allow_employee_form_lang = $this->allow_employee_form_lang;
  949. $helper->fields_value = array(
  950. 'id_product' => $id_product,
  951. 'id_product_attribute' => $id_product_attribute,
  952. 'reference' => $reference,
  953. 'manufacturer_reference' => $manufacturer_reference,
  954. 'name' => $name,
  955. 'ean13' => $ean13,
  956. 'upc' => $upc,
  957. 'check' => md5(_COOKIE_KEY_.$id_product.$id_product_attribute),
  958. 'quantity' => Tools::getValue('quantity', ''),
  959. 'id_warehouse' => Tools::getValue('id_warehouse', ''),
  960. 'usable' => ($this->fields_value['usable'] ? $this->fields_value['usable'] : Tools::getValue('usable', '')),
  961. 'price' => Tools::getValue('price', ''),
  962. 'id_currency' => Tools::getValue('id_currency', ''),
  963. 'id_stock_mvt_reason' => Tools::getValue('id_stock_mvt_reason', ''),
  964. 'is_post' => 1,
  965. );
  966. if ($this->display == 'addstock')
  967. $_POST['id_product'] = (int)$id_product;
  968. if ($this->display == 'transferstock')
  969. {
  970. $helper->fields_value['id_warehouse_from'] = Tools::getValue('id_warehouse_from', '');
  971. $helper->fields_value['id_warehouse_to'] = Tools::getValue('id_warehouse_to', '');
  972. $helper->fields_value['usable_from'] = Tools::getValue('usable_from', '');
  973. $helper->fields_value['usable_to'] = Tools::getValue('usable_to', '');
  974. }
  975. $this->content .= $helper->generateForm($this->fields_form);
  976. $this->context->smarty->assign(array(
  977. 'content' => $this->content
  978. ));
  979. }
  980. else
  981. $this->errors[] = Tools::displayError('The specified product is not valid');
  982. }
  983. }
  984. else
  985. {
  986. $this->display = 'list';
  987. parent::initContent();
  988. }
  989. }
  990. /**
  991. * Display addstock action link
  992. * @param string $token the token to add to the link
  993. * @param int $id the identifier to add to the link
  994. * @return string
  995. */
  996. public function displayAddstockLink($token = null, $id)
  997. {
  998. if (!array_key_exists('AddStock', self::$cache_lang))
  999. self::$cache_lang['AddStock'] = $this->l('Add stock');
  1000. $this->context->smarty->assign(array(
  1001. 'href' => self::$currentIndex.
  1002. '&'.$this->identifier.'='.$id.
  1003. '&addstock&token='.($token != null ? $token : $this->token),
  1004. 'action' => self::$cache_lang['AddStock'],
  1005. ));
  1006. return $this->context->smarty->fetch('helpers/list/list_action_addstock.tpl');
  1007. }
  1008. /**
  1009. * Display removestock action link
  1010. * @param string $token the token to add to the link
  1011. * @param int $id the identifier to add to the link
  1012. * @return string
  1013. */
  1014. public function displayRemovestockLink($token = null, $id)
  1015. {
  1016. if (!array_key_exists('RemoveStock', self::$cache_lang))
  1017. self::$cache_lang['RemoveStock'] = $this->l('Remove stock');
  1018. $this->context->smarty->assign(array(
  1019. 'href' => self::$currentIndex.
  1020. '&'.$this->identifier.'='.$id.
  1021. '&removestock&token='.($token != null ? $token : $this->token),
  1022. 'action' => self::$cache_lang['RemoveStock'],
  1023. ));
  1024. return $this->context->smarty->fetch('helpers/list/list_action_removestock.tpl');
  1025. }
  1026. /**
  1027. * Display transferstock action link
  1028. * @param string $token the token to add to the link
  1029. * @param int $id the identifier to add to the link
  1030. * @return string
  1031. */
  1032. public function displayTransferstockLink($token = null, $id)
  1033. {
  1034. if (!array_key_exists('TransferStock', self::$cache_lang))
  1035. self::$cache_lang['TransferStock'] = $this->l('Transfer stock');
  1036. $this->context->smarty->assign(array(
  1037. 'href' => self::$currentIndex.
  1038. '&'.$this->identifier.'='.$id.
  1039. '&transferstock&token='.($token != null ? $token : $this->token),
  1040. 'action' => self::$cache_lang['TransferStock'],
  1041. ));
  1042. return $this->context->smarty->fetch('helpers/list/list_action_transferstock.tpl');
  1043. }
  1044. public function initProcess()
  1045. {
  1046. if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
  1047. {
  1048. $this->warnings[md5('PS_ADVANCED_STOCK_MANAGEMENT')] = $this->l('You need to activate advanced stock management prior to use this feature.');
  1049. return false;
  1050. }
  1051. parent::initProcess();
  1052. }
  1053. }