<?php //
/**********************************************************************
***********************************************************************/




function view_petty_cash_item_receive_items_by_source($source_type, $source_detail_id)
{
	$sql = "SELECT sir.*,                 
                        iu.name tran_unit_name,                 
                        l.location_name,
                        sm.editable 
                FROM ".TB_PREF."petty_cash_stock_item_receipt_details sir                                          
                        JOIN ".TB_PREF."item_units iu  
                            ON sir.tran_unit = iu.abbr 
                        JOIN ".TB_PREF."locations l   
                            ON l.loc_code = sir.location  
                        JOIN ".TB_PREF."stock_master sm    
                            ON sm.stock_id = sir.stock_id   
                            
                WHERE  sir.source_type=".db_escape($source_type)." AND               
                        sir.source_detail_id=".db_escape($source_detail_id);        
  
	return db_query($sql, "view_petty_cash_item_receive_items");
}

function get_petty_cash_expense_category_invoices($expense_category, $trans_no)
{
	$sql = "SELECT iou.*                   
                FROM ".TB_PREF."i_owe_you_expenses iou    
                            
                WHERE  iou.expense_category_id=".db_escape($expense_category)." AND       
                        iou.iou_batch_no=".db_escape($trans_no);        
  
	return db_query($sql, "get_petty_cash_expense_category_invoices");
}


function view_petty_cash_item_receive_header($type_id, $trans_no)
{
	$sql = "SELECT sir.*,      
                        ex.amount expense_amount, 
                        ex.bill_no,
                        ex.expence_description,
                        ex.iou_batch_no, 
                        l.location_name,
                        pm.description project_description,
                        pt.description task_description,
                        etn.value_name expense_type_name 
                FROM ".TB_PREF."petty_cash_stock_item_receipt_details sir                                          
                        JOIN ".TB_PREF."locations l   
                            ON l.loc_code = sir.location  
                        JOIN ".TB_PREF."i_owe_you_expenses ex    
                            ON ex.id = sir.source_detail_id   
                        JOIN ".TB_PREF."i_owe_you_batches exb    
                            ON exb.batch_no = ex.iou_batch_no   
                        JOIN ".TB_PREF."projects_master pm    
                            ON pm.project_id = exb.project_id   
                        JOIN ".TB_PREF."project_tasks pt     
                            ON ex.task_id = pt.task_id    
                        JOIN ".TB_PREF."list_values etn      
                            ON etn.id = ex.expense_type_id     
                            
                WHERE  sir.type=".db_escape($type_id)." AND         
                        sir.trans_no=".db_escape($trans_no)." 
                LIMIT 1 ";
  
	$result = db_query($sql, "view_petty_cash_item_receive_header");
        return db_fetch($result);
}

function get_sql_for_petty_cash_item_receipts_inquiry($required_by_from = null, $required_by_to = null, 
//            $created_on_from = null, $created_on_to = null,  
            $issuing_location = "",
            $mr_no = 0,  $mr_reference = "",  
            $project_id = null, $originator = null,
            $stock_item = null, $outstanding=0)
{
        $filter = "";
        
        if($required_by_from != null)
        {
            $filter = " AND sir.tran_date > ".db_escape(date2sql($required_by_from));
        }
        
        if($required_by_to != null)
        {
                $filter .= " AND sir.tran_date < ".db_escape(date2sql($required_by_to));
        }
        
//        if($created_on_from != null)
//        {
//                $filter .= " AND mr.created_on > ".db_escape(date2sql($created_on_from));
//        }
//        
//        if($created_on_to != null)
//        {
//                $filter .= " AND mr.created_on < ".db_escape(date2sql($created_on_to));
//        }
        
        if($issuing_location != "")
        {
                $filter .= " AND sir.location = ".db_escape($issuing_location);            
        }
        
        if ($mr_no != 0)
        {
                $filter .= " AND sir.trans_no = ".$mr_no;
        }
        
        if ($project_id > 0)
        {
                $filter .= " AND sir.project_id = ".db_escape($project_id); 
        }
        
        if ($originator > 0)
        {
                $filter .= " AND sir.created_by = ".db_escape($originator); 
        }
        
        if ($stock_item != null)
        {
            $filter .= " AND sm.stock_id = ".db_escape($stock_item);      
        }
        
        if ($outstanding == 1)
        {
            $filter .= " AND sir.approved_by = 0";      
        }else{
            $filter .= " AND sir.approved_by > 0";   
        }
        

//        $filter .= " AND mr.mr_complete = ".db_escape($mr_complete);         

        
        //$filter = "";
        

            $sql = "SELECT sir.*,                 
                        iu.name tran_unit_name,                 
                        l.location_name,
                        pm.description project_description,
                        sm.description item_description,
                        u.real_name 
                FROM ".TB_PREF."petty_cash_stock_item_receipt_details sir                                          
                        JOIN ".TB_PREF."item_units iu  
                            ON sir.tran_unit = iu.abbr 
                        JOIN ".TB_PREF."locations l   
                            ON l.loc_code = sir.location  
                        JOIN ".TB_PREF."projects_master pm    
                            ON pm.project_id = sir.project_id   
                        JOIN ".TB_PREF."stock_master sm    
                            ON sm.stock_id = sir.stock_id    
                        JOIN ".TB_PREF."users u     
                            ON u.id = sir.created_by     
                            
                WHERE  1=1 "
                        .$filter."  
                ORDER By sir.id ";
        
            

	return $sql;
}

function get_petty_cash_item_receive_header_by_source($source_type_id, $source_detail_id)
{
	$sql = "SELECT sir.*,      
                        ex.amount expense_amount, 
                        ex.bill_no,
                        ex.expence_description,
                        ex.iou_batch_no, 
                        l.location_name,
                        pm.description project_description,
                        pt.description task_description 
                FROM ".TB_PREF."petty_cash_stock_item_receipt_details sir                                          
                        JOIN ".TB_PREF."locations l   
                            ON l.loc_code = sir.location  
                        JOIN ".TB_PREF."i_owe_you_expenses ex    
                            ON ex.id = sir.source_detail_id   
                        JOIN ".TB_PREF."i_owe_you_batches exb    
                            ON exb.batch_no = ex.iou_batch_no   
                        JOIN ".TB_PREF."projects_master pm    
                            ON pm.project_id = exb.project_id   
                        JOIN ".TB_PREF."project_tasks pt     
                            ON ex.task_id = pt.task_id    
                            
                WHERE  sir.source_type=".db_escape($source_type_id)." AND         
                        sir.source_detail_id=".db_escape($source_detail_id)." 
                LIMIT 1 ";
  
	$result = db_query($sql, "view_petty_cash_item_receive_header");
        return db_fetch($result);
}
