Deploy Ladill Care / deploy (push) Failing after 13s
Healthcare management app: patients, appointments, consultations, lab, pharmacy inventory, billing, and reports at care.ladill.com. Co-authored-by: Cursor <cursoragent@cursor.com>
177 lines
6.0 KiB
PHP
177 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Care;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
|
|
use App\Models\Bill;
|
|
use App\Models\Visit;
|
|
use App\Services\Care\BillService;
|
|
use App\Services\Care\OrganizationResolver;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class BillController extends Controller
|
|
{
|
|
use ScopesToAccount;
|
|
|
|
public function __construct(
|
|
protected BillService $bills,
|
|
) {}
|
|
|
|
public function index(Request $request): View
|
|
{
|
|
$this->authorizeAbility($request, 'bills.view');
|
|
$organization = $this->organization($request);
|
|
$branchScope = app(OrganizationResolver::class)->branchScope($this->member($request));
|
|
|
|
$bills = $this->bills->list(
|
|
$this->ownerRef($request),
|
|
$organization->id,
|
|
$request->only(['status', 'patient_id']),
|
|
$branchScope,
|
|
);
|
|
|
|
return view('care.bills.index', [
|
|
'organization' => $organization,
|
|
'bills' => $bills,
|
|
'statuses' => config('care.bill_statuses'),
|
|
]);
|
|
}
|
|
|
|
public function generate(Request $request, Visit $visit): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'bills.manage');
|
|
$this->authorizeVisit($request, $visit);
|
|
|
|
$bill = $this->bills->generateFromVisit($visit, $this->ownerRef($request), $this->ownerRef($request));
|
|
|
|
return redirect()->route('care.bills.show', $bill)
|
|
->with('success', 'Bill generated from visit.');
|
|
}
|
|
|
|
public function show(Request $request, Bill $bill): View
|
|
{
|
|
$this->authorizeAbility($request, 'bills.view');
|
|
$this->authorizeBill($request, $bill);
|
|
|
|
$bill->load(['patient', 'branch', 'visit', 'lineItems', 'payments']);
|
|
|
|
$canManage = app(\App\Services\Care\CarePermissions::class)
|
|
->can($this->member($request), 'bills.manage');
|
|
$canPay = app(\App\Services\Care\CarePermissions::class)
|
|
->can($this->member($request), 'payments.manage');
|
|
|
|
return view('care.bills.show', [
|
|
'bill' => $bill,
|
|
'statuses' => config('care.bill_statuses'),
|
|
'lineTypes' => config('care.bill_line_types'),
|
|
'paymentMethods' => config('care.payment_methods'),
|
|
'canManage' => $canManage,
|
|
'canPay' => $canPay,
|
|
]);
|
|
}
|
|
|
|
public function addLineItem(Request $request, Bill $bill): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'bills.manage');
|
|
$this->authorizeBill($request, $bill);
|
|
|
|
$this->bills->addManualLineItem(
|
|
$bill,
|
|
$this->ownerRef($request),
|
|
$request->validate([
|
|
'type' => ['required', 'string', 'in:'.implode(',', array_keys(config('care.bill_line_types')))],
|
|
'description' => ['required', 'string', 'max:255'],
|
|
'quantity' => ['required', 'integer', 'min:1'],
|
|
'unit_price_minor' => ['required', 'integer', 'min:0'],
|
|
]),
|
|
$this->ownerRef($request),
|
|
);
|
|
|
|
return back()->with('success', 'Line item added.');
|
|
}
|
|
|
|
public function applyAdjustments(Request $request, Bill $bill): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'bills.manage');
|
|
$this->authorizeBill($request, $bill);
|
|
|
|
$validated = $request->validate([
|
|
'discount_minor' => ['nullable', 'integer', 'min:0'],
|
|
'tax_minor' => ['nullable', 'integer', 'min:0'],
|
|
]);
|
|
|
|
$this->bills->applyAdjustments(
|
|
$bill,
|
|
$this->ownerRef($request),
|
|
(int) ($validated['discount_minor'] ?? 0),
|
|
(int) ($validated['tax_minor'] ?? 0),
|
|
$this->ownerRef($request),
|
|
);
|
|
|
|
return back()->with('success', 'Bill updated.');
|
|
}
|
|
|
|
public function recordPayment(Request $request, Bill $bill): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'payments.manage');
|
|
$this->authorizeBill($request, $bill);
|
|
|
|
$this->bills->recordPayment(
|
|
$bill,
|
|
$this->ownerRef($request),
|
|
$request->validate([
|
|
'amount_minor' => ['required', 'integer', 'min:1'],
|
|
'method' => ['required', 'string', 'in:'.implode(',', array_keys(config('care.payment_methods')))],
|
|
'reference' => ['nullable', 'string', 'max:100'],
|
|
'notes' => ['nullable', 'string', 'max:500'],
|
|
]),
|
|
$this->ownerRef($request),
|
|
);
|
|
|
|
return back()->with('success', 'Payment recorded.');
|
|
}
|
|
|
|
public function void(Request $request, Bill $bill): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'bills.manage');
|
|
$this->authorizeBill($request, $bill);
|
|
|
|
$this->bills->void($bill, $this->ownerRef($request), $this->ownerRef($request));
|
|
|
|
return redirect()->route('care.bills.index')->with('success', 'Bill voided.');
|
|
}
|
|
|
|
public function print(Request $request, Bill $bill): View
|
|
{
|
|
$this->authorizeAbility($request, 'bills.view');
|
|
$this->authorizeBill($request, $bill);
|
|
|
|
$bill->load(['patient', 'branch', 'lineItems', 'payments', 'organization']);
|
|
|
|
return view('care.bills.print', [
|
|
'bill' => $bill,
|
|
'organization' => $this->organization($request),
|
|
]);
|
|
}
|
|
|
|
protected function authorizeBill(Request $request, Bill $bill): void
|
|
{
|
|
$this->authorizeOwner($request, $bill);
|
|
abort_unless($bill->organization_id === $this->organization($request)->id, 404);
|
|
|
|
$branchId = app(OrganizationResolver::class)->branchScope($this->member($request));
|
|
if ($branchId !== null && $bill->branch_id !== $branchId) {
|
|
abort(404);
|
|
}
|
|
}
|
|
|
|
protected function authorizeVisit(Request $request, Visit $visit): void
|
|
{
|
|
$this->authorizeOwner($request, $visit);
|
|
abort_unless($visit->organization_id === $this->organization($request)->id, 404);
|
|
}
|
|
}
|