<?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/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");



$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);


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

function display_employee_projects($company_code, $employee_code, $month_id)
{
    display_heading2("Default (Scheduled) Projects");
    
    start_table(TABLESTYLE, "width=60%");
    $th = array( _("Start Date"), _('Project'), _('No Of Days'),_('Rate Category'), "Rate");
    //inactive_control_column($th);

    table_header($th);
    
//    $result = get_employee_all_projects($company_code, $employee_code);
    $month = get_month($month_id);
    
    $k = 0; //row colour counter
    $myrow = get_emplyee_default_previous_project_details($company_code, $employee_code, $month['month_begin_date']);
    if(isset($myrow['id']) && $myrow['id']> 0){
        alt_table_row_color($k);

        label_cell(sql2date($myrow["start_date"]), "align=center");
        label_cell($myrow["project_description"]);
        label_cell($myrow["days_per_month"],"align=right");
        label_cell($myrow["rate_category_name"]);
        label_cell(number_format($myrow["rate"],2),"align=right");
        end_row();
    }
        
    
    $result = get_emplyee_default_project_details($company_code, $employee_code, $month['month_begin_date']);
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

        label_cell(sql2date($myrow["start_date"]), "align=center");
        label_cell($myrow["project_description"]);
        label_cell($myrow["days_per_month"],"align=right");
        label_cell($myrow["rate_category_name"]);
        label_cell(number_format($myrow["rate"],2),"align=right");
        end_row();
    }

    //inactive_control_row($th);
    end_table(1);
}


function display_employee_month_project_work($company_code, $employee_code, $month_id)
{
     br(1);
     display_heading2("Project Work");
     

    $result = get_employee_month_project_work($company_code, $employee_code, $month_id);
    

    
//    if (db_num_rows($result) == 0)
//    {
//        br(1);
//    	display_note(_("There is no project work entries found for this employee."));
//        br(1);
//    }
//    else
    {
        start_table(TABLESTYLE, "width=60%");

		$th = array("Id", _("Project"), _("Days"), _("Comment"), "", "");

        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);

            label_cell($myrow["id"],  "align=center");
            label_cell($myrow["project_description"]." - (".$myrow["project_id"].")");
            label_cell($myrow["no_of_days"],  "align=right");

            label_cell($myrow["comment"]);
            
            edit_button_cell("Edit".$myrow['id'], _("Edit"));
            delete_button_cell("Delete".$myrow['id'], _("Delete"));
            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

        end_table();
    }
}
    

function check_data($selected_id)
{
    if ($_POST['company_code'] == "" || !isset($_POST['company_code']))
    {
        display_error( _("Please select a company."));
        set_focus('company_code');
        return false;
    }

    if ($_POST['employee_code'] == "" || !isset($_POST['employee_code']))
    {
        display_error( _("There is no employee selected."));
        set_focus('employee_code');
        return false;
    }
   	
    if(!($_POST['project_id'] > 0))
    {
        display_error( _("Please select a project."));
        set_focus('project_id');
        return false;
    }
    
    if(!($_POST['month_id'] > 0))
    {
        display_error( _("Please select a month."));
        set_focus('month_id');
        return false;
    }
    
    if(!is_numeric($_POST['no_of_days']))
    {
        display_error( _("Entere value for no of days is not numeric."));
        set_focus('no_of_days');
        return false;
    }
    
    if(!(input_num('no_of_days') > 0))
    {
        display_error( _("No of days should be greater than zero."));
        set_focus('no_of_days');
        return false;
    }
    

//    $row = get_existing_employee_project_work($_POST['company_code'], $_POST['employee_code'], 
//                                            date2sql($_POST['from_date']), date2sql($_POST['to_date']),$selected_id);
//    if(isset($row['id']) && $row['id'] > 0)
//    {
//        display_error( _("Record id ".$row['id'].": There is a period overlapping project work entered for this employee."));
//        set_focus('from_date');
//        return false;
//    }
     
//    $ep_result = get_employee_all_projects($_POST['company_code'], $_POST['employee_code']);
//    $ep_first_row = db_fetch($ep_result);
//    if(!(isset($ep_first_row['id']) && $ep_first_row['id'] > 0)){
//        display_error( _("No default (scheduled) projects entered for this employee. (At lease one project assignment from the beginning of the respective month will be required for project employee cost calculation)."));
//        set_focus('employee_code');
//        return false;
//    }
//    else{
//        $employee = get_group_company_employee($_POST['company_code'], $_POST['employee_code']);
//        if($employee['date_of_joinning'] < $ep_first_row['from_date']){
//            display_error( _("This employee is not assigned to any project. A project assignment required from the date of joinning."));
//            set_focus('employee_code');
//            return false;
//        }
//    }
        
    return true;
            
}

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

    if ($Mode == 'ADD_ITEM') 
    {
            add_employee_project_work($_POST['company_code'], $_POST['employee_code'], $_POST['project_id'], 
                                    $_POST['month_id'], input_num('no_of_days'), $_POST['comment']);


            display_notification(_("A new employee project work has been added."));
    } 
    if ($Mode == 'UPDATE_ITEM') 
    {
            update_employee_project_work($selected_id, $_POST['project_id'], 
                                    $_POST['month_id'], input_num('no_of_days'), $_POST['comment']);

            $selected_id = -1;
            display_notification(_("Employee project work has been updated."));
    }
    $Mode = 'RESET';
    unset($_POST['is_closed']);

}

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

if ($Mode == 'Delete')
{
//    if(check_leave_entitlement_used($selected_id))
//   	{
//            display_error( _("There are leaves applied for this leave entitlement. Not possible to delete."));
//            //set_focus('leave_entitlement');
//            return false;
//   	}
    
    delete_employee_project_work($selected_id);
    display_notification(_("Selected eployee project work has been deleted."));
    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

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


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

function display_employee_project_work_edit_controls($selected_id, &$Mode)
{
    $dec2 = 6;
    if ($Mode =='Edit')
    {
        $myrow = get_employee_project_work($selected_id);

        $_POST['no_of_days'] = $myrow["no_of_days"];
        $_POST['project_id'] = $myrow["project_id"];
        $_POST['comment'] = $myrow["comment"];

    }

    br();

    hidden('selected_id', $selected_id);


    start_table(TABLESTYLE2);

//    date_row("From Date:", 'from_date');
//    date_row("To Date:", 'to_date');

    project_list_row("Project:", 'project_id', null, 'Select a project');
    
    small_qty_row("No of Days", 'no_of_days', null, null, null, 1);
//    small_qty_row($label, $name, $init, $params, $post_label, $dec)

    textarea_row(_("Comment:"), 'comment', null, 35, 2);

    end_table();

    
    br(1);
    
    submit_add_or_update_center($selected_id == -1, '', 'both');

}

$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, 'Select an employee', true);
    end_row();
end_table(1);
start_table(TABLESTYLE_NOBORDER);
    start_row();
//        date_cells("Display From", 'display_from_date', '', null, -45);
//        date_cells("Display To", 'display_to_date');
//        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
        
        months_list_cells("Month", 'month_id', null, 'Select month', true);
    end_row();

end_table(1);

div_start('price_table');

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

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

div_end();

display_employee_project_work_edit_controls($selected_id, $Mode);

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

?>
