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


function get_sql_for_miscellaneous_pc_inquiry($project_id, $from_date, $to_date, $selected_id, $reference)
{
    $sql = "SELECT mpc.*,
                    pm.description project_description

            FROM ".TB_PREF."miscellaneous_project_costs mpc  
                LEFT JOIN ".TB_PREF."projects_master pm ON
                    mpc.project_id = pm.project_id
             
            WHERE  mpc.date >".db_escape(date2sql($from_date))." AND  mpc.date <".db_escape(date2sql($to_date));  
            
   
    if($project_id > 0){
        $sql .= "AND mpc.project_id=".db_escape($project_id);
    }
    
    if($selected_id > 0){
        $sql .= "AND mpc.id=".db_escape($selected_id);
    }
    
    if($reference != ''){
        $sql .= "AND mpc.reference=".db_escape($reference);
    }
    
    return $sql;
    
}
        

function view_miscellaneous_pc_header($trans_no)
{
    $sql = "SELECT mpc.*,
                    pm.description project_description

            FROM ".TB_PREF."miscellaneous_project_costs mpc
                LEFT JOIN ".TB_PREF."projects_master pm ON
                    mpc.project_id = pm.project_id
            
            WHERE mpc.id=".db_escape($trans_no);
    
    $result = db_query($sql);
    return db_fetch($result);
    
}


function view_miscellaneous_pc_details($trans_no)
{
     $sql = "SELECT mpcd.*,
                    pbig.description boq_item_group_description,
                    iu.name unit_name,
                    pt.description budget_name,
                    lv.value_name miscellaneous_type,
                    da.account_name debit_account_name,
                    ca.account_name credit_account_name 
         
            FROM ".TB_PREF."miscellaneous_project_cost_details mpcd                
                LEFT JOIN ".TB_PREF."project_boq_item_groups pbig ON
                    mpcd.task_boq_group_id = pbig.id

                LEFT JOIN ".TB_PREF."item_units iu ON
                    mpcd.unit = iu.abbr
                    
                LEFT JOIN ".TB_PREF."project_tasks pt ON
                    mpcd.task_id = pt.task_id
                    
                LEFT JOIN ".TB_PREF."list_values lv ON
                    mpcd.mpc_type_id = lv.id 
                LEFT JOIN ".TB_PREF."chart_master da ON 
                    mpcd.debit_account = da.account_code  
                LEFT JOIN ".TB_PREF."chart_master ca ON 
                    mpcd.credit_account = ca.account_code  

            WHERE mpcd.mpc_id=".db_escape($trans_no)." 
                
            ORDER BY id";
    
    return db_query($sql);
    
}



function get_sql_for_approve_miscellaneous_project_cost_inquiry($current_user_id)
{        
    $sql = "SELECT mpc.*,
                    pm.description project_description                
            
            FROM ".TB_PREF."miscellaneous_project_costs mpc 
                JOIN ".TB_PREF."projects_master pm ON
                    mpc.project_id = pm.project_id          
                  
                JOIN ".TB_PREF."users c ON
                    mpc.created_by = c.id
                                 
                JOIN ".TB_PREF."approvers apr ON
                   mpc.next_approval_group_id = apr.approval_group_id                
                    
                JOIN ".TB_PREF."user_data_access_groups udag ON 
                        pm.dag_id = udag.dag_id AND 
                        udag.user_id = apr.user_id   
                    
                JOIN ".TB_PREF."users u ON
                    apr.user_id = u.id                    
                                             
            WHERE (mpc.approval_status = ".AS_AWAITING_APPROVAL." OR mpc.approval_status = ".AS_WITH_THE_APPROVER.") AND  
                    apr.user_id =".db_escape($current_user_id);                    
                        
    return $sql; 
    
}



        
?>