This commit is contained in:
2026-06-12 19:49:53 +03:30
parent 11fb7272cb
commit 98e1fe969c
6 changed files with 310 additions and 9 deletions
@@ -134,6 +134,10 @@ class ReportController extends Controller
{
$config = app(ConfigService::class);
if ($config->isStore()) {
return $this->storeLastPrice($config);
}
$closed = false;
$montzar_price = 0;
@@ -499,6 +503,190 @@ class ReportController extends Controller
]);
}
/**
* فروشگاه — منطق 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;
}
public function check()
{
$service = Http::