<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PAYROLL_VIEW';
$path_to_root = "../..";

include($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/db/process_payroll_calculation_db.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_calculation_db.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_run_db.inc");
include_once($path_to_root . "/payroll/includes/ui/calculate_payroll_departments_summary_ui.inc");
include_once($path_to_root . "/payroll/includes/db/calculate_payroll_departments_summary_db.inc");



$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "View Payroll Summary"), true, false, "", $js);


if (isset($_GET["trans_no"]))
{
	$payroll_run_id = $_GET["trans_no"];
}

$db_name = '';
if (isset($_GET["comp"]))
{
	$db_name = $_GET["comp"];
}

function display_payroll_run($payroll_run_id, $db_name)
{
    $payroll = view_payroll_run_details($payroll_run_id, $db_name);
	 	 	 	 	 	 	 	 	 	
    start_table(TABLESTYLE, "width=50%");
        label_cells("Payroll Name", $payroll['payroll_name'], "class='tableheader2'", "width=200px");
        end_row();
        start_row();  
        label_cells("Description", $payroll['description'], "class='tableheader2'", "width=200px");
        end_row();
        start_row();
        label_cells("Period Start Date", sql2date($payroll['period_start_date']), "class='tableheader2'", "width=200px");
        end_row();
        start_row();
        label_cells("Period End Date", sql2date($payroll['period_end_date']), "class='tableheader2'", "width=200px");
        end_row();
        start_row();
        label_cells("Transaction Date", sql2date($payroll['tran_date']), "class='tableheader2'", "width=200px");
        end_row();
        start_row();
        $link_id = '';
        if($payroll["linked_payroll_run_id"] > 0){
            $link_id = get_payroll_trans_view_str(ST_PAYROLL, $payroll["linked_payroll_run_id"], "", false, '', '', $db_name);
        }
        label_cells("Linked Payroll Run", $link_id, "class='tableheader2'", "width=200px");
        end_row();
        start_row();  
        label_cells("Status", $payroll['payroll_run_status_name'], "class='tableheader2'", "width=200px");
        end_row();
        start_row();  
        label_cells("Run By", $payroll['real_name'], "class='tableheader2'", "width=200px");
        end_row();
        start_row();  
        label_cells("Run Started Time", $payroll['start_time'], "class='tableheader2'", "width=200px");
        end_row();
        start_row();  
        label_cells("Run Completed Time", $payroll['end_time'], "class='tableheader2'", "width=200px");
        end_row();
        start_row();  
        label_cells("Last Completed Employee", $payroll['last_completed_employee_code'], "class='tableheader2'", "width=200px");
        end_row();

    end_table();  
    
}

function display_payroll_components_departments_summary($payroll_run_id, $db_name) 
{      
//    $payroll_run = get_payroll_run_details($payroll_run_id);
//    $payroll_run_id = $payroll_run['id'];
        
    
    
    start_table(TABLESTYLE, "width=50%");
        $th = array(_("ID"), _("Process Code"), _("Process Name"), _("Amount"));
        table_header($th);

            $result = select_departments_components_summary($payroll_run_id, $db_name);

            $department_name = null;
            $department_total = 0;
            $k = $j = 0; //row colour counter
             
            while ($myrow = db_fetch($result)) {

                if ($department_name != $myrow["department_name"]) {
                    if ($department_name != null && $department_name != $myrow["department_name"]) {
                        start_row();
                            label_cell('<b>Total:<b/>', 'align=right colspan=3');
                            qty_cell($department_total, true, 2);
                        end_row();
                        $department_total = 0;
                    }
                    
                    start_row();
                        label_cell('<b>'.$myrow["department_name"].'<b/>', 'align=center colspan=5');
                    end_row();
                }
                
                alt_table_row_color($k);
                    label_cell($myrow["id"], 'align=center');
                    label_cell($myrow["component"]);
                    label_cell($myrow["process_name"]);     

                    qty_cell($myrow["value"], false, 2);
                end_row();
                
                $department_name = $myrow["department_name"];
                $department_total += $myrow["value"];
            } 
            
            start_row();
                label_cell('<b>Total:<b/>', 'align=right colspan=3');
                qty_cell($department_total, true, 2);
            end_row();
    end_table();
}


function display_payroll_summary($payroll_run_id, $db_name)
{
    $result = select_payroll_run_summary($payroll_run_id, $db_name);
    $k = 0;
        
    $th = array(_("Id"), _("Component Code"), _("Component Name"), _("Value"));

    start_table(TABLESTYLE, "width=60%");
    table_header($th);
                    
    while ($myrow = db_fetch($result))
    {

        start_row();

        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);

        end_row();


    }

    end_table(1);
}

br();

display_heading("Payroll Run #$payroll_run_id");

br(1);

display_payroll_run($payroll_run_id, $db_name);

br(1);
display_heading("<b>Payroll Department Summaries</b>");
    
br(1);

display_payroll_components_departments_summary($payroll_run_id, $db_name);
//display_calculate_payroll_components_summary($payroll_run_id);   

br(1);

display_heading("Payroll Summary");
br(1);
//display_payroll_summary($payroll_run_id, $db_name);
display_get_all_departments_components_summary($payroll_run_id, $db_name);


end_page(true, false, false, ST_PAYROLL, $payroll_run_id);
?>