Commit 92f507dc authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(subscriptions): fix getRate() returning truthy '0.00' for missing service codes

PHP's ?: operator treats '0.00' as truthy, preventing fallback to generic
service codes. Now returns empty string for missing/zero rows, with hard
fallback defaults as last resort.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 509d0559
...@@ -16,13 +16,13 @@ final class SubscriptionGenerator ...@@ -16,13 +16,13 @@ final class SubscriptionGenerator
$ts = date('Y-m-d H:i:s'); $ts = date('Y-m-d H:i:s');
$empId = $employee ? (int) $employee->id : null; $empId = $employee ? (int) $employee->id : null;
// Get year-specific rates (e.g. 2024/2025 -> suffix 2024) // Get year-specific rates (e.g. 2026/2027 -> suffix 2026), fall back to generic code
$yearParts = explode('/', $financialYear); $yearParts = explode('/', $financialYear);
$yearSuffix = $yearParts[0] ?? ''; $yearSuffix = $yearParts[0] ?? '';
$memberRate = self::getRate('SVC_ANNUAL_MEMBER_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_MEMBER'); $memberRate = self::getRate('SVC_ANNUAL_MEMBER_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_MEMBER') ?: '492.00';
$spouseRate = self::getRate('SVC_ANNUAL_SPOUSE_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_SPOUSE'); $spouseRate = self::getRate('SVC_ANNUAL_SPOUSE_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_SPOUSE') ?: '492.00';
$childRate = self::getRate('SVC_ANNUAL_CHILD_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_CHILD'); $childRate = self::getRate('SVC_ANNUAL_CHILD_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_CHILD') ?: '222.00';
$tempRate = self::getRate('SVC_ANNUAL_TEMP_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_TEMP'); $tempRate = self::getRate('SVC_ANNUAL_TEMP_' . $yearSuffix) ?: self::getRate('SVC_ANNUAL_TEMP') ?: '222.00';
$devFeeData = RuleEngine::get('DEVELOPMENT_FEE'); $devFeeData = RuleEngine::get('DEVELOPMENT_FEE');
$devFee = $devFeeData['amount'] ?? '35.00'; $devFee = $devFeeData['amount'] ?? '35.00';
...@@ -192,10 +192,14 @@ final class SubscriptionGenerator ...@@ -192,10 +192,14 @@ final class SubscriptionGenerator
private static function getRate(string $serviceCode): string private static function getRate(string $serviceCode): string
{ {
$db = App::getInstance()->db(); $db = App::getInstance()->db();
$today = date('Y-m-d');
$row = $db->selectOne( $row = $db->selectOne(
"SELECT base_amount FROM service_catalog WHERE service_code = ? AND is_active = 1 AND branch_id IS NULL ORDER BY effective_from DESC LIMIT 1", "SELECT base_amount FROM service_catalog WHERE service_code = ? AND is_active = 1 AND branch_id IS NULL AND effective_from <= ? AND (effective_to IS NULL OR effective_to >= ?) ORDER BY effective_from DESC LIMIT 1",
[$serviceCode] [$serviceCode, $today, $today]
); );
return $row['base_amount'] ?? '0.00'; if (!$row || !$row['base_amount'] || bccomp($row['base_amount'], '0', 2) <= 0) {
return '';
}
return $row['base_amount'];
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment