added price_offset_percent

This commit is contained in:
2026-06-02 16:00:33 +03:30
parent 6cbbde9834
commit c40ac0a3fe
6 changed files with 215 additions and 36 deletions
@@ -15,6 +15,7 @@ use Illuminate\Support\Facades\Http;
use \App\Http\Controllers\Customer\FundController;
use App\Http\Controllers\Site\ReportController;
use App\Services\ConfigService;
use App\Services\ProductPriceService;
class ProductController extends SuperController
@@ -73,7 +74,7 @@ class ProductController extends SuperController
public function store(Request $request)
{
$data = $request->all();
$data = $this->filterProductData($request->all());
$product = Product::create($data);
return $this->resultCreate($product->id);
@@ -83,9 +84,12 @@ class ProductController extends SuperController
{
// return 1;
$data = $request->all();
$data = $this->filterProductData($request->all());
if (isset($data["price"])){
$data["reading_method"] = null;
if (Product::hasPriceOffsetColumn()) {
$data['price_offset_percent'] = 0;
}
}
if (isset($data["auto_close_at"]) and isset($data["auto_close_at"])){
if ($data["auto_close_at"] == $data["auto_open_at"]){
@@ -103,8 +107,23 @@ class ProductController extends SuperController
// $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);
Product::find($id)->update($data);
$product = Product::findOrFail($id);
$oldOffsetPercent = (float) ($product->price_offset_percent ?? 0);
if (
Product::hasPriceOffsetColumn()
&& $product->reading_method
&& array_key_exists('price_offset_percent', $data)
) {
ProductPriceService::reapplyOffsetPercent(
$product,
(float) $data['price_offset_percent'],
$oldOffsetPercent
);
unset($data['price'], $data['price_profit']);
}
$product->update($data);
return response()->json(true);
}
@@ -121,8 +140,13 @@ class ProductController extends SuperController
{
// $this->getPrice();
// $this->auto_close_open();
$product = Product::with(['file'])->where('active', 1)->get();
return response()->json($product);
$products = Product::with(['file'])->where('active', 1)->get();
if (Product::hasPriceOffsetColumn() && str_contains(request()->path(), '/customer/')) {
$products->each->makeHidden(['price_offset_percent']);
}
return response()->json($products);
}
public function getPrice()
@@ -159,11 +183,16 @@ class ProductController extends SuperController
if ($RPrice['name'] == 'گلد دات ای از آبشده - رسمی - نقد فردا') {
$RPrice['price'] = $RPrice['price'] / 4.3318;
}
$p = Product::where('id', $product->id)->update([
'price' => $RPrice['price'],
'price_profit' => $RPrice['profit'],
]);
$productPrice = (float) $RPrice['price'] + (float) ($RPrice['profit'] ?? 0);
ProductPriceService::syncFromReference(
$product,
(int) $RPrice['price'],
(int) ($RPrice['profit'] ?? 0)
);
$productPrice = (float) ProductPriceService::adjustedTotal(
(int) $RPrice['price'],
(int) ($RPrice['profit'] ?? 0),
ProductPriceService::offsetPercent($product)
);
if (
$hasExtremeKeys
&& $mainProductId
@@ -183,10 +212,12 @@ class ProductController extends SuperController
}
}
if (isset($copper) && isset($dollar)) {
$cp = ($copper / 1000000) * $dollar;
$p = Product::where('metal_id', 3)->update([
'price' => $cp,
'price_profit' => 0,
$cp = (int) round(($copper / 1000000) * $dollar);
$copperProducts = Product::where('metal_id', 3)->get();
foreach ($copperProducts as $copperProduct) {
ProductPriceService::syncFromReference($copperProduct, $cp, 0);
}
Product::where('metal_id', 3)->update([
'updated_at' => \Carbon\Carbon::now()->format('Y-m-d H:i:s'),
]);
if (
@@ -194,7 +225,11 @@ class ProductController extends SuperController
&& $mainProductId
&& Product::where('id', $mainProductId)->where('metal_id', 3)->exists()
) {
$config->trackDailyPriceExtreme((float) $cp);
$mainCopper = Product::find($mainProductId);
$extremePrice = $mainCopper
? (float) ($mainCopper->price + ($mainCopper->price_profit ?? 0))
: (float) $cp;
$config->trackDailyPriceExtreme($extremePrice);
}
}
@@ -291,4 +326,13 @@ class ProductController extends SuperController
}
private function filterProductData(array $data): array
{
if (!Product::hasPriceOffsetColumn()) {
unset($data['price_offset_percent']);
}
return $data;
}
}