<?php

function sc_calculate_employees_project_work($calculation_id, $calculation_type, 
                                        $month_id, $month_begin_date, $month_end_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'];
        
        delete_monthly_summary_project_work($month_id, $company_code);

        $mda = array();
//        build_month_days_array($epw, $from_date, $to_date);
//        
        if($company_code != 'SAW')
        {continue;}
        
        build_month_days_array($mda, $month_begin_date, $month_end_date);
        add_worktime_calendar_to_array($mda, $group_company, $month_begin_date, $month_end_date);
        add_holidays_to_array($mda, $group_company, $month_begin_date, $month_end_date);
        
        $emp_res = get_group_company_all_employees($company_code);       
        while ($employee = db_fetch($emp_res)) {
            $epw = $mda;  
            $summary = array();
            
            add_employee_project_work_details_to_array($epw, $group_company, $employee, $month_id, $month_begin_date, $month_end_date);            
            calculate_employee_project_work_summary($epw, $summary);
            save_employee_project_work_summary($month_id, $company_code, $employee['employee_code'], $calculation_id, $summary);
            
        }
    }
}

function save_employee_project_work_summary($month_id, $company_code, $employee_code, $calculation_id, $summary)
{
    foreach ($summary as $line) {
        add_to_monthly_summary_project_work($month_id, $company_code, $employee_code, $line['project_id'], $line['worked_days'], 
                                            $line['rate_category_id'], $line['rate'], $line['comment'], $calculation_id);
    }
}

function calculate_employee_project_work_summary($epw, &$summary)
{
    foreach ($epw as $date=>$project_work_line) {
        $added = false;
        if(!($project_work_line['project_id'] > 0 && $project_work_line['rate_category_id'] > 0 && $project_work_line['worked_days'] > 0)){
            continue;
        } 
        if(is_array($summary)){
            foreach ($summary as &$line) {
                if(isset($line['project_id']) && 
                    $line['project_id'] == $project_work_line['project_id'] && 
                        $line['rate_category_id'] == $project_work_line['rate_category_id'])
                    {
                        $line['worked_days'] += $project_work_line["worked_days"];
                        $text = $date." - ".$project_work_line['worked_days']."\r\n";
                        $line['comment'] .= $text;
                        $added = true;
                        break;
                    }        
            }
        }

        if($added){
            continue;
        }
        
        $text = $date." - ".$project_work_line['worked_days']."\r\n";
        $new_summary_line = array('project_id' => $project_work_line['project_id'], 
                                    'worked_days' => $project_work_line['worked_days'],
                                    'rate_category_id' => $project_work_line['rate_category_id'],
                                    'rate' => $project_work_line['rate'],
                                    'comment' => $text);
        
        $summary[] = $new_summary_line;
    }
}


function add_employee_project_work_details_to_array(&$epw, $group_company, $employee, $month_id, $month_begin_date, $month_end_date)
{
    $from_date = $to_date = '';
    
    add_employee_status_to_array($epw, $employee, $month_begin_date, $month_end_date, $from_date, $to_date);
    
    add_employee_leaves_to_array($epw, $group_company, $employee, $from_date, $to_date);
    
    add_employee_default_projects_to_array($epw, $group_company, $employee, $from_date, $to_date);
    
    add_employee_project_work_to_array($epw, $group_company, $employee, $from_date, $to_date);
    
}


function add_employee_project_work_to_array(&$epw, $group_company, $employee, $from_date, $to_date)
{
    $company_code = $group_company['company_code'];
    $employee_code = $employee['employee_code'];
    
    $result = get_employee_project_works($company_code, $employee_code, $from_date, $to_date);
    while ($project_work = db_fetch($result)) {
        
        $from = $project_work['from_date'];
        if($project_work['from_date'] < $from_date){
            $from = $from_date;
        }
        
        $to = $project_work['to_date'];
        if($project_work['to_date'] > $to_date){
            $to = $to_date;
        }
        
        $project_work_start = strtotime($from); 
        $project_work_end = strtotime($to); 
   
        for ($i=$project_work_start; $i<=$project_work_end; $i+=86400) {  
            $date = date("Y-m-d", $i); 
            $existing_values = $epw[$date];

            $possible_working_days = $existing_values['possible_working_days'];
        
            if($possible_working_days > 0){
                $epw[$date]['project_id'] = $project_work['project_id'];
                $epw[$date]['comment'] = 'WORKED_PROJECT';
            }
            else{
                $epw[$date]['project_id'] = $project_work['project_id'];
                $epw[$date]['comment'] = 'WORKED_PROJECT';
            }
            
        }
    }
}

