uc_taxes.api.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Taxes module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Calculates tax line items for an order.
  12. *
  13. * @param $order
  14. * An order object or an order id.
  15. *
  16. * @return array
  17. * An array of tax line item objects keyed by a module-specific id.
  18. */
  19. function hook_uc_calculate_tax($order) {
  20. if (!is_object($order)) {
  21. return array();
  22. }
  23. if (empty($order->delivery_postal_code)) {
  24. $order->delivery_postal_code = $order->billing_postal_code;
  25. }
  26. if (empty($order->delivery_zone)) {
  27. $order->delivery_zone = $order->billing_zone;
  28. }
  29. if (empty($order->delivery_country)) {
  30. $order->delivery_country = $order->billing_country;
  31. }
  32. $order->taxes = array();
  33. if (isset($order->order_status)) {
  34. $state = uc_order_status_data($order->order_status, 'state');
  35. $use_same_rates = in_array($state, array('payment_received', 'completed'));
  36. }
  37. else {
  38. $use_same_rates = FALSE;
  39. }
  40. foreach (uc_taxes_rate_load() as $tax) {
  41. if ($use_same_rates) {
  42. foreach ((array) $order->line_items as $old_line) {
  43. if ($old_line['type'] == 'tax' && $old_line['data']['tax_id'] == $tax->id) {
  44. $tax->rate = $old_line['data']['tax_rate'];
  45. break;
  46. }
  47. }
  48. }
  49. $set = rules_config_load('uc_taxes_' . $tax->id);
  50. if ($set->execute($order)) {
  51. $line_item = uc_taxes_apply_tax($order, $tax);
  52. if ($line_item) {
  53. $order->taxes[$line_item->id] = $line_item;
  54. }
  55. }
  56. }
  57. return $order->taxes;
  58. }
  59. /**
  60. * @} End of "addtogroup hooks".
  61. */