<?php
function transcription($dbc,$senderData,$recipientData,$transactionType,$amount,$billCode,$transaction_uid){

        if(debit_forex($dbc,$amount,$senderData['countryCode'],$recipientData['countryCode'],$recipientData,$senderData,$billCode,$transactionType,$transaction_uid)){
            return true;
        }
}
function transcription_token($dbc,$senderData,$recipientData,$transactionType,$amount,$billCode,$transaction_uid){
        if(debit_forex($dbc,$amount,$senderData['countryCode'],$recipientData['countryCode'],$recipientData,$senderData,$billCode,$transactionType,$transaction_uid)){
            return true;
        }
}

function transcription_roaming($dbc,$senderData,$recipientData,$transactionType,$convertedAmount,$billCode,$transaction_uid){
    if(redraw_funds($dbc,$senderData['id'],$convertedAmount,$senderData['countryCode'])){
        if(debit_forex_roaming($dbc,$convertedAmount,$senderData['countryCode'],$recipientData['countryCode'],$recipientData,$senderData,$billCode,$transactionType,$transaction_uid)){
            return true;
        }
    }
}

function translation($dbc,$transactionData){
    $redrawAmount = $transactionData['sendersAmount'] + $transactionData['fee'];
    if (redraw_funds($dbc, $transactionData['senderID'], $redrawAmount, $transactionData['senderCountryCode'])) {
        if (add_funds($dbc, $transactionData['recipientID'], $transactionData['sendersAmount'],$transactionData['recipientCountryCode'])) {
            return true;
        }
    }

}

