Initial commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
USE [Database]
|
||||
|
||||
IF OBJECT_ID('[Procedures].[products_join_product_types]', 'P') IS NOT NULL
|
||||
DROP PROCEDURE [Procedures].[products_join_product_types];
|
||||
GO
|
||||
|
||||
CREATE PROCEDURE [Procedures].[products_join_product_types]
|
||||
AS
|
||||
SELECT [P].[id] AS [product_id], [P].[barcode],
|
||||
[P].[product_type_id] AS [product_type_id],
|
||||
[P].[name] AS [product_name],
|
||||
[PT].[name] AS [product_type_name]
|
||||
FROM [Tables].[products] AS [P]
|
||||
JOIN [Tables].[product_types] AS [PT]
|
||||
ON [PT].[id] = [P].[product_type_id]
|
||||
GO
|
||||
@@ -0,0 +1,14 @@
|
||||
USE [Database]
|
||||
|
||||
IF OBJECT_ID('[Procedures].[rename_product]', 'P') IS NOT NULL
|
||||
DROP PROCEDURE [Procedures].[rename_product];
|
||||
GO
|
||||
CREATE PROCEDURE [Procedures].[rename_product]
|
||||
@product_id AS INT,
|
||||
@name AS NVARCHAR(128)
|
||||
AS
|
||||
UPDATE [Tables].[products]
|
||||
SET
|
||||
[name] = @name
|
||||
WHERE [id] = @product_id;
|
||||
GO
|
||||
@@ -0,0 +1,14 @@
|
||||
USE [Database]
|
||||
|
||||
IF OBJECT_ID('[Procedures].[rename_product_type]', 'P') IS NOT NULL
|
||||
DROP PROCEDURE [Procedures].[rename_product_type];
|
||||
GO
|
||||
CREATE PROCEDURE [Procedures].[rename_product_type]
|
||||
@product_type_id AS INT,
|
||||
@name AS NVARCHAR(128)
|
||||
AS
|
||||
UPDATE [Tables].[product_types]
|
||||
SET
|
||||
[name] = @name
|
||||
WHERE [id] = @product_type_id;
|
||||
GO
|
||||
@@ -0,0 +1,27 @@
|
||||
USE [Database]
|
||||
|
||||
IF OBJECT_ID('[Procedures].[specify_product_type]', 'P') IS NOT NULL
|
||||
DROP PROCEDURE [Procedures].[specify_product_type];
|
||||
GO
|
||||
CREATE PROCEDURE [Procedures].[specify_product_type]
|
||||
@parent_id INT = NULL,
|
||||
@name NVARCHAR(128),
|
||||
@inserted_product_type_id AS INT = NULL OUTPUT
|
||||
AS
|
||||
BEGIN TRANSACTION;
|
||||
SET NOCOUNT ON;
|
||||
|
||||
INSERT INTO [Tables].[product_types]([parent_id], [name])
|
||||
VALUES(DEFAULT, @name);
|
||||
SET @inserted_product_type_id = SCOPE_IDENTITY();
|
||||
|
||||
UPDATE [Tables].[products]
|
||||
SET [product_type_id] = @inserted_product_type_id
|
||||
WHERE [product_type_id] = @parent_id;
|
||||
|
||||
UPDATE [Tables].[product_types]
|
||||
SET [parent_id] = @parent_id
|
||||
WHERE [id] = @inserted_product_type_id
|
||||
|
||||
COMMIT TRANSACTION;
|
||||
GO
|
||||
@@ -0,0 +1,18 @@
|
||||
USE [Database]
|
||||
|
||||
IF OBJECT_ID('[Procedures].[update_barcode]', 'P') IS NOT NULL
|
||||
DROP PROCEDURE [Procedures].[update_barcode];
|
||||
GO
|
||||
CREATE PROCEDURE [Procedures].[update_barcode]
|
||||
@product_id INT,
|
||||
@barcode BIGINT
|
||||
AS
|
||||
BEGIN TRANSACTION;
|
||||
SET NOCOUNT ON;
|
||||
|
||||
UPDATE [Tables].[products]
|
||||
SET [barcode] = @barcode
|
||||
WHERE [id] = @product_id
|
||||
|
||||
COMMIT TRANSACTION;
|
||||
GO
|
||||
@@ -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
|
||||
+79
@@ -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;
|
||||
+31
@@ -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];
|
||||
Reference in New Issue
Block a user