<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_CALCULATE_MONTHLY_SUMMARIES';
$path_to_root = "..";
//include_once($path_to_root . "/includes/ui/items_cart.inc");
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Calculate Monthly Summaries"));

include_once($path_to_root . "/includes/ui.inc");

include_once($path_to_root . "/includes/ui/items_cart.inc");
include_once($path_to_root . "/summary/includes/monthly_summary_calculation.inc");
include_once($path_to_root . "/summary/includes/db/monthly_summary_calculation_db.inc");
include_once($path_to_root . "/admin/db/months_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");


simple_page_mode(false);



function display_summary_calculation_history($month_id, $calculation_type, $group_code)
{
    start_table(TABLESTYLE, "width=90%");

    $th = array(_("Id"), _("Month"), _("Calculated By"), _("Calculated On"), _("Duration (mm:ss)"), _("Comments"));
      
    table_header($th);
    
    switch ($calculation_type) {
        case 'PROJECT_SUMMARY':
//            display_summary_calculation_history_project_summary($month_id, $calculation_type, $group_code);
//            break;
        
        case 'COMPANY_SUMMARY':
        case 'LOANS_SUMMARY':          
        case 'PAYROLL_SUMMARY':
        case 'EMPLOYEE_PROJECT_WORK_SUMMARY';
        case 'SALES_SUMMARY';
            display_summary_calculation_history_common($month_id, $calculation_type, $group_code);
            break;
        
        default :
            display_error("No value selected");
            break;
    }
    
    end_table();
}


function display_summary_calculation_history_common($month_id, $calculation_type, $group_code)
{
    
    $result = get_summary_calculation_history($month_id, $calculation_type, $group_code);
    while ($row = db_fetch($result)) {
        start_row();
//        label_cell("All Departments");
        label_cell($row['id'], "align=center");
        label_cell($row['month_description']);

        label_cell($row['user_name']);
        label_cell($row['created_on'], "align=center");

        if($row['duration']>0){
            label_cell(gmdate("i:s", $row['duration']), "align=center");
        }
        else{
            label_cell('');
        }
        
        label_cell($row['comments']);

        end_row();
    }
    

}

function display_summary_calculation_history_project_summary($month_id, $calculation_type, $group_code)
{
    
    $result = get_all_list_values("DEPARTMENTS");
    while ($department = db_fetch($result)) {
        start_row();
        label_cell($department['value_name']);
        $row = get_summary_calculation_history($month_id, $calculation_type, $department['value_code']);
        label_cell($row['month_description']);
        
        label_cell($row['user_name']);
        label_cell($row['created_on'], "align=center");
        
        if($row['duration']>0){
            label_cell(gmdate("i:s", $row['duration']), "align=center");
        }
        else{
            label_cell('');
        }
    
        
        end_row();
    }

}

//--------------------------------------------------------------------------------



if(isset($_POST['CALCULATE_SUMMARY']) || isset($_POST['CONTINUE_CALCULATION']))
{
    $summary_calculation_log_folder_path = $path_to_root.'/tmp/summary_calculation_logs/';

    if (!file_exists($summary_calculation_log_folder_path))
    {
        create_folder_index_file($summary_calculation_log_folder_path);
    }
    
    $continue_calculation = false;
    if(isset($_POST['CONTINUE_CALCULATION'])){
        $continue_calculation = true;
    }
    run_summary_calculation($path_to_root, $_POST['month_id'], $_POST['summary_value'], $_POST['group_code'], $continue_calculation);
    
}





//----------------------------------------------------------------------------------

//$result = get_all_item_units(check_value('show_inactive'));

$_POST['custom_script'] = null;

start_form();

br(1);

$values_array['COMPANY_SUMMARY'] = 'Company Summary';
$values_array['LOANS_SUMMARY'] = 'Loans Summary';
$values_array['PROJECT_SUMMARY'] = 'Project Summary';
$values_array['PAYROLL_SUMMARY'] = 'Payroll Summary';
$values_array['EMPLOYEE_PROJECT_WORK_SUMMARY'] = 'Employees Project Work Summary';
$values_array['SALES_SUMMARY'] = 'Sales Summary';

start_table(TABLESTYLE); 
months_list_row("Month", "month_id", null, false, true);
custom_values_list_row("Values", 'summary_value', $values_array, null, false, true);

if($_POST['summary_value'] == 'PROJECT_SUMMARY'){
    value_list_code_row("Department", 'group_code', 'DEPARTMENTS', null, "Calculate all departments", true);
}
else{
    $_POST['group_code'] = '';
    hidden('group_code', $_POST['group_code']);
}
end_table();



br(1);


//submit_center("RECALCULATE_SUMMARY", "Calculate");
//br(1);
//submit_center("CONTINUE_CALCULATION", "Continue Calculation");

submit_center_first('CALCULATE_SUMMARY', "Calculate", '', 'process', ICON_OK);
submit_center_last('CONTINUE_CALCULATION', "Continue Calculation", '', 'process', ICON_OK);

br(3);

display_heading2("Calculation History");

$Ajax->activate('cal_summary');
div_start('cal_summary');
display_summary_calculation_history($_POST['month_id'], $_POST['summary_value'], $_POST['group_code']);
div_end();
br(2);

end_form();

end_page();

?>



