<?php

class PurchaseOrder {

    /*************************************************
    / Add new Purchase Order (header only)
    *************************************************/
    public static function AddPurchaseOrder($data) {
        global $db;

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

    /*************************************************
    / Update Purchase Order (header only)
    *************************************************/
    public static function UpdatePurchaseOrder($pid, $data) {
        global $db;

        $id = $db->query_update(TABLE_PURCHASEORDER, $data, "id='$pid'");
        // Keep same semantics as your sample: return $id even if 0 rows changed
        if ($db->affected_rows > 0) {
            return $id;
        } else {
            return $id;
        }
    } // UpdatePurchaseOrder

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

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

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

    /*************************************************
    / Get by PO Code (prefix match, like your Customer code)
    *************************************************/
    public static function GetPurchaseOrderByCode($pcode) {
        global $db;

        $query = "SELECT * FROM " . TABLE_PURCHASEORDER . " WHERE po_code LIKE '$pcode%'";
        $rows  = $db->query_first($query);

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

    /*************************************************
    / Check duplicate PO code (exclude current id)
    *************************************************/
    public static function CheckPurchaseOrder($po_code, $id) {
        global $db;

        $query = "SELECT * FROM " . TABLE_PURCHASEORDER . " WHERE po_code = '$po_code' AND id <> '$id'";
        $rows  = $db->query_first($query);

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

    /*************************************************
    / List Purchase Orders (supports $filter, pagination)
    / $filter example: "WHERE status='issued' AND po_date BETWEEN '2025-01-01' AND '2025-12-31'"
    *************************************************/
    public static function GetPurchaseOrderList($filter = "", $pageno = "null", $pagesize = "null") {
        global $db;

        $query = "SELECT * FROM " . TABLE_PURCHASEORDER . " " . $filter . " ORDER BY id DESC";
        $rows  = $db->fetch_all_array($query, $pageno, $pagesize);

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

    /*************************************************
    / (Optional) Delete PO header (items will cascade)
    *************************************************/
    public static function DeletePurchaseOrder($id) {
        global $db;

        $db->query("DELETE FROM " . TABLE_PURCHASEORDER . " WHERE id = '$id'");
        return ($db->affected_rows > 0);
    } // DeletePurchaseOrder

    /*************************************************
    / Pagination helpers passthrough
    *************************************************/
    public static function GetNumRows() { global $db; return $db->GetNumRows(); }
    public static function GetNumberOfPages() { global $db; return $db->GetNumberOfPages(); }

}

?>
