feat(admin): implement smart pricing, rounding rules, formula margins and bulk price adjustment
This commit is contained in:
1 parent
382ebc0ce6
commit
ac15ece619
21 files changed
+17219
-14083
No files matched your search
@@ -1,12 +1,12 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { DollarSign, Save, Plus, X, Heart, Percent, Wallet } from 'lucide-react';
|
||||
import { DollarSign, Save, Plus, X, Heart, Percent, Wallet, Calculator, ArrowUpRight, ArrowDownRight, Compass } from 'lucide-react';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import api from '../services/api';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
import PriceInput from '../components/ui/PriceInput';
|
||||
import Button from '../components/ui/Button';
|
||||
import type { FinancialSettings } from '../types/admin';
|
||||
|
||||
import type { FinancialSettings, PricingSettings } from '../types/admin';
|
||||
import { calculateSellingPrice } from '../utils/pricing';
|
||||
|
||||
export default function FinancialSettingsPage() {
|
||||
const [financial, setFinancial] = useState<FinancialSettings>({
|
||||
@@ -19,6 +19,16 @@ export default function FinancialSettingsPage() {
|
||||
const [charityStep, setCharityStep] = useState('10000');
|
||||
const [refillEnabled, setRefillEnabled] = useState(false);
|
||||
const [refillPercent, setRefillPercent] = useState('5');
|
||||
|
||||
// Smart Pricing & Rounding Settings
|
||||
const [pricing, setPricing] = useState<PricingSettings>({
|
||||
roundingStep: 5000,
|
||||
roundingMode: 'UP',
|
||||
defaultRetailMarginPercent: 30,
|
||||
defaultWholesaleMarginPercent: 15,
|
||||
});
|
||||
const [testBuyPrice, setTestBuyPrice] = useState('1000000');
|
||||
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [donationInput, setDonationInput] = useState('');
|
||||
@@ -28,8 +38,9 @@ export default function FinancialSettingsPage() {
|
||||
Promise.all([
|
||||
api.get('/settings/financial'),
|
||||
api.get('/admin/settings'),
|
||||
api.get('/admin/pricing/settings'),
|
||||
])
|
||||
.then(([resFin, resAdmin]) => {
|
||||
.then(([resFin, resAdmin, resPricing]) => {
|
||||
if (!isSubscribed) return;
|
||||
const data = resFin.data?.data || resFin.data;
|
||||
if (data) {
|
||||
@@ -48,6 +59,15 @@ export default function FinancialSettingsPage() {
|
||||
setRefillEnabled(resAdmin.data.data.REFILL_SUBSCRIPTION_ENABLED === 'true');
|
||||
setRefillPercent(resAdmin.data.data.REFILL_REWARD_PERCENT || '5');
|
||||
}
|
||||
if (resPricing.data?.success && resPricing.data.data) {
|
||||
const p = resPricing.data.data;
|
||||
setPricing({
|
||||
roundingStep: Number(p.roundingStep || 5000),
|
||||
roundingMode: p.roundingMode || 'UP',
|
||||
defaultRetailMarginPercent: Number(p.defaultRetailMarginPercent || 30),
|
||||
defaultWholesaleMarginPercent: Number(p.defaultWholesaleMarginPercent || 15),
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn('Failed to fetch Financial settings', err);
|
||||
@@ -247,6 +267,186 @@ export default function FinancialSettingsPage() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Smart Pricing & Rounding Rules Section */}
|
||||
<div className="md:col-span-2 pt-6 border-t-2 border-dashed border-gray-200 space-y-5">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h3 className="text-base font-black text-gray-900 flex items-center gap-2">
|
||||
<Calculator className="w-5 h-5 text-indigo-600" />
|
||||
تنظیمات هوشمند فرمول سود و گرد کردن قیمتها (Pricing & Rounding Rules)
|
||||
</h3>
|
||||
<p className="text-xs text-gray-500 mt-1">
|
||||
تعیین گام و جهت رند شدن قیمتها و درصد سود پیشفرض برای فروش خرد و عمده. این تنظیمات در فرم ویرایش محصول و تغییرات گروهی قیمت اعمال میگردد.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="bg-gradient-to-br from-indigo-50/60 via-purple-50/30 to-blue-50/50 p-5 rounded-2xl border border-indigo-100/80 space-y-5">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-5">
|
||||
{/* Rounding Step */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-800 mb-1.5 flex items-center gap-1.5">
|
||||
<Compass className="w-4 h-4 text-indigo-600" />
|
||||
<span>گام گرد کردن قیمت (پله رند شدن به تومان)</span>
|
||||
</label>
|
||||
<PriceInput
|
||||
value={String(pricing.roundingStep)}
|
||||
onChange={(val) => setPricing({ ...pricing, roundingStep: Math.max(0, Number(val) || 0) })}
|
||||
placeholder="5000"
|
||||
className="px-4 py-2.5 bg-white rounded-xl border border-indigo-200 focus:border-indigo-600 text-sm font-bold"
|
||||
/>
|
||||
<div className="flex flex-wrap gap-1.5 mt-2">
|
||||
{[1000, 5000, 10000, 50000, 100000].map((stepVal) => (
|
||||
<button
|
||||
key={stepVal}
|
||||
type="button"
|
||||
onClick={() => setPricing({ ...pricing, roundingStep: stepVal })}
|
||||
className={`px-2.5 py-1 text-xs font-bold rounded-lg border transition-all cursor-pointer ${
|
||||
pricing.roundingStep === stepVal
|
||||
? 'bg-indigo-600 text-white border-indigo-600 shadow-sm'
|
||||
: 'bg-white text-gray-600 border-gray-200 hover:border-indigo-300'
|
||||
}`}
|
||||
>
|
||||
{stepVal.toLocaleString('fa-IR')} تومان
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Rounding Mode / Direction */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-800 mb-1.5 flex items-center gap-1.5">
|
||||
<span>جهت گرد کردن قیمت (Rounding Direction)</span>
|
||||
</label>
|
||||
<div className="grid grid-cols-3 gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setPricing({ ...pricing, roundingMode: 'UP' })}
|
||||
className={`p-3 rounded-xl border text-center transition-all cursor-pointer ${
|
||||
pricing.roundingMode === 'UP'
|
||||
? 'bg-indigo-600 text-white border-indigo-600 shadow-md ring-2 ring-indigo-300'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-indigo-300'
|
||||
}`}
|
||||
>
|
||||
<ArrowUpRight className={`w-4 h-4 mx-auto mb-1 ${pricing.roundingMode === 'UP' ? 'text-white' : 'text-emerald-600'}`} />
|
||||
<span className="block text-xs font-black">به سمت بالا</span>
|
||||
<span className="block text-[10px] opacity-80 mt-0.5">سقف (Ceil)</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setPricing({ ...pricing, roundingMode: 'DOWN' })}
|
||||
className={`p-3 rounded-xl border text-center transition-all cursor-pointer ${
|
||||
pricing.roundingMode === 'DOWN'
|
||||
? 'bg-indigo-600 text-white border-indigo-600 shadow-md ring-2 ring-indigo-300'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-indigo-300'
|
||||
}`}
|
||||
>
|
||||
<ArrowDownRight className={`w-4 h-4 mx-auto mb-1 ${pricing.roundingMode === 'DOWN' ? 'text-white' : 'text-rose-600'}`} />
|
||||
<span className="block text-xs font-black">به سمت پایین</span>
|
||||
<span className="block text-[10px] opacity-80 mt-0.5">کف (Floor)</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setPricing({ ...pricing, roundingMode: 'NEAREST' })}
|
||||
className={`p-3 rounded-xl border text-center transition-all cursor-pointer ${
|
||||
pricing.roundingMode === 'NEAREST'
|
||||
? 'bg-indigo-600 text-white border-indigo-600 shadow-md ring-2 ring-indigo-300'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-indigo-300'
|
||||
}`}
|
||||
>
|
||||
<Compass className={`w-4 h-4 mx-auto mb-1 ${pricing.roundingMode === 'NEAREST' ? 'text-white' : 'text-indigo-600'}`} />
|
||||
<span className="block text-xs font-black">نزدیکترین</span>
|
||||
<span className="block text-[10px] opacity-80 mt-0.5">ریاضی (Round)</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Default Retail Margin */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-800 mb-1.5">
|
||||
درصد سود پیشفرض تکفروشی (Retail Margin %)
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="500"
|
||||
step="0.5"
|
||||
value={pricing.defaultRetailMarginPercent}
|
||||
onChange={(e) => setPricing({ ...pricing, defaultRetailMarginPercent: Number(e.target.value) || 0 })}
|
||||
className="w-full px-4 py-2.5 bg-white rounded-xl border border-indigo-200 focus:border-indigo-600 outline-none font-bold text-sm"
|
||||
dir="ltr"
|
||||
/>
|
||||
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold text-xs">%</span>
|
||||
</div>
|
||||
<p className="text-[11px] text-gray-500 mt-1">فرمول: قیمت خرید × (۱ + سود٪)</p>
|
||||
</div>
|
||||
|
||||
{/* Default Wholesale Margin */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-800 mb-1.5">
|
||||
درصد سود پیشفرض عمدهفروشی (Wholesale Margin %)
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="500"
|
||||
step="0.5"
|
||||
value={pricing.defaultWholesaleMarginPercent}
|
||||
onChange={(e) => setPricing({ ...pricing, defaultWholesaleMarginPercent: Number(e.target.value) || 0 })}
|
||||
className="w-full px-4 py-2.5 bg-white rounded-xl border border-indigo-200 focus:border-indigo-600 outline-none font-bold text-sm"
|
||||
dir="ltr"
|
||||
/>
|
||||
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold text-xs">%</span>
|
||||
</div>
|
||||
<p className="text-[11px] text-gray-500 mt-1">برای مشتریان همکار و داروخانههای دامپزشکی B2B</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Live Simulation / Test Box */}
|
||||
<div className="bg-white/90 p-4 rounded-xl border border-indigo-100 shadow-sm space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-xs font-black text-indigo-900 flex items-center gap-1.5">
|
||||
<Calculator className="w-4 h-4 text-indigo-600" />
|
||||
پیشنمایش زنده خروجی با تنظیمات فعلی:
|
||||
</span>
|
||||
<span className="text-[11px] text-indigo-600 font-medium">
|
||||
گام: {pricing.roundingStep.toLocaleString('fa-IR')} تومان ({pricing.roundingMode === 'UP' ? 'گرد به بالا' : pricing.roundingMode === 'DOWN' ? 'گرد به پایین' : 'گرد به نزدیکترین'})
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3 items-center">
|
||||
<div>
|
||||
<span className="block text-[11px] text-gray-500 mb-1 font-bold">قیمت خرید فرضی (تست):</span>
|
||||
<PriceInput
|
||||
value={testBuyPrice}
|
||||
onChange={(val) => setTestBuyPrice(val)}
|
||||
placeholder="1,000,000"
|
||||
className="px-3 py-2 bg-gray-50 rounded-lg border border-gray-200 text-xs font-bold"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="bg-emerald-50/70 p-3 rounded-xl border border-emerald-200">
|
||||
<span className="block text-[10px] text-emerald-800 font-bold">قیمت تکفروشی محاسبهشده (+{pricing.defaultRetailMarginPercent}٪):</span>
|
||||
<span className="text-sm font-black text-emerald-700 block mt-0.5">
|
||||
{calculateSellingPrice(Number(testBuyPrice) || 0, pricing.defaultRetailMarginPercent, pricing.roundingStep, pricing.roundingMode).toLocaleString('fa-IR')} تومان
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="bg-blue-50/70 p-3 rounded-xl border border-blue-200">
|
||||
<span className="block text-[10px] text-blue-800 font-bold">قیمت عمدهفروشی محاسبهشده (+{pricing.defaultWholesaleMarginPercent}٪):</span>
|
||||
<span className="text-sm font-black text-blue-700 block mt-0.5">
|
||||
{calculateSellingPrice(Number(testBuyPrice) || 0, pricing.defaultWholesaleMarginPercent, pricing.roundingStep, pricing.roundingMode).toLocaleString('fa-IR')} تومان
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Wallet Withdrawal Feature Toggle */}
|
||||
<div className="md:col-span-2 pt-4 border-t border-gray-100">
|
||||
<label className="flex items-center gap-3 cursor-pointer p-4 bg-emerald-50/50 hover:bg-emerald-50 rounded-2xl border border-emerald-100 transition-colors">
|
||||
@@ -277,7 +477,7 @@ export default function FinancialSettingsPage() {
|
||||
startIcon={Save}
|
||||
isLoading={isSaving}
|
||||
>
|
||||
ذخیره تنظیمات مالی
|
||||
ذخیره تنظیمات مالی و قیمتگذاری
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -1,6 +1,34 @@
|
||||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { Search, Filter, Box, Plus, Edit2, Trash2, Image as ImageIcon, X, HelpCircle, ArrowUpDown, ArrowUp, ArrowDown, GripVertical, Film, Play, ChevronRight, ChevronLeft, Link2 } from 'lucide-react';
|
||||
import {
|
||||
Search,
|
||||
Filter,
|
||||
Box,
|
||||
Plus,
|
||||
Edit2,
|
||||
Trash2,
|
||||
Image as ImageIcon,
|
||||
X,
|
||||
HelpCircle,
|
||||
ArrowUpDown,
|
||||
ArrowUp,
|
||||
ArrowDown,
|
||||
GripVertical,
|
||||
Film,
|
||||
Play,
|
||||
ChevronRight,
|
||||
ChevronLeft,
|
||||
Link2,
|
||||
Calculator,
|
||||
TrendingUp,
|
||||
TrendingDown,
|
||||
DollarSign,
|
||||
Percent,
|
||||
Layers,
|
||||
RotateCcw,
|
||||
CheckCircle2,
|
||||
Compass,
|
||||
} from 'lucide-react';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import api, { BASE_DOMAIN } from '../services/api';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
@@ -10,8 +38,12 @@ import ConfirmModal from '../components/ui/ConfirmModal';
|
||||
import PriceInput from '../components/ui/PriceInput';
|
||||
import Button from '../components/ui/Button';
|
||||
import RichTextEditor from '../components/ui/RichTextEditor';
|
||||
|
||||
|
||||
import type { PricingSettings } from '../types/admin';
|
||||
import {
|
||||
DEFAULT_PRICING_SETTINGS,
|
||||
calculateSellingPrice,
|
||||
applyRounding,
|
||||
} from '../utils/pricing';
|
||||
|
||||
export interface Category {
|
||||
id: string;
|
||||
@@ -34,6 +66,8 @@ export interface Product {
|
||||
priceValue?: number;
|
||||
wholesalePrice?: number;
|
||||
buyPrice?: number;
|
||||
marginRetailPercent?: number;
|
||||
marginWholesalePercent?: number;
|
||||
priceDisplay?: string;
|
||||
unit?: string;
|
||||
packageSize?: number;
|
||||
@@ -224,6 +258,33 @@ export default function Products() {
|
||||
preorderDeposit: '' as number | string
|
||||
});
|
||||
|
||||
// Smart Pricing & Rounding state
|
||||
const [pricingSettings, setPricingSettings] = useState<PricingSettings>(DEFAULT_PRICING_SETTINGS);
|
||||
|
||||
// Bulk Price Modal State
|
||||
const [isBulkPriceModalOpen, setIsBulkPriceModalOpen] = useState(false);
|
||||
const [bulkPriceTab, setBulkPriceTab] = useState<'adjust' | 'margin'>('adjust');
|
||||
const [isBulkApplying, setIsBulkApplying] = useState(false);
|
||||
|
||||
// Bulk Adjust form state
|
||||
const [bulkAdjustForm, setBulkAdjustForm] = useState({
|
||||
scope: 'ALL' as 'ALL' | 'CATEGORY',
|
||||
categoryIds: [] as string[],
|
||||
targetField: 'RETAIL' as 'RETAIL' | 'WHOLESALE' | 'BUY' | 'BOTH_SELLING' | 'ALL',
|
||||
adjustmentType: 'PERCENT' as 'PERCENT' | 'FIXED',
|
||||
adjustmentDirection: 'INCREASE' as 'INCREASE' | 'DECREASE',
|
||||
value: 10,
|
||||
applyRounding: true,
|
||||
});
|
||||
|
||||
// Global Margin form state
|
||||
const [globalMarginForm, setGlobalMarginForm] = useState({
|
||||
scope: 'ALL' as 'ALL' | 'CATEGORY',
|
||||
categoryIds: [] as string[],
|
||||
retailMarginPercent: 30,
|
||||
wholesaleMarginPercent: 15,
|
||||
});
|
||||
|
||||
const [draggedMediaIdx, setDraggedMediaIdx] = useState<number | null>(null);
|
||||
const [dragOverMediaIdx, setDragOverMediaIdx] = useState<number | null>(null);
|
||||
const [customMediaUrl, setCustomMediaUrl] = useState('');
|
||||
@@ -313,12 +374,13 @@ export default function Products() {
|
||||
const pPrice = product.priceValue !== undefined && product.priceValue !== null ? Number(product.priceValue) : '';
|
||||
const wPrice = product.wholesalePrice !== undefined && product.wholesalePrice !== null ? Number(product.wholesalePrice) : '';
|
||||
|
||||
let pMargin: number | string = '';
|
||||
let wMargin: number | string = '';
|
||||
if (bPrice && pPrice !== '') {
|
||||
let pMargin: number | string = product.marginRetailPercent !== undefined && product.marginRetailPercent !== null ? Number(product.marginRetailPercent) : '';
|
||||
let wMargin: number | string = product.marginWholesalePercent !== undefined && product.marginWholesalePercent !== null ? Number(product.marginWholesalePercent) : '';
|
||||
|
||||
if (bPrice && pPrice !== '' && pMargin === '') {
|
||||
pMargin = Math.round(((Number(pPrice) - Number(bPrice)) / Number(bPrice)) * 100);
|
||||
}
|
||||
if (bPrice && wPrice !== '') {
|
||||
if (bPrice && wPrice !== '' && wMargin === '') {
|
||||
wMargin = Math.round(((Number(wPrice) - Number(bPrice)) / Number(bPrice)) * 100);
|
||||
}
|
||||
|
||||
@@ -373,7 +435,9 @@ export default function Products() {
|
||||
updateUrlParams({ modal: 'create', productId: undefined, tab: activeTab || 'general' });
|
||||
setFormData({
|
||||
artNo: '', nameFa: '', nameEn: '', scientificTagline: '', description: '', shortDescription: '', categoryId: '',
|
||||
buyPrice: '', priceValue: '', wholesalePrice: '', priceValueMarginPercent: '', wholesaleMarginPercent: '',
|
||||
buyPrice: '', priceValue: '', wholesalePrice: '',
|
||||
priceValueMarginPercent: pricingSettings.defaultRetailMarginPercent,
|
||||
wholesaleMarginPercent: pricingSettings.defaultWholesaleMarginPercent,
|
||||
priceDisplay: '', unit: '', packageSize: '', dosageLogic: '', suitableFor: 'سگ و گربه',
|
||||
imageUrl: '', metaTitle: '', metaDescription: '', keywords: '', canonicalUrl: '', slug: '',
|
||||
stockStatus: 'IN_STOCK', noIndex: false, noFollow: false, ogImage: '', featuredImageAlt: '',
|
||||
@@ -444,6 +508,27 @@ export default function Products() {
|
||||
} catch (err) {
|
||||
console.warn('Could not fetch categories', err);
|
||||
}
|
||||
|
||||
// Fetch pricing settings
|
||||
try {
|
||||
const pricingRes = await api.get('/admin/pricing/settings');
|
||||
if (pricingRes.data?.data) {
|
||||
const p = pricingRes.data.data;
|
||||
setPricingSettings({
|
||||
roundingStep: Number(p.roundingStep || 5000),
|
||||
roundingMode: p.roundingMode || 'UP',
|
||||
defaultRetailMarginPercent: Number(p.defaultRetailMarginPercent || 30),
|
||||
defaultWholesaleMarginPercent: Number(p.defaultWholesaleMarginPercent || 15),
|
||||
});
|
||||
setGlobalMarginForm(prev => ({
|
||||
...prev,
|
||||
retailMarginPercent: Number(p.defaultRetailMarginPercent || 30),
|
||||
wholesaleMarginPercent: Number(p.defaultWholesaleMarginPercent || 15),
|
||||
}));
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Could not fetch pricing settings', err);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
console.error(err);
|
||||
toast.error('خطا در دریافت لیست محصولات');
|
||||
@@ -459,6 +544,61 @@ export default function Products() {
|
||||
return () => clearTimeout(timer);
|
||||
}, [fetchData]);
|
||||
|
||||
// Handle Apply Global Margins
|
||||
const handleApplyGlobalMargins = async () => {
|
||||
try {
|
||||
setIsBulkApplying(true);
|
||||
const res = await api.post('/admin/pricing/apply-global-margins', {
|
||||
scope: globalMarginForm.scope,
|
||||
categoryIds: globalMarginForm.scope === 'CATEGORY' ? globalMarginForm.categoryIds : undefined,
|
||||
retailMarginPercent: Number(globalMarginForm.retailMarginPercent),
|
||||
wholesaleMarginPercent: Number(globalMarginForm.wholesaleMarginPercent),
|
||||
roundingStep: pricingSettings.roundingStep,
|
||||
roundingMode: pricingSettings.roundingMode,
|
||||
});
|
||||
const data = res.data?.data || res.data;
|
||||
toast.success(data?.message || 'درصد سود با موفقیت بر روی محصولات اعمال شد');
|
||||
setIsBulkPriceModalOpen(false);
|
||||
await fetchData();
|
||||
} catch (err: any) {
|
||||
console.error('Failed to apply global margins', err);
|
||||
toast.error(err.response?.data?.message || 'خطا در اعمال سراسری درصد سود');
|
||||
} finally {
|
||||
setIsBulkApplying(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Handle Bulk Price Adjustment (fixed / percentage)
|
||||
const handleBulkPriceAdjustment = async () => {
|
||||
if (!bulkAdjustForm.value || Number(bulkAdjustForm.value) <= 0) {
|
||||
toast.error('لطفاً مقدار تغییر قیمت را وارد نمایید');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setIsBulkApplying(true);
|
||||
const res = await api.post('/admin/pricing/bulk-adjust', {
|
||||
scope: bulkAdjustForm.scope,
|
||||
categoryIds: bulkAdjustForm.scope === 'CATEGORY' ? bulkAdjustForm.categoryIds : undefined,
|
||||
targetField: bulkAdjustForm.targetField,
|
||||
adjustmentType: bulkAdjustForm.adjustmentType,
|
||||
adjustmentDirection: bulkAdjustForm.adjustmentDirection,
|
||||
value: Number(bulkAdjustForm.value),
|
||||
applyRounding: bulkAdjustForm.applyRounding,
|
||||
roundingStep: pricingSettings.roundingStep,
|
||||
roundingMode: pricingSettings.roundingMode,
|
||||
});
|
||||
const data = res.data?.data || res.data;
|
||||
toast.success(data?.message || 'تغییرات گروهی قیمت با موفقیت اعمال شد');
|
||||
setIsBulkPriceModalOpen(false);
|
||||
await fetchData();
|
||||
} catch (err: any) {
|
||||
console.error('Failed to adjust bulk prices', err);
|
||||
toast.error(err.response?.data?.message || 'خطا در اعمال تغییرات گروهی قیمت');
|
||||
} finally {
|
||||
setIsBulkApplying(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
const handleSave = async (e: React.FormEvent) => {
|
||||
@@ -593,14 +733,26 @@ export default function Products() {
|
||||
</h2>
|
||||
<p className="text-gray-500 font-medium mt-1">افزودن و ویرایش پیشرفته محصولات با تنظیمات سئو</p>
|
||||
</div>
|
||||
<Button
|
||||
variant="primary"
|
||||
size="sm"
|
||||
startIcon={Plus}
|
||||
onClick={() => openModal()}
|
||||
>
|
||||
محصول جدید
|
||||
</Button>
|
||||
<div className="flex items-center gap-3">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
startIcon={Calculator}
|
||||
onClick={() => setIsBulkPriceModalOpen(true)}
|
||||
className="border-purple-200 text-purple-700 hover:bg-purple-50 font-bold"
|
||||
>
|
||||
مدیریت گروهی قیمتها
|
||||
</Button>
|
||||
<Button
|
||||
variant="primary"
|
||||
size="sm"
|
||||
startIcon={Plus}
|
||||
onClick={() => openModal()}
|
||||
>
|
||||
محصول جدید
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1119,15 +1271,29 @@ export default function Products() {
|
||||
let wMargin = formData.wholesaleMarginPercent;
|
||||
|
||||
if (bPrice && pMargin !== '') {
|
||||
pPrice = Math.round(Number(bPrice) * (1 + Number(pMargin) / 100));
|
||||
pPrice = calculateSellingPrice(
|
||||
Number(bPrice),
|
||||
Number(pMargin),
|
||||
pricingSettings.roundingStep,
|
||||
pricingSettings.roundingMode,
|
||||
);
|
||||
} else if (bPrice && pPrice) {
|
||||
pMargin = Math.round(((Number(pPrice) - Number(bPrice)) / Number(bPrice)) * 100);
|
||||
pMargin = Math.round(
|
||||
((Number(pPrice) - Number(bPrice)) / Number(bPrice)) * 100,
|
||||
);
|
||||
}
|
||||
|
||||
if (bPrice && wMargin !== '') {
|
||||
wPrice = Math.round(Number(bPrice) * (1 + Number(wMargin) / 100));
|
||||
wPrice = calculateSellingPrice(
|
||||
Number(bPrice),
|
||||
Number(wMargin),
|
||||
pricingSettings.roundingStep,
|
||||
pricingSettings.roundingMode,
|
||||
);
|
||||
} else if (bPrice && wPrice) {
|
||||
wMargin = Math.round(((Number(wPrice) - Number(bPrice)) / Number(bPrice)) * 100);
|
||||
wMargin = Math.round(
|
||||
((Number(wPrice) - Number(bPrice)) / Number(bPrice)) * 100,
|
||||
);
|
||||
}
|
||||
|
||||
setFormData({
|
||||
@@ -1179,7 +1345,12 @@ export default function Products() {
|
||||
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
|
||||
let pPrice = formData.priceValue;
|
||||
if (bPrice && margin !== '') {
|
||||
pPrice = Math.round(bPrice * (1 + Number(margin) / 100));
|
||||
pPrice = calculateSellingPrice(
|
||||
bPrice,
|
||||
Number(margin),
|
||||
pricingSettings.roundingStep,
|
||||
pricingSettings.roundingMode,
|
||||
);
|
||||
}
|
||||
setFormData({
|
||||
...formData,
|
||||
@@ -1230,7 +1401,12 @@ export default function Products() {
|
||||
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
|
||||
let wPrice = formData.wholesalePrice;
|
||||
if (bPrice && margin !== '') {
|
||||
wPrice = Math.round(bPrice * (1 + Number(margin) / 100));
|
||||
wPrice = calculateSellingPrice(
|
||||
bPrice,
|
||||
Number(margin),
|
||||
pricingSettings.roundingStep,
|
||||
pricingSettings.roundingMode,
|
||||
);
|
||||
}
|
||||
setFormData({
|
||||
...formData,
|
||||
@@ -1247,6 +1423,23 @@ 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>
|
||||
</div>
|
||||
<span className="text-[11px] text-gray-500">
|
||||
هنگام تغییر قیمت خرید یا درصد سود، قیمت نهایی خودکار با این قاعده رند میشود.
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 pt-2">
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-bold text-gray-700">موجودی *</label>
|
||||
@@ -2233,6 +2426,470 @@ export default function Products() {
|
||||
)}
|
||||
|
||||
|
||||
{/* Bulk Price & Margins Management Modal */}
|
||||
{isBulkPriceModalOpen && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-3 sm:p-4 bg-black/60 backdrop-blur-sm animate-in fade-in duration-200">
|
||||
<div className="bg-white rounded-3xl w-full max-w-2xl max-h-[92vh] flex flex-col shadow-2xl overflow-hidden border border-gray-100 animate-in zoom-in-95 duration-200">
|
||||
{/* Modal Header */}
|
||||
<div className="px-5 sm:px-6 py-4 border-b border-gray-100 flex justify-between items-center bg-white shrink-0">
|
||||
<div className="flex items-center gap-2.5">
|
||||
<div className="w-9 h-9 rounded-xl bg-purple-50 text-purple-600 flex items-center justify-center">
|
||||
<Calculator className="w-5 h-5" />
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="text-base font-black text-gray-900">
|
||||
مدیریت و تغییرات گروهی قیمتها
|
||||
</h3>
|
||||
<p className="text-[11px] text-gray-500 mt-0.5">
|
||||
افزایش/کاهش درصدی یا مبلغی و اعمال فرمول سراسری سود بر روی محصولات
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setIsBulkPriceModalOpen(false)}
|
||||
className="w-8 h-8 rounded-xl bg-gray-50 text-gray-400 hover:text-gray-700 hover:bg-gray-100 flex items-center justify-center transition-colors cursor-pointer"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Modal Tabs */}
|
||||
<div className="flex border-b border-gray-100 px-5 sm:px-6 pt-2 gap-3 shrink-0 bg-gray-50/50">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkPriceTab('adjust')}
|
||||
className={`pb-3 px-2 text-xs sm:text-sm font-bold border-b-2 whitespace-nowrap transition-colors cursor-pointer flex items-center gap-1.5 ${
|
||||
bulkPriceTab === 'adjust'
|
||||
? 'border-purple-600 text-purple-700'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
<TrendingUp className="w-4 h-4" />
|
||||
<span>تغییرات گروهی قیمت (افزایش / کاهش)</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkPriceTab('margin')}
|
||||
className={`pb-3 px-2 text-xs sm:text-sm font-bold border-b-2 whitespace-nowrap transition-colors cursor-pointer flex items-center gap-1.5 ${
|
||||
bulkPriceTab === 'margin'
|
||||
? 'border-purple-600 text-purple-700'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
<Percent className="w-4 h-4" />
|
||||
<span>اعمال سراسری درصد سود فرمولی</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Modal Body */}
|
||||
<div className="p-5 sm:p-6 overflow-y-auto flex-1 space-y-5 bg-gray-50/30">
|
||||
{bulkPriceTab === 'adjust' ? (
|
||||
/* Tab 1: Bulk Price Adjustment */
|
||||
<div className="space-y-5">
|
||||
{/* Scope */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-2">
|
||||
دامنه اعمال تغییرات:
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, scope: 'ALL' })}
|
||||
className={`p-3 rounded-xl border text-right transition-all cursor-pointer ${
|
||||
bulkAdjustForm.scope === 'ALL'
|
||||
? 'bg-purple-50 border-purple-500 text-purple-900 ring-2 ring-purple-200'
|
||||
: 'bg-white border-gray-200 text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
<span className="block text-xs font-bold">همه محصولات فروشگاه</span>
|
||||
<span className="block text-[10px] text-gray-500 mt-0.5">اعمال بر روی تمامی کالاها</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, scope: 'CATEGORY' })}
|
||||
className={`p-3 rounded-xl border text-right transition-all cursor-pointer ${
|
||||
bulkAdjustForm.scope === 'CATEGORY'
|
||||
? 'bg-purple-50 border-purple-500 text-purple-900 ring-2 ring-purple-200'
|
||||
: 'bg-white border-gray-200 text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
<span className="block text-xs font-bold">دستهبندی خاص</span>
|
||||
<span className="block text-[10px] text-gray-500 mt-0.5">انتخاب دستههای مشخص</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{bulkAdjustForm.scope === 'CATEGORY' && (
|
||||
<div className="mt-3 p-3 bg-white rounded-xl border border-gray-200 space-y-2">
|
||||
<span className="block text-[11px] font-bold text-gray-600">انتخاب دستهبندیها:</span>
|
||||
<div className="flex flex-wrap gap-2 max-h-36 overflow-y-auto">
|
||||
{categories.map((c) => {
|
||||
const isSelected = bulkAdjustForm.categoryIds.includes(c.id);
|
||||
return (
|
||||
<button
|
||||
key={c.id}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
if (isSelected) {
|
||||
setBulkAdjustForm({
|
||||
...bulkAdjustForm,
|
||||
categoryIds: bulkAdjustForm.categoryIds.filter((id) => id !== c.id),
|
||||
});
|
||||
} else {
|
||||
setBulkAdjustForm({
|
||||
...bulkAdjustForm,
|
||||
categoryIds: [...bulkAdjustForm.categoryIds, c.id],
|
||||
});
|
||||
}
|
||||
}}
|
||||
className={`px-3 py-1 rounded-lg text-xs font-bold border transition-all cursor-pointer ${
|
||||
isSelected
|
||||
? 'bg-purple-600 text-white border-purple-600'
|
||||
: 'bg-gray-50 text-gray-700 border-gray-200 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
{c.nameFa || c.name}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Target Field */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-2">
|
||||
فیلد هدف برای تغییر قیمت:
|
||||
</label>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 gap-2">
|
||||
{[
|
||||
{ id: 'RETAIL', label: 'قیمت تکفروشی' },
|
||||
{ id: 'WHOLESALE', label: 'قیمت عمده (B2B)' },
|
||||
{ id: 'BOTH_SELLING', label: 'هر دو قیمت فروش' },
|
||||
{ id: 'BUY', label: 'قیمت خرید ما' },
|
||||
{ id: 'ALL', label: 'همه فیلدهای قیمت' },
|
||||
].map((item) => (
|
||||
<button
|
||||
key={item.id}
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, targetField: item.id as any })}
|
||||
className={`p-2.5 rounded-xl border text-center text-xs font-bold transition-all cursor-pointer ${
|
||||
bulkAdjustForm.targetField === item.id
|
||||
? 'bg-purple-600 text-white border-purple-600 shadow-sm'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-purple-200'
|
||||
}`}
|
||||
>
|
||||
{item.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Type & Direction */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
{/* Direction */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-1.5">
|
||||
جهت تغییر:
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, adjustmentDirection: 'INCREASE' })}
|
||||
className={`p-2.5 rounded-xl border flex items-center justify-center gap-1.5 text-xs font-bold transition-all cursor-pointer ${
|
||||
bulkAdjustForm.adjustmentDirection === 'INCREASE'
|
||||
? 'bg-emerald-600 text-white border-emerald-600 shadow-sm'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-emerald-300'
|
||||
}`}
|
||||
>
|
||||
<TrendingUp className="w-4 h-4" />
|
||||
<span>افزایش (گرانتر)</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, adjustmentDirection: 'DECREASE' })}
|
||||
className={`p-2.5 rounded-xl border flex items-center justify-center gap-1.5 text-xs font-bold transition-all cursor-pointer ${
|
||||
bulkAdjustForm.adjustmentDirection === 'DECREASE'
|
||||
? 'bg-rose-600 text-white border-rose-600 shadow-sm'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-rose-300'
|
||||
}`}
|
||||
>
|
||||
<TrendingDown className="w-4 h-4" />
|
||||
<span>کاهش (ارزانتر)</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Adjustment Type */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-1.5">
|
||||
نوع تغییر:
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, adjustmentType: 'PERCENT' })}
|
||||
className={`p-2.5 rounded-xl border flex items-center justify-center gap-1.5 text-xs font-bold transition-all cursor-pointer ${
|
||||
bulkAdjustForm.adjustmentType === 'PERCENT'
|
||||
? 'bg-purple-600 text-white border-purple-600 shadow-sm'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-purple-300'
|
||||
}`}
|
||||
>
|
||||
<Percent className="w-4 h-4" />
|
||||
<span>درصدی (%)</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, adjustmentType: 'FIXED' })}
|
||||
className={`p-2.5 rounded-xl border flex items-center justify-center gap-1.5 text-xs font-bold transition-all cursor-pointer ${
|
||||
bulkAdjustForm.adjustmentType === 'FIXED'
|
||||
? 'bg-purple-600 text-white border-purple-600 shadow-sm'
|
||||
: 'bg-white text-gray-700 border-gray-200 hover:border-purple-300'
|
||||
}`}
|
||||
>
|
||||
<DollarSign className="w-4 h-4" />
|
||||
<span>مبلغ ثابت (تومان)</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Value Input */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-1.5">
|
||||
مقدار تغییر {bulkAdjustForm.adjustmentType === 'PERCENT' ? '(درصد)' : '(تومان)'}:
|
||||
</label>
|
||||
{bulkAdjustForm.adjustmentType === 'PERCENT' ? (
|
||||
<div className="relative">
|
||||
<input
|
||||
type="number"
|
||||
min="0.1"
|
||||
max="200"
|
||||
step="0.5"
|
||||
value={bulkAdjustForm.value}
|
||||
onChange={(e) => setBulkAdjustForm({ ...bulkAdjustForm, value: Number(e.target.value) || 0 })}
|
||||
className="w-full px-4 py-2.5 bg-white rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm font-bold"
|
||||
dir="ltr"
|
||||
/>
|
||||
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold text-xs">%</span>
|
||||
</div>
|
||||
) : (
|
||||
<PriceInput
|
||||
value={String(bulkAdjustForm.value)}
|
||||
onChange={(val) => setBulkAdjustForm({ ...bulkAdjustForm, value: Number(val) || 0 })}
|
||||
placeholder="مثال: ۱۰۰,۰۰۰"
|
||||
className="px-4 py-2.5 bg-white rounded-xl border border-gray-200 focus:border-purple-500 text-sm font-bold"
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="flex flex-wrap gap-1.5 mt-2">
|
||||
{bulkAdjustForm.adjustmentType === 'PERCENT'
|
||||
? [5, 10, 15, 20, 25, 30].map((pVal) => (
|
||||
<button
|
||||
key={pVal}
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, value: pVal })}
|
||||
className="px-2.5 py-1 bg-white text-gray-600 hover:text-purple-600 text-xs font-bold rounded-lg border border-gray-200 hover:border-purple-200 cursor-pointer"
|
||||
>
|
||||
{pVal}٪
|
||||
</button>
|
||||
))
|
||||
: [50000, 100000, 200000, 500000].map((fVal) => (
|
||||
<button
|
||||
key={fVal}
|
||||
type="button"
|
||||
onClick={() => setBulkAdjustForm({ ...bulkAdjustForm, value: fVal })}
|
||||
className="px-2.5 py-1 bg-white text-gray-600 hover:text-purple-600 text-xs font-bold rounded-lg border border-gray-200 hover:border-purple-200 cursor-pointer"
|
||||
>
|
||||
{fVal.toLocaleString('fa-IR')} تومان
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Apply Rounding Toggle */}
|
||||
<div className="p-3.5 bg-indigo-50/70 rounded-2xl border border-indigo-100 flex items-center justify-between">
|
||||
<label className="flex items-center gap-2.5 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={bulkAdjustForm.applyRounding}
|
||||
onChange={(e) => setBulkAdjustForm({ ...bulkAdjustForm, applyRounding: e.target.checked })}
|
||||
className="w-4 h-4 rounded text-indigo-600 focus:ring-indigo-500 border-gray-300"
|
||||
/>
|
||||
<div>
|
||||
<span className="block text-xs font-bold text-indigo-950">
|
||||
اعمال خودکار گرد کردن بر روی قیمتهای جدید
|
||||
</span>
|
||||
<span className="block text-[11px] text-indigo-700 mt-0.5">
|
||||
گام: {pricingSettings.roundingStep.toLocaleString('fa-IR')} تومان ({pricingSettings.roundingMode === 'UP' ? 'به بالا' : pricingSettings.roundingMode === 'DOWN' ? 'به پایین' : 'به نزدیکترین'})
|
||||
</span>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
/* Tab 2: Global Formula Margins */
|
||||
<div className="space-y-5">
|
||||
<div className="p-4 bg-gradient-to-br from-indigo-50 to-purple-50 rounded-2xl border border-indigo-100 text-xs leading-relaxed text-indigo-900 space-y-1">
|
||||
<p className="font-black flex items-center gap-1.5">
|
||||
<Compass className="w-4 h-4 text-indigo-600" />
|
||||
منطق و اولویت تغییرات سراسری درصد سود:
|
||||
</p>
|
||||
<p className="text-gray-600 text-[11px]">
|
||||
با اجرای این عملیات، قیمتهای فروش تمام کالاهایی که «قیمت خرید» دارند، مجدداً با فرمول <span className="font-bold font-mono">قیمت خرید × (۱ + درصد سود)</span> محاسبه و بر اساس قاعده گرد کردن تنظیمشده (پله {pricingSettings.roundingStep.toLocaleString('fa-IR')} تومان {pricingSettings.roundingMode === 'UP' ? 'به بالا' : pricingSettings.roundingMode === 'DOWN' ? 'به پایین' : 'به نزدیکترین'}) رند میشوند.
|
||||
</p>
|
||||
<p className="text-[11px] text-purple-700 font-bold">
|
||||
✨ قانون اولویت: هر تغییر جدیدتر (چه در اینجا و چه بعداً در صفحه ویرایش محصول) ملاک نهایی قیمت خواهد بود.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Scope */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-2">
|
||||
دامنه اعمال فرمول:
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setGlobalMarginForm({ ...globalMarginForm, scope: 'ALL' })}
|
||||
className={`p-3 rounded-xl border text-right transition-all cursor-pointer ${
|
||||
globalMarginForm.scope === 'ALL'
|
||||
? 'bg-purple-50 border-purple-500 text-purple-900 ring-2 ring-purple-200'
|
||||
: 'bg-white border-gray-200 text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
<span className="block text-xs font-bold">همه محصولات فروشگاه</span>
|
||||
<span className="block text-[10px] text-gray-500 mt-0.5">محاسبه برای تمامی کالاها</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setGlobalMarginForm({ ...globalMarginForm, scope: 'CATEGORY' })}
|
||||
className={`p-3 rounded-xl border text-right transition-all cursor-pointer ${
|
||||
globalMarginForm.scope === 'CATEGORY'
|
||||
? 'bg-purple-50 border-purple-500 text-purple-900 ring-2 ring-purple-200'
|
||||
: 'bg-white border-gray-200 text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
<span className="block text-xs font-bold">دستهبندی خاص</span>
|
||||
<span className="block text-[10px] text-gray-500 mt-0.5">انتخاب دستههای مشخص</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{globalMarginForm.scope === 'CATEGORY' && (
|
||||
<div className="mt-3 p-3 bg-white rounded-xl border border-gray-200 space-y-2">
|
||||
<span className="block text-[11px] font-bold text-gray-600">انتخاب دستهبندیها:</span>
|
||||
<div className="flex flex-wrap gap-2 max-h-36 overflow-y-auto">
|
||||
{categories.map((c) => {
|
||||
const isSelected = globalMarginForm.categoryIds.includes(c.id);
|
||||
return (
|
||||
<button
|
||||
key={c.id}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
if (isSelected) {
|
||||
setGlobalMarginForm({
|
||||
...globalMarginForm,
|
||||
categoryIds: globalMarginForm.categoryIds.filter((id) => id !== c.id),
|
||||
});
|
||||
} else {
|
||||
setGlobalMarginForm({
|
||||
...globalMarginForm,
|
||||
categoryIds: [...globalMarginForm.categoryIds, c.id],
|
||||
});
|
||||
}
|
||||
}}
|
||||
className={`px-3 py-1 rounded-lg text-xs font-bold border transition-all cursor-pointer ${
|
||||
isSelected
|
||||
? 'bg-purple-600 text-white border-purple-600'
|
||||
: 'bg-gray-50 text-gray-700 border-gray-200 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
{c.nameFa || c.name}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
{/* Retail Margin */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-1.5">
|
||||
درصد سود تکفروشی جدید:
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="500"
|
||||
step="0.5"
|
||||
value={globalMarginForm.retailMarginPercent}
|
||||
onChange={(e) => setGlobalMarginForm({ ...globalMarginForm, retailMarginPercent: Number(e.target.value) || 0 })}
|
||||
className="w-full px-4 py-2.5 bg-white rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm font-bold"
|
||||
dir="ltr"
|
||||
/>
|
||||
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold text-xs">%</span>
|
||||
</div>
|
||||
<p className="text-[11px] text-gray-400 mt-1">پیشفرض سیستم: {pricingSettings.defaultRetailMarginPercent}٪</p>
|
||||
</div>
|
||||
|
||||
{/* Wholesale Margin */}
|
||||
<div>
|
||||
<label className="block text-xs font-bold text-gray-700 mb-1.5">
|
||||
درصد سود عمدهفروشی (B2B) جدید:
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="500"
|
||||
step="0.5"
|
||||
value={globalMarginForm.wholesaleMarginPercent}
|
||||
onChange={(e) => setGlobalMarginForm({ ...globalMarginForm, wholesaleMarginPercent: Number(e.target.value) || 0 })}
|
||||
className="w-full px-4 py-2.5 bg-white rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm font-bold"
|
||||
dir="ltr"
|
||||
/>
|
||||
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold text-xs">%</span>
|
||||
</div>
|
||||
<p className="text-[11px] text-gray-400 mt-1">پیشفرض سیستم: {pricingSettings.defaultWholesaleMarginPercent}٪</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Modal Footer */}
|
||||
<div className="px-5 sm:px-6 py-4 border-t border-gray-100 flex justify-between items-center bg-white shrink-0">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setIsBulkPriceModalOpen(false)}
|
||||
disabled={isBulkApplying}
|
||||
>
|
||||
انصراف
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
variant="primary"
|
||||
size="sm"
|
||||
startIcon={isBulkApplying ? Spinner : CheckCircle2}
|
||||
isLoading={isBulkApplying}
|
||||
onClick={bulkPriceTab === 'adjust' ? handleBulkPriceAdjustment : handleApplyGlobalMargins}
|
||||
>
|
||||
{bulkPriceTab === 'adjust' ? 'اعمال تغییرات قیمت بر روی کالاها' : 'محاسبه و اعمال سراسری سود'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Media Selector Modal */}
|
||||
<MediaSelector
|
||||
isOpen={isMediaSelectorOpen}
|
||||
|
||||
@@ -213,4 +213,11 @@ export interface Product {
|
||||
stockQuantity?: number;
|
||||
}
|
||||
|
||||
export interface PricingSettings {
|
||||
roundingStep: number;
|
||||
roundingMode: 'UP' | 'DOWN' | 'NEAREST';
|
||||
defaultRetailMarginPercent: number;
|
||||
defaultWholesaleMarginPercent: number;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
export type RoundingMode = 'UP' | 'DOWN' | 'NEAREST';
|
||||
|
||||
export interface PricingSettings {
|
||||
roundingStep: number;
|
||||
roundingMode: RoundingMode;
|
||||
defaultRetailMarginPercent: number;
|
||||
defaultWholesaleMarginPercent: number;
|
||||
}
|
||||
|
||||
export const DEFAULT_PRICING_SETTINGS: PricingSettings = {
|
||||
roundingStep: 5000,
|
||||
roundingMode: 'UP',
|
||||
defaultRetailMarginPercent: 30,
|
||||
defaultWholesaleMarginPercent: 15,
|
||||
};
|
||||
|
||||
export function applyRounding(
|
||||
price: number,
|
||||
step: number = 5000,
|
||||
mode: RoundingMode = 'UP',
|
||||
): number {
|
||||
if (isNaN(price) || price <= 0) return 0;
|
||||
if (!step || step <= 1) return Math.round(price);
|
||||
|
||||
if (mode === 'UP') {
|
||||
return Math.ceil(price / step) * step;
|
||||
} else if (mode === 'DOWN') {
|
||||
return Math.floor(price / step) * step;
|
||||
} else {
|
||||
return Math.round(price / step) * step;
|
||||
}
|
||||
}
|
||||
|
||||
export function calculateSellingPrice(
|
||||
buyPrice: number,
|
||||
marginPercent: number,
|
||||
step: number = 5000,
|
||||
mode: RoundingMode = 'UP',
|
||||
): number {
|
||||
if (isNaN(buyPrice) || buyPrice <= 0) return 0;
|
||||
const rawPrice = buyPrice * (1 + (marginPercent || 0) / 100);
|
||||
return applyRounding(rawPrice, step, mode);
|
||||
}
|
||||
Reference in new issue
Block a user