From fc0ef5b41a35e3449a06463b9fa4990a17674514 Mon Sep 17 00:00:00 2001 From: Novath Thomas <57701433+pro-cms@users.noreply.github.com> Date: Tue, 30 Apr 2024 23:16:20 +0300 Subject: [PATCH] Added generate voucher function. --- init.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/init.php b/init.php index de0dc6a0..32971c84 100644 --- a/init.php +++ b/init.php @@ -233,6 +233,31 @@ function showResult($success, $message = '', $result = [], $meta = []) die(); } + +function generateUniqueNumericVouchers($totalVouchers, $length = 8) { + // Define characters allowed in the voucher code + $characters = '0123456789'; + $charactersLength = strlen($characters); + $vouchers = array(); + + // Attempt to generate unique voucher codes + for ($j = 0; $j < $totalVouchers; $j++) { + do { + $voucherCode = ''; + // Generate the voucher code + for ($i = 0; $i < $length; $i++) { + $voucherCode .= $characters[rand(0, $charactersLength - 1)]; + } + // Check if the generated voucher code already exists in the array + $isUnique = !in_array($voucherCode, $vouchers); + } while (!$isUnique); + + $vouchers[] = $voucherCode; + } + + return $vouchers; +} + function sendTelegram($txt) { Message::sendTelegram($txt);