tbusainvoicegen/CRM/Tbusainvoicegen/Timebank.php

125 lines
3.0 KiB
PHP
Raw Normal View History

2019-06-28 20:54:40 +00:00
<?php
class CRM_Tbusainvoicegen_Timebank {
/**
* Live settings
* const TBBILLABLEFIELD = 'custom_202';
* const BILLABLEMEMBERFIELD = 'custom_199';
*/
const TBBILLABLEFIELD = 'custom_9';
const BILLABLEMEMBERFIELD = 'custom_10';
/**
* Set the pricing here.
* Up to the number of members in single quotes means you pay the second number every 6 months.
*/
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;
/**
* 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
* @return array APIv3 standard response.
*/
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.
$contacts = civicrm_api3('Contact', 'get', [
2019-06-28 20:54:40 +00:00
'return' => ["id", self::BILLABLEMEMBERFIELD],
'contact_id' => $cid,
'contact_type' => 'Organization',
self::TBBILLABLEFIELD => 1,
'options' => ['limit' => 0],
])['values'];
self::setContributionExists();
2019-06-28 20:54:40 +00:00
foreach ($result as $k => $contact) {
$tb = new CRM_Tbusainvoicegen_Timebank($k, $contact[self::BILLABLEMEMBERFIELD]);
$price = $tb->setPrice();
}
}
public static function setBillingPeriod($billingPeriod) {
if ($billingPeriod) {
self::$billingPeriod = $billingPeriod;
2019-06-28 20:54:40 +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() {
// API call to contribution.get to find all records with this billing period
// Return an array of contact IDs.
2019-06-28 20:54:40 +00:00
}
private function setPrice() {
if ($this->price) {
return $this->price;
2019-06-28 20:54:40 +00:00
}
$adjustedMemberCount = $this->memberCount - 2;
foreach ($this->priceArray as $members => $cost) {
2019-06-28 20:54:40 +00:00
if ($adjustedMemberCount <= $members) {
$this->price = $cost;
2019-06-28 20:54:40 +00:00
break;
}
}
return $this->price;
2019-06-28 20:54:40 +00:00
}
}