80 lines
2.5 KiB
JavaScript
80 lines
2.5 KiB
JavaScript
export function formatDate(dateStr) {
|
|
if (!dateStr) return ''
|
|
|
|
const normalizedDate = toDateInputValue(dateStr)
|
|
const date = new Date(`${normalizedDate}T00:00:00`)
|
|
|
|
if (Number.isNaN(date.getTime())) return ''
|
|
|
|
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })
|
|
}
|
|
|
|
export function toDateInputValue(dateStr) {
|
|
if (!dateStr) return ''
|
|
return String(dateStr).slice(0, 10)
|
|
}
|
|
|
|
export function getExpiryStatus(expiryDate) {
|
|
if (!expiryDate) return { status: 'Unknown', color: '#999', text: '' }
|
|
|
|
const today = new Date()
|
|
today.setHours(0, 0, 0, 0)
|
|
|
|
const normalizedDate = toDateInputValue(expiryDate)
|
|
const expiry = new Date(`${normalizedDate}T00:00:00`)
|
|
|
|
if (Number.isNaN(expiry.getTime())) {
|
|
return { status: 'Unknown', color: '#999', text: '' }
|
|
}
|
|
|
|
const daysUntilExpiry = Math.floor((expiry - today) / (1000 * 60 * 60 * 24))
|
|
|
|
if (daysUntilExpiry < 0) {
|
|
return { status: 'Expired', color: '#f44336', days: daysUntilExpiry, text: `Expired ${Math.abs(daysUntilExpiry)} days ago` }
|
|
} else if (daysUntilExpiry === 0) {
|
|
return { status: 'Today', color: '#ff9800', days: 0, text: 'Expires today' }
|
|
} else if (daysUntilExpiry <= 7) {
|
|
return { status: 'Soon', color: '#ff9800', days: daysUntilExpiry, text: `Expires in ${daysUntilExpiry} days` }
|
|
} else {
|
|
return { status: 'Fresh', color: '#4CAF50', days: daysUntilExpiry, text: `Expires in ${daysUntilExpiry} days` }
|
|
}
|
|
}
|
|
|
|
export function formatAmount(amount, amountType = '') {
|
|
if (amount == null || amount === '') return 'Amount not set'
|
|
|
|
const unit = amountType?.trim() ?? ''
|
|
return unit ? `${amount} ${unit}` : String(amount)
|
|
}
|
|
|
|
export function filterInventoryItems(items, filters = {}) {
|
|
const {
|
|
locationId = '',
|
|
minAmount = 0,
|
|
maxAmount = Infinity,
|
|
minExpiryDate = '',
|
|
maxExpiryDate = '',
|
|
} = filters
|
|
|
|
return items.filter(item => {
|
|
const itemLocationId = item.locationId ?? item.location?.id ?? ''
|
|
const locationMatch = !locationId || itemLocationId === locationId
|
|
|
|
const numericAmount = item.amount == null ? 0 : Number(item.amount)
|
|
const amountMatch = numericAmount >= minAmount && numericAmount <= maxAmount
|
|
|
|
const normalizedExpiryDate = toDateInputValue(item.expiryDate)
|
|
let expiryMatch = true
|
|
|
|
if (minExpiryDate && (!normalizedExpiryDate || normalizedExpiryDate < minExpiryDate)) {
|
|
expiryMatch = false
|
|
}
|
|
|
|
if (maxExpiryDate && (!normalizedExpiryDate || normalizedExpiryDate > maxExpiryDate)) {
|
|
expiryMatch = false
|
|
}
|
|
|
|
return locationMatch && amountMatch && expiryMatch
|
|
})
|
|
}
|