Advertisement
aadddrr

in_process_costing_finish_goods

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