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,22 @@
USE [Database]
IF OBJECT_ID('[Procedures].[get_next_barcode]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[get_next_barcode];
GO
CREATE PROCEDURE [Procedures].[get_next_barcode]
@next_barcode AS BIGINT OUTPUT
AS
BEGIN TRANSACTION;
SET NOCOUNT ON;
SET @next_barcode = (
SELECT CAST([value] AS BIGINT) + 1
FROM [Tables].[additional_variables]
WHERE [id] = 1
);
UPDATE [Tables].[additional_variables]
SET [value] = CAST(@next_barcode AS NVARCHAR(13))
WHERE [id] = 1;
COMMIT TRANSACTION;
GO
@@ -0,0 +1,79 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_or_update_barcode]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_or_update_barcode];
GO
CREATE PROCEDURE [Procedures].[insert_or_update_barcode]
@product_id AS INT,
@barcode_attribute_id AS INT,
@barcode AS BIGINT = NULL,
@record_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
SET NOCOUNT ON;
IF @barcode IS NOT NULL AND EXISTS(
SELECT *
FROM [Tables].[product_values]
WHERE [attribute_id] = @barcode_attribute_id
AND CAST([value] AS BIGINT) = @barcode
)
BEGIN;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
THROW 53000, 'The barcode is not unique', 1;
END;
CREATE TABLE [Tables].[#existing_hash_codes](
[hash_code] NVARCHAR(13)
);
INSERT INTO [#existing_hash_codes]([hash_code])
SELECT [value]
FROM [Tables].[product_attributes] AS [pa]
JOIN [Tables].[product_values] AS [pv]
ON [pv].[product_id] = [pa].[product_id]
AND [pv].[attribute_id] = [pa].[attribute_id]
WHERE [pa].[attribute_id] = @barcode_attribute_id
-- begin creating barcode
IF @barcode IS NULL
EXECUTE [Procedures].[get_next_barcode] @next_barcode = @barcode OUTPUT;
WHILE EXISTS(
SELECT *
FROM [#existing_hash_codes]
WHERE CAST([hash_code] AS BIGINT) = @barcode
)
OR EXISTS(
SELECT *
FROM [Tables].[product_values]
WHERE [attribute_id] = @barcode_attribute_id
AND CAST([value] AS BIGINT) = @barcode
)
BEGIN
SET @barcode += 1;
END
-- end creating barcode
IF EXISTS(
SELECT *
FROM [Tables].[product_values]
WHERE [attribute_id] = @barcode_attribute_id
AND [product_id] = @product_id
AND [value] IS NOT NULL
)
BEGIN
UPDATE [Tables].[product_values]
SET @record_id = [id],
[value] = [Functions].[to_barcode_ean_13](@barcode)
WHERE [product_id] = @product_id
AND [attribute_id] = @barcode_attribute_id;
END
ELSE BEGIN
INSERT INTO [Tables].[product_values]([product_id], [attribute_id], [value])
VALUES(@product_id, @barcode_attribute_id, [Functions].[to_barcode_ean_13](@barcode));
SET @record_id = SCOPE_IDENTITY();
END
COMMIT TRANSACTION;
GO
@@ -0,0 +1,16 @@
USE [Database]
IF OBJECT_ID('[Procedures].[product-properties]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[product-properties];
GO
CREATE PROCEDURE [Procedures].[product-properties]
@product_id AS INT
AS
SELECT [A].[id], [A].[name], [PV].[value]
FROM [Tables].[product_attributes] AS [PA]
LEFT OUTER JOIN [Tables].[product_values] AS [PV]
ON [PV].[product_id] = [PA].[product_id]
AND [PV].[attribute_id] = [PA].[attribute_id]
JOIN [Tables].[attributes] AS [A]
ON [A].[id] = [PA].[attribute_id]
WHERE [PA].[product_id] = @product_id;
@@ -0,0 +1,31 @@
USE [Database]
IF OBJECT_ID('[Procedures].[select_expired_products]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[select_expired_products];
GO
CREATE PROCEDURE [Procedures].[select_expired_products]
@date_of_manufacture_attribute_id AS INT ,
@expiration_date_attribute_id AS INT ,
@actual_date AS DATE
AS
WITH [values] AS (
SELECT [p].[id], [pa].[attribute_id], [pv].[value]
FROM [Tables].[products] AS [p]
JOIN [Tables].[product_attributes] AS [pa]
ON [pa].[product_id] = [p].[id]
JOIN [Tables].[product_values] AS [pv]
ON [pv].[attribute_id] = [pa].[attribute_id]
AND [pv].[product_id] = [pa].[product_id]
WHERE [pa].[attribute_id] IN (@date_of_manufacture_attribute_id, @expiration_date_attribute_id)
)
SELECT [date_of_manufacture_attributes].[id]
FROM [values] AS date_of_manufacture_attributes
JOIN [values] AS expiration_date_attributes
ON [expiration_date_attributes].[id] = [date_of_manufacture_attributes].[id]
AND [expiration_date_attributes].[attribute_id] = @expiration_date_attribute_id
WHERE [date_of_manufacture_attributes].[attribute_id] = @date_of_manufacture_attribute_id
AND DATEADD(
DAY, CAST([expiration_date_attributes].[value] AS INT),
CAST([date_of_manufacture_attributes].[value] AS DATE)
) < @actual_date;
GO
@@ -0,0 +1,37 @@
USE [Database]
IF OBJECT_ID('[Procedures].[tree_positions]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[tree_positions];
GO
CREATE PROCEDURE [Procedures].[tree_positions]
@product_type_id INT
AS
IF OBJECT_ID('[tempdb].[dbo].[#product_types_row_number]') IS NOT NULL
DROP TABLE [dbo].[#product_types_row_number];
CREATE TABLE [#product_types_row_number]
(
[id] INT NOT NULL,
[parent_id] INT,
[row_number] INT NOT NULL
);
INSERT INTO [#product_types_row_number]
SELECT [id], [parent_id], ROW_NUMBER() OVER (PARTITION BY [parent_id] ORDER BY [parent_id], [id] ASC) - 1
FROM [Tables].[product_types];
WITH [result] AS (
SELECT
[id], [parent_id], [row_number]
FROM [#product_types_row_number]
WHERE [id] = @product_type_id
UNION ALL
SELECT [PT].[id], [PT].[parent_id], [PT].[row_number]
FROM [result] AS [R]
JOIN [#product_types_row_number] AS [PT]
ON [PT].[id] = [R].[parent_id]
)
SELECT [id], [parent_id], [row_number]
FROM [result]
ORDER BY [id];