<?php


function get_sql_for_stock_count_search()
{
	global $all_items;
        
	$sql = "SELECT 
                        sc.stock_count_no,
                        sc.reference,
                        sc.loc_code,
                        sc.tran_date,
                        sc.status_id,
                        loc.location_name 
		FROM ".TB_PREF."stock_counts as sc,
                        ".TB_PREF."locations as loc            
		WHERE sc.loc_code = loc.loc_code";
        
                if(isset($_POST['OrdersAfterDate']) && $_POST['OrdersToDate'])
                {    
                    $data_after = date2sql($_POST['OrdersAfterDate']);
                    $date_before = date2sql($_POST['OrdersToDate']);

                    $sql .= " AND sc.tran_date >= '$data_after'";
                    $sql .= " AND sc.tran_date <= '$date_before'";
                }
                
                if (isset($_POST['Location']) && $_POST['Location'] != $all_items)
		{
                    $sql .= " AND sc.loc_code = ".db_escape($_POST['Location']);
		}
                
                $sql .= " ORDER BY sc.tran_date,sc.stock_count_no ";

	return $sql;
        
}       


      

function view_stock_count_details($stock_count_id, $sheet_no)
{
    
        $sql = "SELECT
                    scd.*,
                    sm.description,
                    bu.name base_unit_name,
                    tu.name tran_unit_name 
                FROM ".TB_PREF."stock_count_details as scd 
                    INNER JOIN ".TB_PREF."stock_master as sm ON 
                            sm.stock_id = scd.stock_id 
                    JOIN ".TB_PREF."item_units bu ON 
                            sm.units =  bu.abbr 
                    LEFT JOIN ".TB_PREF."item_units tu ON 
                            scd.tran_unit =  tu.abbr 
                WHERE scd.stock_count_no = ".db_escape($stock_count_id)." AND 
                    scd.sheet_no = ".db_escape($sheet_no)." 
                ORDER BY scd.stock_id ";
               
        return db_query($sql, "Could not view_stock_count_details"); 
}

function get_stock_count_view_header($stock_count_id)
{
    
        $sql = "SELECT
                    sc.stock_count_no,
                    sc.reference,
                    sc.loc_code,
                    sc.tran_date,
                    sc.status_id,
                    sc.comments,
                    l.location_name  
                FROM ".TB_PREF."stock_counts as sc,
                    ".TB_PREF."locations l  
                WHERE sc.loc_code = l.loc_code AND 
                    sc.stock_count_no = ".db_escape($stock_count_id);
               
        $result = db_query($sql, "Could not retreive stock count view header"); 
        
        return db_fetch($result);
}

?>


