2019-06-28 20:54:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CRM_Tbusainvoicegen_Timebank {
|
|
|
|
|
2019-09-09 20:35:29 +00:00
|
|
|
// Live settings
|
2019-09-09 22:12:29 +00:00
|
|
|
const TBBILLABLEFIELD = 'custom_233';
|
|
|
|
const BILLABLEMEMBERFIELD = 'custom_236';
|
|
|
|
const BILLINGPERIODFIELD = 'custom_228';
|
|
|
|
const TBCREATED = 'custom_112';
|
2019-06-28 22:02:38 +00:00
|
|
|
|
|
|
|
// Local dev settings.
|
2019-09-09 22:12:29 +00:00
|
|
|
// const TBBILLABLEFIELD = 'custom_9';
|
|
|
|
// const BILLABLEMEMBERFIELD = 'custom_10';
|
|
|
|
// const BILLINGPERIODFIELD = 'custom_11';
|
|
|
|
// const TBCREATED = 'custom_27';
|
2019-06-28 20:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the pricing here.
|
|
|
|
* Up to the number of members in single quotes means you pay the second number every 6 months.
|
2019-09-09 20:35:29 +00:00
|
|
|
* @var array
|
2019-06-28 20:54:40 +00:00
|
|
|
*/
|
|
|
|
private $priceArray = [
|
|
|
|
'34' => 30,
|
|
|
|
'49' => 60,
|
|
|
|
'79' => 90,
|
|
|
|
'119' => 160,
|
|
|
|
'149' => 200,
|
|
|
|
'199' => 240,
|
|
|
|
'249' => 340,
|
|
|
|
'349' => 440,
|
|
|
|
'499' => 590,
|
|
|
|
'749' => 880,
|
|
|
|
'1000' => 1200,
|
|
|
|
'9999999' => 9999,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The contact ID of this timebank.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $contactId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of billable members.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $memberCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The price for one period of this timebank.
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $price;
|
|
|
|
|
2019-09-09 20:35:29 +00:00
|
|
|
/**
|
|
|
|
* The creation date of the timebank.
|
|
|
|
* @var DateTime
|
|
|
|
*/
|
|
|
|
private $creationDate;
|
|
|
|
|
2019-06-28 21:30:27 +00:00
|
|
|
/**
|
|
|
|
* An array of all the contact IDs for whom a contribution already exists in this billing period.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $contributionExists = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A string that represents the current billing period - e.g. "2019-2".
|
|
|
|
* First number is the year; second is 1 for Jan-Jun, 2 for Jul-Dec.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $billingPeriod = NULL;
|
|
|
|
|
2019-06-28 20:54:40 +00:00
|
|
|
/**
|
|
|
|
* Class constructor.
|
|
|
|
*/
|
|
|
|
public function __construct($cid, $memberCount) {
|
|
|
|
$this->cid = $cid;
|
|
|
|
$this->memberCount = $memberCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate invoices (called from API).
|
|
|
|
* @param int $cid
|
2019-09-09 20:35:29 +00:00
|
|
|
* @param str $billingPeriod The billing period (e.g "2019-2" for the second 2019 payment)
|
2019-06-28 20:54:40 +00:00
|
|
|
* @return array APIv3 standard response.
|
|
|
|
*/
|
2019-06-28 21:30:27 +00:00
|
|
|
public static function generate($cid = NULL, $billingPeriod = NULL) {
|
|
|
|
self::setBillingPeriod($billingPeriod);
|
2019-06-28 20:54:40 +00:00
|
|
|
// Get a list of contact IDs for everyone to generate an invoice for.
|
2019-06-28 21:30:27 +00:00
|
|
|
$contacts = civicrm_api3('Contact', 'get', [
|
2019-09-09 20:35:29 +00:00
|
|
|
'return' => ["id", self::BILLABLEMEMBERFIELD, self::TBCREATED],
|
2019-06-28 20:54:40 +00:00
|
|
|
'contact_id' => $cid,
|
|
|
|
'contact_type' => 'Organization',
|
|
|
|
self::TBBILLABLEFIELD => 1,
|
|
|
|
'options' => ['limit' => 0],
|
2019-06-28 21:30:27 +00:00
|
|
|
])['values'];
|
|
|
|
self::setContributionExists();
|
2019-06-28 22:01:05 +00:00
|
|
|
foreach ($contacts as $k => $contact) {
|
|
|
|
if (!in_array($k, self::$contributionExists)) {
|
|
|
|
$tb = new CRM_Tbusainvoicegen_Timebank($k, $contact[self::BILLABLEMEMBERFIELD]);
|
2019-09-09 22:11:08 +00:00
|
|
|
// Don't generate an invoice if there's no "TB Created" date.
|
|
|
|
if (!$contact[self::TBCREATED]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$tb->creationDate = new DateTime($contact[self::TBCREATED]);
|
2019-06-28 22:01:05 +00:00
|
|
|
$tb->setPrice();
|
|
|
|
$tb->createContribution();
|
|
|
|
}
|
2019-06-28 21:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setBillingPeriod($billingPeriod) {
|
|
|
|
if ($billingPeriod) {
|
|
|
|
self::$billingPeriod = $billingPeriod;
|
2019-06-28 20:54:40 +00:00
|
|
|
}
|
2019-06-28 21:30:27 +00:00
|
|
|
else {
|
|
|
|
// Should we try to auto-calculate it here? Or nah?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an array of contacts who already have a contribution created in this billing period.
|
|
|
|
*/
|
|
|
|
public static function setContributionExists() {
|
2019-06-28 22:01:05 +00:00
|
|
|
$existing = [];
|
|
|
|
$result = civicrm_api3('Contribution', 'get', [
|
|
|
|
self::BILLINGPERIODFIELD => self::$billingPeriod,
|
|
|
|
]);
|
|
|
|
if ($result['count']) {
|
|
|
|
foreach ($result['values'] as $contrib) {
|
|
|
|
$existing[$contrib['contact_id']] = $contrib['contact_id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self::$contributionExists = $existing;
|
2019-06-28 20:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function setPrice() {
|
2019-06-28 21:30:27 +00:00
|
|
|
if ($this->price) {
|
|
|
|
return $this->price;
|
2019-06-28 20:54:40 +00:00
|
|
|
}
|
2019-06-28 21:30:27 +00:00
|
|
|
$adjustedMemberCount = $this->memberCount - 2;
|
|
|
|
foreach ($this->priceArray as $members => $cost) {
|
2019-06-28 20:54:40 +00:00
|
|
|
if ($adjustedMemberCount <= $members) {
|
|
|
|
break;
|
2019-09-09 22:11:08 +00:00
|
|
|
// After this "break", $cost will be accurate.
|
2019-06-28 20:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 20:35:29 +00:00
|
|
|
// Pro-rate the payment if the timebank is new enough.
|
2019-09-09 22:11:08 +00:00
|
|
|
$this->price = round($cost * $this->monthsProRated() / 6, 2);
|
2019-06-28 21:30:27 +00:00
|
|
|
return $this->price;
|
2019-06-28 20:54:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 20:35:29 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2019-09-09 22:11:08 +00:00
|
|
|
$beginDate = $this->calculatePeriodBeginDate();
|
|
|
|
$firstAnniversary = $this->creationDate->modify("+1 year");
|
2019-09-09 20:35:29 +00:00
|
|
|
$interval = $beginDate->diff($firstAnniversary);
|
2019-09-09 22:11:08 +00:00
|
|
|
// 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;
|
2019-09-09 20:35:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 22:01:05 +00:00
|
|
|
private function createContribution() {
|
2019-09-09 22:11:08 +00:00
|
|
|
if (!$this->price) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-28 22:01:05 +00:00
|
|
|
$dueDate = $this->calculateDueDate();
|
|
|
|
civicrm_api3('Contribution', 'create', [
|
|
|
|
'financial_type_id' => 'CW License Fee',
|
|
|
|
'date_received' => $dueDate,
|
|
|
|
'total_amount' => $this->price,
|
|
|
|
'contact_id' => $this->cid,
|
|
|
|
'contribution_status_id' => 'Pending',
|
|
|
|
self::BILLINGPERIODFIELD => self::$billingPeriod,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the due date as a string.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function calculateDueDate() {
|
|
|
|
list($year, $number) = explode('-', self::$billingPeriod);
|
|
|
|
if ($number == 1) {
|
|
|
|
$dueDate = $year . '-03-31';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$dueDate = $year . '-09-30';
|
|
|
|
}
|
|
|
|
return $dueDate;
|
|
|
|
}
|
|
|
|
|
2019-09-09 20:35:29 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2019-06-28 20:54:40 +00:00
|
|
|
}
|