<?php
class PurchaseSetup {

    // insert header (expects sanitized/ready data)
    public static function AddPurchase($data) {
        global $db;
        return $db->query_insert(TABLE_PURCHASE, $data);
    }

    // insert a line item (expects purchase_id in $data)
    public static function AddPurchaseItem($data) {
        global $db;
        return $db->query_insert(TABLE_PURCHASE_ITEM, $data);
    }

    // basic updates
    public static function UpdatePurchase($id, $data) {
        global $db;
        return $db->query_update(TABLE_PURCHASE, $data, "id='$id'");
    }

    public static function UpdatePurchaseItem($id, $data) {
        global $db;
        return $db->query_update(TABLE_PURCHASE_ITEM, $data, "id='$id'");
    }

    // basic getters
    public static function GetPurchaseById($id) {
        global $db;
        $q = "SELECT * FROM ".TABLE_PURCHASE." WHERE id='$id'";
        $row = $db->query_first($q);
        return ($db->affected_rows > 0) ? $row : false;
    }

    public static function GetPurchaseItems($purchaseId) {
        global $db;
        $q = "SELECT * FROM ".TABLE_PURCHASE_ITEM." WHERE purchase_id='$purchaseId' ORDER BY id ASC";
        $rows = $db->fetch_all_array($q);
        return ($db->affected_rows > 0) ? $rows : false;
    }

    public static function GetPurchaseList($filter = "", $pageno = "null", $pagesize = "null") {
        global $db;
        $q = "SELECT * FROM ".TABLE_PURCHASE." ".$filter." ORDER BY date DESC, id DESC";
        $rows = $db->fetch_all_array($q, $pageno, $pagesize);
        return ($db->affected_rows > 0) ? $rows : false;
    }

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