change the giftcard logic
This commit is contained in:
@@ -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)));
|
||||
|
||||
Reference in New Issue
Block a user