u
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/** Drop order = reverse of schema create dependency order. */
|
||||
export const TABLES = [
|
||||
|
||||
'deliverysummary',
|
||||
'shipmentsummary',
|
||||
'quotesummary',
|
||||
'ordersummary',
|
||||
|
||||
@@ -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;
|
||||
`;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { translateQueries } from './translate.js';
|
||||
|
||||
const queries = translateQueries(source, 'article');
|
||||
export const {
|
||||
ADD_CATEGORY, ADD_EAN, ARTICLE_LIST_COLUMNS, CHECK_HAS_WAREHOUSE, CREATE, CREATE_CHILD,
|
||||
ADD_CATEGORY, ADD_EAN, ARTICLE_LIST_COLUMNS, CHECK_ARTICLE_WAREHOUSE_STOCKS, CHECK_HAS_WAREHOUSE, CREATE, CREATE_CHILD,
|
||||
DELETE_CATEGORIES_BY_ARTICLE, DELETE_EANS_BY_ARTICLE, EAN_EXISTS, EAN_EXISTS_FOR_OTHER_ARTICLE,
|
||||
GET_BRAND_BY_COMPANY_AND_ID,
|
||||
GET_CATEGORY_ID_BY_SKU, GET_CATEGORY_TREE_FLAT, GET_ID_BY_SKU,
|
||||
|
||||
@@ -23,6 +23,21 @@ FROM warehouse
|
||||
WHERE id = @warehouseId AND company_id = @companyId;
|
||||
`;
|
||||
|
||||
export const GET_DEFAULT_WAREHOUSE = `
|
||||
SELECT id, name
|
||||
FROM warehouse
|
||||
WHERE company_id = @companyId
|
||||
ORDER BY sort ASC, name ASC, id ASC
|
||||
LIMIT 1;
|
||||
`;
|
||||
|
||||
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
|
||||
|
||||
@@ -931,6 +931,42 @@ CREATE INDEX IF NOT EXISTS IX_shipmentsummary_company_order
|
||||
ON shipmentsummary (company_id, order_id);
|
||||
`;
|
||||
|
||||
export const INIT_DELIVERYSUMMARY = `
|
||||
CREATE TABLE IF NOT EXISTS deliverysummary (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
manifest_id INTEGER NOT NULL UNIQUE,
|
||||
delivery_number TEXT NOT NULL,
|
||||
external_number TEXT,
|
||||
delivery_date TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (${UTC_NOW}),
|
||||
supplier_id INTEGER NULL,
|
||||
supplier_name TEXT NULL,
|
||||
positions INTEGER NOT NULL DEFAULT 0,
|
||||
gross_total REAL NOT NULL DEFAULT 0,
|
||||
net_total REAL NOT NULL DEFAULT 0,
|
||||
total_quantity REAL NOT NULL DEFAULT 0,
|
||||
stock_positions INTEGER NOT NULL DEFAULT 0,
|
||||
booked_positions INTEGER NOT NULL DEFAULT 0,
|
||||
unbooked_positions INTEGER NOT NULL DEFAULT 0,
|
||||
stock_quantity REAL NOT NULL DEFAULT 0,
|
||||
booked_quantity REAL NOT NULL DEFAULT 0,
|
||||
unbooked_quantity REAL NOT NULL DEFAULT 0,
|
||||
is_booked INTEGER NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE NO ACTION,
|
||||
FOREIGN KEY (manifest_id) REFERENCES manifest(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (supplier_id) REFERENCES entity(id) ON DELETE SET NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS IX_deliverysummary_company_date
|
||||
ON deliverysummary (company_id, delivery_date DESC, manifest_id DESC);
|
||||
CREATE INDEX IF NOT EXISTS IX_deliverysummary_company_number
|
||||
ON deliverysummary (company_id, delivery_number);
|
||||
CREATE INDEX IF NOT EXISTS IX_deliverysummary_company_booked
|
||||
ON deliverysummary (company_id, is_booked);
|
||||
CREATE INDEX IF NOT EXISTS IX_deliverysummary_company_supplier
|
||||
ON deliverysummary (company_id, supplier_id);
|
||||
`;
|
||||
|
||||
export const INIT_MCP_CLIENT = `
|
||||
CREATE TABLE IF NOT EXISTS mcp_client (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -1042,6 +1078,7 @@ export const SCHEMA_SCRIPTS = [
|
||||
INIT_ORDERSUMMARY,
|
||||
INIT_QUOTESUMMARY,
|
||||
INIT_SHIPMENTSUMMARY,
|
||||
INIT_DELIVERYSUMMARY,
|
||||
INIT_MCP_CLIENT,
|
||||
INIT_MCP_AUTH_CODE,
|
||||
INIT_MCP_TOKEN,
|
||||
|
||||
Reference in New Issue
Block a user