backbone-inventory 0.17.3

Inventory: stock ledger of record (SLE) + moving-average valuation; emits COGS/asset AccountingPost (Delivery/Receipt -> GL)
Documentation
//! The events this module declares, as typed payloads (`events:` in its schema files).
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! Each declaration is a struct here, namespaced under `declared::` so it cannot collide with a
//! hand-written event type, and implements `DeclaredEvent`: its name, version and aggregate, and
//! `record(..)`, which builds the outbox row. An event declared with `on:` is staged by the write
//! hooks of its aggregate, in the write's transaction. One without is staged by hand code:
//! `ctx.stage(event.record(&id, &ctx))` inside a write's hooks, or
//! `backbone_outbox::outbox::stage` in a transaction of its own.
//! Delivery is at least once; a consumer dedupes with `backbone_outbox::inbox::once`.

use backbone_core::write_hooks::DeclaredEvent;
use serde::{Deserialize, Serialize};

/// a purchase receipt is submitted
/// Declared in `inventory.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StockReceived {
    pub receipt_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub warehouse_id: serde_json::Value,
    pub source_po_id: serde_json::Value,
    pub total_value: serde_json::Value,
}

impl DeclaredEvent for StockReceived {
    const NAME: &'static str = "StockReceived";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a delivery note is submitted
/// Declared in `inventory.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StockDelivered {
    pub delivery_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub warehouse_id: serde_json::Value,
    pub source_so_id: serde_json::Value,
    pub total_cogs: serde_json::Value,
}

impl DeclaredEvent for StockDelivered {
    const NAME: &'static str = "StockDelivered";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a stock transfer is submitted
/// Declared in `inventory.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StockMoved {
    pub entry_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub from_warehouse_id: serde_json::Value,
    pub to_warehouse_id: serde_json::Value,
}

impl DeclaredEvent for StockMoved {
    const NAME: &'static str = "StockMoved";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a stock reconciliation is submitted
/// Declared in `inventory.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StockReconciled {
    pub reconciliation_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub warehouse_id: serde_json::Value,
    pub net_difference: serde_json::Value,
}

impl DeclaredEvent for StockReconciled {
    const NAME: &'static str = "StockReconciled";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a landed cost is validated (cost lines allocated, remaining-share revaluation minted)
/// Declared in `inventory.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LandedCostValidated {
    pub lc_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub target_receipt_id: serde_json::Value,
    pub amount_total: serde_json::Value,
    pub revalued_value: serde_json::Value,
}

impl DeclaredEvent for LandedCostValidated {
    const NAME: &'static str = "LandedCostValidated";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a stock move hits confirmed
/// Declared in `stock.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MoveConfirmed {
    pub move_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub item_id: serde_json::Value,
    pub demand_qty: serde_json::Value,
    pub picking_id: serde_json::Value,
}

impl DeclaredEvent for MoveConfirmed {
    const NAME: &'static str = "MoveConfirmed";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a stock move hits assigned (fully reserved)
/// Declared in `stock.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MoveAssigned {
    pub move_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub picking_id: serde_json::Value,
}

impl DeclaredEvent for MoveAssigned {
    const NAME: &'static str = "MoveAssigned";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a stock move hits done (quants flipped, SLE minted)
/// Declared in `stock.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MoveDone {
    pub move_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub item_id: serde_json::Value,
    pub quantity: serde_json::Value,
    pub price_unit: serde_json::Value,
    pub is_inventory: serde_json::Value,
}

impl DeclaredEvent for MoveDone {
    const NAME: &'static str = "MoveDone";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a stock move hits cancel (reservation released)
/// Declared in `stock.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MoveCancelled {
    pub move_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub released_qty: serde_json::Value,
}

impl DeclaredEvent for MoveCancelled {
    const NAME: &'static str = "MoveCancelled";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a transfer's projected state changes (T1 recompute)
/// Declared in `stock.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TransferProjected {
    pub transfer_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub state: serde_json::Value,
    pub previous_state: serde_json::Value,
}

impl DeclaredEvent for TransferProjected {
    const NAME: &'static str = "TransferProjected";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// a partial validate mints the backorder transfer
/// Declared in `stock.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BackorderCreated {
    pub backorder_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub origin_id: serde_json::Value,
}

impl DeclaredEvent for BackorderCreated {
    const NAME: &'static str = "BackorderCreated";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// the scheduler orders on a reordering rule (T11)
/// Declared in `stock.hook.yaml`; staged by hand code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OrderpointTriggered {
    pub orderpoint_id: serde_json::Value,
    pub company_id: serde_json::Value,
    pub item_id: serde_json::Value,
    pub qty_to_order: serde_json::Value,
    pub forecast_qty: serde_json::Value,
}

impl DeclaredEvent for OrderpointTriggered {
    const NAME: &'static str = "OrderpointTriggered";
    const VERSION: i32 = 1;
    const AGGREGATE: &'static str = "";
}

/// The Postgres schemas whose `outbox_events` table this module's staged events are written to.
pub const OUTBOX_SCHEMAS: &[&str] = &[];

/// Create the outbox and inbox tables of [`OUTBOX_SCHEMAS`] (idempotent). The composing service
/// runs it at startup with a role that may create tables, beside its relay; a write that stages an
/// event needs the table to exist.
pub async fn migrate_outbox(pool: &backbone_orm::PgPool) -> anyhow::Result<()> {
    for schema in OUTBOX_SCHEMAS {
        backbone_core::write_hooks::migrate_outbox(pool, schema).await?;
    }
    Ok(())
}