<?php
/**********************************************************************
***********************************************************************/
function add_salary_advance_account($component_id,
                        $debit_code, $credit_code)
{
	$updated_by = $_SESSION['wa_current_user']->user;
	$updated_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."salary_advances_accounts (
                    component_id,  
                    debit_code, credit_code,
                    updated_by, updated_on) 
		VALUES ("
                    .db_escape($component_id).", "
                    .db_escape($debit_code).", ".db_escape($credit_code).", "
                    .db_escape($updated_by).", ".db_escape($updated_on).")";
	
	db_query($sql,"Salary advance account could not be updated");
        
        return db_insert_id();
        
}

function update_salary_advance_account($row_id, 
                        $debit_code, $credit_code)
{
	                
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
     	
    $sql = "UPDATE ".TB_PREF."salary_advances_accounts "
                . "SET "
                    . "debit_code=".db_escape($debit_code).", "
                    . "credit_code=".db_escape($credit_code).", "
                    . "updated_by=".db_escape($updated_by).","
                    . "updated_on=".db_escape($updated_on)
                . "WHERE "
                    . "id=".db_escape($row_id);
	
    db_query($sql,"Salary advance account could not be updated");	
    
}

function get_salary_advance_accounts()
{
	$sql = "SELECT sc.id component_id, 
                        sc.list_code process_code, 
                        sc.value_code component_code,
                        sc.value_name component_name,
                        pa.id row_id,
                        pa.debit_code,
                        pa.credit_code,
                        dc.account_name debit_account_name,
                        cc.account_name credit_account_name
		FROM "
                        .TB_PREF."list_values  sc 
                LEFT JOIN ".TB_PREF."salary_advances_accounts pa ON   
                        sc.id = pa.component_id  
                LEFT JOIN ".TB_PREF."chart_master dc ON   
                        pa.debit_code = dc.account_code   
                LEFT JOIN ".TB_PREF."chart_master cc ON   
                        pa.credit_code = cc.account_code     
                WHERE 
                        sc.list_code = 'SALARY_ADVANCES'  
                ORDER BY sc.value_code ";
	
	return db_query($sql,"Salary advance accounts could not be retreived");
}

function get_salary_advance_account_detail($component_id)
{
	$sql = "SELECT sc.id component_id, 
                        l.list_name process_type_name, 
                        sc.value_code component_code,
                        sc.value_name component_name,
                        pa.id row_id,
                        pa.debit_code,
                        pa.credit_code 
		FROM "
                        .TB_PREF."list_values  sc 
                LEFT JOIN ".TB_PREF."lists l ON 
                        sc.list_code = l.list_code 
                LEFT JOIN ".TB_PREF."salary_advances_accounts pa ON   
                        sc.id = pa.component_id AND 
                        pa.component_id = ".db_escape($component_id)."  
                WHERE 
                        sc.id = ".db_escape($component_id);
	
	$result = db_query($sql,"Salary advance account detail could not be retreived");
        
        return db_fetch($result);
}


function get_salary_advance_types()
{
	$sql = "SELECT * 
		FROM ".TB_PREF."lists    
                WHERE list_code IN ('SALARY_ADVANCES')  
                ORDER BY list_name";
	
	$result = db_query($sql,"Salary advance types could not be retreived");
        
        return $result;
}

?>