Skip to content

Commit

Permalink
Merge pull request civicrm#12 from dpradeep/VAT-365-New
Browse files Browse the repository at this point in the history
VAT-365 Tax label changes and code optimization.
  • Loading branch information
pradpnayak committed Jun 20, 2014
2 parents bd9a7fa + 57e3be5 commit 3803f46
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 45 deletions.
19 changes: 19 additions & 0 deletions CRM/Contribute/BAO/Contribution/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,5 +852,24 @@ static function getFirstLastDetails($contactID) {
}
return $_cache[$contactID];
}

/**
* 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;
}
}

85 changes: 40 additions & 45 deletions CRM/Price/BAO/PriceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,15 @@ public static function addQuickFormElement(&$qf,
//use value field.
$valueFieldName = 'amount';
$seperator = '|';
$displayOpt = CRM_Contribute_BAO_Contribution::getContributionSettings();
$displayOpt = $displayOpt['tax_display_settings'];
$displayOpt = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
$displayOpt = CRM_Utils_Array::value('tax_display_settings', $displayOpt);
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], '');
$taxAmount = 0;
if (CRM_Utils_Array::value('tax_amount', $customOption[$optionKey])) {
$taxAmount = $customOption[$optionKey]['tax_amount'];
$taxAmount = CRM_Utils_Array::value('tax_amount', $customOption[$optionKey]);
if ($taxAmount && $displayOpt) {
$qf->assign('displayOpt', $displayOpt);
}
$priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName] + $taxAmount, $count, $max_value));
Expand Down Expand Up @@ -353,11 +352,10 @@ public static function addQuickFormElement(&$qf,
}

foreach ($customOption as $opId => $opt) {
$taxAmount = 0;
$taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
if ($field->is_display_amounts) {
$opt['label'] = !empty($opt['label']) ? $opt['label'] : '';
if (CRM_Utils_Array::value('tax_amount', $opt)) {
$taxAmount = $opt['tax_amount'];
if ($taxAmount) {
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>';
}
Expand All @@ -372,6 +370,9 @@ public static function addQuickFormElement(&$qf,
}
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>';
if (isset($opt['tax_amount']) && $displayOpt != 'Do_not_show') {
$opt['label'] .= '<span class="crm-price-amount-label">'. ts(' VAT (exempt)') .'</span>';
}
}
}
$count = CRM_Utils_Array::value('count', $opt, '');
Expand Down Expand Up @@ -445,28 +446,20 @@ public static function addQuickFormElement(&$qf,
$selectOption = $allowedOptions = $priceVal = array();

foreach ($customOption as $opt) {
$taxAmount = 0;
$taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
$count = CRM_Utils_Array::value('count', $opt, '');
$max_value = CRM_Utils_Array::value('max_value', $opt, '');

if ($field->is_display_amounts) {
$opt['label'] .= '&nbsp;-&nbsp;';
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>';
}
if ($taxAmount) {
$opt['label'] .= self::getTaxLabel($opt, $valueFieldName, $displayOpt);
}
else {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
if (isset($opt['tax_amount']) && $displayOpt != 'Do_not_show') {
$opt['label'] .= '<span class="crm-price-amount-label">'. ts(' VAT (exempt)') .'</span>';
}
}
}
$selectOption[$opt['id']] = $opt['label'];
Expand Down Expand Up @@ -497,28 +490,20 @@ public static function addQuickFormElement(&$qf,

$check = array();
foreach ($customOption as $opId => $opt) {
$taxAmount = 0;
$taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
$count = CRM_Utils_Array::value('count', $opt, '');
$max_value = CRM_Utils_Array::value('max_value', $opt, '');

if ($field->is_display_amounts) {
$opt['label'] .= '&nbsp;-&nbsp;';
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>';
}
if ($taxAmount) {
$opt['label'] .= self::getTaxLabel($opt, $valueFieldName, $displayOpt);
}
else {
$opt['label'] .= CRM_Utils_Money::format($opt[$valueFieldName]);
if (isset($opt['tax_amount']) && $displayOpt != 'Do_not_show') {
$opt['label'] .= '<span class="crm-price-amount-label">'. ts(' VAT (exempt)') .'</span>';
}
}
}
$priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
Expand Down Expand Up @@ -571,7 +556,7 @@ public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FA
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']);
$taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']);
$options[$fieldId][$priceFieldId]['tax_amount'] = round($taxAmount['tax_amount'],2);
}
}
Expand Down Expand Up @@ -750,22 +735,32 @@ public static function priceSetValidation($priceSetId, $fields, &$error, $allowN
}

/**
* Calculate the tax amount based on given tax rate.
* Generate the label for price fields based on tax display setting option on CiviContribute Component Settings page.
*
* @param float $amount amount of field.
* @param float $taxRate tax rate of selected financial account for field.
* @param array $opt
* @param string $valueFieldName amount
* @param string $displayOpt tax display setting option
*
* @return array array of tax amount
* @return string $label tax label for custom field
*
* @access public
* @static
*
*/
public static function calculateTaxAmount($amount, $taxRate) {
$taxAmount = array();
$taxAmount['tax_amount'] = ($taxRate/100) * $amount;
public static function getTaxLabel($opt, $valueFieldName, $displayOpt) {
if ($displayOpt == 'Do_not_show') {
$label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
}
else if ($displayOpt == 'Inclusive') {
$label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
$label .= '<span class="crm-price-amount-label"> (includes '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>';
}
else {
$label = CRM_Utils_Money::format($opt[$valueFieldName]);
$label .= '<span class="crm-price-amount-label"> + '.round($opt['tax_rate'],2).'% VAT - ' . CRM_Utils_Money::format($opt['tax_amount']) . '</span>';
}

return $taxAmount;
return $label;
}
}

3 changes: 3 additions & 0 deletions templates/CRM/Price/Form/PriceSet.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
{/if}
{else}
{$option.amount|crmMoney}
{if $option.tax_amount == "0" && $displayOpt != 'Do_not_show'}
<span class='crm-price-amount-label'>{ts} VAT (exempt){/ts}</span>
{/if}
{/if}
{/foreach}
</span>
Expand Down

0 comments on commit 3803f46

Please sign in to comment.