Files
boursecu_server/app/Http/Controllers/Admin/PriceController.php
T
2026-06-12 19:49:53 +03:30

111 lines
3.6 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\DB\Te\TableEngine;
use App\Http\Controllers\Controller;
use App\Models\Price;
use App\Models\PriceCandle;
use App\Services\ZaarboxPriceService;
use Illuminate\Http\Request;
class PriceController extends Controller
{
public function store(Request $request){
Price::create([
"price"=>$request->price ,
"date_at"=>date("Y-m-d"),
"time_at"=>now()->format("H:m:s"),
"user_id"=>auth('user')->id()
]);
$candle = PriceCandle::whereDate('date_at',date("Y-m-d"))->first();
if (!is_null($candle)){
$candle->end = $request->price;
if ($request->price < $candle->low){
$candle->low = $request->price;
}
elseif ($request->price > $candle->high){
$candle->high = $request->price;
}
$candle->save();
}else{
PriceCandle::create([
"start"=>$request->price,
"end"=>$request->price,
"high"=>$request->price,
"low"=>$request->price,
"date_at"=>date("Y-m-d")
]);
}
return response()->json(true);
}
public function priceData(Request $request){
$prise = Price::with('order');
TableEngine::create($prise, $request)
->initSearch(['price'])
->select(['*'])
->export($prise);
return response()->json($prise);
}
public function chartPrices(ZaarboxPriceService $zaarboxPriceService)
{
return response()->json([
'code' => 0,
'message' => 'عملیات با موفقیت انجام شد.',
'data' => [
'prices' => $zaarboxPriceService->getAllPrices(),
],
]);
}
public function zaarboxProducts(ZaarboxPriceService $zaarboxPriceService)
{
return response()->json([
'code' => 0,
'message' => 'عملیات با موفقیت انجام شد.',
'data' => [
'items' => $zaarboxPriceService->listProductOptions(),
'api_url' => $zaarboxPriceService->apiUrl(),
],
]);
}
/** لیست مرجع قیمت برای ادمین قدیمی (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())
->initSearch([])
->select([
'*'
])->export($records);
return response()->json($records);
}
}