Project

General

Profile

MC_user.txt

Muhammed Azhar Mohamed Maideen, 19/06/2024 06:13 PM

 
1
SELECT 
2
    CASE status 
3
        WHEN 'Active' THEN 'Insert/Update'
4
        WHEN 'Inactive' THEN 'Delete' 
5
    END as action, 
6
    credit_note_header_id as id, 
7
    customer_id as customer_id, 
8
    credit_note_no as docNo, 
9
    docDate,  
10
    cast(currency_value as decimal(18,2))  as docAmount,  
11
    cast(credit_note_balance as decimal(18,2))  as unpaid, 
12
    cast(non_product as int) as nonProduct
13
FROM (
14
    SELECT  
15
        ROW_NUMBER() OVER (ORDER BY credit_note_header_id) AS TotalRowNumber, 
16
        customer_id,
17
        customer_code, 
18
        status, 
19
        currency_value, 
20
        credit_note_balance, 
21
        docDate,
22
        credit_note_header_id,
23
        credit_note_no, 
24
        non_product 
25
    FROM (
26
        -- Active Credit Notes
27
        SELECT 
28
            cn.credit_note_header_id,
29
            CASE 
30
                WHEN @orgMasterAccBilling = 'YES' 
31
                THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_id ELSE c.master_account END)
32
                ELSE c.customer_id 
33
            END as customer_id,
34
            CASE 
35
                WHEN @orgMasterAccBilling = 'YES' 
36
                THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_code ELSE mc.customer_code END)
37
                ELSE c.customer_code 
38
            END as customer_code,
39
            'Active' as status, 
40
            cn.currency_value, 
41
            cn.credit_note_balance, 
42
            cn.credit_note_date as docDate,
43
            cn.credit_note_no, 
44
            '0' as non_product
45
        FROM txn_credit_note_history_header cn
46
        LEFT JOIN customer_to_user_relation crr ON crr.customer_id = cn.customer_id
47
            AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
48
        LEFT JOIN customer c ON c.customer_id = cn.customer_id
49
        LEFT JOIN customer mc ON mc.customer_id = c.master_account
50
        WHERE 
51
            crr.user_id = '8641a786a4384cd8a318a9d723de95ba' 
52
            AND cn.tenant_ID = 'Snekku' 
53
            AND cn.updated_date > '2024-05-10 14:15:05.493' 
54
            AND cn.status <> 'Draft'
55
            AND cn.credit_note_no = 'CN24/003134'
56

    
57
        UNION
58
        
59
        -- Inactive Credit Notes
60
        SELECT 
61
            cnn.credit_note_header_id,
62
            CASE 
63
                WHEN @orgMasterAccBilling = 'YES' 
64
                THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_id ELSE c.master_account END)
65
                ELSE c.customer_id 
66
            END as customer_id,
67
            CASE 
68
                WHEN @orgMasterAccBilling = 'YES' 
69
                THEN (CASE WHEN ISNULL(c.master_account, '') = '' THEN c.customer_code ELSE mc.customer_code END)
70
                ELSE c.customer_code 
71
            END as customer_code,
72
            'Inactive' as status, 
73
            cnn.currency_value, 
74
            cnn.credit_note_balance, 
75
            cnn.credit_note_date as docDate,
76
            cnn.credit_note_no, 
77
            '0' as non_product
78
        FROM txn_credit_note_history_header_at cnn
79
        LEFT JOIN customer c ON c.customer_id = cnn.customer_id
80
        LEFT JOIN customer mc ON mc.customer_id = c.master_account
81
        LEFT JOIN customer_to_user_relation cr ON cr.customer_id = cnn.customer_id
82
            AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
83
        WHERE  
84
            cnn.at_type = 'D' 
85
            AND cnn.tenant_ID = 'Snekku' 
86
            AND cnn.at_date > '2024-05-10 14:15:05.493' 
87
            AND cr.user_id = '8641a786a4384cd8a318a9d723de95ba'
88
            AND NOT EXISTS (
89
                SELECT 1 
90
                FROM txn_credit_note_history_header ccc 
91
                WHERE ccc.credit_note_header_id = cnn.credit_note_header_id
92
            )
93
            AND cnn.credit_note_no = 'CN24/003134'
94

    
95
        UNION
96
        
97
        -- Active Details for on behalf salesman
98
        SELECT 
99
            cn.credit_note_header_id, 
100
            cn.customer_id, 
101
            c.customer_code, 
102
            'Active' as status, 
103
            cn.currency_value, 
104
            cn.credit_note_balance, 
105
            cn.credit_note_date as docDate,
106
            cn.credit_note_no, 
107
            '0' as non_product
108
        FROM txn_credit_note_history_header cn
109
        LEFT JOIN customer c ON c.customer_id = cn.customer_id
110
        WHERE EXISTS (
111
            SELECT 1 
112
            FROM #on_behalf obc 
113
            WHERE obc.customer_id = cn.customer_id
114
        )
115
        AND cn.tenant_ID = 'Snekku' 
116
        AND cn.updated_date > '2024-05-10 14:15:05.493' 
117
        AND cn.status <> 'Draft'
118
        AND cn.credit_note_no = 'CN24/003134'
119

    
120
        UNION
121
        
122
        -- Inactive Details for on behalf salesman
123
        SELECT 
124
            cnn.credit_note_header_id, 
125
            cnn.customer_id, 
126
            c.customer_code, 
127
            'Inactive' as status, 
128
            cnn.currency_value, 
129
            cnn.credit_note_balance, 
