Skip to content

Commit

Permalink
Merge pull request #2 from dpradeep/VAT-365-New
Browse files Browse the repository at this point in the history
VAT-365 Proposed Tax UI changes
  • Loading branch information
kurund committed May 22, 2014
2 parents a02f377 + dc42816 commit dae066a
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -1230,10 +1230,16 @@ public function postProcess() {
$component = 'membership';
}
CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $component);
if ($params['tax_amount']) {
$this->set('tax_amount', $params['tax_amount']);
}

if ($proceFieldAmount) {
$lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
$lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount;
if (isset($lineItem[$params['priceSetId']][$fieldOption]['tax_amount'])) {
$proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount'];
}
$lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
if (!$this->_membershipBlock['is_separate_payment']) {
$params['amount'] = $proceFieldAmount; //require when separate membership not used
}
Expand Down
40 changes: 40 additions & 0 deletions CRM/Core/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ class CRM_Core_PseudoConstant {
*/
private static $accountOptionValues;

/**
* Tax Rates
* @var array
* @static
*/
private static $taxRates;

/**
* Low-level option getter, rarely accessed directly.
* NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
Expand Down Expand Up @@ -1827,5 +1834,38 @@ public static function accountOptionValues($optionGroupName, $id = null, $condit
public static function getModuleExtensions($fresh = FALSE) {
return CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles($fresh);
}


/**
* Get all tax rates
*
* The static array tax rates is returned
*
* @access public
* @static
*
* @return array - array list of tax rates with the financial type
*/
public static function getTaxRates() {
if (!self::$taxRates) {
self::$taxRates = array();
$sql = "
SELECT fa.tax_rate, efa.entity_id
FROM civicrm_entity_financial_account efa
INNER JOIN civicrm_financial_account fa ON fa.id = efa.financial_account_id
INNER JOIN civicrm_option_value cov ON cov.value = efa.account_relationship
INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
WHERE efa.entity_table = 'civicrm_financial_type'
AND cov.name = 'Sales Tax Account is'
AND cog.name = 'account_relationship'
AND fa.is_active = 1";
$dao = CRM_Core_DAO::executeQuery($sql);
while ($dao->fetch()) {
self::$taxRates[$dao->entity_id] = $dao->tax_rate;
}
}

return self::$taxRates;
}
}

102 changes: 95 additions & 7 deletions CRM/Price/BAO/PriceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,19 @@ public static function addQuickFormElement(&$qf,
//use value field.
$valueFieldName = 'amount';
$seperator = '|';
$displayOpt = CRM_Contribute_BAO_Contribution::getContributionSettings();
$displayOpt = $displayOpt['tax_display_settings'];
switch ($field->html_type) {
case 'Text':
$optionKey = key($customOption);
$count = CRM_Utils_Array::value('count', $customOption[$optionKey], '');
$max_value = CRM_Utils_Array::value('max_value', $customOption[$optionKey], '');
$priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName], $count, $max_value));
$taxAmount = 0;
if (CRM_Utils_Array::value('tax_amount', $customOption[$optionKey])) {
$taxAmount = $customOption[$optionKey]['tax_amount'];
$qf->assign('displayOpt', $displayOpt);
}
$priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName] + $taxAmount, $count, $max_value));

$extra = array();
if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) {
Expand Down Expand Up @@ -346,13 +353,30 @@ public static function addQuickFormElement(&$qf,
}

foreach ($customOption as $opId => $opt) {
$taxAmount = 0;
if ($field->is_display_amounts) {
$opt['label'] = !empty($opt['label']) ? $opt['label'] : '';
$opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
if (CRM_Utils_Array::value('tax_amount', $opt)) {
$taxAmount = $opt['tax_amount'];
if ($displayOpt == 'Do_not_show') {
$opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
}
else if ($displayOpt == 'Inclusive') {
$opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
$opt['label'] .= '<span class="crm-price-amount-label"> (includes '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($taxAmount) . ')</span>';
}
else {
$opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
$opt['label'] .= '<span class="crm-price-amount-label"> + '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($taxAmount) . '</span>';
}
}
else {
$opt['label'] = '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span> <span class="crm-price-amount-label">' . $opt['label'] . '</span>';
}
}
$count = CRM_Utils_Array::value('count', $opt, '');
$max_value = CRM_Utils_Array::value('max_value', $opt, '');
$priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value));
$priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
$extra = array('price' => json_encode(array($elementName, $priceVal)),
'data-amount' => $opt[$valueFieldName],
'data-currency' => $currencyName,
Expand Down Expand Up @@ -421,15 +445,32 @@ public static function addQuickFormElement(&$qf,
$selectOption = $allowedOptions = $priceVal = array();

foreach ($customOption as $opt) {
$taxAmount = 0;
$count = CRM_Utils_Array::value('count', $opt, '');
$max_value = CRM_Utils_Array::value('max_value', $opt, '');
$priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName], $count, $max_value));

