<?php
/**********************************************************************
***********************************************************************/
function add_salary_commitments($employee_code, $bank_id, 
                                $branch_id, $account_no, $commitment,
                                $start_date, $end_date)
{
	$created_by = $_SESSION['wa_current_user']->user;
	$created_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."salary_commitments (
                    employee_code, 
                    bank_id, 
                    branch_id, 
                    account_no, 
                    commitment, 
                    start_date, 
                    end_date, 
                    created_by, 
                    created_on) 
		VALUES ("
                .db_escape($employee_code).", "
                .db_escape($bank_id).", "
                .db_escape($branch_id).", "
                .db_escape($account_no).", "
                .db_escape($commitment).", "
                .db_escape($start_date).", "
                .db_escape($end_date).", "
                .db_escape($created_by).", "
                .db_escape($created_on).")";
	
	db_query($sql,"Salary Commitment could not be added");		
}

function update_salary_commitments($selected_id, $bank_id, 
                                $branch_id, $account_no, $commitment,
                                $start_date, $end_date)
{
	                
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
     	
    $sql = "UPDATE ".TB_PREF."salary_commitments "
                . "SET "
                    . "bank_id=".db_escape($bank_id).","
                    . "branch_id=".db_escape($branch_id).","
                    . "account_no=".db_escape($account_no).","
                    . "commitment=".db_escape($commitment).", "
                    . "start_date=".db_escape($start_date).","
                    . "end_date=".db_escape($end_date).","
                    . "updated_by=".db_escape($updated_by).","
                    . "updated_on=".db_escape($updated_on)
                . "WHERE "
                    . "id=".db_escape($selected_id);
	
	db_query($sql,"Salary Commitment could not be updated");		
}
//
function delete_salary_commitments($selected_id)
{
	$sql="DELETE FROM ".TB_PREF."salary_commitments WHERE id= ".db_escape($selected_id);
	db_query($sql,"Salary Commitment could not be deleted");			
}
//
function get_salary_commitments($employee_code)
{
	$sql = "SELECT 
                    sc.*,
                    bb.branch_name bank_branch_name, 
                    bn.value_name bank_name    
		FROM "
                    .TB_PREF."salary_commitments sc, "
                    .TB_PREF."bank_branches bb, "
                    .TB_PREF."list_values bn 
                WHERE 
                    sc.bank_id = bn.id AND 
                    sc.branch_id = bb.id AND 
                    sc.employee_code=".db_escape($employee_code)." 
                ORDER BY sc.employee_code";	
	
	return db_query($sql,"Salary Commitments could not be retreived");
}
//
function get_salary_commitment($selected_id)
{
	$sql = "SELECT 
                    sc.*, 
                    bn.value_name bank_name    
		FROM "
                    .TB_PREF."salary_commitments sc, "
                    .TB_PREF."list_values bn 
                WHERE 
                    sc.bank_id = bn.id AND 
                    sc.id=".db_escape($selected_id)." 
                ORDER BY sc.employee_code";	
	
	return db_query($sql,"Salary Commitment could not be retreived");
}
//
function check_employee_has_salary_commitments($employee_code, $previous_bank_id, $previous_branch_id,
        $previous_account_no)
{
    $date = date('Y-m-d');
    
    $sql = "SELECT 
                    sc.*, 
                    bn.value_name bank_name    
		FROM "
                    .TB_PREF."salary_commitments sc, "
                    .TB_PREF."list_values bn 
                WHERE 
                    sc.bank_id = bn.id AND 
                    sc.employee_code= ".db_escape($employee_code)." AND 
                    sc.bank_id = ".db_escape($previous_bank_id)." AND
                    sc.branch_id = ".db_escape($previous_branch_id)." AND
                    sc.account_no = ".db_escape($previous_account_no)." AND
                    ".db_escape($date)." between sc.start_date AND sc.end_date
                ORDER BY sc.employee_code";
    
    $result = db_query($sql,"Salary Commitment could not be retreived");
    return db_fetch($result);
}
?>