<?php

function cs_get_supplier_payments()
{
    $sql = "SELECT *  
            FROM ".TB_PREF."supp_trans  
            WHERE type = ". db_escape(ST_SUPPAYMENT)."
            ORDER BY tran_date 
            LIMIT 200";
    
    return db_query($sql, "cs_get_reimbursement_ids");
}


function cs_create_supplier_payment_gl_entries()
{
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_create_supplier_payment_gl_entries", $message);
    
    $supp_pay_result = cs_get_supplier_payments();
    $recs = db_num_rows($supp_pay_result);
    write_custom_script_log("cs_create_supplier_payment_gl_entries", $recs." Records selected");
    
    while($row = db_fetch($supp_pay_result)){
        
        $gl_trans_res = get_gl_trans($row['type'], $row['trans_no']);
        $gl_trans_recs = db_num_rows($gl_trans_res);
        if(!($gl_trans_recs > 0)){
            begin_global_transaction();
            $msg = cs_create_supplier_payment_gl_entry($row);
            if($msg['success'] !== true){
                cancel_transaction();
                $message = $msg['message'];
                write_custom_script_log("cs_create_supplier_payment_gl_entries", $message);
                return;
            }else{
                commit_transaction();
                $message = "Type : ".$row['type'].", Trans Number : ".$row['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_supplier_payment_gl_entry($supp_trans){

//    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);

    $supplier_id = $supp_trans['supplier_id'];
    $payment_id = $supp_trans['trans_no'];
    $date_ = sql2date($supp_trans['tran_date']);
    $supp_amount = -$supp_trans['ov_amount'];
    $supp_discount = -$supp_trans['ov_discount'];
	// Now debit creditors account with payment + discount

//    return array("success"=>false, "message"=>"");
    
	$total = 0;
        $supplier_accounts = get_supplier_accounts($supplier_id);
	$total += add_gl_trans_supplier(ST_SUPPAYMENT, $payment_id, $date_, $supplier_accounts["payable_account"], 0, 0,
		$supp_amount + $supp_discount, $supplier_id);

	// Now credit discount received account with discounts
	if ($supp_discount != 0)
	{
		$total += add_gl_trans_supplier(ST_SUPPAYMENT, $payment_id, $date_, $supplier_accounts["payment_discount_account"], 0, 0,
			-$supp_discount, $supplier_id);
	}

        $bank_trans = get_bank_transaction_record(ST_SUPPAYMENT, $payment_id);
        $bank_account = $bank_trans['bank_act'];
        $bank_amount = -$bank_trans['amount'];
	$bank = get_bank_account($bank_account);

        $bank_charge = $bank_amount - ($supp_amount + $supp_discount);
	if ($bank_charge != 0)
	{
		$charge_act = get_company_pref('bank_charge_act');
		$total += add_gl_trans(ST_SUPPAYMENT, $payment_id, $date_, $charge_act, 0, 0, '',
			$bank_charge, $bank['bank_curr_code'], PT_SUPPLIER, $supplier_id);
	}

	$total += add_gl_trans(ST_SUPPAYMENT, $payment_id, $date_, $bank['account_code'], 0, 0, '',
		-($bank_amount + $bank_charge), $bank['bank_curr_code'], PT_SUPPLIER, $supplier_id);

	/*Post a balance post if $total != 0 due to variance in AP and bank posted values*/
	if ($total != 0)
	{
		$variance_act = get_company_pref('exchange_diff_act');
		add_gl_trans(ST_SUPPAYMENT, $payment_id, $date_, $variance_act, 0, 0, '',
			-$total, null, PT_SUPPLIER,  $supplier_id);
	}
    
    return array("success"=>true, "message"=>"");
    
}

