Advertisement
tercnem

vw_trx_sales_indocom_with_gp

Jun 15th, 2020
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DROP VIEW IF EXISTS vw_trx_sales_indocom_with_gp;
  2.  
  3. CREATE OR REPLACE VIEW vw_trx_sales_indocom_with_gp
  4. AS
  5. WITH data_penjualan AS (
  6.     SELECT E.brand_code, E.brand_name, F.ctgr_product_code, F.ctgr_product_name, G.partner_code as salesman_code, G.partner_name as salesman_name,
  7.         H.line_of_business, SUBSTRING(A.ref_doc_date, 1, 6) AS periode,
  8.         CASE WHEN A.ref_doc_type_id = 502 OR A.do_receipt_item_id <> -99 THEN 'Y' ELSE 'N' END AS is_return,
  9.         (A.price_so * ABS(A.qty_dlv_so)) *  f_commercial_rate(A.tenant_id, A.ref_doc_date, A.curr_code, 'IDR') AS sell_amount,
  10.         COALESCE(J.price * ABS(A.qty_dlv_so), 0) AS purch_amount
  11.     FROM sl_so_balance_invoice A
  12.     INNER JOIN sl_so B ON A.so_id = B.so_id
  13.     INNER JOIN sl_do_item C ON A.ref_item_id = C.do_item_id
  14.     INNER JOIN m_product D ON D.product_id = C.product_id
  15.     INNER JOIN m_brand E ON D.brand_id = E.brand_id
  16.     INNER JOIN m_ctgr_product F ON D.ctgr_product_id = F.ctgr_product_id
  17.     INNER JOIN m_partner G ON B.salesman_id = G.partner_id
  18.     INNER JOIN m_partner H ON A.partner_id = H.partner_id
  19.     INNER JOIN m_ou_structure I ON A.ou_id = I.ou_id
  20.     LEFT OUTER JOIN sl_so_item_purchasing J ON J.so_item_id = C.ref_id
  21.     --LEFT JOIN in_summary_monthly_amount J ON J.doc_type_id = -99
  22.         --AND J.date_year_month = TO_CHAR(TO_DATE(A.ref_doc_date,'YYYYMMDD') + INTERVAL '1 Month','YYYYMM')
  23.         --AND J.ou_bu_id = I.ou_bu_id AND J.product_id = D.product_id AND D.base_uom_id = J.base_uom_id
  24. )
  25. SELECT brand_code, brand_name, ctgr_product_code, ctgr_product_name, salesman_code, salesman_name, line_of_business, periode,
  26.     SUM(CASE WHEN is_return = 'Y' THEN -1 * (sell_amount - purch_amount) ELSE sell_amount - purch_amount END) AS profit_amount,
  27.     CASE WHEN SUM(CASE WHEN is_return = 'Y' THEN -1 * sell_amount ELSE sell_amount END) = 0 THEN 0 ELSE
  28.         SUM(CASE WHEN is_return = 'Y' THEN -1 * (sell_amount - purch_amount) ELSE sell_amount - purch_amount END) /
  29.         SUM(CASE WHEN is_return = 'Y' THEN -1 * sell_amount ELSE sell_amount END) * 100
  30.     END AS profit_percentage
  31. FROM data_penjualan
  32. GROUP BY brand_code, brand_name, ctgr_product_code, ctgr_product_name, salesman_code, salesman_name, line_of_business, periode
  33. ORDER BY periode, brand_code, brand_name, ctgr_product_code, ctgr_product_name, salesman_code, salesman_name;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement