<?php

function get_sql_for_insured_item_inquiry($insured_item_id =null,$insured_item_name =null,
                                    $from = null, $to = null)
{
        
        $sql = "SELECT ii.* 
        
                FROM ".TB_PREF."insured_items ii 
                WHERE 1 ";
                        

        if($from != null)
        {
            $sql .= " AND ii.start_date >= ".db_escape(date2sql($from)); 
        }
        
        if($to != null)
        {
            $sql .= " AND ii.start_date <= ".db_escape(date2sql($to)); 
        }
        
        if($insured_item_id > 0)
        {
            $sql .= " AND ii.id = ".db_escape($insured_item_id);            
        }
        
        
        if(strlen($insured_item_name) > 0)
        {
            $sql .= " AND ii.name LIKE ".db_escape('%'.$insured_item_name.'%');            
        }

        $sql .= " ORDER BY ii.id ";   
	
        return $sql;
}

function view_insured_items($insured_item_id)
{
    $sql = "SELECT it.*,
                    dm.name customer_name,
                    s.supp_name supplier_name                                 

            FROM ".TB_PREF."insured_items it
                LEFT JOIN ".TB_PREF."debtors_master dm ON
                   it.insurer_id = dm.debtor_no
                LEFT JOIN ".TB_PREF."suppliers s ON
                   it.insurer_id = s.supplier_id               
              
            WHERE it.id=".db_escape($insured_item_id);
    
    $result = db_query($sql);
    return db_fetch($result);
           
}


function view_insured_item_renewals($insured_item_id){
    
    $sql = "SELECT it.*                  
            FROM ".TB_PREF."insured_item_renewals it 
            WHERE it.insured_item_id=".db_escape($insured_item_id)." 
            ORDER BY it.renewed_on ";
    
    return db_query($sql);

    
}
?>
