<?php


function get_sql_for_item_transactions_inquiry_ST_MATERIAL_ISSUE($filters, $stock_id, $StockLocation, $type, $after_date, $before_date) {
    
    $sql_ST_MATERIAL_ISSUE = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id,   
                IF((imd.item_description = '' OR imd.item_description IS NULL), sk.description, imd.item_description) item_description,
                '' item_trans_description,
                pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                ".db_escape(ST_MATERIAL_ISSUE)." type, im.im_date tran_date, im.im_no trans_no, imd.id trans_id, sk.stock_id, imd.tran_qty, imd.qty_issued qty,
                imd.unit_cost     
            FROM ".TB_PREF."issue_materials im 
                JOIN ".TB_PREF."material_requisitions tran 
                    ON im.source_mr_no = tran.mr_no 
                JOIN ".TB_PREF."issue_material_details imd 
                    ON im.im_no = imd.im_no  
                JOIN ".TB_PREF."stock_master sk  
                    ON imd.item_code = sk.stock_id  
                LEFT JOIN ".TB_PREF."projects_master pm  
                    ON pm.project_id = tran.project_id 
                LEFT JOIN ".TB_PREF."item_units u   
                    ON imd.tran_unit = u.abbr 
                LEFT JOIN ".TB_PREF."item_units bu   
                    ON sk.units = bu.abbr 
                LEFT JOIN ".TB_PREF."locations lo    
                    ON im.issuing_location = lo.loc_code 
            WHERE im.im_date >= ".db_escape($after_date)." 
                AND im.im_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_MATERIAL_ISSUE .= " AND imd.item_code = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_MATERIAL_ISSUE .= " AND im.issuing_location = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_MATERIAL_ISSUE .= " AND ".db_escape(ST_MATERIAL_ISSUE)." = ".db_escape($type);
        }
        
        $sql_ST_MATERIAL_ISSUE .= " )";
            
            return $sql_ST_MATERIAL_ISSUE;
}

function get_sql_for_item_transactions_inquiry_ST_REQTRANSFER($filters, $stock_id, $StockLocation, $type, $after_date, $before_date) {
    
    $sql_ST_REQTRANSFER = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id, 
                        sk.description item_description, '' item_trans_description,   
                        pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                        mrtd.type, mrtd.tran_date, mrt.mrt_no trans_no, mrtd.trans_id, sk.stock_id, mrtd.tran_qty, mrtd.qty,
                        mrtd.standard_cost unit_cost         
                    FROM ".TB_PREF."material_requisition_transfers mrt 
                        JOIN ".TB_PREF."material_transfer_requisitions tran  
                            ON mrt.source_mtr_no = tran.mtr_no 
                        JOIN ".TB_PREF."material_requisition_transfer_details mrtd 
                            ON mrt.mrt_no = mrtd.trans_no 
                                AND mrt.type = mrtd.type  
                        JOIN ".TB_PREF."stock_master sk  
                            ON mrtd.stock_id = sk.stock_id          
                        LEFT JOIN ".TB_PREF."projects_master pm  
                            ON pm.project_id = tran.project_id 
                        LEFT JOIN ".TB_PREF."item_units u   
                            ON mrtd.tran_unit = u.abbr 
                        LEFT JOIN ".TB_PREF."item_units bu   
                            ON sk.units = bu.abbr 
                        LEFT JOIN ".TB_PREF."locations lo    
                            ON mrtd.loc_code = lo.loc_code 
                    WHERE mrtd.tran_date >= ".db_escape($after_date)." 
                            AND mrtd.tran_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_REQTRANSFER .= " AND mrtd.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_REQTRANSFER .= " AND mrtd.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_REQTRANSFER .= " AND mrtd.type = ".db_escape($type);
        }
        
        $sql_ST_REQTRANSFER .= " )";

    return $sql_ST_REQTRANSFER;
}

function get_sql_for_item_transactions_inquiry_ST_INVENTORY_RECEIPT_ISSUE($filters, $stock_id, $StockLocation, $type, $after_date, $before_date){
    
    $sql_ST_INVENTORY_RECEIPT_ISSUE = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id, 
                        sk.description item_description, '' item_trans_description,    
                        pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                        sm.type, sm.tran_date, sm.trans_no, sm.trans_id, sk.stock_id, sm.tran_qty, sm.qty,
                        sm.standard_cost unit_cost             
                    FROM ".TB_PREF."stock_adjustments tran 
                        JOIN ".TB_PREF."stock_moves sm 
                            ON tran.id = sm.trans_no 
                                AND tran.type = sm.type 
                        JOIN ".TB_PREF."stock_master sk  
                            ON sm.stock_id = sk.stock_id          
                        LEFT JOIN ".TB_PREF."projects_master pm  
                            ON pm.project_id = tran.project_id 
                        LEFT JOIN ".TB_PREF."item_units u   
                            ON sm.tran_unit = u.abbr 
                        LEFT JOIN ".TB_PREF."item_units bu   
                            ON sk.units = bu.abbr 
                        LEFT JOIN ".TB_PREF."locations lo    
                            ON sm.loc_code = lo.loc_code 
                    WHERE sm.tran_date >= ".db_escape($after_date)." 
                            AND sm.tran_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_INVENTORY_RECEIPT_ISSUE .= " AND sm.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_INVENTORY_RECEIPT_ISSUE .= " AND sm.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_INVENTORY_RECEIPT_ISSUE .= " AND sm.type = ".db_escape($type);
        }
        
        $sql_ST_INVENTORY_RECEIPT_ISSUE .= " )";
        
    
    return $sql_ST_INVENTORY_RECEIPT_ISSUE;

}