130
            cnn.credit_note_date as docDate,
131
            cnn.credit_note_no, 
132
            '0' as non_product
133
        FROM txn_credit_note_history_header_at cnn
134
        LEFT JOIN customer c ON c.customer_id = cnn.customer_id
135
        WHERE EXISTS (
136
            SELECT 1 
137
            FROM #on_behalf obc 
138
            WHERE obc.customer_id = cnn.customer_id
139
        )
140
        AND cnn.at_type = 'D' 
141
        AND cnn.tenant_ID = 'Snekku' 
142
        AND cnn.at_date > '2024-05-10 14:15:05.493'
143
        AND NOT EXISTS (
144
            SELECT 1 
145
            FROM txn_credit_note_history_header ccc 
146
            WHERE ccc.credit_note_header_id = cnn.credit_note_header_id
147
        )
148
        AND cnn.credit_note_no = 'CN24/003134'
149

    
150
        UNION
151
        
152
        -- CN non product Active
153
        SELECT 
154
            cn.credit_note_id as credit_note_header_id, 
155
            cn.customer_id, 
156
            c.customer_code, 
157
            'Active' as status, 
158
            cn.net_total as currency_value, 
159
            cn.credit_note_balance, 
160
            cn.credit_note_date as docDate,
161
            cn.credit_note_no, 
162
            '1' as non_product
163
        FROM txn_credit_note_non_product_header cn
164
        LEFT JOIN customer_to_user_relation crr ON crr.customer_id = cn.customer_id
165
            AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
166
        LEFT JOIN customer c ON c.customer_id = cn.customer_id
167
        WHERE 
168
            crr.user_id = '8641a786a4384cd8a318a9d723de95ba' 
169
            AND cn.tenant_ID = 'Snekku' 
170
            AND cn.updated_date > '2024-05-10 14:15:05.493' 
171
            AND cn.status <> 'Draft'
172
            AND cn.credit_note_no = 'CN24/003134'
173

    
174
        UNION
175
        
176
        -- CN non product Inactive
177
        SELECT 
178
            cnn.credit_note_id as credit_note_header_id, 
179
            cnn.customer_id, 
180
            c.customer_code, 
181
            'Inactive' as status, 
182
            cnn.net_total as currency_value, 
183
            cnn.credit_note_balance, 
184
            cnn.credit_note_date as docDate,
185
            cnn.credit_note_no, 
186
            '1' as non_product
187
        FROM txn_credit_note_non_product_header_at cnn
188
        LEFT JOIN customer c ON c.customer_id = cnn.customer_id
189
        LEFT JOIN customer_to_user_relation cr ON cr.customer_id = cnn.customer_id
190
            AND CONVERT(date, GETDATE()) BETWEEN start_date AND end_date
191
        WHERE  
192
            cnn.at_type = 'D' 
193
            AND cnn.tenant_ID = 'Snekku' 
194
            AND cnn.at_date > '2024-05-10 14:15:05.493' 
195
            AND cr.user_id = '8641a786a4384cd8a318a9d723de95ba'
196
            AND NOT EXISTS (
197
                SELECT 1 
198
                FROM txn_credit_note_non_product_header ccc 
199
                WHERE ccc.credit_note_id = cnn.credit_note_id
200
            )
201
            AND cnn.credit_note_no = 'CN24/003134'
202

    
203
        UNION
204
        
205
        -- CN non product on behalf salesman Active
206
        SELECT 
207
            cn.credit_note_id as credit_note_header_id, 
208
            cn.customer_id, 
209
            c.customer_code, 
210
            'Active' as status, 
211
            cn.net_total as currency_value, 
212
            cn.credit_note_balance, 
213
            cn.credit_note_date as docDate,
214
            cn.credit_note_no, 
215
            '1' as non_product
216
        FROM txn_credit_note_non_product_header cn
217
        LEFT JOIN customer c ON c.customer_id = cn.customer_id
218
        WHERE EXISTS (
219
            SELECT 1 
220
            FROM #on_behalf obc 
221
            WHERE obc.customer_id = cn.customer_id
222
        )
223
        AND cn.tenant_ID = 'Snekku' 
224
        AND cn.updated_date > '2024-05-10 14:15:05.493' 
225
        AND cn.status <> 'Draft'
226
        AND cn.credit_note_no = 'CN24/003134'
227
 
228
        UNION
229
        
230
        -- CN non product on behalf salesman Inactive
231
        SELECT 
232
            cnn.credit_note_id as credit_note_header_id, 
233
            cnn.customer_id, 
234
            c.customer_code, 
235
            'Inactive' as status, 
236
            cnn.net_total as currency_value, 
237
            cnn.credit_note_balance, 
238
            cnn.credit_note_date as docDate,
239
            cnn.credit_note_no, 
240
            '1' as non_product
241
        FROM txn_credit_note_non_product_header_at cnn
242
        LEFT JOIN customer c ON c.customer_id = cnn.customer_id
243
        WHERE EXISTS (
244
            SELECT 1 
245
            FROM #on_behalf obc 
246
            WHERE obc.customer_id = cnn.customer_id
247
        )
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_non_product_header ccc 
253
            WHERE ccc.credit_note_id = cnn.credit_note_id
254
        )
255
        AND cnn.credit_note_no = 'CN24/003134'
256
    ) AS Combined
257
) AS Final
258
WHERE TotalRowNumber BETWEEN 1 AND 999999
259
ORDER BY TotalRowNumber;
260

    
261
 --Drop table #on_behalf