function add_employee_default_projects_to_array(&$epw, $group_company, $employee, $from_date, $to_date)
{

    $company_code = $group_company['company_code'];
    $employee_code = $employee['employee_code'];
    
    $emp_project = get_emplyee_default_previous_project($company_code, $employee_code, $from_date);
    add_employee_default_project_work_to_array($epw, $group_company, $employee, $from_date, $to_date, $emp_project);
    
    $result = get_emplyee_default_projects($company_code, $employee_code, $from_date);
    while ($emp_project = db_fetch($result)) {
        add_employee_default_project_work_to_array($epw, $group_company, $employee, $from_date, $to_date, $emp_project);
    }
}

function add_employee_default_project_work_to_array(&$epw, $group_company, $employee, $from_date, $to_date, $emp_project)
{
    $start_date = $emp_project['start_date'];
    if($emp_project['start_date'] < $from_date){
        $start_date = $from_date;
    }
    $start = strtotime($start_date); 
    $end = strtotime($to_date); 

    for ($i=$start; $i<=$end; $i+=86400) {  
        $date = date("Y-m-d", $i); 
        $existing_values = $epw[$date];

        $possible_working_days = $existing_values['possible_working_days'];

        if($possible_working_days > 0){
            $epw[$date]["worked_days"] = $possible_working_days;
            $epw[$date]['project_id'] = $emp_project['project_id'];
            $epw[$date]['rate_category_id'] = $emp_project['rate_category_id'];
            $epw[$date]['rate'] = $emp_project['rate'];
            $epw[$date]['comment'] = 'DEFAULT_PROJECT';
        }
        else{
            $epw[$date]["worked_days"] = 0;
            $epw[$date]['project_id'] = $emp_project['project_id'];
            $epw[$date]['rate_category_id'] = $emp_project['rate_category_id'];
            $epw[$date]['rate'] = $emp_project['rate'];
            $epw[$date]['comment'] = 'DEFAULT_PROJECT';
        }

    }
    
}

function add_employee_leaves_to_array(&$epw, $group_company, $employee, $from_date, $to_date)
{
    $db_name = $group_company['db_name'];
    $employee_code = $employee['employee_code'];
    
//    $result = get_employee_leaves($company_code, $employee_code, $from_date, $to_date);
    $result = get_employee_project_work_leave_details($db_name, $employee_code, $from_date, $to_date);
    while ($leave_detail = db_fetch($result)) {
        add_employee_project_work_leave_days($epw, $leave_detail, $from_date, $to_date);              
    }
}

function add_employee_project_work_leave_days(&$epw, $leave_detail, $from_date, $to_date)
{
    $date_from = strtotime($from_date); 
    $date_to = strtotime($to_date); 
  
    // Loop from the start date to end date and output all dates inbetween  
    for ($i=$date_from; $i<=$date_to; $i+=86400) {  
        $date = date("Y-m-d", $i); 
        
        $existing_values = $epw[$date];
        $existing_leave_days = $existing_values['leave_days'];
        $this_leave_days = $leave_detail['leave_days'];
        $company_working_days = $existing_values['company_working_days'];
        $possible_working_days = $existing_values['possible_working_days'];
        
        if($company_working_days == 1){
            if($this_leave_days == 1){
                $new_leave_days = 1;
                $possible_working_days = 0;
                $new_worked_days = 0;
            }
            elseif($this_leave_days == 0.5){
                if($existing_leave_days == 0){
                    $new_leave_days = 0.5;
                    $possible_working_days = 0.5;
                    $new_worked_days = 0.5;
                }
                elseif($existing_leave_days == 0.5){
                    $new_leave_days = 1;
                    $possible_working_days = 0;
                    $new_worked_days = 0;
                }
            }
        }
        elseif($company_working_days == 0.5){
            if($this_leave_days == 1){
                $new_leave_days = 1;
                $possible_working_days = 0;
                $new_worked_days = 0;
            }
            elseif($this_leave_days == 0.5){
                if($existing_leave_days == 0){
                    $new_leave_days = 0.5;
                    $possible_working_days = 0;
                    $new_worked_days = 0;
                }
                elseif($existing_leave_days == 0.5){
                    $new_leave_days = 0.5;
                    $possible_working_days = 0;
                    $new_worked_days = 0;
                }
            }
        }
        else{
            if($this_leave_days == 1){
                $new_leave_days = 1;
                $possible_working_days = 0;
                $new_worked_days = 0;
            }
            elseif($this_leave_days == 0.5){
                if($existing_leave_days == 0){
                    $new_leave_days = 0.5;
                    $possible_working_days = 0;
                    $new_worked_days = 0;
                }
                elseif($existing_leave_days == 0.5){
                    $new_leave_days = 1;
                    $possible_working_days = 0;
                    $new_worked_days = 0;
                }
            }
        }
        
        $epw[$date]["possible_working_days"] = $possible_working_days;
        $epw[$date]["worked_days"] = $new_worked_days;
        $epw[$date]["leave_days"] = $new_leave_days;

    }
}



