<?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 . "/includes/ui/ui_view_custom.inc"); 
include_once($path_to_root . "/includes/ui/ui_controls_custom.inc"); 
include_once($path_to_root . "/services/includes/db/job_orders_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 = "My Pending Jobs 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) {
    return get_employee_formatted_long_name($row["engineer_code"]);
}

function _tech_officer($row) {
    return get_employee_formatted_long_name($row["tech_officer_code"]);
}

function _current_job_owner($row) {
    return get_employee_formatted_long_name($row["next_job_owner_id"]);
}

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

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

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

function _edit_link($row) {
    global $page_nested;

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

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            date_cells(_("From Date:"), 'from_date', null, null, 0, 0);
            date_cells(_("To Date:"), 'to_date');
            
            submit_cells('SearchOrders', _("Search"), '', _('Select documents'), false, ICON_SUBMIT);
        end_row();

    end_table(1);
    
    $sql = get_sql_for_my_pending_jobs_inquiry($_POST['from_date'], $_POST['to_date'], $_SESSION['wa_current_user']->user);

    $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")=> 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'), 
                    _("Job Aacceptance") => array('fun'=>'_job_accepted', 'align'=>'center'), 
                    _("Job Complete") => array('fun'=>'_job_completed', 'align'=>'center'), 
                    _("Released") => array('fun'=>'_job_released', '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();