function debit_forex($dbc,$amount,$senderCountryCode,$recipientCountryCode,$recipientData,$senderData,$billCode,$transactionType,$transaction_uid){
    $data = get_forex($dbc,$senderCountryCode);
    $localAccountDebit = $data['localAccountDebit'];
    $dominantAccountDebit = $data['dominantAccountDebit'];
    $newlocalAccountDebit = $localAccountDebit + $amount;

    //Convert Amount to USD
    $rate = get_conversion_rate($dbc,$senderCountryCode,"us");


    $convertedAmount = $amount * $rate;
    $newDominantAccountDebit = $dominantAccountDebit + $convertedAmount;



    $q = mysqli_query($dbc,"UPDATE payglobal_forex_account_tbl SET
          localAccountDebit = '$newlocalAccountDebit',
          dominantUSDaccountDebit = '$newDominantAccountDebit' WHERE countryCode = '$senderCountryCode'
          ");
    if($q){
        if(credit_forex($dbc,$convertedAmount,$recipientCountryCode,$recipientData,$senderData,$amount,$billCode,$transactionType,$transaction_uid)){
            return true;
        }
    }

}

function debit_forex_roaming($dbc,$amount,$senderCountryCode,$recipientCountryCode,$recipientData,$senderData,$billCode,$transactionType,$transaction_uid){
    $data = get_forex($dbc,$senderCountryCode);
    $localAccountDebit = $data['localAccountDebit'];
    $dominantAccountDebit = $data['dominantAccountDebit'];
    $newlocalAccountDebit = $localAccountDebit + $amount;

    //Convert Amount to USD
    $rate = get_conversion_rate($dbc,$senderCountryCode,"us");
    $convertedAmount = $amount * $rate;
    $newDominantAccountDebit = $dominantAccountDebit + $convertedAmount;



    $q = mysqli_query($dbc,"UPDATE payglobal_forex_account_tbl SET
          localAccountDebit = '$newlocalAccountDebit',
          dominantUSDaccountDebit = '$newDominantAccountDebit' WHERE countryCode = '$senderCountryCode'
          ");
    if($q){
        if(credit_forex($dbc,$convertedAmount,$recipientCountryCode,$recipientData,$senderData,$billCode,$transactionType,$transaction_uid)){
            return true;
        }
    }

}
function debit_forex_main($dbc,$amount,$senderCountryCode,$recipientCountryCode){
    $data = get_forex($dbc,$senderCountryCode);
    $localAccountDebit = $data['localAccountDebit'];
    $dominantAccountDebit = $data['dominantAccountDebit'];
    $newlocalAccountDebit = $localAccountDebit + $amount;

    //Convert Amount to USD
    $rate = get_conversion_rate($dbc,$senderCountryCode,"us");
    $convertedAmount = $amount * $rate;
    $newDominantAccountDebit = $dominantAccountDebit + $convertedAmount;



    $q = mysqli_query($dbc,"UPDATE payglobal_forex_account_tbl SET
          localAccountDebit = '$newlocalAccountDebit',
          dominantUSDaccountDebit = '$newDominantAccountDebit' WHERE countryCode = '$senderCountryCode'
          ");
    if($q){
        $convertsfee = credit_forex_main($dbc,$convertedAmount,$recipientCountryCode);
        return $convertsfee;
    }

}

function credit_forex($dbc,$convertedAmount, $recipientCountryCode,$recipientData,$senderData,$senderAmount,$billCode,$transactionType,$transaction_uid){
    $data = get_forex($dbc,$recipientCountryCode);
    $localAccountCredit = $data['localAccountCredit'];
    $dominantAccountCredit = $data['dominantAccountCredit'];
    $NewDominantAccountCredit = $dominantAccountCredit + $convertedAmount;

    //Convert Amount(USD) to Local
    $rate = get_conversion_rate($dbc,'us',$recipientCountryCode);
    $localConvertedAmount = $convertedAmount * $rate;
    $newLocalAccountCredit = $localAccountCredit + $localConvertedAmount;



    $q = mysqli_query($dbc,"UPDATE payglobal_forex_account_tbl SET
          dominantUSDaccountCredit = '$NewDominantAccountCredit',
          localAccountCredit = '$newLocalAccountCredit' WHERE countryCode = '$recipientCountryCode'
          ");
    if($q){
        return true;
    }

}
function credit_forex_main($dbc,$amount,$recipientCountryCode){
    $data = get_forex($dbc,$recipientCountryCode);
    $localAccountCredit = $data['localAccountCredit'];
    $dominantAccountCredit = $data['dominantAccountCredit'];
    $NewDominantAccountCredit = $dominantAccountCredit + $amount;

    //Convert Amount(USD) to Local
    $rate = get_conversion_rate($dbc,'us',$recipientCountryCode);
    $convertedAmount = $amount * $rate;
    $newLocalAccountCredit = $localAccountCredit + $convertedAmount;



    $q = mysqli_query($dbc,"UPDATE payglobal_forex_account_tbl SET
          dominantUSDaccountCredit = '$NewDominantAccountCredit',
          localAccountCredit = '$newLocalAccountCredit' WHERE countryCode = '$recipientCountryCode'
          ");
    if($q){
        return $convertedAmount;
    }

}


function get_forex($dbc,$senderCountryCode){
    $q = mysqli_query($dbc,"SELECT * FROM payglobal_forex_account_tbl WHERE countryCode = '$senderCountryCode'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}


function credit_account_by_superAgent($dbc,$super_agentID,$customerID,$amount,$transaction_uid,$checkBalance,$w_sender_name,$w_idtype,$w_idno,$w_idexpirydate,$trigger_msg = true){
    $superAgentData = get_customer_by_id($dbc,$super_agentID);
    $customerData = get_customer_by_id($dbc,$customerID);

    $pricing_id = $checkBalance['pricing_id'];

    $pricing = get_pricing_by_id($dbc,$pricing_id);

    if($superAgentData['countryCode'] == $customerData['countryCode']) {
        // Same Country or Local Money Transfer
        if (redraw_funds($dbc, $super_agentID, $amount,$superAgentData['countryCode'])) {
            if (add_funds($dbc, $customerID, $amount,$customerData['countryCode'])) {
                //$transaction_uid = genTransactionID();
                $senderAcc = get_cus_account_info_by_customerID($dbc,$super_agentID,$superAgentData['countryCode']);
                $recipientAcc = get_cus_account_info_by_customerID($dbc,$customerID,$customerData['countryCode']);
                //if (add_main_transaction($dbc, $customerData, $superAgentData, 'MT', "", $transaction_uid, "COMPLETED", $checkBalance['fee'], $amount, $amount, "PC")) {
                if (add_main_transaction($dbc, $customerData, $superAgentData, 'MT', "", $transaction_uid, "COMPLETED", $checkBalance['fee'], $amount, $amount, "PC",$checkBalance['pricing_id'])) {
                    if(update_main_transaction_walking_details($dbc,$transaction_uid,$w_sender_name,$w_idno,$w_idtype,$w_idexpirydate)) {
                        if (update_main_transaction_after_processing($dbc, $transaction_uid, "COMPLETED", $senderAcc['balance'], $recipientAcc['balance'])) {
                            $transactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);

                            /*
                             * This set of code apply when an agent of super agent is loading an individual Wallet.
                             * Thus the agent or the super agent received 0.004% of the amount
                             */

                            if ($superAgentData['accountType'] == 4 AND $customerData['accountType'] == 2) {
                                $newFee = $transactionData['sendersAmount'] * $pricing['senderCommission'];
                                if (add_agent_commission_for_addMoney($dbc, $transactionData, $newFee, $superAgentData['countryCode'], $superAgentData['id'])) {
                                    // Redraw funds
                                    add_commissionExpense($dbc, $superAgentData['countryCode'], $newFee);
                                }
                            }

                            if (($superAgentData['accountType'] == 4) AND $customerData['accountType'] == 1) {
                                $newFee = $transactionData['sendersAmount'] * $pricing['senderCommission'];
                                if (add_agent_commission_for_addMoney($dbc, $transactionData, $newFee, $superAgentData['countryCode'], $superAgentData['id'])) {
                                    // Redraw funds
                                    add_commissionExpense($dbc, $superAgentData['countryCode'], $newFee);
                                }
                            }
                            /*
                             *end of loading money commission
                             */
                            if($trigger_msg) {
                                if ($superAgentData['countryCode'] == 'gh') {
                                    $message = "You have received an amount of {$amount} from GN Money. Transaction ID: {$transaction_uid}";
									
                                } elseif($superAgentData['countryCode'] == 'lr' || $superAgentData['countryCode'] == 'lr1') {
									$message = "You have received an amount of {$amount} from GN Mobile Money. Transaction ID: {$transaction_uid}";
								}else{	
                                    $message = "You have received an amount of {$amount} from PAY GLOBAL. Transaction ID: {$transaction_uid}";
                                }
                                sendSMS($customerData['phoneNum'], $message);
                                if ($customerData['firebaseKey'] != '') {
                                    firebaseCall($customerData['firebaseKey'], $message);
                                }
                            }
                            return true;
                        }else{
                            return false;
                        }
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    } else{
        // International or Inter Country Money Transfer
        $transaction_uid = genTransactionID();
        $rate = get_conversion_rate($dbc,$superAgentData['countryCode'],$customerData['countryCode']);
        $convertedAmount = $amount * $rate;
        if(add_main_transaction($dbc,$customerData,$superAgentData,'MT',"",$transaction_uid,'PROCESSING',$checkBalance['fee'],$amount,$convertedAmount,'PC',$pricing_id)){
            if(redraw_funds($dbc,$superAgentData['id'],$amount,$superAgentData['countryCode'])) {
                if(add_funds($dbc,$customerData['id'],$convertedAmount,$customerData['countryCode'])) {
                    if (transcription($dbc, $superAgentData, $customerData, 'MT', $amount, '', $transaction_uid)) {
                        $agentAcc = get_cus_account_info_by_customerID($dbc, $super_agentID,$superAgentData['countryCode']);
                        $customerAcc = get_cus_account_info_by_customerID($dbc, $customerID,$customerData['countryCode']);
                        if (update_main_transaction_after_processing($dbc, $transaction_uid, "COMPLETED", $agentAcc['balance'], $customerAcc['balance'])) {
                            if(update_main_transaction_walking_details($dbc,$transaction_uid,$w_sender_name,$w_idno,$w_idtype,$w_idexpirydate)) {
                                $newTransactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);
                                if (redraw_funds($dbc, $super_agentID, $checkBalance['fee'],$superAgentData['countryCode'])) {
                                    if ($customerData['accountType'] == 1 OR $customerData['accountType'] == 2 ) {
                                        $agentFee = $pricing['senderCommission'] * $checkBalance['fee'];
                                        $payGlobalFee = $pricing['sysCommission'] * $checkBalance['fee'];
                                        if (add_payGlobal_commission($dbc, $newTransactionData, $payGlobalFee)) {
                                            if (add_agent_commission_for_addMoney($dbc, $newTransactionData, $agentFee, $superAgentData['countryCode'], $superAgentData['id'])) {
                                                //$message = "You have received an amount of {$newTransactionData['recipientAmount']} from {$superAgentData['companyName']}. Transaction ID: {$transaction_uid}";
                                                if($trigger_msg) {
                                                    $message = "You have received an amount of {$newTransactionData['recipientAmount']} from PAY GLOBAL. Transaction ID: {$transaction_uid}";
                                                    sendSMS($customerData['phoneNum'], $message);
                                                    if ($customerData['firebaseKey'] != '') {
                                                        firebaseCall($customerData['firebaseKey'], $message);
                                                    }
                                                }
                                                return true;
                                            }
                                        }
                                    } else {
                                        if (add_payGlobal_commission($dbc, $newTransactionData, $checkBalance['fee'])) {
                                            //$message = "You have received an amount of {$newTransactionData['recipientAmount']} from {$superAgentData['companyName']}. Transaction ID: {$transaction_uid}";
                                            if($trigger_msg) {
                                                $message = "You have received an amount of {$newTransactionData['recipientAmount']} from PAY GLOBAL. Transaction ID: {$transaction_uid}";
                                                sendSMS($customerData['phoneNum'], $message);
                                                if ($customerData['firebaseKey'] != '') {
                                                    firebaseCall($customerData['firebaseKey'], $message);
                                                }
                                            }
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}



