<?php

function get_available_lots($stock_id)
{
    $sql = "SELECT * FROM ".TB_PREF."lots WHERE stock_id=".db_escape($stock_id);
    
     $sql = "SELECT l.id, l.lot_reference, l.row_state, IFNULL(lb.balance_qty,0) qty 
                FROM ".TB_PREF."lots l  
                        LEFT JOIN ".TB_PREF."vw_lot_balance lb ON
                            l.stock_id = lb.stock_id AND
                            l.id= lb.lot_id                    
                WHERE  l.stock_id = ".db_escape($stock_id)." "; 
            
    $result = db_query($sql,"an item could not be retreived");

    return $result;
}

function qty_lot_validation($lot_id,$qty){
    $where_clause = array();
    for($i = 0;$i<count($lot_id);$i++){
        $name = implode("#",array($_POST['stock_id'],"lot",$lot_id[$i],"tran_qty"));
        if($qty[$name] > 0){
            $clause = "(id = ".$lot_id[$i]." AND ". "qty >= ".$qty[$name].")";
            $where_clause[]=$clause;
        }
    }
    $sql = "SELECT * FROM ".TB_PREF."lots WHERE ".implode(" OR ",$where_clause);
    
    $result = db_num_rows(db_query($sql,"an item could not be retreived"));
    return ($result==count($lot_id));
}

