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

PROD-147: Pro-rate creditnote by quantity and allocated amount is also allocated to completed contribution line items #143

Merged
merged 2 commits into from
Feb 21, 2024
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
23 changes: 23 additions & 0 deletions CRM/Financeextras/BAO/CreditNoteAllocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Civi\Financeextras\Utils\OptionValueUtils;
use Civi\Financeextras\Utils\FinancialAccountUtils;
use Civi\Financeextras\Event\ContributionPaymentUpdatedEvent;
use Civi\Financeextras\Utils\ContributionUtils;

class CRM_Financeextras_BAO_CreditNoteAllocation extends CRM_Financeextras_DAO_CreditNoteAllocation {

Expand Down Expand Up @@ -184,6 +185,10 @@ private static function createPayment($account, $paymentParams) {
'payment_processor_id' => NULL,
'payment_instrument_id' => OptionValueUtils::getValueForOptionValue('payment_instrument', 'credit_note'),
], $paymentParams);

if (self::isContributionStatus($params['contribution_id'], 'Completed')) {
$params['line_item'] = ContributionUtils::allocatePaymentToLineItem($params['total_amount'], $params['contribution_id']);
}
$transaction = \CRM_Financial_BAO_Payment::create($params);

// The Payment API typically uses the "Accounts Receivable" as the "from" account
Expand Down Expand Up @@ -275,4 +280,22 @@ private static function getCreditNoteAllocationById(int $id): array {
->first();
}

/**
* Checks if Contribution status has the specified status.
*
* @param int $contributionId
* The contribution ID
* @param string $status
* The status to check against the contribution
*
* @return bool
*/
private static function isContributionStatus($contributionId, $status) {
return !empty(\Civi\Api4\Contribution::get(FALSE)
->addWhere('id', '=', $contributionId)
->addWhere('contribution_status_id:name', '=', $status)
->execute()
->first());
}

}
45 changes: 45 additions & 0 deletions Civi/Financeextras/Utils/ContributionUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Civi\Financeextras\Utils;

class ContributionUtils {

/**
* Porportionally allocates the amount being paid to the contribution line items.
*
* - Gets the total amount for each line item.
* - Determines the portion of payment allocated to each line item by assessing the ratio
* of the total payment for the entire contribution.
* - This ratio is then applied to distribute the payment across all line items.
*
* @param float $amount
* Amount being paid
*
* @param int $contributionId
* The contribution being paid for
*
* @return array
*/
public static function allocatePaymentToLineItem($amount, $contributionId) {
$result = [];
$lineItems = \CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionId);

$totalAmount = \Civi\Api4\Contribution::get(FALSE)
->addSelect('total_amount')
->addWhere('id', '=', $contributionId)
->execute()
->first()['total_amount'] ?? 0;

if ($totalAmount <= 0) {
return $result;
}

$ratio = (float) $amount / (float) $totalAmount;
foreach ($lineItems as $id => $lineItem) {
$result[] = [$id => $lineItem['subTotal'] * $ratio];
}

return $result;
}

}
7 changes: 2 additions & 5 deletions ang/fe-creditnote/directives/creditnote-create.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,8 @@
const dueAmount = Math.max(contribution.total_amount - contribution.paid_amount, 0)
const duePercent = (100 * dueAmount) /contribution.total_amount
for (let i = 0; i < lineItems.length; i++) {
const qty = (duePercent == 100) ? lineItems[i].qty : 1
let unitPrice = (duePercent == 100) ?
lineItems[i].unit_price :
(duePercent/100) * (lineItems[i].unit_price * lineItems[i].qty);
unitPrice = Number(unitPrice.toFixed(2))
const qty = lineItems[i].qty * (duePercent/100)
let unitPrice = Number((lineItems[i].unit_price).toFixed(2))
$scope.creditnotes.items[i] = {
quantity: qty,
unit_price: unitPrice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,8 @@ public function testExpectedEntityFinancialAccountingEntriesAreCreated($status,
->addWhere('financial_trxn_id', '=', $entityFinancialTrxn['financial_trxn_id'])
->execute();

// completed contribution has no unpaid payment to allocate to line items
if ($status === self::CONTRIBUTION_PENDING) {
$this->assertNotEmpty($lineItemEntityFinancialTrxn);
$this->assertEquals($lineItems->count(), $lineItemEntityFinancialTrxn->count());
}
$this->assertNotEmpty($lineItemEntityFinancialTrxn);
$this->assertEquals($lineItems->count(), $lineItemEntityFinancialTrxn->count());

$allocationEntityFinancialTrxn = \Civi\Api4\EntityFinancialTrxn::get(FALSE)
->addWhere('entity_id', '=', $allocation['id'])
Expand Down
Loading