From 562edfadd90602bfa25770af5bca272309a722cd Mon Sep 17 00:00:00 2001 From: gholak tala Date: Fri, 1 May 2026 19:34:10 +0000 Subject: [PATCH] NASER aff gift card controller and models and routes --- .../Controllers/Admin/GiftCardController.php | 191 ++++++++++++++++++ app/Models/GiftCard.php | 51 +++++ app/Models/GiftCardCodes.php | 43 ++++ routes/admin.php | 5 + routes/customer.php | 4 + 5 files changed, 294 insertions(+) create mode 100644 app/Http/Controllers/Admin/GiftCardController.php create mode 100644 app/Models/GiftCard.php create mode 100644 app/Models/GiftCardCodes.php diff --git a/app/Http/Controllers/Admin/GiftCardController.php b/app/Http/Controllers/Admin/GiftCardController.php new file mode 100644 index 0000000..5fbe44b --- /dev/null +++ b/app/Http/Controllers/Admin/GiftCardController.php @@ -0,0 +1,191 @@ +user(); + $count = $request->count; + $amount = $request->amount; + if(!$user){ + throw new UnprocessableEntityHttpException('auth failed!'); + } + $codes = []; + if((!is_numeric($count) || $count < 0) || (!is_numeric($amount) || $amount <= 0)){ + return response()->json(['error' => 'تعداد کد را چک کنید!'], 400); + } + $customer = Customer::select('id', 'name', 'mobile')->where('mobile', $request->mobile)->first(); + + $gift = GiftCard::create([ + 'customer_id' => $customer->id ?? null, + 'count' => $count, + 'amount' => $amount, + 'mobile' => $request->mobile, + 'name' => $customer ? $customer->name : $request->name, + 'amount' => $amount, + 'total' => $count*$amount, + 'sts' => 1, + 'unit' => $request->unit, + 'expiry_date' => $request->expiry_date, + 'created_at' => now(), + 'updated_at' => now(), + 'description' => $request->description, + ]); + + for ($i = 0; $i < $count; $i++) { + do { + $unique_code = strtoupper(bin2hex(random_bytes(4))); + } while (GiftCardCodes::where('code', $unique_code)->exists()); + + $codes[] = [ + 'giftcard_id' => $gift->id, + 'code' => $unique_code, + 'sts' => 1, + 'unit' => $request->unit, + 'amount' => $amount, + 'expiry_date' => $request->expiry_date, + 'created_at' => now(), + ]; + } + GiftCardCodes::insert($codes); + + return response()->json([ + 'customer' => $customer, + 'order' => $gift, + 'codes' => $gift->giftcardcodes, + 'total' => $count*$amount, + ]); + } + + public function useGiftCard(Request $request){ + $user = auth()->user(); + if(!$user){ + throw new UnprocessableEntityHttpException('auth failed!'); + } + + $redeem = $request->code; + $giftcode = GiftCardCodes::where('code', $redeem)->first(); + $gift = GiftCard::find($giftcode->giftcard_id); + + if(!$giftcode){ + return response()->json(['error' => 'کد هدیه یافت نشد!'], 404); + } + + //gift order update + $gift = GiftCard::find($giftcode->giftcard_id); + + if($gift->used_count+1 === $gift->count){ + $gift->sts = 2; + }elseif($gift->expiry_date <= now()){ + $gift->sts = 3; + + $giftcode->sts = 3; + $giftcode->save(); + $gift->save(); + + return response()->json(['error' => 'کد هدیه منقضی شده است!'], 400); + } + + $gift->used_count += 1; + $gift->save(); + + if($giftcode->sts != 1){ + return response()->json(['error' => 'کد هدیه قبلا استفاده شده است!'], 400); + } + + + + + //tranaction + if($giftcode->unit === 1){ + + //add IRR to user wallet + $Withdraw = Withdraw::create([ + 'customer_id' => auth('customer')->id(), + "cash" => $giftcode->amount, + "note" => "کارت هدیه", + 'sts' => 1, + 'order_id' => $giftcode->id, + ]); + $Withdraw->tracked_number = $giftcode->code; // توکن تراکنش برابر کد گیفت کارت + if ($Withdraw->cash == $giftcode->amount) { + if ($Withdraw->order_id){ + $this->verifyOrder($Withdraw->id); + } + $Withdraw->zify_code = $Withdraw->tracked_number; + $Withdraw->tracked_number = $giftcode->id;//gift card id + $Withdraw->sts = 6; + $Withdraw->save(); + $wallet_interface_fund = new WalletInterface($user->id, $user->type); + $transaction = new TransactionInterface($wallet_interface_fund, $_inp = [ + "cash" => $giftcode->amount, + 'expire_at' => now(), + 'withdraw_id' => $Withdraw->id, + 'active' => true + ], 3, false, true); + } + + //set text for sms to customer + $amount = number_format($giftcode->amount); + $message = "$user->name عزیز + کیف پول شما به مقدار $amount ریال + به واسطه کارت هدیه از طرف $gift->name شارژ شد. + قلک طلا"; + + }elseif ($giftcode->unit === 2){ + //add Gold to user wallet + return response()->json(['message'=> 'در دست ساخت👷🏻🚧'], 400); + } + + + + + //gift code update + $giftcode->assigned_to = $user->id; + $giftcode->sts = 2; + $giftcode->used_at = now(); + $giftcode->save(); + + $this->Send($user->mobile, $message); + + return response()->json([ + 'message' => 'کد هدیه با موفقیت استفاده شد!', + 'code' => $giftcode->code, + 'amount' => $giftcode->amount, + 'unit' => $giftcode->unit, + ]); + } + + public function table(Request $request) + { + $user = auth()->user(); + if(!$user){ + throw new UnprocessableEntityHttpException('auth failed!'); + } + $records = GiftCard::with([ + 'giftcardcodes', + 'giftcardcodes.assignedTo' + ]); + TableEngine::create($records, $request->all()) + ->initSearch([]) + ->select([ + '*', GiftCard::$SELECT_STS_STR + ])->export($records); + return response()->json($records); + } + +} diff --git a/app/Models/GiftCard.php b/app/Models/GiftCard.php new file mode 100644 index 0000000..03635e2 --- /dev/null +++ b/app/Models/GiftCard.php @@ -0,0 +1,51 @@ +hasMany(GiftCardCodes::class, 'giftcard_id', 'id'); + } + + static $STS = [ + 1 => "فعال", + 2 => "تمام شده", + 3 => "منقضی شده" + + ]; + static $UNIT = [ + 1=>'IRR', + 2=>'Golden' + ]; + + public function getStsStrAttribute() + { + return self::$STS[$this->sts] ?? null; + } + +} diff --git a/app/Models/GiftCardCodes.php b/app/Models/GiftCardCodes.php new file mode 100644 index 0000000..b177ffe --- /dev/null +++ b/app/Models/GiftCardCodes.php @@ -0,0 +1,43 @@ +'فعال', + 2=>'استفاده شده', + 3=>'منقضی شده' + ]; + + static $UNIT = [ + 1=>'IRR', + 2=>'Golden' + ]; + + public function assignedTo() + { + return $this->belongsTo(Customer::class, 'assigned_to', 'id')->select('id', 'name', 'mobile'); + } + +} diff --git a/routes/admin.php b/routes/admin.php index 3dde9d7..a8b1f9a 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -18,6 +18,7 @@ use App\Http\Controllers\Admin\TahesabController; use App\Http\Controllers\Admin\FileController; use App\Http\Controllers\Site\OfferCodesController; use App\Http\Controllers\Admin\SettingsController; +use \App\Http\Controllers\Admin\GiftCardController; Route::middleware('auth:user')->group(function (){ @@ -154,5 +155,9 @@ Route::middleware('auth:user')->group(function (){ //check card Route::post('/check_card',[CustomerController::class, 'cardChecker']); + //Giftcard + Route::post('giftcard/generate/', [GiftCardController::class, 'redeem']); + Route::post('giftcard/table/', [GiftCardController::class, 'table']); + }); }); diff --git a/routes/customer.php b/routes/customer.php index c10af21..814a3ae 100644 --- a/routes/customer.php +++ b/routes/customer.php @@ -14,6 +14,7 @@ use App\Http\Controllers\Admin\ProductController as AdminProductController; use App\Http\Controllers\Customer\ProductController; use App\Http\Controllers\Admin\TahesabController; use App\Http\Controllers\Site\OfferCodesController; +use App\Http\Controllers\Admin\GiftCardController; //----------gateway-verify @@ -97,6 +98,9 @@ Route::middleware('auth:customer')->group(function () { // Route::post('account/tahesab', [TahesabController::class, 'store']); Route::post('account/tahesab/trade', [TahesabController::class, 'storeTrade']); + //--------------------use gift card + Route::post('giftcard/use', [GiftCardController::class, 'useGiftCard']); + //offer code Route::post('offer_code/check_code', [OfferCodesController::class, 'check_code']);