<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPROVE_PAYROLL';
$path_to_root = "..";
include_once($path_to_root . "/includes/session.inc");

//page(_($help_context = "Approve Payroll Run"));

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");

//below file should have been included by the system already. But it is not. So included manually. Once the issues found remove this inclusion
include_once($path_to_root . "/payroll/includes/process_payroll_calculation.inc");
include_once($path_to_root . "/payroll/includes/db/process_payroll_calculation_db.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_calculation_db.inc");

//check_db_has_purchasable_items(_("There are no inventory items defined in the system."));

	$js = "";
	if ($SysPrefs->use_popup_windows){
		$js .= get_js_open_window(900, 500);
    }
    
page(_($help_context = "Approve Payroll Run"), false, false, "", $js);
simple_page_mode(true);
//--------------------------------------------------------------------------------------------------


//function get_calculated_salary_additions($process_id, $employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_salary_additions($process_id, $employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["due_date"])." - "
//                .$myrow["salary_addition"]."<br>";
//    }
//
//    return $s;
//}
//
//
//function get_calculated_salary_deductions($process_id, $employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_salary_deductions($process_id, $employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["due_date"])." - "
//                .$myrow["salary_deduction"]."<br>";
//    }
//
//    return $s;
//}
//
//
//function get_calculated_nopay_deductions($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_nopay_deductions($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date"])." - ".sql2date($myrow["to_date"])." - "
//                .$myrow["nopay_days"]." - "
//                .$myrow["nopay_amount"]."<br>";
//    }
//
//    return $s;
//}
//
//function get_calculated_worktime_deficit_deductions($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_worktime_deficit_deductions($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date_time1"])." - "
//                .substr($myrow["deducted_deficit_time"],-8,5)." - "
//                .$myrow["deficit_amount"]."<br>";
//    }
//
//    return $s;
//}
//
//function get_paid_ot_amounts($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_paid_ot_amounts($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date_time1"])." - "
//                .substr($myrow["granted_ot_time"],-8,5)." - "
//                .$myrow["ot_amount"]."<br>";
//    }
//
//    return $s;
//}
//
//
//function get_calculated_loan_installments($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_loan_installments($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= $myrow["loan_description"]." ("
//                .$myrow["loan_amount"].") - "
//                .$myrow["this_installment"]."<br>";
//    }
//
//    return $s;
//}


//function get_calculated_ot_amounts($employee_code, $payroll_run_id, $status_codes){
//    
//    $s = "";
//    $result = select_calculated_ot_amounts($employee_code, $payroll_run_id, $status_codes);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date_time1"])." - "
//                .substr($myrow["granted_ot_time"],-8,5)." - "
//                .$myrow["ot_amount"]."<br>";
//    }
//
//    return $s;
//}

function can_process($payroll_run_id)
{	
//    if (!check_num('upto', 0))
//    {
//        display_error( _("Up To value entered was not numeric or positive."));
//        set_focus('upto');
//        return false;
//    }
//
//    if (!check_num('calculation_value', 0))
//    {
//        display_error( _("Calculation value entered was not numeric or positive."));
//        set_focus('calculation_value');
//        return false;
//    }
//    
//    if (!check_num('fixed_value'))
//    {
//        display_error( _("Fixed value entered was not numeric"));
//        set_focus('fixed_value');
//        return false;
//    }
    
    return true;

}

