<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_EMPLOYEE_PROJECT_WORK';

$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/projects/includes/ui/employee_project_work_ui.inc"); 
include_once($path_to_root . "/projects/includes/db/employee_project_work_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/projects/includes/db/employee_projects_db.inc");
include_once($path_to_root . "/admin/db/months_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "Employee Project Work"), false, false, "", $js);

//----------------------------------------------------------------------------------------
simple_page_mode(true);


if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id)) {

    $trans_success = true;
    begin_transaction(); 
    
    if ($Mode == 'ADD_ITEM') {
        $insert_id = add_employee_project_work($_POST['company_code'], $_POST['employee_code'], $_POST['project_id'], $_POST['boq_item_id'], $_POST['month_id'], 
                                               $_POST['employee_project_id'], date2sql($_POST['start_date']), date2sql($_POST['end_date']), input_num('no_of_days'), 
                                               $_POST['comment']);
            if (! ($insert_id > 0)) {
                $trans_success = false;
                $message = "Could not add new employee project work record.";
            }

            $message = _("A new employee project work has been added.");
    } 
    
    if ($Mode == 'UPDATE_ITEM') {
        $af_row = update_employee_project_work($selected_id, date2sql($_POST['start_date']), date2sql($_POST['end_date']), input_num('no_of_days'), $_POST['comment']);
            if ($af_row != 1) {
                $trans_success = false;
                $message = "Could not update employee project work record #$selected_id.";
            }
            
            $message = _("Employee project work has been updated.");
    }
            
           if ($trans_success === false) {
               cancel_transaction();
               display_error($message);
           } else {
                $af_row_2 = update_employee_project_days_worked($_POST['employee_project_id']);
                    if ($af_row_2 != 1) {
                        $trans_success = false;
                        $message = "Could not update employee project work record #$selected_id total days worked.";
                    }
                    
                commit_transaction();
                display_notification($message);
               
                $selected_id = -1;
                $Mode = 'RESET';
           }
           
           $message = '';
}

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

if ($Mode == 'Delete') {
    $dl_af_row = delete_employee_project_work($selected_id);
        if ($dl_af_row != 1) {
            display_error("Could not update employee project work record #$selected_id.");
        } else {
            $af_row_2 = update_employee_project_days_worked($_POST['employee_project_id']);
                if ($af_row_2 != 1) {
                    display_error("Could not update employee project work record #$selected_id total days worked.");
                }
            display_notification(_("Selected eployee project work record #$selected_id has been deleted."));
        }
        
        $Mode = 'RESET';
}

if (isset($_POST['Refresh']) || isset($_POST['cancel']) || $Mode == 'RESET') {
    unset($_POST['no_of_days']);
    unset($_POST['comment']);
    $Mode = 'RESET';
    $selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	$Ajax->activate('_page_body');
}


$Ajax->activate('price_table');


start_form();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        group_companies_list_cells("Company:", 'company_code', null, false, true);
        group_company_employees_simple_list_cells(_("Employee:"), 'employee_code', $_POST['company_code'], null, '', true);
    end_row();
end_table(1);

start_table(TABLESTYLE);
    start_row();
        months_list_cells("Month:", 'month_id', null, '', true);
    end_row();
    
    $month_row = get_month($_POST['month_id']);
        if (isset($month_row['id']) && $month_row['id'] > 0) {
            $month_begin_date = $month_row['month_begin_date'];
            $month_end_date = $month_row['month_end_date'];
            
            $employee_projects_array = create_employee_projects_array($_POST['employee_code'], $month_begin_date, $month_end_date);
            custom_values_list_row("Employee Porject Record:", 'employee_project_id', $employee_projects_array, null, '', true);
        }
end_table(1);

    start_table();
        submit_cells('Refresh', 'Refresh');
    end_table(1);
    
    div_start('price_table');
    
        if (! (strlen($_POST['employee_code']) > 0 && $_POST['month_id'] > 0)) {
            display_note("Please select an employee and a month then click on Refresh button.");
            display_footer_exit(2);
        }
        
        display_employee_project_details_row($_POST['employee_project_id']);

        display_employee_month_project_work($_POST['company_code'], $_POST['employee_code'], $_POST['month_id'], $_POST['employee_project_id']);

    div_end();

    display_employee_project_work_edit_controls($selected_id, $Mode);

end_form(2);
end_page(@$_GET['popup'], false, false);

?>