<?php

class PurchaseOrderItem {

    /*************************************************
    / Add single item row
    *************************************************/
    public static function AddPurchaseOrderItem($data) {
        global $db;

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

    /*************************************************
    / Update single item row
    *************************************************/
    public static function UpdatePurchaseOrderItem($pid, $data) {
        global $db;

        $id = $db->query_update(TABLE_PURCHASEORDERITEM, $data, "id='$pid'");
        if ($db->affected_rows > 0) {
            return $id;
        } else {
            return $id;
        }
    } // UpdatePurchaseOrderItem

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

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

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

    /*************************************************
    / Get all items for a PO (ordered by creation)
    *************************************************/
    public static function GetPurchaseOrderItemsByPO($po_id) {
        global $db;

        $query = "SELECT * FROM " . TABLE_PURCHASEORDERITEM . " WHERE po_id = '$po_id' ORDER BY id ASC";
        $rows  = $db->fetch_all_array($query);

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

    /*************************************************
    / Delete all items for a PO (useful on re-save)
    *************************************************/
    public static function DeleteItemsByPO($po_id) {
        global $db;

        $db->query("DELETE FROM " . TABLE_PURCHASEORDERITEM . " WHERE po_id = '$po_id'");
        return ($db->affected_rows > 0);
    } // DeleteItemsByPO

    /*************************************************
    / Delete one item
    *************************************************/
    public static function DeletePurchaseOrderItem($id) {
        global $db;

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

}

?>
