From e94484960f8f384ab0ee2a4cea34ff1df85eb81d Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Mon, 9 Sep 2019 18:11:08 -0400 Subject: [PATCH] pro-rata handled --- CRM/Tbusainvoicegen/Timebank.php | 44 ++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/CRM/Tbusainvoicegen/Timebank.php b/CRM/Tbusainvoicegen/Timebank.php index 49c0026..7251eff 100644 --- a/CRM/Tbusainvoicegen/Timebank.php +++ b/CRM/Tbusainvoicegen/Timebank.php @@ -3,15 +3,16 @@ class CRM_Tbusainvoicegen_Timebank { // Live settings - const TBBILLABLEFIELD = 'custom_233'; - const BILLABLEMEMBERFIELD = 'custom_236'; - const BILLINGPERIODFIELD = 'custom_228'; - const TBCREATED = 'custom_112'; + // const TBBILLABLEFIELD = 'custom_233'; + // const BILLABLEMEMBERFIELD = 'custom_236'; + // const BILLINGPERIODFIELD = 'custom_228'; + // const TBCREATED = 'custom_112'; // Local dev settings. - // const TBBILLABLEFIELD = 'custom_9'; - // const BILLABLEMEMBERFIELD = 'custom_10'; - // const BILLINGPERIODFIELD = 'custom_11'; + const TBBILLABLEFIELD = 'custom_9'; + const BILLABLEMEMBERFIELD = 'custom_10'; + const BILLINGPERIODFIELD = 'custom_11'; + const TBCREATED = 'custom_27'; /** * Set the pricing here. @@ -98,7 +99,11 @@ class CRM_Tbusainvoicegen_Timebank { foreach ($contacts as $k => $contact) { if (!in_array($k, self::$contributionExists)) { $tb = new CRM_Tbusainvoicegen_Timebank($k, $contact[self::BILLABLEMEMBERFIELD]); - $tb->$dateCreated = new DateTime($contact[self::TBCREATED]); + // Don't generate an invoice if there's no "TB Created" date. + if (!$contact[self::TBCREATED]) { + continue; + } + $tb->creationDate = new DateTime($contact[self::TBCREATED]); $tb->setPrice(); $tb->createContribution(); } @@ -137,11 +142,12 @@ class CRM_Tbusainvoicegen_Timebank { $adjustedMemberCount = $this->memberCount - 2; foreach ($this->priceArray as $members => $cost) { if ($adjustedMemberCount <= $members) { - $this->price = $cost * $this->monthsProRated(); break; + // After this "break", $cost will be accurate. } } // Pro-rate the payment if the timebank is new enough. + $this->price = round($cost * $this->monthsProRated() / 6, 2); return $this->price; } @@ -153,12 +159,28 @@ class CRM_Tbusainvoicegen_Timebank { private function monthsProRated() { // By default, we bill all 6 months in a billing period. $monthsProRated = 6; - $beginDate = $this->calculatePeriodBeginDate; - $firstAnniversary = $this->$creationDate->modify("+1 year"); + $beginDate = $this->calculatePeriodBeginDate(); + $firstAnniversary = $this->creationDate->modify("+1 year"); $interval = $beginDate->diff($firstAnniversary); + // First anniversary is before the billing period. + if ($interval->invert == 1) { + $monthsProRated = 6; + } + // Starts past the end of the billing period. + elseif ($interval->y >= 1 || $interval->m >= 6) { + $monthsProRated = 0; + } + // Falls within the billing period. + else { + $monthsProRated = 5 - $interval->m; + } + return $monthsProRated; } private function createContribution() { + if (!$this->price) { + return; + } $dueDate = $this->calculateDueDate(); civicrm_api3('Contribution', 'create', [ 'financial_type_id' => 'CW License Fee',