<?php

function get_month_loans_total($month_id)
{
    $sql = "SELECT SUM(msl.loan_balance) loan_balance 

            FROM ".TB_PREF."monthly_summary_loans msl  
               
            WHERE msl.month_id = ".db_escape($month_id);

    $result = db_query($sql);
    return db_result_value($result, "loan_balance"); 
}

function get_month_loan_repayments_summary($from_month_display_order, $to_month_display_order)
{
    $sql="SELECT SUM(s.capital_paid) capital_paid,
                SUM(s.interest_paid) interest_paid, 
                SUM(s.late_interest_paid) late_interest_paid 
            FROM ".TB_PREF."monthly_summary_loans s  
                JOIN ".TB_PREF."months m 
                    ON s.month_id = m.id                                
            WHERE m.display_order >= ". db_escape($from_month_display_order)."  
                AND m.display_order <= ". db_escape($to_month_display_order);

    $result = db_query($sql,"get_monthly_loan_payment_summary");
    
    return db_fetch($result);
}

function get_month_interest_total($month_id)
{
    $sql = "SELECT SUM(msl.interest_paid) interest_total 
		   
            FROM ".TB_PREF."monthly_summary_loans msl  
               
            WHERE msl.month_id = ".db_escape($month_id)."  
            GROUP BY msl.month_id";

    $result = db_query($sql);
    $row = db_fetch($result);
    
        if($row['interest_total'] == null || $row['interest_total'] == 0){
            return $row['interest_total'] = 0;
        }else{
            return $row['interest_total'];
        }
}

function get_month_late_interest_total($month_id)
{
    $sql = "SELECT SUM(msl.late_interest_paid) late_interest_total 
		   
            FROM ".TB_PREF."monthly_summary_loans msl  
               
            WHERE msl.month_id = ".db_escape($month_id)."  
            GROUP BY msl.month_id";

    $result = db_query($sql);
    $row = db_fetch($result);
    
        if($row['late_interest_total'] == null || $row['late_interest_total'] == 0){
            return $row['late_interest_total'] = 0;
        }else{
            return $row['late_interest_total'];
        }
}

function get_month_new_loans_total($month_id, $month_begin_date, $month_end_date)
{
    $sql = "SELECT SUM(cl.loan_amount + cl.interest) new_loans_total 
            FROM ".TB_PREF."company_loans cl  
               
            WHERE cl.start_date >= ".db_escape($month_begin_date)." 
                AND cl.start_date <= ".db_escape($month_end_date)." 
                AND cl.completed = 0 ";

    $result = db_query($sql);
    $row = db_fetch($result);
    
        if($row['new_loans_total'] == null || $row['new_loans_total'] == 0){
            return 0;
        }else{
            return $row['new_loans_total'];
        }
}

function get_month_new_lcs_total($month_id, $month_begin_date, $month_end_date)
{
    $sql = "SELECT SUM(cl.lc_amount + cl.bank_charge) new_lcs_total 
            FROM ".TB_PREF."letters_of_credit cl  
               
            WHERE cl.start_date >= ".db_escape($month_begin_date)." 
                AND cl.start_date <= ".db_escape($month_end_date)." 
                AND cl.expired = 0 ";

    $result = db_query($sql, "get_month_new_lcs_total");
    return db_result_value($result, "new_lcs_total");
    
}

function get_month_closed_loans_total($month_id, $month_begin_date, $month_end_date)
{
    $sql = "SELECT SUM(cl.loan_amount) closed_loans_total 
            FROM ".TB_PREF."company_loans cl  
               
            WHERE cl.completed_date >= ".db_escape($month_begin_date)." AND cl.completed_date <= ".db_escape($month_end_date);

    $result = db_query($sql);
    $row = db_fetch($result);
    
        if($row['closed_loans_total'] == null || $row['closed_loans_total'] == 0){
            return 0;
        }else{
            return $row['closed_loans_total'];
        }
}

function get_loan_capital_payments_total($month_id)
{
    $sql = "SELECT SUM(msl.capital_paid) capital_paid 
            FROM ".TB_PREF."monthly_summary_loans msl                 
            WHERE msl.month_id = ".db_escape($month_id);
    
    $result = db_query($sql);
    $row = db_fetch($result);
    
        if($row['capital_paid'] == null || $row['capital_paid'] == 0){
            return $row['capital_paid'] = 0;
        }else{
            return $row['capital_paid'];
        }
}
