<?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){
			//$db->query_insert("vrs_logs", $data);
			return $id;
		}else{
			return false;
		}
	
	} // AddProductionStock

	/*************************************************/*************************************************
    / FIFO Inventory Deduction Method
    *************************************************/
    public static function DeductStockFIFO($product_id, $required_qty) {
        global $db;

        // Retrieve stock in FIFO order
        $query = "SELECT * FROM " . TABLE_BATCH_STOCK . " WHERE product_id = '$product_id' AND remaining_qty > 0 ORDER BY stock_date ASC";
        $stock_batches = $db->fetch_all_array($query);

        if (!$stock_batches) {
            // No stock available
            return false;
        }

        $total_deducted = 0;

        // Loop through each stock batch and deduct stock in FIFO order
        foreach ($stock_batches as $batch) {
            if ($required_qty <= 0) break;  // If no more quantity is required, exit loop

            $available_qty = $batch['remaining_qty'];
            $deduct_qty = min($required_qty, $available_qty);  // Deduct the lesser of required or available

            // Update remaining stock for this batch
            $new_remaining_qty = $available_qty - $deduct_qty;
            $update_data = ['remaining_qty' => $new_remaining_qty];
            $db->query_update(TABLE_BATCH_STOCK, $update_data, "id='" . $batch['id'] . "'");

            // Decrease the required quantity
            $required_qty -= $deduct_qty;
            $total_deducted += $deduct_qty;
        }

        return $total_deducted;
    } // DeductStockFIFO
	/ 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){
			//$db->query_insert("vrs_logs", $data);
			
			return $id;
		}else{
			return $id;
		}
	
	} // UpdateProductionStock


	public static function GetProductionStockSetupById($id) {
	
		global $db;
		$query = "SELECT * from ".TABLE_BATCH_STOCK." WHERE id ='$id'";
		//print($query);exit;
		$rows = $db->query_first($query);
	
                if($db->affected_rows > 0){
			
                        return $rows;
                }else{
			
                        return false;
                }
	}//ProductionStock
	
	/**********************************************************
	/ This function will return ProductionStockSetup
	**********************************************************/
		
	public static function GetProductionStockSetupList($filter="",$pageno="null",$pagesize="null") {
	
		global $db;
		$query = "SELECT * from ".TABLE_BATCH_STOCK." ". $filter;
		//print($query);exit;
		$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(); }
  
  
  /*************************************************
    / FIFO Inventory Deduction Method
    *************************************************/
    public static function DeductStockFIFO($product_id, $required_qty) {
        global $db;

        // Retrieve stock in FIFO order
        $query = "SELECT * FROM " . TABLE_BATCH_STOCK . " WHERE product_id = '$product_id' AND remaining_qty > 0 ORDER BY stock_date ASC";
        $stock_batches = $db->fetch_all_array($query);

        if (!$stock_batches) {
            // No stock available
            return false;
        }

        $total_deducted = 0;

        // Loop through each stock batch and deduct stock in FIFO order
        foreach ($stock_batches as $batch) {
            if ($required_qty <= 0) break;  // If no more quantity is required, exit loop

            $available_qty = $batch['remaining_qty'];
            $deduct_qty = min($required_qty, $available_qty);  // Deduct the lesser of required or available

            // Update remaining stock for this batch
            $new_remaining_qty = $available_qty - $deduct_qty;
            $update_data = ['remaining_qty' => $new_remaining_qty];
            $db->query_update(TABLE_BATCH_STOCK, $update_data, "id='" . $batch['id'] . "'");

            // Decrease the required quantity
            $required_qty -= $deduct_qty;
            $total_deducted += $deduct_qty;
        }

        return $total_deducted;
    } // DeductStockFIFO
		
	
}

?>
