uc_taxes.admin.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * @file
  4. * Taxes administration menu items.
  5. */
  6. /**
  7. * Displays a list of tax rates.
  8. */
  9. function uc_taxes_admin_settings() {
  10. $header = array(t('Name'), t('Rate'), t('Taxed products'), t('Taxed product types'), t('Taxed line items'), t('Weight'), array('data' => t('Operations'), 'colspan' => 4));
  11. $rows = array();
  12. foreach (uc_taxes_rate_load() as $rate_id => $rate) {
  13. $rows[] = array(
  14. check_plain($rate->name),
  15. $rate->rate * 100 . '%',
  16. $rate->shippable ? t('Shippable products') : t('Any product'),
  17. implode(', ', $rate->taxed_product_types),
  18. implode(', ', $rate->taxed_line_items),
  19. $rate->weight,
  20. l(t('edit'), 'admin/store/settings/taxes/' . $rate_id . '/edit'),
  21. l(t('conditions'), 'admin/store/settings/taxes/manage/uc_taxes_' . $rate_id),
  22. l(t('clone'), 'admin/store/settings/taxes/' . $rate_id . '/clone'),
  23. l(t('delete'), 'admin/store/settings/taxes/' . $rate_id . '/delete'),
  24. );
  25. }
  26. $build['taxes'] = array(
  27. '#theme' => 'table',
  28. '#header' => $header,
  29. '#rows' => $rows,
  30. '#empty' => t('No rates available.'),
  31. );
  32. return $build;
  33. }
  34. /**
  35. * Builds a form to add or edit a tax rate.
  36. *
  37. * @param $rate_id
  38. * The ID of the tax rate to edit; leave NULL to add a new rate.
  39. *
  40. * @see uc_taxes_form_validate()
  41. * @see uc_taxes_form_submit()
  42. * @ingroup forms
  43. */
  44. function uc_taxes_form($form, &$form_state, $rate_id = NULL) {
  45. // If a rate ID was specified...
  46. if ($rate_id) {
  47. // Load the tax rate and set the page title.
  48. $rate = uc_taxes_rate_load($rate_id);
  49. drupal_set_title($rate->name);
  50. }
  51. $form['id'] = array(
  52. '#type' => 'value',
  53. '#value' => $rate_id,
  54. );
  55. $form['name'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('Name'),
  58. '#description' => t('This name will appear to the customer when this tax is applied to an order.'),
  59. '#default_value' => $rate_id ? $rate->name : '',
  60. '#required' => TRUE,
  61. );
  62. $form['rate'] = array(
  63. '#type' => 'textfield',
  64. '#title' => t('Rate'),
  65. '#description' => t('The tax rate as a percent or decimal. Examples: 6%, .06'),
  66. '#default_value' => $rate_id ? (($rate->rate * 100) . '%') : '',
  67. '#size' => 15,
  68. '#required' => TRUE,
  69. );
  70. $form['shippable'] = array(
  71. '#type' => 'radios',
  72. '#title' => t('Taxed products'),
  73. '#options' => array(
  74. t('Apply tax to any product regardless of its shippability.'),
  75. t('Apply tax to shippable products only.'),
  76. ),
  77. '#default_value' => $rate_id ? $rate->shippable : 0,
  78. );
  79. // TODO: Remove the need for a special case for product kit module.
  80. $options = uc_product_type_names();
  81. unset($options['product_kit']);
  82. $options['blank-line'] = t('"Blank line" product');
  83. $form['taxed_product_types'] = array(
  84. '#type' => 'checkboxes',
  85. '#title' => t('Taxed product types'),
  86. '#description' => t('Apply taxes to the specified product types/classes.'),
  87. '#options' => $options,
  88. '#default_value' => $rate_id ? $rate->taxed_product_types : array(),
  89. );
  90. $options = array();
  91. foreach (_uc_line_item_list() as $id => $line_item) {
  92. if (!in_array($id, array('subtotal', 'tax_subtotal', 'total', 'tax_display'))) {
  93. $options[$id] = $line_item['title'];
  94. }
  95. }
  96. $form['taxed_line_items'] = array(
  97. '#type' => 'checkboxes',
  98. '#title' => t('Taxed line items'),
  99. '#description' => t('Adds the checked line item types to the total before applying this tax.'),
  100. '#options' => $options,
  101. '#default_value' => $rate_id ? $rate->taxed_line_items : array(),
  102. );
  103. $form['weight'] = array(
  104. '#type' => 'weight',
  105. '#title' => t('Weight'),
  106. '#description' => t('Taxes are sorted by weight and then applied to the order sequentially. This value is important when taxes need to include other tax line items.'),
  107. '#default_value' => $rate_id ? $rate->weight : 0,
  108. );
  109. $form['display_include'] = array(
  110. '#type' => 'checkbox',
  111. '#title' => t('Include this tax when displaying product prices.'),
  112. '#default_value' => $rate_id ? $rate->display_include : 0,
  113. );
  114. $form['inclusion_text'] = array(
  115. '#type' => 'textfield',
  116. '#title' => t('Tax inclusion text'),
  117. '#description' => t('This text will be displayed near the price to indicate that it includes tax.'),
  118. '#default_value' => $rate_id ? $rate->inclusion_text : '',
  119. );
  120. $form['actions'] = array('#type' => 'actions');
  121. $form['actions']['submit'] = array(
  122. '#type' => 'submit',
  123. '#value' => t('Submit'),
  124. '#suffix' => l(t('Cancel'), 'admin/store/settings/taxes'),
  125. );
  126. return $form;
  127. }
  128. /**
  129. * Ensures that tax rates are positive numbers.
  130. *
  131. * @see uc_taxes_form()
  132. * @see uc_taxes_form_submit()
  133. */
  134. function uc_taxes_form_validate($form, &$form_state) {
  135. if (!empty($form_state['values']['rate']) && (floatval($form_state['values']['rate']) < 0)) {
  136. form_set_error('rate', t('Rate must be a positive number. No commas and only one decimal point.'));
  137. }
  138. }
  139. /**
  140. * Form submission handler for uc_taxes_form().
  141. *
  142. * @see uc_taxes_form()
  143. * @see uc_taxes_form_validate()
  144. */
  145. function uc_taxes_form_submit($form, &$form_state) {
  146. // Determine the decimal rate value.
  147. if (strpos($form_state['values']['rate'], '%')) {
  148. $form_state['values']['rate'] = floatval($form_state['values']['rate']) / 100;
  149. }
  150. else {
  151. $form_state['values']['rate'] = floatval($form_state['values']['rate']);
  152. }
  153. // Build the rate object based on the form values and save it.
  154. $rate = (object) array(
  155. 'id' => $form_state['values']['id'],
  156. 'name' => $form_state['values']['name'],
  157. 'rate' => $form_state['values']['rate'],
  158. 'taxed_product_types' => array_filter($form_state['values']['taxed_product_types']),
  159. 'taxed_line_items' => array_filter($form_state['values']['taxed_line_items']),
  160. 'weight' => $form_state['values']['weight'],
  161. 'shippable' => $form_state['values']['shippable'],
  162. 'display_include' => $form_state['values']['display_include'],
  163. 'inclusion_text' => $form_state['values']['inclusion_text'],
  164. );
  165. $rate = uc_taxes_rate_save($rate);
  166. // Update the name of the associated conditions.
  167. $conditions = rules_config_load('uc_taxes_' . $form_state['values']['id']);
  168. if ($conditions) {
  169. $conditions->label = $form_state['values']['name'];
  170. $conditions->save();
  171. }
  172. // Display a message and redirect back to the overview,
  173. // or the conditions page for new taxes.
  174. if ($form_state['values']['id']) {
  175. drupal_set_message(t('Tax rate %name saved.', array('%name' => $form_state['values']['name'])));
  176. $form_state['redirect'] = 'admin/store/settings/taxes';
  177. }
  178. else {
  179. drupal_set_message(t('Tax rate %name created.', array('%name' => $form_state['values']['name'])));
  180. $form_state['redirect'] = 'admin/store/settings/taxes/manage/uc_taxes_' . $rate->id;
  181. }
  182. }
  183. /**
  184. * Clones a tax rate.
  185. */
  186. function uc_taxes_clone($rate_id) {
  187. // Load the source rate object.
  188. $rate = uc_taxes_rate_load($rate_id);
  189. $name = $rate->name;
  190. // Tweak the name and unset the rate ID.
  191. $rate->name = t('Copy of !name', array('!name' => $rate->name));
  192. $rate->id = NULL;
  193. // Save the new rate without clearing the Rules cache.
  194. $rate = uc_taxes_rate_save($rate, FALSE);
  195. // Clone the associated conditions as well.
  196. if ($conditions = rules_config_load('uc_taxes_' . $rate_id)) {
  197. $conditions->id = NULL;
  198. $conditions->name = '';
  199. $conditions->save('uc_taxes_' . $rate->id);
  200. }
  201. entity_flush_caches();
  202. // Display a message and redirect back to the overview.
  203. drupal_set_message(t('Tax rate %name cloned.', array('%name' => $name)));
  204. drupal_goto('admin/store/settings/taxes');
  205. }
  206. /**
  207. * Deletes a tax rule.
  208. *
  209. * @see uc_taxes_delete_form_submit()
  210. * @ingroup forms
  211. */
  212. function uc_taxes_delete_form($form, &$form_state, $rate_id) {
  213. // Bail if we got a bad rate ID.
  214. if (!$rate = uc_taxes_rate_load($rate_id)) {
  215. drupal_set_message(t('That tax rate does not exist.'), 'error');
  216. drupal_goto('admin/store/settings/taxes');
  217. }
  218. $form['rate_id'] = array(
  219. '#type' => 'value',
  220. '#value' => $rate_id,
  221. );
  222. $form['name'] = array(
  223. '#type' => 'value',
  224. '#value' => $rate->name,
  225. );
  226. return confirm_form($form, t('Are you sure you want to delete @name?', array('@name' => $rate->name)), 'admin/store/settings/taxes', t('This action cannot be undone. Any orders that have been charged this tax may lose tax if you proceed.<br />If you just want this tax to no longer be applied to orders, consider disabling its predicate instead.'), t('Delete'), t('Cancel'));
  227. }
  228. /**
  229. * Form submission handler for uc_taxes_delete_form().
  230. *
  231. * @see uc_taxes_delete_form()
  232. */
  233. function uc_taxes_delete_form_submit($form, &$form_state) {
  234. // Delete the tax rate.
  235. uc_taxes_rate_delete($form_state['values']['rate_id']);
  236. // Display a message and redirect back to the overview.
  237. drupal_set_message(t('Tax rate %name deleted.', array('%name' => $form_state['values']['name'])));
  238. $form_state['redirect'] = 'admin/store/settings/taxes';
  239. }