From 845b9e0d4cd2c2e7081ce6602f3bb048976665dc Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Mon, 18 Nov 2019 13:50:39 -0500 Subject: [PATCH] Add Number of Members Offset and discrete Biannual Fee Amount --- CRM/Tbusainvoicegen/Timebank.php | 40 ++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/CRM/Tbusainvoicegen/Timebank.php b/CRM/Tbusainvoicegen/Timebank.php index e6fc716..92132cb 100644 --- a/CRM/Tbusainvoicegen/Timebank.php +++ b/CRM/Tbusainvoicegen/Timebank.php @@ -2,7 +2,6 @@ class CRM_Tbusainvoicegen_Timebank { - // Live settings const TBBILLABLEFIELD = 'custom_233'; const BILLABLEMEMBERFIELD = 'custom_236'; const BILLINGPERIODFIELD = 'custom_228'; @@ -12,17 +11,8 @@ class CRM_Tbusainvoicegen_Timebank { const PERIODBEGIN = 'custom_246'; const PERIODEND = 'custom_247'; const BILLABLEMEMBERSCONTRIB = 'custom_249'; - - // Local dev settings. -// const TBBILLABLEFIELD = 'custom_14'; -// const BILLABLEMEMBERFIELD = 'custom_15'; -// const BILLINGPERIODFIELD = 'custom_7'; -// const TBCREATED = 'custom_16'; -// const INVOICEDATE = 'custom_9'; -// const DUEDATE = 'custom_11'; -// const PERIODBEGIN = 'custom_12'; -// const PERIODEND = 'custom_13'; -// const BILLABLEMEMBERSCONTRIB = 'custom_8'; + const BIANNUALFEERATE = 'custom_250'; + const NUMBEROFMEMBERSOFFSET = 'custom_253'; /** * Set the pricing here. @@ -99,6 +89,12 @@ class CRM_Tbusainvoicegen_Timebank { */ private $periodEnd; + /** + * The biannual fee rate - same as the cost, but not pro-rated for new timebanks. + * @var int + */ + private $biannualFeeRate; + /** * An array of all the contact IDs for whom a contribution already exists in this billing period. * @var array @@ -118,10 +114,6 @@ class CRM_Tbusainvoicegen_Timebank { public function __construct($cid, $memberCount) { $this->cid = $cid; $this->memberCount = $memberCount; - $this->memberCountBillable = $memberCount - 2; - if ($this->memberCountBillable < 0) { - $this->memberCountBillable = 0; - } } /** @@ -140,7 +132,7 @@ class CRM_Tbusainvoicegen_Timebank { self::setBillingPeriod($billingPeriod); // Get a list of contact IDs for everyone to generate an invoice for. $contacts = civicrm_api3('Contact', 'get', [ - 'return' => ["id", self::BILLABLEMEMBERFIELD, self::TBCREATED], + 'return' => ["id", self::BILLABLEMEMBERFIELD, self::TBCREATED, self::NUMBEROFMEMBERSOFFSET], 'contact_id' => $cid, 'contact_type' => 'Organization', self::TBBILLABLEFIELD => 1, @@ -154,6 +146,7 @@ class CRM_Tbusainvoicegen_Timebank { if (!$contact[self::TBCREATED]) { continue; } + $tb->setMemberCountBillable($contact[self::NUMBEROFMEMBERSOFFSET]); $tb->creationDate = new DateTime($contact[self::TBCREATED]); $tb->setInvoiceDate($params); $tb->setDueDate($params); @@ -164,6 +157,15 @@ class CRM_Tbusainvoicegen_Timebank { } } + private function setMemberCountBillable($numberOfMembersOffset) { + // Default offset is 2. + $numberOfMembersOffset = $numberOfMembersOffset ?: 2; + $this->memberCountBillable = $this->memberCount - $numberOfMembersOffset; + if ($this->memberCountBillable < 0) { + $this->memberCountBillable = 0; + } + } + public static function setBillingPeriod($billingPeriod) { if ($billingPeriod) { self::$billingPeriod = $billingPeriod; @@ -180,6 +182,7 @@ class CRM_Tbusainvoicegen_Timebank { $existing = []; $result = civicrm_api3('Contribution', 'get', [ self::BILLINGPERIODFIELD => self::$billingPeriod, + 'options' => ['limit' => 0], ]); if ($result['count']) { foreach ($result['values'] as $contrib) { @@ -200,6 +203,7 @@ class CRM_Tbusainvoicegen_Timebank { // After this "break", $cost will be accurate. } } + $this->biannualFeeRate = $cost; // Pro-rate the payment if the timebank is new enough. $this->price = round($cost * $this->monthsProRated() / 6, 2); return $this->price; @@ -254,6 +258,7 @@ class CRM_Tbusainvoicegen_Timebank { self::PERIODBEGIN => $this->periodBegin->format('Y-m-d'), self::PERIODEND => $this->periodEnd->format('Y-m-d'), self::BILLABLEMEMBERSCONTRIB => $this->memberCountBillable, + self::BIANNUALFEERATE => $this->biannualFeeRate, 'total_amount' => $this->price, 'contact_id' => $this->cid, 'contribution_status_id' => 'Pending', @@ -316,6 +321,7 @@ class CRM_Tbusainvoicegen_Timebank { * $invoiceData[0] is the due date (e.g. "9/30/2019) * $invoiceData[1] is the billing period (e.g. "7/1/2019-12/31/2019"). * $invoiceData[2] is the total amount this contact owes. + * @param int $contactId the contact ID of the timebank. * @param str $billingPeriod */ public static function invoiceData($contactId, $billingPeriod) {