fix tahesab

This commit is contained in:
2026-06-07 14:30:26 +03:30
parent 085237364d
commit 20ed4ee3ad
4 changed files with 128 additions and 39 deletions
@@ -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);
+111 -18
View File
@@ -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),
]);
@@ -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){
@@ -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);