<?php

class PaidToSetup {

    /*************************************************
    / Add a new Paid-To record
    *************************************************/
    public static function AddPaidToSetup($data) {
        global $db;

        $id = $db->query_insert(TABLE_PAIDTOSETUP, $data);
        if ($db->affected_rows > 0) {
            return $id;
        } else {
            return false;
        }
    } // AddPaidToSetup

    /*************************************************
    / Update a Paid-To record
    *************************************************/
    public static function UpdatePaidToSetup($id, $data) {
        global $db;

        $res = $db->query_update(TABLE_PAIDTOSETUP, $data, "id='$id'");
        // Follow existing convention (like UpdateBankSetup): return id either way
        return $id;
    } // UpdatePaidToSetup

    /*************************************************
    / Delete a Paid-To record
    *************************************************/
    public static function DeletePaidToSetup($id) {
        global $db;

        $db->query_delete(TABLE_PAIDTOSETUP, "id='$id'");
        return ($db->affected_rows > 0);
    } // DeletePaidToSetup

    /*************************************************
    / Get single record by ID
    *************************************************/
    public static function GetPaidToSetupById($id) {
        global $db;

        $query = "SELECT * FROM " . TABLE_PAIDTOSETUP . " WHERE id = '$id'";
        $row = $db->query_first($query);

        if ($db->affected_rows > 0) {
            return $row;
        } else {
            return false;
        }
    } // GetPaidToSetupById

    /*************************************************
    / Check duplicate name (exclude current id)
    *************************************************/
    public static function CheckPaidToName($paid_to_name, $exclude_id = 0) {
        global $db;

        $paid_to_name = $db->escape($paid_to_name);
        $exclude_id = (int)$exclude_id;

        $query = "SELECT * FROM " . TABLE_PAIDTOSETUP . " 
                  WHERE paid_to_name = '$paid_to_name' " . 
                  ($exclude_id > 0 ? "AND id <> '$exclude_id'" : "");
        $row = $db->query_first($query);

        if ($db->affected_rows > 0) {
            return $row; // duplicate found
        } else {
            return false; // no duplicate
        }
    } // CheckPaidToName

    /**********************************************************
    / Get list with optional filter + pagination
    / $filter example: " WHERE status='active' ORDER BY paid_to_name"
    **********************************************************/
    public static function GetPaidToSetupList($filter = "", $pageno = "null", $pagesize = "null") {
        global $db;

        $query = "SELECT * FROM " . TABLE_PAIDTOSETUP . " " . $filter;
        $rows = $db->fetch_all_array($query, $pageno, $pagesize);

        if ($db->affected_rows > 0) {
            return $rows;
        } else {
            return false;
        }
    } // GetPaidToSetupList

    public static function GetNumRows() { global $db; return $db->GetNumRows(); }
    public static function GetNumberOfPages() { global $db; return $db->GetNumberOfPages(); }

}