function get_sql_for_item_transactions_inquiry_ST_LOCTRANSFER($filters, $stock_id, $StockLocation, $type, $after_date, $before_date){
    
    $sql_ST_LOCTRANSFER = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id, 
                        sk.description item_description, '' item_trans_description,    
                        pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                        sm.type, sm.tran_date, sm.trans_no, sm.trans_id, sk.stock_id, sm.tran_qty, sm.qty,
                        sm.standard_cost unit_cost                   
                    FROM ".TB_PREF."stock_transfers tran 
                        JOIN ".TB_PREF."stock_moves sm 
                            ON tran.id = sm.trans_no 
                                AND sm.type = ".db_escape(ST_LOCTRANSFER)." 
                        JOIN ".TB_PREF."stock_master sk  
                            ON sm.stock_id = sk.stock_id              
                        LEFT JOIN ".TB_PREF."projects_master pm  
                            ON pm.project_id = tran.project_id 
                        LEFT JOIN ".TB_PREF."item_units u   
                            ON sm.tran_unit = u.abbr 
                        LEFT JOIN ".TB_PREF."item_units bu   
                            ON sk.units = bu.abbr 
                        LEFT JOIN ".TB_PREF."locations lo    
                            ON sm.loc_code = lo.loc_code 
                    WHERE sm.tran_date >= ".db_escape($after_date)." 
                            AND sm.tran_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_LOCTRANSFER .= " AND sm.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_LOCTRANSFER .= " AND sm.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_LOCTRANSFER .= " AND sm.type = ".db_escape($type);
        }
        
        $sql_ST_LOCTRANSFER .= " )";
    
    return $sql_ST_LOCTRANSFER;
    
}

function get_sql_for_item_transactions_inquiry_ST_SUPPRECEIVE($filters, $stock_id, $StockLocation, $type, $after_date, $before_date) {
   
    $sql_ST_SUPPRECEIVE = "(SELECT '-1' movement_type, '-1' linked_field_type, '-1' linked_field_id, tran.project_id,  
                    IF((grni.description = '' OR grni.description IS NULL), sk.description, grni.description) item_description,
                    '' item_trans_description,
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    ".db_escape(ST_SUPPRECEIVE)." type, grn.delivery_date tran_date, grn.id trans_no, grni.id trans_id, sk.stock_id, grni.tran_qty, grni.qty_recd qty,
                    grni.unit_cost             
                FROM ".TB_PREF."grn_batch grn 
                    JOIN ".TB_PREF."purch_orders tran 
                        ON grn.purch_order_no = tran.order_no 
                    JOIN ".TB_PREF."grn_items grni 
                        ON grn.id = grni.grn_batch_id  
                    JOIN ".TB_PREF."stock_master sk  
                        ON grni.item_code = sk.stock_id              
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON grni.tran_unit = u.abbr  
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON grn.loc_code = lo.loc_code 
                WHERE grn.delivery_date >= ".db_escape($after_date)." 
                    AND grn.delivery_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_SUPPRECEIVE .= " AND grni.item_code = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_SUPPRECEIVE .= " AND grn.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_SUPPRECEIVE .= " AND ".db_escape(ST_SUPPRECEIVE)." = ".db_escape($type);
        }
        
        $sql_ST_SUPPRECEIVE .= " )";
        
        return $sql_ST_SUPPRECEIVE;
}
        
function get_sql_for_item_transactions_inquiry_ST_STOCK_COUNT($filters, $stock_id, $StockLocation, $type, $after_date, 
                                                                                   $before_date){
    
            $sql_ST_STOCK_COUNT = "(SELECT '-1' movement_type, '-1' linked_field_type, '-1' linked_field_id, tran.project_id, 
                        sk.description item_description, '' item_trans_description,      
                        pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                        sm.type, sm.tran_date, sm.trans_no, sm.trans_id, sk.stock_id, sm.tran_qty, sm.qty,
                        sm.standard_cost unit_cost                         
                    FROM ".TB_PREF."stock_counts tran  
                        JOIN ".TB_PREF."stock_moves sm 
                            ON tran.stock_count_no = sm.trans_no 
                                AND sm.type = ".db_escape(ST_STOCK_COUNT)." 
                        JOIN ".TB_PREF."stock_master sk  
                            ON sm.stock_id = sk.stock_id              
                        LEFT JOIN ".TB_PREF."projects_master pm  
                            ON pm.project_id = tran.project_id 
                        LEFT JOIN ".TB_PREF."item_units u   
                            ON sm.tran_unit = u.abbr   
                        LEFT JOIN ".TB_PREF."item_units bu   
                            ON sk.units = bu.abbr 
                        LEFT JOIN ".TB_PREF."locations lo    
                            ON sm.loc_code = lo.loc_code 
                    WHERE tran.tran_date >= ".db_escape($after_date)." 
                AND tran.tran_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_STOCK_COUNT .= " AND sm.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_STOCK_COUNT .= " AND tran.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_STOCK_COUNT .= " AND sm.type = ".db_escape(ST_STOCK_COUNT)." = ".db_escape($type);
        }
        
        $sql_ST_STOCK_COUNT .= " )";
            
            return $sql_ST_STOCK_COUNT;
}


