

drop table raena_analytics.pay_on_delivery_payment_dashboard;

CREATE TABLE raena_analytics.pay_on_delivery_payment_dashboard AS
SELECT A.*,
       payment_id,
       total_amount,
       payment_status,
       gateway_expiry,
       DATEDIFF(day, gateway_expiry::date, CURRENT_DATE) AS number_of_day_pending,
       payment_link,
       CASE WHEN no_of_retries >= 2 THEN 'Yes' ELSE 'No' END AS offender_reseller
FROM (
    SELECT
        A.id AS order_id,
        A.created_at::date AS order_created_date,
        A.status AS order_status,
        payment_amount,
        B.name AS Am_name,
        CASE
            WHEN B.email = C."emailid " THEN 'Denny'
            ELSE 'Manthan'
        END AS Team,
        json_extract_path_text(reseller_info, 'mobile', true) AS reseller_mobile,
        json_extract_path_text(reseller_info, 'name', true) AS reseller_name
    FROM raena_order_management.order A
    LEFT JOIN raena_user_management.admin_user B ON A.created_by = CAST(B.id AS VARCHAR)
    LEFT JOIN raena_analytics.denny_acq_team_member C ON B.email = C."emailid "
    WHERE A.id LIKE 'PL%'
) A
LEFT JOIN (
    SELECT
        order_id,
        id AS payment_id,
        total_amount,
        payment_status,
        gateway_expiry,
        gateway_expiry::date - CURRENT_DATE AS number_of_day_pending,
        CASE
            WHEN payment_provider = 'DurianPay' THEN json_extract_path_text(payment_details,'invoice_url',true)
            WHEN payment_provider = 'Xendit' THEN 'https://checkout.xendit.co/web/' || payment_provider_code
        END AS payment_link,
        no_of_retries
    FROM (
        SELECT
            REPLACE(CAST(json_extract_array_element_text(order_ids, 0) AS VARCHAR), '"', '') AS order_id,
            *
        FROM raena_order_management.payment p
    ) A
    WHERE order_id LIKE '%PL%'
) B ON A.order_id = B.order_id;


update raena_analytics.pay_on_delivery_payment_dashboard
set team = 'Denny'
where am_name = 'Iskandar Sani. S Kom' or  am_name = 'Rosidah';



DROP TABLE IF EXISTS raena_analytics.fullfillment_base_data ;


CREATE TABLE raena_analytics.fullfillment_base_data AS
SELECT DISTINCT B.order_id ,
                A.sku ,
                batch,
                split_part(batch,'__',1) po_number,
                shipment_id,
                quantity,
                sum(A.applied_cogs*(CASE WHEN quantity>0 THEN quantity ELSE 1 END))/sum(CASE WHEN quantity>0 THEN quantity ELSE 1 END) applied_cogs,
                A.cogs_type,
                A.expiry_date,
                C.lot_no
FROM raena_order_management.fulfillment_detail A
LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.shipment_id = B.id
LEFT JOIN
  (SELECT DISTINCT sku ,
                   reference_no,
                   expiry_date,
                   lot_no
   FROM raena_erp_management.inbound_grn_sku)C ON A.sku = C.sku
AND split_part(batch,'__',1) = C.reference_no
AND A.expiry_date = C.expiry_date
WHERE applied_cogs IS NOT NULL
  AND applied_cogs <>0
GROUP BY 1,
         2,
         3,
         4,
         5,
         6,
         8,
         9,
         10;
  
  
 
DROP TABLE IF EXISTS raena_analytics.cogs_base_data;


CREATE TABLE raena_analytics.cogs_base_data AS
SELECT sku,
       cogs ,
       promo ,
       CASE
           WHEN cogs <>0 THEN (cast(promo AS float)/cast(cogs AS float))*100
           ELSE 0
       END promo_percentage,
       (created_at+interval'7 hours')::date created_at,
       coalesce(lead((created_at+interval'7 hours')::date-interval'1 day' , 1) over (partition BY sku
        ORDER BY sku,created_at),((CURRENT_DATE+interval'7 hours')+interval'1 day'))::date last_date
FROM raena_catalog_management.cogs_audit ca
ORDER BY 1,
         4;
       

 DROP TABLE raena_analytics.order_sku_table;


CREATE TABLE raena_analytics.order_sku_table AS
SELECT A.order_id ,
       (A.created_at+interval'7 Hours')::date transaction_date,
       sales_sub_order_shipment_id,
       coalesce(B.sku,A.parent_sku) sku,
       coalesce(B.retail_price,A.retail_price)retail_price ,
       coalesce(B.quantity,A.quantity) quantity,
       coalesce(C.cogs,A.cogs) cogs,
       A.effective_cogs ,
       C.promo,
       C.promo_percentage,
       coalesce(B.payment_amount,A.payment_amount) payment_amount
FROM raena_order_management.sales_sub_order A
inner join (select id from raena_order_management.order 
             where (payment_status = 'Paid' or id like 'PL%') or (payment_status='Initiated' and is_cash_on_delivery='true')) BB on A.order_id = BB.id 
