From 0618910bce970f4a4f781909ad1904feb6e33f2b Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 16 Dec 2015 19:18:32 +0530 Subject: [PATCH] --CRM-16259, added function to add payments for partially paid contribution --- CRM/Contribute/BAO/Contribution.php | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index e44de0544d..a82f04b347 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4722,4 +4722,48 @@ LIMIT 1;"; return 'recurring contribution'; } + /** + * Function to add payments for contribution + * for Partially Paid status + * + * @param array $lineItems + * @param array $contributions + * + */ + public static function addPayments($lineItems, $contributions) { + foreach ($contributions as $k => $contribution) { + if ($contribution->contribution_status_id != CRM_Core_OptionGroup::getValue('contribution_status', 'Partially paid', 'name')) { + continue; + } + // get financial trxn which is a payment + $sql = "SELECT ft.id + 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"; + $ftId = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($contribution->id, 'Integer'))); + // 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 + WHERE li.contribution_id = %1"; + $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contribution->id, 'Integer'))); + while ($dao->fetch()) { + $ftIds[$dao->price_field_value_id] = $dao->id; + } + foreach ($lineItems as $key => $value) { + $paid = $value['line_total'] * ($contribution->net_amount / $contribution->total_amount); + // Record Entity Financial Trxn + $params = array( + 'entity_table' => 'civicrm_financial_item', + 'financial_trxn_id' => $ftId, + 'amount' => $paid, + 'entity_id' => $ftIds[$value['price_field_value_id']], + ); + $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn(); + $entityTrxn->copyValues($params); + $entityTrxn->save(); + } + } + } + } -- 2.25.1