function get_sql_for_item_transactions_inquiry_ST_MATERIAL_RETURN($filters, $stock_id, $StockLocation, $type, $after_date, $before_date) {
    
    $sql_ST_MATERIAL_RETURN = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id, 
                sk.description item_description, '' item_trans_description,    
                pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                ".db_escape(ST_MATERIAL_RETURN)." type, rm.rm_date tran_date, rm.rm_no trans_no, rmd.id trans_id, sk.stock_id, rmd.tran_qty, rmd.qty_returned qty,
                rmd.unit_cost                      
            FROM ".TB_PREF."return_materials rm 
                JOIN ".TB_PREF."material_requisitions tran 
                    ON rm.source_mr_no = tran.mr_no 
                JOIN ".TB_PREF."return_material_details rmd  
                    ON rm.rm_no = rmd.rm_no 
                JOIN ".TB_PREF."stock_master sk  
                    ON rmd.item_code = sk.stock_id              
                LEFT JOIN ".TB_PREF."projects_master pm  
                    ON pm.project_id = tran.project_id 
                LEFT JOIN ".TB_PREF."item_units u   
                    ON rmd.tran_unit = u.abbr  
                LEFT JOIN ".TB_PREF."item_units bu   
                    ON sk.units = bu.abbr 
                LEFT JOIN ".TB_PREF."locations lo    
                    ON rm.receiving_location = lo.loc_code 
            WHERE rm.rm_date >= ".db_escape($after_date)." 
                AND rm.rm_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_MATERIAL_RETURN .= " AND rmd.item_code = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_MATERIAL_RETURN .= " AND rm.receiving_location = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_MATERIAL_RETURN .= " AND ".db_escape(ST_MATERIAL_RETURN)." = ".db_escape($type);
        }
        
        $sql_ST_MATERIAL_RETURN .= " )";
            
            return $sql_ST_MATERIAL_RETURN;
}



                                       
        
                                       
        



  

 

//        $sql_8 = "(SELECT '-1' movement_type, '-1' linked_field_type, '-1' linked_field_id, tran.project_id, sk.description,   
//                        pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, sm.*  
//                    FROM ".TB_PREF."purchase_requisition_issues tran  
//                        JOIN ".TB_PREF."stock_moves sm 
//                            ON tran.pri_no = sm.trans_no 
//                                AND sm.type = ".db_escape(ST_PR_ISSUE)." 
//                        JOIN ".TB_PREF."stock_master sk  
//                            ON sm.stock_id = sk.stock_id              
//                        LEFT JOIN ".TB_PREF."projects_master pm  
//                            ON pm.project_id = tran.project_id 
//                        LEFT JOIN ".TB_PREF."item_units u   
//                            ON sm.tran_unit = u.abbr  
//                        LEFT JOIN ".TB_PREF."item_units bu   
//                            ON sk.units = bu.abbr 
//                        LEFT JOIN ".TB_PREF."locations lo    
//                            ON sm.loc_code = lo.loc_code 
//                    WHERE 1 = 1 $filters_2 )"; 

    
function get_sql_for_item_transactions_inquiry_ST_STOCK_RESERVATION($filters, $stock_id, $StockLocation, $type, $after_date, $before_date) {
    
    $sql_ST_STOCK_RESERVATION = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id, 
                    sk.description item_description, '' item_trans_description,    
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    ".db_escape(ST_STOCK_RESERVATION)." type, tran.sr_date tran_date, tran.id trans_no, srd.id trans_id, sk.stock_id, srd.reserved_unit_qty tran_qty, srd.reserved_qty qty,
                    srd.price unit_cost                         
                FROM ".TB_PREF."stock_reservations tran   
                    JOIN ".TB_PREF."stock_reservation_details srd 
                        ON tran.id = srd.sr_no  
                    JOIN ".TB_PREF."stock_master sk  
                        ON srd.stock_id = sk.stock_id              
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON srd.reserved_unit = u.abbr  
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON tran.location = lo.loc_code 
                WHERE tran.sr_date >= ".db_escape($after_date)." 
                    AND tran.sr_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_STOCK_RESERVATION .= " AND srd.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_STOCK_RESERVATION .= " AND tran.location = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_STOCK_RESERVATION .= " AND ".db_escape(ST_STOCK_RESERVATION)." = ".db_escape($type);
        }
        
        $sql_ST_STOCK_RESERVATION .= " )";
        
        return $sql_ST_STOCK_RESERVATION;
}
        
                                       
function get_sql_for_item_transactions_inquiry_ST_STOCK_RELEASE($filters, $stock_id, $StockLocation, $type, $after_date, $before_date){
    
    $sql_ST_STOCK_RELEASE = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id, 
                    sk.description item_description, '' item_trans_description,    
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    ".db_escape(ST_STOCK_RELEASE)." type, srr.srr_date tran_date, srr.id trans_no, srrd.id trans_id, sk.stock_id, srrd.released_unit_qty tran_qty, srrd.released_qty qty,   
                    srrd.price unit_cost                         
                FROM ".TB_PREF."stock_reservation_releases srr    
                    JOIN ".TB_PREF."stock_reservations tran   
                        ON tran.id = srr.source_sr_no  
                    JOIN ".TB_PREF."stock_reservation_release_details srrd 
                        ON srr.id = srrd.srr_no  
                    JOIN ".TB_PREF."stock_master sk  
                        ON srrd.stock_id = sk.stock_id              
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON srrd.released_unit = u.abbr  
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON srr.location = lo.loc_code 
                WHERE srr.srr_date >= ".db_escape($after_date)." 
                    AND srr.srr_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_STOCK_RELEASE .= " AND srrd.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_STOCK_RELEASE .= " AND srr.location = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_STOCK_RELEASE .= " AND ".db_escape(ST_STOCK_RELEASE)." = ".db_escape($type);
        }
        
        $sql_ST_STOCK_RELEASE .= " )";
        
        return $sql_ST_STOCK_RELEASE;
}


