Spaces:
Sleeping
Sleeping
Delete frontend
Browse files- frontend/.env +0 -1
- frontend/.gitignore +0 -24
- frontend/README.md +0 -16
- frontend/eslint.config.js +0 -29
- frontend/hook/useDebounce.js +0 -19
- frontend/index.html +0 -13
- frontend/main.js +0 -13
- frontend/package-lock.json +0 -0
- frontend/package.json +0 -58
- frontend/src/App.jsx +0 -32
- frontend/src/Context/AuthContext.jsx +0 -58
- frontend/src/common/LoaderSpinner.jsx +0 -12
- frontend/src/common/Pagination.jsx +0 -94
- frontend/src/components/AddCustomer.jsx +0 -117
- frontend/src/components/CustomerDetails/CustomerDetails.jsx +0 -492
- frontend/src/components/CustomerDetails/EditInvoiceItemModal.jsx +0 -159
- frontend/src/components/CustomerDetails/PayInvoiceModal.jsx +0 -126
- frontend/src/components/CustomerDetails/PrintInvoice.jsx +0 -145
- frontend/src/components/Customers/Customer.jsx +0 -29
- frontend/src/components/Customers/CustomerBoxView.jsx +0 -148
- frontend/src/components/Customers/CustomersWorkspace.jsx +0 -271
- frontend/src/components/Customers/DeleteCustomerButton.jsx +0 -65
- frontend/src/components/Dashboard/DashboardTab.jsx +0 -331
- frontend/src/components/Inventory/InventoryTab.jsx +0 -251
- frontend/src/components/Inventory/InventoryWorkspace.jsx +0 -400
- frontend/src/components/InvoiceItemsModal/DeleteItemAtInvoice.jsx +0 -73
- frontend/src/components/InvoiceItemsModal/InvoiceItemsModal.jsx +0 -308
- frontend/src/components/Reports/DailyReport.jsx +0 -284
- frontend/src/components/Reports/ReportsWorkspace.jsx +0 -505
- frontend/src/components/Settings/CompanyProfileTab.jsx +0 -182
- frontend/src/components/Suppliers/SuppliersTab.jsx +0 -381
- frontend/src/components/Suppliers/SuppliersWorkspace.jsx +0 -417
- frontend/src/components/Taps.jsx +0 -51
- frontend/src/components/Tenants/TenantsAdminTab.jsx +0 -197
- frontend/src/components/Tenants/TenantsTab.jsx +0 -146
- frontend/src/components/Users/UsersManagementTab.jsx +0 -272
- frontend/src/components/Users/UsersTab.jsx +0 -163
- frontend/src/hooks/useProducts.jsx +0 -21
- frontend/src/index.css +0 -30
- frontend/src/main.jsx +0 -7
- frontend/src/pages/Hero.jsx +0 -178
- frontend/src/pages/Login.jsx +0 -137
- frontend/src/pages/Login_fixed.jsx +0 -286
- frontend/src/pages/Login_new.jsx +0 -2
- frontend/src/routes/routes.jsx +0 -77
- frontend/src/utils/exports.js +0 -55
- frontend/src/utils/formatters.js +0 -24
- frontend/src/utils/permissions.js +0 -29
- frontend/src/utils/whatsapp.js +0 -36
- frontend/vite.config.js +0 -8
frontend/.env
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
VITE_API_URL=http://localhost:5000
|
|
|
|
|
|
frontend/.gitignore
DELETED
|
@@ -1,24 +0,0 @@
|
|
| 1 |
-
# Logs
|
| 2 |
-
logs
|
| 3 |
-
*.log
|
| 4 |
-
npm-debug.log*
|
| 5 |
-
yarn-debug.log*
|
| 6 |
-
yarn-error.log*
|
| 7 |
-
pnpm-debug.log*
|
| 8 |
-
lerna-debug.log*
|
| 9 |
-
|
| 10 |
-
node_modules
|
| 11 |
-
dist
|
| 12 |
-
dist-ssr
|
| 13 |
-
*.local
|
| 14 |
-
|
| 15 |
-
# Editor directories and files
|
| 16 |
-
.vscode/*
|
| 17 |
-
!.vscode/extensions.json
|
| 18 |
-
.idea
|
| 19 |
-
.DS_Store
|
| 20 |
-
*.suo
|
| 21 |
-
*.ntvs*
|
| 22 |
-
*.njsproj
|
| 23 |
-
*.sln
|
| 24 |
-
*.sw?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/README.md
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
# React + Vite
|
| 2 |
-
|
| 3 |
-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
| 4 |
-
|
| 5 |
-
Currently, two official plugins are available:
|
| 6 |
-
|
| 7 |
-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
|
| 8 |
-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
| 9 |
-
|
| 10 |
-
## React Compiler
|
| 11 |
-
|
| 12 |
-
The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
|
| 13 |
-
|
| 14 |
-
## Expanding the ESLint configuration
|
| 15 |
-
|
| 16 |
-
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/eslint.config.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
| 1 |
-
import js from '@eslint/js'
|
| 2 |
-
import globals from 'globals'
|
| 3 |
-
import reactHooks from 'eslint-plugin-react-hooks'
|
| 4 |
-
import reactRefresh from 'eslint-plugin-react-refresh'
|
| 5 |
-
import { defineConfig, globalIgnores } from 'eslint/config'
|
| 6 |
-
|
| 7 |
-
export default defineConfig([
|
| 8 |
-
globalIgnores(['dist']),
|
| 9 |
-
{
|
| 10 |
-
files: ['**/*.{js,jsx}'],
|
| 11 |
-
extends: [
|
| 12 |
-
js.configs.recommended,
|
| 13 |
-
reactHooks.configs.flat.recommended,
|
| 14 |
-
reactRefresh.configs.vite,
|
| 15 |
-
],
|
| 16 |
-
languageOptions: {
|
| 17 |
-
ecmaVersion: 2020,
|
| 18 |
-
globals: globals.browser,
|
| 19 |
-
parserOptions: {
|
| 20 |
-
ecmaVersion: 'latest',
|
| 21 |
-
ecmaFeatures: { jsx: true },
|
| 22 |
-
sourceType: 'module',
|
| 23 |
-
},
|
| 24 |
-
},
|
| 25 |
-
rules: {
|
| 26 |
-
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
|
| 27 |
-
},
|
| 28 |
-
},
|
| 29 |
-
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/hook/useDebounce.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
| 1 |
-
import { useState, useEffect } from 'react';
|
| 2 |
-
|
| 3 |
-
export function useDebounce(value, delay) {
|
| 4 |
-
const [debouncedValue, setDebouncedValue] = useState(value);
|
| 5 |
-
|
| 6 |
-
useEffect(() => {
|
| 7 |
-
// Update debounced value after delay
|
| 8 |
-
const handler = setTimeout(() => {
|
| 9 |
-
setDebouncedValue(value);
|
| 10 |
-
}, delay);
|
| 11 |
-
|
| 12 |
-
// Cancel the timeout if value changes (also on delay change or unmount)
|
| 13 |
-
return () => {
|
| 14 |
-
clearTimeout(handler);
|
| 15 |
-
};
|
| 16 |
-
}, [value, delay]);
|
| 17 |
-
|
| 18 |
-
return debouncedValue;
|
| 19 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/index.html
DELETED
|
@@ -1,13 +0,0 @@
|
|
| 1 |
-
<!doctype html>
|
| 2 |
-
<html lang="ar">
|
| 3 |
-
<head>
|
| 4 |
-
<meta charset="UTF-8" />
|
| 5 |
-
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
| 6 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 7 |
-
<title>نظام إدارة المبيعات </title>
|
| 8 |
-
</head>
|
| 9 |
-
<body>
|
| 10 |
-
<div id="root" dir="rtl"></div>
|
| 11 |
-
<script type="module" src="/src/main.jsx"></script>
|
| 12 |
-
</body>
|
| 13 |
-
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/main.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
| 1 |
-
/* global require */
|
| 2 |
-
const { app, BrowserWindow } = require('electron');
|
| 3 |
-
|
| 4 |
-
function createWindow() {
|
| 5 |
-
const win = new BrowserWindow({
|
| 6 |
-
width: 1000,
|
| 7 |
-
height: 700,
|
| 8 |
-
});
|
| 9 |
-
|
| 10 |
-
win.loadURL('http://localhost:5173/'); // React app
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
app.whenReady().then(createWindow);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/package-lock.json
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
frontend/package.json
DELETED
|
@@ -1,58 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"name": "weak4",
|
| 3 |
-
"private": true,
|
| 4 |
-
"version": "0.0.0",
|
| 5 |
-
"type": "module",
|
| 6 |
-
"scripts": {
|
| 7 |
-
"dev": "vite",
|
| 8 |
-
"electron": "electron main.js",
|
| 9 |
-
"build": "vite build",
|
| 10 |
-
"dist": "electron-builder --win",
|
| 11 |
-
"lint": "eslint .",
|
| 12 |
-
"preview": "vite preview"
|
| 13 |
-
},
|
| 14 |
-
"dependencies": {
|
| 15 |
-
"@tanstack/react-query": "^5.90.12",
|
| 16 |
-
"axios": "^1.13.2",
|
| 17 |
-
"formik": "^2.4.9",
|
| 18 |
-
"lucide-react": "^0.561.0",
|
| 19 |
-
"react": "^18.2.0",
|
| 20 |
-
"react-dom": "^18.2.0",
|
| 21 |
-
"react-icons": "^5.5.0",
|
| 22 |
-
"react-router-dom": "^7.10.1",
|
| 23 |
-
"react-slick": "^0.31.0",
|
| 24 |
-
"react-toastify": "^11.0.5",
|
| 25 |
-
"slick-carousel": "^1.8.1",
|
| 26 |
-
"yup": "^1.7.1"
|
| 27 |
-
},
|
| 28 |
-
"devDependencies": {
|
| 29 |
-
"@eslint/js": "^9.39.1",
|
| 30 |
-
"@tailwindcss/vite": "^4.1.18",
|
| 31 |
-
"@types/react": "^18.2.7",
|
| 32 |
-
"@types/react-dom": "^18.2.4",
|
| 33 |
-
"@vitejs/plugin-react": "^5.1.1",
|
| 34 |
-
"electron": "^25.2.0",
|
| 35 |
-
"electron-builder": "^26.8.1",
|
| 36 |
-
"eslint": "^9.39.1",
|
| 37 |
-
"eslint-plugin-react-hooks": "^7.0.1",
|
| 38 |
-
"eslint-plugin-react-refresh": "^0.4.24",
|
| 39 |
-
"globals": "^16.5.0",
|
| 40 |
-
"tailwindcss": "^4.1.18",
|
| 41 |
-
"vite": "^7.2.7"
|
| 42 |
-
},
|
| 43 |
-
"build": {
|
| 44 |
-
"appId": "com.weak4.app",
|
| 45 |
-
"productName": "Weak4",
|
| 46 |
-
"files": [
|
| 47 |
-
"dist/**/*",
|
| 48 |
-
"main.js",
|
| 49 |
-
"package.json"
|
| 50 |
-
],
|
| 51 |
-
"directories": {
|
| 52 |
-
"buildResources": "assets"
|
| 53 |
-
},
|
| 54 |
-
"win": {
|
| 55 |
-
"target": "nsis"
|
| 56 |
-
}
|
| 57 |
-
}
|
| 58 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/App.jsx
DELETED
|
@@ -1,32 +0,0 @@
|
|
| 1 |
-
import { RouterProvider } from "react-router-dom";
|
| 2 |
-
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
| 3 |
-
import { routes } from './routes/routes';
|
| 4 |
-
import { ToastContainer } from "react-toastify";
|
| 5 |
-
import "react-toastify/dist/ReactToastify.css";
|
| 6 |
-
import axios from "axios";
|
| 7 |
-
import { AuthProvider } from "./Context/AuthContext";
|
| 8 |
-
|
| 9 |
-
axios.defaults.withCredentials = true;
|
| 10 |
-
|
| 11 |
-
const queryClient = new QueryClient();
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
const App = () => {
|
| 15 |
-
return (
|
| 16 |
-
<QueryClientProvider client={queryClient}>
|
| 17 |
-
<AuthProvider>
|
| 18 |
-
<RouterProvider router={routes} />
|
| 19 |
-
<ToastContainer
|
| 20 |
-
position="top-right"
|
| 21 |
-
autoClose={3000}
|
| 22 |
-
hideProgressBar={false}
|
| 23 |
-
closeOnClick
|
| 24 |
-
pauseOnHover
|
| 25 |
-
rtl={true}
|
| 26 |
-
/>
|
| 27 |
-
</AuthProvider>
|
| 28 |
-
</QueryClientProvider>
|
| 29 |
-
);
|
| 30 |
-
};
|
| 31 |
-
|
| 32 |
-
export default App;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/Context/AuthContext.jsx
DELETED
|
@@ -1,58 +0,0 @@
|
|
| 1 |
-
/* eslint-disable react-refresh/only-export-components */
|
| 2 |
-
import { createContext, useContext, useEffect, useState } from "react";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
|
| 5 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 6 |
-
const AuthContext = createContext(null);
|
| 7 |
-
|
| 8 |
-
export const AuthProvider = ({ children }) => {
|
| 9 |
-
const [user, setUser] = useState(null);
|
| 10 |
-
const [isBootstrapping, setIsBootstrapping] = useState(true);
|
| 11 |
-
|
| 12 |
-
const refreshSession = async () => {
|
| 13 |
-
try {
|
| 14 |
-
const res = await axios.get(`${apiUrl}/auth/me`);
|
| 15 |
-
setUser(res.data.user || null);
|
| 16 |
-
return res.data.user || null;
|
| 17 |
-
} catch {
|
| 18 |
-
setUser(null);
|
| 19 |
-
return null;
|
| 20 |
-
} finally {
|
| 21 |
-
setIsBootstrapping(false);
|
| 22 |
-
}
|
| 23 |
-
};
|
| 24 |
-
|
| 25 |
-
useEffect(() => {
|
| 26 |
-
refreshSession();
|
| 27 |
-
}, []);
|
| 28 |
-
|
| 29 |
-
const login = async (credentials) => {
|
| 30 |
-
const res = await axios.post(`${apiUrl}/auth/login`, credentials);
|
| 31 |
-
setUser(res.data.user || null);
|
| 32 |
-
return res.data;
|
| 33 |
-
};
|
| 34 |
-
|
| 35 |
-
const logout = async () => {
|
| 36 |
-
try {
|
| 37 |
-
await axios.post(`${apiUrl}/auth/logout`);
|
| 38 |
-
} finally {
|
| 39 |
-
setUser(null);
|
| 40 |
-
}
|
| 41 |
-
};
|
| 42 |
-
|
| 43 |
-
return (
|
| 44 |
-
<AuthContext.Provider value={{ user, setUser, login, logout, refreshSession, isBootstrapping }}>
|
| 45 |
-
{children}
|
| 46 |
-
</AuthContext.Provider>
|
| 47 |
-
);
|
| 48 |
-
};
|
| 49 |
-
|
| 50 |
-
export const useAuth = () => {
|
| 51 |
-
const context = useContext(AuthContext);
|
| 52 |
-
|
| 53 |
-
if (!context) {
|
| 54 |
-
throw new Error("useAuth must be used within AuthProvider");
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
return context;
|
| 58 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/common/LoaderSpinner.jsx
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
const LodderSpinner = () => {
|
| 3 |
-
return (
|
| 4 |
-
<div className="flex items-center justify-center w-full min-h-dvh">
|
| 5 |
-
<div
|
| 6 |
-
className="border-4 border-gray-300 border-t-black rounded-full animate-spin"
|
| 7 |
-
style={{ width: 40, height: 40 }}
|
| 8 |
-
/>
|
| 9 |
-
</div>)
|
| 10 |
-
}
|
| 11 |
-
|
| 12 |
-
export default LodderSpinner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/common/Pagination.jsx
DELETED
|
@@ -1,94 +0,0 @@
|
|
| 1 |
-
import { ChevronLeft, ChevronRight } from "lucide-react";
|
| 2 |
-
|
| 3 |
-
/**
|
| 4 |
-
* A reusable Pagination component designed with the system's premium aesthetic.
|
| 5 |
-
*
|
| 6 |
-
* @param {Object} props
|
| 7 |
-
* @param {number} props.currentPage - Current active page (1-indexed)
|
| 8 |
-
* @param {number} props.totalPages - Total number of pages
|
| 9 |
-
* @param {function} props.onPageChange - Callback when page is changed
|
| 10 |
-
* @param {number} props.totalItems - Total count of items
|
| 11 |
-
* @param {number} props.limit - Items per page
|
| 12 |
-
*/
|
| 13 |
-
const Pagination = ({ currentPage, totalPages, onPageChange, totalItems, limit }) => {
|
| 14 |
-
if (totalPages <= 1) return null;
|
| 15 |
-
|
| 16 |
-
const startItem = (currentPage - 1) * limit + 1;
|
| 17 |
-
const endItem = Math.min(currentPage * limit, totalItems);
|
| 18 |
-
|
| 19 |
-
const getPageNumbers = () => {
|
| 20 |
-
const pages = [];
|
| 21 |
-
const showPages = 5;
|
| 22 |
-
let start = Math.max(1, currentPage - Math.floor(showPages / 2));
|
| 23 |
-
let end = Math.min(totalPages, start + showPages - 1);
|
| 24 |
-
|
| 25 |
-
if (end - start + 1 < showPages) {
|
| 26 |
-
start = Math.max(1, end - showPages + 1);
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
for (let i = start; i <= end; i++) {
|
| 30 |
-
pages.push(i);
|
| 31 |
-
}
|
| 32 |
-
return pages;
|
| 33 |
-
};
|
| 34 |
-
|
| 35 |
-
return (
|
| 36 |
-
<div className="flex flex-col items-center justify-between gap-4 border-t border-slate-100 p-5 md:flex-row">
|
| 37 |
-
<div className="text-sm font-medium text-slate-500">
|
| 38 |
-
عرض <span className="font-bold text-slate-900">{startItem}-{endItem}</span> من <span className="font-bold text-slate-900">{totalItems}</span> عنصر
|
| 39 |
-
</div>
|
| 40 |
-
|
| 41 |
-
<div className="flex items-center gap-2">
|
| 42 |
-
<button
|
| 43 |
-
type="button"
|
| 44 |
-
disabled={currentPage === 1}
|
| 45 |
-
onClick={() => onPageChange(currentPage - 1)}
|
| 46 |
-
className="flex h-10 w-10 items-center justify-center rounded-xl border border-slate-200 bg-white text-slate-600 transition hover:bg-slate-50 disabled:opacity-40 disabled:hover:bg-white"
|
| 47 |
-
>
|
| 48 |
-
<ChevronRight size={18} />
|
| 49 |
-
</button>
|
| 50 |
-
|
| 51 |
-
<div className="flex items-center gap-1">
|
| 52 |
-
{getPageNumbers().map((page) => (
|
| 53 |
-
<button
|
| 54 |
-
key={page}
|
| 55 |
-
type="button"
|
| 56 |
-
onClick={() => onPageChange(page)}
|
| 57 |
-
className={`flex h-10 w-10 items-center justify-center rounded-xl font-bold transition ${
|
| 58 |
-
currentPage === page
|
| 59 |
-
? "bg-emerald-600 text-white shadow-lg shadow-emerald-600/20"
|
| 60 |
-
: "border border-transparent bg-transparent text-slate-600 hover:bg-slate-100"
|
| 61 |
-
}`}
|
| 62 |
-
>
|
| 63 |
-
{page}
|
| 64 |
-
</button>
|
| 65 |
-
))}
|
| 66 |
-
|
| 67 |
-
{totalPages > 5 && getPageNumbers()[getPageNumbers().length - 1] < totalPages && (
|
| 68 |
-
<>
|
| 69 |
-
<span className="px-1 text-slate-400">...</span>
|
| 70 |
-
<button
|
| 71 |
-
type="button"
|
| 72 |
-
onClick={() => onPageChange(totalPages)}
|
| 73 |
-
className={`flex h-10 w-10 items-center justify-center rounded-xl font-bold transition border border-transparent bg-transparent text-slate-600 hover:bg-slate-100`}
|
| 74 |
-
>
|
| 75 |
-
{totalPages}
|
| 76 |
-
</button>
|
| 77 |
-
</>
|
| 78 |
-
)}
|
| 79 |
-
</div>
|
| 80 |
-
|
| 81 |
-
<button
|
| 82 |
-
type="button"
|
| 83 |
-
disabled={currentPage === totalPages}
|
| 84 |
-
onClick={() => onPageChange(currentPage + 1)}
|
| 85 |
-
className="flex h-10 w-10 items-center justify-center rounded-xl border border-slate-200 bg-white text-slate-600 transition hover:bg-slate-50 disabled:opacity-40 disabled:hover:bg-white"
|
| 86 |
-
>
|
| 87 |
-
<ChevronLeft size={18} />
|
| 88 |
-
</button>
|
| 89 |
-
</div>
|
| 90 |
-
</div>
|
| 91 |
-
);
|
| 92 |
-
};
|
| 93 |
-
|
| 94 |
-
export default Pagination;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/AddCustomer.jsx
DELETED
|
@@ -1,117 +0,0 @@
|
|
| 1 |
-
import axios from "axios";
|
| 2 |
-
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
| 3 |
-
import { toast } from "react-toastify";
|
| 4 |
-
import { UserPlus, User, CheckCircle, X, Loader2 } from "lucide-react";
|
| 5 |
-
import { useFormik } from "formik";
|
| 6 |
-
import * as Yup from "yup";
|
| 7 |
-
|
| 8 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 9 |
-
|
| 10 |
-
const AddCustomer = () => {
|
| 11 |
-
const queryClient = useQueryClient();
|
| 12 |
-
|
| 13 |
-
const createCustomer = async (data) => {
|
| 14 |
-
const res = await axios.post(
|
| 15 |
-
`${apiUrl}/customers/create`,
|
| 16 |
-
{ name: data.name }
|
| 17 |
-
);
|
| 18 |
-
return res.data;
|
| 19 |
-
};
|
| 20 |
-
|
| 21 |
-
const { mutate, isPending } = useMutation({
|
| 22 |
-
mutationFn: createCustomer,
|
| 23 |
-
onSuccess: () => {
|
| 24 |
-
formik.resetForm();
|
| 25 |
-
queryClient.invalidateQueries({ queryKey: ["customers"] });
|
| 26 |
-
toast.success("تم إضافة العميل بنجاح ✅");
|
| 27 |
-
},
|
| 28 |
-
onError: (error) => {
|
| 29 |
-
const message =
|
| 30 |
-
error.response?.data?.message ||
|
| 31 |
-
error.message ||
|
| 32 |
-
"حدث خطأ غير متوقع";
|
| 33 |
-
toast.error(message);
|
| 34 |
-
}
|
| 35 |
-
});
|
| 36 |
-
|
| 37 |
-
const formik = useFormik({
|
| 38 |
-
initialValues: { name: "" },
|
| 39 |
-
validationSchema: Yup.object({
|
| 40 |
-
name: Yup.string()
|
| 41 |
-
.required("يرجى إدخال اسم العميل")
|
| 42 |
-
.matches(/^[\u0600-\u06FFa-zA-Z\s]+$/, "يجب أن يحتوي الاسم على حروف فقط")
|
| 43 |
-
.min(3, "الاسم يجب أن يكون 3 حروف على الأقل")
|
| 44 |
-
}),
|
| 45 |
-
onSubmit: (values) => mutate(values)
|
| 46 |
-
});
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return (
|
| 51 |
-
<div className="bg-white border border-slate-200 w-full mb-8 rounded-2xl p-6 shadow-sm shadow-slate-200/50 relative overflow-hidden" dir="rtl">
|
| 52 |
-
{/* Decorative top border */}
|
| 53 |
-
<div className="absolute top-0 right-0 w-full h-1.5 bg-gradient-to-l from-emerald-400 via-emerald-500 to-teal-600"></div>
|
| 54 |
-
|
| 55 |
-
<div className="flex items-center gap-3 mb-6 mt-2">
|
| 56 |
-
<div className="w-12 h-12 rounded-full bg-emerald-50 flex items-center justify-center text-emerald-600 shadow-inner">
|
| 57 |
-
<UserPlus size={26} />
|
| 58 |
-
</div>
|
| 59 |
-
<div>
|
| 60 |
-
<h3 className="text-xl md:text-2xl font-bold text-slate-800">إضافة عميل جديد</h3>
|
| 61 |
-
<p className="text-sm text-slate-500 mt-1">قم بتسجيل بيانات العميل الجديد لفتح حساب له وإصدار الفواتير.</p>
|
| 62 |
-
</div>
|
| 63 |
-
</div>
|
| 64 |
-
|
| 65 |
-
<div className="flex flex-col md:flex-row gap-4 items-start md:items-start">
|
| 66 |
-
<div className="relative w-full flex-1">
|
| 67 |
-
<div className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400 bg-slate-50/80 p-1 rounded-md">
|
| 68 |
-
<User size={20} />
|
| 69 |
-
</div>
|
| 70 |
-
<input
|
| 71 |
-
name="name"
|
| 72 |
-
type="text"
|
| 73 |
-
placeholder="أدخل اسم العميل (مثال: محمد أحمد)"
|
| 74 |
-
value={formik.values.name}
|
| 75 |
-
onChange={formik.handleChange}
|
| 76 |
-
onBlur={formik.handleBlur}
|
| 77 |
-
className={`w-full pr-14 pl-4 py-3.5 md:text-lg bg-slate-50 border ${formik.touched.name && formik.errors.name ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl focus:outline-none focus:ring-2 focus:bg-white transition-all placeholder:text-slate-400 font-medium text-slate-700`}
|
| 78 |
-
/>
|
| 79 |
-
{formik.touched.name && formik.errors.name && (
|
| 80 |
-
<p className="absolute -bottom-6 right-2 text-rose-500 text-xs font-bold">{formik.errors.name}</p>
|
| 81 |
-
)}
|
| 82 |
-
</div>
|
| 83 |
-
|
| 84 |
-
<div className="flex items-center gap-3 w-full md:w-auto mt-2 md:mt-0">
|
| 85 |
-
<button
|
| 86 |
-
onClick={formik.handleSubmit}
|
| 87 |
-
disabled={isPending}
|
| 88 |
-
className="flex-1 md:flex-none flex items-center justify-center gap-2 bg-emerald-600 text-white px-8 py-3.5 rounded-xl hover:bg-emerald-700 font-bold md:text-lg shadow-sm transition-all shadow-emerald-600/20 disabled:opacity-70 disabled:cursor-not-allowed hover:-translate-y-0.5 whitespace-nowrap"
|
| 89 |
-
>
|
| 90 |
-
{isPending ? (
|
| 91 |
-
<>
|
| 92 |
-
<Loader2 className="animate-spin" size={20} />
|
| 93 |
-
<span>جاري الحفظ...</span>
|
| 94 |
-
</>
|
| 95 |
-
) : (
|
| 96 |
-
<>
|
| 97 |
-
<CheckCircle size={20} />
|
| 98 |
-
<span>حفظ العميل</span>
|
| 99 |
-
</>
|
| 100 |
-
)}
|
| 101 |
-
</button>
|
| 102 |
-
|
| 103 |
-
<button
|
| 104 |
-
type="button"
|
| 105 |
-
className="flex-1 md:flex-none flex items-center justify-center gap-2 bg-white border border-slate-200 px-6 py-3.5 rounded-xl text-slate-600 hover:bg-slate-50 hover:text-rose-600 hover:border-rose-200 font-bold md:text-lg transition-all whitespace-nowrap shadow-sm"
|
| 106 |
-
onClick={() => formik.resetForm()}
|
| 107 |
-
>
|
| 108 |
-
<X size={20} />
|
| 109 |
-
<span>إلغاء</span>
|
| 110 |
-
</button>
|
| 111 |
-
</div>
|
| 112 |
-
</div>
|
| 113 |
-
</div>
|
| 114 |
-
);
|
| 115 |
-
};
|
| 116 |
-
|
| 117 |
-
export default AddCustomer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/CustomerDetails/CustomerDetails.jsx
DELETED
|
@@ -1,492 +0,0 @@
|
|
| 1 |
-
import axios from "axios";
|
| 2 |
-
import { useParams, useNavigate } from "react-router-dom";
|
| 3 |
-
import { useQuery } from "@tanstack/react-query";
|
| 4 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 5 |
-
import {
|
| 6 |
-
ArrowLeft,
|
| 7 |
-
Calendar,
|
| 8 |
-
FileText,
|
| 9 |
-
AlertCircle,
|
| 10 |
-
CheckCircle,
|
| 11 |
-
Clock,
|
| 12 |
-
Pencil,
|
| 13 |
-
ClipboardList,
|
| 14 |
-
MessageCircle,
|
| 15 |
-
Printer,
|
| 16 |
-
Wallet,
|
| 17 |
-
ChevronDown,
|
| 18 |
-
ChevronUp,
|
| 19 |
-
ChevronRight,
|
| 20 |
-
ChevronLeft,
|
| 21 |
-
TrendingUp,
|
| 22 |
-
CreditCard,
|
| 23 |
-
Hourglass,
|
| 24 |
-
} from "lucide-react";
|
| 25 |
-
import { useState } from "react";
|
| 26 |
-
import EditInvoiceItemModal from "./EditInvoiceItemModal";
|
| 27 |
-
import DeleteInvoiceItemButton from "../InvoiceItemsModal/DeleteItemAtInvoice";
|
| 28 |
-
import PayInvoiceModal from "./PayInvoiceModal";
|
| 29 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 30 |
-
import { getPermissions } from "../../utils/permissions";
|
| 31 |
-
import { buildCustomerInvoiceMessage, openWhatsAppMessage } from "../../utils/whatsapp";
|
| 32 |
-
|
| 33 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 34 |
-
|
| 35 |
-
// ─── Status badge ────────────────────────────────────────────────────────────
|
| 36 |
-
const getStatusBadge = (status) => {
|
| 37 |
-
const map = {
|
| 38 |
-
paid: { label: "مدفوعة", color: "bg-emerald-100 text-emerald-800 border-emerald-200", icon: <CheckCircle size={16} /> },
|
| 39 |
-
unpaid: { label: "غير مدفوعة", color: "bg-rose-100 text-rose-800 border-rose-200", icon: <AlertCircle size={16} /> },
|
| 40 |
-
partial: { label: "مدفوعة جزئياً", color: "bg-amber-100 text-amber-800 border-amber-200", icon: <Clock size={16} /> },
|
| 41 |
-
open: { label: "مفتوحة", color: "bg-rose-100 text-rose-800 border-rose-200", icon: <AlertCircle size={16} /> },
|
| 42 |
-
};
|
| 43 |
-
const item = map[status] || map.unpaid;
|
| 44 |
-
return (
|
| 45 |
-
<span className={`${item.color} border px-4 py-1.5 rounded-full text-sm font-bold flex items-center gap-2 w-fit shadow-sm`}>
|
| 46 |
-
{item.icon}{item.label}
|
| 47 |
-
</span>
|
| 48 |
-
);
|
| 49 |
-
};
|
| 50 |
-
|
| 51 |
-
// ─── Summary card ─────────────────────────────────────────────────────────────
|
| 52 |
-
const Summary = ({ title, value, type }) => {
|
| 53 |
-
const variants = {
|
| 54 |
-
blue: "bg-blue-50 border-blue-100 text-blue-700",
|
| 55 |
-
green: "bg-emerald-50 border-emerald-100 text-emerald-700",
|
| 56 |
-
red: "bg-rose-50 border-rose-100 text-rose-700",
|
| 57 |
-
};
|
| 58 |
-
const valueColor = { blue: "text-blue-700", green: "text-emerald-700", red: "text-rose-600" };
|
| 59 |
-
return (
|
| 60 |
-
<div className={`${variants[type]} border rounded-2xl p-5 flex flex-col items-center justify-center text-center shadow-sm hover:-translate-y-1 transition-transform`}>
|
| 61 |
-
<p className="text-sm font-bold opacity-80 mb-2">{title}</p>
|
| 62 |
-
<p className={`text-2xl font-black ${valueColor[type]} drop-shadow-sm`}>
|
| 63 |
-
{value.toFixed(2)} <span className="text-sm font-normal opacity-80">ج.م</span>
|
| 64 |
-
</p>
|
| 65 |
-
</div>
|
| 66 |
-
);
|
| 67 |
-
};
|
| 68 |
-
|
| 69 |
-
// ─── Stat card (customer overview) ───────────────────────────────────────────
|
| 70 |
-
const StatCard = ({ icon, label, value, color }) => (
|
| 71 |
-
<div className={`flex items-center gap-3 bg-white border ${color} rounded-xl px-5 py-4 shadow-sm flex-1 min-w-0`}>
|
| 72 |
-
<div className={`w-10 h-10 rounded-full flex items-center justify-center ${color.replace("border-", "bg-").replace("-200", "-100")}`}>
|
| 73 |
-
{icon}
|
| 74 |
-
</div>
|
| 75 |
-
<div className="min-w-0">
|
| 76 |
-
<p className="text-xs text-slate-500 font-medium truncate">{label}</p>
|
| 77 |
-
<p className="text-lg font-black text-slate-800 truncate">{value}</p>
|
| 78 |
-
</div>
|
| 79 |
-
</div>
|
| 80 |
-
);
|
| 81 |
-
|
| 82 |
-
// ─── Pagination ───────────────────────────────────────────────────────────────
|
| 83 |
-
const Pagination = ({ pagination, page, setPage }) => {
|
| 84 |
-
if (!pagination || pagination.totalPages <= 1) return null;
|
| 85 |
-
const { total, totalPages } = pagination;
|
| 86 |
-
|
| 87 |
-
return (
|
| 88 |
-
<div className="flex flex-col sm:flex-row items-center justify-between gap-3 bg-white border border-slate-200 rounded-2xl px-6 py-4 shadow-sm">
|
| 89 |
-
<p className="text-sm text-slate-500 font-medium">
|
| 90 |
-
إجمالي الفواتير: <span className="font-bold text-slate-700">{total}</span>
|
| 91 |
-
</p>
|
| 92 |
-
<div className="flex items-center gap-2">
|
| 93 |
-
<button
|
| 94 |
-
onClick={() => setPage((p) => Math.max(1, p - 1))}
|
| 95 |
-
disabled={page === 1}
|
| 96 |
-
className="w-9 h-9 flex items-center justify-center rounded-lg border border-slate-200 text-slate-600 hover:bg-slate-100 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
|
| 97 |
-
>
|
| 98 |
-
<ChevronRight size={18} />
|
| 99 |
-
</button>
|
| 100 |
-
|
| 101 |
-
{Array.from({ length: totalPages }, (_, i) => i + 1).map((p) => (
|
| 102 |
-
<button
|
| 103 |
-
key={p}
|
| 104 |
-
onClick={() => setPage(p)}
|
| 105 |
-
className={`w-9 h-9 flex items-center justify-center rounded-lg border text-sm font-bold transition-colors ${p === page
|
| 106 |
-
? "bg-amber-500 border-amber-500 text-white shadow-sm"
|
| 107 |
-
: "border-slate-200 text-slate-600 hover:bg-slate-100"
|
| 108 |
-
}`}
|
| 109 |
-
>
|
| 110 |
-
{p}
|
| 111 |
-
</button>
|
| 112 |
-
))}
|
| 113 |
-
|
| 114 |
-
<button
|
| 115 |
-
onClick={() => setPage((p) => Math.min(totalPages, p + 1))}
|
| 116 |
-
disabled={page === totalPages}
|
| 117 |
-
className="w-9 h-9 flex items-center justify-center rounded-lg border border-slate-200 text-slate-600 hover:bg-slate-100 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
|
| 118 |
-
>
|
| 119 |
-
<ChevronLeft size={18} />
|
| 120 |
-
</button>
|
| 121 |
-
</div>
|
| 122 |
-
</div>
|
| 123 |
-
);
|
| 124 |
-
};
|
| 125 |
-
|
| 126 |
-
// ─── Invoice card ─────────────────────────────────────────────────────────────
|
| 127 |
-
const InvoiceCard = ({
|
| 128 |
-
invoice, customer, permissions, companyName,
|
| 129 |
-
onShare, onPrint, onEditItem, onPay,
|
| 130 |
-
}) => {
|
| 131 |
-
const [collapsed, setCollapsed] = useState(false);
|
| 132 |
-
|
| 133 |
-
return (
|
| 134 |
-
<div className="bg-white rounded-2xl shadow-sm shadow-slate-200/50 border border-slate-200 overflow-hidden">
|
| 135 |
-
{/* ── Header ── */}
|
| 136 |
-
<div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 px-6 md:px-8 py-5 border-b border-slate-100">
|
| 137 |
-
<div className="flex flex-col md:flex-row items-start md:items-center gap-4 flex-1 min-w-0">
|
| 138 |
-
<div className="flex items-center gap-3 flex-wrap">
|
| 139 |
-
<h2 className="text-xl font-bold text-slate-800 flex items-center gap-2">
|
| 140 |
-
<FileText size={22} className="text-amber-500 shrink-0" />
|
| 141 |
-
فاتورة: {invoice.invoiceNumber}
|
| 142 |
-
</h2>
|
| 143 |
-
{getStatusBadge(invoice.status)}
|
| 144 |
-
</div>
|
| 145 |
-
|
| 146 |
-
<div className="flex items-center gap-2">
|
| 147 |
-
<button
|
| 148 |
-
onClick={() => onShare(invoice)}
|
| 149 |
-
className="flex items-center gap-2 px-4 py-2 bg-emerald-50 text-emerald-600 rounded-lg hover:bg-emerald-600 hover:text-white transition-colors font-bold text-sm border border-emerald-100 shadow-sm"
|
| 150 |
-
>
|
| 151 |
-
<MessageCircle size={18} />
|
| 152 |
-
<span className="hidden md:inline">واتساب</span>
|
| 153 |
-
</button>
|
| 154 |
-
<button
|
| 155 |
-
onClick={() => onPrint(invoice)}
|
| 156 |
-
className="flex items-center gap-2 px-4 py-2 bg-slate-50 text-slate-600 rounded-lg hover:bg-slate-800 hover:text-white transition-colors font-bold text-sm border border-slate-200 shadow-sm"
|
| 157 |
-
>
|
| 158 |
-
<Printer size={18} />
|
| 159 |
-
<span className="hidden md:inline">طباعة</span>
|
| 160 |
-
</button>
|
| 161 |
-
</div>
|
| 162 |
-
</div>
|
| 163 |
-
|
| 164 |
-
<div className="flex items-center gap-3 self-end md:self-auto">
|
| 165 |
-
<span className="text-sm text-slate-500 font-medium hidden sm:flex items-center gap-2 bg-slate-50 px-4 py-2 rounded-lg border border-slate-100">
|
| 166 |
-
<Clock size={15} className="text-slate-400" />
|
| 167 |
-
{new Date(invoice.updatedAt).toLocaleString("ar-EG")}
|
| 168 |
-
</span>
|
| 169 |
-
{/* Collapse toggle */}
|
| 170 |
-
<button
|
| 171 |
-
onClick={() => setCollapsed((c) => !c)}
|
| 172 |
-
className="w-9 h-9 flex items-center justify-center rounded-lg border border-slate-200 text-slate-500 hover:bg-slate-50 transition-colors"
|
| 173 |
-
title={collapsed ? "توسيع" : "طي"}
|
| 174 |
-
>
|
| 175 |
-
{collapsed ? <ChevronDown size={18} /> : <ChevronUp size={18} />}
|
| 176 |
-
</button>
|
| 177 |
-
</div>
|
| 178 |
-
</div>
|
| 179 |
-
|
| 180 |
-
{/* ── Collapsible body ── */}
|
| 181 |
-
{!collapsed && (
|
| 182 |
-
<div className="px-6 md:px-8 py-6 space-y-6">
|
| 183 |
-
{/* Items */}
|
| 184 |
-
<div className="bg-slate-50 rounded-2xl p-5 md:p-6 border border-slate-100 shadow-inner">
|
| 185 |
-
<h3 className="font-bold text-lg text-slate-800 mb-5 flex items-center gap-2">
|
| 186 |
-
<ClipboardList size={20} className="text-amber-500" /> بنود الفاتورة
|
| 187 |
-
</h3>
|
| 188 |
-
{invoice.items.length === 0 ? (
|
| 189 |
-
<p className="text-slate-500 text-center py-8 bg-white rounded-xl border border-slate-100">لا توجد بنود في هذه الفاتورة</p>
|
| 190 |
-
) : (
|
| 191 |
-
<div className="space-y-3">
|
| 192 |
-
{invoice.items.map((item) => (
|
| 193 |
-
<div key={item._id} className="flex flex-col md:flex-row justify-between items-start md:items-center bg-white border border-slate-200 rounded-xl p-4 shadow-sm hover:shadow-md transition-shadow">
|
| 194 |
-
<div className="mb-3 md:mb-0">
|
| 195 |
-
<span className="text-lg font-bold text-slate-800 block mb-2">{item.name}</span>
|
| 196 |
-
<span className="text-sm font-medium text-slate-600 bg-slate-100 px-3 py-1.5 rounded-lg border border-slate-200 flex items-center gap-2 w-fit">
|
| 197 |
-
<span>الكمية: <b className="text-amber-600">{item.quantity}</b></span>
|
| 198 |
-
<span className="w-1 h-1 bg-slate-300 rounded-full" />
|
| 199 |
-
<span>السعر: <b>{item.price}</b> ج.م</span>
|
| 200 |
-
</span>
|
| 201 |
-
</div>
|
| 202 |
-
<div className="flex items-center gap-4 border-t md:border-t-0 border-slate-100 pt-3 md:pt-0 w-full md:w-auto justify-between md:justify-end">
|
| 203 |
-
<span className="font-black text-lg bg-emerald-50 text-emerald-700 px-4 py-2 rounded-lg border border-emerald-100 shadow-sm">
|
| 204 |
-
{item.total.toFixed(2)} <span className="text-sm font-normal opacity-80">ج.م</span>
|
| 205 |
-
</span>
|
| 206 |
-
<div className="flex items-center gap-2">
|
| 207 |
-
{permissions.canEditInvoice && (
|
| 208 |
-
<button onClick={() => onEditItem(item, invoice)} className="p-2.5 text-blue-600 bg-blue-50 border border-blue-100 rounded-lg hover:bg-blue-600 hover:text-white transition-colors" title="تعديل البند">
|
| 209 |
-
<Pencil size={18} />
|
| 210 |
-
</button>
|
| 211 |
-
)}
|
| 212 |
-
{permissions.canEditInvoice && (
|
| 213 |
-
<DeleteInvoiceItemButton customerId={customer._id} invoiceId={invoice._id} itemId={item._id} />
|
| 214 |
-
)}
|
| 215 |
-
</div>
|
| 216 |
-
</div>
|
| 217 |
-
</div>
|
| 218 |
-
))}
|
| 219 |
-
</div>
|
| 220 |
-
)}
|
| 221 |
-
</div>
|
| 222 |
-
|
| 223 |
-
{/* Totals */}
|
| 224 |
-
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
| 225 |
-
<Summary title="إجمالي الفاتورة" value={invoice.totalAmount} type="blue" />
|
| 226 |
-
<Summary title="المدفوع حتى الآن" value={invoice.paidAmount} type="green" />
|
| 227 |
-
<Summary title="الرصيد المتبقي" value={invoice.remainingAmount} type="red" />
|
| 228 |
-
</div>
|
| 229 |
-
|
| 230 |
-
{/* Payments log */}
|
| 231 |
-
{invoice.payments?.length > 0 && (
|
| 232 |
-
<div className="bg-emerald-50/50 rounded-2xl p-5 md:p-6 border border-emerald-100">
|
| 233 |
-
<h3 className="font-bold text-lg text-emerald-800 mb-4 flex items-center gap-2">
|
| 234 |
-
<Clock size={20} className="text-emerald-500" /> سجل المدفوعات
|
| 235 |
-
</h3>
|
| 236 |
-
<div className="space-y-3">
|
| 237 |
-
{invoice.payments.map((payment) => (
|
| 238 |
-
<div key={payment._id || payment.date} className="flex justify-between items-center bg-white border border-emerald-100 rounded-xl p-3 shadow-sm hover:border-emerald-200 transition-colors">
|
| 239 |
-
<div className="flex items-center gap-3">
|
| 240 |
-
<div className="w-10 h-10 rounded-full bg-emerald-100 flex items-center justify-center text-emerald-600">
|
| 241 |
-
<CheckCircle size={18} />
|
| 242 |
-
</div>
|
| 243 |
-
<div>
|
| 244 |
-
<p className="font-bold text-slate-700 leading-tight">دفعة {payment.method === "cash" ? "نقدية" : payment.method}</p>
|
| 245 |
-
<p className="text-slate-500 text-sm mt-1">{new Date(payment.date).toLocaleString("ar-EG")}</p>
|
| 246 |
-
</div>
|
| 247 |
-
</div>
|
| 248 |
-
<span className="font-black text-emerald-700 bg-emerald-50 px-4 py-2 rounded-xl border border-emerald-100 shadow-sm">
|
| 249 |
-
+{payment.amount.toFixed(2)} <span className="text-xs font-normal opacity-70">ج.م</span>
|
| 250 |
-
</span>
|
| 251 |
-
</div>
|
| 252 |
-
))}
|
| 253 |
-
</div>
|
| 254 |
-
</div>
|
| 255 |
-
)}
|
| 256 |
-
|
| 257 |
-
{/* Pay button */}
|
| 258 |
-
{permissions.canRecordPayments && invoice.remainingAmount > 0 && (
|
| 259 |
-
<div className="flex justify-center md:justify-start pt-2">
|
| 260 |
-
<button
|
| 261 |
-
onClick={() => onPay(invoice)}
|
| 262 |
-
className="w-full md:w-auto flex items-center justify-center gap-3 bg-emerald-600 text-white px-8 py-3.5 rounded-xl hover:bg-emerald-700 font-bold shadow-sm shadow-emerald-500/20 transition-all hover:-translate-y-0.5 text-lg"
|
| 263 |
-
>
|
| 264 |
-
<Wallet size={24} /> تسجيل دفعة
|
| 265 |
-
</button>
|
| 266 |
-
</div>
|
| 267 |
-
)}
|
| 268 |
-
</div>
|
| 269 |
-
)}
|
| 270 |
-
</div>
|
| 271 |
-
);
|
| 272 |
-
};
|
| 273 |
-
|
| 274 |
-
// ─── Main component ───────────────────────────────────────────────────────────
|
| 275 |
-
const CustomerDetails = () => {
|
| 276 |
-
const { id } = useParams();
|
| 277 |
-
const navigate = useNavigate();
|
| 278 |
-
const { user } = useAuth();
|
| 279 |
-
const permissions = getPermissions(user);
|
| 280 |
-
const companyName = user?.tenant?.name || "نظام إدارة المبيعات";
|
| 281 |
-
|
| 282 |
-
const [openEditModel, setOpenEditModel] = useState(false);
|
| 283 |
-
const [selectedItem, setSelectedItem] = useState(null);
|
| 284 |
-
const [openPayModal, setOpenPayModal] = useState(false);
|
| 285 |
-
const [activeInvoice, setActiveInvoice] = useState(null);
|
| 286 |
-
const [page, setPage] = useState(1);
|
| 287 |
-
|
| 288 |
-
// Status filter
|
| 289 |
-
const [statusFilter, setStatusFilter] = useState("all");
|
| 290 |
-
const STATUS_OPTS = [
|
| 291 |
-
{ value: "all", label: "الكل" },
|
| 292 |
-
{ value: "paid", label: "مدفوعة" },
|
| 293 |
-
{ value: "partial", label: "جزئي" },
|
| 294 |
-
{ value: "unpaid", label: "غير مدفوعة" },
|
| 295 |
-
];
|
| 296 |
-
|
| 297 |
-
const limit = 5;
|
| 298 |
-
|
| 299 |
-
const getCustomerDetails = () =>
|
| 300 |
-
axios.get(`${apiUrl}/customers/${id}?includeInvoices=true&page=${page}&limit=${limit}`);
|
| 301 |
-
|
| 302 |
-
const { data, isLoading } = useQuery({
|
| 303 |
-
queryKey: ["customer", id, page],
|
| 304 |
-
queryFn: getCustomerDetails,
|
| 305 |
-
refetchOnWindowFocus: false,
|
| 306 |
-
keepPreviousData: true, // smooth pagination — no flicker
|
| 307 |
-
});
|
| 308 |
-
|
| 309 |
-
if (isLoading) return <LodderSpinner />;
|
| 310 |
-
|
| 311 |
-
const customer = data?.data;
|
| 312 |
-
const allInvoices = customer?.invoices || [];
|
| 313 |
-
const pagination = customer?.pagination;
|
| 314 |
-
|
| 315 |
-
// Client-side status filter (applied on top of paginated results)
|
| 316 |
-
const invoices =
|
| 317 |
-
statusFilter === "all"
|
| 318 |
-
? allInvoices
|
| 319 |
-
: allInvoices.filter((inv) => inv.status === statusFilter);
|
| 320 |
-
|
| 321 |
-
// Customer-level stats derived from current page data
|
| 322 |
-
const totalRevenue = allInvoices.reduce((s, inv) => s + inv.totalAmount, 0);
|
| 323 |
-
const totalPaid = allInvoices.reduce((s, inv) => s + inv.paidAmount, 0);
|
| 324 |
-
const totalRemaining = allInvoices.reduce((s, inv) => s + inv.remainingAmount, 0);
|
| 325 |
-
|
| 326 |
-
const handleShareWhatsApp = (invoice) => {
|
| 327 |
-
if (!invoice) return;
|
| 328 |
-
openWhatsAppMessage(
|
| 329 |
-
buildCustomerInvoiceMessage({ companyName, customerName: customer.name, invoice })
|
| 330 |
-
);
|
| 331 |
-
};
|
| 332 |
-
|
| 333 |
-
const handlePrint = (invoice) => {
|
| 334 |
-
window.open(`/print/invoice/${customer._id}/${invoice._id}`, "_blank");
|
| 335 |
-
};
|
| 336 |
-
|
| 337 |
-
const handleOpenEditModel = (item, invoice) => {
|
| 338 |
-
setSelectedItem({ ...item, invoiceId: invoice._id });
|
| 339 |
-
setOpenEditModel(true);
|
| 340 |
-
};
|
| 341 |
-
|
| 342 |
-
const closeEditModel = () => {
|
| 343 |
-
setSelectedItem(null);
|
| 344 |
-
setOpenEditModel(false);
|
| 345 |
-
};
|
| 346 |
-
|
| 347 |
-
return (
|
| 348 |
-
<div className="min-h-screen bg-slate-50 p-4 lg:p-8" dir="rtl">
|
| 349 |
-
<div className="max-w-6xl mx-auto space-y-6">
|
| 350 |
-
|
| 351 |
-
{/* ── Customer header ── */}
|
| 352 |
-
<div className="bg-white rounded-2xl p-6 md:p-8 shadow-sm shadow-slate-200/50 border border-slate-200 relative overflow-hidden">
|
| 353 |
-
<div className="absolute top-0 right-0 w-2 h-full bg-gradient-to-b from-amber-400 to-amber-600" />
|
| 354 |
-
<button
|
| 355 |
-
onClick={() => navigate(-1)}
|
| 356 |
-
className="flex items-center gap-2 cursor-pointer text-slate-500 hover:text-slate-900 mb-6 w-fit hover:translate-x-1 transition-transform bg-slate-50 px-4 py-2 rounded-xl border border-slate-100 font-medium"
|
| 357 |
-
>
|
| 358 |
-
<ArrowLeft size={18} /> العودة للقائمة
|
| 359 |
-
</button>
|
| 360 |
-
|
| 361 |
-
<div className="flex flex-col md:flex-row md:items-start justify-between gap-6">
|
| 362 |
-
<div>
|
| 363 |
-
<h1 className="text-3xl font-black text-slate-800 mb-2 flex items-center gap-3">
|
| 364 |
-
<div className="w-14 h-14 rounded-full bg-slate-100 flex items-center justify-center text-slate-500 text-2xl shadow-inner font-bold border border-slate-200">
|
| 365 |
-
{customer?.name?.charAt(0)}
|
| 366 |
-
</div>
|
| 367 |
-
{customer?.name}
|
| 368 |
-
</h1>
|
| 369 |
-
<div className="flex items-center gap-2 text-slate-500 mt-3 font-medium bg-slate-50 w-fit px-4 py-2 rounded-lg border border-slate-100">
|
| 370 |
-
<Calendar size={18} className="text-amber-500" />
|
| 371 |
-
<span>
|
| 372 |
-
عميل منذ{" "}
|
| 373 |
-
<span className="text-slate-700 font-bold">
|
| 374 |
-
{new Date(customer?.createdAt).toLocaleDateString("ar-EG")}
|
| 375 |
-
</span>
|
| 376 |
-
</span>
|
| 377 |
-
</div>
|
| 378 |
-
</div>
|
| 379 |
-
|
| 380 |
-
{/* ── Quick stats row ── */}
|
| 381 |
-
<div className="flex flex-wrap gap-3 flex-1 md:justify-end">
|
| 382 |
-
<StatCard
|
| 383 |
-
icon={<TrendingUp size={18} className="text-blue-600" />}
|
| 384 |
-
label="إجمالي الفواتير"
|
| 385 |
-
value={`${totalRevenue.toFixed(2)} ج.م`}
|
| 386 |
-
color="border-blue-200"
|
| 387 |
-
/>
|
| 388 |
-
<StatCard
|
| 389 |
-
icon={<CheckCircle size={18} className="text-emerald-600" />}
|
| 390 |
-
label="تم تحصيله"
|
| 391 |
-
value={`${totalPaid.toFixed(2)} ج.م`}
|
| 392 |
-
color="border-emerald-200"
|
| 393 |
-
/>
|
| 394 |
-
<StatCard
|
| 395 |
-
icon={<Hourglass size={18} className="text-rose-600" />}
|
| 396 |
-
label="المتبقي"
|
| 397 |
-
value={`${totalRemaining.toFixed(2)} ج.م`}
|
| 398 |
-
color="border-rose-200"
|
| 399 |
-
/>
|
| 400 |
-
</div>
|
| 401 |
-
</div>
|
| 402 |
-
</div>
|
| 403 |
-
|
| 404 |
-
{/* ── Filter bar ── */}
|
| 405 |
-
{allInvoices.length > 0 && (
|
| 406 |
-
<div className="bg-white border border-slate-200 rounded-2xl px-6 py-4 shadow-sm flex flex-wrap items-center gap-3">
|
| 407 |
-
<span className="text-sm font-bold text-slate-600 ml-2">تصفية حسب الحالة:</span>
|
| 408 |
-
{STATUS_OPTS.map((opt) => (
|
| 409 |
-
<button
|
| 410 |
-
key={opt.value}
|
| 411 |
-
onClick={() => { setStatusFilter(opt.value); setPage(1); }}
|
| 412 |
-
className={`px-4 py-1.5 rounded-full text-sm font-bold border transition-colors ${statusFilter === opt.value
|
| 413 |
-
? "bg-amber-500 border-amber-500 text-white shadow-sm"
|
| 414 |
-
: "bg-slate-50 border-slate-200 text-slate-600 hover:bg-slate-100"
|
| 415 |
-
}`}
|
| 416 |
-
>
|
| 417 |
-
{opt.label}
|
| 418 |
-
</button>
|
| 419 |
-
))}
|
| 420 |
-
{pagination && (
|
| 421 |
-
<span className="mr-auto text-sm text-slate-400 font-medium">
|
| 422 |
-
الصفحة {pagination.page} من {pagination.totalPages} · {pagination.total} فاتورة
|
| 423 |
-
</span>
|
| 424 |
-
)}
|
| 425 |
-
</div>
|
| 426 |
-
)}
|
| 427 |
-
|
| 428 |
-
{/* ── Invoice list ── */}
|
| 429 |
-
{invoices.length === 0 ? (
|
| 430 |
-
<div className="bg-white rounded-2xl p-16 text-center shadow-sm shadow-slate-200/50 border border-slate-200 flex flex-col items-center justify-center">
|
| 431 |
-
<div className="w-24 h-24 bg-slate-50 rounded-full flex items-center justify-center mb-6">
|
| 432 |
-
<FileText size={48} className="text-slate-300" />
|
| 433 |
-
</div>
|
| 434 |
-
<h3 className="text-2xl font-bold text-slate-700 mb-2">
|
| 435 |
-
{statusFilter === "all" ? "لا توجد فواتير" : "لا توجد فواتير بهذه الحالة"}
|
| 436 |
-
</h3>
|
| 437 |
-
<p className="text-slate-500 max-w-md">
|
| 438 |
-
{statusFilter === "all"
|
| 439 |
-
? "هذا العميل ليس لديه أي فواتير مسجلة بعد."
|
| 440 |
-
: "جرّب تصفية مختلفة للاطلاع على الفواتير الأخرى."}
|
| 441 |
-
</p>
|
| 442 |
-
{statusFilter !== "all" && (
|
| 443 |
-
<button
|
| 444 |
-
onClick={() => setStatusFilter("all")}
|
| 445 |
-
className="mt-4 px-5 py-2 bg-amber-50 border border-amber-200 text-amber-700 rounded-xl font-bold text-sm hover:bg-amber-100 transition-colors"
|
| 446 |
-
>
|
| 447 |
-
إظهار الكل
|
| 448 |
-
</button>
|
| 449 |
-
)}
|
| 450 |
-
</div>
|
| 451 |
-
) : (
|
| 452 |
-
invoices.map((invoice) => (
|
| 453 |
-
<InvoiceCard
|
| 454 |
-
key={invoice._id}
|
| 455 |
-
invoice={invoice}
|
| 456 |
-
customer={customer}
|
| 457 |
-
permissions={permissions}
|
| 458 |
-
companyName={companyName}
|
| 459 |
-
onShare={handleShareWhatsApp}
|
| 460 |
-
onPrint={handlePrint}
|
| 461 |
-
onEditItem={handleOpenEditModel}
|
| 462 |
-
onPay={(inv) => { setActiveInvoice(inv); setOpenPayModal(true); }}
|
| 463 |
-
/>
|
| 464 |
-
))
|
| 465 |
-
)}
|
| 466 |
-
|
| 467 |
-
{/* ── Pagination ── */}
|
| 468 |
-
<Pagination pagination={pagination} page={page} setPage={setPage} />
|
| 469 |
-
</div>
|
| 470 |
-
|
| 471 |
-
{/* ── Modals ── */}
|
| 472 |
-
{permissions.canEditInvoice && openEditModel && selectedItem && (
|
| 473 |
-
<EditInvoiceItemModal
|
| 474 |
-
customerId={customer._id}
|
| 475 |
-
onClose={closeEditModel}
|
| 476 |
-
invoiceId={selectedItem.invoiceId}
|
| 477 |
-
item={selectedItem}
|
| 478 |
-
/>
|
| 479 |
-
)}
|
| 480 |
-
{permissions.canRecordPayments && openPayModal && activeInvoice && (
|
| 481 |
-
<PayInvoiceModal
|
| 482 |
-
customerId={customer._id}
|
| 483 |
-
invoiceId={activeInvoice._id}
|
| 484 |
-
remainingAmount={activeInvoice.remainingAmount}
|
| 485 |
-
onClose={() => setOpenPayModal(false)}
|
| 486 |
-
/>
|
| 487 |
-
)}
|
| 488 |
-
</div>
|
| 489 |
-
);
|
| 490 |
-
};
|
| 491 |
-
|
| 492 |
-
export default CustomerDetails;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/CustomerDetails/EditInvoiceItemModal.jsx
DELETED
|
@@ -1,159 +0,0 @@
|
|
| 1 |
-
import { X, Save, Box, Hash, Banknote, Loader2 } from "lucide-react";
|
| 2 |
-
import { useState } from "react";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
| 5 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 6 |
-
|
| 7 |
-
const EditInvoiceItemModal = ({ customerId, invoiceId, item, onClose }) => {
|
| 8 |
-
const queryClient = useQueryClient();
|
| 9 |
-
const [form, setForm] = useState({
|
| 10 |
-
name: item.name,
|
| 11 |
-
quantity: item.quantity,
|
| 12 |
-
price: item.price
|
| 13 |
-
});
|
| 14 |
-
|
| 15 |
-
const updateItem = async () => {
|
| 16 |
-
const res = await axios.put(
|
| 17 |
-
`${apiUrl}/invoices/${invoiceId}/items/${item._id}`,
|
| 18 |
-
form
|
| 19 |
-
);
|
| 20 |
-
return res.data;
|
| 21 |
-
};
|
| 22 |
-
|
| 23 |
-
const { mutate, isPending } = useMutation({
|
| 24 |
-
mutationFn: updateItem,
|
| 25 |
-
onSuccess: () => {
|
| 26 |
-
queryClient.invalidateQueries({
|
| 27 |
-
queryKey: ["customer", customerId],
|
| 28 |
-
});
|
| 29 |
-
onClose();
|
| 30 |
-
},
|
| 31 |
-
});
|
| 32 |
-
|
| 33 |
-
return (
|
| 34 |
-
<div className="fixed inset-0 flex items-center justify-center z-50 bg-slate-900/40 backdrop-blur-sm p-4" >
|
| 35 |
-
<div className="bg-white w-full max-w-md rounded-2xl shadow-2xl border border-slate-200 overflow-hidden transform transition-all">
|
| 36 |
-
|
| 37 |
-
{/* Header */}
|
| 38 |
-
<div className="bg-slate-50 border-b border-slate-100 p-4 md:p-6 flex items-center justify-between relative">
|
| 39 |
-
<div className="absolute top-0 right-0 w-1.5 h-full bg-blue-500"></div>
|
| 40 |
-
<div className="flex items-center gap-3">
|
| 41 |
-
<div className="p-2 border border-blue-100 bg-white text-blue-600 rounded-lg shadow-sm">
|
| 42 |
-
<Box size={20} />
|
| 43 |
-
</div>
|
| 44 |
-
<h2 className="text-xl font-bold text-slate-800">
|
| 45 |
-
تعديل بيانات الصنف
|
| 46 |
-
</h2>
|
| 47 |
-
</div>
|
| 48 |
-
|
| 49 |
-
<button
|
| 50 |
-
onClick={onClose}
|
| 51 |
-
className="p-2 text-slate-400 hover:text-rose-500 hover:bg-rose-50 rounded-full transition-colors cursor-pointer"
|
| 52 |
-
title="إغلاق"
|
| 53 |
-
>
|
| 54 |
-
<X size={24} />
|
| 55 |
-
</button>
|
| 56 |
-
</div>
|
| 57 |
-
|
| 58 |
-
{/* Body */}
|
| 59 |
-
<div className="p-4 md:p-6 space-y-5">
|
| 60 |
-
|
| 61 |
-
{/* Name */}
|
| 62 |
-
<div>
|
| 63 |
-
<label className="block text-sm font-bold text-slate-700 mb-1.5">اسم الصنف</label>
|
| 64 |
-
<div className="relative">
|
| 65 |
-
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400">
|
| 66 |
-
<Box size={18} />
|
| 67 |
-
</span>
|
| 68 |
-
<input
|
| 69 |
-
type="text"
|
| 70 |
-
value={form.name}
|
| 71 |
-
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
| 72 |
-
className="w-full bg-white border border-slate-200 rounded-xl py-3 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all text-slate-800 font-medium placeholder:text-slate-400"
|
| 73 |
-
placeholder="أدخل اسم البند (مثال: سلك 2 ملي)"
|
| 74 |
-
/>
|
| 75 |
-
</div>
|
| 76 |
-
</div>
|
| 77 |
-
|
| 78 |
-
{/* Quantity & Price Grid */}
|
| 79 |
-
<div className="grid grid-cols-2 gap-4">
|
| 80 |
-
{/* Quantity */}
|
| 81 |
-
<div>
|
| 82 |
-
<label className="block text-sm font-bold text-slate-700 mb-1.5">الكمية</label>
|
| 83 |
-
<div className="relative">
|
| 84 |
-
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400">
|
| 85 |
-
<Hash size={18} />
|
| 86 |
-
</span>
|
| 87 |
-
<input
|
| 88 |
-
type="number"
|
| 89 |
-
min="1"
|
| 90 |
-
value={form.quantity}
|
| 91 |
-
onChange={(e) => setForm({ ...form, quantity: Number(e.target.value) })}
|
| 92 |
-
className="w-full bg-white border border-slate-200 rounded-xl py-3 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all text-slate-800 font-medium placeholder:text-slate-400"
|
| 93 |
-
placeholder="الكمية"
|
| 94 |
-
/>
|
| 95 |
-
</div>
|
| 96 |
-
</div>
|
| 97 |
-
|
| 98 |
-
{/* Price */}
|
| 99 |
-
<div>
|
| 100 |
-
<label className="block text-sm font-bold text-slate-700 mb-1.5">السعر (ج.م)</label>
|
| 101 |
-
<div className="relative">
|
| 102 |
-
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400">
|
| 103 |
-
<Banknote size={18} />
|
| 104 |
-
</span>
|
| 105 |
-
<input
|
| 106 |
-
type="number"
|
| 107 |
-
min="0"
|
| 108 |
-
value={form.price}
|
| 109 |
-
onChange={(e) => setForm({ ...form, price: Number(e.target.value) })}
|
| 110 |
-
className="w-full bg-white border border-slate-200 rounded-xl py-3 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all text-slate-800 font-bold text-left focus:text-right text-blue-700"
|
| 111 |
-
placeholder="0.00"
|
| 112 |
-
/>
|
| 113 |
-
</div>
|
| 114 |
-
</div>
|
| 115 |
-
</div>
|
| 116 |
-
|
| 117 |
-
{/* Subtotal preview */}
|
| 118 |
-
<div className="flex items-center justify-between bg-blue-50 border border-blue-100 p-3 rounded-lg mx-1 mt-2">
|
| 119 |
-
<span className="text-sm font-bold text-blue-800">إجمالي الصنف:</span>
|
| 120 |
-
<span className="text-lg font-black text-blue-700">
|
| 121 |
-
{(Number(form.price || 0) * Number(form.quantity || 0)).toFixed(2)} <span className="text-sm font-normal opacity-80">ج.م</span>
|
| 122 |
-
</span>
|
| 123 |
-
</div>
|
| 124 |
-
</div>
|
| 125 |
-
|
| 126 |
-
{/* Footer */}
|
| 127 |
-
<div className="p-4 md:p-6 bg-slate-50 border-t border-slate-100 flex gap-3">
|
| 128 |
-
<button
|
| 129 |
-
onClick={() => mutate()}
|
| 130 |
-
disabled={isPending || !form.name.trim() || form.quantity <= 0 || form.price < 0}
|
| 131 |
-
className="flex-1 flex items-center justify-center gap-2 bg-blue-600 text-white px-2 py-3 rounded-xl hover:bg-blue-700 font-bold shadow-sm shadow-blue-600/20 disabled:opacity-50 disabled:cursor-not-allowed transition-all hover:-translate-y-0.5"
|
| 132 |
-
>
|
| 133 |
-
{isPending ? (
|
| 134 |
-
<>
|
| 135 |
-
<Loader2 className="animate-spin" size={18} />
|
| 136 |
-
<span>جاري الحفظ...</span>
|
| 137 |
-
</>
|
| 138 |
-
) : (
|
| 139 |
-
<>
|
| 140 |
-
<Save size={18} />
|
| 141 |
-
<span>حفظ التعديلات</span>
|
| 142 |
-
</>
|
| 143 |
-
)}
|
| 144 |
-
</button>
|
| 145 |
-
<button
|
| 146 |
-
onClick={onClose}
|
| 147 |
-
disabled={isPending}
|
| 148 |
-
className="px-6 py-3 bg-white border border-slate-200 text-slate-600 rounded-xl hover:bg-rose-50 hover:text-rose-600 hover:border-rose-200 font-bold transition-all disabled:opacity-50"
|
| 149 |
-
>
|
| 150 |
-
إلغاء
|
| 151 |
-
</button>
|
| 152 |
-
</div>
|
| 153 |
-
|
| 154 |
-
</div>
|
| 155 |
-
</div>
|
| 156 |
-
);
|
| 157 |
-
};
|
| 158 |
-
|
| 159 |
-
export default EditInvoiceItemModal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/CustomerDetails/PayInvoiceModal.jsx
DELETED
|
@@ -1,126 +0,0 @@
|
|
| 1 |
-
import { X, Banknote, Loader2, CheckCircle, Wallet } from "lucide-react";
|
| 2 |
-
import { useState } from "react";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
| 5 |
-
import { toast } from "react-toastify";
|
| 6 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 7 |
-
|
| 8 |
-
const PayInvoiceModal = ({ customerId, remainingAmount, onClose }) => {
|
| 9 |
-
const queryClient = useQueryClient();
|
| 10 |
-
const [amount, setAmount] = useState('');
|
| 11 |
-
|
| 12 |
-
const payInvoice = async () => {
|
| 13 |
-
const numAmount = Number(amount);
|
| 14 |
-
if (numAmount <= 0) throw new Error("يرجى إدخال مبلغ دفع صحيح أكبر من صفر");
|
| 15 |
-
if (numAmount > remainingAmount) throw new Error(`عذراً، المبلغ لا يمكن أن يتخطى إجمالي المتبقي (${remainingAmount.toFixed(2)} ج.م)`);
|
| 16 |
-
|
| 17 |
-
const res = await axios.post(`${apiUrl}/invoices/${customerId}/pay`, {
|
| 18 |
-
amount: numAmount
|
| 19 |
-
});
|
| 20 |
-
return res.data;
|
| 21 |
-
};
|
| 22 |
-
|
| 23 |
-
const { mutate, isPending } = useMutation({
|
| 24 |
-
mutationFn: payInvoice,
|
| 25 |
-
onSuccess: () => {
|
| 26 |
-
queryClient.invalidateQueries({ queryKey: ["customer", customerId] });
|
| 27 |
-
queryClient.invalidateQueries({ queryKey: ["customers"] });
|
| 28 |
-
toast.success("تم تسجيل وتحصيل الدفعة بنجاح 💵");
|
| 29 |
-
onClose();
|
| 30 |
-
},
|
| 31 |
-
onError: (err) => {
|
| 32 |
-
toast.error(err.message || "حدث خطأ أثناء إجراء الدفع");
|
| 33 |
-
}
|
| 34 |
-
});
|
| 35 |
-
|
| 36 |
-
return (
|
| 37 |
-
<div className="fixed inset-0 bg-slate-900/40 backdrop-blur-sm flex items-center justify-center z-50 p-4" dir="rtl">
|
| 38 |
-
<div className="bg-white w-full max-w-sm rounded-2xl shadow-xl border border-slate-200 overflow-hidden transform transition-all animate-in fade-in zoom-in-95">
|
| 39 |
-
|
| 40 |
-
{/* Header */}
|
| 41 |
-
<div className="bg-emerald-50 border-b border-emerald-100 p-5 flex items-center justify-between relative">
|
| 42 |
-
<div className="absolute top-0 right-0 w-1.5 h-full bg-emerald-500"></div>
|
| 43 |
-
<div className="flex items-center gap-3">
|
| 44 |
-
<div className="p-2 border border-emerald-200 bg-white text-emerald-600 rounded-lg shadow-sm">
|
| 45 |
-
<Wallet size={24} />
|
| 46 |
-
</div>
|
| 47 |
-
<h2 className="text-xl font-bold text-emerald-900">
|
| 48 |
-
تحصيل دفعة نقدية
|
| 49 |
-
</h2>
|
| 50 |
-
</div>
|
| 51 |
-
|
| 52 |
-
<button
|
| 53 |
-
onClick={onClose}
|
| 54 |
-
className="p-2 text-slate-400 hover:text-rose-500 hover:bg-rose-50 rounded-full transition-colors cursor-pointer"
|
| 55 |
-
title="إغلاق"
|
| 56 |
-
>
|
| 57 |
-
<X size={24} />
|
| 58 |
-
</button>
|
| 59 |
-
</div>
|
| 60 |
-
|
| 61 |
-
{/* Body */}
|
| 62 |
-
<div className="p-5 space-y-5 bg-white">
|
| 63 |
-
|
| 64 |
-
<div className="flex items-center justify-between bg-rose-50 p-3 rounded-lg border border-rose-100">
|
| 65 |
-
<span className="text-rose-700 font-bold text-sm">المديونية المتبقية:</span>
|
| 66 |
-
<span className="text-rose-600 font-black text-lg">{remainingAmount.toFixed(2)} <span className="font-normal text-sm">ج.م</span></span>
|
| 67 |
-
</div>
|
| 68 |
-
|
| 69 |
-
<div>
|
| 70 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">المبلغ المُراد تحصيله (كاش)</label>
|
| 71 |
-
<div className="relative">
|
| 72 |
-
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-emerald-500">
|
| 73 |
-
<Banknote size={20} />
|
| 74 |
-
</span>
|
| 75 |
-
<input
|
| 76 |
-
type="number"
|
| 77 |
-
min="1"
|
| 78 |
-
max={remainingAmount}
|
| 79 |
-
value={amount}
|
| 80 |
-
onChange={(e) => setAmount(e.target.value)}
|
| 81 |
-
className="w-full bg-slate-50 border border-emerald-200 rounded-xl py-3.5 pr-10 pl-4 focus:outline-none focus:ring-2 focus:ring-emerald-500 focus:bg-white transition-all text-emerald-800 font-black text-lg text-left focus:text-right placeholder:text-slate-400 placeholder:font-medium"
|
| 82 |
-
placeholder="أدخل المبلغ..."
|
| 83 |
-
/>
|
| 84 |
-
</div>
|
| 85 |
-
</div>
|
| 86 |
-
|
| 87 |
-
{/* Quick Fill Options */}
|
| 88 |
-
<div className="flex gap-2">
|
| 89 |
-
<button onClick={() => setAmount(remainingAmount)} className="flex-1 py-2 bg-emerald-50 text-emerald-700 font-bold text-sm rounded-lg hover:bg-emerald-100 border border-emerald-200 transition-colors">
|
| 90 |
-
تصفية الحساب كامل
|
| 91 |
-
</button>
|
| 92 |
-
{remainingAmount >= 1000 && (
|
| 93 |
-
<button onClick={() => setAmount(1000)} className="flex-1 py-2 bg-slate-50 text-slate-700 font-bold text-sm rounded-lg hover:bg-slate-100 border border-slate-200 transition-colors">
|
| 94 |
-
دفع 1000 ج.م
|
| 95 |
-
</button>
|
| 96 |
-
)}
|
| 97 |
-
</div>
|
| 98 |
-
</div>
|
| 99 |
-
|
| 100 |
-
{/* Footer */}
|
| 101 |
-
<div className="p-5 bg-slate-50 border-t border-slate-100 flex gap-3">
|
| 102 |
-
<button
|
| 103 |
-
onClick={() => mutate()}
|
| 104 |
-
disabled={isPending || !amount || Number(amount) <= 0 || Number(amount) > remainingAmount}
|
| 105 |
-
className="flex-1 flex items-center justify-center gap-2 bg-emerald-600 text-white py-3.5 rounded-xl hover:bg-emerald-700 font-bold shadow-sm shadow-emerald-600/20 disabled:opacity-50 disabled:cursor-not-allowed transition-all hover:-translate-y-0.5"
|
| 106 |
-
>
|
| 107 |
-
{isPending ? (
|
| 108 |
-
<>
|
| 109 |
-
<Loader2 className="animate-spin" size={20} />
|
| 110 |
-
<span>جاري التحصيل...</span>
|
| 111 |
-
</>
|
| 112 |
-
) : (
|
| 113 |
-
<>
|
| 114 |
-
<CheckCircle size={20} />
|
| 115 |
-
<span>تأكيد تسجيل الدفعة</span>
|
| 116 |
-
</>
|
| 117 |
-
)}
|
| 118 |
-
</button>
|
| 119 |
-
</div>
|
| 120 |
-
|
| 121 |
-
</div>
|
| 122 |
-
</div>
|
| 123 |
-
);
|
| 124 |
-
};
|
| 125 |
-
|
| 126 |
-
export default PayInvoiceModal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/CustomerDetails/PrintInvoice.jsx
DELETED
|
@@ -1,145 +0,0 @@
|
|
| 1 |
-
import { useEffect } from "react";
|
| 2 |
-
import { useParams } from "react-router-dom";
|
| 3 |
-
import { useQuery } from "@tanstack/react-query";
|
| 4 |
-
import axios from "axios";
|
| 5 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 6 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 7 |
-
|
| 8 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 9 |
-
|
| 10 |
-
const PrintInvoice = () => {
|
| 11 |
-
// Route should be: /print/invoice/:customerId/:invoiceId
|
| 12 |
-
const { customerId, invoiceId } = useParams();
|
| 13 |
-
const { user } = useAuth();
|
| 14 |
-
const companyName = user?.tenant?.name || "نظام إدارة المبيعات";
|
| 15 |
-
const companyAddress = user?.tenant?.address || "";
|
| 16 |
-
|
| 17 |
-
const { data, isLoading } = useQuery({
|
| 18 |
-
queryKey: ["print-invoice", customerId, invoiceId],
|
| 19 |
-
queryFn: () =>
|
| 20 |
-
axios.get(`${apiUrl}/customers/${customerId}?includeInvoices=true`),
|
| 21 |
-
refetchOnWindowFocus: false,
|
| 22 |
-
});
|
| 23 |
-
|
| 24 |
-
const customer = data?.data;
|
| 25 |
-
// Find the specific invoice from the invoices array
|
| 26 |
-
const invoice = customer?.invoices?.find((inv) => inv._id === invoiceId);
|
| 27 |
-
|
| 28 |
-
useEffect(() => {
|
| 29 |
-
if (invoice) setTimeout(() => window.print(), 500);
|
| 30 |
-
}, [invoice]);
|
| 31 |
-
|
| 32 |
-
if (isLoading) return <LodderSpinner />;
|
| 33 |
-
|
| 34 |
-
if (!customer)
|
| 35 |
-
return <div style={{ padding: 20, textAlign: "center" }}>لم يتم العثور على العميل</div>;
|
| 36 |
-
|
| 37 |
-
if (!invoice)
|
| 38 |
-
return <div style={{ padding: 20, textAlign: "center" }}>لا توجد فاتورة بهذا الرقم</div>;
|
| 39 |
-
|
| 40 |
-
return (
|
| 41 |
-
<div
|
| 42 |
-
className="receipt-root"
|
| 43 |
-
dir="rtl"
|
| 44 |
-
style={{
|
| 45 |
-
width: "80mm",
|
| 46 |
-
margin: "0 auto",
|
| 47 |
-
fontFamily: "'Courier New', monospace",
|
| 48 |
-
fontSize: "12px",
|
| 49 |
-
color: "#000",
|
| 50 |
-
background: "#fff",
|
| 51 |
-
padding: "8px",
|
| 52 |
-
lineHeight: "1.5",
|
| 53 |
-
}}
|
| 54 |
-
>
|
| 55 |
-
<style>{`
|
| 56 |
-
@media print {
|
| 57 |
-
@page {
|
| 58 |
-
size: 80mm auto;
|
| 59 |
-
margin: 4mm;
|
| 60 |
-
}
|
| 61 |
-
body {
|
| 62 |
-
margin: 0;
|
| 63 |
-
background: white;
|
| 64 |
-
-webkit-print-color-adjust: exact;
|
| 65 |
-
}
|
| 66 |
-
}
|
| 67 |
-
`}</style>
|
| 68 |
-
|
| 69 |
-
{/* Header */}
|
| 70 |
-
<div style={{ textAlign: "center", borderBottom: "1px dashed #000", paddingBottom: 8, marginBottom: 8 }}>
|
| 71 |
-
<div style={{ fontSize: "15px", fontWeight: "bold", letterSpacing: "1px" }}>
|
| 72 |
-
{companyName}
|
| 73 |
-
</div>
|
| 74 |
-
{companyAddress && (
|
| 75 |
-
<div style={{ fontSize: "10px", marginTop: 2 }}>{companyAddress}</div>
|
| 76 |
-
)}
|
| 77 |
-
</div>
|
| 78 |
-
|
| 79 |
-
{/* Meta */}
|
| 80 |
-
<Row label="فاتورة #" value={invoice.invoiceNumber || invoice._id.slice(-6).toUpperCase()} />
|
| 81 |
-
<Row label="العميل" value={customer.name} />
|
| 82 |
-
<Row label="التاريخ" value={new Date().toLocaleString("ar-EG")} />
|
| 83 |
-
|
| 84 |
-
{/* Items header */}
|
| 85 |
-
<div style={{ borderTop: "1px solid #000", borderBottom: "1px solid #000", marginTop: 8, padding: "4px 0", display: "flex", justifyContent: "space-between", fontSize: "10px" }}>
|
| 86 |
-
<span style={{ flex: 2 }}>الصنف</span>
|
| 87 |
-
<span style={{ flex: 1, textAlign: "center" }}>كمية</span>
|
| 88 |
-
<span style={{ flex: 1, textAlign: "center" }}>سعر</span>
|
| 89 |
-
<span style={{ flex: 1, textAlign: "left" }}>إجمالي</span>
|
| 90 |
-
</div>
|
| 91 |
-
|
| 92 |
-
{/* Items */}
|
| 93 |
-
{invoice.items.map((item, i) => (
|
| 94 |
-
<div
|
| 95 |
-
key={i}
|
| 96 |
-
style={{ display: "flex", justifyContent: "space-between", padding: "3px 0", borderBottom: "1px dashed #ccc", fontSize: "11px" }}
|
| 97 |
-
>
|
| 98 |
-
<span style={{ flex: 2, wordBreak: "break-word" }}>{item.name}</span>
|
| 99 |
-
<span style={{ flex: 1, textAlign: "center" }}>{item.quantity}</span>
|
| 100 |
-
<span style={{ flex: 1, textAlign: "center" }}>{item.price}</span>
|
| 101 |
-
<span style={{ flex: 1, textAlign: "left", fontWeight: "bold" }}>{item.total.toFixed(2)}</span>
|
| 102 |
-
</div>
|
| 103 |
-
))}
|
| 104 |
-
|
| 105 |
-
{/* Totals */}
|
| 106 |
-
<div style={{ marginTop: 8, borderTop: "1px solid #000", paddingTop: 6 }}>
|
| 107 |
-
<Row label="إجمالي الفاتورة" value={`${invoice.totalAmount.toFixed(2)} ج.م`} />
|
| 108 |
-
<Row label="المدفوعات المستلمة" value={`${invoice.paidAmount.toFixed(2)} ج.م`} />
|
| 109 |
-
<div style={{ display: "flex", justifyContent: "space-between", fontWeight: "bold", fontSize: "13px", borderTop: "2px solid #000", marginTop: 6, paddingTop: 6 }}>
|
| 110 |
-
<span>الرصيد المتبقي</span>
|
| 111 |
-
<span>{invoice.remainingAmount.toFixed(2)} ج.م</span>
|
| 112 |
-
</div>
|
| 113 |
-
</div>
|
| 114 |
-
|
| 115 |
-
{/* Payments log */}
|
| 116 |
-
{invoice.payments?.length > 0 && (
|
| 117 |
-
<div style={{ marginTop: 8, borderTop: "1px dashed #000", paddingTop: 6 }}>
|
| 118 |
-
<div style={{ fontSize: "10px", fontWeight: "bold", marginBottom: 4 }}>سجل المدفوعات</div>
|
| 119 |
-
{invoice.payments.map((p, i) => (
|
| 120 |
-
<div key={i} style={{ display: "flex", justifyContent: "space-between", fontSize: "10px", marginBottom: 2 }}>
|
| 121 |
-
<span>{new Date(p.date).toLocaleDateString("ar-EG")}</span>
|
| 122 |
-
<span>{p.method === "cash" ? "نقدي" : p.method}</span>
|
| 123 |
-
<span style={{ fontWeight: "bold" }}>{p.amount.toFixed(2)} ج.م</span>
|
| 124 |
-
</div>
|
| 125 |
-
))}
|
| 126 |
-
</div>
|
| 127 |
-
)}
|
| 128 |
-
|
| 129 |
-
{/* Footer */}
|
| 130 |
-
<div style={{ textAlign: "center", borderTop: "1px dashed #000", marginTop: 12, paddingTop: 8, fontSize: "10px" }}>
|
| 131 |
-
<div style={{ fontWeight: "bold" }}>شكراً لاختياركم {companyName}</div>
|
| 132 |
-
<div style={{ marginTop: 4, color: "#555" }}>هذه الفاتورة وثيقة رسمية</div>
|
| 133 |
-
</div>
|
| 134 |
-
</div>
|
| 135 |
-
);
|
| 136 |
-
};
|
| 137 |
-
|
| 138 |
-
const Row = ({ label, value }) => (
|
| 139 |
-
<div style={{ display: "flex", justifyContent: "space-between", marginBottom: 3, fontSize: "11px" }}>
|
| 140 |
-
<span style={{ color: "#555" }}>{label}</span>
|
| 141 |
-
<span style={{ fontWeight: "bold", maxWidth: "60%", textAlign: "left", wordBreak: "break-word" }}>{value}</span>
|
| 142 |
-
</div>
|
| 143 |
-
);
|
| 144 |
-
|
| 145 |
-
export default PrintInvoice;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Customers/Customer.jsx
DELETED
|
@@ -1,29 +0,0 @@
|
|
| 1 |
-
import Taps from "../Taps"
|
| 2 |
-
import CustomerBoxView from "./CustomerBoxView"
|
| 3 |
-
import { useQuery } from '@tanstack/react-query';
|
| 4 |
-
import axios from 'axios';
|
| 5 |
-
import LodderSpinner from '../../common/LoaderSpinner';
|
| 6 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
const Customer = () => {
|
| 10 |
-
|
| 11 |
-
function getAllCustomers() {
|
| 12 |
-
return axios.get(`${apiUrl}/customers`)
|
| 13 |
-
}
|
| 14 |
-
const { data, isLoading } = useQuery({
|
| 15 |
-
queryKey: ['customers'],
|
| 16 |
-
queryFn: getAllCustomers,
|
| 17 |
-
refetchOnWindowFocus: false
|
| 18 |
-
})
|
| 19 |
-
if (isLoading) return <LodderSpinner />
|
| 20 |
-
const customerData = data?.data;
|
| 21 |
-
|
| 22 |
-
return (
|
| 23 |
-
<div className="bg-white shadow rounded-2xl py-4">
|
| 24 |
-
<Taps customerData={customerData} />
|
| 25 |
-
</div>
|
| 26 |
-
)
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
export default Customer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Customers/CustomerBoxView.jsx
DELETED
|
@@ -1,148 +0,0 @@
|
|
| 1 |
-
import { Eye, Search, PlusCircle, Inbox } from "lucide-react";
|
| 2 |
-
import { useState } from "react";
|
| 3 |
-
import { Link } from "react-router-dom";
|
| 4 |
-
import InvoiceItemsModal from "../InvoiceItemsModal/InvoiceItemsModal";
|
| 5 |
-
import DeleteCustomerButton from "./DeleteCustomerButton";
|
| 6 |
-
|
| 7 |
-
const CustomerBoxView = ({ customerData }) => {
|
| 8 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 9 |
-
const [showModal, setShowModal] = useState(false);
|
| 10 |
-
const [activeCustomerId, setActiveCustomerId] = useState(null);
|
| 11 |
-
|
| 12 |
-
const filteredCustomers = customerData?.filter((c) =>
|
| 13 |
-
c.name.toLowerCase().includes(searchTerm.toLowerCase())
|
| 14 |
-
);
|
| 15 |
-
|
| 16 |
-
const openModal = (customerId) => {
|
| 17 |
-
setActiveCustomerId(customerId);
|
| 18 |
-
setShowModal(true);
|
| 19 |
-
};
|
| 20 |
-
|
| 21 |
-
const closeModal = () => {
|
| 22 |
-
setShowModal(false);
|
| 23 |
-
setActiveCustomerId(null);
|
| 24 |
-
};
|
| 25 |
-
|
| 26 |
-
return (
|
| 27 |
-
<div className="m-4">
|
| 28 |
-
{/* Search Input Section */}
|
| 29 |
-
<div className="relative flex-1 max-w-full mb-6">
|
| 30 |
-
<div className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400 bg-slate-100 p-1.5 rounded-md">
|
| 31 |
-
<Search size={20} />
|
| 32 |
-
</div>
|
| 33 |
-
<input
|
| 34 |
-
type="text"
|
| 35 |
-
placeholder="ابحث عن اسم العميل..."
|
| 36 |
-
value={searchTerm}
|
| 37 |
-
onChange={(e) => setSearchTerm(e.target.value)}
|
| 38 |
-
className="w-full pr-14 pl-4 py-3.5 text-lg bg-white border border-slate-200 rounded-xl shadow-sm focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-transparent transition-all placeholder:text-slate-400"
|
| 39 |
-
dir="rtl"
|
| 40 |
-
/>
|
| 41 |
-
</div>
|
| 42 |
-
|
| 43 |
-
{/* Table Container */}
|
| 44 |
-
<div className="max-h-[500px] overflow-y-auto custom-scroll rounded-2xl bg-white shadow-lg shadow-slate-200/50 border border-slate-100 scroll-smooth">
|
| 45 |
-
<table className="w-full border-collapse text-right" dir="rtl">
|
| 46 |
-
{/* Table Head */}
|
| 47 |
-
<thead className="sticky top-0 bg-slate-50 border-b-2 border-slate-200 z-10">
|
| 48 |
-
<tr className="text-slate-600">
|
| 49 |
-
<th className="p-5 font-bold text-base whitespace-nowrap">اسم العميل</th>
|
| 50 |
-
<th className="p-5 font-bold text-base text-center whitespace-nowrap">تاريخ الإنشاء</th>
|
| 51 |
-
<th className="p-5 font-bold text-base text-center whitespace-nowrap">إجمالي الديون</th>
|
| 52 |
-
<th className="p-5 font-bold text-base text-center whitespace-nowrap">عدد الفواتير</th>
|
| 53 |
-
<th className="p-5 font-bold text-base text-center whitespace-nowrap">الإجراءات</th>
|
| 54 |
-
</tr>
|
| 55 |
-
</thead>
|
| 56 |
-
|
| 57 |
-
{/* Table Body */}
|
| 58 |
-
<tbody className="divide-y divide-slate-100">
|
| 59 |
-
{filteredCustomers?.length > 0 &&
|
| 60 |
-
filteredCustomers.map((customer) => (
|
| 61 |
-
<tr
|
| 62 |
-
key={customer._id}
|
| 63 |
-
className="hover:bg-amber-50/50 transition-colors group"
|
| 64 |
-
>
|
| 65 |
-
<td className="p-5 text-lg font-bold text-slate-800">
|
| 66 |
-
<div className="flex items-center gap-3">
|
| 67 |
-
<div className="w-10 h-10 rounded-full bg-slate-100 flex items-center justify-center text-slate-500 font-bold group-hover:bg-amber-100 group-hover:text-amber-600 transition-colors flex-shrink-0">
|
| 68 |
-
{customer.name.charAt(0)}
|
| 69 |
-
</div>
|
| 70 |
-
<span className="truncate max-w-[200px] block" title={customer.name}>
|
| 71 |
-
{customer.name}
|
| 72 |
-
</span>
|
| 73 |
-
</div>
|
| 74 |
-
</td>
|
| 75 |
-
|
| 76 |
-
<td className="p-5 text-slate-500 font-medium text-center">
|
| 77 |
-
<span className="bg-slate-100 px-3 py-1.5 rounded-full text-sm">
|
| 78 |
-
{new Date(customer.createdAt).toLocaleDateString('ar-EG')}
|
| 79 |
-
</span>
|
| 80 |
-
</td>
|
| 81 |
-
|
| 82 |
-
<td className="p-5 font-bold text-rose-600 text-center text-lg">
|
| 83 |
-
<span className="bg-rose-50 px-3 py-1.5 rounded-lg border border-rose-100 inline-block">
|
| 84 |
-
{Number(customer.totalDebt || 0).toFixed(2)} <span className="text-sm font-normal text-rose-400">ج.م</span>
|
| 85 |
-
</span>
|
| 86 |
-
</td>
|
| 87 |
-
|
| 88 |
-
<td className="p-5 text-center">
|
| 89 |
-
<span className="inline-flex items-center justify-center min-w-[32px] h-8 px-2 rounded-full bg-blue-50 text-blue-700 font-bold text-lg border border-blue-100">
|
| 90 |
-
{customer.invoices?.length || 0}
|
| 91 |
-
</span>
|
| 92 |
-
</td>
|
| 93 |
-
<td className="p-5">
|
| 94 |
-
<div className="flex justify-center gap-2 lg:gap-3 flex-wrap md:flex-nowrap">
|
| 95 |
-
<Link
|
| 96 |
-
to={`/customers/${customer._id}`}
|
| 97 |
-
className="bg-slate-800 text-white px-3 lg:px-4 py-2 rounded-lg hover:bg-slate-900 flex items-center gap-1 lg:gap-2 shadow-sm transition-all hover:shadow-md hover:-translate-y-0.5 whitespace-nowrap"
|
| 98 |
-
>
|
| 99 |
-
<Eye size={18} className="text-amber-400" />
|
| 100 |
-
<span>التفاصيل</span>
|
| 101 |
-
</Link>
|
| 102 |
-
|
| 103 |
-
<button
|
| 104 |
-
onClick={() => openModal(customer._id)}
|
| 105 |
-
className="bg-amber-500 text-white/90 font-bold px-3 lg:px-4 py-2 rounded-lg hover:bg-amber-600 flex items-center justify-center gap-1 lg:gap-2 shadow-sm transition-all hover:shadow-md hover:-translate-y-0.5 whitespace-nowrap"
|
| 106 |
-
>
|
| 107 |
-
<PlusCircle size={18} />
|
| 108 |
-
<span>إضافة بنود</span>
|
| 109 |
-
</button>
|
| 110 |
-
<DeleteCustomerButton customerId={customer._id} customerName={customer.name} />
|
| 111 |
-
</div>
|
| 112 |
-
</td>
|
| 113 |
-
</tr>
|
| 114 |
-
))}
|
| 115 |
-
</tbody>
|
| 116 |
-
</table>
|
| 117 |
-
|
| 118 |
-
{/* Empty State when no characters match search */}
|
| 119 |
-
{filteredCustomers?.length === 0 && customerData?.length > 0 && (
|
| 120 |
-
<div className="flex flex-col items-center justify-center py-16 text-slate-400">
|
| 121 |
-
<Search size={48} className="mb-4 text-slate-300 opacity-50" />
|
| 122 |
-
<p className="text-xl font-medium">عذراً، لم يتم العثور على عميل بهذا الاسم</p>
|
| 123 |
-
</div>
|
| 124 |
-
)}
|
| 125 |
-
|
| 126 |
-
{showModal && activeCustomerId && (
|
| 127 |
-
<InvoiceItemsModal
|
| 128 |
-
customerId={activeCustomerId}
|
| 129 |
-
onClose={closeModal}
|
| 130 |
-
/>
|
| 131 |
-
)}
|
| 132 |
-
</div>
|
| 133 |
-
|
| 134 |
-
{/* Empty State overall */}
|
| 135 |
-
{(!customerData || customerData.length === 0) && (
|
| 136 |
-
<div className="flex flex-col items-center justify-center p-16 bg-white rounded-2xl shadow-sm border border-slate-100 mt-4">
|
| 137 |
-
<div className="w-24 h-24 bg-slate-50 rounded-full flex items-center justify-center mb-4">
|
| 138 |
-
<Inbox size={48} className="text-slate-300" />
|
| 139 |
-
</div>
|
| 140 |
-
<p className="text-2xl font-bold text-slate-500 mb-2">لا يوجد عملاء مسجلين بعد</p>
|
| 141 |
-
<p className="text-slate-400">قم بإضافة عميل جديد للبدء في إدارة الفواتير والديون</p>
|
| 142 |
-
</div>
|
| 143 |
-
)}
|
| 144 |
-
</div>
|
| 145 |
-
);
|
| 146 |
-
};
|
| 147 |
-
|
| 148 |
-
export default CustomerBoxView;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Customers/CustomersWorkspace.jsx
DELETED
|
@@ -1,271 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
| 4 |
-
import { Link } from "react-router-dom";
|
| 5 |
-
import { Eye, Loader2, PlusCircle, Search, Trash2, UserRound } from "lucide-react";
|
| 6 |
-
import { useFormik } from "formik";
|
| 7 |
-
import * as Yup from "yup";
|
| 8 |
-
import { toast } from "react-toastify";
|
| 9 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 10 |
-
import InvoiceItemsModal from "../InvoiceItemsModal/InvoiceItemsModal";
|
| 11 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 12 |
-
import { getPermissions } from "../../utils/permissions";
|
| 13 |
-
import { formatCurrency, formatDate, formatNumber } from "../../utils/formatters";
|
| 14 |
-
import Pagination from "../../common/Pagination";
|
| 15 |
-
|
| 16 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 17 |
-
const EMPTY_LIST = [];
|
| 18 |
-
|
| 19 |
-
const CustomersWorkspace = () => {
|
| 20 |
-
const { user } = useAuth();
|
| 21 |
-
const permissions = getPermissions(user);
|
| 22 |
-
const currency = user?.tenant?.settings?.currency || "EGP";
|
| 23 |
-
const queryClient = useQueryClient();
|
| 24 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 25 |
-
const [page, setPage] = useState(1);
|
| 26 |
-
const [activeCustomerId, setActiveCustomerId] = useState(null);
|
| 27 |
-
const [pendingDeleteId, setPendingDeleteId] = useState(null);
|
| 28 |
-
|
| 29 |
-
const { data: response, isLoading } = useQuery({
|
| 30 |
-
queryKey: ["customers", page, searchTerm],
|
| 31 |
-
queryFn: () => axios.get(`${apiUrl}/customers`, {
|
| 32 |
-
params: {
|
| 33 |
-
page,
|
| 34 |
-
limit: 5,
|
| 35 |
-
search: searchTerm
|
| 36 |
-
}
|
| 37 |
-
}),
|
| 38 |
-
refetchOnWindowFocus: false,
|
| 39 |
-
});
|
| 40 |
-
|
| 41 |
-
const createCustomerMutation = useMutation({
|
| 42 |
-
mutationFn: (values) => axios.post(`${apiUrl}/customers/create`, values),
|
| 43 |
-
onSuccess: () => {
|
| 44 |
-
formik.resetForm();
|
| 45 |
-
queryClient.invalidateQueries({ queryKey: ["customers"] });
|
| 46 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 47 |
-
toast.success("تم إنشاء حساب العميل");
|
| 48 |
-
},
|
| 49 |
-
onError: (error) => {
|
| 50 |
-
toast.error(error.response?.data?.message || "تعذر إنشاء العميل");
|
| 51 |
-
},
|
| 52 |
-
});
|
| 53 |
-
|
| 54 |
-
const deleteCustomerMutation = useMutation({
|
| 55 |
-
mutationFn: (customerId) => axios.delete(`${apiUrl}/customers/${customerId}`),
|
| 56 |
-
onSuccess: () => {
|
| 57 |
-
queryClient.invalidateQueries({ queryKey: ["customers"] });
|
| 58 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 59 |
-
toast.success("تم حذف العميل");
|
| 60 |
-
setPendingDeleteId(null);
|
| 61 |
-
},
|
| 62 |
-
onError: (error) => {
|
| 63 |
-
toast.error(error.response?.data?.message || "تعذر حذف العميل");
|
| 64 |
-
setPendingDeleteId(null);
|
| 65 |
-
},
|
| 66 |
-
});
|
| 67 |
-
|
| 68 |
-
const formik = useFormik({
|
| 69 |
-
initialValues: {
|
| 70 |
-
name: "",
|
| 71 |
-
phone: "",
|
| 72 |
-
},
|
| 73 |
-
validationSchema: Yup.object({
|
| 74 |
-
name: Yup.string().required("اسم العميل مطلوب").min(3, "3 أحرف على الأقل"),
|
| 75 |
-
}),
|
| 76 |
-
onSubmit: (values) => createCustomerMutation.mutate(values),
|
| 77 |
-
});
|
| 78 |
-
|
| 79 |
-
const customers = response?.data?.data ?? EMPTY_LIST;
|
| 80 |
-
const pagination = response?.data?.pagination || { total: 0, totalPages: 0 };
|
| 81 |
-
|
| 82 |
-
const handleSearchChange = (e) => {
|
| 83 |
-
setSearchTerm(e.target.value);
|
| 84 |
-
setPage(1); // Reset to page 1 when searching
|
| 85 |
-
};
|
| 86 |
-
|
| 87 |
-
if (isLoading) {
|
| 88 |
-
return <LodderSpinner />;
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
return (
|
| 92 |
-
<div className="space-y-6">
|
| 93 |
-
{permissions.canCreateCustomers ? (
|
| 94 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 95 |
-
<div className="mb-5 flex items-center gap-3">
|
| 96 |
-
<div className="flex h-12 w-12 items-center justify-center rounded-2xl border border-emerald-100 bg-emerald-50 text-emerald-700">
|
| 97 |
-
<UserRound size={22} />
|
| 98 |
-
</div>
|
| 99 |
-
<div>
|
| 100 |
-
<h3 className="text-lg font-black text-slate-900">تسجيل عميل</h3>
|
| 101 |
-
<p className="text-sm text-slate-500">أنشئ حساب عميل قبل إصدار الفواتير وتتبع المدفوعات.</p>
|
| 102 |
-
</div>
|
| 103 |
-
</div>
|
| 104 |
-
|
| 105 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4 md:grid-cols-[1.2fr_1fr_auto]">
|
| 106 |
-
<Field label="اسم العميل" name="name" formik={formik} placeholder="الاسم الكامل للعميل" />
|
| 107 |
-
<Field label="الهاتف" name="phone" formik={formik} placeholder="+20..." />
|
| 108 |
-
<button
|
| 109 |
-
type="submit"
|
| 110 |
-
disabled={createCustomerMutation.isPending}
|
| 111 |
-
className="mt-[22px] flex h-12 items-center justify-center gap-2 rounded-2xl bg-emerald-600 px-5 font-bold text-white transition hover:bg-emerald-700 disabled:opacity-60"
|
| 112 |
-
>
|
| 113 |
-
{createCustomerMutation.isPending ? <Loader2 size={18} className="animate-spin" /> : <PlusCircle size={18} />}
|
| 114 |
-
إضافة عميل
|
| 115 |
-
</button>
|
| 116 |
-
</form>
|
| 117 |
-
</section>
|
| 118 |
-
) : null}
|
| 119 |
-
|
| 120 |
-
<section className="rounded-3xl border border-slate-200 bg-white shadow-sm">
|
| 121 |
-
<div className="flex flex-col gap-4 border-b border-slate-100 p-5 md:flex-row md:items-center md:justify-between">
|
| 122 |
-
<div>
|
| 123 |
-
<h3 className="text-lg font-black text-slate-900">حسابات العملاء</h3>
|
| 124 |
-
<p className="text-sm text-slate-500">فتح التفاصيل، إصدار الفواتير، وتتبع الأرصدة المعلقة.</p>
|
| 125 |
-
</div>
|
| 126 |
-
<div className="relative w-full md:max-w-sm">
|
| 127 |
-
<Search size={18} className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400" />
|
| 128 |
-
<input
|
| 129 |
-
type="text"
|
| 130 |
-
value={searchTerm}
|
| 131 |
-
onChange={handleSearchChange}
|
| 132 |
-
placeholder="البحث في العملاء..."
|
| 133 |
-
className="h-11 w-full rounded-2xl border border-slate-200 bg-slate-50 pr-11 pl-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-amber-500"
|
| 134 |
-
/>
|
| 135 |
-
</div>
|
| 136 |
-
</div>
|
| 137 |
-
|
| 138 |
-
<div className="overflow-x-auto">
|
| 139 |
-
<table className="min-w-full border-collapse">
|
| 140 |
-
<thead>
|
| 141 |
-
<tr className="border-b border-slate-200 text-right text-sm font-bold text-slate-500">
|
| 142 |
-
<th className="px-4 py-3">العميل</th>
|
| 143 |
-
<th className="px-4 py-3">تاريخ الإنشاء</th>
|
| 144 |
-
<th className="px-4 py-3">الفواتير</th>
|
| 145 |
-
<th className="px-4 py-3">المستحقات</th>
|
| 146 |
-
<th className="px-4 py-3">الإجراءات</th>
|
| 147 |
-
</tr>
|
| 148 |
-
</thead>
|
| 149 |
-
<tbody>
|
| 150 |
-
{customers.length ? (
|
| 151 |
-
customers.map((customer) => (
|
| 152 |
-
<tr key={customer._id} className="border-b border-slate-100 text-sm text-slate-700">
|
| 153 |
-
<td className="px-4 py-4">
|
| 154 |
-
<div className="flex items-center gap-3">
|
| 155 |
-
<div className="flex h-10 w-10 items-center justify-center rounded-2xl bg-slate-100 font-black text-slate-700">
|
| 156 |
-
{customer.name.charAt(0)}
|
| 157 |
-
</div>
|
| 158 |
-
<div>
|
| 159 |
-
<p className="font-bold text-slate-900">{customer.name}</p>
|
| 160 |
-
<p className="text-xs text-slate-500">{customer.phone || "لا يوجد رقم هاتف"}</p>
|
| 161 |
-
</div>
|
| 162 |
-
</div>
|
| 163 |
-
</td>
|
| 164 |
-
<td className="px-4 py-4">{formatDate(customer.createdAt)}</td>
|
| 165 |
-
<td className="px-4 py-4 font-bold">{formatNumber(customer.invoicesCount || customer.invoices?.length || 0)}</td>
|
| 166 |
-
<td className="px-4 py-4 font-bold text-rose-600">
|
| 167 |
-
{formatCurrency(customer.totalDebt || 0, currency)}
|
| 168 |
-
</td>
|
| 169 |
-
<td className="px-4 py-4">
|
| 170 |
-
<div className="flex flex-wrap items-center gap-2">
|
| 171 |
-
<Link
|
| 172 |
-
to={`/customers/${customer._id}`}
|
| 173 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl bg-slate-900 px-4 font-bold text-white transition hover:bg-slate-800"
|
| 174 |
-
>
|
| 175 |
-
<Eye size={16} />
|
| 176 |
-
التفاصيل
|
| 177 |
-
</Link>
|
| 178 |
-
{permissions.canCreateInvoices ? (
|
| 179 |
-
<button
|
| 180 |
-
type="button"
|
| 181 |
-
onClick={() => setActiveCustomerId(customer._id)}
|
| 182 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl bg-amber-500 px-4 font-bold text-slate-900 transition hover:bg-amber-400"
|
| 183 |
-
>
|
| 184 |
-
<PlusCircle size={16} />
|
| 185 |
-
فاتورة جديدة
|
| 186 |
-
</button>
|
| 187 |
-
) : null}
|
| 188 |
-
{permissions.canDeleteCustomers ? (
|
| 189 |
-
pendingDeleteId === customer._id ? (
|
| 190 |
-
<div className="flex items-center gap-2 rounded-2xl border border-rose-200 bg-rose-50 px-3 py-2">
|
| 191 |
-
<span className="text-xs font-bold text-rose-700">حذف الحساب؟</span>
|
| 192 |
-
<button
|
| 193 |
-
type="button"
|
| 194 |
-
onClick={() => deleteCustomerMutation.mutate(customer._id)}
|
| 195 |
-
disabled={deleteCustomerMutation.isPending}
|
| 196 |
-
className="rounded-xl bg-rose-600 px-3 py-1.5 text-xs font-bold text-white transition hover:bg-rose-700 disabled:opacity-60"
|
| 197 |
-
>
|
| 198 |
-
{deleteCustomerMutation.isPending ? <Loader2 size={14} className="animate-spin" /> : "تأكيد"}
|
| 199 |
-
</button>
|
| 200 |
-
<button
|
| 201 |
-
type="button"
|
| 202 |
-
onClick={() => setPendingDeleteId(null)}
|
| 203 |
-
disabled={deleteCustomerMutation.isPending}
|
| 204 |
-
className="rounded-xl border border-slate-200 bg-white px-3 py-1.5 text-xs font-bold text-slate-600 transition hover:bg-slate-50"
|
| 205 |
-
>
|
| 206 |
-
إلغاء
|
| 207 |
-
</button>
|
| 208 |
-
</div>
|
| 209 |
-
) : (
|
| 210 |
-
<button
|
| 211 |
-
type="button"
|
| 212 |
-
onClick={() => setPendingDeleteId(customer._id)}
|
| 213 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl border border-rose-100 bg-rose-50 px-3 font-bold text-rose-600 transition hover:bg-rose-600 hover:text-white"
|
| 214 |
-
>
|
| 215 |
-
<Trash2 size={16} />
|
| 216 |
-
حذف
|
| 217 |
-
</button>
|
| 218 |
-
)
|
| 219 |
-
) : null}
|
| 220 |
-
</div>
|
| 221 |
-
</td>
|
| 222 |
-
</tr>
|
| 223 |
-
))
|
| 224 |
-
) : (
|
| 225 |
-
<tr>
|
| 226 |
-
<td colSpan="5" className="px-4 py-12 text-center text-sm font-medium text-slate-500">
|
| 227 |
-
لا يوجد عملاء مطابقون لهذا البحث.
|
| 228 |
-
</td>
|
| 229 |
-
</tr>
|
| 230 |
-
)}
|
| 231 |
-
</tbody>
|
| 232 |
-
</table>
|
| 233 |
-
</div>
|
| 234 |
-
|
| 235 |
-
<Pagination
|
| 236 |
-
currentPage={page}
|
| 237 |
-
totalPages={pagination.totalPages}
|
| 238 |
-
onPageChange={setPage}
|
| 239 |
-
totalItems={pagination.total}
|
| 240 |
-
limit={5}
|
| 241 |
-
/>
|
| 242 |
-
</section>
|
| 243 |
-
|
| 244 |
-
{activeCustomerId ? (
|
| 245 |
-
<InvoiceItemsModal customerId={activeCustomerId} onClose={() => setActiveCustomerId(null)} />
|
| 246 |
-
) : null}
|
| 247 |
-
</div>
|
| 248 |
-
);
|
| 249 |
-
};
|
| 250 |
-
|
| 251 |
-
const Field = ({ label, name, formik, placeholder }) => (
|
| 252 |
-
<div>
|
| 253 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">{label}</label>
|
| 254 |
-
<input
|
| 255 |
-
name={name}
|
| 256 |
-
type="text"
|
| 257 |
-
value={formik.values[name]}
|
| 258 |
-
onChange={formik.handleChange}
|
| 259 |
-
onBlur={formik.handleBlur}
|
| 260 |
-
placeholder={placeholder}
|
| 261 |
-
className={`h-12 w-full rounded-2xl border bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-emerald-500 ${
|
| 262 |
-
formik.touched[name] && formik.errors[name] ? "border-rose-400" : "border-slate-200"
|
| 263 |
-
}`}
|
| 264 |
-
/>
|
| 265 |
-
{formik.touched[name] && formik.errors[name] ? (
|
| 266 |
-
<p className="mt-1 text-xs font-bold text-rose-500">{formik.errors[name]}</p>
|
| 267 |
-
) : null}
|
| 268 |
-
</div>
|
| 269 |
-
);
|
| 270 |
-
|
| 271 |
-
export default CustomersWorkspace;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Customers/DeleteCustomerButton.jsx
DELETED
|
@@ -1,65 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import { Trash2, Loader2 } from "lucide-react";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
| 5 |
-
import { toast } from "react-toastify";
|
| 6 |
-
|
| 7 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 8 |
-
|
| 9 |
-
const DeleteCustomerButton = ({ customerId, customerName }) => {
|
| 10 |
-
const [isConfirming, setIsConfirming] = useState(false);
|
| 11 |
-
const queryClient = useQueryClient();
|
| 12 |
-
|
| 13 |
-
const deleteCustomer = async () => {
|
| 14 |
-
const res = await axios.delete(`${apiUrl}/customers/${customerId}`);
|
| 15 |
-
return res.data;
|
| 16 |
-
};
|
| 17 |
-
|
| 18 |
-
const { mutate, isPending } = useMutation({
|
| 19 |
-
mutationFn: deleteCustomer,
|
| 20 |
-
onSuccess: () => {
|
| 21 |
-
queryClient.invalidateQueries({ queryKey: ["customers"] });
|
| 22 |
-
toast.success(`تم حذف العميل ${customerName} بنجاح`);
|
| 23 |
-
setIsConfirming(false);
|
| 24 |
-
},
|
| 25 |
-
onError: (err) => {
|
| 26 |
-
toast.error(err.response?.data?.message || err.message || "حدث خطأ أثناء الحذف");
|
| 27 |
-
setIsConfirming(false);
|
| 28 |
-
}
|
| 29 |
-
});
|
| 30 |
-
|
| 31 |
-
if (isConfirming) {
|
| 32 |
-
return (
|
| 33 |
-
<div className="flex items-center gap-2 bg-rose-50 p-1.5 rounded-xl border border-rose-200 shrink-0 shadow-sm animate-in fade-in slide-in-from-right-2">
|
| 34 |
-
<span className="text-xs font-black text-rose-700 whitespace-nowrap px-1">حذف العميل؟</span>
|
| 35 |
-
<button
|
| 36 |
-
onClick={(e) => { e.preventDefault(); e.stopPropagation(); mutate(); }}
|
| 37 |
-
disabled={isPending}
|
| 38 |
-
className="bg-rose-500 text-white px-3 py-1.5 rounded-lg font-bold text-xs hover:bg-rose-600 transition-colors disabled:opacity-50 flex items-center justify-center min-w-[60px]"
|
| 39 |
-
>
|
| 40 |
-
{isPending ? <Loader2 size={14} className="animate-spin" /> : "تأكيد"}
|
| 41 |
-
</button>
|
| 42 |
-
<button
|
| 43 |
-
onClick={(e) => { e.preventDefault(); e.stopPropagation(); setIsConfirming(false); }}
|
| 44 |
-
disabled={isPending}
|
| 45 |
-
className="bg-white text-slate-500 px-3 py-1.5 rounded-lg font-bold text-xs hover:bg-slate-100 hover:text-slate-800 border border-slate-200 transition-colors"
|
| 46 |
-
>
|
| 47 |
-
إلغاء
|
| 48 |
-
</button>
|
| 49 |
-
</div>
|
| 50 |
-
);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
return (
|
| 54 |
-
<button
|
| 55 |
-
onClick={(e) => { e.preventDefault(); e.stopPropagation(); setIsConfirming(true); }}
|
| 56 |
-
className="bg-rose-50 text-rose-500 px-3 lg:px-4 py-2 rounded-lg hover:bg-rose-500 hover:text-white transition-all shadow-sm flex items-center justify-center gap-1.5 lg:gap-2 whitespace-nowrap"
|
| 57 |
-
title="حذف العميل نهائياً"
|
| 58 |
-
>
|
| 59 |
-
<Trash2 size={18} />
|
| 60 |
-
<span className="hidden xl:inline font-bold">حذف</span>
|
| 61 |
-
</button>
|
| 62 |
-
);
|
| 63 |
-
};
|
| 64 |
-
|
| 65 |
-
export default DeleteCustomerButton;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Dashboard/DashboardTab.jsx
DELETED
|
@@ -1,331 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import { useQuery } from "@tanstack/react-query";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import {
|
| 5 |
-
AlertTriangle,
|
| 6 |
-
ArrowDownRight,
|
| 7 |
-
ArrowUpRight,
|
| 8 |
-
Boxes,
|
| 9 |
-
PackageSearch,
|
| 10 |
-
ReceiptText,
|
| 11 |
-
ShoppingCart,
|
| 12 |
-
UsersRound,
|
| 13 |
-
} from "lucide-react";
|
| 14 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 15 |
-
import Pagination from "../../common/Pagination";
|
| 16 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 17 |
-
import { formatCurrency, formatDate, formatDateTime, formatNumber } from "../../utils/formatters";
|
| 18 |
-
|
| 19 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 20 |
-
const EMPTY_LIST = [];
|
| 21 |
-
|
| 22 |
-
const metricCards = [
|
| 23 |
-
{ key: "salesInRange", label: "المبيعات في الفترة", icon: ShoppingCart, tone: "emerald" },
|
| 24 |
-
{ key: "collectionsInRange", label: "التحصيلات النقدية", icon: ArrowUpRight, tone: "blue" },
|
| 25 |
-
{ key: "expensesInRange", label: "المصروفات", icon: ArrowDownRight, tone: "rose" },
|
| 26 |
-
{ key: "outstandingReceivables", label: "المستحقات المعلقة", icon: ReceiptText, tone: "amber" },
|
| 27 |
-
];
|
| 28 |
-
|
| 29 |
-
const toneClasses = {
|
| 30 |
-
emerald: "bg-emerald-50 text-emerald-700 border-emerald-100",
|
| 31 |
-
blue: "bg-blue-50 text-blue-700 border-blue-100",
|
| 32 |
-
rose: "bg-rose-50 text-rose-700 border-rose-100",
|
| 33 |
-
amber: "bg-amber-50 text-amber-700 border-amber-100",
|
| 34 |
-
slate: "bg-slate-50 text-slate-700 border-slate-100",
|
| 35 |
-
};
|
| 36 |
-
|
| 37 |
-
const DashboardTab = () => {
|
| 38 |
-
const { user } = useAuth();
|
| 39 |
-
const currency = user?.tenant?.settings?.currency || "EGP";
|
| 40 |
-
|
| 41 |
-
const [invoicesPage, setInvoicesPage] = useState(1);
|
| 42 |
-
const [paymentsPage, setPaymentsPage] = useState(1);
|
| 43 |
-
const [customersPage, setCustomersPage] = useState(1);
|
| 44 |
-
const [lowStockPage, setLowStockPage] = useState(1);
|
| 45 |
-
const limit = 5;
|
| 46 |
-
|
| 47 |
-
const { data, isLoading } = useQuery({
|
| 48 |
-
queryKey: ["reports", "overview", invoicesPage, paymentsPage, customersPage, lowStockPage],
|
| 49 |
-
queryFn: () => axios.get(`${apiUrl}/reports/overview`, {
|
| 50 |
-
params: {
|
| 51 |
-
invoicesPage,
|
| 52 |
-
paymentsPage,
|
| 53 |
-
customersPage,
|
| 54 |
-
lowStockPage,
|
| 55 |
-
limit
|
| 56 |
-
}
|
| 57 |
-
}),
|
| 58 |
-
refetchOnWindowFocus: false,
|
| 59 |
-
});
|
| 60 |
-
|
| 61 |
-
const overview = data?.data;
|
| 62 |
-
const metrics = overview?.metrics || {};
|
| 63 |
-
const trend = overview?.trends?.last7Days ?? EMPTY_LIST;
|
| 64 |
-
const highestPoint = Math.max(
|
| 65 |
-
...trend.flatMap((point) => [point.sales || 0, point.collections || 0, point.expenses || 0]),
|
| 66 |
-
1
|
| 67 |
-
);
|
| 68 |
-
|
| 69 |
-
if (isLoading) {
|
| 70 |
-
return <LodderSpinner />;
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
return (
|
| 74 |
-
<div className="space-y-6">
|
| 75 |
-
<div className="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
|
| 76 |
-
{metricCards.map(({ key, label, icon, tone }) => {
|
| 77 |
-
const IconComponent = icon;
|
| 78 |
-
return (
|
| 79 |
-
<div key={key} className="rounded-3xl border border-slate-200 bg-white p-5 shadow-sm">
|
| 80 |
-
<div className="mb-4 flex items-start justify-between">
|
| 81 |
-
<div className={`flex h-12 w-12 items-center justify-center rounded-2xl border ${toneClasses[tone]}`}>
|
| 82 |
-
<IconComponent size={22} />
|
| 83 |
-
</div>
|
| 84 |
-
<span className="rounded-full bg-slate-100 px-3 py-1 text-xs font-bold text-slate-500">
|
| 85 |
-
آخر 30 يوم
|
| 86 |
-
</span>
|
| 87 |
-
</div>
|
| 88 |
-
<p className="text-sm font-bold text-slate-500">{label}</p>
|
| 89 |
-
<p className="mt-2 text-3xl font-black text-slate-900">
|
| 90 |
-
{formatCurrency(metrics[key], currency)}
|
| 91 |
-
</p>
|
| 92 |
-
</div>
|
| 93 |
-
)})}
|
| 94 |
-
</div>
|
| 95 |
-
|
| 96 |
-
<div className="grid gap-6 xl:grid-cols-[1.8fr_1fr]">
|
| 97 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 98 |
-
<div className="mb-6 flex items-center justify-between gap-4">
|
| 99 |
-
<div>
|
| 100 |
-
<h3 className="text-xl font-black text-slate-900">الاتجاه المالي</h3>
|
| 101 |
-
<p className="text-sm text-slate-500">المبيعات والتحصيلات والمصروفات خلال آخر 7 أيام.</p>
|
| 102 |
-
</div>
|
| 103 |
-
<div className="rounded-2xl bg-slate-50 px-4 py-2 text-sm font-bold text-slate-600">
|
| 104 |
-
صافي التدفق النقدي: {formatCurrency(metrics.netCashFlow, currency)}
|
| 105 |
-
</div>
|
| 106 |
-
</div>
|
| 107 |
-
|
| 108 |
-
<div className="grid grid-cols-7 gap-3">
|
| 109 |
-
{trend.map((point) => {
|
| 110 |
-
const salesHeight = `${Math.max((point.sales / highestPoint) * 160, 12)}px`;
|
| 111 |
-
const collectionsHeight = `${Math.max((point.collections / highestPoint) * 160, 12)}px`;
|
| 112 |
-
const expensesHeight = `${Math.max((point.expenses / highestPoint) * 160, 12)}px`;
|
| 113 |
-
|
| 114 |
-
return (
|
| 115 |
-
<div key={point.date} className="rounded-2xl bg-slate-50 p-3">
|
| 116 |
-
<div className="flex h-44 items-end justify-center gap-2">
|
| 117 |
-
<div className="w-3 rounded-full bg-emerald-500" style={{ height: salesHeight }} />
|
| 118 |
-
<div className="w-3 rounded-full bg-blue-500" style={{ height: collectionsHeight }} />
|
| 119 |
-
<div className="w-3 rounded-full bg-rose-400" style={{ height: expensesHeight }} />
|
| 120 |
-
</div>
|
| 121 |
-
<p className="mt-3 text-center text-xs font-bold text-slate-500">{point.label}</p>
|
| 122 |
-
</div>
|
| 123 |
-
);
|
| 124 |
-
})}
|
| 125 |
-
</div>
|
| 126 |
-
</section>
|
| 127 |
-
|
| 128 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 129 |
-
<h3 className="text-xl font-black text-slate-900">لمحة تشغيلية</h3>
|
| 130 |
-
<p className="mt-1 text-sm text-slate-500">صحة الأعمال الحية لمساحة عمل هذا المستأجر.</p>
|
| 131 |
-
|
| 132 |
-
<div className="mt-6 space-y-3">
|
| 133 |
-
<SnapshotRow icon={UsersRound} label="العملاء" value={formatNumber(metrics.customersCount)} />
|
| 134 |
-
<SnapshotRow icon={Boxes} label="وحدات المخزون" value={formatNumber(metrics.inventoryUnits)} />
|
| 135 |
-
<SnapshotRow icon={PackageSearch} label="قيمة المخزون" value={formatCurrency(metrics.inventoryValue, currency)} />
|
| 136 |
-
<SnapshotRow icon={AlertTriangle} label="أصناف منخفضة المخزون" value={formatNumber(metrics.lowStockCount)} alert />
|
| 137 |
-
</div>
|
| 138 |
-
|
| 139 |
-
<div className="mt-6 rounded-2xl bg-slate-950 p-5 text-white">
|
| 140 |
-
<p className="text-sm font-bold text-slate-300">متوسط قيمة الفاتورة</p>
|
| 141 |
-
<p className="mt-2 text-3xl font-black">{formatCurrency(metrics.averageInvoiceValue, currency)}</p>
|
| 142 |
-
<p className="mt-4 text-sm text-slate-400">
|
| 143 |
-
{formatNumber(metrics.paidInvoicesCount)} فاتورة مدفوعة و {formatNumber(metrics.partialInvoicesCount)} فاتورة مدفوعة جزئياً.
|
| 144 |
-
</p>
|
| 145 |
-
</div>
|
| 146 |
-
</section>
|
| 147 |
-
</div>
|
| 148 |
-
|
| 149 |
-
<div className="grid gap-6 xl:grid-cols-3">
|
| 150 |
-
<Panel title="تنبيهات المخزون المنخفض" subtitle="أصناف تحتاج إعادة تموين قريباً.">
|
| 151 |
-
{overview?.lowStockItems?.data?.length ? (
|
| 152 |
-
<div className="space-y-3">
|
| 153 |
-
{overview.lowStockItems.data.map((item) => (
|
| 154 |
-
<div key={item._id} className="rounded-2xl border border-amber-100 bg-amber-50 p-4">
|
| 155 |
-
<div className="flex items-center justify-between gap-3">
|
| 156 |
-
<div>
|
| 157 |
-
<p className="font-bold text-slate-900">{item.name}</p>
|
| 158 |
-
<p className="text-sm text-slate-500">الحد الأدنى {item.lowStockThreshold} {item.unit || "وحدة"}</p>
|
| 159 |
-
</div>
|
| 160 |
-
<span className="rounded-full bg-white px-3 py-1 text-sm font-black text-amber-700">
|
| 161 |
-
متبقي {item.stock}
|
| 162 |
-
</span>
|
| 163 |
-
</div>
|
| 164 |
-
</div>
|
| 165 |
-
))}
|
| 166 |
-
<Pagination
|
| 167 |
-
currentPage={lowStockPage}
|
| 168 |
-
totalPages={overview.lowStockItems.pagination.totalPages}
|
| 169 |
-
onPageChange={setLowStockPage}
|
| 170 |
-
totalItems={overview.lowStockItems.pagination.total}
|
| 171 |
-
limit={limit}
|
| 172 |
-
/>
|
| 173 |
-
</div>
|
| 174 |
-
) : (
|
| 175 |
-
<EmptyPanel text="لا توجد تنبيهات مخزون منخفض حالياً." />
|
| 176 |
-
)}
|
| 177 |
-
</Panel>
|
| 178 |
-
|
| 179 |
-
<Panel title="أفضل العملاء" subtitle="أفضل الحسابات حسب قيمة الفواتير.">
|
| 180 |
-
{overview?.topCustomers?.data?.length ? (
|
| 181 |
-
<div className="space-y-3">
|
| 182 |
-
{overview.topCustomers.data.map((customer) => (
|
| 183 |
-
<div key={customer.customerId || customer.customerName} className="rounded-2xl border border-slate-100 bg-slate-50 p-4">
|
| 184 |
-
<div className="flex items-center justify-between gap-3">
|
| 185 |
-
<div>
|
| 186 |
-
<p className="font-bold text-slate-900">{customer.customerName}</p>
|
| 187 |
-
<p className="text-sm text-slate-500">{customer.invoices} فاتورة</p>
|
| 188 |
-
</div>
|
| 189 |
-
<div className="text-left">
|
| 190 |
-
<p className="font-black text-slate-900">{formatCurrency(customer.sales, currency)}</p>
|
| 191 |
-
<p className="text-xs text-rose-500">مستحقات {formatCurrency(customer.outstanding, currency)}</p>
|
| 192 |
-
</div>
|
| 193 |
-
</div>
|
| 194 |
-
</div>
|
| 195 |
-
))}
|
| 196 |
-
<Pagination
|
| 197 |
-
currentPage={customersPage}
|
| 198 |
-
totalPages={overview.topCustomers.pagination.totalPages}
|
| 199 |
-
onPageChange={setCustomersPage}
|
| 200 |
-
totalItems={overview.topCustomers.pagination.total}
|
| 201 |
-
limit={limit}
|
| 202 |
-
/>
|
| 203 |
-
</div>
|
| 204 |
-
) : (
|
| 205 |
-
<EmptyPanel text="ستظهر بيانات أداء العملاء هنا بعد بدء إصدار الفواتير." />
|
| 206 |
-
)}
|
| 207 |
-
</Panel>
|
| 208 |
-
|
| 209 |
-
<Panel title="آخر المدفوعات" subtitle="أحدث التحصيلات في الفترة المحددة.">
|
| 210 |
-
{overview?.recentPayments?.data?.length ? (
|
| 211 |
-
<div className="space-y-3">
|
| 212 |
-
{overview.recentPayments.data.map((payment) => (
|
| 213 |
-
<div key={`${payment.invoiceNumber}-${payment.date}`} className="rounded-2xl border border-slate-100 bg-slate-50 p-4">
|
| 214 |
-
<div className="flex items-center justify-between gap-3">
|
| 215 |
-
<div>
|
| 216 |
-
<p className="font-bold text-slate-900">{payment.customerName}</p>
|
| 217 |
-
<p className="text-sm text-slate-500">{payment.invoiceNumber} | {formatDateTime(payment.date)}</p>
|
| 218 |
-
</div>
|
| 219 |
-
<span className="font-black text-emerald-600">{formatCurrency(payment.amount, currency)}</span>
|
| 220 |
-
</div>
|
| 221 |
-
</div>
|
| 222 |
-
))}
|
| 223 |
-
<Pagination
|
| 224 |
-
currentPage={paymentsPage}
|
| 225 |
-
totalPages={overview.recentPayments.pagination.totalPages}
|
| 226 |
-
onPageChange={setPaymentsPage}
|
| 227 |
-
totalItems={overview.recentPayments.pagination.total}
|
| 228 |
-
limit={limit}
|
| 229 |
-
/>
|
| 230 |
-
</div>
|
| 231 |
-
) : (
|
| 232 |
-
<EmptyPanel text="لا توجد مدفوعات مسجلة في هذه الفترة." />
|
| 233 |
-
)}
|
| 234 |
-
</Panel>
|
| 235 |
-
</div>
|
| 236 |
-
|
| 237 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 238 |
-
<div className="mb-6 flex items-center justify-between gap-4">
|
| 239 |
-
<div>
|
| 240 |
-
<h3 className="text-xl font-black text-slate-900">آخر الفواتير</h3>
|
| 241 |
-
<p className="text-sm text-slate-500">
|
| 242 |
-
فترة التقرير: {overview?.dateRange?.start ? formatDate(overview.dateRange.start) : "--"} إلى {overview?.dateRange?.end ? formatDate(overview.dateRange.end) : "--"}
|
| 243 |
-
</p>
|
| 244 |
-
</div>
|
| 245 |
-
</div>
|
| 246 |
-
|
| 247 |
-
<div className="overflow-x-auto">
|
| 248 |
-
<table className="min-w-full border-collapse">
|
| 249 |
-
<thead>
|
| 250 |
-
<tr className="border-b border-slate-200 text-right text-sm font-bold text-slate-500">
|
| 251 |
-
<th className="px-3 py-3">الفاتورة</th>
|
| 252 |
-
<th className="px-3 py-3">العميل</th>
|
| 253 |
-
<th className="px-3 py-3">تاريخ الإنشاء</th>
|
| 254 |
-
<th className="px-3 py-3">الحالة</th>
|
| 255 |
-
<th className="px-3 py-3">الإجمالي</th>
|
| 256 |
-
<th className="px-3 py-3">المتبقي</th>
|
| 257 |
-
</tr>
|
| 258 |
-
</thead>
|
| 259 |
-
<tbody>
|
| 260 |
-
{overview?.recentInvoices?.data?.length ? (
|
| 261 |
-
overview.recentInvoices.data.map((invoice) => (
|
| 262 |
-
<tr key={invoice._id} className="border-b border-slate-100 text-sm text-slate-700">
|
| 263 |
-
<td className="px-3 py-4 font-bold text-slate-900">{invoice.invoiceNumber}</td>
|
| 264 |
-
<td className="px-3 py-4">{invoice.customerName}</td>
|
| 265 |
-
<td className="px-3 py-4">{formatDateTime(invoice.createdAt)}</td>
|
| 266 |
-
<td className="px-3 py-4">
|
| 267 |
-
<span className="rounded-full bg-slate-100 px-3 py-1 text-xs font-bold uppercase tracking-wide text-slate-600">
|
| 268 |
-
{invoice.status}
|
| 269 |
-
</span>
|
| 270 |
-
</td>
|
| 271 |
-
<td className="px-3 py-4 font-bold">{formatCurrency(invoice.totalAmount, currency)}</td>
|
| 272 |
-
<td className="px-3 py-4 font-bold text-rose-600">{formatCurrency(invoice.remainingAmount, currency)}</td>
|
| 273 |
-
</tr>
|
| 274 |
-
))
|
| 275 |
-
) : (
|
| 276 |
-
<tr>
|
| 277 |
-
<td colSpan="6" className="px-3 py-10 text-center text-sm font-medium text-slate-500">
|
| 278 |
-
لم يتم إنشاء فواتير في الفترة المحددة بعد.
|
| 279 |
-
</td>
|
| 280 |
-
</tr>
|
| 281 |
-
)}
|
| 282 |
-
</tbody>
|
| 283 |
-
</table>
|
| 284 |
-
</div>
|
| 285 |
-
|
| 286 |
-
{overview?.recentInvoices?.pagination && (
|
| 287 |
-
<div className="mt-4">
|
| 288 |
-
<Pagination
|
| 289 |
-
currentPage={invoicesPage}
|
| 290 |
-
totalPages={overview.recentInvoices.pagination.totalPages}
|
| 291 |
-
onPageChange={setInvoicesPage}
|
| 292 |
-
totalItems={overview.recentInvoices.pagination.total}
|
| 293 |
-
limit={limit}
|
| 294 |
-
/>
|
| 295 |
-
</div>
|
| 296 |
-
)}
|
| 297 |
-
</section>
|
| 298 |
-
</div>
|
| 299 |
-
);
|
| 300 |
-
};
|
| 301 |
-
|
| 302 |
-
const SnapshotRow = ({ icon, label, value, alert = false }) => {
|
| 303 |
-
const IconComponent = icon;
|
| 304 |
-
return (
|
| 305 |
-
<div className={`flex items-center justify-between rounded-2xl border p-4 ${alert ? "border-amber-100 bg-amber-50" : "border-slate-100 bg-slate-50"}`}>
|
| 306 |
-
<div className="flex items-center gap-3">
|
| 307 |
-
<div className={`flex h-10 w-10 items-center justify-center rounded-2xl ${alert ? "bg-white text-amber-700" : "bg-white text-slate-700"}`}>
|
| 308 |
-
<IconComponent size={18} />
|
| 309 |
-
</div>
|
| 310 |
-
<span className="font-bold text-slate-700">{label}</span>
|
| 311 |
-
</div>
|
| 312 |
-
<span className={`font-black ${alert ? "text-amber-700" : "text-slate-900"}`}>{value}</span>
|
| 313 |
-
</div>
|
| 314 |
-
);
|
| 315 |
-
};
|
| 316 |
-
|
| 317 |
-
const Panel = ({ title, subtitle, children }) => (
|
| 318 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 319 |
-
<h3 className="text-lg font-black text-slate-900">{title}</h3>
|
| 320 |
-
<p className="mt-1 text-sm text-slate-500">{subtitle}</p>
|
| 321 |
-
<div className="mt-5">{children}</div>
|
| 322 |
-
</section>
|
| 323 |
-
);
|
| 324 |
-
|
| 325 |
-
const EmptyPanel = ({ text }) => (
|
| 326 |
-
<div className="rounded-2xl border border-dashed border-slate-200 bg-slate-50 px-4 py-10 text-center text-sm font-medium text-slate-500">
|
| 327 |
-
{text}
|
| 328 |
-
</div>
|
| 329 |
-
);
|
| 330 |
-
|
| 331 |
-
export default DashboardTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Inventory/InventoryTab.jsx
DELETED
|
@@ -1,251 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import { Search, PlusCircle, Package, Edit, Trash2, AlertCircle } from "lucide-react";
|
| 5 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 6 |
-
import { toast } from "react-toastify";
|
| 7 |
-
import { useFormik } from "formik";
|
| 8 |
-
import * as Yup from "yup";
|
| 9 |
-
|
| 10 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 11 |
-
|
| 12 |
-
const InventoryTab = () => {
|
| 13 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 14 |
-
const [isModalOpen, setIsModalOpen] = useState(false);
|
| 15 |
-
const [editProduct, setEditProduct] = useState(null);
|
| 16 |
-
|
| 17 |
-
const { data, isLoading } = useQuery({
|
| 18 |
-
queryKey: ["products"],
|
| 19 |
-
queryFn: () => axios.get(`${apiUrl}/products`),
|
| 20 |
-
});
|
| 21 |
-
|
| 22 |
-
const products = data?.data || [];
|
| 23 |
-
const filteredProducts = products.filter(p => p.name.toLowerCase().includes(searchTerm.toLowerCase()));
|
| 24 |
-
|
| 25 |
-
if (isLoading) return <LodderSpinner />;
|
| 26 |
-
|
| 27 |
-
return (
|
| 28 |
-
<div className="space-y-6">
|
| 29 |
-
{/* Top Bar */}
|
| 30 |
-
<div className="flex flex-col md:flex-row gap-4 items-center justify-between bg-white p-4 rounded-2xl shadow-sm border border-slate-200">
|
| 31 |
-
<div className="relative w-full md:w-1/2">
|
| 32 |
-
<div className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400 bg-slate-100 p-1.5 rounded-lg">
|
| 33 |
-
<Search size={18} />
|
| 34 |
-
</div>
|
| 35 |
-
<input
|
| 36 |
-
type="text"
|
| 37 |
-
placeholder="ابحث عن منتج في المخزن (مثال: سلك 10مم)..."
|
| 38 |
-
value={searchTerm}
|
| 39 |
-
onChange={(e) => setSearchTerm(e.target.value)}
|
| 40 |
-
className="w-full pr-14 pl-4 py-3 bg-slate-50 border border-slate-200 rounded-xl shadow-inner focus:ring-2 focus:ring-indigo-500 outline-none font-medium"
|
| 41 |
-
/>
|
| 42 |
-
</div>
|
| 43 |
-
<button
|
| 44 |
-
onClick={() => { setEditProduct(null); setIsModalOpen(true); }}
|
| 45 |
-
className="flex items-center gap-2 px-6 py-3 bg-indigo-600 text-white font-bold rounded-xl hover:bg-indigo-700 transition-all shadow-md shadow-indigo-600/20 w-full md:w-auto justify-center"
|
| 46 |
-
>
|
| 47 |
-
<PlusCircle size={20} />
|
| 48 |
-
إضافة صنف جديد
|
| 49 |
-
</button>
|
| 50 |
-
</div>
|
| 51 |
-
|
| 52 |
-
{/* Products Table */}
|
| 53 |
-
<div className="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden">
|
| 54 |
-
<div className="overflow-x-auto max-h-[500px] overflow-y-auto custom-scroll relative">
|
| 55 |
-
<table className="w-full text-right border-collapse">
|
| 56 |
-
<thead className="sticky top-0 bg-slate-50 border-b border-slate-200 z-10 shadow-sm">
|
| 57 |
-
<tr>
|
| 58 |
-
<th className="p-4 font-bold text-slate-600">اسم وتفاصيل الصنف</th>
|
| 59 |
-
<th className="p-4 font-bold text-slate-600 text-center">الباركود</th>
|
| 60 |
-
<th className="p-4 font-bold text-slate-600 text-center">الكمية بالمخزن</th>
|
| 61 |
-
<th className="p-4 font-bold text-slate-600 text-center">سعر البيع الافتراضي</th>
|
| 62 |
-
<th className="p-4 font-bold text-slate-600 text-center">إجراءات</th>
|
| 63 |
-
</tr>
|
| 64 |
-
</thead>
|
| 65 |
-
<tbody className="divide-y divide-slate-100">
|
| 66 |
-
{filteredProducts.map(product => (
|
| 67 |
-
<tr key={product._id} className="hover:bg-slate-50 transition-colors">
|
| 68 |
-
<td className="p-4 font-bold text-slate-800 flex items-center gap-3">
|
| 69 |
-
<div className="w-12 h-12 rounded-xl bg-indigo-50 border border-indigo-100 text-indigo-500 flex items-center justify-center shrink-0 shadow-sm">
|
| 70 |
-
<Package size={24} />
|
| 71 |
-
</div>
|
| 72 |
-
<div>
|
| 73 |
-
<span className="block text-lg">{product.name}</span>
|
| 74 |
-
{product.description && <span className="text-sm text-slate-400 font-normal">{product.description}</span>}
|
| 75 |
-
</div>
|
| 76 |
-
</td>
|
| 77 |
-
<td className="p-4 text-center text-slate-500 font-mono text-sm bg-slate-50/50">
|
| 78 |
-
{product.barcode || "-"}
|
| 79 |
-
</td>
|
| 80 |
-
<td className="p-4 text-center">
|
| 81 |
-
<span className={`px-4 py-1.5 rounded-xl font-bold text-lg border shadow-sm ${
|
| 82 |
-
product.stock === 0 ? "bg-rose-50 text-rose-600 border-rose-200"
|
| 83 |
-
: product.stock < 10 ? "bg-amber-50 text-amber-600 border-amber-200"
|
| 84 |
-
: "bg-emerald-50 text-emerald-600 border-emerald-200"
|
| 85 |
-
}`}>
|
| 86 |
-
{product.stock}
|
| 87 |
-
</span>
|
| 88 |
-
</td>
|
| 89 |
-
<td className="p-4 text-center font-black text-indigo-600 text-xl bg-slate-50/50">
|
| 90 |
-
<span className="bg-white px-3 py-1 rounded-lg border border-slate-200 shadow-sm inline-block">
|
| 91 |
-
{product.price.toFixed(2)} <span className="text-sm font-normal text-indigo-400">ج.م</span>
|
| 92 |
-
</span>
|
| 93 |
-
</td>
|
| 94 |
-
<td className="p-4 text-center">
|
| 95 |
-
<div className="flex items-center justify-center gap-2">
|
| 96 |
-
<button onClick={() => { setEditProduct(product); setIsModalOpen(true); }} className="p-2.5 text-blue-500 bg-blue-50 rounded-lg hover:bg-blue-600 hover:text-white transition-colors border border-blue-100 shadow-sm" title="تعديل">
|
| 97 |
-
<Edit size={18} />
|
| 98 |
-
</button>
|
| 99 |
-
<DeleteProductButton productId={product._id} />
|
| 100 |
-
</div>
|
| 101 |
-
</td>
|
| 102 |
-
</tr>
|
| 103 |
-
))}
|
| 104 |
-
{filteredProducts.length === 0 && (
|
| 105 |
-
<tr>
|
| 106 |
-
<td colSpan="5" className="p-16 text-center text-slate-400">
|
| 107 |
-
<div className="w-24 h-24 bg-slate-50 rounded-full flex items-center justify-center mx-auto mb-4 border border-slate-100 shadow-sm">
|
| 108 |
-
<Package size={40} className="text-slate-300" />
|
| 109 |
-
</div>
|
| 110 |
-
<p className="text-xl font-bold text-slate-500">لا توجد أصناف تطابق بحثك أو المخزن فارغ</p>
|
| 111 |
-
</td>
|
| 112 |
-
</tr>
|
| 113 |
-
)}
|
| 114 |
-
</tbody>
|
| 115 |
-
</table>
|
| 116 |
-
</div>
|
| 117 |
-
</div>
|
| 118 |
-
|
| 119 |
-
{isModalOpen && (
|
| 120 |
-
<ProductModal
|
| 121 |
-
product={editProduct}
|
| 122 |
-
onClose={() => setIsModalOpen(false)}
|
| 123 |
-
/>
|
| 124 |
-
)}
|
| 125 |
-
</div>
|
| 126 |
-
);
|
| 127 |
-
};
|
| 128 |
-
|
| 129 |
-
// --- Product Modal Component ---
|
| 130 |
-
const ProductModal = ({ product, onClose }) => {
|
| 131 |
-
const queryClient = useQueryClient();
|
| 132 |
-
|
| 133 |
-
const { mutate, isPending } = useMutation({
|
| 134 |
-
mutationFn: async (data) => {
|
| 135 |
-
if (product) return axios.put(`${apiUrl}/products/${product._id}`, data);
|
| 136 |
-
return axios.post(`${apiUrl}/products`, data);
|
| 137 |
-
},
|
| 138 |
-
onSuccess: () => {
|
| 139 |
-
queryClient.invalidateQueries(["products"]);
|
| 140 |
-
toast.success(product ? "تم تعديل الصنف" : "تم إضافة الصنف للمخزن بنجاح");
|
| 141 |
-
onClose();
|
| 142 |
-
},
|
| 143 |
-
onError: (err) => {
|
| 144 |
-
toast.error(err.response?.data?.message || "حدث خطأ");
|
| 145 |
-
}
|
| 146 |
-
});
|
| 147 |
-
|
| 148 |
-
const formik = useFormik({
|
| 149 |
-
initialValues: {
|
| 150 |
-
name: product?.name || "",
|
| 151 |
-
price: product?.price || "",
|
| 152 |
-
stock: product?.stock || "",
|
| 153 |
-
barcode: product?.barcode || "",
|
| 154 |
-
description: product?.description || ""
|
| 155 |
-
},
|
| 156 |
-
validationSchema: Yup.object({
|
| 157 |
-
name: Yup.string()
|
| 158 |
-
.required('بيان الصنف المطلوب')
|
| 159 |
-
.trim()
|
| 160 |
-
.matches(/^[\u0600-\u06FFa-zA-Z\s]+$/, 'يجب أن يحتوي على حروف فقط'), price: Yup.number().required('مطلوب').typeError('يجب أن يكون رقماً').positive('يجب أن يكون أكبر من 0'),
|
| 161 |
-
stock: Yup.number().required('مطلوب').typeError('يجب أن يكون رقماً').min(0, 'لا يمكن إضافة مخزون سالب'),
|
| 162 |
-
barcode: Yup.string(),
|
| 163 |
-
description: Yup.string()
|
| 164 |
-
}),
|
| 165 |
-
onSubmit: (values) => {
|
| 166 |
-
mutate({
|
| 167 |
-
...values,
|
| 168 |
-
price: Number(values.price),
|
| 169 |
-
stock: Number(values.stock)
|
| 170 |
-
});
|
| 171 |
-
}
|
| 172 |
-
});
|
| 173 |
-
|
| 174 |
-
return (
|
| 175 |
-
<div className="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4">
|
| 176 |
-
<div className="bg-white rounded-3xl w-full max-w-lg shadow-2xl border border-slate-200 animate-in zoom-in-95 duration-200 overflow-hidden">
|
| 177 |
-
<div className="p-6 border-b border-slate-100 bg-slate-50 flex justify-between items-center">
|
| 178 |
-
<h2 className="text-2xl font-black text-slate-800 flex items-center gap-2">
|
| 179 |
-
<Package size={24} className="text-indigo-600" />
|
| 180 |
-
{product ? "تعديل تفاصيل الصنف" : "إضافة صنف جديد للمخزن"}
|
| 181 |
-
</h2>
|
| 182 |
-
<button onClick={onClose} className="w-8 h-8 flex items-center justify-center bg-white text-slate-400 hover:text-rose-500 hover:bg-rose-50 rounded-full transition-colors shadow-sm">×</button>
|
| 183 |
-
</div>
|
| 184 |
-
<form onSubmit={formik.handleSubmit} className="p-6 md:p-8 space-y-7">
|
| 185 |
-
<div className="relative">
|
| 186 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">اسم الصنف بالكامل <span className="text-rose-500">*</span></label>
|
| 187 |
-
<input name="name" type="text" placeholder="مثال: سلك السويدي 10 مم" value={formik.values.name} onChange={formik.handleChange} onBlur={formik.handleBlur} className={`w-full p-3.5 bg-slate-50 border ${formik.touched.name && formik.errors.name ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:border-indigo-500 focus:ring-indigo-500/20'} rounded-xl outline-none focus:ring-2 focus:bg-white transition-colors`} />
|
| 188 |
-
{formik.touched.name && formik.errors.name && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold">{formik.errors.name}</span>}
|
| 189 |
-
</div>
|
| 190 |
-
|
| 191 |
-
<div className="flex flex-col md:flex-row gap-5">
|
| 192 |
-
<div className="w-full md:w-1/2 relative">
|
| 193 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">سعر البيع الافتراضي <span className="text-rose-500">*</span></label>
|
| 194 |
-
<div className="relative">
|
| 195 |
-
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400 font-bold">ج.م</span>
|
| 196 |
-
<input name="price" type="number" step="0.01" value={formik.values.price} onChange={formik.handleChange} onBlur={formik.handleBlur} className={`w-full p-3.5 pl-12 bg-slate-50 border ${formik.touched.price && formik.errors.price ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:border-indigo-500 focus:ring-indigo-500/20'} rounded-xl outline-none focus:ring-2 focus:bg-white transition-colors text-left font-mono font-bold`} dir="ltr" placeholder="0.00" />
|
| 197 |
-
</div>
|
| 198 |
-
{formik.touched.price && formik.errors.price && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold">{formik.errors.price}</span>}
|
| 199 |
-
</div>
|
| 200 |
-
<div className="w-full md:w-1/2 relative">
|
| 201 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">الكمية الافتتاحية للمخزن <span className="text-rose-500">*</span></label>
|
| 202 |
-
<input name="stock" type="number" value={formik.values.stock} onChange={formik.handleChange} onBlur={formik.handleBlur} className={`w-full p-3.5 bg-slate-50 border ${formik.touched.stock && formik.errors.stock ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:border-indigo-500 focus:ring-indigo-500/20'} rounded-xl outline-none focus:ring-2 focus:bg-white transition-colors text-left font-mono font-bold text-indigo-600`} dir="ltr" placeholder="0" />
|
| 203 |
-
{formik.touched.stock && formik.errors.stock && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold">{formik.errors.stock}</span>}
|
| 204 |
-
</div>
|
| 205 |
-
</div>
|
| 206 |
-
|
| 207 |
-
<div className="relative">
|
| 208 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">رقم الباركود (إن وُجد)</label>
|
| 209 |
-
<input name="barcode" type="text" value={formik.values.barcode} onChange={formik.handleChange} onBlur={formik.handleBlur} className="w-full p-3.5 bg-slate-50 border border-slate-200 rounded-xl outline-none focus:border-indigo-500 focus:ring-2 focus:ring-indigo-500/20 focus:bg-white transition-colors text-left font-mono tracking-widest text-slate-600" dir="ltr" placeholder="أو دعه فارغا..." />
|
| 210 |
-
</div>
|
| 211 |
-
|
| 212 |
-
<button disabled={isPending || formik.isSubmitting} type="submit" className="w-full py-4 bg-indigo-600 text-white rounded-xl font-black text-lg hover:bg-indigo-700 disabled:opacity-50 mt-4 shadow-lg shadow-indigo-600/30 transition-all hover:-translate-y-0.5 border border-indigo-500 flex justify-center items-center gap-2">
|
| 213 |
-
{isPending || formik.isSubmitting ? <LodderSpinner /> : "حفظ بيانات الصنف"}
|
| 214 |
-
</button>
|
| 215 |
-
</form>
|
| 216 |
-
</div>
|
| 217 |
-
</div>
|
| 218 |
-
);
|
| 219 |
-
};
|
| 220 |
-
|
| 221 |
-
// --- Delete Button Component ---
|
| 222 |
-
const DeleteProductButton = ({ productId }) => {
|
| 223 |
-
const [isConfirming, setIsConfirming] = useState(false);
|
| 224 |
-
const queryClient = useQueryClient();
|
| 225 |
-
|
| 226 |
-
const { mutate, isPending } = useMutation({
|
| 227 |
-
mutationFn: () => axios.delete(`${apiUrl}/products/${productId}`),
|
| 228 |
-
onSuccess: () => {
|
| 229 |
-
queryClient.invalidateQueries(["products"]);
|
| 230 |
-
toast.success("تم مسح الصنف من المخزن");
|
| 231 |
-
}
|
| 232 |
-
});
|
| 233 |
-
|
| 234 |
-
if (isConfirming) {
|
| 235 |
-
return (
|
| 236 |
-
<div className="flex items-center gap-1.5 bg-rose-50 p-1.5 rounded-xl border border-rose-200 shadow-sm animate-in fade-in slide-in-from-right-1 absolute left-10">
|
| 237 |
-
<span className="text-xs font-bold text-rose-700 px-1">حذف؟</span>
|
| 238 |
-
<button onClick={() => mutate()} disabled={isPending} className="bg-rose-500 text-white px-3 py-1.5 rounded-lg text-xs font-bold hover:bg-rose-600">نعم</button>
|
| 239 |
-
<button onClick={() => setIsConfirming(false)} className="bg-white text-slate-500 border border-slate-200 px-3 py-1.5 rounded-lg text-xs hover:text-slate-800">لا</button>
|
| 240 |
-
</div>
|
| 241 |
-
);
|
| 242 |
-
}
|
| 243 |
-
|
| 244 |
-
return (
|
| 245 |
-
<button onClick={() => setIsConfirming(true)} className="p-2.5 text-rose-500 bg-rose-50 rounded-lg hover:bg-rose-600 hover:text-white transition-colors border border-rose-100 shadow-sm" title="حذف">
|
| 246 |
-
<Trash2 size={18} />
|
| 247 |
-
</button>
|
| 248 |
-
);
|
| 249 |
-
};
|
| 250 |
-
|
| 251 |
-
export default InventoryTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Inventory/InventoryWorkspace.jsx
DELETED
|
@@ -1,400 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
| 4 |
-
import { AlertTriangle, Boxes, Loader2, PackagePlus, Search, Share2, SquarePen, Trash2 } from "lucide-react";
|
| 5 |
-
import { useFormik } from "formik";
|
| 6 |
-
import * as Yup from "yup";
|
| 7 |
-
import { toast } from "react-toastify";
|
| 8 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 9 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 10 |
-
import { getPermissions } from "../../utils/permissions";
|
| 11 |
-
import { formatCurrency, formatNumber } from "../../utils/formatters";
|
| 12 |
-
import { buildLowStockMessage, openWhatsAppMessage } from "../../utils/whatsapp";
|
| 13 |
-
import Pagination from "../../common/Pagination";
|
| 14 |
-
|
| 15 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 16 |
-
const EMPTY_LIST = [];
|
| 17 |
-
|
| 18 |
-
const InventoryWorkspace = () => {
|
| 19 |
-
const { user } = useAuth();
|
| 20 |
-
const permissions = getPermissions(user);
|
| 21 |
-
const currency = user?.tenant?.settings?.currency || "EGP";
|
| 22 |
-
const companyName = user?.tenant?.name || "الشركة";
|
| 23 |
-
const queryClient = useQueryClient();
|
| 24 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 25 |
-
const [page, setPage] = useState(1);
|
| 26 |
-
const [filterMode, setFilterMode] = useState("all");
|
| 27 |
-
const [isModalOpen, setIsModalOpen] = useState(false);
|
| 28 |
-
const [editingProduct, setEditingProduct] = useState(null);
|
| 29 |
-
|
| 30 |
-
const { data: response, isLoading } = useQuery({
|
| 31 |
-
queryKey: ["products", page, searchTerm, filterMode],
|
| 32 |
-
queryFn: () => axios.get(`${apiUrl}/products`, {
|
| 33 |
-
params: {
|
| 34 |
-
page,
|
| 35 |
-
limit: 5,
|
| 36 |
-
search: searchTerm,
|
| 37 |
-
filter: filterMode
|
| 38 |
-
}
|
| 39 |
-
}),
|
| 40 |
-
refetchOnWindowFocus: false,
|
| 41 |
-
});
|
| 42 |
-
|
| 43 |
-
const mutation = useMutation({
|
| 44 |
-
mutationFn: ({ id, values }) =>
|
| 45 |
-
id ? axios.put(`${apiUrl}/products/${id}`, values) : axios.post(`${apiUrl}/products`, values),
|
| 46 |
-
onSuccess: () => {
|
| 47 |
-
queryClient.invalidateQueries({ queryKey: ["products"] });
|
| 48 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 49 |
-
toast.success(editingProduct ? "تم تحديث المنتج" : "تم إنشاء المنتج");
|
| 50 |
-
setIsModalOpen(false);
|
| 51 |
-
setEditingProduct(null);
|
| 52 |
-
},
|
| 53 |
-
onError: (error) => {
|
| 54 |
-
toast.error(error.response?.data?.message || "تعذر حفظ المنتج");
|
| 55 |
-
},
|
| 56 |
-
});
|
| 57 |
-
|
| 58 |
-
const deleteMutation = useMutation({
|
| 59 |
-
mutationFn: (productId) => axios.delete(`${apiUrl}/products/${productId}`),
|
| 60 |
-
onSuccess: () => {
|
| 61 |
-
queryClient.invalidateQueries({ queryKey: ["products"] });
|
| 62 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 63 |
-
toast.success("تم حذف المنتج");
|
| 64 |
-
},
|
| 65 |
-
onError: (error) => {
|
| 66 |
-
toast.error(error.response?.data?.message || "تعذر حذف المنتج");
|
| 67 |
-
},
|
| 68 |
-
});
|
| 69 |
-
|
| 70 |
-
const products = response?.data?.data ?? EMPTY_LIST;
|
| 71 |
-
const pagination = response?.data?.pagination || { total: 0, totalPages: 0 };
|
| 72 |
-
|
| 73 |
-
// We still need all low stock items for the alert section and WhatsApp sharing.
|
| 74 |
-
// Ideally, this should be a separate query or the backend should return it.
|
| 75 |
-
// For now, I'll fetch them separately or assume the alert section shows the first few from the current page/filter.
|
| 76 |
-
// But the alert section says "راجع هذه الأصناف أولاً", which usually means overall low stock.
|
| 77 |
-
|
| 78 |
-
const { data: allProductsResponse } = useQuery({
|
| 79 |
-
queryKey: ["products", "all-low-stock"],
|
| 80 |
-
queryFn: () => axios.get(`${apiUrl}/products`, { params: { limit: 1000, filter: "low" } }),
|
| 81 |
-
refetchOnWindowFocus: false,
|
| 82 |
-
});
|
| 83 |
-
const lowStockItems = allProductsResponse?.data?.data || [];
|
| 84 |
-
|
| 85 |
-
const handleSearchChange = (e) => {
|
| 86 |
-
setSearchTerm(e.target.value);
|
| 87 |
-
setPage(1);
|
| 88 |
-
};
|
| 89 |
-
|
| 90 |
-
const handleFilterChange = (mode) => {
|
| 91 |
-
setFilterMode(mode);
|
| 92 |
-
setPage(1);
|
| 93 |
-
};
|
| 94 |
-
|
| 95 |
-
if (isLoading) {
|
| 96 |
-
return <LodderSpinner />;
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
return (
|
| 100 |
-
<div className="space-y-6">
|
| 101 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 102 |
-
<div className="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
|
| 103 |
-
<div>
|
| 104 |
-
<h3 className="text-xl font-black text-slate-900">إدارة المخزون</h3>
|
| 105 |
-
<p className="text-sm text-slate-500">تتبع مستويات المخزون وأسعار البيع ومخاطر النفاد.</p>
|
| 106 |
-
</div>
|
| 107 |
-
<div className="flex flex-wrap items-center gap-3">
|
| 108 |
-
<button
|
| 109 |
-
type="button"
|
| 110 |
-
onClick={() => openWhatsAppMessage(buildLowStockMessage({ companyName, items: lowStockItems.slice(0, 10) }))}
|
| 111 |
-
disabled={!lowStockItems.length}
|
| 112 |
-
className="flex h-11 items-center justify-center gap-2 rounded-2xl border border-amber-200 bg-amber-50 px-4 font-bold text-amber-700 transition hover:bg-amber-100 disabled:opacity-40"
|
| 113 |
-
>
|
| 114 |
-
<Share2 size={16} />
|
| 115 |
-
مشاركة قائمة النقص
|
| 116 |
-
</button>
|
| 117 |
-
{permissions.canManageInventory ? (
|
| 118 |
-
<button
|
| 119 |
-
type="button"
|
| 120 |
-
onClick={() => {
|
| 121 |
-
setEditingProduct(null);
|
| 122 |
-
setIsModalOpen(true);
|
| 123 |
-
}}
|
| 124 |
-
className="flex h-11 items-center justify-center gap-2 rounded-2xl bg-indigo-600 px-4 font-bold text-white transition hover:bg-indigo-700"
|
| 125 |
-
>
|
| 126 |
-
<PackagePlus size={16} />
|
| 127 |
-
إضافة منتج
|
| 128 |
-
</button>
|
| 129 |
-
) : null}
|
| 130 |
-
</div>
|
| 131 |
-
</div>
|
| 132 |
-
|
| 133 |
-
<div className="mt-6 grid gap-4 md:grid-cols-3">
|
| 134 |
-
<StatCard label="المنتجات" value={formatNumber(pagination.total)} />
|
| 135 |
-
<StatCard label="تنبيهات نقص المخزون" value={formatNumber(lowStockItems.length)} alert />
|
| 136 |
-
<StatCard
|
| 137 |
-
label="القيمة الإجمالية للمخزون"
|
| 138 |
-
value={formatCurrency(products.reduce((sum, product) => sum + Number(product.price || 0) * Number(product.stock || 0), 0), currency)}
|
| 139 |
-
/>
|
| 140 |
-
</div>
|
| 141 |
-
</section>
|
| 142 |
-
|
| 143 |
-
{lowStockItems.length ? (
|
| 144 |
-
<section className="rounded-3xl border border-amber-200 bg-amber-50 p-6 shadow-sm">
|
| 145 |
-
<div className="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
|
| 146 |
-
<div className="flex items-start gap-3">
|
| 147 |
-
<div className="mt-1 flex h-11 w-11 items-center justify-center rounded-2xl bg-white text-amber-700">
|
| 148 |
-
<AlertTriangle size={20} />
|
| 149 |
-
</div>
|
| 150 |
-
<div>
|
| 151 |
-
<h3 className="text-lg font-black text-slate-900">سير عمل المخزون المنخفض</h3>
|
| 152 |
-
<p className="text-sm text-slate-600">راجع هذه الأصناف أولاً، حدّث حدود المخزون، وشارك قائمة إعادة الطلب مع فريق المشتريات.</p>
|
| 153 |
-
</div>
|
| 154 |
-
</div>
|
| 155 |
-
<div className="flex flex-wrap gap-2">
|
| 156 |
-
<FilterChip active={filterMode === "all"} label="الكل" onClick={() => handleFilterChange("all")} />
|
| 157 |
-
<FilterChip active={filterMode === "low"} label="مخزون منخفض فقط" onClick={() => handleFilterChange("low")} />
|
| 158 |
-
<FilterChip active={filterMode === "available"} label="متوفر" onClick={() => handleFilterChange("available")} />
|
| 159 |
-
</div>
|
| 160 |
-
</div>
|
| 161 |
-
|
| 162 |
-
<div className="mt-5 grid gap-3 md:grid-cols-2 xl:grid-cols-3">
|
| 163 |
-
{lowStockItems.slice(0, 6).map((item) => (
|
| 164 |
-
<div key={item._id} className="rounded-2xl border border-amber-200 bg-white p-4">
|
| 165 |
-
<div className="flex items-center justify-between gap-3">
|
| 166 |
-
<div>
|
| 167 |
-
<p className="font-bold text-slate-900">{item.name}</p>
|
| 168 |
-
<p className="text-xs text-slate-500">الحد الأدنى {item.lowStockThreshold} {item.unit || "وحدة"}</p>
|
| 169 |
-
</div>
|
| 170 |
-
<span className="rounded-full bg-amber-50 px-3 py-1 text-sm font-black text-amber-700">
|
| 171 |
-
متبقي {item.stock}
|
| 172 |
-
</span>
|
| 173 |
-
</div>
|
| 174 |
-
</div>
|
| 175 |
-
))}
|
| 176 |
-
</div>
|
| 177 |
-
</section>
|
| 178 |
-
) : null}
|
| 179 |
-
|
| 180 |
-
<section className="rounded-3xl border border-slate-200 bg-white shadow-sm">
|
| 181 |
-
<div className="flex flex-col gap-4 border-b border-slate-100 p-5 md:flex-row md:items-center md:justify-between">
|
| 182 |
-
<div className="relative w-full md:max-w-sm">
|
| 183 |
-
<Search size={18} className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400" />
|
| 184 |
-
<input
|
| 185 |
-
type="text"
|
| 186 |
-
value={searchTerm}
|
| 187 |
-
onChange={handleSearchChange}
|
| 188 |
-
placeholder="البحث في المنتجات..."
|
| 189 |
-
className="h-11 w-full rounded-2xl border border-slate-200 bg-slate-50 pr-11 pl-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-indigo-500"
|
| 190 |
-
/>
|
| 191 |
-
</div>
|
| 192 |
-
<div className="rounded-2xl bg-slate-50 px-4 py-2 text-sm font-bold text-slate-600">
|
| 193 |
-
{pagination.total} عنصر مطابق
|
| 194 |
-
</div>
|
| 195 |
-
</div>
|
| 196 |
-
|
| 197 |
-
<div className="overflow-x-auto">
|
| 198 |
-
<table className="min-w-full border-collapse">
|
| 199 |
-
<thead>
|
| 200 |
-
<tr className="border-b border-slate-200 text-right text-sm font-bold text-slate-500">
|
| 201 |
-
<th className="px-4 py-3">المنتج</th>
|
| 202 |
-
<th className="px-4 py-3">المخزون</th>
|
| 203 |
-
<th className="px-4 py-3">الحد الأدنى</th>
|
| 204 |
-
<th className="px-4 py-3">سعر الوحدة</th>
|
| 205 |
-
<th className="px-4 py-3">الحالة</th>
|
| 206 |
-
<th className="px-4 py-3">الإجراءات</th>
|
| 207 |
-
</tr>
|
| 208 |
-
</thead>
|
| 209 |
-
<tbody>
|
| 210 |
-
{products.length ? (
|
| 211 |
-
products.map((product) => (
|
| 212 |
-
<tr key={product._id} className="border-b border-slate-100 text-sm text-slate-700">
|
| 213 |
-
<td className="px-4 py-4">
|
| 214 |
-
<div className="flex items-center gap-3">
|
| 215 |
-
<div className="flex h-10 w-10 items-center justify-center rounded-2xl bg-indigo-50 text-indigo-700">
|
| 216 |
-
<Boxes size={18} />
|
| 217 |
-
</div>
|
| 218 |
-
<div>
|
| 219 |
-
<p className="font-bold text-slate-900">{product.name}</p>
|
| 220 |
-
<p className="text-xs text-slate-500">{product.description || product.barcode || "لا توجد تفاصيل إضافية"}</p>
|
| 221 |
-
</div>
|
| 222 |
-
</div>
|
| 223 |
-
</td>
|
| 224 |
-
<td className="px-4 py-4 font-bold">{formatNumber(product.stock)}</td>
|
| 225 |
-
<td className="px-4 py-4">{formatNumber(product.lowStockThreshold)}</td>
|
| 226 |
-
<td className="px-4 py-4 font-bold">{formatCurrency(product.price, currency)}</td>
|
| 227 |
-
<td className="px-4 py-4">
|
| 228 |
-
<span className={`rounded-full px-3 py-1 text-xs font-bold ${product.isLowStock ? "bg-amber-50 text-amber-700" : "bg-emerald-50 text-emerald-700"}`}>
|
| 229 |
-
{product.isLowStock ? "يحتاج إعادة طلب" : "مخزون سليم"}
|
| 230 |
-
</span>
|
| 231 |
-
</td>
|
| 232 |
-
<td className="px-4 py-4">
|
| 233 |
-
{permissions.canManageInventory ? (
|
| 234 |
-
<div className="flex items-center gap-2">
|
| 235 |
-
<button
|
| 236 |
-
type="button"
|
| 237 |
-
onClick={() => {
|
| 238 |
-
setEditingProduct(product);
|
| 239 |
-
setIsModalOpen(true);
|
| 240 |
-
}}
|
| 241 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl border border-blue-100 bg-blue-50 px-3 font-bold text-blue-700 transition hover:bg-blue-100"
|
| 242 |
-
>
|
| 243 |
-
<SquarePen size={16} />
|
| 244 |
-
تعديل
|
| 245 |
-
</button>
|
| 246 |
-
<button
|
| 247 |
-
type="button"
|
| 248 |
-
onClick={() => deleteMutation.mutate(product._id)}
|
| 249 |
-
disabled={deleteMutation.isPending}
|
| 250 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl border border-rose-100 bg-rose-50 px-3 font-bold text-rose-700 transition hover:bg-rose-100 disabled:opacity-60"
|
| 251 |
-
>
|
| 252 |
-
<Trash2 size={16} />
|
| 253 |
-
حذف
|
| 254 |
-
</button>
|
| 255 |
-
</div>
|
| 256 |
-
) : (
|
| 257 |
-
<span className="text-xs font-bold text-slate-400">عرض فقط</span>
|
| 258 |
-
)}
|
| 259 |
-
</td>
|
| 260 |
-
</tr>
|
| 261 |
-
))
|
| 262 |
-
) : (
|
| 263 |
-
<tr>
|
| 264 |
-
<td colSpan="6" className="px-4 py-12 text-center text-sm font-medium text-slate-500">
|
| 265 |
-
لا توجد منتجات مطابقة لهذا البحث.
|
| 266 |
-
</td>
|
| 267 |
-
</tr>
|
| 268 |
-
)}
|
| 269 |
-
</tbody>
|
| 270 |
-
</table>
|
| 271 |
-
</div>
|
| 272 |
-
|
| 273 |
-
<Pagination
|
| 274 |
-
currentPage={page}
|
| 275 |
-
totalPages={pagination.totalPages}
|
| 276 |
-
onPageChange={setPage}
|
| 277 |
-
totalItems={pagination.total}
|
| 278 |
-
limit={5}
|
| 279 |
-
/>
|
| 280 |
-
</section>
|
| 281 |
-
|
| 282 |
-
{isModalOpen ? (
|
| 283 |
-
<ProductModal
|
| 284 |
-
product={editingProduct}
|
| 285 |
-
onClose={() => {
|
| 286 |
-
setIsModalOpen(false);
|
| 287 |
-
setEditingProduct(null);
|
| 288 |
-
}}
|
| 289 |
-
onSubmit={(values) => mutation.mutate({ id: editingProduct?._id, values })}
|
| 290 |
-
isPending={mutation.isPending}
|
| 291 |
-
/>
|
| 292 |
-
) : null}
|
| 293 |
-
</div>
|
| 294 |
-
);
|
| 295 |
-
};
|
| 296 |
-
|
| 297 |
-
const ProductModal = ({ product, onClose, onSubmit, isPending }) => {
|
| 298 |
-
const formik = useFormik({
|
| 299 |
-
enableReinitialize: true,
|
| 300 |
-
initialValues: {
|
| 301 |
-
name: product?.name || "",
|
| 302 |
-
description: product?.description || "",
|
| 303 |
-
barcode: product?.barcode || "",
|
| 304 |
-
price: product?.price || "",
|
| 305 |
-
costPrice: product?.costPrice || "",
|
| 306 |
-
stock: product?.stock || "",
|
| 307 |
-
lowStockThreshold: product?.lowStockThreshold || 5,
|
| 308 |
-
},
|
| 309 |
-
validationSchema: Yup.object({
|
| 310 |
-
name: Yup.string().required("اسم المنتج مطلوب"),
|
| 311 |
-
price: Yup.number().required("السعر مطلوب").min(0, "لا يمكن أن يكون السعر سالباً"),
|
| 312 |
-
stock: Yup.number().required("المخزون مطلوب").min(0, "لا يمكن أن يكون المخزون سالباً"),
|
| 313 |
-
lowStockThreshold: Yup.number().required("الحد الأدنى مطلوب").min(1, "الحد الأدنى 1"),
|
| 314 |
-
}),
|
| 315 |
-
onSubmit: (values) =>
|
| 316 |
-
onSubmit({
|
| 317 |
-
...values,
|
| 318 |
-
price: Number(values.price),
|
| 319 |
-
costPrice: Number(values.costPrice || 0),
|
| 320 |
-
stock: Number(values.stock),
|
| 321 |
-
lowStockThreshold: Number(values.lowStockThreshold),
|
| 322 |
-
}),
|
| 323 |
-
});
|
| 324 |
-
|
| 325 |
-
return (
|
| 326 |
-
<div className="fixed inset-0 z-50 flex items-center justify-center bg-slate-950/50 p-4" dir="rtl">
|
| 327 |
-
<div className="w-full max-w-2xl rounded-3xl border border-slate-200 bg-white p-6 shadow-2xl">
|
| 328 |
-
<div className="mb-6 flex items-center justify-between">
|
| 329 |
-
<div>
|
| 330 |
-
<h3 className="text-xl font-black text-slate-900">{product ? "تعديل المنتج" : "إنشاء منتج"}</h3>
|
| 331 |
-
<p className="text-sm text-slate-500">تحديث المخزون والأسعار والحدود الدنيا.</p>
|
| 332 |
-
</div>
|
| 333 |
-
<button type="button" onClick={onClose} className="rounded-2xl border border-slate-200 px-4 py-2 text-sm font-bold text-slate-600">
|
| 334 |
-
إغلاق
|
| 335 |
-
</button>
|
| 336 |
-
</div>
|
| 337 |
-
|
| 338 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4 md:grid-cols-2">
|
| 339 |
-
<ModalField label="الاسم" name="name" formik={formik} />
|
| 340 |
-
<ModalField label="الباركود" name="barcode" formik={formik} />
|
| 341 |
-
<div className="md:col-span-2">
|
| 342 |
-
<ModalField label="الوصف" name="description" formik={formik} />
|
| 343 |
-
</div>
|
| 344 |
-
<ModalField label="السعر" name="price" formik={formik} type="number" />
|
| 345 |
-
<ModalField label="سعر التكلفة" name="costPrice" formik={formik} type="number" />
|
| 346 |
-
<ModalField label="المخزون" name="stock" formik={formik} type="number" />
|
| 347 |
-
<ModalField label="الحد الأدنى للمخزون" name="lowStockThreshold" formik={formik} type="number" />
|
| 348 |
-
<button
|
| 349 |
-
type="submit"
|
| 350 |
-
disabled={isPending}
|
| 351 |
-
className="md:col-span-2 flex h-12 items-center justify-center gap-2 rounded-2xl bg-indigo-600 font-bold text-white transition hover:bg-indigo-700 disabled:opacity-60"
|
| 352 |
-
>
|
| 353 |
-
{isPending ? <Loader2 size={18} className="animate-spin" /> : null}
|
| 354 |
-
حفظ المنتج
|
| 355 |
-
</button>
|
| 356 |
-
</form>
|
| 357 |
-
</div>
|
| 358 |
-
</div>
|
| 359 |
-
);
|
| 360 |
-
};
|
| 361 |
-
|
| 362 |
-
const ModalField = ({ label, name, formik, type = "text" }) => (
|
| 363 |
-
<div>
|
| 364 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">{label}</label>
|
| 365 |
-
<input
|
| 366 |
-
name={name}
|
| 367 |
-
type={type}
|
| 368 |
-
value={formik.values[name]}
|
| 369 |
-
onChange={formik.handleChange}
|
| 370 |
-
onBlur={formik.handleBlur}
|
| 371 |
-
className={`h-12 w-full rounded-2xl border bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-indigo-500 ${
|
| 372 |
-
formik.touched[name] && formik.errors[name] ? "border-rose-400" : "border-slate-200"
|
| 373 |
-
}`}
|
| 374 |
-
/>
|
| 375 |
-
{formik.touched[name] && formik.errors[name] ? (
|
| 376 |
-
<p className="mt-1 text-xs font-bold text-rose-500">{formik.errors[name]}</p>
|
| 377 |
-
) : null}
|
| 378 |
-
</div>
|
| 379 |
-
);
|
| 380 |
-
|
| 381 |
-
const StatCard = ({ label, value, alert = false }) => (
|
| 382 |
-
<div className={`rounded-2xl border p-4 ${alert ? "border-amber-100 bg-amber-50" : "border-slate-100 bg-slate-50"}`}>
|
| 383 |
-
<p className="text-sm font-bold text-slate-500">{label}</p>
|
| 384 |
-
<p className={`mt-2 text-2xl font-black ${alert ? "text-amber-700" : "text-slate-900"}`}>{value}</p>
|
| 385 |
-
</div>
|
| 386 |
-
);
|
| 387 |
-
|
| 388 |
-
const FilterChip = ({ active, label, onClick }) => (
|
| 389 |
-
<button
|
| 390 |
-
type="button"
|
| 391 |
-
onClick={onClick}
|
| 392 |
-
className={`rounded-2xl px-4 py-2 text-sm font-bold transition ${
|
| 393 |
-
active ? "bg-slate-900 text-white" : "bg-white text-slate-600 hover:bg-slate-100"
|
| 394 |
-
}`}
|
| 395 |
-
>
|
| 396 |
-
{label}
|
| 397 |
-
</button>
|
| 398 |
-
);
|
| 399 |
-
|
| 400 |
-
export default InventoryWorkspace;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/InvoiceItemsModal/DeleteItemAtInvoice.jsx
DELETED
|
@@ -1,73 +0,0 @@
|
|
| 1 |
-
import axios from "axios";
|
| 2 |
-
import { Trash2Icon, Loader2, AlertTriangle, X } from "lucide-react";
|
| 3 |
-
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
| 4 |
-
import { toast } from "react-toastify";
|
| 5 |
-
import { useState } from "react";
|
| 6 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 7 |
-
|
| 8 |
-
const DeleteInvoiceItemButton = ({ customerId, invoiceId, itemId }) => {
|
| 9 |
-
const queryClient = useQueryClient();
|
| 10 |
-
const [isConfirming, setIsConfirming] = useState(false);
|
| 11 |
-
|
| 12 |
-
const { mutate, isPending } = useMutation({
|
| 13 |
-
mutationFn: () =>
|
| 14 |
-
axios.delete(
|
| 15 |
-
`${apiUrl}/invoices/${invoiceId}/${itemId}`
|
| 16 |
-
),
|
| 17 |
-
|
| 18 |
-
onSuccess: () => {
|
| 19 |
-
queryClient.invalidateQueries({
|
| 20 |
-
queryKey: ["customer", customerId],
|
| 21 |
-
});
|
| 22 |
-
toast.success("تم حذف البند بنجاح 🗑️");
|
| 23 |
-
setIsConfirming(false);
|
| 24 |
-
},
|
| 25 |
-
onError: () => {
|
| 26 |
-
toast.error("حدث خطأ أثناء محاولة الحذف");
|
| 27 |
-
setIsConfirming(false);
|
| 28 |
-
}
|
| 29 |
-
});
|
| 30 |
-
|
| 31 |
-
const handleDelete = () => {
|
| 32 |
-
mutate();
|
| 33 |
-
};
|
| 34 |
-
|
| 35 |
-
// Render inline confirmation
|
| 36 |
-
if (isConfirming) {
|
| 37 |
-
return (
|
| 38 |
-
<div className="flex items-center gap-2 bg-rose-50 border border-rose-200 p-1.5 rounded-xl shadow-sm w-max transition-all">
|
| 39 |
-
<span className="text-xs font-bold text-rose-800 mx-1 flex items-center gap-1">
|
| 40 |
-
<AlertTriangle size={14} className="text-rose-500" /> هل أنت متأكد؟
|
| 41 |
-
</span>
|
| 42 |
-
<button
|
| 43 |
-
onClick={handleDelete}
|
| 44 |
-
disabled={isPending}
|
| 45 |
-
className="px-3 py-1.5 bg-rose-600 text-white rounded-lg text-xs font-bold hover:bg-rose-700 disabled:opacity-50 transition-colors flex items-center gap-1 shadow-sm"
|
| 46 |
-
title="قمت بتأكيد الحذف"
|
| 47 |
-
>
|
| 48 |
-
{isPending ? <Loader2 size={14} className="animate-spin" /> : "نعم، احذف"}
|
| 49 |
-
</button>
|
| 50 |
-
<button
|
| 51 |
-
onClick={() => setIsConfirming(false)}
|
| 52 |
-
disabled={isPending}
|
| 53 |
-
className="p-1.5 bg-white text-slate-500 rounded-lg hover:bg-slate-100 hover:text-slate-800 transition-colors border border-slate-200 shadow-sm"
|
| 54 |
-
title="إلغاء الحذف"
|
| 55 |
-
>
|
| 56 |
-
<X size={14} />
|
| 57 |
-
</button>
|
| 58 |
-
</div>
|
| 59 |
-
);
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
return (
|
| 63 |
-
<button
|
| 64 |
-
onClick={() => setIsConfirming(true)}
|
| 65 |
-
className="p-2.5 text-rose-600 bg-rose-50 border border-rose-100 rounded-lg hover:bg-rose-600 hover:text-white hover:border-rose-600 transition-colors cursor-pointer shadow-sm tooltip"
|
| 66 |
-
title="حذف الصنف نهائياً"
|
| 67 |
-
>
|
| 68 |
-
<Trash2Icon size={18} />
|
| 69 |
-
</button>
|
| 70 |
-
);
|
| 71 |
-
};
|
| 72 |
-
|
| 73 |
-
export default DeleteInvoiceItemButton;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/InvoiceItemsModal/InvoiceItemsModal.jsx
DELETED
|
@@ -1,308 +0,0 @@
|
|
| 1 |
-
import { X, Trash2, Plus, Receipt, Box, Hash, Banknote, Loader2, Calculator } from "lucide-react";
|
| 2 |
-
import { useMemo } from "react";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
| 5 |
-
import { toast } from "react-toastify";
|
| 6 |
-
import { useFormik } from "formik";
|
| 7 |
-
import * as Yup from "yup";
|
| 8 |
-
|
| 9 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 10 |
-
|
| 11 |
-
const InvoiceItemsModal = ({ customerId, invoiceId, onClose }) => {
|
| 12 |
-
const queryClient = useQueryClient();
|
| 13 |
-
|
| 14 |
-
const { data: productsData } = useQuery({
|
| 15 |
-
queryKey: ["products", "all"],
|
| 16 |
-
queryFn: () => axios.get(`${apiUrl}/products`, { params: { limit: 1000 } }),
|
| 17 |
-
});
|
| 18 |
-
const products = productsData?.data?.data || [];
|
| 19 |
-
|
| 20 |
-
const formik = useFormik({
|
| 21 |
-
initialValues: {
|
| 22 |
-
items: [{ name: "", quantity: "", price: "", productId: null }],
|
| 23 |
-
paymentType: 'unpaid',
|
| 24 |
-
partialAmount: ''
|
| 25 |
-
},
|
| 26 |
-
validationSchema: Yup.object({
|
| 27 |
-
items: Yup.array().of(
|
| 28 |
-
Yup.object().shape({
|
| 29 |
-
name: Yup.string()
|
| 30 |
-
.required('مطلوب')
|
| 31 |
-
.trim()
|
| 32 |
-
.matches(/^[\u0600-\u06FFa-zA-Z\s]+$/, 'يجب أن يحتوي على حروف فقط'), quantity: Yup.number()
|
| 33 |
-
.required('مطلوب')
|
| 34 |
-
.typeError('أرقام فقط')
|
| 35 |
-
.positive('أكبر من 0')
|
| 36 |
-
.test('max-stock', '', function(value) {
|
| 37 |
-
const { productId } = this.parent;
|
| 38 |
-
const product = products.find(p => p._id === productId);
|
| 39 |
-
if (product && value > product.stock) {
|
| 40 |
-
return this.createError({ message: `الرصيد المتاح: ${product.stock}` });
|
| 41 |
-
}
|
| 42 |
-
return true;
|
| 43 |
-
}),
|
| 44 |
-
price: Yup.number().required('مطلوب').typeError('أرقام فقط').min(0, 'غير صالح')
|
| 45 |
-
})
|
| 46 |
-
),
|
| 47 |
-
paymentType: Yup.string(),
|
| 48 |
-
partialAmount: Yup.number().when('paymentType', {
|
| 49 |
-
is: 'partial',
|
| 50 |
-
then: (schema) => schema.required('مطلوب').positive('أكبر من 0'),
|
| 51 |
-
otherwise: (schema) => schema.notRequired()
|
| 52 |
-
})
|
| 53 |
-
}),
|
| 54 |
-
onSubmit: async (values, { setSubmitting }) => {
|
| 55 |
-
const filteredItems = values.items;
|
| 56 |
-
|
| 57 |
-
const grandTotalCalc = filteredItems.reduce((sum, item) => sum + (Number(item.quantity || 0) * Number(item.price || 0)), 0);
|
| 58 |
-
let amountToPay = 0;
|
| 59 |
-
|
| 60 |
-
if (values.paymentType === 'full') {
|
| 61 |
-
amountToPay = grandTotalCalc;
|
| 62 |
-
} else if (values.paymentType === 'partial') {
|
| 63 |
-
amountToPay = Number(values.partialAmount);
|
| 64 |
-
if (amountToPay > grandTotalCalc) {
|
| 65 |
-
toast.error("لا يمكن دفع مبلغ أكبر من إجمالي الفاتورة");
|
| 66 |
-
setSubmitting(false);
|
| 67 |
-
return;
|
| 68 |
-
}
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
try {
|
| 72 |
-
await axios.post(`${apiUrl}/invoices/${customerId}/items`, { items: filteredItems });
|
| 73 |
-
if (amountToPay > 0) {
|
| 74 |
-
await axios.post(`${apiUrl}/invoices/${customerId}/pay`, { amount: amountToPay });
|
| 75 |
-
}
|
| 76 |
-
if (invoiceId) {
|
| 77 |
-
queryClient.invalidateQueries({ queryKey: ["invoice", invoiceId] });
|
| 78 |
-
}
|
| 79 |
-
queryClient.invalidateQueries({ queryKey: ["customers"] });
|
| 80 |
-
queryClient.invalidateQueries({ queryKey: ["customer", customerId] });
|
| 81 |
-
queryClient.invalidateQueries({ queryKey: ["products"] });
|
| 82 |
-
toast.success("تم تسجيل الفاتورة وتفريغ المخزون بنجاح");
|
| 83 |
-
onClose();
|
| 84 |
-
} catch (err) {
|
| 85 |
-
console.log(err)
|
| 86 |
-
toast.error(err.response?.data?.message || err.message || "حدث خطأ أثناء الحفظ");
|
| 87 |
-
} finally {
|
| 88 |
-
setSubmitting(false);
|
| 89 |
-
}
|
| 90 |
-
}
|
| 91 |
-
});
|
| 92 |
-
|
| 93 |
-
const { items, paymentType, partialAmount } = formik.values;
|
| 94 |
-
|
| 95 |
-
const grandTotal = useMemo(() => {
|
| 96 |
-
return items.reduce((sum, item) => sum + (Number(item.quantity || 0) * Number(item.price || 0)), 0);
|
| 97 |
-
}, [items]);
|
| 98 |
-
|
| 99 |
-
const addItemRow = () => {
|
| 100 |
-
formik.setFieldValue('items', [...items, { name: "", quantity: "", price: "", productId: null }]);
|
| 101 |
-
};
|
| 102 |
-
|
| 103 |
-
const removeItemRow = (index) => {
|
| 104 |
-
if (items.length === 1) return;
|
| 105 |
-
formik.setFieldValue('items', items.filter((_, i) => i !== index));
|
| 106 |
-
};
|
| 107 |
-
|
| 108 |
-
return (
|
| 109 |
-
<div className="fixed inset-0 bg-slate-900/40 backdrop-blur-sm flex items-center justify-center z-50 p-2 md:p-4" dir="rtl">
|
| 110 |
-
<div className="bg-white w-full max-w-4xl rounded-2xl shadow-2xl border border-slate-200 overflow-hidden flex flex-col max-h-[95vh] md:max-h-[90vh]">
|
| 111 |
-
|
| 112 |
-
{/* Header */}
|
| 113 |
-
<div className="bg-slate-50 border-b border-slate-200 p-4 md:p-6 flex items-center justify-between relative shrink-0">
|
| 114 |
-
<div className="absolute top-0 right-0 w-1.5 h-full bg-amber-500"></div>
|
| 115 |
-
<div className="flex items-center gap-3 md:gap-4">
|
| 116 |
-
<div className="p-3 border border-amber-200 bg-amber-50 text-amber-600 rounded-xl shadow-sm hidden md:block">
|
| 117 |
-
<Receipt size={24} />
|
| 118 |
-
</div>
|
| 119 |
-
<div>
|
| 120 |
-
<h2 className="text-xl md:text-2xl font-bold text-slate-800">إضافة بنود للفاتورة</h2>
|
| 121 |
-
<p className="text-sm text-slate-500 mt-1 hidden md:block">أضف جميع الأصناف المباعة دفعة واحدة لضمها لحساب العميل وتحديث المخزون آلياً.</p>
|
| 122 |
-
</div>
|
| 123 |
-
</div>
|
| 124 |
-
<button onClick={onClose} className="p-2 text-slate-400 hover:text-rose-500 hover:bg-rose-50 rounded-full transition-colors cursor-pointer" title="إغلاق"><X size={26} /></button>
|
| 125 |
-
</div>
|
| 126 |
-
|
| 127 |
-
{/* Body */}
|
| 128 |
-
<div className="p-3 md:p-6 overflow-y-auto custom-scroll flex-1 bg-slate-50/50">
|
| 129 |
-
<div className="space-y-4">
|
| 130 |
-
{/* Labels Header Form - Only on Desktop */}
|
| 131 |
-
<div className="hidden md:flex items-center px-4 pt-2 text-slate-500 font-bold text-sm">
|
| 132 |
-
<div className="w-5/12 flex items-center gap-1.5"><Box size={16}/> اسم البند</div>
|
| 133 |
-
<div className="w-2/12 flex items-center gap-1.5 justify-center"><Hash size={16}/> الكمية</div>
|
| 134 |
-
<div className="w-2/12 flex items-center gap-1.5 justify-center"><Banknote size={16}/> السعر (ج.م)</div>
|
| 135 |
-
<div className="w-2/12 flex items-center gap-1.5 justify-center text-blue-600"><Calculator size={16}/> إجمالي البند</div>
|
| 136 |
-
<div className="w-1/12 text-center">حذف</div>
|
| 137 |
-
</div>
|
| 138 |
-
|
| 139 |
-
{/* Items rows */}
|
| 140 |
-
<div className="space-y-4 md:space-y-2">
|
| 141 |
-
{items.map((item, index) => {
|
| 142 |
-
const itemTotal = Number(item.quantity || 0) * Number(item.price || 0);
|
| 143 |
-
const errors = formik.errors.items?.[index] || {};
|
| 144 |
-
const touched = formik.touched.items?.[index] || {};
|
| 145 |
-
return (
|
| 146 |
-
<div key={index} className="flex flex-col md:flex-row items-start md:items-center bg-white border border-slate-200 p-3 md:px-4 md:py-3 rounded-2xl shadow-sm gap-2 md:gap-3 transition-colors hover:border-amber-200 group">
|
| 147 |
-
{/* Name */}
|
| 148 |
-
<div className="w-full md:w-5/12 relative">
|
| 149 |
-
<label className="md:hidden block text-sm font-bold text-slate-600 mb-1.5">اسم الصنف</label>
|
| 150 |
-
<input
|
| 151 |
-
list={`products-list-${index}`}
|
| 152 |
-
type="text"
|
| 153 |
-
name={`items[${index}].name`}
|
| 154 |
-
placeholder="ابحث عن الصنف..."
|
| 155 |
-
value={item.name}
|
| 156 |
-
onBlur={formik.handleBlur}
|
| 157 |
-
onChange={(e) => {
|
| 158 |
-
const val = e.target.value;
|
| 159 |
-
formik.setFieldValue(`items[${index}].name`, val);
|
| 160 |
-
const matchedProduct = products.find(p => p.name === val);
|
| 161 |
-
if (matchedProduct) {
|
| 162 |
-
formik.setFieldValue(`items[${index}].price`, matchedProduct.price);
|
| 163 |
-
formik.setFieldValue(`items[${index}].productId`, matchedProduct._id);
|
| 164 |
-
} else {
|
| 165 |
-
formik.setFieldValue(`items[${index}].productId`, null);
|
| 166 |
-
}
|
| 167 |
-
}}
|
| 168 |
-
className={`w-full bg-slate-50 border ${touched.name && errors.name ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-amber-500'} rounded-xl py-2 md:py-2.5 px-3 focus:outline-none focus:ring-2 focus:bg-white transition-all text-slate-800 font-medium`}
|
| 169 |
-
/>
|
| 170 |
-
<datalist id={`products-list-${index}`}>
|
| 171 |
-
{products?.map(p => <option key={p._id} value={p.name} />)}
|
| 172 |
-
</datalist>
|
| 173 |
-
{touched.name && errors.name && <span className="absolute -bottom-5 right-2 text-rose-500 text-[10px] sm:text-xs font-bold whitespace-nowrap">{errors.name}</span>}
|
| 174 |
-
</div>
|
| 175 |
-
|
| 176 |
-
<div className="flex w-full md:w-4/12 gap-3 mt-4 md:mt-0">
|
| 177 |
-
{/* Quantity */}
|
| 178 |
-
<div className="w-1/2 md:w-full relative">
|
| 179 |
-
<label className="md:hidden block text-sm font-bold text-slate-600 mb-1.5">الكمية</label>
|
| 180 |
-
<input
|
| 181 |
-
type="number"
|
| 182 |
-
name={`items[${index}].quantity`}
|
| 183 |
-
placeholder="1"
|
| 184 |
-
value={item.quantity}
|
| 185 |
-
onChange={formik.handleChange}
|
| 186 |
-
onBlur={formik.handleBlur}
|
| 187 |
-
className={`w-full bg-slate-50 border ${touched.quantity && errors.quantity ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-amber-500'} rounded-xl py-2 md:py-2.5 px-3 text-center focus:outline-none focus:ring-2 focus:bg-white transition-all text-slate-800 font-medium`}
|
| 188 |
-
dir="ltr"
|
| 189 |
-
/>
|
| 190 |
-
{touched.quantity && errors.quantity && <span className="absolute -bottom-5 right-0 text-rose-500 text-[10px] sm:text-xs font-bold leading-tight w-full truncate" title={errors.quantity}>{errors.quantity}</span>}
|
| 191 |
-
</div>
|
| 192 |
-
|
| 193 |
-
{/* Price */}
|
| 194 |
-
<div className="w-1/2 md:w-full relative">
|
| 195 |
-
<label className="md:hidden block text-sm font-bold text-slate-600 mb-1.5">السعر (ج.م)</label>
|
| 196 |
-
<input
|
| 197 |
-
type="number"
|
| 198 |
-
name={`items[${index}].price`}
|
| 199 |
-
placeholder="0.00"
|
| 200 |
-
value={item.price}
|
| 201 |
-
onChange={formik.handleChange}
|
| 202 |
-
onBlur={formik.handleBlur}
|
| 203 |
-
className={`w-full bg-slate-50 border ${touched.price && errors.price ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-amber-500'} rounded-xl py-2 md:py-2.5 px-3 text-center focus:outline-none focus:ring-2 focus:bg-white transition-all text-slate-800 font-bold`}
|
| 204 |
-
dir="ltr"
|
| 205 |
-
/>
|
| 206 |
-
{touched.price && errors.price && <span className="absolute -bottom-5 right-0 text-rose-500 text-[10px] sm:text-xs font-bold leading-tight">{errors.price}</span>}
|
| 207 |
-
</div>
|
| 208 |
-
</div>
|
| 209 |
-
|
| 210 |
-
<div className="flex w-full md:w-3/12 justify-between items-center bg-blue-50 md:bg-transparent p-2 md:p-0 rounded-xl border border-blue-100 md:border-transparent mt-4 md:mt-0">
|
| 211 |
-
{/* Subtotal */}
|
| 212 |
-
<div className="w-full flex justify-center items-center font-bold text-blue-700 md:bg-blue-50 py-1.5 md:py-2 rounded-lg">
|
| 213 |
-
<span className="md:hidden ml-2 text-sm text-blue-800">الإجمالي:</span>
|
| 214 |
-
<span>{itemTotal.toFixed(2)}</span>
|
| 215 |
-
</div>
|
| 216 |
-
|
| 217 |
-
{/* Remove Action */}
|
| 218 |
-
<div className="md:w-1/3 flex justify-end md:justify-center pr-2">
|
| 219 |
-
<button
|
| 220 |
-
type="button"
|
| 221 |
-
onClick={() => removeItemRow(index)}
|
| 222 |
-
disabled={items.length === 1}
|
| 223 |
-
className="p-1.5 md:p-2 text-rose-400 hover:text-rose-600 hover:bg-rose-50 rounded-lg disabled:opacity-30 disabled:hover:bg-transparent transition-colors cursor-pointer"
|
| 224 |
-
title={items.length === 1 ? "لا يمكن حذف البند الوحيد" : "حذف البند"}
|
| 225 |
-
>
|
| 226 |
-
<Trash2 size={20} />
|
| 227 |
-
</button>
|
| 228 |
-
</div>
|
| 229 |
-
</div>
|
| 230 |
-
</div>
|
| 231 |
-
);
|
| 232 |
-
})}
|
| 233 |
-
</div>
|
| 234 |
-
|
| 235 |
-
{/* Add Item Trigger */}
|
| 236 |
-
<div className="pt-2">
|
| 237 |
-
<button type="button" onClick={addItemRow} className="flex items-center justify-center gap-2 w-full md:w-fit px-6 py-3 border-2 border-dashed border-amber-300 text-amber-600 hover:bg-amber-50 hover:border-amber-400 rounded-xl transition-all font-bold group">
|
| 238 |
-
<Plus size={20} className="group-hover:rotate-90 transition-transform" />
|
| 239 |
-
صنف إضافي
|
| 240 |
-
</button>
|
| 241 |
-
</div>
|
| 242 |
-
</div>
|
| 243 |
-
</div>
|
| 244 |
-
|
| 245 |
-
{/* Payment Section */}
|
| 246 |
-
<div className="p-4 md:p-5 bg-slate-50 border-t border-slate-200 shrink-0">
|
| 247 |
-
<h3 className="font-bold text-slate-800 mb-3 flex items-center gap-2"><Banknote size={20} className="text-emerald-500" />خيارات الدفع فور التسجيل</h3>
|
| 248 |
-
<div className="flex flex-col md:flex-row gap-3">
|
| 249 |
-
<label className={`flex-1 flex items-center gap-3 p-3 rounded-xl border-2 cursor-pointer transition-all ${paymentType === 'unpaid' ? 'border-rose-500 bg-rose-50' : 'border-slate-200 bg-white hover:border-slate-300'}`}>
|
| 250 |
-
<input type="radio" name="paymentType" className="w-4 h-4 text-rose-600" checked={paymentType === 'unpaid'} onChange={() => formik.setFieldValue('paymentType', 'unpaid')} />
|
| 251 |
-
<div className="flex flex-col"><span className="font-bold text-slate-800 text-sm">آجل (عالحساب)</span><span className="text-xs text-slate-500">لن يُدفع أي مبلغ الآن</span></div>
|
| 252 |
-
</label>
|
| 253 |
-
|
| 254 |
-
<label className={`flex-1 flex items-center gap-3 p-3 rounded-xl border-2 cursor-pointer transition-all ${paymentType === 'full' ? 'border-emerald-500 bg-emerald-50' : 'border-slate-200 bg-white hover:border-slate-300'}`}>
|
| 255 |
-
<input type="radio" name="paymentType" className="w-4 h-4 text-emerald-600" checked={paymentType === 'full'} onChange={() => formik.setFieldValue('paymentType', 'full')} />
|
| 256 |
-
<div className="flex flex-col"><span className="font-bold text-slate-800 text-sm">دفع كامل الشيت</span><span className="text-xs text-slate-500">دفع {grandTotal.toFixed(2)} ج.م فوراً</span></div>
|
| 257 |
-
</label>
|
| 258 |
-
|
| 259 |
-
<label className={`flex-1 flex items-center gap-3 p-3 rounded-xl border-2 cursor-pointer transition-all ${paymentType === 'partial' ? 'border-amber-500 bg-amber-50' : 'border-slate-200 bg-white hover:border-slate-300'}`}>
|
| 260 |
-
<input type="radio" name="paymentType" className="w-4 h-4 text-amber-600" checked={paymentType === 'partial'} onChange={() => formik.setFieldValue('paymentType', 'partial')} />
|
| 261 |
-
<div className="flex flex-col"><span className="font-bold text-slate-800 text-sm">دفع جزء من المبلغ</span><span className="text-xs text-slate-500">تحديد مبلغ مخصص للدفعة</span></div>
|
| 262 |
-
</label>
|
| 263 |
-
</div>
|
| 264 |
-
|
| 265 |
-
{/* Partial Amount Input */}
|
| 266 |
-
{paymentType === 'partial' && (
|
| 267 |
-
<div className="animate-in fade-in slide-in-from-top-2 mt-3 bg-white p-3 rounded-xl border border-amber-200 flex flex-col md:flex-row items-start md:items-center gap-3 md:gap-4 relative">
|
| 268 |
-
<label className="font-bold text-slate-700 whitespace-nowrap text-sm">المبلغ المدفوع كاش (ج.م):</label>
|
| 269 |
-
<div className="relative w-full md:max-w-[200px]">
|
| 270 |
-
<input
|
| 271 |
-
type="number"
|
| 272 |
-
name="partialAmount"
|
| 273 |
-
value={partialAmount}
|
| 274 |
-
onChange={formik.handleChange}
|
| 275 |
-
onBlur={formik.handleBlur}
|
| 276 |
-
placeholder="أدخل المبلغ..."
|
| 277 |
-
className={`w-full bg-slate-50 border ${formik.touched.partialAmount && formik.errors.partialAmount ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-300 focus:ring-amber-500'} rounded-lg py-2 px-3 focus:outline-none focus:ring-2 font-bold text-slate-800`}
|
| 278 |
-
dir="ltr"
|
| 279 |
-
/>
|
| 280 |
-
{formik.touched.partialAmount && formik.errors.partialAmount && <span className="absolute -bottom-5 right-0 text-rose-500 text-xs font-bold whitespace-nowrap">{formik.errors.partialAmount}</span>}
|
| 281 |
-
</div>
|
| 282 |
-
<span className="text-xs font-bold text-slate-500 bg-slate-100 py-1.5 px-3 rounded-lg w-fit mt-3 md:mt-0">
|
| 283 |
-
المديونية المتبقية: <span className="text-rose-600">{(grandTotal - Number(partialAmount || 0)).toFixed(2)} ج.م</span>
|
| 284 |
-
</span>
|
| 285 |
-
</div>
|
| 286 |
-
)}
|
| 287 |
-
</div>
|
| 288 |
-
|
| 289 |
-
{/* Footer Summary & Actions */}
|
| 290 |
-
<div className="p-4 bg-white border-t border-slate-200 flex flex-col md:flex-row items-center justify-between gap-3 shrink-0 shadow-[0_-4px_15px_-10px_rgba(0,0,0,0.05)]">
|
| 291 |
-
<div className="flex items-center justify-between md:justify-start gap-4 bg-slate-50 px-4 py-2.5 rounded-xl border border-slate-100 w-full md:w-auto">
|
| 292 |
-
<span className="text-slate-500 font-bold text-sm">إجمالي فاتورة الأصناف:</span>
|
| 293 |
-
<span className="text-xl font-black text-blue-600">{grandTotal.toFixed(2)} <span className="text-sm font-normal text-slate-400">ج.م</span></span>
|
| 294 |
-
</div>
|
| 295 |
-
|
| 296 |
-
<div className="flex w-full md:w-auto gap-3">
|
| 297 |
-
<button onClick={onClose} disabled={formik.isSubmitting} className="px-6 py-2.5 bg-white border border-slate-200 text-slate-600 rounded-xl hover:bg-rose-50 hover:text-rose-600 hover:border-rose-200 font-bold transition-all disabled:opacity-50 flex-1 md:flex-none text-center">إلغاء</button>
|
| 298 |
-
<button onClick={formik.submitForm} disabled={formik.isSubmitting} className="px-6 py-2.5 bg-amber-500 text-slate-900 rounded-xl hover:bg-amber-600 font-bold transition-all disabled:opacity-50 flex-1 md:flex-none flex justify-center items-center gap-2 shadow-sm shadow-amber-500/20 whitespace-nowrap">
|
| 299 |
-
{formik.isSubmitting ? <><Loader2 className="animate-spin" size={18} /> جاري الحفظ...</> : <><Receipt size={18} /> حفظ واعتماد الفاتورة</>}
|
| 300 |
-
</button>
|
| 301 |
-
</div>
|
| 302 |
-
</div>
|
| 303 |
-
</div>
|
| 304 |
-
</div>
|
| 305 |
-
);
|
| 306 |
-
};
|
| 307 |
-
|
| 308 |
-
export default InvoiceItemsModal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Reports/DailyReport.jsx
DELETED
|
@@ -1,284 +0,0 @@
|
|
| 1 |
-
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 4 |
-
import { Banknote, TrendingUp, ReceiptText, Clock, ArrowDownRight, PlusCircle, Trash2, Wallet } from "lucide-react";
|
| 5 |
-
import { useState } from "react";
|
| 6 |
-
import { toast } from "react-toastify";
|
| 7 |
-
import { useFormik } from "formik";
|
| 8 |
-
import * as Yup from "yup";
|
| 9 |
-
|
| 10 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 11 |
-
|
| 12 |
-
const DailyReport = () => {
|
| 13 |
-
const [openExpenseModal, setOpenExpenseModal] = useState(false);
|
| 14 |
-
|
| 15 |
-
const { data, isLoading, refetch } = useQuery({
|
| 16 |
-
queryKey: ["dailyReport"],
|
| 17 |
-
queryFn: () => axios.get(`${apiUrl}/reports/daily`),
|
| 18 |
-
refetchInterval: 30000,
|
| 19 |
-
});
|
| 20 |
-
if (isLoading) return <LodderSpinner />;
|
| 21 |
-
|
| 22 |
-
const report = data?.data;
|
| 23 |
-
const recentPayments = Array.isArray(report?.recentPayments) ? report.recentPayments : [];
|
| 24 |
-
const expensesList = Array.isArray(report?.expensesList) ? report.expensesList : [];
|
| 25 |
-
|
| 26 |
-
return (
|
| 27 |
-
<div className="space-y-6">
|
| 28 |
-
{/* Header Row */}
|
| 29 |
-
<div className="flex flex-col md:flex-row items-start md:items-center justify-between bg-white p-6 rounded-3xl shadow-sm border border-slate-200 gap-4">
|
| 30 |
-
<div>
|
| 31 |
-
<h2 className="text-3xl font-black text-slate-800 mb-2">تقرير يومية الخزنة والمصروفات</h2>
|
| 32 |
-
<p className="text-slate-500 font-bold flex items-center gap-2 bg-slate-50 px-4 py-2 rounded-xl border border-slate-100 w-fit">
|
| 33 |
-
<Clock size={18} className="text-blue-500"/>
|
| 34 |
-
تاريخ اليوم: {new Date().toLocaleDateString("ar-EG", { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })}
|
| 35 |
-
</p>
|
| 36 |
-
</div>
|
| 37 |
-
<div className="flex gap-3 w-full md:w-auto">
|
| 38 |
-
<button
|
| 39 |
-
onClick={() => setOpenExpenseModal(true)}
|
| 40 |
-
className="px-6 py-3 bg-rose-500 hover:bg-rose-600 text-white font-bold rounded-xl transition-all border border-rose-600 flex items-center justify-center gap-2 shadow-[0_2px_10px_-4px_rgba(225,29,72,0.4)] hover:-translate-y-0.5 flex-1 md:flex-none"
|
| 41 |
-
>
|
| 42 |
-
<PlusCircle size={18} />
|
| 43 |
-
تسجيل مصروف
|
| 44 |
-
</button>
|
| 45 |
-
<button
|
| 46 |
-
onClick={() => refetch()}
|
| 47 |
-
className="px-6 py-3 bg-slate-50 hover:bg-slate-100 text-slate-700 font-bold rounded-xl transition-all border border-slate-200 flex items-center justify-center gap-2 shadow-[0_2px_10px_-4px_rgba(0,0,0,0.1)] hover:-translate-y-0.5 flex-1 md:flex-none"
|
| 48 |
-
>
|
| 49 |
-
<TrendingUp size={18} />
|
| 50 |
-
تحديث
|
| 51 |
-
</button>
|
| 52 |
-
</div>
|
| 53 |
-
</div>
|
| 54 |
-
|
| 55 |
-
{/* Stats Cards - Net Cash & Expenses */}
|
| 56 |
-
<div className="grid grid-cols-1 md:grid-cols-4 gap-6">
|
| 57 |
-
|
| 58 |
-
{/* NET CASH (صافي الدرج) */}
|
| 59 |
-
<div className="bg-gradient-to-br from-indigo-600 to-indigo-800 rounded-3xl p-6 shadow-xl shadow-indigo-500/20 text-white relative overflow-hidden group md:col-span-2">
|
| 60 |
-
<div className="absolute -left-6 -top-6 w-32 h-32 bg-white/10 rounded-full blur-2xl group-hover:scale-150 transition-transform duration-700"></div>
|
| 61 |
-
<div className="flex justify-between items-start mb-6 relative z-10">
|
| 62 |
-
<div className="p-3 bg-white/20 rounded-2xl backdrop-blur-sm border border-indigo-400/30">
|
| 63 |
-
<Wallet size={32} className="text-white" />
|
| 64 |
-
</div>
|
| 65 |
-
<span className="text-sm font-bold bg-indigo-900/40 px-4 py-1.5 rounded-full border border-indigo-400/30">الهدف النهائي</span>
|
| 66 |
-
</div>
|
| 67 |
-
<h3 className="text-indigo-100 font-bold mb-2 text-lg">صافي درج الكاش (بعد الخصم)</h3>
|
| 68 |
-
<p className="text-5xl font-black drop-shadow-md">
|
| 69 |
-
{report?.netCashInDrawer?.toFixed(2) || "0.00"} <span className="text-xl font-normal opacity-80">ج.م</span>
|
| 70 |
-
</p>
|
| 71 |
-
</div>
|
| 72 |
-
|
| 73 |
-
{/* Total Inward Cash */}
|
| 74 |
-
<div className="bg-white rounded-3xl p-6 shadow-sm border border-slate-200 relative overflow-hidden group">
|
| 75 |
-
<div className="flex justify-between items-start mb-4 relative z-10">
|
| 76 |
-
<div className="p-3 bg-emerald-50 rounded-2xl border border-emerald-100">
|
| 77 |
-
<Banknote size={24} className="text-emerald-500" />
|
| 78 |
-
</div>
|
| 79 |
-
</div>
|
| 80 |
-
<h3 className="text-slate-500 font-bold mb-1 text-sm">إجمالي الكاش المورّد</h3>
|
| 81 |
-
<p className="text-3xl font-black text-slate-800">
|
| 82 |
-
{report?.totalCashCollected?.toFixed(2) || "0.00"} <span className="text-lg font-normal text-slate-400">ج.م</span>
|
| 83 |
-
</p>
|
| 84 |
-
</div>
|
| 85 |
-
|
| 86 |
-
{/* Total Expenses */}
|
| 87 |
-
<div className="bg-white rounded-3xl p-6 shadow-sm border border-slate-200 relative overflow-hidden group">
|
| 88 |
-
<div className="flex justify-between items-start mb-4 relative z-10">
|
| 89 |
-
<div className="p-3 bg-rose-50 rounded-2xl border border-rose-100">
|
| 90 |
-
<ArrowDownRight size={24} className="text-rose-500" />
|
| 91 |
-
</div>
|
| 92 |
-
</div>
|
| 93 |
-
<h3 className="text-slate-500 font-bold mb-1 text-sm">إجمالي مسحوبات المصروفات</h3>
|
| 94 |
-
<p className="text-3xl font-black text-rose-600">
|
| 95 |
-
{report?.totalExpenses?.toFixed(2) || "0.00"} <span className="text-lg font-normal text-rose-400">ج.م</span>
|
| 96 |
-
</p>
|
| 97 |
-
</div>
|
| 98 |
-
</div>
|
| 99 |
-
|
| 100 |
-
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
| 101 |
-
{/* Recent Payments Breakdown */}
|
| 102 |
-
<div className="bg-white border border-slate-200 rounded-3xl p-6 shadow-sm">
|
| 103 |
-
<h3 className="text-lg font-bold text-slate-800 mb-6 flex items-center gap-3">
|
| 104 |
-
<div className="p-2.5 bg-emerald-50 text-emerald-600 rounded-xl">
|
| 105 |
-
<Banknote size={20}/>
|
| 106 |
-
</div>
|
| 107 |
-
الدفعات المحصلة للإيرادات
|
| 108 |
-
</h3>
|
| 109 |
-
|
| 110 |
-
{recentPayments?.length === 0 ? (
|
| 111 |
-
<div className="text-center py-12 bg-slate-50/50 rounded-2xl border-2 border-dashed border-slate-200">
|
| 112 |
-
<p className="text-slate-500 font-bold">لا توجد إيرادات كاش مسجلة اليوم</p>
|
| 113 |
-
</div>
|
| 114 |
-
) : (
|
| 115 |
-
<div className="overflow-x-auto overflow-y-auto max-h-[350px] custom-scroll rounded-2xl border border-slate-100 relative">
|
| 116 |
-
<table className="w-full text-right border-collapse">
|
| 117 |
-
<thead className="sticky top-0 z-10 bg-slate-50 shadow-sm border-b border-slate-200">
|
| 118 |
-
<tr>
|
| 119 |
-
<th className="py-3 px-4 font-bold text-slate-500 text-sm">العميل</th>
|
| 120 |
-
<th className="py-3 px-4 font-bold text-slate-500 text-sm">المبلغ</th>
|
| 121 |
-
<th className="py-3 px-4 font-bold text-slate-500 text-sm">التوقيت</th>
|
| 122 |
-
</tr>
|
| 123 |
-
</thead>
|
| 124 |
-
<tbody className="divide-y divide-slate-100 bg-white">
|
| 125 |
-
{recentPayments?.map((payment, idx) => (
|
| 126 |
-
<tr key={idx} className="hover:bg-slate-50">
|
| 127 |
-
<td className="py-3 px-4 font-bold text-slate-700">{payment.customerName}</td>
|
| 128 |
-
<td className="py-3 px-4 font-black text-emerald-600">+{payment.amount.toFixed(2)}</td>
|
| 129 |
-
<td className="py-3 px-4 text-xs font-bold text-slate-400">
|
| 130 |
-
{new Date(payment.date).toLocaleTimeString("ar-EG", { hour: '2-digit', minute: '2-digit', hour12: true })}
|
| 131 |
-
</td>
|
| 132 |
-
</tr>
|
| 133 |
-
))}
|
| 134 |
-
</tbody>
|
| 135 |
-
</table>
|
| 136 |
-
</div>
|
| 137 |
-
)}
|
| 138 |
-
</div>
|
| 139 |
-
|
| 140 |
-
{/* Expenses Breakdown */}
|
| 141 |
-
<div className="bg-white border border-slate-200 rounded-3xl p-6 shadow-sm">
|
| 142 |
-
<h3 className="text-lg font-bold text-slate-800 mb-6 flex items-center gap-3">
|
| 143 |
-
<div className="p-2.5 bg-rose-50 text-rose-600 rounded-xl">
|
| 144 |
-
<ArrowDownRight size={20}/>
|
| 145 |
-
</div>
|
| 146 |
-
سجل المصروفات والمسحوبات النقدية
|
| 147 |
-
</h3>
|
| 148 |
-
|
| 149 |
-
{expensesList?.length === 0 ? (
|
| 150 |
-
<div className="text-center py-12 bg-slate-50/50 rounded-2xl border-2 border-dashed border-slate-200">
|
| 151 |
-
<p className="text-slate-500 font-bold">لم يتم تسجيل أي مصاريف حتى الآن</p>
|
| 152 |
-
</div>
|
| 153 |
-
) : (
|
| 154 |
-
<div className="overflow-x-auto overflow-y-auto max-h-[350px] custom-scroll rounded-2xl border border-slate-100 relative w-full">
|
| 155 |
-
<table className="w-full text-right border-collapse">
|
| 156 |
-
<thead className="sticky top-0 z-10 bg-slate-50 shadow-sm border-b border-slate-200">
|
| 157 |
-
<tr>
|
| 158 |
-
<th className="py-3 px-4 font-bold text-slate-500 text-sm">بيان المصروف</th>
|
| 159 |
-
<th className="py-3 px-4 font-bold text-slate-500 text-sm">القيمة</th>
|
| 160 |
-
<th className="py-3 px-4 font-bold text-slate-500 text-sm text-center">إلغاء</th>
|
| 161 |
-
</tr>
|
| 162 |
-
</thead>
|
| 163 |
-
<tbody className="divide-y divide-slate-100 bg-white">
|
| 164 |
-
{expensesList?.map((expense) => (
|
| 165 |
-
<tr key={expense._id} className="hover:bg-slate-50 relative">
|
| 166 |
-
<td className="py-3 px-4 font-bold text-slate-700">
|
| 167 |
-
{expense.title}
|
| 168 |
-
<div className="text-xs text-slate-400 font-normal mt-0.5">
|
| 169 |
-
{new Date(expense.date).toLocaleTimeString("ar-EG", { hour: '2-digit', minute: '2-digit', hour12: true })}
|
| 170 |
-
</div>
|
| 171 |
-
</td>
|
| 172 |
-
<td className="py-3 px-4 font-black text-rose-600">-{expense.amount.toFixed(2)}</td>
|
| 173 |
-
<td className="py-3 px-4 text-center">
|
| 174 |
-
<DeleteExpenseButton expenseId={expense._id} />
|
| 175 |
-
</td>
|
| 176 |
-
</tr>
|
| 177 |
-
))}
|
| 178 |
-
</tbody>
|
| 179 |
-
</table>
|
| 180 |
-
</div>
|
| 181 |
-
)}
|
| 182 |
-
</div>
|
| 183 |
-
</div>
|
| 184 |
-
|
| 185 |
-
{openExpenseModal && <AddExpenseModal onClose={() => setOpenExpenseModal(false)} />}
|
| 186 |
-
</div>
|
| 187 |
-
);
|
| 188 |
-
};
|
| 189 |
-
|
| 190 |
-
// --- Add Expense Modal ---
|
| 191 |
-
const AddExpenseModal = ({ onClose }) => {
|
| 192 |
-
const queryClient = useQueryClient();
|
| 193 |
-
|
| 194 |
-
const { mutate, isPending } = useMutation({
|
| 195 |
-
mutationFn: (values) => axios.post(`${apiUrl}/expenses`, values),
|
| 196 |
-
onSuccess: () => {
|
| 197 |
-
queryClient.invalidateQueries(["dailyReport"]);
|
| 198 |
-
toast.success("تم تسجيل المصروف بنجاح وخصمه من الدرج");
|
| 199 |
-
onClose();
|
| 200 |
-
},
|
| 201 |
-
onError: (err) => {
|
| 202 |
-
toast.error(err.response?.data?.message || "حدث خطأ أثناء التسجيل");
|
| 203 |
-
}
|
| 204 |
-
});
|
| 205 |
-
|
| 206 |
-
const formik = useFormik({
|
| 207 |
-
initialValues: { title: "", amount: "" },
|
| 208 |
-
validationSchema: Yup.object({
|
| 209 |
-
title: Yup.string()
|
| 210 |
-
.required('بيان المصروف مطلوب')
|
| 211 |
-
.trim()
|
| 212 |
-
.matches(/^[\u0600-\u06FFa-zA-Z\s]+$/, 'يجب أن يحتوي على حروف فقط'),
|
| 213 |
-
amount: Yup.number()
|
| 214 |
-
.required('المبلغ مطلوب')
|
| 215 |
-
.typeError('الرجاء إدخال رقم صحيح')
|
| 216 |
-
.positive('يجب أن يكون أكبر من الصفر')
|
| 217 |
-
}),
|
| 218 |
-
onSubmit: (values) => mutate(values)
|
| 219 |
-
});
|
| 220 |
-
|
| 221 |
-
return (
|
| 222 |
-
<div className="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4">
|
| 223 |
-
<div className="bg-white rounded-3xl w-full max-w-md shadow-2xl border border-slate-200 animate-in zoom-in-95 duration-200 overflow-hidden">
|
| 224 |
-
<div className="p-6 border-b border-slate-100 bg-slate-50 flex justify-between items-center">
|
| 225 |
-
<h2 className="text-xl font-black text-slate-800 flex items-center gap-2">
|
| 226 |
-
<ArrowDownRight size={24} className="text-rose-600" />
|
| 227 |
-
تسجيل مصروف جديد للدرج
|
| 228 |
-
</h2>
|
| 229 |
-
<button onClick={onClose} className="w-8 h-8 flex items-center justify-center bg-white text-slate-400 hover:text-rose-500 rounded-full transition-colors shadow-sm">×</button>
|
| 230 |
-
</div>
|
| 231 |
-
<form onSubmit={formik.handleSubmit} className="p-6 space-y-5">
|
| 232 |
-
<div className="relative">
|
| 233 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">فيما صرفت؟ (البيان) <span className="text-rose-500">*</span></label>
|
| 234 |
-
<input name="title" type="text" placeholder="مثال: فاتورة كهرباء، يوميات عمال..." value={formik.values.title} onChange={formik.handleChange} onBlur={formik.handleBlur} className={`w-full p-3.5 bg-slate-50 border ${formik.touched.title && formik.errors.title ? 'border-rose-500 focus:ring-rose-600' : 'border-slate-200 focus:ring-rose-500/20'} rounded-xl outline-none focus:ring-2 focus:bg-white transition-colors`} />
|
| 235 |
-
{formik.touched.title && formik.errors.title && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold">{formik.errors.title}</span>}
|
| 236 |
-
</div>
|
| 237 |
-
<div className="relative mt-8">
|
| 238 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">قيمة المصروف <span className="text-rose-500">*</span></label>
|
| 239 |
-
<div className="relative">
|
| 240 |
-
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400 font-bold">ج.م</span>
|
| 241 |
-
<input name="amount" type="number" step="0.01" value={formik.values.amount} onChange={formik.handleChange} onBlur={formik.handleBlur} className={`w-full p-3.5 pl-12 bg-slate-50 border ${formik.touched.amount && formik.errors.amount ? 'border-rose-500 focus:ring-rose-600' : 'border-slate-200 focus:ring-rose-500/20'} rounded-xl outline-none focus:ring-2 focus:bg-white transition-colors text-left font-mono font-bold text-rose-600 text-lg`} dir="ltr" placeholder="0.00" />
|
| 242 |
-
</div>
|
| 243 |
-
{formik.touched.amount && formik.errors.amount && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold">{formik.errors.amount}</span>}
|
| 244 |
-
</div>
|
| 245 |
-
<button disabled={isPending || formik.isSubmitting} type="submit" className="w-full py-4 bg-rose-600 text-white rounded-xl font-black text-lg hover:bg-rose-700 disabled:opacity-50 mt-4 shadow-lg shadow-rose-600/30 transition-all hover:-translate-y-0.5">
|
| 246 |
-
{isPending || formik.isSubmitting ? "جاري التسجيل خصماً من الدرج..." : "تسجيل وخصم من الدرج"}
|
| 247 |
-
</button>
|
| 248 |
-
</form>
|
| 249 |
-
</div>
|
| 250 |
-
</div>
|
| 251 |
-
);
|
| 252 |
-
};
|
| 253 |
-
|
| 254 |
-
// --- Delete Expense Button ---
|
| 255 |
-
const DeleteExpenseButton = ({ expenseId }) => {
|
| 256 |
-
const [isConfirming, setIsConfirming] = useState(false);
|
| 257 |
-
const queryClient = useQueryClient();
|
| 258 |
-
|
| 259 |
-
const { mutate, isPending } = useMutation({
|
| 260 |
-
mutationFn: () => axios.delete(`${apiUrl}/expenses/${expenseId}`),
|
| 261 |
-
onSuccess: () => {
|
| 262 |
-
queryClient.invalidateQueries(["dailyReport"]);
|
| 263 |
-
toast.success("تم التراجع عن المصروف ورد المبلغ للدرج");
|
| 264 |
-
}
|
| 265 |
-
});
|
| 266 |
-
|
| 267 |
-
if (isConfirming) {
|
| 268 |
-
return (
|
| 269 |
-
<div className="flex items-center justify-center gap-1.5 bg-rose-50 p-1.5 rounded-xl border border-rose-200 shadow-sm animate-in fade-in slide-in-from-left-2 absolute left-10 z-20">
|
| 270 |
-
<span className="text-xs font-bold text-rose-700 px-1">تراجع؟</span>
|
| 271 |
-
<button onClick={() => mutate()} disabled={isPending} className="bg-rose-500 text-white py-1 px-2 rounded-lg text-xs font-bold hover:bg-rose-600">نعم</button>
|
| 272 |
-
<button onClick={() => setIsConfirming(false)} className="bg-white text-slate-500 border border-slate-200 py-1 px-2 rounded-lg text-xs hover:text-slate-800">لا</button>
|
| 273 |
-
</div>
|
| 274 |
-
);
|
| 275 |
-
}
|
| 276 |
-
|
| 277 |
-
return (
|
| 278 |
-
<button onClick={() => setIsConfirming(true)} className="p-2 text-rose-500 bg-rose-50 rounded-lg hover:bg-rose-600 hover:text-white transition-colors border border-rose-100 shadow-sm inline-flex justify-center items-center" title="حذف الريكورد">
|
| 279 |
-
<Trash2 size={16} />
|
| 280 |
-
</button>
|
| 281 |
-
);
|
| 282 |
-
};
|
| 283 |
-
|
| 284 |
-
export default DailyReport;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Reports/ReportsWorkspace.jsx
DELETED
|
@@ -1,505 +0,0 @@
|
|
| 1 |
-
import axios from "axios";
|
| 2 |
-
import { useState } from "react";
|
| 3 |
-
import { useQuery } from "@tanstack/react-query";
|
| 4 |
-
import { Download, Printer, Share2, ChevronRight, ChevronLeft, Calendar } from "lucide-react";
|
| 5 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 6 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 7 |
-
import { downloadCsv, printHtmlDocument } from "../../utils/exports";
|
| 8 |
-
import { formatCurrency, formatDate, formatDateTime } from "../../utils/formatters";
|
| 9 |
-
import { openWhatsAppMessage } from "../../utils/whatsapp";
|
| 10 |
-
|
| 11 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 12 |
-
const LIMIT = 5;
|
| 13 |
-
|
| 14 |
-
// ── helpers ─────────────────────────────────────────────────────────────────
|
| 15 |
-
const toArr = (val) => (Array.isArray(val) ? val : []);
|
| 16 |
-
|
| 17 |
-
const getDefaultStart = () => {
|
| 18 |
-
const d = new Date();
|
| 19 |
-
d.setDate(d.getDate() - 29);
|
| 20 |
-
return d.toISOString().split("T")[0];
|
| 21 |
-
};
|
| 22 |
-
const getDefaultEnd = () => new Date().toISOString().split("T")[0];
|
| 23 |
-
|
| 24 |
-
const formatRangeLabel = (start, end) => {
|
| 25 |
-
const opts = { day: "numeric", month: "long", year: "numeric" };
|
| 26 |
-
const s = new Date(start).toLocaleDateString("ar-EG", opts);
|
| 27 |
-
const e = new Date(end).toLocaleDateString("ar-EG", opts);
|
| 28 |
-
return `${s} — ${e}`;
|
| 29 |
-
};
|
| 30 |
-
|
| 31 |
-
// ── Pagination component ─────────────────────────────────────────────────────
|
| 32 |
-
const Pagination = ({ pagination, page, setPage }) => {
|
| 33 |
-
if (!pagination || pagination.totalPages <= 1) return null;
|
| 34 |
-
const { total, totalPages } = pagination;
|
| 35 |
-
const pages = Array.from({ length: totalPages }, (_, i) => i + 1);
|
| 36 |
-
|
| 37 |
-
return (
|
| 38 |
-
<div className="flex items-center justify-between mt-4 pt-4 border-t border-slate-100">
|
| 39 |
-
<span className="text-xs font-bold text-slate-400">
|
| 40 |
-
{total} نتيجة • صفحة {page} من {totalPages}
|
| 41 |
-
</span>
|
| 42 |
-
<div className="flex gap-2">
|
| 43 |
-
<button
|
| 44 |
-
onClick={() => setPage((p) => Math.max(1, p - 1))}
|
| 45 |
-
disabled={page === 1}
|
| 46 |
-
className="p-1.5 rounded-lg border border-slate-200 text-slate-500 hover:bg-slate-50 disabled:opacity-30 disabled:cursor-not-allowed"
|
| 47 |
-
>
|
| 48 |
-
<ChevronRight size={16} />
|
| 49 |
-
</button>
|
| 50 |
-
{/* Number buttons */}
|
| 51 |
-
{pages.map((p) => (
|
| 52 |
-
<button
|
| 53 |
-
key={p}
|
| 54 |
-
onClick={() => setPage(p)}
|
| 55 |
-
className={`w-8 h-8 rounded-lg text-sm font-bold transition-all ${p === page
|
| 56 |
-
? "bg-slate-800 text-white border border-slate-800"
|
| 57 |
-
: "border border-slate-200 text-slate-500 hover:bg-slate-50"
|
| 58 |
-
}`}
|
| 59 |
-
>
|
| 60 |
-
{p}
|
| 61 |
-
</button>
|
| 62 |
-
))}
|
| 63 |
-
<button
|
| 64 |
-
onClick={() => setPage((p) => Math.min(totalPages, p + 1))}
|
| 65 |
-
disabled={page === totalPages}
|
| 66 |
-
className="p-1.5 rounded-lg border border-slate-200 text-slate-500 hover:bg-slate-50 disabled:opacity-30 disabled:cursor-not-allowed"
|
| 67 |
-
>
|
| 68 |
-
<ChevronLeft size={16} />
|
| 69 |
-
</button>
|
| 70 |
-
</div>
|
| 71 |
-
</div>
|
| 72 |
-
);
|
| 73 |
-
};
|
| 74 |
-
|
| 75 |
-
// ── Main component ───────────────────────────────────────────────────────────
|
| 76 |
-
const ReportsWorkspace = () => {
|
| 77 |
-
const { user } = useAuth();
|
| 78 |
-
const currency = user?.tenant?.settings?.currency || "EGP";
|
| 79 |
-
const companyName = user?.tenant?.name || "الشركة";
|
| 80 |
-
|
| 81 |
-
// date range state
|
| 82 |
-
const [startDate, setStartDate] = useState(getDefaultStart());
|
| 83 |
-
const [endDate, setEndDate] = useState(getDefaultEnd());
|
| 84 |
-
|
| 85 |
-
// pagination state per table
|
| 86 |
-
const [customersPage, setCustomersPage] = useState(1);
|
| 87 |
-
const [productsPage, setProductsPage] = useState(1);
|
| 88 |
-
const [suppliersPage, setSuppliersPage] = useState(1);
|
| 89 |
-
const [paymentsPage, setPaymentsPage] = useState(1);
|
| 90 |
-
const [invoicesPage, setInvoicesPage] = useState(1);
|
| 91 |
-
|
| 92 |
-
// ── queries ────────────────────────────────────────────────────────────
|
| 93 |
-
const { data: overviewData, isLoading: overviewLoading } = useQuery({
|
| 94 |
-
queryKey: ["reports", "overview", startDate, endDate, paymentsPage, invoicesPage],
|
| 95 |
-
queryFn: () =>
|
| 96 |
-
axios.get(`${apiUrl}/reports/overview`, {
|
| 97 |
-
params: {
|
| 98 |
-
start: startDate,
|
| 99 |
-
end: endDate,
|
| 100 |
-
limit: LIMIT,
|
| 101 |
-
paymentsPage,
|
| 102 |
-
invoicesPage,
|
| 103 |
-
},
|
| 104 |
-
}),
|
| 105 |
-
refetchOnWindowFocus: false,
|
| 106 |
-
});
|
| 107 |
-
|
| 108 |
-
const { data: customersData, isLoading: customersLoading } = useQuery({
|
| 109 |
-
queryKey: ["customers", "paginated", customersPage],
|
| 110 |
-
queryFn: () =>
|
| 111 |
-
axios.get(`${apiUrl}/customers`, { params: { limit: LIMIT, page: customersPage } }),
|
| 112 |
-
refetchOnWindowFocus: false,
|
| 113 |
-
keepPreviousData: true,
|
| 114 |
-
staleTime: 2 * 60 * 1000, // 5 minutes cache
|
| 115 |
-
});
|
| 116 |
-
const { data: productsData, isLoading: productsLoading } = useQuery({
|
| 117 |
-
queryKey: ["products", "paginated", productsPage],
|
| 118 |
-
queryFn: () =>
|
| 119 |
-
axios.get(`${apiUrl}/products`, { params: { limit: LIMIT, page: productsPage } }),
|
| 120 |
-
refetchOnWindowFocus: false,
|
| 121 |
-
});
|
| 122 |
-
|
| 123 |
-
const { data: suppliersData, isLoading: suppliersLoading } = useQuery({
|
| 124 |
-
queryKey: ["suppliers", "paginated", suppliersPage],
|
| 125 |
-
queryFn: () =>
|
| 126 |
-
axios.get(`${apiUrl}/suppliers`, { params: { limit: LIMIT, page: suppliersPage } }),
|
| 127 |
-
refetchOnWindowFocus: false,
|
| 128 |
-
});
|
| 129 |
-
|
| 130 |
-
// ── derived data ───────────────────────────────────────────────────────
|
| 131 |
-
const overview = overviewData?.data;
|
| 132 |
-
const customers = toArr(customersData?.data?.data);
|
| 133 |
-
const customersPagination = customersData?.data?.pagination;
|
| 134 |
-
const products = toArr(productsData?.data?.data);
|
| 135 |
-
const productsPagination = productsData?.data?.pagination;
|
| 136 |
-
const suppliers = toArr(suppliersData?.data?.data);
|
| 137 |
-
const suppliersPagination = suppliersData?.data?.pagination;
|
| 138 |
-
|
| 139 |
-
const metrics = overview?.metrics || {};
|
| 140 |
-
const recentPayments = toArr(overview?.recentPayments?.data);
|
| 141 |
-
const paymentsPagination = overview?.recentPayments?.pagination;
|
| 142 |
-
const recentInvoices = toArr(overview?.recentInvoices?.data);
|
| 143 |
-
const invoicesPagination = overview?.recentInvoices?.pagination;
|
| 144 |
-
|
| 145 |
-
// actual range returned by backend (more reliable than local state)
|
| 146 |
-
const rangeLabel = overview?.dateRange
|
| 147 |
-
? formatRangeLabel(overview.dateRange.start, overview.dateRange.end)
|
| 148 |
-
: formatRangeLabel(startDate, endDate);
|
| 149 |
-
|
| 150 |
-
const financialRows = [
|
| 151 |
-
{
|
| 152 |
-
metric: "المبيعات",
|
| 153 |
-
sub: rangeLabel,
|
| 154 |
-
value: formatCurrency(metrics.salesInRange, currency),
|
| 155 |
-
color: "text-indigo-600",
|
| 156 |
-
},
|
| 157 |
-
{
|
| 158 |
-
metric: "التحصيلات النقدية",
|
| 159 |
-
sub: rangeLabel,
|
| 160 |
-
value: formatCurrency(metrics.collectionsInRange, currency),
|
| 161 |
-
color: "text-emerald-600",
|
| 162 |
-
},
|
| 163 |
-
{
|
| 164 |
-
metric: "المصروفات",
|
| 165 |
-
sub: rangeLabel,
|
| 166 |
-
value: formatCurrency(metrics.expensesInRange, currency),
|
| 167 |
-
color: "text-rose-600",
|
| 168 |
-
},
|
| 169 |
-
{
|
| 170 |
-
metric: "صافي التدفق النقدي",
|
| 171 |
-
sub: "التحصيلات ناقص المصروفات",
|
| 172 |
-
value: formatCurrency(metrics.netCashFlow, currency),
|
| 173 |
-
color: metrics.netCashFlow >= 0 ? "text-emerald-600" : "text-rose-600",
|
| 174 |
-
},
|
| 175 |
-
{
|
| 176 |
-
metric: "المستحقات على العملاء",
|
| 177 |
-
sub: "إجمالي الرصيد المتبقي غير المسدد",
|
| 178 |
-
value: formatCurrency(metrics.outstandingReceivables, currency),
|
| 179 |
-
color: "text-amber-600",
|
| 180 |
-
},
|
| 181 |
-
{
|
| 182 |
-
metric: "المستحقات للموردين",
|
| 183 |
-
sub: "إجمالي ما نستحقه لمورديننا",
|
| 184 |
-
value: formatCurrency(metrics.supplierOutstanding, currency),
|
| 185 |
-
color: "text-slate-700",
|
| 186 |
-
},
|
| 187 |
-
];
|
| 188 |
-
|
| 189 |
-
if (overviewLoading || customersLoading || productsLoading || suppliersLoading) {
|
| 190 |
-
return <LodderSpinner />;
|
| 191 |
-
}
|
| 192 |
-
|
| 193 |
-
const printReport = () => {
|
| 194 |
-
printHtmlDocument(
|
| 195 |
-
`تقارير ${companyName}`,
|
| 196 |
-
`
|
| 197 |
-
<h1>التقرير التشغيلي - ${companyName}</h1>
|
| 198 |
-
<p>الفترة: ${rangeLabel}</p>
|
| 199 |
-
<p>تم الإنشاء: ${formatDateTime(new Date())}</p>
|
| 200 |
-
<div class="section">
|
| 201 |
-
<h2>الملخص المالي</h2>
|
| 202 |
-
<div class="kpi-grid">
|
| 203 |
-
${financialRows
|
| 204 |
-
.map(
|
| 205 |
-
(row) =>
|
| 206 |
-
`<div class="kpi-card"><strong>${row.metric}</strong><small style="display:block;color:#888">${row.sub}</small><div>${row.value}</div></div>`
|
| 207 |
-
)
|
| 208 |
-
.join("")}
|
| 209 |
-
</div>
|
| 210 |
-
</div>
|
| 211 |
-
<div class="section">
|
| 212 |
-
<h2>مستحقات العملاء</h2>
|
| 213 |
-
${tableHtml(
|
| 214 |
-
["#", "العميل", "الفواتير", "المستحقات"],
|
| 215 |
-
customers
|
| 216 |
-
.filter(c => Number(c.totalDebt) > 0) // keep only customers with debt
|
| 217 |
-
.map((c, index) => [
|
| 218 |
-
index + 1, // serial number
|
| 219 |
-
c.name,
|
| 220 |
-
c.invoices?.length || 0,
|
| 221 |
-
formatCurrency(Number(c.totalDebt), currency)
|
| 222 |
-
]),
|
| 223 |
-
true // add serial column
|
| 224 |
-
)}
|
| 225 |
-
</div>
|
| 226 |
-
<div class="section">
|
| 227 |
-
<h2>المخزون</h2>
|
| 228 |
-
${tableHtml(
|
| 229 |
-
["المنتج", "المخزون", "الحد الأدنى", "سعر الوحدة"],
|
| 230 |
-
products.map((p) => [p.name, p.stock, p.lowStockThreshold, formatCurrency(p.price, currency)])
|
| 231 |
-
)}
|
| 232 |
-
</div>
|
| 233 |
-
<div class="section">
|
| 234 |
-
<h2>الموردون</h2>
|
| 235 |
-
${tableHtml(
|
| 236 |
-
["المورد", "المستحقات"],
|
| 237 |
-
suppliers.map((s) => [s.name, formatCurrency(s.totalDebt || 0, currency)])
|
| 238 |
-
)}
|
| 239 |
-
</div>
|
| 240 |
-
`
|
| 241 |
-
);
|
| 242 |
-
};
|
| 243 |
-
|
| 244 |
-
return (
|
| 245 |
-
<div className="space-y-6" dir="rtl">
|
| 246 |
-
|
| 247 |
-
{/* ── Header & actions ── */}
|
| 248 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 249 |
-
<div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
|
| 250 |
-
<div>
|
| 251 |
-
<h3 className="text-xl font-black text-slate-900">التقارير التفصيلية</h3>
|
| 252 |
-
<p className="text-sm text-slate-500 mt-1">تصدير وطباعة ومشاركة الحالة المالية والتشغيلية للشركة.</p>
|
| 253 |
-
</div>
|
| 254 |
-
|
| 255 |
-
{/* Date range picker */}
|
| 256 |
-
<div className="flex flex-wrap items-center gap-3">
|
| 257 |
-
<div className="flex items-center gap-2 bg-slate-50 border border-slate-200 rounded-2xl px-4 py-2">
|
| 258 |
-
<Calendar size={16} className="text-slate-400 shrink-0" />
|
| 259 |
-
<input
|
| 260 |
-
type="date"
|
| 261 |
-
value={startDate}
|
| 262 |
-
onChange={(e) => { setStartDate(e.target.value); setPaymentsPage(1); setInvoicesPage(1); }}
|
| 263 |
-
className="bg-transparent text-sm font-bold text-slate-700 outline-none"
|
| 264 |
-
/>
|
| 265 |
-
<span className="text-slate-400 text-sm">—</span>
|
| 266 |
-
<input
|
| 267 |
-
type="date"
|
| 268 |
-
value={endDate}
|
| 269 |
-
onChange={(e) => { setEndDate(e.target.value); setPaymentsPage(1); setInvoicesPage(1); }}
|
| 270 |
-
className="bg-transparent text-sm font-bold text-slate-700 outline-none"
|
| 271 |
-
/>
|
| 272 |
-
</div>
|
| 273 |
-
</div>
|
| 274 |
-
</div>
|
| 275 |
-
|
| 276 |
-
{/* Action buttons */}
|
| 277 |
-
<div className="flex flex-wrap gap-3 mt-4 pt-4 border-t border-slate-100">
|
| 278 |
-
<ActionButton
|
| 279 |
-
onClick={() =>
|
| 280 |
-
downloadCsv(
|
| 281 |
-
"financial-summary.csv",
|
| 282 |
-
[{ key: "metric", label: "المؤشر" }, { key: "sub", label: "الفترة" }, { key: "value", label: "القيمة" }],
|
| 283 |
-
financialRows
|
| 284 |
-
)
|
| 285 |
-
}
|
| 286 |
-
icon={Download}
|
| 287 |
-
label="تصدير الملخص"
|
| 288 |
-
/>
|
| 289 |
-
<ActionButton onClick={printReport} icon={Printer} label="طباعة التقرير" />
|
| 290 |
-
<button
|
| 291 |
-
type="button"
|
| 292 |
-
onClick={() =>
|
| 293 |
-
openWhatsAppMessage(
|
| 294 |
-
[
|
| 295 |
-
`📊 لقطة تقرير ${companyName}`,
|
| 296 |
-
`📅 الفترة: ${rangeLabel}`,
|
| 297 |
-
`💰 المبيعات: ${formatCurrency(metrics.salesInRange, currency)}`,
|
| 298 |
-
`✅ التحصيلات: ${formatCurrency(metrics.collectionsInRange, currency)}`,
|
| 299 |
-
`🔴 المصروفات: ${formatCurrency(metrics.expensesInRange, currency)}`,
|
| 300 |
-
`📌 المستحقات على العملاء: ${formatCurrency(metrics.outstandingReceivables, currency)}`,
|
| 301 |
-
`📦 أصناف مخزون منخفض: ${metrics.lowStockCount || 0}`,
|
| 302 |
-
].join("\n")
|
| 303 |
-
)
|
| 304 |
-
}
|
| 305 |
-
className="flex h-11 items-center justify-center gap-2 rounded-2xl bg-emerald-600 px-4 font-bold text-white transition hover:bg-emerald-700"
|
| 306 |
-
>
|
| 307 |
-
<Share2 size={16} />
|
| 308 |
-
مشاركة الملخص
|
| 309 |
-
</button>
|
| 310 |
-
</div>
|
| 311 |
-
</section>
|
| 312 |
-
|
| 313 |
-
{/* ── Financial summary ── */}
|
| 314 |
-
<Section title="الملخص المالي" subtitle={`بيانات الفترة: ${rangeLabel}`}>
|
| 315 |
-
<div className="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
|
| 316 |
-
{financialRows.map((row) => (
|
| 317 |
-
<div key={row.metric} className="rounded-2xl border border-slate-100 bg-slate-50 p-4">
|
| 318 |
-
<p className="text-sm font-black text-slate-700">{row.metric}</p>
|
| 319 |
-
<p className="text-xs text-slate-400 font-medium mt-0.5 mb-2">{row.sub}</p>
|
| 320 |
-
<p className={`text-2xl font-black ${row.color}`}>{row.value}</p>
|
| 321 |
-
</div>
|
| 322 |
-
))}
|
| 323 |
-
</div>
|
| 324 |
-
</Section>
|
| 325 |
-
|
| 326 |
-
{/* ── Customer receivables ── */}
|
| 327 |
-
<Section title="مستحقات العملاء" subtitle="الرصيد المتبقي غير المسدد لكل عميل">
|
| 328 |
-
<SimpleTable
|
| 329 |
-
headers={["#", "ال��ميل", "عدد الفواتير", "المستحقات"]}
|
| 330 |
-
rows={customers.map((c, index) => [
|
| 331 |
-
index + 1 + (customersPage - 1) * LIMIT, // <-- correct global index
|
| 332 |
-
c.name,
|
| 333 |
-
c.invoices?.length || 0,
|
| 334 |
-
formatCurrency(c.totalDebt || 0, currency),
|
| 335 |
-
])}
|
| 336 |
-
/>
|
| 337 |
-
<Pagination pagination={customersPagination} page={customersPage} setPage={setCustomersPage} />
|
| 338 |
-
</Section>
|
| 339 |
-
|
| 340 |
-
{/* ── Inventory & Suppliers ── */}
|
| 341 |
-
<div className="grid gap-6 xl:grid-cols-2">
|
| 342 |
-
<Section title="تقارير المخزون" subtitle="الكميات المتاحة وتنبيهات النفاد">
|
| 343 |
-
<SimpleTable
|
| 344 |
-
headers={["المنتج", "المخزون", "الحد الأدنى", "سعر الوحدة", "الحالة"]}
|
| 345 |
-
rows={products.map((p) => [
|
| 346 |
-
p.name,
|
| 347 |
-
p.stock,
|
| 348 |
-
p.lowStockThreshold,
|
| 349 |
-
formatCurrency(p.price, currency),
|
| 350 |
-
p.isLowStock ? (
|
| 351 |
-
<span className="text-xs font-bold text-rose-600 bg-rose-50 px-2 py-1 rounded-lg border border-rose-100">
|
| 352 |
-
مخزون منخفض
|
| 353 |
-
</span>
|
| 354 |
-
) : (
|
| 355 |
-
<span className="text-xs font-bold text-emerald-600 bg-emerald-50 px-2 py-1 rounded-lg border border-emerald-100">
|
| 356 |
-
سليم
|
| 357 |
-
</span>
|
| 358 |
-
),
|
| 359 |
-
])}
|
| 360 |
-
/>
|
| 361 |
-
<Pagination pagination={productsPagination} page={productsPage} setPage={setProductsPage} />
|
| 362 |
-
</Section>
|
| 363 |
-
|
| 364 |
-
<Section title="مستحقات الموردين" subtitle="ما يستحقه المورد من مدفوعات">
|
| 365 |
-
<SimpleTable
|
| 366 |
-
headers={["المورد", "المستحقات"]}
|
| 367 |
-
rows={suppliers.map((s) => [
|
| 368 |
-
s.name,
|
| 369 |
-
formatCurrency(s.totalDebt || 0, currency),
|
| 370 |
-
])}
|
| 371 |
-
/>
|
| 372 |
-
<Pagination pagination={suppliersPagination} page={suppliersPage} setPage={setSuppliersPage} />
|
| 373 |
-
</Section>
|
| 374 |
-
</div>
|
| 375 |
-
|
| 376 |
-
{/* ── Recent activity ── */}
|
| 377 |
-
<Section title="نشاط المدفوعات والفواتير" subtitle={`المعاملات خلال: ${rangeLabel}`}>
|
| 378 |
-
<div className="grid gap-6 xl:grid-cols-2">
|
| 379 |
-
|
| 380 |
-
{/* Payments */}
|
| 381 |
-
<div>
|
| 382 |
-
<p className="text-sm font-black text-slate-600 mb-3">آخر المدفوعات المستلمة</p>
|
| 383 |
-
<SimpleTable
|
| 384 |
-
headers={["العميل", "المبلغ", "التاريخ"]}
|
| 385 |
-
rows={recentPayments.map((p) => [
|
| 386 |
-
p.customerName,
|
| 387 |
-
formatCurrency(p.amount, currency),
|
| 388 |
-
formatDateTime(p.date),
|
| 389 |
-
])}
|
| 390 |
-
/>
|
| 391 |
-
<Pagination pagination={paymentsPagination} page={paymentsPage} setPage={(fn) => { setPaymentsPage(fn); }} />
|
| 392 |
-
</div>
|
| 393 |
-
|
| 394 |
-
{/* Invoices */}
|
| 395 |
-
<div>
|
| 396 |
-
<p className="text-sm font-black text-slate-600 mb-3">آخر الفواتير المنشأة</p>
|
| 397 |
-
<SimpleTable
|
| 398 |
-
headers={["رقم الفاتورة", "العميل", "الحالة", "تاريخ الإنشاء"]}
|
| 399 |
-
rows={recentInvoices.map((inv) => [
|
| 400 |
-
inv.invoiceNumber,
|
| 401 |
-
inv.customerName,
|
| 402 |
-
<StatusBadge status={inv.status} />,
|
| 403 |
-
formatDate(inv.createdAt),
|
| 404 |
-
])}
|
| 405 |
-
/>
|
| 406 |
-
<Pagination pagination={invoicesPagination} page={invoicesPage} setPage={setInvoicesPage} />
|
| 407 |
-
</div>
|
| 408 |
-
|
| 409 |
-
</div>
|
| 410 |
-
</Section>
|
| 411 |
-
</div>
|
| 412 |
-
);
|
| 413 |
-
};
|
| 414 |
-
|
| 415 |
-
// ── Sub-components ────────────────────────────────────────────────────────────
|
| 416 |
-
|
| 417 |
-
const StatusBadge = ({ status }) => {
|
| 418 |
-
const map = {
|
| 419 |
-
paid: { label: "مسددة", cls: "text-emerald-600 bg-emerald-50 border-emerald-100" },
|
| 420 |
-
partial: { label: "جزئية", cls: "text-amber-600 bg-amber-50 border-amber-100" },
|
| 421 |
-
unpaid: { label: "غير مسددة", cls: "text-rose-600 bg-rose-50 border-rose-100" },
|
| 422 |
-
};
|
| 423 |
-
const { label, cls } = map[status] || { label: status, cls: "text-slate-500 bg-slate-50 border-slate-100" };
|
| 424 |
-
return (
|
| 425 |
-
<span className={`text-xs font-bold px-2 py-1 rounded-lg border ${cls}`}>{label}</span>
|
| 426 |
-
);
|
| 427 |
-
};
|
| 428 |
-
|
| 429 |
-
const ActionButton = ({ onClick, icon, label }) => {
|
| 430 |
-
const Icon = icon;
|
| 431 |
-
return (
|
| 432 |
-
<button
|
| 433 |
-
type="button"
|
| 434 |
-
onClick={onClick}
|
| 435 |
-
className="flex h-11 items-center justify-center gap-2 rounded-2xl border border-slate-200 bg-slate-50 px-4 font-bold text-slate-700 transition hover:bg-slate-100"
|
| 436 |
-
>
|
| 437 |
-
<Icon size={16} />
|
| 438 |
-
{label}
|
| 439 |
-
</button>
|
| 440 |
-
);
|
| 441 |
-
};
|
| 442 |
-
|
| 443 |
-
const Section = ({ title, subtitle, children }) => (
|
| 444 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 445 |
-
<div className="mb-5">
|
| 446 |
-
<h3 className="text-lg font-black text-slate-900">{title}</h3>
|
| 447 |
-
{subtitle && <p className="text-xs text-slate-400 font-medium mt-0.5">{subtitle}</p>}
|
| 448 |
-
</div>
|
| 449 |
-
{children}
|
| 450 |
-
</section>
|
| 451 |
-
);
|
| 452 |
-
|
| 453 |
-
const SimpleTable = ({ headers, rows }) => (
|
| 454 |
-
<div className="overflow-x-auto">
|
| 455 |
-
<table className="min-w-full border-collapse">
|
| 456 |
-
<thead>
|
| 457 |
-
<tr className="border-b border-slate-200 text-right text-sm font-bold text-slate-500">
|
| 458 |
-
{headers.map((h) => (
|
| 459 |
-
<th key={h} className="px-3 py-3">{h}</th>
|
| 460 |
-
))}
|
| 461 |
-
</tr>
|
| 462 |
-
</thead>
|
| 463 |
-
<tbody>
|
| 464 |
-
{rows.length ? (
|
| 465 |
-
rows.map((row, i) => (
|
| 466 |
-
<tr key={i} className="border-b border-slate-100 text-sm text-slate-700">
|
| 467 |
-
{row.map((cell, j) => (
|
| 468 |
-
<td key={j} className="px-3 py-3">{cell}</td>
|
| 469 |
-
))}
|
| 470 |
-
</tr>
|
| 471 |
-
))
|
| 472 |
-
) : (
|
| 473 |
-
<tr>
|
| 474 |
-
<td colSpan={headers.length} className="px-3 py-10 text-center text-sm font-medium text-slate-400">
|
| 475 |
-
لا توجد بيانات متاحة.
|
| 476 |
-
</td>
|
| 477 |
-
</tr>
|
| 478 |
-
)}
|
| 479 |
-
</tbody>
|
| 480 |
-
</table>
|
| 481 |
-
</div>
|
| 482 |
-
);
|
| 483 |
-
const tableHtml = (headers, rows, addSerial = false) => `
|
| 484 |
-
<table border="1" cellspacing="0" cellpadding="5">
|
| 485 |
-
<thead>
|
| 486 |
-
<tr>
|
| 487 |
-
${addSerial ? "<th>#</th>" : ""}
|
| 488 |
-
${headers.map((h) => `<th>${h}</th>`).join("")}
|
| 489 |
-
</tr>
|
| 490 |
-
</thead>
|
| 491 |
-
<tbody>
|
| 492 |
-
${rows
|
| 493 |
-
.map(
|
| 494 |
-
(row, index) => `
|
| 495 |
-
<tr>
|
| 496 |
-
${addSerial ? `<td>${index + 1}</td>` : ""}
|
| 497 |
-
${row.map((cell) => `<td>${cell}</td>`).join("")}
|
| 498 |
-
</tr>
|
| 499 |
-
`
|
| 500 |
-
)
|
| 501 |
-
.join("")}
|
| 502 |
-
</tbody>
|
| 503 |
-
</table>
|
| 504 |
-
`;
|
| 505 |
-
export default ReportsWorkspace;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Settings/CompanyProfileTab.jsx
DELETED
|
@@ -1,182 +0,0 @@
|
|
| 1 |
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
import { Building2, MessageCircle, Save, Settings2 } from "lucide-react";
|
| 4 |
-
import { useFormik } from "formik";
|
| 5 |
-
import * as Yup from "yup";
|
| 6 |
-
import { toast } from "react-toastify";
|
| 7 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 8 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 9 |
-
import { openWhatsAppMessage } from "../../utils/whatsapp";
|
| 10 |
-
|
| 11 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 12 |
-
|
| 13 |
-
const CompanyProfileTab = () => {
|
| 14 |
-
const queryClient = useQueryClient();
|
| 15 |
-
const { refreshSession } = useAuth();
|
| 16 |
-
|
| 17 |
-
const { data, isLoading } = useQuery({
|
| 18 |
-
queryKey: ["tenant-profile"],
|
| 19 |
-
queryFn: () => axios.get(`${apiUrl}/auth/tenant-profile`),
|
| 20 |
-
refetchOnWindowFocus: false,
|
| 21 |
-
});
|
| 22 |
-
|
| 23 |
-
const mutation = useMutation({
|
| 24 |
-
mutationFn: (values) => axios.put(`${apiUrl}/auth/tenant-profile`, values),
|
| 25 |
-
onSuccess: async (response) => {
|
| 26 |
-
queryClient.invalidateQueries({ queryKey: ["tenant-profile"] });
|
| 27 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 28 |
-
await refreshSession();
|
| 29 |
-
formik.setValues(toFormValues(response.data.tenant));
|
| 30 |
-
toast.success(response.data.message);
|
| 31 |
-
},
|
| 32 |
-
onError: (error) => {
|
| 33 |
-
toast.error(error.response?.data?.message || "تعذر تحديث ملف الشركة");
|
| 34 |
-
},
|
| 35 |
-
});
|
| 36 |
-
|
| 37 |
-
const formik = useFormik({
|
| 38 |
-
enableReinitialize: true,
|
| 39 |
-
initialValues: toFormValues(data?.data?.tenant),
|
| 40 |
-
validationSchema: Yup.object({
|
| 41 |
-
name: Yup.string().required("اسم الشركة مطلوب").min(3, "3 أحرف على الأقل"),
|
| 42 |
-
ownerName: Yup.string().required("اسم المالك مطلوب"),
|
| 43 |
-
ownerEmail: Yup.string().email("بريد إلكتروني غير صالح").nullable(),
|
| 44 |
-
currency: Yup.string().required("العملة مطلوبة").max(5),
|
| 45 |
-
lowStockThreshold: Yup.number().required("الحد الأدنى مطلوب").min(1, "الحد الأدنى 1"),
|
| 46 |
-
}),
|
| 47 |
-
onSubmit: (values) => mutation.mutate(values),
|
| 48 |
-
});
|
| 49 |
-
|
| 50 |
-
if (isLoading) {
|
| 51 |
-
return <LodderSpinner />;
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
const values = formik.values;
|
| 55 |
-
|
| 56 |
-
return (
|
| 57 |
-
<div className="grid gap-6 xl:grid-cols-[1.25fr_0.75fr]">
|
| 58 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 59 |
-
<div className="mb-6 flex items-center gap-3">
|
| 60 |
-
<div className="flex h-12 w-12 items-center justify-center rounded-2xl border border-emerald-100 bg-emerald-50 text-emerald-700">
|
| 61 |
-
<Building2 size={22} />
|
| 62 |
-
</div>
|
| 63 |
-
<div>
|
| 64 |
-
<h3 className="text-xl font-black text-slate-900">ملف الشركة</h3>
|
| 65 |
-
<p className="text-sm text-slate-500">تحديث هوية المستأجر وقنوات الاتصال وإعدادات المخزون الافتراضية.</p>
|
| 66 |
-
</div>
|
| 67 |
-
</div>
|
| 68 |
-
|
| 69 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4 md:grid-cols-2">
|
| 70 |
-
<Field label="اسم الشركة" name="name" formik={formik} placeholder="شركة تجارية" />
|
| 71 |
-
<Field label="اسم المالك" name="ownerName" formik={formik} placeholder="صاحب العمل" />
|
| 72 |
-
<Field label="بريد المالك" name="ownerEmail" formik={formik} placeholder="owner@example.com" />
|
| 73 |
-
<Field label="هاتف المالك" name="ownerPhone" formik={formik} placeholder="+20..." />
|
| 74 |
-
<Field label="رقم واتساب" name="whatsappNumber" formik={formik} placeholder="+20..." />
|
| 75 |
-
<Field label="العملة" name="currency" formik={formik} placeholder="EGP" />
|
| 76 |
-
<div className="md:col-span-2">
|
| 77 |
-
<Field label="العنوان" name="address" formik={formik} placeholder="عنوان الفرع أو المدينة" />
|
| 78 |
-
</div>
|
| 79 |
-
<Field label="الحد الأدنى الافتراضي للمخزون" name="lowStockThreshold" formik={formik} placeholder="5" type="number" />
|
| 80 |
-
<div className="rounded-2xl border border-slate-200 bg-slate-50 p-4">
|
| 81 |
-
<p className="text-xs font-bold uppercase tracking-wide text-slate-500">معاينة مباشرة</p>
|
| 82 |
-
<p className="mt-2 text-lg font-black text-slate-900">{values.name || "اسم الشركة"}</p>
|
| 83 |
-
<p className="mt-1 text-sm text-slate-600">
|
| 84 |
-
{values.ownerName || "المالك"} • {values.currency || "EGP"} • إعادة طلب عند {values.lowStockThreshold || 0} وحدة
|
| 85 |
-
</p>
|
| 86 |
-
</div>
|
| 87 |
-
<button
|
| 88 |
-
type="submit"
|
| 89 |
-
disabled={mutation.isPending}
|
| 90 |
-
className="md:col-span-2 flex h-12 items-center justify-center gap-2 rounded-2xl bg-emerald-600 font-bold text-white transition hover:bg-emerald-700 disabled:opacity-60"
|
| 91 |
-
>
|
| 92 |
-
<Save size={18} /> حفظ إعدادات الشركة
|
| 93 |
-
</button>
|
| 94 |
-
</form>
|
| 95 |
-
</section>
|
| 96 |
-
|
| 97 |
-
<section className="space-y-6">
|
| 98 |
-
<div className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 99 |
-
<div className="flex items-center gap-3">
|
| 100 |
-
<div className="flex h-12 w-12 items-center justify-center rounded-2xl border border-sky-100 bg-sky-50 text-sky-700">
|
| 101 |
-
<Settings2 size={22} />
|
| 102 |
-
</div>
|
| 103 |
-
<div>
|
| 104 |
-
<h3 className="text-lg font-black text-slate-900">الإعدادات التشغيلية</h3>
|
| 105 |
-
<p className="text-sm text-slate-500">هذه الإعدادات تُستخدم عبر المخزون وتنبيهات لوحة التحكم.</p>
|
| 106 |
-
</div>
|
| 107 |
-
</div>
|
| 108 |
-
|
| 109 |
-
<div className="mt-5 space-y-3">
|
| 110 |
-
<InfoRow label="العملة" value={values.currency || "EGP"} />
|
| 111 |
-
<InfoRow label="حد المخزون المنخفض" value={`${values.lowStockThreshold || 0} وحدة`} />
|
| 112 |
-
<InfoRow label="واتساب" value={values.whatsappNumber || "غير محدد"} />
|
| 113 |
-
</div>
|
| 114 |
-
</div>
|
| 115 |
-
|
| 116 |
-
<div className="rounded-3xl border border-slate-200 bg-slate-950 p-6 text-white shadow-sm">
|
| 117 |
-
<h3 className="text-lg font-black">مشاركة بيانات الشركة</h3>
|
| 118 |
-
<p className="mt-2 text-sm text-slate-300">
|
| 119 |
-
افتح رسالة واتساب جاهزة للإرسال تحتوي على بيانات الاتصال الحالية لشركتك.
|
| 120 |
-
</p>
|
| 121 |
-
<button
|
| 122 |
-
type="button"
|
| 123 |
-
onClick={() =>
|
| 124 |
-
openWhatsAppMessage(
|
| 125 |
-
[
|
| 126 |
-
`بيانات الاتصال - ${values.name || "شركتنا"}`,
|
| 127 |
-
`المالك: ${values.ownerName || "-"}`,
|
| 128 |
-
`الهاتف: ${values.ownerPhone || "-"}`,
|
| 129 |
-
`واتساب: ${values.whatsappNumber || "-"}`,
|
| 130 |
-
`العنوان: ${values.address || "-"}`,
|
| 131 |
-
].join("\n")
|
| 132 |
-
)
|
| 133 |
-
}
|
| 134 |
-
className="mt-5 flex h-11 w-full items-center justify-center gap-2 rounded-2xl bg-white font-bold text-slate-900 transition hover:bg-slate-100"
|
| 135 |
-
>
|
| 136 |
-
<MessageCircle size={18} /> مشاركة عبر واتساب
|
| 137 |
-
</button>
|
| 138 |
-
</div>
|
| 139 |
-
</section>
|
| 140 |
-
</div>
|
| 141 |
-
);
|
| 142 |
-
};
|
| 143 |
-
|
| 144 |
-
const toFormValues = (tenant) => ({
|
| 145 |
-
name: tenant?.name || "",
|
| 146 |
-
ownerName: tenant?.ownerName || "",
|
| 147 |
-
ownerEmail: tenant?.ownerEmail || "",
|
| 148 |
-
ownerPhone: tenant?.ownerPhone || "",
|
| 149 |
-
whatsappNumber: tenant?.whatsappNumber || "",
|
| 150 |
-
address: tenant?.address || "",
|
| 151 |
-
currency: tenant?.settings?.currency || "EGP",
|
| 152 |
-
lowStockThreshold: tenant?.settings?.lowStockThreshold || 5,
|
| 153 |
-
});
|
| 154 |
-
|
| 155 |
-
const Field = ({ label, name, formik, placeholder, type = "text" }) => (
|
| 156 |
-
<div>
|
| 157 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">{label}</label>
|
| 158 |
-
<input
|
| 159 |
-
name={name}
|
| 160 |
-
type={type}
|
| 161 |
-
value={formik.values[name]}
|
| 162 |
-
onChange={formik.handleChange}
|
| 163 |
-
onBlur={formik.handleBlur}
|
| 164 |
-
placeholder={placeholder}
|
| 165 |
-
className={`h-12 w-full rounded-2xl border bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-emerald-500 ${
|
| 166 |
-
formik.touched[name] && formik.errors[name] ? "border-rose-400" : "border-slate-200"
|
| 167 |
-
}`}
|
| 168 |
-
/>
|
| 169 |
-
{formik.touched[name] && formik.errors[name] ? (
|
| 170 |
-
<p className="mt-1 text-xs font-bold text-rose-500">{formik.errors[name]}</p>
|
| 171 |
-
) : null}
|
| 172 |
-
</div>
|
| 173 |
-
);
|
| 174 |
-
|
| 175 |
-
const InfoRow = ({ label, value }) => (
|
| 176 |
-
<div className="flex items-center justify-between rounded-2xl border border-slate-100 bg-slate-50 px-4 py-3">
|
| 177 |
-
<span className="text-sm font-bold text-slate-500">{label}</span>
|
| 178 |
-
<span className="font-black text-slate-900">{value}</span>
|
| 179 |
-
</div>
|
| 180 |
-
);
|
| 181 |
-
|
| 182 |
-
export default CompanyProfileTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Suppliers/SuppliersTab.jsx
DELETED
|
@@ -1,381 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import { Factory, PlusCircle, Search, Trash2, Box, ArrowUpRight } from "lucide-react";
|
| 5 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 6 |
-
import { toast } from "react-toastify";
|
| 7 |
-
import { useFormik } from "formik";
|
| 8 |
-
import * as Yup from "yup";
|
| 9 |
-
|
| 10 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 11 |
-
|
| 12 |
-
const SuppliersTab = () => {
|
| 13 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 14 |
-
const [invoiceModalSupplierId, setInvoiceModalSupplierId] = useState(null);
|
| 15 |
-
const [payModalSupplierId, setPayModalSupplierId] = useState(null);
|
| 16 |
-
const queryClient = useQueryClient();
|
| 17 |
-
|
| 18 |
-
// 1. Fetch Suppliers
|
| 19 |
-
const { data, isLoading } = useQuery({
|
| 20 |
-
queryKey: ["suppliers"],
|
| 21 |
-
queryFn: () => axios.get(`${apiUrl}/suppliers`),
|
| 22 |
-
});
|
| 23 |
-
const suppliers = data?.data || [];
|
| 24 |
-
const filteredSuppliers = suppliers.filter(s => s.name.toLowerCase().includes(searchTerm.toLowerCase()));
|
| 25 |
-
|
| 26 |
-
// 2. Add Supplier Mutation
|
| 27 |
-
const addSupplierFormik = useFormik({
|
| 28 |
-
initialValues: { name: "" },
|
| 29 |
-
validationSchema: Yup.object({
|
| 30 |
-
name: Yup.string()
|
| 31 |
-
.required('مطلوب إدخال اسم المورد')
|
| 32 |
-
.trim()
|
| 33 |
-
.matches(/^[\u0600-\u06FFa-zA-Z\s]+$/, 'يجب أن يحتوي على حروف فقط'),
|
| 34 |
-
}),
|
| 35 |
-
onSubmit: (values, { resetForm }) => {
|
| 36 |
-
axios.post(`${apiUrl}/suppliers/create`, { name: values.name })
|
| 37 |
-
.then(() => {
|
| 38 |
-
queryClient.invalidateQueries(["suppliers"]);
|
| 39 |
-
resetForm();
|
| 40 |
-
toast.success("تم إضافة المورد (الشركة) بنجاح");
|
| 41 |
-
})
|
| 42 |
-
.catch(err => {
|
| 43 |
-
toast.error(err.response?.data?.message || "خطأ في الإضافة");
|
| 44 |
-
});
|
| 45 |
-
}
|
| 46 |
-
});
|
| 47 |
-
|
| 48 |
-
if (isLoading) return <LodderSpinner />;
|
| 49 |
-
|
| 50 |
-
return (
|
| 51 |
-
<div className="space-y-6">
|
| 52 |
-
{/* Top Section: Add & Search */}
|
| 53 |
-
<div className="flex flex-col md:flex-row gap-4 items-center justify-between bg-white p-6 rounded-3xl shadow-sm border border-slate-200">
|
| 54 |
-
{/* Add */}
|
| 55 |
-
<form onSubmit={addSupplierFormik.handleSubmit} className="flex flex-col w-full md:w-1/2 relative gap-1">
|
| 56 |
-
<div className="flex w-full gap-2 bg-slate-50 p-2 rounded-2xl border border-slate-100 relative">
|
| 57 |
-
<input
|
| 58 |
-
name="name"
|
| 59 |
-
type="text"
|
| 60 |
-
placeholder="اسم الشركة/المصنع المورد الجديد..."
|
| 61 |
-
value={addSupplierFormik.values.name}
|
| 62 |
-
onChange={addSupplierFormik.handleChange}
|
| 63 |
-
onBlur={addSupplierFormik.handleBlur}
|
| 64 |
-
className={`flex-1 bg-white border ${addSupplierFormik.touched.name && addSupplierFormik.errors.name ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-indigo-500'} rounded-xl px-4 py-3 outline-none focus:ring-2 font-bold text-slate-700 w-full`}
|
| 65 |
-
/>
|
| 66 |
-
<button
|
| 67 |
-
type="submit"
|
| 68 |
-
disabled={addSupplierFormik.isSubmitting}
|
| 69 |
-
className="bg-indigo-600 hover:bg-indigo-700 text-white px-6 py-3 rounded-xl font-bold transition-all disabled:opacity-50 flex items-center justify-center gap-2 whitespace-nowrap"
|
| 70 |
-
>
|
| 71 |
-
<PlusCircle size={20} /> إضافة
|
| 72 |
-
</button>
|
| 73 |
-
</div>
|
| 74 |
-
{addSupplierFormik.touched.name && addSupplierFormik.errors.name && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold">{addSupplierFormik.errors.name}</span>}
|
| 75 |
-
</form>
|
| 76 |
-
|
| 77 |
-
{/* Search */}
|
| 78 |
-
<div className="relative w-full md:w-1/2 mt-4 md:mt-0">
|
| 79 |
-
<div className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400 bg-slate-100 p-1.5 rounded-lg">
|
| 80 |
-
<Search size={18} />
|
| 81 |
-
</div>
|
| 82 |
-
<input
|
| 83 |
-
type="text"
|
| 84 |
-
placeholder="ابحث عن مورد مسجل لفتح حسابه..."
|
| 85 |
-
value={searchTerm}
|
| 86 |
-
onChange={(e) => setSearchTerm(e.target.value)}
|
| 87 |
-
className="w-full pr-14 pl-4 py-3.5 bg-slate-50 border border-slate-200 rounded-xl shadow-inner focus:ring-2 focus:ring-indigo-500 outline-none font-medium"
|
| 88 |
-
/>
|
| 89 |
-
</div>
|
| 90 |
-
</div>
|
| 91 |
-
|
| 92 |
-
{/* Suppliers Table */}
|
| 93 |
-
<div className="bg-white rounded-3xl shadow-sm border border-slate-200 overflow-hidden">
|
| 94 |
-
<div className="overflow-x-auto max-h-[500px] overflow-y-auto custom-scroll relative">
|
| 95 |
-
<table className="w-full text-right border-collapse">
|
| 96 |
-
<thead className="sticky top-0 bg-slate-50 border-b border-slate-200 z-10 shadow-sm">
|
| 97 |
-
<tr>
|
| 98 |
-
<th className="p-4 font-bold text-slate-600">��لشركة / المورد</th>
|
| 99 |
-
<th className="p-4 font-bold text-slate-600 text-center">إجمالي الديون علينا</th>
|
| 100 |
-
<th className="p-4 font-bold text-slate-600 justify-center flex">إجراءات الاستلام والدفع</th>
|
| 101 |
-
</tr>
|
| 102 |
-
</thead>
|
| 103 |
-
<tbody className="divide-y divide-slate-100">
|
| 104 |
-
{filteredSuppliers.map(supplier => (
|
| 105 |
-
<tr key={supplier._id} className="hover:bg-slate-50 transition-colors">
|
| 106 |
-
<td className="p-4 font-bold text-slate-800 flex items-center gap-3">
|
| 107 |
-
<div className="w-12 h-12 rounded-xl bg-indigo-50 border border-indigo-100 text-indigo-500 flex items-center justify-center shrink-0 shadow-sm">
|
| 108 |
-
<Factory size={24} />
|
| 109 |
-
</div>
|
| 110 |
-
<span className="text-xl">{supplier.name}</span>
|
| 111 |
-
</td>
|
| 112 |
-
<td className="p-4 text-center">
|
| 113 |
-
<span className="bg-rose-50 px-5 py-2 rounded-xl border border-rose-200 inline-block shadow-[0_2px_10px_-4px_rgba(225,29,72,0.2)]">
|
| 114 |
-
<span className="font-black text-rose-600 text-2xl">{Number(supplier.totalDebt || 0).toFixed(2)}</span>
|
| 115 |
-
<span className="text-sm text-rose-400 mr-1">ج.م</span>
|
| 116 |
-
</span>
|
| 117 |
-
</td>
|
| 118 |
-
<td className="p-4">
|
| 119 |
-
<div className="flex justify-center gap-3 w-full">
|
| 120 |
-
<button
|
| 121 |
-
onClick={() => setInvoiceModalSupplierId(supplier._id)}
|
| 122 |
-
className="bg-emerald-500 text-white font-bold px-4 py-2.5 rounded-xl hover:bg-emerald-600 flex items-center gap-2 shadow-sm transition-all hover:-translate-y-0.5 whitespace-nowrap"
|
| 123 |
-
>
|
| 124 |
-
<Box size={18} /> استلام بضاعة للمخزن
|
| 125 |
-
</button>
|
| 126 |
-
<button
|
| 127 |
-
onClick={() => setPayModalSupplierId(supplier._id)}
|
| 128 |
-
className="bg-slate-800 text-white font-bold px-4 py-2.5 rounded-xl hover:bg-slate-900 flex items-center gap-2 shadow-sm transition-all hover:-translate-y-0.5 whitespace-nowrap"
|
| 129 |
-
>
|
| 130 |
-
<ArrowUpRight size={18} className="text-amber-400" /> تسديد دفعة نقدية
|
| 131 |
-
</button>
|
| 132 |
-
<DeleteSupplierButton supplierId={supplier._id} />
|
| 133 |
-
</div>
|
| 134 |
-
</td>
|
| 135 |
-
</tr>
|
| 136 |
-
))}
|
| 137 |
-
{filteredSuppliers.length === 0 && (
|
| 138 |
-
<tr>
|
| 139 |
-
<td colSpan="3" className="p-16 text-center text-slate-400">
|
| 140 |
-
<div className="w-24 h-24 bg-slate-50 rounded-full flex items-center justify-center mx-auto mb-4 border border-slate-100 shadow-sm">
|
| 141 |
-
<Factory size={40} className="text-slate-300" />
|
| 142 |
-
</div>
|
| 143 |
-
<p className="text-xl font-bold text-slate-500">لا توجد شركات موردين مسجلة أو مطابقة للبحث</p>
|
| 144 |
-
</td>
|
| 145 |
-
</tr>
|
| 146 |
-
)}
|
| 147 |
-
</tbody>
|
| 148 |
-
</table>
|
| 149 |
-
</div>
|
| 150 |
-
</div>
|
| 151 |
-
|
| 152 |
-
{/* Sub Modals */}
|
| 153 |
-
{invoiceModalSupplierId && <SupplierInvoiceModal supplierId={invoiceModalSupplierId} onClose={() => setInvoiceModalSupplierId(null)} />}
|
| 154 |
-
{payModalSupplierId && <SupplierPayModal supplierId={payModalSupplierId} onClose={() => setPayModalSupplierId(null)} />}
|
| 155 |
-
</div>
|
| 156 |
-
);
|
| 157 |
-
};
|
| 158 |
-
|
| 159 |
-
// --- Delete Button Component ---
|
| 160 |
-
const DeleteSupplierButton = ({ supplierId }) => {
|
| 161 |
-
const [isConfirming, setIsConfirming] = useState(false);
|
| 162 |
-
const queryClient = useQueryClient();
|
| 163 |
-
|
| 164 |
-
const { mutate, isPending } = useMutation({
|
| 165 |
-
mutationFn: () => axios.delete(`${apiUrl}/suppliers/${supplierId}`),
|
| 166 |
-
onSuccess: () => {
|
| 167 |
-
queryClient.invalidateQueries(["suppliers"]);
|
| 168 |
-
toast.success("تم مسح المورد");
|
| 169 |
-
}
|
| 170 |
-
});
|
| 171 |
-
|
| 172 |
-
if (isConfirming) {
|
| 173 |
-
return (
|
| 174 |
-
<div className="flex items-center gap-1.5 bg-rose-50 p-1.5 rounded-xl border border-rose-200">
|
| 175 |
-
<button onClick={() => mutate()} disabled={isPending} className="bg-rose-500 text-white px-4 py-2 rounded-lg text-sm font-bold hover:bg-rose-600">تأكيد</button>
|
| 176 |
-
<button onClick={() => setIsConfirming(false)} className="bg-white text-slate-500 border border-slate-200 px-4 py-2 rounded-lg text-sm hover:text-slate-800">إلغاء</button>
|
| 177 |
-
</div>
|
| 178 |
-
);
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
return (
|
| 182 |
-
<button onClick={() => setIsConfirming(true)} className="p-3 text-rose-500 bg-rose-50 rounded-xl hover:bg-rose-600 hover:text-white transition-colors border border-rose-100 shadow-[0_2px_10px_-4px_rgba(225,29,72,0.2)]" title="حذف بالكامل">
|
| 183 |
-
<Trash2 size={20} />
|
| 184 |
-
</button>
|
| 185 |
-
);
|
| 186 |
-
};
|
| 187 |
-
|
| 188 |
-
// --- Supplier Invoice Modal (Receive Goods) ---
|
| 189 |
-
const SupplierInvoiceModal = ({ supplierId, onClose }) => {
|
| 190 |
-
const queryClient = useQueryClient();
|
| 191 |
-
|
| 192 |
-
const { data: productsData } = useQuery({
|
| 193 |
-
queryKey: ["products"],
|
| 194 |
-
queryFn: () => axios.get(`${apiUrl}/products`),
|
| 195 |
-
});
|
| 196 |
-
const products = productsData?.data || [];
|
| 197 |
-
|
| 198 |
-
const formik = useFormik({
|
| 199 |
-
initialValues: {
|
| 200 |
-
items: [{ name: "", quantity: "", price: "", productId: null }]
|
| 201 |
-
},
|
| 202 |
-
validationSchema: Yup.object({
|
| 203 |
-
items: Yup.array().of(
|
| 204 |
-
Yup.object().shape({
|
| 205 |
-
name: Yup.string().required('مطلوب').trim(),
|
| 206 |
-
quantity: Yup.number().required('مطلوب').typeError('أرقام فقط').positive('أكبر من 0'),
|
| 207 |
-
price: Yup.number().required('مطلوب').typeError('أرقام فقط').min(0, 'سعر غير صالح')
|
| 208 |
-
})
|
| 209 |
-
)
|
| 210 |
-
}),
|
| 211 |
-
onSubmit: async (values, { setSubmitting }) => {
|
| 212 |
-
try {
|
| 213 |
-
await axios.post(`${apiUrl}/suppliers/${supplierId}/items`, { items: values.items });
|
| 214 |
-
queryClient.invalidateQueries(["suppliers"]);
|
| 215 |
-
queryClient.invalidateQueries(["products"]);
|
| 216 |
-
toast.success("تم استلام البضاعة، زيادتها المخزن، وتقييد ثمنها بالمديونية");
|
| 217 |
-
onClose();
|
| 218 |
-
} catch (err) {
|
| 219 |
-
toast.error(err.response?.data?.message || "خطأ");
|
| 220 |
-
} finally {
|
| 221 |
-
setSubmitting(false);
|
| 222 |
-
}
|
| 223 |
-
}
|
| 224 |
-
});
|
| 225 |
-
|
| 226 |
-
const items = formik.values.items;
|
| 227 |
-
|
| 228 |
-
return (
|
| 229 |
-
<div className="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4" dir="rtl">
|
| 230 |
-
<div className="bg-white rounded-3xl w-full max-w-4xl shadow-2xl border border-slate-200 p-6 md:p-8 animate-in zoom-in-95 duration-200">
|
| 231 |
-
<h2 className="text-2xl font-black text-slate-800 mb-6 flex items-center gap-3 border-b border-slate-100 pb-4">
|
| 232 |
-
<div className="p-3 bg-emerald-50 rounded-xl text-emerald-600 border border-emerald-100">
|
| 233 |
-
<Box size={24}/>
|
| 234 |
-
</div>
|
| 235 |
-
الاستلام وإذن إضافة للمخازن
|
| 236 |
-
</h2>
|
| 237 |
-
|
| 238 |
-
<p className="text-slate-500 mb-4 font-bold bg-slate-50 p-3 rounded-lg border border-slate-100">الأصناف المسجلة هنا سيتم زيادة كميتها (Stock) آلياً داخل المخزن، وسيُسجل إجمالي قيمتها كديون علينا لصالح هذا المورد.</p>
|
| 239 |
-
|
| 240 |
-
<div className="space-y-4 mb-6 max-h-[50vh] overflow-y-auto custom-scroll p-1 pr-2">
|
| 241 |
-
{items.map((item, index) => {
|
| 242 |
-
const touched = formik.touched.items?.[index] || {};
|
| 243 |
-
const errors = formik.errors.items?.[index] || {};
|
| 244 |
-
return (
|
| 245 |
-
<div key={index} className="flex flex-col md:flex-row gap-4 md:gap-3 items-start md:items-center bg-slate-50 border border-slate-200 p-4 rounded-2xl relative">
|
| 246 |
-
<div className="flex-1 w-full relative">
|
| 247 |
-
<label className="block text-sm font-bold text-slate-600 mb-1.5">أبحث بالصنف لإضافته للمخزن <span className="text-rose-500">*</span></label>
|
| 248 |
-
<input
|
| 249 |
-
list={`sup-products-${index}`}
|
| 250 |
-
type="text"
|
| 251 |
-
name={`items[${index}].name`}
|
| 252 |
-
value={item.name}
|
| 253 |
-
onBlur={formik.handleBlur}
|
| 254 |
-
onChange={e => {
|
| 255 |
-
const val = e.target.value;
|
| 256 |
-
formik.setFieldValue(`items[${index}].name`, val);
|
| 257 |
-
const matchedProduct = products.find(p => p.name === val);
|
| 258 |
-
if (matchedProduct) {
|
| 259 |
-
formik.setFieldValue(`items[${index}].productId`, matchedProduct._id);
|
| 260 |
-
} else {
|
| 261 |
-
formik.setFieldValue(`items[${index}].productId`, null);
|
| 262 |
-
}
|
| 263 |
-
}}
|
| 264 |
-
className={`w-full bg-white border ${touched.name && errors.name ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl px-4 py-3 outline-none focus:ring-2 font-bold`}
|
| 265 |
-
placeholder="مثال: سلك السويدي..."
|
| 266 |
-
/>
|
| 267 |
-
<datalist id={`sup-products-${index}`}>
|
| 268 |
-
{products.map(p => <option key={p._id} value={p.name} />)}
|
| 269 |
-
</datalist>
|
| 270 |
-
{touched.name && errors.name && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold whitespace-nowrap">{errors.name}</span>}
|
| 271 |
-
</div>
|
| 272 |
-
<div className="w-full md:w-32 relative">
|
| 273 |
-
<label className="block text-sm font-bold text-slate-600 mb-1.5">الكمية المستلمة <span className="text-rose-500">*</span></label>
|
| 274 |
-
<input
|
| 275 |
-
type="number"
|
| 276 |
-
name={`items[${index}].quantity`}
|
| 277 |
-
value={item.quantity}
|
| 278 |
-
onChange={formik.handleChange}
|
| 279 |
-
onBlur={formik.handleBlur}
|
| 280 |
-
className={`w-full bg-white border ${touched.quantity && errors.quantity ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl px-4 py-3 outline-none focus:ring-2 text-center font-bold text-emerald-600`}
|
| 281 |
-
placeholder="1"
|
| 282 |
-
dir="ltr"
|
| 283 |
-
/>
|
| 284 |
-
{touched.quantity && errors.quantity && <span className="absolute -bottom-5 right-0 text-rose-500 text-[10px] sm:text-xs font-bold">{errors.quantity}</span>}
|
| 285 |
-
</div>
|
| 286 |
-
<div className="w-full md:w-36 relative">
|
| 287 |
-
<label className="block text-sm font-bold text-slate-600 mb-1.5">سعر جملة الشراء <span className="text-rose-500">*</span></label>
|
| 288 |
-
<input
|
| 289 |
-
type="number"
|
| 290 |
-
step="0.01"
|
| 291 |
-
name={`items[${index}].price`}
|
| 292 |
-
value={item.price}
|
| 293 |
-
onChange={formik.handleChange}
|
| 294 |
-
onBlur={formik.handleBlur}
|
| 295 |
-
className={`w-full bg-white border ${touched.price && errors.price ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl px-4 py-3 outline-none focus:ring-2 text-center font-black text-rose-600`}
|
| 296 |
-
placeholder="0.00"
|
| 297 |
-
dir="ltr"
|
| 298 |
-
/>
|
| 299 |
-
{touched.price && errors.price && <span className="absolute -bottom-5 right-0 text-rose-500 text-[10px] sm:text-xs font-bold">{errors.price}</span>}
|
| 300 |
-
</div>
|
| 301 |
-
<button type="button" title="حذف هذا السطر" disabled={items.length === 1} onClick={() => { if(items.length > 1) formik.setFieldValue('items', items.filter((_, i) => i !== index)) }} className="p-3 md:mt-6 bg-white text-rose-500 hover:text-white hover:bg-rose-500 disabled:opacity-30 disabled:hover:bg-white disabled:hover:text-rose-500 border border-slate-200 rounded-xl transition-colors"><Trash2 size={24}/></button>
|
| 302 |
-
</div>
|
| 303 |
-
);
|
| 304 |
-
})}
|
| 305 |
-
<button type="button" onClick={() => formik.setFieldValue('items', [...items, { name: "", quantity: "", price: "", productId: null }])} className="w-full py-4 border-2 border-dashed border-emerald-300 text-emerald-600 font-bold rounded-2xl mt-4 hover:bg-emerald-50 transition-colors flex items-center justify-center gap-2">
|
| 306 |
-
<PlusCircle size={20} /> أضف صنف آخر
|
| 307 |
-
</button>
|
| 308 |
-
</div>
|
| 309 |
-
|
| 310 |
-
<div className="flex gap-4 border-t border-slate-100 pt-4">
|
| 311 |
-
<button onClick={onClose} disabled={formik.isSubmitting} className="flex-1 py-4 bg-slate-100 text-slate-600 font-bold rounded-xl hover:bg-slate-200 border border-slate-200 disabled:opacity-50">إلغاء</button>
|
| 312 |
-
<button onClick={formik.submitForm} disabled={formik.isSubmitting} className="flex-[2] py-4 bg-emerald-600 text-white font-black text-lg rounded-xl hover:bg-emerald-700 disabled:opacity-50 shadow-lg shadow-emerald-500/30 hover:-translate-y-0.5 transition-all">اعتمد ومون المخزن وسجل المديونية</button>
|
| 313 |
-
</div>
|
| 314 |
-
</div>
|
| 315 |
-
</div>
|
| 316 |
-
);
|
| 317 |
-
};
|
| 318 |
-
|
| 319 |
-
// --- Supplier Pay Modal (Pay Cash) ---
|
| 320 |
-
const SupplierPayModal = ({ supplierId, onClose }) => {
|
| 321 |
-
const queryClient = useQueryClient();
|
| 322 |
-
|
| 323 |
-
const formik = useFormik({
|
| 324 |
-
initialValues: { amount: "" },
|
| 325 |
-
validationSchema: Yup.object({
|
| 326 |
-
amount: Yup.number().required('مطلوب إدخال المبلغ').typeError('الرجاء إدخال أرقام').positive('يجب أن يكون أكبر من الصفر')
|
| 327 |
-
}),
|
| 328 |
-
onSubmit: (values) => {
|
| 329 |
-
axios.post(`${apiUrl}/suppliers/${supplierId}/pay`, { amount: values.amount })
|
| 330 |
-
.then(() => {
|
| 331 |
-
// Also log an expense automatically to deduct from drawer!
|
| 332 |
-
axios.post(`${apiUrl}/expenses`, { title: "تسديد دفعة مورد كاش", amount: values.amount }).then(() => {
|
| 333 |
-
queryClient.invalidateQueries(["dailyReport"]); // Update drawer
|
| 334 |
-
});
|
| 335 |
-
queryClient.invalidateQueries(["suppliers"]);
|
| 336 |
-
toast.success("تم تسديد المبلغ وتسجيله في مصروفات الدرج آلياً");
|
| 337 |
-
onClose();
|
| 338 |
-
})
|
| 339 |
-
.catch(err => {
|
| 340 |
-
toast.error(err.response?.data?.message || "خطأ");
|
| 341 |
-
});
|
| 342 |
-
}
|
| 343 |
-
});
|
| 344 |
-
|
| 345 |
-
return (
|
| 346 |
-
<div className="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4" dir="rtl">
|
| 347 |
-
<div className="bg-white rounded-3xl w-full max-w-sm shadow-2xl border border-slate-200 p-8 animate-in zoom-in-95 duration-200">
|
| 348 |
-
<h2 className="text-xl font-black text-slate-800 mb-6 flex items-center gap-3">
|
| 349 |
-
<div className="p-3 bg-rose-50 rounded-xl text-rose-500 border border-rose-100">
|
| 350 |
-
<ArrowUpRight size={24}/>
|
| 351 |
-
</div>
|
| 352 |
-
خروج كاش للمورد
|
| 353 |
-
</h2>
|
| 354 |
-
<form onSubmit={formik.handleSubmit} className="mb-6">
|
| 355 |
-
<div className="relative">
|
| 356 |
-
<label className="block text-sm font-bold text-slate-600 mb-2">المبلغ الخارج من درج الخزنة (ج.م) <span className="text-rose-500">*</span></label>
|
| 357 |
-
<input
|
| 358 |
-
name="amount"
|
| 359 |
-
type="number"
|
| 360 |
-
step="0.01"
|
| 361 |
-
value={formik.values.amount}
|
| 362 |
-
onChange={formik.handleChange}
|
| 363 |
-
onBlur={formik.handleBlur}
|
| 364 |
-
className={`w-full bg-slate-50 border ${formik.touched.amount && formik.errors.amount ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:border-amber-500'} rounded-xl px-4 py-4 outline-none focus:ring-2 text-center text-3xl font-black text-amber-600 placeholder:text-slate-300 transition-colors`}
|
| 365 |
-
placeholder="0.00"
|
| 366 |
-
dir="ltr"
|
| 367 |
-
/>
|
| 368 |
-
{formik.touched.amount && formik.errors.amount && <span className="absolute -bottom-5 right-2 text-rose-500 text-xs font-bold">{formik.errors.amount}</span>}
|
| 369 |
-
</div>
|
| 370 |
-
<p className="text-xs text-rose-500 font-bold mt-8 text-center bg-rose-50 p-2 rounded-lg border border-rose-100">تحذير: هذا الكاش سيتم خصمه فوراً من صافي يومية الدرج كـ "مصروف مالي" لضبط الحسابات.</p>
|
| 371 |
-
<div className="flex gap-3 mt-6">
|
| 372 |
-
<button type="button" onClick={onClose} disabled={formik.isSubmitting} className="flex-1 py-3.5 bg-slate-100 text-slate-600 font-bold rounded-xl hover:bg-slate-200 border border-slate-200 disabled:opacity-50">إلغاء الأمر</button>
|
| 373 |
-
<button type="submit" disabled={formik.isSubmitting} className="flex-[2] py-3.5 bg-slate-900 text-white font-bold rounded-xl hover:bg-slate-800 disabled:opacity-50 hover:-translate-y-0.5 transition-all shadow-lg shadow-slate-900/20 text-lg">سحب وتسديد</button>
|
| 374 |
-
</div>
|
| 375 |
-
</form>
|
| 376 |
-
</div>
|
| 377 |
-
</div>
|
| 378 |
-
);
|
| 379 |
-
};
|
| 380 |
-
|
| 381 |
-
export default SuppliersTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Suppliers/SuppliersWorkspace.jsx
DELETED
|
@@ -1,417 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
| 4 |
-
import { ArrowUpRight, Boxes, Factory, Loader2, MessageCircle, PlusCircle, Search, Trash2 } from "lucide-react";
|
| 5 |
-
import { useFormik } from "formik";
|
| 6 |
-
import * as Yup from "yup";
|
| 7 |
-
import { toast } from "react-toastify";
|
| 8 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 9 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 10 |
-
import { formatCurrency } from "../../utils/formatters";
|
| 11 |
-
import { openWhatsAppMessage } from "../../utils/whatsapp";
|
| 12 |
-
import Pagination from "../../common/Pagination";
|
| 13 |
-
|
| 14 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 15 |
-
|
| 16 |
-
const SuppliersWorkspace = () => {
|
| 17 |
-
const { user } = useAuth();
|
| 18 |
-
const currency = user?.tenant?.settings?.currency || "EGP";
|
| 19 |
-
const companyName = user?.tenant?.name || "الشركة";
|
| 20 |
-
const queryClient = useQueryClient();
|
| 21 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 22 |
-
const [page, setPage] = useState(1);
|
| 23 |
-
const [purchaseSupplier, setPurchaseSupplier] = useState(null);
|
| 24 |
-
const [paymentSupplier, setPaymentSupplier] = useState(null);
|
| 25 |
-
|
| 26 |
-
const { data: response, isLoading } = useQuery({
|
| 27 |
-
queryKey: ["suppliers", page, searchTerm],
|
| 28 |
-
queryFn: () => axios.get(`${apiUrl}/suppliers`, {
|
| 29 |
-
params: {
|
| 30 |
-
page,
|
| 31 |
-
limit: 5,
|
| 32 |
-
search: searchTerm
|
| 33 |
-
}
|
| 34 |
-
}),
|
| 35 |
-
refetchOnWindowFocus: false,
|
| 36 |
-
});
|
| 37 |
-
const { data: productsData } = useQuery({
|
| 38 |
-
queryKey: ["products", "all"],
|
| 39 |
-
queryFn: () => axios.get(`${apiUrl}/products`, { params: { limit: 1000 } }),
|
| 40 |
-
refetchOnWindowFocus: false,
|
| 41 |
-
});
|
| 42 |
-
|
| 43 |
-
const createMutation = useMutation({
|
| 44 |
-
mutationFn: (values) => axios.post(`${apiUrl}/suppliers/create`, values),
|
| 45 |
-
onSuccess: () => {
|
| 46 |
-
formik.resetForm();
|
| 47 |
-
queryClient.invalidateQueries({ queryKey: ["suppliers"] });
|
| 48 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 49 |
-
toast.success("تم إنشاء المورد");
|
| 50 |
-
},
|
| 51 |
-
onError: (error) => {
|
| 52 |
-
toast.error(error.response?.data?.message || "تعذر إنشاء المورد");
|
| 53 |
-
},
|
| 54 |
-
});
|
| 55 |
-
|
| 56 |
-
const deleteMutation = useMutation({
|
| 57 |
-
mutationFn: (supplierId) => axios.delete(`${apiUrl}/suppliers/${supplierId}`),
|
| 58 |
-
onSuccess: () => {
|
| 59 |
-
queryClient.invalidateQueries({ queryKey: ["suppliers"] });
|
| 60 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 61 |
-
toast.success("تم حذف المورد");
|
| 62 |
-
},
|
| 63 |
-
onError: (error) => {
|
| 64 |
-
toast.error(error.response?.data?.message || "تعذر حذف المورد");
|
| 65 |
-
},
|
| 66 |
-
});
|
| 67 |
-
|
| 68 |
-
const formik = useFormik({
|
| 69 |
-
initialValues: {
|
| 70 |
-
name: "",
|
| 71 |
-
phone: "",
|
| 72 |
-
whatsappNumber: "",
|
| 73 |
-
},
|
| 74 |
-
validationSchema: Yup.object({
|
| 75 |
-
name: Yup.string().required("اسم المورد مطلوب").min(2, "حرفين على الأقل"),
|
| 76 |
-
}),
|
| 77 |
-
onSubmit: (values) => createMutation.mutate(values),
|
| 78 |
-
});
|
| 79 |
-
|
| 80 |
-
const suppliers = response?.data?.data ?? [];
|
| 81 |
-
const pagination = response?.data?.pagination || { total: 0, totalPages: 0 };
|
| 82 |
-
|
| 83 |
-
const handleSearchChange = (e) => {
|
| 84 |
-
setSearchTerm(e.target.value);
|
| 85 |
-
setPage(1);
|
| 86 |
-
};
|
| 87 |
-
|
| 88 |
-
if (isLoading) {
|
| 89 |
-
return <LodderSpinner />;
|
| 90 |
-
}
|
| 91 |
-
|
| 92 |
-
return (
|
| 93 |
-
<div className="space-y-6">
|
| 94 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 95 |
-
<div className="mb-5 flex items-center gap-3">
|
| 96 |
-
<div className="flex h-12 w-12 items-center justify-center rounded-2xl border border-indigo-100 bg-indigo-50 text-indigo-700">
|
| 97 |
-
<Factory size={22} />
|
| 98 |
-
</div>
|
| 99 |
-
<div>
|
| 100 |
-
<h3 className="text-lg font-black text-slate-900">دليل الموردين</h3>
|
| 101 |
-
<p className="text-sm text-slate-500">إدارة سجلات الموردين ومتابعة المستحقات بوضوح.</p>
|
| 102 |
-
</div>
|
| 103 |
-
</div>
|
| 104 |
-
|
| 105 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4 md:grid-cols-3 xl:grid-cols-4">
|
| 106 |
-
<Field label="اسم المورد" name="name" formik={formik} />
|
| 107 |
-
<Field label="الهاتف" name="phone" formik={formik} />
|
| 108 |
-
<Field label="واتساب" name="whatsappNumber" formik={formik} />
|
| 109 |
-
<button
|
| 110 |
-
type="submit"
|
| 111 |
-
disabled={createMutation.isPending}
|
| 112 |
-
className="mt-[22px] flex h-12 items-center justify-center gap-2 rounded-2xl bg-indigo-600 px-5 font-bold text-white transition hover:bg-indigo-700 disabled:opacity-60"
|
| 113 |
-
>
|
| 114 |
-
{createMutation.isPending ? <Loader2 size={18} className="animate-spin" /> : <PlusCircle size={18} />}
|
| 115 |
-
إضافة مورد
|
| 116 |
-
</button>
|
| 117 |
-
</form>
|
| 118 |
-
</section>
|
| 119 |
-
|
| 120 |
-
<section className="rounded-3xl border border-slate-200 bg-white shadow-sm">
|
| 121 |
-
<div className="flex flex-col gap-4 border-b border-slate-100 p-5 md:flex-row md:items-center md:justify-between">
|
| 122 |
-
<div>
|
| 123 |
-
<h3 className="text-lg font-black text-slate-900">أرصدة الموردين المعلقة</h3>
|
| 124 |
-
<p className="text-sm text-slate-500">مشاركة ملخصات الموردين أو حذف السجلات القديمة.</p>
|
| 125 |
-
</div>
|
| 126 |
-
<div className="relative w-full md:max-w-sm">
|
| 127 |
-
<Search size={18} className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400" />
|
| 128 |
-
<input
|
| 129 |
-
type="text"
|
| 130 |
-
value={searchTerm}
|
| 131 |
-
onChange={handleSearchChange}
|
| 132 |
-
placeholder="البحث في الموردين..."
|
| 133 |
-
className="h-11 w-full rounded-2xl border border-slate-200 bg-slate-50 pr-11 pl-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-indigo-500"
|
| 134 |
-
/>
|
| 135 |
-
</div>
|
| 136 |
-
</div>
|
| 137 |
-
|
| 138 |
-
<div className="overflow-x-auto">
|
| 139 |
-
<table className="min-w-full border-collapse">
|
| 140 |
-
<thead>
|
| 141 |
-
<tr className="border-b border-slate-200 text-right text-sm font-bold text-slate-500">
|
| 142 |
-
<th className="px-4 py-3">المورد</th>
|
| 143 |
-
<th className="px-4 py-3">المستحقات</th>
|
| 144 |
-
<th className="px-4 py-3">الإجراءات</th>
|
| 145 |
-
</tr>
|
| 146 |
-
</thead>
|
| 147 |
-
<tbody>
|
| 148 |
-
{suppliers.length ? (
|
| 149 |
-
suppliers.map((supplier) => (
|
| 150 |
-
<tr key={supplier._id} className="border-b border-slate-100 text-sm text-slate-700">
|
| 151 |
-
<td className="px-4 py-4">
|
| 152 |
-
<div className="flex items-center gap-3">
|
| 153 |
-
<div className="flex h-10 w-10 items-center justify-center rounded-2xl bg-indigo-50 text-indigo-700">
|
| 154 |
-
<Factory size={18} />
|
| 155 |
-
</div>
|
| 156 |
-
<div>
|
| 157 |
-
<p className="font-bold text-slate-900">{supplier.name}</p>
|
| 158 |
-
<p className="text-xs text-slate-500">{supplier.whatsappNumber || supplier.phone || "لا يوجد رقم تواصل"}</p>
|
| 159 |
-
</div>
|
| 160 |
-
</div>
|
| 161 |
-
</td>
|
| 162 |
-
<td className="px-4 py-4 font-bold text-rose-600">
|
| 163 |
-
{formatCurrency(supplier.totalDebt || 0, currency)}
|
| 164 |
-
</td>
|
| 165 |
-
<td className="px-4 py-4">
|
| 166 |
-
<div className="flex flex-wrap gap-2">
|
| 167 |
-
<button
|
| 168 |
-
type="button"
|
| 169 |
-
onClick={() =>
|
| 170 |
-
openWhatsAppMessage(
|
| 171 |
-
[
|
| 172 |
-
`ملخص مورد ${companyName}`,
|
| 173 |
-
`المورد: ${supplier.name}`,
|
| 174 |
-
`المستحقات: ${formatCurrency(supplier.totalDebt || 0, currency)}`,
|
| 175 |
-
`واتساب: ${supplier.whatsappNumber || supplier.phone || "-"}`,
|
| 176 |
-
].join("\n")
|
| 177 |
-
)
|
| 178 |
-
}
|
| 179 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl bg-emerald-600 px-4 font-bold text-white transition hover:bg-emerald-700"
|
| 180 |
-
>
|
| 181 |
-
<MessageCircle size={16} />
|
| 182 |
-
مشاركة
|
| 183 |
-
</button>
|
| 184 |
-
<button
|
| 185 |
-
type="button"
|
| 186 |
-
onClick={() => setPurchaseSupplier(supplier)}
|
| 187 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl border border-blue-100 bg-blue-50 px-4 font-bold text-blue-700 transition hover:bg-blue-100"
|
| 188 |
-
>
|
| 189 |
-
<Boxes size={16} />
|
| 190 |
-
استلام بضاعة
|
| 191 |
-
</button>
|
| 192 |
-
<button
|
| 193 |
-
type="button"
|
| 194 |
-
onClick={() => setPaymentSupplier(supplier)}
|
| 195 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl border border-slate-200 bg-slate-50 px-4 font-bold text-slate-700 transition hover:bg-slate-100"
|
| 196 |
-
>
|
| 197 |
-
<ArrowUpRight size={16} />
|
| 198 |
-
تسجيل دفعة
|
| 199 |
-
</button>
|
| 200 |
-
<button
|
| 201 |
-
type="button"
|
| 202 |
-
onClick={() => deleteMutation.mutate(supplier._id)}
|
| 203 |
-
disabled={deleteMutation.isPending}
|
| 204 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl border border-rose-100 bg-rose-50 px-4 font-bold text-rose-700 transition hover:bg-rose-100 disabled:opacity-60"
|
| 205 |
-
>
|
| 206 |
-
<Trash2 size={16} />
|
| 207 |
-
حذف
|
| 208 |
-
</button>
|
| 209 |
-
</div>
|
| 210 |
-
</td>
|
| 211 |
-
</tr>
|
| 212 |
-
))
|
| 213 |
-
) : (
|
| 214 |
-
<tr>
|
| 215 |
-
<td colSpan="3" className="px-4 py-12 text-center text-sm font-medium text-slate-500">
|
| 216 |
-
لا يوجد موردون مطابقون لهذا البحث.
|
| 217 |
-
</td>
|
| 218 |
-
</tr>
|
| 219 |
-
)}
|
| 220 |
-
</tbody>
|
| 221 |
-
</table>
|
| 222 |
-
</div>
|
| 223 |
-
|
| 224 |
-
<Pagination
|
| 225 |
-
currentPage={page}
|
| 226 |
-
totalPages={pagination.totalPages}
|
| 227 |
-
onPageChange={setPage}
|
| 228 |
-
totalItems={pagination.total}
|
| 229 |
-
limit={5}
|
| 230 |
-
/>
|
| 231 |
-
</section>
|
| 232 |
-
|
| 233 |
-
{purchaseSupplier ? (
|
| 234 |
-
<ReceiveGoodsModal
|
| 235 |
-
supplier={purchaseSupplier}
|
| 236 |
-
products={productsData?.data?.data || []}
|
| 237 |
-
onClose={() => setPurchaseSupplier(null)}
|
| 238 |
-
onSaved={() => {
|
| 239 |
-
setPurchaseSupplier(null);
|
| 240 |
-
queryClient.invalidateQueries({ queryKey: ["suppliers"] });
|
| 241 |
-
queryClient.invalidateQueries({ queryKey: ["products"] });
|
| 242 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 243 |
-
}}
|
| 244 |
-
/>
|
| 245 |
-
) : null}
|
| 246 |
-
|
| 247 |
-
{paymentSupplier ? (
|
| 248 |
-
<SupplierPaymentModal
|
| 249 |
-
supplier={paymentSupplier}
|
| 250 |
-
onClose={() => setPaymentSupplier(null)}
|
| 251 |
-
onSaved={() => {
|
| 252 |
-
setPaymentSupplier(null);
|
| 253 |
-
queryClient.invalidateQueries({ queryKey: ["suppliers"] });
|
| 254 |
-
queryClient.invalidateQueries({ queryKey: ["reports", "overview"] });
|
| 255 |
-
}}
|
| 256 |
-
/>
|
| 257 |
-
) : null}
|
| 258 |
-
</div>
|
| 259 |
-
);
|
| 260 |
-
};
|
| 261 |
-
|
| 262 |
-
const Field = ({ label, name, formik, type = "text" }) => (
|
| 263 |
-
<div>
|
| 264 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">{label}</label>
|
| 265 |
-
<input
|
| 266 |
-
name={name}
|
| 267 |
-
type={type}
|
| 268 |
-
value={formik.values[name]}
|
| 269 |
-
onChange={formik.handleChange}
|
| 270 |
-
onBlur={formik.handleBlur}
|
| 271 |
-
className={`h-12 w-full rounded-2xl border bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-indigo-500 ${
|
| 272 |
-
formik.touched[name] && formik.errors[name] ? "border-rose-400" : "border-slate-200"
|
| 273 |
-
}`}
|
| 274 |
-
/>
|
| 275 |
-
{formik.touched[name] && formik.errors[name] ? (
|
| 276 |
-
<p className="mt-1 text-xs font-bold text-rose-500">{formik.errors[name]}</p>
|
| 277 |
-
) : null}
|
| 278 |
-
</div>
|
| 279 |
-
);
|
| 280 |
-
|
| 281 |
-
const ReceiveGoodsModal = ({ supplier, products, onClose, onSaved }) => {
|
| 282 |
-
const mutation = useMutation({
|
| 283 |
-
mutationFn: (values) => axios.post(`${apiUrl}/suppliers/${supplier._id}/items`, values),
|
| 284 |
-
onSuccess: () => {
|
| 285 |
-
toast.success("تم تسجيل بضاعة المورد");
|
| 286 |
-
onSaved();
|
| 287 |
-
},
|
| 288 |
-
onError: (error) => {
|
| 289 |
-
toast.error(error.response?.data?.message || "تعذر تسجيل البضاعة");
|
| 290 |
-
},
|
| 291 |
-
});
|
| 292 |
-
|
| 293 |
-
const formik = useFormik({
|
| 294 |
-
initialValues: {
|
| 295 |
-
productId: "",
|
| 296 |
-
quantity: 1,
|
| 297 |
-
price: "",
|
| 298 |
-
},
|
| 299 |
-
validationSchema: Yup.object({
|
| 300 |
-
productId: Yup.string().required("المنتج مطلوب"),
|
| 301 |
-
quantity: Yup.number().required("الكمية مطلوبة").min(1, "الحد الأدنى 1"),
|
| 302 |
-
price: Yup.number().required("التكلفة مطلوبة").min(0, "لا يمكن أن تكون سالبة"),
|
| 303 |
-
}),
|
| 304 |
-
onSubmit: (values) => {
|
| 305 |
-
const selectedProduct = products.find((product) => product._id === values.productId);
|
| 306 |
-
mutation.mutate({
|
| 307 |
-
items: [
|
| 308 |
-
{
|
| 309 |
-
productId: values.productId,
|
| 310 |
-
name: selectedProduct?.name || "منتج",
|
| 311 |
-
quantity: Number(values.quantity),
|
| 312 |
-
price: Number(values.price),
|
| 313 |
-
},
|
| 314 |
-
],
|
| 315 |
-
});
|
| 316 |
-
},
|
| 317 |
-
});
|
| 318 |
-
|
| 319 |
-
return (
|
| 320 |
-
<ModalShell title={`استلام بضاعة من ${supplier.name}`} onClose={onClose}>
|
| 321 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4">
|
| 322 |
-
<div>
|
| 323 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">المنتج</label>
|
| 324 |
-
<select
|
| 325 |
-
name="productId"
|
| 326 |
-
value={formik.values.productId}
|
| 327 |
-
onChange={formik.handleChange}
|
| 328 |
-
className="h-12 w-full rounded-2xl border border-slate-200 bg-slate-50 px-4 font-medium text-slate-800 outline-none focus:ring-2 focus:ring-blue-500"
|
| 329 |
-
>
|
| 330 |
-
<option value="">اختر منتج</option>
|
| 331 |
-
{products.map((product) => (
|
| 332 |
-
<option key={product._id} value={product._id}>
|
| 333 |
-
{product.name}
|
| 334 |
-
</option>
|
| 335 |
-
))}
|
| 336 |
-
</select>
|
| 337 |
-
</div>
|
| 338 |
-
<div className="grid gap-4 md:grid-cols-2">
|
| 339 |
-
<Field label="الكمية" name="quantity" formik={formik} type="number" />
|
| 340 |
-
<Field label="تكلفة الوحدة" name="price" formik={formik} type="number" />
|
| 341 |
-
</div>
|
| 342 |
-
<button
|
| 343 |
-
type="submit"
|
| 344 |
-
disabled={mutation.isPending}
|
| 345 |
-
className="flex h-12 items-center justify-center gap-2 rounded-2xl bg-blue-600 font-bold text-white transition hover:bg-blue-700 disabled:opacity-60"
|
| 346 |
-
>
|
| 347 |
-
{mutation.isPending ? <Loader2 size={18} className="animate-spin" /> : <Boxes size={18} />}
|
| 348 |
-
حفظ إيصال الاستلام
|
| 349 |
-
</button>
|
| 350 |
-
</form>
|
| 351 |
-
</ModalShell>
|
| 352 |
-
);
|
| 353 |
-
};
|
| 354 |
-
|
| 355 |
-
const SupplierPaymentModal = ({ supplier, onClose, onSaved }) => {
|
| 356 |
-
const mutation = useMutation({
|
| 357 |
-
mutationFn: async (values) => {
|
| 358 |
-
const response = await axios.post(`${apiUrl}/suppliers/${supplier._id}/pay`, values);
|
| 359 |
-
await axios.post(`${apiUrl}/expenses`, {
|
| 360 |
-
title: `دفعة مورد - ${supplier.name}`,
|
| 361 |
-
amount: Number(values.amount),
|
| 362 |
-
});
|
| 363 |
-
return response;
|
| 364 |
-
},
|
| 365 |
-
onSuccess: () => {
|
| 366 |
-
toast.success("تم تسجيل دفعة المورد");
|
| 367 |
-
onSaved();
|
| 368 |
-
},
|
| 369 |
-
onError: (error) => {
|
| 370 |
-
toast.error(error.response?.data?.message || "تعذر تسجيل الدفعة");
|
| 371 |
-
},
|
| 372 |
-
});
|
| 373 |
-
|
| 374 |
-
const formik = useFormik({
|
| 375 |
-
initialValues: {
|
| 376 |
-
amount: "",
|
| 377 |
-
},
|
| 378 |
-
validationSchema: Yup.object({
|
| 379 |
-
amount: Yup.number().required("المبلغ مطلوب").min(0.01, "يجب أن يكون المبلغ أكبر من صفر"),
|
| 380 |
-
}),
|
| 381 |
-
onSubmit: (values) => mutation.mutate({ amount: Number(values.amount) }),
|
| 382 |
-
});
|
| 383 |
-
|
| 384 |
-
return (
|
| 385 |
-
<ModalShell title={`تسجيل دفعة لـ ${supplier.name}`} onClose={onClose}>
|
| 386 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4">
|
| 387 |
-
<Field label="مبلغ الدفعة" name="amount" formik={formik} type="number" />
|
| 388 |
-
<button
|
| 389 |
-
type="submit"
|
| 390 |
-
disabled={mutation.isPending}
|
| 391 |
-
className="flex h-12 items-center justify-center gap-2 rounded-2xl bg-slate-900 font-bold text-white transition hover:bg-slate-800 disabled:opacity-60"
|
| 392 |
-
>
|
| 393 |
-
{mutation.isPending ? <Loader2 size={18} className="animate-spin" /> : <ArrowUpRight size={18} />}
|
| 394 |
-
حفظ الدفعة
|
| 395 |
-
</button>
|
| 396 |
-
</form>
|
| 397 |
-
</ModalShell>
|
| 398 |
-
);
|
| 399 |
-
};
|
| 400 |
-
|
| 401 |
-
const ModalShell = ({ title, onClose, children }) => (
|
| 402 |
-
<div className="fixed inset-0 z-50 flex items-center justify-center bg-slate-950/50 p-4" dir="rtl">
|
| 403 |
-
<div className="w-full max-w-xl rounded-3xl border border-slate-200 bg-white p-6 shadow-2xl">
|
| 404 |
-
<div className="mb-6 flex items-center justify-between">
|
| 405 |
-
<div>
|
| 406 |
-
<h3 className="text-xl font-black text-slate-900">{title}</h3>
|
| 407 |
-
</div>
|
| 408 |
-
<button type="button" onClick={onClose} className="rounded-2xl border border-slate-200 px-4 py-2 text-sm font-bold text-slate-600">
|
| 409 |
-
إغلاق
|
| 410 |
-
</button>
|
| 411 |
-
</div>
|
| 412 |
-
{children}
|
| 413 |
-
</div>
|
| 414 |
-
</div>
|
| 415 |
-
);
|
| 416 |
-
|
| 417 |
-
export default SuppliersWorkspace;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Taps.jsx
DELETED
|
@@ -1,51 +0,0 @@
|
|
| 1 |
-
import { useState } from 'react';
|
| 2 |
-
import SearchModule from './AddCustomer';
|
| 3 |
-
import DailyReport from './Reports/DailyReport';
|
| 4 |
-
import InventoryTab from './Inventory/InventoryTab';
|
| 5 |
-
import SuppliersTab from './Suppliers/SuppliersTab';
|
| 6 |
-
import CustomerBoxView from './Customers/CustomerBoxView';
|
| 7 |
-
import { Users2, LineChart, Package, Factory } from 'lucide-react';
|
| 8 |
-
import { useAuth } from '../Context/AuthContext';
|
| 9 |
-
|
| 10 |
-
const Taps = ({ customerData }) => {
|
| 11 |
-
const [activeTab, setActiveTab] = useState('customers');
|
| 12 |
-
const { user } = useAuth();
|
| 13 |
-
const isAdmin = ['owner', 'admin'].includes(user?.role);
|
| 14 |
-
|
| 15 |
-
return (
|
| 16 |
-
<div className='p-4 md:p-6 space-y-6' dir="rtl">
|
| 17 |
-
<div className="flex flex-col md:flex-row items-center gap-4 border-b border-slate-200 pb-4">
|
| 18 |
-
<button onClick={() => setActiveTab('customers')} className={`w-full md:w-auto flex justify-center items-center gap-2 px-8 py-3.5 rounded-xl font-bold transition-all text-lg ${activeTab === 'customers' ? 'bg-amber-500 text-slate-900 shadow-lg shadow-amber-500/20' : 'bg-white text-slate-500 hover:bg-slate-100 border border-slate-200'}`}>
|
| 19 |
-
<Users2 size={24} /> العملاء
|
| 20 |
-
</button>
|
| 21 |
-
{isAdmin && (
|
| 22 |
-
<button onClick={() => setActiveTab('reports')} className={`w-full md:w-auto flex justify-center items-center gap-2 px-8 py-3.5 rounded-xl font-bold transition-all text-lg ${activeTab === 'reports' ? 'bg-blue-600 text-white shadow-lg shadow-blue-500/20' : 'bg-white text-slate-500 hover:bg-slate-100 border border-slate-200'}`}>
|
| 23 |
-
<LineChart size={24} /> التقارير
|
| 24 |
-
</button>
|
| 25 |
-
)}
|
| 26 |
-
<button onClick={() => setActiveTab('inventory')} className={`w-full md:w-auto flex justify-center items-center gap-2 px-8 py-3.5 rounded-xl font-bold transition-all text-lg ${activeTab === 'inventory' ? 'bg-indigo-600 text-white shadow-lg shadow-indigo-500/20' : 'bg-white text-slate-500 hover:bg-slate-100 border border-slate-200'}`}>
|
| 27 |
-
<Package size={24} /> المخزون
|
| 28 |
-
</button>
|
| 29 |
-
{isAdmin && (
|
| 30 |
-
<button onClick={() => setActiveTab('suppliers')} className={`w-full md:w-auto flex justify-center items-center gap-2 px-8 py-3.5 rounded-xl font-bold transition-all text-lg ${activeTab === 'suppliers' ? 'bg-rose-600 text-white shadow-lg shadow-rose-500/20' : 'bg-white text-slate-500 hover:bg-slate-100 border border-slate-200'}`}>
|
| 31 |
-
<Factory size={24} /> الموردون
|
| 32 |
-
</button>
|
| 33 |
-
)}
|
| 34 |
-
</div>
|
| 35 |
-
|
| 36 |
-
<div className="animate-in fade-in slide-in-from-bottom-2 duration-300">
|
| 37 |
-
{activeTab === 'customers' && (
|
| 38 |
-
<>
|
| 39 |
-
<SearchModule activeTab={activeTab} />
|
| 40 |
-
<CustomerBoxView customerData={customerData} />
|
| 41 |
-
</>
|
| 42 |
-
)}
|
| 43 |
-
{activeTab === 'reports' && <DailyReport />}
|
| 44 |
-
{activeTab === 'inventory' && <InventoryTab />}
|
| 45 |
-
{activeTab === 'suppliers' && <SuppliersTab />}
|
| 46 |
-
</div>
|
| 47 |
-
</div>
|
| 48 |
-
)
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
export default Taps;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Tenants/TenantsAdminTab.jsx
DELETED
|
@@ -1,197 +0,0 @@
|
|
| 1 |
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
import { Building2, MessageCircle, Plus, Power, Store, UserCircle2 } from "lucide-react";
|
| 4 |
-
import { useFormik } from "formik";
|
| 5 |
-
import * as Yup from "yup";
|
| 6 |
-
import { toast } from "react-toastify";
|
| 7 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 8 |
-
|
| 9 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 10 |
-
|
| 11 |
-
const TenantsAdminTab = () => {
|
| 12 |
-
const queryClient = useQueryClient();
|
| 13 |
-
|
| 14 |
-
const { data, isLoading } = useQuery({
|
| 15 |
-
queryKey: ["tenants"],
|
| 16 |
-
queryFn: () => axios.get(`${apiUrl}/auth/tenants`),
|
| 17 |
-
refetchOnWindowFocus: false,
|
| 18 |
-
});
|
| 19 |
-
|
| 20 |
-
const createTenantMutation = useMutation({
|
| 21 |
-
mutationFn: (values) => axios.post(`${apiUrl}/auth/tenants`, values),
|
| 22 |
-
onSuccess: (response) => {
|
| 23 |
-
queryClient.invalidateQueries({ queryKey: ["tenants"] });
|
| 24 |
-
formik.resetForm();
|
| 25 |
-
toast.success(response.data.message);
|
| 26 |
-
if (response.data.onboarding?.whatsappLink) {
|
| 27 |
-
window.open(response.data.onboarding.whatsappLink, "_blank", "noopener,noreferrer");
|
| 28 |
-
}
|
| 29 |
-
},
|
| 30 |
-
onError: (error) => {
|
| 31 |
-
toast.error(error.response?.data?.message || "تعذر إنشاء المستأجر");
|
| 32 |
-
},
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
const statusMutation = useMutation({
|
| 36 |
-
mutationFn: ({ tenantId, status }) => axios.patch(`${apiUrl}/auth/tenants/${tenantId}/status`, { status }),
|
| 37 |
-
onSuccess: () => {
|
| 38 |
-
queryClient.invalidateQueries({ queryKey: ["tenants"] });
|
| 39 |
-
toast.success("تم تحديث حالة المستأجر");
|
| 40 |
-
},
|
| 41 |
-
onError: (error) => {
|
| 42 |
-
toast.error(error.response?.data?.message || "تعذر تحديث حالة المستأجر");
|
| 43 |
-
},
|
| 44 |
-
});
|
| 45 |
-
|
| 46 |
-
const formik = useFormik({
|
| 47 |
-
initialValues: {
|
| 48 |
-
companyName: "",
|
| 49 |
-
ownerName: "",
|
| 50 |
-
username: "",
|
| 51 |
-
password: "",
|
| 52 |
-
email: "",
|
| 53 |
-
phone: "",
|
| 54 |
-
whatsappNumber: "",
|
| 55 |
-
address: "",
|
| 56 |
-
},
|
| 57 |
-
validationSchema: Yup.object({
|
| 58 |
-
companyName: Yup.string().required("اسم الشركة مطلوب").min(3, "3 أحرف على الأقل"),
|
| 59 |
-
ownerName: Yup.string().required("اسم المالك مطلوب"),
|
| 60 |
-
username: Yup.string().required("اسم المستخدم مطلوب").min(3, "3 أحرف على الأقل"),
|
| 61 |
-
password: Yup.string().required("كلمة المرور مطلوبة").min(6, "6 أحرف على الأقل"),
|
| 62 |
-
}),
|
| 63 |
-
onSubmit: (values) => createTenantMutation.mutate(values),
|
| 64 |
-
});
|
| 65 |
-
|
| 66 |
-
const tenants = data?.data || [];
|
| 67 |
-
|
| 68 |
-
if (isLoading) {
|
| 69 |
-
return <LodderSpinner />;
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
return (
|
| 73 |
-
<div className="space-y-6">
|
| 74 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 75 |
-
<div className="mb-6 flex items-center gap-3">
|
| 76 |
-
<div className="flex h-12 w-12 items-center justify-center rounded-2xl border border-emerald-100 bg-emerald-50 text-emerald-700">
|
| 77 |
-
<Building2 size={22} />
|
| 78 |
-
</div>
|
| 79 |
-
<div>
|
| 80 |
-
<h3 className="text-xl font-black text-slate-900">إنشاء مساحة عمل مستأجر</h3>
|
| 81 |
-
<p className="text-sm text-slate-500">تجهيز حساب مالك عمل جديد وفتح رسالة واتساب تأهيلية جاهزة.</p>
|
| 82 |
-
</div>
|
| 83 |
-
</div>
|
| 84 |
-
|
| 85 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
|
| 86 |
-
<Field label="اسم الشركة" name="companyName" formik={formik} />
|
| 87 |
-
<Field label="اسم المالك" name="ownerName" formik={formik} />
|
| 88 |
-
<Field label="اسم المستخدم" name="username" formik={formik} />
|
| 89 |
-
<Field label="كلمة المرور المؤقتة" name="password" formik={formik} type="password" />
|
| 90 |
-
<Field label="البريد الإلكتروني" name="email" formik={formik} />
|
| 91 |
-
<Field label="الهاتف" name="phone" formik={formik} />
|
| 92 |
-
<Field label="رقم واتساب" name="whatsappNumber" formik={formik} />
|
| 93 |
-
<Field label="العنوان" name="address" formik={formik} />
|
| 94 |
-
<button
|
| 95 |
-
type="submit"
|
| 96 |
-
disabled={createTenantMutation.isPending}
|
| 97 |
-
className="xl:col-span-4 flex h-12 items-center justify-center gap-2 rounded-2xl bg-emerald-600 font-bold text-white transition hover:bg-emerald-700 disabled:opacity-60"
|
| 98 |
-
>
|
| 99 |
-
<Plus size={18} />
|
| 100 |
-
إنشاء مستأجر
|
| 101 |
-
</button>
|
| 102 |
-
</form>
|
| 103 |
-
</section>
|
| 104 |
-
|
| 105 |
-
<div className="grid gap-4 xl:grid-cols-2">
|
| 106 |
-
{tenants.map((tenant) => (
|
| 107 |
-
<section key={tenant._id} className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 108 |
-
<div className="flex items-start justify-between gap-4">
|
| 109 |
-
<div className="flex items-center gap-3">
|
| 110 |
-
<div className="flex h-12 w-12 items-center justify-center rounded-2xl border border-slate-200 bg-slate-100 text-slate-700">
|
| 111 |
-
<Store size={20} />
|
| 112 |
-
</div>
|
| 113 |
-
<div>
|
| 114 |
-
<h4 className="text-lg font-black text-slate-900">{tenant.name}</h4>
|
| 115 |
-
<p className="text-sm text-slate-500">/{tenant.slug}</p>
|
| 116 |
-
</div>
|
| 117 |
-
</div>
|
| 118 |
-
<span className={`rounded-full px-3 py-1 text-xs font-bold ${tenant.status === "active" ? "bg-emerald-50 text-emerald-700" : "bg-rose-50 text-rose-700"}`}>
|
| 119 |
-
{tenant.status === "active" ? "نشط" : "موقوف"}
|
| 120 |
-
</span>
|
| 121 |
-
</div>
|
| 122 |
-
|
| 123 |
-
<div className="mt-5 grid gap-3 sm:grid-cols-2">
|
| 124 |
-
<Info label="المالك" value={tenant.ownerName || "-"} icon={<UserCircle2 size={16} />} />
|
| 125 |
-
<Info label="واتساب" value={tenant.whatsappNumber || tenant.ownerPhone || "-"} icon={<MessageCircle size={16} />} />
|
| 126 |
-
<Info label="العملة" value={tenant.settings?.currency || "EGP"} icon={<Building2 size={16} />} />
|
| 127 |
-
<Info label="حد المخزون المنخفض" value={`${tenant.settings?.lowStockThreshold || 5}`} icon={<Power size={16} />} />
|
| 128 |
-
</div>
|
| 129 |
-
|
| 130 |
-
<div className="mt-5 flex flex-wrap gap-3">
|
| 131 |
-
<button
|
| 132 |
-
type="button"
|
| 133 |
-
onClick={() => statusMutation.mutate({ tenantId: tenant._id, status: tenant.status === "active" ? "suspended" : "active" })}
|
| 134 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl border border-slate-200 bg-slate-50 px-4 font-bold text-slate-700 transition hover:bg-slate-100"
|
| 135 |
-
>
|
| 136 |
-
<Power size={16} />
|
| 137 |
-
{tenant.status === "active" ? "تعليق المستأجر" : "إعادة تفعيل المستأجر"}
|
| 138 |
-
</button>
|
| 139 |
-
<button
|
| 140 |
-
type="button"
|
| 141 |
-
onClick={() =>
|
| 142 |
-
window.open(
|
| 143 |
-
`https://wa.me/?text=${encodeURIComponent(
|
| 144 |
-
[
|
| 145 |
-
`المستأجر: ${tenant.name}`,
|
| 146 |
-
`المالك: ${tenant.ownerName || "-"}`,
|
| 147 |
-
`واتساب: ${tenant.whatsappNumber || tenant.ownerPhone || "-"}`,
|
| 148 |
-
`الحالة: ${tenant.status === "active" ? "نشط" : "موقوف"}`,
|
| 149 |
-
].join("\n")
|
| 150 |
-
)}`,
|
| 151 |
-
"_blank",
|
| 152 |
-
"noopener,noreferrer"
|
| 153 |
-
)
|
| 154 |
-
}
|
| 155 |
-
className="flex h-10 items-center justify-center gap-2 rounded-2xl bg-emerald-600 px-4 font-bold text-white transition hover:bg-emerald-700"
|
| 156 |
-
>
|
| 157 |
-
<MessageCircle size={16} />
|
| 158 |
-
مشاركة عبر واتساب
|
| 159 |
-
</button>
|
| 160 |
-
</div>
|
| 161 |
-
</section>
|
| 162 |
-
))}
|
| 163 |
-
</div>
|
| 164 |
-
</div>
|
| 165 |
-
);
|
| 166 |
-
};
|
| 167 |
-
|
| 168 |
-
const Field = ({ label, name, formik, type = "text" }) => (
|
| 169 |
-
<div>
|
| 170 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">{label}</label>
|
| 171 |
-
<input
|
| 172 |
-
name={name}
|
| 173 |
-
type={type}
|
| 174 |
-
value={formik.values[name]}
|
| 175 |
-
onChange={formik.handleChange}
|
| 176 |
-
onBlur={formik.handleBlur}
|
| 177 |
-
className={`h-12 w-full rounded-2xl border bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-emerald-500 ${
|
| 178 |
-
formik.touched[name] && formik.errors[name] ? "border-rose-400" : "border-slate-200"
|
| 179 |
-
}`}
|
| 180 |
-
/>
|
| 181 |
-
{formik.touched[name] && formik.errors[name] ? (
|
| 182 |
-
<p className="mt-1 text-xs font-bold text-rose-500">{formik.errors[name]}</p>
|
| 183 |
-
) : null}
|
| 184 |
-
</div>
|
| 185 |
-
);
|
| 186 |
-
|
| 187 |
-
const Info = ({ icon, label, value }) => (
|
| 188 |
-
<div className="flex items-center gap-3 rounded-2xl border border-slate-100 bg-slate-50 p-3">
|
| 189 |
-
<div className="text-emerald-700">{icon}</div>
|
| 190 |
-
<div>
|
| 191 |
-
<p className="text-[11px] font-bold uppercase tracking-wide text-slate-500">{label}</p>
|
| 192 |
-
<p className="font-bold text-slate-900">{value}</p>
|
| 193 |
-
</div>
|
| 194 |
-
</div>
|
| 195 |
-
);
|
| 196 |
-
|
| 197 |
-
export default TenantsAdminTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Tenants/TenantsTab.jsx
DELETED
|
@@ -1,146 +0,0 @@
|
|
| 1 |
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
| 2 |
-
import { Building2, MessageCircle, Plus, ShieldCheck, Store, UserCircle2 } from "lucide-react";
|
| 3 |
-
import { useFormik } from "formik";
|
| 4 |
-
import * as Yup from "yup";
|
| 5 |
-
import axios from "axios";
|
| 6 |
-
import { toast } from "react-toastify";
|
| 7 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 8 |
-
|
| 9 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 10 |
-
|
| 11 |
-
const TenantsTab = () => {
|
| 12 |
-
const queryClient = useQueryClient();
|
| 13 |
-
|
| 14 |
-
const { data, isLoading } = useQuery({
|
| 15 |
-
queryKey: ["tenants"],
|
| 16 |
-
queryFn: () => axios.get(`${apiUrl}/auth/tenants`),
|
| 17 |
-
refetchOnWindowFocus: false,
|
| 18 |
-
});
|
| 19 |
-
|
| 20 |
-
const createTenantMutation = useMutation({
|
| 21 |
-
mutationFn: (values) => axios.post(`${apiUrl}/auth/tenants`, values),
|
| 22 |
-
onSuccess: (response) => {
|
| 23 |
-
queryClient.invalidateQueries({ queryKey: ["tenants"] });
|
| 24 |
-
formik.resetForm();
|
| 25 |
-
toast.success(response.data.message);
|
| 26 |
-
if (response.data.onboarding?.whatsappLink) {
|
| 27 |
-
window.open(response.data.onboarding.whatsappLink, "_blank");
|
| 28 |
-
}
|
| 29 |
-
},
|
| 30 |
-
onError: (error) => {
|
| 31 |
-
toast.error(error.response?.data?.message || "Failed to create tenant");
|
| 32 |
-
},
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
const formik = useFormik({
|
| 36 |
-
initialValues: {
|
| 37 |
-
companyName: "",
|
| 38 |
-
ownerName: "",
|
| 39 |
-
username: "",
|
| 40 |
-
password: "",
|
| 41 |
-
email: "",
|
| 42 |
-
phone: "",
|
| 43 |
-
whatsappNumber: "",
|
| 44 |
-
address: "",
|
| 45 |
-
},
|
| 46 |
-
validationSchema: Yup.object({
|
| 47 |
-
companyName: Yup.string().required("Required").min(3, "Minimum 3 characters"),
|
| 48 |
-
ownerName: Yup.string().required("Required"),
|
| 49 |
-
username: Yup.string().required("Required").min(3, "Minimum 3 characters"),
|
| 50 |
-
password: Yup.string().required("Required").min(6, "Minimum 6 characters"),
|
| 51 |
-
}),
|
| 52 |
-
onSubmit: (values) => createTenantMutation.mutate(values),
|
| 53 |
-
});
|
| 54 |
-
|
| 55 |
-
const tenants = data?.data || [];
|
| 56 |
-
|
| 57 |
-
if (isLoading) {
|
| 58 |
-
return <LodderSpinner />;
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
return (
|
| 62 |
-
<div className="space-y-6">
|
| 63 |
-
<div className="bg-white rounded-3xl shadow-sm border border-slate-200 p-6">
|
| 64 |
-
<div className="flex items-center gap-3 mb-6">
|
| 65 |
-
<div className="w-12 h-12 rounded-2xl bg-emerald-50 text-emerald-600 flex items-center justify-center border border-emerald-100">
|
| 66 |
-
<Building2 size={24} />
|
| 67 |
-
</div>
|
| 68 |
-
<div>
|
| 69 |
-
<h3 className="text-xl font-black text-slate-800">Create a new tenant</h3>
|
| 70 |
-
<p className="text-sm text-slate-500">Provision a business owner account and open the ready-made WhatsApp onboarding message.</p>
|
| 71 |
-
</div>
|
| 72 |
-
</div>
|
| 73 |
-
|
| 74 |
-
<form onSubmit={formik.handleSubmit} className="grid md:grid-cols-2 xl:grid-cols-4 gap-4">
|
| 75 |
-
<Field label="Company name" name="companyName" formik={formik} placeholder="Example: Demo Store" />
|
| 76 |
-
<Field label="Owner name" name="ownerName" formik={formik} placeholder="Business owner" />
|
| 77 |
-
<Field label="Username" name="username" formik={formik} placeholder="owner.demo" />
|
| 78 |
-
<Field label="Temporary password" name="password" formik={formik} placeholder="******" />
|
| 79 |
-
<Field label="Email" name="email" formik={formik} placeholder="owner@example.com" />
|
| 80 |
-
<Field label="Phone" name="phone" formik={formik} placeholder="01xxxxxxxxx" />
|
| 81 |
-
<Field label="WhatsApp" name="whatsappNumber" formik={formik} placeholder="01xxxxxxxxx" />
|
| 82 |
-
<Field label="Address" name="address" formik={formik} placeholder="City or address" />
|
| 83 |
-
<button type="submit" disabled={createTenantMutation.isPending} className="md:col-span-2 xl:col-span-4 h-12 bg-emerald-600 hover:bg-emerald-700 disabled:opacity-60 text-white rounded-2xl font-bold flex items-center justify-center gap-2">
|
| 84 |
-
<Plus size={18} /> Create tenant owner account
|
| 85 |
-
</button>
|
| 86 |
-
</form>
|
| 87 |
-
</div>
|
| 88 |
-
|
| 89 |
-
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
|
| 90 |
-
{tenants.map((tenant) => (
|
| 91 |
-
<div key={tenant._id} className="bg-white rounded-3xl border border-slate-200 shadow-sm p-6 space-y-4">
|
| 92 |
-
<div className="flex items-start justify-between gap-4">
|
| 93 |
-
<div className="flex items-center gap-3">
|
| 94 |
-
<div className="w-12 h-12 rounded-2xl bg-slate-100 text-slate-700 flex items-center justify-center border border-slate-200">
|
| 95 |
-
<Store size={22} />
|
| 96 |
-
</div>
|
| 97 |
-
<div>
|
| 98 |
-
<h4 className="text-lg font-black text-slate-800">{tenant.name}</h4>
|
| 99 |
-
<p className="text-sm text-slate-500">/{tenant.slug}</p>
|
| 100 |
-
</div>
|
| 101 |
-
</div>
|
| 102 |
-
<span className={`px-3 py-1 rounded-full text-xs font-bold border ${tenant.status === 'active' ? 'bg-emerald-50 text-emerald-700 border-emerald-100' : 'bg-rose-50 text-rose-700 border-rose-100'}`}>
|
| 103 |
-
{tenant.status === 'active' ? 'Active' : 'Suspended'}
|
| 104 |
-
</span>
|
| 105 |
-
</div>
|
| 106 |
-
|
| 107 |
-
<div className="grid sm:grid-cols-2 gap-3 text-sm">
|
| 108 |
-
<Info icon={<UserCircle2 size={16} />} label="Owner" value={tenant.ownerName || '-'} />
|
| 109 |
-
<Info icon={<ShieldCheck size={16} />} label="Currency" value={tenant.settings?.currency || 'EGP'} />
|
| 110 |
-
<Info icon={<MessageCircle size={16} />} label="WhatsApp" value={tenant.whatsappNumber || tenant.ownerPhone || '-'} />
|
| 111 |
-
<Info icon={<Building2 size={16} />} label="Created" value={new Date(tenant.createdAt).toLocaleDateString('en-US')} />
|
| 112 |
-
</div>
|
| 113 |
-
</div>
|
| 114 |
-
))}
|
| 115 |
-
</div>
|
| 116 |
-
</div>
|
| 117 |
-
);
|
| 118 |
-
};
|
| 119 |
-
|
| 120 |
-
const Field = ({ label, name, formik, placeholder }) => (
|
| 121 |
-
<div>
|
| 122 |
-
<label className="block text-xs font-bold text-slate-500 mb-1">{label}</label>
|
| 123 |
-
<input
|
| 124 |
-
name={name}
|
| 125 |
-
type="text"
|
| 126 |
-
value={formik.values[name]}
|
| 127 |
-
onChange={formik.handleChange}
|
| 128 |
-
onBlur={formik.handleBlur}
|
| 129 |
-
placeholder={placeholder}
|
| 130 |
-
className={`w-full bg-slate-50 border ${formik.touched[name] && formik.errors[name] ? 'border-rose-400' : 'border-slate-200'} rounded-2xl px-4 py-3 outline-none focus:ring-2 focus:ring-emerald-500 font-bold text-slate-700`}
|
| 131 |
-
/>
|
| 132 |
-
{formik.touched[name] && formik.errors[name] && <p className="text-rose-500 text-[11px] font-bold mt-1">{formik.errors[name]}</p>}
|
| 133 |
-
</div>
|
| 134 |
-
);
|
| 135 |
-
|
| 136 |
-
const Info = ({ icon, label, value }) => (
|
| 137 |
-
<div className="bg-slate-50 rounded-2xl border border-slate-100 p-3 text-slate-700 flex items-center gap-3">
|
| 138 |
-
<div className="text-emerald-600">{icon}</div>
|
| 139 |
-
<div>
|
| 140 |
-
<p className="text-[11px] text-slate-500 font-bold">{label}</p>
|
| 141 |
-
<p className="font-bold">{value}</p>
|
| 142 |
-
</div>
|
| 143 |
-
</div>
|
| 144 |
-
);
|
| 145 |
-
|
| 146 |
-
export default TenantsTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Users/UsersManagementTab.jsx
DELETED
|
@@ -1,272 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
| 4 |
-
import { Loader2, ShieldCheck, UserCog, UserPlus } from "lucide-react";
|
| 5 |
-
import { useFormik } from "formik";
|
| 6 |
-
import * as Yup from "yup";
|
| 7 |
-
import { toast } from "react-toastify";
|
| 8 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 9 |
-
import { useAuth } from "../../Context/AuthContext";
|
| 10 |
-
import { getPermissions } from "../../utils/permissions";
|
| 11 |
-
import { formatDateTime } from "../../utils/formatters";
|
| 12 |
-
|
| 13 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 14 |
-
|
| 15 |
-
const UsersManagementTab = () => {
|
| 16 |
-
const { user } = useAuth();
|
| 17 |
-
const permissions = getPermissions(user);
|
| 18 |
-
const queryClient = useQueryClient();
|
| 19 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 20 |
-
|
| 21 |
-
const { data, isLoading } = useQuery({
|
| 22 |
-
queryKey: ["users"],
|
| 23 |
-
queryFn: () => axios.get(`${apiUrl}/auth/users`),
|
| 24 |
-
refetchOnWindowFocus: false,
|
| 25 |
-
});
|
| 26 |
-
|
| 27 |
-
const createMutation = useMutation({
|
| 28 |
-
mutationFn: (values) => axios.post(`${apiUrl}/auth/users`, values),
|
| 29 |
-
onSuccess: () => {
|
| 30 |
-
formik.resetForm();
|
| 31 |
-
queryClient.invalidateQueries({ queryKey: ["users"] });
|
| 32 |
-
toast.success("تم إنشاء المستخدم");
|
| 33 |
-
},
|
| 34 |
-
onError: (error) => {
|
| 35 |
-
toast.error(error.response?.data?.message || "تعذر إنشاء المستخدم");
|
| 36 |
-
},
|
| 37 |
-
});
|
| 38 |
-
|
| 39 |
-
const updateMutation = useMutation({
|
| 40 |
-
mutationFn: ({ userId, values }) => axios.patch(`${apiUrl}/auth/users/${userId}`, values),
|
| 41 |
-
onSuccess: () => {
|
| 42 |
-
queryClient.invalidateQueries({ queryKey: ["users"] });
|
| 43 |
-
toast.success("تم تحديث المستخدم");
|
| 44 |
-
},
|
| 45 |
-
onError: (error) => {
|
| 46 |
-
toast.error(error.response?.data?.message || "تعذر تحديث المستخدم");
|
| 47 |
-
},
|
| 48 |
-
});
|
| 49 |
-
|
| 50 |
-
const deleteMutation = useMutation({
|
| 51 |
-
mutationFn: (userId) => axios.delete(`${apiUrl}/auth/users/${userId}`),
|
| 52 |
-
onSuccess: () => {
|
| 53 |
-
queryClient.invalidateQueries({ queryKey: ["users"] });
|
| 54 |
-
toast.success("تم حذف المستخدم");
|
| 55 |
-
},
|
| 56 |
-
onError: (error) => {
|
| 57 |
-
toast.error(error.response?.data?.message || "تعذر حذف المستخدم");
|
| 58 |
-
},
|
| 59 |
-
});
|
| 60 |
-
|
| 61 |
-
const formik = useFormik({
|
| 62 |
-
initialValues: {
|
| 63 |
-
username: "",
|
| 64 |
-
fullName: "",
|
| 65 |
-
email: "",
|
| 66 |
-
phone: "",
|
| 67 |
-
password: "",
|
| 68 |
-
role: "cashier",
|
| 69 |
-
},
|
| 70 |
-
validationSchema: Yup.object({
|
| 71 |
-
username: Yup.string().required("اسم المستخدم مطلوب").min(3, "3 أحرف على الأقل"),
|
| 72 |
-
password: Yup.string().required("كلمة المرور مطلوبة").min(6, "6 أحرف على الأقل"),
|
| 73 |
-
role: Yup.string().oneOf(["admin", "cashier"]),
|
| 74 |
-
}),
|
| 75 |
-
onSubmit: (values) => createMutation.mutate(values),
|
| 76 |
-
});
|
| 77 |
-
|
| 78 |
-
if (isLoading) {
|
| 79 |
-
return <LodderSpinner />;
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
const users = (data?.data || []).filter((item) => {
|
| 83 |
-
const normalized = searchTerm.trim().toLowerCase();
|
| 84 |
-
if (!normalized) {
|
| 85 |
-
return true;
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
return `${item.fullName} ${item.username}`.toLowerCase().includes(normalized);
|
| 89 |
-
});
|
| 90 |
-
|
| 91 |
-
return (
|
| 92 |
-
<div className="space-y-6">
|
| 93 |
-
{permissions.canManageUsers ? (
|
| 94 |
-
<section className="rounded-3xl border border-slate-200 bg-white p-6 shadow-sm">
|
| 95 |
-
<div className="mb-5 flex items-center gap-3">
|
| 96 |
-
<div className="flex h-12 w-12 items-center justify-center rounded-2xl border border-emerald-100 bg-emerald-50 text-emerald-700">
|
| 97 |
-
<UserPlus size={22} />
|
| 98 |
-
</div>
|
| 99 |
-
<div>
|
| 100 |
-
<h3 className="text-lg font-black text-slate-900">إضافة مستخدم داخلي</h3>
|
| 101 |
-
<p className="text-sm text-slate-500">إنشاء حسابات مدير وأمين صندوق داخل هذا المستأجر.</p>
|
| 102 |
-
</div>
|
| 103 |
-
</div>
|
| 104 |
-
|
| 105 |
-
<form onSubmit={formik.handleSubmit} className="grid gap-4 md:grid-cols-3 xl:grid-cols-6">
|
| 106 |
-
<Field label="اسم المستخدم" name="username" formik={formik} />
|
| 107 |
-
<Field label="الاسم الكامل" name="fullName" formik={formik} />
|
| 108 |
-
<Field label="البريد الإلكتروني" name="email" formik={formik} />
|
| 109 |
-
<Field label="الهاتف" name="phone" formik={formik} />
|
| 110 |
-
<Field label="كلمة المرور" name="password" formik={formik} type="password" />
|
| 111 |
-
<div>
|
| 112 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">الدور</label>
|
| 113 |
-
<select
|
| 114 |
-
name="role"
|
| 115 |
-
value={formik.values.role}
|
| 116 |
-
onChange={formik.handleChange}
|
| 117 |
-
className="h-12 w-full rounded-2xl border border-slate-200 bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-emerald-500"
|
| 118 |
-
>
|
| 119 |
-
<option value="cashier">أمين صندوق</option>
|
| 120 |
-
<option value="admin">مدير الشركة</option>
|
| 121 |
-
</select>
|
| 122 |
-
</div>
|
| 123 |
-
<button
|
| 124 |
-
type="submit"
|
| 125 |
-
disabled={createMutation.isPending}
|
| 126 |
-
className="xl:col-span-6 flex h-12 items-center justify-center gap-2 rounded-2xl bg-emerald-600 font-bold text-white transition hover:bg-emerald-700 disabled:opacity-60"
|
| 127 |
-
>
|
| 128 |
-
{createMutation.isPending ? <Loader2 size={18} className="animate-spin" /> : <UserPlus size={18} />}
|
| 129 |
-
إنشاء مستخدم
|
| 130 |
-
</button>
|
| 131 |
-
</form>
|
| 132 |
-
</section>
|
| 133 |
-
) : null}
|
| 134 |
-
|
| 135 |
-
<section className="rounded-3xl border border-slate-200 bg-white shadow-sm">
|
| 136 |
-
<div className="flex flex-col gap-4 border-b border-slate-100 p-5 md:flex-row md:items-center md:justify-between">
|
| 137 |
-
<div>
|
| 138 |
-
<h3 className="text-lg font-black text-slate-900">الصلاحيات والموظفين</h3>
|
| 139 |
-
<p className="text-sm text-slate-500">مراجعة المستخدمين النشطين وضبط صلاحيات أمين الصندوق والمدير.</p>
|
| 140 |
-
</div>
|
| 141 |
-
<input
|
| 142 |
-
type="text"
|
| 143 |
-
value={searchTerm}
|
| 144 |
-
onChange={(event) => setSearchTerm(event.target.value)}
|
| 145 |
-
placeholder="البحث في المستخدمين..."
|
| 146 |
-
className="h-11 w-full rounded-2xl border border-slate-200 bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-emerald-500 md:max-w-sm"
|
| 147 |
-
/>
|
| 148 |
-
</div>
|
| 149 |
-
|
| 150 |
-
<div className="overflow-x-auto">
|
| 151 |
-
<table className="min-w-full border-collapse">
|
| 152 |
-
<thead>
|
| 153 |
-
<tr className="border-b border-slate-200 text-right text-sm font-bold text-slate-500">
|
| 154 |
-
<th className="px-4 py-3">المستخدم</th>
|
| 155 |
-
<th className="px-4 py-3">الدور</th>
|
| 156 |
-
<th className="px-4 py-3">الحالة</th>
|
| 157 |
-
<th className="px-4 py-3">آخر دخول</th>
|
| 158 |
-
<th className="px-4 py-3">الإجراءات</th>
|
| 159 |
-
</tr>
|
| 160 |
-
</thead>
|
| 161 |
-
<tbody>
|
| 162 |
-
{users.length ? (
|
| 163 |
-
users.map((item) => (
|
| 164 |
-
<tr key={item._id} className="border-b border-slate-100 text-sm text-slate-700">
|
| 165 |
-
<td className="px-4 py-4">
|
| 166 |
-
<div className="flex items-center gap-3">
|
| 167 |
-
<div className="flex h-10 w-10 items-center justify-center rounded-2xl bg-slate-100 text-slate-700">
|
| 168 |
-
<UserCog size={18} />
|
| 169 |
-
</div>
|
| 170 |
-
<div>
|
| 171 |
-
<p className="font-bold text-slate-900">{item.fullName || item.username}</p>
|
| 172 |
-
<p className="text-xs text-slate-500">{item.username}</p>
|
| 173 |
-
</div>
|
| 174 |
-
</div>
|
| 175 |
-
</td>
|
| 176 |
-
<td className="px-4 py-4">
|
| 177 |
-
{permissions.canManageUsers && item.role !== "owner" ? (
|
| 178 |
-
<select
|
| 179 |
-
value={item.role}
|
| 180 |
-
onChange={(event) =>
|
| 181 |
-
updateMutation.mutate({
|
| 182 |
-
userId: item._id,
|
| 183 |
-
values: { role: event.target.value },
|
| 184 |
-
})
|
| 185 |
-
}
|
| 186 |
-
className="h-10 rounded-2xl border border-slate-200 bg-slate-50 px-4 font-bold text-slate-800 outline-none focus:ring-2 focus:ring-emerald-500"
|
| 187 |
-
>
|
| 188 |
-
<option value="cashier">أمين صندوق</option>
|
| 189 |
-
<option value="admin">مدير الشركة</option>
|
| 190 |
-
</select>
|
| 191 |
-
) : (
|
| 192 |
-
<span className="rounded-full bg-slate-100 px-3 py-1 text-xs font-bold uppercase tracking-wide text-slate-600">
|
| 193 |
-
{item.role}
|
| 194 |
-
</span>
|
| 195 |
-
)}
|
| 196 |
-
</td>
|
| 197 |
-
<td className="px-4 py-4">
|
| 198 |
-
{permissions.canManageUsers && item.role !== "owner" ? (
|
| 199 |
-
<select
|
| 200 |
-
value={item.status}
|
| 201 |
-
onChange={(event) =>
|
| 202 |
-
updateMutation.mutate({
|
| 203 |
-
userId: item._id,
|
| 204 |
-
values: { status: event.target.value },
|
| 205 |
-
})
|
| 206 |
-
}
|
| 207 |
-
className="h-10 rounded-2xl border border-slate-200 bg-slate-50 px-4 font-bold text-slate-800 outline-none focus:ring-2 focus:ring-emerald-500"
|
| 208 |
-
>
|
| 209 |
-
<option value="active">نشط</option>
|
| 210 |
-
<option value="suspended">موقوف</option>
|
| 211 |
-
</select>
|
| 212 |
-
) : (
|
| 213 |
-
<span className="rounded-full bg-slate-100 px-3 py-1 text-xs font-bold uppercase tracking-wide text-slate-600">
|
| 214 |
-
{item.status}
|
| 215 |
-
</span>
|
| 216 |
-
)}
|
| 217 |
-
</td>
|
| 218 |
-
<td className="px-4 py-4">{item.lastLoginAt ? formatDateTime(item.lastLoginAt) : "لم يسجل دخول"}</td>
|
| 219 |
-
<td className="px-4 py-4">
|
| 220 |
-
{permissions.canManageUsers && item.role !== "owner" ? (
|
| 221 |
-
<button
|
| 222 |
-
type="button"
|
| 223 |
-
onClick={() => deleteMutation.mutate(item._id)}
|
| 224 |
-
disabled={deleteMutation.isPending}
|
| 225 |
-
className="rounded-2xl border border-rose-100 bg-rose-50 px-4 py-2 text-sm font-bold text-rose-700 transition hover:bg-rose-100 disabled:opacity-60"
|
| 226 |
-
>
|
| 227 |
-
حذف
|
| 228 |
-
</button>
|
| 229 |
-
) : (
|
| 230 |
-
<span className="flex items-center gap-2 text-xs font-bold text-slate-500">
|
| 231 |
-
<ShieldCheck size={14} />
|
| 232 |
-
محمي
|
| 233 |
-
</span>
|
| 234 |
-
)}
|
| 235 |
-
</td>
|
| 236 |
-
</tr>
|
| 237 |
-
))
|
| 238 |
-
) : (
|
| 239 |
-
<tr>
|
| 240 |
-
<td colSpan="5" className="px-4 py-12 text-center text-sm font-medium text-slate-500">
|
| 241 |
-
لا يوجد مستخدمون مطابقون لهذا البحث.
|
| 242 |
-
</td>
|
| 243 |
-
</tr>
|
| 244 |
-
)}
|
| 245 |
-
</tbody>
|
| 246 |
-
</table>
|
| 247 |
-
</div>
|
| 248 |
-
</section>
|
| 249 |
-
</div>
|
| 250 |
-
);
|
| 251 |
-
};
|
| 252 |
-
|
| 253 |
-
const Field = ({ label, name, formik, type = "text" }) => (
|
| 254 |
-
<div>
|
| 255 |
-
<label className="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">{label}</label>
|
| 256 |
-
<input
|
| 257 |
-
name={name}
|
| 258 |
-
type={type}
|
| 259 |
-
value={formik.values[name]}
|
| 260 |
-
onChange={formik.handleChange}
|
| 261 |
-
onBlur={formik.handleBlur}
|
| 262 |
-
className={`h-12 w-full rounded-2xl border bg-slate-50 px-4 font-medium text-slate-800 outline-none transition focus:bg-white focus:ring-2 focus:ring-emerald-500 ${
|
| 263 |
-
formik.touched[name] && formik.errors[name] ? "border-rose-400" : "border-slate-200"
|
| 264 |
-
}`}
|
| 265 |
-
/>
|
| 266 |
-
{formik.touched[name] && formik.errors[name] ? (
|
| 267 |
-
<p className="mt-1 text-xs font-bold text-rose-500">{formik.errors[name]}</p>
|
| 268 |
-
) : null}
|
| 269 |
-
</div>
|
| 270 |
-
);
|
| 271 |
-
|
| 272 |
-
export default UsersManagementTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/components/Users/UsersTab.jsx
DELETED
|
@@ -1,163 +0,0 @@
|
|
| 1 |
-
import { useState } from "react";
|
| 2 |
-
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
| 3 |
-
import axios from "axios";
|
| 4 |
-
import { UserPlus, Trash2, ShieldCheck, User, Crown } from "lucide-react";
|
| 5 |
-
import LodderSpinner from "../../common/LoaderSpinner";
|
| 6 |
-
import { toast } from "react-toastify";
|
| 7 |
-
import { useFormik } from "formik";
|
| 8 |
-
import * as Yup from "yup";
|
| 9 |
-
|
| 10 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 11 |
-
|
| 12 |
-
const roleConfig = {
|
| 13 |
-
owner: {
|
| 14 |
-
icon: <Crown size={24} />,
|
| 15 |
-
badge: 'bg-amber-50 border-amber-300 text-amber-800',
|
| 16 |
-
iconWrap: 'bg-amber-50 text-amber-600 border-amber-100',
|
| 17 |
-
label: 'Business Owner',
|
| 18 |
-
},
|
| 19 |
-
admin: {
|
| 20 |
-
icon: <ShieldCheck size={24} />,
|
| 21 |
-
badge: 'bg-indigo-600 border-indigo-700 text-white',
|
| 22 |
-
iconWrap: 'bg-indigo-50 text-indigo-600 border-indigo-100',
|
| 23 |
-
label: 'Company Admin',
|
| 24 |
-
},
|
| 25 |
-
cashier: {
|
| 26 |
-
icon: <User size={24} />,
|
| 27 |
-
badge: 'bg-emerald-50 border-emerald-300 text-emerald-800',
|
| 28 |
-
iconWrap: 'bg-emerald-50 text-emerald-600 border-emerald-100',
|
| 29 |
-
label: 'Cashier',
|
| 30 |
-
},
|
| 31 |
-
};
|
| 32 |
-
|
| 33 |
-
const UsersTab = () => {
|
| 34 |
-
const [searchTerm, setSearchTerm] = useState("");
|
| 35 |
-
const queryClient = useQueryClient();
|
| 36 |
-
|
| 37 |
-
const { data, isLoading } = useQuery({
|
| 38 |
-
queryKey: ["users"],
|
| 39 |
-
queryFn: () => axios.get(`${apiUrl}/auth/users`),
|
| 40 |
-
});
|
| 41 |
-
|
| 42 |
-
const users = data?.data || [];
|
| 43 |
-
const filteredUsers = users.filter((u) => u.username.toLowerCase().includes(searchTerm.toLowerCase()));
|
| 44 |
-
|
| 45 |
-
const formik = useFormik({
|
| 46 |
-
initialValues: { username: "", password: "", role: "cashier", fullName: "", email: "", phone: "" },
|
| 47 |
-
validationSchema: Yup.object({
|
| 48 |
-
username: Yup.string().required('Required').trim().min(3, 'Minimum 3 characters'),
|
| 49 |
-
password: Yup.string().required('Required').min(6, 'Minimum 6 characters'),
|
| 50 |
-
role: Yup.string().oneOf(['admin', 'cashier']),
|
| 51 |
-
}),
|
| 52 |
-
onSubmit: (values, { resetForm }) => {
|
| 53 |
-
axios.post(`${apiUrl}/auth/users`, values)
|
| 54 |
-
.then(() => {
|
| 55 |
-
queryClient.invalidateQueries({ queryKey: ["users"] });
|
| 56 |
-
resetForm();
|
| 57 |
-
toast.success("User created successfully");
|
| 58 |
-
})
|
| 59 |
-
.catch((err) => toast.error(err.response?.data?.message || "Failed to create user"));
|
| 60 |
-
},
|
| 61 |
-
});
|
| 62 |
-
|
| 63 |
-
const deleteMutation = useMutation({
|
| 64 |
-
mutationFn: (id) => axios.delete(`${apiUrl}/auth/users/${id}`),
|
| 65 |
-
onSuccess: () => {
|
| 66 |
-
queryClient.invalidateQueries({ queryKey: ["users"] });
|
| 67 |
-
toast.success("User deleted successfully");
|
| 68 |
-
},
|
| 69 |
-
onError: (err) => toast.error(err.response?.data?.message || "Delete failed"),
|
| 70 |
-
});
|
| 71 |
-
|
| 72 |
-
if (isLoading) return <LodderSpinner />;
|
| 73 |
-
|
| 74 |
-
return (
|
| 75 |
-
<div className="space-y-6">
|
| 76 |
-
<div className="flex flex-col gap-4 bg-white p-6 rounded-3xl shadow-sm border border-slate-200">
|
| 77 |
-
<form onSubmit={formik.handleSubmit} className="w-full bg-slate-50 p-4 rounded-2xl border border-slate-100 grid md:grid-cols-3 xl:grid-cols-6 gap-4 relative">
|
| 78 |
-
<Input label="Username" name="username" placeholder="Login username" formik={formik} />
|
| 79 |
-
<Input label="Full name" name="fullName" placeholder="Staff name" formik={formik} />
|
| 80 |
-
<Input label="Password" name="password" placeholder="Temporary password" formik={formik} />
|
| 81 |
-
<Input label="Email" name="email" placeholder="email@example.com" formik={formik} />
|
| 82 |
-
<Input label="Phone" name="phone" placeholder="01xxxxxxxxx" formik={formik} />
|
| 83 |
-
<div>
|
| 84 |
-
<label className="text-xs font-bold text-slate-500 mb-1 block">Role</label>
|
| 85 |
-
<select name="role" value={formik.values.role} onChange={formik.handleChange} onBlur={formik.handleBlur} className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2.5 outline-none focus:ring-2 focus:ring-emerald-500 font-bold text-slate-700 cursor-pointer">
|
| 86 |
-
<option value="cashier">Cashier</option>
|
| 87 |
-
<option value="admin">Company Admin</option>
|
| 88 |
-
</select>
|
| 89 |
-
</div>
|
| 90 |
-
<button type="submit" disabled={formik.isSubmitting} className="md:col-span-3 xl:col-span-6 h-[46px] bg-emerald-600 hover:bg-emerald-700 text-white px-4 rounded-xl font-bold transition-all disabled:opacity-50 flex items-center justify-center gap-2 shadow-sm shadow-emerald-600/20">
|
| 91 |
-
<UserPlus size={18} /> Add user
|
| 92 |
-
</button>
|
| 93 |
-
</form>
|
| 94 |
-
|
| 95 |
-
<input type="text" placeholder="Search by username..." value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} className="w-full md:w-72 bg-slate-50 border border-slate-200 rounded-xl px-4 py-3 outline-none font-medium text-slate-700" />
|
| 96 |
-
</div>
|
| 97 |
-
|
| 98 |
-
<div className="bg-white rounded-3xl shadow-sm border border-slate-200 overflow-hidden">
|
| 99 |
-
<div className="overflow-x-auto relative">
|
| 100 |
-
<table className="w-full text-left border-collapse">
|
| 101 |
-
<thead className="bg-slate-50 border-b border-slate-200">
|
| 102 |
-
<tr>
|
| 103 |
-
<th className="p-4 font-bold text-slate-600">User</th>
|
| 104 |
-
<th className="p-4 font-bold text-slate-600 text-center">Role</th>
|
| 105 |
-
<th className="p-4 font-bold text-slate-600 text-center">Delete</th>
|
| 106 |
-
</tr>
|
| 107 |
-
</thead>
|
| 108 |
-
<tbody className="divide-y divide-slate-100">
|
| 109 |
-
{filteredUsers.map((user) => {
|
| 110 |
-
const config = roleConfig[user.role] || roleConfig.cashier;
|
| 111 |
-
|
| 112 |
-
return (
|
| 113 |
-
<tr key={user._id} className="hover:bg-slate-50 transition-colors">
|
| 114 |
-
<td className="p-4 font-bold text-slate-800 flex items-center gap-3">
|
| 115 |
-
<div className={`w-12 h-12 rounded-xl flex items-center justify-center border shadow-sm ${config.iconWrap}`}>
|
| 116 |
-
{config.icon}
|
| 117 |
-
</div>
|
| 118 |
-
<div>
|
| 119 |
-
<p className="text-lg">{user.fullName || user.username}</p>
|
| 120 |
-
<p className="text-sm text-slate-500 font-medium">{user.username}</p>
|
| 121 |
-
</div>
|
| 122 |
-
</td>
|
| 123 |
-
<td className="p-4 text-center">
|
| 124 |
-
<span className={`px-4 py-1.5 rounded-full text-sm font-bold border shadow-sm ${config.badge}`}>
|
| 125 |
-
{config.label}
|
| 126 |
-
</span>
|
| 127 |
-
</td>
|
| 128 |
-
<td className="p-4 text-center">
|
| 129 |
-
<button onClick={() => {
|
| 130 |
-
if (window.confirm("Delete this user account permanently?")) {
|
| 131 |
-
deleteMutation.mutate(user._id);
|
| 132 |
-
}
|
| 133 |
-
}} className="p-3 text-rose-500 bg-rose-50 rounded-lg hover:bg-rose-600 hover:text-white transition-colors border border-rose-100 shadow-sm" title="Delete user">
|
| 134 |
-
<Trash2 size={20} />
|
| 135 |
-
</button>
|
| 136 |
-
</td>
|
| 137 |
-
</tr>
|
| 138 |
-
);
|
| 139 |
-
})}
|
| 140 |
-
{filteredUsers.length === 0 && (
|
| 141 |
-
<tr>
|
| 142 |
-
<td colSpan="3" className="p-16 text-center text-slate-400 font-bold">
|
| 143 |
-
No matching users found
|
| 144 |
-
</td>
|
| 145 |
-
</tr>
|
| 146 |
-
)}
|
| 147 |
-
</tbody>
|
| 148 |
-
</table>
|
| 149 |
-
</div>
|
| 150 |
-
</div>
|
| 151 |
-
</div>
|
| 152 |
-
);
|
| 153 |
-
};
|
| 154 |
-
|
| 155 |
-
const Input = ({ label, name, placeholder, formik }) => (
|
| 156 |
-
<div className="relative">
|
| 157 |
-
<label className="text-xs font-bold text-slate-500 mb-1 block">{label}</label>
|
| 158 |
-
<input name={name} type="text" value={formik.values[name]} onChange={formik.handleChange} onBlur={formik.handleBlur} className={`w-full bg-white border ${formik.touched[name] && formik.errors[name] ? 'border-rose-400' : 'border-slate-200'} rounded-xl px-3 py-2.5 outline-none focus:ring-2 focus:ring-emerald-500 font-bold text-slate-700`} placeholder={placeholder} />
|
| 159 |
-
{formik.touched[name] && formik.errors[name] && <span className="absolute -bottom-4 left-1 text-rose-500 text-[10px] font-bold">{formik.errors[name]}</span>}
|
| 160 |
-
</div>
|
| 161 |
-
);
|
| 162 |
-
|
| 163 |
-
export default UsersTab;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/hooks/useProducts.jsx
DELETED
|
@@ -1,21 +0,0 @@
|
|
| 1 |
-
import { useQuery } from "@tanstack/react-query";
|
| 2 |
-
import axios from "axios";
|
| 3 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 4 |
-
|
| 5 |
-
const useProducts = (category) => {
|
| 6 |
-
function fetchProducts() {
|
| 7 |
-
return axios.get(`${apiUrl}/products`, {
|
| 8 |
-
params: category ? { category } : {},
|
| 9 |
-
});
|
| 10 |
-
}
|
| 11 |
-
const { data, isLoading } = useQuery({
|
| 12 |
-
queryKey: ['products', category ? category :''],
|
| 13 |
-
queryFn: fetchProducts,
|
| 14 |
-
refetchOnMount: false,
|
| 15 |
-
refetchOnWindowFocus: false,
|
| 16 |
-
// enabled: category ? true : false,
|
| 17 |
-
})
|
| 18 |
-
return { data, isLoading };
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
export default useProducts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/index.css
DELETED
|
@@ -1,30 +0,0 @@
|
|
| 1 |
-
@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@200..1000&family=Outfit:wght@100..900&family=Playfair:ital,opsz,wght@0,5..1200,300..900;1,5..1200,300..900&family=Roboto:ital,wght@0,100..900;1,100..900&display=swap');
|
| 2 |
-
|
| 3 |
-
@import 'tailwindcss';
|
| 4 |
-
|
| 5 |
-
html,body{
|
| 6 |
-
width: 100%;
|
| 7 |
-
overflow-x: hidden;
|
| 8 |
-
}
|
| 9 |
-
body{
|
| 10 |
-
background-color: #F7F9FB;
|
| 11 |
-
font-family: "Cairo", "Outfit", sans-serif;
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
/* width */
|
| 16 |
-
.custom-scroll::-webkit-scrollbar {
|
| 17 |
-
width: 10px;
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
/* Track */
|
| 21 |
-
.custom-scroll::-webkit-scrollbar-track {
|
| 22 |
-
background: #f1f1f1;
|
| 23 |
-
border-radius: 10px;
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
/* Handle */
|
| 27 |
-
.custom-scroll::-webkit-scrollbar-thumb {
|
| 28 |
-
background: linear-gradient(180deg, #eee, #cecece);
|
| 29 |
-
border-radius: 10px;
|
| 30 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/main.jsx
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
import { createRoot } from 'react-dom/client'
|
| 2 |
-
import './index.css'
|
| 3 |
-
import App from './App.jsx'
|
| 4 |
-
|
| 5 |
-
createRoot(document.getElementById('root')).render(
|
| 6 |
-
<App />
|
| 7 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/pages/Hero.jsx
DELETED
|
@@ -1,178 +0,0 @@
|
|
| 1 |
-
import {
|
| 2 |
-
Building2,
|
| 3 |
-
LayoutDashboard,
|
| 4 |
-
LogOut,
|
| 5 |
-
Package,
|
| 6 |
-
ReceiptText,
|
| 7 |
-
Settings2,
|
| 8 |
-
ShieldCheck,
|
| 9 |
-
Users2,
|
| 10 |
-
Wrench,
|
| 11 |
-
} from "lucide-react";
|
| 12 |
-
import { useMemo, useState } from "react";
|
| 13 |
-
import { useNavigate } from "react-router-dom";
|
| 14 |
-
import { useQuery } from "@tanstack/react-query";
|
| 15 |
-
import axios from "axios";
|
| 16 |
-
import DashboardTab from "../components/Dashboard/DashboardTab";
|
| 17 |
-
import CustomersWorkspace from "../components/Customers/CustomersWorkspace";
|
| 18 |
-
import InventoryWorkspace from "../components/Inventory/InventoryWorkspace";
|
| 19 |
-
import ReportsWorkspace from "../components/Reports/ReportsWorkspace";
|
| 20 |
-
import UsersManagementTab from "../components/Users/UsersManagementTab";
|
| 21 |
-
import TenantsAdminTab from "../components/Tenants/TenantsAdminTab";
|
| 22 |
-
import CompanyProfileTab from "../components/Settings/CompanyProfileTab";
|
| 23 |
-
import SuppliersWorkspace from "../components/Suppliers/SuppliersWorkspace";
|
| 24 |
-
import { useAuth } from "../Context/AuthContext";
|
| 25 |
-
import { getPermissions } from "../utils/permissions";
|
| 26 |
-
import { formatCurrency, formatNumber } from "../utils/formatters";
|
| 27 |
-
|
| 28 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 29 |
-
|
| 30 |
-
const roleLabelMap = {
|
| 31 |
-
super_admin: "مسؤول المنصة",
|
| 32 |
-
owner: "صاحب العمل",
|
| 33 |
-
admin: "مدير الشركة",
|
| 34 |
-
cashier: "أمين الصندوق",
|
| 35 |
-
};
|
| 36 |
-
|
| 37 |
-
const Hero = () => {
|
| 38 |
-
const navigate = useNavigate();
|
| 39 |
-
const { user, logout } = useAuth();
|
| 40 |
-
const permissions = getPermissions(user);
|
| 41 |
-
const companyName = user?.tenant?.name || "نظام إدارة المبيعات";
|
| 42 |
-
const currency = user?.tenant?.settings?.currency || "EGP";
|
| 43 |
-
const [activeTab, setActiveTab] = useState(permissions.isSuperAdmin ? "tenants" : "dashboard");
|
| 44 |
-
|
| 45 |
-
const tabs = permissions.isSuperAdmin
|
| 46 |
-
? [{ id: "tenants", label: "المستأجرين", icon: Building2 }]
|
| 47 |
-
: [
|
| 48 |
-
{ id: "dashboard", label: "لوحة التحكم", icon: LayoutDashboard },
|
| 49 |
-
{ id: "customers", label: "العملاء", icon: Users2 },
|
| 50 |
-
{ id: "inventory", label: "المخزون", icon: Package },
|
| 51 |
-
...(permissions.canViewSuppliers ? [{ id: "suppliers", label: "الموردون", icon: Wrench }] : []),
|
| 52 |
-
...(permissions.canViewReports ? [{ id: "reports", label: "التقارير", icon: ReceiptText }] : []),
|
| 53 |
-
...(permissions.canManageUsers ? [{ id: "users", label: "المستخدمين", icon: ShieldCheck }] : []),
|
| 54 |
-
...(permissions.canManageCompany ? [{ id: "company", label: "الشركة", icon: Settings2 }] : []),
|
| 55 |
-
];
|
| 56 |
-
|
| 57 |
-
const renderActiveTab = () => {
|
| 58 |
-
switch (activeTab) {
|
| 59 |
-
case "tenants":
|
| 60 |
-
return <TenantsAdminTab />;
|
| 61 |
-
case "dashboard":
|
| 62 |
-
return <DashboardTab />;
|
| 63 |
-
case "customers":
|
| 64 |
-
return <CustomersWorkspace />;
|
| 65 |
-
case "inventory":
|
| 66 |
-
return <InventoryWorkspace />;
|
| 67 |
-
case "suppliers":
|
| 68 |
-
return permissions.canViewSuppliers ? <SuppliersWorkspace /> : null;
|
| 69 |
-
case "reports":
|
| 70 |
-
return permissions.canViewReports ? <ReportsWorkspace /> : null;
|
| 71 |
-
case "users":
|
| 72 |
-
return permissions.canManageUsers ? <UsersManagementTab /> : null;
|
| 73 |
-
case "company":
|
| 74 |
-
return permissions.canManageCompany ? <CompanyProfileTab /> : null;
|
| 75 |
-
default:
|
| 76 |
-
return permissions.isSuperAdmin ? <TenantsAdminTab /> : <DashboardTab />;
|
| 77 |
-
}
|
| 78 |
-
};
|
| 79 |
-
|
| 80 |
-
const { data } = useQuery({
|
| 81 |
-
queryKey: ["reports", "overview"],
|
| 82 |
-
queryFn: () => axios.get(`${apiUrl}/reports/overview`),
|
| 83 |
-
refetchOnWindowFocus: false,
|
| 84 |
-
enabled: permissions.isTenantUser,
|
| 85 |
-
});
|
| 86 |
-
|
| 87 |
-
const metrics = data?.data?.metrics || {};
|
| 88 |
-
const statCards = useMemo(
|
| 89 |
-
() => [
|
| 90 |
-
{ label: "العملاء", value: formatNumber(metrics.customersCount) },
|
| 91 |
-
{ label: "المستحقات", value: formatCurrency(metrics.outstandingReceivables, currency) },
|
| 92 |
-
{ label: "مخزون منخفض", value: formatNumber(metrics.lowStockCount) },
|
| 93 |
-
],
|
| 94 |
-
[currency, metrics.customersCount, metrics.lowStockCount, metrics.outstandingReceivables]
|
| 95 |
-
);
|
| 96 |
-
|
| 97 |
-
const handleLogout = async () => {
|
| 98 |
-
await logout();
|
| 99 |
-
navigate("/login", { replace: true });
|
| 100 |
-
};
|
| 101 |
-
|
| 102 |
-
return (
|
| 103 |
-
<div className="min-h-screen bg-slate-50 text-slate-900" dir="rtl">
|
| 104 |
-
<header className="relative overflow-hidden bg-[radial-gradient(circle_at_top_left,_rgba(16,185,129,0.28),_transparent_32%),linear-gradient(135deg,_#0f172a,_#111827_48%,_#1e293b)] text-white">
|
| 105 |
-
<div className="absolute inset-0 opacity-30 [background-image:linear-gradient(rgba(255,255,255,0.06)_1px,transparent_1px),linear-gradient(90deg,rgba(255,255,255,0.06)_1px,transparent_1px)] [background-size:28px_28px]" />
|
| 106 |
-
<div className="relative mx-auto max-w-7xl px-4 py-10 md:px-6 md:py-12">
|
| 107 |
-
<div className="flex flex-col gap-8 lg:flex-row lg:items-start lg:justify-between">
|
| 108 |
-
<div className="max-w-3xl">
|
| 109 |
-
<div className="mb-4 inline-flex items-center gap-2 rounded-full border border-white/10 bg-white/10 px-3 py-1 text-xs font-bold uppercase tracking-[0.25em] text-emerald-200 backdrop-blur-sm">
|
| 110 |
-
{permissions.isSuperAdmin ? "مساحة عمل المنصة" : "مساحة عمل الشركة"}
|
| 111 |
-
</div>
|
| 112 |
-
<h1 className="text-4xl font-black tracking-tight md:text-5xl">
|
| 113 |
-
{permissions.isSuperAdmin ? "مركز تحكم المنصة" : companyName}
|
| 114 |
-
</h1>
|
| 115 |
-
<p className="mt-4 max-w-2xl text-sm leading-7 text-slate-300 md:text-base">
|
| 116 |
-
{permissions.isSuperAdmin
|
| 117 |
-
? "إدارة المستأجرين التجاريين، التحكم في حالتهم، وإرسال بيانات الاعتماد عبر واتساب من مكان واحد."
|
| 118 |
-
: "مساحة عمل محاسبية متعددة المستأجرين لإدارة العملاء والمخزون والمدفوعات والتقارير وصلاحيات الموظفين."}
|
| 119 |
-
</p>
|
| 120 |
-
</div>
|
| 121 |
-
|
| 122 |
-
<div className="grid gap-4 md:grid-cols-2 lg:w-[420px] lg:grid-cols-1">
|
| 123 |
-
<div className="rounded-3xl border border-white/10 bg-white/10 p-5 backdrop-blur-sm">
|
| 124 |
-
<p className="text-xs font-bold uppercase tracking-[0.22em] text-slate-300">مسجّل الدخول كـ</p>
|
| 125 |
-
<p className="mt-2 text-xl font-black text-white">{user?.fullName || user?.username}</p>
|
| 126 |
-
<p className="mt-1 text-sm font-bold text-emerald-200">{roleLabelMap[user?.role] || user?.role}</p>
|
| 127 |
-
</div>
|
| 128 |
-
|
| 129 |
-
{!permissions.isSuperAdmin ? (
|
| 130 |
-
<div className="grid gap-3 sm:grid-cols-3 lg:grid-cols-3">
|
| 131 |
-
{statCards.map((card) => (
|
| 132 |
-
<div key={card.label} className="rounded-3xl border border-white/10 bg-white/10 p-4 backdrop-blur-sm">
|
| 133 |
-
<p className="text-xs font-bold uppercase tracking-wide text-slate-300">{card.label}</p>
|
| 134 |
-
<p className="mt-2 text-lg font-black text-white">{card.value}</p>
|
| 135 |
-
</div>
|
| 136 |
-
))}
|
| 137 |
-
</div>
|
| 138 |
-
) : null}
|
| 139 |
-
|
| 140 |
-
<button
|
| 141 |
-
type="button"
|
| 142 |
-
onClick={handleLogout}
|
| 143 |
-
className="inline-flex h-12 items-center justify-center gap-2 rounded-2xl border border-rose-400/20 bg-rose-500/10 px-4 font-bold text-rose-100 transition hover:bg-rose-500/20"
|
| 144 |
-
>
|
| 145 |
-
<LogOut size={18} /> تسجيل الخروج
|
| 146 |
-
</button>
|
| 147 |
-
</div>
|
| 148 |
-
</div>
|
| 149 |
-
</div>
|
| 150 |
-
</header>
|
| 151 |
-
|
| 152 |
-
<main className="mx-auto max-w-7xl px-4 py-8 md:px-6">
|
| 153 |
-
<div className="mb-6 flex flex-wrap gap-2 rounded-3xl border border-slate-200 bg-white p-2 shadow-sm">
|
| 154 |
-
{tabs.map((tab) => {
|
| 155 |
-
const Icon = tab.icon;
|
| 156 |
-
const active = activeTab === tab.id;
|
| 157 |
-
return (
|
| 158 |
-
<button
|
| 159 |
-
key={tab.id}
|
| 160 |
-
type="button"
|
| 161 |
-
onClick={() => setActiveTab(tab.id)}
|
| 162 |
-
className={`inline-flex h-11 items-center justify-center gap-2 rounded-2xl px-4 font-bold transition ${active ? "bg-emerald-600 text-white shadow-lg shadow-emerald-600/20" : "bg-slate-50 text-slate-600 hover:bg-slate-100"
|
| 163 |
-
}`}
|
| 164 |
-
>
|
| 165 |
-
<Icon size={16} />
|
| 166 |
-
{tab.label}
|
| 167 |
-
</button>
|
| 168 |
-
);
|
| 169 |
-
})}
|
| 170 |
-
</div>
|
| 171 |
-
|
| 172 |
-
{renderActiveTab()}
|
| 173 |
-
</main>
|
| 174 |
-
</div>
|
| 175 |
-
);
|
| 176 |
-
};
|
| 177 |
-
|
| 178 |
-
export default Hero;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/pages/Login.jsx
DELETED
|
@@ -1,137 +0,0 @@
|
|
| 1 |
-
import axios from 'axios';
|
| 2 |
-
import { toast } from 'react-toastify';
|
| 3 |
-
import { Lock, User, LogIn, Store, LoaderCircle } from 'lucide-react';
|
| 4 |
-
import { useFormik } from 'formik';
|
| 5 |
-
import * as Yup from 'yup';
|
| 6 |
-
import { useEffect, useState } from 'react';
|
| 7 |
-
import { useNavigate } from 'react-router-dom';
|
| 8 |
-
import { useAuth } from '../Context/AuthContext';
|
| 9 |
-
|
| 10 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 11 |
-
|
| 12 |
-
const Login = () => {
|
| 13 |
-
const [setupLoading, setSetupLoading] = useState(false);
|
| 14 |
-
const [hasAdmin, setHasAdmin] = useState(true);
|
| 15 |
-
const navigate = useNavigate();
|
| 16 |
-
const { login } = useAuth();
|
| 17 |
-
|
| 18 |
-
useEffect(() => {
|
| 19 |
-
axios.get(`${apiUrl}/auth/has-admin`)
|
| 20 |
-
.then((res) => setHasAdmin(res.data.hasAdmin))
|
| 21 |
-
.catch(() => setHasAdmin(true));
|
| 22 |
-
}, []);
|
| 23 |
-
|
| 24 |
-
const handleSetup = async () => {
|
| 25 |
-
try {
|
| 26 |
-
setSetupLoading(true);
|
| 27 |
-
const res = await axios.post(`${apiUrl}/auth/setup`);
|
| 28 |
-
toast.success(res.data.message);
|
| 29 |
-
|
| 30 |
-
if (res.data.credentials) {
|
| 31 |
-
toast.info(`Username: ${res.data.credentials.username} | Password: ${res.data.credentials.password}`);
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
setHasAdmin(true);
|
| 35 |
-
} catch (err) {
|
| 36 |
-
toast.error(err.response?.data?.message || 'Setup failed');
|
| 37 |
-
} finally {
|
| 38 |
-
setSetupLoading(false);
|
| 39 |
-
}
|
| 40 |
-
};
|
| 41 |
-
|
| 42 |
-
const formik = useFormik({
|
| 43 |
-
initialValues: { identifier: '', password: '' },
|
| 44 |
-
validationSchema: Yup.object({
|
| 45 |
-
identifier: Yup.string().required('Username or email is required'),
|
| 46 |
-
password: Yup.string().required('Password is required'),
|
| 47 |
-
}),
|
| 48 |
-
onSubmit: async (values, { setSubmitting }) => {
|
| 49 |
-
try {
|
| 50 |
-
const res = await login(values);
|
| 51 |
-
toast.success(`Welcome back ${res.user?.fullName || res.user?.username}`);
|
| 52 |
-
navigate('/');
|
| 53 |
-
} catch (err) {
|
| 54 |
-
toast.error(err.response?.data?.message || 'Invalid login credentials');
|
| 55 |
-
} finally {
|
| 56 |
-
setSubmitting(false);
|
| 57 |
-
}
|
| 58 |
-
},
|
| 59 |
-
});
|
| 60 |
-
|
| 61 |
-
return (
|
| 62 |
-
<div className="min-h-screen bg-slate-900 flex items-center justify-center p-4 relative overflow-hidden">
|
| 63 |
-
<div className="absolute top-[-10%] left-[-10%] w-96 h-96 bg-emerald-500/15 rounded-full blur-3xl"></div>
|
| 64 |
-
<div className="absolute bottom-[-10%] right-[-10%] w-96 h-96 bg-amber-500/10 rounded-full blur-3xl"></div>
|
| 65 |
-
|
| 66 |
-
<div className="bg-white rounded-3xl shadow-2xl w-full max-w-md overflow-hidden relative z-10 animate-in fade-in zoom-in-95 duration-500">
|
| 67 |
-
<div className="bg-slate-50 border-b border-slate-100 p-8 text-center relative overflow-hidden">
|
| 68 |
-
<div className="absolute top-0 right-0 w-full h-1.5 bg-gradient-to-l from-emerald-500 to-amber-500"></div>
|
| 69 |
-
<div className="w-20 h-20 bg-emerald-100 text-emerald-600 rounded-2xl flex items-center justify-center mx-auto mb-4 shadow-inner rotate-3">
|
| 70 |
-
<Store size={40} />
|
| 71 |
-
</div>
|
| 72 |
-
<h1 className="text-3xl font-black text-slate-800 tracking-tight">Cloud Accounting Platform</h1>
|
| 73 |
-
<p className="text-slate-500 mt-2 font-medium">Sign in to manage your company, invoices, and staff</p>
|
| 74 |
-
</div>
|
| 75 |
-
|
| 76 |
-
<form onSubmit={formik.handleSubmit} className="p-8 space-y-6">
|
| 77 |
-
<div>
|
| 78 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">Username or Email</label>
|
| 79 |
-
<div className="relative">
|
| 80 |
-
<div className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400">
|
| 81 |
-
<User size={20} />
|
| 82 |
-
</div>
|
| 83 |
-
<input
|
| 84 |
-
name="identifier"
|
| 85 |
-
type="text"
|
| 86 |
-
value={formik.values.identifier}
|
| 87 |
-
onChange={formik.handleChange}
|
| 88 |
-
onBlur={formik.handleBlur}
|
| 89 |
-
className={`w-full pr-4 pl-12 py-3.5 bg-slate-50 border ${formik.touched.identifier && formik.errors.identifier ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl outline-none focus:ring-2 font-bold text-slate-700 transition-colors focus:bg-white`}
|
| 90 |
-
placeholder="Enter your username or email"
|
| 91 |
-
/>
|
| 92 |
-
</div>
|
| 93 |
-
{formik.touched.identifier && formik.errors.identifier && (
|
| 94 |
-
<p className="text-rose-500 text-xs font-bold mt-1.5">{formik.errors.identifier}</p>
|
| 95 |
-
)}
|
| 96 |
-
</div>
|
| 97 |
-
|
| 98 |
-
<div>
|
| 99 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">Password</label>
|
| 100 |
-
<div className="relative">
|
| 101 |
-
<div className="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400">
|
| 102 |
-
<Lock size={20} />
|
| 103 |
-
</div>
|
| 104 |
-
<input
|
| 105 |
-
name="password"
|
| 106 |
-
type="password"
|
| 107 |
-
value={formik.values.password}
|
| 108 |
-
onChange={formik.handleChange}
|
| 109 |
-
onBlur={formik.handleBlur}
|
| 110 |
-
className={`w-full pr-4 pl-12 py-3.5 bg-slate-50 border ${formik.touched.password && formik.errors.password ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl outline-none focus:ring-2 font-bold text-slate-700 transition-colors focus:bg-white tracking-widest`}
|
| 111 |
-
placeholder="��������"
|
| 112 |
-
/>
|
| 113 |
-
</div>
|
| 114 |
-
{formik.touched.password && formik.errors.password && (
|
| 115 |
-
<p className="text-rose-500 text-xs font-bold mt-1.5">{formik.errors.password}</p>
|
| 116 |
-
)}
|
| 117 |
-
</div>
|
| 118 |
-
|
| 119 |
-
<button disabled={formik.isSubmitting} type="submit" className="w-full py-4 bg-emerald-600 text-white rounded-xl font-black text-lg hover:bg-emerald-700 disabled:opacity-50 transition-all shadow-lg shadow-emerald-600/30 flex justify-center items-center gap-2 hover:-translate-y-0.5 mt-2 border border-emerald-500">
|
| 120 |
-
{formik.isSubmitting ? <LoaderCircle className="w-4 h-4 animate-spin" /> : <><LogIn size={20} /> Sign In</>}
|
| 121 |
-
</button>
|
| 122 |
-
</form>
|
| 123 |
-
|
| 124 |
-
{!hasAdmin && (
|
| 125 |
-
<div className="p-4 bg-slate-50 text-center border-t border-slate-100 pb-5">
|
| 126 |
-
<button onClick={handleSetup} disabled={setupLoading} className="text-xs text-slate-500 hover:text-emerald-600 font-bold transition-colors">
|
| 127 |
-
Run first-time platform setup
|
| 128 |
-
</button>
|
| 129 |
-
</div>
|
| 130 |
-
)}
|
| 131 |
-
</div>
|
| 132 |
-
</div>
|
| 133 |
-
);
|
| 134 |
-
};
|
| 135 |
-
|
| 136 |
-
export default Login;
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/pages/Login_fixed.jsx
DELETED
|
@@ -1,286 +0,0 @@
|
|
| 1 |
-
import axios from 'axios';
|
| 2 |
-
import { toast } from 'react-toastify';
|
| 3 |
-
import { Lock, User, LogIn, Store, LoaderCircle, ShieldCheck, X, Eye, EyeOff } from 'lucide-react';
|
| 4 |
-
import { useFormik } from 'formik';
|
| 5 |
-
import * as Yup from 'yup';
|
| 6 |
-
import { useEffect, useState } from 'react';
|
| 7 |
-
import { useNavigate } from 'react-router-dom';
|
| 8 |
-
import { useAuth } from '../Context/AuthContext';
|
| 9 |
-
|
| 10 |
-
const apiUrl = import.meta.env.VITE_API_URL;
|
| 11 |
-
|
| 12 |
-
const Login = () => {
|
| 13 |
-
const [setupLoading, setSetupLoading] = useState(false);
|
| 14 |
-
const [hasAdmin, setHasAdmin] = useState(true);
|
| 15 |
-
const [showSetupModal, setShowSetupModal] = useState(false);
|
| 16 |
-
const [showSetupPassword, setShowSetupPassword] = useState(false);
|
| 17 |
-
const [setupForm, setSetupForm] = useState({ username: '', password: '', fullName: '' });
|
| 18 |
-
const [setupErrors, setSetupErrors] = useState({});
|
| 19 |
-
const navigate = useNavigate();
|
| 20 |
-
const { login } = useAuth();
|
| 21 |
-
|
| 22 |
-
useEffect(() => {
|
| 23 |
-
axios.get(`${apiUrl}/auth/has-admin`)
|
| 24 |
-
.then((res) => setHasAdmin(res.data.hasAdmin))
|
| 25 |
-
.catch(() => setHasAdmin(true));
|
| 26 |
-
}, []);
|
| 27 |
-
|
| 28 |
-
const validateSetupForm = () => {
|
| 29 |
-
const errors = {};
|
| 30 |
-
if (!setupForm.username.trim()) errors.username = 'اسم المستخدم مطلوب';
|
| 31 |
-
else if (setupForm.username.trim().length < 3) errors.username = '3 أحرف على الأقل';
|
| 32 |
-
if (!setupForm.password.trim()) errors.password = 'كلمة المرور مطلوبة';
|
| 33 |
-
else if (setupForm.password.trim().length < 6) errors.password = '6 أحرف على الأقل';
|
| 34 |
-
setSetupErrors(errors);
|
| 35 |
-
return Object.keys(errors).length === 0;
|
| 36 |
-
};
|
| 37 |
-
|
| 38 |
-
const handleSetup = async () => {
|
| 39 |
-
if (!validateSetupForm()) return;
|
| 40 |
-
|
| 41 |
-
setSetupLoading(true);
|
| 42 |
-
try {
|
| 43 |
-
const res = await axios.post(`${apiUrl}/auth/setup`, {
|
| 44 |
-
username: setupForm.username.trim(),
|
| 45 |
-
password: setupForm.password.trim(),
|
| 46 |
-
fullName: setupForm.fullName.trim() || setupForm.username.trim(),
|
| 47 |
-
});
|
| 48 |
-
toast.success(res.data.message || 'تم إنشاء حساب المسؤول بنجاح');
|
| 49 |
-
if (res.data.credentials) {
|
| 50 |
-
toast.info(`اسم المستخدم: ${res.data.credentials.username}`);
|
| 51 |
-
}
|
| 52 |
-
setHasAdmin(true);
|
| 53 |
-
setShowSetupModal(false);
|
| 54 |
-
setSetupForm({ username: '', password: '', fullName: '' });
|
| 55 |
-
} catch (err) {
|
| 56 |
-
toast.error(err.response?.data?.message || 'فشل الإعداد. يرجى المحاولة مرة أخرى.');
|
| 57 |
-
} finally {
|
| 58 |
-
setSetupLoading(false);
|
| 59 |
-
}
|
| 60 |
-
};
|
| 61 |
-
|
| 62 |
-
const formik = useFormik({
|
| 63 |
-
initialValues: { identifier: '', password: '' },
|
| 64 |
-
validationSchema: Yup.object({
|
| 65 |
-
identifier: Yup.string().required('اسم المستخدم أو البريد الإلكتروني مطلوب'),
|
| 66 |
-
password: Yup.string().required('كلمة المرور مطلوبة'),
|
| 67 |
-
}),
|
| 68 |
-
onSubmit: async (values, { setSubmitting }) => {
|
| 69 |
-
try {
|
| 70 |
-
const res = await login(values);
|
| 71 |
-
toast.success(`مرحباً بعودتك ${res.user?.fullName || res.user?.username}`);
|
| 72 |
-
navigate('/');
|
| 73 |
-
} catch (err) {
|
| 74 |
-
toast.error(err.response?.data?.message || 'بيانات الدخول غير صحيحة');
|
| 75 |
-
} finally {
|
| 76 |
-
setSubmitting(false);
|
| 77 |
-
}
|
| 78 |
-
},
|
| 79 |
-
});
|
| 80 |
-
|
| 81 |
-
return (
|
| 82 |
-
<div className="min-h-screen bg-slate-900 flex items-center justify-center p-4 relative overflow-hidden" dir="rtl">
|
| 83 |
-
<div className="absolute top-[-10%] left-[-10%] w-96 h-96 bg-emerald-500/15 rounded-full blur-3xl"></div>
|
| 84 |
-
<div className="absolute bottom-[-10%] right-[-10%] w-96 h-96 bg-amber-500/10 rounded-full blur-3xl"></div>
|
| 85 |
-
|
| 86 |
-
<div className="bg-white rounded-3xl shadow-2xl w-full max-w-md overflow-hidden relative z-10 animate-in fade-in zoom-in-95 duration-500">
|
| 87 |
-
<div className="bg-slate-50 border-b border-slate-100 p-8 text-center relative overflow-hidden">
|
| 88 |
-
<div className="absolute top-0 right-0 w-full h-1.5 bg-gradient-to-l from-emerald-500 to-amber-500"></div>
|
| 89 |
-
<div className="w-20 h-20 bg-emerald-100 text-emerald-600 rounded-2xl flex items-center justify-center mx-auto mb-4 shadow-inner rotate-3">
|
| 90 |
-
<Store size={40} />
|
| 91 |
-
</div>
|
| 92 |
-
<h1 className="text-3xl font-black text-slate-800 tracking-tight">نظام إدارة المبيعات</h1>
|
| 93 |
-
<p className="text-slate-500 mt-2 font-medium">سجّل دخولك لإدارة الشركة والفواتير والموظفين</p>
|
| 94 |
-
</div>
|
| 95 |
-
|
| 96 |
-
<form onSubmit={formik.handleSubmit} className="p-8 space-y-6">
|
| 97 |
-
<div>
|
| 98 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">اسم المستخدم أو البريد الإلكتروني</label>
|
| 99 |
-
<div className="relative">
|
| 100 |
-
<div className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400">
|
| 101 |
-
<User size={20} />
|
| 102 |
-
</div>
|
| 103 |
-
<input
|
| 104 |
-
name="identifier"
|
| 105 |
-
type="text"
|
| 106 |
-
value={formik.values.identifier}
|
| 107 |
-
onChange={formik.handleChange}
|
| 108 |
-
onBlur={formik.handleBlur}
|
| 109 |
-
className={`w-full pl-4 pr-12 py-3.5 bg-slate-50 border ${formik.touched.identifier && formik.errors.identifier ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl outline-none focus:ring-2 font-bold text-slate-700 transition-colors focus:bg-white`}
|
| 110 |
-
placeholder="أدخل اسم المستخدم أو البريد الإلكتروني"
|
| 111 |
-
/>
|
| 112 |
-
</div>
|
| 113 |
-
{formik.touched.identifier && formik.errors.identifier && (
|
| 114 |
-
<p className="text-rose-500 text-xs font-bold mt-1.5">{formik.errors.identifier}</p>
|
| 115 |
-
)}
|
| 116 |
-
</div>
|
| 117 |
-
|
| 118 |
-
<div>
|
| 119 |
-
<label className="block text-sm font-bold text-slate-700 mb-2">كلمة المرور</label>
|
| 120 |
-
<div className="relative">
|
| 121 |
-
<div className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400">
|
| 122 |
-
<Lock size={20} />
|
| 123 |
-
</div>
|
| 124 |
-
<input
|
| 125 |
-
name="password"
|
| 126 |
-
type="password"
|
| 127 |
-
value={formik.values.password}
|
| 128 |
-
onChange={formik.handleChange}
|
| 129 |
-
onBlur={formik.handleBlur}
|
| 130 |
-
className={`w-full pl-4 pr-12 py-3.5 bg-slate-50 border ${formik.touched.password && formik.errors.password ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl outline-none focus:ring-2 font-bold text-slate-700 transition-colors focus:bg-white tracking-widest`}
|
| 131 |
-
placeholder="أدخل كلمة المرور"
|
| 132 |
-
/>
|
| 133 |
-
</div>
|
| 134 |
-
{formik.touched.password && formik.errors.password && (
|
| 135 |
-
<p className="text-rose-500 text-xs font-bold mt-1.5">{formik.errors.password}</p>
|
| 136 |
-
)}
|
| 137 |
-
</div>
|
| 138 |
-
|
| 139 |
-
<button disabled={formik.isSubmitting} type="submit" className="w-full py-4 bg-emerald-600 text-white rounded-xl font-black text-lg hover:bg-emerald-700 disabled:opacity-50 transition-all shadow-lg shadow-emerald-600/30 flex justify-center items-center gap-2 hover:-translate-y-0.5 mt-2 border border-emerald-500">
|
| 140 |
-
{formik.isSubmitting ? <LoaderCircle className="w-4 h-4 animate-spin" /> : <><LogIn size={20} /> تسجيل الدخول</>}
|
| 141 |
-
</button>
|
| 142 |
-
</form>
|
| 143 |
-
|
| 144 |
-
{!hasAdmin && (
|
| 145 |
-
<div className="p-4 bg-amber-50 text-center border-t border-amber-100 pb-5">
|
| 146 |
-
<p className="text-xs text-amber-700 font-medium mb-2">لم يتم العثور على مسؤول للمنصة. يرجى إعداد حساب أولاً.</p>
|
| 147 |
-
<button
|
| 148 |
-
onClick={() => setShowSetupModal(true)}
|
| 149 |
-
className="text-xs text-amber-600 hover:text-emerald-600 font-bold transition-colors inline-flex items-center gap-1"
|
| 150 |
-
>
|
| 151 |
-
<ShieldCheck size={14} /> إعداد مسؤول المنصة
|
| 152 |
-
</button>
|
| 153 |
-
</div>
|
| 154 |
-
)}
|
| 155 |
-
|
| 156 |
-
{hasAdmin && (
|
| 157 |
-
<div className="p-4 bg-slate-50 text-center border-t border-slate-100 pb-5">
|
| 158 |
-
<button
|
| 159 |
-
onClick={() => setShowSetupModal(true)}
|
| 160 |
-
disabled={setupLoading}
|
| 161 |
-
className="text-xs text-slate-400 hover:text-emerald-600 font-bold transition-colors"
|
| 162 |
-
>
|
| 163 |
-
إعداد حساب المسؤول
|
| 164 |
-
</button>
|
| 165 |
-
</div>
|
| 166 |
-
)}
|
| 167 |
-
</div>
|
| 168 |
-
|
| 169 |
-
{showSetupModal && (
|
| 170 |
-
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
|
| 171 |
-
<div className="bg-white rounded-2xl shadow-2xl w-full max-w-sm overflow-hidden" dir="rtl">
|
| 172 |
-
<div className="flex items-center justify-between p-6 border-b border-slate-100 bg-slate-50">
|
| 173 |
-
<div className="flex items-center gap-3">
|
| 174 |
-
<div className="w-10 h-10 bg-emerald-100 text-emerald-600 rounded-xl flex items-center justify-center">
|
| 175 |
-
<ShieldCheck size={20} />
|
| 176 |
-
</div>
|
| 177 |
-
<div>
|
| 178 |
-
<h2 className="text-lg font-black text-slate-800">إعداد المسؤول</h2>
|
| 179 |
-
<p className="text-xs text-slate-500">إنشاء حساب مسؤول المنصة</p>
|
| 180 |
-
</div>
|
| 181 |
-
</div>
|
| 182 |
-
<button
|
| 183 |
-
onClick={() => { setShowSetupModal(false); setSetupErrors({}); }}
|
| 184 |
-
className="text-slate-400 hover:text-slate-700 transition-colors"
|
| 185 |
-
>
|
| 186 |
-
<X size={20} />
|
| 187 |
-
</button>
|
| 188 |
-
</div>
|
| 189 |
-
|
| 190 |
-
<div className="p-6 space-y-4">
|
| 191 |
-
<div>
|
| 192 |
-
<label className="block text-sm font-bold text-slate-700 mb-1.5">
|
| 193 |
-
الاسم الكامل <span className="text-slate-400 font-normal">(اختياري)</span>
|
| 194 |
-
</label>
|
| 195 |
-
<input
|
| 196 |
-
type="text"
|
| 197 |
-
value={setupForm.fullName}
|
| 198 |
-
onChange={e => setSetupForm(f => ({ ...f, fullName: e.target.value }))}
|
| 199 |
-
placeholder="مسؤول المنصة"
|
| 200 |
-
className="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl outline-none focus:ring-2 focus:ring-emerald-500 font-medium text-slate-700 transition-colors focus:bg-white"
|
| 201 |
-
/>
|
| 202 |
-
</div>
|
| 203 |
-
|
| 204 |
-
<div>
|
| 205 |
-
<label className="block text-sm font-bold text-slate-700 mb-1.5">
|
| 206 |
-
اسم المستخدم <span className="text-rose-500">*</span>
|
| 207 |
-
</label>
|
| 208 |
-
<div className="relative">
|
| 209 |
-
<div className="absolute right-3.5 top-1/2 -translate-y-1/2 text-slate-400">
|
| 210 |
-
<User size={16} />
|
| 211 |
-
</div>
|
| 212 |
-
<input
|
| 213 |
-
type="text"
|
| 214 |
-
value={setupForm.username}
|
| 215 |
-
onChange={e => {
|
| 216 |
-
setSetupForm(f => ({ ...f, username: e.target.value }));
|
| 217 |
-
setSetupErrors(errs => ({ ...errs, username: '' }));
|
| 218 |
-
}}
|
| 219 |
-
placeholder="superadmin"
|
| 220 |
-
className={`w-full pr-10 pl-4 py-3 bg-slate-50 border ${setupErrors.username ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl outline-none focus:ring-2 font-bold text-slate-700 transition-colors focus:bg-white`}
|
| 221 |
-
/>
|
| 222 |
-
</div>
|
| 223 |
-
{setupErrors.username && (
|
| 224 |
-
<p className="text-rose-500 text-xs font-bold mt-1">{setupErrors.username}</p>
|
| 225 |
-
)}
|
| 226 |
-
</div>
|
| 227 |
-
|
| 228 |
-
<div>
|
| 229 |
-
<label className="block text-sm font-bold text-slate-700 mb-1.5">
|
| 230 |
-
كلمة المرور <span className="text-rose-500">*</span>
|
| 231 |
-
</label>
|
| 232 |
-
<div className="relative">
|
| 233 |
-
<div className="absolute right-3.5 top-1/2 -translate-y-1/2 text-slate-400">
|
| 234 |
-
<Lock size={16} />
|
| 235 |
-
</div>
|
| 236 |
-
<input
|
| 237 |
-
type={showSetupPassword ? 'text' : 'password'}
|
| 238 |
-
value={setupForm.password}
|
| 239 |
-
onChange={e => {
|
| 240 |
-
setSetupForm(f => ({ ...f, password: e.target.value }));
|
| 241 |
-
setSetupErrors(errs => ({ ...errs, password: '' }));
|
| 242 |
-
}}
|
| 243 |
-
placeholder="6 أحرف على الأقل"
|
| 244 |
-
className={`w-full pr-10 pl-10 py-3 bg-slate-50 border ${setupErrors.password ? 'border-rose-400 focus:ring-rose-500' : 'border-slate-200 focus:ring-emerald-500'} rounded-xl outline-none focus:ring-2 font-bold text-slate-700 transition-colors focus:bg-white`}
|
| 245 |
-
/>
|
| 246 |
-
<button
|
| 247 |
-
type="button"
|
| 248 |
-
onClick={() => setShowSetupPassword(v => !v)}
|
| 249 |
-
className="absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-700 transition-colors"
|
| 250 |
-
>
|
| 251 |
-
{showSetupPassword ? <EyeOff size={16} /> : <Eye size={16} />}
|
| 252 |
-
</button>
|
| 253 |
-
</div>
|
| 254 |
-
{setupErrors.password && (
|
| 255 |
-
<p className="text-rose-500 text-xs font-bold mt-1">{setupErrors.password}</p>
|
| 256 |
-
)}
|
| 257 |
-
</div>
|
| 258 |
-
</div>
|
| 259 |
-
|
| 260 |
-
<div className="p-6 pt-2 flex gap-3">
|
| 261 |
-
<button
|
| 262 |
-
onClick={() => { setShowSetupModal(false); setSetupErrors({}); }}
|
| 263 |
-
className="flex-1 py-3 border border-slate-200 text-slate-600 rounded-xl font-bold hover:bg-slate-50 transition-colors"
|
| 264 |
-
>
|
| 265 |
-
إلغاء
|
| 266 |
-
</button>
|
| 267 |
-
<button
|
| 268 |
-
onClick={handleSetup}
|
| 269 |
-
disabled={setupLoading}
|
| 270 |
-
className="flex-1 py-3 bg-emerald-600 text-white rounded-xl font-bold hover:bg-emerald-700 disabled:opacity-50 transition-all flex items-center justify-center gap-2 shadow-lg shadow-emerald-600/20"
|
| 271 |
-
>
|
| 272 |
-
{setupLoading
|
| 273 |
-
? <LoaderCircle size={16} className="animate-spin" />
|
| 274 |
-
: <ShieldCheck size={16} />
|
| 275 |
-
}
|
| 276 |
-
{setupLoading ? 'جاري الإنشاء...' : 'إنشاء المسؤول'}
|
| 277 |
-
</button>
|
| 278 |
-
</div>
|
| 279 |
-
</div>
|
| 280 |
-
</div>
|
| 281 |
-
)}
|
| 282 |
-
</div>
|
| 283 |
-
);
|
| 284 |
-
};
|
| 285 |
-
|
| 286 |
-
export default Login;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/pages/Login_new.jsx
DELETED
|
@@ -1,2 +0,0 @@
|
|
| 1 |
-
// test file
|
| 2 |
-
const x = 1;
|
|
|
|
|
|
|
|
|
frontend/src/routes/routes.jsx
DELETED
|
@@ -1,77 +0,0 @@
|
|
| 1 |
-
/* eslint-disable react-refresh/only-export-components */
|
| 2 |
-
import { createBrowserRouter, Navigate } from 'react-router-dom';
|
| 3 |
-
import Hero from '../pages/Hero';
|
| 4 |
-
import PrintInvoice from '../components/CustomerDetails/PrintInvoice';
|
| 5 |
-
import CustomerDetails from '../components/CustomerDetails/CustomerDetails';
|
| 6 |
-
import Login from '../pages/Login_fixed';
|
| 7 |
-
import LodderSpinner from '../common/LoaderSpinner';
|
| 8 |
-
import { useAuth } from '../Context/AuthContext';
|
| 9 |
-
|
| 10 |
-
const ProtectedRoute = ({ children }) => {
|
| 11 |
-
const { user, isBootstrapping } = useAuth();
|
| 12 |
-
|
| 13 |
-
if (isBootstrapping) {
|
| 14 |
-
return <LodderSpinner />;
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
if (!user) {
|
| 18 |
-
return <Navigate to="/login" replace />;
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
return children;
|
| 22 |
-
};
|
| 23 |
-
|
| 24 |
-
const PublicOnlyRoute = ({ children }) => {
|
| 25 |
-
const { user, isBootstrapping } = useAuth();
|
| 26 |
-
|
| 27 |
-
if (isBootstrapping) {
|
| 28 |
-
return <LodderSpinner />;
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
if (user) {
|
| 32 |
-
return <Navigate to="/" replace />;
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
return children;
|
| 36 |
-
};
|
| 37 |
-
|
| 38 |
-
export const routes = createBrowserRouter([
|
| 39 |
-
{
|
| 40 |
-
path: '/login',
|
| 41 |
-
element: (
|
| 42 |
-
<PublicOnlyRoute>
|
| 43 |
-
<Login />
|
| 44 |
-
</PublicOnlyRoute>
|
| 45 |
-
),
|
| 46 |
-
},
|
| 47 |
-
{
|
| 48 |
-
path: '/',
|
| 49 |
-
children: [
|
| 50 |
-
{
|
| 51 |
-
path: '/',
|
| 52 |
-
element: (
|
| 53 |
-
<ProtectedRoute>
|
| 54 |
-
<Hero />
|
| 55 |
-
</ProtectedRoute>
|
| 56 |
-
),
|
| 57 |
-
},
|
| 58 |
-
{
|
| 59 |
-
path: '/customers/:id',
|
| 60 |
-
element: (
|
| 61 |
-
<ProtectedRoute>
|
| 62 |
-
<CustomerDetails />
|
| 63 |
-
</ProtectedRoute>
|
| 64 |
-
),
|
| 65 |
-
},
|
| 66 |
-
{
|
| 67 |
-
path: '/print/invoice/:customerId/:invoiceId',
|
| 68 |
-
element: (
|
| 69 |
-
<ProtectedRoute>
|
| 70 |
-
<PrintInvoice />
|
| 71 |
-
</ProtectedRoute>
|
| 72 |
-
),
|
| 73 |
-
},
|
| 74 |
-
],
|
| 75 |
-
},
|
| 76 |
-
]);
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/utils/exports.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
| 1 |
-
const escapeCsvValue = (value) => {
|
| 2 |
-
const stringValue = `${value ?? ""}`;
|
| 3 |
-
if (/[",\n]/.test(stringValue)) {
|
| 4 |
-
return `"${stringValue.replace(/"/g, '""')}"`;
|
| 5 |
-
}
|
| 6 |
-
|
| 7 |
-
return stringValue;
|
| 8 |
-
};
|
| 9 |
-
|
| 10 |
-
export const downloadCsv = (filename, columns, rows) => {
|
| 11 |
-
const content = [
|
| 12 |
-
columns.map((column) => escapeCsvValue(column.label)).join(","),
|
| 13 |
-
...rows.map((row) => columns.map((column) => escapeCsvValue(row[column.key])).join(",")),
|
| 14 |
-
].join("\n");
|
| 15 |
-
|
| 16 |
-
const blob = new Blob([content], { type: "text/csv;charset=utf-8;" });
|
| 17 |
-
const link = document.createElement("a");
|
| 18 |
-
link.href = URL.createObjectURL(blob);
|
| 19 |
-
link.setAttribute("download", filename);
|
| 20 |
-
document.body.appendChild(link);
|
| 21 |
-
link.click();
|
| 22 |
-
document.body.removeChild(link);
|
| 23 |
-
};
|
| 24 |
-
|
| 25 |
-
export const printHtmlDocument = (title, html) => {
|
| 26 |
-
const printWindow = window.open("", "_blank", "width=1280,height=900");
|
| 27 |
-
if (!printWindow) {
|
| 28 |
-
return;
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
printWindow.document.write(`
|
| 32 |
-
<html>
|
| 33 |
-
<head>
|
| 34 |
-
<title>${title}</title>
|
| 35 |
-
<style>
|
| 36 |
-
body { font-family: Arial, sans-serif; padding: 24px; color: #0f172a; }
|
| 37 |
-
h1, h2, h3 { margin: 0 0 12px; }
|
| 38 |
-
.section { margin-bottom: 24px; }
|
| 39 |
-
table { width: 100%; border-collapse: collapse; margin-top: 12px; }
|
| 40 |
-
th, td { border: 1px solid #cbd5e1; padding: 10px; text-align: left; }
|
| 41 |
-
th { background: #f8fafc; }
|
| 42 |
-
.kpi-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
|
| 43 |
-
.kpi-card { border: 1px solid #cbd5e1; border-radius: 12px; padding: 16px; }
|
| 44 |
-
@media print {
|
| 45 |
-
body { padding: 0; }
|
| 46 |
-
}
|
| 47 |
-
</style>
|
| 48 |
-
</head>
|
| 49 |
-
<body>${html}</body>
|
| 50 |
-
</html>
|
| 51 |
-
`);
|
| 52 |
-
printWindow.document.close();
|
| 53 |
-
printWindow.focus();
|
| 54 |
-
printWindow.print();
|
| 55 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/utils/formatters.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
| 1 |
-
export const formatCurrency = (value, currency = "EGP") =>
|
| 2 |
-
new Intl.NumberFormat("en-US", {
|
| 3 |
-
style: "currency",
|
| 4 |
-
currency,
|
| 5 |
-
maximumFractionDigits: 2,
|
| 6 |
-
}).format(Number(value || 0));
|
| 7 |
-
|
| 8 |
-
export const formatDate = (value) =>
|
| 9 |
-
new Date(value).toLocaleDateString("en-US", {
|
| 10 |
-
year: "numeric",
|
| 11 |
-
month: "short",
|
| 12 |
-
day: "numeric",
|
| 13 |
-
});
|
| 14 |
-
|
| 15 |
-
export const formatDateTime = (value) =>
|
| 16 |
-
new Date(value).toLocaleString("en-US", {
|
| 17 |
-
year: "numeric",
|
| 18 |
-
month: "short",
|
| 19 |
-
day: "numeric",
|
| 20 |
-
hour: "2-digit",
|
| 21 |
-
minute: "2-digit",
|
| 22 |
-
});
|
| 23 |
-
|
| 24 |
-
export const formatNumber = (value) => new Intl.NumberFormat("en-US").format(Number(value || 0));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/utils/permissions.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
| 1 |
-
export const getPermissions = (user) => {
|
| 2 |
-
const role = user?.role;
|
| 3 |
-
const isTenantUser = Boolean(user?.tenant);
|
| 4 |
-
const canManageBusiness = ["owner", "admin"].includes(role);
|
| 5 |
-
|
| 6 |
-
return {
|
| 7 |
-
role,
|
| 8 |
-
isTenantUser,
|
| 9 |
-
isSuperAdmin: role === "super_admin",
|
| 10 |
-
isOwner: role === "owner",
|
| 11 |
-
isAdmin: role === "admin",
|
| 12 |
-
isCashier: role === "cashier",
|
| 13 |
-
canManageBusiness,
|
| 14 |
-
canManageCustomers: isTenantUser,
|
| 15 |
-
canCreateCustomers: isTenantUser,
|
| 16 |
-
canCreateInvoices: isTenantUser,
|
| 17 |
-
canRecordPayments: isTenantUser,
|
| 18 |
-
canEditInvoice: canManageBusiness,
|
| 19 |
-
canDeleteCustomers: canManageBusiness,
|
| 20 |
-
canViewInventory: isTenantUser,
|
| 21 |
-
canManageInventory: canManageBusiness,
|
| 22 |
-
canViewSuppliers: canManageBusiness,
|
| 23 |
-
canManageSuppliers: canManageBusiness,
|
| 24 |
-
canViewReports: canManageBusiness,
|
| 25 |
-
canManageUsers: canManageBusiness,
|
| 26 |
-
canManageCompany: canManageBusiness,
|
| 27 |
-
canManageTenants: role === "super_admin",
|
| 28 |
-
};
|
| 29 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/src/utils/whatsapp.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
| 1 |
-
export const openWhatsAppMessage = (message) => {
|
| 2 |
-
const url = `https://wa.me/?text=${encodeURIComponent(message)}`;
|
| 3 |
-
window.open(url, "_blank", "noopener,noreferrer");
|
| 4 |
-
};
|
| 5 |
-
|
| 6 |
-
export const buildCustomerInvoiceMessage = ({ companyName, customerName, invoice }) => {
|
| 7 |
-
const lines = [
|
| 8 |
-
`تحديث فاتورة ${companyName}`,
|
| 9 |
-
`العميل: ${customerName}`,
|
| 10 |
-
`الفاتورة: ${invoice.invoiceNumber || invoice._id}`,
|
| 11 |
-
`الحالة: ${invoice.status}`,
|
| 12 |
-
`الإجمالي: ${Number(invoice.totalAmount || 0).toFixed(2)} ج.م`,
|
| 13 |
-
`المدفوع: ${Number(invoice.paidAmount || 0).toFixed(2)} ج.م`,
|
| 14 |
-
`المتبقي: ${Number(invoice.remainingAmount || 0).toFixed(2)} ج.م`,
|
| 15 |
-
"",
|
| 16 |
-
"البنود:",
|
| 17 |
-
...(invoice.items || []).map(
|
| 18 |
-
(item) => `- ${item.name}: ${item.quantity} × ${Number(item.price || 0).toFixed(2)} = ${Number(item.total || 0).toFixed(2)} ج.م`
|
| 19 |
-
),
|
| 20 |
-
];
|
| 21 |
-
|
| 22 |
-
return lines.join("\n");
|
| 23 |
-
};
|
| 24 |
-
|
| 25 |
-
export const buildLowStockMessage = ({ companyName, items }) => {
|
| 26 |
-
const lines = [
|
| 27 |
-
`تنبيه مخزون منخفض - ${companyName}`,
|
| 28 |
-
"",
|
| 29 |
-
...items.map(
|
| 30 |
-
(item) =>
|
| 31 |
-
`- ${item.name}: متبقي ${item.stock} (الحد الأدنى ${item.lowStockThreshold})`
|
| 32 |
-
),
|
| 33 |
-
];
|
| 34 |
-
|
| 35 |
-
return lines.join("\n");
|
| 36 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend/vite.config.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
import tailwindcss from "@tailwindcss/vite";
|
| 2 |
-
import react from "@vitejs/plugin-react";
|
| 3 |
-
import { defineConfig } from "vite";
|
| 4 |
-
|
| 5 |
-
// https://vite.dev/config/
|
| 6 |
-
export default defineConfig({
|
| 7 |
-
plugins: [react(), tailwindcss()],
|
| 8 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|