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

function add_payroll_run($linked_payroll_run_id, $payroll_id, $description, $payroll_type,  
                    $period_start_date, $period_end_date, 
                    $tran_date, $status)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        
	$sql = "INSERT INTO ".TB_PREF."payroll_runs (
                    linked_payroll_run_id, 
                    payroll_id, description,   
                    period_start_date, period_end_date, 
                    tran_date, status, 
                    created_by, created_on) 
                VALUES ("
                    .db_escape($linked_payroll_run_id).", "
                    .db_escape($payroll_id).", ".db_escape($description).", "
                    .db_escape($period_start_date). ", ".db_escape($period_end_date).", "
                    .db_escape($tran_date).", ".db_escape($status).", "
                    .db_escape($created_by).", ".db_escape($created_on).")";
        
        db_query($sql,"Payroll run could not be added");

}


function update_payroll_run($rec_id, $linked_payroll_run_id, $payroll_id, $description, $payroll_type,  
                    $period_start_date, $period_end_date, 
                    $tran_date, $status)
{

         $updated_by = $_SESSION['wa_current_user']->user;
         $updated_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."payroll_runs 
                SET 
                    payroll_id=".db_escape($payroll_id).",
                    linked_payroll_run_id=".db_escape($linked_payroll_run_id).",
                    description=".db_escape($description).",
                    period_start_date =".db_escape($period_start_date).",
                    period_end_date =".db_escape($period_end_date).",
                    tran_date =".db_escape($tran_date).",
                    status =".db_escape($status).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)." 
                WHERE id=".db_escape($rec_id);

        db_query($sql,"Payroll run could not be added");
            
}

function get_payroll_runs(){
    	$sql = "SELECT pr.*, 
                        lv.value_name payroll_name,
                        lv1.value_name payroll_run_status_name,
                        IF(pr.run_by = 0, '', (SELECT real_name 
                                                FROM ".TB_PREF."users u 
                                                WHERE pr.run_by = u.id)) real_name 
                FROM ".TB_PREF."payroll_runs pr,
                        ".TB_PREF."list_values lv,
                        ".TB_PREF."list_values lv1 
                WHERE pr.payroll_id = lv.id AND 
                        pr.status = lv1.value_code AND lv1.list_code = 'PAYROLL_RUN_STATUS' 
                ORDER BY id ";
	
	$result = db_query($sql,"Could not retrieve payrolls");	
        
        return $result;
    
}

function get_payroll_run_details($selected_id, $db_name='')
{
   if((strlen($db_name) > 1)){
        $db = $db_name.".";
    }
    else{
        $db = '';
    }
    	$sql = "SELECT pr.*, lv.value_name payroll_name     
                FROM ".$db.TB_PREF."payroll_runs pr,
                        ".$db.TB_PREF."list_values lv  
                WHERE pr.payroll_id = lv.id AND 
                        pr.id = ".db_escape($selected_id);
	
	$result = db_query($sql,"Could not retrieve payroll run details");	
        
        return db_fetch($result);
    
}

function view_payroll_run_details($selected_id, $db_name='')
{
    if((strlen($db_name) > 1)){
        $db = $db_name.".";
    }
    else{
        $db = '';
    }
    
    $sql = "SELECT pr.*, 
                    lv.value_name payroll_name,
                    lv1.value_name payroll_run_status_name,
                    IF(pr.run_by = 0, '', (SELECT real_name 
                                            FROM ".$db.TB_PREF."users u 
                                            WHERE pr.run_by = u.id)) real_name 
            FROM ".$db.TB_PREF."payroll_runs pr,
                    ".$db.TB_PREF."list_values lv,
                    ".$db.TB_PREF."list_values lv1 
            WHERE pr.payroll_id = lv.id AND 
                    pr.status = lv1.value_code AND lv1.list_code = 'PAYROLL_RUN_STATUS' AND 
                    pr.id = ".db_escape($selected_id);
	
	$result = db_query($sql,"Could not view_payroll_run_details");	
        
    return db_fetch($result);
    
}


function get_process_codes()
{  
	$sql = "SELECT process_code  
                FROM ".TB_PREF."payroll_calculation_seq   
		ORDER BY seq_no";
	
	$result = db_query($sql,"Could not retrieve process codes");	
        
        return $result;
}

function get_all_employee_codes()
{  
	$sql = "SELECT employee_code 
                FROM ".TB_PREF."employee_master  
		ORDER BY employee_code";
	
	$result = db_query($sql,"Could not retrieve employee codes");	
        
        return $result;
}


function get_next_payroll_run_id()
{     
        return rand(1000, 9999);
}


function insert_log_message($payroll_run_id, $payroll_type, $message_type, $message_details)
{  
	$added_by = $_SESSION['wa_current_user']->user;
        $added_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."payroll_log(
                        payroll_run_id, payroll_type,
                        message_type, message_details,
                        added_by, added_on) 
                VALUES(".$payroll_run_id.",".db_escape($payroll_type).","
                        .db_escape($message_type).",".db_escape($message_details).","
                        .$added_by.",".db_escape($added_on).")";
	
	db_query($sql,"Could not add log message");	
        
}


function update_payroll_run_joq_request_id($payroll_run_id, $jobq_id)
{
    $sql = "UPDATE ".TB_PREF."payroll_runs 
            SET jobq_request_id=".db_escape($jobq_id)." 
            WHERE id = ".db_escape($payroll_run_id);
    
    db_query($sql,"Could not update payroll run jobq id");
}

function update_payroll_link_id($linked_payroll_run_id, $selected_id)
{
    
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."payroll_runs 
            SET linked_payroll_run_id=".db_escape($linked_payroll_run_id).",  
                updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)." 
            WHERE id = ".db_escape($selected_id);
    
    db_query($sql,"Could not update payroll run link id");
    return db_num_affected_rows();
}

?>