This commit is contained in:
seb
2026-10-16 22:09:12 +02:00
parent 796f6a3272
commit 7635821dad
38 changed files with 6191 additions and 3350 deletions

View File

@@ -1,5 +1,7 @@
/** Drop order = reverse of schema create dependency order. */
export const TABLES = [
'deliverysummary',
'shipmentsummary',
'quotesummary',
'ordersummary',

View File

@@ -658,3 +658,21 @@ SELECT TOP 1 id
FROM warehouse
WHERE company_id = @companyId;
`;
export const CHECK_ARTICLE_WAREHOUSE_STOCKS = `
SELECT w.id AS warehouseId, w.name AS warehouseName, COALESCE(stock_calc.stock, 0) AS stock
FROM warehouse w
LEFT JOIN (
SELECT l.warehouse_id, SUM(l.amount) AS stock
FROM ledger l
INNER JOIN manifestarticle ma ON ma.id = l.manifestarticle_id
INNER JOIN manifest m ON m.id = ma.manifest_id
WHERE m.company_id = @companyId
AND (
ma.sku = @sku
OR ma.sku IN (SELECT asup.sku FROM articlesupplier asup WHERE asup.article_id = @articleId)
)
GROUP BY l.warehouse_id
) stock_calc ON stock_calc.warehouse_id = w.id
WHERE w.company_id = @companyId;
`;

View File

@@ -23,6 +23,20 @@ FROM warehouse
WHERE id = @warehouseId AND company_id = @companyId;
`;
export const GET_DEFAULT_WAREHOUSE = `
SELECT TOP 1 id, name
FROM warehouse
WHERE company_id = @companyId
ORDER BY sort ASC, name ASC, id ASC;
`;
export const CHECK_MANIFESTARTICLE_BOOKED = `
SELECT COALESCE(SUM(amount), 0) AS bookedAmount
FROM ledger
WHERE manifestarticle_id = @manifestarticleId
AND warehouse_id IS NULL;
`;
export const FIND_ARTICLES_FOR_HISTORY = `
SELECT DISTINCT asup.article_id AS articleId
FROM articlesupplier asup

View File

@@ -1278,6 +1278,49 @@ BEGIN
END
`;
export const INIT_DELIVERYSUMMARY = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'deliverysummary')
BEGIN
CREATE TABLE deliverysummary (
id INT IDENTITY(1,1) PRIMARY KEY,
company_id INT NOT NULL,
manifest_id INT NOT NULL CONSTRAINT UQ_deliverysummary_manifest UNIQUE,
delivery_number NVARCHAR(100) NOT NULL,
external_number NVARCHAR(100) NULL,
delivery_date DATETIME2 NOT NULL,
created_at DATETIME2 NOT NULL CONSTRAINT DF_deliverysummary_created_at DEFAULT SYSUTCDATETIME(),
supplier_id INT NULL,
supplier_name NVARCHAR(255) NULL,
positions INT NOT NULL CONSTRAINT DF_deliverysummary_positions DEFAULT 0,
gross_total FLOAT NOT NULL CONSTRAINT DF_deliverysummary_gross_total DEFAULT 0,
net_total FLOAT NOT NULL CONSTRAINT DF_deliverysummary_net_total DEFAULT 0,
total_quantity FLOAT NOT NULL CONSTRAINT DF_deliverysummary_total_quantity DEFAULT 0,
stock_positions INT NOT NULL CONSTRAINT DF_deliverysummary_stock_positions DEFAULT 0,
booked_positions INT NOT NULL CONSTRAINT DF_deliverysummary_booked_positions DEFAULT 0,
unbooked_positions INT NOT NULL CONSTRAINT DF_deliverysummary_unbooked_positions DEFAULT 0,
stock_quantity FLOAT NOT NULL CONSTRAINT DF_deliverysummary_stock_quantity DEFAULT 0,
booked_quantity FLOAT NOT NULL CONSTRAINT DF_deliverysummary_booked_quantity DEFAULT 0,
unbooked_quantity FLOAT NOT NULL CONSTRAINT DF_deliverysummary_unbooked_quantity DEFAULT 0,
is_booked TINYINT NOT NULL CONSTRAINT DF_deliverysummary_is_booked DEFAULT 0,
CONSTRAINT FK_deliverysummary_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE NO ACTION,
CONSTRAINT FK_deliverysummary_manifest FOREIGN KEY (manifest_id) REFERENCES manifest(id) ON DELETE CASCADE,
CONSTRAINT FK_deliverysummary_supplier FOREIGN KEY (supplier_id) REFERENCES entity(id) ON DELETE SET NULL
);
CREATE INDEX IX_deliverysummary_company_date
ON deliverysummary (company_id, delivery_date DESC, manifest_id DESC);
CREATE INDEX IX_deliverysummary_company_number
ON deliverysummary (company_id, delivery_number);
CREATE INDEX IX_deliverysummary_company_booked
ON deliverysummary (company_id, is_booked);
CREATE INDEX IX_deliverysummary_company_supplier
ON deliverysummary (company_id, supplier_id);
END
`;
export const INIT_MCP_CLIENT = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'mcp_client')
BEGIN
@@ -1403,6 +1446,7 @@ export const SCHEMA_SCRIPTS = [
INIT_ORDERSUMMARY,
INIT_QUOTESUMMARY,
INIT_SHIPMENTSUMMARY,
INIT_DELIVERYSUMMARY,
INIT_MCP_CLIENT,
INIT_MCP_AUTH_CODE,
INIT_MCP_TOKEN,