You are on page 1of 13

--alter table vnbi_tracking.

shopee_payment_module_order_status_info add column


exchange_rate decimal;
delete from vnbi_tracking.shopee_payment_module_order_status_info;
insert into vnbi_tracking.shopee_payment_module_order_status_info
with
payment_raw as
(
select payment_id
,transaction_id
,status
,extra_data
,amount/100000 as amount
,from_unixtime(ctime-3600) as pmt_ctime
,payment_type
,case when channel_id = 5000101 then 'Cybersource Installment'
when channel_id = 5001300 then 'Seller Wallet'
when channel_id = 5001800 then 'ShopeePay Wallet'
when channel_id = 5002900 then 'ShopeePay GIRO'
when channel_id = 5002601 then 'Shopee Pay Later'
when channel_id = 5000102 then 'Cybersource'
when channel_id = 5002800 then 'Online Banking'
when channel_id = 5003100 then 'Manual Bank Transfer'
when channel_id = 5007201 then 'Virtual Bank Transfer'
when channel_id = 5011000 then 'Airpay Authpay'
end as payment_channel
,p.channel_status
,CAST(JSON_EXTRACT_SCALAR(p.extra_data,
'$.params.checkout.checkout_id') AS BIGINT) AS checkout_id
-- CARD NUMBER coalesce theo thứ tự 4, 2, 3, 1 nha --
,trim(json_extract_scalar(extra_data,
'$.cybs_response.req_card_number')) as card_number_1
,trim(json_extract_scalar(extra_data, '$.channel_item.card_number')) as
card_number_2
,trim(json_extract_scalar(extra_data, '$.card_number')) as
card_number_3
,trim(json_extract_scalar(extra_data,
'$.params.channel_item_option_info.credit_card_data.card_number')) as card_number_4
from
shopeepay.shopee_payment_module_payment_vn_db__payment_v2_tab__vn_continuous_s0_liv
e p
where 1 = 1
and ctime >= cast(to_unixtime(date '2022-01-01') as bigint) + 3600
and payment_type = 4
and channel_id not in (5003200)
)
,get_order_id as
(
select payment_id
,transaction_id
,checkout_id
,status
,json_extract_scalar(order_info, '$.order_id') as order_id
,amount
,pmt_ctime
,payment_type
,coalesce(card_number_4, card_number_3, card_number_2, card_number_1)
as card_number
,substr(coalesce(card_number_4, card_number_3, card_number_2,
card_number_1), 1, 6) as bin
,payment_channel
,channel_status
from payment_raw
cross join unnest (cast(json_extract(extra_data, '$.params.checkout.orders') as
array < json>)) as orders(order_info)
where 1 = 1
)
,ckt_order_fraction as
(
select payment_id
,count(distinct order_id) as orders
from get_order_id
where 1=1
group by 1
)
-- GET PROVISION
,provision as
(
select entity_id
,transaction_id
,case when channel_id = 5005601 then 'Foody Order'
when channel_id = 5000301 then 'Digital Product'
when channel_id = 5000101 then 'Shopee Checkout'
when channel_id = 5008101 then 'Seller Center SVS Order'
when channel_id = 5006601 then 'Shopee Play Order'
when channel_id = 500740131 then 'Insurance General'
when channel_id = 500870131 then 'SPX COD Collection VN'
end as provision
from
shopeepay.shopee_payment_module_provision_vn_db__provision_v2_tab__vn_daily_s0_live
where 1 = 1
and ctime >= cast(to_unixtime(date '2022-01-01') as bigint) + 3600
and channel_id in (5000101, 5000301, 5005601, 5006601, 5008101, 500740131,
500870131)
)
-- GET REFUND ON SPM
,refund_spm as
(
select target_payment_id
,count(distinct payment_id) as refund_cnt
,count(distinct case when channel_id = 5001300 then payment_id end) as
refund_cnt_shopee_wallet
,sum(amount/100000) as refund_amt
,sum(case when channel_id = 5001300 then amount/100000 end) as
refund_amt_shopee_wallet
from
shopeepay.shopee_payment_module_payment_vn_db__payment_v2_tab__vn_continuous_s0_liv
e p
where 1 = 1
and ctime >= cast(to_unixtime(date '2022-01-01') as bigint) + 3600
and payment_type = 8
and (status in (20, 24) or (status not in (20, 24) and channel_status not
in ('150', '151', '152', '475', '476')))
and channel_id not in (5003200)
and (json_extract_scalar(extra_data, '$.void_result.reason_code_text') <>
'Success' or json_extract_scalar(extra_data, '$.void_result.reason_code_text') is
null)
-- and json_extract_scalar(extra_data, '$.refund_response._links.void.href')
is null
and REGEXP_LIKE(extra_data, 'void_result') = false
and (json_extract_scalar(extra_data, '$.void_result.reason_code_text') <>
'Success' or json_extract_scalar(extra_data, '$.void_result.reason_code_text') is
null)
group by 1
)
,void_result as
(
select target_payment_id
,case when json_extract_scalar(extra_data,
'$.void_result.reason_code_text') = 'Success' then 'void'
when REGEXP_LIKE(extra_data, 'void_result') then 'void'
-- when json_extract_scalar(extra_data,
'$.refund_response._links.void.href') is not null then 'void'
else 'refund'
end as void_result
from
shopeepay.shopee_payment_module_payment_vn_db__payment_v2_tab__vn_continuous_s0_liv
e
where 1 = 1
and currency = 'VND'
and status = 20 -- success
and channel_id IN (5000100, 5000101, 5000102) -- CC+ CC installment
and ctime >= cast(to_unixtime(date '2022-01-01') as int) + 3600
and payment_type = 8
-- and json_extract_scalar(extra_data, '$.void_result.reason_code_text') =
'Success'
-- and REGEXP_LIKE(extra_data, 'void_result')
-- and json_extract_scalar(extra_data, '$.refund_response._links.void.href')
)
-- MARKETPLACE ORDERS
,mp_order as
(
select order_id
,is_cb_shop
,order_be_status
,logistics_status
,case when payment_be_channel_id = 59000 then 'COD'
else 'Non-COD'
end as payment_method
,from_unixtime(create_timestamp-3600) as create_datetime
,gmv
,checkout_id
from mp_order.dwd_order_all_ent_df__vn_s0_live
where 1=1
and grass_date >= date '2022-01-01'
and create_timestamp >= cast(to_unixtime(date '2022-01-01') as bigint) +
3600
)
,refund_mp as
(
select order_id
,count(distinct refund_id) as refund_cnt
,count(distinct case when json_extract_scalar(_decoded_extinfo,
'$.refund_destination') = '5001300' then refund_id end) as refund_cnt_shopee_wallet
,sum(amount/100000) as refund_amt
,sum(case when json_extract_scalar(_decoded_extinfo,
'$.refund_destination') = '5001300' then amount/100000 else 0 end) as
refund_amt_shopee_wallet
from marketplace.shopee_refund_v2_db__refund_v2_tab__vn_continuous_s0_live r
where 1=1
and create_time >= cast(to_unixtime(date '2022-01-01') as bigint) + 3600
and status in (4, 5) -- REFUND_PAID = 4 REFUND_PAYOUT = 5
group by 1
)
,mp_summary as
(
select cast(o.order_id as varchar) as order_id
,concat('@',cast(o.order_id as varchar)) as order_id_str
,cast(o.checkout_id as varchar) as checkout_id
,o.is_cb_shop
,o.payment_method
,o.order_be_status as order_status
,o.logistics_status as fulfillment_status
,o.create_datetime
,o.gmv
,r.refund_amt
,r.refund_amt_shopee_wallet -- example order: paid by CYBS but refund
to Shopee Wallet orderid 119034019281205 MP
,r.refund_cnt
,r.refund_cnt_shopee_wallet
from mp_order o
left join refund_mp r
on o.order_id = r.order_id
where 1=1
)
-- DIGITAL PRODUCTS
,dp_order as
(
select order_id
,concat('@', order_id) as order_id_str
,pay_order_id as checkout_id
,0 as is_cb_shop
,'Non-COD' as payment_method
,case when o.order_status = 'OR1' then 'Order Created'
when o.order_status = 'OR2' then 'Order Expired'
when o.order_status = 'OR3' then 'Order Paid'
when o.order_status = 'OR4' then 'Order Cancelled'
when o.order_status = 'OR5' then 'Order Fulfilled'
when o.order_status = 'OR6' then 'Order Completed'
when o.order_status = 'OR7' then 'Order Refunded'
end as order_status
,case when fulfillment_status = 'F0' then 'Fulfillment Not Yet'
when fulfillment_status = 'F1' then 'Fulfillment Ready'
when fulfillment_status = 'F2' then 'Fulfillment Not Ready'
when fulfillment_status = 'F3' then 'Fulfillment Expired'
when fulfillment_status = 'F4' then 'Fulfillment Initiated'
when fulfillment_status = 'F5' then 'Fulfillment Successful'
when fulfillment_status = 'F6' then 'Fulfillment Failed'
when fulfillment_status = 'F7' then 'Fulfillment Pending'
end as fulfillment_status
,from_unixtime(create_time-3600) as create_datetime
,final_price/100000 as gmv
,case when refund_status = 'MR2' then final_price/100000 else 0 end as
refund_amt
,null as refund_amt_shopee_wallet
,null as refund_cnt
,null as refund_cnt_shopee_wallet
from digitalpurchase.vn_order_dwd_dp_trd_ord_df_s0_live o
where 1=1
and date(o.grass_date) = current_date - interval '1' day
and o.create_time >= cast(to_unixtime(date '2022-01-01') as bigint) + 3600
and o.platform = 1
and (final_price !=0 or (final_price = 0 and payment_status = 'P2'))
)
,foody_order as
(
select cast(id as varchar) as order_id
,concat('@', cast(id as varchar)) as order_id_str
,cast(id as varchar) as checkout_id
,0 as is_cb_shop
,'Non-COD' as payment_method
,case when order_status_id = 1 then 'Draft'
when order_status_id = 2 then 'Received'
when order_status_id = 3 then 'Processing'
when order_status_id = 4 then 'Verified'
when order_status_id = 5 then 'Assigned'
when order_status_id = 6 then 'Picked'
when order_status_id = 7 then 'Delivered'
when order_status_id = 8 then 'Cancel'
when order_status_id = 9 then 'Quit'
when order_status_id = 10 then 'ReAssigned'
when order_status_id = 11 then 'Incharged'
when order_status_id = 12 then 'Denied'
when order_status_id = 13 then 'Confirmed'
when order_status_id = 14 then 'M_Assigned'
when order_status_id = 15 then 'M_Received'
when order_status_id = 16 then 'M_TimeOut'
when order_status_id = 17 then 'M_OutOfService'
when order_status_id = 18 then 'M_Cooked'
when order_status_id = 19 then 'M_Changed'
when order_status_id = 20 then 'CanNotConnect'
when order_status_id = 21 then 'AutoAssign'
when order_status_id = 22 then 'AutoAssign_TimeOut'
when order_status_id = 23 then 'Pending'
when order_status_id = 24 then 'Rejected'
when order_status_id = 25 then 'Fail'
end as order_status
,case when order_status_id = 1 then 'Draft'
when order_status_id = 2 then 'Received'
when order_status_id = 3 then 'Processing'
when order_status_id = 4 then 'Verified'
when order_status_id = 5 then 'Assigned'
when order_status_id = 6 then 'Picked'
when order_status_id = 7 then 'Delivered'
when order_status_id = 8 then 'Cancel'
when order_status_id = 9 then 'Quit'
when order_status_id = 10 then 'ReAssigned'
when order_status_id = 11 then 'Incharged'
when order_status_id = 12 then 'Denied'
when order_status_id = 13 then 'Confirmed'
when order_status_id = 14 then 'M_Assigned'
when order_status_id = 15 then 'M_Received'
when order_status_id = 16 then 'M_TimeOut'
when order_status_id = 17 then 'M_OutOfService'
when order_status_id = 18 then 'M_Cooked'
when order_status_id = 19 then 'M_Changed'
when order_status_id = 20 then 'CanNotConnect'
when order_status_id = 21 then 'AutoAssign'
when order_status_id = 22 then 'AutoAssign_TimeOut'
when order_status_id = 23 then 'Pending'
when order_status_id = 24 then 'Rejected'
when order_status_id = 25 then 'Fail'
end as fulfillment_status
,from_unixtime(create_timestamp-3600) as create_datetime
,o.paid_by_user as gmv
,null as refund_amt
,null as refund_amt_shopee_wallet
,null as refund_cnt
,null as refund_cnt_shopee_wallet
from shopeefood.foody_mart__fact_gross_order_join_detail o
where 1=1
and app_type_id in (50, 51)
and create_timestamp >= cast(to_unixtime(date '2022-01-01') as bigint) +
3600
)
,order_summary as
(
select * from mp_summary
union all
select * from dp_order
union all
select * from foody_order
)
-- SUMMARY
,joined as
(
select p.payment_id
,p.transaction_id
,p.checkout_id
,p.status
,case when v.provision = 'Digital Product' then coalesce(d.order_id,
p.order_id)
else p.order_id
end as order_id
,coalesce(o.is_cb_shop, 0) as is_cb_shop
,case when v.provision = 'Digital Product' then
coalesce(d.order_status, o.order_status)
else o.order_status
end as order_status
,case when v.provision = 'Digital Product' then
coalesce(d.fulfillment_status, o.fulfillment_status)
else o.fulfillment_status
end as fulfillment_status
,case when v.provision = 'Digital Product' then
coalesce(d.create_datetime, o.create_datetime)
else coalesce(o.create_datetime, p.pmt_ctime)
end as order_create_datetime
,case when v.provision = 'Digital Product' then coalesce(d.gmv, o.gmv)
when v.provision in ('Shopee Checkout', 'Foody Order') then o.gmv
else p.amount
end as gmv
,p.amount as pmt_amount
,p.pmt_ctime
,p.payment_type
,p.card_number
,p.bin
,p.payment_channel
,p.channel_status
,v.entity_id
,v.provision
,b.card_type
,b.brand
,b.country
,case when b.bank_name_2 like '%INDOVINA%' then 'Indovina Bank'
when b.bank_name_2 in ('Ngân hàng TNHH Indovina - Indovina Bank
Limited') then 'Indovina Bank'
when b.bank_name_2 like '%AN BINH C%' then 'ABBank'
when b.bank_name_2 like '%ACB%' then 'ACB'
when b.bank_name_2 like '%VIETNAM BANK FOR AGRICULTURE%' then
'AgriBank'
when b.bank_name_2 like '%BIDV%' then 'BIDV'
when b.bank_name_2 like '%ĐẦU TƯ VÀ PHÁT TRIỂN VIỆT NAM%' then
'BIDV'
when b.bank_name_2 like '%CIMB%' then 'CIMB'
when b.bank_name_2 like '%CITIBANK%' then 'Citibank'
when b.bank_name_2 in ('DONGA COMMERCIAL JSB') then 'DongABank'
when b.bank_name_2 like '%VIETNAM EXPORT IMPORT%' then
'EximBank'
when b.bank_name_2 like 'FE%CREDIT%' then 'FE Credit'
when b.bank_name_2 like 'HO CHI MINH CITY DEVELOPMENT%' then
'HDBank'
when b.bank_name_2 in ('Ho Chi Minh City Development Commercial
Joint Stock Bank', 'Ho Chi Minh City Development Joint Stock Commercial Bank') then
'HDBank'
when b.bank_name_2 like 'HDSAISON' then 'HDSaison'
when b.bank_name_2 like 'HOME CREDIT%' then 'Home Credit'
when b.bank_name_2 in ('Home Credit Vietnam Finance Company
Limited') then 'Home Credit'
when b.bank_name_2 like 'HONG LEONG BANK%' then 'HONG LEONG '
when b.bank_name_2 like '%HONGKONG AND SHANGHAI BANKING%' then
'HSBC'
when b.bank_name_2 like 'HSBC%' then 'HSBC'
when b.bank_name_2 like 'KIEN LONG COMMERCIAL%' then 'Kien Long
Bank'
when b.bank_name_2 like 'KIEN LONG%' then 'Kien Long Bank'
when b.bank_name_2 in ('Kien Long Commercial Joint Stock Bank')
then 'Kien Long Bank'
when b.bank_name_2 like 'MILITARY COMMERCIAL%' then 'MB Bank'
when b.bank_name_2 like 'MS BANK%' then 'MSB'
when b.bank_name_2 like 'MSB%' then 'MSB'
when b.bank_name_2 like 'VIETNAM MARITIME COMMERCIAL%' then
'MSB'
when b.bank_name_2 like 'NAM%ABANK%' then 'NamABank'
when b.bank_name_2 like 'NAM%Á%' then 'NamABank'
when b.bank_name_2 in ('National Citizen Commercial Joint Stock
Bank') then 'NCB'
when b.bank_name_2 in ('Ocean Commercial Joint Stock Bank')
then 'OceanBank'
when b.bank_name_2 in ('Orient Commercial Joint Stock Bank')
then 'OCB'
when b.bank_name_2 like '%PETROLIMEX%' then 'PGBank'
when b.bank_name_2 in ('NH TMCP Xăng dầu Petrolimex -
Petrolimex Group Commercial Joint Stock Bank (PGB)') then 'PGBank'
when b.bank_name_2 like ('%Dai%Chung%') then 'PVComBank'
when b.bank_name_2 like '%SAIGON THUONG TIN%' then 'Sacombank'
when b.bank_name_2 like '%SACOMBANK%' then 'Sacombank'
when b.bank_name_2 like 'SCB%' then 'SCB'
when b.bank_name_2 like '%SOUTHEAST ASIA COMMERCIAL%BANK%' then
'SeABank'
when b.bank_name_2 like '%SEA%BANK%' then 'SeABank'
when b.bank_name_2 like 'SAIGON HANOI COMMERCIAL%BANK%' then
'SHB'
when b.bank_name_2 in ('Saigon Hanoi Commercial Joint Stock
Bank') then 'SHB'
when b.bank_name_2 like 'SHINHAN%BANK%' then 'ShinhanBank'
when b.bank_name_2 in ('Shinhan Bank Vietnam', 'ShinhanBank')
then 'ShinhanBank'
when b.bank_name_2 like 'STANDARD%CHARTERED%' then 'Standard
Chartered'
when b.bank_name_2 like 'VIETNAM%TECHNOLOGICAL AND COMMERCIAL%'
then 'Techcombank'
when b.bank_name_2 like 'TIENPHONG%BANK%' then 'TPBank'
when b.bank_name_2 like 'TPBANK%' then 'TPBank'
when b.bank_name_2 in ('TienPhong Commercial Joint Stock Bank')
then 'TPBank'
when b.bank_name_2 like 'UNITED OVERSEAS BANK%' then 'UOB'
when b.bank_name_2 like 'VIB%' then 'VIB'
when b.bank_name_2 like '%VIETNAM INTERNATIONAL COMMERCIAL
JOINT STOCK BANK%' then 'VIB'
when b.bank_name_2 like 'VIETBANK%' then 'VietBank'
when b.bank_name_2 like 'VIETNAM THUONG TIN%' then 'VietBank'
when b.bank_name_2 in ('Vietnam Thuong Tin Commercial Joint
Stock Bank') then 'VietBank'
when b.bank_name_2 in ('VIETNAM-ASIA COMMERCIAL JSB') then
'VietABank'
when b.bank_name_2 like 'VIET%CAPITAL%BANK%' then 'VietCapital'
when b.bank_name_2 in ('Viet Capital Bank - NHTMCP Ban Viet')
then 'VietCapital'
when b.bank_name_2 like 'BANK%FOREIGN TRADE OF VIETNAM%' then
'Vietcombank'
when b.bank_name_2 like '%VIETCOMBANK%' then 'Vietcombank'
when b.bank_name_2 like 'VIETNAM%INDUSTRY AND TRADE%' then
'VietinBank'
when b.bank_name_2 in ('INDUSTRIAL AND COMMERCIAL BANK OF
VIETNAM') then 'VietinBank'
when b.bank_name_2 like 'VPBANK%' then 'VPBank'
when b.bank_name_2 like 'VP%BANK%' then 'VPBank'
when b.bank_name_2 like 'VIETNAM%RUSSIA%' then 'VRB'
when b.bank_name_2 in ('Vietnam - Russia Joint Venture Bank')
then 'VRB'
when b.bank_name_2 in ('Lien Viet Post Joint Stock Commercial
Bank') then 'Lien Viet Post Bank'
else b.bank_name_2
end as bank_name
-- ,coalesce(o.refund_cnt, r.refund_cnt, 0) as refund_cnt
,case when v.provision = 'Shopee Checkout' and p.status = 20 then
o.refund_cnt
when p.status = 20 then r.refund_cnt
else 0
end as refund_cnt
,case when v.provision = 'Shopee Checkout' and p.status = 20 then
o.refund_amt
when p.status = 20 then r.refund_amt
else 0
end as refund_amt
,coalesce(o.refund_cnt_shopee_wallet, r.refund_cnt_shopee_wallet) as
refund_cnt_shopee_wallet
,coalesce(o.refund_amt_shopee_wallet, r.refund_amt_shopee_wallet) as
refund_amt_shopee_wallet
,case when o.payment_method = 'COD' then 1 else 0 end as
is_switched_to_cod
from get_order_id p
join provision v
on p.transaction_id = v.transaction_id
left join vnbi_tracking.shopee_payment_module_bin_bank_card_type b
on p.bin = b.bin
left join refund_spm r
on p.payment_id = r.target_payment_id
left join order_summary o
on p.order_id = cast(o.order_id as varchar)
left join dp_order d
on p.order_id = d.checkout_id
where 1 = 1
)
,exrate as
(
select grass_date
,CAST(exchange_rate AS double) as exchange_rate
,currency
from mp_order.dim_exchange_rate__reg_s0_live
where 1=1
and grass_region = 'VN'
and grass_date >= date '2022-01-01'
)
,summary as
(
select p.payment_id
,p.transaction_id
,p.checkout_id
,case when p.status = 0 then 'INITIAL'
when p.status = 2 then 'PAYMENT_INIT'
when p.status = 4 then 'AUTHORIZE'
when p.status = 6 then 'USER_PROCESSING'
when p.status = 8 then 'PENDING'
when p.status = 9 then 'BINDING'
when p.status = 10 then 'BLOCKED'
when p.status = 14 then 'RECONCILING'
when p.status = 16 then 'LOCAL_OPS_PROCESSING'
when p.status = 17 then 'SSG_BLOCKED'
when p.status = 20 then 'SUCCESS'
when p.status = 22 then 'FAILED'
when p.status = 24 then 'LATE_SUCCESS'
when p.status = 26 then 'SUCCESS_BUT_REJECTED_BY_SERVER'
when p.status = 40 then 'OVERPAID'
when p.status = 50 then 'CANCELLED'
when p.status = 51 then 'CANCEL_READY'
when p.status = 52 then 'VOID_READY'
when p.status = 54 then 'EXPIRED'
when p.status = 100 then 'FRAUD'
when p.status = 999 then 'REJECTED'
else cast(p.status as varchar)
end as status
,p.order_id
,p.order_status
,p.fulfillment_status
,p.order_create_datetime
,p.pmt_amount
,p.pmt_ctime
,p.payment_type
,p.card_number
,p.bin
,p.payment_channel
,p.channel_status
,case when p.payment_channel in ('Cybersource', 'Cybersource
Installment') then coalesce(v.void_result, 'non-void')
else null
end as void_result
,p.entity_id
,p.provision
,p.card_type
,p.brand
,p.country
,p.bank_name
,p.is_cb_shop
,p.gmv as gmv_vnd
,case when p.payment_channel in ('ShopeePay Wallet', 'ShopeePay
GIRO', 'Airpay Authpay') then 'SPP'
when p.payment_channel in ('Cybersource', 'Cybersource
Installment') and p.brand = 'AMEX' and p.is_cb_shop = 0 then 'CC-VCB-AMEX'
when p.payment_channel in ('Cybersource', 'Cybersource
Installment') and p.brand <> 'AMEX' and bank_name = 'Vietcombank' and p.is_cb_shop
= 0 then 'CC-VCB-VCB'
when p.payment_channel in ('Cybersource', 'Cybersource
Installment') and p.brand <> 'AMEX' and bank_name <> 'Vietcombank' and country =
'VN' and p.is_cb_shop = 0 then 'CC-VCB-VN'
when p.payment_channel in ('Cybersource', 'Cybersource
Installment') and country <> 'VN' and p.is_cb_shop = 0 then 'CC-VCB-NN'
when p.payment_channel in ('Cybersource', 'Cybersource
Installment') and country = 'VN' and p.is_cb_shop = 1 then 'CC-Sacom-VN'
when p.payment_channel in ('Cybersource', 'Cybersource
Installment') and country <> 'VN' and p.is_cb_shop = 1 then 'CC-Sacom-NN'
else 'Free'
end as scheme_charge
,coalesce(p.refund_cnt, 0) as refund_cnt
,coalesce(p.refund_amt, 0) as refund_amt
,case when p.provision = 'Shopee Checkout' and p.pmt_amount <> 0 then
cast(p.gmv as double)/p.pmt_amount
else 1
end as ckt_amt_fraction
,case when p.provision = 'Shopee Checkout' and c.orders <> 0 then
1.00/c.orders
else 1
end as ckt_order_fraction
,coalesce(p.refund_cnt_shopee_wallet, 0) as refund_cnt_shopee_wallet
,coalesce(p.refund_amt_shopee_wallet, 0) as refund_amt_shopee_wallet
,coalesce(p.is_switched_to_cod, 0) as is_switched_to_cod
,fx.exchange_rate
from joined p
join ckt_order_fraction c
on p.payment_id = c.payment_id
left join void_result v
on p.payment_id = v.target_payment_id
left join exrate fx
on date(p.pmt_ctime) = fx.grass_date
where 1=1
)
,add_cost as
(
select *
-- FIXED CHARGE
,case when payment_channel in ('Cybersource', 'Cybersource
Installment')
and (status = 'SUCCESS' or (status <> 'SUCCESS' and
channel_status not in ('150', '151', '152', '475', '476')))
then 0.036*exchange_rate*ckt_order_fraction +
0.036*exchange_rate*(refund_cnt - refund_cnt_shopee_wallet)
else 0
end as cybs_api_cost
,case when payment_channel in ('Cybersource', 'Cybersource
Installment') and is_cb_shop = 0 and void_result <> 'void'
and status = 'SUCCESS'
then (1500/1.1)*ckt_order_fraction +
(1500/1.1)*(refund_cnt - refund_cnt_shopee_wallet)
when payment_channel in ('Cybersource', 'Cybersource Installment')
and is_cb_shop = 1 and void_result <> 'void'
and status = 'SUCCESS'
then (2000/1.1)*ckt_order_fraction +
(2000/1.1)*(refund_cnt - refund_cnt_shopee_wallet)
else 0
end as acq_fixed_cost
-- PERCENTAGE CHARGE BASE
,case when payment_channel in ('ShopeePay Wallet', 'ShopeePay GIRO',
'Shopee Pay Later', 'Airpay Authpay') and scheme_charge = 'SPP' and status =
'SUCCESS'
then cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet
when payment_channel in ('Cybersource', 'Cybersource
Installment') and status = 'SUCCESS' and void_result <> 'void'
then cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet
when payment_channel not in ('ShopeePay Wallet', 'ShopeePay
GIRO', 'Shopee Pay Later', 'Airpay Authpay', 'Cybersource', 'Cybersource
Installment')
then cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet
else 0
end as base_pctg_cost_pmt_amt
-- PERCENTAGE COST
,case when payment_channel in ('ShopeePay Wallet', 'ShopeePay GIRO',
'Shopee Pay Later', 'Airpay Authpay') and scheme_charge = 'SPP' and status =
'SUCCESS'
then (cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet)*(cast(1.5 as double)/100)
when payment_channel in ('Cybersource', 'Cybersource
Installment') and status = 'SUCCESS' and void_result <> 'void' and scheme_charge =
'CC-VCB-AMEX'
then (cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet)*(cast(2.05 as double)/100)
when payment_channel in ('Cybersource', 'Cybersource
Installment') and status = 'SUCCESS' and void_result <> 'void' and scheme_charge =
'CC-VCB-VCB'
then (cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet)*(cast(1.31 as double)/100)
when payment_channel in ('Cybersource', 'Cybersource
Installment') and status = 'SUCCESS' and void_result <> 'void' and scheme_charge =
'CC-VCB-VN'
then (cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet)*(cast(1.47 as double)/100)
when payment_channel in ('Cybersource', 'Cybersource
Installment') and status = 'SUCCESS' and void_result <> 'void' and scheme_charge =
'CC-VCB-NN'
then (cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet)*(cast(1.88 as double)/100)
when payment_channel in ('Cybersource', 'Cybersource
Installment') and status = 'SUCCESS' and void_result <> 'void' and scheme_charge =
'CC-Sacom-VN'
then (cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet)*(cast(2.1 as double)/1.1/100)
when payment_channel in ('Cybersource', 'Cybersource
Installment') and status = 'SUCCESS' and void_result <> 'void' and scheme_charge =
'CC-Sacom-NN'
then (cast(pmt_amount as double)*ckt_amt_fraction -
refund_amt + refund_amt_shopee_wallet)*(cast(2.5 as double)/1.1/100)
else (cast(pmt_amount as double)*ckt_amt_fraction - refund_amt +
refund_amt_shopee_wallet)*0
end as pctg_cost
from summary
where 1=1
)
select payment_id
,transaction_id
,checkout_id
,status
,order_id
,order_status
,fulfillment_status
,order_create_datetime
,pmt_amount
,pmt_ctime
,payment_type
,card_number
,bin
,payment_channel
,channel_status
,void_result
,entity_id
,provision
,card_type
,brand
,country
,bank_name
,is_cb_shop
,gmv_vnd
,scheme_charge
,refund_cnt
,refund_amt
,ckt_amt_fraction
,ckt_order_fraction
,refund_cnt_shopee_wallet
,refund_amt_shopee_wallet
,is_switched_to_cod
,cybs_api_cost
,acq_fixed_cost
,base_pctg_cost_pmt_amt
,pctg_cost
,exchange_rate
from add_cost
-- select date(date_trunc('month', pmt_ctime)) as month
-- ,scheme_charge
-- ,is_cb_shop
-- ,sum(acq_pctg_cost/exchange_rate) as acq_pctg_cost
-- ,sum(acq_fixed_cost/exchange_rate) as acq_fixed_cost
-- from add_cost
-- group by 1, 2, 3
;

--select * from vnbi_tracking.shopee_payment_module_order_status_info limit 1000;

You might also like