<?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_MAINTENANCE_SCHEDULE_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/db/job_orders_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 = "Maintenance Schedule Inquiry"), false, false, "", $js);

function _vehicle($row) {
    return get_asset_formatted_name($row['asset_id']);
}

function _last_service_date($row) {
    if ($row['last_service_date'] > INFINITE_START_DATE) {
        return sql2date($row['last_service_date']);
    } else {
        return;
    }
}

function _next_service_days($row) {
    return $row['next_service_days'];
}

function _next_service_date($row) {
    if ($row['next_service_days'] > 0) {
        return sql2date($row['next_service_date']);
    } else {
        return;
    }
}

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            date_cells(_("Schedule From Date:"), 'from_schedule_date', null, null, 0, 0);
            date_cells(_("Schedule To Date:"), 'to_schedule_date', null, null, 0, 4);
            submit_cells('SearchOrders', _("Search"), '', _('Search'), false, ICON_SUBMIT);
        end_row();
    end_table(1);
    
    $customer_name = '';
    if (isset($_POST['customer_name'])) {
        $customer_name = $_POST['customer_name'];
    }

    $sql = get_sql_for_maintenance_schedule_inquiry($_POST['from_schedule_date'], $_POST['to_schedule_date']);

        $cols = array(  _("Vehicle/Tool/Location") => array('fun'=>'_vehicle', 'align'=>'left'), 
                        _("Last Service Date") => array('fun'=>'_last_service_date', 'align'=>'center'), 
                        _("Next Service Days") => array('fun'=>'_next_service_days', 'align'=>'center'), 
                        _("Next Service Date") => array('fun'=>'_next_service_date', 'align'=>'center'), 
                        array('insert'=>true, 'fun'=>'_edit_link', 'align'=>'center')
        );

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

$table->width = "65%";

display_db_pager($table);

end_form(2);
end_page();
