2026-01-24 14:03:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Controllers\SuperController;
|
|
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\DB\Te\TableEngine;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use \App\Http\Controllers\Customer\FundController;
|
2026-05-25 19:42:48 +03:30
|
|
|
use App\Http\Controllers\Site\ReportController;
|
|
|
|
|
use App\Services\ConfigService;
|
2026-01-24 14:03:11 +00:00
|
|
|
|
|
|
|
|
class ProductController extends SuperController
|
|
|
|
|
{
|
2026-05-25 19:42:48 +03:30
|
|
|
|
|
|
|
|
private $configService;
|
|
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
2026-05-25 19:42:48 +03:30
|
|
|
$this->configService = app(ConfigService::class);
|
|
|
|
|
|
2026-05-17 16:39:53 +00:00
|
|
|
$this->with = ['file'/*, 'price', 'default', 'sub'*/];
|
2026-05-25 19:42:48 +03:30
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
parent::__construct(Product::class, $this->with, ['*'], null, null, ['title']);
|
|
|
|
|
}
|
2026-05-25 19:42:48 +03:30
|
|
|
|
|
|
|
|
private function shouldUpdatePriceOnList(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->configService->ConfigBool('product_update_price_on_list', true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function shouldAutoCloseOpenOnList(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->configService->ConfigBool('product_auto_close_open_on_list', true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
public function tableProduct(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$data = $request->all();
|
2026-05-25 19:42:48 +03:30
|
|
|
if (isset($data["select"]) && is_null($data["select"])) {
|
2026-01-24 14:03:11 +00:00
|
|
|
$data["select"] = [];
|
|
|
|
|
}
|
2026-05-25 19:42:48 +03:30
|
|
|
if (is_null($this->query)) {
|
2026-01-24 14:03:11 +00:00
|
|
|
$query_table = $this->model::with($this->with);
|
|
|
|
|
//*******کداضافه شده
|
|
|
|
|
// ->where('active', '<=', now()) // فیلتر محصولات فعال
|
|
|
|
|
// ->get();
|
|
|
|
|
//*********
|
2026-05-25 19:42:48 +03:30
|
|
|
} else {
|
2026-01-24 14:03:11 +00:00
|
|
|
$query_table = $this->query;
|
|
|
|
|
// $query_table = $query_table->where('active', '<=', now());
|
|
|
|
|
}
|
|
|
|
|
TableEngine::create($query_table, $data)
|
|
|
|
|
->initSearch($this->search)
|
|
|
|
|
->select($this->select)
|
|
|
|
|
->export($query_table);
|
|
|
|
|
$query_table['auto_open_at'] = Product::selectRaw(
|
|
|
|
|
'SEC_TO_TIME(AVG(TIME_TO_SEC(TIME(auto_open_at)))) as avg_time'
|
2026-05-25 19:42:48 +03:30
|
|
|
)->value('avg_time');
|
|
|
|
|
$query_table['auto_close_at'] = Product::selectRaw(
|
2026-01-24 14:03:11 +00:00
|
|
|
'SEC_TO_TIME(AVG(TIME_TO_SEC(TIME(auto_close_at)))) as avg_time'
|
|
|
|
|
)->value('avg_time');
|
|
|
|
|
|
|
|
|
|
// $query_table['11'] = $avgDuration;
|
|
|
|
|
// $query_table['11'] = Product::avg('auto_open_at');
|
|
|
|
|
// $query_table['112'] = Product::avg('auto_close_at');
|
|
|
|
|
return $query_table;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function editProducts(Request $request)
|
|
|
|
|
{
|
|
|
|
|
if (isset($request->auto_open_at, $request->auto_close_at)) {
|
|
|
|
|
Product::query()->update([
|
|
|
|
|
'auto_open_at' => $request->auto_open_at,
|
|
|
|
|
'auto_close_at' => $request->auto_close_at,
|
|
|
|
|
]);
|
|
|
|
|
return response()->json(['message' => 'Products updated successfully.']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json(['error' => 'auto_open_at and auto_close_at must be set'], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$data = $request->all();
|
|
|
|
|
|
|
|
|
|
$product = Product::create($data);
|
|
|
|
|
return $this->resultCreate($product->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
|
{
|
|
|
|
|
// return 1;
|
|
|
|
|
|
2026-05-25 19:42:48 +03:30
|
|
|
$data = $request->all();
|
|
|
|
|
if (isset($data["price"])) {
|
|
|
|
|
$data["reading_method"] = null;
|
|
|
|
|
}
|
|
|
|
|
if (isset($data["auto_close_at"], $data["auto_open_at"])) {
|
|
|
|
|
if ($data["auto_close_at"] == $data["auto_open_at"]) {
|
|
|
|
|
unset($data["auto_open_at"], $data["auto_close_at"]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-24 14:03:11 +00:00
|
|
|
|
|
|
|
|
// return $data['price'];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if(!isset($data['default'])){
|
|
|
|
|
// throw new UnprocessableEntityHttpException('مقادیر اعتبارات را وارد کنید');
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// $data['auto_open_at'] = Carbon::parse($data['auto_open_at'])->format('Y-m-d H:i:s');
|
|
|
|
|
// $data['auto_close_at'] = Carbon::parse($data['auto_close_at'])->format('Y-m-d H:i:s');
|
|
|
|
|
// Product::find($id)->update($data);
|
2026-05-25 19:42:48 +03:30
|
|
|
Product::findOrFail($id)->update($data);
|
2026-01-24 14:03:11 +00:00
|
|
|
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
|
|
|
|
Product::findOrFail($id)->delete();
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function listProduct()
|
|
|
|
|
{
|
2026-05-25 19:42:48 +03:30
|
|
|
if ($this->shouldUpdatePriceOnList()) {
|
|
|
|
|
$this->syncProductPrices();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->shouldAutoCloseOpenOnList()) {
|
|
|
|
|
$this->syncAutoCloseOpen();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 16:24:20 +00:00
|
|
|
$product = Product::with(['file'])->where('active', 1)->get();
|
2026-05-25 19:42:48 +03:30
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
return response()->json($product);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPrice()
|
|
|
|
|
{
|
2026-05-25 19:42:48 +03:30
|
|
|
$this->syncProductPrices();
|
2026-03-31 18:28:27 +03:30
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 19:42:48 +03:30
|
|
|
private function syncProductPrices(): void
|
|
|
|
|
{
|
|
|
|
|
$RPrices = $this->fetchPriceList();
|
|
|
|
|
|
|
|
|
|
if (empty($RPrices) || !is_array($RPrices)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$collection = collect($RPrices);
|
|
|
|
|
|
|
|
|
|
$products = Product::with([])->whereNotNull('reading_method')->get();
|
|
|
|
|
|
|
|
|
|
$copper = null;
|
|
|
|
|
$dollar = null;
|
|
|
|
|
|
|
|
|
|
foreach ($products as $product) {
|
|
|
|
|
$RPrice = $collection->firstWhere('key', $product->reading_method)
|
|
|
|
|
?: $collection->firstWhere('name', $product->reading_method);
|
|
|
|
|
|
|
|
|
|
if (empty($RPrice)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!isset($RPrice['price'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($RPrice['name'] ?? null) === 'TGJU فلز مس') {
|
|
|
|
|
$copper = ((float)$RPrice['price']) + ((float)($RPrice['profit'] ?? 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($RPrice['name'] ?? null) === 'TGJU دلار') {
|
|
|
|
|
$dollar = ((float)$RPrice['price']) + ((float)($RPrice['profit'] ?? 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($RPrice['name'] ?? null) === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
|
|
|
|
|
$RPrice['price'] = ((float)$RPrice['price']) / 4.3318;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Product::where('id', $product->id)->update([
|
|
|
|
|
'price' => $RPrice['price'],
|
|
|
|
|
'price_profit' => $RPrice['profit'] ?? 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ((int)$product->bubble === 1) {
|
|
|
|
|
$ReportController = new ReportController();
|
|
|
|
|
$liquidGold = $ReportController->lastPrice();
|
|
|
|
|
|
|
|
|
|
if (!empty($liquidGold['price'])) {
|
|
|
|
|
$p = Product::find($product->id);
|
|
|
|
|
$p->amount = ((float)$RPrice['price']) / ((float)$liquidGold['price']);
|
|
|
|
|
$p->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($copper !== null && $dollar !== null) {
|
|
|
|
|
$cp = ($copper / 1000000) * $dollar;
|
|
|
|
|
|
|
|
|
|
Product::where('metal_id', 3)->update([
|
|
|
|
|
'price' => $cp,
|
|
|
|
|
'price_profit' => 0,
|
|
|
|
|
'updated_at' => now(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function fetchPriceList(): array
|
|
|
|
|
{
|
|
|
|
|
$urls = [];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* اگر خواستی برای هر پروژه از دیتابیس URL بدی:
|
|
|
|
|
* product_price_api_url = http://...
|
|
|
|
|
*/
|
|
|
|
|
$customUrl = $this->configService->Config('product_price_api_url', null);
|
|
|
|
|
|
|
|
|
|
if (!empty($customUrl)) {
|
|
|
|
|
$urls[] = $customUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* اگر پروژه از مدل جدید قیمت استفاده میکنه
|
|
|
|
|
*/
|
|
|
|
|
if (
|
|
|
|
|
$this->configService->ConfigBool('tabanGohar_price', false) ||
|
|
|
|
|
$this->configService->ConfigBool('goldir_price', false)
|
|
|
|
|
) {
|
|
|
|
|
$urls[] = 'http://109.122.246.48:4949/api/free';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* fallback قدیمی
|
|
|
|
|
*/
|
|
|
|
|
$urls[] = 'http://212.23.201.173:8381/price_list';
|
|
|
|
|
|
|
|
|
|
foreach (array_unique($urls) as $url) {
|
|
|
|
|
try {
|
|
|
|
|
$response = Http::timeout(40)->get($url);
|
|
|
|
|
|
|
|
|
|
if (!$response->successful()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$json = $response->json();
|
|
|
|
|
|
|
|
|
|
if (isset($json['data']) && is_array($json['data'])) {
|
|
|
|
|
return $json['data'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_array($json) && !empty($json)) {
|
|
|
|
|
return $json;
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
report($e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
public function auto_close_open()
|
2026-05-25 19:42:48 +03:30
|
|
|
{
|
|
|
|
|
$this->syncAutoCloseOpen();
|
|
|
|
|
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function syncAutoCloseOpen(): void
|
2026-01-24 14:03:11 +00:00
|
|
|
{
|
|
|
|
|
$products = Product::with([])->get();
|
|
|
|
|
$current_time = now()->format('H:i:s');
|
2026-05-25 19:42:48 +03:30
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
foreach ($products as $product) {
|
2026-05-25 19:42:48 +03:30
|
|
|
if (empty($product->auto_open_at) || empty($product->auto_close_at)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
$show = ($current_time >= $product->auto_open_at && $current_time <= $product->auto_close_at);
|
2026-05-25 19:42:48 +03:30
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
Product::where('id', $product->id)->update([
|
|
|
|
|
'show' => $show,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-05-25 19:42:48 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function ProductById($id)
|
|
|
|
|
{
|
|
|
|
|
if ($this->shouldUpdatePriceOnList()) {
|
|
|
|
|
$this->syncProductPrices();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->shouldAutoCloseOpenOnList()) {
|
|
|
|
|
$this->syncAutoCloseOpen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$product = Product::with(['file'])
|
|
|
|
|
->where('id', $id)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (!$product) {
|
|
|
|
|
return response()->json(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$product->similar = Product::with(['file'])
|
|
|
|
|
->where('group_type', $product->group_type)
|
|
|
|
|
->where('id', '!=', $id)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
return response()->json($product);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getProduct($id)
|
|
|
|
|
{
|
|
|
|
|
$product = Product::with($this->with)->where('id', $id)->first();
|
|
|
|
|
return response()->json($product);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
*/
|
|
|
|
|
public function hide($id)
|
|
|
|
|
{
|
|
|
|
|
$query = Product::findOrFail($id);
|
|
|
|
|
$query->update([
|
|
|
|
|
'show' => !$query->show
|
|
|
|
|
]);
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function openSell($id)
|
|
|
|
|
{
|
|
|
|
|
return $this->toggleBooleanStatus($id, 'open_sell');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function openBuy($id)
|
|
|
|
|
{
|
|
|
|
|
return $this->toggleBooleanStatus($id, 'open_buy');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function auto_buy($id)
|
|
|
|
|
{
|
|
|
|
|
return $this->toggleBooleanStatus($id, 'auto_buy');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function auto_sell($id)
|
|
|
|
|
{
|
|
|
|
|
return $this->toggleBooleanStatus($id, 'auto_sell');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function toggleBooleanStatus($id, $field)
|
|
|
|
|
{
|
|
|
|
|
$product = Product::findOrFail($id);
|
|
|
|
|
$product->$field = !$product->$field;
|
|
|
|
|
$product->save();
|
|
|
|
|
if (!$product->open_buy and !$product->open_sell) {
|
|
|
|
|
$product->show = false;
|
|
|
|
|
$product->active = false;
|
|
|
|
|
} else {
|
|
|
|
|
$product->show = true;
|
|
|
|
|
$product->active = true;
|
|
|
|
|
}
|
|
|
|
|
$product->save();
|
|
|
|
|
return response()->json($product->$field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function auto_group($action, $method)
|
|
|
|
|
{
|
|
|
|
|
// Define valid actions and methods
|
|
|
|
|
$validActions = ['sell' => 'auto_sell', 'buy' => 'auto_buy', 'all' => ['auto_buy', 'auto_sell']];
|
|
|
|
|
$validMethods = ['true' => 1, 'false' => 0];
|
|
|
|
|
// Validate action and method
|
|
|
|
|
if (!isset($validActions[$action]) || !array_key_exists($method, $validMethods)) {
|
|
|
|
|
return response()->json(['error' => 'Invalid action or method'], 400);
|
|
|
|
|
}
|
|
|
|
|
// Get fields to update and value to set
|
|
|
|
|
$fields = $validActions[$action];
|
|
|
|
|
$value = $validMethods[$method];
|
|
|
|
|
// Update products
|
|
|
|
|
Product::query()->update(array_fill_keys((array)$fields, $value));
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 19:42:48 +03:30
|
|
|
public function updatePrice(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$this->syncProductPrices();
|
2026-01-24 14:03:11 +00:00
|
|
|
|
2026-05-25 19:42:48 +03:30
|
|
|
return response()->json(true);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
}
|