added percent in product price

This commit is contained in:
2026-07-04 18:00:57 +03:30
parent 4dea2fb212
commit 4ef0dfb2c0
7 changed files with 161 additions and 6 deletions
@@ -18,6 +18,7 @@ use App\Models\Price;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\GiftCardController;
use App\Services\ConfigService;
use App\Services\ProductPriceService;
@@ -243,11 +244,16 @@ class CartController extends Controller
//محاسبه مجموع درصد کارمزد سبد با احتساب تعداد
$data['service_charge_percentage'] += $product->percent_fee_buy * $item->quantity;
//محاسبه مجموع کارمزد ثابت سبد با احتساب تعداد
$data['fee'] += $product->cash_fee_buy * $item->quantity;
$unitCashFee = ProductPriceService::resolveCashFeeRial(
$product,
1,
max((float) $goldPrice, ProductPriceService::productBasePriceRial($product))
);
$data['fee'] += $unitCashFee * $item->quantity;
// قیمت نهایی هر گرم
$priceWithFee = $goldPrice + $data['fee'];
//محاسبه مجموع کارمزد ثابت سبد با احتساب تعداد
$product_fees += $product->cash_fee_buy;
$product_fees += $unitCashFee;
// قیمت آیتم
$itemTotalPrice = $finalAmount * $priceWithFee;
@@ -375,7 +381,8 @@ class CartController extends Controller
throw new UnprocessableEntityHttpException("قیمت محصول {$product->title} مشخص نیست.");
}
$cashFee = (float) ($product->cash_fee_buy ?? 0) * $qty;
$unitCashFee = ProductPriceService::resolveCashFeeRial($product, 1, $unitPrice);
$cashFee = $unitCashFee * $qty;
$lineTotal = ($unitPrice * $qty) + $cashFee;
$total_price += $lineTotal;
@@ -25,6 +25,7 @@ use App\Models\Setting;
use App\Models\Cnt;
use App\Services\MontzarService;
use App\Services\ConfigService;
use App\Services\ProductPriceService;
use App\Services\ProductDisplayPriceService;
use App\Services\ReferralGiftService;
use App\Services\WalletBroadcastService;
@@ -1057,7 +1058,11 @@ class SingleOrderController extends Controller
}
}
return (float) ($direction === 1 ? $product->cash_fee_buy : $product->cash_fee_sell);
return ProductPriceService::resolveCashFeeRial(
$product,
$direction,
ProductPriceService::productBasePriceRial($product)
);
}
private function bonusGift($customer, $referrer)
+34
View File
@@ -24,6 +24,7 @@ class Product extends Model
private static ?bool $tradeByQuantityColumnExists = null;
private static ?bool $inventoryColumnExists = null;
private static ?bool $overrideTradingHoursColumnExists = null;
private static ?bool $cashFeePercentModeColumnsExist = null;
protected static function booted(): void
{
@@ -56,6 +57,12 @@ class Product extends Model
if (!static::hasOverrideTradingHoursColumn()) {
unset($product->attributes['override_trading_hours']);
}
if (!static::hasCashFeePercentModeColumns()) {
unset(
$product->attributes['cash_fee_buy_is_percent'],
$product->attributes['cash_fee_sell_is_percent']
);
}
});
}
@@ -77,6 +84,27 @@ class Product extends Model
return static::$overrideTradingHoursColumnExists;
}
public static function hasCashFeePercentModeColumns(): bool
{
if (static::$cashFeePercentModeColumnsExist !== null) {
return static::$cashFeePercentModeColumnsExist;
}
try {
static::$cashFeePercentModeColumnsExist = Schema::hasColumn(
(new static)->getTable(),
'cash_fee_buy_is_percent'
) && Schema::hasColumn(
(new static)->getTable(),
'cash_fee_sell_is_percent'
);
} catch (\Throwable) {
static::$cashFeePercentModeColumnsExist = false;
}
return static::$cashFeePercentModeColumnsExist;
}
public static function hasPriceOffsetColumn(): bool
{
if (static::$priceOffsetColumnExists !== null) {
@@ -283,6 +311,8 @@ class Product extends Model
'percent_fee_sell', // Add percent_fee_sell to fillable
'cash_fee_buy', // Add cash_fee_buy to fillable
'cash_fee_sell', // Add cash_fee_sell to fillable
'cash_fee_buy_is_percent',
'cash_fee_sell_is_percent',
'percent_fee_buy', // Add percent_fee_buy to fillable
'order_limit_enabled',
'min_order_cash',
@@ -300,6 +330,8 @@ class Product extends Model
'show_in_assets' => false,
'trade_by_quantity' => false,
'use_last_price_if_zero' => false,
'cash_fee_buy_is_percent' => false,
'cash_fee_sell_is_percent' => false,
];
protected $casts = [
@@ -323,6 +355,8 @@ class Product extends Model
'max_order_cash' => 'int',
'cash_fee_buy' => 'int', // Cast to int or decimal if needed
'cash_fee_sell' => 'int', // Cast to int or decimal if needed
'cash_fee_buy_is_percent' => 'boolean',
'cash_fee_sell_is_percent' => 'boolean',
'price_offset_percent' => 'float',
'price_profit' => 'int',
];
+43
View File
@@ -6,6 +6,49 @@ use App\Models\Product;
class ProductPriceService
{
/** 5 => 5% ، 0.05 => 5% (اعشاری) */
public static function feePercentRatio(float $percentValue): float
{
if ($percentValue == 0.0) {
return 0.0;
}
if (abs($percentValue) > 0 && abs($percentValue) < 1) {
return $percentValue;
}
return $percentValue / 100;
}
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;
}
return round(abs($basePriceRial * self::feePercentRatio($rawFee)));
}
public static function productBasePriceRial(Product $product): float
{
return (float) (($product->price ?? 0) + ($product->price_profit ?? 0));
}
public static function referenceTotal(int $basePrice, int $baseProfit = 0): int
{
return $basePrice + $baseProfit;