<?php

function add_payroll_account($payroll_id, $department_id, $component_id, $debit_code, $credit_code) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');

    $sql = "INSERT INTO ".TB_PREF."payroll_accounts (
                payroll_id, department_id, component_id,  
                debit_code, credit_code,
                updated_by, updated_on) 
            VALUES ("
                .db_escape($payroll_id).", ".db_escape($department_id).", ".db_escape($component_id).", "
                .db_escape($debit_code).", ".db_escape($credit_code).", "
                .db_escape($created_by).", ".db_escape($created_on).")";

    db_query($sql,"Payroll account could not be updated");

    return db_insert_id();
}

function update_payroll_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."payroll_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,"Payroll account could not be updated");	
    
}

function get_payroll_accounts($payroll_id, $department_id, $process_code) {
    $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."payroll_accounts pa 
                    ON sc.id = pa.component_id 
                        AND pa.payroll_id = ".db_escape($payroll_id)." 
                        AND pa.department_id = ".db_escape($department_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 = ".db_escape($process_code)." 
            ORDER BY sc.value_code ";

    return db_query($sql, "Payroll accounts could not be retreived");
}

function get_payroll_account_detail($payroll_id, $department_id, $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."payroll_accounts pa 
                ON sc.id = pa.component_id 
                    AND pa.payroll_id = ".db_escape($payroll_id)." 
                    AND pa.department_id = ".db_escape($department_id)." 
                    AND pa.component_id = ".db_escape($component_id)."  
            WHERE sc.id = ".db_escape($component_id);

    $result = db_query($sql, "Payroll accounts could not be retreived");

    return db_fetch($result);
}

function delete_payroll_account($selected_id)
{
//	$sql = "DELETE FROM ".TB_PREF."salary_components  
//                WHERE id = ".db_escape($selected_id);	
//	
//	db_query($sql,"Salary component could not be deleted");
}

function get_payroll_process_types()
{
	$sql = "SELECT * 
		FROM ".TB_PREF."lists    
                WHERE list_code IN ('COMPANY_ADDITION_SALARY_COMP', 
                                    'COMPANY_DEDUCTION_SALARY_COMP', 
                                    'GROUP_ADDITION_SALARY_COMP', 
                                    'GROUP_DEDUCTION_SALARY_COMP',
                                    'EMPLOYEE_ADDITION_SALARY_COMP',
                                    'EMPLOYEE_DEDUCTION_SALARY_COMP',
                                    'EMPLOYEE_ALLOWANCES_OVER_USAGE',
                                    'ATTENDANCE_ALLOWANCES',
                                    'WORKTIME_DEFICITS',
                                    'PERFORMANCE_RATE_INCENTIVES',
                                    'PERFORMANCE_VALUE_INCENTIVES',
                                    'MOBILE_ALLOWANCES_OVER_USAGE',
                                    'SALARY_ADDITIONS',
                                    'SALARY_ADVANCES',
                                    'SALARY_DEDUCTIONS',
                                    'SALARY_GROUP_TOTALS',
                                    'SALARY_TOTALS',
                                    'COMMON_PAYROLL_PROCESSES')  
                ORDER BY list_name";
	
	$result = db_query($sql,"Payroll process types could not be retreived");
        
        return $result;
}

function get_defined_payroll_accounts($payroll_type_code, $department_code)
{
    $sql = "SELECT pa.*,
                    cn.value_code component_code,
                    cn.value_name component_name 
            FROM ".TB_PREF."payroll_accounts pa 
                JOIN ".TB_PREF."list_values dn  
                    ON pa.department_id = dn.id 
                JOIN ".TB_PREF."list_values cn  
                    ON pa.component_id = cn.id 
                JOIN ".TB_PREF."list_values ptn    
                    ON pa.payroll_id = ptn.id  
            WHERE ptn.value_code = ".db_escape($payroll_type_code)." 
                    AND dn.value_code = ".db_escape($department_code)." 
                    AND (pa.debit_code <> '' OR credit_code <> '')"; 

    return db_query($sql, "get_defined_payroll_accounts");
}


function get_hr_db_mismatching_departments($db)
{
    $sql = "SELECT lps_dep.value_code hr_value_code, lps_dep.value_name hr_value_name, 
                    saw_dep.value_code, saw_dep.value_name 
            FROM ".$db.".".TB_PREF."list_values lps_dep 
                LEFT JOIN ".TB_PREF."list_values saw_dep 
                    ON lps_dep.value_code = saw_dep.value_code 
                    AND saw_dep.list_code = 'DEPARTMENTS' 
            WHERE lps_dep.list_code = 'DEPARTMENTS' 
                AND saw_dep.value_code IS NULL ";
    
    return db_query($sql, "get_hr_db_mismatching_departments");
}