<?php


function add_fixed_deposit($name, $description, $bank_id, $branch_id, $bank_account_id, 
                            $amount, $start_date, $end_date, $interest, 
                            $security_type_id, $security_id, $lien_amount,
                            $matured, $original_fixed_deposit_id = 0 )                 
               
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');        

        $sql = "INSERT INTO ".TB_PREF."fixed_deposits
                      ( name, description,                       
                        bank_id, branch_id, 
                        bank_account_id, amount, 
                        start_date, end_date, 
                        interest, security_type_id, 
                        security_id, lien_amount, 
                        matured, original_fixed_deposit_id, 
                        created_by, created_on ) 
                        
                VALUES (".db_escape($name).",  ".db_escape($description).","
                        .db_escape($bank_id).",  ".db_escape($branch_id).","
                        .db_escape($bank_account_id).",  ".db_escape($amount).","                       
                        .db_escape(date2sql($start_date)).",  ".db_escape(date2sql($end_date)).","                        
                        .db_escape($interest).", ".db_escape($security_type_id).", "
                        .db_escape($security_id).", ".db_escape($lien_amount).", "
                        .db_escape($matured).", ".db_escape($original_fixed_deposit_id).", "
                        .db_escape($id_user).",  ".db_escape($datetime).")";     
        
    db_query($sql, "could not add fix deposit.");
         //return ();
    $n = db_insert_id();
    if($n > 0){
        return array("success" => true, "trans_no" => $n, "message" => "New fix deposit $n has been created.");
    }
    else{
        return array("success" => false, "message" => "Could not create fix deposit.");
    }    
    
}


function update_fixed_deposit($selected_id, $name, $description, $bank_id, $branch_id, $bank_account_id, 
                            $amount, $start_date, $end_date, $interest,
                            $security_type_id, $security_id, $lien_amount, $matured)
                    
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."fixed_deposits 
                             SET name=".db_escape($name).
                              ", description=".db_escape($description).                                   
                              ", bank_id=".db_escape($bank_id).  
                              ", branch_id=".db_escape($branch_id). 
                              ", bank_account_id=".db_escape($bank_account_id).
                              ", amount=".db_escape($amount).
                              ", start_date=".db_escape(date2sql($start_date)).
                              ", end_date=".db_escape(date2sql($end_date)).
                              ", interest=".db_escape($interest).
                              ", security_type_id=".db_escape($security_type_id). 
                              ", security_id=".db_escape($security_id).
                              ", lien_amount=".db_escape($lien_amount).
                              ", matured=".db_escape($matured).
                              ", updated_by=".db_escape($id_user).
                              ", updated_on=".db_escape($datetime)."                
                                    
			WHERE id=".db_escape($selected_id); 
                   
                 
    db_query($sql, "could not update fix deposit");
    
    $n = db_num_affected_rows();
    if(!($n == 1)){
        return array("success" => false, "message" => "Another user has updated this Fixed Deposit.");
    }
    else{
        return array("success" => true, "message" => "Fixed Deposit $selected_id has been updated.");
    }
        
    
//    if(!$expired){
//        return array("success" => true, "trans_no" => $selected_id, "message" => "Bond $selected_id has been updated.");
//    }
//    else{
//        return expire_bond($selected_id);
//    }
}


function get_fixed_deposit($selected_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."fixed_deposits
            WHERE id=".db_escape($selected_id);
    
    $result = db_query($sql);
    return db_fetch($result);
}

//function get_bonds_for_update($selected_id)
//{
//	$sql="SELECT * FROM ".TB_PREF."bonds WHERE id=".db_escape($selected_id);
//
//	$result = db_query($sql, "could not retrive Bonds");
//        return db_fetch($result);
//}

//function delete_bonds($selected_id)
//{
//	$sql="DELETE FROM ".TB_PREF."bonds WHERE id=".db_escape($selected_id);
//
//	db_query($sql, "Selected bond could not delete.");
//}

//function get_bonds()
//{
//    $sql = "SELECT bo.*, 
//                    dm.name guarantor,
//                    lv.value_name,
//                    pm.description project_description                   
//        
//            FROM ".TB_PREF."bonds AS bo
//                LEFT JOIN ".TB_PREF."debtors_master AS dm ON
//                        bo.guarantor = dm.debtor_no
//                LEFT JOIN ".TB_PREF."projects_master AS pm ON
//                        bo.project_id = pm.project_id
//                LEFT JOIN ".TB_PREF."list_values AS lv ON
//                        bo.type_id = lv.id 
//                      
//            ORDER BY bo.id"; 
//    
//    return db_query($sql, "Could not get bonds");    
//    
//}

