<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_JOB_CARD_INQ';
$path_to_root = "../..";

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc"); 
include_once($path_to_root . "/services/includes/ui/job_order_entry_ui.inc"); 
include_once($path_to_root . "/services/includes/db/job_orders_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc"); 
include_once($path_to_root . "/includes/ui/ui_controls_custom.inc"); 
include_once($path_to_root . "/includes/ui/ui_view_custom.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/payroll/includes/db/employees_db.inc"); 
include_once($path_to_root . "/assets/includes/db/assets_db.inc"); 

$js = "";
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

page(_($help_context = "Job Card Inquiry"), false, false, "", $js);

function _job_trans_view($row) {
    $jo_id = $row["id"];
    return get_service_trans_view_str(ST_JOB_ORDER, $jo_id);
}

function _parent_job_view($row) {
    $jo_id = $row["parent_jo_id"];
    
    if ($jo_id == 0) {
        return;
    }
    
    return get_service_trans_view_str(ST_JOB_ORDER, $jo_id);
}

function _customer_complain_view($row) {
    $comp_id = $row["source_trans_no"];
    
    if ($comp_id == 0) {
        return;
    }
    
    return get_service_trans_view_str(ST_SERVICE_COMPLAIN, $comp_id);
}

function _customer_name($row) {
    return $row["customer_name"];
}

function _job_reference($row) {
    return $row["jo_ref"];
}

function _job_date($row) {
    return $row["date_"];
}

function _job_vehicle($row) {
    $vehicle_name = get_asset_formatted_name($row["vehicle_id"]);
    return text_bold($vehicle_name);
}

function _request_department($row) {
    return $row["req_department"];
}

function _department($row) {
    return $row["department"];
}

function _section($row) {
    return $row["section"];
}

function _engineer($row) {
    if (isset($row["engineer_code"]) && strlen($row["engineer_code"]) > 1) {
        return get_employee_formatted_long_name($row["engineer_code"]);
    } else {
        return;
    }
}

function _tech_officer($row) {
    if (isset($row["tech_officer_code"]) && strlen($row["tech_officer_code"]) > 1) {
        return get_employee_formatted_long_name($row["tech_officer_code"]);
    } else {
        return;
    }
}

function _current_job_owner($row) {
    if (isset($row["next_job_owner_id"]) && strlen($row["next_job_owner_id"]) > 1) {
        return get_employee_formatted_long_name($row["next_job_owner_id"]);
    } else {
        return;
    }
}

function _service($row) {
    return $row["service"] == 1 ? 'Yes' : 'No';
}

function _job_accepted($row) {
    return $row["job_accepted"] == 1 ? 'Yes' : 'No';
}

function _job_completed($row) {
    return $row["job_completed"] == 1 ? 'Yes' : 'No';
}

function _approval_status($row) {
    global $approval_statuses;
    return $approval_statuses[$row['approval_status']];
}

function _edit_link($row) {
    global $page_nested;

    if ($row['parent_jo_id'] > 0) {
        return $page_nested ? '' : pager_link( _("Edit"),"/services/sub_job_order_entry.php?trans_no=".$row['id'], ICON_EDIT);
    } else {
        return $page_nested ? '' : pager_link( _("Edit"),"/services/job_order_entry.php?trans_no=".$row['id'], ICON_EDIT);
    }
}

