Project

General

Profile

Stock_count_sp_analysis.txt

Muhammed Azhar Mohamed Maideen, 12/06/2024 01:47 PM

 
1
--USE [DMS_EcoSystem]
2
--GO
3
--/****** Object:  StoredProcedure [dbo].[sp_rpt_stock_count_history_report]    Script Date: 12/6/2024 12:01:52 PM ******/
4
--SET ANSI_NULLS ON
5
--GO
6
--SET QUOTED_IDENTIFIER ON
7
--GO
8
--ALTER procedure [dbo].[sp_rpt_stock_count_history_report]
9

    
10
declare
11
@stock_count_start_date date,
12
@stock_count_end_date date,
13
@branch varchar(max),
14
@area varchar(max),
15
@salesman varchar(max),
16
@tenant_id varchar(50),
17
@stockCountType nvarchar(200)
18

    
19
select @stock_count_start_date='2024-01-01 00:00:00',@stock_count_end_date='2024-06-10 00:00:00',
20
@branch=N'BDBA44360C224941833A9125EF87F9AB,07CF8ABF-956D-4C5C-B541-17ED15900347,EC9CBA299A3448C89DE0A43E51523DCE,3543556ACA4E4D8F9FBA2870BA2D1AE5,81DFF21F957F44C19D3CF86399E2867B,9997DBBA-C412-444A-8712-5273260C5373,CA3B8C87A89749AABF20FFEA0972F71B',@area=N'16FF6280C8544440B61DCD1A840D0207,2E03D6B4-B990-4FFE-8821-622E5025AA00,1E6C4AD0-9B3E-4CF9-B3D3-F9CDE0DA8D1C,A08F543987B945E091AD731C9608604F,87791E915FC449E58EAC4C9662DA39D6,D7742372-1B20-4721-927A-412869E583C0,EB8F61A3-7D4E-45E0-87EE-0A2A8B32EFF6,80165083-B164-4465-B00A-A0A283AFBDBC,7F0E13F1-BAAE-4AB1-B546-25D9886DAD18,820A7928A6164635A2A174D59B1261CC,88CDEBC3444E43D2A2B0A1692454078B,04C57D3D29A04ED9AAC70B8BB2AA0341,410343F6-DAE5-41F6-BB66-071C5ACFF4D8,FE07AE11-6A3D-4030-8FA0-DB60E5330BF9',@salesman=N'NIBOU-admin,ac3337e4-cec3-44f7-ae04-b55d8a603be0,ba7de64d9dea4fa39fee490cc732d500,27153f64bbea4a0ea28ae1c7bb981e9f,e0ce3e8f-f593-4265-904c-617ae4525ea5,44df41ab-f9e9-431c-8892-01ec2b2fe6f7,518d3ace-b940-4079-b981-ea72bdb383dd,d02112a5a86f4285a77056250a82aa98,d71824b6-a0c2-490e-9136-8208744c2a4c,82beaa9a25b34657879ac20010f2b295,416b5244-ee12-40d1-8932-eab7b269035b',
21
@tenant_id=N'NIBOU',
22
@stockCountType=N'Opening Stock,Closing Stock,Goods Receive Stock'
23

    
24

    
25
--version 2 changes 20210925 ES - change sorting by product
26

    
27
create table #azhar_report_performance
28
(
29
	id varchar(100),
30
	description  varchar(100),
31
	time datetime,
32
)
33

    
34
insert into #azhar_report_performance
35
select newid(),'start',getdate()
36

    
37
create table #area
38
(
39
area_id varchar(50) collate database_default
40
)
41
insert into #area
42
select * from mcc_fn_splitMVParam(@area,',')
43

    
44

    
45
create table #branch
46
(
47
branch_id varchar (50) collate DATABASE_DEFAULT
48
)
49
INSERT into #branch
50
select * from mcc_fn_splitMVParam(@branch,',')
51

    
52

    
53
create table #salesman
54
(
55
salesman_id varchar (50) collate DATABASE_DEFAULT
56
)
57
INSERT INTO #salesman select * from mcc_fn_splitMVParam(@salesman,',')
58

    
59

    
60
create table #stockCountType
61
(
62
stockCountType nvarchar(200) collate DATABASE_DEFAULT
63
)
64
INSERT INTO #stockCountType select * from mcc_fn_splitMVParam(@stockCountType,',')
65

    
66
create table #temp_mobile_txn_stock_count
67
(
68
	row_no int,
69
	txn_id varchar(50) collate database_default,
70
	stock_count_id varchar(50) collate database_default,
71
	customer_id varchar(50) collate database_default,
72
	product_id varchar(50) collate database_default,
73
	expiry_date datetime,
74
	remark nvarchar(max) collate database_default,
75
	uom_1 varchar(50),
76
	uom_2 varchar(50),
77
	uom_3 varchar(50),
78
	quantity_1 int,
79
	quantity_2 int,
80
	quantity_3 int,
81
	created_date datetime,
82
	stock_count_type_desc nvarchar(200)
83
)
84

    
85
insert into #azhar_report_performance
86
select newid(),'part1',getdate()
87

    
88
insert into #temp_mobile_txn_stock_count
89
select 
90
row_number() over (partition by customer_id,item_id order by s.created_date,txn_no desc) as row_no,
91
h.txn_id,s.stock_count_id, customer_id, item_id,
92
s.expiry_date, s.remark, uom1.product_uom_code as uom_1,uom2.product_uom_code as uom_2, uom3.product_uom_code as uom_3, s.quantity_1,s.quantity_2, s.quantity_3, s.created_date,
93
msct.stock_count_type_desc
94
from txn_mobile_header h
95
inner join txn_mobile_stock_count s on h.txn_id = s.txn_id
96
left join mst_product_uom uom1 on s.uom_1=uom1.product_uom_id
97
left join mst_product_uom uom2 on s.uom_2=uom2.product_uom_id
98
left join mst_product_uom uom3 on s.uom_3=uom3.product_uom_id
99
left join mst_stock_count_type msct on msct.stock_count_type_id = s.stock_count_type_id
100

    
101
insert into #azhar_report_performance
102
select newid(),'part2',getdate()
103

    
104
select c.customer_code, c.customer_name, mcc.customer_class_description, l1.customer_id, l1.product_id, p.item_name, b.branch_desc, a.area_description, 
105
m.member_cd, CONCAT(m.first_name, ' ', m.last_name) AS salesman_name, l1.expiry_date as exp_date,  l1.remark, l1.txn_id, b.branch_code, p.item_code,
106
l1.stock_count_type_desc,
107

    
108
l1.uom_1 as c1_last_count1, isnull(l1.quantity_1, 0) as c1_last_qty1, l1.created_date as c1_stock_date1, 
109
l1.uom_2 as c1_last_count2, isnull(l1.quantity_2, 0) as c1_last_qty2, l1.created_date as c1_stock_date2,
110
l1.uom_3 as c1_last_count3, isnull(l1.quantity_3, 0) as c1_last_qty3, l1.created_date as c1_stock_date3,
111

    
112
l2.uom_1 as c2_last_count1, isnull(l2.quantity_1,0) as c2_last_qty1, l2.created_date as c2_stock_date1, 
113
l2.uom_2 as c2_last_count2, isnull(l2.quantity_2,0) as c2_last_qty2 , l2.created_date as c2_stock_date2,
114
l2.uom_3 as c2_last_count3, isnull(l2.quantity_3,0) as c2_last_qty3 , l2.created_date as c2_stock_date3,
115

    
116
l3.uom_1 as c3_last_count1, isnull(l3.quantity_1,0) as c3_last_qty1, l3.created_date as c3_stock_date1, 
117
l3.uom_2 as c3_last_count2, isnull(l3.quantity_2,0) as c3_last_qty2 , l3.created_date as c3_stock_date2,
118
l3.uom_3 as c3_last_count3, isnull(l3.quantity_3,0) as c3_last_qty3 , l3.created_date as c3_stock_date3
119

    
120
from #temp_mobile_txn_stock_count l1
121
left join #temp_mobile_txn_stock_count l2 on l1.row_no =l2.row_no + 1 and l1.customer_id=l2.customer_id and l1.product_id=l2.product_id
122
left join #temp_mobile_txn_stock_count l3 on l2.row_no =l3.row_no + 1 and l2.customer_id=l3.customer_id and l2.product_id=l3.product_id
123
INNER JOIN txn_mobile_header mh ON mh.customer_id = l1.customer_id and l1.txn_id=mh.txn_id
124
INNER JOIN txn_mobile_check_in mc ON mc.txn_id = mh.txn_id
125
inner join customer c on c.customer_id = l1.customer_id
126
left join mst_customer_class mcc on c.customer_class_id = mcc.customer_class_id
127
INNER JOIN product_item	p ON l1.product_id = p.item_id
128
inner join mst_branch b on b.branch_id = c.branch_id
129
inner join mst_area a on a.area_id = c.area_id
130
inner join member m on m.member_id = mh.salesman_id
131
WHERE cast(l1.created_date as date) BETWEEN @stock_count_start_date AND @stock_count_end_date
132
AND exists (select 1 from #branch b1 where b.branch_id = b1.branch_id)
133
AND exists (select 1 from #area a1 where a.area_id = a1.area_id)
134
AND exists (select 1 from #salesman s1 where m.member_id = s1.salesman_id)
135
AND exists (select 1 from #stockCountType sct1 where l1.stock_count_type_desc = sct1.stockCountType)
136
AND mh.tenant_id = @tenant_id
137
--version 2 changes
138
--ORDER BY  l1.customer_id,l1.product_id,l1.created_date DESC
139
ORDER BY  l1.customer_id,p.sorting,l1.created_date DESC
140
--end version 2 changes
141

    
142
insert into #azhar_report_performance
143
select newid(),'end',getdate()
144

    
145
DROP TABLE #area
146
DROP TABLE #branch
147
DROP TABLE #salesman
148
DROP TABLE #stockCountType
149

    
150
DROP TABLE #temp_mobile_txn_stock_count
151

    
152
--drop table #azhar_report_performance
153
select * from #azhar_report_performance
154