<?php

if (isset($Mode) && $Mode == 'Delete') {
    $msg = handle_delete_job_order_labour($jo_id, $selected_id);
        if ($msg['success'] === false) {
            display_error($msg['message']);
        } else {
            clear_job_order_inputs($selected_id);
            display_notification($msg['message']);
        }
}

function job_order_labours(&$Mode, &$selected_id, &$jo_id, $state=true) {
    global $Ajax;
    
    $Ajax->activate('job_order_labours');
    div_start('job_order_labours');
        
        display_job_order_labours($jo_id, $selected_id, $Mode, $state);
        
    div_end();
}

function display_job_order_labours($jo_id, $selected_id, &$Mode, $state=true) {
    display_heading("Job Card Labour Details");    

	start_table(TABLESTYLE, "width=88%");
        
            $th = array(_("Id"), _("Employee"), _("From Date"), _("To Date"), _("Num Days Worked"), _("Rate Category"), _("Rate"), _("Description"), _(""), _(""));
            table_header($th);
            
            $k = 0;  
            $result = get_job_order_labours_details($jo_id);
                while ($row = db_fetch($result)) {
                    $id = $row['id'];
                    
                    if ($selected_id != $id || $Mode == 'Delete') {
                        alt_table_row_color($k);

                            label_cell($row['id'], 'align=center');
                            $labour_name = get_employee_formatted_long_name($row['employee_code']);
                            label_cell($labour_name);
                            label_cell(sql2date($row['from_date']), 'align=center');
                            label_cell(sql2date($row['to_date']), 'align=center');
                            label_cell($row['num_days_worked'], 'align=center');
                            label_cell($row['rate_category_name']);
                            qty_cell($row['rate']);
                            label_cell(nl2br($row['description']));
                            edit_button_cell("Edit".$row['id'], _("Edit"));
                            delete_button_cell("Delete".$row['id'], _("Delete"));

                        end_row();
                    } else {
                        display_job_order_labour_controls($Mode, $selected_id, $jo_id, $state);
                    }
                }
                
                if (! ($selected_id > 0) || $Mode == 'Delete') {
                    display_job_order_labour_controls($Mode, $selected_id, $jo_id, $state);
                }
                
    end_table(2);
}

function display_job_order_labour_controls(&$Mode, &$selected_id, $jo_id, $state=true) {
    global $Ajax, $path_to_root;
    
    include_once($path_to_root."/payroll/includes/db/employee_rate_categories_db.inc");
    include_once($path_to_root."/payroll/includes/db/employee_rates_db.inc");
    
        if ($jo_id > 0 && $Mode != 'Delete' && $selected_id > 0 && $state === true) {
            $jo_labour_row = get_job_order_labour_record($selected_id);
                if (is_array($jo_labour_row)) {
                    if (! isset($_POST['employee_code']) || $_POST['employee_code'] == 0 || $Mode == 'Edit') {
                        $_POST['employee_code'] = $jo_labour_row['employee_code'];
                    }
                    if (! isset($_POST['from_date']) || $Mode == 'Edit') {
                        $_POST['from_date'] = sql2date($jo_labour_row['from_date']);
                    }
                    if (! isset($_POST['to_date']) || $Mode == 'Edit') {
                        $_POST['to_date'] = sql2date($jo_labour_row['to_date']);
                    }
                    if (! isset($_POST['num_days_worked']) || $Mode == 'Edit') {
                        $_POST['num_days_worked'] = $jo_labour_row['num_days_worked'];
                    }
                    if (! isset($_POST['description']) || $Mode == 'Edit') {
                        $_POST['description'] = $jo_labour_row['description'];
                    }
                }

            hidden('selected_id', $selected_id);
        }

        echo "<td colspan=8>";
            $Ajax->activate('job_order_labour_control');
            div_start('job_order_labour_control');

                start_outer_table(TABLESTYLE2);
                
                    table_section(1);
                    
                        if ($selected_id > 0 && $Mode != 'Delete' ) {
                            $row = get_job_order_labour_record($selected_id);
                                if (is_array($row)) {
                                    label_row('Id #:', $selected_id);
                                    label_row('Employee:', get_employee_formatted_long_name($row['employee_code']));
                                    $_POST['employee_code'] = $row['employee_code'];
                                    hidden('employee_code', $row['employee_code']);
                                }
                        } else {
                            start_cell("colspan=2");
                                start_table(false, 'align=left');
                                    employee_search_list_row(_("Employee:"), 'employee_code', null, false, true);
                                end_table();
                            end_cell();
                        }

                        date_row(_("From Date:"), 'from_date', null, null, 0, 0, 0, null, true);
                        date_row(_("To Date:"), 'to_date', null, null, 0, 0, 0, null, true);
                    
                    table_section(2);
                    
                        amount_row('No of Days Worked:', 'num_days_worked');
                        $cat_row = get_employee_rate_category_on_date($_POST['employee_code'], $_POST['from_date']);
                            if (is_array($cat_row)) {
                                label_row('Employee Rate Category:', $cat_row['rate_category_name']);
                                $rate_row = get_employee_rate_on_date_and_category($_POST['from_date'], $cat_row['emp_rate_category']);
                                    if (is_array($rate_row)) {
                                        label_row('Employee Rate:', $rate_row['rate']);
                                    } else {
                                        label_row('Employee Rate:', 'Not found');
                                    }
                            } else {
                                label_row('Employee Rate Category:', 'Not found');
                                label_row('Employee Rate:', 'Not found');
                            }
                            
                    table_section(3);
                        textarea_row('Description:', 'description', null, 40, 3);
                    
                end_outer_table(1);

            div_end();
        echo "</td>";

            if ($jo_id > 0 && $selected_id > 0 && $Mode != 'Delete') {
                button_cell('UPDATE_JO_LABOUR', _("Update Job Card Labour"), _('Confirm changes'), ICON_UPDATE);
                hidden('selected_id', $selected_id);

            } elseif ($jo_id > 0) {        
                button_cell('ADD_JO_LABOUR', _("Add Job Card Labour"), _('Confirm changes'), ICON_ADD);
            }

            button_cell("CLEAR_DATA", 'Clear', 'Clear entered data', ICON_CANCEL);
}

