122 lines
3.6 KiB
PHP
122 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\AvailabilityModel;
|
|
|
|
class AvailabilityController extends BaseController
|
|
{
|
|
public function save()
|
|
{
|
|
$doctorId = $this->request->getPost('doctor_id');
|
|
$availabilityJson = $this->request->getPost('availability_json');
|
|
|
|
// Validation
|
|
if (!$doctorId || !$availabilityJson) {
|
|
return redirect()->back()
|
|
->with('error', 'Doctor ID or availability data missing');
|
|
}
|
|
|
|
$availability = json_decode($availabilityJson, true);
|
|
foreach ($availability as $day => $slots) {
|
|
foreach ($slots as $slot) {
|
|
|
|
$start = $slot['start'];
|
|
$end = $slot['end'];
|
|
|
|
// Start >= End
|
|
if ($start >= $end) {
|
|
return redirect()->back()->with('error', 'Invalid time slot: Start must be before End');
|
|
}
|
|
|
|
// Less than 1 hour
|
|
$startTime = strtotime($start);
|
|
$endTime = strtotime($end);
|
|
|
|
if (($endTime - $startTime) < 3600) {
|
|
return redirect()->back()->with('error', 'Minimum slot must be 1 hour');
|
|
}
|
|
}
|
|
}
|
|
// VALIDATION END
|
|
|
|
|
|
if (!is_array($availability) || empty($availability)) {
|
|
return redirect()->back()
|
|
->with('error', 'No availability slots provided');
|
|
}
|
|
|
|
$model = new AvailabilityModel();
|
|
|
|
// Deactivate current active schedule for this doctor
|
|
$deactivated = $model->where('doctor_id', $doctorId)
|
|
->where('status', 1)
|
|
->set(['status' => 0])
|
|
->update();
|
|
|
|
log_message('info', "Deactivated {$deactivated} old availability records for doctor {$doctorId}");
|
|
|
|
// Generate ONE batch id for this save
|
|
$batchId = strtoupper(bin2hex(random_bytes(16)));
|
|
|
|
$insertedCount = 0;
|
|
|
|
// Insert new active schedule
|
|
foreach ($availability as $day => $slots) {
|
|
foreach ($slots as $slot) {
|
|
$inserted = $model->insert([
|
|
'doctor_id' => $doctorId,
|
|
'batch_id' => $batchId,
|
|
'day_number' => (int) $day,
|
|
'start_time' => $slot['start'],
|
|
'end_time' => $slot['end'],
|
|
'status' => 1
|
|
]);
|
|
|
|
if ($inserted) {
|
|
$insertedCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
log_message('info', "Inserted {$insertedCount} new availability records for doctor {$doctorId}");
|
|
|
|
if ($insertedCount === 0) {
|
|
return redirect()->back()
|
|
->with('warning', 'No availability slots were saved. Please try again.');
|
|
}
|
|
|
|
return redirect()->back()
|
|
->with('success', "Availability updated ({$insertedCount} slots saved)");
|
|
}
|
|
public function getAvailability($doctorId)
|
|
{
|
|
$model = new AvailabilityModel();
|
|
|
|
$slots = $model->where('doctor_id', $doctorId)
|
|
->where('status', 1)
|
|
->orderBy('day_number', 'ASC')
|
|
->orderBy('start_time', 'ASC')
|
|
->findAll();
|
|
|
|
return $this->response->setJSON($slots);
|
|
}
|
|
|
|
/**
|
|
* Diagnostic endpoint to verify availability data for all doctors
|
|
*/
|
|
public function diagnostic()
|
|
{
|
|
$model = new AvailabilityModel();
|
|
|
|
$allRecords = $model->where('status', 1)
|
|
->orderBy('doctor_id', 'ASC')
|
|
->orderBy('day_number', 'ASC')
|
|
->findAll();
|
|
|
|
return $this->response->setJSON([
|
|
'total_active_records' => count($allRecords),
|
|
'records' => $allRecords
|
|
]);
|
|
}
|
|
} |