Harden per-branch billing with seat caps and mid-cycle expansion.
Deploy Ladill Frontdesk / deploy (push) Successful in 55s

Enforce billed_branches when adding or reactivating branches, charge
wallet for extra seats mid-cycle, fix Settings for Enterprise, and
cover multi-branch checkout and renewal amounts in tests.
This commit is contained in:
isaacclad
2026-07-16 08:08:10 +00:00
parent bdbf572f19
commit 59433f2b7b
8 changed files with 449 additions and 24 deletions
+100 -16
View File
@@ -27,6 +27,10 @@ class ProController extends Controller
: null;
$planKey = $plans->planKey($organization);
$branchCount = max(1, $plans->activeBranchCount($organization));
$billedBranches = max(
$plans->billedBranches($organization),
$branchCount,
);
return view('frontdesk.pro.index', [
'organization' => $organization,
@@ -45,7 +49,7 @@ class ProController extends Controller
'prepaidMonths' => (array) config('frontdesk.prepaid_months', [6, 12, 24]),
'currency' => (string) config('billing.currency', 'GHS'),
'planExpiresAt' => $expiresAt,
'billedBranches' => (int) ($settings['billed_branches'] ?? $branchCount),
'billedBranches' => $billedBranches,
'salesUrl' => ladill_platform_url('contact-sales'),
]);
}
@@ -57,14 +61,13 @@ class ProController extends Controller
if ($plans->hasPaidPlan($organization)) {
return redirect()->route('frontdesk.pro.index')
->with('success', 'Your organization already has an active paid plan.');
->with('success', 'Your organization already has an active paid plan. Use "Add seats" on this page to expand your allotment.');
}
$validated = $request->validate([
'branches' => ['required', 'integer', 'min:1', 'max:999'],
]);
$branches = $plans->resolveCheckoutBranches($organization, (int) $validated['branches']);
$branches = $plans->resolveCheckoutBranches(
$organization,
$this->requestedBranches($request, $plans, $organization),
);
$amountMinor = $plans->priceForBranches('pro', $branches);
return $this->chargeMonthlyWallet(
@@ -87,7 +90,7 @@ class ProController extends Controller
if ($plans->hasPaidPlan($organization)) {
return redirect()->route('frontdesk.pro.index')
->with('success', 'Your organization already has an active paid plan.');
->with('success', 'Your organization already has an active paid plan. Use "Add seats" on this page to expand your allotment.');
}
if (! $plans->canSubscribeEnterprise($organization)) {
@@ -97,11 +100,10 @@ class ProController extends Controller
->with('error', "Enterprise requires at least {$min} active branch. Add a branch or contact sales.");
}
$validated = $request->validate([
'branches' => ['required', 'integer', 'min:1', 'max:999'],
]);
$branches = $plans->resolveCheckoutBranches($organization, (int) $validated['branches']);
$branches = $plans->resolveCheckoutBranches(
$organization,
$this->requestedBranches($request, $plans, $organization),
);
$amountMinor = $plans->priceForBranches('enterprise', $branches);
return $this->chargeMonthlyWallet(
@@ -123,13 +125,13 @@ class ProController extends Controller
$validated = $request->validate([
'plan' => ['required', 'in:pro,enterprise'],
'months' => ['required', 'integer', 'in:6,12,24'],
'branches' => ['required', 'integer', 'min:1', 'max:999'],
'branches' => ['nullable', 'integer', 'min:1', 'max:999'],
]);
$organization = $this->organization($request);
if ($plans->hasPaidPlan($organization)) {
return redirect()->route('frontdesk.pro.index')
->with('success', 'Your organization already has an active plan.');
->with('success', 'Your organization already has an active plan. Use "Add seats" on this page to expand your allotment.');
}
$plan = $validated['plan'];
@@ -142,7 +144,10 @@ class ProController extends Controller
->with('error', "Enterprise requires at least {$min} active branch.");
}
$branches = $plans->resolveCheckoutBranches($organization, (int) $validated['branches']);
$branches = $plans->resolveCheckoutBranches(
$organization,
(int) ($validated['branches'] ?? $plans->activeBranchCount($organization) ?: 1),
);
$amountMinor = $plans->priceForBranches($plan, $branches) * $months;
try {
@@ -171,6 +176,76 @@ class ProController extends Controller
return redirect()->away($url);
}
/**
* Mid-cycle expansion: charge for additional seats and raise billed_branches.
*/
public function expandBranches(Request $request, PlanService $plans, BillingClient $billing): RedirectResponse
{
$this->authorizeAbility($request, 'settings.manage');
$organization = $this->organization($request);
if (! $plans->hasPaidPlan($organization)) {
return redirect()->route('frontdesk.pro.index')
->with('error', 'Subscribe to a paid plan before adding branch seats.');
}
$validated = $request->validate([
'branches' => ['required', 'integer', 'min:1', 'max:999'],
]);
$plan = $plans->planKey($organization);
$currentBilled = max(
1,
$plans->billedBranches($organization),
$plans->activeBranchCount($organization),
);
$newTotal = max((int) $validated['branches'], $currentBilled);
if ($newTotal <= $currentBilled) {
return redirect()->route('frontdesk.pro.index')
->with('error', 'Choose a branch count higher than your current allotment ('.$currentBilled.').');
}
$extra = $newTotal - $currentBilled;
$rate = $plan === 'enterprise'
? $plans->enterprisePricePerBranchMinor()
: $plans->proPricePerBranchMinor();
$amountMinor = $extra * $rate;
$label = $plan === 'enterprise' ? 'Enterprise' : 'Pro';
$reference = 'frontdesk-'.$plan.'-expand-'.$organization->id.'-'.now()->format('Y-m-d-His');
try {
if (! $billing->canAfford($organization->owner_ref, $amountMinor)) {
return redirect()->route('frontdesk.pro.index')
->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to add branches.');
}
$charged = $billing->debit(
$organization->owner_ref,
$amountMinor,
'frontdesk_'.$plan.'_branch_expand',
$reference,
'Ladill Frontdesk '.$label.' — +'.$extra.' branch seat(s)',
);
} catch (\Throwable) {
return redirect()->route('frontdesk.pro.index')
->with('error', 'Could not process branch expansion. Please try again.');
}
if (! $charged) {
return redirect()->route('frontdesk.pro.index')
->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to add branches.');
}
$settings = $organization->settings ?? [];
$settings['billed_branches'] = $newTotal;
unset($settings['plan_renewal_error']);
$organization->update(['settings' => $settings]);
return redirect()->route('frontdesk.pro.index')
->with('success', "Branch allotment updated to {$newTotal}. Charged for {$extra} additional seat(s).");
}
public function paystackCallback(Request $request, BillingClient $billing, PlanService $plans): RedirectResponse
{
$reference = (string) $request->query('reference', '');
@@ -216,6 +291,15 @@ class ProController extends Controller
->with('success', "{$label} is active for {$months} months.");
}
private function requestedBranches(Request $request, PlanService $plans, Organization $organization): int
{
$validated = $request->validate([
'branches' => ['nullable', 'integer', 'min:1', 'max:999'],
]);
return (int) ($validated['branches'] ?? max(1, $plans->activeBranchCount($organization)));
}
private function chargeMonthlyWallet(
Organization $organization,
BillingClient $billing,