LEFT JOIN raena_order_management.sales_sub_order_parent_child B ON A.id = B.sales_sub_order_id
LEFT JOIN raena_analytics.cogs_base_data C ON B.sku = C.sku
AND B.product_class = 'Bundle'
AND (B.created_at+interval'7 Hours')::date BETWEEN C.created_at AND C.last_date
WHERE (A.created_at+interval'7 Hours')::date >='2023-01-01'
  AND A.status NOT IN ('Returned',
                       'Cancelled') ;    


 
DROP TABLE raena_analytics.consignment_base_data ;


CREATE TABLE raena_analytics.consignment_base_data AS
SELECT transaction_date,
       paid_status,
       F.name Brand_name ,
       D.sku raena_code,
       E.name sku_name,
       B.Quantity consignment_quantity,
       B.pkp_cogs,
       A.po_number,
       C.lot_no,
       batch_no,
       D.order_id ,
       applied_cogs ,
       cogs_type ,
       C.quantity fulfilled_quantity,
       C.shipment_id,
       D.sku,
       D.retail_price,
       D.quantity,
       D.cogs,
       D.effective_cogs,
       D.promo,
       D.promo_percentage,
       D.payment_amount
       from raena_analytics.order_sku_table D
left join raena_analytics.fullfillment_base_data C on D.order_id = C.order_Id
and sales_sub_order_shipment_id= shipment_id
AND D.sku = C.sku
left join raena_erp_management.inbound_order_sku B
on C.batch=B.batch_no
AND C.sku=B.raena_code
left join raena_erp_management.inbound_order A
on B.reference_no=A.po_number
left join raena_catalog_management.product E on D.sku = E.sku 
left join raena_catalog_management.brand F on E.brand_id = F.id 
WHERE transaction_date>='2023-01-01';

--FROM raena_erp_management.inbound_order A
--INNER JOIN raena_erp_management.inbound_order_sku B ON A.po_number = B.reference_no
--INNER JOIN raena_analytics.fullfillment_base_data C ON B.batch_no = C.batch
--AND B.raena_code = C.sku
--INNER JOIN raena_analytics.order_sku_table D ON C.order_id = D.order_id
--AND shipment_id = sales_sub_order_shipment_id
--AND C.sku = D.sku
--WHERE transaction_date>='2023-01-01';


drop table if exists raena_analytics.clevertap_event_data_vishnu_new;

create table raena_analytics.clevertap_event_data_vishnu_new
as
select ts::date ,events, name , email ,phone,
json_extract_path_text(event_props,'CT Session Id',true) sessionid ,
json_extract_path_text(event_props,'item_id',true) item_id ,
json_extract_path_text(event_props,'isGuestUser',true) isGuestUser ,
json_extract_path_text(event_props,'CT Source',true) Source ,
json_extract_path_text(event_props,'id',true) id ,
json_extract_path_text(event_props,'brandName',true) brandName ,
json_extract_path_text(event_props,'item_name',true) item_name ,
json_extract_path_text(event_props,'offenderItems',true) offenderItems ,
json_extract_path_text(event_props,'items',true) items ,
json_extract_path_text(event_props,'search_term',true) search_term ,
json_extract_path_text(event_props,'screen',true) screen,
json_extract_path_text(event_props,'position',true) position_value ,
json_extract_path_text(event_props,'story_name',true) story_name ,
json_extract_path_text(event_props,'caraousel_title',true) caraousel_title ,
json_extract_path_text(event_props,'title',true) title
from clevertap.clevertap_master_data where events in (
'brands_tab_press' ,
'brand_logo_carousel_press',
'App Launched',
'view_item',
'begin_checkout',
'search',
'Charged',
'App Installed',
'view_cart',
'add_to_cart',
'customer_search',
'Notification Clicked',
'Notification Viewed',
'Push Impressions',
'home_category',
--'pdp_brandprice',
'order_items_view',
--'sidebar_browsebrands',
'confirm_payment',
'finish_checkout',
'loyalty_page_visit',
'home_brand_Caraousel',
'home_brand_grid_view',
'WISHLIST',
'brand_carousel_press',
--'continue_shopping',
'home_banner',
'brandpage_image_banner_press',
'skip_login',
'App Uninstalled','coupon_applied_successfully',
'coupon_could_not_be_applied',
'view_rewards',
'view_order_checkout',
'coupon_remove',
--'homeintent_buyws',
'homepage_image_banner_press',
'home_continue_payment',
'homepage_carousel_category_press',
'flashsale_carousel_item_press',
'flashsale_carousel_view_all_press',
--'homeintent_managemp'
'home_product_Caraousel',
'bottom_bar_seller_center',
'bottom_bar_challenge'
)
and event_props not like '%OOREDOO%'
and ts::date between current_date+interval'-100 days'  and current_date ;

drop table if exists raena_analytics.clevertap_event_data_vishnu;

create table raena_analytics.clevertap_event_data_vishnu
as
select * from raena_analytics.clevertap_event_data_vishnu_new ;



