uc_taxes.install 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the uc_taxes module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function uc_taxes_schema() {
  10. $schema = array();
  11. $schema['uc_taxes'] = array(
  12. 'description' => 'Stores tax information.',
  13. 'fields' => array(
  14. 'id' => array(
  15. 'description' => 'Primary key: Unique tax rate id.',
  16. 'type' => 'serial',
  17. 'unsigned' => TRUE,
  18. 'not null' => TRUE,
  19. ),
  20. 'name' => array(
  21. 'description' => 'The tax rate name.',
  22. 'type' => 'varchar',
  23. 'length' => 255,
  24. 'not null' => TRUE,
  25. 'default' => '',
  26. ),
  27. 'rate' => array(
  28. 'description' => 'The tax rate multiplier.',
  29. 'type' => 'float',
  30. 'not null' => TRUE,
  31. 'default' => 0.0,
  32. ),
  33. 'shippable' => array(
  34. 'description' => 'Flag that describes how this rate applies to shippable and non-shippable products. 0 => Disregard shipability. 1 => Apply tax to shippable products only.',
  35. 'type' => 'int',
  36. 'size' => 'tiny',
  37. 'unsigned' => TRUE,
  38. 'not null' => TRUE,
  39. 'default' => 0,
  40. ),
  41. 'taxed_product_types' => array(
  42. 'description' => 'Serialized array of node types to be taxed.',
  43. 'type' => 'text',
  44. 'serialize' => TRUE,
  45. ),
  46. 'taxed_line_items' => array(
  47. 'description' => 'Serialized array of line item types to be taxed.',
  48. 'type' => 'text',
  49. 'serialize' => TRUE,
  50. ),
  51. 'weight' => array(
  52. 'description' => 'The weight of this tax rate in relation to other rates.',
  53. 'type' => 'int',
  54. 'size' => 'tiny',
  55. 'not null' => TRUE,
  56. 'default' => 0,
  57. ),
  58. 'display_include' => array(
  59. 'description' => 'Boolean flag indicating that product prices should be displayed including the tax.',
  60. 'type' => 'int',
  61. 'size' => 'tiny',
  62. 'unsigned' => TRUE,
  63. 'not null' => TRUE,
  64. 'default' => 0,
  65. ),
  66. 'inclusion_text' => array(
  67. 'description' => 'Text to be shown near a product price that includes tax.',
  68. 'type' => 'varchar',
  69. 'length' => 255,
  70. 'not null' => TRUE,
  71. 'default' => '',
  72. ),
  73. ),
  74. 'primary key' => array('id'),
  75. );
  76. $schema['uc_taxed_product_types'] = array(
  77. 'fields' => array(
  78. 'tax_id' => array(
  79. 'description' => 'Tax rate id',
  80. 'type' => 'int',
  81. 'not null' => TRUE,
  82. 'default' => 0,
  83. ),
  84. 'type' => array(
  85. 'description' => 'Node type',
  86. 'type' => 'varchar',
  87. 'length' => 32,
  88. 'not null' => TRUE,
  89. 'default' => '',
  90. ),
  91. ),
  92. 'primary key' => array('tax_id', 'type'),
  93. 'indexes' => array(
  94. 'type' => array('type'),
  95. ),
  96. );
  97. $schema['uc_taxed_line_items'] = $schema['uc_taxed_product_types'];
  98. $schema['uc_taxed_line_items']['fields']['type']['description'] = 'Line item type';
  99. return $schema;
  100. }
  101. /**
  102. * Implements hook_update_last_removed().
  103. */
  104. function uc_taxes_update_last_removed() {
  105. return 6003;
  106. }
  107. /**
  108. * Add "price including tax" columns.
  109. */
  110. function uc_taxes_update_7000() {
  111. db_add_field('uc_taxes', 'include', array(
  112. 'description' => 'Boolean flag indicating that product prices should be displayed including the tax.',
  113. 'type' => 'int',
  114. 'size' => 'tiny',
  115. 'unsigned' => TRUE,
  116. 'not null' => TRUE,
  117. 'default' => 0,
  118. ));
  119. db_add_field('uc_taxes', 'inclusion_text', array(
  120. 'description' => 'Text to be shown near a product price that includes tax.',
  121. 'type' => 'varchar',
  122. 'length' => 255,
  123. 'not null' => TRUE,
  124. 'default' => '',
  125. ));
  126. return t('Added "price including tax" columns.');
  127. }
  128. /**
  129. * Separate taxed product types and line items to joined tables.
  130. */
  131. function uc_taxes_update_7001() {
  132. $table = array(
  133. 'fields' => array(
  134. 'tax_id' => array(
  135. 'description' => 'Tax rate id',
  136. 'type' => 'int',
  137. 'not null' => TRUE,
  138. 'default' => 0,
  139. ),
  140. 'type' => array(
  141. 'description' => 'Node type',
  142. 'type' => 'varchar',
  143. 'length' => 32,
  144. 'not null' => TRUE,
  145. 'default' => '',
  146. ),
  147. ),
  148. 'primary key' => array('tax_id', 'type'),
  149. 'indexes' => array(
  150. 'type' => array('type'),
  151. ),
  152. );
  153. db_create_table('uc_taxed_product_types', $table);
  154. $table['fields']['type']['description'] = 'Line item type';
  155. db_create_table('uc_taxed_line_items', $table);
  156. $p_insert = db_insert('uc_taxed_product_types')->fields(array('tax_id', 'type'));
  157. $l_insert = db_insert('uc_taxed_line_items')->fields(array('tax_id', 'type'));
  158. $result = db_query("SELECT id, taxed_product_types, taxed_line_items FROM {uc_taxes}");
  159. foreach ($result as $tax) {
  160. $tax->taxed_product_types = unserialize($tax->taxed_product_types);
  161. $tax->taxed_line_items = unserialize($tax->taxed_line_items);
  162. foreach ($tax->taxed_product_types as $type) {
  163. $p_insert->values(array(
  164. 'tax_id' => $tax->id,
  165. 'type' => $type,
  166. ));
  167. }
  168. foreach ($tax->taxed_line_items as $type) {
  169. $l_insert->values(array(
  170. 'tax_id' => $tax->id,
  171. 'type' => $type,
  172. ));
  173. }
  174. $p_insert->execute();
  175. $l_insert->execute();
  176. }
  177. }
  178. /**
  179. * Fix schema mismatch in update 7000.
  180. */
  181. function uc_taxes_update_7002() {
  182. if (db_field_exists('uc_taxes', 'include')) {
  183. db_change_field('uc_taxes', 'include', 'display_include', array(
  184. 'description' => 'Boolean flag indicating that product prices should be displayed including the tax.',
  185. 'type' => 'int',
  186. 'size' => 'tiny',
  187. 'unsigned' => TRUE,
  188. 'not null' => TRUE,
  189. 'default' => 0,
  190. ));
  191. }
  192. db_change_field('uc_taxes', 'inclusion_text', 'inclusion_text', array(
  193. 'description' => 'Text to be shown near a product price that includes tax.',
  194. 'type' => 'varchar',
  195. 'length' => 255,
  196. 'not null' => TRUE,
  197. 'default' => '',
  198. ));
  199. }
  200. /**
  201. * Make sure orders have stored tax data.
  202. */
  203. function uc_taxes_update_7003(&$sandbox) {
  204. if (!isset($sandbox['progress'])) {
  205. $taxes = db_query("SELECT id FROM {uc_taxes}")->fetchField();
  206. if (!$taxes) {
  207. return;
  208. }
  209. $sandbox['current'] = 0;
  210. $sandbox['progress'] = 0;
  211. $sandbox['max'] = db_query("SELECT COUNT(order_id) FROM {uc_orders}")->fetchField();
  212. }
  213. $t = get_t();
  214. $limit = 100;
  215. $orders = db_query_range("SELECT order_id FROM {uc_orders} WHERE order_id > :current ORDER BY order_id", 0, $limit, array(':current' => $sandbox['current']))->fetchCol();
  216. foreach ($orders as $order_id) {
  217. $order = uc_order_load($order_id);
  218. uc_order_save($order);
  219. $sandbox['current'] = $order_id;
  220. $sandbox['progress']++;
  221. }
  222. if ($sandbox['progress'] < $sandbox['max']) {
  223. $sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['max']);
  224. }
  225. else {
  226. $sandbox['#finished'] = 1;
  227. }
  228. }