function get_sql_for_item_transactions_inquiry_ST_SITE_MATERIAL_RETURN($project_id, $stock_id, $StockLocation, $type, $after_date, $before_date){
    

        $sql_ST_SITE_MATERIAL_RETURN = "(SELECT '-1' movement_type, '-1' linked_field_type, '-1' linked_field_id, sm.project_id, 
                        sk.description item_description, '' item_trans_description,    
                        pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name,                         
                        sm.type, sm.tran_date, sm.trans_no, sm.trans_id, sk.stock_id, sm.tran_qty, sm.qty,
                        sm.standard_cost unit_cost                               
                    FROM ".TB_PREF."site_material_returns tran     
                        JOIN ".TB_PREF."stock_moves sm 
                            ON tran.id = sm.trans_no 
                                AND sm.type = ".db_escape(ST_SITE_MATERIAL_RETURN)." 
                        JOIN ".TB_PREF."stock_master sk  
                            ON sm.stock_id = sk.stock_id              
                        LEFT JOIN ".TB_PREF."projects_master pm  
                            ON pm.project_id = tran.from_project_id  
                        LEFT JOIN ".TB_PREF."item_units u   
                            ON sm.tran_unit = u.abbr  
                        LEFT JOIN ".TB_PREF."item_units bu   
                            ON sk.units = bu.abbr 
                        LEFT JOIN ".TB_PREF."locations lo    
                            ON sm.loc_code = lo.loc_code 
                    WHERE sm.project_id = ".db_escape($project_id)."  
                        AND sm.tran_date >= ".db_escape($after_date)." 
                        AND sm.tran_date <= ".db_escape($before_date);
              
            if (strlen($stock_id) > 0) {
                $sql_ST_SITE_MATERIAL_RETURN .= " AND sm.stock_id = ".db_escape($stock_id); 
            }
            if ($StockLocation) {
                $sql_ST_SITE_MATERIAL_RETURN .= " AND sm.loc_code = ".db_escape($StockLocation);
            }
            if ($type > 0) {
                $sql_ST_SITE_MATERIAL_RETURN .= " AND sm.type = ".db_escape($type);
            }
            
                $sql_ST_SITE_MATERIAL_RETURN .= " )";
                
                        
        return $sql_ST_SITE_MATERIAL_RETURN;
}
    
                
function get_sql_for_item_transactions_inquiry_ST_PR_ISSUE($filters, $stock_id, $StockLocation, $type, $after_date, $before_date) {

    $sql_ST_PR_ISSUE = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id,  
                    IF((prid.description = '' OR prid.description IS NULL), sk.description, prid.description) item_description,
                    '' item_trans_description,  
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    ".db_escape(ST_PR_ISSUE)." type, tran.pri_date tran_date, tran.pri_no trans_no, prid.id trans_id, sk.stock_id, prid.tran_qty, prid.qty_issued qty   ,
                    prid.unit_cost                          
                FROM ".TB_PREF."purchase_requisition_issues tran     
                    JOIN ".TB_PREF."purchase_requistion_issue_details prid 
                        ON tran.pri_no = prid.pri_no  
                    LEFT JOIN ".TB_PREF."stock_master sk  
                        ON prid.item_code = sk.stock_id              
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON prid.tran_unit = u.abbr  
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON tran.issuing_location = lo.loc_code 
                WHERE tran.pri_date >= ".db_escape($after_date)." 
                    AND tran.pri_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_PR_ISSUE .= " AND prid.item_code = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_PR_ISSUE .= " AND tran.issuing_location = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_PR_ISSUE .= " AND ".db_escape(ST_PR_ISSUE)." = ".db_escape($type);
        }
        
        $sql_ST_PR_ISSUE .= " )";
        
        return $sql_ST_PR_ISSUE;
}

