Advertisement
widana

with ou id

Dec 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DROP VIEW IF EXISTS vw_outstanding_history_achievement_per_salesman;
  2. CREATE OR REPLACE VIEW vw_outstanding_history_achievement_per_salesman AS
  3. SELECT tenant_id, salesman_id, year_month,
  4.     total_item_amount+total_warranty_item AS sales_amount,
  5.     (total_item_amount+total_warranty_item+tax_item_amount+tax_warranty_item-total_purch_amount) AS gross_profit_amount
  6. FROM ( SELECT COALESCE(SUM(f_get_amount_before_tax(B.qty_dlv_so * (D.gross_sell_price - D.discount_amount), D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS total_item_amount,
  7.         COALESCE(SUM(f_tax_rounding(A.tenant_id, f_get_amount_before_tax(B.qty_dlv_so * (D.gross_sell_price - D.discount_amount), D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, C.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax')), D.tax_percentage)), 0) tax_item_amount,
  8.         COALESCE(SUM(f_get_amount_before_tax(B.qty_dlv_so * H.warranty_sell_price, D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS total_warranty_item,
  9.         COALESCE(SUM(f_tax_rounding(A.tenant_id, f_get_amount_before_tax(B.qty_dlv_so * H.warranty_sell_price, D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, C.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax')), D.tax_percentage)), 0) AS tax_warranty_item,
  10.         COALESCE(SUM(F.price * B.qty_dlv_so), 0) AS total_purch_amount, C.salesman_id, SUBSTRING(A.doc_date, 0 , 7) AS year_month, A.tenant_id, A.ou_id
  11.     FROM sl_do A
  12.     INNER JOIN sl_do_item B ON A.do_id = B.do_id
  13.     INNER JOIN sl_so C ON A.ref_id = C.so_id AND A.ref_doc_type_id = C.doc_type_id
  14.     INNER JOIN sl_so_item D ON C.so_id = D.so_id AND B.ref_id = D.so_item_id
  15.     LEFT OUTER JOIN sl_so_item_purchasing F ON F.so_item_id = D.so_item_id
  16.     LEFT OUTER JOIN sl_so_warranty_item H ON H.so_item_id = D.so_item_id
  17.     WHERE A.status_doc NOT IN ('R')
  18.     AND NOT EXISTS (SELECT 1 FROM fi_po_internal_so_igs_tagging Z WHERE Z.so_id = C.so_id)
  19.     GROUP BY C.salesman_id, A.doc_date, A.tenant_id, A.ou_id
  20. ) tt_in_do
  21. UNION
  22. SELECT tenant_id, salesman_id, year_month,
  23.     total_item_amount+total_warranty_item AS sales_amount,
  24.     (total_item_amount+total_warranty_item+tax_item_amount+tax_warranty_item-total_purch_amount) AS gross_profit_amount
  25. FROM ( SELECT  COALESCE(SUM(-1 * f_get_amount_before_tax(COALESCE(B.qty_return * D.qty_so / NULLIF(D.qty_int, 0), 0) * (D.gross_sell_price - D.discount_amount), D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS total_item_amount,
  26.         COALESCE(SUM(-1 * f_tax_rounding(A.tenant_id, f_get_amount_before_tax(COALESCE(B.qty_return * D.qty_so / NULLIF(D.qty_int, 0), 0) * (D.gross_sell_price - D.discount_amount), D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax')), D.tax_percentage)), 0) AS tax_item_amount,
  27.         COALESCE(SUM(-1 * f_get_amount_before_tax(COALESCE(B.qty_return * D.qty_so / NULLIF(D.qty_int, 0), 0) * H.warranty_sell_price, D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS total_warranty_item,
  28.         COALESCE(SUM(-1 * f_tax_rounding(E.tenant_id, f_get_amount_before_tax(COALESCE(B.qty_return * D.qty_so / NULLIF(D.qty_int, 0), 0) * H.warranty_sell_price, D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax')), D.tax_percentage)), 0) AS tax_warranty_item,
  29.         COALESCE(SUM(G.price * D.qty_int), 0) AS total_purch_amount, E.salesman_id, SUBSTRING(A.doc_date, 0 , 7) AS year_month, A.tenant_id, A.ou_id       
  30.     FROM in_do_receipt A
  31.     INNER JOIN in_do_receipt_item B ON A.do_receipt_id = B.do_receipt_id
  32.     INNER JOIN in_balance_do_item C ON B.ref_id = C.do_item_id
  33.     INNER JOIN sl_so_item D ON C.so_item_id = D.so_item_id
  34.     INNER JOIN sl_so E ON D.so_id = E.so_id
  35.     INNER JOIN sl_do F ON F.do_id = A.ref_id AND A.ref_doc_type_id = F.doc_type_id
  36.     LEFT OUTER JOIN sl_so_item_purchasing G ON G.so_item_id = D.so_item_id
  37.     LEFT OUTER JOIN sl_so_warranty_item H ON H.so_item_id = D.so_item_id
  38.     WHERE A.status_doc NOT IN ('R')
  39.         AND NOT EXISTS (SELECT 1 FROM fi_po_internal_so_igs_tagging Z WHERE Z.so_id = E.so_id)
  40.     GROUP BY E.salesman_id, A.doc_date, A.tenant_id, A.ou_id
  41. ) tt_temp_do_receipt
  42. UNION
  43. SELECT tenant_id, salesman_id, year_month,
  44.     total_item_amount+total_warranty_item AS sales_amount,
  45.     (total_item_amount+total_warranty_item+tax_item_amount+tax_warranty_item-total_purch_amount) AS gross_profit_amount
  46. FROM ( SELECT COALESCE(SUM(f_get_amount_before_tax_and_disc((B.qty_realization * D.qty_so * D.gross_sell_price) / D.qty_int, (H.regular_disc_amount + H.promo_disc_amount) * (B.qty_realization * D.qty_so) / (D.qty_int / H.qty_dlv_so), D.flg_tax_amount,
  47.                 D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS total_item_amount,
  48.         COALESCE(SUM(f_get_amount_before_tax_and_disc((B.qty_realization * D.qty_so * D.gross_sell_price) / D.qty_int, (B.qty_realization * D.qty_so * D.discount_amount) / D.qty_int, D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS tax_item_amount,
  49.         COALESCE(SUM(((B.qty_realization * D.qty_so) / D.qty_int) * I.warranty_sell_price) ,0) AS total_warranty_item,
  50.         COALESCE(SUM(f_tax_rounding(A.tenant_id, f_get_amount_before_tax((B.qty_realization * D.qty_so * I.warranty_sell_price) / D.qty_int, D.flg_tax_amount, D.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, D.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax')), D.tax_percentage)), 0) AS tax_warranty_item,
  51.         COALESCE(SUM(J.price * D.qty_int), 0) AS total_purch_amount, E.salesman_id, SUBSTRING(A.doc_date, 0 , 7) AS year_month, A.tenant_id, A.ou_to_id AS ou_id
  52.     FROM in_inventory A
  53.     INNER JOIN in_inventory_item B ON A.inventory_id = B.inventory_id
  54.     INNER JOIN in_balance_do_item C ON B.ref_item_id = C.do_item_id
  55.     INNER JOIN sl_so_item D ON C.so_item_id = D.so_item_id
  56.     INNER JOIN sl_so E ON D.so_id = E.so_id
  57.     INNER JOIN sl_do F ON E.so_id = F.ref_id
  58.         AND E.doc_type_id = F.ref_doc_type_id
  59.     INNER JOIN sl_do_item G ON F.do_id = G.do_id
  60.     INNER JOIN sl_so_balance_invoice H ON H.ref_id = F.do_id
  61.         AND H.ref_item_id = G.do_item_id
  62.         AND H.do_receipt_item_id = -99
  63.     LEFT OUTER JOIN sl_so_warranty_item I ON G.ref_id = I.so_item_id
  64.     LEFT OUTER JOIN sl_so_item_purchasing J ON J.so_item_id = D.so_item_id
  65.     WHERE A.status_doc NOT IN ('R')
  66.         AND A.doc_type_id IN (502)
  67.         AND NOT EXISTS (SELECT 1 FROM fi_po_internal_so_igs_tagging Z WHERE Z.so_id = E.so_id)
  68.     GROUP BY E.salesman_id, A.doc_date, A.tenant_id, A.ou_to_id
  69. ) tt_temp_rn
  70. UNION
  71. SELECT tenant_id, salesman_id, year_month,
  72.     total_item_amount+total_warranty_item AS sales_amount,
  73.     (total_item_amount+total_warranty_item+tax_item_amount+tax_warranty_item-total_purch_amount) AS gross_profit_amount
  74. FROM (SELECT COALESCE(SUM(f_get_amount_before_tax(C.qty_so * (B.gross_sell_price - B.discount_amount), B.flg_tax_amount, B.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, B.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS total_item_amount,
  75.         COALESCE(SUM(f_tax_rounding(A.tenant_id, f_get_amount_before_tax(C.qty_so * (B.gross_sell_price - B.discount_amount), B.flg_tax_amount, B.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, B.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax')), B.tax_percentage)), 0) AS tax_item_amount,
  76.         COALESCE(SUM(f_get_amount_before_tax(E.warranty_sell_qty * E.warranty_sell_price, B.flg_tax_amount, B.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, B.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax'))), 0) AS total_warranty_item,
  77.         COALESCE(SUM(f_tax_rounding(A.tenant_id, f_get_amount_before_tax(E.warranty_sell_qty * E.warranty_sell_price, B.flg_tax_amount, B.tax_percentage, f_get_digit_decimal_doc_curr(A.doc_type_id, B.curr_code), f_get_value_system_config_by_param_code(A.tenant_id, 'rounding.mode.non.tax')), B.tax_percentage)), 0) AS tax_warranty_item,
  78.         COALESCE(SUM(F.price * C.qty_so), 0) AS total_purch_amount, A.salesman_id, SUBSTRING(A.doc_date, 0 , 7) AS year_month, A.tenant_id, A.ou_id
  79.     FROM sl_so A
  80.     INNER JOIN sl_so_item B ON A.so_id = B.so_id
  81.     INNER JOIN sl_so_balance_item C ON B.so_item_id = C.so_item_id
  82.     LEFT OUTER JOIN sl_so_warranty_item D ON D.so_item_id = C.so_item_id
  83.     LEFT OUTER JOIN sl_so_balance_warranty_item E ON D.so_warranty_item_id = E.so_warranty_item_id
  84.         AND D.so_item_id = E.so_item_id
  85.         AND E.status_item IN ('R', 'I')
  86.     LEFT OUTER JOIN sl_so_item_purchasing F ON F.so_item_id = D.so_item_id
  87.     WHERE A.status_doc IN ('R')
  88.         AND C.status_item NOT IN ('V', 'C', 'F')
  89.         AND C.qty_so - C.qty_cancel + C.qty_add + C.qty_return - C.qty_dlv > 0
  90.         AND NOT EXISTS (SELECT 1 FROM fi_po_internal_so_igs_tagging Z WHERE Z.so_id = A.so_id)
  91.     GROUP BY A.salesman_id, A.doc_date, A.tenant_id, A.ou_id
  92. ) AS tt_temp_so;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement