2025-11-15 21:21:06 +01:00
|
|
|
// ================================================================
|
|
|
|
|
// frontend/src/components/dashboard/PurchaseOrderDetailsModal.tsx
|
|
|
|
|
// ================================================================
|
|
|
|
|
/**
|
|
|
|
|
* Purchase Order Details Modal
|
2025-11-18 11:59:23 +01:00
|
|
|
* Unified view/edit modal for PO details from the Action Queue
|
|
|
|
|
* Now using EditViewModal with proper API response structure
|
2025-11-15 21:21:06 +01:00
|
|
|
*/
|
|
|
|
|
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
import React, { useState, useEffect, useMemo } from 'react';
|
2025-11-15 21:21:06 +01:00
|
|
|
import {
|
|
|
|
|
Package,
|
|
|
|
|
Building2,
|
|
|
|
|
Calendar,
|
|
|
|
|
Euro,
|
|
|
|
|
FileText,
|
|
|
|
|
CheckCircle,
|
2025-11-18 11:59:23 +01:00
|
|
|
Edit,
|
2025-11-15 21:21:06 +01:00
|
|
|
} from 'lucide-react';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-11-18 11:59:23 +01:00
|
|
|
import { usePurchaseOrder, useUpdatePurchaseOrder } from '../../api/hooks/purchase-orders';
|
|
|
|
|
import { useUserById } from '../../api/hooks/user';
|
|
|
|
|
import { EditViewModal, EditViewModalSection } from '../ui/EditViewModal/EditViewModal';
|
|
|
|
|
import type { PurchaseOrderItem } from '../../api/services/purchase_orders';
|
2025-11-15 21:21:06 +01:00
|
|
|
|
|
|
|
|
interface PurchaseOrderDetailsModalProps {
|
|
|
|
|
poId: string;
|
|
|
|
|
tenantId: string;
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
onApprove?: (poId: string) => void;
|
2025-11-18 11:59:23 +01:00
|
|
|
initialMode?: 'view' | 'edit';
|
2025-11-15 21:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const PurchaseOrderDetailsModal: React.FC<PurchaseOrderDetailsModalProps> = ({
|
|
|
|
|
poId,
|
|
|
|
|
tenantId,
|
|
|
|
|
isOpen,
|
|
|
|
|
onClose,
|
|
|
|
|
onApprove,
|
2025-11-18 11:59:23 +01:00
|
|
|
initialMode = 'view',
|
2025-11-15 21:21:06 +01:00
|
|
|
}) => {
|
2025-11-18 11:59:23 +01:00
|
|
|
const { t, i18n } = useTranslation(['purchase_orders', 'common']);
|
|
|
|
|
const { data: po, isLoading, refetch } = usePurchaseOrder(tenantId, poId);
|
|
|
|
|
const [mode, setMode] = useState<'view' | 'edit'>(initialMode);
|
|
|
|
|
const updatePurchaseOrderMutation = useUpdatePurchaseOrder();
|
|
|
|
|
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
// Form state for edit mode
|
|
|
|
|
const [formData, setFormData] = useState<Record<string, any>>({});
|
|
|
|
|
|
|
|
|
|
// Initialize form data when entering edit mode
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (mode === 'edit' && po) {
|
|
|
|
|
setFormData({
|
|
|
|
|
priority: po.priority,
|
|
|
|
|
required_delivery_date: po.required_delivery_date || '',
|
|
|
|
|
notes: po.notes || '',
|
|
|
|
|
items: (po.items || []).map((item: PurchaseOrderItem) => ({
|
|
|
|
|
id: item.id,
|
|
|
|
|
inventory_product_id: item.inventory_product_id,
|
|
|
|
|
product_code: item.product_code || '',
|
|
|
|
|
product_name: item.product_name || '',
|
|
|
|
|
ordered_quantity: item.ordered_quantity,
|
|
|
|
|
unit_of_measure: item.unit_of_measure,
|
|
|
|
|
unit_price: parseFloat(item.unit_price),
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [mode, po]);
|
|
|
|
|
|
|
|
|
|
// Field change handler for edit mode
|
|
|
|
|
const handleFieldChange = (sectionIndex: number, fieldIndex: number, value: any) => {
|
|
|
|
|
// Map section/field indices to form field names
|
|
|
|
|
// Section 0 = supplier_info (not editable)
|
|
|
|
|
// Section 1 = order_details: [priority, required_delivery_date, notes]
|
|
|
|
|
// Section 2 = products: [items]
|
|
|
|
|
|
|
|
|
|
if (sectionIndex === 1) {
|
|
|
|
|
// Order details section
|
|
|
|
|
const fieldNames = ['priority', 'required_delivery_date', 'notes'];
|
|
|
|
|
const fieldName = fieldNames[fieldIndex];
|
|
|
|
|
if (fieldName) {
|
|
|
|
|
setFormData(prev => ({ ...prev, [fieldName]: value }));
|
|
|
|
|
}
|
|
|
|
|
} else if (sectionIndex === 2 && fieldIndex === 0) {
|
|
|
|
|
// Products section - items field
|
|
|
|
|
setFormData(prev => ({ ...prev, items: value }));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
// Component to display user name with data fetching
|
|
|
|
|
const UserName: React.FC<{ userId: string | undefined | null }> = ({ userId }) => {
|
|
|
|
|
if (!userId) return <>{t('common:not_available')}</>;
|
2025-11-15 21:21:06 +01:00
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
if (userId === '00000000-0000-0000-0000-000000000001' || userId === '00000000-0000-0000-0000-000000000000') {
|
|
|
|
|
return <>{t('common:system')}</>;
|
|
|
|
|
}
|
2025-11-15 21:21:06 +01:00
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
const { data: user, isLoading } = useUserById(userId, {
|
|
|
|
|
retry: 1,
|
|
|
|
|
staleTime: 10 * 60 * 1000,
|
|
|
|
|
});
|
2025-11-15 21:21:06 +01:00
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
if (isLoading) return <>{t('common:loading')}</>;
|
|
|
|
|
if (!user) return <>{t('common:unknown_user')}</>;
|
|
|
|
|
|
|
|
|
|
return <>{user.full_name || user.email || t('common:user')}</>;
|
2025-11-15 21:21:06 +01:00
|
|
|
};
|
|
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
// Component to display PO items
|
|
|
|
|
const PurchaseOrderItemsTable: React.FC<{ items: PurchaseOrderItem[] }> = ({ items }) => {
|
|
|
|
|
if (!items || items.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="text-center py-8 text-[var(--text-secondary)] border-2 border-dashed border-[var(--border-secondary)] rounded-lg">
|
|
|
|
|
<Package className="h-12 w-12 mx-auto mb-2 opacity-50" />
|
|
|
|
|
<p>{t('no_items')}</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-11-15 21:21:06 +01:00
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
const totalAmount = items.reduce((sum, item) => {
|
|
|
|
|
const price = parseFloat(item.unit_price) || 0;
|
|
|
|
|
const quantity = item.ordered_quantity || 0;
|
|
|
|
|
return sum + (price * quantity);
|
|
|
|
|
}, 0);
|
2025-11-15 21:21:06 +01:00
|
|
|
|
|
|
|
|
return (
|
2025-11-18 11:59:23 +01:00
|
|
|
<div className="space-y-3">
|
|
|
|
|
{items.map((item: PurchaseOrderItem, index: number) => {
|
|
|
|
|
const unitPrice = parseFloat(item.unit_price) || 0;
|
|
|
|
|
const quantity = item.ordered_quantity || 0;
|
|
|
|
|
const itemTotal = unitPrice * quantity;
|
|
|
|
|
const productName = item.product_name || `${t('product')} ${index + 1}`;
|
2025-11-15 21:21:06 +01:00
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
return (
|
2025-11-15 21:21:06 +01:00
|
|
|
<div
|
2025-11-18 11:59:23 +01:00
|
|
|
key={item.id || index}
|
|
|
|
|
className="p-4 border border-[var(--border-secondary)] rounded-lg bg-[var(--bg-secondary)]/50 space-y-3"
|
2025-11-15 21:21:06 +01:00
|
|
|
>
|
2025-11-18 11:59:23 +01:00
|
|
|
<div className="flex justify-between items-start">
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
<h4 className="font-semibold text-[var(--text-primary)]">{productName}</h4>
|
|
|
|
|
{item.product_code && (
|
|
|
|
|
<p className="text-sm text-[var(--text-secondary)]">
|
|
|
|
|
{t('sku')}: {item.product_code}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2025-11-15 21:21:06 +01:00
|
|
|
</div>
|
2025-11-18 11:59:23 +01:00
|
|
|
<div className="text-right">
|
|
|
|
|
<p className="font-bold text-lg text-[var(--color-primary-600)]">
|
|
|
|
|
€{itemTotal.toFixed(2)}
|
2025-11-15 21:21:06 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-11-18 11:59:23 +01:00
|
|
|
<div className="grid grid-cols-2 gap-4 text-sm">
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-[var(--text-secondary)]">{t('quantity')}</p>
|
|
|
|
|
<p className="font-medium text-[var(--text-primary)]">
|
|
|
|
|
{quantity} {item.unit_of_measure}
|
|
|
|
|
</p>
|
2025-11-15 21:21:06 +01:00
|
|
|
</div>
|
2025-11-18 11:59:23 +01:00
|
|
|
<div>
|
|
|
|
|
<p className="text-[var(--text-secondary)]">{t('unit_price')}</p>
|
|
|
|
|
<p className="font-medium text-[var(--text-primary)]">€{unitPrice.toFixed(2)}</p>
|
2025-11-15 21:21:06 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-11-18 11:59:23 +01:00
|
|
|
{item.quality_requirements && (
|
|
|
|
|
<div className="pt-2 border-t border-[var(--border-secondary)]">
|
|
|
|
|
<p className="text-xs text-[var(--text-secondary)]">{t('quality_requirements')}</p>
|
|
|
|
|
<p className="text-sm text-[var(--text-primary)]">{item.quality_requirements}</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{item.item_notes && (
|
|
|
|
|
<div className="pt-2 border-t border-[var(--border-secondary)]">
|
|
|
|
|
<p className="text-xs text-[var(--text-secondary)]">{t('common:notes')}</p>
|
|
|
|
|
<p className="text-sm text-[var(--text-primary)]">{item.item_notes}</p>
|
2025-11-15 21:21:06 +01:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2025-11-18 11:59:23 +01:00
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
<div className="flex justify-between items-center pt-4 border-t-2 border-[var(--border-primary)]">
|
|
|
|
|
<span className="font-semibold text-lg text-[var(--text-primary)]">{t('total')}</span>
|
|
|
|
|
<span className="font-bold text-2xl text-[var(--color-primary-600)]">€{totalAmount.toFixed(2)}</span>
|
2025-11-15 21:21:06 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-11-18 11:59:23 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Priority and unit options for edit mode
|
|
|
|
|
const priorityOptions = [
|
|
|
|
|
{ value: 'urgent', label: t('priority_urgent') },
|
|
|
|
|
{ value: 'high', label: t('priority_high') },
|
|
|
|
|
{ value: 'normal', label: t('priority_normal') },
|
|
|
|
|
{ value: 'low', label: t('priority_low') }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const unitOptions = [
|
|
|
|
|
{ value: 'kg', label: t('unit_kg') },
|
|
|
|
|
{ value: 'g', label: t('unit_g') },
|
|
|
|
|
{ value: 'l', label: t('unit_l') },
|
|
|
|
|
{ value: 'ml', label: t('unit_ml') },
|
|
|
|
|
{ value: 'units', label: t('unit_units') },
|
|
|
|
|
{ value: 'boxes', label: t('unit_boxes') },
|
|
|
|
|
{ value: 'bags', label: t('unit_bags') }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Build sections for EditViewModal
|
|
|
|
|
const buildViewSections = (): EditViewModalSection[] => {
|
|
|
|
|
if (!po) return [];
|
|
|
|
|
|
|
|
|
|
const formatCurrency = (value: any) => {
|
|
|
|
|
const num = Number(value);
|
|
|
|
|
return isNaN(num) ? '0.00' : num.toFixed(2);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sections: EditViewModalSection[] = [
|
|
|
|
|
{
|
|
|
|
|
title: t('general_information'),
|
|
|
|
|
icon: FileText,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('po_number'),
|
|
|
|
|
value: po.po_number,
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: t('status_label'),
|
|
|
|
|
value: t(`status.${po.status}`),
|
|
|
|
|
type: 'status' as const
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: t('priority'),
|
|
|
|
|
value: t(`priority_${po.priority}` as any) || po.priority,
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: t('created'),
|
|
|
|
|
value: new Date(po.created_at).toLocaleDateString(i18n.language, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'long',
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
minute: '2-digit'
|
|
|
|
|
}),
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('supplier_info'),
|
|
|
|
|
icon: Building2,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('supplier_name'),
|
|
|
|
|
value: po.supplier?.name || t('common:unknown'),
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('financial_summary'),
|
|
|
|
|
icon: Euro,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('total_amount'),
|
|
|
|
|
value: `€${formatCurrency(po.total_amount)}`,
|
|
|
|
|
type: 'text' as const,
|
|
|
|
|
highlight: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('items'),
|
|
|
|
|
icon: Package,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: '',
|
|
|
|
|
value: <PurchaseOrderItemsTable items={po.items || []} />,
|
|
|
|
|
type: 'component' as const,
|
|
|
|
|
span: 2
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('dates'),
|
|
|
|
|
icon: Calendar,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('order_date'),
|
|
|
|
|
value: new Date(po.order_date).toLocaleDateString(i18n.language, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric'
|
|
|
|
|
}),
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
},
|
|
|
|
|
...(po.required_delivery_date ? [{
|
|
|
|
|
label: t('required_delivery_date'),
|
|
|
|
|
value: new Date(po.required_delivery_date).toLocaleDateString(i18n.language, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric'
|
|
|
|
|
}),
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
}] : []),
|
|
|
|
|
...(po.estimated_delivery_date ? [{
|
|
|
|
|
label: t('expected_delivery'),
|
|
|
|
|
value: new Date(po.estimated_delivery_date).toLocaleDateString(i18n.language, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'short',
|
|
|
|
|
day: 'numeric'
|
|
|
|
|
}),
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
}] : [])
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Add notes section if present
|
|
|
|
|
if (po.notes) {
|
|
|
|
|
sections.push({
|
|
|
|
|
title: t('notes'),
|
|
|
|
|
icon: FileText,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('order_notes'),
|
|
|
|
|
value: po.notes,
|
|
|
|
|
type: 'text' as const
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sections;
|
|
|
|
|
};
|
|
|
|
|
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
// Component to edit PO items
|
|
|
|
|
const EditablePurchaseOrderItems: React.FC<{ value: any; onChange?: (value: any) => void }> = ({ value: items, onChange }) => {
|
|
|
|
|
const handleItemChange = (index: number, field: string, value: any) => {
|
|
|
|
|
if (!items || !onChange) return;
|
|
|
|
|
const updatedItems = [...items];
|
|
|
|
|
updatedItems[index] = {
|
|
|
|
|
...updatedItems[index],
|
|
|
|
|
[field]: value
|
|
|
|
|
};
|
|
|
|
|
onChange(updatedItems);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!items || items.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="text-center py-8 text-[var(--text-secondary)] border-2 border-dashed border-[var(--border-secondary)] rounded-lg">
|
|
|
|
|
<Package className="h-12 w-12 mx-auto mb-2 opacity-50" />
|
|
|
|
|
<p>{t('no_items')}</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const totalAmount = items.reduce((sum, item) => {
|
|
|
|
|
const price = parseFloat(item.unit_price) || 0;
|
|
|
|
|
const quantity = item.ordered_quantity || 0;
|
|
|
|
|
return sum + (price * quantity);
|
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
{items.map((item: any, index: number) => {
|
|
|
|
|
const unitPrice = parseFloat(item.unit_price) || 0;
|
|
|
|
|
const quantity = item.ordered_quantity || 0;
|
|
|
|
|
const itemTotal = unitPrice * quantity;
|
|
|
|
|
const productName = item.product_name || `${t('product')} ${index + 1}`;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={item.id || index}
|
|
|
|
|
className="p-4 border border-[var(--border-secondary)] rounded-lg bg-[var(--bg-secondary)]/50 space-y-3"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex justify-between items-start">
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
<h4 className="font-semibold text-[var(--text-primary)]">{productName}</h4>
|
|
|
|
|
{item.product_code && (
|
|
|
|
|
<p className="text-sm text-[var(--text-secondary)]">
|
|
|
|
|
{t('sku')}: {item.product_code}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-right">
|
|
|
|
|
<p className="font-bold text-lg text-[var(--color-primary-600)]">
|
|
|
|
|
€{itemTotal.toFixed(2)}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Editable fields */}
|
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
|
|
|
<div>
|
|
|
|
|
<label className="block text-xs text-[var(--text-secondary)] mb-1">
|
|
|
|
|
{t('quantity')}
|
|
|
|
|
</label>
|
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
<input
|
|
|
|
|
type="number"
|
|
|
|
|
value={quantity}
|
|
|
|
|
onChange={(e) => handleItemChange(index, 'ordered_quantity', parseFloat(e.target.value) || 0)}
|
|
|
|
|
min="0"
|
|
|
|
|
step="0.01"
|
|
|
|
|
className="flex-1 px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)] bg-[var(--bg-primary)]"
|
|
|
|
|
/>
|
|
|
|
|
<select
|
|
|
|
|
value={item.unit_of_measure}
|
|
|
|
|
onChange={(e) => handleItemChange(index, 'unit_of_measure', e.target.value)}
|
|
|
|
|
className="px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)] bg-[var(--bg-primary)]"
|
|
|
|
|
>
|
|
|
|
|
{unitOptions.map(opt => (
|
|
|
|
|
<option key={opt.value} value={opt.value}>{opt.label}</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<label className="block text-xs text-[var(--text-secondary)] mb-1">
|
|
|
|
|
{t('unit_price')}
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
type="number"
|
|
|
|
|
value={unitPrice}
|
|
|
|
|
onChange={(e) => handleItemChange(index, 'unit_price', e.target.value)}
|
|
|
|
|
min="0"
|
|
|
|
|
step="0.01"
|
|
|
|
|
className="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)] bg-[var(--bg-primary)]"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
<div className="flex justify-between items-center pt-4 border-t-2 border-[var(--border-primary)]">
|
|
|
|
|
<span className="font-semibold text-lg text-[var(--text-primary)]">{t('total')}</span>
|
|
|
|
|
<span className="font-bold text-2xl text-[var(--color-primary-600)]">€{totalAmount.toFixed(2)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-18 11:59:23 +01:00
|
|
|
// Build sections for edit mode
|
|
|
|
|
const buildEditSections = (): EditViewModalSection[] => {
|
|
|
|
|
if (!po) return [];
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
title: t('supplier_info'),
|
|
|
|
|
icon: Building2,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('supplier'),
|
|
|
|
|
value: po.supplier?.name || t('common:unknown'),
|
|
|
|
|
type: 'text' as const,
|
|
|
|
|
editable: false,
|
|
|
|
|
helpText: t('supplier_cannot_modify')
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('order_details'),
|
|
|
|
|
icon: Calendar,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('priority'),
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
value: formData.priority || po.priority,
|
2025-11-18 11:59:23 +01:00
|
|
|
type: 'select' as const,
|
|
|
|
|
editable: true,
|
|
|
|
|
options: priorityOptions,
|
|
|
|
|
helpText: t('adjust_priority')
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: t('required_delivery_date'),
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
value: formData.required_delivery_date || po.required_delivery_date || '',
|
2025-11-18 11:59:23 +01:00
|
|
|
type: 'date' as const,
|
|
|
|
|
editable: true,
|
|
|
|
|
helpText: t('delivery_deadline')
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: t('notes'),
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
value: formData.notes !== undefined ? formData.notes : (po.notes || ''),
|
2025-11-18 11:59:23 +01:00
|
|
|
type: 'textarea' as const,
|
|
|
|
|
editable: true,
|
|
|
|
|
placeholder: t('special_instructions'),
|
|
|
|
|
span: 2,
|
|
|
|
|
helpText: t('additional_info')
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: t('products'),
|
|
|
|
|
icon: Package,
|
|
|
|
|
fields: [
|
|
|
|
|
{
|
|
|
|
|
label: t('products'),
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
value: formData.items || (po.items || []).map((item: PurchaseOrderItem) => ({
|
2025-11-18 11:59:23 +01:00
|
|
|
id: item.id,
|
|
|
|
|
inventory_product_id: item.inventory_product_id,
|
|
|
|
|
product_code: item.product_code || '',
|
|
|
|
|
product_name: item.product_name || '',
|
|
|
|
|
ordered_quantity: item.ordered_quantity,
|
|
|
|
|
unit_of_measure: item.unit_of_measure,
|
|
|
|
|
unit_price: parseFloat(item.unit_price),
|
|
|
|
|
})),
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
type: 'component' as const,
|
|
|
|
|
component: EditablePurchaseOrderItems,
|
2025-11-18 11:59:23 +01:00
|
|
|
span: 2,
|
|
|
|
|
helpText: t('modify_quantities')
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Save handler for edit mode
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
const handleSave = async () => {
|
2025-11-18 11:59:23 +01:00
|
|
|
try {
|
|
|
|
|
const items = formData.items || [];
|
|
|
|
|
|
|
|
|
|
if (items.length === 0) {
|
|
|
|
|
throw new Error(t('at_least_one_product'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate quantities
|
|
|
|
|
const invalidQuantities = items.some((item: any) => item.ordered_quantity <= 0);
|
|
|
|
|
if (invalidQuantities) {
|
|
|
|
|
throw new Error(t('quantities_greater_zero'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate required fields
|
|
|
|
|
const invalidProducts = items.some((item: any) => !item.product_name);
|
|
|
|
|
if (invalidProducts) {
|
|
|
|
|
throw new Error(t('products_need_names'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare the update data
|
|
|
|
|
const updateData: any = {
|
|
|
|
|
notes: formData.notes || undefined,
|
|
|
|
|
priority: formData.priority || undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Add delivery date if changed
|
|
|
|
|
if (formData.required_delivery_date) {
|
|
|
|
|
updateData.required_delivery_date = formData.required_delivery_date;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update purchase order
|
|
|
|
|
await updatePurchaseOrderMutation.mutateAsync({
|
|
|
|
|
tenantId,
|
|
|
|
|
poId,
|
|
|
|
|
data: updateData
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Refetch data and switch back to view mode
|
|
|
|
|
await refetch();
|
|
|
|
|
setMode('view');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error modifying purchase order:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Build actions for modal footer - only Approve button for pending approval POs
|
|
|
|
|
const buildActions = () => {
|
|
|
|
|
if (!po) return undefined;
|
|
|
|
|
|
|
|
|
|
// Only show Approve button in view mode for pending approval POs
|
|
|
|
|
if (mode === 'view' && po.status === 'pending_approval') {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
label: t('actions.approve'),
|
|
|
|
|
icon: CheckCircle,
|
|
|
|
|
onClick: () => {
|
|
|
|
|
onApprove?.(poId);
|
|
|
|
|
onClose();
|
|
|
|
|
},
|
|
|
|
|
variant: 'primary' as const
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
};
|
|
|
|
|
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
const sections = useMemo(() => {
|
|
|
|
|
return mode === 'view' ? buildViewSections() : buildEditSections();
|
|
|
|
|
}, [mode, po, formData, i18n.language]);
|
2025-11-18 11:59:23 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<EditViewModal
|
|
|
|
|
isOpen={isOpen}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
setMode('view');
|
|
|
|
|
onClose();
|
|
|
|
|
}}
|
|
|
|
|
mode={mode}
|
|
|
|
|
onModeChange={setMode}
|
|
|
|
|
title={po?.po_number || t('purchase_order')}
|
|
|
|
|
subtitle={po ? new Date(po.created_at).toLocaleDateString(i18n.language, {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'long',
|
|
|
|
|
day: 'numeric'
|
|
|
|
|
}) : undefined}
|
|
|
|
|
sections={sections}
|
|
|
|
|
actions={buildActions()}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
size="lg"
|
|
|
|
|
// Enable edit mode via standard Edit button (only for pending approval)
|
|
|
|
|
onEdit={po?.status === 'pending_approval' ? () => setMode('edit') : undefined}
|
|
|
|
|
onSave={mode === 'edit' ? handleSave : undefined}
|
|
|
|
|
onCancel={mode === 'edit' ? () => setMode('view') : undefined}
|
Fix purchase order items display in edit mode
Problem:
- When editing a PO in the dashboard, the products section showed "[object Object]" instead of the actual product list
- The EditViewModal's 'list' type expects simple text arrays, not complex objects
Solution:
- Created EditablePurchaseOrderItems custom component to properly render and edit PO items
- Added form state management with useState and useEffect to track edited values
- Implemented handleFieldChange to update form data when users modify fields
- Changed field type from 'list' to 'component' with the custom editor
- Added editable input fields for quantity, unit of measure, and unit price
- Displays real-time item totals and grand total
Technical Details:
- Custom component receives value and onChange props from EditViewModal
- Form data is initialized when entering edit mode with all PO item details
- Each item shows: product name, SKU, quantity with unit selector, and price
- Unit options include kg, g, l, ml, units, boxes, bags
- Proper decimal handling for prices (parseFloat for display, string for API)
- Save handler validates items and updates only priority, delivery date, and notes
(item modifications are validated but not persisted in this iteration)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:04:06 +01:00
|
|
|
onFieldChange={handleFieldChange}
|
2025-11-18 11:59:23 +01:00
|
|
|
saveLabel={t('actions.save')}
|
|
|
|
|
cancelLabel={t('actions.cancel')}
|
|
|
|
|
/>
|
2025-11-15 21:21:06 +01:00
|
|
|
);
|
|
|
|
|
};
|