2026-05-01 19:34:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\GiftCard;
|
|
|
|
|
use App\Models\GiftCardCodes;
|
|
|
|
|
use App\DB\Te\TableEngine;
|
|
|
|
|
use App\Models\Withdraw;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
|
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
|
|
|
|
use Rooyesh\Wallet\interfaces\WalletInterface;
|
2026-06-11 22:08:47 +03:30
|
|
|
use App\Services\ConfigService;
|
2026-05-01 19:34:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class GiftCardController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function redeem(Request $request)
|
|
|
|
|
{
|
2026-06-11 22:08:47 +03:30
|
|
|
if (app(ConfigService::class)->isStore()) {
|
|
|
|
|
return $this->storeRedeem($request);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 19:34:10 +00:00
|
|
|
$user = auth()->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)){
|
2026-05-04 14:52:50 +00:00
|
|
|
return response()->json(['message' => 'تعداد کد را چک کنید!'], 400);
|
2026-05-01 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
$customer = Customer::select('id', 'name', 'mobile')->where('mobile', $request->mobile)->first();
|
|
|
|
|
|
|
|
|
|
$gift = GiftCard::create([
|
|
|
|
|
'customer_id' => $customer->id ?? null,
|
|
|
|
|
'count' => $count,
|
|
|
|
|
'amount' => $amount,
|
2026-05-04 16:47:28 +00:00
|
|
|
'mobile' => $request->mobile ?? null,
|
|
|
|
|
'name' => ($customer ? $customer->name : $request->name) ?? null,
|
2026-05-01 19:34:10 +00:00
|
|
|
'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([
|
2026-05-04 16:47:28 +00:00
|
|
|
'customer' => $customer ?? null,
|
2026-05-01 19:34:10 +00:00
|
|
|
'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){
|
2026-05-04 14:52:50 +00:00
|
|
|
return response()->json(['message' => 'کد هدیه یافت نشد!'], 404);
|
2026-05-01 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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();
|
|
|
|
|
|
2026-05-04 14:52:50 +00:00
|
|
|
return response()->json(['message' => 'کد هدیه منقضی شده است!'], 400);
|
2026-05-01 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$gift->used_count += 1;
|
|
|
|
|
$gift->save();
|
|
|
|
|
|
|
|
|
|
if($giftcode->sts != 1){
|
2026-05-04 14:52:50 +00:00
|
|
|
return response()->json(['message' => 'کد هدیه قبلا استفاده شده است!'], 400);
|
2026-05-01 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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);
|
2026-07-26 14:15:26 +03:30
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
app(\App\Services\PargarSyncService::class)->queueWalletDocument(
|
|
|
|
|
$user,
|
|
|
|
|
'cash',
|
|
|
|
|
(float) $giftcode->amount,
|
|
|
|
|
[
|
|
|
|
|
'withdraw_id' => $Withdraw->id,
|
|
|
|
|
'note' => 'کارت هدیه',
|
|
|
|
|
'source' => 'gift_card',
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
report($e);
|
|
|
|
|
}
|
2026-05-01 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:08:47 +03:30
|
|
|
public function useDiscount(Request $request)
|
|
|
|
|
{
|
|
|
|
|
if (! app(ConfigService::class)->isStore()) {
|
|
|
|
|
return response()->json(['message' => 'این قابلیت فقط برای فروشگاه فعال است'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $this->applyDiscount((string) $request->input('code'), auth('customer')->id());
|
|
|
|
|
|
|
|
|
|
if (! empty($result['message'])) {
|
|
|
|
|
return response()->json(['message' => $result['message']], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json(['amount' => $result['amount']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validateDiscount(Request $request)
|
|
|
|
|
{
|
|
|
|
|
if (! app(ConfigService::class)->isStore()) {
|
|
|
|
|
return response()->json(['message' => 'این قابلیت فقط برای فروشگاه فعال است'], 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$giftcode = $this->verifyDiscount((string) $request->input('code'));
|
|
|
|
|
if (! empty($giftcode['message'])) {
|
|
|
|
|
return response()->json(['message' => $giftcode['message']], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json(['giftcard' => $giftcode], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function storeRedeem(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
$count = $request->count;
|
|
|
|
|
$amount = $request->amount;
|
|
|
|
|
if (! $user) {
|
|
|
|
|
throw new UnprocessableEntityHttpException('auth failed!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((! is_numeric($count) || $count < 0) || (! is_numeric($amount) || $amount <= 0)) {
|
|
|
|
|
return response()->json(['message' => 'تعداد کد را چک کنید!'], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($request->code) && GiftCardCodes::where('code', $request->code)->where('unit', $request->unit)->exists()) {
|
|
|
|
|
return response()->json(['message' => 'کد تکراری میباشد!'], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$codes = [];
|
|
|
|
|
$unitAmount = $request->unit == 3 ? $amount / 100 : $amount;
|
|
|
|
|
|
|
|
|
|
if ($request->use_once) {
|
|
|
|
|
$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);
|
|
|
|
|
} else {
|
|
|
|
|
do {
|
|
|
|
|
$unique_code = strtoupper(bin2hex(random_bytes(4)));
|
|
|
|
|
} while (GiftCardCodes::where('code', $unique_code)->exists());
|
|
|
|
|
|
|
|
|
|
$gift = GiftCard::create([
|
|
|
|
|
'use_once' => $request->use_once ? 1 : 0,
|
|
|
|
|
'count' => $count,
|
|
|
|
|
'unique_code' => ! empty($request->code) ? $request->code : $unique_code,
|
|
|
|
|
'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,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
GiftCardCodes::insert([
|
|
|
|
|
'giftcard_id' => $gift->id,
|
|
|
|
|
'use_once' => $request->use_once ? 1 : 0,
|
|
|
|
|
'code' => ! empty($request->code) ? $request->code : $unique_code,
|
|
|
|
|
'sts' => 1,
|
|
|
|
|
'unit' => $request->unit,
|
|
|
|
|
'amount' => $unitAmount,
|
|
|
|
|
'expiry_date' => $request->expiry_date,
|
|
|
|
|
'created_at' => now(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'order' => $gift,
|
|
|
|
|
'codes' => $gift->giftcardcodes,
|
|
|
|
|
'total' => $count * $amount,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-29 19:58:06 +03:30
|
|
|
public function applyDiscount(string $code, $customerId): array
|
2026-06-11 22:08:47 +03:30
|
|
|
{
|
|
|
|
|
$giftcode = $this->verifyDiscount($code);
|
|
|
|
|
if (! empty($giftcode['message'])) {
|
|
|
|
|
return ['message' => $giftcode['message']];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$giftcode = GiftCardCodes::where('code', $code)->first();
|
|
|
|
|
$gift = GiftCard::find($giftcode->giftcard_id);
|
|
|
|
|
|
|
|
|
|
$idsArray = json_decode($giftcode->assigned_to, true);
|
|
|
|
|
if (! is_array($idsArray)) {
|
|
|
|
|
$idsArray = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$idsArray[] = $customerId;
|
|
|
|
|
if ($gift->use_once == 1) {
|
|
|
|
|
$giftcode->sts = 2;
|
|
|
|
|
}
|
|
|
|
|
$giftcode->assigned_to = json_encode($idsArray);
|
|
|
|
|
$giftcode->save();
|
|
|
|
|
|
|
|
|
|
$gift->used_count += 1;
|
|
|
|
|
if ($gift->used_count + 1 > $gift->count) {
|
|
|
|
|
$gift->sts = 2;
|
|
|
|
|
}
|
|
|
|
|
$gift->save();
|
|
|
|
|
|
|
|
|
|
return ['amount' => $giftcode->amount];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function verifyDiscount(string $code)
|
|
|
|
|
{
|
|
|
|
|
$giftcode = GiftCardCodes::where('code', $code)->first();
|
|
|
|
|
|
|
|
|
|
if (! $giftcode) {
|
|
|
|
|
return ['message' => 'کد تخفیف یافت نشد!'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$gift = GiftCard::find($giftcode->giftcard_id);
|
|
|
|
|
$customer_id = auth('customer')->id();
|
|
|
|
|
|
|
|
|
|
if ($gift->unit !== 3 || $giftcode->unit !== 3) {
|
|
|
|
|
return ['message' => 'کد تخفیف صحیح نمیباشد!'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($gift->use_once === 0) {
|
|
|
|
|
$stored_ids = json_decode($giftcode->assigned_to, true);
|
|
|
|
|
if (is_array($stored_ids) && in_array($customer_id, $stored_ids)) {
|
|
|
|
|
return ['message' => 'شما قبلا از تخفیف خود استفاده کرده اید!'];
|
|
|
|
|
}
|
|
|
|
|
} elseif ($gift->use_once === 1) {
|
|
|
|
|
if (GiftCardCodes::where('giftcard_id', $gift->id)->where('assigned_to', '['.auth('customer')->id().']')->exists()) {
|
|
|
|
|
return ['message' => 'شما قبلا از تخفیف خود استفاده کرده اید!'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($gift->expiry_date <= now() || $gift->sts === 3) {
|
|
|
|
|
$gift->sts = 3;
|
|
|
|
|
$giftcode->sts = 3;
|
|
|
|
|
$giftcode->save();
|
|
|
|
|
$gift->save();
|
|
|
|
|
|
|
|
|
|
return ['message' => 'کد تخفیف منقضی شده است!'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($giftcode->sts === 2) {
|
|
|
|
|
return ['message' => 'کد تخفیف قبلا استفاده شده است!'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($gift->used_count + 1 > $gift->count) {
|
|
|
|
|
return ['message' => 'تعداد استفاده کد تخفیف به پایان رسیده است!'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $giftcode;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 19:34:10 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|