<?php

function  display_create_gate_pass_selecting_parameters() {
    start_table(TABLESTYLE_NOBORDER);
    
        start_row();
            $gp_trans_array = build_gate_pass_transaction_types_array();
            custom_values_list_cells('Trans Type:', 'trans_type_id', $gp_trans_array, null, 'Select a transaction type', true);
            date_cells(_("From:"), 'from_date');
            date_cells(_("To:"), 'to_date');
            three_value_list_cells('Printed:', 'printed', null, 'All', 'All', 'Printed', 'Printed', 'Not Printed', 'Not Printed', true);
            text_cells_ex('Gate Pass No:', 'filtr_gp_no', 12, 20);
            text_cells_ex('Trans No:', 'filtr_trans_no', 12, 20);
            
            submit_cells("Search", 'Search');
        end_row();
        
    end_table(2);
}

function build_gate_pass_transaction_types_array() {
    global $systypes_array;
    
    $gp_trans_array = array();
    
        $gp_trans_array[ST_REQTRANSFER] = $systypes_array[ST_REQTRANSFER];
        $gp_trans_array[ST_PR_ISSUE] = $systypes_array[ST_PR_ISSUE];
        $gp_trans_array[ST_MATERIAL_ISSUE] = $systypes_array[ST_MATERIAL_ISSUE];
        $gp_trans_array[ST_LOCTRANSFER] = $systypes_array[ST_LOCTRANSFER];
        $gp_trans_array[ST_INVENTORY_ISSUE] = $systypes_array[ST_INVENTORY_ISSUE];
        $gp_trans_array[ST_DAILY_ITEM_ISSUE] = $systypes_array[ST_DAILY_ITEM_ISSUE];
        $gp_trans_array[ST_DAILY_TOOL_ISSUE] = $systypes_array[ST_DAILY_TOOL_ISSUE];
        $gp_trans_array[ST_RAW_MATERIAL_ISSUE] = $systypes_array[ST_RAW_MATERIAL_ISSUE];
        $gp_trans_array[ST_ASSET_TRANSFER] = $systypes_array[ST_ASSET_TRANSFER];
        $gp_trans_array[ST_ASSET_REQUISITION_TRANSFER] = $systypes_array[ST_ASSET_REQUISITION_TRANSFER];
        
        return $gp_trans_array;
}

