Skip to main content

Overview

The FEFO (First Expired, First Out) ordering system automatically prioritizes inventory items based on their expiration dates, ensuring products are used or sold before they expire. This intelligent system reduces waste, maintains product quality, and optimizes warehouse operations.
FEFO prioritizes items by expiration date, unlike FIFO which prioritizes by purchase date. This is critical for perishable goods management.

FEFO Algorithm

The system implements a sophisticated expiry-based prioritization algorithm (analyticsController.js:69-115):

Core Implementation

exports.getFEFOOrdering = async (req, res) => {
  const today = new Date();
  
  // Get products sorted by expiry date (nearest first)
  const products = await Inventory.find({
    expiryDate: { $gte: today },     // Only non-expired items
    quantity: { $gt: 0 }              // Only items in stock
  })
  .sort({ expiryDate: 1 })            // Ascending order (nearest first)
  .limit(limit);
  
  const fefoData = products.map(product => {
    const daysUntilExpiry = Math.ceil(
      (new Date(product.expiryDate) - today) / (1000 * 60 * 60 * 24)
    );
    
    // Calculate urgency level
    let urgency = 'low';
    if (daysUntilExpiry <= 3) urgency = 'critical';
    else if (daysUntilExpiry <= 7) urgency = 'high';
    else if (daysUntilExpiry <= 14) urgency = 'medium';
    
    return { ...product, daysUntilExpiry, urgency };
  });
};

Filtering Logic

The FEFO system only includes items that meet specific criteria:
1

Active Items Only

Items must have an expiry date in the future (expiryDate >= today)
2

In Stock

Items must have quantity greater than 0
3

Sorted by Expiry

Results are sorted by expiry date in ascending order (nearest first)
4

Urgency Classification

Each item is assigned an urgency level based on days until expiry

Urgency Levels

The system classifies items into four urgency categories:

Critical

≤ 3 days until expiryImmediate action required. These items should be:
  • Moved to front of warehouse
  • Marked for immediate sale
  • Discounted if necessary
  • Used in production first

High

≤ 7 days until expiryHigh priority attention needed:
  • Monitor daily
  • Promote for sale
  • Consider bundle offers
  • Alert sales team

Medium

≤ 14 days until expiryModerate attention required:
  • Weekly monitoring
  • Normal sales priority
  • Track consumption rate
  • Plan promotions if needed

Low

> 14 days until expiryStandard handling:
  • Regular rotation
  • Normal stock flow
  • Routine monitoring
  • Follow FEFO sequence

Urgency Calculation

const daysUntilExpiry = Math.ceil(
  (new Date(product.expiryDate) - today) / (1000 * 60 * 60 * 24)
);

let urgency = 'low';
if (daysUntilExpiry <= 3) urgency = 'critical';      // Red alert
else if (daysUntilExpiry <= 7) urgency = 'high';     // Orange alert
else if (daysUntilExpiry <= 14) urgency = 'medium';  // Yellow alert
// else urgency remains 'low'                         // Blue info

Visual Indicators

The Manager Dashboard displays FEFO ordering with clear visual indicators (ManagerDashboard.tsx:717-755):

Priority Ranking

Each item is numbered based on FEFO priority:
<div className="flex items-center gap-2">
  <span className="text-lg font-bold text-slate-700">
    #{index + 1}
  </span>
  <h3 className="font-semibold">{item.productName}</h3>
</div>

Urgency Badges

Color-coded badges indicate urgency level:
const getUrgencyColor = (urgency: string) => {
  switch (urgency) {
    case 'critical':
      return 'bg-red-100 text-red-800 dark:bg-red-900/50 dark:text-red-300';
    case 'high':
      return 'bg-orange-100 text-orange-800 dark:bg-orange-900/50 dark:text-orange-300';
    case 'medium':
      return 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/50 dark:text-yellow-300';
    default:
      return 'bg-blue-100 text-blue-800 dark:bg-blue-900/50 dark:text-blue-300';
  }
};

<span className={`px-2 py-1 text-xs font-semibold rounded-full ${getUrgencyColor(item.urgency)}`}>
  {item.daysUntilExpiry}d left
</span>

Item Display Card

