Refactored Barcode Page and made fixes to logic

This commit is contained in:
2026-04-15 14:08:16 +01:00
parent b2f34e308e
commit c4cf6a92fe
25 changed files with 4387 additions and 1028 deletions

View File

@@ -1,18 +1,32 @@
import { inventoryData } from '../data/inventory.js'
export function formatDate(dateStr) {
if (!dateStr) return ''
const date = new Date(dateStr + 'T00:00:00')
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 expiry = new Date(expiryDate + 'T00:00:00')
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) {
@@ -26,24 +40,40 @@ export function getExpiryStatus(expiryDate) {
}
}
export function searchInventory(searchName = '', selectedLocation = 'All', minQuantity = 0, maxQuantity = Infinity, minExpiryDate = '', maxExpiryDate = '') {
return inventoryData.filter(item => {
const nameMatch = item.name.toLowerCase().includes(searchName.toLowerCase())
const locationMatch = selectedLocation === 'All' || item.location === selectedLocation
const quantityMatch = item.quantity >= minQuantity && item.quantity <= maxQuantity
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 || maxExpiryDate) {
const itemExpiry = item.expiryDate
if (minExpiryDate && itemExpiry < minExpiryDate) expiryMatch = false
if (maxExpiryDate && itemExpiry > maxExpiryDate) expiryMatch = false
if (minExpiryDate && (!normalizedExpiryDate || normalizedExpiryDate < minExpiryDate)) {
expiryMatch = false
}
return nameMatch && locationMatch && quantityMatch && expiryMatch
if (maxExpiryDate && (!normalizedExpiryDate || normalizedExpiryDate > maxExpiryDate)) {
expiryMatch = false
}
return locationMatch && amountMatch && expiryMatch
})
}
export function getLocations() {
const locations = [...new Set(inventoryData.map(item => item.location))]
return ['All', ...locations.sort()]
}