PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminTaxesController.php

https://gitlab.com/goolic/PrestaShop
PHP | 297 lines | 218 code | 26 blank | 53 comment | 29 complexity | bc5343e03002e288de4edc9ff05f52bb MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 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-2015 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. * @property Tax $object
  28. */
  29. class AdminTaxesControllerCore extends AdminController
  30. {
  31. public function __construct()
  32. {
  33. $this->bootstrap = true;
  34. $this->table = 'tax';
  35. $this->className = 'Tax';
  36. $this->lang = true;
  37. $this->addRowAction('edit');
  38. $this->addRowAction('delete');
  39. $this->bulk_actions = array(
  40. 'delete' => array(
  41. 'text' => $this->l('Delete selected'),
  42. 'confirm' => $this->l('Delete selected items?'),
  43. 'icon' => 'icon-trash'
  44. )
  45. );
  46. $this->fields_list = array(
  47. 'id_tax' => array('title' => $this->l('ID'), 'align' => 'center', 'class' => 'fixed-width-xs'),
  48. 'name' => array('title' => $this->l('Name'), 'width' => 'auto'),
  49. 'rate' => array('title' => $this->l('Rate'), 'align' => 'center', 'suffix' => '%' , 'class' => 'fixed-width-md'),
  50. 'active' => array('title' => $this->l('Enabled'), 'width' => 25, 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false, 'class' => 'fixed-width-sm', 'remove_onclick' => true)
  51. );
  52. $ecotax_desc = '';
  53. if (Configuration::get('PS_USE_ECOTAX')) {
  54. $ecotax_desc = $this->l('If you disable the ecotax, the ecotax for all your products will be set to 0.');
  55. }
  56. $this->fields_options = array(
  57. 'general' => array(
  58. 'title' => $this->l('Tax options'),
  59. 'fields' => array(
  60. 'PS_TAX' => array(
  61. 'title' => $this->l('Enable tax'),
  62. 'desc' => $this->l('Select whether or not to include tax on purchases.'),
  63. 'cast' => 'intval', 'type' => 'bool'),
  64. 'PS_TAX_DISPLAY' => array(
  65. 'title' => $this->l('Display tax in the shopping cart'),
  66. 'desc' => $this->l('Select whether or not to display tax on a distinct line in the cart.'),
  67. 'cast' => 'intval',
  68. 'type' => 'bool'),
  69. 'PS_TAX_ADDRESS_TYPE' => array(
  70. 'title' => $this->l('Based on'),
  71. 'cast' => 'pSQL',
  72. 'type' => 'select',
  73. 'list' => array(
  74. array(
  75. 'name' => $this->l('Invoice address'),
  76. 'id' => 'id_address_invoice'
  77. ),
  78. array(
  79. 'name' => $this->l('Delivery address'),
  80. 'id' => 'id_address_delivery')
  81. ),
  82. 'identifier' => 'id'
  83. ),
  84. 'PS_USE_ECOTAX' => array(
  85. 'title' => $this->l('Use ecotax'),
  86. 'desc' => $ecotax_desc,
  87. 'validation' => 'isBool',
  88. 'cast' => 'intval',
  89. 'type' => 'bool'
  90. ),
  91. ),
  92. 'submit' => array('title' => $this->l('Save'))
  93. ),
  94. );
  95. if (Configuration::get('PS_USE_ECOTAX') || Tools::getValue('PS_USE_ECOTAX')) {
  96. $this->fields_options['general']['fields']['PS_ECOTAX_TAX_RULES_GROUP_ID'] = array(
  97. 'title' => $this->l('Ecotax'),
  98. 'hint' => $this->l('Define the ecotax (e.g. French ecotax: 19.6%).'),
  99. 'cast' => 'intval',
  100. 'type' => 'select',
  101. 'identifier' => 'id_tax_rules_group',
  102. 'list' => TaxRulesGroup::getTaxRulesGroupsForOptions()
  103. );
  104. }
  105. parent::__construct();
  106. $this->_where .= ' AND a.deleted = 0';
  107. }
  108. public function initPageHeaderToolbar()
  109. {
  110. if (empty($this->display)) {
  111. $this->page_header_toolbar_btn['new_tax'] = array(
  112. 'href' => self::$currentIndex.'&addtax&token='.$this->token,
  113. 'desc' => $this->l('Add new tax', null, null, false),
  114. 'icon' => 'process-icon-new'
  115. );
  116. }
  117. parent::initPageHeaderToolbar();
  118. }
  119. /**
  120. * Display delete action link
  121. *
  122. * @param string|null $token
  123. * @param int $id
  124. *
  125. * @return string
  126. * @throws Exception
  127. * @throws SmartyException
  128. */
  129. public function displayDeleteLink($token = null, $id)
  130. {
  131. if (!array_key_exists('Delete', self::$cache_lang)) {
  132. self::$cache_lang['Delete'] = $this->l('Delete');
  133. }
  134. if (!array_key_exists('DeleteItem', self::$cache_lang)) {
  135. self::$cache_lang['DeleteItem'] = $this->l('Delete item #', __CLASS__, true, false);
  136. }
  137. if (TaxRule::isTaxInUse($id)) {
  138. $confirm = $this->l('This tax is currently in use as a tax rule. Are you sure you\'d like to continue?', null, true, false);
  139. }
  140. $this->context->smarty->assign(array(
  141. 'href' => self::$currentIndex.'&'.$this->identifier.'='.$id.'&delete'.$this->table.'&token='.($token != null ? $token : $this->token),
  142. 'confirm' => (isset($confirm) ? '\r'.$confirm : self::$cache_lang['DeleteItem'].$id.' ? '),
  143. 'action' => self::$cache_lang['Delete'],
  144. ));
  145. return $this->context->smarty->fetch('helpers/list/list_action_delete.tpl');
  146. }
  147. /**
  148. * Fetch the template for action enable
  149. *
  150. * @param string $token
  151. * @param int $id
  152. * @param int $value state enabled or not
  153. * @param string $active status
  154. * @param int $id_category
  155. * @param int $id_product
  156. */
  157. public function displayEnableLink($token, $id, $value, $active, $id_category = null, $id_product = null)
  158. {
  159. if ($value && TaxRule::isTaxInUse($id)) {
  160. $confirm = $this->l('This tax is currently in use as a tax rule. If you continue, this tax will be removed from the tax rule. Are you sure you\'d like to continue?', null, true, false);
  161. }
  162. $tpl_enable = $this->context->smarty->createTemplate('helpers/list/list_action_enable.tpl');
  163. $tpl_enable->assign(array(
  164. 'enabled' => (bool)$value,
  165. 'url_enable' => self::$currentIndex.'&'.$this->identifier.'='.(int)$id.'&'.$active.$this->table.
  166. ((int)$id_category && (int)$id_product ? '&id_category='.(int)$id_category : '').'&token='.($token != null ? $token : $this->token),
  167. 'confirm' => isset($confirm) ? $confirm : null,
  168. ));
  169. return $tpl_enable->fetch();
  170. }
  171. public function renderForm()
  172. {
  173. $this->fields_form = array(
  174. 'legend' => array(
  175. 'title' => $this->l('Taxes'),
  176. 'icon' => 'icon-money'
  177. ),
  178. 'input' => array(
  179. array(
  180. 'type' => 'text',
  181. 'label' => $this->l('Name'),
  182. 'name' => 'name',
  183. 'required' => true,
  184. 'lang' => true,
  185. 'hint' => $this->l('Tax name to display in carts and on invoices (e.g. "VAT").').' - '.$this->l('Invalid characters').' <>;=#{}'
  186. ),
  187. array(
  188. 'type' => 'text',
  189. 'label' => $this->l('Rate'),
  190. 'name' => 'rate',
  191. 'maxlength' => 6,
  192. 'required' => true,
  193. 'hint' => $this->l('Format: XX.XX or XX.XXX (e.g. 19.60 or 13.925)').' - '.$this->l('Invalid characters').' <>;=#{}'
  194. ),
  195. array(
  196. 'type' => 'switch',
  197. 'label' => $this->l('Enable'),
  198. 'name' => 'active',
  199. 'required' => false,
  200. 'is_bool' => true,
  201. 'values' => array(
  202. array(
  203. 'id' => 'active_on',
  204. 'value' => 1,
  205. 'label' => $this->l('Enabled')
  206. ),
  207. array(
  208. 'id' => 'active_off',
  209. 'value' => 0,
  210. 'label' => $this->l('Disabled')
  211. )
  212. )
  213. )
  214. ),
  215. 'submit' => array(
  216. 'title' => $this->l('Save')
  217. )
  218. );
  219. return parent::renderForm();
  220. }
  221. public function postProcess()
  222. {
  223. if ($this->action == 'save') {
  224. /* Checking fields validity */
  225. $this->validateRules();
  226. if (!count($this->errors)) {
  227. $id = (int)(Tools::getValue('id_'.$this->table));
  228. /* Object update */
  229. if (isset($id) && !empty($id)) {
  230. /** @var Tax $object */
  231. $object = new $this->className($id);
  232. if (Validate::isLoadedObject($object)) {
  233. $this->copyFromPost($object, $this->table);
  234. $result = $object->update(false, false);
  235. if (!$result) {
  236. $this->errors[] = Tools::displayError('An error occurred while updating an object.').' <b>'.$this->table.'</b>';
  237. } elseif ($this->postImage($object->id)) {
  238. Tools::redirectAdmin(self::$currentIndex.'&id_'.$this->table.'='.$object->id.'&conf=4'.'&token='.$this->token);
  239. }
  240. } else {
  241. $this->errors[] = Tools::displayError('An error occurred while updating an object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
  242. }
  243. }
  244. /* Object creation */
  245. else {
  246. /** @var Tax $object */
  247. $object = new $this->className();
  248. $this->copyFromPost($object, $this->table);
  249. if (!$object->add()) {
  250. $this->errors[] = Tools::displayError('An error occurred while creating an object.').' <b>'.$this->table.'</b>';
  251. } elseif (($_POST['id_'.$this->table] = $object->id /* voluntary */) && $this->postImage($object->id) && $this->_redirect) {
  252. Tools::redirectAdmin(self::$currentIndex.'&id_'.$this->table.'='.$object->id.'&conf=3'.'&token='.$this->token);
  253. }
  254. }
  255. }
  256. } else {
  257. parent::postProcess();
  258. }
  259. }
  260. public function updateOptionPsUseEcotax($value)
  261. {
  262. $old_value = (int)Configuration::get('PS_USE_ECOTAX');
  263. if ($old_value != $value) {
  264. // Reset ecotax
  265. if ($value == 0) {
  266. Product::resetEcoTax();
  267. }
  268. Configuration::updateValue('PS_USE_ECOTAX', (int)$value);
  269. }
  270. }
  271. }