Each FEFO item is displayed in a comprehensive card:
<div className="p-4 rounded-lg bg-slate-50 border border-slate-200">
  <div className="flex items-start justify-between mb-2">
    <div className="flex-1">
      <div className="flex items-center gap-2">
        <span className="text-lg font-bold">#1</span>
        <h3 className="font-semibold">Fresh Milk</h3>
      </div>
      <p className="text-sm text-slate-500">
        DairySKU: DA-001
      </p>
    </div>
    <span className="px-2 py-1 bg-red-100 text-red-800 rounded-full">
      2d left
    </span>
  </div>
  <div className="flex items-center justify-between text-sm">
    <span>Qty: <strong>25</strong></span>
    <span>Local Dairy Farm</span>
  </div>
</div>

API Endpoint

Get FEFO Ordering

GET /api/analytics/fefo-ordering?limit=10
Query Parameters:
  • limit (optional) - Maximum number of items to return (default: 10)
Response:
{
  "success": true,
  "message": "FEFO ordering fetched successfully",
  "data": {
    "items": [
      {
        "id": "673ab12c5f8e9a001234abcd",
        "productName": "Fresh Milk",
        "category": "Dairy",
        "sku": "DA-001",
        "quantity": 25,
        "expiryDate": "2025-11-17T00:00:00.000Z",
        "daysUntilExpiry": 2,
        "urgency": "critical",
        "supplier": "Local Dairy Farm"
      },
      {
        "id": "673ab12c5f8e9a001234abce",
        "productName": "Greek Yogurt",
        "category": "Dairy",
        "sku": "DA-003",
        "quantity": 15,
        "expiryDate": "2025-11-18T00:00:00.000Z",
        "daysUntilExpiry": 3,
        "urgency": "critical",
        "supplier": "Dairy Co."
      }
    ],
    "count": 2
  }
}

Use Cases

Pick Order Optimization
  • Workers follow FEFO order for picking items
  • Reduces waste from expired products
  • Improves inventory turnover rate
Storage Management
  • Place near-expiry items in accessible locations
  • Organize shelves by expiry priority
  • Reduce search time for workers
Promotional Planning
  • Target promotions for high-urgency items
  • Create bundle deals with near-expiry products
  • Schedule flash sales for critical items
Discount Strategy
  • Automatic discount suggestions based on urgency
  • Tiered pricing by days until expiry
  • Clear-out campaigns for critical stock
Freshness Guarantee
  • Ensure customers receive freshest available stock
  • Maintain brand reputation for quality
  • Reduce customer complaints
Compliance
  • Meet food safety regulations
  • Maintain audit trail for expiry management
  • Document proper rotation procedures
Waste Reduction
  • Minimize losses from expired inventory
  • Improve profit margins
  • Better cash flow management
Inventory Valuation
  • More accurate stock valuation
  • Identify potential write-offs early
  • Optimize purchasing decisions

Integration with Other Features

Alert System

FEFO integrates with the alert system to notify relevant personnel:
// Critical items trigger immediate alerts
if (daysUntilExpiry <= 3) {
  // Send critical alert
  createAlert({
    type: 'expiring_critical',
    severity: 'CRITICAL',
    productId: product.id,
    message: `${product.productName} expires in ${daysUntilExpiry} days`
  });
}

Task Management

Automatic task creation for near-expiry items:
// Create task for critical items
if (urgency === 'critical') {
  createTask({
    description: `Urgent: Process ${productName} - expires in ${daysUntilExpiry} days`,
    assignedTo: warehouseManagerId,
    priority: 'HIGH'
  });
}

Best Practices

1

Daily Review

Check FEFO ordering every morning to identify critical items
2

Physical Arrangement

Arrange warehouse shelves to match FEFO priority
3

Staff Training

Train workers to always follow FEFO sequence
4

Regular Audits

Verify FEFO compliance through periodic audits
5

Automated Actions

Set up automatic alerts and tasks for critical items
Never override FEFO ordering unless there’s a valid quality or safety reason. Document all exceptions.

Performance Metrics

Track these KPIs to measure FEFO effectiveness:
MetricTargetMeasurement
Waste Rate< 2%(Expired units / Total units) × 100
FEFO Compliance> 95%(FEFO picks / Total picks) × 100
Average Days to Expiry> 7 daysAverage across all sold items
Critical Items< 5Items with ≤3 days until expiry

Limitations

Be aware of these constraints:
FEFO doesn’t account for quality differences between batches. Some newer batches may be higher quality than older ones.
Sudden demand spikes may require deviating from strict FEFO order.
Items from different suppliers with same expiry date need additional sorting logic.

Alert System

Expiry-based notifications

Demand Forecasting

Predict consumption rates

Inventory Management

Manage expiry dates

Task Management

Assign expiry-related tasks

Build docs developers (and LLMs) love