if ($field->is_display_amounts) {
$opt['label'] .= '&nbsp;-&nbsp;';
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
if (CRM_Utils_Array::value('tax_amount', $opt)) {
$taxAmount = $opt['tax_amount'];
if ($displayOpt == 'Do_not_show') {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount);
}
else if ($displayOpt == 'Inclusive') {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount);
$opt['label'] .= '<span class="crm-price-amount-label"> (includes '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($taxAmount) . ')</span>';
}
else {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
$opt['label'] .= '<span class="crm-price-amount-label"> + '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($taxAmount) . '</span>';
}
}
else {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
}
}
$selectOption[$opt['id']] = $opt['label'];
$priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));

if (!in_array($opt['id'], $feezeOptions)) {
$allowedOptions[] = $opt['id'];
Expand All @@ -456,14 +497,31 @@ public static function addQuickFormElement(&$qf,

$check = array();
foreach ($customOption as $opId => $opt) {
$taxAmount = 0;
$count = CRM_Utils_Array::value('count', $opt, '');
$max_value = CRM_Utils_Array::value('max_value', $opt, '');
$priceVal = implode($seperator, array($opt[$valueFieldName], $count, $max_value));

if ($field->is_display_amounts) {
$opt['label'] .= '&nbsp;-&nbsp;';
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
if (CRM_Utils_Array::value('tax_amount', $opt)) {
$taxAmount = $opt['tax_amount'];
if ($displayOpt == 'Do_not_show') {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount);
}
else if ($displayOpt == 'Inclusive') {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount);
$opt['label'] .= '<span class="crm-price-amount-label"> (includes '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($taxAmount) . ')</span>';
}
else {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
$opt['label'] .= '<span class="crm-price-amount-label"> + '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($taxAmount) . '</span>';
}
}
else {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
}
}
$priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
$check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'],
array('price' => json_encode(array($opt['id'], $priceVal)),
'data-amount' => $opt[$valueFieldName],
Expand Down Expand Up @@ -506,6 +564,17 @@ public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FA
$values = array();
CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded);
$options[$fieldId] = $values;
$taxRates = CRM_Core_PseudoConstant::getTaxRates();

// ToDo - Code for Hook Invoke

foreach ($options[$fieldId] as $priceFieldId => $priceFieldValues) {
if (array_key_exists($priceFieldValues['financial_type_id'], $taxRates)) {
$options[$fieldId][$priceFieldId]['tax_rate'] = $taxRates[$priceFieldValues['financial_type_id']];
$taxAmount = self::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']);
$options[$fieldId][$priceFieldId]['tax_amount'] = round($taxAmount['tax_amount'],2);
}
}
}

return $options[$fieldId];
Expand Down Expand Up @@ -679,5 +748,24 @@ public static function priceSetValidation($priceSetId, $fields, &$error, $allowN
}
}
}

/**
* Calculate the tax amount based on given tax rate.
*
* @param float $amount amount of field.
* @param float $taxRate tax rate of selected financial account for field.
*
* @return array array of tax amount
*
* @access public
* @static
*
*/
public static function calculateTaxAmount($amount, $taxRate) {
$taxAmount = array();
$taxAmount['tax_amount'] = ($taxRate/100) * $amount;

return $taxAmount;
}
}

