<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SITE_MATERIAL_RETURN_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 . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/inventory/includes/db/site_material_returns_inquiry_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/inventory/includes/db/movement_types_db.inc");
include_once($path_to_root . "/includes/ui/ui_view.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");

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

page(_($help_context = "Site Material Returns Inquiry"), false, false, "", $js);

if(isset($_GET['project_id'])){
    $_POST['from_project_id'] = $_GET['project_id'];
}

if(isset($_GET['FromDate'])){
    $_POST['from_date'] = $_GET['FromDate'];
}

if(isset($_GET['ToDate'])){
    $_POST['to_date'] = $_GET['ToDate'];
}

//	Query format functions -----------------------------------------------------------

function check_overdue($row){
    global $trans_type;
    if($trans_type == ST_SALESQUOTE)
        return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
}

function id($row){ 
    return get_inventory_trans_view_str(ST_SITE_MATERIAL_RETURN, $row['id']);
}

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

function from_location($row){
    return $row['from_location_name'];
}

function to_location($row){
    return $row['to_location_name'];
}

function from_project_name($row){
    return $row['from_project_description'];
}

function to_project_name($row){
    return $row['to_project_description'];
}

function tran_date($row){
    return sql2date($row['tran_date']);
}

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

function employee_name($row){
    if(strlen($row['employee_code']) > 0){
        return get_group_company_employee_formatted_long_name($row['company_code'], $row['employee_code']);
    }else{
        return;
    }
}

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

function edit_link($row){
    
    if($row['status'] == AS_NEW){
        $modify = "OrderNo";
        return pager_link( _("Edit"), "/inventory/site_material_returns.php?$modify=" . $row['id'], ICON_EDIT);
    }
    else{
        return null;
    }
}

//---------------------------------------------------------------------------------------------

if(!@$_GET['popup'])
    start_form();


start_table(TABLESTYLE_NOBORDER);
    start_row();
        ref_cells(text_bold("Id:"), 'id', '',null, '', true);
        ref_cells(text_bold("Reference:"), 'reference', '',null, '', true);
        date_cells("From Date:", 'from_date', '', null, -30);
        date_cells("To Date:", 'to_date', '', null, +30);
        group_companies_list_cells("Company:", "company_code", null, "Select a company", true);
        group_company_employees_simple_list_cells("Employee:", "employee_code", $_POST["company_code"], null, "Select an employee", true);
    end_row();
end_table();

start_table(TABLESTYLE_NOBORDER);
    start_row();
        locations_list_cells(_("Location:"), 'to_location', null, true, true);
        project_list_cells(_("From Project:"), 'from_project_id', null, _('Select a from project'), true);
        project_list_cells(_("To Project:"), 'to_project_id', null, _('Select a to project'), true);
        submit_cells('SearchOrders', _("Search"),'',_('Search site materials returns'), 'default');
    end_row();    
end_table();

//---------------------------------------------------------------------------------------------

$sql = get_sql_for_site_material_returns_inquiry($_POST['id'], $_POST['reference'], $_POST['to_location'], $_POST['from_date'], $_POST['to_date'],
                                                 $_POST['from_project_id'], $_POST['to_project_id'], $_POST['company_code'], $_POST['employee_code']);  
        
    $cols = array(  _("ID #") => array('fun'=>'id', 'align'=>'center'), 
                    _("Ref") => array('fun'=>'reference', 'align'=>'left'), 
                    _("Location") => array('fun'=>'to_location'), 
                    _("From Project") => array('fun'=>'from_project_name'),      
                    _("To Project") => array('fun'=>'to_project_name'),         
                    _("Date") => array('fun'=>'tran_date', 'align'=>'center'),
                    _("Company") => array('fun'=>'company_name'), 
                    _("Employee") => array('fun'=>'employee_name'),
                    _("Total") => array('fun'=>'total', 'align'=>'right'));

    array_append($cols, array( array('insert'=>true, 'fun'=>'edit_link', 'align'=>'center')));

$table =& new_db_pager('orders_tbl', $sql, $cols);
$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "99%";

display_db_pager($table);
submit_center('Update', _("Update"), true, '', null);


if(!@$_GET['popup']){
    end_form();
    end_page();
}

?>