feat: dynamic warehouse management and fix admin header

This commit is contained in:
2026-06-12 11:09:07 +03:30
parent aabcea3aeb
commit 73b87193e1
7 changed files with 109 additions and 13 deletions
-1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+20 -5
View File
@@ -5,11 +5,27 @@ import { AlertTriangle, CheckCircle, PackageSearch, RefreshCw, Send, ListTree, A
export default function DiscrepancyDashboard() {
const [warehouse, setWarehouse] = useState('11');
const [warehouses, setWarehouses] = useState([]);
const [data, setData] = useState({ discrepancies: [], accurate: [] });
const [loading, setLoading] = useState(true);
useEffect(() => {
fetchData();
fetch('/api/settings')
.then(res => res.json())
.then(data => {
if (data.warehouses) {
setWarehouses(data.warehouses);
if (data.warehouses.length > 0) {
setWarehouse(data.warehouses[0].id);
}
}
});
}, []);
useEffect(() => {
if (warehouse) {
fetchData();
}
}, [warehouse]);
const fetchData = async () => {
@@ -55,10 +71,9 @@ export default function DiscrepancyDashboard() {
onChange={(e) => setWarehouse(e.target.value)}
className="bg-gray-50 border border-gray-200 rounded-xl px-4 py-2.5 text-sm font-bold focus:outline-none focus:border-indigo-500 flex-1 sm:w-48"
>
<option value="11">انبار مرکزی (11)</option>
<option value="13">انبار فروشگاه (13)</option>
<option value="14">انبار کارگاه شارژ (14)</option>
<option value="15">انبار کارگاه تعمیرات (15)</option>
{warehouses.map(wh => (
<option key={wh.id} value={wh.id}>{wh.name} ({wh.id})</option>
))}
</select>
<button onClick={fetchData} className="bg-gray-900 text-white p-3 rounded-xl hover:bg-gray-800 transition-colors">
<RefreshCw size={18} className={loading ? 'animate-spin' : ''} />
+62
View File
@@ -133,6 +133,68 @@ export default function SettingsPage() {
</div>
</div>
{/* Warehouses Setting */}
<div className="border-t border-gray-50 pt-8">
<div className="flex items-center gap-2 mb-2">
<div className="w-8 h-8 rounded-xl bg-orange-50 text-orange-600 flex items-center justify-center">
<span className="font-black text-sm">WH</span>
</div>
<h2 className="text-base font-bold text-gray-800">مدیریت انبارهای حسابفا</h2>
</div>
<p className="text-xs text-gray-500 leading-relaxed mb-4">
لیست انبارهایی که انبارداران مجاز به انبارگردانی آنها هستند. کد انبار باید دقیقاً با کد حسابفا مطابقت داشته باشد.
</p>
<div className="flex flex-col gap-3">
{settings.warehouses?.map((wh) => (
<div key={wh.id} className="flex items-center justify-between p-3 rounded-[16px] border border-gray-200 bg-gray-50">
<div className="flex items-center gap-3">
<div className="bg-gray-200 text-gray-600 px-3 py-1 rounded-lg text-xs font-black">کد: {wh.id}</div>
<span className="text-sm font-bold text-gray-800">{wh.name}</span>
</div>
<button
onClick={() => setSettings(s => ({ ...s, warehouses: s.warehouses.filter(w => w.id !== wh.id) }))}
className="text-red-500 hover:bg-red-50 p-2 rounded-xl transition-colors"
>
حذف
</button>
</div>
))}
<div className="flex gap-2 mt-2">
<input
type="number"
placeholder="کد انبار..."
id="wh_id"
className="w-24 bg-white border border-gray-200 rounded-[16px] px-3 py-2 text-sm focus:outline-none focus:border-indigo-500"
/>
<input
type="text"
placeholder="نام انبار..."
id="wh_name"
className="flex-1 bg-white border border-gray-200 rounded-[16px] px-3 py-2 text-sm focus:outline-none focus:border-indigo-500"
/>
<button
onClick={() => {
const id = document.getElementById('wh_id').value;
const name = document.getElementById('wh_name').value;
if (id && name) {
setSettings(s => ({
...s,
warehouses: [...(s.warehouses || []), { id, name }]
}));
document.getElementById('wh_id').value = '';
document.getElementById('wh_name').value = '';
}
}}
className="bg-indigo-50 text-indigo-600 px-4 py-2 rounded-[16px] text-xs font-bold hover:bg-indigo-100 transition-colors"
>
افزودن
</button>
</div>
</div>
</div>
</div>
<div className="flex justify-end">
+8
View File
@@ -16,6 +16,14 @@ export async function GET() {
if (settingsMap['correction_roles'] === undefined) {
settingsMap['correction_roles'] = ['ADMIN', 'SUPERVISOR'];
}
if (settingsMap['warehouses'] === undefined) {
settingsMap['warehouses'] = [
{ id: '11', name: 'انبار مرکزی' },
{ id: '13', name: 'انبار فروشگاه' },
{ id: '14', name: 'انبار کارگاه شارژ' },
{ id: '15', name: 'انبار کارگاه تعمیرات' }
];
}
return NextResponse.json(settingsMap);
} catch (error) {
+18 -5
View File
@@ -1,5 +1,5 @@
'use client';
import { useState } from 'react';
import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import Header from '@/components/Header';
import dynamic from 'next/dynamic';
@@ -15,8 +15,22 @@ export default function ScanPage() {
const [camError, setCamError] = useState('');
const [loading, setLoading] = useState(false);
const [lockError, setLockError] = useState('');
const [warehouses, setWarehouses] = useState([]);
const router = useRouter();
useEffect(() => {
fetch('/api/settings')
.then(res => res.json())
.then(data => {
if (data.warehouses) {
setWarehouses(data.warehouses);
if (data.warehouses.length > 0) {
setWarehouse(data.warehouses[0].id);
}
}
});
}, []);
const handleGoToCounting = async () => {
if (!code) return;
setLockError('');
@@ -179,10 +193,9 @@ export default function ScanPage() {
onChange={(e) => setWarehouse(e.target.value)}
className="w-full appearance-none pl-12 pr-5 py-4 bg-white border border-gray-200 rounded-[24px] focus:outline-none focus:border-indigo-500 focus:ring-4 focus:ring-indigo-50 transition-all text-sm font-bold text-gray-700 shadow-sm"
>
<option value="11">انبار مرکزی (11)</option>
<option value="13">انبار فروشگاه (13)</option>
<option value="14">انبار کارگاه شارژ (14)</option>
<option value="15">انبار کارگاه تعمیرات (15)</option>
{warehouses.map(wh => (
<option key={wh.id} value={wh.id}>{wh.name} ({wh.id})</option>
))}
</select>
</div>
+1 -1
View File
@@ -55,7 +55,7 @@ export default function Header({ title = 'داشبورد', showBack = false }) {
</Link>
<div className="flex items-center gap-2 pl-1">
{user?.role === 'ADMIN' && (
{user?.roles?.includes('ADMIN') && (
<Link
href="/admin"
className="w-10 h-10 bg-gray-50 text-gray-600 rounded-[18px] hover:bg-gray-100 transition-colors flex items-center justify-center"