<?php

function add_month_reporting_adjustment($year_id, $month_id, $report_adjust_type_id, $date, $amount, $comment) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
        
	$sql = "INSERT INTO ".TB_PREF."month_reporting_adjustments (year_id, month_id, report_adjust_type_id, date, amount, comment, created_by, created_on)
		VALUES (".db_escape($year_id).", ".db_escape($month_id).", ".db_escape($report_adjust_type_id).", 
                        ".db_escape(date2sql($date)).", ".db_escape($amount).", ".db_escape($comment).",
                        ".db_escape($created_by).", ".db_escape($created_on).")";

            db_query($sql, 'add_month_reporting_adjustment');
            
                return db_insert_id();
}

function update_month_reporting_adjustment($id, $date, $amount, $comment) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."month_reporting_adjustments 
                SET date = ".db_escape(date2sql($date)) .", amount = ".db_escape($amount).", comment = ".db_escape($comment).", 
                    updated_by = ".db_escape($updated_by).", updated_on = ".db_escape($updated_on)." 
                WHERE id = ".db_escape($id);

            db_query($sql, "update_month_reporting_adjustment");
            
                return db_num_affected_rows();
}

function get_month_reporting_adjustments_details($year_id, $month_id, $report_adjust_type_id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."month_reporting_adjustments
            WHERE year_id = ".db_escape($year_id)." 
                AND month_id = ".db_escape($month_id)."
                AND report_adjust_type_id = ".db_escape($report_adjust_type_id);

	return db_query($sql, "get_month_reporting_adjustments_details");
}

function get_month_reporting_adjustment_record_by_id($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."month_reporting_adjustments  
            WHERE id = ".db_escape($id);

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

function delete_month_reporting_adjustment_record_by_id($id) {
    $sql = "DELETE 
            FROM ".TB_PREF."month_reporting_adjustments  
            WHERE id = ".db_escape($id);

	db_query($sql, "delete_month_reporting_adjustment_record_by_id");
        
            return db_num_affected_rows();
}

function get_month_reporting_adjustments_type_summary($adjustment_type, $from_date, $to_date) {
    $sql = "SELECT SUM(mra.amount) total_cost 
            FROM ".TB_PREF."month_reporting_adjustments mra     
                JOIN ".TB_PREF."list_values tn      
                    ON tn.id = mra.report_adjust_type_id 
            WHERE tn.value_code = ".db_escape($adjustment_type)." 
                AND mra.date >= ".db_escape($from_date)." 
                AND mra.date <= ".db_escape($to_date);
    
        $result = db_query($sql, "Could not get_month_reporting_adjustments_summary");
            $row = db_fetch($result);
                if (isset($row['total_cost']) && $row['total_cost'] != 0) {
                    return $row['total_cost'];
                } else {
                    return 0;
                }
}
function view_month_reporting_adjustment($id) {
    
    $sql = "SELECT a.*,
                    an.value_name adjustment_type_name 
            FROM ".TB_PREF."month_reporting_adjustments a 
                JOIN ".TB_PREF."list_values an 
                    ON an.id = a.report_adjust_type_id  
            WHERE a.id = ".db_escape($id);
    $result = db_query($sql, "view_month_reporting_adjustment");
    
    return db_fetch($result);
}