<?php

function display_create_job_gate_pass_selecting_parameters() {
    start_table(TABLESTYLE_NOBORDER);
    
        start_row();
            text_cells_ex(text_bold('Gate Pass No:'), 'filtr_gp_no', 12, 20);
            text_cells_ex(text_bold('Job Card:'), 'jo_id', 12, 20);
            date_cells(_("From:"), 'from_date', inc_months: -2);
            date_cells(_("To:"), 'to_date');
            three_value_list_cells('Job Completed:', 'job_completed', null, '', 'All', 'Yes', 'Yes', 'No', 'No', true);
            three_value_list_cells('Temp. Printed:', 'temp_print', null, '', 'All', 'Yes', 'Yes', 'No', 'No', true);
            three_value_list_cells('Printed:', 'printed', null, '', 'All', 'Printed', 'Printed', 'Not Printed', 'Not Printed', true);
            
            submit_cells("Search", 'Search', '', false, false, ICON_SUBMIT);
        end_row();
        
    end_table(2);
}

function display_create_job_gate_pass_details($jo_id, $from_date, $to_date, $job_completed, $printed, $filtr_gp_no, $temp_print) {
    global $approval_statuses;
    
    div_start('trans_tbl');

        start_table(TABLESTYLE, "width=98%");

        $th = array(_("Gate Pass No"), _("Job Card"), _("Reference"), _("Date"), _("Approval Status"), _("Job Completed"), _("Vehicle"), _("Driver Name"), _("Comment"), 
                    _("Temp.Printed"), _("Entrance"), _("Temp."), _("Exit"));
            table_header($th);
            
        $j = 1; $k = 0;
        
        $result = get_job_gate_pass_details($jo_id, $from_date, $to_date, $job_completed, $printed, $filtr_gp_no, $temp_print);	
            while ($row = db_fetch($result)) {
                $jo_id = $row["id"];
                
                alt_table_row_color($k);

                    label_cell($row["rec_id"], 'align=center');
                    label_cell(get_service_trans_view_str(ST_JOB_ORDER, $jo_id), 'align=center');
                    label_cell($row['jo_ref']);
                    label_cell(sql2date($row["date_"]));
                    label_cell($approval_statuses[$row["approval_status"]], 'align=center');
                    label_cell($row['job_completed'] == 1 ? 'Yes' : 'No', 'align=center');
                    label_cell(text_bold(get_asset_formatted_name($row['vehicle_id'])));
                    
                    if ($row['rec_id'] > 0) {
                        if (! isset($_POST['driver_name_'.$jo_id]) || ! (strlen($_POST['driver_name_'.$jo_id]) > 0)) {
                            $_POST['driver_name_'.$jo_id] = $row["driver_name"];
                        }
                        if (! isset($_POST['comment_'.$jo_id]) || ! (strlen($_POST['comment_'.$jo_id]) > 0)) {
                            $_POST['comment_'.$jo_id] = $row["comment"];
                        }
                    } 

                    text_cells_ex(null, 'driver_name_'.$jo_id, 30, 200, null, null, null, null, true);
                    text_cells_ex(null, 'comment_'.$jo_id, 50, 200, null, null, null, null, true);
                    label_cell(get_yes_no_name($row['temp_print']), 'align=center');

                    $order_no = array("jo_id" => $jo_id, "driver_name" => $_POST['driver_name_'.$jo_id], "comment" => $_POST['comment_'.$jo_id]);

                    $print_entrance_text = 'Print Entrance';
                    $print_link = print_document_link($order_no, $print_entrance_text, true, ST_JOB_GATE_PASS_ENTRANCE);
                    label_cell($print_link, 'align=center');

                    $print_temp_text = 'Print Temp.';
                    $print_link_2 = print_document_link($order_no, $print_temp_text, true, ST_JOB_GATE_PASS_TEMP);
                    label_cell($print_link_2, 'align=center');
                    
                    if ($row["job_completed"] == 1) {
                        $print_exit_text = 'Print Exit';
                        $print_link = print_document_link($order_no, $print_exit_text, true, ST_JOB_GATE_PASS_EXIT);
                        label_cell($print_link, 'align=center');
                    } else {
                        label_cell('');
                    }

                end_row();

                    if ($j == 12) {
                        $j = 1;
                        table_header($th);
                    }
                    
                    $j++;
            }

        end_table(2);
    div_end();
}

function save_job_gate_pass_($jo_id, $driver_name, $comment, $temp_print=0) {
    $msg = "Job card # $jo_id";
    
    if (! (strlen($driver_name) > 0)) {
        return array('success' => false, 'message' => "$msg Driver name cannot be empty.");
    }
    
    $gp_row = get_job_gate_pass_record_by_jo_id($jo_id);
        if (is_array($gp_row)) {
            update_job_gate_pass($gp_row['id'], $driver_name, $comment, $temp_print);
        } else {
            add_job_gate_pass($jo_id, $driver_name, $comment, $temp_print);
        }
        
        return array('success' => true, 'message' => '');
}

function clear_create_job_gate_pass() {
    if (! ($_POST['jo_id'] > 0)) {
        return;
    }
   
       $result = get_job_gate_pass_details($_POST['jo_id'], $_POST['from_date'], $_POST['to_date'], $_POST['job_completed'], $_POST['printed'], $_POST['filtr_gp_no']);	
            while ($row = db_fetch($result)) {
                $jo_id = $row["id"];
                
                unset($_POST['driver_name_'.$jo_id]);
                unset($_POST['comment_'.$jo_id]);
            }
}

?>