Skip to content

About

A stock-movement ledger and date-based inventory report for Magento 2 MSI. It records every change to a product's source-item quantity or stock status - what changed, when, why, who did it, and which order caused it - and answers the question "how much stock did product X have on date D?"

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

WB_InventoryLog

Version: 1.0.0 Compatibility: Magento 2.4.8-p5 (tested) · PHP 8.1 / 8.2 / 8.3 (tested on 8.3) · Magento MSI required License: MIT

A stock-movement ledger and date-based inventory report for Magento 2 MSI. It records every change to a product's source-item quantity or stock status - what changed, when, why, who did it, and which order caused it - and answers the question "how much stock did product X have on date D?"

InventoryLog demo

Features

  • Movement ledger (wb_inventory_log): old qty, qty change, new qty, in-stock status before/after, event type, reference (e.g. order increment id), message, actor (admin / storefront / API / cron / system) and admin username.
  • "Inventory log" link on the product edit page, under Quantity, opening a Stock Movement modal for that product (Created At, Message, Is In Stock?, Old Qty, Qty Change, New Qty, By, Username).
  • Movement Log grid - Catalog > WB Inventory > Movement Log: filter by SKU, event, actor type, capture layer, reference, date range and quantities; CSV / XML export.
  • Stock on Date report - Catalog > WB Inventory > Stock on Date: set the As of Date filter to see every product's quantity at the end of that day next to its current quantity, the change since, and the last movement before that date. CSV / XML export.
  • Three capture layers (plugins, optional DB triggers, nightly reconciliation) so the ledger matches reality.
  • Daily closing-stock snapshot (wb_inventory_snapshot) and configurable retention.
  • Logging never blocks an order or a stock save: failures are caught and written to var/log/system.log.