function display_create_gate_pass_details($trans_type_id, $from_date, $to_date, $printed, $filtr_gp_no, $filtr_trans_no) {
    global $systypes_array;
    
    div_start('trans_tbl');

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

        $th = array(_("Gate Pass No"), _("Tran. Type"), _("#"), _("Reference"), _("Date"), _("Employee"), _("Memo"),  _("Vehicle"), 
                    _("Vehicle No"), _("Driver Name"), _("Comment"), "");
            table_header($th);
            
            if (! ($trans_type_id > 0)) {
                end_table();
                div_end();
                
                display_note('Please select a transation type and vehicle in the table then click print.', 2, 2, 'style=color:red');
                return;
            }

        $j = 1;     $k = 0; //row colour counter
        
        $result = get_gate_pass_details($trans_type_id, $from_date, $to_date, $printed, $filtr_gp_no, $filtr_trans_no);	
            while ($row = db_fetch($result)) {
                $trans_no = $row["trans_no"];
                
                alt_table_row_color($k);

                    label_cell($row["rec_id"], 'align=center');
                    label_cell($systypes_array[$row["type"]]);
                    label_cell(get_trans_view_str($row["type"], $trans_no));
                    label_cell($row['reference']);
                    label_cell(sql2date($row["trans_date"]));
                    
                    $emp_name = '';
                    if (isset($row["employee_code"])) {
                        $emp_row = get_employee($row["employee_code"]);
                        if (is_array($emp_row)) {
                            $emp_name = '['.$emp_row['employee_code'].'] '.$emp_row['initials'].' '.$emp_row['first_name'].' '.$emp_row['last_name'];
                        }  
                    }
                    label_cell($emp_name);
                        
                    label_cell(nl2br($row["memo"]));
                    
                    if ($row['rec_id'] > 0) {
//                        
//                        label_cell($row["vehicle_name"]);
//                        label_cell($row["vehicle_no"]);
//                        label_cell($row["driver_name"]);
//                        label_cell(nl2br($row["comment"]));
//                        
                        if (isset($_POST['Search'])) {
                            $_POST['vehicle_id_'.$trans_no] = $row["vehicle_id"];
                        } 
                        if (! isset($_POST['vehicle_id_'.$trans_no])) {
                            $_POST['vehicle_id_'.$trans_no] = $row["vehicle_id"];
                        }
                        if (! isset($_POST['vehicle_no_'.$trans_no]) || ! (strlen($_POST['vehicle_no_'.$trans_no]) > 0)) {
                            $_POST['vehicle_no_'.$trans_no] = $row["vehicle_no"];
                        }
                        if (! isset($_POST['driver_name_'.$trans_no]) || ! (strlen($_POST['driver_name_'.$trans_no]) > 0)) {
                            $_POST['driver_name_'.$trans_no] = $row["driver_name"];
                        }
                        if (! isset($_POST['comment_'.$trans_no]) || ! (strlen($_POST['comment_'.$trans_no]) > 0)) {
                            $_POST['comment_'.$trans_no] = $row["comment"];
                        }
//                        
//                        hidden('vehicle_id_'.$trans_no, $row["vehicle_id"]);
//                        hidden('vehicle_no_'.$trans_no, $row["vehicle_no"]);
//                        hidden('driver_name_'.$trans_no, $row["driver_name"]);
//                        hidden('comment_'.$trans_no, $row["comment"]);
                    } 
//                    else {

                        vehicles_list_cells('', 'vehicle_id_'.$trans_no, null, 'Temporary Vehicle', true);
                        if ($_POST['vehicle_id_'.$trans_no] > 0) {
                            $vehicle = get_vehicle($_POST['vehicle_id_'.$trans_no]);
                                label_cell($vehicle['plate_number']);
                                $_POST['vehicle_no_'.$trans_no] = $vehicle['plate_number'];
                                hidden('vehicle_no_'.$trans_no, $vehicle['plate_number']);
                        } else {
                            text_cells_ex(null, 'vehicle_no_'.$trans_no, 8, 20, null, null, null, null, true);
                        }
                        text_cells_ex(null, 'driver_name_'.$trans_no, 8, 50, null, null, null, null, true);
                        text_cells_ex(null, 'comment_'.$trans_no, 20, 200, null, null, null, null, true);
                        
//                    }
                    
                        $order_no = array("type" => $row['type'], 
                                          "trans_no" => $trans_no, 
                                          "vehicle_id" => $_POST['vehicle_id_'.$trans_no],
                                          "vehicle_no" => $_POST['vehicle_no_'.$trans_no], 
                                          "driver_name" => $_POST['driver_name_'.$trans_no],
                                          "comment" => $_POST['comment_'.$trans_no]);
                        
                        $print_cheque_text = 'Print';
                        $print_link = print_document_link($order_no, $print_cheque_text, true, ST_GATE_PASS);
                        
                        label_cell($print_link, 'align=center');

                end_row();

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

        end_table(2);
    div_end();
}

function save_gate_pass($trans_type, $trans_no, $vehicle_id, $vehicle_no, $driver_name, $comment) {
    $msg = "Trans # $trans_no";
    
    $gp_row = get_gate_pass_record($trans_no, $trans_type);
        if (is_array($gp_row)) {
            update_gate_pass($gp_row['id'], $vehicle_id, $vehicle_no, $driver_name, $comment);
        } else {
            
            if (! ($vehicle_id > 0)) {
                if (! (strlen($vehicle_no) > 0)) {
                    return array('success' => false, 'message' => "$msg Vehicle no is mandatory when selecting temporaly vehicle.");
                }
            } 
            
            if (! (strlen($driver_name) > 0)) {
                return array('success' => false, 'message' => "$msg Driver name cannot be empty.");
            }
            
            add_gate_pass($trans_type, $trans_no, $vehicle_id, $vehicle_no, $driver_name, $comment);
        }
        
        return array('success' => true, 'message' => '');
}

function clear_create_gate_pass() {
    if (! ($_POST['trans_type_id'] > 0)) {
        return;
    }
   
       $result = get_gate_pass_details($_POST['trans_type_id'], $_POST['from_date'], $_POST['to_date'], $_POST['printed'], $_POST['filtr_gp_no'], 
                                       $_POST['filtr_trans_no']);	
            while ($row = db_fetch($result)) {
                $trans_no = $row["trans_no"];
                
                unset($_POST['vehicle_id_'.$trans_no]);
                unset($_POST['vehicle_no_'.$trans_no]);
                unset($_POST['driver_name_'.$trans_no]);
                unset($_POST['comment_'.$trans_no]);
            }
}

?>