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

CRM-17647 fix ContributionForm to use skipCleanMoney on update & upda… #11575

Merged
Merged
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
18 changes: 7 additions & 11 deletions CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1550,10 +1550,12 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {

// get the required field value only.

$params = $ids = array();

$params['contact_id'] = $this->_contactID;
$params['currency'] = $this->getCurrency($submittedValues);
$params = [
'contact_id' => $this->_contactID,
'currency' => $this->getCurrency($submittedValues),
'skipCleanMoney' => TRUE,
'id' => $this->_id,
];

//format soft-credit/pcp param first
CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($submittedValues, $this);
Expand All @@ -1573,10 +1575,6 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
$params[$f] = CRM_Utils_Array::value($f, $formValues);
}

// CRM-5740 if priceset is used, no need to cleanup money.
if ($priceSetId) {
$params['skipCleanMoney'] = 1;
}
$params['revenue_recognition_date'] = NULL;
if (!empty($formValues['revenue_recognition_date'])
&& count(array_filter($formValues['revenue_recognition_date'])) == 2
Expand Down Expand Up @@ -1609,8 +1607,6 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
$params['is_pay_later'] = 0;
}

$ids['contribution'] = $params['id'] = $this->_id;

// Add Additional common information to formatted params.
CRM_Contribute_Form_AdditionalInfo::postProcessCommon($formValues, $params, $this);
if ($pId) {
Expand All @@ -1636,7 +1632,7 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
if (!empty($params['note']) && !empty($submittedValues['note'])) {
unset($params['note']);
}
$contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
$contribution = CRM_Contribute_BAO_Contribution::create($params);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$ids is deprecated & we have $params['id'] set


// process associated membership / participant, CRM-4395
if ($contribution->id && $action & CRM_Core_Action::UPDATE) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ public static function processContribution(

$contribParams['skipLineItem'] = 1;
// create contribution record
$contribution = CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

$ids is empty & deprecated

$contribution = CRM_Contribute_BAO_Contribution::add($contribParams);
// CRM-11124
CRM_Event_BAO_Participant::createDiscountTrxn($form->_eventId, $contribParams, NULL, CRM_Price_BAO_PriceSet::parseFirstPriceSetValueIDFromParams($params));

Expand Down
127 changes: 76 additions & 51 deletions tests/phpunit/CRM/Contribute/Form/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,12 +863,17 @@ public function testEnterNegativeContribution() {

/**
* Test the submit function on the contribution page.
*
* @param string $thousandSeparator
*
* @dataProvider getThousandSeparators
*/
public function testSubmitUpdate() {
public function testSubmitUpdate($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
$form = new CRM_Contribute_Form_Contribution();

$form->testSubmit(array(
'total_amount' => 50,
'total_amount' => $this->formatMoneyInput(6100.10),
'financial_type_id' => 1,
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
Expand All @@ -878,8 +883,8 @@ public function testSubmitUpdate() {
CRM_Core_Action::ADD);
$contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
$form->testSubmit(array(
'total_amount' => 45,
'net_amount' => 45,
'total_amount' => $this->formatMoneyInput(5200.20),
'net_amount' => $this->formatMoneyInput(5200.20),
'financial_type_id' => 1,
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
Expand All @@ -889,25 +894,30 @@ public function testSubmitUpdate() {
),
CRM_Core_Action::UPDATE);
$contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
$this->assertEquals(45, (int) $contribution['total_amount']);
$this->assertEquals(5200.20, $contribution['total_amount'], 2);

$financialTransactions = $this->callAPISuccess('FinancialTrxn', 'get', array('sequential' => TRUE));
$this->assertEquals(2, $financialTransactions['count']);
$this->assertEquals(50, $financialTransactions['values'][0]['total_amount']);
$this->assertEquals(-5, $financialTransactions['values'][1]['total_amount']);
$this->assertEquals(-5, $financialTransactions['values'][1]['net_amount']);
$this->assertEquals(6100.10, $financialTransactions['values'][0]['total_amount']);
$this->assertEquals(-899.90, $financialTransactions['values'][1]['total_amount']);
$this->assertEquals(-899.90, $financialTransactions['values'][1]['net_amount']);
$lineItem = $this->callAPISuccessGetSingle('LineItem', array());
$this->assertEquals(45, $lineItem['line_total']);
$this->assertEquals(5200.20, $lineItem['line_total']);
}

/**
* Test the submit function if only payment instrument is changed from 'Check' to 'Credit Card'
*
* @param string $thousandSeparator
*
* @dataProvider getThousandSeparators
*/
public function testSubmitUpdateChangePaymentInstrument() {
public function testSubmitUpdateChangePaymentInstrument($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
$form = new CRM_Contribute_Form_Contribution();

$form->testSubmit(array(
'total_amount' => 50,
'total_amount' => 1200.55,
'financial_type_id' => 1,
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
Expand All @@ -918,8 +928,8 @@ public function testSubmitUpdateChangePaymentInstrument() {
CRM_Core_Action::ADD);
$contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
$form->testSubmit(array(
'total_amount' => 50,
'net_amount' => 50,
'total_amount' => 1200.55,
'net_amount' => 1200.55,
'financial_type_id' => 1,
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
Expand All @@ -931,22 +941,22 @@ public function testSubmitUpdateChangePaymentInstrument() {
),
CRM_Core_Action::UPDATE);
$contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
$this->assertEquals(50, (int) $contribution['total_amount']);
$this->assertEquals(1200.55, $contribution['total_amount']);

$financialTransactions = $this->callAPISuccess('FinancialTrxn', 'get', array('sequential' => TRUE));
$this->assertEquals(3, $financialTransactions['count']);

list($oldTrxn, $reversedTrxn, $latestTrxn) = $financialTransactions['values'];

$this->assertEquals(50, $oldTrxn['total_amount']);
$this->assertEquals(1200.55, $oldTrxn['total_amount']);
$this->assertEquals('123AX', $oldTrxn['check_number']);
$this->assertEquals(array_search('Check', $this->paymentInstruments), $oldTrxn['payment_instrument_id']);

$this->assertEquals(-50, $reversedTrxn['total_amount']);
$this->assertEquals(-1200.55, $reversedTrxn['total_amount']);
$this->assertEquals('123AX', $reversedTrxn['check_number']);
$this->assertEquals(array_search('Check', $this->paymentInstruments), $reversedTrxn['payment_instrument_id']);

$this->assertEquals(50, $latestTrxn['total_amount']);
$this->assertEquals(1200.55, $latestTrxn['total_amount']);
$this->assertEquals('1011', $latestTrxn['pan_truncation']);
$this->assertEquals(array_search('Credit Card', $this->paymentInstruments), $latestTrxn['payment_instrument_id']);
$lineItem = $this->callAPISuccessGetSingle('LineItem', array());
Expand Down Expand Up @@ -1015,14 +1025,19 @@ public function testPartialPaymentWithCreditCard() {

/**
* Test the submit function for FT with tax.
*
* @param string $thousandSeparator
*
* @dataProvider getThousandSeparators
*/
public function testSubmitSaleTax() {
public function testSubmitSaleTax($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
$this->enableTaxAndInvoicing();
$this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
$form = new CRM_Contribute_Form_Contribution();

$form->testSubmit(array(
'total_amount' => 100,
'total_amount' => $this->formatMoneyInput(1000.00),
'financial_type_id' => $this->_financialTypeId,
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
Expand All @@ -1037,13 +1052,13 @@ public function testSubmitSaleTax() {
'return' => array('tax_amount', 'total_amount'),
)
);
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(10, $contribution['tax_amount']);
$this->assertEquals(1100, $contribution['total_amount']);
$this->assertEquals(100, $contribution['tax_amount']);
$this->callAPISuccessGetCount('FinancialTrxn', array(), 1);
$this->callAPISuccessGetCount('FinancialItem', array(), 2);
$lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
$this->assertEquals(100, $lineItem['line_total']);
$this->assertEquals(10, $lineItem['tax_amount']);
$this->assertEquals(1000, $lineItem['line_total']);
$this->assertEquals(100, $lineItem['tax_amount']);

// CRM-20423: Upon simple submit of 'Edit Contribution' form ensure that total amount is same
$form->testSubmit(array(
Expand All @@ -1058,7 +1073,7 @@ public function testSubmitSaleTax() {

$contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
// Check if total amount is unchanged
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(1100, $contribution['total_amount']);
}

/**
Expand Down Expand Up @@ -1103,15 +1118,20 @@ public function testSubmitWithOutSaleTax() {
/**
* Create a contribution & then edit it via backoffice form, checking tax with: default price_set
*
* @param string $thousandSeparator
*
* @dataProvider getThousandSeparators
*
* @throws \Exception
*/
public function testReSubmitSaleTax() {
public function testReSubmitSaleTax($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
$this->enableTaxAndInvoicing();
$this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
list($form, $contribution) = $this->doInitialSubmit();
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(10, $contribution['tax_amount']);
$this->assertEquals(110, $contribution['net_amount']);
$this->assertEquals(11000, $contribution['total_amount']);
$this->assertEquals(1000, $contribution['tax_amount']);
$this->assertEquals(11000, $contribution['net_amount']);

$mut = new CiviMailUtils($this, TRUE);
// Testing here if when we edit something trivial like adding a check_number tax, net, total amount stay the same:
Expand All @@ -1135,13 +1155,13 @@ public function testReSubmitSaleTax() {
'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
)
);
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(10, $contribution['tax_amount']);
$this->assertEquals(110, $contribution['net_amount']);
$this->assertEquals(11000, $contribution['total_amount']);
$this->assertEquals(1000, $contribution['tax_amount']);
$this->assertEquals(11000, $contribution['net_amount']);

$strings = array(
'Total Tax Amount : $ 10.00',
'Total Amount : $ 110.00',
'Total Tax Amount : $ ' . $this->formatMoneyInput(1000.00),
'Total Amount : $ ' . $this->formatMoneyInput(11000.00),
'Date Received: April 21st, 2015',
'Paid By: Check',
'Check Number: 12345',
Expand All @@ -1154,16 +1174,21 @@ public function testReSubmitSaleTax() {
$this->assertEquals('Contribution Amount', $items['values'][0]['description']);
$this->assertEquals('Sales Tax', $items['values'][1]['description']);

$this->assertEquals(100, $items['values'][0]['amount']);
$this->assertEquals(10, $items['values'][1]['amount']);
$this->assertEquals(10000, $items['values'][0]['amount']);
$this->assertEquals(1000, $items['values'][1]['amount']);
}

/**
* Create a contribution & then edit it via backoffice form, checking tax with: default price_set
*
* @param string $thousandSeparator
*
* @dataProvider getThousandSeparators
*
* @throws \Exception
*/
public function testReSubmitSaleTaxAlteredAmount() {
public function testReSubmitSaleTaxAlteredAmount($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
$this->enableTaxAndInvoicing();
$this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
list($form, $contribution) = $this->doInitialSubmit();
Expand All @@ -1172,8 +1197,8 @@ public function testReSubmitSaleTaxAlteredAmount() {
// Testing here if when we edit something trivial like adding a check_number tax, net, total amount stay the same:
$form->testSubmit(array(
'id' => $contribution['id'],
'total_amount' => 200,
'tax_amount' => 20,
'total_amount' => $this->formatMoneyInput(20000),
'tax_amount' => $this->formatMoneyInput(2000),
'financial_type_id' => $contribution['financial_type_id'],
'receive_date' => $contribution['receive_date'],
'payment_instrument_id' => $contribution['payment_instrument_id'],
Expand All @@ -1191,13 +1216,13 @@ public function testReSubmitSaleTaxAlteredAmount() {
'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
)
);
$this->assertEquals(220, $contribution['total_amount']);
$this->assertEquals(20, $contribution['tax_amount']);
$this->assertEquals(220, $contribution['net_amount']);
$this->assertEquals(22000, $contribution['total_amount']);
$this->assertEquals(2000, $contribution['tax_amount']);
$this->assertEquals(22000, $contribution['net_amount']);

$strings = array(
'Total Tax Amount : $ 20.00',
'Total Amount : $ 220.00',
'Total Tax Amount : $ ' . $this->formatMoneyInput(2000),
'Total Amount : $ ' . $this->formatMoneyInput(22000.00),
'Date Received: April 21st, 2015',
'Paid By: Check',
'Check Number: 12345',
Expand All @@ -1212,10 +1237,10 @@ public function testReSubmitSaleTaxAlteredAmount() {
$this->assertEquals('Contribution Amount', $items['values'][0]['description']);
$this->assertEquals('Sales Tax', $items['values'][1]['description']);

$this->assertEquals(100, $items['values'][0]['amount']);
$this->assertEquals(10, $items['values'][1]['amount']);
$this->assertEquals(100, $items['values'][2]['amount']);
$this->assertEquals(10, $items['values'][3]['amount']);
$this->assertEquals(10000, $items['values'][0]['amount']);
$this->assertEquals(1000, $items['values'][1]['amount']);
$this->assertEquals(10000, $items['values'][2]['amount']);
$this->assertEquals(1000, $items['values'][3]['amount']);
}

/**
Expand All @@ -1229,7 +1254,7 @@ protected function doInitialSubmit() {
$form = new CRM_Contribute_Form_Contribution();

$form->testSubmit(array(
'total_amount' => 100,
'total_amount' => $this->formatMoneyInput(10000),
'financial_type_id' => $this->_financialTypeId,
'receive_date' => '2015-04-21 00:00:00',
'contact_id' => $this->_individualId,
Expand All @@ -1252,9 +1277,9 @@ protected function doInitialSubmit() {
),
)
);
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(10, $contribution['tax_amount']);
$this->assertEquals(110, $contribution['net_amount']);
$this->assertEquals(11000, $contribution['total_amount']);
$this->assertEquals(1000, $contribution['tax_amount']);
$this->assertEquals(11000, $contribution['net_amount']);
return array($form, $contribution);
}

Expand Down