//function get_bond($selected_id)
//{
//    $sql = "SELECT b.*,                         
//                    tn.value_name bond_type_name,                        
//                    sup.supp_name supplier_name,
//                    dm.name customer_name,                        
//                    cf.name credit_facility_name,
//                    bnk.value_name bank_name,
//                    br.branch_name,
//                    ba.bank_account_number 
//            FROM ".TB_PREF."bonds b 
//                    LEFT JOIN ".TB_PREF."list_values tn ON
//                            b.type_id = tn.id 
//                    LEFT JOIN ".TB_PREF."debtors_master dm ON 
//                            b.customer_id = dm.debtor_no 
//                    LEFT JOIN ".TB_PREF."suppliers sup ON
//                            b.supplier_id = sup.supplier_id 
//                    LEFT JOIN ".TB_PREF."credit_facilities cf ON
//                            b.credit_facility_id = cf.id 
//                    LEFT JOIN ".TB_PREF."list_values bnk ON
//                            b.bank_id = bnk.id 
//                    LEFT JOIN ".TB_PREF."bank_branches br ON
//                            b.branch_id = br.id 
//                    LEFT JOIN ".TB_PREF."bank_accounts ba ON
//                            b.bank_account_id = ba.id  
//            WHERE b.id=".db_escape($selected_id);
//            
//    $result = db_query($sql, 'Could not get_bond');
//    return db_fetch($result);
//    
//}


//function get_credit_facilities_for_bonds($selected_id)
//{
//    $sql = "SELECT cf.*,
//                    lv.value_name bank_name,
//                    bb.branch_name,
//                    ba.bank_account_name
//
//            FROM ".TB_PREF."credit_facilities cf,
//                    ".TB_PREF."list_values lv,
//                    ".TB_PREF."bank_branches bb,
//                    ".TB_PREF."bank_accounts ba
//
//            WHERE lv.id = cf.bank_id AND
//                    bb.id = cf.branch_id AND
//                    ba.id = cf.bank_account_id AND
//                    cf.id=".db_escape($selected_id);
//
//        $result = db_query($sql, 'Could not get_credit_facilities_for_bonds');
//        return db_fetch($result);    
//}

function mature_fixed_deposit($fixed_deposit_id)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."fixed_deposits 
                SET matured= 1, 
                    updated_by=".db_escape($id_user).", 
                    updated_on=".db_escape($datetime)."                
                                    
		WHERE  id=".db_escape($fixed_deposit_id)." AND matured = 0";
                             
	db_query($sql, "Could not mature fix deposit");
    
    $n = db_num_affected_rows();
    if(!($n == 1)){
        return array("success" => false, "message" => "Could not make fix deposit $fixed_deposit_id matured.");
    }
    
    return array("success" => true, "trans_no" => $fixed_deposit_id, "message" => "Fix deposit $fixed_deposit_id has been made matured.");
}


function extend_fixed_deposit($fixed_deposit_id, $extend_name, $extend_description, $extend_bank_id, $extend_branch_id, $extend_bank_account_id, 
                                $extend_amount, $extend_start_date, $extend_end_date, $extend_interest, 
                                $extend_security_type_id, $extend_security_id, $extend_lien_amount)
{
    $msg = mature_fixed_deposit($fixed_deposit_id);
    if($msg['success'] !== true){
        return $msg;
    }

    //$old_fixed_deposit = get_fixed_deposit($fixed_deposit_id);
    add_fixed_deposit($extend_name, $extend_description, $extend_bank_id, $extend_branch_id, $extend_bank_account_id, 
                        $extend_amount, $extend_start_date, $extend_end_date, $extend_interest, 
                        $extend_security_type_id, $extend_security_id, $extend_lien_amount, 
                        0, $fixed_deposit_id);
            
    $new_fixed_deposit_id  = db_insert_id();
    if(!($new_fixed_deposit_id > 0)){
        return array("success" => false, "message" => "Could not create a new Fixed Deposit for matured Fixed Deposit $fixed_deposit_id.");
    }
    
    return array("success" => true, "trans_no" => $new_fixed_deposit_id, "message" => "Fix deposit $fixed_deposit_id has been made matured and new fix deposit $new_fixed_deposit_id created.");
    
}

function get_matured_fixed_deposit($selected_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."fixed_deposits
            WHERE original_fixed_deposit_id=".db_escape($selected_id);
    
    $result = db_query($sql);            
    return db_fetch($result);    
}


function view_fixed_deposit($fixed_deposit_id)
{
    $sql = "SELECT fd.name,
                        fd.*,                        
                        lv.value_name bank_name,
                        bb.branch_name,
                        ba.bank_account_name 
        
            FROM ".TB_PREF."fixed_deposits fd 
                    LEFT JOIN ".TB_PREF."list_values lv ON
                        lv.id = fd.bank_id
                    LEFT JOIN ".TB_PREF."bank_branches bb ON
                        bb.id = fd.branch_id
                    LEFT JOIN ".TB_PREF."bank_accounts ba ON
                        ba.id = fd.bank_account_id 
              
            WHERE fd.id=".db_escape($fixed_deposit_id); 
              
    $result = db_query($sql);
    return db_fetch($result);    
    
}



?>
