From 20ed4ee3ad332732a3ac385ddcbe1235c9ce4ce6 Mon Sep 17 00:00:00 2001 From: adonis Date: Sun, 7 Jun 2026 14:30:26 +0330 Subject: [PATCH] fix tahesab --- .../Controllers/Admin/CustomerController.php | 18 ++- .../Controllers/Admin/TahesabController.php | 129 +++++++++++++++--- .../Controllers/Admin/WalletController.php | 2 +- .../Customer/CustomerController.php | 18 ++- 4 files changed, 128 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/Admin/CustomerController.php b/app/Http/Controllers/Admin/CustomerController.php index 97ce8cc..c4d32ed 100644 --- a/app/Http/Controllers/Admin/CustomerController.php +++ b/app/Http/Controllers/Admin/CustomerController.php @@ -218,11 +218,10 @@ class CustomerController extends SuperController if($this->Config('send_to_tahesab') == "true"){ try { $tahesab = new TahesabController(); - $tahesab->walletGold([ - 'customer_id'=> $customer->id, - 'value'=> $gift_for_inviter, - 'direction'=> 0, - 'desc' => 'پاداش بابت معرفی' + $tahesab->goldReward([ + 'customer_id' => $customer->referral->id, + 'value' => $gift_for_inviter, + 'desc' => 'پاداش بابت معرفی', ]); }catch (\Exception $e){ return response($e->getMessage(), 500); @@ -268,11 +267,10 @@ class CustomerController extends SuperController if($this->Config('send_to_tahesab') == "true"){ try { $tahesab = new TahesabController(); - $tahesab->walletGold([ - 'customer_id'=> $customer->id, - 'value'=> $gift_for_inviter, - 'direction'=> 0, - 'desc' => 'پاداش بابت ثبت نام' + $tahesab->goldReward([ + 'customer_id' => $customer->id, + 'value' => $gift_for_invited, + 'desc' => 'پاداش بابت ثبت نام', ]); }catch (\Exception $e){ return response($e->getMessage(), 500); diff --git a/app/Http/Controllers/Admin/TahesabController.php b/app/Http/Controllers/Admin/TahesabController.php index 1794181..f0a8b9a 100644 --- a/app/Http/Controllers/Admin/TahesabController.php +++ b/app/Http/Controllers/Admin/TahesabController.php @@ -48,6 +48,10 @@ class TahesabController extends Controller [$year, $month, $day] = explode('-', $date); $amount = $data['amount']; + $isBuy = (int) ($data['direction'] ?? 0) === 1; + $mablaghKol = $isBuy + ? (int) round((float) $amount * (float) $data['price']) + : (int) ($data['total_price'] ?? round((float) $amount * (float) $data['price'])); $tahesab = Tahesab::create([ "customer_id" => $customer->id, @@ -81,16 +85,16 @@ class TahesabController extends Controller (int)$data['price'], //Mazaneh 1, //MazanehIsMesghalOrGeram_0_1 1, //IsMotefaregheOrAbshode_0_1 - (int)$data['total_price'], //MablaghKol + $mablaghKol, //MablaghKol — خرید: وزن خالص × قیمت (بعد از کارمزد) $data['direction'] == 1 ? "خرید آبشده" : "فروش آبشده", //sharh - "Code_Factor", - "", //HavalehBe_Mcode - "", //MultiRadif + -1, //Factor_Code — خودکار + -1, //HavalehBe_Mcode + -1, //MultiRadif $data['product']['metal_id'] == 1 ? 0 : 2, //Jens_Felez (string)Verta::instance($data['pay_at']), ] ])->json(); - } catch (Exception $e) { + } catch (\Exception $e) { return $e->getMessage(); } if ($response['OK']) { @@ -98,7 +102,7 @@ class TahesabController extends Controller "sh_factor" => $response['Sh_factor'], "tahesab_id" => $response['OK'], "factor_number" => $response['Factor_Number'], - "tah_id" => $response['ID'], + "tah_id" => $response['ID'] ?? null, "data" => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), "res" => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), ]); @@ -281,6 +285,89 @@ class TahesabController extends Controller return false; } + /** + * پاداش/هدیه طلایی — سند بدهی وزنی (مثل ثبت دستی «بدهی / بابت معرفی» در ته‌حساب). + */ + public function goldReward($data) + { + if (!isset($data['customer_id'], $data['value'])) { + return request()->expectsJson() + ? response()->json(['error' => 'Data missing: customer_id or value'], 422) + : false; + } + + $customer = Customer::where('id', $data['customer_id'])->first(); + if (!$customer) { + return false; + } + + if ($customer->moshtari_code === null) { + $this->create_user($customer->id); + $customer = Customer::where('id', $data['customer_id'])->first(); + } + + $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); + + $tahesab = Tahesab::create([ + 'customer_id' => $customer->id, + 'desc' => $desc, + 'amount' => $amount, + 'date' => Carbon::now(), + 'type' => 'طلا', + 'created_at' => Carbon::now(), + ]); + + try { + $response = Http::withoutVerifying()->timeout(10)->withHeaders([ + 'DBName' => $this->Config('tahesab_db'), + 'Content-Type' => 'application/json', + 'Authorization' => 'Bearer ' . $this->Config('tahesab_token'), + ])->post((string) env('TAHESAB'), [ + 'DoNewSanadTalabBedehi' => [ + 1, // Sabte_Kol + (int) $customer->moshtari_code, + -1, // Factor_Number + 1, // Radif_Number + (int) $year, + (int) $month, + (int) $day, + 0, // IsBedehiOrTalab — بدهی (فروشگاه به مشتری طلا بدهکار است) + 0, // MablaghKol — بدون مبلغ ریالی + $desc, + $desc, // Baabat + (float) $amount, + -1, // Factor_Code + -1, // HavalehBe_Mcode + -1, // MultiRadif + 0, // Jens_Felez — طلا + 'IRR', + ], + ])->json(); + } catch (\Exception $e) { + return $e->getMessage(); + } + + if ($response['OK']) { + Tahesab::where('id', $tahesab->id)->update([ + 'sh_factor' => $response['Sh_factor'] ?? null, + 'tahesab_id' => $response['OK'], + 'factor_number' => $response['Factor_Number'] ?? null, + 'tah_id' => $response['ID'] ?? null, + 'data' => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), + 'res' => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), + ]); + + return $data; + } + + return false; + } + + // direction: 1 = واریز طلا (خرید)، 0 = برداشت طلا (فروش) public function walletGold($data) { // if ($input instanceof \Illuminate\Http\Request) { @@ -313,10 +400,12 @@ class TahesabController extends Controller [$year, $month, $day] = explode('-', $date); $amount = abs($data['value']); + $buyOrSale = (int) ($data['direction'] ?? 1) === 1 ? 1 : 0; + $isDeposit = $buyOrSale === 1; $tahesab = Tahesab::create([ "customer_id" => $customer->id, - "desc" => $data['direction'] == 0 ? "شارژ کیف پول آبشده" : "برداشت از کیف پول آبشده", + "desc" => $isDeposit ? "شارژ کیف پول آبشده" : "برداشت از کیف پول آبشده", "amount" => $amount, "date" => Carbon::now(), "type" => "طلا", @@ -325,6 +414,12 @@ class TahesabController extends Controller $price = new ReportController(); $prices = $price->lastPrice(); + $mazaneh = $isDeposit ? (int) $prices['buy'] : (int) $prices['sell']; + $mablaghKol = (int) round($mazaneh * $amount); + $sharh = !empty($data['desc']) + ? $data['desc'] + : ($isDeposit ? 'خرید آبشده' : 'فروش آبشده'); + try { $response = Http::withoutVerifying()->timeout(10)->withHeaders([ 'DBName' => $this->Config('tahesab_db'), @@ -343,22 +438,20 @@ class TahesabController extends Controller 750, //Ayar 0, "", - $data['direction'], //BuyOrSale_0_1 - $data['direction'] == 0 ? (int)$prices['buy'] : (int)$prices['sell'], //Mazaneh + $buyOrSale, //BuyOrSale_0_1 — 1=خرید، 0=فروش + $mazaneh, //Mazaneh 1, //MazanehIsMesghalOrGeram_0_1 1, //IsMotefaregheOrAbshode_0_1 - $data['direction'] == 0 ? (int)$prices['buy'] * $amount : (int)$prices['sell'] * $amount, //MablaghKol - !empty($data['desc']) - ? $data['desc'] - : (($data['direction'] ?? null) == 0 ? "خرید آبشده" : "فروش آبشده"),//Sharh - "Code_Factor", - "", //HavalehBe_Mcode - "", //MultiRadif + $mablaghKol, //MablaghKol + $sharh,//Sharh + -1, //Factor_Code — خودکار + -1, //HavalehBe_Mcode + -1, //MultiRadif 0, //Jens_Felez (string)$date, ] ])->json(); - } catch (Exception $e) { + } catch (\Exception $e) { return $e->getMessage(); } if ($response['OK']) { @@ -366,7 +459,7 @@ class TahesabController extends Controller "sh_factor" => $response['Sh_factor'], "tahesab_id" => $response['OK'], "factor_number" => $response['Factor_Number'], - "tah_id" => $response['ID'], + "tah_id" => $response['ID'] ?? null, "data" => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), "res" => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), ]); diff --git a/app/Http/Controllers/Admin/WalletController.php b/app/Http/Controllers/Admin/WalletController.php index 854dfb7..95f9e59 100644 --- a/app/Http/Controllers/Admin/WalletController.php +++ b/app/Http/Controllers/Admin/WalletController.php @@ -189,7 +189,7 @@ class WalletController extends Controller //send to tahesan if($this->Config('send_to_tahesab') == "true"){ try { - $data['direction'] = $deposit ? 1 : 2; + $data['direction'] = $deposit ? 1 : 0; $tahesab = new TahesabController(); $tahesab->walletGold($data); }catch (\Exception $e){ diff --git a/app/Http/Controllers/Customer/CustomerController.php b/app/Http/Controllers/Customer/CustomerController.php index 3ba7fbf..072aeb4 100644 --- a/app/Http/Controllers/Customer/CustomerController.php +++ b/app/Http/Controllers/Customer/CustomerController.php @@ -220,11 +220,10 @@ class CustomerController extends Controller if($this->Config('send_to_tahesab') == "true"){ try { $tahesab = new TahesabController(); - $tahesab->walletGold([ - 'customer_id'=> $customer->id, - 'value'=> $gift_for_inviter, - 'direction'=> 0, - 'desc' => 'پاداش بابت معرفی' + $tahesab->goldReward([ + 'customer_id' => $customer->referral->id, + 'value' => $gift_for_inviter, + 'desc' => 'پاداش بابت معرفی', ]); }catch (\Exception $e){ return response($e->getMessage(), 500); @@ -270,11 +269,10 @@ class CustomerController extends Controller if($this->Config('send_to_tahesab') == "true"){ try { $tahesab = new TahesabController(); - $tahesab->walletGold([ - 'customer_id'=> $customer->id, - 'value'=> $gift_for_inviter, - 'direction'=> 0, - 'desc' => 'پاداش بابت ثبت نام' + $tahesab->goldReward([ + 'customer_id' => $customer->id, + 'value' => $gift_for_invited, + 'desc' => 'پاداش بابت ثبت نام', ]); }catch (\Exception $e){ return response($e->getMessage(), 500);