diff --git a/app/Http/Controllers/Admin/PriceController.php b/app/Http/Controllers/Admin/PriceController.php index 16de69c..0c6c7f6 100644 --- a/app/Http/Controllers/Admin/PriceController.php +++ b/app/Http/Controllers/Admin/PriceController.php @@ -6,6 +6,7 @@ use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\Price; use App\Models\PriceCandle; +use App\Services\MontzarService; use App\Services\ZaarboxPriceService; use Illuminate\Http\Request; @@ -71,6 +72,17 @@ class PriceController extends Controller ]); } + public function montzarProducts(MontzarService $montzarService) + { + return response()->json([ + 'code' => 0, + 'message' => 'عملیات با موفقیت انجام شد.', + 'data' => [ + 'items' => $montzarService->listProductOptions(), + ], + ]); + } + /** لیست مرجع قیمت برای ادمین قدیمی (ghatreh_admin) — GET /admin/p */ public function methodPrices(ZaarboxPriceService $zaarboxPriceService) { diff --git a/app/Services/MontzarService.php b/app/Services/MontzarService.php index ba5fa66..127d233 100644 --- a/app/Services/MontzarService.php +++ b/app/Services/MontzarService.php @@ -118,6 +118,56 @@ class MontzarService return $response; } + /** + * @return array + */ + public function listProductOptions(): array + { + try { + $response = $this->productList(); + } catch (\Throwable $e) { + Log::warning('Montzar product options failed', ['error' => $e->getMessage()]); + + return []; + } + + if (! is_array($response) || (isset($response['message']) && ! array_is_list($response))) { + return []; + } + + return collect($response) + ->filter(fn ($row) => is_array($row) && array_key_exists('id', $row)) + ->map(function (array $row) { + $sub = is_array($row['sub_agent'] ?? null) ? $row['sub_agent'] : []; + $buy = (float) ($sub['rate_buy_gram_price'] ?? 0); + $sell = (float) ($sub['rate_sell_gram_price'] ?? 0); + $price = ($buy > 0 && $sell > 0) + ? (int) round(($buy + $sell) / 2) + : (int) round(max($buy, $sell)); + + $id = (int) ($row['id'] ?? 0); + $name = trim((string) ( + $row['name_fa'] + ?? $row['name'] + ?? $row['title'] + ?? $row['product_name'] + ?? '' + )); + + return [ + 'id' => (string) $id, + 'name' => $name !== '' ? $name : ('محصول #' . $id), + 'price' => $price, + 'buy' => (int) round($buy), + 'sell' => (int) round($sell), + ]; + }) + ->filter(fn (array $row) => $row['id'] !== '' && $row['id'] !== '0') + ->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE) + ->values() + ->all(); + } + /** * مانده طلا/ریال اکانت اپ روی نود همکار (دفتر پنل همکار — نه ته‌حساب اپ). * diff --git a/routes/admin.php b/routes/admin.php index 3dde05c..fb556c1 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -187,6 +187,7 @@ Route::middleware('auth:user')->group(function (){ Route::get('price-sync-logs/{id}', [PriceSyncLogController::class, 'show']); Route::post('price-sync/run', [PriceSyncLogController::class, 'runSync']); Route::get('zaarbox/products', [PriceController::class, 'zaarboxProducts']); + Route::get('montzar/products', [PriceController::class, 'montzarProducts']); Route::post('finance', [FinanceController::class, 'fall']);