<SkupaExport database="AuroraRetail" generatedUtc="2026-07-11T16:25:45.1189541Z">
  <Objects>
    <Object id="8" schema="audit" name="AuditLog" type="Table" />
    <Object id="5" schema="dbo" name="Categories" type="Table" />
    <Object id="1" schema="dbo" name="Customers" type="Table">
      <Definition><![CDATA[CREATE TABLE dbo.Customers (
  CustomerId INT IDENTITY PRIMARY KEY,
  Email NVARCHAR(256) NOT NULL UNIQUE,
  FullName NVARCHAR(200) NOT NULL,
  CreatedUtc DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME()
);]]></Definition>
    </Object>
    <Object id="16" schema="dbo" name="fn_CustomerTier" type="Function" />
    <Object id="15" schema="dbo" name="fn_OrderTotal" type="Function">
      <Definition><![CDATA[CREATE FUNCTION dbo.fn_OrderTotal (@OrderId INT)
RETURNS DECIMAL(12,2) AS
BEGIN
  RETURN (SELECT SUM(Quantity * UnitPrice)
          FROM dbo.OrderLines WHERE OrderId = @OrderId);
END]]></Definition>
    </Object>
    <Object id="7" schema="dbo" name="Inventory" type="Table" />
    <Object id="3" schema="dbo" name="OrderLines" type="Table">
      <Definition><![CDATA[CREATE TABLE dbo.OrderLines (
  OrderLineId BIGINT IDENTITY PRIMARY KEY,
  OrderId INT NOT NULL REFERENCES dbo.Orders(OrderId),
  ProductId INT NOT NULL REFERENCES dbo.Products(ProductId),
  Quantity INT NOT NULL,
  UnitPrice DECIMAL(10,2) NOT NULL
);]]></Definition>
    </Object>
    <Object id="2" schema="dbo" name="Orders" type="Table">
      <Definition><![CDATA[CREATE TABLE dbo.Orders (
  OrderId INT IDENTITY PRIMARY KEY,
  CustomerId INT NOT NULL REFERENCES dbo.Customers(CustomerId),
  Status TINYINT NOT NULL DEFAULT 0,
  PlacedUtc DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME()
);]]></Definition>
    </Object>
    <Object id="4" schema="dbo" name="Products" type="Table">
      <Definition><![CDATA[CREATE TABLE dbo.Products (
  ProductId INT IDENTITY PRIMARY KEY,
  CategoryId INT NOT NULL REFERENCES dbo.Categories(CategoryId),
  SupplierId INT NOT NULL REFERENCES dbo.Suppliers(SupplierId),
  Sku NVARCHAR(32) NOT NULL UNIQUE,
  Name NVARCHAR(200) NOT NULL,
  ListPrice DECIMAL(10,2) NOT NULL
);]]></Definition>
    </Object>
    <Object id="6" schema="dbo" name="Suppliers" type="Table" />
    <Object id="17" schema="dbo" name="trg_Orders_Audit" type="Trigger">
      <Definition><![CDATA[CREATE TRIGGER dbo.trg_Orders_Audit ON dbo.Orders AFTER INSERT, UPDATE AS
INSERT audit.AuditLog (TableName, KeyValue, ChangedUtc)
SELECT 'Orders', OrderId, SYSUTCDATETIME() FROM inserted;]]></Definition>
    </Object>
    <Object id="12" schema="dbo" name="usp_PlaceOrder" type="StoredProcedure">
      <Definition><![CDATA[CREATE PROCEDURE dbo.usp_PlaceOrder
  @CustomerId INT, @Lines dbo.OrderLineList READONLY
AS
BEGIN
  SET NOCOUNT ON;
  BEGIN TRAN;
    INSERT dbo.Orders (CustomerId) VALUES (@CustomerId);
    DECLARE @OrderId INT = SCOPE_IDENTITY();
    INSERT dbo.OrderLines (OrderId, ProductId, Quantity, UnitPrice)
    SELECT @OrderId, ProductId, Quantity, dbo.fn_CurrentPrice(ProductId)
    FROM @Lines;
    UPDATE i SET OnHand = i.OnHand - l.Quantity
    FROM dbo.Inventory i JOIN @Lines l ON l.ProductId = i.ProductId;
  COMMIT;
END]]></Definition>
    </Object>
    <Object id="13" schema="dbo" name="usp_RestockInventory" type="StoredProcedure" />
    <Object id="9" schema="dbo" name="vw_OpenOrders" type="View">
      <Definition><![CDATA[CREATE VIEW dbo.vw_OpenOrders AS
SELECT o.OrderId, c.FullName, o.PlacedUtc,
       dbo.fn_OrderTotal(o.OrderId) AS OrderTotal
FROM dbo.Orders o
JOIN dbo.Customers c ON c.CustomerId = o.CustomerId
WHERE o.Status IN (0, 1);]]></Definition>
    </Object>
    <Object id="10" schema="dbo" name="vw_ProductCatalog" type="View">
      <Definition><![CDATA[CREATE VIEW dbo.vw_ProductCatalog AS
SELECT p.Sku, p.Name, cat.Name AS Category, s.Name AS Supplier,
       p.ListPrice, i.OnHand
FROM dbo.Products p
JOIN dbo.Categories cat ON cat.CategoryId = p.CategoryId
JOIN dbo.Suppliers s   ON s.SupplierId   = p.SupplierId
JOIN dbo.Inventory i   ON i.ProductId    = p.ProductId;]]></Definition>
    </Object>
    <Object id="14" schema="sales" name="usp_MonthlySalesReport" type="StoredProcedure" />
    <Object id="11" schema="sales" name="vw_CustomerLifetimeValue" type="View" />
  </Objects>
  <Dependencies>
    <Dependency source="10" target="4" kind="SqlExpression" />
    <Dependency source="10" target="5" kind="SqlExpression" />
    <Dependency source="10" target="6" kind="SqlExpression" />
    <Dependency source="10" target="7" kind="SqlExpression" />
    <Dependency source="11" target="1" kind="SqlExpression" />
    <Dependency source="11" target="16" kind="SqlExpression" />
    <Dependency source="11" target="3" kind="SqlExpression" />
    <Dependency source="12" target="2" kind="SqlExpression" />
    <Dependency source="12" target="3" kind="SqlExpression" />
    <Dependency source="12" target="7" kind="SqlExpression" />
    <Dependency source="13" target="6" kind="SqlExpression" />
    <Dependency source="13" target="7" kind="SqlExpression" />
    <Dependency source="14" target="11" kind="SqlExpression" />
    <Dependency source="14" target="9" kind="SqlExpression" />
    <Dependency source="15" target="3" kind="SqlExpression" />
    <Dependency source="16" target="2" kind="SqlExpression" />
    <Dependency source="16" target="3" kind="SqlExpression" />
    <Dependency source="17" target="2" kind="Trigger" label="AFTER INSERT, UPDATE" />
    <Dependency source="17" target="8" kind="Trigger" />
    <Dependency source="2" target="1" kind="ForeignKey" label="FK_Orders_Customers" />
    <Dependency source="3" target="2" kind="ForeignKey" label="FK_OrderLines_Orders" />
    <Dependency source="3" target="4" kind="ForeignKey" label="FK_OrderLines_Products" />
    <Dependency source="4" target="5" kind="ForeignKey" label="FK_Products_Categories" />
    <Dependency source="4" target="6" kind="ForeignKey" label="FK_Products_Suppliers" />
    <Dependency source="7" target="4" kind="ForeignKey" label="FK_Inventory_Products" />
    <Dependency source="9" target="1" kind="SqlExpression" />
    <Dependency source="9" target="15" kind="SqlExpression" />
    <Dependency source="9" target="2" kind="SqlExpression" />
  </Dependencies>
</SkupaExport>