Advertisement
Kwards

Untitled

Feb 28th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Function: sl_so_mlm_remove_item(bigint, character varying, bigint, bigint, bigint)
  2.  
  3. -- DROP FUNCTION sl_so_mlm_remove_item(bigint, character varying, bigint, bigint, bigint);
  4.  
  5. CREATE OR REPLACE FUNCTION sl_so_mlm_remove_item(bigint, character varying, bigint, bigint, bigint)
  6.   RETURNS SETOF refcursor AS
  7. $BODY$
  8. DECLARE
  9.     pTenantId               alias for $1;
  10.     pDateTime               alias for $2;              
  11.     pUserId                 alias for $3;
  12.     pSoMlmItemId            alias for $4;
  13.     pSoMlmId                alias for $5;
  14.  
  15.     vProductId              bigint;
  16.     vProductCatalogId           bigint;
  17.     vDecimalRounding            integer;
  18.     vRoundingMode           character varying;
  19.     vFlagTaxAmount          character varying;
  20.    
  21.     vSubTotalPriceAfterDisc numeric;
  22.     vYearMonth              character varying(6);
  23.     vPartnerId              bigint;
  24.    
  25.     pRefHeader              REFCURSOR := 'refHeader';
  26.    
  27. BEGIN
  28. /**
  29.  * 1. cari Sub Total Price After Disc untuk di gunakan sebagai pengurang usage_amount
  30.  * 2. Update table reserved, dikurangin dengan seluruh qty_so dari item tersebut
  31.  * 3. Delete dari sl_so_mlm_item
  32.  * 4. Update commission amount di sl_so_mlm, kurangi dengan komisi dari item tersebut
  33.  * 5. Kurangi nilai usage_amount di table sl_partner_credit_limit
  34.  *
  35.  */
  36.    
  37.         vDecimalRounding := 0;
  38.         vRoundingMode := 'RD';
  39.         vFlagTaxAmount := 'Y';
  40.        
  41.         vSubTotalPriceAfterDisc := 0;
  42.         vYearMonth := ' ';
  43.         vPartnerId := -99;
  44.        
  45.        
  46.      -- 1. cari Sub Total Price After Disc untuk di gunakan sebagai pengurang usage_amount
  47.         SELECT (qty_so * price_after_disc) INTO vSubTotalPriceAfterDisc
  48.             FROM sl_so_mlm_item WHERE so_mlm_item_id = pSoMlmItemId;
  49.        
  50.         SELECT product_catalog_id INTO vProductCatalogId FROM sl_so_mlm_item WHERE so_mlm_item_id = pSoMlmItemId;
  51.    
  52.         SELECT f_get_product_by_product_catalog_id(vProductCatalogId) INTO vProductId;
  53.    
  54.      -- 2. update ke in_product_balance_reserved_stock
  55.         DELETE FROM in_product_balance_stock_reserved Z WHERE EXISTS(
  56.                  SELECT 1
  57.                  FROM sl_so_mlm A
  58.                  WHERE A.so_mlm_id = pSoMlmId
  59.                  AND Z.ref_id =A.so_mlm_id
  60.                  AND Z.ref_doc_type_id = A.doc_type_id
  61.         );
  62.              
  63.      -- 3. Delete dari sl_so_mlm_item
  64.         DELETE FROM sl_so_mlm_item WHERE so_mlm_item_id = pSoMlmItemId;
  65.        
  66.      -- 4. Update commission amount di sl_so_mlm, kurangi dengan komisi dari item tersebut
  67.        UPDATE sl_so_mlm SET commission_amount = f_get_commission_amount_by_so_mlm_id(pTenantId, pSoMlmId, vDecimalRounding, vRoundingMode)
  68.        WHERE so_mlm_id = pSoMlmId;
  69.  
  70.        
  71.       -- 5. Penambahan untuk update ke table sl_partner_credit limit_usage --
  72.         -- Cari year_month dari doc_date
  73.         SELECT LEFT(doc_date, 6) INTO vYearMonth
  74.             FROM sl_so_mlm WHERE so_mlm_id = pSoMlmId;
  75.        
  76.         -- Cari partner_id yang pesan SO MLM
  77.         SELECT partner_id INTO vPartnerId
  78.             FROM sl_so_mlm WHERE so_mlm_id = pSoMlmId;
  79.        
  80.         -- [update 22 desember 2015 by hary] partner credit limit usage dihitung pada saat approve
  81.            
  82. --      -- Update sl_partner_usage_amount + vSubTotalPriceAfterDisc
  83. --          UPDATE sl_partner_credit_limit_usage
  84. --          SET    usage_amount = usage_amount - vSubTotalPriceAfterDisc,
  85. --                 version = version + 1,
  86. --                 update_datetime = pDateTime,
  87. --                 update_user_id = pUserId
  88. --          WHERE  tenant_id = pTenantId
  89. --          AND    partner_id = vPartnerId
  90. --          AND    year_month = vYearMonth;
  91. --     
  92. --         
  93. --      -- Insert sl_log_partner_usage_amount
  94. --      INSERT INTO sl_log_partner_credit_limit_usage(
  95. --              tenant_id, partner_id, doc_type_id,
  96. --              doc_id, doc_no, doc_date, amount, version,
  97. --              create_datetime, create_user_id, update_datetime, update_user_id)
  98. --      SELECT  pTenantId, A.partner_id, A.doc_type_id,
  99. --              A.so_mlm_id, A.doc_no, A.doc_date, 0 - vSubTotalPriceAfterDisc, 0,
  100. --              pDateTime, pUserId, pDateTime, pUserId
  101. --      FROM    sl_so_mlm A
  102. --      WHERE   A.so_mlm_id = pSoMlmId;
  103.    
  104.        Open pRefHeader FOR
  105.        SELECT pSoMlmItemId;
  106.        RETURN NEXT pRefHeader;
  107.        
  108. END;
  109. $BODY$
  110.   LANGUAGE plpgsql VOLATILE
  111.   COST 100
  112.   ROWS 1000;
  113. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement