<?php


function create_next_gl_batch_no($gl_batch_type, $tran_date_)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."gl_batches(
                        gl_batch_type, tran_date, 
                        created_by, created_on)

            VALUES (".db_escape($gl_batch_type).", ".db_escape(date2sql($tran_date_)).", "
                    .db_escape($id_user).", ".db_escape($datetime).") ";

    db_query($sql, "create_next_gl_batch_no");

    return db_insert_id();
}


function update_fi_trans_gl_counter($ids, $account, $project_id, $gl_counter, $gl_batch_no)
{
    $sql = "UPDATE ".TB_PREF."fi_trans 
            SET gl_counter = ".db_escape($gl_counter).", 
                gl_batch_no = ".db_escape($gl_batch_no)." 
            WHERE batch_no IN (".implode(",", $ids).")  
                AND account = ".db_escape($account)." 
                AND dimension_id = ".db_escape($project_id);
    
    db_query($sql, "update_fi_trans_gl_counter");
}

function update_fi_trans_record_gl_counter($fi_counter, $gl_counter, $gl_batch_no)
{
    $sql = "UPDATE ".TB_PREF."fi_trans 
            SET gl_counter = ".db_escape($gl_counter).", 
                gl_batch_no = ".db_escape($gl_batch_no)." 
            WHERE counter = ".db_escape($fi_counter);
    
    db_query($sql, "update_fi_trans_record_gl_counter");
}


function update_fi_batch_posted($fi_batch_no, $row_state, $gl_batch_no)
{
    $sql = "UPDATE ".TB_PREF."fi_batches 
            SET gl_batch_no = ".db_escape($gl_batch_no).", 
                status = ". db_escape(AS_POSTED).", 
                row_state = ". db_new_row_state($row_state)." 
            WHERE id IN (".$fi_batch_no.")  
                AND row_state = ".db_escape($row_state);
    
    db_query($sql, "update_fi_batch_posted");
    
    return db_num_affected_rows();
}


function update_gl_batch_header_deleted($gl_batch_no)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."gl_batches 
            SET deleted_by = ".db_escape($id_user).", 
                deleted_on = ". db_escape($datetime)." 
            WHERE id = ".db_escape($gl_batch_no)." 
                AND deleted_by = 0";

    db_query($sql, "update_gl_batch_header_deleted");

    return db_num_affected_rows();
}

function get_gl_batch_balance($gl_batch_no)
{

    $sql = "SELECT SUM(t.amount) gl_batch_balance   
            FROM ".TB_PREF."gl_batches b   
                JOIN ".TB_PREF."gl_trans t  
                    ON t.type = b.gl_batch_type 
                    AND t.type_no = b.id 
            WHERE b.id = ".db_escape($gl_batch_no);

    $result = db_query($sql, "get_gl_batch_balance");

    return db_result_value($result, "gl_batch_balance");
    
}


function get_gl_batch_unposted_fi_transactions_count($ids)
{
    $sql = "SELECT COUNT(t.counter) recs    
            FROM ".TB_PREF."fi_trans t   
            WHERE t.batch_no IN (".implode(",", $ids).") 
                AND t.gl_counter = 0 ";
    
    $result = db_query($sql, "get_gl_batch_unposted_fi_transactions_count");

    return db_result_value($result, "recs");
    
}

function get_gl_transaction_balance($type, $type_no)
{

    $sql = "SELECT SUM(gl.amount) gl_trans_balance   
            FROM ".TB_PREF."gl_trans gl   
            WHERE gl.type = ".db_escape($type)." 
                AND gl.type_no = ".db_escape($type_no); 

    $result = db_query($sql, "get_gl_transaction_balance");

    return db_result_value($result, "gl_trans_balance");
    
}