tercnem

in_cancel_closing_costing_raw_materials

Sep 24th, 2020
1,012
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION in_cancel_closing_costing_raw_materials(bigint, character varying, character varying)
  2.   RETURNS void AS
  3. $BODY$
  4. DECLARE
  5.  
  6.     pTenantId           ALIAS FOR $1;
  7.     pSessionId          ALIAS FOR $2;
  8.     pProcessNo          ALIAS FOR $3;
  9.  
  10.     vProcessId              bigint;
  11.     vOuId                   bigint;
  12.     vUserId                 bigint;
  13.     vDatetime               character varying(14);
  14.     vYearMonth              character varying(6);
  15.     vNextYearMonth          character varying(6);
  16.     vGroupProductRM         character varying(5);
  17.     vAdminProcessLedgerId   bigint;
  18.     vLedgerCodeCostRM       character varying(10);
  19.     vDocTypeAwal            bigint;
  20.     vEmptyId                bigint;
  21.     vStatusDraft            character varying(1);
  22.    
  23. BEGIN
  24.    
  25.     vLedgerCodeCostRM := 'COST.RM';
  26.     vDocTypeAwal := -99;
  27.     vGroupProductRM := 'RM';
  28.     vEmptyId := -99;
  29.     vStatusDraft := 'D';
  30.    
  31.     SELECT A.process_message_id INTO vProcessId
  32.     FROM t_process_message A
  33.     WHERE A.tenant_id = pTenantId AND
  34.         A.process_name = 'in_cancel_closing_costing_raw_materials' AND
  35.         A.process_no = pProcessNo;
  36.        
  37.     SELECT CAST(A.process_parameter_value AS bigint) INTO vOuId
  38.     FROM t_process_parameter A
  39.     WHERE A.process_message_id = vProcessId AND
  40.         A.process_parameter_key = 'ouId';
  41.    
  42.     SELECT CAST(A.process_parameter_value AS bigint) INTO vUserId
  43.     FROM t_process_parameter A
  44.     WHERE A.process_message_id = vProcessId AND
  45.         A.process_parameter_key = 'userId';
  46.  
  47.     SELECT CAST(A.process_parameter_value AS character varying(14)) INTO vDatetime
  48.     FROM t_process_parameter A
  49.     WHERE A.process_message_id = vProcessId AND
  50.         A.process_parameter_key = 'datetime';
  51.        
  52.     SELECT CAST(A.process_parameter_value AS character varying(6)) INTO vYearMonth
  53.     FROM t_process_parameter A
  54.     WHERE A.process_message_id = vProcessId AND
  55.         A.process_parameter_key = 'yearmonth';
  56.  
  57.     SELECT CAST(A.process_parameter_value AS bigint) INTO vAdminProcessLedgerId
  58.     FROM t_process_parameter A
  59.     WHERE A.process_message_id = vProcessId AND
  60.         A.process_parameter_key = 'adminProcessLedgerId';
  61.        
  62.     SELECT TO_CHAR(TO_DATE(vYearMonth,'YYYYMM') + interval '1 Month','YYYYMM') INTO vNextYearMonth;
  63.    
  64.     /*
  65.      * Hapus harga dasar pokok di bulan ybs
  66.      */
  67.     DELETE FROM in_summary_monthly_cogs A
  68.     WHERE A.date_year_month = vYearMonth AND
  69.         A.ou_id = vOuId AND
  70.         A.remark = vGroupProductRM;
  71.        
  72.     /*
  73.      * Hapus data saldo akhir tahun bulan ybs, kecuali saldo awal
  74.      */
  75.     DELETE FROM in_summary_monthly_amount A
  76.     WHERE A.date_year_month = vYearMonth AND
  77.         A.ou_bu_id = vOuId AND
  78.         A.doc_type_id <> vDocTypeAwal AND
  79.         A.remark = vGroupProductRM;
  80.        
  81.     /*
  82.      * delete data in_summary_monthly_zero_qty_amount
  83.      */
  84.     DELETE FROM in_summary_monthly_zero_qty_amount A
  85.     WHERE A.date_year_month = vYearMonth AND
  86.         A.ou_bu_id = vOuId AND
  87.         A.doc_type_id <> vDocTypeAwal AND
  88.         A.remark = vGroupProductRM;
  89.        
  90.     /*
  91.      * Hapus data saldo awal tahun bulan berikutnya
  92.      */
  93.     DELETE FROM in_summary_monthly_amount A
  94.     WHERE A.date_year_month = vNextYearMonth AND
  95.         A.ou_bu_id = vOuId AND
  96.         A.remark = vGroupProductRM;
  97.        
  98.     --delete data anomaly
  99.     DELETE FROM in_anomaly_gl_amount_from_process_costing A
  100.     WHERE A.date_year_month = vYearMonth AND
  101.         A.ou_bu_id = vOuId AND
  102.         A.doc_type_id <> vDocTypeAwal AND
  103.         A.remark = vGroupProductRM;
  104.        
  105.     --delete data anomaly tahun bulan berikutnya
  106.     DELETE FROM in_anomaly_gl_amount_from_process_costing A
  107.     WHERE A.date_year_month = vNextYearMonth AND
  108.         A.ou_bu_id = vOuId AND
  109.         A.remark = vGroupProductRM;
  110.  
  111.     UPDATE m_admin_process_ledger SET status_ledger = 0, flag_process = 'N',
  112.         process_no = pProcessNo, process_datetime = vDatetime, process_user_id = vUserId
  113.     WHERE tenant_id = pTenantId AND
  114.         ou_id = vOuId AND
  115.         date_year_month = vYearMonth AND
  116.         ledger_code = vLedgerCodeCostRM;
  117.    
  118.     /* Adrian, Feb 27, 2017
  119.      * update status journal menjadi D
  120.      */
  121.     DELETE FROM tt_gl_journal_costing WHERE session_id = pSessionId;
  122.        
  123.     INSERT INTO tt_gl_journal_costing
  124.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  125.     journal_trx_id, doc_type_id, journal_trx_item_id, sign_journal, product_id,
  126.     qty, uom_id, amount, total_amount_product, total_qty_product)
  127.     SELECT pSessionId, A.tenant_id, A.ou_bu_id,
  128.         CASE WHEN B.ou_branch_id = vEmptyId AND B.ou_sub_bu_id = vEmptyId THEN A.ou_branch_id ELSE B.ou_branch_id END,
  129.         CASE WHEN B.ou_branch_id = vEmptyId AND B.ou_sub_bu_id = vEmptyId THEN A.ou_sub_bu_id ELSE B.ou_sub_bu_id END,
  130.         A.journal_trx_id, A.doc_type_id, B.journal_trx_item_id, B.sign_journal, B.product_id,
  131.         B.qty, B.uom_id, 0, 0, 0
  132.     FROM gl_journal_trx A, gl_journal_trx_item B, dt_date C, vw_raw_materials_product D
  133.     WHERE A.tenant_id = pTenantId AND
  134.         A.ou_bu_id = vOuId AND
  135.         A.doc_type_id IN (502, 521, 413, 511, 311, 154, 354, 431, 526, 537) AND
  136.         A.doc_date = C.string_date AND
  137.         C.year_month_date = vYearMonth AND
  138.         A.journal_trx_id = B.journal_trx_id AND
  139.         B.product_id <> vEmptyId AND
  140.         B.product_id = D.product_id;
  141.        
  142.     UPDATE gl_journal_trx SET status_doc = vStatusDraft, version = version + 1, update_datetime = vDatetime, update_user_id = vUserId
  143.     FROM (SELECT journal_trx_id
  144.             FROM tt_gl_journal_costing A
  145.             WHERE A.session_id = pSessionId
  146.             GROUP BY journal_trx_id) A
  147.     WHERE gl_journal_trx.journal_trx_id = A.journal_trx_id;    
  148.    
  149.     -- update status document khusus untuk document cost allocation to product (528), dan adjustment stok amount (522)
  150.     UPDATE gl_journal_trx SET status_doc = vStatusDraft, version = version + 1, update_datetime = vDatetime, update_user_id = vUserId
  151.     WHERE doc_type_id IN (528,522)
  152.     AND SUBSTRING(doc_date, 1, 6) = vYearMonth;
  153.    
  154.     PERFORM in_delete_generate_adj_stock_amount_doc_for_process_costing(pTenantId, pSessionId, vYearMonth, vOuId, vDatetime, vUserId, vGroupProductRM);
  155.    
  156.     DELETE FROM tt_gl_journal_costing WHERE session_id = pSessionId;
  157.        
  158.  END;
  159. $BODY$
  160.   LANGUAGE plpgsql VOLATILE
  161.   COST 100;
  162.   /
RAW Paste Data