PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminStockMvtController.php

https://bitbucket.org/enurkov/prestashop
PHP | 316 lines | 223 code | 32 blank | 61 comment | 19 complexity | db41278a5852337f73a970b971d2f8b7 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 AdminStockMvtControllerCore extends AdminController
  30. {
  31. public function __construct()
  32. {
  33. $this->context = Context::getContext();
  34. $this->table = 'stock_mvt';
  35. $this->className = 'StockMvt';
  36. $this->identifier = 'id_stock_mvt';
  37. $this->lang = false;
  38. $this->multishop_context = Shop::CONTEXT_ALL;
  39. $this->list_no_link = true;
  40. $this->displayInformation($this->l('This interface allows you to display the stock movement for a selected warehouse.').'<br />');
  41. $this->fields_list = array(
  42. 'product_reference' => array(
  43. 'title' => $this->l('Reference'),
  44. 'width' => 100,
  45. 'havingFilter' => true
  46. ),
  47. 'product_ean13' => array(
  48. 'title' => $this->l('EAN 13'),
  49. 'width' => 75,
  50. 'havingFilter' => true
  51. ),
  52. 'product_upc' => array(
  53. 'title' => $this->l('UPC'),
  54. 'width' => 75,
  55. 'havingFilter' => true
  56. ),
  57. 'product_name' => array(
  58. 'title' => $this->l('Name'),
  59. 'havingFilter' => true
  60. ),
  61. 'warehouse_name' => array(
  62. 'title' => $this->l('Warehouse'),
  63. 'havingFilter' => false,
  64. 'orderby' => true,
  65. 'search' => false,
  66. ),
  67. 'sign' => array(
  68. 'title' => $this->l('Sign'),
  69. 'width' => 100,
  70. 'align' => 'center',
  71. 'type' => 'select',
  72. 'filter_key' => 'a!sign',
  73. 'list' => array(
  74. '1' => $this->l('Increase'),
  75. '-1' => $this->l('Decrease'),
  76. ),
  77. 'icon' => array(
  78. -1 => 'remove_stock.png',
  79. 1 => 'add_stock.png'
  80. ),
  81. ),
  82. 'physical_quantity' => array(
  83. 'title' => $this->l('Quantity'),
  84. 'width' => 40,
  85. 'filter_key' => 'a!physical_quantity'
  86. ),
  87. 'price_te' => array(
  88. 'title' => $this->l('Price (tax excl.)'),
  89. 'width' => 70,
  90. 'align' => 'right',
  91. 'type' => 'price',
  92. 'currency' => true,
  93. 'filter_key' => 'a!price_te'
  94. ),
  95. 'reason' => array(
  96. 'title' => $this->l('Label'),
  97. 'width' => 100,
  98. 'havingFilter' => true
  99. ),
  100. 'employee' => array(
  101. 'title' => $this->l('Employee'),
  102. 'width' => 100,
  103. 'havingFilter' => true
  104. ),
  105. 'date_add' => array(
  106. 'title' => $this->l('Date'),
  107. 'width' => 150,
  108. 'align' => 'right',
  109. 'type' => 'datetime',
  110. 'filter_key' => 'a!date_add'
  111. ),
  112. );
  113. parent::__construct();
  114. }
  115. /**
  116. * AdminController::renderList() override
  117. * @see AdminController::renderList()
  118. */
  119. public function renderList()
  120. {
  121. // removes toolbar btn
  122. $this->toolbar_btn = array();
  123. // overrides select
  124. $this->_select = '
  125. CONCAT(pl.name, \' \', GROUP_CONCAT(IFNULL(al.name, \'\'), \'\')) product_name,
  126. CONCAT(a.employee_lastname, \' \', a.employee_firstname) as employee,
  127. mrl.name as reason,
  128. stock.reference as product_reference,
  129. stock.ean13 as product_ean13,
  130. stock.upc as product_upc,
  131. w.id_currency as id_currency,
  132. w.name as warehouse_name';
  133. // overrides join
  134. $this->_join = 'INNER JOIN '._DB_PREFIX_.'stock stock ON a.id_stock = stock.id_stock
  135. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (
  136. stock.id_product = pl.id_product
  137. AND pl.id_lang = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('pl').'
  138. )
  139. LEFT JOIN `'._DB_PREFIX_.'stock_mvt_reason_lang` mrl ON (
  140. a.id_stock_mvt_reason = mrl.id_stock_mvt_reason
  141. AND mrl.id_lang = '.(int)$this->context->language->id.'
  142. )
  143. LEFT JOIN `'._DB_PREFIX_.'warehouse` w ON (w.id_warehouse = stock.id_warehouse)
  144. LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.id_product_attribute = stock.id_product_attribute)
  145. LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (
  146. al.id_attribute = pac.id_attribute
  147. AND al.id_lang = '.(int)$this->context->language->id.'
  148. )';
  149. // overrides group
  150. $this->_group = 'GROUP BY a.id_stock_mvt';
  151. // overrides where depending on the warehouse
  152. $id_warehouse = (int)$this->getCurrentWarehouseId();
  153. if ($id_warehouse > 0)
  154. {
  155. $this->_where = ' AND w.id_warehouse = '.$id_warehouse;
  156. self::$currentIndex .= '&id_warehouse='.$id_warehouse;
  157. }
  158. // sets the current warehouse
  159. $this->tpl_list_vars['current_warehouse'] = $this->getCurrentWarehouseId();
  160. // sets the list of warehouses
  161. $warehouses = Warehouse::getWarehouses(true);
  162. array_unshift($warehouses, array('id_warehouse' => -1, 'name' => $this->l('All Warehouses')));
  163. $this->tpl_list_vars['list_warehouses'] = $warehouses;
  164. // sets toolbar
  165. $this->initToolbar();
  166. // renders list
  167. $list = parent::renderList();
  168. // if export requested
  169. if (Tools::isSubmit('csv'))
  170. {
  171. if (count($this->_list) > 0)
  172. {
  173. $this->renderCSV();
  174. die;
  175. }
  176. else
  177. $this->displayWarning($this->l('There is nothing to export as CSV.'));
  178. }
  179. return $list;
  180. }
  181. /**
  182. * Gets the current warehouse for this controller
  183. *
  184. * @return int warehouse_id
  185. */
  186. protected function getCurrentWarehouseId()
  187. {
  188. static $warehouse = 0;
  189. if ($warehouse == 0)
  190. {
  191. $warehouse = -1;
  192. if ((int)Tools::getValue('id_warehouse'))
  193. $warehouse = (int)Tools::getValue('id_warehouse');
  194. }
  195. return $warehouse;
  196. }
  197. /**
  198. * AdminController::getList() override
  199. * @see AdminController::getList()
  200. */
  201. public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
  202. {
  203. if (Tools::isSubmit('csv'))
  204. $limit = false;
  205. parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
  206. //If there is a field product_name in the list, check if this field is null and display standard message
  207. foreach ($this->fields_list as $key => $value)
  208. if ($key == 'product_name')
  209. {
  210. $nb_items = count($this->_list);
  211. for ($i = 0; $i < $nb_items; ++$i)
  212. {
  213. $item = &$this->_list[$i];
  214. if (empty($item['product_name']))
  215. $item['product_name'] = $this->l('The name of this product is not available. It may have been deleted from the system.');
  216. }
  217. }
  218. }
  219. /**
  220. * @see AdminController::initToolbar();
  221. */
  222. public function initToolbar()
  223. {
  224. if (Tools::isSubmit('id_warehouse') && (int)Tools::getValue('id_warehouse') != -1)
  225. {
  226. $this->toolbar_btn['export-stock-mvt-csv'] = array(
  227. 'short' => 'Export this list as CSV',
  228. 'href' => $this->context->link->getAdminLink('AdminStockMvt').'&amp;csv&amp;id_warehouse='.(int)$this->getCurrentWarehouseId(),
  229. 'desc' => $this->l('Export (CSV)'),
  230. );
  231. }
  232. parent::initToolbar();
  233. unset($this->toolbar_btn['new']);
  234. }
  235. /**
  236. * Exports CSV
  237. */
  238. public function renderCSV()
  239. {
  240. if (!$this->_list)
  241. return;
  242. // header
  243. if (Tools::getValue('id_warehouse') != -1)
  244. $filename = $this->l('stock_mvt').'_'.Warehouse::getWarehouseNameById((int)Tools::getValue('id_warehouse')).'.csv';
  245. else
  246. $filename = $this->l('stock_mvt').'.csv';
  247. header('Content-type: text/csv');
  248. header('Cache-Control: no-store, no-cache');
  249. header('Content-disposition: attachment; filename="'.$filename);
  250. // puts keys
  251. $keys = array('id_order', 'id_supply_order', 'emloyee_firstname', 'employee_lastname', 'physical_quantity',
  252. 'date_add', 'sign', 'price_te', 'product_name', 'label', 'product_reference', 'product_ean13', 'product_upc');
  253. echo sprintf("%s\n", implode(';', $keys));
  254. // puts rows
  255. foreach ($this->_list as $row)
  256. {
  257. $row_csv = array($row['id_order'], $row['id_supply_order'], $row['employee_firstname'],
  258. $row['employee_lastname'], $row['physical_quantity'], $row['date_add'],
  259. $row['sign'], $row['price_te'], $row['product_name'],
  260. $row['reason'], $row['product_reference'], $row['product_ean13'], $row['product_upc']
  261. );
  262. // puts one row
  263. echo sprintf("%s\n", implode(';', array_map(array('CSVCore', 'wrap'), $row_csv)));
  264. }
  265. }
  266. public function initContent()
  267. {
  268. if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
  269. {
  270. $this->warnings[md5('PS_ADVANCED_STOCK_MANAGEMENT')] = $this->l('You need to activate advanced stock management prior to use this feature.');
  271. return false;
  272. }
  273. parent::initContent();
  274. }
  275. public function initProcess()
  276. {
  277. if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
  278. {
  279. $this->warnings[md5('PS_ADVANCED_STOCK_MANAGEMENT')] = $this->l('You need to activate advanced stock management prior to use this feature.');
  280. return false;
  281. }
  282. parent::initProcess();
  283. }
  284. }