<?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_SERVICE_COMPLAIN_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/ui/service_complains_ui.inc"); 
include_once($path_to_root . "/services/includes/db/service_complains_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/payroll/includes/db/employees_db.inc"); 

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

global $systypes_array;

page(_($help_context = $systypes_array[ST_SERVICE_COMPLAIN]."s Inquiry"), false, false, "", $js);

function _view($row) {
    $id = $row["id"];
    return get_service_trans_view_str(ST_SERVICE_COMPLAIN, $id);
}

function _job_view($row) {
    $jo_id = $row["jo_id"];
    
    if ($jo_id) {
        return get_service_trans_view_str(ST_JOB_ORDER, $jo_id);
    } else {
        return;
    }
}

function _reference($row) {
    return $row["ref"];
}

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

function _vehicle($row) {
    return $row["vehicle_id"].' - '.$row["vehicle_name"];
}

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

function _issuing_officer($row) {
    return $row["real_name"];
}

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

function _customer_address($row) {
    return nl2br($row["customer_address"]);
}

function _job_details($row) {
    return nl2br($row["description"]);
}

function _closed($row) {
    if ($row['closed'] == 1) {
        return 'Yes';
    } else {
        return 'No';
    }
}

function _booking($row) {
    if ($row['booking'] == 1) {
        return 'Yes';
    } else {
        return 'No';
    }
}

function _schedue_date($row) {
    if ($row['booking'] == 1) {
        return sql2date($row['schedule_date']);
    } else {
        return;
    }
}

function _edit_link($row) {
    if ($row['closed'] == 0) {
        if ($row['source_type'] == ST_SERVICE_COMPLAIN) {
            return pager_link( _("Job"),"/services/job_order_entry.php?trans_no=".$row['jo_id']);
        } else {
            return pager_link( _("Edit"),"/services/service_complains.php?cmp_id=".$row['id'], ICON_EDIT);
        }
    } else {
        return;
    }
}

start_form();

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold(_("Id:")), 'id', '',null, '', true);
            ref_cells(text_bold(_("Job Card:")), 'jo_id', '',null, '', true);
            ref_cells(_("Reference:"), 'reference', '', null, '', true);
            three_value_list_cells(_("Has Job:"), 'has_job', null, '', '', 'Yes', 'Yes', 'No', 'No', true);
            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'));
        end_row();
        start_row();
            custom_values_list_cells('Req. Department:', 'req_department_id', create_request_departmens_array(), null, '', true);
            system_users_list_cells("Issuing Officer:", 'issuing_officer', null, '', true);
            text_cells('Name:', 'customer_name', null, 20, 300, 'Customer Name');
            text_cells('Address:', 'customer_address', null, 20, 300, 'Customer Address');
            text_cells('Complain:', 'complain', null, 20, 300, 'Complain/ Request Details');
            three_value_list_cells(_("Closed:"), 'closed', null, '', '', 'Yes', 'Yes', 'No', 'No', true);
            three_value_list_cells(_("Booking:"), 'booking', null, '', '', 'Yes', 'Yes', 'No', 'No', true);
            
            submit_cells('SearchOrders', _("Search"), '', _('Search'), false, ICON_SUBMIT);
        end_row();
    end_table(1);
    
    $customer_name = $customer_address = $complain = '';
    if (isset($_POST['customer_name'])) {
        $customer_name = $_POST['customer_name'];
    }
    if (isset($_POST['customer_address'])) {
        $customer_address = $_POST['customer_address'];
    }
    if (isset($_POST['complain'])) {
        $complain = $_POST['complain'];
    }

    $sql = get_sql_for_service_complains_inquiry($_POST['id'], $_POST['jo_id'], $_POST['has_job'], $_POST['reference'], $_POST['from_date'], $_POST['to_date'], 
                                                 $_POST['vehicle_id'], $_POST['req_department_id'], $_POST['issuing_officer'], $customer_name, $_POST['closed'], null, null, 
                                                 $customer_address, $complain, $_POST['booking']);

    $cols = array(  _("#") => array('fun'=>'_view', 'align'=>'center'), 
                    _("Job Card")=> array('fun'=>'_job_view', 'align'=>'center'), 
                    _("Reference")=> array('fun'=>'_reference', 'align'=>'center'), 
                    _("Date") => array('fun'=>'_date', 'type'=>'date', 'align'=>'center'),
                    _("Vehicle/Tool/Location") => array('fun'=>'_vehicle', 'align'=>'left'), 
                    _("Req. Department") => array('fun'=>'_request_department', 'align'=>'left'), 
                    _("Issuing Officer") => array('fun'=>'_issuing_officer', 'align'=>'left'), 
                    _("Customer Name") => array('fun'=>'_customer_name', 'align'=>'left'), 
                    _("Customer Address") => array('fun'=>'_customer_address', 'align'=>'left'), 
                    _("Complain Details") => array('fun'=>'_job_details', 'align'=>'left'), 
                    _("Closed") => array('fun'=>'_closed', 'align'=>'center'), 
                    _("Booking") => array('fun'=>'_booking', 'align'=>'center'), 
                    _("Schedue Date") => array('fun'=>'_schedue_date', '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();
