added quantity used
This commit is contained in:
parent
d2c44f8386
commit
a036e33fd4
10 changed files with 189 additions and 32 deletions
|
@ -6,15 +6,17 @@ export interface IItem extends Document {
|
||||||
description: string;
|
description: string;
|
||||||
tags: Types.Array<ITag['_id']>;
|
tags: Types.Array<ITag['_id']>;
|
||||||
quantity: number; // ← nuovo
|
quantity: number; // ← nuovo
|
||||||
|
used: number; // ← nuovo, opzionale
|
||||||
dateAdded: Date;
|
dateAdded: Date;
|
||||||
addedBy: string;
|
addedBy: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const itemSchema = new Schema<IItem>({
|
const itemSchema = new Schema<IItem>({
|
||||||
name: { type: String, required: true },
|
name: { type: String, required: true },
|
||||||
description: { type: String, required: true },
|
description: { type: String, required: false },
|
||||||
tags: [{ type: Schema.Types.ObjectId, ref: 'Tag' }],
|
tags: [{ type: Schema.Types.ObjectId, ref: 'Tag' }],
|
||||||
quantity: { type: Number, default: 0, min: 0 }, // default 0, mai negativo
|
quantity: { type: Number, default: 0, min: 0 }, // default 0, mai negativo
|
||||||
|
used: { type: Number, default: 0 }, // opzionale, default false
|
||||||
dateAdded: { type: Date, default: Date.now },
|
dateAdded: { type: Date, default: Date.now },
|
||||||
addedBy: { type: String },
|
addedBy: { type: String },
|
||||||
});
|
});
|
||||||
|
|
|
@ -31,11 +31,12 @@ router.get('/search', async (req, res) => {
|
||||||
|
|
||||||
/* ──────────── POST: crea item ──────────── */
|
/* ──────────── POST: crea item ──────────── */
|
||||||
router.post('/', auth, async (req, res) => {
|
router.post('/', auth, async (req, res) => {
|
||||||
const { name, description, tagIds, quantity = 0 } = req.body as {
|
const { name, description, tagIds, quantity = 0, used = false } = req.body as {
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
tagIds: string[];
|
tagIds: string[];
|
||||||
quantity?: number;
|
quantity?: number;
|
||||||
|
used?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
const tags = await Tag.find({ _id: { $in: tagIds } });
|
const tags = await Tag.find({ _id: { $in: tagIds } });
|
||||||
|
@ -44,6 +45,7 @@ router.post('/', auth, async (req, res) => {
|
||||||
description,
|
description,
|
||||||
tags,
|
tags,
|
||||||
quantity,
|
quantity,
|
||||||
|
used,
|
||||||
addedBy: req.user!.email,
|
addedBy: req.user!.email,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -84,17 +86,46 @@ router.patch('/:id/quantity', auth, async (req, res) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const item = await Item.findByIdAndUpdate(
|
const item = await Item.findById(id);
|
||||||
id,
|
|
||||||
{ quantity },
|
|
||||||
{ new: true }
|
|
||||||
).populate('tags');
|
|
||||||
|
|
||||||
if (!item) {
|
if (!item) {
|
||||||
res.status(404).json({ message: 'Item non trovato' });
|
res.status(404).json({ message: 'Item non trovato' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.json(item);
|
|
||||||
|
if (item.used > quantity) {
|
||||||
|
res.status(400).json({ message: 'La quantità non può essere inferiore alla quantità utilizzata' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
item.quantity = quantity;
|
||||||
|
await item.save();
|
||||||
|
res.json(await item.populate('tags'));
|
||||||
|
});
|
||||||
|
|
||||||
|
/* ──────────── PATCH: modifica solo la quantità utilizzata ──────────── */
|
||||||
|
router.patch('/:id/usedquantity', auth, async (req, res) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
const { quantity } = req.body as { quantity: number };
|
||||||
|
|
||||||
|
if (quantity < 0) {
|
||||||
|
res.status(400).json({ message: 'La quantità utilizzata non può essere negativa' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).json({ message: 'Item non trovato' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (quantity > item.quantity) {
|
||||||
|
res.status(400).json({ message: 'La quantità utilizzata non può essere maggiore della quantità totale' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
item.used = quantity;
|
||||||
|
await item.save();
|
||||||
|
res.json(await item.populate('tags'));
|
||||||
});
|
});
|
||||||
|
|
||||||
/* ──────────── DELETE: elimina item ──────────── */
|
/* ──────────── DELETE: elimina item ──────────── */
|
||||||
|
|
|
@ -25,7 +25,7 @@ services:
|
||||||
depends_on:
|
depends_on:
|
||||||
- backend
|
- backend
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "2222:80"
|
||||||
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { useState } from 'react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import { Checkbox } from './ui/checkbox';
|
||||||
import { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
|
@ -28,6 +29,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState('');
|
||||||
const [description, setDescription] = useState('');
|
const [description, setDescription] = useState('');
|
||||||
const [selectedTagIds, setSelectedTagIds] = useState<string[]>([]);
|
const [selectedTagIds, setSelectedTagIds] = useState<string[]>([]);
|
||||||
|
const [qt, setQt] = useState(1); // Default quantity set to 1
|
||||||
|
const [used, setUsed] = useState(0); // Assuming this is for used tags, not implemented in the original code
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
const { user } = useAuth();
|
const { user } = useAuth();
|
||||||
|
@ -51,7 +54,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
|
||||||
const newItem = await inventoryService.createItem({
|
const newItem = await inventoryService.createItem({
|
||||||
name: name.trim(),
|
name: name.trim(),
|
||||||
description: description.trim(),
|
description: description.trim(),
|
||||||
quantity: 1, // Default quantity set to 1
|
quantity: qt, // Default quantity set to 1
|
||||||
|
used: used,
|
||||||
tagIds: selectedTagIds
|
tagIds: selectedTagIds
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -136,6 +140,34 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
|
||||||
Clicca sui tag per selezionarli
|
Clicca sui tag per selezionarli
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label className="text-sm font-medium text-gray-700">Quantità *</label>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
value={qt} // Default quantity is 1
|
||||||
|
onChange={(e) => {
|
||||||
|
const quantity = Math.max(1, parseInt(e.target.value, 10) || 1);
|
||||||
|
setQt(quantity);
|
||||||
|
setUsed(Math.min(used, quantity)); // Ensure used does not exceed quantity
|
||||||
|
}}
|
||||||
|
placeholder="Quantità totale"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label className="text-sm font-medium text-gray-700">Quantità Usata</label>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
value={used}
|
||||||
|
onChange={(e) => {
|
||||||
|
const usedQuantity = Math.max(0, parseInt(e.target.value, 10) || 0);
|
||||||
|
setUsed(usedQuantity);
|
||||||
|
setQt(Math.max(qt, usedQuantity)); // Ensure quantity is at least as much as used
|
||||||
|
}}
|
||||||
|
placeholder="Quantità usata"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button
|
<Button
|
||||||
|
|
|
@ -120,7 +120,7 @@ export const EditItemDialog = ({
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-2">
|
{/* <div className="space-y-2">
|
||||||
<Label htmlFor="quantity">Quantità</Label>
|
<Label htmlFor="quantity">Quantità</Label>
|
||||||
<Input
|
<Input
|
||||||
id="quantity"
|
id="quantity"
|
||||||
|
@ -130,7 +130,7 @@ export const EditItemDialog = ({
|
||||||
onChange={(e) => setFormData(prev => ({ ...prev, quantity: parseInt(e.target.value) || 0 }))}
|
onChange={(e) => setFormData(prev => ({ ...prev, quantity: parseInt(e.target.value) || 0 }))}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div> */}
|
||||||
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Tag</Label>
|
<Label>Tag</Label>
|
||||||
|
|
|
@ -13,6 +13,7 @@ import { ManageTagsDialog } from '@/components/ManageTagsDialog';
|
||||||
import { DeleteItemDialog } from '@/components/DeleteItemDialog';
|
import { DeleteItemDialog } from '@/components/DeleteItemDialog';
|
||||||
import { EditItemDialog } from '@/components/EditItemDialog';
|
import { EditItemDialog } from '@/components/EditItemDialog';
|
||||||
import { QuantityControls } from '@/components/QuantityControls';
|
import { QuantityControls } from '@/components/QuantityControls';
|
||||||
|
import { QuantityUsedControls } from './QuantityUsedControls';
|
||||||
import { useToast } from '@/hooks/use-toast';
|
import { useToast } from '@/hooks/use-toast';
|
||||||
|
|
||||||
export const InventoryDashboard = () => {
|
export const InventoryDashboard = () => {
|
||||||
|
@ -304,11 +305,12 @@ export const InventoryDashboard = () => {
|
||||||
<TableRow className="bg-slate-50">
|
<TableRow className="bg-slate-50">
|
||||||
<TableHead className="font-semibold">Nome</TableHead>
|
<TableHead className="font-semibold">Nome</TableHead>
|
||||||
<TableHead className="font-semibold">Descrizione</TableHead>
|
<TableHead className="font-semibold">Descrizione</TableHead>
|
||||||
<TableHead className="font-semibold">Quantità</TableHead>
|
|
||||||
<TableHead className="font-semibold">Tag</TableHead>
|
<TableHead className="font-semibold">Tag</TableHead>
|
||||||
<TableHead className="font-semibold">Data Aggiunta</TableHead>
|
<TableHead className="font-semibold w-[12%]">Quantità</TableHead>
|
||||||
<TableHead className="font-semibold">Aggiunto da</TableHead>
|
<TableHead className="font-semibold w-[12%]">Utilizzato</TableHead>
|
||||||
<TableHead className="font-semibold">Azioni</TableHead>
|
<TableHead className="font-semibold w-[12%]">Data Aggiunta</TableHead>
|
||||||
|
{/* <TableHead className="font-semibold">Aggiunto da</TableHead> */}
|
||||||
|
<TableHead className="font-semibold w-[10%]">Azioni</TableHead>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
|
@ -326,12 +328,6 @@ export const InventoryDashboard = () => {
|
||||||
<TableRow key={item._id} className="hover:bg-slate-50 transition-colors">
|
<TableRow key={item._id} className="hover:bg-slate-50 transition-colors">
|
||||||
<TableCell className="font-medium">{item.name}</TableCell>
|
<TableCell className="font-medium">{item.name}</TableCell>
|
||||||
<TableCell className="text-gray-600">{item.description}</TableCell>
|
<TableCell className="text-gray-600">{item.description}</TableCell>
|
||||||
<TableCell>
|
|
||||||
<QuantityControls
|
|
||||||
item={item}
|
|
||||||
onQuantityUpdate={handleQuantityUpdate}
|
|
||||||
/>
|
|
||||||
</TableCell>
|
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<div className="flex flex-wrap gap-1">
|
<div className="flex flex-wrap gap-1">
|
||||||
{item.tags.map(tag => (
|
{item.tags.map(tag => (
|
||||||
|
@ -348,8 +344,20 @@ export const InventoryDashboard = () => {
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<QuantityControls
|
||||||
|
item={item}
|
||||||
|
onQuantityUpdate={handleQuantityUpdate}
|
||||||
|
/>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<QuantityUsedControls
|
||||||
|
item={item}
|
||||||
|
onQuantityUpdate={handleQuantityUpdate}
|
||||||
|
/>
|
||||||
|
</TableCell>
|
||||||
<TableCell className="text-gray-600">{formatDate(item.dateAdded)}</TableCell>
|
<TableCell className="text-gray-600">{formatDate(item.dateAdded)}</TableCell>
|
||||||
<TableCell className="text-gray-600">{item.addedBy}</TableCell>
|
{/* <TableCell className="text-gray-600">{item.addedBy}</TableCell> */}
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<div className="flex gap-1">
|
<div className="flex gap-1">
|
||||||
<Button
|
<Button
|
||||||
|
|
|
@ -43,7 +43,7 @@ export const QuantityControls = ({ item, onQuantityUpdate }: QuantityControlsPro
|
||||||
variant="outline"
|
variant="outline"
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={() => updateQuantity(item.quantity - 1)}
|
onClick={() => updateQuantity(item.quantity - 1)}
|
||||||
disabled={isUpdating || item.quantity <= 0}
|
disabled={isUpdating || item.quantity <= 0 || item.used >= item.quantity}
|
||||||
className="h-8 w-8 p-0"
|
className="h-8 w-8 p-0"
|
||||||
>
|
>
|
||||||
<Minus className="w-3 h-3" />
|
<Minus className="w-3 h-3" />
|
||||||
|
|
65
frontend/src/components/QuantityUsedControls.tsx
Normal file
65
frontend/src/components/QuantityUsedControls.tsx
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Plus, Minus } from 'lucide-react';
|
||||||
|
import { InventoryItem } from '@/types/inventory';
|
||||||
|
import { inventoryService } from '@/services/inventoryService';
|
||||||
|
import { useToast } from '@/hooks/use-toast';
|
||||||
|
|
||||||
|
interface QuantityControlsProps {
|
||||||
|
item: InventoryItem;
|
||||||
|
onQuantityUpdate: (updatedItem: InventoryItem) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const QuantityUsedControls = ({ item, onQuantityUpdate }: QuantityControlsProps) => {
|
||||||
|
const [isUpdating, setIsUpdating] = useState(false);
|
||||||
|
const { toast } = useToast();
|
||||||
|
|
||||||
|
const updateQuantity = async (newQuantity: number) => {
|
||||||
|
if (newQuantity < 0) return;
|
||||||
|
|
||||||
|
setIsUpdating(true);
|
||||||
|
try {
|
||||||
|
const updatedItem = await inventoryService.updateUsedItemQuantity(item._id, newQuantity);
|
||||||
|
onQuantityUpdate(updatedItem);
|
||||||
|
toast({
|
||||||
|
title: "Quantità aggiornata",
|
||||||
|
description: `${item.name}: ${newQuantity} pezzi`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
toast({
|
||||||
|
title: "Errore",
|
||||||
|
description: "Impossibile aggiornare la quantità",
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setIsUpdating(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center space-x-2">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => updateQuantity(item.used - 1)}
|
||||||
|
disabled={isUpdating || item.used <= 0}
|
||||||
|
className="h-8 w-8 p-0"
|
||||||
|
>
|
||||||
|
<Minus className="w-3 h-3" />
|
||||||
|
</Button>
|
||||||
|
<span className="text-sm font-medium min-w-[2rem] text-center">
|
||||||
|
{item.used}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => updateQuantity(item.used + 1)}
|
||||||
|
disabled={isUpdating || item.used >= item.quantity}
|
||||||
|
className="h-8 w-8 p-0"
|
||||||
|
>
|
||||||
|
<Plus className="w-3 h-3" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
|
@ -80,6 +80,23 @@ export const inventoryService = {
|
||||||
return res.json();
|
return res.json();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async updateUsedItemQuantity(itemId: string, quantity: number): Promise<InventoryItem> {
|
||||||
|
const res = await fetch(`${API_URL}/items/${itemId}/usedquantity`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...getAuthHeader(),
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ quantity }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const msg = (await res.json())?.message ?? 'Errore aggiornamento quantità usata';
|
||||||
|
throw new Error(msg);
|
||||||
|
}
|
||||||
|
return res.json();
|
||||||
|
},
|
||||||
|
|
||||||
async deleteItem(itemId: string): Promise<void> {
|
async deleteItem(itemId: string): Promise<void> {
|
||||||
const res = await fetch(`${API_URL}/items/${itemId}`, {
|
const res = await fetch(`${API_URL}/items/${itemId}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
|
|
|
@ -10,6 +10,7 @@ export interface InventoryItem {
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
|
used: number;
|
||||||
tags: Tag[];
|
tags: Tag[];
|
||||||
dateAdded: string;
|
dateAdded: string;
|
||||||
addedBy: string;
|
addedBy: string;
|
||||||
|
@ -19,6 +20,7 @@ export interface CreateItemData {
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
|
used: number;
|
||||||
tagIds: string[];
|
tagIds: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue