fix(pricing): auto-recalculate all products on financial settings save and support per-product custom rounding override
Deploy Canina / deploy (push) Successful in 47s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 6s

This commit is contained in:
parsa aghaei committed 2026-09-02 10:28:29 +03:30
1 parent ac15ece619
commit c4e4a336d8
17 files changed
+15685 -13745

No files matched your search

+97 -7
View File
@@ -456,6 +456,16 @@ export class AdminService {
marginRetail !== undefined ? marginRetail : undefined,
marginWholesalePercent:
marginWholesale !== undefined ? marginWholesale : undefined,
roundingStep:
data.roundingStep !== undefined
? data.roundingStep
? Number(data.roundingStep)
: null
: undefined,
roundingMode:
data.roundingMode !== undefined
? data.roundingMode || null
: undefined,
priceValue: data.priceValue || 0,
wholesalePrice:
data.wholesalePrice !== undefined ? data.wholesalePrice : null,
@@ -540,6 +550,16 @@ export class AdminService {
marginRetail !== undefined ? marginRetail : undefined,
marginWholesalePercent:
marginWholesale !== undefined ? marginWholesale : undefined,
roundingStep:
data.roundingStep !== undefined
? data.roundingStep
? Number(data.roundingStep)
: null
: undefined,
roundingMode:
data.roundingMode !== undefined
? data.roundingMode || null
: undefined,
priceValue: data.priceValue,
wholesalePrice:
data.wholesalePrice !== undefined ? data.wholesalePrice : undefined,
@@ -1099,20 +1119,82 @@ export class AdminService {
create: { key: 'pricing_config', category: 'pricing', value },
});
let updatedCount = 0;
if (dto.applyToAllProducts !== false) {
const products = await this.prisma.product.findMany({
where: { buyPrice: { gt: 0 } },
select: {
id: true,
slug: true,
artNo: true,
buyPrice: true,
roundingStep: true,
roundingMode: true,
},
});
const revalidateSlugs: string[] = [];
for (const prod of products) {
const buyPrice = Number(prod.buyPrice || 0);
if (buyPrice > 0) {
const prodStep = prod.roundingStep
? Number(prod.roundingStep)
: value.roundingStep;
const prodMode = (prod.roundingMode ||
value.roundingMode) as RoundingMode;
const newRetailPrice = calculateSellingPrice(
buyPrice,
value.defaultRetailMarginPercent,
prodStep,
prodMode,
);
const newWholesalePrice = calculateSellingPrice(
buyPrice,
value.defaultWholesaleMarginPercent,
prodStep,
prodMode,
);
await this.prisma.product.update({
where: { id: prod.id },
data: {
priceValue: newRetailPrice,
wholesalePrice: newWholesalePrice,
marginRetailPercent: value.defaultRetailMarginPercent,
marginWholesalePercent: value.defaultWholesaleMarginPercent,
priceDisplay: `${newRetailPrice.toLocaleString('fa-IR')} تومان`,
},
});
updatedCount++;
if (prod.slug) revalidateSlugs.push(prod.slug);
}
}
if (revalidateSlugs.length > 0) {
for (const slug of revalidateSlugs.slice(0, 30)) {
this.revalidationService.revalidateProduct(slug).catch(() => {});
}
}
}
return {
success: true,
message: 'تنظیمات قیمت‌گذاری و گرد کردن با موفقیت ذخیره شد.',
message:
updatedCount > 0
? `تنظیمات ذخیره و قیمت ${updatedCount} محصول با فرمول جدید به‌روزرسانی و گرد شدند.`
: 'تنظیمات قیمت‌گذاری و گرد کردن با موفقیت ذخیره شد.',
data: value,
updatedCount,
};
}
async applyGlobalMargins(dto: ApplyGlobalMarginsDto) {
const pricingSettings = await this.getPricingSettings();
const roundingStep =
const globalRoundingStep =
dto.roundingStep !== undefined
? Number(dto.roundingStep)
: pricingSettings.roundingStep;
const roundingMode = (dto.roundingMode ||
const globalRoundingMode = (dto.roundingMode ||
pricingSettings.roundingMode) as RoundingMode;
const retailMargin = Number(dto.retailMarginPercent);
const wholesaleMargin = Number(dto.wholesaleMarginPercent);
@@ -1139,6 +1221,8 @@ export class AdminService {
slug: true,
artNo: true,
buyPrice: true,
roundingStep: true,
roundingMode: true,
priceValue: true,
wholesalePrice: true,
},
@@ -1150,17 +1234,22 @@ export class AdminService {
for (const prod of products) {
const buyPrice = Number(prod.buyPrice || 0);
if (buyPrice > 0) {
const prodStep = prod.roundingStep
? Number(prod.roundingStep)
: globalRoundingStep;
const prodMode = (prod.roundingMode ||
globalRoundingMode) as RoundingMode;
const newRetailPrice = calculateSellingPrice(
buyPrice,
retailMargin,
roundingStep,
roundingMode,
prodStep,
prodMode,
);
const newWholesalePrice = calculateSellingPrice(
buyPrice,
wholesaleMargin,
roundingStep,
roundingMode,
prodStep,
prodMode,
);
await this.prisma.product.update({
@@ -1170,6 +1259,7 @@ export class AdminService {
wholesalePrice: newWholesalePrice,
marginRetailPercent: retailMargin,
marginWholesalePercent: wholesaleMargin,
priceDisplay: `${newRetailPrice.toLocaleString('fa-IR')} تومان`,
},
});
updatedCount++;
+8
View File
@@ -33,6 +33,14 @@ export class UpdatePricingSettingsDto {
@IsNumber()
@Min(0)
defaultWholesaleMarginPercent!: number;
@ApiPropertyOptional({
description: 'آیا قیمت تمام محصولات بر اساس فرمول جدید بلافاصله به‌روزرسانی شود؟',
default: true,
})
@IsOptional()
@IsBoolean()
applyToAllProducts?: boolean;
}
export class ApplyGlobalMarginsDto {
+13
View File
@@ -83,6 +83,19 @@ export class ProductDto {
@IsNumber()
wholesaleMarginPercent?: number;
@ApiPropertyOptional({ description: 'گام گرد کردن اختصاصی این محصول (اختیاری)' })
@IsOptional()
@IsNumber()
roundingStep?: number;
@ApiPropertyOptional({
description: 'جهت گرد کردن اختصاصی این محصول (اختیاری)',
enum: ['UP', 'DOWN', 'NEAREST'],
})
@IsOptional()
@IsString()
roundingMode?: string;
@ApiPropertyOptional({ description: 'نمایش متنی قیمت' })
@IsOptional()
@IsString()