function get_sql_for_item_transactions_inquiry_ST_PR_RETURN($filters, $stock_id, $StockLocation, $type, $after_date, $before_date) {

    $sql_ST_PR_RETURN = "(SELECT tran.movement_type, tran.linked_field_type, tran.linked_field_id, tran.project_id,  
                    IF((prrd.description = '' OR prrd.description IS NULL), sk.description, prrd.description) item_description,
                    '' item_trans_description,    
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    ".db_escape(ST_PR_RETURN)." type, prr.prr_date tran_date, prr.prr_no trans_no, prrd.id trans_id, sk.stock_id, prrd.tran_qty, prrd.qty  ,
                    prrd. unit_cost                               
                FROM ".TB_PREF."purchase_requisition_returns prr      
                    JOIN ".TB_PREF."purchase_requistion_return_details prrd 
                        ON prr.prr_no = prrd.prr_no  
                    JOIN ".TB_PREF."purchase_requisition_issues tran              
                        ON tran.pri_no = prr.pri_no 
                    JOIN ".TB_PREF."stock_master sk  
                        ON prrd.item_code = sk.stock_id              
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON prrd.tran_unit = u.abbr 
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON prr.location = lo.loc_code 
                WHERE prr.prr_date >= ".db_escape($after_date)." 
                    AND prr.prr_date <= ".db_escape($before_date)." $filters";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_PR_RETURN .= " AND prrd.item_code = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_PR_RETURN .= " AND prr.location = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_PR_RETURN .= " AND ".db_escape(ST_PR_RETURN)." = ".db_escape($type);
        }
        
        $sql_ST_PR_RETURN .= " )";   

        return $sql_ST_PR_RETURN;
}

function get_sql_for_item_transactions_inquiry_ST_PETTY_CASH_ITEM_RECEIPT($no_mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date) {
    
    $sql_ST_PETTY_CASH_ITEM_RECEIPT = "(SELECT '-1' movement_type, '-1' linked_field_type, '-1' linked_field_id, tran.project_id,  
                    IF((tran.item_description = '' OR tran.item_description IS NULL), sk.description, tran.item_description) item_description,
                    '' item_trans_description,    
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    tran.type, tran.tran_date, tran.trans_no trans_no, tran.id  trans_id, sk.stock_id, tran.tran_qty, tran.qty,
                    (tran.tran_unit_cost / tran.conversion_factor) unit_cost 
                FROM ".TB_PREF."petty_cash_stock_item_receipt_details tran     
                    JOIN ".TB_PREF."stock_master sk  
                        ON tran.stock_id = sk.stock_id          
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON tran.tran_unit = u.abbr 
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON tran.location = lo.loc_code 
                WHERE tran.type = ".db_escape(ST_PETTY_CASH_ITEM_RECEIPT)." 
                    AND tran.tran_date >= ".db_escape($after_date)." 
                    AND tran.tran_date <= ".db_escape($before_date)." $no_mv_filter";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_PETTY_CASH_ITEM_RECEIPT .= " AND tran.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_PETTY_CASH_ITEM_RECEIPT .= " AND tran.location = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_PETTY_CASH_ITEM_RECEIPT .= " AND tran.type = ".db_escape($type);
        }
        
        $sql_ST_PETTY_CASH_ITEM_RECEIPT .= " )";
        
        return $sql_ST_PETTY_CASH_ITEM_RECEIPT;
}


function get_sql_for_item_transactions_inquiry_ST_DAILY_ITEM_ISSUE($no_mv_filter, $movement_type_id, $linked_field, $linked_field_id, 
                                                                   $stock_id, $StockLocation, $type, $after_date, $before_date) {
        
    $sql_ST_DAILY_ITEM_ISSUE = "(SELECT tran.movement_type, tran.linked_field_type, di.linked_field_id, tran.project_id,  
                    IF((di.item_description = '' OR di.item_description IS NULL), sk.description, di.item_description) item_description,
                    '' item_trans_description,    
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    di.type, di.tran_date, tran.trans_no, di.trans_id, sk.stock_id, di.tran_qty, di.qty,
                    di.standard_cost unit_cost     
                FROM ".TB_PREF."daily_item_issues tran   
                    JOIN ".TB_PREF."daily_item_issue_details di  
                        ON di.trans_no = tran.trans_no  
                            AND di.type = tran.type   
                    LEFT JOIN ".TB_PREF."stock_master sk  
                        ON di.stock_id = sk.stock_id   
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON di.tran_unit = u.abbr 
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON di.loc_code = lo.loc_code 
                WHERE tran.type = ".db_escape(ST_DAILY_ITEM_ISSUE)."  
                    AND di.tran_date >= ".db_escape($after_date)." 
                    AND di.tran_date <= ".db_escape($before_date)." $no_mv_filter";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_DAILY_ITEM_ISSUE .= " AND di.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_DAILY_ITEM_ISSUE .= " AND di.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_DAILY_ITEM_ISSUE .= " AND di.type = ".db_escape($type);
        }
        if ($movement_type_id > 0) {
            $sql_ST_DAILY_ITEM_ISSUE .= " AND tran.movement_type = ".db_escape($movement_type_id);
        }
        if ($linked_field > -1) {
            $sql_ST_DAILY_ITEM_ISSUE .= " AND tran.linked_field_type = ".db_escape($linked_field);
        }
        if ($linked_field == IMLF_EMPLOYEE) {
            if (strlen($linked_field_id) > 2) {
                $sql_ST_DAILY_ITEM_ISSUE .= " AND di.linked_field_id = ".db_escape($linked_field_id);
            }
        } else {
            if ($linked_field_id > 0) {
                $sql_ST_DAILY_ITEM_ISSUE .= " AND di.linked_field_id = ".db_escape($linked_field_id);
            }
        }

            $sql_ST_DAILY_ITEM_ISSUE .= "  )";

            return $sql_ST_DAILY_ITEM_ISSUE;
}
            
                
                
