<?php
class InvoiceStockUsage {

  public static function Add($data) {
    global $db;
    $id = $db->query_insert(TABLE_INVOICE_STOCK_USAGE, $data);
    return ($db->affected_rows > 0) ? $id : false;
  }

  public static function GetByInvoice($invoice_id) {
    global $db;
    $invoice_id = (int)$invoice_id;
    $sql = "SELECT * FROM ".TABLE_INVOICE_STOCK_USAGE." WHERE invoice_id='$invoice_id' ORDER BY id ASC";
    $rows = $db->fetch_all_array($sql);
    return ($db->affected_rows > 0) ? $rows : false;
  }

  public static function GetByInvoiceItem($invoice_id, $invoice_item_id) {
    global $db;
    $invoice_id = (int)$invoice_id; $invoice_item_id = (int)$invoice_item_id;
    $sql = "SELECT * FROM ".TABLE_INVOICE_STOCK_USAGE." WHERE invoice_id='$invoice_id' AND invoice_item_id='$invoice_item_id' ORDER BY id ASC";
    $rows = $db->fetch_all_array($sql);
    return ($db->affected_rows > 0) ? $rows : false;
  }

  public static function DeleteByInvoice($invoice_id) {
    global $db;
    $invoice_id = (int)$invoice_id;
    return $db->query("DELETE FROM ".TABLE_INVOICE_STOCK_USAGE." WHERE invoice_id='$invoice_id'");
  }

  public static function DeleteByInvoiceItem($invoice_id, $invoice_item_id) {
    global $db;
    $invoice_id = (int)$invoice_id; $invoice_item_id = (int)$invoice_item_id;
    return $db->query("DELETE FROM ".TABLE_INVOICE_STOCK_USAGE." WHERE invoice_id='$invoice_id' AND invoice_item_id='$invoice_item_id'");
  }
}
