api updated; can successfully calculate price now

This commit is contained in:
Jon Goldberg 2019-06-28 17:30:27 -04:00
parent 84a8314761
commit f33b02dc20
No known key found for this signature in database
GPG Key ID: C2D2247364F9DB13
2 changed files with 46 additions and 26 deletions

View File

@ -47,6 +47,19 @@ class CRM_Tbusainvoicegen_Timebank {
*/
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;
/**
* Class constructor.
*/
@ -60,32 +73,52 @@ class CRM_Tbusainvoicegen_Timebank {
* @param int $cid
* @return array APIv3 standard response.
*/
public static function generate($cid = NULL) {
public static function generate($cid = NULL, $billingPeriod = NULL) {
self::setBillingPeriod($billingPeriod);
// Get a list of contact IDs for everyone to generate an invoice for.
$result = civicrm_api3('Contact', 'get', [
$contacts = civicrm_api3('Contact', 'get', [
'return' => ["id", self::BILLABLEMEMBERFIELD],
'contact_id' => $cid,
'contact_type' => 'Organization',
self::TBBILLABLEFIELD => 1,
'options' => ['limit' => 0],
]);
])['values'];
self::setContributionExists();
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;
}
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.
}
private function setPrice() {
if (self::$price) {
return self::$price;
if ($this->price) {
return $this->price;
}
$adjustedMemberCount = $memberCount - 2;
foreach ($priceArray as $members => $cost) {
$adjustedMemberCount = $this->memberCount - 2;
foreach ($this->priceArray as $members => $cost) {
if ($adjustedMemberCount <= $members) {
self::$price = $cost;
$this->price = $cost;
break;
}
}
return self::$price;
return $this->price;
}
}

View File

@ -10,7 +10,8 @@ use CRM_Tbusainvoicegen_ExtensionUtil as E;
* @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
*/
function _civicrm_api3_invoicegen_Generate_spec(&$spec) {
$spec['magicword']['api.required'] = 1;
$spec['contact_id']['api.required'] = 0;
$spec['billing_period']['api.required'] = 0;
}
/**
@ -23,20 +24,6 @@ function _civicrm_api3_invoicegen_Generate_spec(&$spec) {
* @throws API_Exception
*/
function civicrm_api3_invoicegen_Generate($params) {
if (array_key_exists('magicword', $params) && $params['magicword'] == 'sesame') {
$returnValues = array(
// OK, return several data rows
12 => array('id' => 12, 'name' => 'Twelve'),
34 => array('id' => 34, 'name' => 'Thirty four'),
56 => array('id' => 56, 'name' => 'Fifty six'),
);
// ALTERNATIVE: $returnValues = array(); // OK, success
// ALTERNATIVE: $returnValues = array("Some value"); // OK, return a single value
// Spec: civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL)
return civicrm_api3_create_success($returnValues, $params, 'NewEntity', 'NewAction');
}
else {
throw new API_Exception(/*errorMessage*/ 'Everyone knows that the magicword is "sesame"', /*errorCode*/ 1234);
}
$returnValues = CRM_Tbusainvoicegen_Timebank::generate($params['contact_id'], $params['billing_period']);
return civicrm_api3_create_success($returnValues, $params, 'Invoicegen', 'generate');
}