From: Pratik Joshi Date: Tue, 24 Dec 2013 14:04:42 +0000 (+0530) Subject: CRM-13964 : post process handling and improvement X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ede1935ffdcc1044bd1fe0a61afcc9d0b7043ca0;p=civicrm-core.git CRM-13964 : post process handling and improvement --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 585b745cd9..581a5bf90c 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -146,19 +146,20 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { // CRM-13964 partial payment if (empty($contributionID)) { - if ($partialAmtTotal = CRM_Utils_Array('partial_amount_total', $params) - && $partialAmtPay = CRM_Utils_Array('partial_amount_pay', $params)) { + if (!empty($params['partial_payment_total']) && !empty($params['partial_amount_pay'])) { + $partialAmtTotal = $params['partial_payment_total']; + $partialAmtPay = $params['partial_amount_pay']; $params['total_amount'] = $partialAmtTotal; - $params['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name'); + $params['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name'); } } + if ($contributionID) { CRM_Utils_Hook::pre('edit', 'Contribution', $contributionID, $params); } else { CRM_Utils_Hook::pre('create', 'Contribution', NULL, $params); } - $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->copyValues($params); @@ -2534,12 +2535,46 @@ WHERE contribution_id = %1 "; $statusId = $params['contribution']->contribution_status_id; // CRM-13964 partial payment - if (CRM_Utils_Array::value('contribution_status_id', $params) != array_search('Partially Paid', $contributionStatuses) - && $partialAmtTotal = CRM_Utils_Array('partial_amount_total', $params) - && $partialAmtPay = CRM_Utils_Array('partial_amount_pay', $params)) { - $params['total_amount'] = $partialAmtPay; + if (CRM_Utils_Array::value('contribution_status_id', $params) == array_search('Partially paid', $contributionStatuses) + && !empty($params['partial_payment_total']) && !empty($params['partial_amount_pay'])) { + $partialAmtPay = $params['partial_amount_pay']; + $partialAmtTotal = $params['partial_payment_total']; + $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); + $params['total_amount'] = $partialAmtPay; // new creation of financial trasaction for the balance amount + /* steps to follow : + fetch the current balance, if balance is not present add the balance after appropriate calculation + if balance is present take the current amount and re-calculate the balance and update the balance stored in DB + */ + $balanceTrxnInfo = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($params['contribution']->id, $params['financial_type_id']); + if (!empty($balanceTrxnInfo['trxn_id'])) { + // update the balance amt + $balanceTrxnParams['total_amount'] = $balanceTrxnInfo['total_amount'] - $partialAmtPay; + $balanceTrxnParams['id'] = $balanceTrxnInfo['trxn_id']; + } + else { + // create new balance transaction record + $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); + $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); + + $balanceTrxnParams['total_amount'] = $partialAmtTotal - $partialAmtPay; + $balanceTrxnParams['to_financial_account_id'] = $toFinancialAccount; + $balanceTrxnParams['contribution_id'] = $params['contribution']->id; + + $balanceTrxnParams['trxn_date'] = date('YmdHis'); + $balanceTrxnParams['fee_amount'] = CRM_Utils_Array::value('fee_amount', $params); + $balanceTrxnParams['net_amount'] = CRM_Utils_Array::value('net_amount', $params); + $balanceTrxnParams['currency'] = $params['contribution']->currency; + $balanceTrxnParams['trxn_id'] = $params['contribution']->trxn_id; + $balanceTrxnParams['status_id'] = $statusId; + $balanceTrxnParams['payment_instrument_id'] = $params['contribution']->payment_instrument_id; + $balanceTrxnParams['check_number'] = CRM_Utils_Array::value('check_number', $params); + if (CRM_Utils_Array::value('payment_processor', $params)) { + $balanceTrxnParams['payment_processor_id'] = $params['payment_processor']; + } + } + CRM_Core_BAO_FinancialTrxn::create($balanceTrxnParams); } // build line item array if its not set in $params diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index cecf7e294c..3093a09985 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -83,6 +83,22 @@ class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn { return $trxn; } + static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId) { + $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); + $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionFinancialTypeId, $relationTypeId); + $q = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution' AND eft.entity_id = %1) WHERE ft.to_financial_account_id = %2 "; + $p[1] = array($contributionId, 'Integer'); + $p[2] = array($toFinancialAccount, 'Integer'); + $balanceAmtDAO = CRM_Core_DAO::executeQuery($q, $p); + $ret = array(); + while($balanceAmtDAO->fetch()) { + $ret['trxn_id'] = $balanceAmtDAO->id; + $ret['total_amount'] = $balanceAmtDAO->total_amount; + } + + return $ret; + } + /** * Takes a bunch of params that are needed to match certain criteria and * retrieves the relevant objects. Typically the valid params are only diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index f0919c9c56..a4b6d1d80e 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1161,6 +1161,7 @@ loadCampaign( {$this->_eID}, {$eventCampaigns} ); } $this->_params = $params; + $amountOwed = $params['amount']; unset($params['amount']); $params['register_date'] = CRM_Utils_Date::processDate($params['register_date'], $params['register_date_time']); $params['receive_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $params)); @@ -1467,13 +1468,12 @@ loadCampaign( {$this->_eID}, {$eventCampaigns} ); } // CRM-13964 partial_payment_total - if ($params['fee_amount'] > $params['total_amount']) { + if ($amountOwed > $params['total_amount']) { // the owed amount - $contributionParams['partial_payment_total'] = $params['fee_amount']; + $contributionParams['partial_payment_total'] = $amountOwed; // the actual amount paid $contributionParams['partial_amount_pay'] = $params['total_amount']; } - if ($this->_single) { if (empty($ids)) { $ids = array();