Files
boursecu_server/app/Http/Controllers/Site/ReportController.php
T

382 lines
15 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\Http\Controllers\Site;
use App\Http\Controllers\Admin\PriceController;
use App\Http\Controllers\Controller;
use App\Mail\YourMailableClass;
use App\Models\Cnt;
use App\Models\Setting;
2026-01-24 14:03:11 +00:00
use App\Models\ConfigStatic;
use App\Models\Customer;
use App\Models\Price;
use App\Models\PriceCandle;
use App\Models\PriceTrigger;
use App\Models\Product;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Morilog\Jalali\Jalalian;
2026-02-10 12:29:57 +03:30
use App\Http\Controllers\Admin\ProductController;
2026-01-24 14:03:11 +00:00
2026-01-24 14:03:11 +00:00
class ReportController extends Controller
{
public function candleChart()
{
$candle = PriceCandle::select(["start",
"high",
"low",
"end",
"date_at"])->whereDate('date_at', '<=', date("Y-m-d"))
->whereDate('date_at', '>', Carbon::now()->subDays(30)->format("Y-m-d"))
->get()->toArray();
$a = [];
foreach ($candle as $c) {
$create['date'] = $c["date_at"];
unset($c["date_at"]);
$create['candle'] = array_values($c);
$a[] = array_values($create);
}
return response()->json($a);
}
public function updatePrise()
{
// $difference = config('fee.single');
$difference = Setting::where('key', 'fee_single')->first()->value;
2026-01-24 14:03:11 +00:00
$lastPrice = Price::latest()->first();
$PriceTriggers = PriceTrigger::where("is_active", true)->get();
foreach ($PriceTriggers as $PriceTrigger) {
$priseGold = $PriceTrigger->dirction == 1
? $lastPrice->price - $difference
: ($PriceTrigger->dirction == 2 ? $lastPrice->price + $difference : $lastPrice->price);
if (eval('return $priseGold ' . $PriceTrigger->price_condition . ' $PriceTrigger->target_price;')) {
$customer = Customer::find($PriceTrigger->customer_id);
$message = "قیمت طلا به " . number_format($priseGold, -1) . " در هر گرم رسید\n" .$this->Config('name_fa');
if ($PriceTrigger->action == 'email') {
Mail::to("$customer->email")->send(new YourMailableClass($message, '', '', '', ''));
} elseif ($PriceTrigger->action == 'sms') {
$this->send($customer->mobile, $message);
}
$PriceTrigger->is_active = false;
$PriceTrigger->triggered_at = Carbon::now();
$PriceTrigger->save();
}
}
return response()->json();
}
public function upDownPrice()
{
$difference = Setting::where('key', 'fee_single')->first()->value;
2026-01-24 14:03:11 +00:00
$today = Carbon::now()->format("Y-m-d");
$prises = Price::where('date_at', $today)->get("price");
$minP = 0; // Initialize to the highest possible value
$maxP = 0; // Initialize to the lowest possible value
foreach ($prises as $p) {
// Initialize $minP to the first price if it's null
if ($minP == 0 || $p->price < $minP) {
$minP = $p->price;
}
// Initialize $maxP to the first price if it's null
if ($maxP == 0 || $p->price > $maxP) {
$maxP = $p->price;
}
}
if (count($prises) === 0) {
$price = Price::orderBy('id', 'desc')->first();
$priceValue = $price->price;
2026-01-24 14:03:11 +00:00
return ['sell' => $priceValue - $difference, 'buy' => $priceValue + $difference];
2026-01-24 14:03:11 +00:00
} else {
// Assuming $minP and $maxP are defined elsewhere in your code
return ['sell' => $minP - $difference, 'buy' => $maxP + $difference];
}
// return $prises;
// $add_sell_price = config('add_sell_price.major');
// $price = PriceCandle::whereDate('date_at',)->first();
// $price_price = Price::orderBy('id','desc')->first();
}
public function lastPrice()
{
$closed = false;
if($this->Config('close_price') == 'true'){
2026-05-11 17:36:23 +00:00
$startDay = $this->Config('startDay'); // خروجی: "11:00"
[$startHour, $startMinute] = explode(':', $startDay);
$endDay = $this->Config('endDay'); // خروجی: "11:00"
[$endHour, $endMinute] = explode(':', $endDay);
$current = Carbon::now();
2026-05-11 17:36:23 +00:00
$start = Carbon::today()->setTime((int)$startHour, (int)$startMinute);
$end = Carbon::today()->setTime((int)$endHour, (int)$endMinute);
if(!$current->between($start, $end)){
Cnt::where('key', 'app_closed')->update(['value' => "true"]);
Cnt::where('key', 'goldir_closed')->update(['value' => "true"]);
$liman_price = 0;
return [
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => 0,
'taban_gohar' => 0,
'goldir' => 0,
'closed'=> true,
'time'=>now()->format('H:i:s'),
];
}else{
Cnt::where('key', 'app_closed')->update(['value' => "false"]);
Cnt::where('key', 'goldir_closed')->update(['value' => "false"]);
}
}
2026-05-11 17:36:23 +00:00
$settings = Setting::whereIn('key', ['fee_single', 'goldir_price', 'tabanGohar_price', 'main_product_price'])->get();
$settingsMap = $settings->pluck('value', 'key');
$taban_gohar_price = $settingsMap->get('tabanGohar_price');
$difference = $settingsMap->get('fee_single');
$gold_ir_price = $settingsMap->get('goldir_price');
$main_product_price = $settingsMap->get('main_product_price');
2026-01-24 14:03:11 +00:00
$open_buy = true;
$open_sell = true;
if($gold_ir_price == 'true' && !$closed){
2026-05-17 16:27:50 +00:00
2026-03-10 15:39:30 +03:30
$goldir_price = 0;
2026-05-17 16:27:50 +00:00
$goldir_base_url=$this->Config('goldir_base_url')??'https://gold.ir';
2026-03-10 15:39:30 +03:30
try{
$goldir = Http::timeout(7)->withOptions([
2026-03-12 10:47:22 +00:00
'curl' => [
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_TCP_FASTOPEN => 1,
]
])
->withoutVerifying() // غیرفعال کردن بررسی SSL (برای تست سرعت)
->withToken(Cnt::where('key', 'goldir')->first()['value'])
2026-05-17 16:27:50 +00:00
->get($goldir_base_url.'/server/customer/product/list')->json();
2026-03-10 15:39:30 +03:30
if((isset($goldir['message']) && $goldir['message'] == 'no authorised') || $goldir == null){
$cnt = new Cnt();
$goldir = Http::timeout(2)->withToken($cnt->goldir_login())
2026-05-17 16:27:50 +00:00
->get($goldir_base_url.'/server/customer/product/list')->json();
2026-03-10 15:39:30 +03:30
}
$customPrice= [];
$collection = collect($goldir);
2026-05-17 16:27:50 +00:00
$lp = $collection->firstWhere('id', $this->Config('hamkar_product_id'));
2026-03-10 15:39:30 +03:30
// foreach ($goldir as $lp) {
// if ($lp['id'] == 40) {
2026-03-10 15:39:30 +03:30
$buy = $lp['sub_agent']['rate_buy_gram_price'];
$sell = $lp['sub_agent']['rate_sell_gram_price'];
2026-03-10 15:39:30 +03:30
$customPrice['price'] = (ceil($buy) + ceil($sell)) / 2;
Cnt::where('key', 'goldir_price')->update(['value' => (float)$customPrice['price']]);//was float
$customPrice['diff'] = (ceil($buy) - ceil($sell)) / 2;
Cnt::where('key', 'goldir_diff')->update(['value' => (float)$customPrice['diff']]);//was float
2026-03-10 15:39:30 +03:30
Cnt::where('key', 'goldir_mazaneh')->update(['value' => ((float)$customPrice['price'] * 4.331802)]);
Cnt::where('key', 'goldir_truly_buy_mazaneh')->update(['value' => $lp['sub_agent']['rate_buy_price']]);
Cnt::where('key', 'goldir_truly_sell_mazaneh')->update(['value' => $lp['sub_agent']['rate_sell_price']]);
Cnt::where('key', 'log')->update(['value' => json_encode($goldir, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]);
Cnt::where('key', 'goldir_open_sell')->update(['value' => $lp['open_sell']? "true":"false"]);
Cnt::where('key', 'goldir_open_buy')->update(['value' => $lp['open_buy']? "true":"false"]);
2026-03-10 15:39:30 +03:30
$open_buy = $lp['open_buy'];
$open_sell = $lp['open_sell'];
if ($lp['open_sell'] == 'true' || $lp['open_buy'] == 'true') {
$goldir_price = (int)$customPrice['price'];
2026-03-10 15:39:30 +03:30
}
// }
// }
2026-03-10 15:39:30 +03:30
if($goldir_price == 0){
Cnt::where('key', 'goldir_closed')->update(['value' => "true"]);
}else{
Cnt::where('key', 'goldir_closed')->update(['value' => "false"]);
}
2026-05-17 16:27:50 +00:00
} catch (\Exception $e) {
echo $e->getMessage();
$goldir_price = 0;}
2026-03-10 15:39:30 +03:30
}
2026-01-24 14:03:11 +00:00
if($main_product_price == 'true' && !$closed){
$main_product_id = Cnt::where('key', 'main_product')->first()['value'];
$pr_product = Product::find($main_product_id);
$pr_price = $pr_product['price'] + $pr_product['price_profit'];
}
if($taban_gohar_price == 'true' && !$closed) {
try {
$taban_gohar = Http::withOptions([
'curl' => [
// اجبار به استفاده از IPv4 و جلوگیری از تاخیر IPv6
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
// افزایش سرعت برقراری ارتباط TCP
CURLOPT_TCP_FASTOPEN => 1,
]
])->withoutVerifying()->timeout(2)->get('https://tabangohar.com/GheymatKhan/prices_in_table.html')->json();
$tgn = (int)$taban_gohar['c'] * 10;
} catch (\Exception $e) {
$tgn = 0;
}
}
// try {
2026-01-24 14:03:11 +00:00
// $tala_ir = (int)str_replace(',', '', Http::timeout(2)->get('https://www.tala.ir/ajax/price')->json()['gold']['gold_18k']['v']) * 10;
// } catch (\Exception $e) {$tala_ir = 0;}
2026-03-12 10:47:22 +00:00
// $refpricer = new ProductController();
2026-02-16 12:16:11 +00:00
// if ($refpricer->getPrice()) {
// $ref = true;
// } else {
// $ref = false;
// }
2026-02-05 06:14:14 +00:00
if($gold_ir_price == 'true' && ($goldir_price??0) !== 0 && !$closed) {
2026-03-10 15:39:30 +03:30
$priceValue = (int)$goldir_price;
}elseif($taban_gohar_price == 'true' && ($tgn??0) !== 0 && !$closed){
$priceValue = $tgn;
}elseif($main_product_price == 'true' && ($pr_price??0) !== 0 && !$closed){
$priceValue = $pr_price;
2026-03-10 15:39:30 +03:30
}else{
$priceValue = 0;
2026-03-10 15:39:30 +03:30
}
2026-01-24 14:03:11 +00:00
$lastPrice = Price::latest()->first();
if (!$closed && (($lastPrice || $lastPrice->price !== $priceValue) && $priceValue > 30000000)) {
2026-01-24 14:03:11 +00:00
$price = new Price();
$price->price = $priceValue; // Assign the cleaned price value
$price->time_at = Carbon::now()->toTimeString();
$price->date_at = Carbon::now()->toDateString();
$price->save();
$this->updatePrise();
$candle = PriceCandle::whereDate('date_at',date("Y-m-d"))->first();
if (!is_null($candle)){
$candle->end = $priceValue;
if ($priceValue < $candle->low){
$candle->low = $priceValue;}
elseif ($priceValue > $candle->high){
$candle->high = $priceValue;}$candle->save();
}else{
PriceCandle::create([
"start"=>$priceValue,
"end"=>$priceValue,
"high"=>$priceValue,
"low"=>$priceValue,
"date_at"=>date("Y-m-d")
]);
}
}
$price = Price::orderBy('id', 'desc')->first();
if ($price) {
2026-05-13 06:55:42 +00:00
$priceValue=$price->price;
if($this->Config('goldir_price') == 'true' && $goldir_price !==0){
$goldir_profit = Cnt::where('key', 'goldir_diff')->first()['value'];
$difference = $goldir_profit;
}
2026-01-24 14:03:11 +00:00
return [
'sell' => $priceValue !== 0 ? $open_sell ? ceil($price->price - $difference) : 0 : 0,
'buy' => $priceValue !== 0 ? $open_buy ? ceil($price->price + $difference) : 0 : 0,
'price_last' => $priceValue !== 0 ? ceil($price->price + $difference) : 0,
2026-01-24 14:03:11 +00:00
'price' => $priceValue,
'main_product' => $pr_price ?? 0,
2026-03-10 12:32:55 +00:00
'taban_gohar' => $tgn ?? 0,
'goldir' => $goldir_price ?? 0,
'closed'=>$closed ?? null,
'time'=>now(),
2026-01-24 14:03:11 +00:00
];
} else {
return [];
}
}
public function check()
{
$service = Http::
withHeaders([
'accept' => 'application/json',
'Authorization' => 'Bearer ohxsrbjagqvbt0glaygt'
])->get('https://studio.persianapi.com/index.php/web-service/common/gold-currency-coin?format=json&limit=30&page=1')
->json();
$list = [];
if (isset($service['result'])) {
foreach ($service['result'] as $item) {
if ($item['key'] == 137120 || $item['key'] == 137119) {
$list[] = $item;
if ($item['key'] == 137120) {
(new PriceController())->store(new Request(["price" => $item['price']]));
}
}
}
Cnt::where('key', 'price')->update(['value_json' => $list]);
}
}
public function test()
{
$a = '{{12, 23, {4, 0, {1}, {1}}}, 0, {1}}';
$b = str_split($a);
$c = [];
$str = "";
foreach ($b as $item) {
if ($item != " ") {
print_r($item);
print_r('++');
if ($item == ',' || $item == '}') {
if (!in_array($c[count($c) - 1], ['{', '}'])) {
$c[count($c) - 1] += (int)$str;
print_r((int)$str);
print_r('^^');
} else {
$c[] = $str;
print_r($c);
print_r('&&');
}
$str = "";
} elseif (in_array($item, ['{', '}'])) {
$c[] = $item;
print_r($c);
print_r('**');
} else {
$str .= $item;
print_r($str);
print_r('@@');
}
}
}
dd($c);
$d = [];
$e = [];
foreach ($c as $item) {
if ($item == '}') {
print_r($d[count($d) - 1]);
print_r('***');
$e[] = $d[count($d) - 1];
} else {
$d[] = $item;
print_r($d);
print_r('-----');
}
}
dd($e);
}
}