Files
ladill-care/app/Services/Care/PlanService.php
T
isaaccladandCursor 22646dfb62
Deploy Ladill Care / deploy (push) Successful in 1m46s
Add Care Enterprise per-branch billing and Paystack prepaid plans.
Mirrors Queue with GHS 499/branch Enterprise and 6/12/24 month Paystack checkout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-30 01:55:38 +00:00

89 lines
2.5 KiB
PHP

<?php
namespace App\Services\Care;
use App\Models\Branch;
use App\Models\Organization;
use Carbon\Carbon;
class PlanService
{
public function planKey(Organization $organization): string
{
$settings = $organization->settings ?? [];
$plan = (string) ($settings['plan'] ?? 'free');
if (in_array($plan, ['pro', 'enterprise'], true) && ! empty($settings['plan_expires_at'])) {
if (Carbon::parse($settings['plan_expires_at'])->isPast()) {
return 'free';
}
}
return array_key_exists($plan, config('care.plans', [])) ? $plan : 'free';
}
public function plan(Organization $organization): string
{
return $this->planKey($organization);
}
public function isPro(Organization $organization): bool
{
return $this->planKey($organization) === 'pro';
}
public function isEnterprise(Organization $organization): bool
{
return $this->planKey($organization) === 'enterprise';
}
public function hasPaidPlan(Organization $organization): bool
{
return in_array($this->planKey($organization), ['pro', 'enterprise'], true);
}
public function activeBranchCount(Organization $organization): int
{
return Branch::query()
->where('organization_id', $organization->id)
->where('is_active', true)
->count();
}
public function maxBranches(Organization $organization): ?int
{
$plan = config('care.plans.'.$this->planKey($organization));
return $plan['max_branches'] ?? null;
}
public function canAddBranch(Organization $organization, int $currentCount): bool
{
$max = $this->maxBranches($organization);
return $max === null || $currentCount < $max;
}
public function proPriceMinor(): int
{
return (int) config('care.plans.pro.price_minor', 19900);
}
public function enterprisePricePerBranchMinor(): int
{
return (int) config('care.plans.enterprise.price_minor_per_branch', 49900);
}
public function enterprisePriceMinor(Organization $organization): int
{
$branches = max(1, $this->activeBranchCount($organization));
return $branches * $this->enterprisePricePerBranchMinor();
}
public function canSubscribeEnterprise(Organization $organization): bool
{
return $this->activeBranchCount($organization) >= (int) config('care.enterprise.min_branches', 2);
}
}