

DROP TABLE IF EXISTS public.manual_bundle_sku_data ;


CREATE TABLE public.manual_bundle_sku_data AS
SELECT upper(bundle_sku) Parent_sku ,
       Upper(A.sku) child_sku ,
       A.quantity child_quantity,
       'Bundle' parent_sku_class,
                cast(B.retail_price AS decimal(22,2)) bundle_retail_price,
                cast(json_extract_path_text(C.tier_price,'07030fbe-5801-4318-9e97-fe33fa169894',TRUE) AS decimal(22,2)) bronze_price,
                cast(json_extract_path_text(C.tier_price,'8eb95d6e-915a-4a91-9c12-fa43db995e19',TRUE) AS decimal(22,2)) silver_price,
                cast(json_extract_path_text(C.tier_price,'bf645e97-8a48-4977-8367-e987489760f9',TRUE) AS decimal(22,2)) gold_price,
                cast(C.retail_price AS decimal(22,2)) child_retail_price
FROM bundle_data_manual_new A
INNER  JOIN raena_catalog_management.product B ON upper(A.bundle_sku) = upper(B.sku)
INNER  JOIN raena_catalog_management.product C ON upper(A.sku) = upper(C.sku);


DROP TABLE IF EXISTS public.order_level_data;


CREATE TABLE public.order_level_data AS
SELECT DISTINCT external_id,
                transaction_date+interval'7 hours' AS transaction_date,
                                         discount_amount,
                                         shipping_cost,
                                         A.coupon_code,
                                         reseller_tier_name,
                                         CASE
                                             WHEN flash_sale_id IS NOT NULL THEN 'Flash'
                                         END Product_type,
                                         order_loyalty_discount,
                                         total_amount ,
                                         total_retail_price ,
                                         tier_discount,
                                         total_dynamic_and_tier_price,
                                         payment_amount,
                                         coupon_applied_on,
                                         is_campaign,
                                         order_placed_by
FROM
  (SELECT A.id AS external_id ,
          (A.created_at) AS transaction_date ,
          A.discount_amount,
          applied_shipping_amount shipping_cost,
          A.coupon_code,
          json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
          flash_sale_id,
          loyalty_discount AS order_loyalty_discount,
          total_amount ,
          total_retail_price ,
          tier_discount,
          total_dynamic_and_tier_price,
          payment_amount,
          is_campaign,
          order_placed_by
   FROM raena_order_management.order A
   WHERE payment_status='Paid'
     AND cast(A.created_at+interval '7 hours' AS date) >='2023-09-24' ) A
LEFT JOIN raena_order_management.discount_coupon C ON A.coupon_code = C.coupon_code;


SELECT max(created_at)
FROM raena_order_management.order ;


DROP TABLE IF EXISTS public.base_netsuite_stage1_V1;


CREATE TABLE public.base_netsuite_stage1_V1 AS
SELECT DISTINCT transaction_date transaction_date,
                A.external_id,
                CASE
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_sku
                    WHEN B.id = F.sales_sub_order_id THEN F.sku
                    ELSE B.parent_sku
                END sku,
                B.parent_sku parent_sku,
                CASE
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_quantity*B.quantity
                    WHEN B.id = F.sales_sub_order_id THEN F.quantity
                    ELSE B.quantity
                END quantity,
                B.quantity parent_quantity,
                CASE
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar) THEN D.child_retail_price
                    WHEN B.id = F.sales_sub_order_id THEN F.retail_price
                    ELSE B.retail_price
                END retail_price,
                B.retail_price parent_retail_price,
                CASE
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
                         AND reseller_tier_name = 'BRONZE' THEN bronze_price
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
                         AND reseller_tier_name = 'SILVER' THEN silver_price
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
                         AND reseller_tier_name = 'GOLD' THEN gold_price
                    WHEN B.id = F.sales_sub_order_id THEN F.effective_wholesale_price
                    ELSE B.effective_wholesale_price
                END effective_wholesale_price,
                B.effective_wholesale_price parent_wholesale_price,
                CASE
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
                         AND reseller_tier_name = 'BRONZE' THEN bronze_price
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
                         AND reseller_tier_name = 'SILVER' THEN silver_price
                    WHEN cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
                         AND reseller_tier_name = 'GOLD' THEN gold_price
                    WHEN B.id = F.sales_sub_order_id THEN F.actual_wholesale_price
                    ELSE B.actual_wholesale_price
                END actual_wholesale_price,
                B.actual_wholesale_price parent_actual_wholesale_price,
                CASE
                    WHEN B.id = F.sales_sub_order_id THEN F.coupon_discount
                    ELSE B.coupon_discount
                END discount_price,
                CASE
                    WHEN discount_amount > 0
                         AND A.coupon_code IS NULL
                         AND A.order_placed_by = 'admin'
                         AND coupon_applied_on IS NULL THEN 'Cart'
                    ELSE coupon_applied_on
                END coupon_applied_on,
                A.discount_amount,
                0 Dynamic_price ,
                  0 parent_dynamic_price,
                    CASE
                        WHEN B.id = F.sales_sub_order_id THEN F.payment_amount
                        ELSE B.payment_amount
                    END payment_price,
                    A.payment_amount,
                    B.product_class,
                    coalesce(F.product_class,B.product_class) parent_product_class,
                    CASE
                        WHEN A.product_type = 'Flash'
                             AND B.parent_sku LIKE 'BAZ%' THEN 'Flash Bundle'
                        WHEN A.product_type = 'Flash'
                             AND B.parent_sku NOT LIKE 'BAZ%' THEN 'Flash'
                        WHEN B.parent_sku LIKE 'BAZ%' THEN 'Bundle'
                        ELSE 'Regular'
                    END AS product_type_class,
                    B.id sales_sub_order_id,
                    A.order_loyalty_discount,
                    B.loyalty_discount,
                    reseller_tier_name,
                    price_type,
                    a.is_campaign,
                    B.cogs_discount_type
FROM public.order_level_data A
LEFT JOIN raena_order_management.sales_sub_order B ON A.external_id = B.order_id and status <> 'Cancelled'
LEFT JOIN public.manual_bundle_sku_data D ON cast(B.parent_sku AS varchar) = cast(D.parent_sku AS varchar)
LEFT JOIN
  (SELECT id ,
          sales_sub_order_id ,
          retail_price,
          Sku ,
          quantity,
          effective_wholesale_price,
          actual_wholesale_price,
          coupon_discount,
          payment_amount,
          product_class,
          loyalty_discount
   FROM raena_order_management.sales_sub_order_parent_child ssopc
   WHERE product_class = 'Bundle'
     AND cast(created_at+interval'7 hours' AS date)>='2023-09-24') F ON B.id = F.sales_sub_order_id
ORDER BY 1,
         2;


DROP TABLE IF EXISTS public.base_netsuite_stage2_v1_base ;


