File size: 12,878 Bytes
245e020
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

queries = {
    # Query 1
    '1. 09-Sheet1': '''
        with main_table as (
        SELECT
        *
        FROM
        `fynd-db.Outstanding.09_Payable_File_table`
        where
        expected_payout_date <= '{selected_date}'
        ),
        calculated_table as (
        select
        *
        from
        (with Bag_count as
        (select 
        bag_id,
        count(bag_id) as bag_count
        from
        `fynd-db.Outstanding.09_Payable_File_table`
        where
        expected_payout_date <= '{selected_date}'
        group by
        1
        ),
        merged_count  as
        (select 
        concat(bag_id,transaction_type) as merged,
        count(concat(bag_id,transaction_type)) as merged_count
        from
        `fynd-db.Outstanding.09_Payable_File_table`
        where
        expected_payout_date <= '{selected_date}'
        group by
        1
        )
        select
        A.bag_id,
        settlement_type,
        transaction_type,
        A.merged,
        B.bag_count,
        C.merged_count,
        settled_merged,
        A.seller_net_collection,
        A.net_remitted,
        collection_payout_diff,
        Collection_amount_comment,
        paid_settled_merged_transaction,
        REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)')) as cc1,
        case 
        when A.bag_id in (select 
        bag_id,
        from
        `fynd-db.Outstanding.09_Payable_File_table`
        where
        expected_payout_date <= '{selected_date}'
        group by
        1
        having
        round(sum(seller_net_collection),0) in (0)) then 'collected'
        when Collection_amount_comment = 'Collection_Pending' and paid_settled_merged_transaction is null then 'Collection_Pending'
        when Collection_amount_comment = 'collection_diff' and paid_settled_merged_transaction is null then 'Collection_Pending'
        when bag_count = 3 and merged_count = 1 and collection_payout_diff in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false or paid_settled_merged_transaction is null) and  Collection_amount_comment = 'Match' then 'collected'
        when bag_count = 3 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false or paid_settled_merged_transaction is null) and  Collection_amount_comment = 'Collection_Pending' then 'collected'
        when bag_count = 3 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false or paid_settled_merged_transaction is null) and  Collection_amount_comment = 'collection_diff' then 'collected'
        when bag_count = 2 and merged_count = 1 and collection_payout_diff in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false or paid_settled_merged_transaction is null) and  Collection_amount_comment = 'Match' then 'collected'
        when bag_count = 2 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false or paid_settled_merged_transaction is null) and  Collection_amount_comment = 'Collection_Pending' then 'collected'
        when bag_count = 2 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false or paid_settled_merged_transaction is null) and  Collection_amount_comment = 'collection_diff' then 'collected'
        when bag_count = 1 and merged_count = 1 and collection_payout_diff in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false or paid_settled_merged_transaction is null) and  Collection_amount_comment = 'Match' then 'collected'
        when bag_count = 1 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false) and seller_net_collection < 0  and seller_net_collection < 0 and  Collection_amount_comment = 'Collection_Pending' then 'collected'
        when bag_count = 1 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and paid_settled_merged_transaction is null and  Collection_amount_comment = 'Collection_Pending' then 'Collection_Pending'
        when bag_count = 1 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)'))  = false) and  Collection_amount_comment = 'collection_diff' and seller_net_collection < 0 then 'collected' 
        when bag_count = 1 and merged_count = 1 and collection_payout_diff not in (0,1,-1) and paid_settled_merged_transaction is null and  Collection_amount_comment = 'collection_diff' and seller_net_collection < 0 then 'Collection_Pending' else 'Collection_Pending'
        end as Final_Comment
        from `fynd-db.Outstanding.09_Payable_File_table` as A
        left join
        Bag_count as B
        on
        A.bag_id = B.bag_id
        left join
        merged_count as C
        on
        A.merged = C.merged
        -- where
        --   A.bag_id not in (select 
        -- bag_id,
        -- from
        -- `fynd-db.Outstanding.09_Payable_File_table`
        -- group by
        --   1
        -- having
        -- round(sum(seller_net_collection),0) in (0))
        -- and A.bag_id = 21628339
        where
        expected_payout_date <= '{selected_date}'
        order by
        1 asc)
        -- where
        --   Final_Comment is null
        order by
        bag_id asc
        )

        select 
        *
        from
        main_table 
        left join
        calculated_table
        on 
        main_table.merged = calculated_table.merged
    ''',

    '2. 11-Sheet2': '''
        SELECT
        *
        FROM
        `fynd-db.Outstanding.11_fees_payable`
        where
        expected_payout_date <= '{selected_date}'
    ''',


    '3. 12-Sheet3': '''
        SELECT
        *
        FROM
        `fynd-db.Outstanding.12_claim_payable`
        where
        claim_settle_date <= '{selected_date}'
        ''',


    '4. MD-Sheet4': '''
        SELECT
        *
        FROM
        `fynd-db.Outstanding.Manual_Dispute`
        ''',


    '5. Not_match_09-Sheet5': '''
        WITH main_table AS (
            SELECT *
            FROM `fynd-db.Outstanding.09_Payable_File_table`
            WHERE expected_payout_date <= '{selected_date}'
        ),
        calculated_table AS (
            SELECT *
            FROM (
                WITH Bag_count AS (
                    SELECT 
                        bag_id,
                        COUNT(bag_id) AS bag_count
                    FROM `fynd-db.Outstanding.09_Payable_File_table`
                    WHERE expected_payout_date <= '{selected_date}'
                    GROUP BY 1
                ),
                merged_count AS (
                    SELECT 
                        CONCAT(bag_id, transaction_type) AS merged,
                        COUNT(CONCAT(bag_id, transaction_type)) AS merged_count
                    FROM `fynd-db.Outstanding.09_Payable_File_table`
                    WHERE expected_payout_date <= '{selected_date}'
                    GROUP BY 1
                )
                SELECT
                    A.bag_id,
                    settlement_type,
                    transaction_type,
                    A.merged,
                    B.bag_count,
                    C.merged_count,
                    settled_merged,
                    A.seller_net_collection,
                    A.net_remitted,
                    collection_payout_diff,
                    Collection_amount_comment,
                    paid_settled_merged_transaction,
                    REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)')) AS cc1,
                    CASE 
                        WHEN A.bag_id IN (
                            SELECT bag_id
                            FROM `fynd-db.Outstanding.09_Payable_File_table`
                            WHERE expected_payout_date <= '{selected_date}'
                            GROUP BY 1
                            HAVING ROUND(SUM(seller_net_collection), 0) IN (0)
                        ) THEN 'collected'
                        WHEN Collection_amount_comment = 'Collection_Pending' AND paid_settled_merged_transaction IS NULL THEN 'Collection_Pending'
                        WHEN Collection_amount_comment = 'collection_diff' AND paid_settled_merged_transaction IS NULL THEN 'Collection_Pending'
                        WHEN bag_count = 3 AND merged_count = 1 AND collection_payout_diff IN (0,1,-1) 
                            AND (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)')) = FALSE OR paid_settled_merged_transaction IS NULL) 
                            AND Collection_amount_comment = 'Match' THEN 'collected'
                        WHEN bag_count = 3 AND merged_count = 1 AND collection_payout_diff NOT IN (0,1,-1) 
                            AND (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)')) = FALSE OR paid_settled_merged_transaction IS NULL) 
                            AND Collection_amount_comment IN ('Collection_Pending', 'collection_diff') THEN 'collected'
                        WHEN bag_count = 1 AND merged_count = 1 AND collection_payout_diff NOT IN (0,1,-1) 
                            AND paid_settled_merged_transaction IS NULL 
                            AND Collection_amount_comment = 'Collection_Pending' THEN 'Collection_Pending'
                        WHEN bag_count = 1 AND merged_count = 1 AND collection_payout_diff NOT IN (0,1,-1) 
                            AND (REGEXP_CONTAINS(paid_settled_merged_transaction, CONCAT(r'(^|,)', A.merged, r'(,|$)')) = FALSE) 
                            AND Collection_amount_comment = 'collection_diff' 
                            AND seller_net_collection < 0 THEN 'collected'
                        ELSE 'Collection_Pending'
                    END AS Final_Comment
                FROM `fynd-db.Outstanding.09_Payable_File_table` AS A
                LEFT JOIN Bag_count AS B ON A.bag_id = B.bag_id
                LEFT JOIN merged_count AS C ON A.merged = C.merged
                WHERE expected_payout_date <= '{selected_date}'
                ORDER BY 1 ASC
            )
            ORDER BY bag_id ASC
        ),
        sub_query AS (
            SELECT *
            FROM main_table 
            LEFT JOIN calculated_table ON main_table.merged = calculated_table.merged
        )
        SELECT 
        sett_id, 
        bag_id, 
        settlement_type,
        '' as transaction_type,
        concat(bag_id, settlement_type) as merged,
        '' as New_sett_id
        FROM `finance_recon_tool_asia.09_seller_net_collection_daily` AS nine
        WHERE CONCAT(bag_id, settlement_type) NOT IN (
            SELECT merged_09
            FROM sub_query
        )
        AND nine.sett_id IN (
            SELECT sett_id
            FROM sub_query
        )
        ''',

    '6. Not_match_11-Sheet6': '''
        WITH main_table AS (
            SELECT *
            FROM   `fynd-db.Outstanding.11_fees_payable`
            WHERE expected_payout_date <= '{selected_date}'
        )
        SELECT 
        sett_id, 
        bag_id, 
        '' as settlement_type,
        transaction_type,
        concat(bag_id, transaction_type) as merged,
        '' as New_sett_id
        FROM `finance_recon_tool_asia.11_seller_fees_daily` AS nine
        WHERE CONCAT(bag_id, transaction_type) NOT IN (
            SELECT merged
            FROM main_table
        )
        AND nine.sett_id IN (
            SELECT sett_id
            FROM main_table
        )
        ''',

    '7. Not_match_12-Sheet7 ': '''
        WITH main_table AS (
            SELECT 
            *
            FROM  `fynd-db.Outstanding.12_claim_payable`
            WHERE claim_settle_date <= '{selected_date}'
        )
        SELECT 
        sett_id, 
        '' as bag_id, 
        '' as settlement_type,
        '' as transaction_type,
        concat(current_shipment_id, recon_status) as merged,
        '' as New_sett_id
        FROM `fynd-db.finance_recon_tool_asia.12_seller_claims_daily` AS nine
        WHERE concat(current_shipment_id, recon_status) NOT IN (
            SELECT 
            concat(current_shipment_id, recon_status) as merged 
            FROM main_table
        )
        AND nine.sett_id IN (
            SELECT sett_id
            FROM main_table
        )
        '''
}