<?php
/**
 * Created by PhpStorm.
 * User: Ludah
 * Date: 12/08/2017
 * Time: 1:09 PM
 */
function get_payGlobal_commission_summary_by_countryCode($dbc,$countryCode){
    $q = mysqli_query($dbc,"SELECT * FROM payglobal_commission_summary_tbl WHERE countryCode = '$countryCode'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function add_funds_to_payGlobal_commission_summary_by_countryCode($dbc,$countryCode,$funds){
    $old_data = get_payGlobal_commission_summary_by_countryCode($dbc,$countryCode);
    $newFunds = $old_data['balance'] + $funds;

    $q = mysqli_query($dbc,"UPDATE payglobal_commission_summary_tbl SET balance = '$newFunds' WHERE countryCode = '$countryCode'");
    if($q){
        return true;
    }

}

function get_all_payGlobal_commission_summary($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM payglobal_commission_summary_tbl");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function add_payGlobal_commission($dbc,$all_transactionData,$fee){
    $q = mysqli_query($dbc,"INSERT INTO payglobal_commission_tbl(id,countryCode, customer_id, amount, serviceType, transaction_uid) VALUES 
      (UUID(),'$all_transactionData[senderCountryCode]','$all_transactionData[senderID]','$fee','$all_transactionData[transactionTypes]','$all_transactionData[transaction_uid]')
    ");
    if($q){
        if(add_funds_to_payGlobal_commission_summary_by_countryCode($dbc,$all_transactionData['senderCountryCode'],$fee)){
            return true;
        }
    }
}

function add_agent_commission($dbc,$all_transactionData,$fee,$agentCountryCode){
    $customerData = get_cus_account_info_by_customerID($dbc,$all_transactionData['recipientID'],$agentCountryCode);
    $q = mysqli_query($dbc,"INSERT INTO agent_commission_tbl(id,countryCode, customer_id,agent_id, amount, serviceType, transaction_uid) VALUES 
      (UUID(),'$agentCountryCode','$all_transactionData[senderID]','$all_transactionData[recipientID]','$fee','$all_transactionData[transactionTypes]','$all_transactionData[transaction_uid]')
    ");
    $totalFee = $customerData['commissionAccount'] + $fee;
    if($q){
        $qq = mysqli_query($dbc, "UPDATE customeraccount_tbl SET commissionAccount = '$totalFee' WHERE customerID = '$all_transactionData[recipientID]' AND countryCode = '$agentCountryCode' ");
        if($qq){
            return true;
        }
    }
}

function reverse_agent_commission($dbc,$all_transactionData,$fee,$agentCountryCode){
    $customerData = get_cus_account_info_by_customerID($dbc,$all_transactionData['recipientID'],$agentCountryCode);
    $q = mysqli_query($dbc,"INSERT INTO agent_commission_tbl(id,countryCode, customer_id,agent_id, amount, serviceType, transaction_uid) VALUES 
      (UUID(),'$agentCountryCode','$all_transactionData[senderID]','$all_transactionData[recipientID]','$fee','$all_transactionData[transactionTypes]','$all_transactionData[transaction_uid]')
    ");
    $totalFee = $customerData['commissionAccount'] - ($fee * 0.5);
    if($q){
        $qq = mysqli_query($dbc, "UPDATE customeraccount_tbl SET commissionAccount = '$totalFee' WHERE customerID = '$all_transactionData[recipientID]' AND countryCode = '$agentCountryCode' ");
        if($qq){
            return true;
        }
    }
}

function add_agent_commission_for_addMoney($dbc,$all_transactionData,$fee,$agentCountryCode,$beneficiaryID){
    $customerData = get_cus_account_info_by_customerID($dbc,$beneficiaryID,$agentCountryCode);
    $q = mysqli_query($dbc,"INSERT INTO agent_commission_tbl(id,countryCode, customer_id,agent_id, amount, serviceType, transaction_uid) VALUES 
      (UUID(),'$agentCountryCode','$all_transactionData[senderID]','$beneficiaryID','$fee','$all_transactionData[transactionTypes]','$all_transactionData[transaction_uid]')
    ");
    $totalFee = $customerData['commissionAccount'] + $fee;
    if($q){
        $qq = mysqli_query($dbc, "UPDATE customeraccount_tbl SET commissionAccount = '$totalFee' WHERE customerID = '$beneficiaryID' and countryCode = '$agentCountryCode' ");
        if($qq){
            return true;
        }
    }
}

function add_commissionExpense($dbc,$countryCode,$fee){
    $globalAccounts = get_global_Accounts($dbc,$countryCode);
    $commissionExAccount = $globalAccounts['commissionExpense'];
    $newCommission = $commissionExAccount + $fee;
    $q = mysqli_query($dbc,"UPDATE payGlobal_accounts_tbl SET commissionExpense = '$newCommission' WHERE countryCode = '$countryCode'");
    if($q){
        return true;
    }
}

function add_airtimeExpense($dbc,$countryCode,$fee){
    $globalAccounts = get_global_Accounts($dbc,$countryCode);
    $airtimeExAccount = $globalAccounts['airtimeExpense'];
    $newCommission = $airtimeExAccount + $fee;
    $q = mysqli_query($dbc,"UPDATE payGlobal_accounts_tbl SET airtimeExpense = '$newCommission' WHERE countryCode = '$countryCode'");
    if($q){
        return true;
    }
}



function get_commission_by_customer($dbc,$customerID){
    $query = "
        SELECT * FROM agent_commission_tbl WHERE agent_id =  '$customerID' ORDER BY dateTime DESC LIMIT 500
    ";
    $q = mysqli_query($dbc,$query);
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function get_total_earned_commission_by_customer($dbc,$customerID,$countryCode){
    $query = "SELECT sum(amount) FROM agent_commission_tbl WHERE agent_id =  '$customerID' AND countryCode='$countryCode'";
    $q = mysqli_query($dbc,$query);
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_total_redeemed_commission_by_customer($dbc,$customerID,$countryCode){
    $query = "
        SELECT sum(recipientAmount) as fee_total FROM maintransaction_tbl WHERE recipientID = '$customerID' AND transactionStatus = 'COMPLETED' AND senderCountryCode = '$countryCode' AND transactionTypes = 'COMMISSION' ";
    $q = mysqli_query($dbc,$query);
    while ($row = mysqli_fetch_assoc($q)){
        $data = $row['fee_total'];
    }
    return $data;
}


function add_bank_commission($dbc,$customerID,$senderBankAccount,$recipientBankAccount,$bankCommissionAccount,$fee,$transaction_uid,$foreignId,$status){
    $q = mysqli_query($dbc,"INSERT INTO bank_commission_tbl (id, customerID, senderBankAccount, recipientBankAccount, bankCommissionAccount, fee, transaction_uid, foreignID,status) VALUES 
    (UUID(),'$customerID','$senderBankAccount','$recipientBankAccount','$bankCommissionAccount','$fee','$transaction_uid','$foreignId','$status')
    ");
    if($q){
        return true;
    }
}



function get_agent_commission_by_uid($dbc,$transaction_uid){
    $q = mysqli_query($dbc,"SELECT * FROM agent_commission_tbl WHERE transaction_uid = '$transaction_uid'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_payGlobal_commission_by_uid($dbc,$transaction_uid){
    $q = mysqli_query($dbc,"SELECT * FROM payglobal_commission_tbl WHERE transaction_uid = '$transaction_uid'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function update_agent_commission_acc($dbc,$amount,$agentID){
    $q = mysqli_query($dbc,"UPDATE customeraccount_tbl SET commissionAccount = '$amount' WHERE customerID = '$agentID'");
    if($q){
        return true;
    }
}

function update_agent_commission_acc_new($dbc,$amount,$agentID,$countryCode){
    $q = mysqli_query($dbc,"UPDATE customeraccount_tbl SET commissionAccount = '$amount' WHERE customerID = '$agentID' AND countryCode='$countryCode'");
    if($q){
        return true;
    }
}

function redraw_agent_commission_by_uid($dbc,$transaction_uid,$agentID){
    $commData = get_agent_commission_by_uid($dbc,$transaction_uid);
	$cusData = get_customer_by_id($dbc,$agentID);
    $agentAcc = get_cus_account_info_by_customerID($dbc,$agentID,$cusData['countryCode']);
    $newCommission = ($agentAcc['commissionAccount'] * 1) - ($commData['amount'] * 1);
    if(update_agent_commission_acc($dbc,$newCommission,$agentID)){
        $q = mysqli_query($dbc,"UPDATE agent_commission_tbl SET amount = 0 WHERE transaction_uid = '$transaction_uid'");
        if($q){
            return true;
        }
    }

}

function redraw_payGlobal_commission_by_uid($dbc,$transaction_uid){
    $comData = get_payGlobal_commission_by_uid($dbc,$transaction_uid);
    $payGlobalCom = get_payGlobal_commission_summary_by_countryCode($dbc,$comData['countryCode']);
    $totalAmount = $payGlobalCom['balance'] - $comData['amount'];
    $q = mysqli_query($dbc, "UPDATE payglobal_commission_summary_tbl SET balance = '$totalAmount' WHERE countryCode = '$comData[countryCode]' ");
    if($q){
        $qq = mysqli_query($dbc,"UPDATE payglobal_commission_tbl SET amount = 0 WHERE transaction_uid = '$transaction_uid'");
        if($qq){
            return true;
        }
    }
}