Files
boursecu_server/app/Http/Controllers/Admin/TahesabController.php
T

363 lines
14 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Site\ReportController;
2026-01-24 14:03:11 +00:00
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use App\Models\Tahesab;
use App\Models\Customer;
2026-02-08 08:02:11 +00:00
use Morilog\Jalali\Jalalian;
2026-01-24 14:03:11 +00:00
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Hekmatinasser\Verta\Verta;
class TahesabController extends Controller
{
public function storeTrade($data)
2026-01-24 14:03:11 +00:00
{
// if ($input instanceof \Illuminate\Http\Request) {
// $data = $input->all();
// } elseif (is_array($input)) {
// $data = $input;
// } else {
// $data = request()->all();
// }
2026-01-24 14:03:11 +00:00
// return ($data);
if (!isset($data['customer_id']) || !isset($data['product']['amount'])) {
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)) {
2026-03-14 09:57:15 +03:30
$this->create_user($customer->id);
$customer = Customer::where('id', $data['customer_id'])->first();
2026-01-24 14:03:11 +00:00
}
$date = Verta::instance(Carbon::parse($data['pay_at']))->format('Y-m-d');
[$year, $month, $day] = explode('-', $date);
2026-02-08 08:02:11 +00:00
2026-01-24 14:03:11 +00:00
$amount = $data['amount'];
$tahesab = Tahesab::create([
"customer_id" => $customer->id,
"desc" => $data['direction'] == 1 ? "خرید آبشده" : "فروش آبشده",
"amount" => $amount,
"order_id" => $data['order']['id'] ?? null,
2026-01-24 14:03:11 +00:00
"date" => $data['pay_at'],
"type" => $data['product']['metal_id'] == 1 ? "طلا" : "نقره",
"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'),
2026-02-08 08:02:11 +00:00
])->post((string)env('TAHESAB'), [
2026-01-24 14:03:11 +00:00
"DoNewSanadBuySaleGOLD" => [
1,//Sabte_Kol_Or_Movaghat_1_0
(int)$customer->moshtari_code, //Moshtari_Code
-1, //Factor_Number
1, //Radif_Number
(int)$year, //Shamsi_Year
(int)$month, //Shamsi_Month
(int)$day, //Shamsi_Day
(float)$amount, //Vazn
$data['product']['metal_id'] == 1 ? 750 : "", //Ayar
0,
"",
$data['direction'] == 1 ? 0 : 1, //BuyOrSale_0_1
(int)$data['price'], //Mazaneh
1, //MazanehIsMesghalOrGeram_0_1
1, //IsMotefaregheOrAbshode_0_1
(int)$data['total_price_original'], //MablaghKol
$data['direction'] == 1 ? "خرید آبشده" : "فروش آبشده",
"Code_Factor",
"", //HavalehBe_Mcode
"", //MultiRadif
$data['product']['metal_id'] == 1 ? 0 : 2, //Jens_Felez
(string)Verta::instance($data['pay_at']),
]
])->json();
2026-01-24 14:03:11 +00:00
} catch (Exception $e) {
return $e->getMessage();
}
if ($response['OK']) {
Tahesab::where('id', $tahesab->id)->update([
"sh_factor" => $response['Sh_factor'],
"tahesab_id" => $response['OK'],
"factor_number" => $response['Factor_Number'],
"tah_id" => $response['ID'],
"data" => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
"res" => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
]);
2026-01-24 14:03:11 +00:00
return $data;
}
return false;
}
// directon: 0 = daryaft az moshtari
// directon: 1 = pardakht be moshtari
2026-03-14 16:21:42 +00:00
public function wallet($data)
2026-01-24 14:03:11 +00:00
{
2026-03-14 16:21:42 +00:00
// if ($input instanceof \Illuminate\Http\Request) {
// $data = $input->all();
// } elseif (is_array($input)) {
// $data = $input;
// } else {
// $data = request()->all();
// }
2026-01-24 14:03:11 +00:00
if (!isset($data['customer_id']) || !isset($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();
2026-03-14 09:57:15 +03:30
if ($customer && ($customer->moshtari_code === null)) {
$this->create_user($customer->id);
$customer = Customer::where('id', $data['customer_id'])->first();
2026-01-24 14:03:11 +00:00
}
$tahesab = Tahesab::create([
"customer_id" => $customer->id,
"desc" => $data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب",
"amount" => $data['value'],
"type" => "ريال",
2026-01-24 14:03:11 +00:00
"date" => Carbon::now(),
"created_at" => Carbon::now(),
]);
$date = Verta::instance(Carbon::now())->format('Y-m-d');
[$year, $month, $day] = explode('-', $date);
2026-01-24 14:03:11 +00:00
try {
$response = Http::withoutVerifying()->timeout(10)->withHeaders([
'DBName' => $this->Config('tahesab_db'),
'Content-Type' => 'application/json',
'Authorization' => "Bearer " . $this->Config('tahesab_token'),
2026-02-08 08:02:11 +00:00
])->post((string)env('TAHESAB'), [
2026-01-24 14:03:11 +00:00
"DoNewSanadVKHBank" => [
1,//Sabte_Kol_Or_Movaghat_1_0
$customer->moshtari_code,//Moshtari_Code
-1,//Factor_Number
1,//Radif_Number
2026-03-14 12:46:09 +00:00
(int)$year,//Shamsi_Year
(int)$month,//Shamsi_Month
(int)$day,//Shamsi_Day
(int)$data['direction'],//IsDayaftAzMoshtariOrPardakhtBeMoshtari_0_1
2026-01-24 14:03:11 +00:00
(int)$data['value'],//Mablagh
$data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب",//Sharh
"test",
"$tahesab->id",//Code_Rahgiri
-1,//Factor_Code
-1,
//MultiRadif
]
2026-03-14 16:21:42 +00:00
])->json();
2026-01-24 14:03:11 +00:00
} catch (\Exception $e) {
return $e->getMessage();
}
if ($response['OK']) {
2026-03-14 16:21:42 +00:00
Tahesab::where('id', $tahesab->id)->update([
"sh_factor" => $response['Sh_factor'],
"tahesab_id" => $response['OK'],
"tah_id" => $response['ID'],
"factor_number" => $response['Factor_Number'],
"data" => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
2026-03-14 16:21:42 +00:00
"res" => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
]);
2026-01-24 14:03:11 +00:00
return $data;
}
return false;
}
2026-03-14 09:57:15 +03:30
2026-03-14 16:21:42 +00:00
public function walletGold($data)
{
2026-03-14 16:21:42 +00:00
// if ($input instanceof \Illuminate\Http\Request) {
// $data = $input->all();
// } elseif (is_array($input)) {
// $data = $input;
// } else {
// $data = request()->all();
// }
// return ($data);
if (!isset($data['customer_id'])) {
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)) {
$this->create_user($customer->id);
$customer = Customer::where('id', $data['customer_id'])->first();
}
$date = Verta::instance(Carbon::now())->format('Y-m-d');
[$year, $month, $day] = explode('-', $date);
$amount = $data['value'];
$tahesab = Tahesab::create([
"customer_id" => $customer->id,
"desc" => $data['direction'] == 0 ? "شارژ کیف پول آبشده" : "برداشت از کیف پول آبشده",
"amount" => $amount,
"date" => Carbon::now(),
"type" => "طلا",
"created_at" => Carbon::now(),
]);
$price = new ReportController();
$prices = $price->lastPrice();
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'), [
"DoNewSanadBuySaleGOLD" => [
1,//Sabte_Kol_Or_Movaghat_1_0
(int)$customer->moshtari_code, //Moshtari_Code
-1, //Factor_Number
1, //Radif_Number
(int)$year, //Shamsi_Year
(int)$month, //Shamsi_Month
(int)$day, //Shamsi_Day
(float)$amount, //Vazn
750, //Ayar
0,
"",
$data['direction'], //BuyOrSale_0_1
$data['direction'] == 0 ? (int)$prices['buy'] : (int)$prices['sell'], //Mazaneh
1, //MazanehIsMesghalOrGeram_0_1
1, //IsMotefaregheOrAbshode_0_1
$data['direction'] == 0 ? (int)$prices['buy'] * $amount : (int)$prices['sell'] * $amount, //MablaghKol
$data['direction'] == 0 ? "خرید آبشده" : "فروش آبشده",//Sharh
"Code_Factor",
"", //HavalehBe_Mcode
"", //MultiRadif
0, //Jens_Felez
(string)$date,
]
2026-03-14 16:21:42 +00:00
])->json();
} catch (Exception $e) {
return $e->getMessage();
}
if ($response['OK']) {
2026-03-14 16:21:42 +00:00
Tahesab::where('id', $tahesab->id)->update([
"sh_factor" => $response['Sh_factor'],
"tahesab_id" => $response['OK'],
"factor_number" => $response['Factor_Number'],
"tah_id" => $response['ID'],
"data" => json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
"res" => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
]);
return $data;
}
return false;
}
2026-03-14 09:57:15 +03:30
private function create_user($customer_id)
{
$customer = Customer::findOrFail($customer_id);
if ($customer && ($customer->moshtari_code == null)) {
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'), [
"DoNewMoshtari" => [
(string)$customer->name,
$this->Config('name_fa'),
(string)$customer->mobile,
"",
"",
"",
"",
"",
-1,
0
]
//{"DoNewMoshtari":["Name", "GroupName", "Tel", "Address", "CodeMeli", "Tarikh_Tavalod", "Moaref", "Code_Moaref", Moshtari_Code, Jens_Felez]}
]);
if (!empty($response['OK'])) {
$customer->moshtari_code = $response['OK'];
$customer->save();
return $response['OK'];
}
} catch (\Exception $e) {
return $e->getMessage();
}
}else {
if($customer && $customer->birth_date !== null){
$birth_date = Verta::instance(Carbon::parse($customer->birth_date))->format('Y-m-d');
}
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'), [
"DoEditMoshtari" => [
$customer->name,
$this->Config('name_fa'),
$customer->mobile,
"",
$customer->n_code ?? "",
$birth_date ?? "",
"",
"",
(int)$customer->moshtari_code,
0 // tala
]
//{"DoEditMoshtari":["Name","GroupName", "Tel", "Address", "CodeMeli", "Tarikh_Tavalod", "Moaref", "Code_Moaref", Moshtari_Code]}
]);
} catch (\Exception $e) {
return response($e->getMessage(), 500);
}
}
if (empty($response['OK'])) {
return "tahesab api error!!!";
}
return true;
}
2026-01-24 14:03:11 +00:00
}