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

@@ -28,6 +28,7 @@ export default function FinancialSettingsPage() {
defaultWholesaleMarginPercent: 15,
});
const [testBuyPrice, setTestBuyPrice] = useState('1000000');
const [applyToAllProductsNow, setApplyToAllProductsNow] = useState(true);
const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false);
@@ -84,7 +85,7 @@ export default function FinancialSettingsPage() {
e.preventDefault();
try {
setIsSaving(true);
await Promise.all([
const [, , resPricing] = await Promise.all([
api.patch('/settings/financial', financial),
api.put('/admin/settings', {
TAX_PERCENTAGE: String(financial.taxPercentage),
@@ -92,8 +93,13 @@ export default function FinancialSettingsPage() {
REFILL_SUBSCRIPTION_ENABLED: refillEnabled ? 'true' : 'false',
REFILL_REWARD_PERCENT: refillPercent,
}),
api.put('/admin/pricing/settings', {
...pricing,
applyToAllProducts: applyToAllProductsNow,
}),
]);
toast.success('تنظیمات مالی با موفقیت بروزرسانی شد');
const pricingMsg = resPricing.data?.message;
toast.success(pricingMsg || 'تنظیمات مالی و قیمت‌گذاری با موفقیت ذخیره شد');
} catch (err) {
console.error('Failed to update financial settings:', err);
toast.error('خطا در بروزرسانی تنظیمات مالی');
@@ -444,6 +450,26 @@ export default function FinancialSettingsPage() {
</div>
</div>
</div>
{/* Instant Recalculation Toggle */}
<div className="p-4 bg-gradient-to-r from-purple-100/90 to-indigo-100/80 border border-purple-200 rounded-2xl flex items-center justify-between">
<label className="flex items-start gap-3 cursor-pointer">
<input
type="checkbox"
checked={applyToAllProductsNow}
onChange={(e) => setApplyToAllProductsNow(e.target.checked)}
className="w-5 h-5 mt-0.5 rounded-lg text-purple-600 focus:ring-purple-500 border-purple-300 cursor-pointer shrink-0"
/>
<div>
<span className="text-xs font-black text-purple-950 block">
محاسبه و بروزرسانی فوری قیمت تمام کالاهای فروشگاه با این درصد سود و رند کردن جدید
</span>
<span className="text-[11px] text-purple-800 block mt-0.5 font-medium leading-relaxed">
با فعال بودن این گزینه هنگام زدن دکمه ذخیره، قیمت فروش تک و عمده تمامی محصولات بر اساس قیمت خرید، درصد سود جدید ({pricing.defaultRetailMarginPercent}٪ / {pricing.defaultWholesaleMarginPercent}٪) و پله رند کردن ({pricing.roundingStep.toLocaleString('fa-IR')} تومان {pricing.roundingMode === 'UP' ? 'به بالا' : pricing.roundingMode === 'DOWN' ? 'به پایین' : 'به نزدیک‌ترین'}) فوراً در دیتابیس مجدداً محاسبه و به‌روزرسانی می‌شوند.
</span>
</div>
</label>
</div>
</div>
</div>
+193 -24
View File
@@ -68,6 +68,8 @@ export interface Product {
buyPrice?: number;
marginRetailPercent?: number;
marginWholesalePercent?: number;
roundingStep?: number | null;
roundingMode?: string | null;
priceDisplay?: string;
unit?: string;
packageSize?: number;
@@ -255,7 +257,9 @@ export default function Products() {
pdfCover: '',
symptoms: [] as string[],
isPreorder: false,
preorderDeposit: '' as number | string
preorderDeposit: '' as number | string,
roundingStep: '' as number | string,
roundingMode: '' as string,
});
// Smart Pricing & Rounding state
@@ -428,7 +432,9 @@ export default function Products() {
pdfCover: product.pdfCover || '',
symptoms: product.symptoms ? product.symptoms.map((s: { symptom: string } | string) => typeof s === 'string' ? s : s.symptom) : [],
isPreorder: Boolean(product.isPreorder),
preorderDeposit: product.preorderDeposit !== undefined && product.preorderDeposit !== null ? product.preorderDeposit : ''
preorderDeposit: product.preorderDeposit !== undefined && product.preorderDeposit !== null ? product.preorderDeposit : '',
roundingStep: product.roundingStep !== undefined && product.roundingStep !== null ? product.roundingStep : '',
roundingMode: product.roundingMode || '',
});
} else {
setEditingProduct(null);
@@ -438,6 +444,8 @@ export default function Products() {
buyPrice: '', priceValue: '', wholesalePrice: '',
priceValueMarginPercent: pricingSettings.defaultRetailMarginPercent,
wholesaleMarginPercent: pricingSettings.defaultWholesaleMarginPercent,
roundingStep: '',
roundingMode: '',
priceDisplay: '', unit: '', packageSize: '', dosageLogic: '', suitableFor: 'سگ و گربه',
imageUrl: '', metaTitle: '', metaDescription: '', keywords: '', canonicalUrl: '', slug: '',
stockStatus: 'IN_STOCK', noIndex: false, noFollow: false, ogImage: '', featuredImageAlt: '',
@@ -646,6 +654,8 @@ export default function Products() {
wholesalePrice: formData.wholesalePrice ? Number(formData.wholesalePrice) : undefined,
priceValueMarginPercent: formData.priceValueMarginPercent !== '' ? Number(formData.priceValueMarginPercent) : undefined,
wholesaleMarginPercent: formData.wholesaleMarginPercent !== '' ? Number(formData.wholesaleMarginPercent) : undefined,
roundingStep: formData.roundingStep !== '' ? Number(formData.roundingStep) : null,
roundingMode: formData.roundingMode || null,
priceDisplay: formData.priceDisplay || `${Number(formData.priceValue || 0).toLocaleString('fa-IR')} تومان`,
unit: (formData.unit || '').trim() || undefined,
packageSize: Number(formData.packageSize) || 0,
@@ -1270,12 +1280,15 @@ export default function Products() {
let pMargin = formData.priceValueMarginPercent;
let wMargin = formData.wholesaleMarginPercent;
const effStep = formData.roundingStep !== '' ? Number(formData.roundingStep) : pricingSettings.roundingStep;
const effMode = (formData.roundingMode || pricingSettings.roundingMode) as 'UP' | 'DOWN' | 'NEAREST';
if (bPrice && pMargin !== '') {
pPrice = calculateSellingPrice(
Number(bPrice),
Number(pMargin),
pricingSettings.roundingStep,
pricingSettings.roundingMode,
effStep,
effMode,
);
} else if (bPrice && pPrice) {
pMargin = Math.round(
@@ -1287,8 +1300,8 @@ export default function Products() {
wPrice = calculateSellingPrice(
Number(bPrice),
Number(wMargin),
pricingSettings.roundingStep,
pricingSettings.roundingMode,
effStep,
effMode,
);
} else if (bPrice && wPrice) {
wMargin = Math.round(
@@ -1344,12 +1357,14 @@ export default function Products() {
const margin = e.target.value !== '' ? Number(e.target.value) : '';
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
let pPrice = formData.priceValue;
const effStep = formData.roundingStep !== '' ? Number(formData.roundingStep) : pricingSettings.roundingStep;
const effMode = (formData.roundingMode || pricingSettings.roundingMode) as 'UP' | 'DOWN' | 'NEAREST';
if (bPrice && margin !== '') {
pPrice = calculateSellingPrice(
bPrice,
Number(margin),
pricingSettings.roundingStep,
pricingSettings.roundingMode,
effStep,
effMode,
);
}
setFormData({
@@ -1400,12 +1415,14 @@ export default function Products() {
const margin = e.target.value !== '' ? Number(e.target.value) : '';
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
let wPrice = formData.wholesalePrice;
const effStep = formData.roundingStep !== '' ? Number(formData.roundingStep) : pricingSettings.roundingStep;
const effMode = (formData.roundingMode || pricingSettings.roundingMode) as 'UP' | 'DOWN' | 'NEAREST';
if (bPrice && margin !== '') {
wPrice = calculateSellingPrice(
bPrice,
Number(margin),
pricingSettings.roundingStep,
pricingSettings.roundingMode,
effStep,
effMode,
);
}
setFormData({
@@ -1423,21 +1440,173 @@ export default function Products() {
</div>
</div>
{/* Active Rounding Info Badge */}
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 px-4 py-2.5 bg-indigo-50/70 border border-indigo-100 rounded-2xl text-xs text-indigo-900">
<div className="flex items-center gap-2">
<Compass className="w-4 h-4 text-indigo-600 shrink-0" />
<span className="font-bold">قاعده گرد کردن هوشمند فعال:</span>
<span className="font-mono font-bold bg-white px-2 py-0.5 rounded-lg border border-indigo-200">
{pricingSettings.roundingStep.toLocaleString('fa-IR')} تومان
</span>
<span>
({pricingSettings.roundingMode === 'UP' ? 'گرد به بالا / سقف' : pricingSettings.roundingMode === 'DOWN' ? 'گرد به پایین / کف' : 'گرد به نزدیک‌ترین مضرب'})
</span>
{/* Custom Rounding Rules & Link to Global Settings */}
<div className="bg-gradient-to-br from-indigo-50/80 to-purple-50/60 border border-indigo-100 p-4 rounded-2xl space-y-3">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 border-b border-indigo-100/80 pb-2.5">
<div className="flex items-center gap-2 flex-wrap">
<Compass className="w-4 h-4 text-indigo-600 shrink-0" />
<span className="font-bold text-xs text-indigo-950">قاعده گرد کردن هوشمند قیمت:</span>
<span className="font-mono font-bold text-xs bg-white text-indigo-700 px-2 py-0.5 rounded-lg border border-indigo-200">
{(formData.roundingStep !== '' ? Number(formData.roundingStep) : pricingSettings.roundingStep).toLocaleString('fa-IR')} تومان
</span>
<span className="text-xs text-indigo-900 font-medium">
({(formData.roundingMode || pricingSettings.roundingMode) === 'UP' ? 'گرد به بالا / سقف' : (formData.roundingMode || pricingSettings.roundingMode) === 'DOWN' ? 'گرد به پایین / کف' : 'گرد به نزدیک‌ترین مضرب'})
</span>
{formData.roundingStep !== '' || formData.roundingMode !== '' ? (
<span className="text-[10px] bg-amber-100 text-amber-800 font-black px-2 py-0.5 rounded-md">اختصاصی این کالا</span>
) : (
<span className="text-[10px] bg-indigo-100 text-indigo-800 font-black px-2 py-0.5 rounded-md">پیروی از تنظیمات کلی</span>
)}
</div>
<a
href="/settings/financial"
target="_blank"
rel="noopener noreferrer"
className="text-xs text-indigo-600 hover:text-indigo-800 font-bold flex items-center gap-1 shrink-0 underline decoration-indigo-300 hover:decoration-indigo-600 transition-colors"
>
تنظیمات کلی فرمول و رند کردن ⚙️
</a>
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3 pt-1">
<div>
<div className="flex items-center justify-between mb-1.5">
<label className="text-xs font-bold text-gray-700">پله گرد کردن اختصاصی این کالا (تومان):</label>
{formData.roundingStep !== '' && (
<button
type="button"
onClick={() => {
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
let pPrice = formData.priceValue;
let wPrice = formData.wholesalePrice;
const activeMode = (formData.roundingMode || pricingSettings.roundingMode) as 'UP' | 'DOWN' | 'NEAREST';
if (bPrice && formData.priceValueMarginPercent !== '') {
pPrice = calculateSellingPrice(bPrice, Number(formData.priceValueMarginPercent), pricingSettings.roundingStep, activeMode);
}
if (bPrice && formData.wholesaleMarginPercent !== '') {
wPrice = calculateSellingPrice(bPrice, Number(formData.wholesaleMarginPercent), pricingSettings.roundingStep, activeMode);
}
setFormData({
...formData,
roundingStep: '',
priceValue: pPrice,
wholesalePrice: wPrice,
priceDisplay: `${Number(pPrice || 0).toLocaleString('fa-IR')} تومان`,
});
}}
className="text-[10px] text-red-500 hover:underline font-bold"
>
بازنشانی به پیش‌فرض کلی ({pricingSettings.roundingStep.toLocaleString('fa-IR')} تومان)
</button>
)}
</div>
<div className="flex items-center gap-1.5 flex-wrap">
{[1000, 5000, 10000, 50000, 100000].map((step) => (
<button
key={step}
type="button"
onClick={() => {
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
let pPrice = formData.priceValue;
let wPrice = formData.wholesalePrice;
const activeMode = (formData.roundingMode || pricingSettings.roundingMode) as 'UP' | 'DOWN' | 'NEAREST';
if (bPrice && formData.priceValueMarginPercent !== '') {
pPrice = calculateSellingPrice(bPrice, Number(formData.priceValueMarginPercent), step, activeMode);
}
if (bPrice && formData.wholesaleMarginPercent !== '') {
wPrice = calculateSellingPrice(bPrice, Number(formData.wholesaleMarginPercent), step, activeMode);
}
setFormData({
...formData,
roundingStep: step,
priceValue: pPrice,
wholesalePrice: wPrice,
priceDisplay: `${Number(pPrice || 0).toLocaleString('fa-IR')} تومان`,
});
}}
className={`px-2.5 py-1 text-xs font-bold rounded-lg border transition-all ${
(formData.roundingStep !== '' ? Number(formData.roundingStep) === step : pricingSettings.roundingStep === step)
? 'bg-indigo-600 text-white border-indigo-600 shadow-sm'
: 'bg-white text-gray-700 border-gray-200 hover:border-indigo-300'
}`}
>
{step.toLocaleString('fa-IR')}
</button>
))}
</div>
</div>
<div>
<div className="flex items-center justify-between mb-1.5">
<label className="text-xs font-bold text-gray-700">جهت گرد کردن اختصاصی این کالا:</label>
{formData.roundingMode !== '' && (
<button
type="button"
onClick={() => {
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
let pPrice = formData.priceValue;
let wPrice = formData.wholesalePrice;
const activeStep = formData.roundingStep !== '' ? Number(formData.roundingStep) : pricingSettings.roundingStep;
if (bPrice && formData.priceValueMarginPercent !== '') {
pPrice = calculateSellingPrice(bPrice, Number(formData.priceValueMarginPercent), activeStep, pricingSettings.roundingMode);
}
if (bPrice && formData.wholesaleMarginPercent !== '') {
wPrice = calculateSellingPrice(bPrice, Number(formData.wholesaleMarginPercent), activeStep, pricingSettings.roundingMode);
}
setFormData({
...formData,
roundingMode: '',
priceValue: pPrice,
wholesalePrice: wPrice,
priceDisplay: `${Number(pPrice || 0).toLocaleString('fa-IR')} تومان`,
});
}}
className="text-[10px] text-red-500 hover:underline font-bold"
>
بازنشانی به پیش‌فرض کلی ({pricingSettings.roundingMode === 'UP' ? 'به بالا' : pricingSettings.roundingMode === 'DOWN' ? 'به پایین' : 'نزدیک‌ترین'})
</button>
)}
</div>
<div className="grid grid-cols-3 gap-1.5">
{[
{ mode: 'UP', label: 'به بالا (سقف)' },
{ mode: 'DOWN', label: 'به پایین (کف)' },
{ mode: 'NEAREST', label: 'نزدیک‌ترین' },
].map((item) => (
<button
key={item.mode}
type="button"
onClick={() => {
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
let pPrice = formData.priceValue;
let wPrice = formData.wholesalePrice;
const activeStep = formData.roundingStep !== '' ? Number(formData.roundingStep) : pricingSettings.roundingStep;
if (bPrice && formData.priceValueMarginPercent !== '') {
pPrice = calculateSellingPrice(bPrice, Number(formData.priceValueMarginPercent), activeStep, item.mode as any);
}
if (bPrice && formData.wholesaleMarginPercent !== '') {
wPrice = calculateSellingPrice(bPrice, Number(formData.wholesaleMarginPercent), activeStep, item.mode as any);
}
setFormData({
...formData,
roundingMode: item.mode,
priceValue: pPrice,
wholesalePrice: wPrice,
priceDisplay: `${Number(pPrice || 0).toLocaleString('fa-IR')} تومان`,
});
}}
className={`px-2 py-1.5 text-xs font-bold rounded-lg border transition-all text-center ${
(formData.roundingMode !== '' ? formData.roundingMode === item.mode : pricingSettings.roundingMode === item.mode)
? 'bg-purple-600 text-white border-purple-600 shadow-sm'
: 'bg-white text-gray-700 border-gray-200 hover:border-purple-300'
}`}
>
{item.label}
</button>
))}
</div>
</div>
</div>
<span className="text-[11px] text-gray-500">
هنگام تغییر قیمت خرید یا درصد سود، قیمت نهایی خودکار با این قاعده رند می‌شود.
</span>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 pt-2">