<?php

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_begin_date = '';
    $month = get_month($month_id);
    if (isset($month['id']) && $month['id'] > 0) {
        $month_begin_date = $month['month_begin_date'];
    }
    
    $k = 0; //row colour counter
    $myrow = get_emplyee_default_previous_project_details($company_code, $employee_code, $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_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_project_details_row($employee_project_id) {
    $row = get_employee_projects_by_filters($employee_project_id, fetch: true);

    if (isset($row['id']) && $row['id'] > 0) {
        display_heading2("Employee Project BOQ Item Trans #$employee_project_id Details");
        
        start_table(TABLESTYLE, "width=80%");
            $th = array(_("Rec. Id"), _("Start Date"), _("End Date"), _('Project BOQ Item'), _("Calendar Working Days"), _("Days Assigned"), _("Factor"), _("Days Worked"));

            table_header($th);
            $k = 0; //row colour counter

                alt_table_row_color($k);

                    label_cell($row["id"], "align=center");
                    label_cell(sql2date($row["start_date"]), "align=center");

                    if(substr($row["end_date"], 0, 4) == '3000'){
                        label_cell("");
                    }
                    else{
                        label_cell(sql2date($row["end_date"]), "align=center");
                    }
                    label_cell($row["boq_item_code"]." - ".$row["boq_item_name"]);
                    label_cell($row["calendar_working_days"], 'align=right');
                    label_cell($row["days_per_month"], 'align=right');
                    label_cell($row["factor"], 'align=right');
                    if ($row['days_worked'] > 0) {
                        label_cell(get_project_trans_view_str(DASH_VW_EMPLOYEE_PROJECT_BOQ_ITEM_WORK_DONE, $row["id"], $row['days_worked']), "align=right");
                    } else {
                        label_cell('');
                    }

                end_row();

        end_table(2);
    }
}

function display_employee_month_project_work($company_code, $employee_code, $month_id, $employee_project_id)
{
     br(1);
     display_heading2("Project Work Details");
     
    $result = get_employee_month_project_work($company_code, $employee_code, $month_id, $employee_project_id);
    
        start_table(TABLESTYLE, "width=95%");

            $th = array("Id", _("Project"), _("BOQ Item"), _("Start Date"), _("End Date"), _("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(get_project_formatted_name($myrow["project_id"]));
                        label_cell($myrow["boq_item_code"].' - '.$myrow["boq_item_name"]);
                        label_cell(sql2date($myrow["start_date"]),  "align=center");
                        label_cell(sql2date($myrow["end_date"]),  "align=center");
                        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['month_id'] > 0))
    {
        display_error( _("Please select a month."));
        set_focus('month_id');
        return false;
    }
   	
    if(!($_POST['employee_project_id'] > 0))
    {
        display_error( _("Please select a employee project record."));
        set_focus('employee_project_id');
        return false;
    }
   	
    if(!($_POST['project_id'] > 0))
    {
        display_error( _("Could not find employee project."));
        set_focus('project_id');
        return false;
    }
   	
    if(!($_POST['boq_item_id'] > 0))
    {
        display_error( _("Could not find BOQ item."));
        set_focus('boq_item_id');
        return false;
    }
    
    if (!is_date($_POST['start_date']))
    {
        display_error( _("The start date entered is invalid."));
        set_focus('start_date');
        return false;
    }
    
    if (!is_date($_POST['end_date']))
    {
        display_error( _("The end date entered is invalid."));
        set_focus('start_date');
        return false;
    }
    
    if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
    {
        display_error( _("End date can not be before the end date."));
        set_focus('end_date');
        return false;
    }

    $start_date = date2sql($_POST['start_date']);
    $end_date = date2sql($_POST['end_date']);
    
    // check dates overlapping with old records
    $row = get_employee_project_works_by_from_and_date_dates($start_date, $end_date, $_POST['employee_code'], $_POST['employee_project_id'], not_id: $selected_id, fetch: true);
        if (isset($row['id']) && $row['id']) {
            $id = $row['id'];
            display_error( _("Entered dates are overlapping with record #$id."));
            set_focus('start_date');
            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;
    }
    
    $no_of_days = input_num('no_of_days');
    
    $start_date__ = new DateTime($start_date);
    $end_date__ = new DateTime($end_date);

    $interval = $start_date__->diff($end_date__);
    $actual_num_days = $interval->days; 
    
    if ($no_of_days > $actual_num_days) {
        display_error( _("No of days in between start and end dates is $actual_num_days. Cannot exceed that value."));
        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;
}
            
function display_employee_project_work_edit_controls($selected_id, &$Mode)
{
    $dec2 = 6;
    if ($Mode =='Edit' && $selected_id > 0)
    {
        $myrow = get_employee_project_work($selected_id);

        $_POST['start_date'] = sql2date($myrow["start_date"]);
        $_POST['end_date'] = sql2date($myrow["end_date"]);
        $_POST['no_of_days'] = $myrow["no_of_days"];
        $_POST['comment'] = $myrow["comment"];

    }
    
    if (! isset($_POST['start_date'])) {
        $_POST['start_date'] = add_days(Today(), -1);
    }

    br();

    hidden('selected_id', $selected_id);


    start_table(TABLESTYLE2);

//    date_row("From Date:", 'from_date');
//    date_row("To Date:", 'to_date');
    
        if ($_POST['employee_project_id'] > 0) {
            $emp_pro_row = get_employee_project($_POST['employee_project_id']);
                if (isset($emp_pro_row['id']) && $emp_pro_row['id'] > 0) {
                    label_row("Project:", get_project_formatted_name($emp_pro_row['project_id']));
                    hidden('project_id', $emp_pro_row['project_id']);
                }
                    
            $emp_boq_row = get_project_boq_item_record($emp_pro_row['boq_item_id']);
                if (isset($emp_boq_row['id']) && $emp_boq_row['id'] > 0) {
                    label_row("Boq Item:", $emp_boq_row['code']." - ".$emp_boq_row['description']);
                }
                hidden('boq_item_id', $emp_pro_row['id']);
        }

        date_row(_('Start Date:'), 'start_date'); 
        date_row(_('End Date:'), 'end_date'); 
        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, 45, 2);

    end_table();

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

}

function view_employee_project_boq_item_work_done_details($employee_project_id) {
     display_heading2("Employee Project BOQ Item Work Done Details");
     
        start_table(TABLESTYLE, "width=60%");

            $th = array("Id", _("Start Date"), _("End Date"), _("Days"), _("Comment"));
            table_header($th);

                $k = $j = $total_no_of_days = 0; //row colour counter
                $result = get_employee_project_works_by_filters(employee_project_id: $employee_project_id);
                    while ($row = db_fetch($result)) {
                        alt_table_row_color($k);
                            label_cell($row["id"],  "align=center");
                            label_cell(sql2date($row["start_date"]), "align=center");
                            label_cell(sql2date($row["end_date"]), "align=center");
                            label_cell($row["no_of_days"],  "align=right");
                            label_cell($row["comment"]);
                        end_row();
                        
                        $total_no_of_days += $row["no_of_days"];

                        $j++;
                        If ($j == 12)
                        {
                            $j = 1;
                                    table_header($th);
                        } //end of page full new headings
                    } //end of while loop
                    
                    start_row();
                        label_cell(text_bold(number_format($total_no_of_days, 2)), "align=right colspan=4");
                        label_cell('');
                    end_row();

        end_table(2);
}

function display_employee_project_employee_and_project($employee_project_id) {
    $row = get_employee_projects_by_filters($employee_project_id, fetch: true);
        if (isset($row['id']) && $row['id'] > 0) {
            start_table(TABLESTYLE, "width=60%");
                label_row("Employee", get_employee_formatted_long_name($row['employee_code']), "class='tableheader2'");
                label_row("Project", get_project_formatted_name($row['project_id']), "class='tableheader2'");
            end_table(2);
        }
}