<?php

function cs_get_suspened_tax_entries($trans_type)
{

    switch ($trans_type) {
        case ST_SUPPINVOICE:
        case ST_SUPPCREDIT:
            return cs_get_suspened_trans_tax_entries($trans_type); 
        case ST_SUBCONTRACTOR_INVOICE:
            return cs_get_suspened_sub_invoice_tax_entries($trans_type); 
        case ST_SUBCONTRACTOR_CREDIT_NOTE:
            return cs_get_suspened_sub_cn_tax_entries($trans_type); 
        case ST_UTILITY_BILLS:
            return cs_get_suspened_utility_bill_tax_entries($trans_type); 
        case ST_UTILITY_BILL_CREDIT_NOTE:
            return cs_get_suspened_utility_cn_tax_entries($trans_type); 

        default:
            display_error("Unknown transaction type ".$trans_type);
    }
    
}

function cs_get_suspened_utility_cn_tax_entries($trans_type)
{
    $sql = "SELECT ttd.id,
                    ".ST_UTILITY_BILL_CREDIT_NOTE." trans_type,
                    ttd.ubcn_id trans_no,
                    ttd.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    tt.name tax_type_name 
                    
            FROM ".TB_PREF."utility_bill_credit_note_taxes ttd 
                JOIN ".TB_PREF."tax_types tt 
                    ON ttd.tax_type_id = tt.id 
                JOIN ".TB_PREF."utility_bill_credit_notes h  
                    ON h.id = ttd.ubcn_id 
                    AND h.approval_status = ".AS_APPROVED."
            WHERE tt.suspended = 1 "; 

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

function cs_get_suspened_utility_bill_tax_entries($trans_type)
{
    $sql = "SELECT ttd.id,
                    ".ST_UTILITY_BILLS." trans_type,
                    ttd.ub_id trans_no,
                    ttd.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    tt.name tax_type_name 
                    
            FROM ".TB_PREF."utility_bill_taxes ttd 
                JOIN ".TB_PREF."tax_types tt 
                    ON ttd.tax_type_id = tt.id 
                JOIN ".TB_PREF."utility_bills h  
                    ON h.id = ttd.ub_id 
                    AND h.approval_status = ".AS_APPROVED."
            WHERE tt.suspended = 1 "; 

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

function cs_get_suspened_sub_cn_tax_entries($trans_type)
{
    $sql = "SELECT ttd.id,
                    ".ST_SUBCONTRACTOR_CREDIT_NOTE." trans_type,
                    ttd.order_no trans_no,
                    ttd.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    tt.name tax_type_name 
                    
            FROM ".TB_PREF."subcontractor_credit_note_taxes ttd 
                JOIN ".TB_PREF."tax_types tt 
                    ON ttd.tax_type_id = tt.id 
                JOIN ".TB_PREF."subcontractor_credit_notes h  
                    ON h.order_no = ttd.order_no 
                    AND h.approval_status = ".AS_APPROVED."
            WHERE  tt.suspended = 1 "; 

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

function cs_get_suspened_sub_invoice_tax_entries($trans_type)
{
    $sql = "SELECT ttd.id,
                    ".ST_SUBCONTRACTOR_INVOICE." trans_type,
                    ttd.order_no trans_no,
                    ttd.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    tt.name tax_type_name 
                    
            FROM ".TB_PREF."subcontractor_invoice_taxes ttd 
                JOIN ".TB_PREF."tax_types tt 
                    ON ttd.tax_type_id = tt.id 
                JOIN ".TB_PREF."subcontractor_invoices h  
                    ON h.order_no = ttd.order_no 
                    AND h.approval_status = ".AS_APPROVED."
            WHERE  tt.suspended = 1 "; 

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

function cs_get_suspened_trans_tax_entries($trans_type)
{
    $sql = "SELECT ttd.id,
                    ttd.trans_type,
                    ttd.trans_no,
                    ttd.amount, 
                    tt.sales_gl_code,               
                    tt.purchasing_gl_code,
                    tt.name tax_type_name 
                    
            FROM ".TB_PREF."trans_tax_details ttd 
                JOIN ".TB_PREF."tax_types tt 
                    ON ttd.tax_type_id = tt.id 
            WHERE ttd.trans_type = ".db_escape($trans_type)."  
                    AND tt.suspended = 1 "; 
    
//    $sql .= " LIMIT 10";

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

function cs_get_fi_transaction($trans_type, $trans_no)
{
    $sql = "SELECT fi.*, gl.type_no gl_batch_no   
            FROM ".TB_PREF."fi_trans fi    
                LEFT JOIN ".TB_PREF."gl_trans gl   
                    ON gl.counter = fi.gl_counter 
            WHERE fi.type = ".db_escape($trans_type)." 
                AND fi.type_no = ".db_escape($trans_no)."  
            LIMIT 1 ";
    
    $result = db_query($sql, "cs_get_fi_transaction");
    return db_fetch($result);
    
}

function cs_get_suspened_tax_account_fi_entry($trans_type, $trans_no, $taxitem_id, $account_code)
{
    $sql = "SELECT *  
            FROM ".TB_PREF."fi_trans    
            WHERE type = ".db_escape($trans_type)." 
                AND type_no = ".db_escape($trans_no)." 
                AND detail_line_id = ".db_escape($taxitem_id)." 
                AND account = ".db_escape($account_code)." 
            ORDER BY tran_date 
            LIMIT 1 ";
    
    $result = db_query($sql, "cs_get_suspened_tax_account_fi_entry");
    return db_fetch($result);
    
}


function cs_create_stax_fi_entries()
{
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_create_supplier_payment_gl_entries", $message);
    
    $trans_type = ST_UTILITY_BILL_CREDIT_NOTE;
    
    $tax_entry_result = cs_get_suspened_tax_entries($trans_type);
    $recs = db_num_rows($tax_entry_result);
    write_custom_script_log("cs_create_stax_fi_entries", $recs." Records selected");
    
    while($taxitem = db_fetch($tax_entry_result)){    
        $total = 0;
        
        $trans_type = $taxitem['trans_type'];
        $trans_no = $taxitem['trans_no']; 
        
        $fi_transaction = cs_get_fi_transaction($trans_type,$trans_no );
        if(!(isset($fi_transaction['counter']) && $fi_transaction['counter'] > 0)){ //FI not available
            continue;
        }
         
        $taxitem_id  = $taxitem['id'];
        $tax_type_name  = $taxitem['id'];
        
        $purchasing_gl_amount = $sales_gl_amount = 8888888888;
        
        $purchasing_gl_code = $taxitem['purchasing_gl_code'];  
        $sales_gl_code = $taxitem['sales_gl_code']; 
        
        switch ($trans_type) {
            case ST_SUPPINVOICE:                
            case ST_SUBCONTRACTOR_INVOICE:                
            case ST_UTILITY_BILLS:                
                $purchasing_gl_amount = $taxitem['amount'];
                break;
            case ST_SUPPCREDIT: 
            case ST_SUBCONTRACTOR_CREDIT_NOTE: 
            case ST_SUBCONTRACTOR_INVOICE: 
                $purchasing_gl_amount = -$taxitem['amount'];  
                break;
            default:
                break;
        }
        
        
                $sales_gl_amount = -$purchasing_gl_amount;
         
        
        begin_global_transaction();
        
        $fi_sus_entry = cs_get_suspened_tax_account_fi_entry($trans_type, $trans_no, $taxitem_id, $purchasing_gl_code);
        if(!(isset($fi_sus_entry['counter']) && $fi_sus_entry['counter'] > 0)){ //Suspened tax FI entry not available
            $total = cs_create_stax_fi_entry($taxitem, $fi_transaction, $purchasing_gl_code, $purchasing_gl_amount);
        }else{
            $total = $fi_sus_entry['amount'];
        }
        
        $fi_sus_entry = cs_get_suspened_tax_account_fi_entry($trans_type, $trans_no, $taxitem_id, $sales_gl_code);
        if(!(isset($fi_sus_entry['counter']) && $fi_sus_entry['counter'] > 0)){ //Suspened tax FI entry not available
            $total += cs_create_stax_fi_entry($taxitem, $fi_transaction, $sales_gl_code, $sales_gl_amount);
        }else{
            $total += $fi_sus_entry['amount'];
        }
        
        if($total != 0){
            cancel_transaction();
            $message = "Type : ".$trans_type." #".$trans_no." : ".$tax_type_name." non-zero total ".$total;
            write_custom_script_log("cs_create_supplier_payment_gl_entries", $message);
            return;
        }else{
            commit_transaction();
            $message = "Type : ".$trans_type.", Trans Number : ".$trans_no." completed";
            write_custom_script_log("cs_create_supplier_payment_gl_entries", $message);
        }
        
        
   
    }
    
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_create_supplier_payment_gl_entries", $message);
}

function cs_create_stax_fi_entry($taxitem, $fi_transaction, $account_code, $amount)
{
//    return $amount;
            
    $trans_type = $taxitem['trans_type'];
    $trans_no = $taxitem['trans_no']; 
    $batch_no = $fi_transaction['batch_no'];
    $saved_amount = 0;
    
    if($taxitem['amount'] != 0 && $taxitem['suspended'] = 1){
        $trans_no = $taxitem['trans_no'];
        $taxitem_id = $taxitem['id']; 
        $date_  = sql2date($fi_transaction['tran_date']);
        
        if($fi_transaction['person_type_id'] != PT_SUPPLIER){
            $saved_amount = -9999999999;
        }else{        
            $supplier_id = $fi_transaction['person_id']; 
            $gl_batch_no = 0;
            if(isset($fi_transaction['gl_batch_no']) && $fi_transaction['gl_batch_no'] > 0){
                $gl_batch_no = $fi_transaction['gl_batch_no']; 
            }
            $supplier = get_supplier($supplier_id);
            $currency_code = $supplier['curr_code'];          

            $saved_amount = add_fi_trans_supplier($batch_no, $trans_type, $trans_no, $taxitem_id, $date_, $account_code, 0, $gl_batch_no, 
                                        $amount, $supplier_id, $currency_code);
        }


    }        

    return $saved_amount;
}