Initial commit

This commit is contained in:
user
2026-07-12 13:56:05 +04:00
commit 04bf3a4b58
850 changed files with 9057 additions and 0 deletions
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_barcode_exist]') IS NOT NULL
DROP FUNCTION [Functions].[does_barcode_exist];
GO
CREATE FUNCTION [Functions].[does_barcode_exist](
@barcode AS BIGINT
)
RETURNS BIT
AS
BEGIN
IF EXISTS(
SELECT *
FROM [Tables].[products]
WHERE [barcode] = @barcode
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_product_exist_with_name]') IS NOT NULL
DROP FUNCTION [Functions].[does_product_exist_with_name];
GO
CREATE FUNCTION [Functions].[does_product_exist_with_name](
@name AS NVARCHAR(128)
)
RETURNS BIT
AS
BEGIN
IF EXISTS (
SELECT *
FROM [Tables].[products]
WHERE [name] = @name
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_product_exist_with_product_type_id]') IS NOT NULL
DROP FUNCTION [Functions].[does_product_exist_with_product_type_id];
GO
CREATE FUNCTION [Functions].[does_product_exist_with_product_type_id](
@product_type_id AS INT
)
RETURNS BIT
AS
BEGIN
IF EXISTS (
SELECT *
FROM [Tables].[products]
WHERE [product_type_id] = @product_type_id
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_product_type_exist_with_name]') IS NOT NULL
DROP FUNCTION [Functions].[does_product_type_exist_with_name];
GO
CREATE FUNCTION [Functions].[does_product_type_exist_with_name](
@name AS NVARCHAR(128)
)
RETURNS BIT
AS
BEGIN
IF EXISTS (
SELECT *
FROM [Tables].[product_types]
WHERE [name] = @name
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Functions].[product_type_id]') IS NOT NULL
DROP FUNCTION [Functions].[product_type_id];
GO
CREATE FUNCTION [Functions].[product_type_id](
@product_id AS INT
)
RETURNS INT
BEGIN
DECLARE @product_type AS INT;
SET @product_type = (
SELECT [product_type_id]
FROM [Tables].[products]
WHERE [id] = @product_id);
RETURN @product_type;
END
@@ -0,0 +1,26 @@
USE [Database]
IF OBJECT_ID('[Functions].[same_products_join_product_types]') IS NOT NULL
DROP FUNCTION [Functions].[same_products_join_product_types];
GO
CREATE FUNCTION [Functions].[same_products_join_product_types](
@product_type_id AS INT
)
RETURNS TABLE
RETURN
WITH [result] AS (
SELECT [id], [parent_id], [name]
FROM [Tables].[product_types]
WHERE [id] = @product_type_id
)
SELECT [P].[id] AS [product_id],
[P].[barcode] AS [product_barcode],
[P].[product_type_id],
[P].[name] AS [product_name],
[PT].[name] AS [product_type_name]
FROM [result] AS [R]
JOIN [Tables].[products] AS [P]
ON [P].[product_type_id] = [R].[id]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[id] = [P].[product_type_id]
@@ -0,0 +1,34 @@
USE [Database]
IF OBJECT_ID('[Functions].[subgroup_products_join_product_types]') IS NOT NULL
DROP FUNCTION [Functions].[subgroup_products_join_product_types];
GO
CREATE FUNCTION [Functions].[subgroup_products_join_product_types](
@product_type_id AS INT
)
RETURNS TABLE
RETURN
WITH [result] AS
(
SELECT [id], [parent_id], [name]
FROM [Tables].[product_types]
WHERE [id] = @product_type_id
UNION ALL
SELECT [PT].[id], [PT].[parent_id], [PT].[name]
FROM [result] AS [R]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[parent_id] = [R].[id]
)
SELECT [P].[id] AS [product_id],
[P].[barcode] AS [product_barcode],
[P].[product_type_id],
[P].[name] AS [product_name],
[PT].[name] AS [product_type_name]
FROM [result] AS [R]
JOIN [Tables].[products] AS [P]
ON [P].[product_type_id] = [R].[id]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[id] = [P].[product_type_id]
@@ -0,0 +1,16 @@
USE [Database]
IF OBJECT_ID('[Functions].[fill_CHAR_128]') IS NOT NULL
DROP FUNCTION [Functions].[fill_CHAR_128];
GO
CREATE FUNCTION [Functions].[fill_CHAR_128](
@string AS NVARCHAR(128),
@symbol AS NVARCHAR,
@width AS INT
)
RETURNS NVARCHAR(128) AS
BEGIN
RETURN CONCAT(REPLICATE(@symbol, @width - LEN(@string)), @string);
END
GO
@@ -0,0 +1,39 @@
USE [Database]
IF OBJECT_ID('[Functions].[product_with_barcode_stock]') IS NOT NULL
DROP FUNCTION [Functions].[product_with_barcode_stock];
GO
CREATE FUNCTION [Functions].[product_with_barcode_stock](
@barcode AS BIGINT
)
RETURNS INT AS
BEGIN
DECLARE @product_inflow_sum AS INT;
SET @product_inflow_sum = (
SELECT ISNULL(SUM([count]), 0) AS [sum]
FROM [Tables].[product_inflow_records]
WHERE [barcode] = @barcode
);
DECLARE @result AS INT;
WITH [counts] AS (
SELECT [POR].[count] AS [outflow_count], [PWOR].[count] AS [write_off_count]
FROM [Tables].[products] AS [P]
LEFT OUTER JOIN [Tables].[product_outflow_records] AS [POR]
ON [POR].[barcode] = [P].[barcode]
LEFT OUTER JOIN [Tables].[product_write_off_records] AS [PWOR]
ON [PWOR].[barcode] = [P].[barcode]
GROUP BY
GROUPING SETS
(
([POR].[count]),
([PWOR].[count])
)
)
SELECT @result = (
@product_inflow_sum - ISNULL(SUM([outflow_count]), 0) - ISNULL(SUM([write_off_count]), 0)
)
FROM [counts];
RETURN @result;
END
GO