function get_sql_for_item_transactions_inquiry_ST_DAILY_ITEM_RETURN($no_mv_filter, $movement_type_id, $linked_field, $linked_field_id, 
                                                                    $stock_id, $StockLocation, $type, $after_date, $before_date) {
    
    $sql_ST_DAILY_ITEM_RETURN = "(SELECT tran.movement_type, tran.linked_field_type, di.linked_field_id, tran.project_id,  
                    IF((di.item_description = '' OR di.item_description IS NULL), sk.description, di.item_description) item_description,
                    '' item_trans_description,    
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    di.type, di.tran_date, tran.trans_no, di.trans_id, sk.stock_id, di.tran_qty, di.qty,
                    di.standard_cost unit_cost         
                FROM ".TB_PREF."daily_item_issues tran   
                    JOIN ".TB_PREF."daily_item_issue_details di  
                        ON di.trans_no = tran.trans_no  
                            AND di.type = tran.type   
                    LEFT JOIN ".TB_PREF."stock_master sk  
                        ON di.stock_id = sk.stock_id   
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON di.tran_unit = u.abbr  
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON di.loc_code = lo.loc_code 
                WHERE tran.type = ".db_escape(ST_DAILY_ITEM_RETURN)." 
                    AND di.daily_item_type = ".db_escape(NSIT_STOCK_ITEM)."  
                    AND di.tran_date >= ".db_escape($after_date)." 
                    AND di.tran_date <= ".db_escape($before_date)." $no_mv_filter";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_DAILY_ITEM_RETURN .= " AND di.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_DAILY_ITEM_RETURN .= " AND di.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_DAILY_ITEM_RETURN .= " AND di.type = ".db_escape($type);
        }

        if ($movement_type_id > 0) {
            $sql_ST_DAILY_ITEM_RETURN .= " AND tran.movement_type = ".db_escape($movement_type_id);
        }
        if ($linked_field > -1) {
            $sql_ST_DAILY_ITEM_RETURN .= " AND tran.linked_field_type = ".db_escape($linked_field);
        }
        if ($linked_field == IMLF_EMPLOYEE) {
            if (strlen($linked_field_id) > 2) {
                $sql_ST_DAILY_ITEM_RETURN .= " AND di.linked_field_id = ".db_escape($linked_field_id);
            }
        } else {
            if ($linked_field_id > 0) {
                $sql_ST_DAILY_ITEM_RETURN .= " AND di.linked_field_id = ".db_escape($linked_field_id);
            }
        }
            
                $sql_ST_DAILY_ITEM_RETURN .= "  )";
                
            return $sql_ST_DAILY_ITEM_RETURN;
}
                
                
function get_sql_for_item_transactions_inquiry_ST_RAW_MATERIAL_ISSUE($no_mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date) {
    
    $sql_ST_RAW_MATERIAL_ISSUE = "(SELECT tran.company_code movement_type, gc.company_name linked_field_type, di.employee_code linked_field_id, tran.project_id,  
                    IF((di.item_description = '' OR di.item_description IS NULL), sk.description, di.item_description) item_description,
                    '' item_trans_description,    
                    pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                    tran.type, tran.tran_date, tran.trans_no, di.id trans_id, sk.stock_id, di.tran_qty, di.qty,
                    di.standard_cost unit_cost 
                FROM ".TB_PREF."raw_material_issues tran   
                    JOIN ".TB_PREF."raw_material_issue_details di  
                        ON di.trans_no = tran.trans_no  
                            AND di.type = tran.type   
                    JOIN ".TB_PREF."stock_master sk  
                        ON di.stock_id = sk.stock_id   
                    JOIN group_companies gc   
                        ON tran.company_code = gc.company_code    
                    LEFT JOIN ".TB_PREF."projects_master pm  
                        ON pm.project_id = tran.project_id 
                    LEFT JOIN ".TB_PREF."item_units u   
                        ON di.tran_unit = u.abbr  
                    LEFT JOIN ".TB_PREF."item_units bu   
                        ON sk.units = bu.abbr 
                    LEFT JOIN ".TB_PREF."locations lo    
                        ON di.loc_code = lo.loc_code 
                WHERE tran.type = ".db_escape(ST_RAW_MATERIAL_ISSUE)." 
                    AND tran.tran_date >= ".db_escape($after_date)." 
                    AND tran.tran_date <= ".db_escape($before_date)." $no_mv_filter";
            
        if (strlen($stock_id) > 0) {
            $sql_ST_RAW_MATERIAL_ISSUE .= " AND di.stock_id = ".db_escape($stock_id); 
        }
        if (strlen($StockLocation) > 0) {
            $sql_ST_RAW_MATERIAL_ISSUE .= " AND di.loc_code = ".db_escape($StockLocation);
        }
        if ($type > 0) {
            $sql_ST_RAW_MATERIAL_ISSUE .= " AND di.type = ".db_escape($type);
        }
        
        $sql_ST_RAW_MATERIAL_ISSUE .= "  )";
        
        return $sql_ST_RAW_MATERIAL_ISSUE;
}

