<?php

function cs_get_bank_float_reimbursements()
{
    $sql = "SELECT fr.* 
            FROM ".TB_PREF."i_owe_you_float_reimbursements  fr 
            WHERE bank_account_id > 0 
            ORDER BY fr.id 
            LIMIT 200";
    
    return db_query($sql, "cs_get_float_reimbursements");
}

function cs_get_float_reimbursement_gl_bank_trans($type, $trans_id)
{
    $sql = "SELECT gl.* 
            FROM ".TB_PREF."gl_trans as gl 
            WHERE gl.type= ".db_escape($type)." 
                AND gl.type_no = ".db_escape($trans_id). " 
		AND gl.amount < 0";
    
    $result = db_query($sql, "cs_get_gl_trans");
    return db_fetch($result);
}

function cs_get_float_reimbursement_gl_trans_total($type, $trans_id)
{
    $sql = "SELECT SUM(gl.amount) gl_amount_sum  
            FROM ".TB_PREF."gl_trans as gl 
            WHERE gl.type= ".db_escape($type)." 
                AND gl.type_no = ".db_escape($trans_id);
    
    $result = db_query($sql, "cs_get_gl_trans");
    return db_result_value($result, "gl_amount_sum");
}


function cs_create_float_reimbursement_gl_entries()
{
    $script_name = "cs_create_float_reimbursement_gl_entries";
    
    $message = "==================Script Sarted==========================";
    write_custom_script_log($script_name, $message);
    
    $result = cs_get_bank_float_reimbursements();
    $recs = db_num_rows($result);
    write_custom_script_log($script_name, $recs." Records selected");
    
    while($row = db_fetch($result)){
        if($row['from_float_id'] > 0){
            write_custom_script_log($script_name, "# ".$row['trans_no']." reimbursed from float ".$row['from_float_id']);
            continue;
        }
        
            begin_global_transaction();
            $msg = cs_create_float_reimbursement_gl_entry($script_name, $row);
            if($msg['success'] !== true){
                cancel_transaction();
                $message = $msg['message'];
                write_custom_script_log($script_name, $message);
                return;
            }else{
//                cancel_transaction();
                commit_transaction();
                $message = "Trans Number : ".$row['id']." completed";
                write_custom_script_log($script_name, $message);
            }
        
    }
    
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log($script_name, $message);
}

function cs_create_float_reimbursement_gl_entry($script_name, $row){

    $type = ST_PETTY_CASH_FLOAT_REIMBUR;
//    get_supp_trans_record($trans_no);
//    $payment_id = write_supp_trans(ST_SUPPAYMENT, 0, $supplier_id, $date_, $date_,
//		$ref, "", -$supp_amount, 0, -$supp_discount, "", 0, 0, $po_no, $float_id);

    $type_no = $row['id'];
//    $trans_id = $row['trans_no'];
    $date_ = sql2date($row['tran_date']);
    $float_id = $row['float_id'];
    $amount = $row['amount'];
	// Now debit creditors account with payment + discount

//    return array("success"=>false, "message"=>"");
    
    $bank_trans = get_bank_transaction_record($type, $type_no);
    $bank_account_id = $bank_trans['bank_act'];
    
    $bank_account = get_bank_account($bank_account_id);
    $bank_gl_account = $bank_account['account_code'];
    
    $float = get_petty_cash_float_line($float_id, $type_no);
    $float_account = $float['account_code'];
    $gl_memo = "Reimbursing ".$float['name'];
    
    $gl_bank_trans = cs_get_float_reimbursement_gl_bank_trans($type, $type_no);
        if(!is_array($gl_bank_trans)){
            // Bank account GL entry
            add_gl_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $type_no, $date_, $bank_gl_account, 0, 0, $gl_memo,
		-$amount, null, PT_FLOAT, $float_id); 
        }
            
        $gl_trans_total = cs_get_float_reimbursement_gl_trans_total($type, $type_no);
        if(!(abs($gl_trans_total) == 0)){
            // Float GL entry
            add_gl_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $type_no, $date_, $float_account, 0, 0, $gl_memo,
		$amount, null, PT_FLOAT, $float_id); 
        }
            
        $gl_trans_total = cs_get_float_reimbursement_gl_trans_total($type, $type_no);
        if(!(abs($gl_trans_total) == 0)){
            $message = "Trans Number ".$type_no." : gl entry does not balance";
            write_custom_script_log($script_name, $message);
            return array("success"=>false, "message"=>$message);
        }
    
        $audit_trail_last = get_audit_trail_last(ST_PETTY_CASH_FLOAT_REIMBUR, $type_no);
        if(!is_array($audit_trail_last)){
            add_audit_trail(ST_PETTY_CASH_FLOAT_REIMBUR, $type_no, $date_);
        }
    
    return array("success"=>true, "message"=>"");
    
}

