namespace App\Models; use Config\Database; use PDO; class Form107 { public int $id; public int $worker_id; public bool $aplica; public ?string $filename; public string $created_at; public function __construct(array $data) { $this->id = (int) ($data['id'] ?? 0); $this->worker_id = (int) $data['worker_id']; $this->aplica = (bool) $data['aplica']; $this->filename = $data['filename'] ?? null; $this->created_at = $data['created_at'] ?? date('Y-m-d H:i:s'); } /** * Obtiene todos los registros de formulario 107. * * @return Form107[] */ public static function findAll(): array { $db = Database::getInstance()->getConnection(); $stmt = $db->query('SELECT * FROM form_107 ORDER BY created_at DESC'); $list = []; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $list[] = new self($row); } return $list; } /** * Obtiene un registro por ID. */ public static function findById(int $id): ?self { $db = Database::getInstance()->getConnection(); $stmt = $db->prepare('SELECT * FROM form_107 WHERE id = :id'); $stmt->execute([':id' => $id]); $data = $stmt->fetch(PDO::FETCH_ASSOC); return $data ? new self($data) : null; } /** * Obtiene todos los registros para un trabajador. * * @param int $workerId * @return Form107[] */ public static function findByWorker(int $workerId): array { $db = Database::getInstance()->getConnection(); $stmt = $db->prepare('SELECT * FROM form_107 WHERE worker_id = :w ORDER BY created_at DESC'); $stmt->execute([':w' => $workerId]); $list = []; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $list[] = new self($row); } return $list; } /** * Inserta un nuevo registro. */ public function create(): bool { $db = Database::getInstance()->getConnection(); $stmt = $db->prepare( 'INSERT INTO form_107 (worker_id, aplica, filename) VALUES (:w, :a, :f)' ); return $stmt->execute([ ':w' => $this->worker_id, ':a' => $this->aplica, ':f' => $this->filename ]); } /** * Actualiza un registro existente. */ public function update(): bool { $db = Database::getInstance()->getConnection(); $stmt = $db->prepare( 'UPDATE form_107 SET aplica = :a, filename = :f WHERE id = :id' ); return $stmt->execute([ ':a' => $this->aplica, ':f' => $this->filename, ':id' => $this->id ]); } /** * Elimina el registro. */ public function delete(): bool { $db = Database::getInstance()->getConnection(); $stmt = $db->prepare('DELETE FROM form_107 WHERE id = :id'); return $stmt->execute([':id' => $this->id]); } }