<?php


function cs_get_fi_suspened_tax_entries()
{
    $sql = "SELECT fib.id gl_batch_no, fib.tran_date, fib.gl_batch_type,  
                SUM(fi.amount) total, fi.account, fi.dimension2_id 
            FROM ".TB_PREF."fi_trans fi 
                JOIN ".TB_PREF."gl_batches fib 
                    ON fib.id = fi.dimension2_id 
            WHERE fi.dimension2_id > 0 
                AND fi.gl_counter = 0 
            GROUP BY fi.dimension2_id, fi.account";                            

//    $sql .= " LIMIT 2 ";
    
    return db_query($sql, "cs_get_fi_suspened_tax_entries");
}

function cs_get_gl_transaction($trans_type,$trans_no, $account)
{
    $sql = "SELECT gl.*    
            FROM ".TB_PREF."gl_trans gl   
            WHERE gl.type = ".db_escape($trans_type)." 
                AND gl.type_no = ".db_escape($trans_no)."  
                AND gl.account = ".db_escape($account)."  
            LIMIT 1 ";
    
    $result = db_query($sql, "cs_get_gl_transaction");
    return db_fetch($result);
    
}

function cs_get_fi_record_count($gl_batch_no, $account_code)
{
    $sql = "SELECT count(counter) rec_count    
            FROM ".TB_PREF."fi_trans    
            WHERE dimension2_id = ".db_escape($gl_batch_no)."  
                AND account = ".db_escape($account_code);
    
    $result = db_query($sql, "cs_get_fi_record_count"); 

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

function cs_get_fi_batch_ids($gl_batch_no, $account_code)
{
    $sql = "SELECT DISTINCT batch_no   
            FROM ".TB_PREF."fi_trans    
            WHERE dimension2_id = ".db_escape($gl_batch_no)."  
                AND account = ".db_escape($account_code);
    
    $result = db_query($sql, "cs_get_fi_batch_ids"); 
    
    $ids = array();
    while ($row = db_fetch($result)) {
        $ids[] = $row['batch_no'];
    }
    
    return $ids;
}


function cs_post_stax_fi_entries()
{
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_post_stax_fi_entries", $message);

    $tax_entry_result = cs_get_fi_suspened_tax_entries();
    $recs = db_num_rows($tax_entry_result);
    write_custom_script_log("cs_post_stax_fi_entries", $recs." Records selected");
    
    while($taxitem = db_fetch($tax_entry_result)){    
        
        $gl_batch_type = $taxitem['gl_batch_type'];
        $gl_batch_no = $taxitem['gl_batch_no']; 
        $account = $taxitem['account']; 
        
        $gl_transaction = cs_get_gl_transaction($gl_batch_type, $gl_batch_no, $account);
        if(isset($gl_transaction['counter']) && $gl_transaction['counter'] > 0){ //gl entry available
            continue;
        }
         
        $rec_count = (int)cs_get_fi_record_count($gl_batch_no, $account);        
        $ids = cs_get_fi_batch_ids($gl_batch_no, $account);        
        
        $gl_tran_date = $taxitem['tran_date']; 
        $total = $taxitem['total']; 
        
        $tran_date_ = sql2date($gl_tran_date);        
        $memo_ = "";
        begin_global_transaction();
        
        add_gl_trans($gl_batch_type, $gl_batch_no, $tran_date_, $account, 0, 0, $memo_, $total);
        
        $gl_counter = db_insert_id();
        
        update_fi_trans_gl_counter($ids, $account, $gl_counter, $gl_batch_no);
        $n = db_num_affected_rows();
        
        if($n !== $rec_count){ 
            cancel_transaction();
            $message = "Type : ".$gl_batch_type." #".$gl_batch_no." : records ".$rec_count." updated ".$n;
            write_custom_script_log("cs_post_stax_fi_entries", $message);
            return;
        }else{
            commit_transaction();
            $message = "Type : ".$gl_batch_type.", Trans Number : ".$gl_batch_no." completed";
            write_custom_script_log("cs_post_stax_fi_entries", $message);
        }
        
    }
    
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_post_stax_fi_entries", $message);
}
