change gift-card
This commit is contained in:
@@ -39,7 +39,7 @@ class GiftCardController extends Controller
|
||||
return response()->json(['message' => $expiryError], 400);
|
||||
}
|
||||
|
||||
$customer = Customer::select('id', 'name', 'mobile')->where('mobile', $request->mobile)->first();
|
||||
$customer = $this->resolveGiftCardCustomerForCreate($request);
|
||||
$gift = $this->createGiftCardWithUniqueCodes($request, $customer);
|
||||
$createdCodes = $gift->giftcardcodes->pluck('code')->all();
|
||||
|
||||
@@ -342,6 +342,10 @@ class GiftCardController extends Controller
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->isPublicGiftVisibleInApp($gift)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$codes = $gift->giftcardcodes
|
||||
->filter(function ($code) use ($gift) {
|
||||
return (int) $code->sts !== 2
|
||||
@@ -350,23 +354,16 @@ class GiftCardController extends Controller
|
||||
})
|
||||
->values();
|
||||
|
||||
// عمومی: فقط پویش تککد مشترک (جلوگیری از افشای بچ تصادفی)
|
||||
if ($codes->count() !== 1 && empty($gift->unique_code)) {
|
||||
continue;
|
||||
foreach ($codes as $code) {
|
||||
if ($this->customerAlreadyUsedCode($code, $customerId, $gift)) {
|
||||
continue;
|
||||
}
|
||||
if (isset($seenCodes[$code->code])) {
|
||||
continue;
|
||||
}
|
||||
$seenCodes[$code->code] = true;
|
||||
$available[] = $this->mapCustomerGiftItem($gift, $code, 'public');
|
||||
}
|
||||
|
||||
$code = $codes->first();
|
||||
if (! $code) {
|
||||
continue;
|
||||
}
|
||||
if ($this->customerAlreadyUsedCode($code, $customerId, $gift)) {
|
||||
continue;
|
||||
}
|
||||
if (isset($seenCodes[$code->code])) {
|
||||
continue;
|
||||
}
|
||||
$seenCodes[$code->code] = true;
|
||||
$available[] = $this->mapCustomerGiftItem($gift, $code, 'public');
|
||||
}
|
||||
|
||||
$history = [];
|
||||
@@ -403,11 +400,55 @@ class GiftCardController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
private function resolveGiftCardCustomer(Request $request): ?Customer
|
||||
{
|
||||
if ($request->filled('customer_id')) {
|
||||
return Customer::select('id', 'name', 'mobile')->find((int) $request->customer_id);
|
||||
}
|
||||
|
||||
if ($request->filled('mobile')) {
|
||||
return Customer::select('id', 'name', 'mobile')->where('mobile', $request->mobile)->first();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* کارت عمومی: show_in_app=1 → در لیست اپ همه؛ ۰ → فقط با وارد کردن دستی کد.
|
||||
* سازگاری با رکوردهای قبل از ستون show_in_app: تککد مشترک مثل قبل در اپ میآید.
|
||||
*/
|
||||
private function isPublicGiftVisibleInApp(GiftCard $gift): bool
|
||||
{
|
||||
if (! isset($gift->show_in_app)) {
|
||||
$codesCount = $gift->relationLoaded('giftcardcodes')
|
||||
? $gift->giftcardcodes->count()
|
||||
: $gift->giftcardcodes()->count();
|
||||
|
||||
return $codesCount === 1 || ! empty($gift->unique_code);
|
||||
}
|
||||
|
||||
return (int) $gift->show_in_app === 1;
|
||||
}
|
||||
|
||||
private function resolveGiftCardCustomerForCreate(Request $request): ?Customer
|
||||
{
|
||||
$customer = $this->resolveGiftCardCustomer($request);
|
||||
if ($request->filled('customer_id') && ! $customer) {
|
||||
throw new UnprocessableEntityHttpException('مشتری انتخابشده یافت نشد!');
|
||||
}
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* یک رکورد giftcards + N کد یکتا در giftcard_codes (نه N رکورد جدا).
|
||||
*/
|
||||
private function createGiftCardWithUniqueCodes(Request $request, ?Customer $customer = null): GiftCard
|
||||
{
|
||||
if ($customer === null) {
|
||||
$customer = $this->resolveGiftCardCustomer($request);
|
||||
}
|
||||
|
||||
$count = (int) $request->count;
|
||||
$amount = $request->amount;
|
||||
$unit = (int) $request->unit;
|
||||
@@ -416,6 +457,7 @@ class GiftCardController extends Controller
|
||||
$gift = GiftCard::create([
|
||||
'customer_id' => $customer?->id,
|
||||
'use_once' => $request->use_once ? 1 : 0,
|
||||
'show_in_app' => $customer ? 0 : ($request->boolean('show_in_app') ? 1 : 0),
|
||||
'count' => $count,
|
||||
'amount' => $unitAmount,
|
||||
'mobile' => $customer?->mobile ?? $request->mobile ?? null,
|
||||
@@ -453,7 +495,7 @@ class GiftCardController extends Controller
|
||||
$gift->save();
|
||||
}
|
||||
|
||||
return $gift->load('giftcardcodes');
|
||||
return $gift->load(['giftcardcodes', 'customer']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -598,7 +640,12 @@ class GiftCardController extends Controller
|
||||
$createUniqueCodes = $request->use_once || $unit === 1 || $unit === 2;
|
||||
|
||||
if ($createUniqueCodes) {
|
||||
$gift = $this->createGiftCardWithUniqueCodes($request);
|
||||
try {
|
||||
$customer = $this->resolveGiftCardCustomerForCreate($request);
|
||||
} catch (UnprocessableEntityHttpException $e) {
|
||||
return response()->json(['message' => $e->getMessage()], 400);
|
||||
}
|
||||
$gift = $this->createGiftCardWithUniqueCodes($request, $customer);
|
||||
} else {
|
||||
do {
|
||||
$unique_code = strtoupper(bin2hex(random_bytes(4)));
|
||||
@@ -722,8 +769,9 @@ class GiftCardController extends Controller
|
||||
throw new UnprocessableEntityHttpException('auth failed!');
|
||||
}
|
||||
$records = GiftCard::with([
|
||||
'customer',
|
||||
'giftcardcodes',
|
||||
'giftcardcodes.assignedTo'
|
||||
'giftcardcodes.assignedTo',
|
||||
]);
|
||||
TableEngine::create($records, $request->all())
|
||||
->initSearch([])
|
||||
|
||||
Reference in New Issue
Block a user