<?php

function display_calculate_payroll_components_summary($payroll_run_id) {      
    $payroll_run = get_payroll_run_details($payroll_run_id);
//    $payroll_run_id = $payroll_run['id'];
        
    display_heading("<b>Payroll Department Summaries</b>");
    br();
    
    start_table(TABLESTYLE, "width=50%");
        $th = array(_("ID"), _("Process Code"), _("Process Name"), _("Amount"));
        table_header($th);

            $result = get_departments_components_summary($payroll_run_id);

            $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(3);
}

function display_get_all_departments_components_summary($payroll_run_id, $db_name='') 
{   
    $payroll_run = get_payroll_run_details($payroll_run_id, $db_name);
    $payroll_run_id = $payroll_run['id'];

    display_heading("<b>Company Summary</b>");
    br();
        
    start_table(TABLESTYLE, "width=80%");
	$th = array(_("ID"), _("Process Code"), _("Process Name"), _("Company Summery"), _("All Departments Summary"), _("Difference"));
        table_header($th);
        
        $result = select_payroll_run_summary($payroll_run_id, $db_name);
        
        $k = $j = 0; //row colour counter
        while ($myrow = db_fetch($result)) {
            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);

                $dep_com_total = get_all_departments_component_total_by_component($payroll_run_id, $myrow["component"]);
                if (isset($dep_com_total['value'])) {
                    qty_cell($dep_com_total['value'], false, 2);
                } else {
                    label_cell('');
                    $dep_com_total['value'] = 0;
                }
                
                qty_cell($dep_com_total['value'] - $myrow["value"], false, 2);
            end_row();
        } 

    end_table(3);
}

function calculate_payroll_components_summary($payroll_run_id) 
{
    $payroll_run = get_payroll_run_details($payroll_run_id);
    
//    $result = get_payroll_calculation_seq($payroll_run['payroll_id']);
    $result = select_payroll_run_summary($payroll_run_id);
    if (! db_num_rows($result) > 0) {
        return array("passed" => false, "message" => _("No applicable components found in the payroll calculation sequence."));
    }
    
    while ($myrow = db_fetch($result)) {
        $pcs[] = array('process_code' => $myrow['component'], 'process_type' => $myrow['process_type']);
    } 
    
    delete_payroll_departments_summary($payroll_run_id);
    
    $departments = get_all_list_values('DEPARTMENTS');
    
    while ($department = db_fetch($departments)) {
        foreach ($pcs as $process) {
            $com_total = get_department_component_total_by_payroll_tran_date($department['id'], $payroll_run['tran_date'], $payroll_run['id'], $process['process_code']);
            if ($com_total) {
                $component_total = $com_total['component_total'];
                if ($component_total != 0) {
                    insert_payroll_departments_summaries($payroll_run['id'],  $department['value_code'], $process['process_code'], $component_total);
                }
            }
        }
    }

    foreach ($pcs as $process) {
        $total = get_all_departments_component_total_by_payroll_run_id($payroll_run['id'], $process['process_code']);
        insert_payroll_departments_summaries($payroll_run['id'], '', $process['process_code'], $total['all_departments_component_total']);
    }    
}