function build_month_days_array(&$mda, $from_date, $to_date, $format = 'Y-m-d')
{
    // Variable that store the date interval 
    // of period 1 day 
    $interval = new DateInterval('P1D'); 
  
    $realEnd = new DateTime($to_date); 
    $realEnd->add($interval); 
  
    $period = new DatePeriod(new DateTime($from_date), $interval, $realEnd); 
  
    // Use loop to store date into array 
    foreach($period as $date) {        
        $m = $date->format($format);
//        $epw[] = array($m => "11");  
//        array_push($epw,$m);
        
        $mda[$m] = array("project_id" => 0, 
                        "dafault_project" => 0, 
                        "company_working_days" => 0,
                        "worked_days" => 0, 
                        "leave_days" => 0, 
                        "weekend_days" => 0, 
                        "holidays" => 0, 
                        "possible_working_days" => 0,
                        "rate_category_id" => 0, 
                        "rate" => 0, 
                        "comment" => 'NO_DEFAULT_PROJECT');
    } 
    
}

function add_worktime_calendar_to_array(&$mda, $group_company, $from_date, $to_date)
{
    $db_name = $group_company['db_name'];
    
    $result = get_worktime_calendar_details($db_name, $from_date, $to_date);
    while ($calendar_detail = db_fetch($result)) {
        $date = $calendar_detail['date'];
        if($calendar_detail['work_time_code'] == "M" || $calendar_detail['work_time_code'] == "A"){
            $company_working_days = 0.5;
        }
        elseif($calendar_detail['work_time_code'] == "N"){
            $company_working_days = 0;
        }
        else{
            $company_working_days = 1;
        }

        $mda[$date]["company_working_days"] = $company_working_days;
        $mda[$date]["possible_working_days"] = $company_working_days;
    }
    
}


function add_holidays_to_array(&$mda, $group_company, $from_date, $to_date)
{
    $db_name = $group_company['db_name'];
    
    $result = get_holiday_details($db_name, $from_date, $to_date);
    while ($holiday = db_fetch($result)) {
        $date = $holiday['holiday_date'];
        if($holiday['holiday_time_code'] == "M" || $holiday['holiday_time_code'] == "A"){
            $holidays = 0.5;
        }
        else{
            $holidays = 1;
        }

        $mda[$date]["holidays"] = $holidays;
        
        if($holidays == 1){
            $mda[$date]["company_working_days"] = 0;
            $mda[$date]["possible_working_days"] = 0;
        }
        elseif($holidays == 0.5){
            if($mda[$date]["company_working_days"] == 1){
                $mda[$date]["company_working_days"] = 0.5;
                $mda[$date]["possible_working_days"] = 0.5;
            }
            else{
                $mda[$date]["company_working_days"] = 0;
                $mda[$date]["possible_working_days"] = 0;
            }
        }
    }
    
}

function add_employee_status_to_array(&$epw, $employee, $month_begin_date, $month_end_date, &$from_date, &$to_date)
{
    $date_joined = $employee['date_of_joining'];
    $date_resigned = $employee['date_of_resignation'];
            
    if($date_joined < $month_begin_date){
        $from_date = $month_begin_date;
    }
    else{
        $from_date = $date_joined;
    }

    if($month_end_date < $date_resigned){
        $to_date = $month_end_date;
    }
    else{
        $to_date = $date_resigned;
    }
            
    $time_month_begin_date = strtotime($month_begin_date); 
    $time_month_end_date = strtotime($month_end_date); 
  
    for ($i=$time_month_begin_date; $i<=$time_month_end_date; $i+=86400) {  
        $date = date("Y-m-d", $i); 
        
        if($date_joined <= $date){// Joined already
            $comment = '';
        }
        else{// Not yet joined
            $comment = 'NOT_JOINED';
            $epw[$date]['comment'] = $comment;
        }
        
        if($date <= $date_resigned){// Not yet resigned
            $comment = '';
        }
        else{// Resigned already
            $comment = 'RESIGNED';
            $epw[$date]['comment'] = $comment;
        }
     
    }
}
