1
|
--Temp table for on_behalf
|
2
|
Select customer_id, oba.member_id as user_id, ob.member_id as salesman_id Into #on_behalf from on_behalf_access_customer obc
|
3
|
Inner Join on_behalf_access oba On oba.access_id = obc.access_id
|
4
|
Inner Join on_behalf ob On ob.on_behalf_id = obc.on_behalf_id Where oba.member_id = '8641a786a4384cd8a318a9d723de95ba' and (GetDate() between ob.start_date and ob.end_date)
|
5
|
declare @orgMasterAccBilling varchar(10)
|
6
|
select top 1 @orgMasterAccBilling = UPPER(master_acc_billing) from org where org_id = 'Snekku'
|
7
|
|
8
|
--Select
|
9
|
-- CASE status WHEN 'Active' THEN 'Insert/Update'
|
10
|
-- WHEN 'Inactive' THEN 'Delete' END as action,
|
11
|
-- credit_note_header_id as id, customer_id as customer_id, credit_note_no as docNo, docDate, cast(currency_value as decimal(18,2)) as docAmount, cast(credit_note_balance as decimal(18,2)) as unpaid, cast(non_product as int) as nonProduct
|
12
|
-- from(
|
13
|
-- SELECT ROW_NUMBER() OVER (ORDER BY credit_note_header_id) AS TotalRowNumber, customer_id,
|
14
|
-- customer_code, status, currency_value, credit_note_balance, docDate,credit_note_header_id,credit_note_no, non_product from(
|
15
|
-- SELECT cn.credit_note_header_id
|
16
|
-- , CASE WHEN @orgMasterAccBilling = 'YES' THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_id ELSE c.master_account END)
|
17
|
-- ELSE c.customer_id end as customer_id
|
18
|
-- , CASE WHEN @orgMasterAccBilling = 'YES' THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_code ELSE mc.customer_code END)
|
19
|
-- ELSE c.customer_code end as customer_code
|
20
|
-- , 'Active' as status, cn.currency_value, cn.credit_note_balance, cn.credit_note_date as docDate,credit_note_no, '0' as non_product
|
21
|
-- FROM txn_credit_note_history_header cn
|
22
|
-- left JOIN customer_to_user_relation crr on crr.customer_id = cn.customer_id
|
23
|
-- and convert(date,getdate()) between start_date and end_date
|
24
|
-- left JOIN customer c on c.customer_id = cn.customer_id
|
25
|
-- left join customer mc on mc.customer_id = c.master_account
|
26
|
-- where crr.user_id = '8641a786a4384cd8a318a9d723de95ba' and cn.tenant_ID = 'Snekku' and cn.updated_date > '2024-05-10 14:15:05.493' and cn.status <> 'Draft'
|
27
|
|
28
|
-- UNION
|
29
|
-- SELECT cnn.credit_note_header_id
|
30
|
-- , CASE WHEN @orgMasterAccBilling = 'YES' THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_id ELSE c.master_account END)
|
31
|
-- ELSE c.customer_id end as customer_id
|
32
|
-- , CASE WHEN @orgMasterAccBilling = 'YES' THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_code ELSE mc.customer_code END)
|
33
|
-- ELSE c.customer_code end as customer_code
|
34
|
-- , 'Inactive' as status, cnn.currency_value, cnn.credit_note_balance, cnn.credit_note_date as docDate,credit_note_no, '0' as non_product
|
35
|
-- FROM txn_credit_note_history_header_at cnn
|
36
|
-- left JOIN customer c on c.customer_id = cnn.customer_id
|
37
|
-- left join customer mc on mc.customer_id = c.master_account
|
38
|
-- left JOIN customer_to_user_relation cr on cr.customer_id = cnn.customer_id
|
39
|
-- and convert(date,getdate()) between start_date and end_date
|
40
|
-- where cnn.at_type = 'D' and cnn.tenant_ID = 'Snekku' and cnn.at_date > '2024-05-10 14:15:05.493' and cr.user_id = '8641a786a4384cd8a318a9d723de95ba'
|
41
|
-- and not exists(select 1 from txn_credit_note_history_header ccc where ccc.credit_note_header_id = cnn.credit_note_header_id)
|
42
|
-- Union
|
43
|
-- --Details for on behalf salesman
|
44
|
-- SELECT cn.credit_note_header_id, cn.customer_id,
|
45
|
-- c.customer_code, 'Active' as status, cn.currency_value, cn.credit_note_balance, cn.credit_note_date as docDate,credit_note_no, '0' as non_product
|
46
|
-- FROM txn_credit_note_history_header cn
|
47
|
-- left JOIN customer c on c.customer_id = cn.customer_id
|
48
|
-- Where exists (Select 1 from #on_behalf obc Where obc.customer_id = cn.customer_id)
|
49
|
-- and cn.tenant_ID = 'Snekku' and cn.updated_date > '2024-05-10 14:15:05.493' and cn.status <> 'Draft'
|
50
|
|
51
|
-- UNION
|
52
|
-- SELECT cnn.credit_note_header_id, cnn.customer_id,
|
53
|
-- c.customer_code, 'Inactive' as status, cnn.currency_value, cnn.credit_note_balance, cnn.credit_note_date as docDate,credit_note_no, '0' as non_product
|
54
|
-- FROM txn_credit_note_history_header_at cnn
|
55
|
-- left JOIN customer c on c.customer_id = cnn.customer_id
|
56
|
-- Where exists (Select 1 from #on_behalf obc Where obc.customer_id = cnn.customer_id)
|
57
|
-- and cnn.at_type = 'D' and cnn.tenant_ID = 'Snekku' and cnn.at_date > '2024-05-10 14:15:05.493'
|
58
|
-- and not exists(select 1 from txn_credit_note_history_header ccc where ccc.credit_note_header_id = cnn.credit_note_header_id)
|
59
|
|
60
|
-- --CN non product
|
61
|
-- UNION
|
62
|
-- SELECT cn.credit_note_id as credit_note_header_id, cn.customer_id,
|
63
|
-- c.customer_code, 'Active' as status, cn.net_total as currency_value, cn.credit_note_balance, cn.credit_note_date as docDate,credit_note_no, '1' as non_product
|
64
|
-- FROM txn_credit_note_non_product_header cn
|
65
|
-- left JOIN customer_to_user_relation crr on crr.customer_id = cn.customer_id
|
66
|
-- and convert(date,getdate()) between start_date and end_date
|
67
|
-- left JOIN customer c on c.customer_id = cn.customer_id
|
68
|
-- where crr.user_id = '8641a786a4384cd8a318a9d723de95ba' and cn.tenant_ID = 'Snekku' and cn.updated_date > '2024-05-10 14:15:05.493' and cn.status <> 'Draft'
|
69
|
|
70
|
-- UNION
|
71
|
-- SELECT cnn.credit_note_id as credit_note_header_id, cnn.customer_id,
|
72
|
-- c.customer_code, 'Inactive' as status, cnn.net_total as currency_value, cnn.credit_note_balance, cnn.credit_note_date as docDate,credit_note_no, '1' as non_product
|
73
|
-- FROM txn_credit_note_non_product_header_at cnn
|
74
|
-- left JOIN customer c on c.customer_id = cnn.customer_id
|
75
|
-- left JOIN customer_to_user_relation cr on cr.customer_id = cnn.customer_id
|
76
|
-- and convert(date,getdate()) between start_date and end_date
|
77
|
-- where cnn.at_type = 'D' and cnn.tenant_ID = 'Snekku' and cnn.at_date > '2024-05-10 14:15:05.493' and cr.user_id = '8641a786a4384cd8a318a9d723de95ba'
|
78
|
-- and not exists(select 1 from txn_credit_note_non_product_header ccc where ccc.credit_note_id = cnn.credit_note_id)
|
79
|
-- Union
|
80
|
-- --Details for on behalf salesman
|
81
|
-- SELECT cn.credit_note_id as credit_note_header_id, cn.customer_id,
|
82
|
-- c.customer_code, 'Active' as status, cn.net_total as currency_value, cn.credit_note_balance, cn.credit_note_date as docDate,credit_note_no, '1' as non_product
|
83
|
-- FROM txn_credit_note_non_product_header cn
|
84
|
-- left JOIN customer c on c.customer_id = cn.customer_id
|
85
|
-- Where exists (Select 1 from #on_behalf obc Where obc.customer_id = cn.customer_id)
|
86
|
-- and cn.tenant_ID = 'Snekku' and cn.updated_date > '2024-05-10 14:15:05.493' and cn.status <> 'Draft'
|
87
|
|
88
|
-- UNION
|
89
|
-- SELECT cnn.credit_note_id as credit_note_header_id, cnn.customer_id, '',
|
90
|
-- c.customer_code, 'Inactive' as status, cnn.net_total as currency_value, cnn.credit_note_balance, cnn.credit_note_date as docDate,credit_note_no, '1' as non_product
|
91
|
-- FROM txn_credit_note_non_product_header_at cnn
|
92
|
-- left JOIN customer c on c.customer_id = cnn.customer_id
|
93
|
-- Where exists (Select 1 from #on_behalf obc Where obc.customer_id = cnn.customer_id)
|
94
|
-- and cnn.at_type = 'D' and cnn.tenant_ID = 'Snekku' and cnn.at_date > '2024-05-10 14:15:05.493'
|
95
|
-- and not exists(select 1 from txn_credit_note_non_product_header ccc where ccc.credit_note_id = cnn.credit_note_id)
|
96
|
-- )x where Convert(varchar(10), docDate, 120) >= Convert(varchar(10), DATEADD(MONTH, -9, GetDate()), 120)
|
97
|
-- )v
|
98
|
-- where TotalRowNumber between
|
99
|
-- (1*9999999)+1-9999999 and 1 * 9999999;
|
100
|
|
101
|
--Drop table #on_behalf select * from sync_mobile_token where login_id= 'MC' order by created_date desc
|
102
|
-- select * from member where member_cd='MC' and org_id= 'snekku'
|
103
|
-- select * from txn_credit_note_history_header where customer_id='F80529B316074852B9116AAFE5938803' and credit_note_balance <>0 order by created_date desc
|
104
|
|
105
|
|
106
|
|
107
|
|
108
|
SELECT
|
109
|
CASE status
|
110
|
WHEN 'Active' THEN 'Insert/Update'
|
111
|
WHEN 'Inactive' THEN 'Delete'
|
112
|
END as action,
|
113
|
credit_note_header_id as id,
|
114
|
customer_id as customer_id,
|
115
|
credit_note_no as docNo,
|
116
|
docDate,
|
117
|
cast(currency_value as decimal(18,2)) as docAmount,
|
118
|
cast(credit_note_balance as decimal(18,2)) as unpaid,
|
119
|
cast(non_product as int) as nonProduct
|
120
|
FROM (
|
121
|
SELECT
|
122
|
ROW_NUMBER() OVER (ORDER BY credit_note_header_id) AS TotalRowNumber,
|
123
|
customer_id,
|
124
|
customer_code,
|
125
|
status,
|
126
|
currency_value,
|
127
|
credit_note_balance,
|
128
|
docDate,
|
129
|
credit_note_header_id,
|
130
|
credit_note_no,
|
131
|
non_product
|
132
|
FROM (
|
133
|
-- Active Credit Notes
|
134
|
SELECT
|
135
|
cn.credit_note_header_id,
|
136
|
CASE
|
137
|
WHEN @orgMasterAccBilling = 'YES'
|
138
|
THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_id ELSE c.master_account END)
|
139
|
ELSE c.customer_id
|
140
|
END as customer_id,
|
141
|
CASE
|
142
|
WHEN @orgMasterAccBilling = 'YES'
|
143
|
THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_code ELSE mc.customer_code END)
|
144
|
ELSE c.customer_code
|
145
|
END as customer_code,
|
146
|
'Active' as status,
|
147
|
cn.currency_value,
|
148
|
cn.credit_note_balance,
|
149
|
cn.credit_note_date as docDate,
|
150
|
cn.credit_note_no,
|
151
|
'0' as non_product
|
152
|
FROM txn_credit_note_history_header cn
|
153
|
LEFT JOIN customer_to_user_relation crr ON crr.customer_id = cn.customer_id
|
154
|
AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
|
155
|
LEFT JOIN customer c ON c.customer_id = cn.customer_id
|
156
|
LEFT JOIN customer mc ON mc.customer_id = c.master_account
|
157
|
WHERE
|
158
|
crr.user_id = '8641a786a4384cd8a318a9d723de95ba'
|
159
|
AND cn.tenant_ID = 'Snekku'
|
160
|
--AND cn.updated_date > '2024-05-10 14:15:05.493'
|
161
|
AND cn.status <> 'Draft'
|
162
|
AND cn.credit_note_no = 'CN24/003134'
|
163
|
|
164
|
UNION
|
165
|
|
166
|
-- Inactive Credit Notes
|
167
|
SELECT
|
168
|
cnn.credit_note_header_id,
|
169
|
CASE
|
170
|
WHEN @orgMasterAccBilling = 'YES'
|
171
|
THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_id ELSE c.master_account END)
|
172
|
ELSE c.customer_id
|
173
|
END as customer_id,
|
174
|
CASE
|
175
|
WHEN @orgMasterAccBilling = 'YES'
|
176
|
THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_code ELSE mc.customer_code END)
|
177
|
ELSE c.customer_code
|
178
|
END as customer_code,
|
179
|
'Inactive' as status,
|
180
|
cnn.currency_value,
|
181
|
cnn.credit_note_balance,
|
182
|
cnn.credit_note_date as docDate,
|
183
|
cnn.credit_note_no,
|
184
|
'0' as non_product
|
185
|
FROM txn_credit_note_history_header_at cnn
|
186
|
LEFT JOIN customer c ON c.customer_id = cnn.customer_id
|
187
|
LEFT JOIN customer mc ON mc.customer_id = c.master_account
|
188
|
LEFT JOIN customer_to_user_relation cr ON cr.customer_id = cnn.customer_id
|
189
|
AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
|
190
|
WHERE
|
191
|
cnn.at_type = 'D'
|
192
|
AND cnn.tenant_ID = 'Snekku'
|
193
|
-- AND cnn.at_date > '2024-05-10 14:15:05.493'
|
194
|
AND cr.user_id = '8641a786a4384cd8a318a9d723de95ba'
|
195
|
AND NOT EXISTS (
|
196
|
SELECT 1
|
197
|
FROM txn_credit_note_history_header ccc
|
198
|
WHERE ccc.credit_note_header_id = cnn.credit_note_header_id
|
199
|
)
|
200
|
--AND cnn.credit_note_no = 'CN24/003134'
|
201
|
|
202
|
UNION
|
203
|
|
204
|
-- Active Details for on behalf salesman
|
205
|
SELECT
|
206
|
cn.credit_note_header_id,
|
207
|
cn.customer_id,
|
208
|
c.customer_code,
|
209
|
'Active' as status,
|
210
|
cn.currency_value,
|
211
|
cn.credit_note_balance,
|
212
|
cn.credit_note_date as docDate,
|
213
|
cn.credit_note_no,
|
214
|
'0' as non_product
|
215
|
FROM txn_credit_note_history_header cn
|
216
|
LEFT JOIN customer c ON c.customer_id = cn.customer_id
|
217
|
WHERE EXISTS (
|
218
|
SELECT 1
|
219
|
FROM #on_behalf obc
|
220
|
WHERE obc.customer_id = cn.customer_id
|
221
|
)
|
222
|
AND cn.tenant_ID = 'Snekku'
|
223
|
--AND cn.updated_date > '2024-05-10 14:15:05.493'
|
224
|
AND cn.status <> 'Draft'
|
225
|
--AND cn.credit_note_no = 'CN24/003134'
|
226
|
|
227
|
UNION
|
228
|
|
229
|
-- Inactive Details for on behalf salesman
|
230
|
SELECT
|
231
|
cnn.credit_note_header_id,
|
232
|
cnn.customer_id,
|
233
|
c.customer_code,
|
234
|
'Inactive' as status,
|
235
|
cnn.currency_value,
|
236
|
cnn.credit_note_balance,
|
237
|
cnn.credit_note_date as docDate,
|
238
|
cnn.credit_note_no,
|
239
|
'0' as non_product
|
240
|
FROM txn_credit_note_history_header_at cnn
|
241
|
LEFT JOIN customer c ON c.customer_id = cnn.customer_id
|
242
|
WHERE EXISTS (
|
243
|
SELECT 1
|
244
|
FROM #on_behalf obc
|
245
|
WHERE obc.customer_id = cnn.customer_id
|
246
|
)
|
247
|
AND cnn.at_type = 'D'
|
248
|
AND cnn.tenant_ID = 'Snekku'
|
249
|
-- AND cnn.at_date > '2024-05-10 14:15:05.493'
|
250
|
AND NOT EXISTS (
|
251
|
SELECT 1
|
252
|
FROM txn_credit_note_history_header ccc
|
253
|
WHERE ccc.credit_note_header_id = cnn.credit_note_header_id
|
254
|
)
|
255
|
--AND cnn.credit_note_no = 'CN24/003134'
|
256
|
|
257
|
UNION
|
258
|
|
259
|
-- CN non product Active
|
260
|
SELECT
|
261
|
cn.credit_note_id as credit_note_header_id,
|
262
|
cn.customer_id,
|
263
|
c.customer_code,
|
264
|
'Active' as status,
|
265
|
cn.net_total as currency_value,
|
266
|
cn.credit_note_balance,
|
267
|
cn.credit_note_date as docDate,
|
268
|
cn.credit_note_no,
|
269
|
'1' as non_product
|
270
|
FROM txn_credit_note_non_product_header cn
|
271
|
LEFT JOIN customer_to_user_relation crr ON crr.customer_id = cn.customer_id
|
272
|
AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
|
273
|
LEFT JOIN customer c ON c.customer_id = cn.customer_id
|
274
|
WHERE
|
275
|
crr.user_id = '8641a786a4384cd8a318a9d723de95ba'
|
276
|
AND cn.tenant_ID = 'Snekku'
|
277
|
--AND cn.updated_date > '2024-05-10 14:15:05.493'
|
278
|
AND cn.status <> 'Draft'
|
279
|
AND cn.credit_note_no = 'CN24/003134'
|
280
|
|
281
|
UNION
|
282
|
|
283
|
-- CN non product Inactive
|
284
|
SELECT
|
285
|
cnn.credit_note_id as credit_note_header_id,
|
286
|
cnn.customer_id,
|
287
|
c.customer_code,
|
288
|
'Inactive' as status,
|
289
|
cnn.net_total as currency_value,
|
290
|
cnn.credit_note_balance,
|
291
|
cnn.credit_note_date as docDate,
|
292
|
cnn.credit_note_no,
|
293
|
'1' as non_product
|
294
|
FROM txn_credit_note_non_product_header_at cnn
|
295
|
LEFT JOIN customer c ON c.customer_id = cnn.customer_id
|
296
|
LEFT JOIN customer_to_user_relation cr ON cr.customer_id = cnn.customer_id
|
297
|
AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
|
298
|
WHERE
|
299
|
cnn.at_type = 'D'
|
300
|
AND cnn.tenant_ID = 'Snekku'
|
301
|
--AND cnn.at_date > '2024-05-10 14:15:05.493'
|
302
|
AND cr.user_id = '8641a786a4384cd8a318a9d723de95ba'
|
303
|
AND NOT EXISTS (
|
304
|
SELECT 1
|
305
|
FROM txn_credit_note_non_product_header ccc
|
306
|
WHERE ccc.credit_note_id = cnn.credit_note_id
|
307
|
)
|
308
|
AND cnn.credit_note_no = 'CN24/003134'
|
309
|
|
310
|
UNION
|
311
|
|
312
|
-- CN non product on behalf salesman Active
|
313
|
SELECT
|
314
|
cn.credit_note_id as credit_note_header_id,
|
315
|
cn.customer_id,
|
316
|
c.customer_code,
|
317
|
'Active' as status,
|
318
|
cn.net_total as currency_value,
|
319
|
cn.credit_note_balance,
|
320
|
cn.credit_note_date as docDate,
|
321
|
cn.credit_note_no,
|
322
|
'1' as non_product
|
323
|
FROM txn_credit_note_non_product_header cn
|
324
|
LEFT JOIN customer c ON c.customer_id = cn.customer_id
|
325
|
WHERE EXISTS (
|
326
|
SELECT 1
|
327
|
FROM #on_behalf obc
|
328
|
WHERE obc.customer_id = cn.customer_id
|
329
|
)
|
330
|
AND cn.tenant_ID = 'Snekku'
|
331
|
--AND cn.updated_date > '2024-05-10 14:15:05.493'
|
332
|
AND cn.status <> 'Draft'
|
333
|
AND cn.credit_note_no = 'CN24/003134'
|
334
|
|
335
|
UNION
|
336
|
|
337
|
-- CN non product on behalf salesman Inactive
|
338
|
SELECT
|
339
|
cnn.credit_note_id as credit_note_header_id,
|
340
|
cnn.customer_id,
|
341
|
c.customer_code,
|
342
|
'Inactive' as status,
|
343
|
cnn.net_total as currency_value,
|
344
|
cnn.credit_note_balance,
|
345
|
cnn.credit_note_date as docDate,
|
346
|
cnn.credit_note_no,
|
347
|
'1' as non_product
|
348
|
FROM txn_credit_note_non_product_header_at cnn
|
349
|
LEFT JOIN customer c ON c.customer_id = cnn.customer_id
|
350
|
WHERE EXISTS (
|
351
|
SELECT 1
|
352
|
FROM #on_behalf obc
|
353
|
WHERE obc.customer_id = cnn.customer_id
|
354
|
)
|
355
|
AND cnn.tenant_ID = 'Snekku'
|
356
|
--AND cnn.at_date > '2024-05-10 14:15:05.493'
|
357
|
AND NOT EXISTS (
|
358
|
SELECT 1
|
359
|
FROM txn_credit_note_non_product_header ccc
|
360
|
WHERE ccc.credit_note_id = cnn.credit_note_id
|
361
|
)
|
362
|
--AND cnn.credit_note_no = 'CN24/003134'
|
363
|
) AS Combined
|
364
|
) AS Final
|
365
|
WHERE TotalRowNumber BETWEEN 1 AND 999999
|
366
|
ORDER BY TotalRowNumber;
|
367
|
|
368
|
select * from member where member_cd='admin' and org_id= 'snekku'
|
369
|
--Drop table #on_behalf
|