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

dev/core#2425 - php 7.4 - E_NOTICE every time you save a contribution #19978

Merged
merged 1 commit into from
Apr 7, 2021
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
8 changes: 7 additions & 1 deletion CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,14 @@ public static function formRule($fields, $files, $self) {
}
}
// CRM-16189
$order = new CRM_Financial_BAO_Order();
$order->setPriceSelectionFromUnfilteredInput($fields);
if (isset($fields['total_amount'])) {
$order->setOverrideTotalAmount($fields['total_amount']);
}
$lineItems = $order->getLineItems();
try {
CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($fields, $self->_id, $self->_priceSet['fields']);
CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($fields, $self->_id, $lineItems);
}
catch (CRM_Core_Exception $e) {
$errors['financial_type_id'] = ' ';
Expand Down
17 changes: 4 additions & 13 deletions CRM/Financial/BAO/FinancialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,13 @@ public static function validateFinancialAccount($financialAccountId, $financialA
* @param int $contributionID
* Contribution ID
*
* @param array $priceSetFields
* Array of price fields of a price set.
* @param array $orderLineItems
* The line items from the Order.
*
* @return bool
*
*/
public static function checkFinancialTypeHasDeferred($params, $contributionID = NULL, $priceSetFields = NULL) {
public static function checkFinancialTypeHasDeferred($params, $contributionID = NULL, $orderLineItems = []) {
if (!Civi::settings()->get('deferred_revenue_enabled')) {
return FALSE;
}
Expand All @@ -399,16 +399,7 @@ public static function checkFinancialTypeHasDeferred($params, $contributionID =
$financialTypeID = $params['prevContribution']->financial_type_id;
}
if (($contributionID || !empty($params['price_set_id'])) && empty($lineItems)) {
if (!$contributionID) {
CRM_Price_BAO_PriceSet::processAmount($priceSetFields,
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel so happy seeing the demise of this line

$params, $items);
}
else {
$items = CRM_Price_BAO_LineItem::getLineItems($contributionID, 'contribution', TRUE, TRUE, TRUE);
}
if (!empty($items)) {
$lineItems[] = $items;
}
$lineItems[] = $orderLineItems;
}
$deferredFinancialType = self::getDeferredFinancialType();
$isError = FALSE;
Expand Down
127 changes: 127 additions & 0 deletions tests/phpunit/CRM/Contribute/Form/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2046,4 +2046,131 @@ public function additionalInfoProvider(): array {
];
}

/**
* Test formRule
*/
public function testContributionFormRule() {
$fields = [
'contact_id' => $this->_individualId,
'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
'currency' => 'USD',
'total_amount' => '10',
'price_set_id' => '',
'source' => '',
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
'cancel_date' => '',
'cancel_reason' => '',
'receive_date' => date('Y-m-d H:i:s'),
'from_email_address' => key(CRM_Core_BAO_Email::getFromEmail()),
'receipt_date' => '',
'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
'trxn_id' => '',
'check_number' => '',
'soft_credit_contact_id' => [
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => '',
8 => '',
9 => '',
10 => '',
],
'soft_credit_amount' => [
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => '',
8 => '',
9 => '',
10 => '',
],
'soft_credit_type' => [
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => '',
8 => '',
9 => '',
10 => '',
],
];

$form = new CRM_Contribute_Form_Contribution();
$this->assertSame([], $form->formRule($fields, [], $form));
}

/**
* Check that formRule validates you can only have one contribution with a
* given trxn_id.
*/
public function testContributionFormRuleDuplicateTrxn() {
$contribution = $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['trxn_id' => '1234']));

$fields = [
'contact_id' => $this->_individualId,
'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
'currency' => 'USD',
'total_amount' => '10',
'price_set_id' => '',
'source' => '',
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
'cancel_date' => '',
'cancel_reason' => '',
'receive_date' => date('Y-m-d H:i:s'),
'from_email_address' => key(CRM_Core_BAO_Email::getFromEmail()),
'receipt_date' => '',
'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
'trxn_id' => '1234',
'check_number' => '',
'soft_credit_contact_id' => [
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => '',
8 => '',
9 => '',
10 => '',
],
'soft_credit_amount' => [
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => '',
8 => '',
9 => '',
10 => '',
],
'soft_credit_type' => [
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => '',
8 => '',
9 => '',
10 => '',
],
];

$form = new CRM_Contribute_Form_Contribution();
$this->assertEquals(['trxn_id' => "Transaction ID's must be unique. Transaction '1234' already exists in your database."], $form->formRule($fields, [], $form));
}

}