From: Pradeep Nayak Date: Tue, 25 Oct 2016 14:43:03 +0000 (+0530) Subject: CRM-16189, code cleanup, removed cruft code, called existing function X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=955ee56e4fada542f0ad367741b3850f72dec547;p=civicrm-core.git CRM-16189, code cleanup, removed cruft code, called existing function ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 3078e552f8..1ace2da120 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4902,21 +4902,16 @@ LIMIT 1;"; * Function to add payments for contribution * for Partially Paid status * - * @param array $lineItems * @param array $contributions * @param array $contributionStatusId * */ - public static function addPayments($lineItems, $contributions, $contributionStatusId = NULL) { + public static function addPayments($contributions, $contributionStatusId = NULL) { // get financial trxn which is a payment $ftSql = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution' WHERE eft.entity_id = %1 AND ft.is_payment = 1 ORDER BY ft.id DESC LIMIT 1"; - $sql = "SELECT fi.id, li.price_field_value_id - FROM civicrm_financial_item fi - INNER JOIN civicrm_line_item li ON li.id = fi.entity_id - WHERE li.contribution_id = %1"; $contributionStatus = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', array( 'labelColumn' => 'name', )); @@ -4928,30 +4923,13 @@ LIMIT 1;"; } $ftDao = CRM_Core_DAO::executeQuery($ftSql, array(1 => array($contribution->id, 'Integer'))); $ftDao->fetch(); - $trxnAmount = $ftDao->total_amount; - $ftId = $ftDao->id; - // get financial item - $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contribution->id, 'Integer'))); - while ($dao->fetch()) { - $ftIds[$dao->price_field_value_id] = $dao->id; - } - - $params = array( - 'entity_table' => 'civicrm_financial_item', - 'financial_trxn_id' => $ftId, + // store financial item Proportionaly. + $trxnParams = array( + 'total_amount' => $ftDao->total_amount, + 'contribution_id' => $contribution->id, ); - foreach ($lineItems as $key => $value) { - if ($value['qty'] == 0) { - continue; - } - $paid = $value['line_total'] * ($trxnAmount / $contribution->total_amount); - // Record Entity Financial Trxn - $params['amount'] = round($paid, 2); - $params['entity_id'] = $ftIds[$value['price_field_value_id']]; - - civicrm_api3('EntityFinancialTrxn', 'create', $params); - } + self::assignProportionalLineItems($trxnParams, $ftDao->id, $contribution->total_amount); } } @@ -4959,22 +4937,22 @@ LIMIT 1;"; * Function use to store line item proportionaly in * in entity financial trxn table * - * @param array $params + * @param array $trxnParams * * @param Integer $trxnId * * @param float $contributionTotalAmount * */ - public static function assignProportionalLineItems($params, $trxnId, $contributionTotalAmount) { - $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution_id']); + public static function assignProportionalLineItems($trxnParams, $trxnId, $contributionTotalAmount) { + $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($trxnParams['contribution_id']); if (!empty($lineItems)) { // get financial item $sql = "SELECT fi.id, li.price_field_value_id FROM civicrm_financial_item fi INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item' WHERE li.contribution_id = %1"; - $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($params['contribution_id'], 'Integer'))); + $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($trxnParams['contribution_id'], 'Integer'))); while ($dao->fetch()) { $ftIds[$dao->price_field_value_id] = $dao->id; } @@ -4983,7 +4961,10 @@ LIMIT 1;"; 'financial_trxn_id' => $trxnId, ); foreach ($lineItems as $key => $value) { - $paid = $value['line_total'] * ($params['total_amount'] / $contributionTotalAmount); + if ($value['qty'] == 0) { + continue; + } + $paid = $value['line_total'] * ($trxnParams['total_amount'] / $contributionTotalAmount); // Record Entity Financial Trxn $eftParams['amount'] = round($paid, 2); $eftParams['entity_id'] = $ftIds[$value['price_field_value_id']]; diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index bdf78d7083..99bea89c7d 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -392,10 +392,7 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract // Fetch the contribution & do proportional line item assignment $params = array('id' => $this->_contributionId); $contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params); - $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_contributionId); - if (!empty($lineItems)) { - CRM_Contribute_BAO_Contribution::addPayments($lineItems, array($contribution), $contributionStatusId); - } + CRM_Contribute_BAO_Contribution::addPayments(array($contribution), $contributionStatusId); // email sending if (!empty($result) && !empty($submittedValues['is_email_receipt'])) { diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index cf8fe5a4bc..3a27183ccf 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1457,7 +1457,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $lineItem[$this->_priceSetId][$lineKey] = $line; } CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant'); - CRM_Contribute_BAO_Contribution::addPayments($value, $contributions); + CRM_Contribute_BAO_Contribution::addPayments($contributions); } } } diff --git a/api/v3/Payment.php b/api/v3/Payment.php index 0757d8ebb9..51b9c2f5f8 100644 --- a/api/v3/Payment.php +++ b/api/v3/Payment.php @@ -196,11 +196,7 @@ function civicrm_api3_payment_create(&$params) { } elseif (!empty($trxn)) { // Assign the lineitems proportionally - $itemParams = array( - 'contribution_id' => $params['contribution_id'], - 'total_amount' => $params['total_amount'], - ); - CRM_Contribute_BAO_Contribution::assignProportionalLineItems($itemParams, $trxn->id, $contribution['total_amount']); + CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution['total_amount']); } } } diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index b37326175c..532cb98af6 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -585,9 +585,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { */ public function testAddPayments() { list($lineItems, $contribution) = $this->addParticipantWithContribution(); - foreach ($lineItems as $value) { - CRM_Contribute_BAO_Contribution::addPayments($value, array($contribution)); - } + CRM_Contribute_BAO_Contribution::addPayments(array($contribution)); $this->checkItemValues($contribution); }