From d684290848f32da92257f2487802f68298f1f236 Mon Sep 17 00:00:00 2001 From: adonis Date: Sun, 24 May 2026 16:55:07 +0330 Subject: [PATCH] first change for liman --- .../Controllers/Site/ReportController.php | 490 +++++++++++++----- 1 file changed, 360 insertions(+), 130 deletions(-) diff --git a/app/Http/Controllers/Site/ReportController.php b/app/Http/Controllers/Site/ReportController.php index 1153c53..566c52e 100644 --- a/app/Http/Controllers/Site/ReportController.php +++ b/app/Http/Controllers/Site/ReportController.php @@ -113,22 +113,44 @@ class ReportController extends Controller { $closed = false; - if($this->Config('close_price') == 'true'){ - $startDay = $this->Config('startDay'); // خروجی: "11:00" + $goldir_price = 0; + $liman_price = 0; + $tgn = 0; + $pr_price = 0; + + $open_buy = true; + $open_sell = true; + + $liman_open_buy = false; + $liman_open_sell = false; + + $priceSource = null; + + $toBool = function ($value) { + return filter_var($value, FILTER_VALIDATE_BOOLEAN); + }; + + /* + |-------------------------------------------------------------------------- + | بستن قیمت طبق منطق تابع اول + |-------------------------------------------------------------------------- + */ + if ($this->Config('close_price') == 'true') { + $startDay = $this->Config('startDay'); [$startHour, $startMinute] = explode(':', $startDay); - $endDay = $this->Config('endDay'); // خروجی: "11:00" + $endDay = $this->Config('endDay'); [$endHour, $endMinute] = explode(':', $endDay); $current = Carbon::now(); $start = Carbon::today()->setTime((int)$startHour, (int)$startMinute); $end = Carbon::today()->setTime((int)$endHour, (int)$endMinute); - if(!$current->between($start, $end)){ - + if (!$current->between($start, $end)) { Cnt::where('key', 'app_closed')->update(['value' => "true"]); Cnt::where('key', 'goldir_closed')->update(['value' => "true"]); - $liman_price = 0; + Cnt::where('key', 'liman_closed')->update(['value' => "true"]); + return [ 'sell' => 0, 'buy' => 0, @@ -137,173 +159,381 @@ class ReportController extends Controller 'main_product' => 0, 'taban_gohar' => 0, 'goldir' => 0, - 'closed'=> true, - 'time'=>now()->format('H:i:s'), + 'liman' => 0, + 'closed' => true, + 'time' => now()->format('H:i:s'), ]; - }else{ + } else { Cnt::where('key', 'app_closed')->update(['value' => "false"]); Cnt::where('key', 'goldir_closed')->update(['value' => "false"]); + Cnt::where('key', 'liman_closed')->update(['value' => "false"]); } } - $settings = Setting::whereIn('key', ['fee_single', 'goldir_price', 'tabanGohar_price', 'main_product_price'])->get(); + /* + |-------------------------------------------------------------------------- + | تنظیمات + |-------------------------------------------------------------------------- + */ + $settings = Setting::whereIn('key', [ + 'fee_single', + 'goldir_price', + 'tabanGohar_price', + 'main_product_price', + 'buy_fee', + 'sell_fee', + ])->get(); + $settingsMap = $settings->pluck('value', 'key'); + $taban_gohar_price = $settingsMap->get('tabanGohar_price'); - $difference = $settingsMap->get('fee_single'); + $difference = (float) $settingsMap->get('fee_single', 0); $gold_ir_price = $settingsMap->get('goldir_price'); $main_product_price = $settingsMap->get('main_product_price'); - $open_buy = true; - $open_sell = true; - if($gold_ir_price == 'true' && !$closed){ + $diff_buy = (float) $settingsMap->get('buy_fee', 0); + $diff_sell = (float) $settingsMap->get('sell_fee', 0); - $goldir_price = 0; - $goldir_base_url=$this->Config('goldir_base_url')??'https://gold.ir'; - try{ - $goldir = Http::timeout(7)->withOptions([ + // این همون کلیدیه که تو عکس فرستادی + $liman_price_enabled = $toBool($this->Config('liman_price')); + + /* + |-------------------------------------------------------------------------- + | Liman - اولویت اول + |-------------------------------------------------------------------------- + */ + if ($liman_price_enabled && !$closed) { + try { + $liman = Http::timeout(7) + ->withOptions([ + 'curl' => [ + CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, + CURLOPT_TCP_FASTOPEN => 1, + ] + ]) + ->withToken(Cnt::where('key', 'liman')->value('value')) + ->get('http://212.23.201.173/server/customer/product/list') + ->json(); + + if ((isset($liman['message']) && $liman['message'] == 'no authorised') || $liman == null) { + $cnt = new Cnt(); + $cnt->liman_login(); + + $liman = Http::timeout(2) + ->withToken(Cnt::where('key', 'liman')->value('value')) + ->get('http://212.23.201.173/server/customer/product/list') + ->json(); + } + + $collection = collect($liman); + $lpLiman = $collection->firstWhere('id', 25); + + if ($lpLiman && isset($lpLiman['sub_agent'])) { + $buy = (float) $lpLiman['sub_agent']['rate_buy_gram_price']; + $sell = (float) $lpLiman['sub_agent']['rate_sell_gram_price']; + + $customPrice = []; + $customPrice['price'] = ($buy + $sell) / 2; + $customPrice['diff'] = ($buy - $sell) / 2; + + Cnt::where('key', 'liman_price')->update(['value' => (float) $customPrice['price']]); + Cnt::where('key', 'liman_diff')->update(['value' => (float) $customPrice['diff']]); + + Cnt::where('key', 'liman_mazaneh')->update([ + 'value' => ((float) $customPrice['price'] * 4.331802) + ]); + + Cnt::where('key', 'liman_truly_buy_mazaneh')->update([ + 'value' => $lpLiman['sub_agent']['rate_buy_price'] + ]); + + Cnt::where('key', 'liman_truly_sell_mazaneh')->update([ + 'value' => $lpLiman['sub_agent']['rate_sell_price'] + ]); + + Cnt::where('key', 'log')->update([ + 'value' => json_encode($lpLiman, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) + ]); + + $liman_open_sell = $toBool($lpLiman['open_sell'] ?? false); + $liman_open_buy = $toBool($lpLiman['open_buy'] ?? false); + + Cnt::where('key', 'liman_open_sell')->update([ + 'value' => $liman_open_sell ? "true" : "false" + ]); + + Cnt::where('key', 'liman_open_buy')->update([ + 'value' => $liman_open_buy ? "true" : "false" + ]); + + if ($liman_open_sell || $liman_open_buy) { + $liman_price = (int) $customPrice['price']; + } + } + + Cnt::where('key', 'liman_closed')->update([ + 'value' => $liman_price == 0 ? "true" : "false" + ]); + } catch (\Exception $e) { + $liman_price = 0; + + Cnt::where('key', 'liman_closed')->update([ + 'value' => "true" + ]); + } + } + + /* + |-------------------------------------------------------------------------- + | Goldir + |-------------------------------------------------------------------------- + */ + if ($gold_ir_price == 'true' && !$closed) { + try { + $goldir = Http::timeout(7) + ->withOptions([ + 'curl' => [ + CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, + CURLOPT_TCP_FASTOPEN => 1, + ] + ]) + ->withoutVerifying() + ->withToken(Cnt::where('key', 'goldir')->value('value')) + ->get('https://gold.ir/server/customer/product/list') + ->json(); + + if ((isset($goldir['message']) && $goldir['message'] == 'no authorised') || $goldir == null) { + $cnt = new Cnt(); + + $goldir = Http::timeout(2) + ->withToken($cnt->goldir_login()) + ->get('https://gold.ir/server/customer/product/list') + ->json(); + } + + $collection = collect($goldir); + $lpGoldir = $collection->firstWhere('id', 40); + + if ($lpGoldir && isset($lpGoldir['sub_agent'])) { + $buy = (float) $lpGoldir['sub_agent']['rate_buy_gram_price']; + $sell = (float) $lpGoldir['sub_agent']['rate_sell_gram_price']; + + $customPrice = []; + $customPrice['price'] = (ceil($buy) + ceil($sell)) / 2; + $customPrice['diff'] = (ceil($buy) - ceil($sell)) / 2; + + Cnt::where('key', 'goldir_price')->update(['value' => (float) $customPrice['price']]); + Cnt::where('key', 'goldir_diff')->update(['value' => (float) $customPrice['diff']]); + + Cnt::where('key', 'goldir_mazaneh')->update([ + 'value' => ((float) $customPrice['price'] * 4.331802) + ]); + + Cnt::where('key', 'goldir_truly_buy_mazaneh')->update([ + 'value' => $lpGoldir['sub_agent']['rate_buy_price'] + ]); + + Cnt::where('key', 'goldir_truly_sell_mazaneh')->update([ + 'value' => $lpGoldir['sub_agent']['rate_sell_price'] + ]); + + Cnt::where('key', 'log')->update([ + 'value' => json_encode($goldir, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) + ]); + + $open_sell = $toBool($lpGoldir['open_sell'] ?? false); + $open_buy = $toBool($lpGoldir['open_buy'] ?? false); + + Cnt::where('key', 'goldir_open_sell')->update([ + 'value' => $open_sell ? "true" : "false" + ]); + + Cnt::where('key', 'goldir_open_buy')->update([ + 'value' => $open_buy ? "true" : "false" + ]); + + if ($open_sell || $open_buy) { + $goldir_price = (int) $customPrice['price']; + } + } + + Cnt::where('key', 'goldir_closed')->update([ + 'value' => $goldir_price == 0 ? "true" : "false" + ]); + } catch (\Exception $e) { + $goldir_price = 0; + + Cnt::where('key', 'goldir_closed')->update([ + 'value' => "true" + ]); + } + } + + /* + |-------------------------------------------------------------------------- + | Main Product + |-------------------------------------------------------------------------- + */ + if ($main_product_price == 'true' && !$closed) { + $main_product_id = Cnt::where('key', 'main_product')->value('value'); + $pr_product = Product::find($main_product_id); + + if ($pr_product) { + $pr_price = (float) $pr_product['price'] + (float) $pr_product['price_profit']; + } + } + + /* + |-------------------------------------------------------------------------- + | Taban Gohar + |-------------------------------------------------------------------------- + */ + if ($taban_gohar_price == 'true' && !$closed) { + try { + $taban_gohar = Http::withOptions([ 'curl' => [ CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, CURLOPT_TCP_FASTOPEN => 1, ] ]) - ->withoutVerifying() // غیرفعال کردن بررسی SSL (برای تست سرعت) - ->withToken(Cnt::where('key', 'goldir')->first()['value']) - ->get($goldir_base_url.'/server/customer/product/list')->json(); + ->withoutVerifying() + ->timeout(2) + ->get('https://tabangohar.com/GheymatKhan/prices_in_table.html') + ->json(); - if((isset($goldir['message']) && $goldir['message'] == 'no authorised') || $goldir == null){ - $cnt = new Cnt(); - $goldir = Http::timeout(2)->withToken($cnt->goldir_login()) - ->get($goldir_base_url.'/server/customer/product/list')->json(); - } - $customPrice= []; - $collection = collect($goldir); - $lp = $collection->firstWhere('id', $this->Config('hamkar_product_id')); - -// foreach ($goldir as $lp) { -// if ($lp['id'] == 40) { - - $buy = $lp['sub_agent']['rate_buy_gram_price']; - $sell = $lp['sub_agent']['rate_sell_gram_price']; - - $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 - - 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"]); - - $open_buy = $lp['open_buy']; - $open_sell = $lp['open_sell']; - - if ($lp['open_sell'] == 'true' || $lp['open_buy'] == 'true') { - $goldir_price = (int)$customPrice['price']; - } -// } -// } - if($goldir_price == 0){ - Cnt::where('key', 'goldir_closed')->update(['value' => "true"]); - }else{ - Cnt::where('key', 'goldir_closed')->update(['value' => "false"]); - } - } catch (\Exception $e) { - echo $e->getMessage(); - $goldir_price = 0;} - } - - 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; + $tgn = (int) $taban_gohar['c'] * 10; } catch (\Exception $e) { $tgn = 0; } } - // try { -// $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;} -// $refpricer = new ProductController(); -// if ($refpricer->getPrice()) { -// $ref = true; -// } else { -// $ref = false; -// } - - if($gold_ir_price == 'true' && ($goldir_price??0) !== 0 && !$closed) { - $priceValue = (int)$goldir_price; - }elseif($taban_gohar_price == 'true' && ($tgn??0) !== 0 && !$closed){ + /* + |-------------------------------------------------------------------------- + | انتخاب قیمت اصلی + | اولویت جدید: + | Liman -> Goldir -> Taban Gohar -> Main Product + |-------------------------------------------------------------------------- + */ + if ($liman_price_enabled && ($liman_price ?? 0) !== 0 && !$closed) { + $priceValue = (int) $liman_price; + $priceSource = 'liman'; + } elseif ($gold_ir_price == 'true' && ($goldir_price ?? 0) !== 0 && !$closed) { + $priceValue = (int) $goldir_price; + $priceSource = 'goldir'; + } elseif ($taban_gohar_price == 'true' && ($tgn ?? 0) !== 0 && !$closed) { $priceValue = $tgn; - }elseif($main_product_price == 'true' && ($pr_price??0) !== 0 && !$closed){ + $priceSource = 'taban_gohar'; + } elseif ($main_product_price == 'true' && ($pr_price ?? 0) !== 0 && !$closed) { $priceValue = $pr_price; - }else{ + $priceSource = 'main_product'; + } else { $priceValue = 0; } + + /* + |-------------------------------------------------------------------------- + | ذخیره قیمت + |-------------------------------------------------------------------------- + */ $lastPrice = Price::latest()->first(); - if (!$closed && (($lastPrice || $lastPrice->price !== $priceValue) && $priceValue > 30000000)) { + + if ( + !$closed && + (!$lastPrice || (float) $lastPrice->price != (float) $priceValue) && + $priceValue > 30000000 + ) { $price = new Price(); - $price->price = $priceValue; // Assign the cleaned price value + $price->price = $priceValue; $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 = 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{ + + 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") + "start" => $priceValue, + "end" => $priceValue, + "high" => $priceValue, + "low" => $priceValue, + "date_at" => date("Y-m-d") ]); } } + $price = Price::orderBy('id', 'desc')->first(); - if ($price) { - $priceValue=$price->price; - if($this->Config('goldir_price') == 'true' && $goldir_price !==0){ - $goldir_profit = Cnt::where('key', 'goldir_diff')->first()['value']; - $difference = $goldir_profit; - } - 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, - 'price' => $priceValue, - 'main_product' => $pr_price ?? 0, - 'taban_gohar' => $tgn ?? 0, - 'goldir' => $goldir_price ?? 0, - 'closed'=>$closed ?? null, - 'time'=>now(), - ]; - } else { + + if (!$price) { return []; } + $priceValue = $price->price; + + /* + |-------------------------------------------------------------------------- + | محاسبه sell / buy با حفظ ساختار خروجی تابع اول + |-------------------------------------------------------------------------- + */ + if ($priceSource === 'liman' && $liman_price !== 0) { + $liman_profit = (float) Cnt::where('key', 'liman_diff')->value('value'); + + $final_buy_diff = $diff_buy + $liman_profit; + $final_sell_diff = $diff_sell + $liman_profit; + + $sell = $priceValue !== 0 + ? ($liman_open_sell ? ceil($price->price - $final_sell_diff) : 0) + : 0; + + $buy = $priceValue !== 0 + ? ($liman_open_buy ? ceil($price->price + $final_buy_diff) : 0) + : 0; + + $priceLast = $priceValue !== 0 + ? ceil($price->price + $final_buy_diff) + : 0; + } else { + if ($priceSource === 'goldir' && $goldir_price !== 0) { + $goldir_profit = (float) Cnt::where('key', 'goldir_diff')->value('value'); + $difference = $goldir_profit; + } + + $sell = $priceValue !== 0 + ? ($open_sell ? ceil($price->price - $difference) : 0) + : 0; + + $buy = $priceValue !== 0 + ? ($open_buy ? ceil($price->price + $difference) : 0) + : 0; + + $priceLast = $priceValue !== 0 + ? ceil($price->price + $difference) + : 0; + } + + return [ + 'sell' => $sell, + 'buy' => $buy, + 'price_last' => $priceLast, + 'price' => $priceValue, + 'main_product' => $pr_price ?? 0, + 'taban_gohar' => $tgn ?? 0, + 'goldir' => $goldir_price ?? 0, + 'liman' => $liman_price ?? 0, + 'closed' => $closed ?? null, + 'time' => now(), + ]; } public function check()