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..872b3553756f 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -98,6 +98,13 @@ public function getDefaultEntity() { */ protected $_params = array(); + /** + * Array of information about the membership. + * + * @var array + */ + protected $membershipInfoArray = []; + public function preProcess() { // Check for edit permission. if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { @@ -465,4 +472,40 @@ public function testSubmit($formValues) { $this->submit(); } + + /** + * Build membership info array, which is used when membership type is selected. + * + * Data is used to: + * - set the payment information block + * - set the max related block + * + * @param array $values + * @param string $totalAmount + * @param string $taxAmount + * (only used from membership renewal) + * + * @return array + */ + protected function getMembershipInfoArray($values, $totalAmount, $taxAmount = NULL) { + // @todo - we really should assign the tax rate for the financial_type_id to the template. + + $membershipInfo = [ + 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values), + 'total_amount' => CRM_Utils_Money::format($totalAmount, NULL, '%a'), + 'total_amount_numeric' => $totalAmount, + 'auto_renew' => CRM_Utils_Array::value('auto_renew', $values), + 'has_related' => isset($values['relationship_type_id']), + 'max_related' => CRM_Utils_Array::value('max_related', $values), + 'tax_rate' => $this->getTaxRateForFinancialType($values['financial_type_id']), + // @todo tax message is only used for renewal and is specifically referred to in Renewal.tpl only + // @todo should this really be assigned? Or js calculated? + 'tax_message' => $taxAmount ? ts("Includes %1 amount of %2", [ + 1 => $this->getSalesTaxTerm(), + 2 => CRM_Utils_Money::format($taxAmount) + ]) : $taxAmount, + ]; + return $membershipInfo; + } + } diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index b81bc5ec3bb6..1dcf06987f14 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -396,14 +396,9 @@ 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)); - } + $this->assignSalesTaxMetadataToTemplate(); + // build price set form. $buildPriceSet = FALSE; if ($this->_priceSetId || !empty($_POST['price_set_id'])) { @@ -493,7 +488,7 @@ public function buildQuickForm() { CRM_Core_Error::statusBounce(ts('You do not have all the permissions needed for this page.')); } // retrieve all memberships - $allMembershipInfo = array(); + foreach ($this->allMembershipTypeDetails as $key => $values) { if ($this->_mode && empty($values['minimum_fee'])) { continue; @@ -518,20 +513,11 @@ public function buildQuickForm() { if (!empty($this->_submitValues['total_amount'])) { $totalAmount = $this->_submitValues['total_amount']; } - // build membership info array, which is used when membership type is selected to: - // - set the payment information block - // - set the max related block - $allMembershipInfo[$key] = array( - 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values), - 'total_amount' => CRM_Utils_Money::format($totalAmount, NULL, '%a'), - 'total_amount_numeric' => $totalAmount, - 'auto_renew' => CRM_Utils_Array::value('auto_renew', $values), - 'has_related' => isset($values['relationship_type_id']), - 'max_related' => CRM_Utils_Array::value('max_related', $values), - ); + $this->membershipInfoArray[$key] = $this->getMembershipInfoArray($values, $totalAmount); + } - $this->assign('allMembershipInfo', json_encode($allMembershipInfo)); + $this->assign('allMembershipInfo', json_encode($this->membershipInfoArray)); // show organization by default, if only one organization in // the list @@ -1685,7 +1671,7 @@ public function submit() { } if ($taxAmount) { $this->assign('totalTaxAmount', $totalTaxAmount); - $this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings)); + $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..059c72b33bf4 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -236,22 +236,11 @@ public function buildQuickForm() { $this->assign('entityID', $this->_id); $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -'); - $allMembershipInfo = array(); - // CRM-21485 if (is_array($defaults['membership_type_id'])) { $defaults['membership_type_id'] = $defaults['membership_type_id'][1]; } - //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'); - - // auto renew options if enabled for the membership - $options = CRM_Core_SelectValues::memberAutoRenew(); - foreach ($this->allMembershipTypeDetails as $key => $values) { if (!empty($values['is_active'])) { if ($this->_mode && empty($values['minimum_fee'])) { @@ -273,30 +262,11 @@ 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)) { - $taxAmount = ($taxRate / 100) * CRM_Utils_Array::value('minimum_fee', $values); - $totalAmount = $totalAmount + $taxAmount; - } - - // build membership info array, which is used to set the payment information block when - // membership type is selected. - $allMembershipInfo[$key] = array( - '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, - ); - - if (!empty($values['auto_renew'])) { - $allMembershipInfo[$key]['auto_renew'] = $options[$values['auto_renew']]; - } + $this->membershipInfoArray[$key] = $this->getMembershipInfoArray($values, $totalAmount); } } - $this->assign('allMembershipInfo', json_encode($allMembershipInfo)); + $this->assign('allMembershipInfo', json_encode($this->membershipInfoArray)); if ($this->_memType) { $this->assign('orgName', $selMemTypeOrg[$this->allMembershipTypeDetails[$this->_memType]['member_of_contact_id']]); @@ -718,4 +688,34 @@ protected function submit() { } } + /** + * Build membership info array, which is used when membership type is selected. + * + * Data is used to: + * - set the payment information block + * - set the max related block + * + * @param array $values + * @param string $totalAmount + * @param string $taxAmount + * (only used from membership renewal) + * + * @return array + */ + protected function getMembershipInfoArray($values, $totalAmount, $taxAmount = NULL) { + $taxAmount = NULL; + $taxRate = $this->getTaxRateForFinancialType($values['financial_type_id']); + if ($taxRate) { + $taxAmount = ($taxRate / 100) * CRM_Utils_Array::value('minimum_fee', $values); + $totalAmount = $totalAmount + $taxAmount; + } + $membershipInfo = parent::getMembershipInfoArray($values, $totalAmount, $taxAmount); + // @todo Figure out why this auto_renew value is different for this form than for the Membership form + $options = CRM_Core_SelectValues::memberAutoRenew(); + if (!empty($values['auto_renew'])) { + $membershipInfo['auto_renew'] = $options[$values['auto_renew']]; + } + return $membershipInfo; + } + }