<?php
class Planilla
{
    public $correlativo;
    public $codtipoplanilla;
    public $descripcion;

    public function __construct()
    {
    }

    public static function obtenerPorTipoPlanilla(int $codtipoplanilla, int $anio = 0): object
    {
        $db = obtenerConexion();

        $query = "SELECT * FROM rrhh.view_planillas WHERE codtipoplanilla = :codtipoplanilla";
        if ($anio > 0) $query .= " AND anio = :anio";

        $query .= " ORDER BY correlativo DESC";
        $stmt = $db->prepare($query, [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
        $stmt->bindParam(":codtipoplanilla", $codtipoplanilla, PDO::PARAM_INT);
        if ($anio > 0) $stmt->bindParam(":anio", $anio, PDO::PARAM_INT);

        try {
            $stmt->execute();

            $data = $stmt->fetchAll(PDO::FETCH_OBJ);

            return (object)[
                'status' => 200,
                'data' => $data
            ];
        } catch (PDOException $e) {
            return (object)[
                'status' => 500,
                'message' => $e->getMessage()
            ];
        }
    }
}
