Add batch expenditure assignment
- Implemented batch assignment for expenditures, similar to income. - Added a button to export missing materials. - Updated logic to consider jobs in "acquisition" status. - Added logic to skip jobs with satisfied BOM.
This commit is contained in:
340
src/components/BatchExpenditureForm.tsx
Normal file
340
src/components/BatchExpenditureForm.tsx
Normal file
@@ -0,0 +1,340 @@
|
|||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||||
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||||
|
import { parseTransactionLine, formatISK, PastedTransaction } from '@/utils/priceUtils';
|
||||||
|
import { IndTransactionRecordNoId, IndJobStatusOptions } from '@/lib/pbtypes';
|
||||||
|
import { IndJob } from '@/lib/types';
|
||||||
|
import { X } from 'lucide-react';
|
||||||
|
|
||||||
|
interface BatchExpenditureFormProps {
|
||||||
|
onClose: () => void;
|
||||||
|
onTransactionsAssigned: (assignments: { jobId: string, transactions: IndTransactionRecordNoId[] }[]) => void;
|
||||||
|
jobs: IndJob[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TransactionGroup {
|
||||||
|
itemName: string;
|
||||||
|
transactions: PastedTransaction[];
|
||||||
|
totalQuantity: number;
|
||||||
|
totalValue: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const BatchExpenditureForm: React.FC<BatchExpenditureFormProps> = ({ onClose, onTransactionsAssigned, jobs }) => {
|
||||||
|
const [pastedData, setPastedData] = useState('');
|
||||||
|
const [transactionGroups, setTransactionGroups] = useState<TransactionGroup[]>([]);
|
||||||
|
const [duplicatesFound, setDuplicatesFound] = useState(0);
|
||||||
|
|
||||||
|
// Filter jobs that are in acquisition status
|
||||||
|
const eligibleJobs = jobs.filter(job => job.status === IndJobStatusOptions.Acquisition);
|
||||||
|
|
||||||
|
const findMatchingJob = (itemName: string): string | undefined => {
|
||||||
|
// Find jobs where the item is in the bill of materials and not satisfied
|
||||||
|
for (const job of eligibleJobs) {
|
||||||
|
const billItem = job.billOfMaterials?.find(item =>
|
||||||
|
item.name.toLowerCase() === itemName.toLowerCase()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (billItem) {
|
||||||
|
// Check if this material is already satisfied
|
||||||
|
const ownedQuantity = job.expenditures?.reduce((total, exp) =>
|
||||||
|
exp.itemName.toLowerCase() === itemName.toLowerCase() ? total + exp.quantity : total, 0
|
||||||
|
) || 0;
|
||||||
|
|
||||||
|
// Only return this job if we still need more of this material
|
||||||
|
if (ownedQuantity < billItem.quantity) {
|
||||||
|
return job.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
const normalizeDate = (dateStr: string): string => {
|
||||||
|
return dateStr.replace('T', ' ');
|
||||||
|
};
|
||||||
|
|
||||||
|
const createTransactionKey = (parsed: PastedTransaction): string => {
|
||||||
|
if (!parsed) return '';
|
||||||
|
const key = [
|
||||||
|
normalizeDate(parsed.date.toString()),
|
||||||
|
parsed.itemName,
|
||||||
|
parsed.quantity.toString(),
|
||||||
|
Math.abs(parsed.totalPrice).toString(), // Use absolute value for expenditures
|
||||||
|
parsed.buyer,
|
||||||
|
parsed.location
|
||||||
|
].join('|');
|
||||||
|
return key;
|
||||||
|
};
|
||||||
|
|
||||||
|
const createTransactionKeyFromRecord = (tx: IndTransactionRecordNoId): string => {
|
||||||
|
const key = [
|
||||||
|
normalizeDate(tx.date),
|
||||||
|
tx.itemName,
|
||||||
|
tx.quantity.toString(),
|
||||||
|
Math.abs(tx.totalPrice).toString(), // Use absolute value for expenditures
|
||||||
|
tx.buyer,
|
||||||
|
tx.location
|
||||||
|
].join('|');
|
||||||
|
return key;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePaste = (value: string) => {
|
||||||
|
setPastedData(value);
|
||||||
|
const lines = value.trim().split('\n');
|
||||||
|
const pasteTransactionMap = new Map<string, PastedTransaction>();
|
||||||
|
|
||||||
|
// STEP 1: Combine identical transactions within the pasted data
|
||||||
|
lines.forEach((line) => {
|
||||||
|
const parsed: PastedTransaction | null = parseTransactionLine(line);
|
||||||
|
if (parsed && parsed.totalPrice < 0) { // Only process expenditures (negative amounts)
|
||||||
|
// Convert to positive values for expenditures
|
||||||
|
parsed.totalPrice = Math.abs(parsed.totalPrice);
|
||||||
|
parsed.unitPrice = Math.abs(parsed.unitPrice);
|
||||||
|
|
||||||
|
const transactionKey: string = createTransactionKey(parsed);
|
||||||
|
|
||||||
|
if (pasteTransactionMap.has(transactionKey)) {
|
||||||
|
const existing = pasteTransactionMap.get(transactionKey)!;
|
||||||
|
existing.quantity += parsed.quantity;
|
||||||
|
existing.totalPrice += parsed.totalPrice;
|
||||||
|
const newKey = createTransactionKey(existing);
|
||||||
|
pasteTransactionMap.set(newKey, existing);
|
||||||
|
pasteTransactionMap.delete(transactionKey);
|
||||||
|
} else {
|
||||||
|
pasteTransactionMap.set(transactionKey, parsed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// STEP 2: Identify which jobs these transactions belong to
|
||||||
|
const relevantJobIds = new Set<string>();
|
||||||
|
pasteTransactionMap.forEach((transaction) => {
|
||||||
|
const matchingJobId = findMatchingJob(transaction.itemName);
|
||||||
|
if (matchingJobId) {
|
||||||
|
relevantJobIds.add(matchingJobId);
|
||||||
|
transaction.assignedJobId = matchingJobId;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// STEP 3: Check against existing expenditures from relevant jobs
|
||||||
|
const existingTransactionKeys = new Set<string>();
|
||||||
|
eligibleJobs.forEach(job => {
|
||||||
|
if (relevantJobIds.has(job.id)) {
|
||||||
|
job.expenditures?.forEach(tx => {
|
||||||
|
const key = createTransactionKeyFromRecord(tx);
|
||||||
|
existingTransactionKeys.add(key);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// STEP 4: Mark duplicates and assign jobs
|
||||||
|
let duplicates = 0;
|
||||||
|
pasteTransactionMap.forEach((transaction, key) => {
|
||||||
|
const isDuplicate = existingTransactionKeys.has(key);
|
||||||
|
transaction.isDuplicate = isDuplicate;
|
||||||
|
|
||||||
|
if (isDuplicate) {
|
||||||
|
duplicates++;
|
||||||
|
transaction.assignedJobId = undefined;
|
||||||
|
} else if (!transaction.assignedJobId) {
|
||||||
|
transaction.assignedJobId = findMatchingJob(transaction.itemName);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const transactionList = Array.from(pasteTransactionMap.values());
|
||||||
|
setDuplicatesFound(duplicates);
|
||||||
|
|
||||||
|
// Create individual transaction groups
|
||||||
|
const groups = transactionList.map(tx => ({
|
||||||
|
itemName: tx.itemName,
|
||||||
|
transactions: [tx],
|
||||||
|
totalQuantity: tx.quantity,
|
||||||
|
totalValue: tx.totalPrice
|
||||||
|
}));
|
||||||
|
|
||||||
|
setTransactionGroups(groups);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleAssignJob = (groupIndex: number, jobId: string) => {
|
||||||
|
setTransactionGroups(prev => {
|
||||||
|
const newGroups = [...prev];
|
||||||
|
newGroups[groupIndex].transactions.forEach(tx => {
|
||||||
|
tx.assignedJobId = jobId;
|
||||||
|
});
|
||||||
|
return newGroups;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = () => {
|
||||||
|
// Group transactions by assigned job
|
||||||
|
const assignments = transactionGroups
|
||||||
|
.flatMap(group => group.transactions)
|
||||||
|
.filter(tx => tx.assignedJobId)
|
||||||
|
.reduce((acc, tx) => {
|
||||||
|
const jobId = tx.assignedJobId!;
|
||||||
|
const existing = acc.find(a => a.jobId === jobId);
|
||||||
|
if (existing) {
|
||||||
|
existing.transactions.push(tx);
|
||||||
|
} else {
|
||||||
|
acc.push({ jobId, transactions: [tx] });
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, [] as { jobId: string, transactions: IndTransactionRecordNoId[] }[]);
|
||||||
|
|
||||||
|
onTransactionsAssigned(assignments);
|
||||||
|
onClose();
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="fixed inset-0 bg-black/50 flex items-center justify-center p-4 z-50"
|
||||||
|
onClick={onClose}
|
||||||
|
>
|
||||||
|
<Card
|
||||||
|
className="bg-gray-900 border-gray-700 text-white w-full max-w-4xl max-h-[90vh] overflow-y-auto"
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
>
|
||||||
|
<CardHeader className="flex flex-row items-center justify-between sticky top-0 bg-gray-900 border-b border-gray-700 z-10">
|
||||||
|
<CardTitle className="text-blue-400">Batch Expenditure Assignment</CardTitle>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
onClick={onClose}
|
||||||
|
className="text-gray-400 hover:text-white"
|
||||||
|
>
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label className="text-sm font-medium text-gray-300">
|
||||||
|
Paste EVE expenditure data (negative amounts):
|
||||||
|
</label>
|
||||||
|
<Textarea
|
||||||
|
value={pastedData}
|
||||||
|
onChange={(e) => handlePaste(e.target.value)}
|
||||||
|
placeholder="Paste your EVE expenditure transaction data here..."
|
||||||
|
className="min-h-32 bg-gray-800 border-gray-600 text-white"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{transactionGroups.length > 0 && (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<Badge variant="outline" className="text-blue-400 border-blue-400">
|
||||||
|
{transactionGroups.length} expenditures found
|
||||||
|
</Badge>
|
||||||
|
{duplicatesFound > 0 && (
|
||||||
|
<Badge variant="outline" className="text-yellow-400 border-yellow-400">
|
||||||
|
{duplicatesFound} duplicates found
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow className="border-gray-700">
|
||||||
|
<TableHead className="text-gray-300">Material</TableHead>
|
||||||
|
<TableHead className="text-gray-300">Quantity</TableHead>
|
||||||
|
<TableHead className="text-gray-300">Total Cost</TableHead>
|
||||||
|
<TableHead className="text-gray-300">Assign To Job</TableHead>
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{transactionGroups.map((group, index) => {
|
||||||
|
const autoAssigned = group.transactions[0]?.assignedJobId;
|
||||||
|
const isDuplicate = group.transactions[0]?.isDuplicate;
|
||||||
|
const matchingJob = autoAssigned ? jobs.find(j => j.id === autoAssigned) : undefined;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TableRow
|
||||||
|
key={`${group.itemName}-${index}`}
|
||||||
|
className={`border-gray-700 ${isDuplicate ? 'bg-red-900/30' : ''}`}
|
||||||
|
>
|
||||||
|
<TableCell className="text-white flex items-center gap-2">
|
||||||
|
{group.itemName}
|
||||||
|
{isDuplicate && (
|
||||||
|
<Badge variant="destructive" className="bg-red-600">
|
||||||
|
Duplicate
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell className="text-gray-300">
|
||||||
|
{group.totalQuantity.toLocaleString()}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell className="text-red-400">
|
||||||
|
{formatISK(group.totalValue)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{isDuplicate ? (
|
||||||
|
<div className="text-red-400 text-sm">
|
||||||
|
Transaction already exists
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<Select
|
||||||
|
value={group.transactions[0]?.assignedJobId || ''}
|
||||||
|
onValueChange={(value) => handleAssignJob(index, value)}
|
||||||
|
>
|
||||||
|
<SelectTrigger
|
||||||
|
className={`bg-gray-800 border-gray-600 text-white ${autoAssigned ? 'border-green-600' : ''}`}
|
||||||
|
>
|
||||||
|
<SelectValue placeholder={autoAssigned ? `Auto-assigned to ${matchingJob?.outputItem}` : 'Select a job'} />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent className="bg-gray-800 border-gray-600">
|
||||||
|
{eligibleJobs
|
||||||
|
.filter(job =>
|
||||||
|
job.billOfMaterials?.some(item =>
|
||||||
|
item.name.toLowerCase().includes(group.itemName.toLowerCase())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.map(job => (
|
||||||
|
<SelectItem
|
||||||
|
key={job.id}
|
||||||
|
value={job.id}
|
||||||
|
className="text-white"
|
||||||
|
>
|
||||||
|
{job.outputItem} (Acquisition)
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
|
||||||
|
<div className="flex justify-end gap-2">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={onClose}
|
||||||
|
className="border-gray-600 hover:bg-gray-800"
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={!transactionGroups.some(g => g.transactions.some(tx => !tx.isDuplicate && tx.assignedJobId))}
|
||||||
|
className="bg-blue-600 hover:bg-blue-700"
|
||||||
|
>
|
||||||
|
Assign Expenditures
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default BatchExpenditureForm;
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
|
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
import { Import, Download, FileText } from 'lucide-react';
|
import { Import, Download, FileText, AlertTriangle } from 'lucide-react';
|
||||||
import { IndBillitemRecord, IndBillitemRecordNoId } from '@/lib/pbtypes';
|
import { IndBillitemRecord, IndBillitemRecordNoId } from '@/lib/pbtypes';
|
||||||
import { IndJob } from '@/lib/types';
|
import { IndJob } from '@/lib/types';
|
||||||
import { dataService } from '@/services/dataService';
|
import { dataService } from '@/services/dataService';
|
||||||
|
import { useToast } from '@/hooks/use-toast';
|
||||||
|
|
||||||
interface MaterialsImportExportProps {
|
interface MaterialsImportExportProps {
|
||||||
job?: IndJob;
|
job?: IndJob;
|
||||||
@@ -25,6 +27,7 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const [bomInput, setBomInput] = useState('');
|
const [bomInput, setBomInput] = useState('');
|
||||||
const [consumedInput, setConsumedInput] = useState('');
|
const [consumedInput, setConsumedInput] = useState('');
|
||||||
|
const { toast } = useToast();
|
||||||
|
|
||||||
const parseBillOfMaterials = (text: string): IndBillitemRecordNoId[] => {
|
const parseBillOfMaterials = (text: string): IndBillitemRecordNoId[] => {
|
||||||
const lines = text.split('\n').filter(line => line.trim());
|
const lines = text.split('\n').filter(line => line.trim());
|
||||||
@@ -62,6 +65,35 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
return materials;
|
return materials;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const calculateMissingMaterials = () => {
|
||||||
|
if (!job) return [];
|
||||||
|
|
||||||
|
// Create a map of required materials from bill of materials
|
||||||
|
const requiredMaterials = new Map<string, number>();
|
||||||
|
billOfMaterials.forEach(item => {
|
||||||
|
requiredMaterials.set(item.name, item.quantity);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a map of owned materials from expenditures
|
||||||
|
const ownedMaterials = new Map<string, number>();
|
||||||
|
job.expenditures?.forEach(transaction => {
|
||||||
|
const currentOwned = ownedMaterials.get(transaction.itemName) || 0;
|
||||||
|
ownedMaterials.set(transaction.itemName, currentOwned + transaction.quantity);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Calculate missing materials
|
||||||
|
const missingMaterials: { name: string; quantity: number }[] = [];
|
||||||
|
requiredMaterials.forEach((required, materialName) => {
|
||||||
|
const owned = ownedMaterials.get(materialName) || 0;
|
||||||
|
const missing = required - owned;
|
||||||
|
if (missing > 0) {
|
||||||
|
missingMaterials.push({ name: materialName, quantity: missing });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return missingMaterials;
|
||||||
|
};
|
||||||
|
|
||||||
const exportBillOfMaterials = (): string => {
|
const exportBillOfMaterials = (): string => {
|
||||||
return billOfMaterials.map(item => `${item.name} ${item.quantity}`).join('\n');
|
return billOfMaterials.map(item => `${item.name} ${item.quantity}`).join('\n');
|
||||||
};
|
};
|
||||||
@@ -70,6 +102,11 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
return consumedMaterials.map(item => `${item.name}\t${item.quantity}`).join('\n');
|
return consumedMaterials.map(item => `${item.name}\t${item.quantity}`).join('\n');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const exportMissingMaterials = (): string => {
|
||||||
|
const missing = calculateMissingMaterials();
|
||||||
|
return missing.map(item => `${item.name}\t${item.quantity}`).join('\n');
|
||||||
|
};
|
||||||
|
|
||||||
const handleImportBom = async () => {
|
const handleImportBom = async () => {
|
||||||
if (!job) return;
|
if (!job) return;
|
||||||
|
|
||||||
@@ -103,13 +140,43 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
const handleExportBom = () => {
|
const handleExportBom = () => {
|
||||||
const exported = exportBillOfMaterials();
|
const exported = exportBillOfMaterials();
|
||||||
navigator.clipboard.writeText(exported);
|
navigator.clipboard.writeText(exported);
|
||||||
|
toast({
|
||||||
|
title: "Exported",
|
||||||
|
description: "Bill of materials copied to clipboard",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleExportConsumed = () => {
|
const handleExportConsumed = () => {
|
||||||
const exported = exportConsumedMaterials();
|
const exported = exportConsumedMaterials();
|
||||||
navigator.clipboard.writeText(exported);
|
navigator.clipboard.writeText(exported);
|
||||||
|
toast({
|
||||||
|
title: "Exported",
|
||||||
|
description: "Consumed materials copied to clipboard",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleExportMissing = () => {
|
||||||
|
const exported = exportMissingMaterials();
|
||||||
|
if (exported) {
|
||||||
|
navigator.clipboard.writeText(exported);
|
||||||
|
toast({
|
||||||
|
title: "Exported",
|
||||||
|
description: "Missing materials copied to clipboard",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
toast({
|
||||||
|
title: "Nothing Missing",
|
||||||
|
description: "All materials are satisfied for this job",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const missingMaterials = calculateMissingMaterials();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="bg-gray-900 border-gray-700 text-white">
|
<Card className="bg-gray-900 border-gray-700 text-white">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
@@ -122,15 +189,27 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<Label className="text-gray-300">Bill of Materials</Label>
|
<Label className="text-gray-300">Bill of Materials</Label>
|
||||||
<Button
|
<div className="flex gap-1">
|
||||||
size="sm"
|
<Button
|
||||||
variant="outline"
|
size="sm"
|
||||||
onClick={handleExportBom}
|
variant="outline"
|
||||||
className="border-gray-600 hover:bg-gray-800"
|
onClick={handleExportBom}
|
||||||
>
|
className="border-gray-600 hover:bg-gray-800"
|
||||||
<Download className="w-4 h-4 mr-2" />
|
>
|
||||||
Export
|
<Download className="w-4 h-4 mr-2" />
|
||||||
</Button>
|
Export
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
onClick={handleExportMissing}
|
||||||
|
className="border-gray-600 hover:bg-gray-800"
|
||||||
|
disabled={!job || missingMaterials.length === 0}
|
||||||
|
>
|
||||||
|
<AlertTriangle className="w-4 h-4 mr-2" />
|
||||||
|
Missing
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Textarea
|
<Textarea
|
||||||
placeholder="Paste bill of materials here (e.g., Mexallon 1000)"
|
placeholder="Paste bill of materials here (e.g., Mexallon 1000)"
|
||||||
@@ -200,6 +279,17 @@ const MaterialsImportExport: React.FC<MaterialsImportExportProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{missingMaterials.length > 0 && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label className="text-gray-300">Missing Materials:</Label>
|
||||||
|
<div className="text-sm text-red-400 max-h-32 overflow-y-auto">
|
||||||
|
{missingMaterials.map((item, index) => (
|
||||||
|
<div key={index}>{item.name}: {item.quantity.toLocaleString()}</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
|||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
||||||
import { Plus, Factory, TrendingUp, Briefcase, FileText, Settings, BarChart3 } from 'lucide-react';
|
import { Plus, Factory, TrendingUp, Briefcase, FileText, Settings, BarChart3, ShoppingCart } from 'lucide-react';
|
||||||
import { IndTransactionRecordNoId, IndJobRecordNoId } from '@/lib/pbtypes';
|
import { IndTransactionRecordNoId, IndJobRecordNoId } from '@/lib/pbtypes';
|
||||||
import { formatISK } from '@/utils/priceUtils';
|
import { formatISK } from '@/utils/priceUtils';
|
||||||
import { getStatusPriority } from '@/utils/jobStatusUtils';
|
import { getStatusPriority } from '@/utils/jobStatusUtils';
|
||||||
@@ -12,6 +12,7 @@ import JobForm from '@/components/JobForm';
|
|||||||
import JobGroup from '@/components/JobGroup';
|
import JobGroup from '@/components/JobGroup';
|
||||||
import { IndJob } from '@/lib/types';
|
import { IndJob } from '@/lib/types';
|
||||||
import BatchTransactionForm from '@/components/BatchTransactionForm';
|
import BatchTransactionForm from '@/components/BatchTransactionForm';
|
||||||
|
import BatchExpenditureForm from '@/components/BatchExpenditureForm';
|
||||||
import { useJobs } from '@/hooks/useDataService';
|
import { useJobs } from '@/hooks/useDataService';
|
||||||
import { useJobMetrics } from '@/hooks/useJobMetrics';
|
import { useJobMetrics } from '@/hooks/useJobMetrics';
|
||||||
import SearchOverlay from '@/components/SearchOverlay';
|
import SearchOverlay from '@/components/SearchOverlay';
|
||||||
@@ -35,6 +36,7 @@ const Index = () => {
|
|||||||
const [showJobForm, setShowJobForm] = useState(false);
|
const [showJobForm, setShowJobForm] = useState(false);
|
||||||
const [editingJob, setEditingJob] = useState<IndJob | null>(null);
|
const [editingJob, setEditingJob] = useState<IndJob | null>(null);
|
||||||
const [showBatchForm, setShowBatchForm] = useState(false);
|
const [showBatchForm, setShowBatchForm] = useState(false);
|
||||||
|
const [showBatchExpenditureForm, setShowBatchExpenditureForm] = useState(false);
|
||||||
const [searchOpen, setSearchOpen] = useState(false);
|
const [searchOpen, setSearchOpen] = useState(false);
|
||||||
const [searchQuery, setSearchQuery] = useState('');
|
const [searchQuery, setSearchQuery] = useState('');
|
||||||
const [totalRevenueChartOpen, setTotalRevenueChartOpen] = useState(false);
|
const [totalRevenueChartOpen, setTotalRevenueChartOpen] = useState(false);
|
||||||
@@ -60,7 +62,6 @@ const Index = () => {
|
|||||||
return () => window.removeEventListener('keydown', handleKeyDown);
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Save scroll position before updates
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleScroll = () => {
|
const handleScroll = () => {
|
||||||
scrollPositionRef.current = window.scrollY;
|
scrollPositionRef.current = window.scrollY;
|
||||||
@@ -112,7 +113,6 @@ const Index = () => {
|
|||||||
try {
|
try {
|
||||||
const newJob = await createJob(jobData);
|
const newJob = await createJob(jobData);
|
||||||
|
|
||||||
// If we have bill of materials from the job dump, add them to the newly created job
|
|
||||||
if (billOfMaterials && billOfMaterials.length > 0) {
|
if (billOfMaterials && billOfMaterials.length > 0) {
|
||||||
const billItems = billOfMaterials.map(item => ({
|
const billItems = billOfMaterials.map(item => ({
|
||||||
name: item.name,
|
name: item.name,
|
||||||
@@ -186,18 +186,15 @@ const Index = () => {
|
|||||||
}, {} as Record<string, IndJob[]>);
|
}, {} as Record<string, IndJob[]>);
|
||||||
|
|
||||||
const toggleGroup = async (status: string) => {
|
const toggleGroup = async (status: string) => {
|
||||||
// Save current scroll position
|
|
||||||
const currentScrollY = window.scrollY;
|
const currentScrollY = window.scrollY;
|
||||||
|
|
||||||
const newState = { ...collapsedGroups, [status]: !collapsedGroups[status] };
|
const newState = { ...collapsedGroups, [status]: !collapsedGroups[status] };
|
||||||
setCollapsedGroups(newState);
|
setCollapsedGroups(newState);
|
||||||
localStorage.setItem('jobGroupsCollapsed', JSON.stringify(newState));
|
localStorage.setItem('jobGroupsCollapsed', JSON.stringify(newState));
|
||||||
|
|
||||||
// If expanding and not currently loading this status
|
|
||||||
if (collapsedGroups[status] && !loadingStatuses.has(status)) {
|
if (collapsedGroups[status] && !loadingStatuses.has(status)) {
|
||||||
await loadJobsForStatuses([status]);
|
await loadJobsForStatuses([status]);
|
||||||
|
|
||||||
// Restore scroll position after a brief delay to allow for rendering
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.scrollTo(0, currentScrollY);
|
window.scrollTo(0, currentScrollY);
|
||||||
}, 50);
|
}, 50);
|
||||||
@@ -214,6 +211,16 @@ const Index = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleBatchExpendituresAssigned = async (assignments: { jobId: string, transactions: IndTransactionRecordNoId[] }[]) => {
|
||||||
|
try {
|
||||||
|
for (const { jobId, transactions } of assignments) {
|
||||||
|
await createMultipleTransactions(jobId, transactions, 'expenditure');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error assigning batch expenditures:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (showJobForm) {
|
if (showJobForm) {
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-gray-950 p-6">
|
<div className="min-h-screen bg-gray-950 p-6">
|
||||||
@@ -320,7 +327,15 @@ const Index = () => {
|
|||||||
className="border-gray-600 hover:bg-gray-800"
|
className="border-gray-600 hover:bg-gray-800"
|
||||||
>
|
>
|
||||||
<FileText className="w-4 h-4 mr-2" />
|
<FileText className="w-4 h-4 mr-2" />
|
||||||
Batch Assign
|
Batch Income
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => setShowBatchExpenditureForm(true)}
|
||||||
|
className="border-gray-600 hover:bg-gray-800"
|
||||||
|
>
|
||||||
|
<ShoppingCart className="w-4 h-4 mr-2" />
|
||||||
|
Batch Expenditure
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
@@ -378,6 +393,14 @@ const Index = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{showBatchExpenditureForm && (
|
||||||
|
<BatchExpenditureForm
|
||||||
|
jobs={jobs}
|
||||||
|
onClose={() => setShowBatchExpenditureForm(false)}
|
||||||
|
onTransactionsAssigned={handleBatchExpendituresAssigned}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<TransactionChart
|
<TransactionChart
|
||||||
jobs={regularJobs}
|
jobs={regularJobs}
|
||||||
type="total-revenue"
|
type="total-revenue"
|
||||||
@@ -404,7 +427,6 @@ const SalesTaxConfig = () => {
|
|||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
localStorage.setItem('salesTax', salesTax);
|
localStorage.setItem('salesTax', salesTax);
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
// Trigger a re-render of job cards by dispatching a storage event
|
|
||||||
window.dispatchEvent(new StorageEvent('storage', {
|
window.dispatchEvent(new StorageEvent('storage', {
|
||||||
key: 'salesTax',
|
key: 'salesTax',
|
||||||
newValue: salesTax
|
newValue: salesTax
|
||||||
|
|||||||
Reference in New Issue
Block a user