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


function add_calculation_seq($payroll_id, $list_value_id,
                            $seq_no, $process_code, 
                            $print_ref, $print_seq, $active){
	                
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
     	
    $sql = "INSERT INTO ".TB_PREF."payroll_calculation_seq(
                        payroll_id, list_value_id,
                        seq_no, process_code,
                        print_ref, print_seq, active, 
                        updated_by, updated_on)
            VALUES(".db_escape($payroll_id).", ".db_escape($list_value_id).", "
                    .db_escape($seq_no).", ".db_escape($process_code).", "
                    .db_escape($print_ref).", ".db_escape($print_seq).", ".db_escape($active).", "
                    .db_escape($updated_by).", ".db_escape($updated_on).")";
	
	db_query($sql,"Calculation sequence could not be updated");
        return db_insert_id();
}

function get_calculation_seqs($payroll_id)
{  	
        $sql = "SELECT 
                    lv.id, lv.list_code, lc.list_name, lv.value_code, lv.value_name, lv.value_description,
                    pcs.active, pcs.seq_no, pcs.print_ref, pcs.print_seq 
                FROM ".TB_PREF."list_values lv 
                        INNER JOIN ".TB_PREF."lists lc ON 
                                    lv.list_code = lc.list_code 
                        LEFT JOIN ".TB_PREF."payroll_calculation_seq pcs ON
                                    pcs.list_value_id = lv.id AND 
                                    pcs.payroll_id = ".db_escape($payroll_id)." 
                WHERE lv.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_TOTALS',
                                    'SALARY_GROUP_TOTALS',
                                    'COMMON_PAYROLL_PROCESSES')  
                ORDER BY pcs.active DESC, pcs.seq_no, lc.list_name, value_code ";
                
	return db_query($sql,"Calculation sequences could not be retreived");
}

function get_calculation_seq_detail($selected_id)
{
	$sql = "SELECT *    
		FROM "
                    .TB_PREF."payroll_calculation_seq    
                WHERE 
                    id = ".db_escape($selected_id);	
	
	$result = db_query($sql,"Calculation sequence detail could not be retreived");
        return db_fetch($result);
}

function delete_calculation_seqs($payroll_id) {
    $sql = "DELETE FROM ".TB_PREF."payroll_calculation_seq    
            WHERE payroll_id = ".db_escape($payroll_id);	
	
	db_query($sql, "delete_calculation_seqs");
        return db_num_affected_rows();
}

function get_payroll_calculation_seqs_by_payroll_id($payroll_id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."payroll_calculation_seq    
            WHERE payroll_id = ".db_escape($payroll_id);	
	
	return db_query($sql, "get_payroll_calculation_seqs_by_payroll_id");
}

?>