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

/controllers/admin/AdminSupplyOrdersController.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 2233 lines | 1650 code | 280 blank | 303 comment | 248 complexity | f77f17c5dea2f3090a6b9a9c9960c3c4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * 2007-2014 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-2014 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 AdminSupplyOrdersControllerCore extends AdminController
  30. {
  31. /*
  32. * @var array List of warehouses
  33. */
  34. protected $warehouses;
  35. public function __construct()
  36. {
  37. $this->bootstrap = true;
  38. $this->context = Context::getContext();
  39. $this->table = 'supply_order';
  40. $this->className = 'SupplyOrder';
  41. $this->identifier = 'id_supply_order';
  42. $this->lang = false;
  43. $this->is_template_list = false;
  44. $this->multishop_context = Shop::CONTEXT_ALL;
  45. $this->addRowAction('updatereceipt');
  46. $this->addRowAction('changestate');
  47. $this->addRowAction('edit');
  48. $this->addRowAction('view');
  49. $this->addRowAction('details');
  50. $this->list_no_link = true;
  51. $this->fields_list = array(
  52. 'reference' => array(
  53. 'title' => $this->l('Reference'),
  54. 'havingFilter' => true
  55. ),
  56. 'supplier' => array(
  57. 'title' => $this->l('Supplier'),
  58. 'filter_key' => 's!name'
  59. ),
  60. 'warehouse' => array(
  61. 'title' => $this->l('Warehouse'),
  62. 'filter_key' => 'w!name'
  63. ),
  64. 'state' => array(
  65. 'title' => $this->l('Status'),
  66. 'filter_key' => 'stl!name',
  67. 'color' => 'color',
  68. ),
  69. 'date_add' => array(
  70. 'title' => $this->l('Creation'),
  71. 'align' => 'left',
  72. 'type' => 'date',
  73. 'havingFilter' => true,
  74. 'filter_key' => 'a!date_add'
  75. ),
  76. 'date_upd' => array(
  77. 'title' => $this->l('Last modification'),
  78. 'align' => 'left',
  79. 'type' => 'date',
  80. 'havingFilter' => true,
  81. 'filter_key' => 'a!date_upd'
  82. ),
  83. 'date_delivery_expected' => array(
  84. 'title' => $this->l('Delivery (expected)'),
  85. 'align' => 'left',
  86. 'type' => 'date',
  87. 'havingFilter' => true,
  88. 'filter_key' => 'a!date_delivery_expected'
  89. ),
  90. 'id_export' => array(
  91. 'title' => $this->l('Export'),
  92. 'callback' => 'printExportIcons',
  93. 'orderby' => false,
  94. 'search' => false
  95. ),
  96. );
  97. // gets the list of warehouses available
  98. $this->warehouses = Warehouse::getWarehouses(true);
  99. // gets the final list of warehouses
  100. array_unshift($this->warehouses, array('id_warehouse' => -1, 'name' => $this->l('All Warehouses')));
  101. parent::__construct();
  102. }
  103. /**
  104. * AdminController::init() override
  105. * @see AdminController::init()
  106. */
  107. public function init()
  108. {
  109. if (Tools::isSubmit('submitFilterorders'))
  110. $this->list_id = 'orders';
  111. elseif (Tools::isSubmit('submitFiltertemplates'))
  112. $this->list_id = 'templates';
  113. parent::init();
  114. if (Tools::isSubmit('addsupply_order') ||
  115. Tools::isSubmit('submitAddsupply_order') ||
  116. (Tools::isSubmit('updatesupply_order') && Tools::isSubmit('id_supply_order')))
  117. {
  118. // override table, lang, className and identifier for the current controller
  119. $this->table = 'supply_order';
  120. $this->className = 'SupplyOrder';
  121. $this->identifier = 'id_supply_order';
  122. $this->lang = false;
  123. $this->action = 'new';
  124. $this->display = 'add';
  125. if (Tools::isSubmit('updatesupply_order'))
  126. if ($this->tabAccess['edit'] === '1')
  127. $this->display = 'edit';
  128. else
  129. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  130. }
  131. if (Tools::isSubmit('update_receipt') && Tools::isSubmit('id_supply_order'))
  132. {
  133. // change the display type in order to add specific actions to
  134. $this->display = 'update_receipt';
  135. // display correct toolBar
  136. $this->initToolbar();
  137. }
  138. }
  139. public function initPageHeaderToolbar()
  140. {
  141. if ($this->display == 'details')
  142. $this->page_header_toolbar_btn['back'] = array(
  143. 'href' => Context::getContext()->link->getAdminLink('AdminSupplyOrders'),
  144. 'desc' => $this->l('Back to list', null, null, false),
  145. 'icon' => 'process-icon-back'
  146. );
  147. elseif (empty($this->display))
  148. {
  149. $this->page_header_toolbar_btn['new_supply_order'] = array(
  150. 'href' => self::$currentIndex.'&addsupply_order&token='.$this->token,
  151. 'desc' => $this->l('Add new supply order', null, null, false),
  152. 'icon' => 'process-icon-new'
  153. );
  154. $this->page_header_toolbar_btn['new_supply_order_template'] = array(
  155. 'href' => self::$currentIndex.'&addsupply_order&mod=template&token='.$this->token,
  156. 'desc' => $this->l('Add new supply order template', null, null, false),
  157. 'icon' => 'process-icon-new'
  158. );
  159. }
  160. parent::initPageHeaderToolbar();
  161. }
  162. /**
  163. * AdminController::renderForm() override
  164. * @see AdminController::renderForm()
  165. */
  166. public function renderForm()
  167. {
  168. if (Tools::isSubmit('addsupply_order') ||
  169. Tools::isSubmit('updatesupply_order') ||
  170. Tools::isSubmit('submitAddsupply_order') ||
  171. Tools::isSubmit('submitUpdatesupply_order'))
  172. {
  173. if (Tools::isSubmit('addsupply_order') || Tools::isSubmit('submitAddsupply_order'))
  174. $this->toolbar_title = $this->l('Stock: Create a new supply order');
  175. $update = false;
  176. if (Tools::isSubmit('updatesupply_order') || Tools::isSubmit('submitUpdatesupply_order'))
  177. {
  178. $this->toolbar_title = $this->l('Stock: Manage supply orders');
  179. $update = true;
  180. }
  181. if (Tools::isSubmit('mod') && Tools::getValue('mod') === 'template' || $this->object->is_template)
  182. $this->toolbar_title .= ' ('.$this->l('template').')';
  183. $this->addJqueryUI('ui.datepicker');
  184. //get warehouses list
  185. $warehouses = Warehouse::getWarehouses(true);
  186. // displays warning if there are no warehouses
  187. if (!$warehouses)
  188. $this->displayWarning($this->l('You must have at least one warehouse. See Stock/Warehouses'));
  189. //get currencies list
  190. $currencies = Currency::getCurrencies(false, true, true);
  191. //get suppliers list
  192. $suppliers = array_unique(Supplier::getSuppliers(), SORT_REGULAR);
  193. //get languages list
  194. $languages = Language::getLanguages(true);
  195. $this->fields_form = array(
  196. 'legend' => array(
  197. 'title' => $this->l('Order information'),
  198. 'icon' => 'icon-pencil'
  199. ),
  200. 'input' => array(
  201. array(
  202. 'type' => 'text',
  203. 'label' => $this->l('Reference'),
  204. 'name' => 'reference',
  205. 'required' => true,
  206. 'hint' => $this->l('The reference number for your order.'),
  207. ),
  208. array(
  209. 'type' => 'select',
  210. 'label' => $this->l('Supplier'),
  211. 'name' => 'id_supplier',
  212. 'required' => true,
  213. 'options' => array(
  214. 'query' => $suppliers,
  215. 'id' => 'id_supplier',
  216. 'name' => 'name'
  217. ),
  218. 'hint' => array(
  219. $this->l('Select the supplier you\'ll be purchasing from.'),
  220. $this->l('Warning: All products already added to the order will be removed.')
  221. )
  222. ),
  223. array(
  224. 'type' => 'select',
  225. 'label' => $this->l('Warehouse'),
  226. 'name' => 'id_warehouse',
  227. 'required' => true,
  228. 'options' => array(
  229. 'query' => $warehouses,
  230. 'id' => 'id_warehouse',
  231. 'name' => 'name'
  232. ),
  233. 'hint' => $this->l('Which warehouse will the order be sent to?'),
  234. ),
  235. array(
  236. 'type' => 'select',
  237. 'label' => $this->l('Currency'),
  238. 'name' => 'id_currency',
  239. 'required' => true,
  240. 'options' => array(
  241. 'query' => $currencies,
  242. 'id' => 'id_currency',
  243. 'name' => 'name'
  244. ),
  245. 'hint' => array(
  246. $this->l('The currency of the order.'),
  247. $this->l('Warning: All products already added to the order will be removed.')
  248. )
  249. ),
  250. array(
  251. 'type' => 'select',
  252. 'label' => $this->l('Order Language'),
  253. 'name' => 'id_lang',
  254. 'required' => true,
  255. 'options' => array(
  256. 'query' => $languages,
  257. 'id' => 'id_lang',
  258. 'name' => 'name'
  259. ),
  260. 'hint' => $this->l('The language of the order.')
  261. ),
  262. array(
  263. 'type' => 'text',
  264. 'label' => $this->l('Global discount percentage'),
  265. 'name' => 'discount_rate',
  266. 'required' => false,
  267. 'hint' => $this->l('This is the global discount percentage for the order.'),
  268. ),
  269. array(
  270. 'type' => 'text',
  271. 'label' => $this->l('Automatically load products'),
  272. 'name' => 'load_products',
  273. 'required' => false,
  274. 'hint' => array(
  275. $this->l('This will reset the order.'),
  276. $this->l('If a value specified, each of your current product (from the selected supplier and warehouse) with a quantity lower than or equal to this value will be loaded. This means that PrestaShop will pre-fill this order with the products that are low on quantity.'),
  277. ),
  278. ),
  279. ),
  280. 'submit' => (!$update ? array('title' => $this->l('Save order')) : array()),
  281. 'buttons' => (!$update ?
  282. array(
  283. 'save-and-stay' => array(
  284. 'title' => $this->l('Save order and stay'),
  285. 'name' => 'submitAddsupply_orderAndStay',
  286. 'type' => 'submit',
  287. 'class' => 'btn btn-default pull-right',
  288. 'icon' => 'process-icon-save'
  289. )
  290. ) : array())
  291. );
  292. if (Tools::isSubmit('mod') && Tools::getValue('mod') === 'template' || $this->object->is_template)
  293. {
  294. $this->fields_form['input'][] = array(
  295. 'type' => 'hidden',
  296. 'name' => 'is_template'
  297. );
  298. $this->fields_form['input'][] = array(
  299. 'type' => 'hidden',
  300. 'name' => 'date_delivery_expected',
  301. );
  302. }
  303. else
  304. {
  305. $this->fields_form['input'][] = array(
  306. 'type' => 'date',
  307. 'label' => $this->l('Expected delivery date'),
  308. 'name' => 'date_delivery_expected',
  309. 'required' => true,
  310. 'desc' => $this->l('The expected delivery date for this order is...'),
  311. );
  312. }
  313. //specific discount display
  314. if (isset($this->object->discount_rate))
  315. $this->object->discount_rate = Tools::ps_round($this->object->discount_rate, 4);
  316. //specific date display
  317. if (isset($this->object->date_delivery_expected))
  318. {
  319. $date = explode(' ', $this->object->date_delivery_expected);
  320. if ($date)
  321. $this->object->date_delivery_expected = $date[0];
  322. }
  323. $this->displayInformation(
  324. $this->l('If you wish to order products, they have to be available for the specified supplier/warehouse.')
  325. .' '.
  326. $this->l('See Catalog/Products/[Your Product]/Suppliers & Warehouses.')
  327. .'<br />'.
  328. $this->l('Changing the currency or the supplier will reset the order.')
  329. .'<br />'
  330. .'<br />'.
  331. $this->l('Please note that you can only order from one supplier at a time.')
  332. );
  333. return parent::renderForm();
  334. }
  335. }
  336. /**
  337. * AdminController::getList() override
  338. * @see AdminController::getList()
  339. */
  340. public function getList($id_lang, $order_by = null, $order_way = null,
  341. $start = 0, $limit = null, $id_lang_shop = false)
  342. {
  343. if (Tools::isSubmit('csv_orders') || Tools::isSubmit('csv_orders_details') || Tools::isSubmit('csv_order_details'))
  344. $limit = false;
  345. // defines button specific for non-template supply orders
  346. if (!$this->is_template_list && $this->display != 'details')
  347. {
  348. // adds export csv buttons
  349. $this->toolbar_btn['export-csv-orders'] = array(
  350. 'short' => 'Export Orders',
  351. 'href' => $this->context->link->getAdminLink('AdminSupplyOrders').'&csv_orders&id_warehouse='.$this->getCurrentWarehouse(),
  352. 'desc' => $this->l('Export Orders (CSV)'),
  353. 'class' => 'process-icon-export'
  354. );
  355. $this->toolbar_btn['export-csv-details'] = array(
  356. 'short' => 'Export Orders Details',
  357. 'href' => $this->context->link->getAdminLink('AdminSupplyOrders').'&csv_orders_details&id_warehouse='.$this->getCurrentWarehouse(),
  358. 'desc' => $this->l('Export Orders Details (CSV)'),
  359. 'class' => 'process-icon-export'
  360. );
  361. unset($this->toolbar_btn['new']);
  362. if ($this->tabAccess['add'] === '1')
  363. {
  364. $this->toolbar_btn['new'] = array(
  365. 'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token,
  366. 'desc' => $this->l('Add New')
  367. );
  368. }
  369. }
  370. parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
  371. // adds colors depending on the receipt state
  372. if ($order_by == 'quantity_expected')
  373. {
  374. $nb_items = count($this->_list);
  375. for ($i = 0; $i < $nb_items; ++$i)
  376. {
  377. $item = &$this->_list[$i];
  378. if ($item['quantity_received'] == $item['quantity_expected'])
  379. $item['color'] = '#00bb35';
  380. elseif ($item['quantity_received'] > $item['quantity_expected'])
  381. $item['color'] = '#fb0008';
  382. }
  383. }
  384. // actions filters on supply orders list
  385. if ($this->table == 'supply_order')
  386. {
  387. $nb_items = count($this->_list);
  388. for ($i = 0; $i < $nb_items; $i++)
  389. {
  390. // if the current state doesn't allow order edit, skip the edit action
  391. if ($this->_list[$i]['editable'] == 0)
  392. $this->addRowActionSkipList('edit', $this->_list[$i]['id_supply_order']);
  393. if ($this->_list[$i]['enclosed'] == 1 && $this->_list[$i]['receipt_state'] == 0)
  394. $this->addRowActionSkipList('changestate', $this->_list[$i]['id_supply_order']);
  395. if (1 != $this->_list[$i]['pending_receipt'])
  396. $this->addRowActionSkipList('updatereceipt', $this->_list[$i]['id_supply_order']);
  397. }
  398. }
  399. }
  400. /**
  401. * AdminController::renderList() override
  402. * @see AdminController::renderList()
  403. */
  404. public function renderList()
  405. {
  406. $this->displayInformation($this->l('This interface allows you to manage supply orders.').'<br />');
  407. $this->displayInformation($this->l('You can create pre-filled order templates, from which you can build actual orders much quicker.').'<br />');
  408. if (count($this->warehouses) <= 1)
  409. $this->displayWarning($this->l('You must choose at least one warehouse before creating supply orders. For more information, see Stock/Warehouses.'));
  410. // assigns warehouses
  411. $this->tpl_list_vars['warehouses'] = $this->warehouses;
  412. $this->tpl_list_vars['current_warehouse'] = $this->getCurrentWarehouse();
  413. $this->tpl_list_vars['filter_status'] = $this->getFilterStatus();
  414. // overrides query
  415. $this->_select = '
  416. s.name AS supplier,
  417. w.name AS warehouse,
  418. stl.name AS state,
  419. st.delivery_note,
  420. st.editable,
  421. st.enclosed,
  422. st.receipt_state,
  423. st.pending_receipt,
  424. st.color AS color,
  425. a.id_supply_order as id_export';
  426. $this->_join = '
  427. LEFT JOIN `'._DB_PREFIX_.'supply_order_state_lang` stl ON
  428. (
  429. a.id_supply_order_state = stl.id_supply_order_state
  430. AND stl.id_lang = '.(int)$this->context->language->id.'
  431. )
  432. LEFT JOIN `'._DB_PREFIX_.'supply_order_state` st ON a.id_supply_order_state = st.id_supply_order_state
  433. LEFT JOIN `'._DB_PREFIX_.'supplier` s ON a.id_supplier = s.id_supplier
  434. LEFT JOIN `'._DB_PREFIX_.'warehouse` w ON (w.id_warehouse = a.id_warehouse)';
  435. $this->_where = ' AND a.is_template = 0';
  436. if ($this->getCurrentWarehouse() != -1)
  437. {
  438. $this->_where .= ' AND a.id_warehouse = '.$this->getCurrentWarehouse();
  439. self::$currentIndex .= '&id_warehouse='.(int)$this->getCurrentWarehouse();
  440. }
  441. if ($this->getFilterStatus() != 0)
  442. {
  443. $this->_where .= ' AND st.enclosed != 1';
  444. self::$currentIndex .= '&filter_status=on';
  445. }
  446. $this->list_id = 'orders';
  447. $this->_filterHaving = null;
  448. if (Tools::isSubmit('submitFilter'.$this->list_id)
  449. || $this->context->cookie->{'submitFilter'.$this->list_id} !== false
  450. || Tools::getValue($this->list_id.'Orderby')
  451. || Tools::getValue($this->list_id.'Orderway'))
  452. {
  453. $this->filter = true;
  454. parent::processFilter();
  455. }
  456. $first_list = parent::renderList();
  457. if (Tools::isSubmit('csv_orders') || Tools::isSubmit('csv_orders_details') || Tools::isSubmit('csv_order_details'))
  458. {
  459. if (count($this->_list) > 0)
  460. {
  461. $this->renderCSV();
  462. die;
  463. }
  464. else
  465. $this->displayWarning($this->l('There is nothing to export as a CSV file.'));
  466. }
  467. // second list : templates
  468. $second_list = null;
  469. $this->is_template_list = true;
  470. unset($this->tpl_list_vars['warehouses']);
  471. unset($this->tpl_list_vars['current_warehouse']);
  472. unset($this->tpl_list_vars['filter_status']);
  473. // unsets actions
  474. $this->actions = array();
  475. unset($this->toolbar_btn['export-csv-orders']);
  476. unset($this->toolbar_btn['export-csv-details']);
  477. // adds actions
  478. $this->addRowAction('view');
  479. $this->addRowAction('edit');
  480. $this->addRowAction('createsupplyorder');
  481. $this->addRowAction('delete');
  482. // unsets some fields
  483. unset($this->fields_list['state'],
  484. $this->fields_list['date_upd'],
  485. $this->fields_list['id_pdf'],
  486. $this->fields_list['date_delivery_expected'],
  487. $this->fields_list['id_export']);
  488. // $this->fields_list['date_add']['align'] = 'left';
  489. // adds filter, to gets only templates
  490. unset($this->_where);
  491. $this->_where = ' AND a.is_template = 1';
  492. if ($this->getCurrentWarehouse() != -1)
  493. $this->_where .= ' AND a.id_warehouse = '.$this->getCurrentWarehouse();
  494. // re-defines toolbar & buttons
  495. $this->toolbar_title = $this->l('Stock: Supply order templates');
  496. $this->initToolbar();
  497. unset($this->toolbar_btn['new']);
  498. $this->toolbar_btn['new'] = array(
  499. 'href' => self::$currentIndex.'&add'.$this->table.'&mod=template&token='.$this->token,
  500. 'desc' => $this->l('Add new template'),
  501. 'imgclass' => 'new_1',
  502. 'class' => 'process-icon-new'
  503. );
  504. $this->list_id = 'templates';
  505. $this->_filterHaving = null;
  506. if (Tools::isSubmit('submitFilter'.$this->list_id)
  507. || $this->context->cookie->{'submitFilter'.$this->list_id} !== false
  508. || Tools::getValue($this->list_id.'Orderby')
  509. || Tools::getValue($this->list_id.'Orderway'))
  510. {
  511. $this->filter = true;
  512. parent::processFilter();
  513. }
  514. // inits list
  515. $second_list = parent::renderList();
  516. return $first_list.$second_list;
  517. }
  518. /**
  519. * Init the content of change state action
  520. */
  521. public function initChangeStateContent()
  522. {
  523. $id_supply_order = (int)Tools::getValue('id_supply_order', 0);
  524. if ($id_supply_order <= 0)
  525. {
  526. $this->errors[] = Tools::displayError('The specified supply order is not valid');
  527. return parent::initContent();
  528. }
  529. $supply_order = new SupplyOrder($id_supply_order);
  530. $supply_order_state = new SupplyOrderState($supply_order->id_supply_order_state);
  531. if (!Validate::isLoadedObject($supply_order) || !Validate::isLoadedObject($supply_order_state))
  532. {
  533. $this->errors[] = Tools::displayError('The specified supply order is not valid');
  534. return parent::initContent();
  535. }
  536. // change the display type in order to add specific actions to
  537. $this->display = 'update_order_state';
  538. // overrides parent::initContent();
  539. $this->initToolbar();
  540. $this->initPageHeaderToolbar();
  541. // given the current state, loads available states
  542. $states = SupplyOrderState::getSupplyOrderStates($supply_order->id_supply_order_state);
  543. // gets the state that are not allowed
  544. $allowed_states = array();
  545. foreach ($states as &$state)
  546. {
  547. $allowed_states[] = $state['id_supply_order_state'];
  548. $state['allowed'] = 1;
  549. }
  550. $not_allowed_states = SupplyOrderState::getStates($allowed_states);
  551. // generates the final list of states
  552. $index = count($allowed_states);
  553. foreach ($not_allowed_states as &$not_allowed_state)
  554. {
  555. $not_allowed_state['allowed'] = 0;
  556. $states[$index] = $not_allowed_state;
  557. ++$index;
  558. }
  559. // loads languages
  560. $this->getlanguages();
  561. // defines the fields of the form to display
  562. $this->fields_form[0]['form'] = array(
  563. 'legend' => array(
  564. 'title' => $this->l('Supply order status'),
  565. 'icon' => 'icon-pencil'
  566. ),
  567. 'input' => array(),
  568. 'submit' => array(
  569. 'title' => $this->l('Save')
  570. )
  571. );
  572. $this->displayInformation($this->l('Be careful when changing status. Some of those changes cannot be canceled. '));
  573. // sets up the helper
  574. $helper = new HelperForm();
  575. $helper->submit_action = 'submitChangestate';
  576. $helper->currentIndex = self::$currentIndex;
  577. $helper->toolbar_btn = $this->toolbar_btn;
  578. $helper->toolbar_scroll = false;
  579. $helper->token = $this->token;
  580. $helper->id = null; // no display standard hidden field in the form
  581. $helper->languages = $this->_languages;
  582. $helper->default_form_language = $this->default_form_language;
  583. $helper->allow_employee_form_lang = $this->allow_employee_form_lang;
  584. $helper->title = sprintf($this->l('Stock: Change supply order status #%s'), $supply_order->reference);
  585. $helper->show_cancel_button = true;
  586. $helper->override_folder = 'supply_orders_change_state/';
  587. // assigns our content
  588. $helper->tpl_vars['show_change_state_form'] = true;
  589. $helper->tpl_vars['supply_order_state'] = $supply_order_state;
  590. $helper->tpl_vars['supply_order'] = $supply_order;
  591. $helper->tpl_vars['supply_order_states'] = $states;
  592. // generates the form to display
  593. $content = $helper->generateForm($this->fields_form);
  594. $this->context->smarty->assign(array(
  595. 'content' => $content,
  596. 'url_post' => self::$currentIndex.'&token='.$this->token,
  597. 'show_page_header_toolbar' => $this->show_page_header_toolbar,
  598. 'page_header_toolbar_title' => $this->page_header_toolbar_title,
  599. 'page_header_toolbar_btn' => $this->page_header_toolbar_btn
  600. ));
  601. }
  602. /**
  603. * Init the content of change state action
  604. */
  605. public function initUpdateSupplyOrderContent()
  606. {
  607. $this->addJqueryPlugin('autocomplete');
  608. // load supply order
  609. $id_supply_order = (int)Tools::getValue('id_supply_order', null);
  610. if ($id_supply_order != null)
  611. {
  612. $supply_order = new SupplyOrder($id_supply_order);
  613. $currency = new Currency($supply_order->id_currency);
  614. if (Validate::isLoadedObject($supply_order))
  615. {
  616. // load products of this order
  617. $products = $supply_order->getEntries();
  618. $product_ids = array();
  619. if (isset($this->order_products_errors) && is_array($this->order_products_errors))
  620. {
  621. //for each product in error array, check if it is in products array, and remove it to conserve last user values
  622. foreach ($this->order_products_errors as $pe)
  623. foreach ($products as $index_p => $p)
  624. if (($p['id_product'] == $pe['id_product']) && ($p['id_product_attribute'] == $pe['id_product_attribute']))
  625. unset($products[$index_p]);
  626. // then merge arrays
  627. $products = array_merge($this->order_products_errors, $products);
  628. }
  629. foreach ($products as &$item)
  630. {
  631. // calculate md5 checksum on each product for use in tpl
  632. $item['checksum'] = md5(_COOKIE_KEY_.$item['id_product'].'_'.$item['id_product_attribute']);
  633. $item['unit_price_te'] = Tools::ps_round($item['unit_price_te'], 2);
  634. // add id to ids list
  635. $product_ids[] = $item['id_product'].'_'.$item['id_product_attribute'];
  636. }
  637. $this->tpl_form_vars['products_list'] = $products;
  638. $this->tpl_form_vars['product_ids'] = implode($product_ids, '|');
  639. $this->tpl_form_vars['product_ids_to_delete'] = '';
  640. $this->tpl_form_vars['supplier_id'] = $supply_order->id_supplier;
  641. $this->tpl_form_vars['currency'] = $currency;
  642. }
  643. }
  644. $this->tpl_form_vars['content'] = $this->content;
  645. $this->tpl_form_vars['token'] = $this->token;
  646. $this->tpl_form_vars['show_product_management_form'] = true;
  647. // call parent initcontent to render standard form content
  648. parent::initContent();
  649. }
  650. /**
  651. * Inits the content of 'update_receipt' action
  652. * Called in initContent()
  653. * @see AdminSuppliersOrders::initContent()
  654. */
  655. public function initUpdateReceiptContent()
  656. {
  657. $id_supply_order = (int)Tools::getValue('id_supply_order', null);
  658. // if there is no order to fetch
  659. if (null == $id_supply_order)
  660. return parent::initContent();
  661. $supply_order = new SupplyOrder($id_supply_order);
  662. // if it's not a valid order
  663. if (!Validate::isLoadedObject($supply_order))
  664. return parent::initContent();
  665. $this->initPageHeaderToolbar();
  666. // re-defines fields_list
  667. $this->fields_list = array(
  668. 'supplier_reference' => array(
  669. 'title' => $this->l('Supplier reference'),
  670. 'orderby' => false,
  671. 'filter' => false,
  672. 'search' => false,
  673. ),
  674. 'reference' => array(
  675. 'title' => $this->l('Reference'),
  676. 'orderby' => false,
  677. 'filter' => false,
  678. 'search' => false,
  679. ),
  680. 'ean13' => array(
  681. 'title' => $this->l('EAN-13 or JAN barcode'),
  682. 'orderby' => false,
  683. 'filter' => false,
  684. 'search' => false,
  685. ),
  686. 'upc' => array(
  687. 'title' => $this->l('UPC barcode'),
  688. 'orderby' => false,
  689. 'filter' => false,
  690. 'search' => false,
  691. ),
  692. 'name' => array(
  693. 'title' => $this->l('Name'),
  694. 'orderby' => false,
  695. 'filter' => false,
  696. 'search' => false,
  697. ),
  698. 'quantity_received_today' => array(
  699. 'title' => $this->l('Quantity received today?'),
  700. 'type' => 'editable',
  701. 'orderby' => false,
  702. 'filter' => false,
  703. 'search' => false,
  704. 'hint' => $this->l('The quantity of supplies that you received today.'),
  705. ),
  706. 'quantity_received' => array(
  707. 'title' => $this->l('Quantity received'),
  708. 'orderby' => false,
  709. 'filter' => false,
  710. 'search' => false,
  711. 'badge_danger' => true,
  712. 'badge_success' => true,
  713. 'hint' => $this->l('The quantity of supplies that you received so far (today and the days before, if it applies).'),
  714. ),
  715. 'quantity_expected' => array(
  716. 'title' => $this->l('Quantity expected'),
  717. 'orderby' => false,
  718. 'filter' => false,
  719. 'search' => false,
  720. ),
  721. 'quantity_left' => array(
  722. 'title' => $this->l('Quantity left'),
  723. 'orderby' => false,
  724. 'filter' => false,
  725. 'search' => false,
  726. 'hint' => $this->l('The quantity of supplies left to receive for this order.'),
  727. )
  728. );
  729. // attributes override
  730. unset($this->_select, $this->_join, $this->_where, $this->_orderBy, $this->_orderWay, $this->_group, $this->_filterHaving, $this->_filter);
  731. $this->table = 'supply_order_detail';
  732. $this->identifier = 'id_supply_order_detail';
  733. $this->className = 'SupplyOrderDetail';
  734. $this->list_simple_header = false;
  735. $this->list_no_link = true;
  736. $this->colorOnBackground = true;
  737. $this->row_hover = false;
  738. $this->bulk_actions = array('Update' => array('text' => $this->l('Update selected'), 'confirm' => $this->l('Update selected items?')));
  739. $this->addRowAction('details');
  740. // sets toolbar title with order reference
  741. $this->toolbar_title = sprintf($this->l('Receipt of products for supply order #%s'), $supply_order->reference);
  742. $this->lang = false;
  743. $lang_id = (int)$this->context->language->id; //employee lang
  744. // gets values corresponding to fields_list
  745. $this->_select = '
  746. a.id_supply_order_detail as id,
  747. a.quantity_received as quantity_received,
  748. a.quantity_expected as quantity_expected,
  749. IF (a.quantity_expected < a.quantity_received, 0, a.quantity_expected - a.quantity_received) as quantity_left,
  750. IF (a.quantity_expected < a.quantity_received, 0, a.quantity_expected - a.quantity_received) as quantity_received_today,
  751. IF (a.quantity_expected = a.quantity_received, 1, 0) badge_success,
  752. IF (a.quantity_expected > a.quantity_received, 1, 0) badge_danger';
  753. $this->_where = 'AND a.`id_supply_order` = '.(int)$id_supply_order;
  754. $this->_group = 'GROUP BY a.id_supply_order_detail';
  755. // gets the list ordered by price desc, without limit
  756. $this->getList($lang_id, 'quantity_expected', 'DESC', 0, Tools::getValue('supply_order_pagination'), false);
  757. // defines action for POST
  758. $action = '&id_supply_order='.$id_supply_order.'&update_receipt=1';
  759. // unsets some buttons
  760. unset($this->toolbar_btn['export-csv-orders']);
  761. unset($this->toolbar_btn['export-csv-details']);
  762. unset($this->toolbar_btn['new']);
  763. $this->toolbar_btn['back'] = array(
  764. 'desc' => $this->l('Back'),
  765. 'href' => $this->context->link->getAdminLink('AdminSupplyOrders')
  766. );
  767. // renders list
  768. $helper = new HelperList();
  769. $this->setHelperDisplay($helper);
  770. $helper->actions = array('details');
  771. $helper->force_show_bulk_actions = true;
  772. $helper->override_folder = 'supply_orders_receipt_history/';
  773. $helper->toolbar_btn = $this->toolbar_btn;
  774. $helper->list_id = 'supply_order_detail';
  775. $helper->ajax_params = array(
  776. 'display_product_history' => 1,
  777. );
  778. $helper->currentIndex = self::$currentIndex.$action;
  779. // display these global order informations
  780. $this->displayInformation($this->l('This interface allows you to update the quantities of this ongoing order.').'<br />');
  781. $this->displayInformation($this->l('Be careful! Once you update, you cannot go back unless you add new negative stock movements.').'<br />');
  782. $this->displayInformation($this->l('A green line means that you\'ve received exactly the quantity you expected. A red line means that you\'ve received more than expected.').'<br />');
  783. // generates content
  784. $content = $helper->generateList($this->_list, $this->fields_list);
  785. // assigns var
  786. $this->context->smarty->assign(array(
  787. 'content' => $content,
  788. 'show_page_header_toolbar' => $this->show_page_header_toolbar,
  789. 'page_header_toolbar_title' => $this->page_header_toolbar_title,
  790. 'page_header_toolbar_btn' => $this->page_header_toolbar_btn
  791. ));
  792. }
  793. /**
  794. * AdminController::initContent() override
  795. * @see AdminController::initContent()
  796. */
  797. public function initContent()
  798. {
  799. if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
  800. {
  801. $this->warnings[md5('PS_ADVANCED_STOCK_MANAGEMENT')] =
  802. $this->l('You need to activate the Advanced Stock Management feature prior to using this feature.');
  803. return false;
  804. }
  805. // Manage the add stock form
  806. if (Tools::isSubmit('changestate'))
  807. $this->initChangeStateContent();
  808. elseif (Tools::isSubmit('update_receipt') && Tools::isSubmit('id_supply_order') && !Tools::isSubmit('detailssupply_order_detail'))
  809. $this->initUpdateReceiptContent();
  810. elseif (Tools::isSubmit('viewsupply_order') && Tools::isSubmit('id_supply_order'))
  811. {
  812. $this->action = 'view';
  813. $this->display = 'view';
  814. parent::initContent();
  815. }
  816. elseif (Tools::isSubmit('updatesupply_order'))
  817. $this->initUpdateSupplyOrderContent();
  818. else
  819. {
  820. if (Tools::isSubmit('detailssupply_order_detail'))
  821. {
  822. $this->action = 'details';
  823. $this->display = 'details';
  824. }
  825. parent::initContent();
  826. }
  827. }
  828. /**
  829. * Ths method manage associated products to the order when updating it
  830. */
  831. public function manageOrderProducts()
  832. {
  833. // load supply order
  834. $id_supply_order = (int)Tools::getValue('id_supply_order', null);
  835. $products_already_in_order = array();
  836. if ($id_supply_order != null)
  837. {
  838. $supply_order = new SupplyOrder($id_supply_order);
  839. if (Validate::isLoadedObject($supply_order))
  840. {
  841. // tests if the supplier or currency have changed in the supply order
  842. $new_supplier_id = (int)Tools::getValue('id_supplier');
  843. $new_currency_id = (int)Tools::getValue('id_currency');
  844. if (($new_supplier_id != $supply_order->id_supplier) ||
  845. ($new_currency_id != $supply_order->id_currency))
  846. {
  847. // resets all products in this order
  848. $supply_order->resetProducts();
  849. }
  850. else
  851. {
  852. $products_already_in_order = $supply_order->getEntries();
  853. $currency = new Currency($supply_order->id_ref_currency);
  854. // gets all product ids to manage
  855. $product_ids_str = Tools::getValue('product_ids', null);
  856. $product_ids = explode('|', $product_ids_str);
  857. $product_ids_to_delete_str = Tools::getValue('product_ids_to_delete', null);
  858. $product_ids_to_delete = array_unique(explode('|', $product_ids_to_delete_str));
  859. //delete products that are not managed anymore
  860. foreach ($products_already_in_order as $paio)
  861. {
  862. $product_ok = false;
  863. foreach ($product_ids_to_delete as $id)
  864. {
  865. $id_check = $paio['id_product'].'_'.$paio['id_product_attribute'];
  866. if ($id_check == $id)
  867. $product_ok = true;
  868. }
  869. if ($product_ok === true)
  870. {
  871. $entry = new SupplyOrderDetail($paio['id_supply_order_detail']);
  872. $entry->delete();
  873. }
  874. }
  875. // manage each product
  876. foreach ($product_ids as $id)
  877. {
  878. $errors = array();
  879. // check if a checksum is available for this product and test it
  880. $check = Tools::getValue('input_check_'.$id, '');
  881. $check_valid = md5(_COOKIE_KEY_.$id);
  882. if ($check_valid != $check)
  883. continue;
  884. $pos = strpos($id, '_');
  885. if ($pos === false)
  886. continue;
  887. // Load / Create supply order detail
  888. $entry = new SupplyOrderDetail();
  889. $id_supply_order_detail = (int)Tools::getValue('input_id_'.$id, 0);
  890. if ($id_supply_order_detail > 0)
  891. {
  892. $existing_entry = new SupplyOrderDetail($id_supply_order_detail);
  893. if (Validate::isLoadedObject($supply_order))
  894. $entry = &$existing_entry;
  895. }
  896. // get product informations
  897. $entry->id_product = substr($id, 0, $pos);
  898. $entry->id_product_attribute = substr($id, $pos + 1);
  899. $entry->unit_price_te = (float)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_unit_price_te_'.$id, 0));
  900. $entry->quantity_expected = (int)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_quantity_expected_'.$id, 0));
  901. $entry->discount_rate = (float)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_discount_rate_'.$id, 0));
  902. $entry->tax_rate = (float)str_replace(array(' ', ','), array('', '.'), Tools::getValue('input_tax_rate_'.$id, 0));
  903. $entry->reference = Tools::getValue('input_reference_'.$id, '');
  904. $entry->supplier_reference = Tools::getValue('input_supplier_reference_'.$id, '');
  905. $entry->ean13 = Tools::getValue('input_ean13_'.$id, '');
  906. $entry->upc = Tools::getValue('input_upc_'.$id, '');
  907. //get the product name in the order language
  908. $entry->name = Product::getProductName($entry->id_product, $entry->id_product_attribute, $supply_order->id_lang);
  909. if (empty($entry->name))
  910. $entry->name = '';
  911. if ($entry->supplier_reference == null)
  912. $entry->supplier_reference = '';
  913. $entry->exchange_rate = $currency->conversion_rate;
  914. $entry->id_currency = $currency->id;
  915. $entry->id_supply_order = $supply_order->id;
  916. $errors = $entry->validateController();
  917. //get the product name displayed in the backoffice according to the employee language
  918. $entry->name_displayed = Tools::getValue('input_name_displayed_'.$id, '');
  919. // if there is a problem, handle error for the current product
  920. if (count($errors) > 0)
  921. {
  922. // add the product to error array => display again product line
  923. $this->order_products_errors[] = array(
  924. 'id_product' => $entry->id_product,
  925. 'id_product_attribute' => $entry->id_product_attribute,
  926. 'unit_price_te' => $entry->unit_price_te,
  927. 'quantity_expected' => $entry->quantity_expected,
  928. 'discount_rate' => $entry->discount_rate,
  929. 'tax_rate' => $entry->tax_rate,
  930. 'name' => $entry->name,
  931. 'name_displayed' => $entry->name_displayed,
  932. 'reference' => $entry->reference,
  933. 'supplier_reference' => $entry->supplier_reference,
  934. 'ean13' => $entry->ean13,
  935. 'upc' => $entry->upc,
  936. );
  937. $error_str = '<ul>';
  938. foreach ($errors as $e)
  939. $error_str .= '<li>'.sprintf($this->l('Field: %s'), $e).'</li>';
  940. $error_str .= '</ul>';
  941. $this->errors[] = sprintf(Tools::displayError('Please verify the product information for "%s":'), $entry->name).' '
  942. .$error_str;
  943. }
  944. else
  945. $entry->save();
  946. }
  947. }
  948. }
  949. }
  950. }
  951. /**
  952. * AdminController::postProcess() override
  953. * @see AdminController::postProcess()
  954. */
  955. public function postProcess()
  956. {
  957. $this->is_editing_order = false;
  958. // Checks access
  959. if (Tools::isSubmit('submitAddsupply_order') && !($this->tabAccess['add'] === '1'))
  960. $this->errors[] = Tools::displayError('You do not have permission to add a supply order.');
  961. if (Tools::isSubmit('submitBulkUpdatesupply_order_detail') && !($this->tabAccess['edit'] === '1'))
  962. $this->errors[] = Tools::displayError('You do not have permission to edit an order.');
  963. // Trick to use both Supply Order as template and actual orders
  964. if (Tools::isSubmit('is_template'))
  965. $_GET['mod'] = 'template';
  966. // checks if supply order reference is unique
  967. if (Tools::isSubmit('reference'))
  968. {
  969. // gets the reference
  970. $ref = pSQL(Tools::getValue('reference'));
  971. if (Tools::getValue('id_supply_order') != 0 && SupplyOrder::getReferenceById((int)Tools::getValue('id_supply_order')) != $ref)
  972. {
  973. if ((int)SupplyOrder::exists($ref) != 0)
  974. $this->errors[] = Tools::displayError('The reference has to be unique.');
  975. }
  976. elseif (Tools::getValue('id_supply_order') == 0 && (int)SupplyOrder::exists($ref) != 0)
  977. $this->errors[] = Tools::displayError('The reference has to be unique.');
  978. }
  979. if ($this->errors)
  980. return;
  981. // Global checks when add / update a supply order
  982. if (Tools::isSubmit('submitAddsupply_order') || Tools::isSubmit('submitAddsupply_orderAndStay'))
  983. {
  984. $this->action = 'save';
  985. $this->is_editing_order = true;
  986. // get supplier ID
  987. $id_supplier = (int)Tools::getValue('id_supplier', 0);
  988. if ($id_supplier <= 0 || !Supplier::supplierExists($id_supplier))
  989. $this->errors[] = Tools::displayError('The selected supplier is not valid.');
  990. // get warehouse id
  991. $id_warehouse = (int)Tools::getValue('id_warehouse', 0);
  992. if ($id_warehouse <= 0 || !Warehouse::exists($id_warehouse))
  993. $this->errors[] = Tools::displayError('The selected warehouse is not valid.');
  994. // get currency id
  995. $id_currency = (int)Tools::getValue('id_currency', 0);
  996. if ($id_currency <= 0 || ( !($result = Currency::getCurrency($id_currency)) || empty($result) ))
  997. $this->errors[] = Tools::displayError('The selected currency is not valid.');
  998. // get delivery date
  999. if (Tools::getValue('mod') != 'template' && strtotime(Tools::getValue('date_delivery_expected')) <= strtotime('-1 day'))
  1000. $this->errors[] = Tools::displayError('The specified date cannot be in the past.');
  1001. // gets threshold
  1002. $quantity_threshold = Tools::getValue('load_products');
  1003. if (is_numeric($quantity_threshold))
  1004. $quantity_threshold = (int)$quantity_threshold;
  1005. else
  1006. $quantity_threshold = null;
  1007. if (!count($this->errors))
  1008. {
  1009. // forces date for templates
  1010. if (Tools::isSubmit('is_template') && !Tools::getValue('date_delivery_expected'))
  1011. $_POST['date_delivery_expected'] = date('Y-m-d h:i:s');
  1012. // specify initial state
  1013. $_POST['id_supply_order_state'] = 1; //defaut creation state
  1014. // specify global reference currency
  1015. $_POST['id_ref_currency'] = Currency::getDefaultCurrency()->id;
  1016. // specify supplier name
  1017. $_POST['supplier_name'] = Supplier::getNameById($id_supplier);
  1018. //specific discount check
  1019. $_POST['discount_rate'] = (float)str_replace(array(' ', ','), array('', '.'), Tools::getValue('discount_rate', 0));
  1020. }
  1021. // manage each associated product
  1022. $this->manageOrderProducts();
  1023. // if the threshold is defined and we are saving the order
  1024. if (Tools::isSubmit('submitAddsupply_order') && Validate::isInt($quantity_threshold))
  1025. $this->loadProducts((int)$quantity_threshold);
  1026. }
  1027. // Manage state change
  1028. if (Tools::isSubmit('submitChangestate')
  1029. && Tools::isSubmit('id_supply_order')
  1030. && Tools::isSubmit('id_supply_order_state'))
  1031. {
  1032. if ($this->tabAccess['edit'] != '1')
  1033. $this->errors[] = Tools::displayError('You do not have permission to change the order status.');
  1034. // get state ID
  1035. $id_state = (int)Tools::getValue('id_supply_order_state', 0);
  1036. if ($id_state <= 0)
  1037. $this->errors[] = Tools::displayError('The selected supply order status is not valid.');
  1038. // get supply order ID
  1039. $id_supply_order = (int)Tools::getValue('id_supply_order', 0);
  1040. if ($id_supply_order <= 0)
  1041. $this->errors[] = Tools::displayError('The supply order ID is not valid.');
  1042. if (!count($this->errors))
  1043. {
  1044. // try to load supply order
  1045. $supply_order = new SupplyOrder($id_supply_order);
  1046. if (Validate::isLoadedObject($supply_order))
  1047. {
  1048. // get valid available possible states for this order
  1049. $states = SupplyOrderState::getSupplyOrderStates($supply_order->id_supply_order_state);
  1050. foreach ($states as $state)
  1051. {
  1052. // if state is valid, change it in the order
  1053. if ($id_state == $state['id_supply_order_state'])
  1054. {
  1055. $new_state = new SupplyOrderState($id_state);
  1056. $old_state = new SupplyOrderState($supply_order->id_supply_order_state);
  1057. // special case of validate state - check if there are products in the order and the required state is not an enclosed state
  1058. if ($supply_order->isEditable() && !$supply_order->hasEntries() && !$new_state->enclosed)
  1059. $this->errors[] = Tools::displayError('It is not possible to change the status of this order because you did not order any products.');
  1060. if (!count($this->errors))
  1061. {
  1062. $supply_order->id_supply_order_state = $state['id_supply_order_state'];
  1063. if ($supply_order->save())
  1064. {
  1065. if ($new_state->pending_receipt)
  1066. {
  1067. $supply_order_details = $supply_order->getEntries();
  1068. foreach ($supply_order_details as $supply_order_detail)
  1069. {
  1070. $is_present = Stock::productIsPresentInStock($supply_order_detail['id_product'], $supply_order_detail['id_product_attribute'], $supply_order->id_warehouse);
  1071. if (!$is_present)
  1072. {
  1073. $stock = new Stock();
  1074. $stock_params = array(
  1075. 'id_product_attribute' => $supply_order_detail['id_product_attribute'],
  1076. 'id_product' => $supply_order_detail['id_product'],
  1077. 'physical_quantity' => 0,
  1078. 'price_te' => $supply_order_detail['price_te'],
  1079. 'usable_quantity' => 0,
  1080. 'id_warehouse' => $supply_order->id_warehouse
  1081. );
  1082. // saves stock in warehouse
  1083. $stock->hydrate($stock_params);
  1084. $stock->add();
  1085. }
  1086. }
  1087. }
  1088. // if pending_receipt,
  1089. // or if the order is being canceled,
  1090. // or if the order is received completely
  1091. // synchronizes StockAvailable
  1092. if (($new_state->pending_receipt && !$new_state->receipt_state) ||
  1093. (($old_state->receipt_state || $old_state->pending_receipt) && $new_state->enclosed && !$new_state->receipt_state) ||
  1094. ($new_state->receipt_state && $new_state->enclosed))
  1095. {
  1096. $supply_order_details = $supply_order->getEntries();
  1097. $products_done = array();
  1098. foreach ($supply_order_details as $supply_order_detail)
  1099. {
  1100. if (!in_array($supply_order_detail['id_product'], $products_done))
  1101. {
  1102. StockAvailable::synchronize($supply_order_detail['id_product']);
  1103. $products_done[] = $supply_order_detail['id_product'];
  1104. }
  1105. }
  1106. }
  1107. $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
  1108. $redirect = self::$currentIndex.'&token='.$token;
  1109. $this->redirect_after = $redirect.'&conf=5';
  1110. }
  1111. }
  1112. }
  1113. }
  1114. }
  1115. else
  1116. $this->errors[] = Tools::displayError('The selected supplier is not valid.');
  1117. }
  1118. }
  1119. // updates receipt
  1120. if (Tools::isSubmit('submitBulkUpdatesupply_order_detail') && Tools::isSubmit('id_supply_order'))
  1121. $this->postProcessUpdateReceipt();
  1122. // use template to create a supply order
  1123. if (Tools::isSubmit('create_supply_order') && Tools::isSubmit('id_supply_order'))
  1124. $this->postProcessCopyFromTemplate();
  1125. if ((!count($this->errors) && $this->is_editing_order) || !$this->is_editing_order)
  1126. parent::postProcess();
  1127. }
  1128. /**
  1129. * Exports CSV
  1130. */
  1131. protected function renderCSV()
  1132. {
  1133. // exports orders
  1134. if (Tools::isSubmit('csv_orders'))
  1135. {
  1136. $ids = array();
  1137. foreach ($this->_list as $entry)
  1138. $ids[] = $entry['id_supply_order'];
  1139. if (count($ids) <= 0)
  1140. return;
  1141. $id_lang = Context::getContext()->language->id;
  1142. $orders = new PrestaShopCollection('SupplyOrder', $id_lang);
  1143. $orders->where('is_template', '=', false);
  1144. $orders->where('id_supply_order', 'in', $ids);
  1145. $id_warehouse = $this->getCurrentWarehouse();
  1146. if ($id_warehouse != -1)
  1147. $orders->where('id_warehouse', '=', $id_warehouse);
  1148. $orders->getAll();
  1149. $csv = new CSV($orders, $this->l('supply_orders'));
  1150. $csv->export();
  1151. }
  1152. // exports details for all orders
  1153. elseif (Tools::isSubmit('csv_orders_details'))
  1154. {
  1155. // header
  1156. header('Content-type: text/csv');
  1157. header('Content-Type: application/force-download; charset=UTF-8');
  1158. header('Cache-Control: no-store, no-cache');
  1159. header('Content-disposition: attachment; filename="'.$this->l('supply_orders_details').'.csv"');
  1160. // echoes details
  1161. $ids = array();
  1162. foreach ($this->_list as $entry)
  1163. $ids[] = $entry['id_supply_order'];
  1164. if (count($ids) <= 0)
  1165. return;
  1166. // for each supply order
  1167. $keys = array('id_product', 'id_product_attribute', 'reference', 'supplier_reference', 'ean13', 'upc', 'name',
  1168. 'unit_price_te', 'quantity_expected', 'quantity_received', 'price_te', 'discount_rate', 'discount_value_te',
  1169. 'price_with_discount_te', 'tax_rate', 'tax_value', 'price_ti', 'tax_value_with_order_discount',
  1170. 'price_with_order_discount_te', 'id_supply_order');
  1171. echo sprintf("%s\n", implode(';', array_map(array('CSVCore', 'wrap'), $keys)));
  1172. // overrides keys (in order to add FORMAT calls)
  1173. $keys = array('sod.id_product', 'sod.id_product_attribute', 'sod.reference', 'sod.supplier_reference', 'sod.ean13',
  1174. 'sod.upc', 'sod.name',
  1175. 'FORMAT(sod.unit_price_te, 2)', 'sod.quantity_expected', 'sod.quantity_received', 'FORMAT(sod.price_te, 2)',
  1176. 'FORMAT(sod.discount_rate, 2)', 'FORMAT(sod.discount_value_te, 2)',
  1177. 'FORMAT(sod.price_with_discount_te, 2)', 'FORMAT(sod.tax_rate, 2)', 'FORMAT(sod.tax_value, 2)',
  1178. 'FORMAT(sod.price_ti, 2)', 'FORMAT(sod.tax_value_with_order_discount, 2)',
  1179. 'FORMAT(sod.price_with_order_discount_te, 2)', 'sod.id_supply_order');
  1180. foreach ($ids as $id)
  1181. {
  1182. $query = new DbQuery();
  1183. $query->select(implode(', ', $keys));
  1184. $query->from('supply_order_detail', 'sod');
  1185. $query->leftJoin('supply_order', 'so', 'so.id_supply_order = sod.id_supply_order');
  1186. $id_warehouse = $this->getCurrentWarehouse();
  1187. if ($id_warehouse != -1)
  1188. $query->where('so.id_warehouse = '.(int)$id_warehouse);
  1189. $query->where('sod.id_supply_order = '.(int)$id);
  1190. $query->orderBy('sod.id_supply_order_detail DESC');
  1191. $resource = Db::getInstance()->query($query);
  1192. // gets details
  1193. while ($row = Db::getInstance()->nextRow($resource))
  1194. echo sprintf("%s\n", implode(';', array_map(array('CSVCore', 'wrap'), $row)));
  1195. }
  1196. }
  1197. // exports details for the given order
  1198. elseif (Tools::isSubmit('csv_order_details') && Tools::getValue('id_supply_order'))
  1199. {
  1200. $supply_order = new SupplyOrder((int)Tools::getValue('id_supply_order'));
  1201. if (Validate::isLoadedObject($supply_order))
  1202. {
  1203. $details = $supply_order->getEntriesCollection();
  1204. $details->getAll();
  1205. $csv = new CSV($details, $this->l('supply_order').'_'.$supply_order->reference.'_details');
  1206. $csv->export();
  1207. }
  1208. }
  1209. }
  1210. /**
  1211. * Helper function for AdminSupplyOrdersController::postProcess()
  1212. *
  1213. * @see AdminSupplyOrdersController::postProcess()
  1214. */
  1215. protected function postProcessUpdateReceipt()
  1216. {
  1217. // gets all box selected
  1218. $rows = Tools::getValue('supply_order_detailBox');
  1219. if (!$rows)
  1220. {
  1221. $this->errors[] = Tools::displayError('You did not select any products to update.');
  1222. return;
  1223. }
  1224. // final array with id_supply_order_detail and value to update
  1225. $to_update = array();
  1226. // gets quantity for each id_order_detail
  1227. foreach ($rows as $row)
  1228. {
  1229. if (Tools::getValue('quantity_received_today_'.$row))
  1230. $to_update[$row] = (int)Tools::getValue('quantity_received_today_'.$row);
  1231. }
  1232. // checks if there is something to update
  1233. if (!count($to_update))
  1234. {
  1235. $this->errors[] = Tools::displayError('You did not select any products to update.');
  1236. return;
  1237. }
  1238. $supply_order = new SupplyOrder((int)Tools::getValue('id_supply_order'));
  1239. foreach ($to_update as $id_supply_order_detail => $quantity)
  1240. {
  1241. $supply_order_detail = new SupplyOrderDetail($id_supply_order_detail);
  1242. if (Validate::isLoadedObject($supply_order_detail) && Validate::isLoadedObject($supply_order))
  1243. {
  1244. // checks if quantity is valid
  1245. // It's possible to receive more quantity than expected in case of a shipping error from the supplier
  1246. if (!Validate::isInt($quantity) || $quantity <= 0)
  1247. $this->errors[] = sprintf(Tools::displayError('Quantity (%d) for product #%d is not valid'),
  1248. (int)$quantity, (int)$id_supply_order_detail);
  1249. else // everything is valid : updates
  1250. {
  1251. // creates the history
  1252. $supplier_receipt_history = new SupplyOrderReceiptHistory();
  1253. $supplier_receipt_history->id_supply_order_detail = (int)$id_supply_order_detail;
  1254. $supplier_receipt_history->id_employee = (int)$this->context->employee->id;
  1255. $supplier_receipt_history->employee_firstname = pSQL($this->context->employee->firstname);
  1256. $supplier_receipt_history->employee_lastname = pSQL($this->context->employee->lastname);
  1257. $supplier_receipt_history->id_supply_order_state = (int)$supply_order->id_supply_order_state;
  1258. $supplier_receipt_history->quantity = (int)$quantity;
  1259. // updates quantity received
  1260. $supply_order_detail->quantity_received += (int)$quantity;
  1261. // if current state is "Pending receipt", then we sets it to "Order received in part"
  1262. if (3 == $supply_order->id_s

Large files files are truncated, but you can click here to view the full file