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

function add_loan($employee_code, $loan_description, 
                $loan_amount, $installment_amount,
                $interest, $start_date, 
                $inactive, $completed)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        
	$sql = "INSERT INTO ".TB_PREF."loans(
                        employee_code, loan_description,
                        loan_amount, installment_amount, 
                        interest, start_date, 
                        inactive, completed,
                        created_by, created_on) 
                VALUES ("
                        .db_escape($employee_code).", ".db_escape($loan_description). ", "
                        .db_escape($installment_amount). ", ".db_escape($loan_amount).", "
                        .db_escape($interest). ", ".db_escape($start_date).", "
                        .db_escape($inactive).", ".db_escape($completed).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

	db_query($sql,"New loan could not be added");
}

function update_loan_definition($selected_id, $employee_code, $loan_description, 
                                $loan_amount, $installment_amount,
                                $interest, $start_date, 
                                $inactive, $completed)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."loans 
                SET 
                    loan_description=".db_escape($loan_description).",
                    loan_amount=".$loan_amount.",
                    installment_amount=".db_escape($installment_amount).",
                    interest=".db_escape($interest).",
                    start_date =".db_escape($start_date).",
                    inactive=".db_escape($inactive).",
                    completed=".db_escape($completed).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)." 
		WHERE id=".db_escape($selected_id)." AND 
                    employee_code = ".  db_escape($employee_code);
        
	db_query($sql,"Loan details could not be updated");
}

function delete_loan($selected_id)
{
	return;
        $sql = "DELETE FROM ".TB_PREF."leave_entitlements WHERE id=".db_escape($selected_id);
        
	db_query($sql,"could not leave entitlement");
}

function get_loan_details($selected_id)
{
   $sql = "SELECT * FROM ".TB_PREF."loans WHERE id  = ".$selected_id;
                    
    $result =  db_query($sql, "Loan details could not be retrieved");
    
    return db_fetch($result);
}

function get_active_loans($employee_code)
{
    $sql = "SELECT l.*, vli.total_paid, vli.total_interest  
            FROM ".TB_PREF."loans l 
                LEFT JOIN ".TB_PREF."vw_loan_installments vli ON   
                            l.id = vli.loan_id 
            WHERE l.employee_code = ".db_escape($employee_code);

    $sql .= " ORDER By start_date" ;
                    
    return db_query($sql, "Loans could not be retrieved");
}

?>