From f1eab68f0bb3a34af460d50d77ac6b6066703a3c Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 6 Jul 2016 08:48:39 +0530 Subject: [PATCH] [ready-for-core-team-review]CRM-16189, added function to calculate revenue amount for membership (#8577) * CRM-16189, added function to calculate revenue amount for membership ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, updated function ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, fixed Jenkins error ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 --- CRM/Core/BAO/FinancialTrxn.php | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index d05ce54f94..6f53352283 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -560,4 +560,41 @@ WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFina return $trxn; } + /** + * Get revenue amount for membership. + * + * @param array $lineItem + * + * @return array + */ + public static function getMembershipRevenueAmount($lineItem) { + $revenueAmount = array(); + $membershipDetail = civicrm_api3('Membership', 'getsingle', array( + 'id' => $lineItem['entity_id'], + )); + if (empty($membershipDetail['end_date'])) { + return $revenueAmount; + } + + $startDate = strtotime($membershipDetail['start_date']); + $endDate = strtotime($membershipDetail['end_date']); + $startYear = date('Y', $startDate); + $endYear = date('Y', $endDate); + $startMonth = date('m', $startDate); + $endMonth = date('m', $endDate); + + $monthOfService = (($endYear - $startYear) * 12) + ($endMonth - $startMonth); + $startDateOfRevenue = $membershipDetail['start_date']; + $typicalPayment = round(($lineItem['line_total'] / $monthOfService), 2); + for ($i = 0; $i <= $monthOfService - 1; $i++) { + $revenueAmount[$i]['amount'] = $typicalPayment; + if ($i == 0) { + $revenueAmount[$i]['amount'] -= (($typicalPayment * $monthOfService) - $lineItem['line_total']); + } + $revenueAmount[$i]['revenue_date'] = $startDateOfRevenue; + $startDateOfRevenue = date('Y-m', strtotime('+1 month', strtotime($startDateOfRevenue))) . '-01'; + } + return $revenueAmount; + } + } -- 2.25.1