<?php

function sc_calculate_payroll_summary($calculation_id, $calculation_type, 
                                        $month_id, $from_date, $to_date)
{
    delete_monthly_summary_payrolls($month_id, $calculation_type);
    
    $result = get_group_companies();
    
    while ($group_company = db_fetch($result)) {
        $db_name = $group_company['db_name'];
        $company_code = $group_company['company_code'];
        $payroll_code = 'MAIN';
        $payrol_id = get_payroll_id_from_group_company($db_name, $payroll_code);
        
        $timestamp_from = strtotime($from_date);
        $timestamp_to = strtotime($to_date);
        
        $mid_timestamp = ($timestamp_from + $timestamp_to)/2;
        $mid_date = date('Y-m-d', $mid_timestamp);
        
        $payroll_run = get_group_company_payroll_run_record($db_name, $payrol_id, $mid_date);
        $payroll_run_id = $payroll_run['id'];
        
        $departments_res = get_group_company_all_list_values($company_code, 'DEPARTMENTS');
        
        $ds = array();
        while ($department = db_fetch($departments_res)) {
            calculate_payroll_summary_department($ds, $db_name, $company_code, $department, $from_date, $to_date, $payroll_run_id);            
            
            foreach ($ds as $line) {
                add_to_monthly_summary_payroll($month_id, $calculation_type, $line['company_code'], $line['department_code'], 
                                            $line['value_code'], $line['value'], $calculation_id);
            }
            $ds = array();
        }
        
    }
    
    $d = 6;
}


function calculate_payroll_summary_department(&$ds, $db_name, $company_code, $department, $from_date, $to_date, $payroll_run_id)
{
    $employee_count = $salary_paid_employee_count = 0;
    
    $totals = array(); 
    $totals[SCVT_SALARY_PAID_EMPLOYEE_COUNT] = 0;
    $totals[SCVT_TOTAL_SALARY_COST] = 0;
//    $totals["TAKEHOME_INCENTIVE"] = 0;
    
    $department_code = $department['value_code'];
    
    $employee_codes_in = "'XXXXXXXXXXXXXXXXXXXXXXXXXX'";
    $result = get_group_company_department_employees($db_name, $department, $to_date);
    while ($row = db_fetch($result)){
        $employee_codes_in .= ", '".$row['employee_code']."'";
        
        $employee_count++;
    }
    
    calculate_payroll_summary_department_salaries($totals, $db_name, $payroll_run_id, $employee_codes_in);
    
    foreach ($totals as $key=>$value) {
        $c = array("company_code"=>$company_code,
                    "department_code"=>$department_code,
                    "value_code"=>$key,
                    "value"=>$value);
        $ds[] = $c;
    }
    
//    $totals = array();    
//    $totals["SALARY_PAID_EMPLOYEE_COUNT"] = 0;
//    $totals["TAKEHOME_BASIC"] = 0;
//    $totals["TAKEHOME_INCENTIVE"] = 0;
    
    
    $c = array("company_code"=>$company_code,
                    "department_code"=>$department_code,
                    "value_code"=>SCVT_DEPARTMENT_EMPLOYEE_COUNT,
                    "value"=>$employee_count);
    $ds[] = $c;
    
    $recruitments = get_group_company_department_recruitments_count($db_name, $department, $from_date, $to_date);
    $c = array("company_code"=>$company_code,
                    "department_code"=>$department_code,
                    "value_code"=>SCVT_DEPARTMENT_RECRUITMENT_COUNT,
                    "value"=>$recruitments);
    $ds[] = $c;
    
    $resignations = get_group_company_department_resignations_count($db_name, $department, $from_date, $to_date);
    $c = array("company_code"=>$company_code,
                    "department_code"=>$department_code,
                    "value_code"=>SCVT_DEPARTMENT_RESIGNATIONS_COUNT,
                    "value"=>$resignations);
    $ds[] = $c;
}


function calculate_payroll_summary_department_salaries(&$totals, $db_name, $payroll_run_id, $employee_codes_in)
{
    $TAKE_HOME_INCENTIVE_total = $TAKE_HOME_BASIC_total = $count = 0;
    
    $components_in = "'TAKEHOME_BASIC',
                        'BASIC_SALARY',
                        'BRA_1',
                        'BRA_2',
                        'INCENTIVE',
                        'BANKING_INCENTIVE_II',
                        'BANKING_INCENTIVE',
                        'BANKING_INCENTIVE_I',
                        'VEHICLE_REIMBURSEMEN',
                        'FUEL_ALLOWANCES',
                        'MEAL_REIMBURSEMENT',
                        'SITE_EXPENSES',
                        'TRAVELLING_ALLOWANCE',
                        'SPECIAL_PAYMENT',

                        'INCENTIVE_ARREARS',
                        'FUEL_REIMBURSEMENT_T',
                        'MEAL_REIMBURSEMENT_T',
                        'COMMISSION',
                        'SAVINGS_COMMISSION',
                        'DRIVING_ALLOWANCE',
                        'SPECIAL_ATTENDANCE',
                        'SPECIAL_PAYMENT1',
                        'ATTENDANCE_INCENTIVE',
                        'POWDER_COATING_ALL',      
                        'EPF 12%',
                        'ETF 3%',
                        'OT_TOTAL'";
    
    $total_salary_cost = get_group_company_employees_total_salary_cost($db_name, $payroll_run_id, $employee_codes_in, $components_in);
//    while ($row = db_fetch($result)){
//        switch ($row['component']) {
//            case "TAKEHOME_BASIC":
//                $TAKE_HOME_BASIC_total += $row['value'];
//                break;
//            case "TAKEHOME_INCENTIVE":
//                $TAKE_HOME_INCENTIVE_total += $row['value'];
//                break;
//            default:
//                continue;
//        }        
//        
//        $count++;
//    }
    
    
    $count = get_group_company_department_salary_paid_employee_count($db_name, $payroll_run_id, $employee_codes_in, $components_in);   
    $totals[SCVT_SALARY_PAID_EMPLOYEE_COUNT] = $count;
    
    
    $totals[SCVT_TOTAL_SALARY_COST] = $total_salary_cost;
//    $totals["TAKEHOME_INCENTIVE"] += $TAKE_HOME_INCENTIVE_total;
    
}

