added quantity used
This commit is contained in:
parent
d2c44f8386
commit
a036e33fd4
10 changed files with 189 additions and 32 deletions
|
@ -3,6 +3,7 @@ import { useState } from 'react';
|
|||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Checkbox } from './ui/checkbox';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import {
|
||||
Dialog,
|
||||
|
@ -28,6 +29,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
|
|||
const [name, setName] = useState('');
|
||||
const [description, setDescription] = useState('');
|
||||
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 { user } = useAuth();
|
||||
|
@ -51,7 +54,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
|
|||
const newItem = await inventoryService.createItem({
|
||||
name: name.trim(),
|
||||
description: description.trim(),
|
||||
quantity: 1, // Default quantity set to 1
|
||||
quantity: qt, // Default quantity set to 1
|
||||
used: used,
|
||||
tagIds: selectedTagIds
|
||||
});
|
||||
|
||||
|
@ -119,8 +123,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
|
|||
key={tag._id}
|
||||
variant="outline"
|
||||
className={`cursor-pointer transition-all ${selectedTagIds.includes(tag._id)
|
||||
? 'bg-blue-100 border-blue-300'
|
||||
: 'hover:bg-gray-100'
|
||||
? 'bg-blue-100 border-blue-300'
|
||||
: 'hover:bg-gray-100'
|
||||
}`}
|
||||
onClick={() => toggleTag(tag._id)}
|
||||
>
|
||||
|
@ -136,6 +140,34 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
|
|||
Clicca sui tag per selezionarli
|
||||
</p>
|
||||
</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>
|
||||
<Button
|
||||
|
|
|
@ -120,7 +120,7 @@ export const EditItemDialog = ({
|
|||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
{/* <div className="space-y-2">
|
||||
<Label htmlFor="quantity">Quantità</Label>
|
||||
<Input
|
||||
id="quantity"
|
||||
|
@ -130,7 +130,7 @@ export const EditItemDialog = ({
|
|||
onChange={(e) => setFormData(prev => ({ ...prev, quantity: parseInt(e.target.value) || 0 }))}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</div> */}
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Tag</Label>
|
||||
|
|
|
@ -13,6 +13,7 @@ import { ManageTagsDialog } from '@/components/ManageTagsDialog';
|
|||
import { DeleteItemDialog } from '@/components/DeleteItemDialog';
|
||||
import { EditItemDialog } from '@/components/EditItemDialog';
|
||||
import { QuantityControls } from '@/components/QuantityControls';
|
||||
import { QuantityUsedControls } from './QuantityUsedControls';
|
||||
import { useToast } from '@/hooks/use-toast';
|
||||
|
||||
export const InventoryDashboard = () => {
|
||||
|
@ -304,11 +305,12 @@ export const InventoryDashboard = () => {
|
|||
<TableRow className="bg-slate-50">
|
||||
<TableHead className="font-semibold">Nome</TableHead>
|
||||
<TableHead className="font-semibold">Descrizione</TableHead>
|
||||
<TableHead className="font-semibold">Quantità</TableHead>
|
||||
<TableHead className="font-semibold">Tag</TableHead>
|
||||
<TableHead className="font-semibold">Data Aggiunta</TableHead>
|
||||
<TableHead className="font-semibold">Aggiunto da</TableHead>
|
||||
<TableHead className="font-semibold">Azioni</TableHead>
|
||||
<TableHead className="font-semibold w-[12%]">Quantità</TableHead>
|
||||
<TableHead className="font-semibold w-[12%]">Utilizzato</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>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
|
@ -326,12 +328,6 @@ export const InventoryDashboard = () => {
|
|||
<TableRow key={item._id} className="hover:bg-slate-50 transition-colors">
|
||||
<TableCell className="font-medium">{item.name}</TableCell>
|
||||
<TableCell className="text-gray-600">{item.description}</TableCell>
|
||||
<TableCell>
|
||||
<QuantityControls
|
||||
item={item}
|
||||
onQuantityUpdate={handleQuantityUpdate}
|
||||
/>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{item.tags.map(tag => (
|
||||
|
@ -348,8 +344,20 @@ export const InventoryDashboard = () => {
|
|||
))}
|
||||
</div>
|
||||
</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">{item.addedBy}</TableCell>
|
||||
{/* <TableCell className="text-gray-600">{item.addedBy}</TableCell> */}
|
||||
<TableCell>
|
||||
<div className="flex gap-1">
|
||||
<Button
|
||||
|
|
|
@ -43,7 +43,7 @@ export const QuantityControls = ({ item, onQuantityUpdate }: QuantityControlsPro
|
|||
variant="outline"
|
||||
size="sm"
|
||||
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"
|
||||
>
|
||||
<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();
|
||||
},
|
||||
|
||||
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> {
|
||||
const res = await fetch(`${API_URL}/items/${itemId}`, {
|
||||
method: 'DELETE',
|
||||
|
|
|
@ -10,6 +10,7 @@ export interface InventoryItem {
|
|||
name: string;
|
||||
description: string;
|
||||
quantity: number;
|
||||
used: number;
|
||||
tags: Tag[];
|
||||
dateAdded: string;
|
||||
addedBy: string;
|
||||
|
@ -19,6 +20,7 @@ export interface CreateItemData {
|
|||
name: string;
|
||||
description: string;
|
||||
quantity: number;
|
||||
used: number;
|
||||
tagIds: string[];
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue