<?php
function get_all_developers_account($dbc){
    $q = mysqli_query($dbc, "SELECT * FROM payGlobal_devAccount_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}
function get_all_global_account($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM payglobal_globalaccount_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function add_to_system_transaction($dbc,$gloAccount,$devAccount,$amount,$comment){
    $user = $_SESSION['uid'];
    $q = mysqli_query($dbc,"INSERT INTO dev_to_glo_transaction_tbl (id, amount, devAccount, gloAccount, trans_by,comment) VALUES 
          (UUID(),'$amount','$devAccount','$gloAccount','$user','$comment')
    ");
    if($q){
        return true;
    }
}

function add_to_super_global_transaction($dbc,$gloAccount,$superAgent,$amount,$transaction_uid){
    $user = $_SESSION['uid'];
    $q = mysqli_query($dbc,"INSERT INTO super_glo_acc_transaction_tbl (id, globalAccount, superAgent, amount, transaction_uid, user_by) VALUES 
          (UUID(),'$gloAccount','$superAgent','$amount','$transaction_uid','$user')
    ");
    if($q){
        return true;
    }
}

function get_devAccount_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM payGlobal_devAccount_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function credit_gloAccount_from_devAccount($dbc,$gloAccount,$devAccount,$amount,$comment){
    if(redraw_from_devAccount($dbc,$devAccount,$amount)){
        if(addFund_to_gloAccount($dbc,$gloAccount,$amount)){
            if(add_to_system_transaction($dbc,$gloAccount,$devAccount,$amount,$comment)){
                return true;
            }
        }
    }
}

function credit_superAgent_from_gloAccount($dbc,$gloAccount,$superAgent,$amount){
    if(redraw_from_gloAccount($dbc,$gloAccount,$amount)){
        if(debit_superAccount($dbc,$superAgent,$amount)){
            if(add_to_super_global_transaction($dbc,$gloAccount,$superAgent,$amount,genTransactionID())){
                return true;
            }
        }
    }
}

function debit_superAccount($dbc,$superAccount,$amount){
	$getCus = get_customer_by_id($dbc,$superAccount);
    $superAccountAcc = get_cus_account_info_by_customerID($dbc,$superAccount,$getCus['countryCode']);
    $balance = $superAccountAcc['balance'];
    $credit = $superAccountAcc['credit'];
    $newBalance = $balance + $amount;
    $newCredit = $credit + $amount;
	
	

    $q = mysqli_query($dbc,"UPDATE customeraccount_tbl SET balance = '$newBalance', credit = '$newCredit' WHERE customerID = '$superAccount' AND countryCode = '$getCus[countryCode]'");
    if($q){
        return true;
    }
}

function redraw_from_devAccount($dbc,$id,$amount){
    $devAccountData = get_all_from_devAccount_by_id($dbc,$id);
    $balance = $devAccountData['balance'];
    $debit = $devAccountData['debit'];

    $newBalance = $balance - $amount;
    $newDebit = $debit + $amount;
    $q = mysqli_query($dbc,"UPDATE payGlobal_devAccount_tbl SET 
      balance = '$newBalance',
      debit = '$newDebit'
      WHERE id = '$id'
    ");
    if($q){
        return true;
    }
}

function redraw_from_gloAccount($dbc,$id,$amount){
    $gloAccountData = get_all_from_gloAccount_by_id($dbc,$id);
    $balance = $gloAccountData['balance'];
    $debit = $gloAccountData['debit'];

    $newBalance = $balance - $amount;
    $newDebit = $debit + $amount;
    $q = mysqli_query($dbc,"UPDATE payglobal_globalAccount_tbl SET 
      balance = '$newBalance',
      debit = '$newDebit'
      WHERE id = '$id'
    ");
    if($q){
        return true;
    }
}

function addFund_to_gloAccount($dbc,$id,$amount){
    $devAccountData = get_all_from_gloAccount_by_id($dbc,$id);
    $balance = $devAccountData['balance'];
    $credit = $devAccountData['credit'];

    $newBalance = $balance + $amount;
    $newCredit = $credit + $amount;
    $q = mysqli_query($dbc,"UPDATE payglobal_globalaccount_tbl SET 
      balance = '$newBalance',
      credit = '$newCredit'
      WHERE id = '$id'
    ");
    if($q){
        return true;
    }
}
function get_all_from_devAccount_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM payGlobal_devAccount_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}
function get_all_from_gloAccount_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM payglobal_globalAccount_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_all_PayGlobalAccounts($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM payGlobal_accounts_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}