2026-05-24 13:56:33 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
2026-06-04 14:19:54 +03:30
|
|
|
use App\Models\Cnt;
|
2026-06-11 20:40:47 +03:30
|
|
|
use App\Models\Price;
|
|
|
|
|
use App\Models\PriceCandle;
|
2026-06-04 14:40:02 +03:30
|
|
|
use App\Models\Product;
|
2026-05-24 19:22:09 +03:30
|
|
|
use App\Models\Setting;
|
2026-06-14 21:42:21 +03:30
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
2026-05-24 13:56:33 +03:30
|
|
|
|
|
|
|
|
class ConfigService
|
|
|
|
|
{
|
2026-06-12 16:06:37 +03:30
|
|
|
public const PRICE_PROVIDERS = ['montzar', 'main_product', 'zaarbox'];
|
2026-06-04 12:18:53 +03:30
|
|
|
|
2026-05-25 20:55:05 +03:30
|
|
|
public function Config($name)
|
2026-05-24 13:56:33 +03:30
|
|
|
{
|
2026-06-04 12:18:53 +03:30
|
|
|
return $this->get($name, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* مقدار setting؛ اگر نبود از config/price_providers.php یا default پارامتر.
|
|
|
|
|
*/
|
|
|
|
|
public function get(string $key, $default = null)
|
|
|
|
|
{
|
2026-06-12 19:14:15 +03:30
|
|
|
try {
|
|
|
|
|
$row = Setting::where('key', $key)->first();
|
2026-06-04 12:18:53 +03:30
|
|
|
|
2026-06-12 19:14:15 +03:30
|
|
|
if ($row !== null && $row->is_active && $row->value !== null && $row->value !== '') {
|
|
|
|
|
return $row->value;
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// DB در دسترس نیست (deploy / .env اشتباه) — از config/price_providers.php استفاده شود
|
2026-06-04 12:18:53 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fromFile = config("price_providers.{$key}");
|
|
|
|
|
|
|
|
|
|
return $fromFile !== null ? $fromFile : $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getString(string $key, string $default = ''): string
|
|
|
|
|
{
|
|
|
|
|
$value = $this->get($key, $default);
|
|
|
|
|
|
|
|
|
|
return trim((string) ($value === false ? $default : $value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getBool(string $key, bool $default = false): bool
|
|
|
|
|
{
|
|
|
|
|
$value = $this->get($key, $default);
|
|
|
|
|
|
|
|
|
|
if ($value === false || $value === null || $value === '') {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:08:47 +03:30
|
|
|
/** فروشگاهی (قطره / کشکول) — روتها و رفتار store فقط وقتی true باشد فعال میشوند */
|
|
|
|
|
public function isStore(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->getBool('is_store', false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-30 21:44:39 +03:30
|
|
|
/** کد معرف در ثبتنام اجباری است (تنظیم referral_id = required) */
|
|
|
|
|
public function isReferralRequired(): bool
|
|
|
|
|
{
|
|
|
|
|
return strtolower(trim($this->getString('referral_id'))) === 'required';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 12:18:53 +03:30
|
|
|
public function getInt(string $key, int $default = 0): int
|
|
|
|
|
{
|
|
|
|
|
$value = $this->get($key, $default);
|
|
|
|
|
|
|
|
|
|
return (int) ($value === false || $value === null || $value === '' ? $default : $value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 17:39:06 +03:30
|
|
|
/** X-Client-Id برای API خارجی (HMAC) — اول settings، بعد .env NODE_CLIENT_ID */
|
|
|
|
|
public function nodeClientId(): string
|
|
|
|
|
{
|
|
|
|
|
$fromDb = $this->getString('node_client_id', '');
|
|
|
|
|
|
|
|
|
|
return $fromDb !== '' ? $fromDb : (string) env('NODE_CLIENT_ID', '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** کلید امضای HMAC — اول settings، بعد .env NODE_SHARED_SECRET */
|
|
|
|
|
public function nodeSharedSecret(): string
|
|
|
|
|
{
|
|
|
|
|
$fromDb = $this->getString('node_shared_secret', '');
|
|
|
|
|
|
|
|
|
|
return $fromDb !== '' ? $fromDb : (string) env('NODE_SHARED_SECRET', '');
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 15:30:34 +03:30
|
|
|
/** اختلاف خرید نمایش (/api/price/last) — fee_single / buy_fee */
|
2026-07-02 01:26:26 +03:30
|
|
|
public function displayBuyPriceDiffRial(): float
|
2026-06-04 15:30:34 +03:30
|
|
|
{
|
|
|
|
|
$feeSingle = (float) $this->get('fee_single', 0);
|
|
|
|
|
$buyFee = (float) $this->get('buy_fee', $feeSingle);
|
|
|
|
|
|
|
|
|
|
return $buyFee > 0 ? $buyFee : $feeSingle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** اختلاف فروش نمایش — fee_single / sell_fee */
|
2026-07-02 01:26:26 +03:30
|
|
|
public function displaySellPriceDiffRial(): float
|
2026-06-04 15:30:34 +03:30
|
|
|
{
|
|
|
|
|
$feeSingle = (float) $this->get('fee_single', 0);
|
|
|
|
|
$sellFee = (float) $this->get('sell_fee', $feeSingle);
|
|
|
|
|
|
|
|
|
|
return $sellFee > 0 ? $sellFee : $feeSingle;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 16:33:15 +03:30
|
|
|
/** اختلاف ریالی سفارش — fee_single، بعد fee_cash (legacy)، بعد config/fee.php */
|
2026-07-02 01:26:26 +03:30
|
|
|
public function orderPriceDiffRial(): float
|
2026-07-01 16:33:15 +03:30
|
|
|
{
|
|
|
|
|
$feeSingle = $this->getString('fee_single', '');
|
|
|
|
|
if ($feeSingle !== '') {
|
|
|
|
|
return (float) $feeSingle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$legacyCash = $this->getString('fee_cash', '');
|
|
|
|
|
if ($legacyCash !== '') {
|
|
|
|
|
return (float) $legacyCash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (float) config('fee.single', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** درصد کارمزد سفارش — fee_service_charge_percentage، service_charge_percentage، config/fee.php */
|
|
|
|
|
public function orderServiceChargePercentage(): float
|
|
|
|
|
{
|
|
|
|
|
foreach (['fee_service_charge_percentage', 'service_charge_percentage'] as $key) {
|
|
|
|
|
$raw = $this->getString($key, '');
|
|
|
|
|
if ($raw !== '') {
|
|
|
|
|
return (float) $raw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (float) config('fee.service_charge_percentage', 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 20:59:24 +03:30
|
|
|
/**
|
2026-07-01 23:59:59 +03:30
|
|
|
* روشن: فروش = مبلغ ریالی دقیق (کارمزد از طلا)؛ خرید = وزن دقیق (کارمزد از ریال).
|
|
|
|
|
* خاموش: فروش = کارمزد از ریال واریزی؛ خرید = کارمزد از وزن طلای واریزی.
|
2026-07-01 20:59:24 +03:30
|
|
|
*/
|
|
|
|
|
public function feeDeductFromGold(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->getBool('fee_deduct_from_gold', false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 12:33:29 +03:30
|
|
|
/**
|
|
|
|
|
* sell / buy / price_last — یک منبع حقیقت برای lastPrice و buildFromDatabase.
|
|
|
|
|
*
|
|
|
|
|
* @param float $priceValue قیمت پایه (ریال/گرم) — از API یا DB
|
|
|
|
|
* @param string|null $priceSource montzar | zaarbox | main_product | database_last | null
|
|
|
|
|
* @param array{montzar_open_buy?:bool,montzar_open_sell?:bool} $montzarFlags فقط وقتی API تازه خوانده شده
|
|
|
|
|
* @return array{sell:int,buy:int,priceLast:int}
|
|
|
|
|
*/
|
|
|
|
|
public function buildTradeDisplayPrices(
|
|
|
|
|
float $priceValue,
|
|
|
|
|
?string $priceSource = null,
|
|
|
|
|
array $montzarFlags = []
|
|
|
|
|
): array {
|
2026-07-02 01:26:26 +03:30
|
|
|
$diffBuy = (float) $this->displayBuyPriceDiffRial();
|
|
|
|
|
$diffSell = (float) $this->displaySellPriceDiffRial();
|
2026-06-14 12:33:29 +03:30
|
|
|
$feeSingle = (float) $this->get('fee_single', 0);
|
|
|
|
|
|
|
|
|
|
$openBuy = true;
|
|
|
|
|
$openSell = true;
|
|
|
|
|
|
|
|
|
|
if ($priceSource === 'montzar') {
|
|
|
|
|
$montzarProfit = (float) $this->get('montzar_diff', 0);
|
|
|
|
|
$finalBuyDiff = $diffBuy + $montzarProfit;
|
|
|
|
|
$finalSellDiff = $diffSell + $montzarProfit;
|
|
|
|
|
|
2026-06-28 10:58:37 +03:30
|
|
|
// montzar_open_buy/sell وضعیت همکار است — نمایش قیمت را صفر نکن.
|
|
|
|
|
// باز/بسته بودن معامله = product.open_buy/open_sell در اپ محلی.
|
|
|
|
|
$sell = (int) ceil($priceValue - $finalSellDiff);
|
|
|
|
|
$buy = (int) ceil($priceValue + $finalBuyDiff);
|
2026-06-14 12:33:29 +03:30
|
|
|
$priceLast = (int) ceil($priceValue + $finalBuyDiff);
|
|
|
|
|
|
|
|
|
|
return compact('sell', 'buy', 'priceLast');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sell = $openSell ? (int) ceil($priceValue - $feeSingle) : 0;
|
|
|
|
|
$buy = $openBuy ? (int) ceil($priceValue + $feeSingle) : 0;
|
|
|
|
|
$priceLast = (int) ceil($priceValue + $feeSingle);
|
|
|
|
|
|
|
|
|
|
$provider = $this->priceProvider();
|
|
|
|
|
if (in_array($provider, ['main_product', 'zaarbox'], true)) {
|
|
|
|
|
$sell = $openSell ? (int) ceil($priceValue - $diffSell) : 0;
|
|
|
|
|
$buy = $openBuy ? (int) ceil($priceValue + $diffBuy) : 0;
|
|
|
|
|
$priceLast = (int) ceil($priceValue + $diffBuy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return compact('sell', 'buy', 'priceLast');
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 12:18:53 +03:30
|
|
|
/**
|
2026-06-12 16:06:37 +03:30
|
|
|
* تنها منبع فعال قیمت — montzar | main_product | zaarbox
|
2026-06-04 12:18:53 +03:30
|
|
|
*
|
|
|
|
|
* اولویت:
|
2026-06-12 16:06:37 +03:30
|
|
|
* 1) price_provider (یا price_primary_source قدیمی) — فقط مقادیر معتبر
|
2026-06-30 22:17:09 +03:30
|
|
|
* 2) main_product_price=true → main_product (legacy)
|
|
|
|
|
* 3) پیشفرض فایل: montzar
|
2026-06-04 12:18:53 +03:30
|
|
|
*/
|
|
|
|
|
public function priceProvider(): string
|
|
|
|
|
{
|
|
|
|
|
foreach (['price_provider', 'price_primary_source'] as $key) {
|
|
|
|
|
$explicit = strtolower($this->getString($key, ''));
|
|
|
|
|
if (in_array($explicit, self::PRICE_PROVIDERS, true)) {
|
|
|
|
|
return $explicit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 16:06:37 +03:30
|
|
|
if ($this->getBool('main_product_price', false)) {
|
|
|
|
|
return 'main_product';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 12:18:53 +03:30
|
|
|
$default = strtolower((string) config('price_providers.price_provider', 'montzar'));
|
|
|
|
|
|
|
|
|
|
return in_array($default, self::PRICE_PROVIDERS, true) ? $default : 'montzar';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 16:57:31 +03:30
|
|
|
/**
|
|
|
|
|
* محصولات متصل به کیف اصلی — setting `main_wallet_products`.
|
|
|
|
|
* مقدار: idها با کاما (مثل «13,25») یا آرایه JSON (مثل «[13,25]»).
|
|
|
|
|
* موجودی این محصولات = gold کیف اصلی (دارایی جدا ندارند).
|
2026-07-01 15:47:24 +03:30
|
|
|
* اگر خالی باشد پیشفرض [13] است.
|
2026-06-10 16:57:31 +03:30
|
|
|
*/
|
|
|
|
|
public function mainWalletProductIds(): array
|
|
|
|
|
{
|
|
|
|
|
$raw = trim($this->getString('main_wallet_products', ''));
|
|
|
|
|
if ($raw === '') {
|
2026-07-01 15:47:24 +03:30
|
|
|
return [13];
|
2026-06-10 16:57:31 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$decoded = json_decode($raw, true);
|
|
|
|
|
$parts = is_array($decoded) ? $decoded : explode(',', $raw);
|
|
|
|
|
|
|
|
|
|
$ids = [];
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
$id = (int) trim((string) $part);
|
|
|
|
|
if ($id > 0) {
|
|
|
|
|
$ids[] = $id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 15:47:24 +03:30
|
|
|
$ids = array_values(array_unique($ids));
|
|
|
|
|
|
|
|
|
|
return $ids !== [] ? $ids : [13];
|
2026-06-10 16:57:31 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isMainWalletProduct(int $productId): bool
|
|
|
|
|
{
|
|
|
|
|
return in_array($productId, $this->mainWalletProductIds(), true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 01:17:34 +03:30
|
|
|
/** اولین محصول فعال از `main_wallet_products` — نماینده کیف اصلی */
|
|
|
|
|
public function mainWalletRepresentativeProduct(): ?Product
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->mainWalletProductIds() as $productId) {
|
|
|
|
|
$product = Product::query()->where('active', 1)->find($productId);
|
|
|
|
|
if ($product) {
|
|
|
|
|
return $product;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->mainWalletProductIds() as $productId) {
|
|
|
|
|
$product = Product::query()->find($productId);
|
|
|
|
|
if ($product) {
|
|
|
|
|
return $product;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** metal_id محصول نماینده کیف اصلی — پیشفرض ۱ (طلا) */
|
|
|
|
|
public function mainWalletMetalId(): int
|
|
|
|
|
{
|
|
|
|
|
$product = $this->mainWalletRepresentativeProduct();
|
|
|
|
|
$metalId = $product ? (int) ($product->metal_id ?? 0) : 0;
|
|
|
|
|
|
|
|
|
|
return $metalId > 0 ? $metalId : 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 01:32:32 +03:30
|
|
|
/** عنوان محصول(ات) متصل به کیف اصلی — ترتیب همان main_wallet_products */
|
|
|
|
|
public function mainWalletProductLabels(): string
|
|
|
|
|
{
|
|
|
|
|
$titles = [];
|
|
|
|
|
foreach ($this->mainWalletProductIds() as $productId) {
|
|
|
|
|
$title = Product::query()->whereKey($productId)->value('title');
|
|
|
|
|
if ($title !== null && trim((string) $title) !== '') {
|
|
|
|
|
$titles[] = trim((string) $title);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode(' و ', $titles);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 14:19:54 +03:30
|
|
|
/**
|
2026-06-04 14:40:02 +03:30
|
|
|
* شناسه محصول آبشده: اول settings، بعد const.
|
|
|
|
|
* مقدار settings میتواند id عددی (مثل 13) یا reading_method (مثل estjt_...) باشد.
|
2026-06-04 14:19:54 +03:30
|
|
|
*/
|
|
|
|
|
public function mainProductId(): ?int
|
|
|
|
|
{
|
2026-06-04 14:40:02 +03:30
|
|
|
$product = $this->mainProduct();
|
2026-06-04 14:19:54 +03:30
|
|
|
|
2026-06-04 14:40:02 +03:30
|
|
|
return $product?->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function mainProduct(): ?Product
|
|
|
|
|
{
|
|
|
|
|
foreach ([$this->getString('main_product', ''), (string) (Cnt::where('key', 'main_product')->value('value') ?? '')] as $raw) {
|
|
|
|
|
$raw = trim($raw);
|
|
|
|
|
if ($raw === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_numeric($raw)) {
|
|
|
|
|
$product = Product::find((int) $raw);
|
|
|
|
|
if ($product) {
|
|
|
|
|
return $product;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$byMethod = Product::where('reading_method', $raw)->first();
|
|
|
|
|
if ($byMethod) {
|
|
|
|
|
return $byMethod;
|
|
|
|
|
}
|
2026-06-04 14:19:54 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 12:18:53 +03:30
|
|
|
/** @deprecated از priceProvider() استفاده کن */
|
|
|
|
|
public function pricePrimarySource(): string
|
|
|
|
|
{
|
|
|
|
|
$provider = $this->priceProvider();
|
|
|
|
|
|
2026-06-05 17:43:02 +03:30
|
|
|
return in_array($provider, ['montzar'], true) ? $provider : 'montzar';
|
2026-06-04 12:18:53 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function usesMontzarPricing(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->priceProvider() === 'montzar';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-05 17:43:02 +03:30
|
|
|
/** @deprecated منبع etehadie حذف شده */
|
2026-06-04 12:18:53 +03:30
|
|
|
public function usesEtehadiePricing(): bool
|
|
|
|
|
{
|
2026-06-05 17:43:02 +03:30
|
|
|
return false;
|
2026-05-24 13:56:33 +03:30
|
|
|
}
|
2026-05-24 19:22:09 +03:30
|
|
|
|
2026-06-04 14:19:54 +03:30
|
|
|
public function usesMainProductPricing(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->priceProvider() === 'main_product';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 14:40:02 +03:30
|
|
|
public function usesZaarboxPricing(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->priceProvider() === 'zaarbox';
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 19:22:09 +03:30
|
|
|
public function UpdateConfig($name, $value)
|
2026-05-24 13:56:33 +03:30
|
|
|
{
|
2026-06-01 18:46:57 +03:30
|
|
|
return Setting::updateOrCreate(
|
2026-05-24 22:29:43 +03:30
|
|
|
['key' => $name],
|
|
|
|
|
['value' => $value]
|
|
|
|
|
);
|
2026-05-24 13:56:33 +03:30
|
|
|
}
|
2026-06-01 18:46:57 +03:30
|
|
|
|
|
|
|
|
public function trackDailyPriceExtreme(float $price): void
|
|
|
|
|
{
|
|
|
|
|
if ($price <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$upSetting = Setting::where('key', 'up_price')->first();
|
|
|
|
|
$downSetting = Setting::where('key', 'down_price')->first();
|
|
|
|
|
|
|
|
|
|
if (!$upSetting || !$downSetting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$priceInt = (int) round($price);
|
|
|
|
|
$today = now()->toDateString();
|
2026-06-02 11:18:05 +03:30
|
|
|
$extremeDate = $this->Config('price_extreme_date');
|
2026-06-01 18:46:57 +03:30
|
|
|
|
2026-06-02 11:18:05 +03:30
|
|
|
if ($extremeDate !== $today) {
|
2026-06-01 18:46:57 +03:30
|
|
|
$this->UpdateConfig('up_price', $priceInt);
|
|
|
|
|
$this->UpdateConfig('down_price', $priceInt);
|
2026-06-02 11:18:05 +03:30
|
|
|
$this->UpdateConfig('price_extreme_date', $today);
|
2026-06-01 18:46:57 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$currentUp = (float) ($upSetting->value ?: 0);
|
|
|
|
|
$currentDown = (float) ($downSetting->value ?: 0);
|
|
|
|
|
|
|
|
|
|
if ($currentUp <= 0 || $priceInt > $currentUp) {
|
|
|
|
|
$this->UpdateConfig('up_price', $priceInt);
|
|
|
|
|
}
|
|
|
|
|
if ($currentDown <= 0 || $priceInt < $currentDown) {
|
|
|
|
|
$this->UpdateConfig('down_price', $priceInt);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-11 20:40:47 +03:30
|
|
|
|
|
|
|
|
/**
|
2026-06-16 19:53:49 +03:30
|
|
|
* بیشترین/کمترین قیمت پایه امروز — settings + candle + جدول prices.
|
2026-06-11 20:40:47 +03:30
|
|
|
*
|
|
|
|
|
* @return array{up: int, down: int}
|
|
|
|
|
*/
|
|
|
|
|
public function resolvedDailyBaseExtremes(): array
|
|
|
|
|
{
|
|
|
|
|
$up = $this->getInt('up_price', 0);
|
|
|
|
|
$down = $this->getInt('down_price', 0);
|
|
|
|
|
|
|
|
|
|
$candle = PriceCandle::whereDate('date_at', now()->toDateString())->first();
|
|
|
|
|
$candleHigh = $candle ? (int) round((float) $candle->high) : 0;
|
|
|
|
|
$candleLow = $candle ? (int) round((float) $candle->low) : 0;
|
|
|
|
|
|
2026-06-16 19:53:49 +03:30
|
|
|
if ($candleHigh > 0) {
|
|
|
|
|
$up = $up > 0 ? max($up, $candleHigh) : $candleHigh;
|
|
|
|
|
}
|
|
|
|
|
if ($candleLow > 0) {
|
|
|
|
|
$down = $down > 0 ? min($down, $candleLow) : $candleLow;
|
2026-06-11 20:40:47 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($up <= 0 || $down <= 0) {
|
|
|
|
|
$fromTable = $this->filteredTodayMinMax();
|
|
|
|
|
if ($up <= 0 && $fromTable['max'] > 0) {
|
|
|
|
|
$up = $fromTable['max'];
|
|
|
|
|
}
|
|
|
|
|
if ($down <= 0 && $fromTable['min'] > 0) {
|
|
|
|
|
$down = $fromTable['min'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($up <= 0 && $candleHigh > 0) {
|
|
|
|
|
$up = $candleHigh;
|
|
|
|
|
}
|
|
|
|
|
if ($down <= 0 && $candleLow > 0) {
|
|
|
|
|
$down = $candleLow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($up > 0 && $down > 0 && $down > $up) {
|
|
|
|
|
[$up, $down] = [$down, $up];
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 19:53:49 +03:30
|
|
|
$storedDown = $this->getInt('down_price', 0);
|
|
|
|
|
if ($down > 0 && $down < $storedDown) {
|
|
|
|
|
$this->UpdateConfig('down_price', $down);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 20:40:47 +03:30
|
|
|
return ['up' => $up, 'down' => $down];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array{min: int, max: int}
|
|
|
|
|
*/
|
|
|
|
|
public function filteredTodayMinMax(): array
|
|
|
|
|
{
|
|
|
|
|
$prices = Price::query()
|
|
|
|
|
->whereDate('date_at', now()->toDateString())
|
|
|
|
|
->pluck('price')
|
|
|
|
|
->map(fn ($price) => (int) round((float) $price))
|
|
|
|
|
->filter(fn (int $price) => $price > 0)
|
|
|
|
|
->values();
|
|
|
|
|
|
|
|
|
|
if ($prices->isEmpty()) {
|
|
|
|
|
return ['min' => 0, 'max' => 0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
2026-06-16 19:53:49 +03:30
|
|
|
'min' => (int) $prices->min(),
|
|
|
|
|
'max' => (int) $prices->max(),
|
2026-06-11 20:40:47 +03:30
|
|
|
];
|
|
|
|
|
}
|
2026-06-14 21:42:21 +03:30
|
|
|
|
|
|
|
|
public function isClosePriceEnabled(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->getBool('close_price', false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array{start: string, end: string} */
|
|
|
|
|
public function tradingHourBounds(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'start' => $this->normalizeTradingTime($this->getString('startDay', '00:00'), '00:00'),
|
|
|
|
|
'end' => $this->normalizeTradingTime($this->getString('endDay', '23:59'), '23:59'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function normalizeTradingTime(string $value, string $default): string
|
|
|
|
|
{
|
|
|
|
|
$value = trim($value);
|
|
|
|
|
if ($value === '') {
|
|
|
|
|
$value = $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match('/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/', $value, $matches)) {
|
|
|
|
|
return sprintf('%02d:%02d', (int) $matches[1], (int) $matches[2]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isOutsideTradingHours(?Carbon $now = null): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$this->isClosePriceEnabled()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$now = $now ?? Carbon::now();
|
|
|
|
|
$bounds = $this->tradingHourBounds();
|
|
|
|
|
[$startHour, $startMinute] = array_map('intval', explode(':', $bounds['start']));
|
|
|
|
|
[$endHour, $endMinute] = array_map('intval', explode(':', $bounds['end']));
|
|
|
|
|
|
|
|
|
|
$start = $now->copy()->startOfDay()->setTime($startHour, $startMinute);
|
|
|
|
|
$end = $now->copy()->startOfDay()->setTime($endHour, $endMinute);
|
|
|
|
|
|
|
|
|
|
return !$now->between($start, $end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function syncAppClosedFlag(?Carbon $now = null): bool
|
|
|
|
|
{
|
|
|
|
|
$closed = $this->isOutsideTradingHours($now);
|
|
|
|
|
Cnt::where('key', 'app_closed')->update(['value' => $closed ? 'true' : 'false']);
|
|
|
|
|
|
2026-06-28 10:46:09 +03:30
|
|
|
return $closed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* montzar_closed فقط وضعیت sync زنده است — نباید بهتنهایی سفارش را ببندد.
|
|
|
|
|
* اگر sync خطا داد ولی montzar_live_price و open_buy/sell معتبرند، معامله باز بماند.
|
|
|
|
|
*/
|
|
|
|
|
public function applyMontzarSyncFailure(): void
|
|
|
|
|
{
|
|
|
|
|
$staleLive = $this->getInt('montzar_live_price', 0);
|
|
|
|
|
|
2026-06-28 10:51:56 +03:30
|
|
|
$this->UpdateConfig('montzar_closed', $staleLive > 0 ? 'false' : 'true');
|
2026-06-28 10:46:09 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-28 11:03:54 +03:30
|
|
|
/**
|
|
|
|
|
* باز/بسته بودن خرید و فروش فقط دست ادمین است — sync همکار آن را بازنویسی نمیکند.
|
|
|
|
|
* مقدار ذخیرهشده در تنظیمات همان چیزی است که ادمین روشن/خاموش کرده.
|
|
|
|
|
*/
|
|
|
|
|
public function montzarOpenFlags(): array
|
2026-06-28 10:46:09 +03:30
|
|
|
{
|
2026-06-28 11:03:54 +03:30
|
|
|
return [
|
|
|
|
|
'open_buy' => $this->getBool('montzar_open_buy', true),
|
|
|
|
|
'open_sell' => $this->getBool('montzar_open_sell', true),
|
|
|
|
|
];
|
2026-06-14 21:42:21 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-14 22:00:21 +03:30
|
|
|
public function formatTradingTimeForDisplay(string $time): string
|
|
|
|
|
{
|
|
|
|
|
$normalized = $this->normalizeTradingTime($time, '00:00');
|
|
|
|
|
|
|
|
|
|
return $normalized . ':00';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function tradingClosedMessage(?Carbon $now = null): string
|
|
|
|
|
{
|
|
|
|
|
$bounds = $this->tradingHourBounds();
|
|
|
|
|
$start = $this->formatTradingTimeForDisplay($bounds['start']);
|
|
|
|
|
$end = $this->formatTradingTimeForDisplay($bounds['end']);
|
|
|
|
|
|
|
|
|
|
return "فروشگاه در حال حاضر بسته است. ساعت کاری از {$start} تا {$end} میباشد.";
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:42:21 +03:30
|
|
|
public function assertTradingOpen(?Carbon $now = null): void
|
|
|
|
|
{
|
|
|
|
|
if ($this->isOutsideTradingHours($now)) {
|
2026-06-14 22:00:21 +03:30
|
|
|
throw new UnprocessableEntityHttpException($this->tradingClosedMessage($now));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hasProductTradingWindow(?string $open, ?string $close): bool
|
|
|
|
|
{
|
|
|
|
|
if ($open === null || $close === null || trim($open) === '' || trim($close) === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->formatProductTime($open) !== $this->formatProductTime($close);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function formatProductTime(?string $time): string
|
|
|
|
|
{
|
|
|
|
|
if ($time === null || trim($time) === '') {
|
|
|
|
|
return '00:00:00';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$time = trim($time);
|
|
|
|
|
if (preg_match('/(\d{1,2}:\d{2}(?::\d{2})?)/', $time, $matches)) {
|
|
|
|
|
$parts = explode(':', $matches[1]);
|
|
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
|
'%02d:%02d:%02d',
|
|
|
|
|
(int) ($parts[0] ?? 0),
|
|
|
|
|
(int) ($parts[1] ?? 0),
|
|
|
|
|
(int) ($parts[2] ?? 0)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '00:00:00';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isWithinTimeWindow(?string $open, ?string $close, ?Carbon $now = null): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$this->hasProductTradingWindow($open, $close)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$now = $now ?? Carbon::now();
|
|
|
|
|
$current = $now->format('H:i:s');
|
|
|
|
|
$openStr = $this->formatProductTime($open);
|
|
|
|
|
$closeStr = $this->formatProductTime($close);
|
|
|
|
|
|
|
|
|
|
return $current >= $openStr && $current <= $closeStr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 15:16:39 +03:30
|
|
|
/**
|
|
|
|
|
* آیا محصول بازه زمانی اختصاصی خودش را override کرده؟
|
|
|
|
|
* فقط وقتی سوییچ override_trading_hours روشن باشد و بازه معتبری (شروع != پایان) داشته باشد.
|
|
|
|
|
*/
|
|
|
|
|
public function productOverridesTradingHours(Product $product): bool
|
|
|
|
|
{
|
|
|
|
|
if (!Product::hasOverrideTradingHoursColumn()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!($product->override_trading_hours ?? false)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->hasProductTradingWindow($product->auto_open_at, $product->auto_close_at);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ساعت معامله یک محصول:
|
2026-06-29 02:11:36 +03:30
|
|
|
* - اگر سوییچ override محصول روشن باشد → فقط بازه اختصاصی خود محصول حاکم است (سراسری نادیده گرفته میشود).
|
|
|
|
|
* - اگر خاموش باشد → لایهٔ بیرونیِ ساعت کاری محصول (auto_open_at/auto_close_at) همیشه اعمال میشود،
|
|
|
|
|
* مستقل از اینکه close_price روشن باشد یا نه.
|
|
|
|
|
* - فقط اگر محصول هیچ بازهٔ معتبری نداشته باشد → فیلتر سراسری close_price + startDay/endDay اعمال میشود.
|
2026-06-28 15:16:39 +03:30
|
|
|
*/
|
2026-06-14 22:00:21 +03:30
|
|
|
public function isProductWithinTradingHours(Product $product, ?Carbon $now = null): bool
|
|
|
|
|
{
|
2026-06-28 15:16:39 +03:30
|
|
|
if ($this->productOverridesTradingHours($product)) {
|
|
|
|
|
return $this->isWithinTimeWindow($product->auto_open_at, $product->auto_close_at, $now);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-29 02:11:36 +03:30
|
|
|
if ($this->hasProductTradingWindow($product->auto_open_at, $product->auto_close_at)) {
|
|
|
|
|
return $this->isWithinTimeWindow($product->auto_open_at, $product->auto_close_at, $now);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 22:00:21 +03:30
|
|
|
if ($this->isClosePriceEnabled() && $this->isOutsideTradingHours($now)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 15:16:39 +03:30
|
|
|
return true;
|
2026-06-14 22:00:21 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function productTradingClosedMessage(Product $product): string
|
|
|
|
|
{
|
|
|
|
|
$title = trim((string) ($product->title ?? 'محصول'));
|
|
|
|
|
|
2026-06-29 02:11:36 +03:30
|
|
|
if (
|
|
|
|
|
$this->productOverridesTradingHours($product)
|
|
|
|
|
|| $this->hasProductTradingWindow($product->auto_open_at, $product->auto_close_at)
|
|
|
|
|
) {
|
2026-06-14 22:00:21 +03:30
|
|
|
$open = $this->formatProductTime($product->auto_open_at);
|
|
|
|
|
$close = $this->formatProductTime($product->auto_close_at);
|
|
|
|
|
|
|
|
|
|
return "محصول «{$title}» در حال حاضر بسته است. ساعت فعالسازی از {$open} تا {$close} میباشد.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->tradingClosedMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function assertProductTradingOpen(Product $product, ?Carbon $now = null): void
|
|
|
|
|
{
|
|
|
|
|
if (!$this->isProductWithinTradingHours($product, $now)) {
|
|
|
|
|
throw new UnprocessableEntityHttpException($this->productTradingClosedMessage($product));
|
2026-06-14 21:42:21 +03:30
|
|
|
}
|
|
|
|
|
}
|
2026-06-04 12:18:53 +03:30
|
|
|
}
|