<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_VEHICLE_REQUEST_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 . "/finance/includes/db/insured_items_inquiry_db.inc");
include_once($path_to_root . "/sales/includes/db/customers_db.inc");
include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");
include_once($path_to_root . "/transport/includes/db/vehicles_db.inc");
include_once($path_to_root . "/includes/db/inventory_db.inc");

//include_once($path_to_root . "/includes/ui/ui_view.inc");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();


page(_($help_context = "Insured Items Inquiry"), false, false, "", $js);


function view_link($row)
{
	return get_finance_trans_view_str(ST_INSURED_ITEMS, $row['id']);
}

function display_ii_description($row)
{

    return $row["description"];
    
}

function display_ii_policy_no($row)
{

    return $row["name"];
    
}

function display_ba_name($row)
{
    return $row["bank_account_number"];
}

function display_insurer_type($row)
{
    if($row["insurer_type"] == 0){
        return "Supplier";
    }
    else{
        return "Customer";
    }
}

function display_insurer($row)
{
    if($row["insurer_type"] == 0){
        return get_supplier_name($row["insurer_id"]);
    }
    else{
        return get_customer_name($row["insurer_id"]);
    }
}


function display_insuree_type($row)
{
    global $insuree_types;
    
    if($row["insuree_type"] == 0){
        return "";
    }
    
    return $insuree_types[$row["insuree_type"]];

}

function display_insuree($row)
{
    $insuree_type = $row['insuree_type'];
    
    switch ($insuree_type){
        case INSU_VEHICLE:
            $vehicle = get_vehicle($row['insuree']);
            return $vehicle['description'];
        case INSU_ASSET:
            $asset = get_asset($row['insuree']);
            return $asset['description'];
        case INSU_EMPLOYEE:
            $employee = get_employee($row['insuree']);       
            $emp_name = "[".$employee['employee_code']."] - ".$employee['initials']." ".$employee['last_name'];   
            return $emp_name;
        case INSU_ITEM:
            $item = get_stock_item_edit_info($row['insuree']);
            return $item['description'];
        case INSU_REFERENCE:
            return $row['insuree'];

        default:
            return "";
    }        
}

function format_sum_insured($row)
{

    return number_format($row['insured_amount'],2);

}

function format_premium($row)
{

    return number_format($row['premium'],2);

}


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

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


function convert_last_paid_date($row)
{
    if(substr($row['last_payment_date'], 0, 4) == "0000"){
        return null;
    }
    
    return sql2date($row['last_payment_date']);
}


function display_on_hold_name($row)
{
    return get_yes_no_name($row['inactive']);
}


function display_expired_name($row)
{
    return get_yes_no_name($row['expired']);
}

function edit_link($row) 
{

    if($row['expired'] == 1){
        return null;
    }
    
    $modify = "id";
    return pager_link( _("Edit"), "/finance/insured_items.php?$modify=" . $row['id'], ICON_EDIT);
    
}


function display_original_bond_id($row)
{
    if($row['original_bond_id'] > 0){
        return $row['original_bond_id'];
    }
    else{
        return "";
    }
}



if (get_post('SearchOrders')) 
{
	$Ajax->activate('orders_tbl');
}


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

start_table(TABLESTYLE_NOBORDER);
start_row();

text_cells(_("Item Id:"), 'insured_item_id', '',8, '', false);
ref_cells(_("Policy No :"), 'insured_item_name', '',null, '', false);
date_cells(_("Renewal From:"), 'from_date', '', null, -90);
date_cells(_("Renewal To:"), 'to_date', '', null, +90);



submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
//hidden('order_view_mode', $_POST['order_view_mode']);
//hidden('type', $trans_type);

end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_insured_item_inquiry(input_num('insured_item_id'),get_post('insured_item_name'),
                                        get_post('from_date'),get_post('to_date'));


	$cols = array(
		_("#")=> array('fun'=>'view_link', 'align' => 'center') , 
		_("Description") => array('fun'=>'display_ii_description'),  
        _("Policy No") => array('fun'=>'display_ii_policy_no'),  
        _("Insurer Type") => array('fun'=>'display_insurer_type', 'align' => 'center'),
        _("Insurer") => array('fun'=>'display_insurer'),
        _("Insuree Type") => array('fun'=>'display_insuree_type', 'align' => 'center'),
        _("Insuree") => array('fun'=>'display_insuree'),
        _("Sum Insured") => array('fun'=>'format_sum_insured', 'align' => 'right'), 
        _("Premium") => array('fun'=>'format_premium', 'align' => 'right'), 
        _("Start Date") => array('fun'=>'convert_start_date', 'align' => 'center'),
        _("Renewal Date") => array('fun'=>'convert_renewal_date', 'align' => 'center'),      
        _("On Hold") => array('fun'=>'display_on_hold_name', 'align' => 'center'),                   
        _("Expred") => array('fun'=>'display_expired_name', 'align' => 'center'), 
		);
    
    array_append($cols, array(
        array('insert'=>true, 'fun'=>'edit_link'),
		));


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

$table->width = "95%";

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

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