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

730 lines
27 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;
2026-05-25 12:47:04 +03:30
use App\Services\MontzarService;
2026-06-04 14:40:02 +03:30
use App\Services\ZaarboxPriceService;
2026-01-24 14:03:11 +00:00
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\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Morilog\Jalali\Jalalian;
2026-05-25 12:47:04 +03:30
use App\Services\ConfigService;
2026-06-13 17:35:40 +03:30
use App\Services\LivePriceCacheService;
2026-06-11 16:41:28 +03:30
use App\Services\ProductDisplayPriceService;
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()
{
2026-06-10 13:11:51 +03:30
$difference = (float) (Setting::where('key', 'fee_single')->value('value') ?? config('fee.single', 0));
2026-01-24 14:03:11 +00:00
$lastPrice = Price::latest()->first();
2026-06-10 13:11:51 +03:30
if (!$lastPrice) {
return response()->json();
}
2026-01-24 14:03:11 +00:00
$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);
2026-06-10 13:11:51 +03:30
if (!$customer) {
continue;
}
2026-01-24 14:03:11 +00:00
$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()
2026-06-03 21:52:37 +03:30
{
return $this->buildUpDownPrices();
}
protected function buildUpDownPrices(): array
2026-01-24 14:03:11 +00:00
{
2026-06-11 20:40:47 +03:30
$config = app(ConfigService::class);
$diffBuy = $config->displayBuySpreadRial();
$diffSell = $config->displaySellSpreadRial();
2026-06-01 18:52:59 +03:30
2026-06-11 20:40:47 +03:30
$extremes = $config->resolvedDailyBaseExtremes();
$up = $extremes['up'];
$down = $extremes['down'];
2026-06-01 18:46:57 +03:30
2026-06-11 20:40:47 +03:30
if ($up > 0 && $down > 0) {
return [
'sell' => max(0, (int) ceil($down - $diffSell)),
'buy' => (int) ceil($up + $diffBuy),
];
2026-06-01 18:46:57 +03:30
}
2026-06-11 20:40:47 +03:30
if (!$config->getBool('show_last_price', false)) {
return ['sell' => 0, 'buy' => 0];
2026-01-24 14:03:11 +00:00
}
2026-06-01 18:46:57 +03:30
2026-06-11 20:40:47 +03:30
$price = Price::orderByDesc('id')->first();
$priceValue = $price ? (float) $price->price : 0.0;
if ($priceValue <= 0) {
return ['sell' => 0, 'buy' => 0];
}
return [
'sell' => max(0, (int) ceil($priceValue - $diffSell)),
'buy' => (int) ceil($priceValue + $diffBuy),
];
2026-01-24 14:03:11 +00:00
}
2026-06-14 12:33:29 +03:30
protected function appendUpPrices(array $payload, bool $saveFullCache = false): array
2026-06-03 21:52:37 +03:30
{
$payload['up'] = $this->buildUpDownPrices();
2026-06-11 21:37:17 +03:30
$payload['last_historical'] = app(ProductDisplayPriceService::class)->resolveHistoricalBasePrice();
2026-06-11 16:41:28 +03:30
app(ProductDisplayPriceService::class)->cacheLivePricePayload($payload);
2026-06-03 21:52:37 +03:30
2026-06-14 12:33:29 +03:30
if ($saveFullCache || request()->boolean('force_refresh')) {
2026-06-14 13:15:45 +03:30
$cache = app(LivePriceCacheService::class);
$cache->markTick();
2026-06-18 19:32:28 +03:30
return $cache->put($payload);
2026-06-13 17:35:40 +03:30
}
2026-06-03 21:52:37 +03:30
return $payload;
}
2026-06-13 17:35:40 +03:30
public function lastPrice(Request $request)
2026-01-24 14:03:11 +00:00
{
2026-05-25 12:47:04 +03:30
$config = app(ConfigService::class);
2026-06-13 17:35:40 +03:30
$cache = app(LivePriceCacheService::class);
2026-06-14 12:33:29 +03:30
$forceRefresh = $request->boolean('force_refresh');
2026-06-13 17:35:40 +03:30
2026-06-14 12:33:29 +03:30
if (! $forceRefresh) {
2026-06-13 17:35:40 +03:30
$cached = $cache->get();
if ($cached !== null) {
return $cached;
}
2026-06-14 13:15:45 +03:30
// فقط وقتی کران اخیراً tick زده — وگرنه API زنده (جلوگیری از قفل قیمت)
if ($cache->hasRecentTick()) {
$dbBuilt = $cache->buildFromDatabase();
if (($dbBuilt['price'] ?? 0) > 0) {
return $cache->put($dbBuilt);
}
2026-06-13 17:35:40 +03:30
}
}
2026-05-25 12:47:04 +03:30
2026-06-12 19:49:53 +03:30
if ($config->isStore()) {
2026-06-13 17:35:40 +03:30
return $this->appendUpPrices(array_merge([
'closed' => false,
'time' => now(),
'main_product' => 0,
'zaarbox' => 0,
2026-06-14 12:33:29 +03:30
], $this->storeLastPrice($config)), $forceRefresh);
2026-06-12 19:49:53 +03:30
}
$closed = false;
2026-05-25 12:47:04 +03:30
$montzar_price = 0;
2026-06-04 14:40:02 +03:30
$zaarbox_price = 0;
2026-05-24 16:55:07 +03:30
$pr_price = 0;
2026-05-25 12:47:04 +03:30
$montzar_open_buy = false;
$montzar_open_sell = false;
2026-05-24 16:55:07 +03:30
$priceSource = null;
2026-05-24 22:29:43 +03:30
$priceValue = 0;
2026-05-24 16:55:07 +03:30
$toBool = function ($value) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
};
2026-06-14 12:33:29 +03:30
$zeroResponse = function () use ($forceRefresh) {
2026-06-03 21:52:37 +03:30
return $this->appendUpPrices([
2026-05-25 12:47:04 +03:30
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => 0,
'montzar' => 0,
2026-06-04 14:40:02 +03:30
'zaarbox' => 0,
2026-05-25 12:47:04 +03:30
'closed' => true,
'time' => now(),
2026-06-14 12:33:29 +03:30
], $forceRefresh);
2026-05-25 12:47:04 +03:30
};
2026-06-04 12:18:53 +03:30
$priceProvider = $config->priceProvider();
2026-05-24 16:55:07 +03:30
/*
|--------------------------------------------------------------------------
2026-05-24 22:29:43 +03:30
| بستن قیمت طبق منطق قبلی
2026-05-24 16:55:07 +03:30
|--------------------------------------------------------------------------
*/
2026-05-25 12:47:04 +03:30
if ($toBool($config->Config('close_price'))) {
2026-06-14 21:42:21 +03:30
if ($config->isOutsideTradingHours()) {
$config->syncAppClosedFlag();
2026-05-25 12:47:04 +03:30
return $zeroResponse();
}
2026-05-24 22:29:43 +03:30
2026-05-25 12:47:04 +03:30
Cnt::where('key', 'app_closed')->update(['value' => 'false']);
}
2026-05-11 17:36:23 +00:00
2026-05-24 16:55:07 +03:30
/*
|--------------------------------------------------------------------------
2026-05-24 22:29:43 +03:30
| Settings
2026-05-24 16:55:07 +03:30
|--------------------------------------------------------------------------
*/
$settings = Setting::whereIn('key', [
'main_product_price',
])->get();
$settingsMap = $settings->pluck('value', 'key');
2026-06-08 11:01:07 +03:30
$show_last_price = $config->getBool('show_last_price', false);
$main_product_price = $settingsMap->get('main_product_price');
2026-01-24 14:03:11 +00:00
2026-05-24 16:55:07 +03:30
/*
|--------------------------------------------------------------------------
2026-06-04 12:18:53 +03:30
| Montzar — فقط وقتی price_provider=montzar
2026-05-24 16:55:07 +03:30
|--------------------------------------------------------------------------
*/
2026-06-04 12:18:53 +03:30
if ($priceProvider === 'montzar' && !$closed) {
2026-05-24 16:55:07 +03:30
try {
2026-05-25 12:47:04 +03:30
$montzarBaseUrl = rtrim($config->Config('montzar_base_url') ?: 'https://montpar.ir', '/');
$montzarProductId = (int) ($config->Config('montzar_product_id') ?: 25);
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
$montzar = app(MontzarService::class)->productList();
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
if (isset($montzar['message'])) {
throw new \Exception('Provider message: ' . $montzar['message']);
2026-05-24 16:55:07 +03:30
}
2026-05-25 12:47:04 +03:30
$collection = collect($montzar);
$montzarProduct = $collection->firstWhere('id', $montzarProductId);
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
if (!$montzarProduct) {
throw new \Exception("Provider product not found. product_id={$montzarProductId}");
2026-05-24 22:29:43 +03:30
}
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
if (!isset($montzarProduct['sub_agent'])) {
throw new \Exception("Provider product has no sub_agent. product_id={$montzarProductId}");
2026-05-24 22:29:43 +03:30
}
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
$buy = (float) ($montzarProduct['sub_agent']['rate_buy_gram_price'] ?? 0);
$sell = (float) ($montzarProduct['sub_agent']['rate_sell_gram_price'] ?? 0);
2026-05-24 16:55:07 +03:30
2026-05-24 22:29:43 +03:30
if ($buy <= 0 || $sell <= 0) {
throw new \Exception("Provider invalid gram price. buy={$buy}, sell={$sell}");
}
2026-05-24 16:55:07 +03:30
2026-05-24 22:29:43 +03:30
$customPrice = [];
$customPrice['price'] = ($buy + $sell) / 2;
$customPrice['diff'] = ($buy - $sell) / 2;
2026-05-24 16:55:07 +03:30
2026-05-25 16:17:24 +03:30
$config->UpdateConfig('montzar_live_price', (float) $customPrice['price']);
2026-05-25 12:47:04 +03:30
$config->UpdateConfig('montzar_diff', (float) $customPrice['diff']);
$config->UpdateConfig('montzar_mazaneh', ((float) $customPrice['price'] * 4.331802));
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
$config->UpdateConfig(
'montzar_truly_buy_mazaneh',
$montzarProduct['sub_agent']['rate_buy_price'] ?? 0
);
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
$config->UpdateConfig(
'montzar_truly_sell_mazaneh',
$montzarProduct['sub_agent']['rate_sell_price'] ?? 0
);
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
$config->UpdateConfig(
'montzar_last_product_log',
json_encode($montzarProduct, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
);
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
$montzar_open_sell = $toBool($montzarProduct['open_sell'] ?? false);
$montzar_open_buy = $toBool($montzarProduct['open_buy'] ?? false);
2026-05-24 22:29:43 +03:30
2026-05-25 12:47:04 +03:30
$config->UpdateConfig('montzar_open_sell', $montzar_open_sell ? 'true' : 'false');
$config->UpdateConfig('montzar_open_buy', $montzar_open_buy ? 'true' : 'false');
2026-05-24 22:29:43 +03:30
2026-05-25 12:47:04 +03:30
if ($montzar_open_sell || $montzar_open_buy) {
$montzar_price = (int) $customPrice['price'];
2026-05-24 16:55:07 +03:30
}
2026-05-25 12:47:04 +03:30
$config->UpdateConfig('montzar_closed', $montzar_price == 0 ? 'true' : 'false');
2026-05-24 22:29:43 +03:30
} catch (\Throwable $e) {
2026-05-25 12:47:04 +03:30
$montzar_price = 0;
2026-05-24 16:55:07 +03:30
2026-05-25 12:47:04 +03:30
\Log::error('MONZAR PRICE ERROR', [
2026-05-24 22:29:43 +03:30
'message' => $e->getMessage(),
2026-05-25 12:47:04 +03:30
'montzar_product_id' => $montzarProductId ?? null,
'montzar_response' => $montzar ?? null,
2026-05-24 22:29:43 +03:30
]);
2026-05-25 12:47:04 +03:30
$config->UpdateConfig('montzar_closed', 'true');
2026-05-24 16:55:07 +03:30
}
}
2026-06-04 14:40:02 +03:30
/*
|--------------------------------------------------------------------------
| Zaarbox — price_provider=zaarbox + settings.zaarbox_price_key
|--------------------------------------------------------------------------
*/
if ($priceProvider === 'zaarbox' && !$closed) {
$zaarbox = app(ZaarboxPriceService::class)->fetchPrice();
$zaarbox_price = (int) ($zaarbox['order_price'] ?? 0);
}
2026-05-24 16:55:07 +03:30
/*
|--------------------------------------------------------------------------
2026-06-04 14:19:54 +03:30
| Main Product — settings.main_product سپس const
2026-05-25 12:47:04 +03:30
|--------------------------------------------------------------------------
2026-05-24 16:55:07 +03:30
*/
2026-06-04 14:40:02 +03:30
$pr_product = null;
if ($priceProvider === 'main_product' || $toBool($main_product_price)) {
$pr_product = $config->mainProduct();
}
if ($pr_product && !$closed) {
if ($priceProvider === 'main_product') {
2026-06-04 14:42:22 +03:30
if (Product::hasStoredPriceColumns()) {
2026-06-05 17:43:02 +03:30
app(ZaarboxPriceService::class)->syncProductFromApi($pr_product->fresh());
2026-06-04 14:42:22 +03:30
$pr_product->refresh();
$pr_price = (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0);
} else {
2026-06-05 17:43:02 +03:30
$live = app(ZaarboxPriceService::class)->fetchLiveOrderPrice(
$pr_product->reading_method
2026-06-04 14:42:22 +03:30
);
$pr_price = (float) ($live['order_price'] ?? 0);
}
} else {
$pr_price = Product::hasStoredPriceColumns()
? (float) $pr_product->price + (float) ($pr_product->price_profit ?? 0)
: 0.0;
2026-05-24 16:55:07 +03:30
}
}
/*
|--------------------------------------------------------------------------
2026-06-04 12:18:53 +03:30
| انتخاب قیمت اصلی (همان price_provider)
2026-05-24 16:55:07 +03:30
|--------------------------------------------------------------------------
*/
2026-06-04 14:40:02 +03:30
if ($priceProvider === 'zaarbox' && $zaarbox_price > 0 && !$closed) {
$priceValue = $zaarbox_price;
$priceSource = 'zaarbox';
} elseif ($priceProvider === 'montzar' && ($montzar_price ?? 0) !== 0 && !$closed) {
2026-05-25 12:47:04 +03:30
$priceValue = (int) $montzar_price;
$priceSource = 'montzar';
2026-06-04 14:19:54 +03:30
} elseif ($priceProvider === 'main_product' && ($pr_price ?? 0) !== 0 && !$closed) {
$priceValue = $pr_price;
2026-05-24 16:55:07 +03:30
$priceSource = 'main_product';
} else {
$priceValue = 0;
2026-05-24 22:29:43 +03:30
$priceSource = null;
}
if ($priceValue <= 0) {
2026-05-25 12:47:04 +03:30
$lastPrice = Price::latest()->first();
2026-06-08 11:01:07 +03:30
$storedPrice = $lastPrice ? (float) $lastPrice->price : 0.0;
2026-06-13 17:35:40 +03:30
$useFallback = $request->boolean('force_refresh') || $show_last_price;
2026-05-25 12:47:04 +03:30
2026-06-13 17:35:40 +03:30
if ($storedPrice > 30_000_000 && $useFallback) {
$priceValue = $storedPrice;
$priceSource = 'database_last';
} else {
2026-06-03 21:52:37 +03:30
return $this->appendUpPrices([
2026-05-25 12:47:04 +03:30
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => $pr_price ?? 0,
'montzar' => $montzar_price ?? 0,
2026-06-04 14:40:02 +03:30
'zaarbox' => $zaarbox_price ?? 0,
2026-05-25 12:47:04 +03:30
'closed' => true,
'time' => now(),
2026-06-14 12:33:29 +03:30
], $forceRefresh);
2026-05-25 12:47:04 +03:30
}
2026-03-10 15:39:30 +03:30
}
2026-05-24 16:55:07 +03:30
/*
|--------------------------------------------------------------------------
| ذخیره قیمت
|--------------------------------------------------------------------------
*/
2026-06-04 13:48:37 +03:30
$priceToStore = $priceValue;
2026-01-24 14:03:11 +00:00
$lastPrice = Price::latest()->first();
2026-05-24 16:55:07 +03:30
if (
2026-06-13 17:35:40 +03:30
! $closed &&
(! $lastPrice || (float) $lastPrice->price != (float) $priceToStore) &&
$priceToStore > 30_000_000
2026-05-24 16:55:07 +03:30
) {
2026-01-24 14:03:11 +00:00
$price = new Price();
2026-06-04 13:48:37 +03:30
$price->price = $priceToStore;
2026-01-24 14:03:11 +00:00
$price->time_at = Carbon::now()->toTimeString();
$price->date_at = Carbon::now()->toDateString();
$price->save();
2026-05-24 16:55:07 +03:30
2026-01-24 14:03:11 +00:00
$this->updatePrise();
2026-05-24 16:55:07 +03:30
$candle = PriceCandle::whereDate('date_at', date("Y-m-d"))->first();
if (!is_null($candle)) {
2026-06-04 13:48:37 +03:30
$candle->end = $priceToStore;
2026-05-24 16:55:07 +03:30
2026-06-04 13:48:37 +03:30
if ($priceToStore < $candle->low) {
$candle->low = $priceToStore;
} elseif ($priceToStore > $candle->high) {
$candle->high = $priceToStore;
2026-05-24 16:55:07 +03:30
}
$candle->save();
} else {
2026-01-24 14:03:11 +00:00
PriceCandle::create([
2026-06-04 13:48:37 +03:30
'start' => $priceToStore,
'end' => $priceToStore,
'high' => $priceToStore,
'low' => $priceToStore,
2026-05-25 12:47:04 +03:30
'date_at' => date('Y-m-d'),
2026-01-24 14:03:11 +00:00
]);
}
2026-06-04 13:48:37 +03:30
$lastPrice = $price;
2026-01-24 14:03:11 +00:00
}
2026-05-24 16:55:07 +03:30
/*
|--------------------------------------------------------------------------
2026-05-25 12:47:04 +03:30
| محاسبه sell / buy
2026-05-24 16:55:07 +03:30
|--------------------------------------------------------------------------
*/
2026-06-14 12:33:29 +03:30
$display = $config->buildTradeDisplayPrices(
(float) $priceValue,
$priceSource,
[
'montzar_open_buy' => $montzar_open_buy,
'montzar_open_sell' => $montzar_open_sell,
]
);
$sell = $display['sell'];
$buy = $display['buy'];
$priceLast = $display['priceLast'];
2026-06-04 13:48:37 +03:30
2026-06-01 18:46:57 +03:30
if (!$closed && $priceValue > 0) {
$config->trackDailyPriceExtreme((float) $priceValue);
}
2026-06-03 21:52:37 +03:30
return $this->appendUpPrices([
2026-05-24 16:55:07 +03:30
'sell' => $sell,
'buy' => $buy,
'price_last' => $priceLast,
'price' => $priceValue,
'main_product' => $pr_price ?? 0,
2026-05-25 12:47:04 +03:30
'montzar' => $montzar_price ?? 0,
2026-06-04 14:40:02 +03:30
'zaarbox' => $zaarbox_price ?? 0,
2026-05-24 22:29:43 +03:30
'closed' => false,
2026-05-24 16:55:07 +03:30
'time' => now(),
2026-06-14 12:33:29 +03:30
], $forceRefresh);
2026-01-24 14:03:11 +00:00
}
2026-06-12 19:49:53 +03:30
/**
* فروشگاه — منطق lastPrice قبل مرج (ghatreh): montzar_price / nasiryan / prices
*/
protected function storeLastPrice(ConfigService $config): array
{
$toBool = fn ($value) => filter_var($value, FILTER_VALIDATE_BOOLEAN);
$difference = (float) config('fee.single');
$talaIr = 0;
$nasiryan = 0;
$montzar = 0;
$montzarExists = Setting::where('key', 'montzar_price')->exists();
$montzarEnabled = $montzarExists && $toBool($config->Config('montzar_price'));
if ($montzarEnabled) {
try {
$montzarProductId = (int) ($config->Config('montzar_product_id') ?: 25);
$montzarProducts = app(MontzarService::class)->productList();
if (isset($montzarProducts['message'])) {
throw new \Exception('Montzar message: ' . $montzarProducts['message']);
}
$montzarProduct = collect($montzarProducts)->firstWhere('id', $montzarProductId);
if (! $montzarProduct || ! isset($montzarProduct['sub_agent'])) {
throw new \Exception("Montzar product not found. product_id={$montzarProductId}");
}
$buy = (float) ($montzarProduct['sub_agent']['rate_buy_gram_price'] ?? 0);
$sell = (float) ($montzarProduct['sub_agent']['rate_sell_gram_price'] ?? 0);
if ($buy <= 0 || $sell <= 0) {
throw new \Exception("Montzar invalid gram price. buy={$buy}, sell={$sell}");
}
$customPrice = [
'price' => ($buy + $sell) / 2,
'diff' => ($buy - $sell) / 2,
];
$config->UpdateConfig('montzar_live_price', (float) $customPrice['price']);
$config->UpdateConfig('montzar_diff', (float) $customPrice['diff']);
$config->UpdateConfig('montzar_mazaneh', ((float) $customPrice['price'] * 4.331802));
$config->UpdateConfig('montzar_truly_buy_mazaneh', $montzarProduct['sub_agent']['rate_buy_price'] ?? 0);
$config->UpdateConfig('montzar_truly_sell_mazaneh', $montzarProduct['sub_agent']['rate_sell_price'] ?? 0);
$config->UpdateConfig(
'montzar_last_product_log',
json_encode($montzarProduct, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
);
$openBuy = $toBool($montzarProduct['open_buy'] ?? false);
$openSell = $toBool($montzarProduct['open_sell'] ?? false);
$config->UpdateConfig('montzar_open_buy', $openBuy ? 'true' : 'false');
$config->UpdateConfig('montzar_open_sell', $openSell ? 'true' : 'false');
if ($openBuy || $openSell) {
$montzar = (int) $customPrice['price'];
}
$config->UpdateConfig('montzar_closed', $montzar === 0 ? 'true' : 'false');
} catch (\Throwable $e) {
$montzar = 0;
$config->UpdateConfig('montzar_closed', 'true');
\Log::error('STORE MONZAR PRICE ERROR', ['message' => $e->getMessage()]);
}
}
$nasiryanEnabled = $config->Config('price_from_nasiryan') == 'true';
if ($nasiryanEnabled) {
try {
$nasiryanRes = Http::withoutVerifying()
->timeout(2)
->withToken(Cnt::where('key', 'nasiryan')->first()['value'] ?? '')
->get('https://nasiryan.com/server/customer/product/list')
->json();
if ((isset($nasiryanRes['message']) && $nasiryanRes['message'] == 'no authorised') || $nasiryanRes == null) {
$cnt = new Cnt();
$cnt->nasiryan_login();
$nasiryanRes = Http::withoutVerifying()
->timeout(2)
->withToken(Cnt::where('key', 'nasiryan')->first()['value'] ?? '')
->get('https://nasiryan.com/server/customer/product/list')
->json();
}
$lp = collect($nasiryanRes)->firstWhere('id', 1);
if (! $lp || ! isset($lp['sub_agent'])) {
throw new \Exception('Nasiryan product/sub_agent not found');
}
$buy = (float) $lp['sub_agent']['rate_buy_gram_price'];
$sell = (float) $lp['sub_agent']['rate_sell_gram_price'];
$customPrice = [
'price' => ($buy + $sell) / 2,
'diff' => ($buy - $sell) / 2,
];
Cnt::where('key', 'nasiryan_price')->update(['value' => (float) $customPrice['price']]);
Cnt::where('key', 'nasiryan_diff')->update(['value' => (float) $customPrice['diff']]);
Cnt::where('key', 'nasiryan_mazaneh')->update(['value' => ((float) $customPrice['price'] * 4.331802)]);
Cnt::where('key', 'nasiryan_truly_buy_mazaneh')->update(['value' => $lp['sub_agent']['rate_buy_price']]);
Cnt::where('key', 'nasiryan_truly_sell_mazaneh')->update(['value' => $lp['sub_agent']['rate_sell_price']]);
Cnt::where('key', 'log')->update(['value' => json_encode($nasiryanRes, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]);
$nasiryan = (int) $customPrice['price'];
Cnt::where('key', 'nasiryan_closed')->update(['value' => $nasiryan == 0 ? 'true' : 'false']);
} catch (\Throwable $e) {
$nasiryan = 0;
Cnt::where('key', 'nasiryan_closed')->update(['value' => 'true']);
}
}
if ($montzarEnabled && $montzar !== 0) {
$priceValue = $montzar;
$priceSource = 'montzar';
} elseif ($nasiryanEnabled && $nasiryan !== 0) {
$priceValue = $nasiryan;
$priceSource = 'nasiryan';
} else {
$priceValue = $talaIr;
$priceSource = 'tala_ir';
}
$lastStored = Price::latest()->first();
if (
(! $lastStored || (float) $lastStored->price != (float) $priceValue)
&& $priceValue > 30000000
) {
$price = new Price();
$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 ($candle !== null) {
$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::orderByDesc('id')->first();
if (! $price) {
return [];
}
if ($priceSource === 'montzar') {
$difference += (float) $config->Config('montzar_diff');
} elseif ($priceSource === 'nasiryan') {
$difference += (float) (Cnt::where('key', 'nasiryan_diff')->first()['value'] ?? 0);
}
$response = [
'sell' => (int) ceil($price->price - $difference),
'buy' => (int) ceil($price->price + $difference),
'price_last' => (int) ceil($price->price + $difference),
'price' => $priceValue,
'tala_ir' => $talaIr,
'nasiryan' => $nasiryan,
];
if ($montzarExists) {
$response['montzar'] = $montzar;
}
return $response;
}
2026-01-24 14:03:11 +00:00
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);
}
}