Advertisement
aadddrr

in_process_costing_finish_goods

Feb 7th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- WTC, 160212, menambahkan costing untuk tipe dokumen DO MLM, return/buyback, dan exchange
  2. -- WTC, 161113, saat summary untuk membuat saldo awal bulan berikutnya, ubah filter qty menjadi qty <> 0 (sblmnya qty > 0)
  3. -- WTC, 170418, nilai claim note diperhitungkan sebagai pengurang perhitungan COGS
  4. -- WTC, 170502, khusus untuk product tidak ada COGS, maka ambil nilainya dari master harga standar
  5. CREATE OR REPLACE FUNCTION in_process_costing_finish_goods(bigint, character varying, character varying, bigint, character varying, bigint)
  6.   RETURNS void AS
  7. $BODY$
  8. DECLARE
  9.  
  10.     pTenantId           ALIAS FOR $1;
  11.     pSessionId          ALIAS FOR $2;
  12.     pYearMonth          ALIAS FOR $3;
  13.     pOuId               ALIAS FOR $4;
  14.     pDatetime           ALIAS FOR $5;
  15.     pUserId             ALIAS FOR $6;
  16.  
  17.     vNextYearMonth          character varying(6);
  18.     vCurrGL                 character varying(5);
  19.     vFlagNo                 character varying(1);
  20.     vFlagYes                character varying(1);
  21.     vEmptyValue             character varying(1);
  22.     vEmptyId                bigint;
  23.     vDocTypeAwal            bigint;
  24.     vGroupProductFG         character varying(5);
  25.  
  26.     vRoundingDigit          integer;
  27.     vTypeRate               character varying(5);
  28.     vStatusRelease          character varying(1);
  29. BEGIN
  30.    
  31.     vFlagNo := 'N';
  32.     vFlagYes := 'Y';
  33.     vDocTypeAwal := -99;
  34.     vEmptyId := -99;
  35.     vEmptyValue := ' ';
  36.     vGroupProductFG := 'FG';
  37.  
  38.     vTypeRate := 'COM';
  39.     vStatusRelease := 'R';
  40.    
  41.     SELECT TO_CHAR(TO_DATE(MAX(pYearMonth),'YYYYMM') + interval '1 Month','YYYYMM') INTO vNextYearMonth;
  42.    
  43.     vCurrGL := f_get_value_system_config_by_param_code(pTenantId,'ValutaBuku');
  44.     vRoundingDigit := CAST(f_get_value_system_config_by_param_code(pTenantId,'rounding.gl.amount') AS integer);
  45.  
  46.     DELETE FROM tt_in_doc_product_price WHERE session_id = pSessionId;
  47.    
  48.     DELETE FROM tt_in_add_product_unamount WHERE session_id = pSessionId;
  49.    
  50.     DELETE FROM tt_in_product_base_price WHERE session_id = pSessionId;
  51.    
  52.     DELETE FROM tt_in_summary_monthly_product WHERE session_id = pSessionId;
  53.    
  54.     DELETE FROM tt_in_summary_monthly_cogs WHERE session_id = pSessionId;
  55.    
  56.     DELETE FROM tt_in_summary_monthly_amount_remain WHERE session_id = pSessionId;
  57.    
  58.     /*
  59.      * costing product hanya berdasarkan ou business unit, jadi untuk sementara sub_ou_id diisi dengan -99
  60.      */
  61.    
  62.     /*
  63.      * membuat summary nilai pembelian berdasarkan hasil jurnal
  64.      * untuk transaksi receive goods
  65.      * NK, 16 Feb 2015 : Pembelian Internal menghasilkan dokumen semua sesuai doc type Receive Goods
  66.      * Receive Goods : 111
  67.      *
  68.      * Ali, 22 Nov
  69.      * Penambahan untuk exclude RG yang nilai Qty-nya minus
  70.      */
  71.     INSERT INTO in_summary_monthly_amount
  72.     (date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  73.     doc_type_id, product_id, base_uom_id, qty,
  74.     gl_curr_code, gl_amount, flg_amount,
  75.     "version", create_datetime, create_user_id, update_datetime, update_user_id, remark)
  76.     SELECT pYearMonth, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  77.         A.doc_type_id, C.product_id, C.uom_id, SUM(C.qty),
  78.         C.gl_curr_code, SUM(CASE WHEN C.sign_journal = 'D' THEN C.gl_amount ELSE -1 * C.gl_amount END), 'PURCH_PRICE',
  79.         0, pDatetime, pUserId, pDatetime, pUserId, vGroupProductFG
  80.     FROM gl_journal_trx A, dt_date B, gl_journal_trx_item C, m_document_journal D, vw_finish_goods_product E
  81.     WHERE A.tenant_id = pTenantId AND
  82.         A.ou_bu_id = pOuId AND
  83.         A.status_doc = vStatusRelease AND
  84.         A.doc_date = B.string_date AND
  85.         B.year_month_date = pYearMonth AND
  86.         A.journal_trx_id = C.journal_trx_id AND
  87.         A.doc_type_id = D.doc_type_id AND
  88.         D.ledger_code IN ('PURCH') AND
  89.         C.journal_desc = 'PRODUCT_STOCK' AND
  90.         A.doc_type_id IN (111) AND
  91.         C.product_id = E.product_id AND
  92.         C.qty > 0
  93.     GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.doc_type_id, C.product_id, C.uom_id, C.gl_curr_code;
  94.  
  95.     /*
  96.      * ambil data receive goods dari in_summary_monthly_amount yang product Finish Goods
  97.      * NK, 16 Feb 2015 : receive goods internal menggunakan doc type receive goods
  98.      */        
  99.     INSERT INTO tt_in_doc_product_price
  100.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id, product_id,
  101.      doc_type_id, ref_id, doc_no, doc_date,
  102.      curr_code, amount, qty, uom_id,
  103.      numerator_rate, denominator_rate,
  104.      gl_curr_code, gl_amount)
  105.     SELECT pSessionId, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.product_id,
  106.         A.doc_type_id, vEmptyId, vEmptyValue, vEmptyValue,
  107.         A.gl_curr_code, A.gl_amount, A.qty, A.base_uom_id,
  108.         1, 1,
  109.         A.gl_curr_code, A.gl_amount
  110.     FROM in_summary_monthly_amount A, vw_finish_goods_product E
  111.     WHERE A.date_year_month = pYearMonth AND
  112.         A.doc_type_id IN (111) AND
  113.         A.tenant_id = pTenantId AND
  114.         A.ou_bu_id = pOuId AND
  115.         A.product_id = E.product_id;   
  116.        
  117.     /*
  118.      * ambil data adjusment stok amount
  119.      * jika curr <> curr G/L, maka lakukan hitung rate untuk ke nilai sesuai valuta G/L
  120.      * 522 : adj stock amount ( mempengaruhi COGS )
  121.      * 523 : adj stock amount balance ( mempengaruhi saldo akhir )
  122.      * 528 : costing allocation to product ( mempengaruhi COGS )
  123.      * 511 : claim note (mempengaruhi COGS), added by WTC 170418
  124.      */
  125.     INSERT INTO tt_in_doc_product_price
  126.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id, product_id,
  127.      doc_type_id, ref_id, doc_no, doc_date,
  128.      curr_code, amount, qty, uom_id,
  129.      numerator_rate, denominator_rate,
  130.      gl_curr_code, gl_amount)
  131.     SELECT pSessionId, A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id, A.product_id,
  132.         A.doc_type_id, A.ref_id, A.doc_no, A.doc_date,
  133.         A.curr_code, A.amount, A.qty, A.uom_id,
  134.         1, 1, vCurrGL, ROUND( A.amount, vRoundingDigit)
  135.     FROM in_product_price_balance A, m_ou_structure B, dt_date C, vw_finish_goods_product E
  136.     WHERE A.doc_date = C.string_date AND
  137.         C.year_month_date = pYearMonth AND
  138.         A.tenant_id = pTenantId AND
  139.         A.ou_id = B.ou_id AND
  140.         B.ou_bu_id = pOuId AND
  141.         A.doc_type_id IN (522, 523, 528) AND
  142.         A.product_id = E.product_id;   
  143.    
  144.     -- hitung nilai pembukuan untuk transaksi adjusment stok amount
  145.     UPDATE tt_in_doc_product_price SET numerator_rate = B.amount_to, denominator_rate = B.amount_from
  146.     FROM m_exchange_rate B
  147.     WHERE tt_in_doc_product_price.session_id = pSessionId AND
  148.         tt_in_doc_product_price.tenant_id = B.tenant_id AND
  149.         B.type_exchange_rate = vTypeRate AND
  150.         tt_in_doc_product_price.doc_date = B.date_from AND
  151.         tt_in_doc_product_price.curr_code = B.curr_code_from AND
  152.         tt_in_doc_product_price.gl_curr_code = B.curr_code_to AND
  153.         tt_in_doc_product_price.doc_type_id IN (522, 523, 528) AND
  154.         tt_in_doc_product_price.curr_code <> vCurrGL;
  155.  
  156.     UPDATE tt_in_doc_product_price SET gl_amount = ROUND(amount * numerator_rate / denominator_rate, vRoundingDigit)
  157.     WHERE tt_in_doc_product_price.session_id = pSessionId AND
  158.           tt_in_doc_product_price.doc_type_id IN (522, 523, 528) AND
  159.           tt_in_doc_product_price.curr_code <> vCurrGL;
  160.    
  161.     /*
  162.      * Moved by WTC, 170418, sblmnya tidak diperhitungkan sbg pengurang COGS.
  163.      * membuat summary nilai claim note berdasarkan hasil jurnal
  164.      * Claim Note : 511
  165.      */
  166.     INSERT INTO tt_in_doc_product_price
  167.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id, product_id,
  168.      doc_type_id, ref_id, doc_no, doc_date,
  169.      curr_code, amount, qty, uom_id,
  170.      numerator_rate, denominator_rate,
  171.      gl_curr_code, gl_amount)
  172.     SELECT pSessionId, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, C.product_id,
  173.         A.doc_type_id, vEmptyId, vEmptyValue, vEmptyValue,
  174.         C.gl_curr_code, -1 * C.gl_amount, -1 * C.qty, C.uom_id,
  175.         1, 1,
  176.         C.gl_curr_code, -1 * C.gl_amount
  177.     FROM gl_journal_trx A, dt_date B, gl_journal_trx_item C, m_document_journal D, vw_finish_goods_product E
  178.     WHERE A.tenant_id = pTenantId AND
  179.         A.ou_bu_id = pOuId AND
  180.         A.status_doc = vStatusRelease AND
  181.         A.doc_date = B.string_date AND
  182.         B.year_month_date = pYearMonth AND
  183.         A.journal_trx_id = C.journal_trx_id AND
  184.         A.doc_type_id = D.doc_type_id AND
  185.         D.ledger_code IN ('INV') AND
  186.         C.journal_desc = 'PRODUCT_STOCK' AND
  187.         A.doc_type_id IN (511) AND
  188.         C.product_id = E.product_id;
  189.  
  190.     /**
  191.      * Ali, 22 Nov, dikarenakan RG Qty Minus diexclude perlu dilakukan penambahan data.
  192.      *
  193.      */
  194.     INSERT INTO tt_in_doc_product_price
  195.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id, product_id,
  196.      doc_type_id, ref_id, doc_no, doc_date,
  197.      curr_code, amount, qty, uom_id,
  198.      numerator_rate, denominator_rate,
  199.      gl_curr_code, gl_amount)
  200.     SELECT pSessionId, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, C.product_id,
  201.         A.doc_type_id, vEmptyId, vEmptyValue, vEmptyValue,
  202.         C.gl_curr_code, SUM(CASE WHEN C.sign_journal = 'D' THEN C.gl_amount ELSE -1 * C.gl_amount END), SUM(C.qty), C.uom_id,
  203.         1, 1,
  204.         C.gl_curr_code, SUM(CASE WHEN C.sign_journal = 'D' THEN C.gl_amount ELSE -1 * C.gl_amount END)
  205.     FROM gl_journal_trx A, dt_date B, gl_journal_trx_item C, m_document_journal D, vw_finish_goods_product E
  206.     WHERE A.tenant_id = pTenantId AND
  207.         A.ou_bu_id = pOuId AND
  208.         A.status_doc = vStatusRelease AND
  209.         A.doc_date = B.string_date AND
  210.         B.year_month_date = pYearMonth AND
  211.         A.journal_trx_id = C.journal_trx_id AND
  212.         A.doc_type_id = D.doc_type_id AND
  213.         D.ledger_code IN ('PURCH') AND
  214.         C.journal_desc = 'PRODUCT_STOCK' AND
  215.         A.doc_type_id IN (111) AND
  216.         C.product_id = E.product_id AND
  217.         C.qty < 0
  218.     GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.doc_type_id, C.product_id, C.uom_id, C.gl_curr_code;
  219.          
  220.     /*
  221.      * WTC, 160211, tambahkan dokumen return/buyback
  222.      * product non assembly
  223.      * ambil data transaksi yg tambah stok, tp belum memiliki nilai
  224.      * 1.return note : 502
  225.      * 2.adj stok qty plus (non konsinyasi) : 521
  226.      * 3.adj stok outlet qty plus (non konsinyasi) : 413
  227.      * 4.Return MLM : 562
  228.      * 5.Buyback MLM : 563
  229.      * 6.Exchange MLM in : 560
  230.      */
  231.     INSERT INTO tt_in_add_product_unamount
  232.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id, product_id,
  233.      doc_type_id, qty, uom_id, gl_curr_code, gl_amount, flg_amount)
  234.     SELECT pSessionId, A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id, A.product_id,
  235.         A.doc_type_id, A.qty, A.base_uom_id, vCurrGL, 0, vEmptyValue
  236.     FROM in_combine_log_product_balance_stock A, m_ou_structure B, vw_finish_goods_product E
  237.     WHERE A.ou_id = B.ou_id AND
  238.         B.ou_bu_id = pOuId AND
  239.         A.date_year_month = pYearMonth AND
  240.         A.doc_type_id IN ( 502, 562, 563 ) AND
  241.         A.product_id = E.product_id;   
  242.  
  243.     INSERT INTO tt_in_add_product_unamount
  244.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id, product_id,
  245.      doc_type_id, qty, uom_id, gl_curr_code, gl_amount, flg_amount)
  246.     SELECT pSessionId, A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id, A.product_id,
  247.         A.doc_type_id, A.qty, A.base_uom_id, vCurrGL, 0, vEmptyValue
  248.     FROM in_combine_log_product_balance_stock A, m_ou_structure B, vw_finish_goods_product E
  249.     WHERE A.ou_id = B.ou_id AND
  250.         B.ou_bu_id = pOuId AND
  251.         A.date_year_month = pYearMonth AND
  252.         A.doc_type_id IN ( 521, 413 ) AND
  253.         A.product_id = E.product_id AND
  254.         A.qty > 0 AND
  255.         A.flg_buy_konsinyasi = vFlagNo;
  256.  
  257.     -- WTC, 160621, ambil data exchange MLM in, baik yg konsinyasi maupun non konsinyasi
  258.     INSERT INTO tt_in_add_product_unamount
  259.     (session_id, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id, product_id,
  260.      doc_type_id, qty, uom_id, gl_curr_code, gl_amount, flg_amount)
  261.     SELECT pSessionId, A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id, A.product_id,
  262.         A.doc_type_id, A.qty, A.base_uom_id, vCurrGL, 0, vEmptyValue
  263.     FROM in_combine_log_product_balance_stock A, m_ou_structure B, vw_finish_goods_product E
  264.     WHERE A.ou_id = B.ou_id AND
  265.         B.ou_bu_id = pOuId AND
  266.         A.date_year_month = pYearMonth AND
  267.         A.doc_type_id = 560 AND
  268.         A.product_id = E.product_id AND
  269.         A.qty > 0
  270.        
  271. /* NK, 25 Jan 2014, di submit adj stok qty diubah sehingga log product balance stock akan ada 2 kelompok,
  272.  * kelompok yang qty > 0 dan qty < 0
  273.                    
  274.     INSERT INTO tt_in_add_product_unamount
  275.     (session_id, tenant_id, ou_id, sub_ou_id, product_id,
  276.      doc_type_id, qty, uom_id, gl_curr_code, gl_amount, flg_amount)
  277.     SELECT pSessionId, A.tenant_id, D.ou_bu_id, D.ou_id, B.product_id,
  278.         A.doc_type_id, B.qty_realization, B.base_uom_id, vCurrGL, 0, vEmptyValue
  279.     FROM in_inventory A, in_inventory_item B, dt_date C, m_ou_structure D, vw_finish_goods_product E
  280.     WHERE A.tenant_id = pTenantId AND
  281.         A.doc_date = C.string_date AND
  282.         C.year_month_date = pYearMonth AND
  283.         A.inventory_id = B.inventory_id AND
  284.         A.ou_from_id = D.ou_id AND
  285.         D.ou_bu_id = pOuId AND
  286.         A.doc_type_id = 521 AND
  287.         B.qty_realization > 0 AND
  288.         B.product_id = E.product_id;   
  289.                    
  290.     INSERT INTO tt_in_add_product_unamount
  291.     (session_id, tenant_id, ou_id, sub_ou_id, product_id,
  292.      doc_type_id, qty, uom_id, gl_curr_code, gl_amount, flg_amount)
  293.     SELECT pSessionId, A.tenant_id, D.ou_bu_id, D.ou_id, B.product_id,
  294.         A.doc_type_id, B.qty_realization, B.base_uom_id, vCurrGL, 0, vEmptyValue
  295.     FROM i_trx_inventory A, i_trx_inventory_item B, dt_date C, m_ou_structure D, vw_finish_goods_product E
  296.     WHERE A.tenant_id = pTenantId AND
  297.         A.doc_date = C.string_date AND
  298.         C.year_month_date = pYearMonth AND
  299.         A.trx_inventory_id = B.trx_inventory_id AND
  300.         A.ou_id = D.ou_id AND
  301.         D.ou_bu_id = pOuId AND
  302.         A.doc_type_id = 413 AND
  303.         B.qty_realization > 0 AND
  304.         B.product_id = E.product_id;   
  305.  */
  306.     /*
  307.      * buat data yang akan menjadi acuan harga standard
  308.      * 1.data purch price di bulan bersangkutan
  309.      * 2.data nilai barang di awal bulan
  310.      * 3.data master harga standard
  311.      */
  312.     INSERT INTO tt_in_product_base_price
  313.     (session_id, tenant_id, ou_id, product_id,
  314.      flg_amount, qty, uom_id, gl_curr_code, gl_amount)
  315.     SELECT A.session_id, A.tenant_id, A.ou_bu_id, A.product_id,
  316.         'PURCH_PRICE', SUM(A.qty), A.uom_id, A.gl_curr_code, SUM(A.gl_amount)
  317.     FROM tt_in_doc_product_price A
  318.     WHERE A.session_id = pSessionId AND
  319.         A.doc_type_id IN ( 111)
  320.     GROUP BY A.session_id, A.tenant_id, A.ou_bu_id, A.product_id,
  321.         A.uom_id, A.gl_curr_code;
  322.    
  323.     INSERT INTO tt_in_product_base_price
  324.     (session_id, tenant_id, ou_id, product_id,
  325.      flg_amount, qty, uom_id, gl_curr_code, gl_amount)
  326.     SELECT pSessionId, A.tenant_id, A.ou_bu_id, A.product_id,
  327.         'BGN_BALANCE', SUM(A.qty), A.base_uom_id, A.gl_curr_code, SUM(A.gl_amount)
  328.     FROM in_summary_monthly_amount A, vw_finish_goods_product E
  329.     WHERE A.date_year_month = pYearMonth AND
  330.         A.doc_type_id = vDocTypeAwal AND
  331.         A.tenant_id = pTenantId AND
  332.         A.ou_bu_id = pOuId AND
  333.         A.product_id = E.product_id
  334.     GROUP BY A.tenant_id, A.ou_bu_id, A.product_id,
  335.         A.base_uom_id, A.gl_curr_code;
  336.    
  337.     INSERT INTO tt_in_product_base_price
  338.     (session_id, tenant_id, ou_id, product_id,
  339.      flg_amount, qty, uom_id, gl_curr_code, gl_amount)
  340.     SELECT pSessionId, A.tenant_id, A.ou_id, A.product_id,
  341.         'STANDARD_PRICE', 0, E.base_uom_id, A.curr_code, SUM(A.amount)
  342.     FROM in_product_standard_cogs A, vw_finish_goods_product E
  343.     WHERE A.tenant_id = pTenantId AND
  344.         A.ou_id = pOuId AND
  345.         A.year_month_date = pYearMonth AND
  346.         A.product_id = E.product_id
  347.     GROUP BY A.tenant_id, A.ou_id, A.product_id, A.curr_code, E.base_uom_id;
  348.    
  349.     INSERT INTO tt_in_product_base_price
  350.     (session_id, tenant_id, ou_id, product_id,
  351.      flg_amount, qty, uom_id, gl_curr_code, gl_amount)
  352.     SELECT pSessionId, A.tenant_id, A.ou_id, A.product_id,
  353.         'PREV_MONTH_COGS', A.qty_total, E.base_uom_id, A.curr_code, A.amount_total
  354.     FROM in_summary_monthly_cogs A, vw_finish_goods_product E
  355.     WHERE A.tenant_id = pTenantId AND
  356.         A.ou_id = pOuId AND
  357.         A.product_id = E.product_id AND
  358.         A.date_year_month = (SELECT MAX(Z.date_year_month)
  359.                              FROM in_summary_monthly_cogs Z
  360.                              WHERE A.tenant_id = Z.tenant_id AND
  361.                                 Z.date_year_month < pYearMonth AND
  362.                                 Z.ou_id = A.ou_id AND
  363.                                 Z.product_id = A.product_id AND
  364.                                 Z.qty_total <> 0);
  365.    
  366.     /*
  367.      * isi nilai data tt_in_add_product_unamount dengan urutan :
  368.      * 1. update dengan data flg amount STANDARD_PRICE
  369.      * 2. jika belum ada, update dengan data flg amount BGN_BALANCE
  370.      * 3. jika belum ada, update dengan data flg amount PURCH_PRICE
  371.      * 4. jika belum ada, update dengan data flg amount PREV_MONTH_COGS
  372.      */
  373.    
  374.     UPDATE tt_in_add_product_unamount SET gl_amount = tt_in_add_product_unamount.qty * A.gl_amount, flg_amount = A.flg_amount
  375.     FROM tt_in_product_base_price A
  376.     WHERE A.session_id = pSessionId AND
  377.         tt_in_add_product_unamount.session_id = A.session_id AND
  378.         tt_in_add_product_unamount.tenant_id = A.tenant_id AND
  379.         tt_in_add_product_unamount.ou_bu_id = A.ou_id AND
  380.         tt_in_add_product_unamount.product_id = A.product_id AND
  381.         tt_in_add_product_unamount.flg_amount = vEmptyValue AND
  382.         A.flg_amount = 'STANDARD_PRICE';
  383.    
  384.     UPDATE tt_in_add_product_unamount SET gl_amount = ROUND(tt_in_add_product_unamount.qty * ( A.gl_amount / A.qty ), vRoundingDigit), flg_amount = A.flg_amount
  385.     FROM tt_in_product_base_price A
  386.     WHERE A.session_id = pSessionId AND
  387.         tt_in_add_product_unamount.session_id = A.session_id AND
  388.         tt_in_add_product_unamount.tenant_id = A.tenant_id AND
  389.         tt_in_add_product_unamount.ou_bu_id = A.ou_id AND
  390.         tt_in_add_product_unamount.product_id = A.product_id AND
  391.         tt_in_add_product_unamount.flg_amount = vEmptyValue AND    
  392.         A.flg_amount = 'BGN_BALANCE' AND
  393.         A.qty <> 0;
  394.        
  395.     UPDATE tt_in_add_product_unamount SET gl_amount = ROUND(tt_in_add_product_unamount.qty * ( A.gl_amount / A.qty ), vRoundingDigit), flg_amount = A.flg_amount
  396.     FROM tt_in_product_base_price A
  397.     WHERE A.session_id = pSessionId AND
  398.         tt_in_add_product_unamount.session_id = A.session_id AND
  399.         tt_in_add_product_unamount.tenant_id = A.tenant_id AND
  400.         tt_in_add_product_unamount.ou_bu_id = A.ou_id AND
  401.         tt_in_add_product_unamount.product_id = A.product_id AND
  402.         tt_in_add_product_unamount.flg_amount = vEmptyValue AND    
  403.         A.flg_amount = 'PURCH_PRICE' AND
  404.         A.qty <> 0;
  405.  
  406.     -- Ambil dari monthly cogs terakhir
  407.     UPDATE tt_in_add_product_unamount SET gl_amount = ROUND(tt_in_add_product_unamount.qty * ( A.gl_amount / A.qty ), vRoundingDigit), flg_amount = A.flg_amount
  408.     FROM tt_in_product_base_price A
  409.     WHERE A.session_id = pSessionId AND
  410.         tt_in_add_product_unamount.session_id = A.session_id AND
  411.         tt_in_add_product_unamount.tenant_id = A.tenant_id AND
  412.         tt_in_add_product_unamount.ou_bu_id = A.ou_id AND
  413.         tt_in_add_product_unamount.product_id = A.product_id AND
  414.         tt_in_add_product_unamount.flg_amount = vEmptyValue AND    
  415.         A.flg_amount = 'PREV_MONTH_COGS' AND
  416.         A.qty <> 0;
  417.  
  418.        
  419. /*
  420.  *  menghitung harga rata rata product (non assembly ) berdasarkan :
  421.  *  1. harga purchasing
  422.  *  2. harga adj stock amount ( bukan yg balance amount ), cost allocation product
  423.  *  3. transaksi tambah stok qty, dengan harga sudah diproses : retur jual, adj stok qty > 0
  424.  *  4. harga saldo awal
  425.  */    
  426.     INSERT INTO tt_in_summary_monthly_product
  427.     (session_id, date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  428.     doc_type_id, product_id, base_uom_id, qty,
  429.     gl_curr_code, gl_amount, flg_amount)
  430.     SELECT pSessionId, pYearMonth, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  431.         A.doc_type_id, A.product_id, A.uom_id, SUM(A.qty),
  432.         A.gl_curr_code, SUM(A.gl_amount), 'PURCH_PRICE'
  433.     FROM tt_in_doc_product_price A
  434.     WHERE A.session_id = pSessionId AND
  435.         A.doc_type_id IN (111) AND
  436.         A.qty > 0
  437.     GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.doc_type_id, A.product_id, A.uom_id, A.gl_curr_code;
  438.    
  439.     -- Mod by WTC, 20170418, transaksi claim note juga ikut diambil
  440.     INSERT INTO tt_in_summary_monthly_product
  441.     (session_id, date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  442.     doc_type_id, product_id, base_uom_id, qty,
  443.     gl_curr_code, gl_amount, flg_amount)
  444.     SELECT pSessionId, pYearMonth, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  445.         A.doc_type_id, A.product_id, A.uom_id, SUM(A.qty),
  446.         A.gl_curr_code, SUM(A.gl_amount), 'TRANSACTION'
  447.     FROM tt_in_doc_product_price A
  448.     WHERE A.session_id = pSessionId AND
  449.         (A.doc_type_id IN ( 522, 528, 511 ) OR (A.doc_type_id = 111 AND A.qty < 0))
  450.     GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.doc_type_id, A.product_id, A.uom_id, A.gl_curr_code;
  451.  
  452.     INSERT INTO tt_in_summary_monthly_product
  453.     (session_id, date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  454.     doc_type_id, product_id, base_uom_id, qty,
  455.     gl_curr_code, gl_amount, flg_amount)
  456.     SELECT pSessionId, pYearMonth, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  457.         A.doc_type_id, A.product_id, A.uom_id, SUM(A.qty),
  458.         A.gl_curr_code, SUM(A.gl_amount), A.flg_amount
  459.     FROM tt_in_add_product_unamount A
  460.     WHERE A.session_id = pSessionId
  461.     GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.doc_type_id, A.product_id, A.uom_id, A.gl_curr_code, A.flg_amount;
  462.  
  463.     INSERT INTO tt_in_summary_monthly_product
  464.     (session_id, date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  465.     doc_type_id, product_id, base_uom_id, qty,
  466.     gl_curr_code, gl_amount, flg_amount)
  467.     SELECT pSessionId, pYearMonth, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  468.         A.doc_type_id, A.product_id, A.base_uom_id, SUM(A.qty),
  469.         A.gl_curr_code, SUM(A.gl_amount), A.flg_amount
  470.     FROM in_summary_monthly_amount A, vw_finish_goods_product E
  471.     WHERE A.date_year_month = pYearMonth AND
  472.         A.doc_type_id = vDocTypeAwal AND
  473.         A.ou_bu_id = pOuId AND
  474.         A.tenant_id = pTenantId AND
  475.         A.product_id = E.product_id
  476.     GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.doc_type_id, A.product_id, A.base_uom_id, A.gl_curr_code, A.flg_amount;
  477.  
  478.     /*
  479.      * hitung harga rata-rata sebagai dasar harga pokok
  480.      */
  481.     INSERT INTO tt_in_summary_monthly_cogs
  482.     (session_id, date_year_month, tenant_id, ou_id,
  483.     product_id, qty_total, curr_code, amount_total, avg_price)
  484.     SELECT pSessionId, pYearMonth, A.tenant_id, A.ou_bu_id,
  485.         A.product_id, SUM(A.qty), A.gl_curr_code, SUM(A.gl_amount), 0
  486.     FROM tt_in_summary_monthly_product A
  487.     WHERE A.session_id = pSessionId
  488.     GROUP BY A.tenant_id, A.ou_bu_id, A.product_id, A.gl_curr_code;
  489.    
  490.     UPDATE tt_in_summary_monthly_cogs
  491.       SET avg_price = amount_total / qty_total
  492.     WHERE session_id = pSessionId AND
  493.       qty_total <> 0;
  494.    
  495.     /*
  496.      * memberikan nilai untuk transaksi stok yang mengurangi qty, sesuai harga rata rata bulan ini
  497.      * transaksi selain adj stok qty(521) dan adj stok qty outlet(413) , yang qty < 0
  498.      * transaksi pemakaian Nota klaim, DO, DO Internal, Sales Invoice Konsinyasi, POS Shop, Return POS Shop, Pos Shop In Shop, Return POS Shop In Shop
  499.      * transaksi pemakaian Nota klaim (511), DO (311) , POS Shop (431), Pos Shop In Shop(431)
  500.      * NK, 16 Feb 2015 : tambahan transaksi DO Receipt ( 526 )
  501.      * Sales Invoice Konsinyasi(354), DO Internal tidak jadi dipakai(154)
  502.      * Return POS Shop, Return POS SHop In SHop belum ada
  503.      * WTC, 160622 : tambahan transaksi GTI receipt lost ( 537 )
  504.      * WTC, 160211 : tambahan transaksi DO MLM ( 313 ), exchange MLM out (560), exclude claim
  505.      */
  506.     INSERT INTO tt_in_summary_monthly_product
  507.     (session_id, date_year_month, tenant_id,  ou_bu_id, ou_branch_id, ou_sub_bu_id,
  508.     doc_type_id, product_id, base_uom_id, qty,
  509.     gl_curr_code, gl_amount, flg_amount)
  510.     SELECT pSessionId, pYearMonth, A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id,
  511.         A.doc_type_id, A.product_id, A.base_uom_id, SUM(A.qty),
  512.         vCurrGL, 0, 'MONTHLY_AVG'
  513.     FROM in_combine_log_product_balance_stock A, m_ou_structure B, vw_finish_goods_product E
  514.     WHERE A.ou_id = B.ou_id AND
  515.         B.ou_bu_id = pOuId AND
  516.         A.date_year_month = pYearMonth AND
  517.         A.doc_type_id IN (311,431,526,313,537) AND
  518.         A.product_id = E.product_id
  519.     GROUP BY A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id, A.doc_type_id, A.product_id, A.base_uom_id;  
  520.  
  521.     INSERT INTO tt_in_summary_monthly_product
  522.     (session_id, date_year_month, tenant_id,  ou_bu_id, ou_branch_id, ou_sub_bu_id,
  523.     doc_type_id, product_id, base_uom_id, qty,
  524.     gl_curr_code, gl_amount, flg_amount)
  525.     SELECT pSessionId, pYearMonth, A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id,
  526.         A.doc_type_id, A.product_id, A.base_uom_id, SUM(A.qty),
  527.         vCurrGL, 0, 'MONTHLY_AVG'
  528.     FROM in_combine_log_product_balance_stock A, m_ou_structure B, vw_finish_goods_product E
  529.     WHERE A.ou_id = B.ou_id AND
  530.         B.ou_bu_id = pOuId AND
  531.         A.date_year_month = pYearMonth AND
  532.         A.doc_type_id IN (521, 413) AND
  533.         A.product_id = E.product_id AND
  534.         A.qty < 0 AND
  535.         A.flg_buy_konsinyasi = vFlagNo
  536.     GROUP BY A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id, A.doc_type_id, A.product_id, A.base_uom_id;
  537.  
  538.     -- WTC, 160622, ambil data exchange MLM out
  539.     INSERT INTO tt_in_summary_monthly_product
  540.     (session_id, date_year_month, tenant_id,  ou_bu_id, ou_branch_id, ou_sub_bu_id,
  541.     doc_type_id, product_id, base_uom_id, qty,
  542.     gl_curr_code, gl_amount, flg_amount)
  543.     SELECT pSessionId, pYearMonth, A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id,
  544.         A.doc_type_id, A.product_id, A.base_uom_id, SUM(A.qty),
  545.         vCurrGL, 0, 'MONTHLY_AVG'
  546.     FROM in_combine_log_product_balance_stock A, m_ou_structure B, vw_finish_goods_product E
  547.     WHERE A.ou_id = B.ou_id AND
  548.         B.ou_bu_id = pOuId AND
  549.         A.date_year_month = pYearMonth AND
  550.         A.doc_type_id = 560 AND
  551.         A.product_id = E.product_id AND
  552.         A.qty < 0
  553.     GROUP BY A.tenant_id, B.ou_bu_id, B.ou_branch_id, B.ou_sub_bu_id, A.doc_type_id, A.product_id, A.base_uom_id;
  554.    
  555. /*  NK, 25 Jan 2014, submit adj stok qty diubah sehingga menghasilkan 2 jenis log data product balance stock
  556.  *  qty < 0 dan qty > 0 dipisahkan
  557.     INSERT INTO tt_in_summary_monthly_product
  558.     (session_id, date_year_month, tenant_id, ou_id, sub_ou_id,
  559.     doc_type_id, product_id, base_uom_id, qty,
  560.     gl_curr_code, gl_amount, flg_amount)
  561.     SELECT pSessionId, pYearMonth, A.tenant_id, C.ou_bu_id, C.ou_id,
  562.         A.doc_type_id, B.product_id, B.base_uom_id, SUM(B.qty_realization),
  563.         vCurrGL, 0, 'MONTHLY_AVG'
  564.     FROM in_inventory A, in_inventory_item B, m_ou_structure C, dt_date D, vw_finish_goods_product E
  565.     WHERE A.ou_from_id = C.ou_id AND
  566.         C.ou_bu_id = pOuId AND
  567.         A.inventory_id = B.inventory_id AND
  568.         A.doc_date = D.string_date AND
  569.         D.date_year_month = pYearMonth AND
  570.         A.doc_type_id IN (521, 413) AND
  571.         B.qty_realization < 0 AND
  572.         B.product_id = E.product_id
  573.     GROUP BY A.tenant_id, C.ou_bu_id, C.ou_id, A.doc_type_id, B.product_id, B.base_uom_id;
  574. */
  575.     UPDATE tt_in_summary_monthly_product SET gl_amount = ROUND(qty * (A.amount_total / A.qty_total), vRoundingDigit)
  576.     FROM tt_in_summary_monthly_cogs A
  577.     WHERE A.session_id = pSessionId AND
  578.         tt_in_summary_monthly_product.session_id = A.session_id AND
  579.         tt_in_summary_monthly_product.tenant_id = A.tenant_id AND
  580.         tt_in_summary_monthly_product.ou_bu_id = A.ou_id AND
  581.         tt_in_summary_monthly_product.product_id = A.product_id AND
  582.         tt_in_summary_monthly_product.doc_type_id IN (311,431,526,521,413,313,560,537) AND
  583.         tt_in_summary_monthly_product.flg_amount = 'MONTHLY_AVG' AND
  584.         A.qty_total <> 0;
  585.  
  586.     -- WTC, 170502, khusus untuk product tidak ada COGS, maka ambil nilainya dari master harga standar
  587.     UPDATE tt_in_summary_monthly_product SET gl_amount = ROUND(tt_in_summary_monthly_product.qty * A.gl_amount, vRoundingDigit), flg_amount = A.flg_amount
  588.     FROM tt_in_product_base_price A
  589.     WHERE A.session_id = pSessionId AND
  590.         A.flg_amount = 'STANDARD_PRICE' AND
  591.         tt_in_summary_monthly_product.session_id = A.session_id AND
  592.         tt_in_summary_monthly_product.tenant_id = A.tenant_id AND
  593.         tt_in_summary_monthly_product.ou_bu_id = A.ou_id AND
  594.         tt_in_summary_monthly_product.product_id = A.product_id AND
  595.         tt_in_summary_monthly_product.doc_type_id IN (311,431,526,521,413,313,560,537) AND
  596.         tt_in_summary_monthly_product.flg_amount = 'MONTHLY_AVG' AND
  597.         NOT EXISTS (SELECT 1 FROM tt_in_summary_monthly_cogs B
  598.             WHERE tt_in_summary_monthly_product.session_id = B.session_id AND
  599.                 tt_in_summary_monthly_product.tenant_id = B.tenant_id AND
  600.                 tt_in_summary_monthly_product.ou_bu_id = B.ou_id AND
  601.                 tt_in_summary_monthly_product.product_id = B.product_id AND
  602.                 B.qty_total <> 0);
  603.    
  604.     /*
  605.      * untuk menghitung saldo akhir di bulan tersebut, dengan cara membuat record saldo awal untuk bulan berikutnya
  606.      * 1. ambil data adj stok balance amount ( adjust nilai akhir saldo barang )
  607.      * 2. hitung saldo akhir
  608.      */    
  609.     INSERT INTO tt_in_summary_monthly_product
  610.     (session_id, date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  611.     doc_type_id, product_id, base_uom_id, qty,
  612.     gl_curr_code, gl_amount, flg_amount)
  613.     SELECT pSessionId, pYearMonth, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  614.         A.doc_type_id, A.product_id, A.uom_id, SUM(A.qty),
  615.         A.gl_curr_code, SUM(A.gl_amount), 'TRANSACTION'
  616.     FROM tt_in_doc_product_price A
  617.     WHERE A.session_id = pSessionId AND
  618.         A.doc_type_id = 523
  619.     GROUP BY A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id, A.doc_type_id, A.product_id, A.uom_id, A.gl_curr_code;
  620.        
  621.     /*
  622.      * Persiapan data saldo awal untuk bulan berikutnya. Periksa terlebih dahulu apakah ada data yg qty = 0, tetapi gl_amount <> 0.
  623.      * Nilai gl_amount tsb akan dialokasikan ke data transaksi pengeluaran stok dengan id terbesar untuk product ybs.
  624.      */
  625.     INSERT INTO tt_in_summary_monthly_amount_remain
  626.     (session_id, date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  627.     doc_type_id, product_id, base_uom_id, qty,
  628.     gl_curr_code, gl_amount, flg_amount, summary_monthly_product_id)
  629.     SELECT pSessionId, A.date_year_month, A.tenant_id, A.ou_bu_id, vEmptyId, vEmptyId,
  630.         vDocTypeAwal, A.product_id, A.base_uom_id, SUM(A.qty),
  631.         vCurrGL, SUM(A.gl_amount), 'BGN_BALANCE', vEmptyId
  632.     FROM tt_in_summary_monthly_product A
  633.     WHERE A.session_id = pSessionId
  634.     GROUP BY A.date_year_month, A.tenant_id, A.ou_bu_id, A.product_id, A.base_uom_id;
  635.    
  636.     -- Cari id data sebagai penampung alokasi nilai
  637.     UPDATE tt_in_summary_monthly_amount_remain
  638.         SET summary_monthly_product_id = (
  639.             SELECT MAX(A.summary_monthly_product_id)
  640.             FROM tt_in_summary_monthly_product A
  641.             WHERE A.session_id = pSessionId AND
  642.                 tt_in_summary_monthly_amount_remain.session_id = A.session_id AND
  643.                 tt_in_summary_monthly_amount_remain.date_year_month = A.date_year_month AND
  644.                 tt_in_summary_monthly_amount_remain.tenant_id = A.tenant_id AND
  645.                 tt_in_summary_monthly_amount_remain.ou_bu_id = A.ou_bu_id AND
  646.                 tt_in_summary_monthly_amount_remain.product_id = A.product_id AND
  647.                 A.doc_type_id IN (311,431,526,521,413,313,560,537) AND
  648.                 A.flg_amount = 'MONTHLY_AVG')
  649.     WHERE session_id = pSessionId AND qty = 0 AND gl_amount <> 0;
  650.    
  651.     /*
  652.      * NK, 16 Feb 2015
  653.      * simpan terlebih dahulu data product yang qty = 0, dan nilai <> 0
  654.      * dimana nilai dari product tersebut akan dialokasi ke trx yang menggunakan produk tersebut
  655.      */
  656.     INSERT INTO in_summary_monthly_zero_qty_amount
  657.     (date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  658.     product_id, qty_sum, base_uom_id, gl_curr_code, amount_sum,
  659.     doc_type_id, qty, gl_amount, remark)
  660.     SELECT A.date_year_month, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  661.         A.product_id, A.qty, A.base_uom_id, A.gl_curr_code, A.gl_amount,
  662.         B.doc_type_id, B.qty, B.gl_amount, vGroupProductFG
  663.     FROM tt_in_summary_monthly_amount_remain A, tt_in_summary_monthly_product B
  664.     WHERE A.session_id = pSessionId AND
  665.         A.summary_monthly_product_id <> vEmptyId AND
  666.         B.summary_monthly_product_id = A.summary_monthly_product_id;
  667.    
  668.     UPDATE tt_in_summary_monthly_product SET gl_amount = tt_in_summary_monthly_product.gl_amount - A.gl_amount
  669.     FROM tt_in_summary_monthly_amount_remain A
  670.     WHERE A.session_id = pSessionId AND
  671.         A.summary_monthly_product_id <> vEmptyId AND
  672.         tt_in_summary_monthly_product.summary_monthly_product_id = A.summary_monthly_product_id;
  673.        
  674.     INSERT INTO tt_in_summary_monthly_product
  675.     (session_id, date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  676.     doc_type_id, product_id, base_uom_id, qty, gl_curr_code, gl_amount, flg_amount)
  677.     SELECT A.session_id, vNextYearMonth, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  678.         A.doc_type_id, A.product_id, A.base_uom_id, A.qty, A.gl_curr_code, A.gl_amount, A.flg_amount
  679.     FROM tt_in_summary_monthly_amount_remain A
  680.     WHERE A.session_id = pSessionId AND A.qty <> 0;
  681.                
  682.     INSERT INTO in_summary_monthly_cogs
  683.     (date_year_month, tenant_id, ou_id, product_id,
  684.     qty_total, curr_code, amount_total, avg_price,
  685.     "version", create_datetime, create_user_id, update_datetime, update_user_id, remark)
  686.     SELECT A.date_year_month, A.tenant_id, A.ou_id, A.product_id,
  687.             A.qty_total, A.curr_code, A.amount_total, A.avg_price,
  688.             0, pDatetime, pUserId, pDatetime, pUserId, vGroupProductFG
  689.     FROM tt_in_summary_monthly_cogs A
  690.     WHERE A.session_id = pSessionId;
  691.        
  692.     INSERT INTO in_summary_monthly_amount
  693.     (date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  694.     doc_type_id, product_id, base_uom_id, qty,
  695.     gl_curr_code, gl_amount, flg_amount,
  696.     "version", create_datetime, create_user_id, update_datetime, update_user_id, remark)
  697.     SELECT A.date_year_month, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  698.         A.doc_type_id, A.product_id, A.base_uom_id, A.qty,
  699.         A.gl_curr_code, A.gl_amount, A.flg_amount,
  700.         0, pDatetime, pUserId, pDatetime, pUserId, vGroupProductFG
  701.     FROM tt_in_summary_monthly_product A
  702.     WHERE A.session_id = pSessionId AND
  703.             A.date_year_month = pYearMonth AND
  704.             (A.doc_type_id NOT IN (vDocTypeAwal, 111) OR (A.doc_type_id = 111 AND A.qty < 0));
  705.  
  706.     INSERT INTO in_summary_monthly_amount
  707.     (date_year_month, tenant_id, ou_bu_id, ou_branch_id, ou_sub_bu_id,
  708.     doc_type_id, product_id, base_uom_id, qty,
  709.     gl_curr_code, gl_amount, flg_amount,
  710.     "version", create_datetime, create_user_id, update_datetime, update_user_id, remark)
  711.     SELECT A.date_year_month, A.tenant_id, A.ou_bu_id, A.ou_branch_id, A.ou_sub_bu_id,
  712.         A.doc_type_id, A.product_id, A.base_uom_id, A.qty,
  713.         A.gl_curr_code, A.gl_amount, A.flg_amount,
  714.         0, pDatetime, pUserId, pDatetime, pUserId, vGroupProductFG
  715.     FROM tt_in_summary_monthly_product A
  716.     WHERE A.session_id = pSessionId AND
  717.         A.date_year_month = vNextYearMonth;
  718.  
  719.     -- assembly belum dipakai, jadi dihapus dulu saja
  720.        
  721.     DELETE FROM tt_in_doc_product_price WHERE session_id = pSessionId;
  722.    
  723.     DELETE FROM tt_in_add_product_unamount WHERE session_id = pSessionId;
  724.    
  725.     DELETE FROM tt_in_product_base_price WHERE session_id = pSessionId;
  726.    
  727.     DELETE FROM tt_in_summary_monthly_product WHERE session_id = pSessionId;
  728.    
  729.     DELETE FROM tt_in_summary_monthly_cogs WHERE session_id = pSessionId;
  730.    
  731.     DELETE FROM tt_in_summary_monthly_amount_remain WHERE session_id = pSessionId;
  732.    
  733.  END;
  734. $BODY$
  735.   LANGUAGE plpgsql VOLATILE
  736.   COST 100;
  737.   /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement