Batch Tracking: ERPNext DocType For Perishable Goods

Alex Johnson
-
Batch Tracking: ERPNext DocType For Perishable Goods

This document outlines the creation of a Batch Number DocType within ERPNext, specifically tailored for tracking perishable items. This is crucial for maintaining food safety, ensuring traceability, and complying with relevant regulations. Let's dive into the details of implementing this important feature.

Goal

The primary goal is to implement robust batch/lot number tracking within ERPNext, incorporating expiration dates to effectively manage perishable goods. This system will provide comprehensive visibility into the lifecycle of each batch, facilitating efficient stock management and rapid response to potential issues.

Tasks

To achieve this goal, the following tasks need to be completed:

  • [x] Create Batch Number DocType JSON
    • batch_id (Data, unique, required)
    • product (Link to Product, required)
    • department (Link to Department, required)
    • manufacturing_date (Date, optional)
    • expiration_date (Date, optional)
    • quantity (Float, read-only) - calculated from ledger
    • status (Select: Active, Expired, Consumed)
  • [x] Create Batch Number controller
    • Auto-generate batch_id if not provided
    • Validation: expiration_date > manufacturing_date
    • Auto-update status based on expiration_date
    • Calculate quantity from Stock Ledger Entries
  • [x] Add batch naming series
    • Format: {product_code}-{YYYY}-{####}
    • Example: "TOMATO-2025-0001"
  • [x] Add permissions
    • Department-restricted
    • Read-only quantity field
  • [x] Write unit tests
    • Test batch creation
    • Test expiration status
    • Test quantity calculation

Deep Dive into Batch Number DocType JSON

The Batch Number DocType will serve as the foundation for our batch tracking system. Each field is carefully designed to capture essential information about a specific batch of perishable goods. Let's break down each field:

  • batch_id (Data, unique, required): This is a unique identifier for each batch. Ensuring uniqueness is paramount to avoid confusion and maintain accurate tracking. This field will likely be a string or integer value, depending on the chosen naming convention. The system should enforce the uniqueness constraint to prevent duplicate entries.

  • product (Link to Product, required): This field establishes a link to the 'Product' DocType, allowing us to associate each batch with a specific product. This link ensures that we know exactly what item the batch contains. Proper configuration of this link is essential for accurate inventory management and reporting. The 'Product' DocType should contain relevant details about the product, such as name, description, and unit of measure.

  • department (Link to Department, required): Linking each batch to a specific 'Department' is crucial for access control and accountability. This ensures that only authorized personnel within the relevant department can manage and view batch information. The 'Department' DocType should define the organizational structure within the company, facilitating proper data segregation and security.

  • manufacturing_date (Date, optional): While optional, capturing the manufacturing date provides valuable insights into the batch's history. This date indicates when the batch was produced, aiding in shelf-life management and traceability. If provided, this date will be used in conjunction with the expiration date to determine the batch's status.

  • expiration_date (Date, optional): This is arguably one of the most critical fields, as it determines the shelf life of the perishable goods. The expiration date is used to automatically update the batch's status, ensuring that expired batches are flagged and handled appropriately. The system should provide alerts or notifications when batches are approaching their expiration dates.

  • quantity (Float, read-only): The quantity field represents the current quantity of the batch in stock. This field is read-only and is calculated dynamically from Stock Ledger Entries. This ensures that the quantity accurately reflects the actual inventory levels, preventing discrepancies and improving inventory control.

  • status (Select: Active, Expired, Consumed): This field indicates the current status of the batch. The available options are 'Active', 'Expired', and 'Consumed'. The system automatically updates this status based on the expiration date and stock ledger entries. This provides a clear and concise overview of the batch's usability.

Batch Number Controller Logic

The Batch Number controller will handle the business logic associated with the Batch Number DocType. This includes auto-generating batch IDs, validating data, updating statuses, and calculating quantities.

  • Auto-generate batch_id if not provided: To simplify data entry and ensure consistency, the controller should automatically generate a unique batch ID if one is not provided manually. This can be achieved using a naming series or a custom algorithm.

  • Validation: expiration_date > manufacturing_date: This validation rule ensures that the expiration date is always later than the manufacturing date. This prevents illogical data entries and maintains data integrity.

  • Auto-update status based on expiration_date: The controller should automatically update the batch status based on the current date and the expiration date. If the current date is past the expiration date, the status should be updated to 'Expired'.

  • Calculate quantity from Stock Ledger Entries: The controller should calculate the quantity of the batch based on the Stock Ledger Entries associated with it. This ensures that the quantity accurately reflects the actual stock levels.

Batch Naming Series Configuration

A well-defined naming series is essential for generating consistent and easily identifiable batch IDs. The proposed format is {product_code}-{YYYY}-{####}, where:

  • {product_code} is the code of the product associated with the batch.
  • {YYYY} is the year of the batch's creation.
  • {####} is a sequential number, padded with leading zeros.

For example, if we are creating a batch of tomatoes in 2025, the batch ID might be "TOMATO-2025-0001". This format provides a clear and informative identifier for each batch.

Implementing Permissions

Proper permissions are crucial for ensuring data security and access control. The following permissions should be implemented:

  • Department-restricted: Access to batch information should be restricted to users within the relevant department. This ensures that only authorized personnel can view and manage batch data.

  • Read-only quantity field: The quantity field should be read-only for all users, as it is calculated automatically from Stock Ledger Entries. This prevents accidental modifications and maintains data integrity.

Unit Testing Strategy

Thorough unit testing is essential for verifying the correctness and reliability of the batch tracking system. The following unit tests should be implemented:

  • Test batch creation: This test verifies that batches can be created successfully with valid data.

  • Test expiration status: This test verifies that the expiration status is updated correctly based on the expiration date.

  • Test quantity calculation: This test verifies that the quantity is calculated accurately from Stock Ledger Entries.

Acceptance Criteria

The following acceptance criteria must be met before the batch tracking system can be considered complete:

  • ✅ Batch Number DocType created
  • ✅ Auto-naming works correctly
  • ✅ Expiration status updates automatically
  • ✅ Tests pass

Technical Notes

  • Batch numbers are immutable once created. This ensures that the batch ID remains consistent throughout the batch's lifecycle.
  • Quantity is calculated from Stock Ledger Entries (read-only). This prevents manual modifications and ensures data accuracy.
  • Status auto-updates daily via scheduled job. This ensures that the batch status is always up-to-date.
  • Critical for food safety compliance and recalls. This highlights the importance of the batch tracking system for regulatory compliance and efficient recall management.

References

By implementing this Batch Number DocType and its associated features, ERPNext users can effectively manage perishable goods, ensure food safety, and comply with relevant regulations. The system provides comprehensive visibility into the lifecycle of each batch, facilitating efficient stock management and rapid response to potential issues. For more information on ERPNext and its features, you can check the official ERPNext documentation at https://docs.erpnext.com/.

You may also like