<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPROVE_PAYROLL_ATTENDANCE_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 . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_attendance_db.inc");
include_once($path_to_root . "/payroll/includes/db/salary_deductions_db.inc");
include_once($path_to_root . "/payroll/includes/db/approve_salary_adjustments_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");

include_once($path_to_root . "/includes/db/module_settings_db.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_run_db.inc");
        
include_once($path_to_root . "/payroll/includes/db/payroll_attendance_db.inc");
if (!@$_GET['popup'])
{
	$js = "";
	if ($SysPrefs->use_popup_windows)
		$js .= get_js_open_window(900, 600);
	if (user_use_date_picker())
		$js .= get_js_date_picker();
}
if (!@$_GET['popup'])
	page(_($help_context = "Payroll Attendance"));

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

$payroll_run = get_payroll_run_details($payroll_run_id);

$editable = false;
if($payroll_run){
        $s = $payroll_run["status"];                    
        if($s == "N" || $s == "U" || $s == "C" || $s == "L"){
            $editable = true;
        }
}


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

function display_record($myrow, $payroll_run_id, &$k, $editable = false)
{
//    alt_table_row_color($k);
    $dec = 2;
    $status_code = '';
    $comments = '';
    $status_name = '';

    $prefix = $myrow["employee_code"]."_".$myrow["component"]."_".$payroll_run_id."_";
    if($editable == true && ($status_code == ATD_NEW || $status_code == ATD_UPDATED || $status_code == '')){
        $editable = true;
    }
    else{
        $editable = false;
    }
    label_cell($myrow["employee_code"], "align=center");
    label_cell($myrow["initials"]. " " . $myrow["last_name"]." (".$myrow["first_name"].")");
    label_cell($myrow["value_name"], "align=center");
    label_cell($myrow["calculated_days"], "align=right"); 

    if($myrow["status_code"] == "N" || $myrow["status_code"] == "U" || $myrow["status_code"] == ""){
        qty_cells("", $prefix."granted_days", $myrow["granted_days"], null, null, $dec);
    }
    elseif($myrow["status_code"] == "O"){
        qty_cells("", $prefix."granted_days", $myrow["granted_days"], null, null, $dec);
        $status_code = $myrow["status_code"];
    }
    else{
        label_cell($myrow["granted_days"], "align=right");
//        label_cell($myrow["status_name"], "align=center");
//        label_cell("", "align=center");
    }
    
    $comments = $myrow['comment'];
    $status_name = $myrow['status_name'];
    if($myrow["status_code"] == "N" || $myrow["status_code"] == "U" || $myrow["status_code"] == ""){
        //text_cells(null, $prefix."comments", $myrow["comment"]);
        label_cell(nl2br($comments));
    }
    else{
        label_cell(nl2br($comments));
    }

    hidden($prefix."comments", $myrow["comment"]);
    
    label_cell($status_name);

    end_row();
//    return $total = $total + $mobile_bill;
     
}

function save_payroll_attendance($payroll_run_id, $process = false)
{
    $result = get_payroll_attendance_details($payroll_run_id);

    while ($myrow = db_fetch($result)){
            
        $status_code = '';
        $status_code = $myrow['status_code'];
        $prefix = $myrow["employee_code"]."_".$myrow["component"]."_".$payroll_run_id."_";
        $comments = '';
        
        
        if(isset($_POST[$prefix."comments"])){
            $comments = $_POST[$prefix."comments"];
        }
        if (!check_num($prefix."granted_days", 0.0)){
            continue;
        }
        else{
            $granted_days = input_num($prefix."granted_days");
        }
        
        if($status_code == ATD_NEW || $status_code == ATD_UPDATED){  
                if($status_code == ATD_NEW){
                    $status_code = ATD_UPDATED;
                }
                update_payroll_attendance($myrow['id'], $granted_days, $comments, $status_code);
        }
        
        if($process && ($status_code == ATD_NEW || $status_code == ATD_UPDATED)){
                approve_payroll_attendance($myrow['id']);
            }
	}   
}

$Ajax->activate('show_heading');
$Ajax->activate('allowances');


if (isset($_POST['process'])){
    save_payroll_attendance($payroll_run_id, true);
}

if (isset($_POST['addupdate'])){
    save_payroll_attendance($payroll_run_id);
}

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

$Ajax->activate('allowances');

if (!@$_GET['popup'])
	start_form();

if (!isset($_POST['stock_id']))
	$_POST['stock_id'] = get_global_stock_item();

if (!@$_GET['popup'])
{
    payroll_run_list_row("Payroll Run:", 'payroll_run_id', false,"",true);
    submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
//	echo "<hr></center>";
}
br(2);

div_start('allowances');
br(1);

start_table(TABLESTYLE, "width=80%");

$th = array(_("employee Code"),_("Name"),_("Component"), _("Calculated Days"), _("Granted Days"),_("Comment"),
    _("Status"));

$j = 1;
$k=0; //row colour counter

$result = get_payroll_attendance_details($_POST['payroll_run_id']);

if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}
//$total = 0;
//$previous_mobile_no_prefix = '';
//$current_mobile_no_prefix = '';
$show_buttons = false;
table_header($th);
while ($myrow = db_fetch($result))
{

    display_record($myrow, $payroll_run_id, $k, $editable);
    if($myrow["status_code"] == "N" || $myrow["status_code"] == "U" || $myrow["status_code"] == ""){
        $show_buttons = true;
    }

        
}
alt_table_row_color($k);
end_table(1);

if($editable){
    if($show_buttons == true)
    {
        submit_center_first('addupdate', _("Update"), '', @$_REQUEST['popup'] ? true : 'default');
        submit_center_last('process', _("Approve"), '', @$_REQUEST['popup'] ? true : 'default');
    }
    else
    {
        //submit_center_first('addupdate', _("Update"), '', @$_REQUEST['popup'] ? true : 'default');
    }
}
div_end();
if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}
?>
