From d291ff57284cecb510fa0428ec1888e3a458c89e Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Wed, 11 Sep 2019 14:08:22 -0400 Subject: [PATCH] add invoicedata functions --- CRM/Tbusainvoicegen/Timebank.php | 68 ++++++++++++++++++++++++++++--- api/v3/Invoicegen/Generate.php | 2 +- api/v3/Invoicegen/Invoicedata.php | 29 +++++++++++++ 3 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 api/v3/Invoicegen/Invoicedata.php diff --git a/CRM/Tbusainvoicegen/Timebank.php b/CRM/Tbusainvoicegen/Timebank.php index 487bb8e..9abbd15 100644 --- a/CRM/Tbusainvoicegen/Timebank.php +++ b/CRM/Tbusainvoicegen/Timebank.php @@ -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); + } + } + diff --git a/api/v3/Invoicegen/Generate.php b/api/v3/Invoicegen/Generate.php index 33550c8..e0ee0fa 100644 --- a/api/v3/Invoicegen/Generate.php +++ b/api/v3/Invoicegen/Generate.php @@ -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; } diff --git a/api/v3/Invoicegen/Invoicedata.php b/api/v3/Invoicegen/Invoicedata.php new file mode 100644 index 0000000..5b0d341 --- /dev/null +++ b/api/v3/Invoicegen/Invoicedata.php @@ -0,0 +1,29 @@ +