400 lines
15 KiB
PHP
400 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Site\ReportController;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use App\Models\Tahesab;
|
|
use App\Models\Customer;
|
|
use Morilog\Jalali\Jalalian;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
use Hekmatinasser\Verta\Verta;
|
|
|
|
|
|
class TahesabController extends Controller
|
|
{
|
|
|
|
public function storeTrade($data)
|
|
{
|
|
// 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']) || !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)) {
|
|
$this->create_user($customer->id);
|
|
$customer = Customer::where('id', $data['customer_id'])->first();
|
|
}
|
|
$date = Verta::instance(Carbon::parse($data['pay_at']))->format('Y-m-d');
|
|
[$year, $month, $day] = explode('-', $date);
|
|
|
|
$amount = $data['amount'];
|
|
|
|
$tahesab = Tahesab::create([
|
|
"customer_id" => $customer->id,
|
|
"desc" => $data['direction'] == 1 ? "خرید آبشده" : "فروش آبشده",
|
|
"amount" => $amount,
|
|
"order_id" => $data['order']['id'] ?? null,
|
|
"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'),
|
|
])->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
|
|
$data['product']['metal_id'] == 1 ? 750 : "", //Ayar
|
|
0,
|
|
"",
|
|
$data['direction'] == 1 ? 1 : 0, //BuyOrSale_0_1
|
|
(int)$data['price'], //Mazaneh
|
|
1, //MazanehIsMesghalOrGeram_0_1
|
|
1, //IsMotefaregheOrAbshode_0_1
|
|
(int)$data['total_price'], //MablaghKol
|
|
$data['direction'] == 1 ? "خرید آبشده" : "فروش آبشده", //sharh
|
|
"Code_Factor",
|
|
"", //HavalehBe_Mcode
|
|
"", //MultiRadif
|
|
$data['product']['metal_id'] == 1 ? 0 : 2, //Jens_Felez
|
|
(string)Verta::instance($data['pay_at']),
|
|
]
|
|
])->json();
|
|
} 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),
|
|
]);
|
|
|
|
return $data;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
// directon: 0 = daryaft az moshtari
|
|
// directon: 1 = pardakht be moshtari
|
|
public function wallet($data)
|
|
{
|
|
|
|
// if ($input instanceof \Illuminate\Http\Request) {
|
|
// $data = $input->all();
|
|
// } elseif (is_array($input)) {
|
|
// $data = $input;
|
|
// } else {
|
|
// $data = request()->all();
|
|
// }
|
|
|
|
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();
|
|
|
|
if ($customer && ($customer->moshtari_code === null)) {
|
|
$this->create_user($customer->id);
|
|
$customer = Customer::where('id', $data['customer_id'])->first();
|
|
}
|
|
$tahesab = Tahesab::create([
|
|
"customer_id" => $customer->id,
|
|
"desc" => $data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب",
|
|
"amount" => $data['value'],
|
|
"type" => "ريال",
|
|
"date" => Carbon::now(),
|
|
"created_at" => Carbon::now(),
|
|
]);
|
|
|
|
$date = Verta::instance(Carbon::now())->format('Y-m-d');
|
|
[$year, $month, $day] = explode('-', $date);
|
|
|
|
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'), [
|
|
"DoNewSanadVKHBank" => [
|
|
1,//Sabte_Kol_Or_Movaghat_1_0
|
|
$customer->moshtari_code,//Moshtari_Code
|
|
-1,//Factor_Number
|
|
1,//Radif_Number
|
|
(int)$year,//Shamsi_Year
|
|
(int)$month,//Shamsi_Month
|
|
(int)$day,//Shamsi_Day
|
|
(int)$data['direction'],//IsDayaftAzMoshtariOrPardakhtBeMoshtari_0_1
|
|
(int)$data['value'],//Mablagh
|
|
$data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب",//Sharh
|
|
(string)$this->Config('tahesab_bank_account'), //Name_Bank
|
|
"$tahesab->id",//Code_Rahgiri
|
|
-1,//Factor_Code
|
|
-1,
|
|
//MultiRadif
|
|
]
|
|
])->json();
|
|
} catch (\Exception $e) {
|
|
return $e->getMessage();
|
|
}
|
|
if ($response['OK']) {
|
|
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),
|
|
"res" => json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
|
|
]);
|
|
|
|
return $data;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function walletGold($data)
|
|
{
|
|
// 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 = abs($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,
|
|
]
|
|
])->json();
|
|
} 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),
|
|
]);
|
|
|
|
return $data;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
public function healthCheck()
|
|
{
|
|
$tahesab = Http::withoutVerifying()->withHeaders([
|
|
'Content-Type' => 'application/json',
|
|
'DBName' => $this->Config('tahesab_db'),
|
|
'Authorization' => "Bearer " . $this->Config('tahesab_token'),
|
|
])->post((string)env('TAHESAB'), [
|
|
"CheckHealth" => []
|
|
])->json();
|
|
|
|
return $tahesab;
|
|
// if($tahesab['Api_Status'] === "OK" && $tahesab['DB_Status'] === "OK"){
|
|
// return true;
|
|
// }else{
|
|
// return false;
|
|
// }
|
|
}
|
|
|
|
public function sanadList()
|
|
{
|
|
$tahesab = Http::withoutVerifying()->withHeaders([
|
|
'Content-Type' => 'application/json',
|
|
'DBName' => $this->Config('tahesab_db'),
|
|
'Authorization' => "Bearer " . $this->Config('tahesab_token'),
|
|
])->post((string)env('TAHESAB'), [
|
|
"DoListAsnad" => [
|
|
10, /*Count_Last,*/
|
|
"", /*Moshtari_Code,*/
|
|
"", /*"AzTarikhShamsi"*/
|
|
(string)Verta::instance(Carbon::now()->format('Y-m-d')), /*"TaTarikhShamsi"*/
|
|
"", /*"FilterNoSanad"*/
|
|
0, /*Jens_Felez*/
|
|
]
|
|
])->json();
|
|
return $tahesab;
|
|
|
|
}
|
|
}
|