Add Number of Members Offset and discrete Biannual Fee Amount

This commit is contained in:
Jon Goldberg 2019-11-18 13:50:39 -05:00
parent 7cea8d13be
commit 845b9e0d4c
No known key found for this signature in database
GPG Key ID: C2D2247364F9DB13
1 changed files with 23 additions and 17 deletions

View File

@ -2,7 +2,6 @@
class CRM_Tbusainvoicegen_Timebank { class CRM_Tbusainvoicegen_Timebank {
// Live settings
const TBBILLABLEFIELD = 'custom_233'; const TBBILLABLEFIELD = 'custom_233';
const BILLABLEMEMBERFIELD = 'custom_236'; const BILLABLEMEMBERFIELD = 'custom_236';
const BILLINGPERIODFIELD = 'custom_228'; const BILLINGPERIODFIELD = 'custom_228';
@ -12,17 +11,8 @@ class CRM_Tbusainvoicegen_Timebank {
const PERIODBEGIN = 'custom_246'; const PERIODBEGIN = 'custom_246';
const PERIODEND = 'custom_247'; const PERIODEND = 'custom_247';
const BILLABLEMEMBERSCONTRIB = 'custom_249'; const BILLABLEMEMBERSCONTRIB = 'custom_249';
const BIANNUALFEERATE = 'custom_250';
// Local dev settings. const NUMBEROFMEMBERSOFFSET = 'custom_253';
// 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';
/** /**
* Set the pricing here. * Set the pricing here.
@ -99,6 +89,12 @@ class CRM_Tbusainvoicegen_Timebank {
*/ */
private $periodEnd; 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. * An array of all the contact IDs for whom a contribution already exists in this billing period.
* @var array * @var array
@ -118,10 +114,6 @@ class CRM_Tbusainvoicegen_Timebank {
public function __construct($cid, $memberCount) { public function __construct($cid, $memberCount) {
$this->cid = $cid; $this->cid = $cid;
$this->memberCount = $memberCount; $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); self::setBillingPeriod($billingPeriod);
// Get a list of contact IDs for everyone to generate an invoice for. // Get a list of contact IDs for everyone to generate an invoice for.
$contacts = civicrm_api3('Contact', 'get', [ $contacts = civicrm_api3('Contact', 'get', [
'return' => ["id", self::BILLABLEMEMBERFIELD, self::TBCREATED], 'return' => ["id", self::BILLABLEMEMBERFIELD, self::TBCREATED, self::NUMBEROFMEMBERSOFFSET],
'contact_id' => $cid, 'contact_id' => $cid,
'contact_type' => 'Organization', 'contact_type' => 'Organization',
self::TBBILLABLEFIELD => 1, self::TBBILLABLEFIELD => 1,
@ -154,6 +146,7 @@ class CRM_Tbusainvoicegen_Timebank {
if (!$contact[self::TBCREATED]) { if (!$contact[self::TBCREATED]) {
continue; continue;
} }
$tb->setMemberCountBillable($contact[self::NUMBEROFMEMBERSOFFSET]);
$tb->creationDate = new DateTime($contact[self::TBCREATED]); $tb->creationDate = new DateTime($contact[self::TBCREATED]);
$tb->setInvoiceDate($params); $tb->setInvoiceDate($params);
$tb->setDueDate($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) { public static function setBillingPeriod($billingPeriod) {
if ($billingPeriod) { if ($billingPeriod) {
self::$billingPeriod = $billingPeriod; self::$billingPeriod = $billingPeriod;
@ -180,6 +182,7 @@ class CRM_Tbusainvoicegen_Timebank {
$existing = []; $existing = [];
$result = civicrm_api3('Contribution', 'get', [ $result = civicrm_api3('Contribution', 'get', [
self::BILLINGPERIODFIELD => self::$billingPeriod, self::BILLINGPERIODFIELD => self::$billingPeriod,
'options' => ['limit' => 0],
]); ]);
if ($result['count']) { if ($result['count']) {
foreach ($result['values'] as $contrib) { foreach ($result['values'] as $contrib) {
@ -200,6 +203,7 @@ class CRM_Tbusainvoicegen_Timebank {
// After this "break", $cost will be accurate. // After this "break", $cost will be accurate.
} }
} }
$this->biannualFeeRate = $cost;
// Pro-rate the payment if the timebank is new enough. // Pro-rate the payment if the timebank is new enough.
$this->price = round($cost * $this->monthsProRated() / 6, 2); $this->price = round($cost * $this->monthsProRated() / 6, 2);
return $this->price; return $this->price;
@ -254,6 +258,7 @@ class CRM_Tbusainvoicegen_Timebank {
self::PERIODBEGIN => $this->periodBegin->format('Y-m-d'), self::PERIODBEGIN => $this->periodBegin->format('Y-m-d'),
self::PERIODEND => $this->periodEnd->format('Y-m-d'), self::PERIODEND => $this->periodEnd->format('Y-m-d'),
self::BILLABLEMEMBERSCONTRIB => $this->memberCountBillable, self::BILLABLEMEMBERSCONTRIB => $this->memberCountBillable,
self::BIANNUALFEERATE => $this->biannualFeeRate,
'total_amount' => $this->price, 'total_amount' => $this->price,
'contact_id' => $this->cid, 'contact_id' => $this->cid,
'contribution_status_id' => 'Pending', 'contribution_status_id' => 'Pending',
@ -316,6 +321,7 @@ class CRM_Tbusainvoicegen_Timebank {
* $invoiceData[0] is the due date (e.g. "9/30/2019) * $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[1] is the billing period (e.g. "7/1/2019-12/31/2019").
* $invoiceData[2] is the total amount this contact owes. * $invoiceData[2] is the total amount this contact owes.
* @param int $contactId the contact ID of the timebank.
* @param str $billingPeriod * @param str $billingPeriod
*/ */
public static function invoiceData($contactId, $billingPeriod) { public static function invoiceData($contactId, $billingPeriod) {