function get_sql_for_item_transactions_inquiry_ST_RAW_MATERIAL_RETURN($no_mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date){  
     
        $sql_ST_RAW_MATERIAL_RETURN = "(SELECT tran.company_code movement_type, gc.company_name linked_field_type, di.employee_code linked_field_id, tran.project_id,  
                        IF((di.item_description = '' OR di.item_description IS NULL), sk.description, di.item_description) item_description,
                        '' item_trans_description,      
                        pm.description project_name, u.name tran_unit_name, bu.name base_unit_name, lo.location_name, 
                        tran.type, tran.tran_date, tran.trans_no, di.id trans_id, sk.stock_id, di.tran_qty, di.qty,
                        di.standard_cost unit_cost       
                    FROM ".TB_PREF."raw_material_issues tran   
                        JOIN ".TB_PREF."raw_material_issue_details di  
                            ON di.trans_no = tran.trans_no  
                                AND di.type = tran.type   
                        JOIN ".TB_PREF."stock_moves sm 
                            ON di.trans_no = sm.trans_no 
                                AND di.type = sm.type 
                                AND di.id = sm.ref_line_id 
                                AND sm.type = ".db_escape(ST_RAW_MATERIAL_RETURN)." 
                        JOIN ".TB_PREF."stock_master sk  
                            ON sm.stock_id = sk.stock_id   
                        JOIN group_companies gc   
                            ON tran.company_code = gc.company_code    
                        LEFT JOIN ".TB_PREF."projects_master pm  
                            ON pm.project_id = tran.project_id 
                        LEFT JOIN ".TB_PREF."item_units u   
                            ON sm.tran_unit = u.abbr  
                        LEFT JOIN ".TB_PREF."item_units bu   
                            ON sk.units = bu.abbr 
                        LEFT JOIN ".TB_PREF."locations lo    
                            ON sm.loc_code = lo.loc_code 
                    WHERE tran.type = ".db_escape(ST_RAW_MATERIAL_RETURN)." 
                            AND tran.tran_date >= ".db_escape($after_date)." 
                            AND tran.tran_date <= ".db_escape($before_date)." $no_mv_filter";
            
                if (strlen($stock_id) > 0) {
                    $sql_ST_RAW_MATERIAL_RETURN .= " AND di.stock_id = ".db_escape($stock_id); 
                }
                if (strlen($StockLocation) > 0) {
                    $sql_ST_RAW_MATERIAL_RETURN .= " AND di.loc_code = ".db_escape($StockLocation);
                }
                if ($type > 0) {
                    $sql_ST_RAW_MATERIAL_RETURN .= " AND di.type = ".db_escape($type);
                }

                $sql_ST_RAW_MATERIAL_RETURN .= "  )";
        
        return $sql_ST_RAW_MATERIAL_RETURN;

}

