From ba2ac5aefef1790cd0359c602a923bd2faa93842 Mon Sep 17 00:00:00 2001 From: safavimohadeseh78 Date: Sun, 20 Sep 2026 15:48:53 +0330 Subject: [PATCH] change the giftcard logic --- .../abshode-domain-lookup/learned-notes.md | 2 +- .../Controllers/Admin/GiftCardController.php | 134 ++++++++---------- 2 files changed, 59 insertions(+), 77 deletions(-) diff --git a/.cursor/skills/abshode-domain-lookup/learned-notes.md b/.cursor/skills/abshode-domain-lookup/learned-notes.md index 3a992d5..9039403 100644 --- a/.cursor/skills/abshode-domain-lookup/learned-notes.md +++ b/.cursor/skills/abshode-domain-lookup/learned-notes.md @@ -11,6 +11,6 @@ - 2026-09-09 | کارت هدیه فلزی | `GiftCardController::useGiftCard` | unit=2 دیگر «در دست ساخت» نیست؛ شارژ gold کیف اصلی + سینک ته‌حساب/زرین/پرگار؛ used_count فقط بعد از موفقیت - 2026-09-12 | واریز کیف‌پول + هدیه ریالی | `Withdraw::scopeDeposits` | note«کارت هدیه» با cash>0 در لیست واریز می‌آید؛ هدیه مسی (فقط gold) نه - 2026-09-12 | پیامک کارت هدیه | `GiftCardController::useGiftCard` | پسوند SMS: ریالی→«قلک ریال»، فلزی→«قلک مس» (نه name_fa / قلک طلا) -- 2026-09-12 | تعداد کد هدیه | `GiftCardController::redeem` | count=N → N کارت جدا هرکدام ۱ کد یکتا + مبلغ کامل؛ store هم برای unit 1/2 چندکد می‌سازد +- 2026-09-16 | تعداد کد هدیه | `GiftCardController::redeem` | count=N → یک رکورد giftcards با count=N + N ردیف giftcard_codes؛ مثل storeRedeem - 2026-09-12 | انقضای کارت هدیه | `GiftCardController::validateGiftExpiryDate` + `GiftCardEditorFormBody` | تاریخ قبل از امروز → ۴۰۰ / خطای فرم؛ امروز مجاز - 2026-09-09 | رمز ادمین ≠ لاگین اپ | `Customer::setPasswordAttribute` + `AuthController` + `updateAdmin` | قبل از هش/لاگین ارقام فارسی→انگلیسی؛ jwt hash=false؛ آنلاین فقط password غیرخالی بفرست diff --git a/app/Http/Controllers/Admin/GiftCardController.php b/app/Http/Controllers/Admin/GiftCardController.php index 9cc5402..27b916e 100644 --- a/app/Http/Controllers/Admin/GiftCardController.php +++ b/app/Http/Controllers/Admin/GiftCardController.php @@ -40,49 +40,13 @@ class GiftCardController extends Controller } $customer = Customer::select('id', 'name', 'mobile')->where('mobile', $request->mobile)->first(); - - // هر عدد از «تعداد کدها» = یک کارت هدیه جدا + یک کد یکتا با همان مبلغ شارژ - $createdGifts = []; - $createdCodes = []; - for ($i = 0; $i < $count; $i++) { - do { - $unique_code = strtoupper(bin2hex(random_bytes(4))); - } while (GiftCardCodes::where('code', $unique_code)->exists()); - - $gift = GiftCard::create([ - 'customer_id' => $customer->id ?? null, - 'count' => 1, - 'amount' => $amount, - 'mobile' => $request->mobile ?? null, - 'name' => ($customer ? $customer->name : $request->name) ?? null, - 'unique_code' => $unique_code, - 'total' => $amount, - 'sts' => 1, - 'unit' => $request->unit, - 'expiry_date' => $request->expiry_date, - 'created_at' => now(), - 'updated_at' => now(), - 'description' => $request->description, - ]); - - GiftCardCodes::insert([ - 'giftcard_id' => $gift->id, - 'code' => $unique_code, - 'sts' => 1, - 'unit' => $request->unit, - 'amount' => $amount, - 'expiry_date' => $request->expiry_date, - 'created_at' => now(), - ]); - - $createdGifts[] = $gift->load('giftcardcodes'); - $createdCodes[] = $unique_code; - } + $gift = $this->createGiftCardWithUniqueCodes($request, $customer); + $createdCodes = $gift->giftcardcodes->pluck('code')->all(); return response()->json([ 'customer' => $customer ?? null, - 'order' => $createdGifts[0] ?? null, - 'orders' => $createdGifts, + 'order' => $gift, + 'orders' => [$gift], 'codes' => $createdCodes, 'total' => $count * $amount, ]); @@ -439,6 +403,59 @@ class GiftCardController extends Controller ]); } + /** + * یک رکورد giftcards + N کد یکتا در giftcard_codes (نه N رکورد جدا). + */ + private function createGiftCardWithUniqueCodes(Request $request, ?Customer $customer = null): GiftCard + { + $count = (int) $request->count; + $amount = $request->amount; + $unit = (int) $request->unit; + $unitAmount = $unit === 3 ? $amount / 100 : $amount; + + $gift = GiftCard::create([ + 'customer_id' => $customer?->id, + 'use_once' => $request->use_once ? 1 : 0, + 'count' => $count, + 'amount' => $unitAmount, + 'mobile' => $customer?->mobile ?? $request->mobile ?? null, + 'name' => ($customer ? $customer->name : $request->name) ?? null, + 'total' => $unit === 3 ? null : $count * $amount, + 'sts' => 1, + 'unit' => $request->unit, + 'expiry_date' => $request->expiry_date, + 'created_at' => now(), + 'updated_at' => now(), + 'description' => $request->description, + ]); + + $codeRows = []; + for ($i = 0; $i < $count; $i++) { + do { + $uniqueCode = strtoupper(bin2hex(random_bytes(4))); + } while (GiftCardCodes::where('code', $uniqueCode)->exists()); + + $codeRows[] = [ + 'giftcard_id' => $gift->id, + 'use_once' => $request->use_once ? 1 : 0, + 'code' => $uniqueCode, + 'sts' => 1, + 'unit' => $request->unit, + 'amount' => $unitAmount, + 'expiry_date' => $request->expiry_date, + 'created_at' => now(), + ]; + } + GiftCardCodes::insert($codeRows); + + if ($count === 1) { + $gift->unique_code = $codeRows[0]['code']; + $gift->save(); + } + + return $gift->load('giftcardcodes'); + } + /** * تاریخ انقضا نباید قبل از امروز باشد (امروز مجاز است). */ @@ -575,48 +592,13 @@ class GiftCardController extends Controller return response()->json(['message' => 'کد تکراری میباشد!'], 400); } - $codes = []; $unitAmount = $request->unit == 3 ? $amount / 100 : $amount; $unit = (int) $request->unit; // کارت هدیه (ریالی/فلزی): همیشه N کد یکتا؛ تخفیف با use_once=0 می‌تواند تک‌کد مشترک باشد $createUniqueCodes = $request->use_once || $unit === 1 || $unit === 2; if ($createUniqueCodes) { - $gift = GiftCard::create([ - 'use_once' => $request->use_once ? 1 : 0, - 'count' => $count, - 'amount' => $unitAmount, - 'name' => $request->name, - 'total' => $request->unit == 3 ? null : $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, - 'use_once' => $request->use_once ? 1 : 0, - 'code' => $unique_code, - 'sts' => 1, - 'unit' => $request->unit, - 'amount' => $unitAmount, - 'expiry_date' => $request->expiry_date, - 'created_at' => now(), - ]; - } - GiftCardCodes::insert($codes); - if ($count === 1) { - $gift->unique_code = $codes[0]['code']; - $gift->save(); - } + $gift = $this->createGiftCardWithUniqueCodes($request); } else { do { $unique_code = strtoupper(bin2hex(random_bytes(4)));