| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This file contains the Conditional Action hooks and functions necessary to
|
| 7 |
* make the tax related entity, conditions, events, and actions work.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/******************************************************************************
|
| 11 |
* Conditional Action Hooks *
|
| 12 |
******************************************************************************/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_ca_entity().
|
| 16 |
*/
|
| 17 |
function uc_taxes_ca_entity() {
|
| 18 |
$entities['tax'] = array(
|
| 19 |
'#title' => t('Tax rule'),
|
| 20 |
'#type' => 'object',
|
| 21 |
);
|
| 22 |
return $entities;
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Implementation of hook_ca_trigger().
|
| 27 |
*
|
| 28 |
* Register an event for each tax rule in {uc_taxes}.
|
| 29 |
*/
|
| 30 |
function uc_taxes_ca_trigger() {
|
| 31 |
$triggers = array();
|
| 32 |
$taxes = uc_taxes_get_rates();
|
| 33 |
foreach ($taxes as $tax) {
|
| 34 |
$triggers['calculate_tax_'. $tax->id] = array(
|
| 35 |
'#title' => t('Calculate @name', array('@name' => $tax->name)),
|
| 36 |
'#category' => 'uc_taxes',
|
| 37 |
'#arguments' => array(
|
| 38 |
'order' => array('#entity' => 'order', '#title' => t('Order')),
|
| 39 |
'tax' => array('#entity' => 'tax', '#title' => t('Tax rule')),
|
| 40 |
'user' => array('#entity' => 'user', '#title' => t('User account')),
|
| 41 |
),
|
| 42 |
);
|
| 43 |
}
|
| 44 |
return $triggers;
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Implementation of hook_action_info().
|
| 49 |
*/
|
| 50 |
function uc_taxes_ca_action() {
|
| 51 |
$actions = array();
|
| 52 |
$actions['uc_taxes_action_apply_tax'] = array(
|
| 53 |
'#title' => t('Charge a tax'),
|
| 54 |
'#category' => t('Taxes'),
|
| 55 |
'#arguments' => array(
|
| 56 |
'order' => array('#entity' => 'order', '#title' => t('Order')),
|
| 57 |
'tax' => array('#entity' => 'tax', '#title' => t('Tax')),
|
| 58 |
),
|
| 59 |
);
|
| 60 |
return $actions;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Workflow-ng action callback to calculate a tax.
|
| 65 |
*
|
| 66 |
* @param $order
|
| 67 |
* The order object being considered.
|
| 68 |
* @param $tax
|
| 69 |
* The tax rule calculating the amount.
|
| 70 |
* @return
|
| 71 |
* The line item array representing the amount of tax.
|
| 72 |
*/
|
| 73 |
function uc_taxes_action_apply_tax($order, $tax) {
|
| 74 |
$amount = 0;
|
| 75 |
if (is_array($order->products)) {
|
| 76 |
foreach ($order->products as $item) {
|
| 77 |
$node = node_load($item->nid);
|
| 78 |
if (in_array($node->type, $tax->taxed_product_types)) {
|
| 79 |
$amount += $item->price * $item->qty * $tax->rate;
|
| 80 |
}
|
| 81 |
}
|
| 82 |
}
|
| 83 |
$taxed_line_items = $tax->taxed_line_items;
|
| 84 |
if (is_array($order->line_items) && is_array($taxed_line_items)) {
|
| 85 |
foreach ($order->line_items as $key => $line_item) {
|
| 86 |
if ($line_item['type'] == 'tax' && $line_item['title'] == $tax->name) {
|
| 87 |
// Don't tax yourself.
|
| 88 |
continue;
|
| 89 |
}
|
| 90 |
if (in_array($line_item['type'], $taxed_line_items)) {
|
| 91 |
$amount += $line_item['amount'] * $tax->rate;
|
| 92 |
}
|
| 93 |
}
|
| 94 |
}
|
| 95 |
if (isset($taxed_line_items['tax'])) {
|
| 96 |
foreach ($_SESSION['taxes'] as $other_tax) {
|
| 97 |
$amount += $other_tax['amount'] * $tax->rate;
|
| 98 |
}
|
| 99 |
}
|
| 100 |
if ($amount) {
|
| 101 |
$line_item = (object)array('id' => $tax->id, 'name' => $tax->name, 'amount' => $amount, 'weight' => $tax->weight);
|
| 102 |
return $line_item;
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Implementation of hook_ca_predicate().
|
| 108 |
*
|
| 109 |
* Create a predicate for each event corresponding to a tax rule.
|
| 110 |
*/
|
| 111 |
function uc_taxes_ca_predicate() {
|
| 112 |
$predicates = array();
|
| 113 |
|
| 114 |
$taxes = uc_taxes_get_rates();
|
| 115 |
foreach ($taxes as $tax) {
|
| 116 |
$predicates['uc_taxes_'. $tax->id] = array(
|
| 117 |
'#title' => $tax->name,
|
| 118 |
'#class' => 'uc_taxes',
|
| 119 |
'#trigger' => 'calculate_tax_'. $tax->id,
|
| 120 |
'#status' => 1,
|
| 121 |
'#actions' => array(
|
| 122 |
array(
|
| 123 |
'#name' => 'uc_taxes_action_apply_tax',
|
| 124 |
'#title' => t('Apply @tax', array('@tax' => $tax->name)),
|
| 125 |
'#argument_map' => array(
|
| 126 |
'order' => 'order',
|
| 127 |
'tax' => 'tax',
|
| 128 |
),
|
| 129 |
),
|
| 130 |
),
|
| 131 |
);
|
| 132 |
}
|
| 133 |
|
| 134 |
return $predicates;
|
| 135 |
}
|