-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_db_script.sql
890 lines (833 loc) · 60.5 KB
/
inventory_db_script.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
USE [master]
GO
/****** Object: Database [InventoryMgt] Script Date: 06-04-2024 12:53:04 ******/
CREATE DATABASE [InventoryMgt]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'InventoryMgt', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER01\MSSQL\DATA\InventoryMgt.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'InventoryMgt_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER01\MSSQL\DATA\InventoryMgt_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
WITH CATALOG_COLLATION = DATABASE_DEFAULT, LEDGER = OFF
GO
ALTER DATABASE [InventoryMgt] SET COMPATIBILITY_LEVEL = 160
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [InventoryMgt].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [InventoryMgt] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [InventoryMgt] SET ANSI_NULLS OFF
GO
ALTER DATABASE [InventoryMgt] SET ANSI_PADDING OFF
GO
ALTER DATABASE [InventoryMgt] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [InventoryMgt] SET ARITHABORT OFF
GO
ALTER DATABASE [InventoryMgt] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [InventoryMgt] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [InventoryMgt] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [InventoryMgt] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [InventoryMgt] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [InventoryMgt] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [InventoryMgt] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [InventoryMgt] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [InventoryMgt] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [InventoryMgt] SET ENABLE_BROKER
GO
ALTER DATABASE [InventoryMgt] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [InventoryMgt] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [InventoryMgt] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [InventoryMgt] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [InventoryMgt] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [InventoryMgt] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [InventoryMgt] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [InventoryMgt] SET RECOVERY FULL
GO
ALTER DATABASE [InventoryMgt] SET MULTI_USER
GO
ALTER DATABASE [InventoryMgt] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [InventoryMgt] SET DB_CHAINING OFF
GO
ALTER DATABASE [InventoryMgt] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [InventoryMgt] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [InventoryMgt] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [InventoryMgt] SET ACCELERATED_DATABASE_RECOVERY = OFF
GO
EXEC sys.sp_db_vardecimal_storage_format N'InventoryMgt', N'ON'
GO
ALTER DATABASE [InventoryMgt] SET QUERY_STORE = ON
GO
ALTER DATABASE [InventoryMgt] SET QUERY_STORE (OPERATION_MODE = READ_WRITE, CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30), DATA_FLUSH_INTERVAL_SECONDS = 900, INTERVAL_LENGTH_MINUTES = 60, MAX_STORAGE_SIZE_MB = 1000, QUERY_CAPTURE_MODE = AUTO, SIZE_BASED_CLEANUP_MODE = AUTO, MAX_PLANS_PER_QUERY = 200, WAIT_STATS_CAPTURE_MODE = ON)
GO
USE [InventoryMgt]
GO
/****** Object: User [NT AUTHORITY\SYSTEM] Script Date: 06-04-2024 12:53:05 ******/
CREATE USER [NT AUTHORITY\SYSTEM] FOR LOGIN [NT AUTHORITY\SYSTEM] WITH DEFAULT_SCHEMA=[db_owner]
GO
ALTER ROLE [db_owner] ADD MEMBER [NT AUTHORITY\SYSTEM]
GO
/****** Object: Table [dbo].[Category] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Category](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CreateDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NOT NULL,
[IsDeleted] [bit] NULL,
[CategoryName] [nvarchar](50) NOT NULL,
[CategoryId] [int] NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Product] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Product](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CreateDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NOT NULL,
[IsDeleted] [bit] NULL,
[ProductName] [nvarchar](50) NOT NULL,
[CategoryId] [int] NOT NULL,
[Price] [decimal](18, 2) NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Purchase] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Purchase](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CreateDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NOT NULL,
[IsDeleted] [bit] NULL,
[ProductId] [int] NULL,
[PurchaseDate] [datetime] NOT NULL,
[Quantity] [float] NOT NULL,
[Description] [nvarchar](100) NULL,
[Price] [decimal](18, 2) NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Sale] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Sale](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CreateDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NOT NULL,
[IsDeleted] [bit] NULL,
[ProductId] [int] NOT NULL,
[SellingDate] [datetime] NOT NULL,
[Quantity] [float] NULL,
[Description] [nvarchar](100) NOT NULL,
[Price] [decimal](18, 2) NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Stock] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Stock](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CreateDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NOT NULL,
[IsDeleted] [bit] NULL,
[ProductId] [int] NULL,
[Quantity] [float] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Stock] ADD DEFAULT (getdate()) FOR [CreateDate]
GO
ALTER TABLE [dbo].[Stock] ADD DEFAULT (getdate()) FOR [UpdateDate]
GO
ALTER TABLE [dbo].[Stock] ADD DEFAULT ((0)) FOR [IsDeleted]
GO
ALTER TABLE [dbo].[Product] WITH CHECK ADD FOREIGN KEY([CategoryId])
REFERENCES [dbo].[Category] ([Id])
GO
ALTER TABLE [dbo].[Purchase] WITH CHECK ADD FOREIGN KEY([ProductId])
REFERENCES [dbo].[Product] ([Id])
GO
ALTER TABLE [dbo].[Sale] WITH CHECK ADD FOREIGN KEY([ProductId])
REFERENCES [dbo].[Product] ([Id])
GO
ALTER TABLE [dbo].[Stock] WITH CHECK ADD FOREIGN KEY([ProductId])
REFERENCES [dbo].[Product] ([Id])
GO
/****** Object: StoredProcedure [dbo].[usp_AddCategory] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[usp_AddCategory](@CategoryName nvarchar(50),@CategoryId int null)
as
begin
insert into Category(CreateDate,UpdateDate,IsDeleted,CategoryName,CategoryId)
values(getdate(),getdate(),0,@CategoryName,@CategoryId);
select c.*,parent.CategoryName as ParentCategoryName
from category c left join category parent
on c.CategoryId=parent.Id where c.Id=scope_identity()
end
GO
/****** Object: StoredProcedure [dbo].[Usp_AddProduct] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[Usp_AddProduct]
(
@ProductName nvarchar(50), @CategoryId int, @Price decimal(18,2)
)
as
begin
declare @lastId int
insert into Product (CreateDate,UpdateDate,IsDeleted,ProductName,CategoryId,Price)
values(getdate(),getdate(),0,@ProductName,@CategoryId,@Price)
select @lastId=SCOPE_IDENTITY()
select p.*,c.CategoryName from Product p join Category c on p.CategoryId = c.Id
where p.IsDeleted=0 and c.IsDeleted=0 and p.Id=@lastId
end
GO
/****** Object: StoredProcedure [dbo].[usp_AddPurchase] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_AddPurchase]
@ProductId int , @PurchaseDate datetime ,
@Quantity float,@Price decimal(18,2) ,@Description nvarchar(100)
as begin
begin Transaction;
begin try
declare @createdPurchaseId int;
insert into Purchase(CreateDate,UpdateDate,IsDeleted,ProductId,PurchaseDate,Quantity,Price,[Description])
values
(getdate(),getdate(),0,@ProductId,@PurchaseDate,@Quantity,@Price,@Description);
set @createdPurchaseId=SCOPE_IDENTITY();
-- managing stock
if exists(select 1 from Stock where ProductId=@ProductId)
begin
update Stock set Quantity=Quantity+@Quantity where ProductId=@ProductId;
end
else
begin
insert into Stock(ProductId,Quantity) values (@ProductId,@Quantity)
end
COMMIT TRANSACTION;
-- returning created purchase entry
select purchase.*,product.ProductName from
Purchase purchase join Product product
on purchase.ProductId = product.Id
where purchase.IsDeleted=0 and product.IsDeleted=0 and purchase.Id=@createdPurchaseId
end try
begin catch
ROLLBACK TRANSACTION;
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
end catch
end
GO
/****** Object: StoredProcedure [dbo].[usp_AddSale] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_AddSale]
@ProductId int , @SellingDate datetime,
@Quantity float, @Price decimal(18,2) ,@Description nvarchar(100)
as begin
begin transaction;
begin try
declare @createdSaleId int
insert into Sale(CreateDate,UpdateDate,IsDeleted,ProductId,SellingDate,Quantity,Price,[Description])
values
(getdate(),getdate(),0,@ProductId,@SellingDate,@Quantity,@Price,@Description);
-- select recently created id
set @createdSaleId=SCOPE_IDENTITY();
-- *** Manage Stock ***
if exists(select 1 from stock where ProductId=@ProductId)
begin
update stock set Quantity = Quantity-@Quantity where ProductId=@ProductId
end
COMMIT TRANSACTION;
-- select the resent created item from sale
select s.*,p.ProductName from dbo.Sale s join Product p
on s.ProductId=p.Id where s.IsDeleted=0 and p.IsDeleted=0 and s.Id=@createdSaleId
end try
begin catch
rollback transaction;
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
end catch
end
GO
/****** Object: StoredProcedure [dbo].[usp_DeletePurchase] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_DeletePurchase] @Id int
as
begin
begin transaction;
begin try
declare @productId int,@quantity int;
select @productId=productId,@quantity=Quantity from Purchase where Id=@Id;
update Purchase set IsDeleted=1 where Id=@Id;
if exists(select 1 from stock where productId=@productId)
begin
update Stock set Quantity=Quantity-@quantity where ProductId=@productId;
end
commit transaction;
end try
begin catch
rollback transaction;
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
end catch
end
GO
/****** Object: StoredProcedure [dbo].[usp_DeleteSale] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_DeleteSale]
@id int
as begin
begin transaction;
begin try
declare @productId int,@quantity int;
select @productId=productId,@quantity=Quantity from Sale where Id=@Id;
update Sale set IsDeleted=1 where Id=@Id;
if exists(select 1 from stock where productId=@productId)
begin
update Stock set Quantity=Quantity+@quantity where ProductId=@productId;
end
commit transaction;
end try
begin catch
rollback transaction;
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
end catch
end
GO
/****** Object: StoredProcedure [dbo].[usp_GetAllProductsWithStock] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_GetAllProductsWithStock]
as
begin
select p.Id,p.ProductName, c.CategoryName,p.Price,IsNull(s.Quantity,0) as Quantity from Product p
join Category c
on p.CategoryId=c.Id
left join stock s on p.Id = s.ProductId
where
p.IsDeleted=0 and c.IsDeleted=0
end
GO
/****** Object: StoredProcedure [dbo].[usp_getCategories] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[usp_getCategories] @searchTerm nvarchar(50)=''
as
begin
select c.*,parent.CategoryName as ParentCategoryName
from category c left join category parent
on c.CategoryId=parent.Id where (@searchTerm ='' or c.CategoryName like '' + @searchTerm + '%') and c.IsDeleted=0
end
GO
/****** Object: StoredProcedure [dbo].[usp_getProducts] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_getProducts]
@page int=1,
@limit int=4,
@searchTerm nvarchar(50) = null,
@sortColumn nvarchar(20)='Id',
@sortDirection nvarchar(5)='asc'
as
begin
select p.*, c.CategoryName from Product p join Category c
on p.CategoryId=c.Id where (@searchTerm is null or p.ProductName like '%'+@searchTerm+'%' or c.CategoryName like '%'+@searchTerm+'%')
and p.IsDeleted=0 and c.IsDeleted=0
order by
case when @sortColumn='Id' and @sortDirection='asc' then p.Id end,
case when @sortColumn='Id' and @sortDirection='desc' then p.Id end desc,
case when @sortColumn='ProductName' and @sortDirection='asc' then p.ProductName end,
case when @sortColumn='ProductName' and @sortDirection='desc' then p.ProductName end desc,
case when @sortColumn='Price' and @sortDirection='asc' then p.Price end,
case when @sortColumn='Price' and @sortDirection='desc' then p.Price end desc,
case when @sortColumn='CreateDate' and @sortDirection='asc' then p.CreateDate end,
case when @sortColumn='CreateDate' and @sortDirection='desc' then p.CreateDate end desc,
case when @sortColumn='UpdateDate' and @sortDirection='asc' then p.UpdateDate end,
case when @sortColumn='UpdateDate' and @sortDirection='desc' then p.UpdateDate end desc,
case when @sortColumn='CategoryName' and @sortDirection='asc' then c.CategoryName end,
case when @sortColumn='CategoryName' and @sortDirection='desc' then c.CategoryName end desc
OFFSET(@page-1)*@limit ROWS
FETCH NEXT @limit ROWS ONLY;
select Count(p.Id) as TotalRecords,CAST(CEILING((count(p.Id)*1.0)/@limit)as int) as TotalPages
from Product p join Category c
on p.CategoryId=c.Id where (@searchTerm is null or p.ProductName like '%'+@searchTerm+'%' or c.CategoryName like '%'+@searchTerm+'%')
and p.IsDeleted=0 and c.IsDeleted=0
end
GO
/****** Object: StoredProcedure [dbo].[usp_GetPurchaseById] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[usp_GetPurchaseById] @Id int
as
begin
select purchase.*,product.ProductName from
Purchase purchase join Product product
on purchase.ProductId = product.Id
where purchase.IsDeleted=0 and product.IsDeleted=0 and purchase.Id=@Id
end
GO
/****** Object: StoredProcedure [dbo].[usp_getPurchases] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_getPurchases]
@dateFrom datetime = null,
@dateTo datetime = null,
@productName nvarchar(100) = null,
@page int =1,@limit int=4,@sortDirection nvarchar(4)='asc',@sortColumn nvarchar(50)='Id'
as
begin
select purchase.*,product.ProductName from
Purchase purchase join Product product
on purchase.ProductId = product.Id
where purchase.IsDeleted=0 and product.IsDeleted=0
AND
(
-- Condition 1: No filter columns passed
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NULL
)
OR
-- Condition 2: User passes productName only
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NOT NULL
AND product.ProductName like '%'+@productName+'%'
)
OR
-- Condition 3: User passes dateFrom, dateTo only
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NULL
AND purchase.PurchaseDate >= @dateFrom AND purchase.PurchaseDate <=@dateTo
)
OR
-- Condition 4: User passes dateFrom, dateTo and productName together
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NOT NULL
AND purchase.PurchaseDate >= @dateFrom AND purchase.PurchaseDate <=@dateTo
AND product.ProductName like '%'+@productName+'%'
)
)
order by
case when @sortColumn='Id' and @sortDirection='asc' then purchase.Id end,
case when @sortColumn='Id' and @sortDirection='desc' then purchase.Id end desc,
case when @sortColumn='ProductName' and @sortDirection='asc' then product.ProductName end,
case when @sortColumn='ProductName' and @sortDirection='desc' then product.ProductName end desc,
case when @sortColumn='Price' and @sortDirection='asc' then purchase.Price end,
case when @sortColumn='Price' and @sortDirection='desc' then purchase.Price end desc,
case when @sortColumn='PurchaseDate' and @sortDirection='asc' then purchase.PurchaseDate end,
case when @sortColumn='PurchaseDate' and @sortDirection='desc' then purchase.PurchaseDate end desc
OFFSET(@page-1)*@limit ROWS
fetch next @limit rows only;
-- second result set returns totalRecords with filter in table
select Count(purchase.Id) as TotalRecords,CAST(CEILING((count(purchase.Id)*1.0)/@limit)as int) as TotalPages from
Purchase purchase join Product product
on purchase.ProductId = product.Id
where purchase.IsDeleted=0 and product.IsDeleted=0
AND
(
-- Condition 1: No filter columns passed
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NULL
)
OR
-- Condition 2: User passes productName only
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NOT NULL
AND product.ProductName like '%'+@productName+'%'
)
OR
-- Condition 3: User passes dateFrom, dateTo only
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NULL
AND purchase.PurchaseDate >= @dateFrom AND purchase.PurchaseDate <=@dateTo
)
OR
-- Condition 4: User passes dateFrom, dateTo and productName together
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NOT NULL
AND purchase.PurchaseDate >= @dateFrom AND purchase.PurchaseDate <=@dateTo
AND product.ProductName like '%'+@productName+'%'
)
)
end
GO
/****** Object: StoredProcedure [dbo].[usp_GetSaleById] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_GetSaleById]
@id int
as begin
select s.*,p.ProductName from dbo.Sale s join Product p
on s.ProductId=p.Id where s.IsDeleted=0 and p.IsDeleted=0 and s.Id=@id
end
GO
/****** Object: StoredProcedure [dbo].[usp_GetSales] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[usp_GetSales]
@dateFrom datetime = null,
@dateTo datetime = null,
@productName nvarchar(100) = null,
@page int =1,@limit int=4,@sortDirection nvarchar(4)='asc',@sortColumn nvarchar(50)='Id'
as
begin
select s.*,p.ProductName from dbo.Sale s join Product p
on s.ProductId=p.Id where s.IsDeleted=0 and p.IsDeleted=0
and
(
-- Condition 1: No filter columns passed
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NULL
)
OR
-- Condition 2: User passes productName only
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NOT NULL
AND p.ProductName like '%'+@productName+'%'
)
OR
-- Condition 3: User passes dateFrom, dateTo only
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NULL
AND s.SellingDate >= @dateFrom AND s.SellingDate <=@dateTo
)
OR
-- Condition 4: User passes dateFrom, dateTo and productName together
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NOT NULL
AND s.SellingDate >= @dateFrom AND s.SellingDate <=@dateTo
AND p.ProductName like '%'+@productName+'%'
)
)
order by
case when @sortColumn='Id' and @sortDirection = 'asc' then s.Id end,
case when @sortColumn='Id' and @sortDirection = 'desc' then s.Id end desc,
case when @sortColumn='ProductName' and @sortDirection='asc' then p.ProductName end,
case when @sortColumn='ProductName' and @sortDirection='desc' then p.ProductName end desc,
case when @sortColumn='Price' and @sortDirection='asc' then s.Price end,
case when @sortColumn='Price' and @sortDirection='desc' then s.Price end desc,
case when @sortColumn='SellingDate' and @sortDirection='asc' then s.SellingDate end,
case when @sortColumn='SellingDate' and @sortDirection='desc' then s.SellingDate end desc
offset(@page-1)*@limit rows
fetch next @limit rows only;
-- second result set returns totalRecords with filter in table
select COUNT(s.Id) as TotalRecords, CEILING((count(s.Id)*1.0)/@limit) as TotalPages
from dbo.Sale s join Product p
on s.ProductId=p.Id where s.IsDeleted=0 and p.IsDeleted=0
and
(
-- Condition 1: No filter columns passed
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NULL
)
OR
-- Condition 2: User passes productName only
(
@dateFrom IS NULL AND @dateTo IS NULL AND @productName IS NOT NULL
AND p.ProductName like '%'+@productName+'%'
)
OR
-- Condition 3: User passes dateFrom, dateTo only
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NULL
AND s.SellingDate >= @dateFrom AND s.SellingDate <=@dateTo
)
OR
-- Condition 4: User passes dateFrom, dateTo and productName together
(
@dateFrom IS NOT NULL AND @dateTo IS NOT NULL AND @productName IS NOT NULL
AND s.SellingDate >= @dateFrom AND s.SellingDate <=@dateTo
AND p.ProductName like '%'+@productName+'%'
)
)
end
GO
/****** Object: StoredProcedure [dbo].[usp_GetStock] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_GetStock]
@page int=1,
@limit int=4,
@sortColumn nvarchar(50)='Id',
@sortDirection nvarchar(4)='asc',
@searchTerm nvarchar(50)=null
as
begin
select s.*,p.ProductName,c.CategoryName
from Stock s join Product p on s.ProductId = p.Id
join Category c on p.CategoryId = c.Id
where (@searchTerm is null or p.ProductName like '%'+@searchTerm+'%' or c.CategoryName like '%'+@searchTerm+'%')
and (s.IsDeleted=0 and c.IsDeleted=0 and p.IsDeleted=0)
order by
case when @sortColumn='Id' and @sortDirection='asc' then s.Id end,
case when @sortColumn='Id' and @sortDirection='desc' then s.Id end desc,
case when @sortColumn='ProductName' and @sortDirection='asc' then p.ProductName end,
case when @sortColumn='ProductName' and @sortDirection='desc' then p.ProductName end desc,
case when @sortColumn='CategoryName' and @sortDirection='asc' then c.CategoryName end,
case when @sortColumn='CategoryName' and @sortDirection='desc' then c.CategoryName end desc
offset (@page-1)*@limit rows
fetch next @limit rows only;
select COUNT(s.Id) as TotalRecords, CEILING((count(s.Id)*1.0)/@limit) as TotalPages
from Stock s join Product p on s.ProductId = p.Id
join Category c on p.CategoryId = c.Id
where (@searchTerm is null or p.ProductName like '%'+@searchTerm+'%' or c.CategoryName like '%'+@searchTerm+'%')
and (s.IsDeleted=0 and c.IsDeleted=0 and p.IsDeleted=0)
end
GO
/****** Object: StoredProcedure [dbo].[usp_GetStockByProductId] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[usp_GetStockByProductId]
@ProductId int
as
begin
select stock.*,product.ProductName,category.CategoryName
from Stock stock join Product product on stock.ProductId = product.Id
join Category category on product.CategoryId = category.Id
where stock.IsDeleted=0 and stock.ProductId=@ProductId
end
GO
/****** Object: StoredProcedure [dbo].[usp_UpdateCategory] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[usp_UpdateCategory](@Id int,@CategoryName nvarchar(50),@CategoryId int null)
as
begin
Update Category
set
UpdateDate=getdate(),
CategoryName=@CategoryName,
CategoryId=@CategoryId where Id=@Id
select c.*,parent.CategoryName as ParentCategoryName
from category c left join category parent
on c.CategoryId=parent.Id where c.Id=@Id
end
GO
/****** Object: StoredProcedure [dbo].[usp_updateProduct] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[usp_updateProduct]
@Id int,
@ProductName nvarchar(50),
@CategoryId int,
@Price decimal(18,2)
as
begin
update Product set
UpdateDate=getdate(),
ProductName=@ProductName,
CategoryId=@CategoryId,
Price=@Price
where Id=@Id
select p.*,c.CategoryName from Product p join Category c on p.CategoryId = c.Id
where p.IsDeleted=0 and c.IsDeleted=0 and p.Id=@Id
end
GO
/****** Object: StoredProcedure [dbo].[usp_UpdatePurchase] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_UpdatePurchase]
@Id int,
@ProductId int, @PurchaseDate datetime,
@Quantity float,@Price decimal(18,2),@Description nvarchar(100)
as begin
begin Transaction;
begin try
declare @previousProductId int,@previousQuantity int
select @previousProductId=ProductId, @previousQuantity=Quantity from Purchase where Id=@Id
update Purchase set UpdateDate=getdate(),
ProductId=@ProductId,PurchaseDate=@PurchaseDate,Quantity=@Quantity,Price=@Price,[Description]=@Description
where Id=@Id
-- managing stock
-- if we are having the same product
if(@previousProductId=@ProductId)
begin
update Stock set Quantity=(Quantity-@previousQuantity)+@Quantity where ProductId=@ProductId;
end
-- if we are having the different product
else
begin
--decrease the quantity of previous product
update Stock set Quantity=Quantity-@previousQuantity where ProductId=@previousProductId;
-- increasing quantity of new product
if exists(select 1 from Stock where productId=@ProductId)
begin
update Stock set Quantity=Quantity+@Quantity where ProductId=@ProductId;
end
else
begin
insert into Stock(ProductId,Quantity) values (@ProductId,@Quantity)
end
end
COMMIT TRANSACTION;
-- returning updated purchase entry
select purchase.*,product.ProductName from
Purchase purchase join Product product
on purchase.ProductId = product.Id
where purchase.IsDeleted=0 and product.IsDeleted=0 and purchase.Id=@Id
end try
begin catch
ROLLBACK TRANSACTION;
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
end catch
end
GO
/****** Object: StoredProcedure [dbo].[usp_UpdateSale] Script Date: 06-04-2024 12:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_UpdateSale]
@Id int,
@ProductId int, @SellingDate datetime,
@Quantity float,@Price decimal(18,2),@Description nvarchar(100)
as begin
begin Transaction;
begin try
declare @previousProductId int,@previousQuantity int
select @previousProductId=ProductId, @previousQuantity=Quantity from Sale where Id=@Id
update Sale set UpdateDate=getdate(),
ProductId=@ProductId,SellingDate=@SellingDate,Quantity=@Quantity,Price=@Price,[Description]=@Description
where Id=@Id
-- managing stock
-- if we are having the same product
if(@previousProductId=@ProductId)
begin
update Stock set Quantity=(Quantity+@previousQuantity)-@Quantity where ProductId=@ProductId;
end
-- if we are having the different product
else
begin
--increase the quantity of previous product
update Stock set Quantity=Quantity+@previousQuantity where ProductId=@previousProductId;
-- decrease quantity of new product
if exists(select 1 from Stock where productId=@ProductId)
begin
update Stock set Quantity=Quantity-@Quantity where ProductId=@ProductId;
end
end
COMMIT TRANSACTION;
-- returning updated purchase entry
select s.*,p.ProductName from dbo.Sale s join Product p
on s.ProductId=p.Id where s.IsDeleted=0 and p.IsDeleted=0 and s.Id=@Id
end try
begin catch
ROLLBACK TRANSACTION;
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
end catch
end
GO
USE [master]
GO
ALTER DATABASE [InventoryMgt] SET READ_WRITE
GO