montzar added

This commit is contained in:
2026-05-25 12:47:04 +03:30
parent 7f0f91701e
commit b84ea8aabe
5 changed files with 232 additions and 428 deletions
+133 -275
View File
@@ -9,18 +9,17 @@ use App\Models\Cnt;
use App\Models\Setting;
use App\Models\ConfigStatic;
use App\Models\Customer;
use App\Services\LimanService;
use App\Services\MontzarService;
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;
use App\Http\Controllers\Admin\ProductController;
use App\Services\ConfigService;
class ReportController extends Controller
@@ -112,18 +111,19 @@ class ReportController extends Controller
public function lastPrice()
{
$config = app(ConfigService::class);
$closed = false;
$goldir_price = 0;
$liman_price = 0;
$montzar_price = 0;
$tgn = 0;
$pr_price = 0;
$open_buy = true;
$open_sell = true;
$liman_open_buy = false;
$liman_open_sell = false;
$montzar_open_buy = false;
$montzar_open_sell = false;
$priceSource = null;
$priceValue = 0;
@@ -132,16 +132,30 @@ class ReportController extends Controller
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
};
$zeroResponse = function () {
return [
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => 0,
'taban_gohar' => 0,
'montzar' => 0,
'closed' => true,
'time' => now(),
];
};
/*
|--------------------------------------------------------------------------
| بستن قیمت طبق منطق قبلی
|--------------------------------------------------------------------------
*/
if ($this->Config('close_price') == 'true') {
$startDay = $this->Config('startDay');
if ($toBool($config->Config('close_price'))) {
$startDay = $config->Config('startDay') ?: '00:00';
[$startHour, $startMinute] = explode(':', $startDay);
$endDay = $this->Config('endDay');
$endDay = $config->Config('endDay') ?: '23:59';
[$endHour, $endMinute] = explode(':', $endDay);
$current = Carbon::now();
@@ -149,27 +163,16 @@ class ReportController extends Controller
$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"]);
Cnt::where('key', 'liman_closed')->update(['value' => "true"]);
// عمومی: در Cnt بماند
Cnt::where('key', 'app_closed')->update(['value' => 'true']);
return [
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => 0,
'taban_gohar' => 0,
'goldir' => 0,
'liman' => 0,
'closed' => true,
'time' => now()->format('H:i:s'),
];
// مربوط به منتزر: settings
$config->UpdateConfig('montzar_closed', 'true');
return $zeroResponse();
}
Cnt::where('key', 'app_closed')->update(['value' => "false"]);
Cnt::where('key', 'goldir_closed')->update(['value' => "false"]);
Cnt::where('key', 'liman_closed')->update(['value' => "false"]);
Cnt::where('key', 'app_closed')->update(['value' => 'false']);
}
/*
@@ -179,83 +182,53 @@ class ReportController extends Controller
*/
$settings = Setting::whereIn('key', [
'fee_single',
'goldir_price',
'tabanGohar_price',
'main_product_price',
'buy_fee',
'sell_fee',
'show_last_price',
])->get();
$settingsMap = $settings->pluck('value', 'key');
$show_last_price = $toBool($settingsMap->get('show_last_price', false));
$taban_gohar_price = $settingsMap->get('tabanGohar_price');
$difference = (float) $settingsMap->get('fee_single', 0);
$gold_ir_price = $settingsMap->get('goldir_price');
$main_product_price = $settingsMap->get('main_product_price');
$difference = (float) $settingsMap->get('fee_single', 0);
$diff_buy = (float) $settingsMap->get('buy_fee', $difference);
$diff_sell = (float) $settingsMap->get('sell_fee', $difference);
$liman_price_enabled = $toBool($this->Config('liman_price'));
\Log::info('LAST_PRICE DEBUG START', [
'file' => __FILE__,
'close_price' => $this->Config('close_price'),
'liman_price_raw' => $this->Config('liman_price'),
'liman_price_enabled' => $liman_price_enabled,
'liman_product_id' => $this->Config('liman_product_id'),
'liman_base_url' => $this->Config('liman_base_url'),
'liman_token_exists' => (bool) Setting::where('key', 'liman_token')->value('value'),
]);
$montzar_price_enabled = $toBool($config->Config('montzar_price'));
/*
|--------------------------------------------------------------------------
| Montpar / Liman - اولویت اول
| Montzar - اولویت اول
|--------------------------------------------------------------------------
*/
if ($liman_price_enabled && !$closed) {
if ($montzar_price_enabled && !$closed) {
try {
$limanBaseUrl = rtrim($this->Config('liman_base_url') ?: 'https://montpar.ir', '/');
$limanProductId = (int) ($this->Config('liman_product_id') ?: 25);
$montzarBaseUrl = rtrim($config->Config('montzar_base_url') ?: 'https://montpar.ir', '/');
$montzarProductId = (int) ($config->Config('montzar_product_id') ?: 25);
$liman = app(LimanService::class)->productList();
$montzar = app(MontzarService::class)->productList();
\Log::info('PROVIDER FIRST RESPONSE', [
'base_url' => $limanBaseUrl,
'product_id' => $limanProductId,
'type' => gettype($liman),
'message' => is_array($liman) ? ($liman['message'] ?? null) : null,
'available_ids' => is_array($liman) ? collect($liman)->pluck('id')->values()->all() : null,
]);
if (isset($liman['message'])) {
throw new \Exception('Provider message: ' . $liman['message']);
if (isset($montzar['message'])) {
throw new \Exception('Provider message: ' . $montzar['message']);
}
$collection = collect($liman);
$lpLiman = $collection->firstWhere('id', $limanProductId);
$collection = collect($montzar);
$montzarProduct = $collection->firstWhere('id', $montzarProductId);
\Log::info('PROVIDER PRODUCT PICKED', [
'base_url' => $limanBaseUrl,
'liman_product_id' => $limanProductId,
'found' => (bool) $lpLiman,
'has_sub_agent' => $lpLiman && isset($lpLiman['sub_agent']),
'open_buy' => $lpLiman['open_buy'] ?? null,
'open_sell' => $lpLiman['open_sell'] ?? null,
'buy_gram' => $lpLiman['sub_agent']['rate_buy_gram_price'] ?? null,
'sell_gram' => $lpLiman['sub_agent']['rate_sell_gram_price'] ?? null,
]);
if (!$lpLiman) {
throw new \Exception("Provider product not found. product_id={$limanProductId}");
if (!$montzarProduct) {
throw new \Exception("Provider product not found. product_id={$montzarProductId}");
}
if (!isset($lpLiman['sub_agent'])) {
throw new \Exception("Provider product has no sub_agent. product_id={$limanProductId}");
if (!isset($montzarProduct['sub_agent'])) {
throw new \Exception("Provider product has no sub_agent. product_id={$montzarProductId}");
}
$buy = (float) ($lpLiman['sub_agent']['rate_buy_gram_price'] ?? 0);
$sell = (float) ($lpLiman['sub_agent']['rate_sell_gram_price'] ?? 0);
$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("Provider invalid gram price. buy={$buy}, sell={$sell}");
@@ -265,160 +238,46 @@ class ReportController extends Controller
$customPrice['price'] = ($buy + $sell) / 2;
$customPrice['diff'] = ($buy - $sell) / 2;
Cnt::where('key', 'liman_price')->update([
'value' => (float) $customPrice['price']
]);
$config->UpdateConfig('montzar_price', (float) $customPrice['price']);
$config->UpdateConfig('montzar_diff', (float) $customPrice['diff']);
$config->UpdateConfig('montzar_mazaneh', ((float) $customPrice['price'] * 4.331802));
Cnt::where('key', 'liman_diff')->update([
'value' => (float) $customPrice['diff']
]);
$config->UpdateConfig(
'montzar_truly_buy_mazaneh',
$montzarProduct['sub_agent']['rate_buy_price'] ?? 0
);
Cnt::where('key', 'liman_mazaneh')->update([
'value' => ((float) $customPrice['price'] * 4.331802)
]);
$config->UpdateConfig(
'montzar_truly_sell_mazaneh',
$montzarProduct['sub_agent']['rate_sell_price'] ?? 0
);
Cnt::where('key', 'liman_truly_buy_mazaneh')->update([
'value' => $lpLiman['sub_agent']['rate_buy_price'] ?? 0
]);
$config->UpdateConfig(
'montzar_last_product_log',
json_encode($montzarProduct, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
);
Cnt::where('key', 'liman_truly_sell_mazaneh')->update([
'value' => $lpLiman['sub_agent']['rate_sell_price'] ?? 0
]);
$montzar_open_sell = $toBool($montzarProduct['open_sell'] ?? false);
$montzar_open_buy = $toBool($montzarProduct['open_buy'] ?? false);
Cnt::where('key', 'log')->update([
'value' => json_encode($lpLiman, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
]);
$config->UpdateConfig('montzar_open_sell', $montzar_open_sell ? 'true' : 'false');
$config->UpdateConfig('montzar_open_buy', $montzar_open_buy ? 'true' : 'false');
$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'];
if ($montzar_open_sell || $montzar_open_buy) {
$montzar_price = (int) $customPrice['price'];
}
Cnt::where('key', 'liman_closed')->update([
'value' => $liman_price == 0 ? "true" : "false"
]);
\Log::info('PROVIDER PRICE SUCCESS', [
'base_url' => $limanBaseUrl,
'liman_product_id' => $limanProductId,
'liman_price' => $liman_price,
'liman_diff' => $customPrice['diff'],
'open_buy' => $liman_open_buy,
'open_sell' => $liman_open_sell,
]);
$config->UpdateConfig('montzar_closed', $montzar_price == 0 ? 'true' : 'false');
} catch (\Throwable $e) {
$liman_price = 0;
$montzar_price = 0;
\Log::error('PROVIDER PRICE ERROR', [
'base_url' => $limanBaseUrl ?? null,
\Log::error('MONZAR PRICE ERROR', [
'message' => $e->getMessage(),
'liman_product_id' => $limanProductId ?? null,
'liman_response' => $liman ?? null,
'montzar_product_id' => $montzarProductId ?? null,
'montzar_response' => $montzar ?? null,
]);
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 (\Throwable $e) {
$goldir_price = 0;
Cnt::where('key', 'goldir_closed')->update([
'value' => "true"
]);
$config->UpdateConfig('montzar_closed', 'true');
}
}
@@ -426,10 +285,12 @@ class ReportController extends Controller
|--------------------------------------------------------------------------
| Main Product
|--------------------------------------------------------------------------
| عمومی است، پس از Cnt خوانده می‌شود.
|--------------------------------------------------------------------------
*/
if ($main_product_price == 'true' && !$closed) {
if ($toBool($main_product_price) && !$closed) {
$main_product_id = Cnt::where('key', 'main_product')->value('value');
$pr_product = Product::find($main_product_id);
$pr_product = $main_product_id ? Product::find($main_product_id) : null;
if ($pr_product) {
$pr_price = (float) $pr_product['price'] + (float) $pr_product['price_profit'];
@@ -441,13 +302,13 @@ class ReportController extends Controller
| Taban Gohar
|--------------------------------------------------------------------------
*/
if ($taban_gohar_price == 'true' && !$closed) {
if ($toBool($taban_gohar_price) && !$closed) {
try {
$taban_gohar = Http::withOptions([
'curl' => [
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_TCP_FASTOPEN => 1,
]
],
])
->withoutVerifying()
->timeout(2)
@@ -464,19 +325,16 @@ class ReportController extends Controller
|--------------------------------------------------------------------------
| انتخاب قیمت اصلی
| اولویت:
| Liman/Montpar -> Goldir -> Taban Gohar -> Main Product
| Montzar -> 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) {
if ($montzar_price_enabled && ($montzar_price ?? 0) !== 0 && !$closed) {
$priceValue = (int) $montzar_price;
$priceSource = 'montzar';
} elseif ($toBool($taban_gohar_price) && ($tgn ?? 0) !== 0 && !$closed) {
$priceValue = $tgn;
$priceSource = 'taban_gohar';
} elseif ($main_product_price == 'true' && ($pr_price ?? 0) !== 0 && !$closed) {
} elseif ($toBool($main_product_price) && ($pr_price ?? 0) !== 0 && !$closed) {
$priceValue = $pr_price;
$priceSource = 'main_product';
} else {
@@ -484,32 +342,39 @@ class ReportController extends Controller
$priceSource = null;
}
/*
|--------------------------------------------------------------------------
| اگر هیچ منبعی قیمت نداد، قیمت قبلی دیتابیس را برنگردان
|--------------------------------------------------------------------------
*/
if ($priceValue <= 0) {
\Log::warning('LAST_PRICE NO SOURCE PRICE', [
'liman_price' => $liman_price,
'goldir_price' => $goldir_price,
'taban_gohar' => $tgn,
'main_product' => $pr_price,
'price_source' => $priceSource,
]);
if (!$show_last_price) {
return [
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => $pr_price ?? 0,
'taban_gohar' => $tgn ?? 0,
'montzar' => $montzar_price ?? 0,
'closed' => true,
'time' => now(),
];
}
return [
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => $pr_price ?? 0,
'taban_gohar' => $tgn ?? 0,
'goldir' => $goldir_price ?? 0,
'liman' => $liman_price ?? 0,
'closed' => true,
'time' => now(),
];
$lastPrice = Price::latest()->first();
if (!$lastPrice || (float) $lastPrice->price <= 0) {
return [
'sell' => 0,
'buy' => 0,
'price_last' => 0,
'price' => 0,
'main_product' => $pr_price ?? 0,
'taban_gohar' => $tgn ?? 0,
'montzar' => $montzar_price ?? 0,
'closed' => true,
'time' => now(),
];
}
$priceValue = (float) $lastPrice->price;
$priceSource = 'database_last';
}
/*
@@ -546,41 +411,36 @@ class ReportController extends Controller
$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'),
]);
}
}
/*
|--------------------------------------------------------------------------
| محاسبه sell / buy با همان priceValue همین اجرای فعلی
| محاسبه sell / buy
|--------------------------------------------------------------------------
*/
if ($priceSource === 'liman') {
$liman_profit = (float) Cnt::where('key', 'liman_diff')->value('value');
if ($priceSource === 'montzar') {
$montzar_profit = (float) $config->Config('montzar_diff');
$final_buy_diff = $diff_buy + $liman_profit;
$final_sell_diff = $diff_sell + $liman_profit;
$final_buy_diff = $diff_buy + $montzar_profit;
$final_sell_diff = $diff_sell + $montzar_profit;
$sell = $liman_open_sell
$sell = $montzar_open_sell
? ceil($priceValue - $final_sell_diff)
: 0;
$buy = $liman_open_buy
$buy = $montzar_open_buy
? ceil($priceValue + $final_buy_diff)
: 0;
$priceLast = ceil($priceValue + $final_buy_diff);
} else {
if ($priceSource === 'goldir' && $goldir_price !== 0) {
$goldir_profit = (float) Cnt::where('key', 'goldir_diff')->value('value');
$difference = $goldir_profit;
}
$sell = $open_sell
? ceil($priceValue - $difference)
: 0;
@@ -599,14 +459,12 @@ class ReportController extends Controller
'price' => $priceValue,
'main_product' => $pr_price ?? 0,
'taban_gohar' => $tgn ?? 0,
'goldir' => $goldir_price ?? 0,
'liman' => $liman_price ?? 0,
'montzar' => $montzar_price ?? 0,
'closed' => false,
'time' => now(),
];
}
public function check()
{
$service = Http::