39 changes: 38 additions & 1 deletion CRM/Price/BAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ static function initSet(&$form, $id, $entityTable = 'civicrm_event', $validOnly

static function processAmount(&$fields, &$params, &$lineItem, $component = '') {
// using price set
$totalPrice = 0;
$totalPrice = $totalTax = 0;
$radioLevel = $checkboxLevel = $selectLevel = $textLevel = array();
if ($component) {
$autoRenew = array();
Expand All @@ -671,6 +671,10 @@ static function processAmount(&$fields, &$params, &$lineItem, $component = '') {
case 'Text':
$params["price_{$id}"] = array(key($field['options']) => $params["price_{$id}"]);
CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
if (CRM_Utils_Array::value('tax_rate', $field['options'][key($field['options'])])) {
$lineItem = self::setLineItem($field, $lineItem, key($field['options']));
$totalTax += $field['options'][key($field['options'])]['tax_amount'] * $lineItem[key($field['options'])]['qty'];
}
$totalPrice += $lineItem[key($field['options'])]['line_total'];
break;

Expand All @@ -693,6 +697,10 @@ static function processAmount(&$fields, &$params, &$lineItem, $component = '') {
$radioLevel = array_keys($params['amount_priceset_level_radio']);
}
CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) {
$lineItem = self::setLineItem($field, $lineItem, $optionValueId);
$totalTax += $field['options'][$optionValueId]['tax_amount'];
}
$totalPrice += $lineItem[$optionValueId]['line_total'];
if (
$component &&
Expand All @@ -718,6 +726,10 @@ static function processAmount(&$fields, &$params, &$lineItem, $component = '') {
$selectLevel = array_keys($params['amount_priceset_level_select']);
}
CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionValueId])) {
$lineItem = self::setLineItem($field, $lineItem, $optionValueId);
$totalTax += $field['options'][$optionValueId]['tax_amount'];
}
$totalPrice += $lineItem[$optionValueId]['line_total'];
if (
$component &&
Expand Down Expand Up @@ -747,6 +759,10 @@ static function processAmount(&$fields, &$params, &$lineItem, $component = '') {
}
CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem);
foreach ($optionIds as $optionId) {
if (CRM_Utils_Array::value('tax_rate', $field['options'][$optionId])) {
$lineItem = self::setLineItem($field, $lineItem, $optionId);
$totalTax += $field['options'][$optionId]['tax_amount'];
}
$totalPrice += $lineItem[$optionId]['line_total'];
if (
$component &&
Expand Down Expand Up @@ -779,6 +795,7 @@ static function processAmount(&$fields, &$params, &$lineItem, $component = '') {
}
$params['amount_level'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amount_level) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR;
$params['amount'] = $totalPrice;
$params['tax_amount'] = $totalTax;
if ($component) {
foreach ($autoRenew as $dontCare => $eachAmount) {
if (!$eachAmount) {
Expand Down Expand Up @@ -1257,5 +1274,25 @@ static function copyPriceSet($baoName, $id, $newId) {
}
}
}

/*
* Function to set tax_amount and tax_rate in LineItem
*
*
*/
static function setLineItem($field, $lineItem, $optionValueId) {
if ($field['html_type'] == 'Text') {
$taxAmount = $field['options'][$optionValueId]['tax_amount'] * $lineItem[$optionValueId]['qty'];
}
else {
$taxAmount = $field['options'][$optionValueId]['tax_amount'];
}
$taxRate = $field['options'][$optionValueId]['tax_rate'];
$lineItem[$optionValueId]['line_total'] = $lineItem[$optionValueId]['line_total'] + $taxAmount;
$lineItem[$optionValueId]['tax_amount'] = $taxAmount;
$lineItem[$optionValueId]['tax_rate'] = $taxRate;

return $lineItem;
}
}

17 changes: 16 additions & 1 deletion templates/CRM/Price/Form/PriceSet.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,22 @@
<div class="content {$element.name}-content">{$form.$element_name.html}
{if $element.is_display_amounts && $element.html_type eq 'Text'}
<span class="price-field-amount">
{foreach item=option from=$element.options}{$option.amount|crmMoney}{/foreach}
{foreach item=option from=$element.options}
{if $option.tax_amount && $displayOpt}
{assign var="amount" value=`$option.amount+$option.tax_amount`}
{if $displayOpt == 'Do_not_show'}
{$amount|crmMoney}
{elseif $displayOpt == 'Inclusive'}
{$amount|crmMoney}
<span class='crm-price-amount-label'> (includes {$option.tax_rate|round:2}% VAT - {$option.tax_amount|crmMoney})</span>
{else}
{$option.amount|crmMoney}
<span class='crm-price-amount-label'> + {$option.tax_rate|round:2}% VAT - {$option.tax_amount|crmMoney}</span>
{/if}
{else}
{$option.amount|crmMoney}
{/if}
{/foreach}
</span>
{/if}
{if $element.help_post}<br /><span class="description">{$element.help_post}</span>{/if}
Expand Down

0 comments on commit dae066a

Please sign in to comment.