Skip to content

Commit

Permalink
Merge pull request #12594 from eileenmcnaughton/sales_tax_trait
Browse files Browse the repository at this point in the history
Minor refactor, use sales tax trait to simplify sales tax functions
  • Loading branch information
eileenmcnaughton authored Aug 9, 2018
2 parents 90ec83a + a6e29c9 commit a26f426
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CRM/Contribute/Form/AbstractEditPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
*
*/
class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task {

use CRM_Financial_Form_SalesTaxTrait;

public $_mode;

public $_action;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ public function submit($params) {
}
}
$this->assign('totalTaxAmount', $totalTaxAmount);
$this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings));
$this->assign('taxTerm', $this->getSalesTaxTerm());
$this->assign('dataArray', $dataArray);
}
if (!empty($additionalParticipantDetails)) {
Expand Down
90 changes: 90 additions & 0 deletions CRM/Financial/Form/SalesTaxTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
*/

trait CRM_Financial_Form_SalesTaxTrait {

/**
* Assign the sales tax term to the template.
*/
public function assignSalesTaxTermToTemplate() {
$this->assign('taxTerm', $this->getSalesTaxTerm());
}

/**
* Assign sales tax rates to the template.
*/
public function assignSalesTaxRates() {
$this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates()));
}

/**
* Return the string to be assigned to the template for sales tax - e.g GST, VAT.
*
* @return string
*/
public function getSalesTaxTerm() {
$invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
$invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
if (!$invoicing) {
return '';
}
return CRM_Utils_Array::value('tax_term', $invoiceSettings);
}

/**
* Assign information to the template required for sales tax purposes.
*/
public function assignSalesTaxMetadataToTemplate() {
$this->assignSalesTaxRates();
$this->assignSalesTaxTermToTemplate();
}

/**
* Get sales tax rates.
*
* @return array
*/
public function getTaxRatesForFinancialTypes() {
return CRM_Core_PseudoConstant::getTaxRates();
}

/**
* @param int $financialTypeID
*
* @return string
*/
public function getTaxRateForFinancialType($financialTypeID) {
return CRM_Utils_Array::value($financialTypeID, $this->getTaxRatesForFinancialTypes());
}

}
1 change: 1 addition & 0 deletions CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function setDefaultValues() {
* Build the form object.
*/
public function buildQuickForm() {
$this->assignSalesTaxMetadataToTemplate();

$this->addPaymentProcessorSelect(TRUE, FALSE, TRUE);
CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, $this->getDefaultPaymentInstrumentId());
Expand Down
11 changes: 3 additions & 8 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,8 @@ public function setDefaultValues() {
*/
public function buildQuickForm() {

$this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates()));

$this->assign('currency', CRM_Core_Config::singleton()->defaultCurrencySymbol);
$invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
$invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
if (isset($invoicing)) {
$this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings));
}

// build price set form.
$buildPriceSet = FALSE;
if ($this->_priceSetId || !empty($_POST['price_set_id'])) {
Expand Down Expand Up @@ -1685,7 +1679,8 @@ public function submit() {
}
if ($taxAmount) {
$this->assign('totalTaxAmount', $totalTaxAmount);
$this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings));
// Not sure why would need this on Submit.... unless it's being used when sending mails in which case this is the wrong place
$this->assign('taxTerm', $this->getSalesTaxTerm());
}
$this->assign('dataArray', $dataArray);
}
Expand Down
10 changes: 4 additions & 6 deletions CRM/Member/Form/MembershipRenewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,7 @@ public function buildQuickForm() {
}

//CRM-16950
$taxRates = CRM_Core_PseudoConstant::getTaxRates();
$taxRate = CRM_Utils_Array::value($this->allMembershipTypeDetails[$defaults['membership_type_id']]['financial_type_id'], $taxRates);

$invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
$taxRate = $this->getTaxRateForFinancialType($this->allMembershipTypeDetails[$defaults['membership_type_id']]['financial_type_id']);

// auto renew options if enabled for the membership
$options = CRM_Core_SelectValues::memberAutoRenew();
Expand Down Expand Up @@ -276,7 +273,8 @@ public function buildQuickForm() {
//CRM-16950
$taxAmount = NULL;
$totalAmount = CRM_Utils_Array::value('minimum_fee', $values);
if (CRM_Utils_Array::value($values['financial_type_id'], $taxRates)) {
// @todo - feels a bug - we use taxRate from the form default rather than from the specified type?!?
if ($this->getTaxRateForFinancialType($values['financial_type_id'])) {
$taxAmount = ($taxRate / 100) * CRM_Utils_Array::value('minimum_fee', $values);
$totalAmount = $totalAmount + $taxAmount;
}
Expand All @@ -287,7 +285,7 @@ public function buildQuickForm() {
'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values),
'total_amount' => CRM_Utils_Money::format($totalAmount, NULL, '%a'),
'total_amount_numeric' => $totalAmount,
'tax_message' => $taxAmount ? ts("Includes %1 amount of %2", array(1 => CRM_Utils_Array::value('tax_term', $invoiceSettings), 2 => CRM_Utils_Money::format($taxAmount))) : $taxAmount,
'tax_message' => $taxAmount ? ts("Includes %1 amount of %2", array(1 => $this->getSalesTaxTerm(), 2 => CRM_Utils_Money::format($taxAmount))) : $taxAmount,
);

if (!empty($values['auto_renew'])) {
Expand Down

0 comments on commit a26f426

Please sign in to comment.