Files
boursecu_server/app/Services/ProductPriceService.php
T

173 lines
5.9 KiB
PHP
Raw Normal View History

2026-06-02 16:00:33 +03:30
<?php
namespace App\Services;
use App\Models\Product;
class ProductPriceService
{
2026-07-04 22:14:18 +03:30
/** 0.5 => 0.5% ، 5 => 5% — همیشه عدد درصد، نه اعشار (۰.۵ ≠ ۵۰٪) */
public static function cashFeePercentRatio(float $percentValue): float
2026-07-04 18:00:57 +03:30
{
if ($percentValue == 0.0) {
return 0.0;
}
return $percentValue / 100;
}
2026-07-04 22:14:18 +03:30
/** @deprecated از cashFeePercentRatio استفاده کنید */
public static function feePercentRatio(float $percentValue): float
{
return self::cashFeePercentRatio($percentValue);
}
2026-07-04 18:00:57 +03:30
public static function resolveCashFeeRial(Product $product, int $direction, float $basePriceRial): float
{
$isBuy = $direction === 1;
$rawFee = (float) ($isBuy ? ($product->cash_fee_buy ?? 0) : ($product->cash_fee_sell ?? 0));
if ($rawFee == 0.0) {
return 0.0;
}
$isPercent = $isBuy
? (bool) ($product->cash_fee_buy_is_percent ?? false)
: (bool) ($product->cash_fee_sell_is_percent ?? false);
if (! $isPercent || ! Product::hasCashFeePercentModeColumns()) {
return $rawFee;
}
if ($basePriceRial <= 0) {
return 0.0;
}
2026-07-04 22:14:18 +03:30
return round(abs($basePriceRial * self::cashFeePercentRatio($rawFee)));
2026-07-04 18:00:57 +03:30
}
public static function productBasePriceRial(Product $product): float
{
return (float) (($product->price ?? 0) + ($product->price_profit ?? 0));
}
2026-06-02 16:00:33 +03:30
public static function referenceTotal(int $basePrice, int $baseProfit = 0): int
{
return $basePrice + $baseProfit;
}
public static function adjustedTotal(int $basePrice, int $baseProfit, float $offsetPercent): int
{
$reference = self::referenceTotal($basePrice, $baseProfit);
return (int) round($reference * (1 + ($offsetPercent / 100)));
}
/** مرجع API + درصد محصول → ذخیره در price (سود خرید/فروش جداگانه در سفارش اعمال می‌شود) */
public static function offsetPercent(Product $product): float
{
return Product::hasPriceOffsetColumn()
? (float) ($product->price_offset_percent ?? 0)
: 0.0;
}
2026-06-13 12:42:42 +03:30
/** @return array<string, mixed>|null */
public static function syncFromReference(Product $product, int $basePrice, int $baseProfit = 0, ?string $sourceName = null): ?array
2026-06-02 16:00:33 +03:30
{
2026-06-04 14:42:22 +03:30
if (!Product::hasStoredPriceColumns()) {
2026-06-13 12:42:42 +03:30
return null;
2026-06-04 14:42:22 +03:30
}
2026-06-02 16:00:33 +03:30
if (FormulaPriceService::isEnabled($product)) {
$computed = FormulaPriceService::calculateForProduct($product, [
'price' => $basePrice,
'active' => true,
]);
if ($computed === null || $computed <= 0) {
if (
Product::hasUseLastPriceIfZeroColumn()
&& ($product->use_last_price_if_zero ?? false)
) {
return null;
}
$computed = 0;
}
$oldPrice = (int) ($product->price ?? 0);
$data = ['price' => $computed];
if (\Illuminate\Support\Facades\Schema::hasColumn($product->getTable(), 'price_profit')) {
$data['price_profit'] = 0;
}
Product::where('id', $product->id)->update($data);
return [
'product_id' => $product->id,
'product_title' => $product->title,
'reading_method' => $product->reading_method,
'source_name' => $sourceName ?? $product->reading_method,
'base_price' => $basePrice,
'base_profit' => $baseProfit,
'offset_percent' => 0.0,
'old_price' => $oldPrice,
'new_price' => $computed,
'changed' => $oldPrice !== $computed,
'price_formula' => true,
];
}
2026-06-04 14:42:22 +03:30
$offsetPercent = self::offsetPercent($product);
2026-06-10 13:11:51 +03:30
$adjustedPrice = self::adjustedTotal($basePrice, $baseProfit, $offsetPercent);
2026-06-13 12:42:42 +03:30
$oldPrice = (int) ($product->price ?? 0);
2026-06-10 13:11:51 +03:30
if (
$adjustedPrice <= 0
&& Product::hasUseLastPriceIfZeroColumn()
&& ($product->use_last_price_if_zero ?? false)
) {
2026-06-13 12:42:42 +03:30
return null;
2026-06-10 13:11:51 +03:30
}
$data = ['price' => $adjustedPrice];
2026-06-04 14:42:22 +03:30
if (\Illuminate\Support\Facades\Schema::hasColumn($product->getTable(), 'price_profit')) {
$data['price_profit'] = 0;
}
Product::where('id', $product->id)->update($data);
2026-06-13 12:42:42 +03:30
return [
'product_id' => $product->id,
'product_title' => $product->title,
'reading_method' => $product->reading_method,
'source_name' => $sourceName ?? $product->reading_method,
'base_price' => $basePrice,
'base_profit' => $baseProfit,
'offset_percent' => $offsetPercent,
'old_price' => $oldPrice,
'new_price' => $adjustedPrice,
'changed' => $oldPrice !== $adjustedPrice,
];
2026-06-02 16:00:33 +03:30
}
/** وقتی فقط درصد در ادمین عوض می‌شود (بدون sync جدید) */
public static function reapplyOffsetPercent(Product $product, float $newOffsetPercent, ?float $oldOffsetPercent = null): void
{
2026-06-04 14:42:22 +03:30
if (!Product::hasPriceOffsetColumn() || !Product::hasStoredPriceColumns()) {
2026-06-02 16:00:33 +03:30
return;
}
$oldOffset = $oldOffsetPercent ?? self::offsetPercent($product);
$currentTotal = (int) ($product->price + ($product->price_profit ?? 0));
$reference = $oldOffset != 0.0
? (int) round($currentTotal / (1 + $oldOffset / 100))
: $currentTotal;
Product::where('id', $product->id)->update([
'price' => (int) round($reference * (1 + ($newOffsetPercent / 100))),
'price_profit' => 0,
]);
}
}