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
@@ -71,6 +71,33 @@ class PriceController extends Controller
]);
}
/** لیست مرجع قیمت برای ادمین قدیمی (ghatreh_admin) — GET /admin/p */
public function methodPrices(ZaarboxPriceService $zaarboxPriceService)
{
$prices = $zaarboxPriceService->cachedPriceList(false);
if ($prices === null) {
return response()->json([]);
}
$items = collect($prices)
->map(function ($row) use ($zaarboxPriceService) {
$row = is_array($row) ? $row : (array) $row;
$normalized = $zaarboxPriceService->normalizeFeedRow($row);
return [
'id' => trim((string) ($row['id'] ?? $row['key'] ?? '')),
'key' => trim((string) ($row['key'] ?? '')),
'name' => trim((string) ($row['name'] ?? '')),
'price' => (int) round((float) ($normalized['price'] ?? 0)),
'profit' => (int) round((float) ($normalized['profit'] ?? 0)),
];
})
->filter(fn (array $row) => $row['name'] !== '')
->values();
return response()->json($items);
}
public function table(Request $request){
$records = Price::with(['user']);
TableEngine::create($records, $request->all())
@@ -14,8 +14,10 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Illuminate\Support\Facades\Http;
use \App\Http\Controllers\Customer\FundController;
use App\Http\Controllers\Site\ReportController;
use App\Services\ConfigService;
use App\Services\ProductDisplayPriceService;
use App\Services\ProductPriceService;
use App\Services\ProductPriceSyncService;
use Illuminate\Support\Facades\Schema;
@@ -151,9 +153,18 @@ class ProductController extends SuperController
public function listProduct()
{
// ادمین: sync اختیاری از کش — کاربر: فقط enrich از کش کران (بدون HTTP / بدون UPDATE)
if (app(ConfigService::class)->isStore()) {
app(ProductPriceSyncService::class)->syncStorePrices(true);
$this->auto_close_open();
return response()->json(
Product::with(['file'])->where('active', 1)->get()
);
}
// آبشده خانگی — ادمین: sync از کش؛ کاربر: enrich بدون UPDATE
if (! str_contains(request()->path(), '/customer/')) {
app(\App\Services\ProductPriceSyncService::class)->syncAll(false);
app(ProductPriceSyncService::class)->syncAll(false);
}
$products = Product::with(['file'])->where('active', 1)->get();
@@ -162,7 +173,9 @@ class ProductController extends SuperController
$displayPriceService = app(ProductDisplayPriceService::class);
$displayPriceService->enrichForApi($products);
$products->each(function (Product $product) use ($displayPriceService) {
$product->setAttribute('show', $displayPriceService->resolveCustomerShow($product));
if (! $displayPriceService->hasCustomerDisplayPrice($product)) {
$product->setAttribute('show', false);
}
});
}
@@ -179,7 +192,7 @@ class ProductController extends SuperController
public function ProductById($id)
{
$this->getPrice();
app(ProductPriceSyncService::class)->syncStorePrices(true);
$this->auto_close_open();
$product = Product::with(['file'])
->where('id', $id)
@@ -199,7 +212,11 @@ class ProductController extends SuperController
public function getPrice()
{
app(\App\Services\ProductPriceSyncService::class)->syncAll(true);
if (app(ConfigService::class)->isStore()) {
app(ProductPriceSyncService::class)->syncStorePrices(true);
} else {
app(ProductPriceSyncService::class)->syncAll(true);
}
return response()->json(true);
}
@@ -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::