<?php

function run_summary_calculation($path_to_root, $month_id, $calculation_type, $group_code, $continue_calculation)
{
    
    $started_on = date('Y-m-d H:i:s');
    
    
    $month = get_month($month_id);
    
    switch ($calculation_type) {
        case 'EMPLOYEE_PROJECT_WORK_SUMMARY':
            include_once($path_to_root . "/summary/includes/employees_project_work_calculation.inc");
            include_once($path_to_root . "/summary/includes/db/employees_project_work_calculation_db.inc");
            include_once($path_to_root . "/includes/db/group_companies_db.inc");
//            include_once($path_to_root . "/projects/includes/db/employee_projects_db.inc");

            $calculation_id = add_monthly_summary_calculation_run($month_id, $calculation_type, $group_code, $started_on);
            sc_calculate_employees_project_work($calculation_id, $calculation_type,  
                                        $month_id, $month['month_begin_date'], $month['month_end_date']);
            complete_monthly_summary_calculation_run($calculation_id, get_summary_calculation_elapsed_time($started_on));
            break;
        
        case 'LOANS_SUMMARY':
            include_once($path_to_root . "/summary/includes/loan_summary_calculation.inc");
            include_once($path_to_root . "/summary/includes/db/loan_summary_calculation_db.inc");
            include_once($path_to_root . "/finance/includes/db/company_loans_db.inc");
            include_once($path_to_root . "/admin/db/months_db.inc"); 
            
            $calculation_id = add_monthly_summary_calculation_run($month_id, $calculation_type, $group_code, $started_on);
            sc_calculate_loan_summary($calculation_id, $calculation_type, $group_code, $month_id, 
                                      $month['month_begin_date'], $month['month_end_date'], $continue_calculation);      
            complete_monthly_summary_calculation_run($calculation_id, get_summary_calculation_elapsed_time($started_on));
            break;
        
        case 'PROJECT_SUMMARY':
            include_once($path_to_root . "/summary/includes/project_summary_calculation.inc");
            include_once($path_to_root . "/summary/includes/db/project_summary_calculation_db.inc");
            include_once($path_to_root . "/projects/includes/db/project_status_view_db.inc");            
            include_once($path_to_root . "/projects/includes/db/projects_db.inc");            
            
            sc_calculate_department_project_summary($calculation_type, $group_code, 
                                        $month_id, $month['month_begin_date'], $month['month_end_date'], $continue_calculation);
            
            break;
        
        case 'PAYROLL_SUMMARY':
            include_once($path_to_root . "/summary/includes/payroll_summary_calculation.inc");
            include_once($path_to_root . "/summary/includes/db/payroll_summary_calculation_db.inc");
            include_once($path_to_root . "/includes/db/group_companies_db.inc");
            
            $calculation_id = add_monthly_summary_calculation_run($month_id, $calculation_type, $group_code, $started_on);
            sc_calculate_payroll_summary($calculation_id, $calculation_type,  
                                        $month_id, $month['month_begin_date'], $month['month_end_date']);
            complete_monthly_summary_calculation_run($calculation_id, get_summary_calculation_elapsed_time($started_on));
            break;
        
        case 'SALES_SUMMARY':
            include_once($path_to_root . "/summary/includes/sales_summary_calculation.inc");
            include_once($path_to_root . "/summary/includes/db/sales_summary_calculation_db.inc");
            include_once($path_to_root . "/includes/db/group_companies_db.inc");
            
            $calculation_id = add_monthly_summary_calculation_run($month_id, $calculation_type, $group_code, $started_on);
            sc_calculate_sales_summary($calculation_id, $calculation_type,  
                                        $month_id, $month['month_begin_date'], $month['month_end_date']);
            complete_monthly_summary_calculation_run($calculation_id, get_summary_calculation_elapsed_time($started_on));
            break;
        
        case 'COMPANY_SUMMARY':
            include_once($path_to_root . "/summary/includes/company_summary_calculation.inc");
            include_once($path_to_root . "/summary/includes/db/company_summary_calculation_db.inc");
            include_once($path_to_root . "/includes/db/group_companies_db.inc");
            
            $calculation_id = add_monthly_summary_calculation_run($month_id, $calculation_type, $group_code, $started_on);
            sc_calculate_company_summary($calculation_id, $calculation_type,  
                                        $month_id, $month['month_begin_date'], $month['month_end_date']);
            complete_monthly_summary_calculation_run($calculation_id, get_summary_calculation_elapsed_time($started_on));
            break;
        
        default :
            display_error("No value selected");
            break;
    }
    

    


}


function write_summary_calculation_log($calculation_id, $message)
{

    global $installed_path;
      
    $log_filename = "scl".$calculation_id.".txt"; 
    $message = format_microtime('Y-m-d H:i:s.u')." - ".$message;
    $log_filepathname = $installed_path . '/tmp/summary_calculation_logs/'.$log_filename;
    file_put_contents($log_filepathname, $message."\r\n",FILE_APPEND);
            
}


function get_summary_calculation_elapsed_time($started_on)
{
    $completed_on = date('Y-m-d H:i:s');
    $ts_started_on = strtotime($started_on);
    $ts_completed_on = strtotime($completed_on);
    $duration_s = $ts_completed_on - $ts_started_on;
    if($duration_s == 0){
        $duration_s = 1; // If calculated set minimum posible duration
    }
    
    return $duration_s;
}