function can_save_job_order_labour(&$selected_id) {  
    if ($selected_id > 0) {
        $row = get_job_order_labour_record($selected_id);
            if (! is_array($row)) {
                return array('success' => false, 'message' => "Another user has deleted job card labour record #$selected_id.");
            }
    }
      
    if (! isset($_POST['employee_code']) || ! (strlen($_POST['employee_code']) > 2)) {
        return array("success" => false, "focus_to" => 'employee_code', "message" => "Employee cannot empty. Please select an employee.");        
    }
    
    if (! is_date($_POST['from_date'])){
        return array('success' => false, 'message' => _("From date is not valid date."), 'focus_to' => 'from_date');
    }
    
    if (! is_date($_POST['to_date'])){
        return array('success' => false, 'message' => _("To date is not valid date."), 'focus_to' => 'to_date');
    }
    
    if ($_POST['from_date'] > $_POST['to_date']){
        return array('success' => false, 'message' => _("To date should greater than or equel to from date."), 'focus_to' => 'to_date');
    }
    
    if (! (input_num('num_days_worked') > 0)) {
        return array('success' => false, 'message' => _("No of days worked should greater than zero."), 'focus_to' => 'num_days_worked');
    }
    
    $cat_row = get_employee_rate_category_on_date($_POST['employee_code'], $_POST['from_date']);
        if (! is_array($cat_row)) {
            return array('success' => false, 'message' => _("No employee rate category found for selected from date."), 'focus_to' => 'employee_code');
        } else {
            $rate_row = get_employee_rate_on_date_and_category($_POST['from_date'], $cat_row['emp_rate_category']);
                if (! is_array($rate_row)) {
                    return array('success' => false, 'message' => _("No employee rate found for selected from date."), 'focus_to' => 'employee_code');
                }
        }
        
        return array("success" => true, "message" => "", "focus_to" => '');    
}

