main
This commit is contained in:
+220
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Models\Tahesab;
|
||||
use App\Models\Customer;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
|
||||
|
||||
class TahesabController extends Controller
|
||||
{
|
||||
public function store()
|
||||
{
|
||||
$customer = auth()->user();
|
||||
if (!$customer) {
|
||||
throw new UnprocessableEntityHttpException("Auth needed");
|
||||
}
|
||||
$customer = Customer::where('id', $customer->id)->first();
|
||||
|
||||
if ($customer && ($customer->moshtari_code !== null)) {
|
||||
return "customer exists";
|
||||
}
|
||||
try {
|
||||
$response = Http::withoutVerifying()->timeout(10)->withHeaders([
|
||||
'DBName' => $this->Config('tahesab_db'),
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => "Bearer " . $this->Config('tahesab_token'),
|
||||
])->post('https://185.252.86.194:8081', [
|
||||
"DoNewMoshtari" => [
|
||||
$customer->name,
|
||||
$this->Config('name_fa'),
|
||||
$customer->mobile,
|
||||
"",
|
||||
$customer->n_code ?? "",
|
||||
$customer->birth_date ?? "", // !== null ? jalalian::fromCarbon(Carbon::parse($customer->birth_date))->format('Y/m/d') : null,
|
||||
"",
|
||||
"",
|
||||
-1,
|
||||
0 // tala
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
if (empty($response['OK'])) {
|
||||
return "tahesab api error!!!";
|
||||
}
|
||||
|
||||
$customer->moshtari_code = $response['OK'];
|
||||
$customer->save();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public function storeTrade($input = null)
|
||||
{
|
||||
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->store();
|
||||
}
|
||||
$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,
|
||||
"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('https://185.252.86.194:8081', [
|
||||
"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']),
|
||||
]
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
if ($response['OK']) {
|
||||
$tahesab->sh_factor = $response['Sh_factor'];
|
||||
$tahesab->tahesab_id = $response['OK'];
|
||||
$tahesab->factor_number = $response['Factor_Number'];
|
||||
$tahesab->tah_id = $response['ID'];
|
||||
$tahesab->res = $response;
|
||||
$tahesab->data = $data;
|
||||
$tahesab->save();
|
||||
return $data;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// directon: 0 = daryaft az moshtari
|
||||
// directon: 1 = pardakht be moshtari
|
||||
public function wallet($input = null)
|
||||
{
|
||||
|
||||
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->store();
|
||||
}
|
||||
$tahesab = Tahesab::create([
|
||||
"customer_id" => $customer->id,
|
||||
"desc" => $data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب",
|
||||
"amount" => $data['value'],
|
||||
"date" => Carbon::now(),
|
||||
"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('https://185.252.86.194:8081', [
|
||||
"DoNewSanadVKHBank" => [
|
||||
1,//Sabte_Kol_Or_Movaghat_1_0
|
||||
$customer->moshtari_code,//Moshtari_Code
|
||||
-1,//Factor_Number
|
||||
1,//Radif_Number
|
||||
1404,//Shamsi_Year
|
||||
10,//Shamsi_Month
|
||||
18,//Shamsi_Day
|
||||
$data['direction'],//IsDayaftAzMoshtariOrPardakhtBeMoshtari_0_1
|
||||
(int)$data['value'],//Mablagh
|
||||
$data['direction'] == 0 ? "شارژ حساب" : "برداشت از حساب",//Sharh
|
||||
"test",
|
||||
"$tahesab->id",//Code_Rahgiri
|
||||
-1,//Factor_Code
|
||||
-1,
|
||||
//MultiRadif
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
if ($response['OK']) {
|
||||
$tahesab->sh_factor = $response['Sh_factor'];
|
||||
$tahesab->tahesab_id = $response['OK'];
|
||||
$tahesab->save();
|
||||
return $data;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user