function get_sql_for_item_transactions_inquiry($no_mv_filter, $mv_filter, $movement_type_id, $linked_field, $linked_field_id, $project_id, $stock_id, 
                                               $StockLocation, $type, $after_date, $before_date, $item_description=''){ 
    
    $sql_ST_MATERIAL_ISSUE = get_sql_for_item_transactions_inquiry_ST_MATERIAL_ISSUE($mv_filter, $stock_id, $StockLocation, $type, $after_date, 
                                                                                     $before_date);
    
    $sql_ST_MATERIAL_RETURN = get_sql_for_item_transactions_inquiry_ST_MATERIAL_RETURN($mv_filter, $stock_id, $StockLocation, $type, $after_date, 
                                                                                       $before_date);
    
    $sql_ST_REQTRANSFER = get_sql_for_item_transactions_inquiry_ST_REQTRANSFER($mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date);
    
    $sql_ST_INVENTORY_RECEIPT_ISSUE = get_sql_for_item_transactions_inquiry_ST_INVENTORY_RECEIPT_ISSUE($mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date);
    
    $sql_ST_LOCTRANSFER = get_sql_for_item_transactions_inquiry_ST_LOCTRANSFER($mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date);
    
    $sql_ST_STOCK_RESERVATION = get_sql_for_item_transactions_inquiry_ST_STOCK_RESERVATION($mv_filter, $stock_id, $StockLocation, $type, $after_date, 
                                                                                           $before_date);
    
    $sql_ST_STOCK_RELEASE = get_sql_for_item_transactions_inquiry_ST_STOCK_RELEASE($mv_filter, $stock_id, $StockLocation, $type, $after_date, 
                                                                                   $before_date);
    
    $sql_ST_DAILY_ITEM_ISSUE = get_sql_for_item_transactions_inquiry_ST_DAILY_ITEM_ISSUE($no_mv_filter, $movement_type_id, $linked_field, 
                                                                        $linked_field_id, $stock_id, $StockLocation, $type, $after_date, $before_date);
    
    $sql_ST_DAILY_ITEM_RETURN = get_sql_for_item_transactions_inquiry_ST_DAILY_ITEM_RETURN($no_mv_filter, $movement_type_id, $linked_field, 
                                                                        $linked_field_id, $stock_id, $StockLocation, $type, $after_date, $before_date);
    
    $sql_ST_PR_ISSUE = get_sql_for_item_transactions_inquiry_ST_PR_ISSUE($mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date);
    
    $sql_ST_PR_RETURN = get_sql_for_item_transactions_inquiry_ST_PR_RETURN($mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date);

    
       
    $sql = " $sql_ST_MATERIAL_ISSUE ";
    $sql .= " UNION $sql_ST_MATERIAL_RETURN";
    $sql .= " UNION $sql_ST_REQTRANSFER";
    $sql .= " UNION $sql_ST_INVENTORY_RECEIPT_ISSUE";
    $sql .= " UNION $sql_ST_LOCTRANSFER";
    $sql .= " UNION $sql_ST_STOCK_RELEASE";
    $sql .= " UNION $sql_ST_STOCK_RESERVATION";
    $sql .= " UNION $sql_ST_DAILY_ITEM_ISSUE";
    $sql .= " UNION $sql_ST_DAILY_ITEM_RETURN";
    $sql .= " UNION $sql_ST_PR_ISSUE";
    $sql .= " UNION $sql_ST_PR_RETURN";
    
//    return $sql_ST_DAILY_ITEM_ISSUE;
        
    if(!($movement_type_id > 0 || $linked_field > -1 || $linked_field_id > -1)){
        
        $sql_ST_SUPPRECEIVE = get_sql_for_item_transactions_inquiry_ST_SUPPRECEIVE($no_mv_filter, $stock_id, $StockLocation, $type, $after_date, 
                                                                                   $before_date);
        
        $sql_ST_STOCK_COUNT = get_sql_for_item_transactions_inquiry_ST_STOCK_COUNT($no_mv_filter, $stock_id, $StockLocation, $type, $after_date, 
                                                                                   $before_date);
        
        $sql_ST_PETTY_CASH_ITEM_RECEIPT = get_sql_for_item_transactions_inquiry_ST_PETTY_CASH_ITEM_RECEIPT($no_mv_filter, $stock_id, $StockLocation, 
                                                                                                           $type, $after_date, $before_date);
        
        $sql_ST_RAW_MATERIAL_ISSUE = get_sql_for_item_transactions_inquiry_ST_RAW_MATERIAL_ISSUE($no_mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date);
        
        $sql_ST_RAW_MATERIAL_RETURN = get_sql_for_item_transactions_inquiry_ST_RAW_MATERIAL_RETURN($no_mv_filter, $stock_id, $StockLocation, $type, $after_date, $before_date);
        
        $sql_ST_SITE_MATERIAL_RETURN = get_sql_for_item_transactions_inquiry_ST_SITE_MATERIAL_RETURN($project_id, $stock_id, $StockLocation, $type, $after_date, $before_date);
        
//    
        $sql .= " UNION $sql_ST_SUPPRECEIVE";
        $sql .= " UNION $sql_ST_STOCK_COUNT"; 
        $sql .= " UNION $sql_ST_PETTY_CASH_ITEM_RECEIPT";
        $sql .= " UNION $sql_ST_RAW_MATERIAL_ISSUE";
        $sql .= " UNION $sql_ST_RAW_MATERIAL_RETURN";
        $sql .= " UNION $sql_ST_SITE_MATERIAL_RETURN";
    }
        

//    $sql = $sql_ST_DAILY_ITEM_ISSUE;
    
    if($item_description != ''){
        $item_description = '%'.$item_description.'%';
        $sql = "SELECT * FROM ($sql) tmp 
                WHERE item_description LIKE ".db_escape($item_description)." 
                ORDER BY tran_date, trans_id ";
    }
    else{
        $sql .= " ORDER BY tran_date, trans_id ";
    }


    
    return $sql;
}


function get_item_transactions_inquiry($stock_id, $StockLocation, $BeforeDate, $AfterDate, $project_id, $type, $movement_type_id, $linked_field, 
                                       $linked_field_id, $item_description='') {
    $before_date = date2sql($BeforeDate);
    $after_date = date2sql($AfterDate);
    
    $mv_filter = $no_mv_filter = '';
    
//    if (strlen($stock_id) > 0) {
//        $no_mv_filter .= " AND sm.stock_id = ".db_escape($stock_id); 
//    }
//    if (strlen($StockLocation) > 0) {
//        $no_mv_filter .= " AND sm.loc_code = ".db_escape($StockLocation);
//    }
//    if ($type > 0) {
//        $no_mv_filter .= " AND sm.type = ".db_escape($type);
//    }
//    $no_mv_filter .= " AND sm.tran_date >= ".db_escape($after_date)." 
//                  AND sm.tran_date <= ".db_escape($before_date);
    
    
    if ($project_id > 0) {
        $no_mv_filter .= " AND tran.project_id = ".db_escape($project_id);
    }
    
    
    $mv_filter = $no_mv_filter;
    
    if ($movement_type_id > 0) {
        $mv_filter .= " AND tran.movement_type = ".db_escape($movement_type_id);
    }
    if ($linked_field > -1) {
        $mv_filter .= " AND tran.linked_field_type = ".db_escape($linked_field);
    }
    if ($linked_field == IMLF_EMPLOYEE) {
        if (strlen($linked_field_id) > 2) {
            $mv_filter .= " AND tran.linked_field_id = ".db_escape($linked_field_id);
        }
    } else {
        if ($linked_field_id > 0) {
            $mv_filter .= " AND tran.linked_field_id = ".db_escape($linked_field_id);
        }
    }
                                       
    $sql = get_sql_for_item_transactions_inquiry($no_mv_filter, $mv_filter, $movement_type_id, $linked_field, $linked_field_id, $project_id, $stock_id, 
                                                 $StockLocation, $type, $after_date, $before_date, $item_description);
    
    $result = db_query($sql, "get_item_transactions_inquiry");
    
    return $result;
}    
