<?php


function get_monthly_summary_company_iou_paymets($month_begin_date, $month_end_date)
{

    $sql=" SELECT SUM(amount) total 
            FROM ".TB_PREF."i_owe_you_payments  
            WHERE paid = 1  
                AND is_return = 0 
                AND payment_date >= ".db_escape($month_begin_date)." 
                AND payment_date <= ".db_escape($month_end_date);

    $result = db_query($sql,"get_monthly_summary_company_iou_paymet_returns");
    return db_result_value($result, "total");

}


function get_monthly_summary_company_bank_payments($month_begin_date, $month_end_date)
{

    $sql=" SELECT SUM(amount) total 
            FROM ".TB_PREF."bank_trans  
            WHERE type = ".db_escape(ST_BANKPAYMENT)."   
                AND trans_date >= ".db_escape($month_begin_date)." 
                AND trans_date <= ".db_escape($month_end_date);

    $result = db_query($sql,"get_monthly_summary_company_bank_deposits");
    return db_result_value($result, "total");

}


function get_monthly_summary_company_supplier_payments($month_begin_date, $month_end_date)
{

    $sql=" SELECT SUM(ov_amount - ov_discount) total 
            FROM ".TB_PREF."supp_trans  
            WHERE type = ".db_escape(ST_SUPPAYMENT)."   
                AND tran_date >= ".db_escape($month_begin_date)." 
                AND tran_date <= ".db_escape($month_end_date);

    $result = db_query($sql,"get_monthly_summary_company_supplier_payments");
    return db_result_value($result, "total");

}



function get_monthly_summary_company_customer_payment_receipts($month_begin_date, $month_end_date)
{

    $sql=" SELECT SUM(ov_amount + ov_gst + ov_freight + ov_freight_tax - ov_discount) total 
            FROM ".TB_PREF."debtor_trans  
            WHERE type = ".db_escape(ST_CUSTPAYMENT)."   
                AND tran_date >= ".db_escape($month_begin_date)." 
                AND tran_date <= ".db_escape($month_end_date);

    $result = db_query($sql,"get_monthly_summary_department_invoices");
    return db_result_value($result, "total");

}

function get_monthly_summary_company_bank_deposits($month_begin_date, $month_end_date)
{

    $sql=" SELECT SUM(amount) total 
            FROM ".TB_PREF."bank_trans  
            WHERE type = ".db_escape(ST_BANKDEPOSIT)."   
                AND trans_date >= ".db_escape($month_begin_date)." 
                AND trans_date <= ".db_escape($month_end_date);

    $result = db_query($sql,"get_monthly_summary_company_bank_deposits");
    return db_result_value($result, "total");

}

function get_monthly_summary_company_iou_paymet_returns($month_begin_date, $month_end_date)
{

    $sql=" SELECT SUM(amount) total 
            FROM ".TB_PREF."i_owe_you_payments  
            WHERE paid = 1    
                AND is_return = 1 
                AND payment_date >= ".db_escape($month_begin_date)." 
                AND payment_date <= ".db_escape($month_end_date);

    $result = db_query($sql,"get_monthly_summary_company_iou_paymet_returns");
    return db_result_value($result, "total");

}


function add_to_monthly_summary_company($month_id,  
                                        $value_type, $calculation_type, 
                                        $value, 
                                        $calculation_run_id)
{
    if($value == 0){
        return;
    }
	$sql = "INSERT INTO ".TB_PREF."monthly_summary_company 
                            ( month_id, 
                              value_type, calculation_type, 
                              value,  
                              calculation_run_id ) 
                     
		VALUES( ".db_escape($month_id).", 
                        ".db_escape($value_type).",".db_escape($calculation_type).",
                        ".db_escape($value).", 
                        ".db_escape($calculation_run_id).")";

	db_query($sql,"add_to_monthly_summary_company");
}


function delete_monthly_summary_company($month_id)
{
    $sql = "DELETE FROM ".TB_PREF."monthly_summary_company   
            WHERE month_id = ".db_escape($month_id)."";

    db_query($sql,"delete_monthly_summary_company");
}

