texasstarbc

Codeunits - Business Logic

This document describes the codeunits that implement the core business logic for Texas Star Nuts operations.

Overview

The extension includes 9 codeunits organized by functional area:

  1. TSN Event Subscriber (50004) - Primary event handler
  2. TSN Functions (50007) - Utility and tracking functions
  3. TSN Warehouse Functions (50008) - Warehouse-specific utilities
  4. TSN WMS Extension (50000) - Warehouse Insight integration
  5. TSN Shelf Life Management (50002) - Expiration validation
  6. TSN Planning Management (50005) - Inventory planning
  7. TSN Item Application Management (50003) - Item ledger management
  8. TSN Miracle Functions (50001) - Miracle ERP integration
  9. TSN Report Helper Functions (50006) - Reporting utilities

1. TSN Event Subscriber (Codeunit 50004)

Location: src/codeunits/TSNEventSubscriber.Codeunit.al

Purpose

Primary event handler that intercepts standard Business Central processes to inject custom logic. This is the core integration point for most custom functionality.

Key Responsibilities

A. Warehouse Shipment Processing

B. Sales Order Management

C. Purchase Order Processing

D. Item Tracking

E. Production Order Management

Key Event Subscribers

Event: OnAfterCreateWhseShipmentHeaderFromWhseRequest

Trigger: When warehouse shipment is created from warehouse request Action:

// Pseudo-code example
OnAfterCreateWhseShipmentHeaderFromWhseRequest(WarehouseShipmentHeader)
begin
    // Generate BOL number
    WarehouseShipmentHeader."Bill of Lading No." := GetNextBOLNumber();

    // Initialize TrueCommerce fields
    WarehouseShipmentHeader.TRCBillofLading := WarehouseShipmentHeader."Bill of Lading No.";
    WarehouseShipmentHeader.TRCUDF2 := WarehouseShipmentHeader."Bill of Lading No.";
end;

Event: OnBeforeWhseShptHeaderModify

Trigger: Before warehouse shipment header is modified Action:

// Pseudo-code example
OnBeforeWhseShptHeaderModify(WarehouseShipmentHeader)
begin
    // Sync seal number
    if WarehouseShipmentHeader."Seal No." <> '' then
        WarehouseShipmentHeader.TRCUDF3 := WarehouseShipmentHeader."Seal No.";

    // Sync trailer reference
    if WarehouseShipmentHeader."Trailer Reference" <> '' then begin
        WarehouseShipmentHeader.TRCTrailerNumber := WarehouseShipmentHeader."Trailer Reference";
        WarehouseShipmentHeader.TRCCarrierPro := WarehouseShipmentHeader."Trailer Reference";
    end;

    // Sync shipping agent code
    if WarehouseShipmentHeader."Shipping Agent Code" <> '' then
        WarehouseShipmentHeader.TRCUDF5 := WarehouseShipmentHeader."Shipping Agent Code";
end;

Event: OnAfterWhseShipmentLineInsert

Trigger: After warehouse shipment line is inserted Action:

// Pseudo-code example
OnAfterWhseShipmentLineInsert(WarehouseShipmentLine)
begin
    if SalesHeader.Get(SalesHeader."Document Type"::Order, WarehouseShipmentLine."Source No.") then begin
        SalesHeader.CalcFields("TSN Total Quantity");

        // Determine ASN Type based on quantity
        if SalesHeader."TSN Total Quantity" > 25 then
            SalesHeader.TRCASNType := SalesHeader.TRCASNType::"Pallet Pick and Pack"
        else
            SalesHeader.TRCASNType := SalesHeader.TRCASNType::"Pick and Pack";

        SalesHeader.Modify();
    end;
end;

Event: OnBeforePostWhseShipment

Trigger: Before warehouse shipment is posted Action:

Event: OnAfterRegisterPick

Trigger: After pick is registered Action:

Event: OnBeforeProductionOrderStatusChange

Trigger: Before production order status changes Action:


2. TSN Functions (Codeunit 50007)

Location: src/codeunits/TSNFunctions.Codeunit.al

Purpose

General-purpose utility functions for lot tracking, barcode generation, and data access control.

Key Functions

A. Item Tracking Functions

SyncRegisteredPicksWithTracking()

Purpose: Synchronizes registered pick quantities with item tracking specifications Parameters:

SuggestLotNoByLocation()

Purpose: Suggests lot numbers for picking based on location and FEFO logic Parameters:

GetItemTracking()

Purpose: Retrieves item tracking information for a document line Parameters:

B. Financial Data Access Control

CanViewFinancialData()

Purpose: Determines if current user can view financial information Returns: Boolean Logic:

C. Global Group Management

GetGlobalGroupCaption()

Purpose: Retrieves the custom caption for a global group Parameters:

ValidateGlobalGroupValue()

Purpose: Validates a global group value exists Parameters:

D. Barcode Generation

GenerateBarcode()

Purpose: Generates barcodes for labels and documents Parameters:

Supported formats:

Usage Examples

Example 1: Suggest Lot for Picking

// Suggest best lot for item at location MAIN
LotNo := TSNFunctions.SuggestLotNoByLocation('NUT-ALMOND-1000', '', 'MAIN', '');
// Returns lot with earliest expiration (FEFO)

Example 2: Check Financial Data Access

// Hide cost fields if user doesn't have access
if not TSNFunctions.CanViewFinancialData() then
    CurrPage.UnitCost.Visible := false;

Example 3: Get Global Group Caption

// Get caption for Global Group 1
GroupCaption := TSNFunctions.GetGlobalGroupCaption(1);
// Returns "Product Category" (if configured) or "Global Group 1"

