diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index 2eb458f..480c582 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -197,14 +197,14 @@ class TahesabController extends Controller // directon: 1 = pardakht be moshtari public function wallet($data, ?Tahesab $existing = null) { - -// if ($input instanceof \Illuminate\Http\Request) { -// $data = $input->all(); -// } elseif (is_array($input)) { -// $data = $input; -// } else { -// $data = request()->all(); -// } + if (empty($data['customer_id']) && $existing) { + $data = array_merge($data, [ + 'customer_id' => $existing->customer_id, + 'value' => $existing->amount, + 'direction' => str_contains((string) $existing->desc, 'برداشت') ? 1 : 0, + 'withdraw_id' => $existing->withdraw_id, + ]); + } if (!isset($data['customer_id']) || !isset($data['value'])) { return request()->expectsJson() @@ -234,7 +234,12 @@ class TahesabController extends Controller "data" => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), ]); - $date = Verta::instance(Carbon::now())->format('Y-m-d'); + if ($existing && empty($existing->data)) { + $existing->update(['data' => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]); + } + + $dateSource = ($existing !== null && !empty($existing->date)) ? Carbon::parse($existing->date) : Carbon::now(); + $date = Verta::instance($dateSource)->format('Y-m-d'); [$year, $month, $day] = explode('-', $date); try { @@ -384,6 +389,14 @@ class TahesabController extends Controller */ public function goldReward($data, ?Tahesab $existing = null) { + if (empty($data['customer_id']) && $existing) { + $data = array_merge($data, [ + 'customer_id' => $existing->customer_id, + 'value' => $existing->amount, + 'desc' => $existing->desc, + ]); + } + if (!isset($data['customer_id'], $data['value'])) { return request()->expectsJson() ? response()->json(['error' => 'Data missing: customer_id or value'], 422) @@ -403,9 +416,6 @@ class TahesabController extends Controller $amount = abs((float) $data['value']); $desc = !empty($data['desc']) ? (string) $data['desc'] : 'پاداش'; - $date = Verta::instance(Carbon::now())->format('Y-m-d'); - [$year, $month, $day] = explode('-', $date); - $data['handler'] = 'goldReward'; $tahesab = $existing ?? Tahesab::create([ 'customer_id' => $customer->id, @@ -417,6 +427,14 @@ class TahesabController extends Controller 'data' => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), ]); + if ($existing && empty($existing->data)) { + $existing->update(['data' => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]); + } + + $dateSource = ($existing !== null && !empty($existing->date)) ? Carbon::parse($existing->date) : Carbon::now(); + $date = Verta::instance($dateSource)->format('Y-m-d'); + [$year, $month, $day] = explode('-', $date); + try { $response = Http::withoutVerifying()->timeout(10)->withHeaders([ 'DBName' => $this->Config('tahesab_db'), @@ -460,25 +478,21 @@ class TahesabController extends Controller // direction: 1 = واریز طلا (خرید)، 0 = برداشت طلا (فروش) public function walletGold($data, ?Tahesab $existing = null) { -// if ($input instanceof \Illuminate\Http\Request) { -// $data = $input->all(); -// } elseif (is_array($input)) { -// $data = $input; -// } else { -// $data = request()->all(); -// } -// return ($data); + if (empty($data['customer_id']) && $existing) { + $data = array_merge($data, [ + 'customer_id' => $existing->customer_id, + 'value' => $existing->amount, + 'direction' => str_contains((string) $existing->desc, 'برداشت') ? 0 : 1, + 'withdraw_id' => $existing->withdraw_id, + ]); + } - if (!isset($data['customer_id'])) { + if (!isset($data['customer_id'], $data['value'])) { return request()->expectsJson() ? response()->json(['error' => 'Data missing: customer_id or value'], 422) : false; } -// $customer = auth()->user(); -// if (!$customer) { -// throw new UnprocessableEntityHttpException("Auth needed"); -// } $customer = Customer::where('id', $data['customer_id'])->first(); if ($customer && ($customer->moshtari_code === null)) { @@ -486,9 +500,6 @@ class TahesabController extends Controller $customer = Customer::where('id', $data['customer_id'])->first(); } - $date = Verta::instance(Carbon::now())->format('Y-m-d'); - [$year, $month, $day] = explode('-', $date); - $amount = abs($data['value']); $buyOrSale = (int) ($data['direction'] ?? 1) === 1 ? 1 : 0; $isDeposit = $buyOrSale === 1; @@ -505,6 +516,14 @@ class TahesabController extends Controller "data" => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), ]); + if ($existing && empty($existing->data)) { + $existing->update(['data' => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]); + } + + $dateSource = ($existing !== null && !empty($existing->date)) ? Carbon::parse($existing->date) : Carbon::now(); + $date = Verta::instance($dateSource)->format('Y-m-d'); + [$year, $month, $day] = explode('-', $date); + $prices = $this->lastPriceData(); $mazaneh = $isDeposit ? (int) ($prices['buy'] ?? 0) : (int) ($prices['sell'] ?? 0);