<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPROVE_PAYROLL_WORKTIME_DEFICIT_SUMMARY';
if (!@$_GET['popup'])
    $path_to_root = "..";
else	
    $path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/payroll/includes/ui/payroll_work_time_deficits_ui.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_work_time_deficits_db.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_attendance_db.inc");
include_once($path_to_root . "/payroll/includes/db/attendance_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc"); 
include_once($path_to_root . "/payroll/includes/db/payroll_run_db.inc");   

$js = "";
$js .= get_js_open_window(900, 600);
$js .= get_js_date_picker();

page(_($help_context = "Payroll Worktime Deficits"));

$status = true;

$payroll_run_id = 0;
if (isset($_POST['payroll_run_id'])) {
    $payroll_run_id = $_POST['payroll_run_id'];
}

if (isset($_POST['Save'])) {
    $msg = save_payroll_worktime_deficits($payroll_run_id);
        if ($msg['success'] === false) {
            display_error($msg['message']);
            $status = false;
        } else {
            display_notification($msg['message']);
        }
}

if (isset($_POST['Approve'])) {
    $msg = save_payroll_worktime_deficits($payroll_run_id, true);
        if ($msg['success'] === false) {
            display_error($msg['message']);
            $status = false;
        } else {
            display_notification($msg['message']);
        }
}

if (list_updated('payroll_run_id') || isset($_POST['Refresh'])) {
    clear_payroll_work_time_deficit_of_all_employees($_POST['payroll_run_id']);
}

start_form();

    start_table();
        payroll_run_list_cells("Payroll Run:", 'payroll_run_id', false, "", true);
        submit_cells('Refresh', 'Refresh');
    end_table(2);
    
    $pr_row = get_payroll_run_details($_POST['payroll_run_id']);
        if (is_array($pr_row)) {
            $Ajax->activate('payroll_worktime_deficit');
            div_start('payroll_worktime_deficit');
                display_payroll_worktime_deficit_details($_POST['payroll_run_id'], $pr_row['period_start_date'], $pr_row['period_end_date'], $status);
            div_end();

            submit_center_first('Save', _("Save"), '', 'default');
            submit_center_last('Approve', _("Approve"), false, false, ICON_SUBMIT);
        } else {
            display_note("Please select a Payroll Run", 2, 2, "style=color:red");
        }

end_form(2);
end_page(@$_GET['popup'], false, false);

?>