|
|
@@ -5,6 +5,7 @@
|
|
|
use App\Models\StudentContract;
|
|
|
use App\Models\StudentContractInstallment;
|
|
|
use App\Models\StudentMedia;
|
|
|
+use App\Models\Unit;
|
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
@@ -62,6 +63,47 @@ public function findById(int $id): ?StudentContract
|
|
|
return $contract;
|
|
|
}
|
|
|
|
|
|
+ public function generateNextProtocol(): string
|
|
|
+ {
|
|
|
+ $nextNumber = $this->getMaxProtocolNumber() + 1;
|
|
|
+
|
|
|
+ return str_pad((string) $nextNumber, 6, '0', STR_PAD_LEFT);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getMaxProtocolNumber(): int
|
|
|
+ {
|
|
|
+ $driver = DB::connection()->getDriverName();
|
|
|
+
|
|
|
+ if ($driver === 'pgsql') {
|
|
|
+ $max = StudentContract::withTrashed()
|
|
|
+ ->whereNotNull('protocol')
|
|
|
+ ->whereRaw("protocol ~ '^[0-9]+$'")
|
|
|
+ ->selectRaw("COALESCE(MAX(CAST(protocol AS BIGINT)), 0) as max_protocol")
|
|
|
+ ->value('max_protocol');
|
|
|
+
|
|
|
+ return (int) $max;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($driver === 'mysql') {
|
|
|
+ $max = StudentContract::withTrashed()
|
|
|
+ ->whereNotNull('protocol')
|
|
|
+ ->whereRaw("protocol REGEXP '^[0-9]+$'")
|
|
|
+ ->selectRaw("COALESCE(MAX(CAST(protocol AS UNSIGNED)), 0) as max_protocol")
|
|
|
+ ->value('max_protocol');
|
|
|
+
|
|
|
+ return (int) $max;
|
|
|
+ }
|
|
|
+
|
|
|
+ $max = StudentContract::withTrashed()
|
|
|
+ ->whereNotNull('protocol')
|
|
|
+ ->get(['protocol'])
|
|
|
+ ->filter(fn ($contract) => ctype_digit((string) $contract->protocol))
|
|
|
+ ->map(fn ($contract) => (int) $contract->protocol)
|
|
|
+ ->max();
|
|
|
+
|
|
|
+ return (int) ($max ?? 0);
|
|
|
+ }
|
|
|
+
|
|
|
public function create(array $data): StudentContract
|
|
|
{
|
|
|
if (! empty($data['due_day'])) {
|
|
|
@@ -70,6 +112,20 @@ public function create(array $data): StudentContract
|
|
|
|
|
|
unset($data['due_day']);
|
|
|
|
|
|
+ $unitId = $data['unit_id'] ?? null;
|
|
|
+ $isAutomatic = true;
|
|
|
+
|
|
|
+ if ($unitId) {
|
|
|
+ $unit = Unit::find($unitId);
|
|
|
+ if ($unit && $unit->automatic_protocol === false) {
|
|
|
+ $isAutomatic = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($isAutomatic) {
|
|
|
+ $data['protocol'] = $this->generateNextProtocol();
|
|
|
+ }
|
|
|
+
|
|
|
$contract = StudentContract::create($data);
|
|
|
|
|
|
$this->generateInstallments($contract);
|