#!/bin/bash echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n" backDay=$1 echo $backDay echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n" reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d") date echo 'reportDate'=$reportDate echo " 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; 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, 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 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 AND applied_cogs <>0 GROUP BY 1, 2, 3, 4, 5, 7; DROP TABLE IF EXISTS raena_analytics.cogs_base_data; CREATE TABLE raena_analytics.cogs_base_data AS 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; DROP TABLE raena_analytics.consignment_base_data ; CREATE TABLE raena_analytics.consignment_base_data AS SELECT transaction_date, paid_status, Brand_name , raena_code, B.Quantity consignment_quantity, B.pkp_cogs, batch_no, C.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.payment_amount 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 (SELECT A.order_id , date_trunc('Month',A.created_at)::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, coalesce(B.payment_amount,A.payment_amount) payment_amount 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_analytics.cogs_base_data C ON B.sku = C.sku AND B.product_class = 'Bundle' AND B.created_at::date BETWEEN C.created_at AND C.last_date WHERE A.created_at::date >='2023-01-01' ORDER BY 1) 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 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' ) 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 ; " > /home/ec2-user/cronjob/redshift/sql_code/consignment_sales_report.sql psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/consignment_sales_report.sql