Files
ladill-care/app/Services/Care/PatientNumberGenerator.php
T
isaaccladandCursor 6c9c742ed8
Deploy Ladill Care / deploy (push) Failing after 13s
Initial Ladill Care release.
Healthcare management app: patients, appointments, consultations, lab,
pharmacy inventory, billing, and reports at care.ladill.com.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 11:36:22 +00:00

33 lines
969 B
PHP

<?php
namespace App\Services\Care;
use App\Models\Organization;
use App\Models\Patient;
use Illuminate\Support\Facades\DB;
class PatientNumberGenerator
{
public function generate(Organization $organization): string
{
$prefix = config('care.patient_number.prefix', 'LC');
$year = now()->format('Y');
return DB::transaction(function () use ($organization, $prefix, $year) {
$latest = Patient::withTrashed()
->where('organization_id', $organization->id)
->where('patient_number', 'like', "{$prefix}-{$year}-%")
->orderByDesc('patient_number')
->lockForUpdate()
->value('patient_number');
$sequence = 1;
if ($latest && preg_match('/-(\d+)$/', $latest, $matches)) {
$sequence = (int) $matches[1] + 1;
}
return sprintf('%s-%s-%05d', $prefix, $year, $sequence);
});
}
}