function handle_save_job_order_labour(&$jo_id, $selected_id) {
    $msg = can_save_job_order_labour($selected_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
        
    begin_transaction();   
        $row_state = 0;
        $hd_msg = handle_save_job_order_header($jo_id, $row_state);   
            if ($hd_msg['success'] === false) {
                return $hd_msg;
            }
            
        $num_days_worked = input_num('num_days_worked');
            
        $cat_row = get_employee_rate_category_on_date($_POST['employee_code'], $_POST['from_date']);
            $emp_rate_category = $cat_row['emp_rate_category'];
        
        $rate_row = get_employee_rate_on_date_and_category($_POST['from_date'], $emp_rate_category);
            $rate = $rate_row['rate'];

        if ($selected_id > 0) {
            $msg = update_job_order_labour($jo_id, $selected_id, $row_state, $_POST['employee_code'], $_POST['from_date'], $_POST['to_date'], 
                                           $num_days_worked, $emp_rate_category, $rate, $_POST['description']);
                if ($msg['success'] === false) {
                    cancel_transaction();
                    
                    return $msg;
                } else {
                    commit_transaction();
                    
                    return array('success' => true, 'message' => "Job card labour record #$selected_id has been updated.");
                }
        } else {
            $msg = add_job_order_labour($jo_id, $row_state, $_POST['employee_code'], $_POST['from_date'], $_POST['to_date'], $num_days_worked, 
                                        $emp_rate_category, $rate, $_POST['description']);
                if ($msg['success'] === false) {
                    cancel_transaction();
                    
                    return $msg;
                } else {
                    commit_transaction();
                    
                    return array('success' => true, 'message' => "New job card labour record #".$msg['id']." has been added.");
                }
        }
}

function can_delete_job_order_labour(&$jo_id, &$selected_id) { 
    $jo_hd_row = get_job_order_record($jo_id);
        if (is_array($jo_hd_row)) {
            if ($jo_hd_row['closed'] == 1) {
                return array('success' => false, 'message' => "Job card #$jo_id is already closed. Not allow to further update.");
            }
        }
        
    if ($selected_id > 0) {
        $row = get_job_order_labour_record($selected_id);
            if (! is_array($row)) {
                return array('success' => false, 'message' => "Another user has deleted job card labour record #$selected_id.");
            }
            
        hidden('selected_id', $selected_id);
    }

    return array("success" => true, "message" => "", "focus_to" => '');    
}

function handle_delete_job_order_labour(&$jo_id, &$selected_id) {
    $msg = can_delete_job_order_labour($jo_id, $selected_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
        
        $del_msg = delete_job_order_labour($selected_id);
            if ($del_msg != 1) {
                return array('success' => true, 'message' => "Could not delete job card labour record #$selected_id.");
            } else {
                return array('success' => true, 'message' => "Job card labour record #$selected_id has been deleted.");
            }
}

function view_job_order_labour_by_jo_id($jo_id, $width=70) {
    display_heading2("Job Card Labour Details");    br();

	start_table(TABLESTYLE, "width=$width%");
        
            $th = array(_("Id"), _("Employee"), _("From Date"), _("To Date"), _("Num Days Worked"), _("Rate Category"), _("Rate"), _("Description"));
            table_header($th);
            
            $k = 0;  
            $result = get_job_order_labours_details($jo_id);
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);

			label_cell($row['id'], 'align=center');
                        $labour_name = get_employee_formatted_long_name($row['employee_code']);
			label_cell($labour_name);
			label_cell(sql2date($row['from_date']), 'align=center');
			label_cell(sql2date($row['to_date']), 'align=center');
			label_cell($row['num_days_worked'], 'align=center');
			label_cell($row['rate_category_name']);
			qty_cell($row['rate']);
			label_cell(nl2br($row['description']));
                        
                    end_row();
            }

        end_table(2);
}

function display_job_order_labours_inquiry($jo_id, $employee_code, $from_date_, $to_date_, $emp_rate_category) {
    global $Ajax;
    
    $Ajax->activate('labours_inquiry');
    div_start('labours_inquiry');
	start_table(TABLESTYLE, "width=88%");
        
            $th = array(_("Id"), _("Job Card"), _("Employee"), _("From Date"), _("To Date"), _("Num Days Worked"), _("Rate Category"), _("Rate"), 
                        _("Description"));
            table_header($th);
            
            $k = 0;  
            $result = get_job_order_labour_details_by_filters(0, $jo_id, $employee_code, $from_date_, $to_date_, $emp_rate_category);
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);

			label_cell($row['id'], 'align=center');
                        $jo_id = $row["jo_id"];
                        label_cell(get_service_trans_view_str(ST_JOB_ORDER, $jo_id), 'align=center');
                        $labour_name = get_employee_formatted_long_name($row['employee_code']);
			label_cell($labour_name);
			label_cell(sql2date($row['from_date']), 'align=center');
			label_cell(sql2date($row['to_date']), 'align=center');
			label_cell($row['num_days_worked'], 'align=center');
			label_cell($row['emp_rate_category_name']);
			qty_cell($row['rate']);
			label_cell(nl2br($row['description']));
                        
                    end_row();
            }

        end_table(2);
    div_end();
}

?>