3. TSN Warehouse Functions (Codeunit 50008)

Location: src/codeunits/TSNWarehouseFunctions.Codeunit.al

Purpose

Specialized functions for warehouse operations.

Key Functions

GetNextBOLNumber()

Purpose: Generates next Bill of Lading number from number series Returns: Next BOL number Logic:

Usage:

BOLNo := TSNWarehouseFunctions.GetNextBOLNumber();
WarehouseShipmentHeader."Bill of Lading No." := BOLNo;

ValidateBOLNumber()

Purpose: Validates BOL number format and uniqueness Parameters:

CalculateShipmentWeight()

Purpose: Calculates total weight for warehouse shipment Parameters:

CalculateShipmentVolume()

Purpose: Calculates total volume/cubage for warehouse shipment Parameters:


4. TSN WMS Extension (Codeunit 50000)

Location: src/codeunits/TSNWMSExtension.Codeunit.al

Purpose

Integration layer for Warehouse Insight (IWorks) Advanced WMS system.

Key Responsibilities

Key Event Subscribers

Event: OnAfterManualItemReceiptLineCreate

Trigger: After manual warehouse receipt line is created in Warehouse Insight Action:

// Pseudo-code example
OnAfterManualItemReceiptLineCreate(WarehouseReceiptLine, PurchaseLine)
begin
    // Copy alternate lot number
    if PurchaseLine."Alternate Lot No." <> '' then
        WarehouseReceiptLine."Alternate Lot No." := PurchaseLine."Alternate Lot No.";

    // Copy country of origin
    if PurchaseLine."Country of Origin" <> '' then
        WarehouseReceiptLine."Country of Origin" := PurchaseLine."Country of Origin";
end;

Event: OnBeforeLicensePlateCreate

Trigger: Before license plate is created in WMS Action:

Integration Points


5. TSN Shelf Life Management (Codeunit 50002)

Location: src/codeunits/TSNShelfLifeManagement.Codeunit.al

Purpose

Enforces customer-specific shelf life requirements during picking operations. Prevents shipping products that don’t meet minimum shelf life requirements.

Key Functions

ValidateShelfLife()

Purpose: Validates lot meets customer shelf life requirement Parameters:

Event: OnBeforeCreatePick

Trigger: Before pick is created for warehouse shipment Action:

// Pseudo-code example
OnBeforeCreatePick(WarehouseShipmentLine, var TempBinContent)
begin
    CustomerNo := GetCustomerNo(WarehouseShipmentLine);
    ShipmentDate := WarehouseShipmentLine."Shipment Date";

    // Filter bin content to only include lots meeting shelf life
    TempBinContent.SetFilter("Lot No.", '<>%1', '');
    if TempBinContent.FindSet() then
        repeat
            if not ValidateShelfLife(CustomerNo, TempBinContent."Item No.",
                                     TempBinContent."Lot No.", ShipmentDate) then
                TempBinContent.Delete();
        until TempBinContent.Next() = 0;
end;

GetCustomerShelfLifeRequirement()

Purpose: Retrieves shelf life requirement for customer Parameters:

CalculateRemainingShelfLife()

Purpose: Calculates remaining shelf life for a lot Parameters:

Business Impact


6. TSN Planning Management (Codeunit 50005)

Location: src/codeunits/TSNPlanningManagement.Codeunit.al

Purpose

Adjusts inventory planning calculations to exclude quantities in bins marked as “Exclude from Planning.”

Key Functions

Event: OnAfterCalcAvailableQty

Trigger: After available quantity is calculated for planning Action:

// Pseudo-code example
OnAfterCalcAvailableQty(ItemNo, LocationCode, var AvailableQty)
begin
    // Get quantity in excluded bins
    ExcludedQty := CalculateExcludedBinQty(ItemNo, LocationCode);

    // Adjust available quantity
    AvailableQty := AvailableQty - ExcludedQty;
end;

CalculateExcludedBinQty()

Purpose: Calculates quantity in bins excluded from planning Parameters:

Use Cases

1. Quarantine Bins

Bins holding quarantined or hold inventory should not be included in available supply.

2. Samples and Testing

Bins with samples or items reserved for testing shouldn’t be planned.

3. Customer-Consigned Inventory

Bins with customer-owned consignment inventory excluded from planning.


7. TSN Item Application Management (Codeunit 50003)

Location: src/codeunits/TSNItemApplicationManagement.Codeunit.al

Purpose

Manages item application and ledger entry relationships.

Key Responsibilities


8. TSN Miracle Functions (Codeunit 50001)

Location: src/codeunits/TSNMiracleFunctions.Codeunit.al

Purpose

Integration functions for Miracle ERP system.

Key Functions


9. TSN Report Helper Functions (Codeunit 50006)

Location: src/codeunits/TSNReportHelperFunctions.Codeunit.al

Purpose

Utility functions for report generation and data formatting.

Key Functions

FormatAddress()

Purpose: Formats addresses for reports Parameters:

CalculateReportTotals()

Purpose: Calculates totals for report groupings Parameters:

FormatBarcodeForReport()

Purpose: Prepares barcode data for report printing Parameters:


Codeunit Architecture

Event-Driven Design

Most codeunits use event subscriber pattern to integrate with standard BC processes without modifying core code.

Benefits:

Key Event Types:


Summary

The codeunit architecture provides:

  1. Integration: Seamless connection with third-party systems (WMS, EDI)
  2. Automation: Automatic lot number generation, BOL numbering, field synchronization
  3. Validation: Shelf life compliance, planning exclusions, data validation
  4. Utilities: Barcode generation, financial access control, reporting helpers
  5. Flexibility: Event-driven design enables customization without core code changes

See Also: