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


function get_mobile_allowances_assigned_employee_list($salary_component_id, $from_date, $to_date)
{  
	$sql = " SELECT em.initials, em.first_name,em.middle_name,em.last_name,em.office_mobile_no,
                        sc.amount,sc.calculation_type,sc.table_id,sc.employee_code,
                        lv.value_code salary_component 
            FROM ".TB_PREF."employee_master em,
                    ".TB_PREF."salary_components sc,
                    ".TB_PREF."list_values lv     
            WHERE sc.employee_code = em.employee_code AND
                  sc.type = ".  db_escape(SCT_MOBILE_OVER_USAGE)." AND 
                  sc.salary_component_id = lv.id AND 
                  sc.salary_component_id = ".db_escape($salary_component_id)." AND 
                  sc.effective_date <= ".db_escape($to_date)." AND 
                  sc.end_date >= ".  db_escape($from_date)." 
            ORDER BY office_mobile_no
                    ";
	$result = db_query($sql,"Mobile allowances assigned employee list could not be retrieved");	
        
        return $result;
}

function get_saved_employee_mobile_bill_values($payroll_run_id, $employee_code, $salary_component_id)
{  
	$sql = "SELECT payroll_run_id, mobile_bill, over_used_amount, comments, status_code, mad.id,
                    lv.value_name status_name 
            FROM ".TB_PREF."mobile_allowances_details mad,
                    ".TB_PREF."list_values lv,
                    ".TB_PREF."list_values sc   
            WHERE mad.status_code = lv.value_code AND lv.list_code = 'ATTENDANCE_STATUS' AND 
                    mad.component = sc.value_code AND sc.list_code = 'MOBILE_ALLOWANCES_OVER_USAGE' AND 
                    payroll_run_id = ".db_escape($payroll_run_id)." AND 
                    employee_code = ".db_escape($employee_code)." AND 
                    sc.id = ".db_escape($salary_component_id)." LIMIT 1 ";
    
	$result = db_query($sql,"Saved mobile bill details could not be retrieved");	
        
    return db_fetch($result);
}
//
//function get_allowances($employee_code, $salary_component_id)
//{
//    $sql="SELECT * FROM ".TB_PREF."mobile_allowances_details 
//                WHERE employee_id=".db_escape($employee_code)."AND
//                    component_id=".db_escape($employee_code)."
//                        " ;
//
//	$result = db_query($sql,"Details could not be retrieved");
//
//	return db_fetch($result);
//}

function add_mobile_allowance($employee_code, $salary_component, $bill_value, $over_used_amount, $payroll_run_id, $comments)
{
    
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."mobile_allowances_details
                            (employee_code, component,  
                            mobile_bill, over_used_amount, 
                            payroll_run_id, comments,  
                            status_code, 
                            created_by, created_on) 
			VALUES( ".db_escape($employee_code).",".db_escape($salary_component).","
                            .db_escape($bill_value).",".db_escape($over_used_amount).","
                            .db_escape($payroll_run_id).",".db_escape($comments).","
                            .db_escape(ATD_NEW).","
                            .db_escape($id_user).",".db_escape($datetime).")";

	db_query($sql,"Allowance entry could not be added");
    return db_insert_id();
}

function update_mobile_allowance($row_id, $employee_code, 
                                    $allowance, $over_used_amount, 
                                    $payroll_run_id, $comments, $status_code)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."mobile_allowances_details 
            SET
                mobile_bill = ".db_escape($allowance).",
                over_used_amount = ".db_escape($over_used_amount).",
                comments = ".db_escape($comments).",
                status_code = ".db_escape($status_code).", 
                updated_by = ".db_escape($id_user).",
                updated_on = ".db_escape($datetime)."

        	WHERE employee_code = ".db_escape($employee_code)."AND 
                    payroll_run_id = ".db_escape($payroll_run_id)."AND
                    id=".db_escape($row_id) ;

	db_query($sql,"Allowance entry could not be updated");
        return db_num_affected_rows();
        
}

function approve_mobile_allowance($row_id, $employee_code, $payroll_run_id)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."mobile_allowances_details 
            SET status_code = ".db_escape(ATD_APPROVED).", 
                approved_by = ".db_escape($id_user).",
                approved_on = ".db_escape($datetime)."
        	WHERE payroll_run_id = ".db_escape($payroll_run_id)."AND
                employee_code = ".db_escape($employee_code)."AND 
                id=".db_escape($row_id) ;

	db_query($sql,"Allowance entry could not be updated");        
}

//function get_mobile_allowance($id)
//{
//    $sql="SELECT mad.*,
//                sc.calculation_type, sc.amount, employee_code  
//        FROM ".TB_PREF."mobile_allowances_details mad,
//            ".TB_PREF."salary_components sc
//                WHERE mad.id=".db_escape($id)." AND 
//                        mad.employee_id = sc.employee_code AND 
//                        mad.component_id = sc.salary_component_id " ;
//
//	$result = db_query($sql,"Details could not be retrieved");
//
//	return db_fetch($result);
//}

?>