db = db_connect(); $this->ensureDoctorMasterTable(); } public function ensureDoctorMasterTable(): bool { if ($this->db->tableExists('doctor_master')) { $fields = $this->db->getFieldNames('doctor_master') ?? []; if (! in_array('app_pin', $fields, true)) { $this->db->query("ALTER TABLE doctor_master ADD COLUMN app_pin VARCHAR(255) NULL"); } if (! in_array('device_code', $fields, true)) { $this->db->query("ALTER TABLE doctor_master ADD COLUMN device_code VARCHAR(50) NULL"); } } return true; } public function getDoctors(): array { $builder = $this->db->table('doctor_master d'); if ($this->db->tableExists('user_profiles')) { $builder->select('d.*, u.id as linked_user_id, u.username as linked_username, u.active as linked_user_active') ->join('user_profiles up', 'up.doctor_id = d.id', 'left') ->join('users u', 'u.id = up.user_id AND u.deleted_at IS NULL', 'left'); } else { $builder->select('d.*'); } $list = $builder->get()->getResult(); foreach ($list as &$doc) { if (empty($doc->device_code)) { $newCode = (string) random_int(100000, 999999); $this->db->table('doctor_master')->where('id', $doc->id)->update(['device_code' => $newCode]); $doc->device_code = $newCode; } } unset($doc); return $list; } public function getDoctorById(int $id): array { $rows = $this->db->table('doctor_master')->where('id', $id)->get()->getResult(); if (! empty($rows) && empty($rows[0]->device_code)) { $newCode = (string) random_int(100000, 999999); $this->db->table('doctor_master')->where('id', $id)->update(['device_code' => $newCode]); $rows[0]->device_code = $newCode; } return $rows; } public function insert(array $data): int { $this->db->table('doctor_master')->insert($data); return (int) $this->db->insertID(); } public function updateDoctor(array $data, int $id): bool { return (bool) $this->db->table('doctor_master')->where('id', $id)->update($data); } public function insertSpec(array $data): int { $this->db->table('doc_spec')->insert($data); return (int) $this->db->insertID(); } public function removeSpec(int $id): bool { return (bool) $this->db->table('doc_spec')->where('id', $id)->delete(); } public function specExists(int $docId, int $specId): bool { return $this->db->table('doc_spec') ->where('doc_id', $docId) ->where('med_spec_id', $specId) ->countAllResults() > 0; } public function getDoctorSpecs(int $docId): array { return $this->db->table('med_spec m') ->select('m.SpecName, m.id, d.doc_id, d.med_spec_id, d.id as doc_spec_id') ->join('doc_spec d', 'm.id = d.med_spec_id') ->where('d.doc_id', $docId) ->get() ->getResult(); } public function getSpecsList(): array { return $this->db->table('med_spec')->orderBy('SpecName')->get()->getResult(); } public function getFeeTypes(): array { return $this->db->table('doc_fee_type')->orderBy('fee_type')->get()->getResult(); } public function getDoctorFees(int $docId): array { return $this->db->table('doc_fee_type f') ->select('d.id, f.fee_type, d.doc_fee_desc, d.amount') ->join('doc_opd_fee d', 'd.doc_fee_type = f.id AND d.doc_id = ' . (int) $docId, 'left') ->orderBy('f.id') ->get() ->getResult(); } public function getDoctorIpdFees(int $docId): array { return $this->db->table('doc_ipd_fee_type f') ->select('d.id, f.fee_type, d.doc_fee_desc, d.amount') ->join('doc_ipd_fee d', 'd.doc_fee_type = f.id AND d.doc_id = ' . (int) $docId, 'left') ->orderBy('f.id') ->get() ->getResult(); } public function getDoctorFeeByType(int $docId, int $feeType): ?object { return $this->db->table('doc_opd_fee') ->where('doc_id', $docId) ->where('doc_fee_type', $feeType) ->get() ->getRow(); } public function getIpdFeeTypes(): array { return $this->db->table('doc_ipd_fee_type')->orderBy('fee_type')->get()->getResult(); } public function getDoctorIpdFeeByType(int $docId, int $feeType): ?object { return $this->db->table('doc_ipd_fee') ->where('doc_id', $docId) ->where('doc_fee_type', $feeType) ->get() ->getRow(); } public function insertFee(array $data): int { $this->db->table('doc_opd_fee')->insert($data); return (int) $this->db->insertID(); } public function insertIpdFee(array $data): int { $this->db->table('doc_ipd_fee')->insert($data); return (int) $this->db->insertID(); } public function removeFee(int $id, array $auditData = []): bool { $row = $this->db->table('doc_opd_fee')->where('id', $id)->get()->getRowArray(); if ($row === null) { return false; } $row = array_merge($row, $auditData); $this->db->table('doc_opd_fee_update')->insert($row); return (bool) $this->db->table('doc_opd_fee')->where('id', $id)->delete(); } public function removeIpdFee(int $id): bool { return (bool) $this->db->table('doc_ipd_fee')->where('id', $id)->delete(); } public function insertIpdFeeType(string $name): int { $this->db->table('doc_ipd_fee_type')->insert(['fee_type' => $name]); return (int) $this->db->insertID(); } public function updateIpdFeeType(int $id, string $name): bool { return (bool) $this->db->table('doc_ipd_fee_type')->where('id', $id)->update(['fee_type' => $name]); } public function deleteIpdFeeType(int $id): bool { return (bool) $this->db->table('doc_ipd_fee_type')->where('id', $id)->delete(); } public function getMedSpecs(): array { return $this->db->table('med_spec')->orderBy('SpecName')->get()->getResult(); } public function insertMedSpec(string $name): int { $this->db->table('med_spec')->insert(['SpecName' => $name]); return (int) $this->db->insertID(); } public function updateMedSpec(int $id, string $name): bool { return (bool) $this->db->table('med_spec')->where('id', $id)->update(['SpecName' => $name]); } public function deleteMedSpec(int $id): bool { return (bool) $this->db->table('med_spec')->where('id', $id)->delete(); } }