start_form();

    start_table(TABLESTYLE_NOBORDER);
    
        start_row();
            ref_cells(text_bold(_("Job Card:")), 'id', '',null, '', true);
            ref_cells(text_bold(_("Parent Job:")), 'p_jo_id', '',null, '', true);
            three_value_list_cells(_("Has Complain:"), 'has_complain', null, '', '', 'Yes', 'Yes', 'No', 'No', true);
            text_cells('Customer Name:', 'customer_name', null, 20, 300, 'Customer Name');
            ref_cells(_("Reference:"), 'reference', '', null, '', true);
        end_row();
        start_row();
            date_cells(_("From Date:"), 'from_date', null, null, 0, -1);
            date_cells(_("To Date:"), 'to_date');
            label_cells(_("Vehicle/Tool/Location:"), asset_list('vehicle_id'));
            custom_values_list_cells('Req. Department:', 'req_department_id', create_request_departmens_array(), null, '', true);
            value_list_cells('Department:', 'department_id', 'DEPARTMENTS', null, '', true);
        end_row();            
        start_row();
            $dep_row = get_listvalue_by_id($_POST['department_id']);
                if (is_array($dep_row)) {
                    $section_list_code = $dep_row['value_code'];
                    value_list_cells('Section:', 'section_id', "VMS_$section_list_code", null, '', true);
                } else {
                    $_POST['section_id'] = 0;    
                }
            employee_search_list_cells(_("Sectional Engineer:"), 'engineer_code', null, false, true);
            employee_search_list_cells(_("Technical Officer:"), 'tech_officer_code', null, false, true);
            employee_search_list_cells(_("Current Job Owner:"), 'next_job_owner_id', null, false, true);
        end_row();            
        start_row();
            three_value_list_cells(_("Service:"), 'service', null, '', '', 'Yes', 'Yes', 'No', 'No', true);
            three_value_list_cells(_("Job Acceptance:"), 'job_accepted', null, '', '', 'Yes', 1, 'No', 0, true);
            three_value_list_cells(_("Work Completed:"), 'job_completed', null, '', '', 'Yes', 1, 'No', 0, true);
            three_value_list_cells(_("Sub Job:"), 'sub_job', null, '', '', 'Yes', 'Yes', 'No', 'No', true);
            custom_values_list_cells("Approval Status:", "approval_status", job_order_approval_status_array(true), null, false, true);
            
            submit_cells('SearchOrders', _("Search"), '', _('Select documents'), false, ICON_SUBMIT);
        end_row();

    end_table(1);
    
    $customer_name = '';
    if (isset($_POST['customer_name'])) {
        $customer_name = $_POST['customer_name'];
    }

    $sql = get_sql_for_job_orders_inquiry($_POST['id'], $_POST['p_jo_id'], $_POST['has_complain'], $customer_name, $_POST['reference'], $_POST['from_date'], $_POST['to_date'], 
                                          $_POST['vehicle_id'], $_POST['req_department_id'], $_POST['department_id'], $_POST['section_id'], $_POST['next_job_owner_id'], 
                                          $_POST['job_accepted'], $_POST['job_completed'], $_POST['approval_status'], $_POST['sub_job'], $_POST['engineer_code'], 
                                          $_POST['tech_officer_code'], $_POST['service']);

    $cols = array(  _("#") => array('fun'=>'_job_trans_view', 'align'=>'center'), 
                    _("Parent")=> array('fun'=>'_parent_job_view', 'align'=>'center'), 
                    _("Complain") => array('fun'=>'_customer_complain_view', 'align'=>'center'), 
                    _("Customer/ Request")=> array('fun'=>'_customer_name', 'align'=>'left'), 
                    _("Reference")=> array('fun'=>'_job_reference', 'align'=>'center'), 
                    _("Date") => array('fun'=>'_job_date', 'type'=>'date', 'align'=>'center'),
                    _("Vehicle/ Tool/ Location") => array('fun'=>'_job_vehicle', 'align'=>'left'), 
                    _("Req. Department") => array('fun'=>'_request_department', 'align'=>'left'), 
                    _("Department") => array('fun'=>'_department', 'align'=>'left'), 
                    _("Section") => array('fun'=>'_section', 'align'=>'left'), 
                    _("Sectional Engineer") => array('fun'=>'_engineer', 'align'=>'left'), 
                    _("Technical Officer") => array('fun'=>'_tech_officer', 'align'=>'left'), 
                    _("Current Job Owner") => array('fun'=>'_current_job_owner', 'align'=>'left'), 
                    _("Service") => array('fun'=>'_service', 'align'=>'center'), 
                    _("Job Aacceptance") => array('fun'=>'_job_accepted', 'align'=>'center'), 
                    _("Work Completed") => array('fun'=>'_job_completed', 'align'=>'center'), 
                    _("Approval Status") => array('fun'=>'_approval_status', 'align'=>'center'), 
                    array('insert'=>true, 'fun'=>'_edit_link', 'align'=>'center')
    );


$table =& new_db_pager('orders_tbl', $sql, $cols);

$table->width = "99%";

display_db_pager($table);

end_form(2);
end_page();
