add invoicedata functions

This commit is contained in:
Jon Goldberg 2019-09-11 14:08:22 -04:00
parent e4afac16a5
commit d291ff5728
No known key found for this signature in database
GPG Key ID: C2D2247364F9DB13
3 changed files with 92 additions and 7 deletions

View File

@ -159,7 +159,7 @@ class CRM_Tbusainvoicegen_Timebank {
private function monthsProRated() {
// By default, we bill all 6 months in a billing period.
$monthsProRated = 6;
$beginDate = $this->calculatePeriodBeginDate();
$beginDate = $this->calculatePeriodBeginDate(self::$billingPeriod);
$firstAnniversary = $this->creationDate->modify("+1 year");
$interval = $beginDate->diff($firstAnniversary);
// First anniversary is before the billing period.
@ -181,13 +181,14 @@ class CRM_Tbusainvoicegen_Timebank {
if (!$this->price) {
return;
}
$dueDate = $this->calculateDueDate();
$dueDate = $this->calculateDueDate(self::$billingPeriod);
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',
'is_pay_later' => 1,
self::BILLINGPERIODFIELD => self::$billingPeriod,
]);
}
@ -196,8 +197,8 @@ class CRM_Tbusainvoicegen_Timebank {
* Returns the due date as a string.
* @return string
*/
private function calculateDueDate() {
list($year, $number) = explode('-', self::$billingPeriod);
public static function calculateDueDate($billingPeriod) {
list($year, $number) = explode('-', $billingPeriod);
if ($number == 1) {
$dueDate = $year . '-03-31';
}
@ -212,8 +213,8 @@ class CRM_Tbusainvoicegen_Timebank {
* This is to calculate the "new timebank" pro-rated value.
* @return Date
*/
private function calculatePeriodBeginDate() {
list($year, $number) = explode('-', self::$billingPeriod);
private static function calculatePeriodBeginDate($billingPeriod) {
list($year, $number) = explode('-', $billingPeriod);
if ($number == 1) {
$beginDate = new DateTime($year . '-01-01');
}
@ -223,4 +224,59 @@ class CRM_Tbusainvoicegen_Timebank {
return $beginDate;
}
public static function calculatePeriod($billingPeriod) {
$beginDate = self::calculatePeriodBeginDate($billingPeriod);
$endDate = clone $beginDate;
$endDate->add(new DateInterval('P6M'))->sub(new DateInterval('P1D'));
$period = $beginDate->format('n/j/Y') . '-' . $endDate->format('n/j/Y');
return $period;
}
/**
* This is a helper function to generate values for the custom invoice template.
* It takes a billing period (e.g. "2019-2") and returns an array $invoiceData.
* $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[2] is the total amount this contact owes.
* @param str $billingPeriod
*/
public static function invoiceData($contactId, $billingPeriod) {
$invoiceData[0] = self::calculateDueDate($billingPeriod);
$invoiceData[1] = self::calculatePeriod($billingPeriod);
$invoiceData[2] = self::calculateTotalDue($contactId);
return $invoiceData;
}
public static function calculateTotalDue($contactId) {
// Get all contributions that aren't paid, sum up their total amounts.
// Then get all those contributions' payments, sum up THEIR total amounts.
// Subtract the second number from the first.
$totalDue = 0;
$contributions = civicrm_api3('Contribution', 'get', [
'sequential' => 1,
'return' => ["total_amount"],
'contact_id' => $contactId,
'contribution_status_id' => ['!=' => "Completed"],
'options' => ['limit' => 0],
]);
if ($contributions['count']) {
foreach ($contributions['values'] as $contribution) {
$totalDue += $contribution['total_amount'];
$contributionIds[] = $contribution['id'];
}
}
$payments = civicrm_api3('Payment', 'get', [
'sequential' => 1,
'contribution_id' => ['IN' => $contributionIds],
'options' => ['limit' => 0],
]);
if ($payments['count']) {
foreach ($payments['values'] as $payment) {
$totalDue -= $payment['total_amount'];
}
}
return round($totalDue, 2);
}
}

View File

@ -10,7 +10,7 @@ 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['contact_id']['api.required'] = 0;
$spec['contact_id']['api.required'] = 1;
$spec['billing_period']['api.required'] = 1;
}

View File

@ -0,0 +1,29 @@
<?php
use CRM_Tbusainvoicegen_ExtensionUtil as E;
/**
* Invoicegen.Generate API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
* @return void
* @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
*/
function _civicrm_api3_invoicegen_Invoicedata_spec(&$spec) {
$spec['contact_id']['api.required'] = 1;
$spec['billing_period']['api.required'] = 1;
}
/**
* Invoicegen.Generate API
*
* @param array $params
* @return array API result descriptor
* @see civicrm_api3_create_success
* @see civicrm_api3_create_error
* @throws API_Exception
*/
function civicrm_api3_invoicegen_Invoicedata($params) {
$returnValues = CRM_Tbusainvoicegen_Timebank::invoiceData($params['contact_id'], $params['billing_period']);
return civicrm_api3_create_success($returnValues, $params, 'Invoicegen', 'invoicedata');
}