Skip to content

Commit

Permalink
MMB-501: Add insurance tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Shahrukh committed Nov 13, 2024
1 parent 2ece640 commit ef419ab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
56 changes: 54 additions & 2 deletions CRM/Certificate/Token/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static function entityTokens() {
'{membership.join_date}' => ts('Member Since'),
'{membership.end_date}' => ts('Membership Expiration Date'),
'{membership.fee}' => ts('Membership Fee'),
'{membership.insurance_premium_ex_ipt}' => ts('Insurance Premium (excl IPT)'),
'{membership.insurance_premium_ipt_only}' => ts(' Insurance Premium (IPT)'),
'{membership.insurance_premium_inc_ipt}' => ts('Insurance Premium (incl IPT)'),
];
$extraTokens = [
'source' => ts('Membership Source'),
Expand All @@ -56,14 +59,18 @@ public static function entityTokens() {
public function prefetch(TokenValueEvent $e) {
$entityTypeId = $e->getTokenProcessor()->getContextValues('entityId');
$contactId = $e->getTokenProcessor()->getContextValues('contactId');
$certificateId = $e->getTokenProcessor()->getContextValues('certificateId');

$resolvedTokens = [];

try {
if (is_array($entityTypeId)) {
$entityTypeId = $entityTypeId[0];
$contactId = $contactId[0];
$result = $this->getMembership($entityTypeId, $contactId);
$certificateId = $certificateId[0] ?? 0;
$certificate = $certificateId > 0 ?
CRM_Certificate_BAO_CompuCertificate::findById($certificateId) : new stdClass();
$result = $this->getMembership($entityTypeId, $contactId, $certificate);
if (empty($result)) {
return $resolvedTokens;
}
Expand Down Expand Up @@ -108,10 +115,11 @@ private function resolveFields($membership, &$resolvedTokens) {
*
* @param int $membershipId
* @param int $contactId
* @param object $certificate
*
* @return array
*/
private function getMembership($membershipId, $contactId) {
private function getMembership($membershipId, $contactId, $certificate) {
$result = civicrm_api3('Membership', 'getsingle', [
'id' => $membershipId,
'contact_id' => $contactId,
Expand All @@ -126,8 +134,52 @@ private function getMembership($membershipId, $contactId) {
$result['status_idlabel'] = $status['membership_status'] ?? '';
$result['membership_type_idlabel'] = $type['name'] ?? '';
$result['fee'] = CRM_Utils_Money::format($type['minimum_fee'] ?? '');
$insuranceTokens = ['insurance_premium_ex_ipt', 'insurance_premium_inc_ipt', 'insurance_premium_ipt_only'];

if (empty(array_intersect($this->activeTokens, $insuranceTokens))) {
return $result;
}

$insuranceLineItem = $this->getValidInsuranceLineItem($certificate, $result);

$result['insurance_premium_ex_ipt'] = !empty($insuranceLineItem['lineTotal']) ?
CRM_Utils_Money::format($insuranceLineItem['lineTotal']) : '';
$result['insurance_premium_ipt_only'] = !empty($insuranceLineItem['taxAmount']) ?
CRM_Utils_Money::format($insuranceLineItem['taxAmount']) : '';
$result['insurance_premium_inc_ipt'] = !empty($insuranceLineItem['taxAmount']) && !empty($insuranceLineItem['lineTotal']) ?
CRM_Utils_Money::format((float) $insuranceLineItem['taxAmount'] + (float) $insuranceLineItem['lineTotal']) : '';

return $result;
}

private function getValidInsuranceLineItem($certificate, $membership): array {
$insuranceFinancialType = (int) Civi::settings()->get('insurance_premium_financial_type');
$query = "
SELECT MAX(c.id) FROM civicrm_contribution c
INNER JOIN civicrm_membership_payment mp ON c.id = mp.contribution_id
WHERE mp.membership_id =" . $membership['id'] ?? 0;

if (!empty($certificate->min_valid_from_date)) {
$query .= " AND c.receive_date >= '" . $certificate->min_valid_from_date . "'";
}
if (!empty($certificate->max_valid_through_date)) {
$query .= " AND c.receive_date <= '" . $certificate->max_valid_through_date . "'";
}

$contribution = CRM_Core_DAO::singleValueQuery($query);

if ($contribution === NULL || $insuranceFinancialType === 0) {
return [];
}

$lineItem = Civi\Api4\LineItem::get(FALSE)
->addSelect('SUM(tax_amount) AS taxAmount', 'SUM(line_total) AS lineTotal')
->addWhere('contribution_id', '=', $contribution)
->addWhere('financial_type_id', '=', $insuranceFinancialType)
->execute()
->first();

return $lineItem ?? [];
}

}
1 change: 1 addition & 0 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</civix>
<requires>
<ext>uk.co.compucorp.civicase</ext>
<ext>io.compuco.financeextras</ext>
</requires>
<mixins>
<mixin>[email protected]</mixin>
Expand Down

0 comments on commit ef419ab

Please sign in to comment.