completed liman for using in sell and buy

This commit is contained in:
2026-05-24 22:29:43 +03:30
parent 81d4aa8926
commit 7f0f91701e
4 changed files with 591 additions and 99 deletions
+164 -93
View File
@@ -126,6 +126,7 @@ class ReportController extends Controller
$liman_open_sell = false;
$priceSource = null;
$priceValue = 0;
$toBool = function ($value) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
@@ -133,7 +134,7 @@ class ReportController extends Controller
/*
|--------------------------------------------------------------------------
| بستن قیمت طبق منطق تابع اول
| بستن قیمت طبق منطق قبلی
|--------------------------------------------------------------------------
*/
if ($this->Config('close_price') == 'true') {
@@ -144,8 +145,8 @@ class ReportController extends Controller
[$endHour, $endMinute] = explode(':', $endDay);
$current = Carbon::now();
$start = Carbon::today()->setTime((int)$startHour, (int)$startMinute);
$end = Carbon::today()->setTime((int)$endHour, (int)$endMinute);
$start = Carbon::today()->setTime((int) $startHour, (int) $startMinute);
$end = Carbon::today()->setTime((int) $endHour, (int) $endMinute);
if (!$current->between($start, $end)) {
Cnt::where('key', 'app_closed')->update(['value' => "true"]);
@@ -164,16 +165,16 @@ class ReportController extends Controller
'closed' => true,
'time' => now()->format('H:i:s'),
];
} 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"]);
}
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
|--------------------------------------------------------------------------
*/
$settings = Setting::whereIn('key', [
@@ -192,97 +193,144 @@ class ReportController extends Controller
$gold_ir_price = $settingsMap->get('goldir_price');
$main_product_price = $settingsMap->get('main_product_price');
$diff_buy = (float) $settingsMap->get('buy_fee', 0);
$diff_sell = (float) $settingsMap->get('sell_fee', 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'),
]);
/*
|--------------------------------------------------------------------------
| Liman - اولویت اول
| Montpar / Liman - اولویت اول
|--------------------------------------------------------------------------
*/
if ($liman_price_enabled && !$closed) {
try {
$liman = Http::timeout(7)
->withOptions([
'curl' => [
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_TCP_FASTOPEN => 1,
]
])
->withToken(Setting::where('key', 'liman_token')->value('value'))
->get('http://212.23.201.173/server/customer/product/list')
->json();
$limanBaseUrl = rtrim($this->Config('liman_base_url') ?: 'https://montpar.ir', '/');
$limanProductId = (int) ($this->Config('liman_product_id') ?: 25);
if ((isset($liman['message']) && $liman['message'] == 'no authorised') || $liman == null) {
$newToken = app(LimanService::class)->liman_login();
$liman = app(LimanService::class)->productList();
$liman = Http::timeout(7)
->withToken($newToken)
->get('http://212.23.201.173/server/customer/product/list')
->json();
\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']);
}
$collection = collect($liman);
$lpLiman = $collection->firstWhere('id', 25);
$lpLiman = $collection->firstWhere('id', $limanProductId);
if ($lpLiman && isset($lpLiman['sub_agent'])) {
$buy = (float) $lpLiman['sub_agent']['rate_buy_gram_price'];
$sell = (float) $lpLiman['sub_agent']['rate_sell_gram_price'];
\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,
]);
$customPrice = [];
$customPrice['price'] = ($buy + $sell) / 2;
$customPrice['diff'] = ($buy - $sell) / 2;
if (!$lpLiman) {
throw new \Exception("Provider product not found. product_id={$limanProductId}");
}
Cnt::where('key', 'liman_price')->update(['value' => (float) $customPrice['price']]);
Cnt::where('key', 'liman_diff')->update(['value' => (float) $customPrice['diff']]);
if (!isset($lpLiman['sub_agent'])) {
throw new \Exception("Provider product has no sub_agent. product_id={$limanProductId}");
}
Cnt::where('key', 'liman_mazaneh')->update([
'value' => ((float) $customPrice['price'] * 4.331802)
]);
$buy = (float) ($lpLiman['sub_agent']['rate_buy_gram_price'] ?? 0);
$sell = (float) ($lpLiman['sub_agent']['rate_sell_gram_price'] ?? 0);
Cnt::where('key', 'liman_truly_buy_mazaneh')->update([
'value' => $lpLiman['sub_agent']['rate_buy_price']
]);
if ($buy <= 0 || $sell <= 0) {
throw new \Exception("Provider invalid gram price. buy={$buy}, sell={$sell}");
}
Cnt::where('key', 'liman_truly_sell_mazaneh')->update([
'value' => $lpLiman['sub_agent']['rate_sell_price']
]);
$customPrice = [];
$customPrice['price'] = ($buy + $sell) / 2;
$customPrice['diff'] = ($buy - $sell) / 2;
Cnt::where('key', 'log')->update([
'value' => json_encode($lpLiman, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
]);
Cnt::where('key', 'liman_price')->update([
'value' => (float) $customPrice['price']
]);
$liman_open_sell = $toBool($lpLiman['open_sell'] ?? false);
$liman_open_buy = $toBool($lpLiman['open_buy'] ?? false);
Cnt::where('key', 'liman_diff')->update([
'value' => (float) $customPrice['diff']
]);
Cnt::where('key', 'liman_open_sell')->update([
'value' => $liman_open_sell ? "true" : "false"
]);
Cnt::where('key', 'liman_mazaneh')->update([
'value' => ((float) $customPrice['price'] * 4.331802)
]);
Cnt::where('key', 'liman_open_buy')->update([
'value' => $liman_open_buy ? "true" : "false"
]);
Cnt::where('key', 'liman_truly_buy_mazaneh')->update([
'value' => $lpLiman['sub_agent']['rate_buy_price'] ?? 0
]);
if ($liman_open_sell || $liman_open_buy) {
$liman_price = (int) $customPrice['price'];
}
Cnt::where('key', 'liman_truly_sell_mazaneh')->update([
'value' => $lpLiman['sub_agent']['rate_sell_price'] ?? 0
]);
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) {
\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,
]);
} catch (\Throwable $e) {
$liman_price = 0;
\Log::error('PROVIDER PRICE ERROR', [
'base_url' => $limanBaseUrl ?? null,
'message' => $e->getMessage(),
'liman_product_id' => $limanProductId ?? null,
'liman_response' => $liman ?? null,
]);
Cnt::where('key', 'liman_closed')->update([
'value' => "true"
]);
}
}
/*
|--------------------------------------------------------------------------
| Goldir
@@ -322,8 +370,13 @@ class ReportController extends Controller
$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_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)
@@ -360,7 +413,7 @@ class ReportController extends Controller
Cnt::where('key', 'goldir_closed')->update([
'value' => $goldir_price == 0 ? "true" : "false"
]);
} catch (\Exception $e) {
} catch (\Throwable $e) {
$goldir_price = 0;
Cnt::where('key', 'goldir_closed')->update([
@@ -402,7 +455,7 @@ class ReportController extends Controller
->json();
$tgn = (int) $taban_gohar['c'] * 10;
} catch (\Exception $e) {
} catch (\Throwable $e) {
$tgn = 0;
}
}
@@ -410,8 +463,8 @@ class ReportController extends Controller
/*
|--------------------------------------------------------------------------
| انتخاب قیمت اصلی
| اولویت جدید:
| Liman -> Goldir -> Taban Gohar -> Main Product
| اولویت:
| Liman/Montpar -> Goldir -> Taban Gohar -> Main Product
|--------------------------------------------------------------------------
*/
if ($liman_price_enabled && ($liman_price ?? 0) !== 0 && !$closed) {
@@ -428,6 +481,35 @@ class ReportController extends Controller
$priceSource = 'main_product';
} else {
$priceValue = 0;
$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,
]);
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(),
];
}
/*
@@ -473,53 +555,41 @@ class ReportController extends Controller
}
}
$price = Price::orderBy('id', 'desc')->first();
if (!$price) {
return [];
}
$priceValue = $price->price;
/*
|--------------------------------------------------------------------------
| محاسبه sell / buy با حفظ ساختار خروجی تابع اول
| محاسبه sell / buy با همان priceValue همین اجرای فعلی
|--------------------------------------------------------------------------
*/
if ($priceSource === 'liman' && $liman_price !== 0) {
if ($priceSource === 'liman') {
$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)
$sell = $liman_open_sell
? ceil($priceValue - $final_sell_diff)
: 0;
$buy = $priceValue !== 0
? ($liman_open_buy ? ceil($price->price + $final_buy_diff) : 0)
$buy = $liman_open_buy
? ceil($priceValue + $final_buy_diff)
: 0;
$priceLast = $priceValue !== 0
? ceil($price->price + $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 = $priceValue !== 0
? ($open_sell ? ceil($price->price - $difference) : 0)
$sell = $open_sell
? ceil($priceValue - $difference)
: 0;
$buy = $priceValue !== 0
? ($open_buy ? ceil($price->price + $difference) : 0)
$buy = $open_buy
? ceil($priceValue + $difference)
: 0;
$priceLast = $priceValue !== 0
? ceil($price->price + $difference)
: 0;
$priceLast = ceil($priceValue + $difference);
}
return [
@@ -531,11 +601,12 @@ class ReportController extends Controller
'taban_gohar' => $tgn ?? 0,
'goldir' => $goldir_price ?? 0,
'liman' => $liman_price ?? 0,
'closed' => $closed ?? null,
'closed' => false,
'time' => now(),
];
}
public function check()
{
$service = Http::