Advertisement
aadddrr

in_process_costing_finish_goods_20180212

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