From a6e29c959b30e5fe5d2aa62b43ffe52381992ebd Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 30 Jul 2018 12:47:29 +1200 Subject: [PATCH] Minor refactor, use sales tax trait to simplify sales tax functions --- CRM/Contribute/Form/AbstractEditPayment.php | 3 + CRM/Event/Form/Participant.php | 2 +- CRM/Financial/Form/SalesTaxTrait.php | 90 +++++++++++++++++++++ CRM/Member/Form.php | 1 + CRM/Member/Form/Membership.php | 11 +-- CRM/Member/Form/MembershipRenewal.php | 10 +-- 6 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 CRM/Financial/Form/SalesTaxTrait.php diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 62295f1c70ca..9e59495c6192 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -46,6 +46,9 @@ * */ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { + + use CRM_Financial_Form_SalesTaxTrait; + public $_mode; public $_action; diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 5897aff52bb9..ec7863ccac2a 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -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)) { diff --git a/CRM/Financial/Form/SalesTaxTrait.php b/CRM/Financial/Form/SalesTaxTrait.php new file mode 100644 index 000000000000..072126a2f45d --- /dev/null +++ b/CRM/Financial/Form/SalesTaxTrait.php @@ -0,0 +1,90 @@ +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()); + } + +} diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 4c41ab201f61..2d2a75726413 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -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()); diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index b81bc5ec3bb6..62afa07d483b 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -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'])) { @@ -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); } diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 25c90c496990..d5eb08968a68 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -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(); @@ -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; } @@ -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'])) {