update live site custom fields; first draft of pro-rated timebanks

This commit is contained in:
Jon Goldberg 2019-09-09 16:35:29 -04:00
parent 21dc82fa19
commit 9f939bc840
No known key found for this signature in database
GPG Key ID: C2D2247364F9DB13

View File

@ -3,9 +3,10 @@
class CRM_Tbusainvoicegen_Timebank {
// Live settings
const TBBILLABLEFIELD = 'custom_202';
const BILLABLEMEMBERFIELD = 'custom_199';
const TBBILLABLEFIELD = 'custom_233';
const BILLABLEMEMBERFIELD = 'custom_236';
const BILLINGPERIODFIELD = 'custom_228';
const TBCREATED = 'custom_112';
// Local dev settings.
// const TBBILLABLEFIELD = 'custom_9';
@ -15,6 +16,7 @@ class CRM_Tbusainvoicegen_Timebank {
/**
* Set the pricing here.
* Up to the number of members in single quotes means you pay the second number every 6 months.
* @var array
*/
private $priceArray = [
'34' => 30,
@ -49,6 +51,12 @@ class CRM_Tbusainvoicegen_Timebank {
*/
private $price;
/**
* The creation date of the timebank.
* @var DateTime
*/
private $creationDate;
/**
* An array of all the contact IDs for whom a contribution already exists in this billing period.
* @var array
@ -73,13 +81,14 @@ class CRM_Tbusainvoicegen_Timebank {
/**
* Generate invoices (called from API).
* @param int $cid
* @param str $billingPeriod The billing period (e.g "2019-2" for the second 2019 payment)
* @return array APIv3 standard response.
*/
public static function generate($cid = NULL, $billingPeriod = NULL) {
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],
'return' => ["id", self::BILLABLEMEMBERFIELD, self::TBCREATED],
'contact_id' => $cid,
'contact_type' => 'Organization',
self::TBBILLABLEFIELD => 1,
@ -89,6 +98,7 @@ 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]);
$tb->setPrice();
$tb->createContribution();
}
@ -127,13 +137,27 @@ class CRM_Tbusainvoicegen_Timebank {
$adjustedMemberCount = $this->memberCount - 2;
foreach ($this->priceArray as $members => $cost) {
if ($adjustedMemberCount <= $members) {
$this->price = $cost;
$this->price = $cost * $this->monthsProRated();
break;
}
}
// Pro-rate the payment if the timebank is new enough.
return $this->price;
}
/**
* If the first annual anniversary of the TB is before the billing period, then the TB pays for the full billing period (6 months).
* If the first annual anniversary of the TB is during any of the first 5 months of the billing period, then the TB pays a pro-rated fee for the number of full billing period months after the first annual anniversary of the TB (1 to 5 months).
* If the first annual anniversary of the TB is during the 6th month of the current billing OR after the billing period ends, then the TB pays $0 for the billing period.
*/
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");
$interval = $beginDate->diff($firstAnniversary);
}
private function createContribution() {
$dueDate = $this->calculateDueDate();
civicrm_api3('Contribution', 'create', [
@ -161,4 +185,20 @@ class CRM_Tbusainvoicegen_Timebank {
return $dueDate;
}
/**
* Returns the first day of the billing period as a date.
* This is to calculate the "new timebank" pro-rated value.
* @return Date
*/
private function calculatePeriodBeginDate() {
list($year, $number) = explode('-', self::$billingPeriod);
if ($number == 1) {
$beginDate = new DateTime($year . '-01-01');
}
else {
$beginDate = new DateTime($year . '-07-01');
}
return $beginDate;
}
}