Sort transactions properly
This commit is contained in:
@@ -18,12 +18,21 @@ interface JobCardProps {
|
||||
const JobCard: React.FC<JobCardProps> = ({ job, onEdit, onDelete, onUpdateProduced }) => {
|
||||
const [isEditingProduced, setIsEditingProduced] = useState(false);
|
||||
const [producedValue, setProducedValue] = useState(job.produced?.toString() || '0');
|
||||
const totalExpenditure = job.expenditures.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||
const totalIncome = job.income.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||
|
||||
// Sort transactions by date descending
|
||||
const sortedExpenditures = [...job.expenditures].sort((a, b) =>
|
||||
new Date(b.date).getTime() - new Date(a.date).getTime()
|
||||
);
|
||||
const sortedIncome = [...job.income].sort((a, b) =>
|
||||
new Date(b.date).getTime() - new Date(a.date).getTime()
|
||||
);
|
||||
|
||||
const totalExpenditure = sortedExpenditures.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||
const totalIncome = sortedIncome.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||
const profit = totalIncome - totalExpenditure;
|
||||
const margin = totalIncome > 0 ? ((profit / totalIncome) * 100) : 0;
|
||||
|
||||
const itemsSold = job.income.reduce((sum, tx) => sum + tx.quantity, 0);
|
||||
const itemsSold = sortedIncome.reduce((sum, tx) => sum + tx.quantity, 0);
|
||||
const saleStartTime = job.saleStart ? new Date(job.saleStart).getTime() : null;
|
||||
const daysSinceStart = saleStartTime ? Math.max(1, Math.ceil((Date.now() - saleStartTime) / (1000 * 60 * 60 * 24))) : 0;
|
||||
const itemsPerDay = daysSinceStart > 0 ? itemsSold / daysSinceStart : 0;
|
||||
|
||||
@@ -26,7 +26,12 @@ const TransactionTable: React.FC<TransactionTableProps> = ({
|
||||
const [editingId, setEditingId] = useState<string | null>(null);
|
||||
const [editingTransaction, setEditingTransaction] = useState<IndTransactionRecord | null>(null);
|
||||
|
||||
const totalAmount = transactions.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||
// Sort transactions by date descending
|
||||
const sortedTransactions = [...transactions].sort((a, b) =>
|
||||
new Date(b.date).getTime() - new Date(a.date).getTime()
|
||||
);
|
||||
|
||||
const totalAmount = sortedTransactions.reduce((sum, tx) => sum + tx.totalPrice, 0);
|
||||
|
||||
const handleEdit = (transaction: IndTransactionRecord) => {
|
||||
setEditingId(transaction.id);
|
||||
@@ -92,7 +97,7 @@ const TransactionTable: React.FC<TransactionTableProps> = ({
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{transactions.map((transaction) => (
|
||||
{sortedTransactions.map((transaction) => (
|
||||
<TableRow key={transaction.id} className="border-gray-700">
|
||||
<TableCell className="text-gray-300">
|
||||
{editingId === transaction.id ? (
|
||||
|
||||
Reference in New Issue
Block a user