Requirements

  • Magento 2.4.x with MSI (Magento_Inventory* modules enabled). Stores running without MSI are not supported.
  • MySQL 8 / MariaDB equivalent. The optional triggers need the TRIGGER privilege (Magento's own Update by Schedule indexers create triggers too, so the Magento DB user usually has it already).
  • A running Magento cron (cron:run) for the nightly reconcile / snapshot / prune jobs.

Installation

Copy the module into app/code/WB/InventoryLog (the folder must be named exactly that), or require it from a VCS repository with Composer, then:

bin/magento module:enable WB_InventoryLog
bin/magento setup:upgrade
bin/magento setup:di:compile            # production mode
bin/magento cache:flush

# One-time: opening balance for every source item + first snapshot (REQUIRED, see below)
bin/magento wb:inventory-log:baseline

# Optional: also log raw SQL changes (needs the DB user's TRIGGER privilege)
bin/magento wb:inventory-log:triggers --install
  • New stock changes are logged automatically as soon as the module is enabled.
  • wb:inventory-log:baseline is required once per environment. Without it, Stock on Date is blank for a product until its first stock change. (The nightly job would add baselines later, but not for SKUs that already changed.)
  • wb:inventory-log:triggers --install is optional. The Log Direct Database Changes setting only switches installed triggers on or off; it never creates them. Without triggers, raw SQL changes are still found by the nightly reconciliation (logged as "Reconciliation" at reconcile time instead of the real moment).
  • These steps are also listed inside the admin under Stores > Configuration > WB > Inventory Log > Setup Notes.
  • Enable state lives in app/etc/config.php; repeat module:enable on every environment if that file is not deployed.

How it works

Capture layers

  1. Plugins (primary, with full context). around plugins on the core funnels that move stock:

    • Magento\InventoryApi\Api\SourceItemsSaveInterface::execute - absolute writes: admin product save, REST/API, product import, legacy-stock synchronisation.
    • Magento\Inventory\Model\SourceItem\Command\DecrementSourceItemQty::execute - the raw-SQL decrement used when stock is deducted for orders, cancellations, credit memos and shipments (it bypasses SourceItemsSave).
    • Magento\InventorySourceDeductionApi\Model\SourceDeductionServiceInterface::execute - publishes the sales event (type + order id) so the decrement is labelled e.g. "Product ordered (order: 000000051)".
    • Magento\InventoryApi\Api\SourceItemsDeleteInterface::execute - stock removal (product deletion).
    • Magento\ImportExport\Model\Import::importSource - flags stock written by System > Import.

    Each plugin reads the quantity/status before, lets the write happen, reads it after, and logs the real difference. Saves that change nothing are not logged.

  2. MySQL triggers (optional safety net). AFTER INSERT/UPDATE/DELETE triggers on inventory_source_item (wb_il_source_item_ai, _au, _ad) log raw SQL changes as Direct DB change. Plugins set the session variable @wb_il_skip while they write, so a change is never logged twice. The triggers stay silent when Enable Inventory Log or Log Direct Database Changes is off in the default-scope config.

  3. Nightly reconciliation. Compares every source item with its last ledger row; any mismatch is logged as Reconciliation. This works even where triggers cannot be installed.

Stock on Date

Quantity at the end of day D (admin timezone converted to UTC) is the new_qty of the latest ledger row at or before that moment per SKU/source. A product with no ledger row up to that moment has no history for that date (shown blank), e.g. dates before logging started. Results are exact from the baseline date forward.

The "latest row" is chosen by the highest log_id inside the time window, which is correct because rows are only ever appended in time order. Do not back-fill ledger rows with older timestamps.

Reservations

The module logs the source-item quantity (what MSI stores in inventory_source_item), not salable quantity or reservations. It was developed and tested on a store where stock reservations are disabled and quantity is deducted from the source item at order placement. With default MSI behaviour (reservations at order placement, source deduction at shipment) the quantity is logged when the shipment deducts it; that setup has not been tested yet.

Events logged

Event When
Order placed / canceled / failed MSI sales events passing through the source-deduction service
Credit memo / Shipment / Invoice MSI sales events
Admin edit Stock changed from the admin panel
API REST / SOAP / GraphQL stock updates
Import System > Import runs
Mobile app Requests whose path contains mobileassistant (Biztech Mobile Assistant endpoints)
Scheduled job Stock changed while running under cron
Stock created / removed First source item for a SKU / source item deleted
Direct DB change Raw SQL on inventory_source_item (needs the triggers)
Reconciliation Nightly: stock changed outside every tracked path
Baseline Opening balance written when logging starts

Console commands

Command Purpose
wb:inventory-log:baseline Seed an opening-balance row for every source item without history, then take the first snapshot
wb:inventory-log:reconcile Log baseline/drift rows now (what the nightly job does first); expect 0 drift on a healthy store
wb:inventory-log:snapshot Write today's closing snapshot (re-running the same day overwrites it)
wb:inventory-log:prune Apply the configured retention now
wb:inventory-log:triggers [--install|--remove] Show (default) / install / drop the direct-DB triggers

Cron (group wb_inventory_log)

Job Schedule Does
wb_inventorylog_nightly 55 23 * * * Reconcile, then snapshot
wb_inventorylog_prune 15 3 * * * Retention cleanup

Configuration

Stores > Configuration > WB > Inventory Log > General

Setting Default Notes
Enable Inventory Log Yes Master switch for plugins, triggers and the nightly job. Existing data and reports stay available
Log Direct Database Changes Yes Makes installed triggers log (or stay silent) without dropping them
Log Retention (days) 0 0 = keep forever. Pruned rows reduce how far back Stock on Date can be rebuilt
Snapshot Retention (days) 730 0 = keep forever

The trigger switches are read from core_config_data at default scope; values set only in env.php / config.php are not seen by the triggers.

The config section uses the shared admin tab 💻 WB (wajahatbashir).

Permissions (ACL)

WB_InventoryLog::inventory (menu group), ::log (Movement Log + product modal), ::stock_on_date, ::config.

Database

  • wb_inventory_log - the ledger (indexes on sku, product_id + created_at, created_at, event_type).
  • wb_inventory_snapshot - one row per snapshot_date + product_id + source_code (unique).
  • Triggers wb_il_source_item_ai, wb_il_source_item_au, wb_il_source_item_ad (optional).

Uninstalling

The module has no automatic uninstall script. Remove the triggers first - they insert into wb_inventory_log, so if the table is dropped while they still exist, every stock write fails:

bin/magento wb:inventory-log:triggers --remove
bin/magento module:disable WB_InventoryLog

Then delete app/code/WB/InventoryLog and, if you also want the data gone:

DROP TABLE wb_inventory_log, wb_inventory_snapshot;

Known limitations

  • Only products that currently have source items appear in Stock on Date; source items whose SKU has no catalog product are ignored by baseline, snapshot and the report.
  • Multi-source stores: the report shows the sum across sources per SKU; the ledger itself keeps source_code.
  • Salable quantity is not logged, only the source-item quantity and status.
  • A change made by raw SQL while no trigger is installed is caught by the nightly reconciliation, but stamped at reconcile time, not the real moment.
  • The Movement Log has an SKU filter but no product-name filter; the Reference column is plain text (no order link).
  • No automatic uninstall script (see above).

Troubleshooting

  • "Stock on Date" is blank for a product - it has no ledger row up to the chosen date. Run wb:inventory-log:baseline (opening balance) and pick a date on/after it.
  • triggers --install fails - the DB user lacks TRIGGER (or, with binary logging on, the privilege/setting MySQL needs for trigger creation). Plugins and nightly reconciliation keep working; ask your host or DBA.
  • Nothing is logged at all - check Enable Inventory Log, that module:status WB_InventoryLog says enabled, and var/log/system.log for lines starting WB_InventoryLog:.
  • Nightly job does not run - the Magento system cron is not running; check cron_schedule for group wb_inventory_log.

Files

WB/InventoryLog/
  registration.php  composer.json  README.md  LICENSE
  Block/Adminhtml/System/Config/SetupNotes
  etc/  module.xml  db_schema.xml  db_schema_whitelist.json  di.xml  acl.xml  config.xml
        crontab.xml  cron_groups.xml  adminhtml/{menu,routes,system,di}.xml
  Model/  Config  Log  Logger  ChangeTracker  ActorResolver  EventResolver
          Context/EventContext  Report/AsOfDate  Source/{EventType,UserType,CaptureType}
          Service/{Reconciler,SnapshotService,Pruner,TriggerManager}
          ResourceModel/{Log,Log/Collection,StockOnDate/Collection}
  Plugin/  SourceItemsSavePlugin  DecrementSourceItemQtyPlugin  SourceDeductionServicePlugin
           SourceItemsDeletePlugin  ImportContextPlugin
  Cron/  Nightly  Prune
  Console/Command/  Baseline  Reconcile  Snapshot  Prune  Triggers
  Controller/Adminhtml/{Log,Report}/Index.php
  Ui/  Component/Listing/{ProductDataProvider,StockOnDateDataProvider}
       DataProvider/Product/Form/Modifier/InventoryLog
  view/adminhtml/  layout/*.xml
                   ui_component/{wb_inventorylog_log_listing,wb_inventorylog_product_listing,
                                 wb_inventorylog_report_listing,product_form}.xml

Verification checklist

bin/magento module:status WB_InventoryLog
bin/magento wb:inventory-log:triggers
bin/magento wb:inventory-log:reconcile        # expect 0 drift on a healthy store

Then: change a product's quantity in admin (a row appears as Admin edit), place and cancel a test order (Product ordered / Product restocked after order cancellation with the order number), open the product's Inventory log link, and run Stock on Date with a past date.

Author

Wajahat Bashir Email: wajahat.bashir786@gmail.com GitHub: wajahatbashir Repository: wajahatbashir/wb-inventory-log

License

MIT © 2026 Wajahat Bashir

About

A stock-movement ledger and date-based inventory report for Magento 2 MSI. It records every change to a product's source-item quantity or stock status - what changed, when, why, who did it, and which order caused it - and answers the question "how much stock did product X have on date D?"

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages