function commerce_avatax_line_item_new in Drupal Commerce Connector for AvaTax 7.5
Same name and namespace in other branches
- 7.3 commerce_avatax.module \commerce_avatax_line_item_new()
- 7.4 commerce_avatax.module \commerce_avatax_line_item_new()
Creates a new AvaTax line item populated with the proper values.
Parameters
array $tax_price: A price array used to initialize the value of the line item's unit price.
string $tax_type: A string determining the price component to add.
int $order_id: The ID of the order the line item belongs to.
array $data: An array value to initialize the line item's data array with.
Return value
The AvaTax line item initialized to the given unit price.
File
- ./
commerce_avatax.module, line 312 - AvaTax service integration from Avalara, Inc.
Code
function commerce_avatax_line_item_new($tax_price, $tax_type = 'sales_tax', $order_id = 0, $data = array()) {
$types_mapping = array(
'sales_tax' => array(
'line_item_label' => t('Sales Tax'),
'price_component' => 'avatax_sales_tax',
),
'vat' => array(
'line_item_label' => t('VAT'),
'price_component' => 'avatax_vat',
),
);
if (isset($types_mapping[$tax_type])) {
$line_item_label = $types_mapping[$tax_type]['line_item_label'];
$price_component = $types_mapping[$tax_type]['price_component'];
}
else {
// Defaults to Sales Tax.
$line_item_label = t('Sales Tax');
$price_component = $types_mapping['sales_tax']['price_component'];
}
// Create the new line item.
$line_item = entity_create('commerce_line_item', array(
'type' => 'avatax',
'order_id' => $order_id,
'quantity' => 1,
'line_item_label' => $line_item_label,
'data' => $data,
));
// Set the unit price.
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->commerce_unit_price->amount = $tax_price['amount'];
$line_item_wrapper->commerce_unit_price->currency_code = $tax_price['currency_code'];
// Reset the data array of the line item total field to only include a
// base price component, set the currency code from the order.
$base_price = array(
'amount' => 0,
'currency_code' => $tax_price['currency_code'],
'data' => array(),
);
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($base_price, $price_component, $tax_price, TRUE);
// Return the line item.
return $line_item;
}