<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_ASSETTRANSVIEW';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/finance/includes/db/insured_items_db.inc");
include_once($path_to_root . "/transport/includes/db/vehicles_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 . "/includes/db/inventory_db.inc");
include_once($path_to_root . "/includes/sysnames.inc");
include_once($path_to_root . "/finance/includes/db/insured_items_inquiry_db.inc");


$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Insured Items"), true, false, "", $js);

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


if ($_GET['trans_no'] != "")
{
    $insured_item_id = $_GET['trans_no'];
}

display_heading("Insured Item ID: " . $insured_item_id);

function display_isured_item($insured_item_id)
{
        start_table(TABLESTYLE2, "width=60%");


        $myrow = view_insured_items($insured_item_id);
        start_row();

        label_cells(_("Description"), $myrow['description'], "class='tableheader2'");
        end_row();
        label_cells(_("Policy No"), $myrow['name'], "class='tableheader2'");
        end_row();

        if($myrow['insurer_type'] == 0){
            $insurer_type = "Supplier";
        }else{
            $insurer_type = "Customer";
        }

        label_cells(_("Insurer Type"), $insurer_type, "class='tableheader2'");
        end_row();


        if($myrow['insurer_type'] == 0){
            label_cells(_("Insurer"), $myrow['supplier_name'], "class='tableheader2'");
            end_row();
        }else{
            label_cells(_("Insurer"), $myrow['customer_name'], "class='tableheader2'");
            end_row(); 
        }


            switch ($myrow['insuree_type']){       

                case INSU_VEHICLE:
                    $vehicle = get_vehicle($myrow['insuree']);
                    $description = $vehicle['description'];             
                    break;
                case INSU_ASSET:
                    $asset = get_asset($myrow['insuree']);
                    $description = $asset['description'];            
                    break;
                case INSU_EMPLOYEE:
                    $employee = get_employee($myrow['insuree']);
                    $description = $employee['employee_code'].'-'.$employee['initials'].'-'.$employee['last_name'];             
                    break;
                case INSU_ITEM:
                    $item = get_item_edit_info($myrow['insuree']);
                    $description = $item['description'];                       
                    break;
                case INSU_REFERENCE:
                    $description = $myrow['insuree'];             
                    break;

                default:
                    break;
            }

            if($myrow["insuree_type"] > 0){
                global $insuree_types;
            }else{
                $insuree_types =-1;
            }


        label_cells(_("Insuree Type"), $insuree_types[$myrow["insuree_type"]], "class='tableheader2'");
        end_row();

         if($myrow["insuree_type"] > 0){
                $description;
            }else{
                $description = '';
            }

        label_cells(_("Insuree"), $description, "class='tableheader2'");
        end_row();
        label_cells(_("Sum Insured"), number_format($myrow['insured_amount'], 2), "class='tableheader2'");
        end_row();
        label_cells(_("Premium"), number_format($myrow['premium'], 2), "class='tableheader2'");
        end_row();
        label_cells(_("Start Date"), sql2date($myrow['start_date']), "class='tableheader2'");
        end_row();
        label_cells(_("Renewal Date"), sql2date($myrow['renewal_date']), "class='tableheader2'");
        end_row();
        label_cells(_("Last Renewed On"), sql2date($myrow['last_renewed_on']), "class='tableheader2'");
        end_row();
        label_cells(_("Comment"), $myrow['comment'], "class='tableheader2'");
        end_row();

        if($myrow['inactive'] == 0){
            $inactive = "NO";
        }else{
            $inactive = "YES";
        }

        label_cells(_("On Hold"), $inactive, "class='tableheader2'");
        end_row();

        if($myrow['expired'] == 0){
            $expired = "NO";
        }else{
            $expired = "YES";
        }

        label_cells(_("Expired"), $expired, "class='tableheader2'");
        end_row();

}


function display_isured_item_renewals($insured_item_id)
{ 
        $result = view_insured_item_renewals($insured_item_id);

        if (db_num_rows($result) == 0)
        {
            display_note(_("There are no renewals for this item."));
        }
        else
        {
            start_table(TABLESTYLE, "width=60%");

            br();
            display_heading("Renewal History");

            $th = array(_("Id"), _("Renewed On"),_("Next Renewal Date"),  
                         _("Amount"), _("Reference"), _("Comments"));


            table_header($th);

            $k = $j = 0; //row colour counter

            while ($myrow = db_fetch($result))
            {
                alt_table_row_color($k);

                label_cell($myrow["id"], "align=center");
                label_cell(sql2date($myrow["renewed_on"]), "align=center");
                label_cell(sql2date($myrow["next_renewal_date"]), "align=center");

                label_cell($myrow['amount'], "align=right");
                label_cell($myrow['reference']); 
                label_cell($myrow["comment"]); 

                end_row();

                $j++;

            } //end of while loop

            end_table();
        }

        div_end();
}


display_isured_item($insured_item_id);

display_isured_item_renewals($insured_item_id);

end_page(true, false, false, ST_INSURED_ITEMS, $insured_item_id);


?>
