<?php
//create_new_account($dbc,$customerID)
//get_cus_account_info_by_customerID($dbc,$customerID,$countryCode)
//get_cus_account_info_all_by_CountryCode($dbc,$countryCode)
//create_new_account_CountryCode($dbc,$customerID,$countryCode)

function create_new_account($dbc,$customerID){
    $query = "
        INSERT INTO customeraccount_tbl (id,balance, customerID, remark,idStatus)
        VALUES (UUID(),0,'$customerID','paid','MINIMUM')
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function create_new_account_CountryCode($dbc,$customerID,$countryCode){
    $query = "
        INSERT INTO customeraccount_tbl (id,balance, customerID, remark,idStatus,countryCode)
        VALUES (UUID(),0,'$customerID','paid','MINIMUM','$countryCode')
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function  check_existing_balance($dbc,$customerID,$amount,$countryCode){
    $customerData  = get_cus_account_info_by_customerID($dbc,$customerID,$countryCode);
    if($customerData['balance'] >= $amount){
        return true;
    }
}

function add_funds($dbc,$customerID,$funds,$countryCode){
    $data = get_cus_account_info_by_customerID($dbc,$customerID,$countryCode);
    $newFunds = $data['balance'] + $funds;
    $newCredit = $data['credit'] + $funds;
    $query = "
        UPDATE customeraccount_tbl SET balance = '$newFunds', credit = '$newCredit' WHERE customerID = '$customerID' AND countryCode = '$countryCode'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}
function redraw_funds($dbc,$customerID,$funds,$countryCode){
    $data = get_cus_account_info_by_customerID($dbc,$customerID,$countryCode);
    $newFunds = $data['balance'] - $funds;
    $newDebit = $data['debit'] + $funds;
    $query = "
        UPDATE customeraccount_tbl SET balance = '$newFunds', debit = '$newDebit' WHERE customerID = '$customerID' AND countryCode = '$countryCode'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}


function restore_updated_account_special($dbc,$customerID,$credit,$debit,$balance,$countryCode){
    $query = "
        UPDATE customeraccount_tbl SET balance = '$balance', debit = '$debit', credit = '$credit'  WHERE customerID = '$customerID' AND countryCode = '$countryCode'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}



function check_if_balance_is_greater_than_redraw($dbc, $senderData, $amount,$recipientData,$general_service){
    $cus_acc_data = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
    $serviceID = get_service_id($dbc,$senderData,$amount,$general_service);
    $pricing = get_pricing($dbc,$senderData,$recipientData,$serviceID);
    $data = array();
    if($pricing['priceType'] == 'ABSOLUTE'){
        $newAmount = $pricing['price_in_percent_absolute'] + $amount;
        if($cus_acc_data['balance'] >= $newAmount){

            if($recipientData['companyName'] == ''){
                $data['recipientName'] = $recipientData['fullName'];
            }else{
                $data['recipientName'] = $recipientData['companyName'];
            }

            $data['status'] = true;
            $data['exactAmount'] = $amount;
            $data['senderID'] = $senderData['id'];
            $data['recipientID'] = $recipientData['id'];
            $data['transaction_uid'] = genTransactionID();
            $data['newAmount'] = $newAmount;
            $data['fee'] = $pricing['price_in_percent_absolute'];
            $data['pricing_id'] = $pricing['id'];

        } else{
            $data['status'] = false;
        }
    } elseif($pricing['priceType'] == 'PERCENTAGE'){
        $newAmount = $amount + ($pricing['price_in_percent_absolute'] * $amount);
        $fee = $newAmount - $amount;
        if($cus_acc_data['balance'] >= $newAmount){

            if($recipientData['companyName'] == ''){
                $data['recipientName'] = $recipientData['fullName'];
            }else{
                $data['recipientName'] = $recipientData['companyName'];
            }

            $data['status'] = true;
            $data['exactAmount'] = $amount;
            $data['senderID'] = $senderData['id'];
            $data['recipientID'] = $recipientData['id'];
            $data['transaction_uid'] = genTransactionID();
            $data['fee'] = $fee;
            $data['newAmount'] = $newAmount;
            $data['pricing_id'] = $pricing['id'];
            $data['serviceID'] = $serviceID;
        } else{
            $data['status'] = false;

        }
    }else{
            $data['status'] = 'error';
            //$data['serviceID'] = $serviceID;

    }
        return $data;
}

function check_if_balance_is_greater_than_redraw_roaming($dbc, $senderData, $amount,$recipientData,$general_service){
    $cus_acc_data = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);

    $foreignRate = get_conversion_rate($dbc,$recipientData['countryCode'],'us');
    $localRate = get_conversion_rate($dbc,'us',$senderData['countryCode']);

    $NewForeignRate = get_conversion_rate($dbc,$senderData['countryCode'],'us');
    $NewLocalRate = get_conversion_rate($dbc,'us',$recipientData['countryCode']);



    $converted_roaming_amount = $foreignRate * $amount * $localRate;

    $serviceID = get_service_id($dbc,$senderData,$converted_roaming_amount,$general_service);
    $pricing = get_pricing($dbc,$senderData,$recipientData,$serviceID);

    $data = array();
    if($pricing['priceType'] == 'ABSOLUTE'){
        $fee = $NewForeignRate * $pricing['price_in_percent_absolute'] * $NewLocalRate;
        $newAmount = $pricing['price_in_percent_absolute'] + $converted_roaming_amount;
        if($cus_acc_data['balance'] >= $newAmount){

            $data['status'] = true;
            $data['exactAmount'] = $amount;
            $data['newAmount'] = $amount + $fee;
            $data['convertedAmount'] = $converted_roaming_amount;
            $data['NewConvertedAmount'] = $converted_roaming_amount + $pricing['price_in_percent_absolute'];
            $data['senderID'] = $senderData['id'];
            $data['recipientID'] = $recipientData['id'];
            $data['recipientName'] = $recipientData['fullName'];
            $data['transaction_uid'] = genTransactionID();
            $data['convertedFee'] = $pricing['price_in_percent_absolute'];
            $data['fee'] = $fee;
            $data['pricing_id'] = $pricing['id'];
        } else{
            $data['status'] = false;
        }
    } elseif($pricing['priceType'] == 'PERCENTAGE'){
        $newAmount = $converted_roaming_amount + ($pricing['price_in_percent_absolute'] * $converted_roaming_amount);
        $convertedFee = $newAmount - $converted_roaming_amount;

        $fee = $NewForeignRate * $convertedFee * $NewLocalRate;

        if($cus_acc_data['balance'] >= $newAmount){
            $data['status'] = true;
            $data['exactAmount'] = $amount;
            $data['newAmount'] = $amount + $fee;
            $data['convertedAmount'] = $converted_roaming_amount;
            $data['NewConvertedAmount'] = $converted_roaming_amount + $convertedFee;
            $data['senderID'] = $senderData['id'];
            $data['recipientID'] = $recipientData['id'];
            $data['recipientName'] = $recipientData['fullName'];
            $data['transaction_uid'] = genTransactionID();
            $data['convertedFee'] = $convertedFee;
            $data['fee'] = $fee;
            $data['pricing_id'] = $pricing['id'];
        } else{
            $data['status'] = false;
        }
    }
    return $data;
}

function get_cus_account_info_by_customerID($dbc,$customerID,$countryCode){
    $query = "
        SELECT * FROM customeraccount_tbl WHERE customerID = '$customerID' AND countryCode = '$countryCode'
    ";
    $q = mysqli_query($dbc,$query);
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_cus_account_info_all($dbc,$customerID,$countryCode){
    $query = "
        SELECT * FROM customeraccount_tbl WHERE customerID = '$customerID' AND countryCode = '$countryCode'
    ";
    $q = mysqli_query($dbc,$query);
    $data = mysqli_fetch_assoc($q);
    return $data;
}


function get_cus_account_info_all_by_CountryCode($dbc,$countryCode){
    $query = "
        SELECT * FROM customeraccount_tbl WHERE countryCode = '$countryCode'
    ";
    $q = mysqli_query($dbc,$query);
    $data = mysqli_fetch_assoc($q);
    return $data;
}

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

function redraw_from_customer_commission($dbc,$customerID,$funds,$countryCode){
    $cus_acc = get_cus_account_info_by_customerID($dbc,$customerID,$countryCode);
    $newCommission = $cus_acc['commissionAccount'] - $funds;
    $q = mysqli_query($dbc,"UPDATE customeraccount_tbl SET commissionAccount = '$newCommission' WHERE customerID = '$customerID' AND countryCode='$countryCode'");
    if($q){
        return true;
    }
}

function funds_from_commission_to_wallet($dbc,$customerData,$funds){
    if(redraw_from_customer_commission($dbc,$customerData['id'],$funds,$customerData['countryCode'])){

        if(add_funds($dbc,$customerData['id'],$funds,$customerData['countryCode'])){
            return true;
        }
    }
}

function add_commission_to_acc_transaction($dbc,$customerID,$amount,$deviceType,$transaction_uid){
    $q = mysqli_query($dbc,"INSERT INTO agent_com_to_acc_tbl (id, customerID, amount, deviceType, transaction_uid) VALUES 
      (UUID(),'$customerID','$amount','$deviceType','$transaction_uid')
    ");
    if($q){
        return true;
    }
}
