[ready-for-core-team-review]CRM-16189, added function to calculate revenue amount...
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 6 Jul 2016 03:18:39 +0000 (08:48 +0530)
committercolemanw <coleman@civicrm.org>
Wed, 6 Jul 2016 03:18:39 +0000 (23:18 -0400)
* 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

index d05ce54f94d0e0c8b53af33365bda67f3f568a22..6f53352283811ae031dcee5676cfd4128824dd6a 100644 (file)
@@ -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;
+  }
+
 }