<?php

class ProductionStock {

    /*************************************************
    / This function add new ProductionStock Setup
    *************************************************/
    public static function AddProductionStockSetup($data) {
        global $db;

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

    } // AddProductionStock

    /*************************************************
    / This function update ProductionStock
    *************************************************/
    public static function UpdateProductionStockSetup($pid, $data) {
        global $db;

        $id = $db->query_update(TABLE_BATCH_STOCK, $data, "id='$pid'");

        if ($db->affected_rows > 0) {
            return $id;
        } else {
            return $id;
        }

    } // UpdateProductionStock

    /*************************************************
    / Get Production Stock Setup by ID
    *************************************************/
    public static function GetProductionStockSetupById($id) {

        global $db;
        $query = "SELECT * from " . TABLE_BATCH_STOCK . " WHERE id ='$id'";
        $rows = $db->query_first($query);

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

    /*************************************************
    / This function will return ProductionStockSetup
    *************************************************/
    public static function GetProductionStockSetupList($filter = "", $pageno = "null", $pagesize = "null") {

        global $db;
        $query = "SELECT * from " . TABLE_BATCH_STOCK . " " . $filter;
        $rows = $db->fetch_all_array($query, $pageno, $pagesize);

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

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

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

    /*************************************************
    / Deduct Ingredient Stock using FIFO Method
    *************************************************/
    public static function DeductStockFIFO($ingredientId, $requiredQty) {
        global $db;

        // Get ingredient stock ordered by oldest first (FIFO)
        $query = "SELECT * FROM ingredient_stock WHERE ing_id = '$ingredientId' AND remaining_qty > 0 ORDER BY purchase_date ASC";
        $stocks = $db->fetch_all_array($query);

        $totalDeducted = 0;

        // Loop through each stock entry and deduct until required quantity is met
        foreach ($stocks as $stock) {
            if ($totalDeducted >= $requiredQty) {
                break; // Stop if we've deducted enough stock
            }

            $availableQty = $stock['remaining_qty'];
            $toDeduct = min($availableQty, $requiredQty - $totalDeducted); // Deduct either the remaining required quantity or available stock

            // Update stock record
            $newQty = $availableQty - $toDeduct;
            $updateQuery = "UPDATE ingredient_stock SET remaining_qty = '$newQty' WHERE id = '{$stock['id']}'";
            $db->query($updateQuery);

            $totalDeducted += $toDeduct; // Keep track of total deducted stock
        }

        // Return the total deducted stock amount
        return $totalDeducted;
    } // DeductStockFIFO
}

?>