CREATE TABLE public.base_netsuite_stage2_v1_base AS
SELECT transaction_date,
       A.external_id,
       A.sku,
       parent_sku,
       quantity,
       parent_quantity,
       retail_price,
       parent_retail_price,
       effective_wholesale_price,
       parent_wholesale_price,
       actual_wholesale_price,
       discount_price,
       coupon_applied_on,
       discount_amount,
       dynamic_price,
       parent_dynamic_price,
       payment_price,
       payment_amount,
       product_class,
       parent_product_class,
       product_type_class,
       sales_sub_order_id,
       CASE
           WHEN cast(transaction_date AS date)< '2022-02-28'
                AND A.external_id = B.external_id THEN B.final_loyalty_point
           ELSE A.loyalty_discount
       END loyalty_discount,
       price_type,
       is_campaign,
       coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs,
       coalesce(cogs_type,CASE WHEN A.sku = D.sku
                AND A.transaction_date::date BETWEEN D.created_at AND last_date
                AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag,
                cogs_discount_type
FROM public.base_netsuite_stage1_V1 A
LEFT JOIN
  (SELECT external_id ,
          sku ,
          final_loyalty_point
   FROM public.final_loyalty_point) B ON A.external_id = B.external_id
AND A.sku = B.sku
LEFT JOIN
  (SELECT distinct  B.order_id ,
          A.sku ,
          A.applied_cogs ,
          A.cogs_type
   FROM raena_order_management.fulfillment_detail A
   LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.shipment_id = B.id
   WHERE applied_cogs IS NOT NULL) C ON A.sku = C.sku
AND A.external_id = C.order_id
LEFT JOIN
  (SELECT sku,
          cogs,
          promo,
          (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) D ON A.sku = D.sku
AND A.transaction_date::date BETWEEN D.created_at AND last_date;


DROP TABLE IF EXISTS public.base_netsuite_stage2_v1;


CREATE TABLE public.base_netsuite_stage2_v1 AS
SELECT transaction_date,
       A.external_id,
       A.sku,
       parent_sku,
       quantity quantity,
       parent_quantity,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE retail_price
       END retail_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE parent_retail_price
       END parent_retail_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE effective_wholesale_price
       END effective_wholesale_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE parent_wholesale_price
       END parent_wholesale_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE actual_wholesale_price
       END actual_wholesale_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE discount_price
       END discount_price,
       coupon_applied_on,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE discount_amount
       END discount_amount,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE dynamic_price
       END dynamic_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE parent_dynamic_price
       END parent_dynamic_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE payment_price
       END payment_price,
       CASE
           WHEN cogs = 0
                OR cogs IS NULL
                OR A.sku = D.sku THEN 0
           ELSE payment_amount
       END payment_amount,
       product_class,
       parent_product_class,
       product_type_class,
       sales_sub_order_id,
       loyalty_discount,
       price_type,
       is_campaign,
       CASE
           WHEN A.sku = D.sku THEN 0
           ELSE cogs
       END cogs,
       cogs_flag,
       cogs_discount_type
FROM public.base_netsuite_stage2_v1_base A
LEFT JOIN
  (SELECT DISTINCT sku
   FROM raena_catalog_management.product
   WHERE lower(name) LIKE '%not for sale%'
     OR lower(name) LIKE '%free item%'
     OR lower(name) LIKE '%free gift%') D ON A.sku = D.sku ;


DROP TABLE IF EXISTS public.base_netsuite_stage3_v1 ;


CREATE TABLE public.base_netsuite_stage3_v1 AS
SELECT A.* ,
       CASE
           WHEN dynamic_price>0 THEN dynamic_price
           ELSE effective_wholesale_price
       END final_wholesale_price,
       CASE
           WHEN coupon_applied_on <> 'Cart'
                AND discount_price>0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN discount_price>0
                                                                                       AND retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id)
           WHEN coupon_applied_on <> 'Cart'
                AND discount_price=0 THEN 0
           WHEN coupon_applied_on ='Cart'
                AND discount_price>=0 THEN discount_price

           WHEN coupon_applied_on ='Cart'
                AND discount_price=0 THEN retail_price*quantity*(discount_amount) /sum(CASE WHEN retail_price<>0 THEN retail_price*quantity END)over(partition BY A.external_id)
       END AS Final_discount ,
       retail_price*quantity-coalesce(actual_wholesale_price,effective_wholesale_price)*quantity AS seller_margin,
       CASE
           WHEN product_type_class IN ('Flash Bundle',
                                       'Flash') THEN 'Flash'
           WHEN coupon_applied_on ='Cart'
                AND price_type ='NEGOTIATED_PRICE' THEN 'Negotiated'
           WHEN coupon_applied_on = 'Shipping Fee' THEN 'Shipping'
           WHEN coupon_applied_on IS NOT NULL THEN 'Brand'
       END discount_type
FROM public.base_netsuite_stage2_v1 A ;


DROP TABLE IF EXISTS public.base_netsuite_stage4_v1 ;


CREATE TABLE public.base_netsuite_stage4_v1 AS
SELECT A.* ,
       CASE
           WHEN (parent_product_class= 'Bundle'
                 OR product_type_class IN('Bundle',
                                          'Flash Bundle'))
                AND A.external_id = B.external_id
                AND A.parent_sku = B.parent_sku THEN (effective_wholesale_price*quantity)-((cast(final_wholesale_price AS decimal(22,2))*quantity*(parent_wholesale_price*parent_quantity))/ttl_wholesale_price)
       END AS additional_discount,
       CASE
           WHEN final_discount<>0 THEN final_discount-seller_margin
       END AS effective_coupon_discount
FROM public.base_netsuite_stage3_v1 A
LEFT JOIN
  (SELECT external_id ,
          parent_sku ,
          round(sum(retail_price*quantity))ttl_retail_price,
          round(sum(final_wholesale_price*quantity))ttl_wholesale_price
   FROM public.base_netsuite_stage3_v1
   WHERE parent_product_class= 'Bundle'
     OR product_type_class IN('Bundle',
                              'Flash Bundle')
   GROUP BY external_id ,
            parent_sku) B ON A.external_id = B.external_id
AND A.parent_sku = B.parent_sku;

DELETE
FROM public.base_netsuite_final
WHERE external_id IN
    (SELECT DISTINCT external_id
     FROM public.order_level_data);


     INSERT INTO public.base_netsuite_final
SELECT A.external_id,
       A.transaction_date transaction_date ,
       A.discount_amount order_discount_amount,
       A.shipping_cost ,
       coupon_code ,
       A.coupon_applied_on ,
       reseller_tier_name tier,
       B.sku product_sku,
       B.quantity ,
       retail_price,
       B.seller_margin/B.quantity seller_margin,
       ((coalesce(retail_price,0)*quantity) -coalesce(seller_margin,0) -coalesce(effective_coupon_discount,0) -coalesce((CASE WHEN final_discount >0 THEN 0 ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity END),0) -(coalesce(additional_discount,0)) -coalesce(loyalty_discount,0))/quantity discounted_price,
       cast(final_discount AS decimal(22,2))/quantity coupon_discount,
       effective_coupon_discount/quantity effective_coupon_discount ,
       loyalty_discount/quantity loyalty_discount,
       coalesce(additional_discount,0)/quantity additional_discount,
       CASE
           WHEN (parent_product_class = 'Bundle'
                 AND product_type_class = 'Flash')
                OR product_type_class = 'Flash Bundle' THEN 'Flash Bundle'
           WHEN (parent_product_class = 'Flash'
                 OR product_type_class = 'Flash') THEN 'Flash'
           WHEN (parent_product_class = 'Bundle'
                 OR product_type_class = 'Bundle')THEN 'Bundle'
           ELSE 'Regular'
       END item_type,
       0 diff,
         CASE
             WHEN price_type IS NOT NULL THEN price_type
             ELSE discount_type
         END discount_type ,
         CASE
             WHEN final_discount >0 THEN 0
             ELSE actual_wholesale_price*quantity - effective_wholesale_price*quantity
         END promo_amount,
         actual_wholesale_price,
         effective_wholesale_price,
         A.is_campaign,
         '' sales_sub_order_id,
         cogs_discount_type,
	 parent_sku
FROM public.order_level_data A
LEFT  JOIN public.base_netsuite_stage4_v1 B ON A.external_id = B.external_id;


with sales_sub_order_table as(
select order_id , parent_sku ,quantity, payment_amount,payment_amount/quantity payment_amount_quantity   
from raena_order_management.sales_sub_order sso 
where order_id in ('OD1668674111980647REG','OD1669347541350078REG','OD1668588329391772REG')
)
update public.base_netsuite_final  
set discounted_price =payment_amount_quantity
from public.base_netsuite_final  G 
Inner join sales_sub_order_table B on G.external_id = B.order_id
and G.product_sku = B.parent_sku ;


SELECT max(transaction_date) Netsuite_base_date
FROM public.base_netsuite_final;



DROP TABLE IF EXISTS public.gm_dimensions_stage1;

CREATE TABLE public.gm_dimensions_stage1 AS
SELECT distinct  A.id AS external_id ,
       shipping_province,
       reseller_id ,
       json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
       json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
       json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
       discount_type,
       json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
       --C.sku ,
       shipping_to,
       --C.name sku_name,
       --brand_name ,
       --product_type ,
       --category_name,
       case when provider='dsf' then 'DSF'
            when provider='rdash360' then 'RDash'
            when order_placed_by='merchant' then 'Merchant'
            when order_placed_by='admin' then 'Admin Panel'
            when order_placed_by='reseller' then 'App' end order_placed_by,
       case
         when is_campaign='true' then 'Campaign Order'
         when is_consignment='true' then 'Consignment Order'
         when provider='dsf' then 'DSF Order'
         when order_placed_by='merchant' then 'Merchant Order'
         when D.name is not null then 'Channel Order' 
 else 'Reseller Order' end order_type,
       is_campaign
FROM raena_order_management.order A
LEFT JOIN raena_order_management.discount_coupon B ON A.coupon_code = B.coupon_code
LEFT JOIN raena_order_management.order_item C ON A.id = C.order_id
LEFT JOIN raena_order_management.channel   D ON A.channel_id = D.id
WHERE payment_status ='Paid'
  AND cast(A.created_at+interval'7 hours' AS date) >='2023-09-24';

drop table if exists sku_gm_mapping;  

create table sku_gm_mapping
as
select A.sku ,A.name sku_name,B.name brand_name,C.name as category_name,product_type
  from raena_catalog_management.product A
  left join raena_catalog_management.brand b on A.brand_id = B.id 
  left join raena_catalog_management.category c   on A.category_id = C.id ;	


DELETE
FROM public.GM_dashboard
WHERE external_id IN
    (SELECT DISTINCT external_id
     FROM public.gm_dimensions_stage1);

INSERT INTO public.GM_dashboard
SELECT A.external_id ,
       B.transaction_date AS transaction_date,
       B.order_discount_amount ,
       B.shipping_cost ,
       A.shipping_province,
       B.coupon_code ,
       A.discount_type ,
       B.coupon_applied_on ,
       brand_name,
       category_name ,
       product_type ,
       product_sku sku,
       sku_name ,
       A.reseller_name ,
       A.reseller_email ,
       A.reseller_mobile ,
       reseller_tier_name tier_name,
       reseller_id ,
       B.quantity,
       B.retail_price ,
       B.seller_margin ,
       B.discounted_price ,
       B.additional_discount ,
       B.loyalty_discount ,
       B.effective_coupon_discount,
       B.item_type ,
       order_placed_by ,
       coalesce(C.applied_cogs,D.cogs-coalesce(promo,0)) cogs,
       shipping_to order_recipient ,
       CASE
           WHEN brand_name IN ('LUXCRIME',
                               'SKINTIFIC',
                               'TRUEVE',
                               'SANIYE',
                               'BEAUDELAB',
                               'BRASOV',
                               'FACE REPUBLIC',
                               'SKIN1004',
                               'PREMIERE BEAUTE',
                               'ALLURA',
                               'LIPLAPIN',
                               'ROUNDLAB',
                               'FACE FLUX',
                               'DOLLGORAE',
                               'SKINUA',
                               'PUREFORET',
                               'SKINTIFIC',
                               'OHMYSKIN',
                               'FEAT FOR SKIN',
                               'SECONDATE',
                               'KYND',
                               'PURNAMA',
                               'BASE',
                               'LAVIE LASH',
                               'REI SKIN',
                               'USTRAA',
                               'BRUNBRUN PARISGLOWINC',
                               'SOONHAN',
                               'THE YEON',
                               'MIXSOON',
                               'KOSE COSMEPORT') THEN 'High GM'
           WHEN brand_name IN ('W DRESSROOM',
                               'BEAUSTA',
                               'Dewycel',
                               'GLUTANEX',
                               'HISTOIRE NATURELLE',
                               'FORENCOS',
                               'BELLFLOWER',
                               'MAXCLINIC',
			       'INGRID',
                               'ONE THING',
                               'ITFER') THEN 'EL/PL'
       END brand_type ,
       Customer_type,
       BB.gm_target,
       B.discount_type discount_promo_type ,
       promo_amount,
       actual_wholesale_price,
       effective_wholesale_price,
       coalesce(cogs_type,CASE WHEN B.product_sku = D.sku
                AND B.transaction_date::date BETWEEN D.created_at AND last_date
                AND promo IS NOT NULL THEN 'Promo' ELSE 'Non-Promo' END) cogs_flag,
       A.is_campaign,
       CASE
           WHEN A.external_id LIKE '%DSF%' THEN 'Yes'
           ELSE 'No'
       END AS DSF_Flag,
       cogs_discount_type,
       order_type,
       parent_sku
FROM public.gm_dimensions_stage1 A
INNER JOIN public.base_netsuite_final B ON A.external_id =B.external_id
LEFT JOIN
  (SELECT distinct  B.order_id ,
          A.sku ,
          A.applied_cogs ,
          A.cogs_type
   FROM raena_order_management.fulfillment_detail A
   LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.shipment_id = B.id
   WHERE applied_cogs IS NOT NULL) C ON B.product_sku = C.sku
AND A.external_id = C.order_id
LEFT JOIN
  (SELECT sku,
          cogs,
          promo,
	  (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) D ON B.product_sku = D.sku
AND B.transaction_date::date BETWEEN D.created_at AND last_date
LEFT JOIN
  (SELECT DISTINCT id order_id,
                   CASE
                       WHEN customer_id IS NOT NULL
                            AND order_placed_by = 'admin' THEN 'Offline Dropshipper'
                       WHEN customer_id IS NOT NULL
                            AND order_placed_by <> 'admin' THEN 'Online Dropshipper'
                       WHEN customer_id IS NULL
                            AND order_placed_by <> 'admin' THEN 'Online Reseller'
                       WHEN customer_id IS NULL
                            AND order_placed_by = 'admin' THEN 'Offline Reseller'
                   END Customer_type
   FROM raena_order_management.order) AA ON A.external_id = AA.order_id
LEFT JOIN
  (SELECT DISTINCT SKU,
                   gm_target,
                   t.name AS tierName
   FROM raena_gross_margin_management.gross_margin_config AA
   LEFT JOIN raena_user_management.tier t ON cast(AA.gm_target_tier AS TEXT) =cast(t.id AS TEXT)) BB ON B.product_sku = BB.sku
AND A.reseller_tier_name =BB.tierName
left join sku_gm_mapping AAA on B.product_sku = AAA.sku 
WHERE A.external_id NOT IN
    (SELECT DISTINCT id
     FROM raena_order_management.order
     WHERE status IN ('Cancelled')
       AND payment_status= 'Paid') ;

SELECT max(transaction_date) gm_dashboard_date
FROM public.GM_dashboard;

DROP TABLE IF EXISTS public.GM_dashboard_v1;

CREATE TABLE public.GM_dashboard_v1 AS
SELECT *
FROM public.GM_dashboard
WHERE external_id IN
    (SELECT DISTINCT external_id
     FROM public.gm_dimensions_stage1);

DELETE
FROM public.GM_dashboard
WHERE (lower(sku_name) LIKE '%not for sale%'
       OR lower(sku_name) LIKE '%free item%'
       OR lower(sku_name) LIKE '%free gift%'
       OR cogs IS NULL
       OR COGS =0)
  AND external_id IN
    (SELECT DISTINCT external_id
     FROM public.gm_dimensions_stage1);

INSERT INTO public.GM_dashboard
SELECT external_id,
       transaction_date,
       order_discount_amount,
       shipping_cost,
       shipping_province,
       coupon_code,
       discount_type,
       coupon_applied_on,
       brand_name,
       category_name,
       product_type,
       sku,
       sku_name,
       reseller_name,
       reseller_email,
       reseller_mobile,
       tier_name,
       reseller_id,
       quantity,
       0 retail_price,
         0 seller_margin,
           0 ,
           0 ,
           0,
           0,
           item_type,
           order_placed_by,
           0 cogs,
             order_recipient,
             brand_type,
             customer_type,
             gm_target,
             discount_promo_type,
             0 promo_amount,
               0,
               0,
               cogs_flag,
               is_campaign,
               dsf_flag,
               cogs_discount_type,
               order_type,
	       parent_sku
FROM public.GM_dashboard_v1
WHERE (lower(sku_name) LIKE '%not for sale%'
       OR lower(sku_name) LIKE '%free item%'
       OR lower(sku_name) LIKE '%free gift%'
       OR cogs IS NULL
       OR COGS =0)
  AND external_id IN
    (SELECT DISTINCT external_id
     FROM public.gm_dimensions_stage1);



DROP TABLE IF EXISTS public.business_dimensions_stage1;

CREATE TABLE public.business_dimensions_stage1 AS
SELECT A.id AS external_id ,
       reseller_id,
       customer_id,
       cart_id,
       A.flash_sale_id,
       shipping_to,
       payment_status,
       A.status,
       A.stock_type,
       case when provider='dsf' then 'DSF'
            when provider='rdash360' then 'RDash'
            when order_placed_by='merchant' then 'Merchant'
            when order_placed_by='admin' then 'Admin Panel'
            when order_placed_by='reseller' then 'App' end order_placed_by,
       case
         when is_campaign='true' then 'Campaign Order'
         when is_consignment='true' then 'Consignment Order'
         when provider='dsf' then 'DSF Order'
         when order_placed_by='merchant' then 'Merchant Order'
         when E.name is not null then 'Channel Order'
      else 'Reseller Order'  
      end order_type,
       order_warehouse,
       cast(channel_id AS text) channel_id ,
       medium,
       marketplace,
       provider,
       coalesce(cast(B.tier_id AS varchar),cast(C.tier_id AS varchar)) tier_id ,
       json_extract_path_text(A.reseller_info,'city',TRUE) reseller_city,
       json_extract_path_text(A.reseller_info,'province',TRUE) reseller_province,
       json_extract_path_text(A.reseller_info,'name',TRUE) reseller_name,
       json_extract_path_text(A.reseller_info,'email',TRUE) reseller_email,
       json_extract_path_text(A.reseller_info,'mobile',TRUE) reseller_mobile,
       json_extract_path_text(A.reseller_info,'store',TRUE) reseller_store,
       json_extract_path_text(A.reseller_info,'tierName',TRUE) reseller_tier_name,
       shipping_address_line1 ,
       shipping_address_line2 ,
       shipping_pincode ,
       shipping_district,
       shipping_city ,
       shipping_province ,
       is_bank_transfer ,
       is_campaign ,
       discount_type,
       shipping_coupon_discount
FROM raena_order_management.order A
LEFT JOIN public.tier_name B ON A.id = B.id
LEFT JOIN raena_user_management.user C ON cast(A.reseller_id AS varchar) = cast(C.id AS varchar)
LEFT JOIN raena_order_management.discount_coupon D ON A.coupon_code = D.coupon_code
LEFT JOIN raena_order_management.channel E ON A.channel_id = E.id
WHERE cast(A.created_at+interval'7 hours' AS date) >='2023-09-24' ;



DELETE
FROM public.business_report
WHERE external_id IN
    (SELECT DISTINCT external_id
     FROM public.business_dimensions_stage1);

INSERT INTO public.business_report
SELECT DISTINCT A.external_id,
                cast(B.transaction_date AS date) created_date ,
                B.order_discount_amount,
                B.shipping_cost ,
                coupon_code ,
                D.name brand_name,
                C.product_type,
                E.name category_name,
                C.name sku_name,
                B.product_sku sku,
                reseller_id,
                customer_id,
                cart_id,
                A.flash_sale_id,
                shipping_to,
                payment_status,
                status,
                A.stock_type,
                order_placed_by,
                order_warehouse,
                channel_id,
                medium,
                marketplace,
                provider,
                reseller_tier_name tier,
                reseller_city,
                reseller_name,
                reseller_email,
                reseller_mobile,
                reseller_store,
                shipping_address_line1 ,
                shipping_address_line2 ,
                shipping_pincode ,
                shipping_district,
                shipping_city ,
                shipping_province ,
                is_bank_transfer ,
                a.is_campaign ,
                C.Country,
                B.quantity ,
                B.retail_price,
                seller_margin,
                discounted_price,
                loyalty_discount,
                effective_coupon_discount ,
                additional_discount,
                item_type,
                B.discount_type discount_promo_type,
                promo_amount,
                actual_wholesale_price,
                effective_wholesale_price,
                reseller_province,
                CASE
                    WHEN A.external_id LIKE '%DSF%' THEN 'Yes'
                    ELSE 'No'
                END DSF_flag,
                CASE
                    WHEN brand_name IN ('LUXCRIME',
                                        'SKINTIFIC',
                                        'TRUEVE',
                                        'SANIYE',
                                        'BEAUDELAB',
                                        'BRASOV',
                                        'FACE REPUBLIC',
                                        'SKIN1004',
                                        'PREMIERE BEAUTE',
                                        'ALLURA',
                                        'LIPLAPIN',
                                        'ROUNDLAB',
                                        'FACE FLUX',
                                        'DOLLGORAE',
                                        'SKINUA',
                                        'PUREFORET',
                                        'SKINTIFIC',
                                        'OHMYSKIN',
                                        'FEAT FOR SKIN',
                                        'SECONDATE',
                                        'KYND',
                                        'PURNAMA',
                                        'BASE',
                                        'LAVIE LASH',
                                        'REI SKIN',
                                        'USTRAA',
                                        'BRUNBRUN PARISGLOWINC',
                                        'SOONHAN',
                                        'THE YEON',
                                        'MIXSOON',
                                        'KOSE COSMEPORT') THEN 'High GM'
                    WHEN brand_name IN ('W DRESSROOM',
                                        'BEAUSTA',
                                        'Dewycel',
                                        'GLUTANEX',
                                        'HISTOIRE NATURELLE',
                                        'FORENCOS',
                                        'BELLFLOWER',
                                        'MAXCLINIC',
					'INGRID',
                               'ONE THING',
                               'ITFER') THEN 'EL/PL'
                END brand_type,
                A.order_type
FROM public.business_dimensions_stage1 A
LEFT JOIN public.base_netsuite_final B ON A.external_id = B.external_id
LEFT JOIN raena_catalog_management.product C ON B.product_sku = C.sku
LEFT JOIN raena_catalog_management.brand D ON C.brand_id = D.id
LEFT JOIN raena_catalog_management.category E ON C.category_id = E.id
WHERE A.external_id NOT IN
    ( SELECT DISTINCT id
     FROM raena_order_management.order
     WHERE status IN ('Cancelled')
       AND payment_status= 'Paid');

DROP TABLE IF EXISTS public.business_report_v1;


CREATE TABLE public.business_report_v1 AS
SELECT *
FROM public.business_report
WHERE external_id IN
    (SELECT DISTINCT external_id
     FROM public.business_dimensions_stage1);

DELETE
FROM public.business_report USING
  (SELECT DISTINCT sku,
                   sku_name,
                   external_id
   FROM public.gm_dashboard gd
   WHERE lower(sku_name) LIKE '%not for sale%'
     OR lower(sku_name) LIKE '%free item%'
     OR lower(sku_name) LIKE '%free gift%'
     OR cogs IS NULL
     OR COGS =0) B
WHERE public.business_report.external_id = B.external_id
  AND public.business_report.sku=B.sku
  AND public.business_report.external_id IN
    (SELECT DISTINCT external_id
     FROM public.business_dimensions_stage1);

INSERT INTO public.business_report
SELECT external_id,
       created_date,
       order_discount_amount,
       shipping_cost,
       coupon_code,
       brand_name,
       product_type,
       category_name,
       sku_name,
       sku,
       reseller_id,
       customer_id,
       cart_id,
       flash_sale_id,
       shipping_to,
       payment_status,
       status,
       stock_type,
       order_placed_by,
       order_warehouse,
       channel_id,
       medium,
       marketplace,
       provider,
       tier,
       reseller_city,
       reseller_name,
       reseller_email,
       reseller_mobile,
       reseller_store,
       shipping_address_line1,
       shipping_address_line2,
       shipping_pincode,
       shipping_district,
       shipping_city,
       shipping_province,
       is_bank_transfer,
       is_campaign,
       country,
       quantity,
       0 retail_price,
       0 seller_margin,
       0 discount_price,
       0 loyalty_discount,
       0 effective_coupon_discount,
       0 additional_discount,
       item_type,
       discount_promo_type,
       0 promo_amount,
       0 actual_wholesale_price,
       0 effective_wholesale_price,
       reseller_province,
       dsf_flag,
       brand_type,
       order_type
FROM
  (SELECT public.business_report_v1.*
   FROM public.business_report_v1
   INNER JOIN
     (SELECT DISTINCT sku,
                      sku_name,
                      external_id
      FROM public.gm_dashboard gd
      WHERE lower(sku_name) LIKE '%not for sale%'
        OR lower(sku_name) LIKE '%free item%'
        OR lower(sku_name) LIKE '%free gift%'
        OR cogs IS NULL
        OR COGS =0) B ON public.business_report_v1.external_id = B.external_id
   AND public.business_report_v1.sku=B.sku) A
WHERE external_id IN
    (SELECT DISTINCT external_id
     FROM public.business_dimensions_stage1);

SELECT max(created_date) business_date
FROM public.business_report;

DELETE
FROM public.OM_GM_DB_Product_category
WHERE external_Id IN
    (SELECT DISTINCT external_id
     FROM public.gm_dimensions_stage1);


INSERT INTO OM_GM_DB_Product_category
SELECT external_id,
       transaction_date,
       "Time",
       order_discount_amount,
       shipping_cost,
       coupon_code ,
       discount_type ,
       coupon_applied_on ,
       brand_name,
       category_name ,
       product_type ,
       sku,
       sku_name ,
       reseller_name ,
       reseller_email ,
       base.reseller_mobile ,
       tier_name ,
       reseller_id ,
       quantity ,
       retail_price ,
       seller_margin ,
       discounted_price ,
       additional_discount,
       item_type ,
       order_placed_by ,
       cogs,
       shipping_province ,
       Flag ,
       wholesale_price,
       order_recipient,
       brand_type,
       Customer_type,
       gm_target,
       discount_promo_type,
       promo_amount,
       actual_wholesale_price,
       effective_wholesale_price,
       CASE
           WHEN date_trunc('Month',base.transaction_date)::date = date_trunc('Month',first_install_date)::date
                AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
           WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>= 30
                AND date_trunc('Month',base.transaction_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation'
           WHEN date_trunc('Month',base.transaction_date)::date-date_trunc('Month',first_install_date)::date>=30
                AND date_trunc('Month',base.transaction_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing'
           ELSE 'Na'
       END AS New_existing_flag,
       is_campaign,
       dsf_flag,
       cogs_flag,
       cogs_discount_type,
       order_type,
       parent_sku
FROM
  (SELECT A.external_id ,
          cast(transaction_date AS date) transaction_date ,
          concat(left(TO_char(transaction_date,'month'),3),date_part('year',transaction_date)) AS Time,
          order_discount_amount ,
          shipping_cost ,
          coupon_code ,
          discount_type ,
          coupon_applied_on ,
          D.brand_name,
          D.category_name ,
          D.product_type ,
          A.sku,
          D.name sku_name ,
          reseller_name ,
          reseller_email ,
          reseller_mobile ,
          tier_name ,
          A.reseller_id ,
          quantity ,
          retail_price ,
          seller_margin ,
          discounted_price ,
          additional_discount,
          item_type ,
          order_placed_by ,
          A.cogs,
          shipping_province ,
          CASE
              WHEN A.external_id = C.external_id THEN 'Yes'
              ELSE 'No'
          END Flag ,
          CASE
              WHEN tier_name='GOLD' THEN gold_price
              WHEN tier_name ='SILVER' THEN silver_price
              WHEN tier_name ='BRONZE' THEN bronze_price
              ELSE retail_price-seller_margin
          END wholesale_price,
          order_recipient,
          brand_type,
          Customer_type,
          gm_target,
          discount_promo_type,
          promo_amount,
          actual_wholesale_price,
          effective_wholesale_price,
          is_campaign,
          A.dsf_flag,
          A.cogs_flag,
          A.cogs_discount_type,
          A.order_type,
	  parent_sku
   FROM public.GM_dashboard A
   LEFT JOIN
     (SELECT DISTINCT external_id
      FROM
        (SELECT external_id,
                discounted_price ,
                sku ,
                (((retail_price-seller_margin)*quantity) -(cogs*quantity))/(CASE WHEN retail_price<>0 THEN (retail_price-seller_margin)*quantity END) Pre_discount,
                CASE WHEN discounted_price>0 THEN ((discounted_price*quantity) -(cogs*quantity))/(discounted_price*quantity) ELSE 0 END Post_discount
         FROM public.GM_dashboard) A
      WHERE Post_discount>Pre_discount) C ON A.external_id = C.external_id
   LEFT JOIN
     (SELECT DISTINCT Sku ,
                      A.name,
                      A.product_type,
                      C.name category_name ,
                      B.name brand_name
      FROM raena_catalog_management.product A
      LEFT JOIN raena_catalog_management.brand B ON A.brand_id = B.id
      LEFT JOIN raena_catalog_management.category c ON A.category_id=C.id) D ON A.sku = D.sku
   LEFT JOIN pricing_sheet P ON A.sku=p.skucode
   WHERE cast(transaction_date AS date) >='2023-09-24'
     AND A.external_id NOT IN
       (SELECT DISTINCT id
        FROM raena_order_management.order
        WHERE status IN ('Cancelled')
          AND payment_status= 'Paid')) base
LEFT JOIN user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile;

SELECT max(transaction_date) om_gm_db_product_date
FROM public.OM_GM_DB_Product_category;

update public.gm_dashboard 
 set cogs_discount_type='BUY_N_GET_X'
 where external_id in (select distinct external_id from public.gm_dashboard where sku like '______X');
 
 update public.om_gm_db_product_category  
 set cogs_discount_type='BUY_N_GET_X'
 where external_id in (select distinct external_id from public.om_gm_db_product_category where sku like '______X');

DELETE
FROM public.GM_GROWTH_TAB1
WHERE created_date >='2023-09-24';

INSERT INTO public.GM_GROWTH_TAB1
SELECT tier,
       created_date,
       brand_name,
       product_type,
       category_name,
       sku,
       sku_name,
       order_placed_by,
       shipping_province,
       shipping_to,
       status,
       channel_name,
       reseller_name,
       reseller_email,
       base.reseller_mobile,
       Number_of_Orders ,
       RSP ,
       wholesale_price ,
       Payment_Price ,
       Effective_Coupon_Discount,
       Total_Loyalty_point,
       additional_discount,
       seller_margin,
       AOV,
       quantity,
       discount_promo_type,
       promo_amount,
       actual_wholesale_price,
       effective_wholesale_price,
       reseller_province,
       is_campaign,
       dsf_flag,
       brand_type,
       CASE
           WHEN date_trunc('Month',base.created_date)::date = date_trunc('Month',first_install_date)::date
                AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'New'
           WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>= 30
                AND date_trunc('Month',base.created_date)::date = date_trunc('Month',First_transaction_date)::date THEN 'Activation'
           WHEN date_trunc('Month',base.created_date)::date-date_trunc('Month',first_install_date)::date>=30
                AND date_trunc('Month',base.created_date)::date - date_trunc('Month',First_transaction_date)::date>=30 THEN 'Existing'
           ELSE 'Na'
       END AS New_existing_flag,
       external_id,
       order_type
FROM
  (SELECT tier,
          created_date,
          brand_name,
          product_type,
          category_name,
          sku,
          sku_name,
          order_placed_by,
          shipping_province,
          shipping_to,
          status,
          channel_name,
          reseller_name,
          reseller_email,
          reseller_mobile,
          count(DISTINCT external_id) AS Number_of_Orders ,
          sum(retail_price*quantity) RSP ,
          sum((retail_price*quantity)-(seller_margin*quantity))wholesale_price ,
          sum(discounted_price*quantity) AS Payment_Price ,
          sum(effective_coupon_discount*quantity) AS Effective_Coupon_Discount,
          sum(loyalty_discount*quantity)Total_Loyalty_point,
          sum(additional_discount*quantity)additional_discount,
          sum(seller_margin*quantity)seller_margin,
          sum(discounted_price*quantity)/count(DISTINCT external_id) AS AOV,
          sum(quantity) AS quantity,
          discount_promo_type,
          sum(promo_amount) promo_amount,
          sum(actual_wholesale_price* quantity) actual_wholesale_price,
          sum(effective_wholesale_price*quantity) effective_wholesale_price,
          reseller_province,
          is_campaign,
          dsf_flag,
          brand_type,
          external_id,
          order_type
   FROM
     (SELECT DISTINCT coalesce(a.tier,'NA') AS tier,
                      a.created_date,
                      brand_name,
                      product_type,
                      category_name,
                      sku,
                      sku_name,
                      order_placed_by,
                      shipping_province,
                      shipping_to,
                      status,
                      b.channel_name,
                      reseller_name,
                      reseller_email,
                      reseller_mobile,
                      external_id,
                      retail_price,
                      quantity,
                      seller_margin,
                      discounted_price,
                      effective_coupon_discount,
                      loyalty_discount,
                      additional_discount,
                      discount_promo_type,
                      promo_amount,
                      actual_wholesale_price,
                      effective_wholesale_price,
                      reseller_province,
                      is_campaign,
                      dsf_flag,
                      brand_type,
                      order_type,
                      row_number() over (partition BY external_id
                                         ORDER BY external_id) AS R
      FROM public.business_report a
      LEFT JOIN
        (SELECT DISTINCT A.id ,
                         channel_id ,
                         D.name channel_name
         FROM raena_order_management.order A
         LEFT JOIN
           (SELECT id,
                   name
            FROM raena_order_management.channel) D ON A.channel_id = D.id
         WHERE cast(created_at AS date) >= '2023-09-24'
           AND channel_id IS NOT NULL) b ON a.external_id=b.id
      WHERE payment_status='Paid'
        AND created_date >= '2023-09-24')
   GROUP BY tier,
            created_date,
            brand_name,
            product_type,
            category_name,
            sku,
            sku_name,
            order_placed_by,
            shipping_province,
            shipping_to,
            status,
            channel_name,
            reseller_name,
            reseller_email,
            reseller_mobile,
            external_id,
            discount_promo_type,
            reseller_province,
            is_campaign,
            dsf_flag,
            brand_type,
            order_type) base
LEFT JOIN user_type_table flag ON replace(base.reseller_mobile,'+','')=flag.reseller_mobile;

SELECT max(created_date) gm_growth_date
FROM public.GM_GROWTH_TAB1;


DROP TABLE IF EXISTS public.GM_GROWTH_TAB2;


CREATE TABLE public.GM_GROWTH_TAB2 AS
SELECT 'year' AS frequency,
       cast(date_part('year',created_date) AS varchar) AS time,
       cast(date_part('year',created_date) AS int) AS sort ,
       *
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'quarter' AS frequency,
       To_char(created_date,'quarter') AS month_name,
       date_part('quarter',created_date) AS sort ,
       *
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'month' AS frequency,
       To_char(created_date,'month') AS month_name,
       date_part('month',created_date) AS sort ,
       *
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'week' AS frequency,
       To_char(created_date,'week') AS month_name,
       cast(left(To_char(created_date,'week'),1) AS int) AS sort ,
       *
FROM public.GM_GROWTH_TAB1
UNION
SELECT 'day' AS frequency,
       cast(date_part('day',created_date) AS varchar) AS month_name,
       cast(date_part('day',created_date) AS int) AS sort ,
       *
FROM public.GM_GROWTH_TAB1
ORDER BY 3;


DROP TABLE IF EXISTS public.GM_GROWTH_TAB3;
CREATE TABLE public.GM_GROWTH_TAB3 AS
(
SELECT 'year' AS frequency,
           'All' AS brand_name,
       'All' AS product_type,
       'All' AS category_name,
       'All' AS sku,
       'All' AS sku_name,tier_name,new_existing_flag,
       cast(date_part('year',transaction_date) AS varchar) AS time,
       cast(date_part('year',transaction_date) AS int) AS sort,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by 7,8,9,10,11,12,13,14,15,16,17
       union
SELECT 'year' AS frequency,
           brand_name,
       product_type,
       category_name,
       sku,
       sku_name,tier_name,new_existing_flag,
       cast(date_part('year',transaction_date) AS varchar) AS time,
       cast(date_part('year',transaction_date) AS int) AS sort,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
)
union
(
SELECT 'quarter' AS frequency,
           'All' AS brand_name,
       'All' AS product_type,
       'All' AS category_name,
       'All' AS sku,
       'All' AS sku_name,tier_name,new_existing_flag,
       To_char(transaction_date,'quarter') AS month_name,
       date_part('quarter',transaction_date) as sort ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by 7,8,9,10,11,12,13,14,15,16,17
 union
SELECT 'quarter' AS frequency,
           brand_name,
       product_type,
       category_name,
       sku,
       sku_name,tier_name,new_existing_flag,
       To_char(transaction_date,'quarter') AS month_name,
       date_part('quarter',transaction_date) as sort ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by  2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
)
union
(
SELECT 'month' AS frequency,
           'All' AS brand_name,
       'All' AS product_type,
       'All' AS category_name,
       'All' AS sku,
       'All' AS sku_name,tier_name,new_existing_flag,
       To_char(transaction_date,'month') AS month_name,
       date_part('month',transaction_date) as sort  ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by 7,8,9,10,11,12,13,14,15,16,17
       union
SELECT 'month' AS frequency,
           brand_name,
       product_type,
       category_name,
       sku,
       sku_name,tier_name,new_existing_flag,
       To_char(transaction_date,'month') AS month_name,
       date_part('month',transaction_date) as sort  ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by  2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17)
union
(
SELECT 'week' AS frequency,
           'All' AS brand_name,
       'All' AS product_type,
       'All' AS category_name,
       'All' AS sku,
       'All' AS sku_name,tier_name,new_existing_flag,
       To_char(transaction_date,'week') AS month_name,
       cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by 7,8,9,10,11,12,13,14,15,16,17
       union
SELECT 'week' AS frequency,
           brand_name,
       product_type,
       category_name,
       sku,
       sku_name,tier_name,new_existing_flag,
       To_char(transaction_date,'week') AS month_name,
       cast(left(To_char(transaction_date,'week'),1) as int) AS sort ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by  2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
)
union
(
SELECT 'day' AS frequency,
           'All' AS brand_name,
       'All' AS product_type,
       'All' AS category_name,
       'All' AS sku,
       'All' AS sku_name,tier_name,new_existing_flag,
       cast(date_part('day',transaction_date) as varchar) AS month_name,
       cast(date_part('day',transaction_date) AS int) AS sort ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by 7,8,9,10,11,12,13,14,15,16,17
       union
SELECT 'day' AS frequency,
           brand_name,
       product_type,
       category_name,
       sku,
       sku_name,tier_name,new_existing_flag,
       cast(date_part('day',transaction_date) as varchar) AS month_name,
       cast(date_part('day',transaction_date) AS int) AS sort ,o.status,
       order_placed_by,reseller_email,shipping_province,order_recipient,transaction_date,
       order_type,
       sum((quantity*discounted_price)-(cogs*quantity)) as Numerator,sum(quantity*discounted_price) as Denominator
from public.OM_GM_DB_Product_category a
inner join (select distinct id,status from raena_order_management.order) o on a.external_id=o.id
group by  2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
)
ORDER BY 3;

DROP TABLE shipping_fee_base_table;

CREATE TABLE shipping_fee_base_table AS
SELECT A.order_id ,
       sum(CASE WHEN A.sales_sub_order_shipment_id = C.reference_id THEN ((C.shipping_amount)/(C.order_weight))*(C.final_weight) ELSE B.shipping_amount END) shipping_amount
FROM
  (SELECT DISTINCT order_id ,
                   sales_sub_order_shipment_id
   FROM raena_order_management.sales_sub_order) A
LEFT JOIN raena_order_management.sales_sub_order_shipment B ON A.sales_sub_order_shipment_id = B.id
LEFT JOIN public.OM_Logistic_final_shipping C ON A.sales_sub_order_shipment_id = C.reference_id
GROUP BY A.order_id;

DROP TABLE public.shipping_fee_sku_base_stage1 ;

CREATE TABLE public.shipping_fee_sku_base_stage1 AS
SELECT A.order_id ,
       A.id ,
       coalesce(B.sku,A.parent_sku) Sku ,
       A.sales_sub_order_shipment_id ,
       coalesce((B.quantity*A.quantity),A.quantity) Quantity,
       CASE
           WHEN A.sales_sub_order_shipment_id=D.reference_id THEN ((D.shipping_amount)/(D.order_weight))*(D.final_weight)
           ELSE C.shipping_amount
       END shipping_amount ,
       A.product_class
FROM raena_order_management.sales_sub_order A
LEFT JOIN raena_order_management.sales_sub_order_parent_child B ON A.id = B.sales_sub_order_id
LEFT JOIN raena_order_management.sales_sub_order_shipment C ON A.sales_sub_order_shipment_id = C.id
LEFT JOIN OM_Logistic_final_shipping D ON A.sales_sub_order_shipment_id=D.reference_id ;

DROP TABLE public.weight_sku_table;

CREATE TABLE public.weight_sku_table AS
SELECT sku,
       CASE
           WHEN weight>=volume_weight THEN weight
           ELSE volume_weight
       END weight
FROM
  (SELECT a.sku,
          weight,
          (height*width*LENGTH)/6000 AS volume_weight
   FROM raena_catalog_management.product a
   WHERE CLASS<>'Bundle') A;

DROP TABLE public.shipping_fee_sku_base_stage2_v1;

CREATE TABLE public.shipping_fee_sku_base_stage2_v1 AS
SELECT order_id , sales_sub_order_shipment_id,
       CASE
           WHEN (sum(B.weight*Quantity))<1.3 THEN 1
           WHEN (sum(B.weight*Quantity))>=1.3
                AND (ABS(sum(B.weight*Quantity)) - FLOOR(ABS(sum(B.weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR(sum(B.weight*Quantity))+1
           ELSE FLOOR(sum(B.weight*Quantity))
       END AS sku_weight
FROM public.shipping_fee_sku_base_stage1 A
LEFT JOIN public.weight_sku_table B ON A.sku = B.sku 
group by 1,2;

DROP TABLE public.shipping_fee_sku_base_stage2;

CREATE TABLE public.shipping_fee_sku_base_stage2 AS
SELECT A.*,
       B.weight*sku_weight/sum(B.weight) over(partition by A.order_id , A.sales_sub_order_shipment_id ) Final_weight
FROM public.shipping_fee_sku_base_stage1 A
left join public.shipping_fee_sku_base_stage2_v1 C on A.order_id = C.order_id and A.sales_sub_order_shipment_id = C.sales_sub_order_shipment_id
LEFT join public.weight_sku_table B ON A.sku = B.sku ;



DROP TABLE public.shipping_fee_sku_base_stage3;

CREATE TABLE public.shipping_fee_sku_base_stage3 AS
SELECT A.*,
       (Final_weight*coalesce(Calculated_shipping_fee,shipping_amount))/sum(Final_weight) over(partition BY A.sales_sub_order_shipment_id) new_shipment_amount
FROM public.shipping_fee_sku_base_stage2 A
left join public.calculated_ninja_jne_oct_shipping_fee B on A.order_id = B.order_id and A.sales_sub_order_shipment_id = B.reference_id ;


DROP TABLE sku_level_shipping_fee_final;

CREATE TABLE public.sku_level_shipping_fee_final AS
SELECT order_id ,
       sku,
       product_class ,
       sum(new_shipment_amount) new_shipment_amount
FROM public.shipping_fee_sku_base_stage3
GROUP BY 1,
         2,
         3;

DROP TABLE public.shipping_fee_sku_base_old_stage1;

CREATE TABLE public.shipping_fee_sku_base_old_stage1 AS
SELECT external_id ,
       A.sku ,
       quantity,
       shipping_cost ,
       CASE
           WHEN item_type LIKE '%Bundle%' THEN 'Bundle'
           ELSE 'Product'
       END product_class,
       weight*Quantity sku_weight,
       CASE
           WHEN (weight*Quantity)<1.3 THEN 1
           WHEN (weight*Quantity)>=1.3
                AND (ABS((weight*Quantity)) - FLOOR(ABS((weight*Quantity)))) BETWEEN 0.3 AND 0.999999 THEN FLOOR((weight*Quantity))+1
           ELSE FLOOR((weight*Quantity))
       END AS final_weight
FROM gm_dashboard A
LEFT JOIN public.weight_sku_table B ON A.sku = B.sku;

DROP TABLE public.shipping_fee_sku_base_old_stage2;

CREATE TABLE public.shipping_fee_sku_base_old_stage2 AS
SELECT *,
       (sku_weight*shipping_cost)/sum(sku_weight) over(partition BY external_id) new_shipment_amount
FROM public.shipping_fee_sku_base_old_stage1 ;

DROP TABLE public.sku_level_shipping_fee_old_final;

CREATE TABLE public.sku_level_shipping_fee_old_final AS
SELECT external_id ,
       sku,
       product_class ,
       sum(new_shipment_amount) new_shipment_amount
FROM public.shipping_fee_sku_base_old_stage2
GROUP BY 1,
         2,
         3;

DROP TABLE IF EXISTS public.payment_order_stage;

CREATE TABLE public.payment_order_stage AS WITH NS AS
  (SELECT 1 AS n
   UNION ALL SELECT 2
   UNION ALL SELECT 3
   UNION ALL SELECT 4
   UNION ALL SELECT 5
   UNION ALL SELECT 6
   UNION ALL SELECT 7
   UNION ALL SELECT 8
   UNION ALL SELECT 9
   UNION ALL SELECT 10
   UNION ALL SELECT 11
   UNION ALL SELECT 12
   UNION ALL SELECT 13
   UNION ALL SELECT 14
   UNION ALL SELECT 15
   UNION ALL SELECT 16
   UNION ALL SELECT 17
   UNION ALL SELECT 18
   UNION ALL SELECT 19
   UNION ALL SELECT 20
   UNION ALL SELECT 21
   UNION ALL SELECT 22
   UNION ALL SELECT 23
   UNION ALL SELECT 24
   UNION ALL SELECT 25)
SELECT id,
       max(created_at) created_at,
       replace(cast(substring(cast(TRIM(SPLIT_PART(B.order_ids , ',', NS.n))AS varchar),2,length(cast(TRIM(SPLIT_PART(B.order_ids , ',', NS.n))AS varchar))-2) AS varchar),'"','') AS order_id
FROM NS
INNER JOIN raena_order_management.payment B ON NS.n <= REGEXP_COUNT(B.order_ids , ',') + 1
GROUP BY 1,
         3;

DROP TABLE IF EXISTS public.payment_order;

CREATE TABLE public.payment_order AS
SELECT A.id ,
       A.created_at,
       A.order_id
FROM public.payment_order_stage A
INNER JOIN
  (SELECT order_id ,
          max(created_at) max_created_at
   FROM public.payment_order_stage
   GROUP BY order_id) B ON A.created_at= B.max_created_at
AND A.order_id = B.order_id;

select count(1) from public.payment_order;

drop table if exists  om_app_admin_dump_table;

CREATE TABLE om_app_admin_dump_table AS
SELECT raena_order_management.order.id AS order_id,
       dateadd(hour,7,raena_order_management.order.created_at) AS order_date,
       dateadd(hour,7,raena_order_management.order.created_at) AS id_order_date,
       status AS order_status,
       Payment_status,
       D.name AS channel_name,
       cast(is_bank_transfer AS varchar) AS bank_transfer,
       cast(is_campaign AS varchar) AS campaign,
       dynamic_price,
       total_dynamic_and_tier_price AS total_tier_price,
       total_retail_price AS total_MRP,
       tier_discount AS total_tier_discount,
       discount_amount AS coupon_discount,
       payment_amount,
       shipping_amount AS Actual_shipping_amount,
       applied_shipping_amount,
       shipping_coupon_discount,
       Earnings,
       Coupon_code,
       json_extract_path_text(reseller_info, 'name') AS Reseller_name,
       json_extract_path_text(reseller_info, 'email') AS Reseller_email,
       json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile,
       concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address,
       json_extract_path_text(reseller_info, 'store') AS store_name,
       json_extract_path_text(reseller_info,'tierName',TRUE) AS tier,
       json_extract_path_text(customer_info, 'name') AS customer_name,
       json_extract_path_text(customer_info, 'email') AS customer_email,
       concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address,
       Shipping_to AS Order_recipient,
       shipping_address_line1 ,
       shipping_address_line2 ,
       shipping_city ,
       shipping_district ,
       shipping_sub_district ,
       shipping_province ,
       shipping_country_code ,
       shipping_pincode ,
       tracking_url ,
       CASE
           WHEN order_placed_by ='reseller' THEN 'App Order'
           ELSE 'Admin'
       END AS order_placed_by,
       raena_order_management.order.stock_type AS product_stock_type,
       sku AS product_sku,
       oi.name AS product_name,
       product_code,
       date_key,
       product_type,
       batch,
        country,
       quantity,
       wholesale_price ,
       retail_price,
       total_price,
       Total_earnings,
       raena_order_management.order.is_slash_price_enabled ,
       Brand_name,
       category_name,
       product_class,
       source_order_id ,
       marketplace_provider_order_id AS provider_order_id,
       medium,
       marketplace,
       provider,
       C.id as payment_id,
       0 total_cogs,
       'NA' bundle_sku,
       0 sku_level_shipping_fee
FROM raena_order_management.order
INNER JOIN raena_order_management.order_item oi ON raena_order_management.order .id=oi.order_id
left join  public.payment_order C on raena_order_management.order.id=C.order_id
left join raena_order_management.channel D on raena_order_management.order.channel_id = D.id
WHERE raena_order_management.order.created_at <'2021-10-28'
UNION all
SELECT A.id AS order_id,
       (A.created_at+interval '7 hours') AS order_date,
       (A.created_at+interval '7 hours') AS id_order_date,
       A.status AS order_status,
       A.Payment_status,
       D.name AS channel_name,
       cast(A.is_bank_transfer AS varchar) AS bank_transfer,
       cast(A.is_campaign AS varchar) AS campaign,
       dynamic_price,
       total_dynamic_and_tier_price AS total_tier_price,
       total_retail_price AS total_MRP,
       tier_discount AS total_tier_discount,
       discount_amount AS coupon_discount,
       GB.payment_amount,
       coalesce(GB.new_shipping_amount_final,shipping_amount) AS Actual_shipping_amount,
       applied_shipping_amount,
       shipping_coupon_discount,
       Earnings,
       A.Coupon_code,
       json_extract_path_text(reseller_info, 'name') AS Reseller_name,
       json_extract_path_text(reseller_info, 'email') AS Reseller_email,
       json_extract_path_text(reseller_info, 'mobile') AS Reseller_mobile,
         concat(concat(concat(json_extract_path_text(reseller_info, 'addressLine1') || ' ,',json_extract_path_text(reseller_info, 'addressLine2') || ', '), concat(json_extract_path_text(reseller_info, 'city') || ' ,',json_extract_path_text(reseller_info, 'pincode') || ' ,')), json_extract_path_text(reseller_info, 'province')) AS Reseller_address,
       json_extract_path_text(reseller_info, 'store') AS store_name,
       json_extract_path_text(reseller_info,'tierName',TRUE) AS tier,
       json_extract_path_text(customer_info, 'name') AS customer_name,
       json_extract_path_text(customer_info, 'email') AS customer_email,
       concat(concat(concat(json_extract_path_text(customer_info, 'addressLine1') || ' ,',json_extract_path_text(customer_info, 'addressLine2') || ', '), concat(json_extract_path_text(customer_info, 'city') || ' ,',json_extract_path_text(customer_info, 'pincode') || ' ,')), json_extract_path_text(customer_info, 'province')) AS customer_address,
       Shipping_to AS Order_recipient,
       shipping_address_line1 ,
       shipping_address_line2 ,
       shipping_city ,
       shipping_district ,
       shipping_sub_district ,
       A.shipping_province ,
       shipping_country_code ,
       shipping_pincode ,
       tracking_url ,
       CASE
           WHEN A.order_placed_by ='reseller' THEN 'App Order'
           ELSE 'Admin'
       END AS order_placed_by,
       A.stock_type AS product_stock_type,
       coalesce(GB.sku,oi.sku) AS product_sku,
       coalesce(gb.sku_name,oi.name) AS product_name,
       product_code,
       date_key,
       coalesce(GB.product_type,oi.product_type),
       batch,
       country,
       coalesce(GB.quantity,oi.quantity),
       coalesce(GB.actual_wholesale_price,oi.wholesale_price) ,
       coalesce(GB.retail_price,oi.retail_price),
       ((GB.retail_price*GB.quantity)-(GB.seller_margin*GB.quantity)),
       Total_earnings,
       A.is_slash_price_enabled ,
       coalesce(gb.brand_name,oi.Brand_name),
       coalesce(gb.category_name,oi.category_name),
       oi.product_class,
       source_order_id ,
       marketplace_provider_order_id AS provider_order_id,
       medium,
       marketplace,
       provider,
       C.id as payment_id,
       (GB.cogs) as total_cogs,
       case when oi.parent_sku is null then 'NA' else oi.parent_sku end bundle_sku,
	       new_shipping_amount sku_level_shipping_amount
FROM raena_order_management.order A
inner join (select  AD.* ,
                 sum(new_shipping_amount) over(partition by AD.external_id)  new_shipping_amount_final ,
                 sum(discounted_price*quantity) over(partition by AD.external_id) payment_amount
            from  (SELECT  A.*,
                          discounted_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_discounted_price,
                          actual_wholesale_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_actual_wholesale_price ,
                          retail_price/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_retail_price,
                          seller_margin/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_seller_margin ,
                          cogs/count(1) over(partition BY A.external_id,A.sku,A.item_type,A.quantity) new_cogs,
                          (case when (case when item_type like '%Bundle%' then 'Bundle' else 'Product' end )=B.product_class
                           then B.new_shipment_amount else C.new_shipment_amount end)/count(1) over(partition BY A.external_id,A.sku,A.item_type) new_shipping_amount
                   FROM   public.gm_dashboard A
                   left join public.sku_level_shipping_fee_final B
                           on a.external_id = B.order_id
                           and a.sku = B.sku
                           and (case when item_type like '%Bundle%' then 'Bundle' else 'Product' end )=B.product_class
                   left join public.sku_level_shipping_fee_old_final C
                           on a.external_id = C.external_id
                            and a.sku = C.sku
                            and (case when item_type like '%Bundle%' then 'Bundle' else 'Product' end )=C.product_class) AD
              )GB
              ON A.id = GB.external_id AND GB.sku not like 'BAZ%'
	      left JOIN (select A.*,B.sku parent_sku  
	                 from  raena_order_management.order_item A
                         left join raena_order_management.order_item B on A.parent_item_id = B.id ) oi 
			 ON GB.external_id=oi.order_id 
			 AND OI.SKU not like 'BAZ%' 
			 AND  GB.sku=oi.sku 
			 and GB.parent_sku= oi.parent_sku		 
left join  public.payment_order C on A.id=C.order_id
left join raena_order_management.channel D on A.channel_id = D.id 
WHERE cast(A.created_at+interval '7 hours' as date)  >='2021-10-28'  ;



DROP TABLE IF EXISTS public.remove_duplicate_from_app_admin;


CREATE TABLE public.remove_duplicate_from_app_admin AS
SELECT A.external_id,
       wholesale_price_gm,
       wholesale_price_app_admin,
       wholesale_price_gm-wholesale_price_app_admin diff
FROM
  (SELECT external_id ,
          sum(discounted_price*quantity) P1,
          sum((retail_price*quantity)-(seller_margin*quantity)) wholesale_price_gm
   FROM public.gm_dashboard gd
   GROUP BY external_id) A
FULL OUTER JOIN
  (SELECT order_id ,
          payment_amount p2,
          sum(total_price) wholesale_price_app_admin
   FROM public.om_app_admin_dump_table oaadt
   GROUP BY order_id,
            payment_amount) B ON A.external_id= B.order_id
WHERE wholesale_price_gm-wholesale_price_app_admin<>0;
 

DROP TABLE IF EXISTS public.dubplicate_record_app_admin;


CREATE TABLE public.dubplicate_record_app_admin AS
SELECT DISTINCT *
FROM public.om_app_admin_dump_table oaadt
WHERE order_id IN
    (SELECT DISTINCT external_id
     FROM public.remove_duplicate_from_app_admin);


DELETE
FROM public.om_app_admin_dump_table
WHERE order_id IN
    (SELECT DISTINCT external_id
     FROM public.remove_duplicate_from_app_admin);

INSERT INTO public.om_app_admin_dump_table
SELECT *
FROM public.dubplicate_record_app_admin;




