Skip to content

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb authored and sluc23 committed Jan 10, 2018
1 parent fc271ab commit d07078d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
12 changes: 10 additions & 2 deletions CRM/Price/BAO/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public static function changeFeeSelections(
if (!empty($requiredChanges['line_items_to_cancel']) || !empty($requiredChanges['line_items_to_update'])) {
// @todo - this IF is to get this through PR merge but I suspect that it should not
// be necessary & is masking something else.
$financialItemsArray = $lineItemObj->getReverseFinancialItemsToRecord(
$financialItemsArray = $lineItemObj->getAdjustedFinancialItemsToRecord(
$entityID,
$entityTable,
$contributionId,
Expand Down Expand Up @@ -750,7 +750,7 @@ public static function changeFeeSelections(
* @return array
* List of formatted reverse Financial Items to be recorded
*/
protected function getReverseFinancialItemsToRecord($entityID, $entityTable, $contributionID, $priceFieldValueIDsToCancel, $lineItemsToUpdate) {
protected function getAdjustedFinancialItemsToRecord($entityID, $entityTable, $contributionID, $priceFieldValueIDsToCancel, $lineItemsToUpdate) {
$previousLineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, str_replace('civicrm_', '', $entityTable));

$financialItemsArray = array();
Expand Down Expand Up @@ -780,11 +780,19 @@ protected function getReverseFinancialItemsToRecord($entityID, $entityTable, $co
// INSERT negative financial_items for tax amount
$financialItemsArray[$updateFinancialItemInfoValues['entity_id']] = $updateFinancialItemInfoValues;
}
// INSERT a financial item to record surplus/lesser amount when a text price fee is changed
elseif (!empty($lineItemsToUpdate) &&
$lineItemsToUpdate[$updateFinancialItemInfoValues['price_field_value_id']]['html_type'] == 'Text' &&
$updateFinancialItemInfoValues['amount'] > 0
) {
// calculate the amount difference, considered as financial item amount
$updateFinancialItemInfoValues['amount'] = $lineItemsToUpdate[$updateFinancialItemInfoValues['price_field_value_id']]['line_total'] - $totalFinancialAmount;
// add a flag, later used to link financial trxn and this new financial item
$updateFinancialItemInfoValues['link-financial-trxn'] = TRUE;
if ($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']) {
$updateFinancialItemInfoValues['tax']['amount'] = $lineItemsToUpdate[$updateFinancialItemInfoValues['entity_id']]['tax_amount'] - $previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount'];
$updateFinancialItemInfoValues['tax']['description'] = $this->getSalesTaxTerm();
}
$financialItemsArray[$updateFinancialItemInfoValues['entity_id']] = $updateFinancialItemInfoValues;
}
}
Expand Down
48 changes: 44 additions & 4 deletions tests/phpunit/CRM/Event/BAO/CRM19273Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ private function totalIncome($participantId) {
private function balanceCheck($amount) {
$this->assertEquals($amount, $this->contributionInvoice($this->_contributionId), "Invoice must a total of $amount");
$this->assertEquals($amount, $this->totalIncome($this->_participantId), "The recorded income must be $amount ");
$this->assertEquals($amount, $this->totalIncome($this->_contributionId), "The accumulated assets must be $amount ");
}

/**
Expand Down Expand Up @@ -351,18 +350,59 @@ public function testCRM21513() {
'contribution_id' => $this->_contributionId,
));

// CASE 1: Choose text price qty 1 (x$10 = $10 amount)
$priceSetParams['price_1'] = 1;
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
CRM_Price_BAO_PriceSet::processAmount($this->_feeBlock, $priceSetParams, $lineItem);
$lineItemVal[$this->_priceSetID] = $lineItem;
CRM_Price_BAO_LineItem::processPriceSet($this->_participantId, $lineItemVal, $contribution, 'civicrm_participant');

// CASE 2: Choose text price qty 3 (x$10 = $30 amount)
$priceSetParams['price_1'] = 3;
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, 0);

// CASE 3: Choose text price qty 2 (x$10 = $20 amount)
$priceSetParams['price_1'] = 2;
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant');
$lineitem = civicrm_api3('LineItem', 'Getsingle', array(
'entity_table' => 'civicrm_participant',
));
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, 0);

$financialItems = $this->callAPISuccess('FinancialItem', 'Get', array(
'entity_table' => 'civicrm_line_item',
'entity_id' => array('IN' => array_keys($lineItem)),
'sequential' => 1,
));

$unpaidStatus = CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid');
$expectedResults = array(
array(
'amount' => 10.00, // when qty 1 is used
'status_id' => $unpaidStatus,
'entity_table' => 'civicrm_line_item',
'entity_id' => 1,
),
array(
'amount' => 20.00, // when qty 3 is used, add the surplus amount i.e. $30 - $10 = $20
'status_id' => $unpaidStatus,
'entity_table' => 'civicrm_line_item',
'entity_id' => 1,
),
array(
'amount' => -10.00, // when qty 2 is used, add the surplus amount i.e. $20 - $30 = -$10
'status_id' => $unpaidStatus,
'entity_table' => 'civicrm_line_item',
'entity_id' => 1,
),
);
// Check if 3 financial items were recorded
$this->assertEquals(count($expectedResults), $financialItems['count']);
foreach ($expectedResults as $key => $expectedResult) {
foreach ($expectedResult as $column => $value) {
$this->assertEquals($expectedResult[$column], $financialItems['values'][$key][$column]);
}
}

$this->balanceCheck(20);
}

}

0 comments on commit d07078d

Please sign in to comment.