Calibration Log module while still allowing manual entries. * * @param \App\Models\WeldingEquipment|array $equipment * @return \App\Models\CalibrationLog */ function syncCalibrationFromWeldingEquipment($equipment): CalibrationLog { if (is_array($equipment)) { $data = $equipment; } else { $data = $equipment->toArray(); } $sourceId = $data['id'] ?? null; \Log::debug("Sync attempt for equipment ID: " . $sourceId); $log = CalibrationLog::firstOrNew([ 'source_module' => 'naks_welding_equipment', 'source_id' => $sourceId, ]); \Log::debug("CalibrationLog exists: " . ($log->exists ? "yes (id: {$log->id})" : "no, will create new")); $log->instrument = 'Welding Equipment'; $log->description = $data['type_of_welding_machine'] ?? null; $log->item_no = $log->item_no ?? null; $log->equipment_name = $data['brand'] ?? null; // brand -> equipment_name $log->manufacturer = $data['producer'] ?? null; // producer -> manufacturer $log->serial_no = $data['manufacturer_code'] ?? null; // manufacturer_code -> serial_no $log->passport_certificate_no = $data['attestation'] ?? null; $log->quantity = 1; // Always set quantity to 1 // Use date_of_issue as calibration_date and valid_until as calibration_due_date if (!empty($data['date_of_issue'])) { $log->calibration_date = $data['date_of_issue']; } if (!empty($data['valid_until'])) { $log->calibration_due_date = $data['valid_until']; } // Status calculation based on due date if (!empty($log->calibration_due_date)) { $today = Carbon::today(); $dueDate = Carbon::parse($log->calibration_due_date)->startOfDay(); if ($dueDate->lt($today)) { $log->status = 'Out Of service'; // Expired -> Out Of service } elseif ($dueDate->lte($today->copy()->addDays(20))) { $log->status = 'Calibration-Verification'; // Due Soon -> needs calibration } else { $log->status = 'In use'; // Valid -> In use } } try { $result = $log->save(); \Log::debug("Save result: " . ($result ? "success, new id: {$log->id}" : "failed")); } catch (\Exception $e) { \Log::error("Save exception: " . $e->getMessage()); throw $e; } return $log; } }