<?php
function check_voucher_exist($dbc,$voucherCode,$amount,$countryCode){
    $query = "SELECT * FROM  voucher_tbl WHERE recharge_code = '$voucherCode' AND status = 'UNUSED' AND amount = '$amount' AND countryCode = '$countryCode' ";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function get_voucher_by_code($dbc,$voucherCode){
    $query = "SELECT * FROM voucher_tbl WHERE recharge_code = '$voucherCode'";
    $q = mysqli_query($dbc,$query);
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function recharge_with_voucher($dbc,$phoneNum,$voucherCode,$countryCode){
    $voucher = get_voucher_by_code($dbc,$voucherCode);
    if(add_funds($dbc,$phoneNum,$voucher['amount'],$countryCode)){
        if(update_voucher_after_recharge($dbc,$voucherCode,$phoneNum)){
            return true;
        }
    }
}



function update_voucher_after_recharge($dbc,$voucherCode,$phoneNum){
    $query = "
        UPDATE voucher_tbl SET
        contactNumber = '$phoneNum',
        status = 'USED',
        usedDate = NOW() WHERE
        recharge_code = '$voucherCode'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

    function generate_voucher($dbc,$amount,$prodQuantity,$country){
    $seed = get_voucher_seed($dbc);
    $serialSeed = $seed['serialNumber'];
    $batchSeed = $seed['batchNumber'];
    $serialNumber = null;
    $batchNumber = null;
    for($check=0; $check<=($prodQuantity -1); $check++){
        $rnd1 = mt_rand(1000000,9999999);
        $rnd = mt_rand(1000000,9999999);
        $rnd2 = mt_rand(1,26);
        $rnd4 = mt_rand(1,26);
        $rnd3 = mt_rand(1,5);

        switch($rnd2){
            case'1': $a = "A";break;case'2': $a = "B";break;case'3': $a = "C";break;case'4': $a = "D";break;
            case'5': $a = "E";break;case'6': $a = "F";break;case'7': $a = "G";break;case'8': $a = "H";break;
            case'8': $a = "I";break;case'10': $a = "J";break;case'11': $a = "K";break;case'12': $a = "L";break;
            case'13': $a = "M";break;case'14': $a = "N";break;case'15': $a = "A";break;case'16': $a = "P";break;
            case'17': $a = "Q";break;case'18': $a = "R";break;case'19': $a = "S";break;case'20': $a = "T";break;
            case'21': $a = "U";break;case'22': $a = "V";break;case'23': $a = "W";break;case'24': $a = "X";break;
            case'25': $a = "Y";break;case'26': $a = "Z";break;
        }
        switch($rnd4){
            case'1': $a1 = "A";break;case'2': $a1 = "B";break;case'3': $a1 = "C";break;case'4': $a1 = "D";break;
            case'5': $a1 = "E";break;case'6': $a1 = "F";break;case'7': $a1 = "G";break;case'8': $a1 = "H";break;
            case'8': $a1 = "I";break;case'10': $a1 = "J";break;case'11': $a1 = "K";break;case'12': $a1 = "L";break;
            case'13': $a1 = "M";break;case'14': $a1 = "N";break;case'15': $a1 = "Z";break;case'16': $a1 = "P";break;
            case'17': $a1 = "Q";break;case'18': $a1 = "R";break;case'19': $a1 = "S";break;case'20': $a1 = "T";break;
            case'21': $a1 = "U";break;case'22': $a1 = "V";break;case'23': $a1 = "W";break;case'24': $a1 = "X";break;
            case'25': $a1 = "Y";break;case'26': $a1 = "Z";break;
        }

        $recharge_code = $rnd.$a.$a1.$rnd1;
        $serialNumber = $serialSeed + $check;
        $batchNumber = $batchSeed + $check;


        $q = mysqli_query($dbc,"INSERT INTO voucher_tbl (id,recharge_code, amount, status, serialNum, batchNumber, countryCode) 
          VALUES (UUID(),'$recharge_code','$amount','NEW','$serialNumber','$batchNumber','$country')
      ");
    }

    $serialSeed = $seed['serialNumber'] + $check;
    $batchSeed = $seed['batchNumber'] + $check;
    //$bn3 = $bn1 - 1;
    $updpay = "update payglobalconfig_tbl set serialNumber='$serialSeed',batchNumber='$batchSeed' where id like '1%'";
    if(mysqli_query($dbc,$updpay)){
        return true;
    }
}

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


function generate_promoCode($length = 6){
    $characters = '123456789ABCDEFGHIJKLMNPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString.date("md");
}

function promoCode($dbc,$countryCode,$expiredDate,$phoneNum,$quantity,$amount){

    for ($i = 0; $i < $quantity; $i++) {
        $code = generate_promoCode();

        $q = mysqli_query($dbc,"INSERT INTO promo_tbl (id, countryCode, promocode, exp_date, phoneNum, generated_by,amount) 
        VALUES (UUID(),'$countryCode','$code','$expiredDate','$phoneNum','$_SESSION[uid]','$amount')
        ");


    }

    return true;



}

function get_single_promoCode($dbc,$promoCode){
    $q = mysqli_query($dbc,"SELECT * FROM promo_tbl WHERE promocode = '$promoCode' AND status = 'ACTIVE'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function update_promoCode_after_use($dbc,$recipientPhone,$promoCode){
    $q = mysqli_query($dbc,"UPDATE promo_tbl SET used_by = '$recipientPhone', used_date = NOW(), status = 'USED' WHERE promocode = '$promoCode'");
    if($q){
        return true;
    }
}