Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ waiting on #12594 ] Membership code cleanup #12593

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}
43 changes: 43 additions & 0 deletions CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opps this todo can go ... I did & later I found using it improved the tpl - but thinking to hold this & rebase when the trait pr is merged


$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;
}

}
28 changes: 7 additions & 21 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
64 changes: 32 additions & 32 deletions CRM/Member/Form/MembershipRenewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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']]);
Expand Down Expand Up @@ -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;
}

}