function commerce_pricelist_unit_price_amount in Commerce Pricelist 7
Rules action: set the price to a specific amount and apply inclusive taxes.
See also
commerce_line_item_unit_price_amount
commerce_tax_field_attach_load
File
- ./
commerce_pricelist.rules.inc, line 122 - Rules integration for line items.
Code
function commerce_pricelist_unit_price_amount($line_item, $amount, $component_name, $round_mode) {
if (is_numeric($amount)) {
$wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
// Calculate the updated amount and create a price array representing the
// difference between it and the current amount.
$current_amount = $unit_price['amount'];
$updated_amount = commerce_round($round_mode, $amount);
$difference = array(
'amount' => $updated_amount - $current_amount,
'currency_code' => $unit_price['currency_code'],
'data' => array(),
);
// If it specifies a inclusive tax and we can load it...
if (!empty($unit_price['data']['include_tax']) && ($tax_rate = commerce_tax_rate_load($unit_price['data']['include_tax']))) {
// Reverse apply the tax.
$tax_amount = $difference['amount'] - $difference['amount'] / (1 + $tax_rate['rate']);
$tax_amount = commerce_tax_rate_round_amount($tax_rate, $tax_amount);
// Subtract taxes
$difference['amount'] -= $tax_amount;
// Add the tax to the data array.
$tax_component = $difference;
$tax_component['amount'] = $tax_amount;
$tax_component['data']['tax_rate'] = $tax_rate;
$wrapper->commerce_unit_price->data = commerce_price_component_add($wrapper->commerce_unit_price
->value(), $tax_rate['price_component'], $tax_component, TRUE);
}
// Set the amount of the unit price and add the difference as a component.
$wrapper->commerce_unit_price->amount = $updated_amount;
$wrapper->commerce_unit_price->data = commerce_price_component_add($wrapper->commerce_unit_price
->value(), $component_name, $difference, TRUE);
}
}