function dispaly_payroll_employee_result($payroll_run)
{
        $payroll_run_id = $payroll_run['id'];
    
        $result = select_payroll_employee_result($payroll_run_id);
    
        start_table(TABLESTYLE, "width=60%");

	$th = array(_("ID"), _("Component Code"), _("Component Name"),
                        _("Amount"), _("Comment"));

        $k = $j = 0; //row colour counter
        $previous_employee_code = "";

        while ($myrow = db_fetch($result))
        {
            $employee_code = $myrow["employee_code"];
            if($previous_employee_code != $employee_code){
                table_header($th);
                
                $name_row = select_employee_name($employee_code);
                $emp_name = "[".$employee_code."] - ".$name_row["initials"]." ".$name_row["last_name"]." (".$name_row["first_name"].")";
                label_cell("<b>".$emp_name."</b>", "align=center colspan=5");
            }
            alt_table_row_color($k);
            
            $process_type = $myrow['process_type'];
            $process_id = $myrow['process_id'];
            $component = $myrow['component'];

            label_cell($myrow["id"]);
            label_cell($myrow["component"]);
            label_cell($myrow["process_name"]);   

            $dec = 2;       
            qty_cell($myrow["value"], false, $dec);
            label_cell(nl2br($myrow["comment"])); 
            
            $details = "";
            
//            switch ($process_type){
//                case 'WORKTIME_DEFICITS':
//                    if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                            $status_code = 'L';}
//                    elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                            $status_code = 'Y';}
//
//                    //$details = get_calculated_worktime_deficit_deductions($employee_code, $payroll_run_id, $status_code);
//                    break;
//                case 'SALARY_DEDUCTIONS':
//                    if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                            $status_code = 'L';}
//                    elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                            $status_code = 'Y';}
//
//                    //$details = get_calculated_salary_deductions($process_id, $employee_code, $payroll_run_id, $status_code);
//                    break;
//                case 'SALARY_ADDITIONS':
//                    if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                            $status_code = 'L';}
//                    elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R" || $payroll_run['status'] == "T"){
//                            $status_code = 'Y';}
//
//                    //$details = get_calculated_salary_additions($process_id, $employee_code, $payroll_run_id, $status_code);
//                    break;
//                case 'COMMON_PAYROLL_PROCESSES':    
//                    switch ($component){
//                        case 'LOAN_INSTALLMENTS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_code = 'L';}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_code = 'Y';}
//
//                            //$details = get_calculated_loan_installments($employee_code, $payroll_run_id, $status_code);
//                            break;
//                        case 'NO_PAY_DEDUCTIONS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_code = 'L';}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_code = 'Y';}
//
//                            //$details = get_calculated_nopay_deductions($employee_code, $payroll_run_id, $status_code);
//                            break;
//                        case 'OT_CALCULATIONS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_codes = "'L'";}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_codes = "'D', 'S', 'Y'";}
//
//                            //$details = get_calculated_ot_amounts($employee_code, $payroll_run_id, $status_codes);
//                            break;
//                        case 'GET_OT_PAYMENTS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_code = 'S';}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_code = 'Y';}
//                            //$details = get_paid_ot_amounts($employee_code, $payroll_run_id, $status_code);
//                            break;
//
//                        default:
//                }
//                default:
//            }
            
            //label_cell($details);

            end_row();
            
            $previous_employee_code = $myrow["employee_code"];

        } //end of while loop

	end_table();
}


function display_payroll_run_summary($payroll_run)
{               
        $payroll_run_id = $payroll_run['id'];
        
        $result = select_payroll_run_summary($payroll_run_id);
        
//        label_cell("<b>"." Payroll Summary "."</b>", "align=center colspan=5");
        display_heading("Payroll Summary");
        
        start_table(TABLESTYLE, "width=50%");

	$th = array(_("ID"), _("Process Code"), _("Process Name"),
                        _("Amount"), _(""));

        table_header($th);
        
        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {

            alt_table_row_color($k);

            label_cell($myrow["id"]);
            label_cell($myrow["component"]);
            label_cell($myrow["process_name"]);   
                $dec = 2;       

            qty_cell($myrow["value"], false, $dec);
            
            $comment = "";
            if($payroll_run['status'] == "R" && is_numeric($myrow['comment'])){
                $comment = get_gl_view_str(ST_PAYROLL, $payroll_run['id']);
            }
            
            label_cell($comment, 'align=center');

            end_row();

        } //end of while loop

	end_table();
}

if(isset($_POST['apply_payroll_calculations']) && can_process($_POST['payroll_run_id'])) 
{
    process_payroll($_POST['payroll_run_id']);

}

//--------------------------------------------------------------------------------------------------



if ($Mode == 'RESET')
{
	$selected_id = -1;
	unset($_POST);
}

if (list_updated('payroll_run_id')) 
	$Ajax->activate('code_table');

//--------------------------------------------------------------------------------------------------

start_form();

//if (!isset($_POST['table_id']))
//        $_POST['table_id'] = get_global_table_id ();

start_table(TABLESTYLE2);
payroll_run_list_row("Payroll Run:", 'payroll_run_id', false,"", true,"all");
end_table(1);
$payroll_run = get_payroll_run_details($_POST['payroll_run_id']);

$payroll_run_id = $_POST['payroll_run_id'];


//set_global_table_id($_POST['table_id']);


div_start('code_table');
	
if($payroll_run_id > 0){
    br(2);
    dispaly_payroll_employee_result($payroll_run);   
    br(2);
    display_payroll_run_summary($payroll_run);        
}else{
    display_note("Please select a payroll run");
}
        
div_end();

//-----------------------------------------------------------------------------------------------
br();

if ($selected_id != '') {

} 

$s = "";
if($payroll_run_id > 0){
    $pr_row = select_payroll_run_status($payroll_run_id);
    $s = $pr_row['status'];
}

if($s == "L" || $s == "P" || $s == "T"){ 
    submit_center_first("apply_payroll_calculations", "Process Payroll Calculations" , '', 'default'); 
}

end_form(2);
end_page();

?>
