Skip to content

Developer notes

Table Content
?:kv_ia_items Current state per tracked product (latest snapshot): stock, on hand, unit cost + source, price, value, warehouse split flag, units and revenue in the ABC period, units in the velocity window, average daily sales, days of cover, ABC classes (store-wide and within the company), last sale, creation time, lead time, on order, reorder point and quantity.
?:kv_ia_item_wh Stock per warehouse of products split by the core Warehouses add-on.
?:kv_ia_sales Units and line revenue per product and shop day (orders in the counted statuses, Multi-Vendor parent orders skipped).
?:kv_ia_snapshots Daily totals per company (company_id 0 = everything): products, in stock, out of stock, units, value, estimated value, dead stock value.
?:kv_ia_snapshot_groups Daily value per category (kind c) and warehouse (kind w).
?:kv_ia_stockouts Stockout periods (end_day 0 = still out).
?:kv_ia_filters Saved filters (user, company, JSON parameters).
?:kv_ia_meta Snapshot state (phase, cursor, days), last cron run, first snapshot day.

Days are shop days YYYYMMDD in the store time zone (Settings → Appearance).

Tygh\Addons\KvInventoryAnalytics\Snapshot::run($budget) works for $budget seconds and is resumable:

  1. sales: rebuilds ?:kv_ia_sales day by day (15 days per step) from ?:orders (index on timestamp) + ?:order_details. The first run reads history_days, later runs the last lookback_days.
  2. items: 500 products per step (product_id > cursor), tracked products only (tracking != D, global inventory tracking on, Multi-Vendor common products with company_id = 0 skipped). Costs (Costs::resolve()), warehouse split, sales metrics, reorder (Calc), stockout open/close.
  3. finish: removes rows of products deleted or untracked since, ABC classes (Calc::abc()), daily totals and groups, retention.

A MySQL named lock (GET_LOCK) prevents parallel runs. Re-running the same day replaces the day’s rows. Report pages only read ?:kv_ia_* (plus product, category and company names of the rows on screen).

Entry points: cron kv_inventory_analytics.cron (console or HTTP with the core cron password), admin AJAX kv_inventory_analytics.snapshot (POST, restart=Y starts over).

  • value(amount, unit_cost) = max(0, amount) × max(0, cost), rounded to cents.
  • abc(values, a, b): rank by value (ties by id); an item is A while the cumulative share before it is below a, B below b, else C; zero values are C.
  • avgDaily(qty, window, age): qty / min(window, age) when the product is younger than the window.
  • daysOfCover(on_hand, avg): on hand / avg; 0 when out of stock; null (infinite) when nothing sells.
  • reorder(avg, on_hand, on_order, lead, safety, review): see Configuration.

Unit tests: php tests/unit_pure.php (no store needed).

  • schemas/permissions/admin.post.php: View reports for the reports, export and filters; Manage catalog for the snapshot.
  • schemas/permissions/vendor_multivendor.post.php: each vendor mode listed; snapshot and cron denied. The controller forces the vendor’s company on every query (runtime.company_id).
  • schemas/permissions/trusted_controllers.post.php: only the cron mode, which checks the console or the cron password itself.
  • Controller index.post.php assigns the dashboard card data; templates hooks/index/simple_dashboard_content.pre.tpl (default dashboard) and hooks/index/products_statistic.post.tpl (classic dashboard).
  • No storefront templates, no storefront hooks.

License\Client::isActive() gates reports, export, the dashboard card and snapshots (Config::isOn()). Without a licence the pages show a switched-off panel; data is kept.