How to calculate opening and closing balance for period

Hi all,
i have to find out opening and closing balance.
the table structure of temporary table is
select * from hwcn_xn_fa_report_temp1 where asset_id=10029400
PERIOD_COUNTER CST_OP_BAL CST_ADDITION CST_TRANSFER CST_DISPOSAL COST_CLOSING_BALANCE
24108 0 0 0
24109 12000
24110 0 0 0
24111 0 0 0
in this table cst_op_balnce and cost_closing_balace is null
i have display cost_op_bal and cost_closing_balnce
cost_closing_balance=cst_op_bal+cst_addition+cst_transfer+cst_disposal
for period 2408 op_balnce=0 closing_bal=0
for period 2409 op_balnce=0 closing_balce=1200
for period 2410 op_bal=1200 closing_bal=1200
closing balance of dec will be opening bal of jan
thanks and regards
Edited by: user10664276 on Apr 19, 2009 11:08 PM
Edited by: user10664276 on Apr 19, 2009 11:13 PM

Hi,
user11118871 wrote:
Can you explain what that is? Thank you if you have one example.
ROWS BETWEEN  UNBOUNDED PRECEDING AND 1 PRECEDING
When you use the analytic SUM (c) function, then, on each row, it returns the values of column (or expression) c from several rows in the result set added together.
Which rows? That depends.
If the analytic clause (the part in parentheses after OVER) does not include ORDER BY, then it is all rows.
If the analytic clause has an ORDER BY clause, but no windowing clause (that is, ROWS BETWEEN ... or RANGE BETWEEN ...), then the rows included in the sum are all rows up to and including the row where the function is being called (as sorted by the analytic ORDER BY).
If the analytic cluase has both ORDER BY and a windowing clause "ROWS BETWEEN x PRECEDING AND y PRECEDING", then the rows included in the sum are the rows from x to y rows before the one where the function is called.
Do some experiments with different values of x and y.
First, create a table like the one in the problem above, but simplified a little.
CREATE TABLE     test_sum
(      period     NUMBER
,      new_amt     NUMBER
INSERT INTO test_sum (period, new_amt) VALUES (24108,     1);
INSERT INTO test_sum (period, new_amt) VALUES (24109,     4);
INSERT INTO test_sum (period, new_amt) VALUES (24110,     2);
INSERT INTO test_sum (period, new_amt) VALUES (24111,     8);
INSERT INTO test_sum (period, new_amt) VALUES (25001,     32);
INSERT INTO test_sum (period, new_amt) VALUES (25002,     16);
COMMIT;The original problem above used names that were meaningful for its application, and columns that have nothing to do with the SUM function. Let's simplify the former and lose the latter.
That problem involved the SUM of three columns added together. Since we just want to understand how the windowing clause works, let's simplify that to one column.
With these simplifications, my original query is:
SELECT       period
,       new_amt     
,       SUM (new_amt) OVER ( ORDER BY          period
                                     ROWS BETWEEN  UNBOUNDED PRECEDING
                             AND          1            PRECEDING
                    ) AS opening_balance
,       SUM (new_amt) OVER ( ORDER BY          period
                    ) AS closing_balance
FROM       test_sum
ORDER BY  period;Given the data above, it produces these results:
.   PERIOD    NEW_AMT OPENING_BALANCE CLOSING_BALANCE
     24108          1                               1
     24109          4               1               5
     24110          2               5               7
     24111          8               7              15
     25001         32              15              47
     25002         16              47              63So, for example, on the row where period=24110,
opening_balance=5, which is the total of new_amt from all rows up to but not including that row: 5=1+4, and
closing_balance=7, which is the total of new_amt from all rows up to and including that row: 7=1+4+2.
To really understand how the windowing clause works, do some experiments. Change the definition of opening_balance to include " BETWEEN x PRECEDING AND y PRECEDING". You'll find that:
(a) "UNBOUNDED PRECEDING" means the same as "n PRECEDING", where n is greater than the number of rows in your result set.
(b) "CURRENT ROW" means the same as "0 PRECEDING"
(c) x must be greater than or equal to y
(d) neither x nor y can be negative (but you can use "FOLLOWING" instead of "PRECEDING" to get the same effect).
For more, see the introduction to "Analytic Functions" in the [SQL Language manual|http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions001.htm#sthref972]
When you're finished, don't forget to
DROP TABLE     test_sum;

Similar Messages

  • Query For Finding Yearly Opening and Closing Balance for All the Items

    Hi Experts,
    I am working on Query Based Report for finding the Yearly Opening and Closing Stock for all the Items
    i will give yearwise selection and I want opening and closing stock in between that years
    Warm Regards,
    Sandip Kokate
    Edited by: Sandipk on May 20, 2011 1:58 PM

    Hi,
    Declare @SDate DateTime
    Declare @EDate DateTime
    Declare @Whse nvarchar(10)
    Set @SDate= (SELECT min(F_RefDate)  FROM  OFPR T1 where  T1.[Name] ='[1%]' )
    Set @EDate= (SELECT max(T_RefDate)  FROM  OFPR T1 where  T1.[Name] ='[%1]' )
    Set @Whse=(Select Max(s2.Warehouse) from OINM S2 Where S2.Warehouse = '[%2]')
    BEGIN
    Select @Whse as 'Warehouse', a.Itemcode, max(a.Dscription) as ItemName,
    sum(a.OpeningBalance) as OpeningBalance, sum(a.INq) as 'IN', sum(a.OUT) as OUT,
    ((sum(a.OpeningBalance) + sum(a.INq)) - Sum(a.OUT)) as Closing ,
    (Select i.InvntryUom from OITM i where i.ItemCode=a.Itemcode) as UOM
    from( Select N1.Warehouse, N1.Itemcode, N1.Dscription, (sum(N1.inqty)-sum(n1.outqty))
    as OpeningBalance, 0 as INq, 0 as OUT From dbo.OINM N1
    Where N1.DocDate < @SDate and N1.Warehouse = @Whse Group By N1.Warehouse,N1.ItemCode,
    N1.Dscription Union All select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance,
    sum(N1.inqty) , 0 as OUT From dbo.OINM N1 Where N1.DocDate >= @SDate and N1.DocDate <= @EDate
    and N1.Inqty >0 and N1.Warehouse = @Whse Group By N1.Warehouse,N1.ItemCode,N1.Dscription
    Union All select N1.Warehouse, N1.Itemcode, N1.Dscription, 0 as OpeningBalance, 0 , sum(N1.outqty) as OUT
    From dbo.OINM N1 Where N1.DocDate >= @SDate and N1.DocDate <=@EDate and N1.OutQty > 0
    and N1.Warehouse = @Whse Group By N1.Warehouse,N1.ItemCode,N1.Dscription) a, dbo.OITM I1
    where a.ItemCode=I1.ItemCode
    Group By a.Itemcode Having sum(a.OpeningBalance) + sum(a.INq) + sum(a.OUT) > 0 Order By a.Itemcode
    END
    I hope this will work for you.
    In above query you can also user OFPR.Code, OFPR, Category OFPR.Indicator instead of OFPR.Name.
    Regards
    Vaibhav Anharwadkar
    Edited by: Vaibhav Ancharwadkar on May 24, 2011 9:23 AM

  • Opening and closing balance for Excise duties for a particular plant

    Dear Experts,
                         Please suggest how I can get balance of excise duties for a particular plant at a given date in registers(RG23A & RG23C PartII).
    Thanks
    Lokesh

    Dear Lokesh,
    Just find the Google with "J33_BPP_32_CHEM_EN_IN" you will find the Word document  for standard SAP document
    With all the steps in details

  • Opening and closing balance logic

    How to get opening and closing balances for a material and chapter-id for tarif-wise report for sales and captive consumption...
    Edited by: vijetasap on Apr 13, 2009 2:25 PM

    Hi Vijeta...
    Try posting this question related to the functional domain forum of your requirement.. you might have more luck there..

  • Insert and update query to calculate the opening and closing balance

    create table purchase(productid number(5) ,dateofpurchase date,
    qty number(5));
    create table inventory(invid number(5),productid number(5),
    idate date,openingqty number(5),closingqty number(5));
    Records in inventory:
    1,1,'01-jan-2009', 10, 20
    2,1,'03-jan-2009', 20, 30
    3,1,'04-jan-2009', 40, 50
    when I enter the purchase invoice for 15 qty on 02-jan-2009
    after say '15-jan-09' , a new record should get inserted
    with opening balance = (closing balance before 02-jan-2009)
    and all the opening and closing balance for that product should
    get affected.
    If the invoice for 20 qty is entered for the existing date say
    '03-jan-2009' in inventory , then the closing balance
    for 03-jan-2009 should get updated and all the following records
    should get affected.
    I need the insert for the first one and update query for the
    second one.
    Vinodh

    <strike>You can do this in one statement by using the merge statement</strike>
    Hmm, maybe I spoke too soon.
    Edited by: Boneist on 25-Sep-2009 13:56
    Thinking about it, why do you want to design your system like this?
    Why not simply have your purchases table hold the required information and then either work out the inventory on the fly, or have a job that calls a procedure to add a row for the previous day?
    If you continue with this design, you're opening yourself up to a world of pain - what happens when the data doesn't match the purchases table? Also when is the inventory cut-off to reset the opening/closing balances? Monthly? Annually? Weekly? If it's set to one of those, what happens when the business request the inventory for a particular week?
    Edited by: Boneist on 25-Sep-2009 13:59

  • GL a/c opening and closing balances

    Hi All,
    Below is my selection screen:
    select-options : s_bukrs for bkpf-bukrs obligatory.
    parameter      :  p_gjahr like bkpf-gjahr obligatory.
    select-options : s_budat for bkpf-budat,
                           s_monat for bkpf-monat,
                           s_hkont for bseg-hkont.
    I am displaying the data from tables BKPF, BSEG, KNA1-NAME1, LFA1-NAME1, SKAT-TXT50. I have to fetch Opening and Closing balances for GL a/c's. I am fetching data from table GLT0-HSLVT(opening balance). But unable to get the data for closing balance based on the period given on the selection screen. In my report I am looping on table it_bseg to populate the final internal table. How to get the closing balances's total based on the input.
    Please help me.
    Thanks,
    Haritha

    Hello Haritha,
                        Try this code by building a function module. This would cover both opening and closing balance.
    FUNCTION ZGL_OPENING_CLOSING_BAL_KEYDAT.
    *"*"Local Interface:
    *"  IMPORTING
    *"     REFERENCE(COMP) TYPE  BKPF-BUKRS
    *"     REFERENCE(DATE) TYPE  BKPF-BUDAT
    *"     REFERENCE(ACCT) TYPE  BSEG-HKONT
    *"  EXPORTING
    *"     REFERENCE(BALANCE) TYPE  BSEG-DMBTR
    DATA : COMPANYCODEID  LIKE  BAPI0002_2-COMP_CODE     ,
    POSTING_DATE   LIKE  BAPI0002_4-POSTING_DATE  ,
    FISCAL_YEAR    LIKE  BAPI0002_4-FISCAL_YEAR   ,
    FISCAL_PERIOD  LIKE  BAPI0002_4-FISCAL_PERIOD .
    DATA : ACCOUNT_BALANCES  TYPE TABLE OF BAPI3006_4 WITH HEADER LINE.
    DATA : BAL TYPE BSEG-DMBTR.
    DATA : IV_DATE  TYPE  D   ,
    EV_MONTH_BEGIN_DATE  TYPE  D,
    EV_MONTH_END_DATE  TYPE  D .
    DATA : COMPANYCODE  LIKE  BAPI3006_0-COMP_CODE,
    GLACCT  LIKE  BAPI3006_0-GL_ACCOUNT ,
    FISCALYEAR  LIKE  BAPI3006_4-FISC_YEAR,
    CURRENCYTYPE  LIKE  BAPI3006_5-CURR_TYPE VALUE 10.
    DATA : IT_BKPF TYPE TABLE OF BKPF WITH HEADER LINE.
    DATA : BEGIN OF IT_BSEG OCCURS 0,
    BELNR TYPE BSEG-BELNR,
    GJAHR TYPE BSEG-GJAHR,
    SHKZG TYPE BSEG-SHKZG  ,
    DMBTR TYPE BSEG-DMBTR,
    END OF IT_BSEG.
    COMPANYCODEID = COMP.
    POSTING_DATE = DATE.
    IV_DATE  = DATE.
    CALL FUNCTION 'HR_JP_MONTH_BEGIN_END_DATE'
    EXPORTING
    IV_DATE             = IV_DATE
    IMPORTING
    EV_MONTH_BEGIN_DATE = EV_MONTH_BEGIN_DATE
    EV_MONTH_END_DATE   = EV_MONTH_END_DATE.
    CALL FUNCTION 'BAPI_COMPANYCODE_GET_PERIOD'
    EXPORTING
    COMPANYCODEID = COMPANYCODEID
    POSTING_DATE  = POSTING_DATE
    IMPORTING
    FISCAL_YEAR   = FISCAL_YEAR
    FISCAL_PERIOD = FISCAL_PERIOD.
    IF EV_MONTH_END_DATE NE DATE.
    IF FISCAL_PERIOD = 1.
    FISCAL_PERIOD = 12              .
    FISCAL_YEAR = FISCAL_YEAR - 1  .
    ELSE.
    FISCAL_PERIOD =  FISCAL_PERIOD - 1.
    ENDIF.
    ENDIF.
    COMPANYCODE  =  COMP.
    GLACCT  = ACCT.
    FISCALYEAR  = FISCAL_YEAR.
    CALL FUNCTION 'BAPI_GL_ACC_GETPERIODBALANCES'
    EXPORTING
    COMPANYCODE      = COMPANYCODE
    GLACCT           = GLACCT
    FISCALYEAR       = FISCALYEAR
    CURRENCYTYPE     = CURRENCYTYPE
    TABLES
    ACCOUNT_BALANCES = ACCOUNT_BALANCES.
    READ TABLE ACCOUNT_BALANCES  WITH KEY FISC_YEAR = FISCALYEAR FIS_PERIOD = FISCAL_PERIOD.
    IF SY-SUBRC = 0.
    BALANCE =  ACCOUNT_BALANCES-BALANCE.
    ENDIF.
    IF EV_MONTH_END_DATE NE DATE.
    SELECT * FROM BKPF INTO TABLE IT_BKPF WHERE BUKRS = COMP AND BUDAT >= EV_MONTH_BEGIN_DATE
    AND BUDAT <= DATE.
    IF IT_BKPF[] IS NOT INITIAL.
    SELECT BELNR GJAHR SHKZG DMBTR
    FROM BSEG INTO CORRESPONDING FIELDS OF TABLE IT_BSEG
    FOR ALL ENTRIES IN IT_BKPF
    WHERE BELNR = IT_BKPF-BELNR AND GJAHR = IT_BKPF-GJAHR AND HKONT = ACCT AND BUKRS = COMP.
    ENDIF.
    LOOP AT IT_BSEG.
    IF IT_BSEG-SHKZG = 'H'.
    BAL = BAL - IT_BSEG-DMBTR.
    ELSEIF IT_BSEG-SHKZG = 'S'.
    BAL = BAL + IT_BSEG-DMBTR.
    ENDIF.
    ENDLOOP.
    BALANCE = BALANCE + BAL.
    ENDIF.
    ENDFUNCTION.
    Raghav

  • Opening and closing balance

    Hi experts.
    I am looking for a formula to calculate opening and closing balance @ query level.
    points are waiting.

    Hi,
    I think you are using 0IC_C03 Cube.
    You can get ...liek below
    Opening Stock Qty =
    Valuated stock qty-- 0VALSTCKQTY
    Calendar Year/Month 0CALMONTH
    Z_SNGVAL-1
    Restrict  0VALSTCKQTY keyfigure with  0CALMONTH , Z_SNGVAL (Less then or equal to) is user entry Variable for Month, so Z_SNGVAL-1 give OIpening Stock.
    Closing Stock..
    Valuated stock qty ---0VALSTCKQTY
    Calendar Year/Month 0CALMONTH 8
    <=SNGVAL (Restrict with Less then or equal to)
    Thanks
    Reddy

  • Opening and Closing balance of a material

    How to check the opening and closing balance of a material in a given period?
    GL account /Plant/material number is known
    Can we see along with the PO/reservations made  in this period?
    Is there any report or Tcode?

    In mbew and mbewh table u will get the value and stock of material at plant level  for each period( if plant is valuation level ) . GL account u will get from valuation class attched to material master.
    Else use fi tcodefbl3n .
    if u want closing balance for period 6 then you  goto fbl3n and run the report for the GL ACC of stock till period 5. In that go to menu and in that special fields add bseg-matnr and bseg-plant and then sort the report on material/plant level. this will opening balance for period 6.
    then run same report till period 6 and this will give closing balance for period 6.
    this two report give opening and closing balace for period 6.

  • Opening and closing balance between 2 dates

    hi.
    i am facing a problem while fetching the opening and closing balance for a particular G/L account.
    In my  program  i am entering G/L account for a date range i.e, between two dates and i want opening and closing balance for these 2 date ranges.
    So, please tell me is there any function module to fetch opening and closing balance between two dates.
    Regards,
    ROHINI.K.

    Hi Rohini,
                 Check this FM :   BAPI_GL_GETGLACCPERIODBALANCES.
    If we use this functional module, we will get opening and closing balance of corresponding G/L Acc.
    (e.g)
    CALL FUNCTION 'BAPI_GL_GETGLACCPERIODBALANCES'
        EXPORTING
          companycode                   =  '1000'
          glacct                              = '0000548101'
          fiscalyear                         = '2007'    
         currencytype                      = '10'
       IMPORTING
    BALANCE_CARRIED_FORWARD       =  gs_bal-balance
         RETURN                                        =  v_return
        TABLES
          account_balances                            = gt_bal.

  • Opening balance and closing balances for discounts?

    Hi all,
    I have a typical sceanario where in
    opening balance for discounts 10,000 (balance of last month)
    actual discounts  from the sales order 5,000
    manual upload is   from flat file            2,000
    closing balance is 7,000 (which is opening balance + manual upload - actual discounts)
    opening and closing balance needs to be calculated, how to calculate nor handle this on every month?
    level of granularity of the data is
    sku, month, rebate category and rebate amount?
    Thanks

    Hi Pooja,
    can you please let us know few things about your report requirements
    What is the extractor you are using?
    What are the KFs ?
    what are the selection parameters that you want to enter at the reporting level?
    is it the report related to FI AR. do you want to display the values based on the customer -->profitcenter?
    Bcoz we had a same requirement where our requirement was global recievables aging report in which the report need to display opening bal, sales,adjustment,colelction and closing balance and the aging values.
    here what we did is changed the modeling of the DSO and infocube.
    we had used non-cumulative KF in order to calculate closing balance and from that derived opening balance and again create a new formula to get the closing balance.
    if you can be specific with your requirement hope can help you with your report.

  • Performance Issue For Opening And Closing Balance In FBL1N/3N/5N

    Dear experts,
                        I Am Having Requirement to Bring Opening And Closing Balance In FBL1N, FBL3N, FBL5N.
    For This requirement I Used BADI : FI_ITEMS_CH_DATA~CHANGE_ITEMS, below is my Code For FBL1N, And I've Done the same For 3N/5N...With Related BAPI
    *   IF SY-TCODE = 'FBL1N'.
    *    LOOP AT ct_items INTO gs_items.
    *      CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
    *        EXPORTING
    *          date      = gs_items-budat
    *          days      = '01'
    *          months    = '00'
    *          signum    = '-'
    *          years     = '00'
    *        IMPORTING
    *          calc_date = lv_date.
    *      CALL FUNCTION 'BAPI_AP_ACC_GETKEYDATEBALANCE'
    *        EXPORTING
    *          companycode        = gs_items-bukrs
    *          vendor             = gs_items-konto
    *          keydate            = lv_date
    **   BALANCESPGLI       = ' '
    **   NOTEDITEMS         = ' '
    ** IMPORTING
    **   RETURN             =
    *        TABLES
    *          keybalance         =  lv_obal.
    *      CALL FUNCTION 'BAPI_AP_ACC_GETKEYDATEBALANCE'
    *        EXPORTING
    *          companycode        = gs_items-bukrs
    *          vendor             = gs_items-konto
    *          keydate            = gs_items-budat
    **   BALANCESPGLI       = ' '
    **   NOTEDITEMS         = ' '
    ** IMPORTING
    **   RETURN             =
    *        TABLES
    *          keybalance         = lv_cbal
    *      READ TABLE lv_cbal INTO gs_cbal INDEX 1.
    *      gs_items-cbal = gs_cbal-lc_bal.
    *      READ TABLE lv_obal INTO gs_obal INDEX 1.
    *      gs_items-obal = gs_obal-lc_bal.
    *      MODIFY ct_items FROM gs_items TRANSPORTING obal cbal.
    *      CLEAR: gs_items,gs_obal,gs_cbal.
    *    ENDLOOP.
    *   ENDIF.
    So, Above Code Causing Me the Performance Issue, Kindly Suggest Me the Solution..
    Regards,
    uday.

    Hi Uday,
    I am sending you the code i used for the creation a Zreport based on FBL5N. Please check if it can of any help.
    *& Report  ZFBL5N                                                      *
    REPORT  zfbl5n_new  .
    TABLES : bsid,knc1,lfc1.
    TYPE-POOLS: slis.
    TYPES: BEGIN OF ty_bsid,
              bukrs TYPE bsid-bukrs,
              kunnr TYPE bsid-kunnr,
              belnr TYPE bsid-belnr,
              buzei TYPE bsid-buzei,
              bldat TYPE bsid-bldat,
              blart TYPE bsid-blart,
              bschl TYPE bsid-bschl,
              shkzg TYPE bsid-shkzg,
              dmbtr TYPE bsid-dmbtr,
              augdt TYPE bsid-augdt,
              augbl TYPE bsid-augbl,
              zuonr TYPE bsid-zuonr,
              sgtxt TYPE bsid-sgtxt,
              zfbdt TYPE bsid-zfbdt,
              zterm TYPE bsid-zterm,
              zbd1t TYPE bsid-zbd1t,
              zbd2t TYPE bsid-zbd2t,
              zbd3t TYPE bsid-zbd3t,
              kkber TYPE bsid-kkber,
              bstat TYPE bsid-bstat,
              umskz TYPE bsid-umskz,
            END OF ty_bsid.
    TYPES: BEGIN OF ty_bsik,
             bukrs TYPE bsik-bukrs,
              lifnr TYPE bsik-lifnr,
              belnr TYPE bsik-belnr,
              buzei TYPE bsik-buzei,
              bldat TYPE bsik-bldat,
              blart TYPE bsik-blart,
              bschl TYPE bsik-bschl,
              shkzg TYPE bsik-shkzg,
              dmbtr TYPE bsik-dmbtr,
              augdt TYPE bsik-augdt,
              augbl TYPE bsik-augbl,
              zuonr TYPE bsik-zuonr,
              sgtxt TYPE bsik-sgtxt,
               zfbdt TYPE bsik-zfbdt,
    *         KKBER TYPE bsik-kkber,
              zterm TYPE bsik-zterm,
               zbd1t TYPE bsik-zbd1t,
              zbd2t TYPE bsik-zbd2t,
              zbd3t TYPE bsik-zbd3t,
              bstat TYPE bsid-bstat,
              umskz TYPE bsid-umskz,
            END OF ty_bsik.
    TYPES: BEGIN OF ty_final,
              belnr TYPE bsid-belnr,
    *         buzei TYPE bsak-buzei,
              bldat TYPE bsid-bldat,
              blart TYPE bsid-blart,
              chq TYPE bsid-zuonr,
              debit TYPE bsid-dmbtr,
              credit TYPE bsid-dmbtr,
              txt TYPE bsid-sgtxt,
              date TYPE bsid-zfbdt,
              kkber TYPE bsid-kkber,
              zterm TYPE bsid-zterm,
              augbl TYPE bsid-augbl,
              augdt TYPE bsid-augdt,
              flag TYPE c,
            END OF ty_final.
    TYPES : BEGIN OF gs_openbal,
              bukrs TYPE bapi3007_2-comp_code,
              kunnr TYPE bapi3007_2-customer,
              dmbtr TYPE bapi3007_2-lc_amount,
             END OF gs_openbal.
    DATA: it_bsid TYPE STANDARD TABLE OF ty_bsid,
           it_bsik TYPE STANDARD TABLE OF ty_bsik,
           it_final TYPE STANDARD TABLE OF ty_final.
    DATA: wa_bsid TYPE ty_bsid,
           wa_bsik TYPE ty_bsik,
           wa_final TYPE ty_final.
    DATA: w_days TYPE t5a4a-dlydy,
           w_month TYPE t5a4a-dlymo,
           w_year TYPE t5a4a-dlyyr,
           w_date TYPE p0001-begda,
           w_name1 TYPE kna1-name1,
           w_ort01 TYPE kna1-ort01,
           w_lifnr TYPE kna1-lifnr,
           w_dmbtr1 TYPE bsid-dmbtr,
           w_dmbtr2 TYPE bsid-dmbtr,
           w_dmbtr3 TYPE bsad-dmbtr,
           w_dmbtr4 TYPE bsad-dmbtr,
           w_opbal TYPE bsid-dmbtr,
           w_credit TYPE bsik-dmbtr,
           w_debit TYPE bsik-dmbtr,
           w_clobal TYPE bsik-dmbtr,
           w_credit1 TYPE bsik-dmbtr,
           w_debit1 TYPE bsik-dmbtr,
           w_clobal1 TYPE bsik-dmbtr.
    DATA: ld_yrper LIKE rwcoom-fiscper,
           kunnr LIKE kna1-kunnr,
           x_norm TYPE c,
           x_park,
           x_apar,
           x_merk,
           ok_code(4),
           wa_x001 LIKE x001,
           return LIKE bapireturn,
           line_count LIKE sy-loopc,
           number_of_records TYPE i,
           xindex LIKE sy-tabix,
           open LIKE knc1-um01s,
           temp(20),
           close LIKE knc1-um01s,
           gjahr LIKE bsid-gjahr,
           period LIKE bkpf-monat,
           f(1),
           v_char(2),
           closec(20),
           openc(20),
           debit LIKE bapi3007_2-lc_amount,
           credit LIKE debit.
    DATA : v_dmbtr LIKE bsid-dmbtr.
    *DATA : tot_debit LIKE t_ar-debit,
    *       tot_credit LIKE t_ar-credit.
    DATA : t_kna1 LIKE kna1 OCCURS 1  WITH HEADER LINE,
            t_knb1 LIKE knb1 OCCURS 10 WITH HEADER LINE.
    DATA ibsid LIKE bsid OCCURS 0 WITH HEADER LINE.
    DATA ibsad LIKE bsad OCCURS 0 WITH HEADER LINE.
    DATA ibsik LIKE bsik OCCURS 0 WITH HEADER LINE.
    DATA ibsak LIKE bsak OCCURS 0 WITH HEADER LINE.
    DATA : it_fieldcat_alv   TYPE slis_t_fieldcat_alv,
            wa_fieldcat_alv     TYPE slis_fieldcat_alv,
            is_layout_alv  TYPE slis_layout_alv,
            wa_layout_alv  TYPE slis_layout_alv,
            it_list_top_of_page TYPE slis_t_listheader,
            it_events TYPE slis_t_event,
            wa_events TYPE LINE OF slis_t_event.
    DATA : BEGIN OF ibukrs OCCURS 0,
               bukrs LIKE t001-bukrs,
              END OF ibukrs.
    DATA : BEGIN OF ikunnr1 OCCURS 0,
              kunnr LIKE knc1-kunnr,
             END OF ikunnr1.
    DATA : BEGIN OF ikunnr OCCURS 0,
               kunnr LIKE knc1-kunnr,
               bukrs LIKE t001-bukrs,
               lifnr LIKE lfc1-lifnr,
              END OF ikunnr.
    DATA: it_sort TYPE slis_t_sortinfo_alv,
           wa_sort TYPE slis_sortinfo_alv.
    DATA:    r_bschl TYPE RANGE OF bschl,
              wa_bschl LIKE LINE OF r_bschl.
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
    PARAMETERS : p_kunnr TYPE bsid-kunnr OBLIGATORY,
                  p_bukrs TYPE bsid-bukrs OBLIGATORY.
    SELECT-OPTIONS: so_budat FOR bsid-budat .
    SELECTION-SCREEN END OF BLOCK b1.
    SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
    PARAMETERS : p_normal AS CHECKBOX,
                  p_spl    AS CHECKBOX,
                  p_vendor AS CHECKBOX.
    SELECTION-SCREEN END OF BLOCK b2.
    PERFORM get_data.
    PERFORM process_data.
    *PERFORM calculate_openbal. " Commented by anish
    PERFORM calculate_open_bal.
    PERFORM calculate_closing_bal.
    PERFORM build_catalog_sort USING it_sort.
    PERFORM reuse_alv_events_get .
    PERFORM display_data.
    *&      Form  GET_DATA
    *       text
    *  -->  p1        text
    *  <--  p2        text
    FORM get_data .
       SELECT bukrs kunnr belnr buzei bldat blart bschl shkzg dmbtr augdt augbl zuonr sgtxt zfbdt zterm zbd1t zbd2t zbd3t kkber
         bstat umskz FROM bsid
         INTO TABLE it_bsid
         WHERE bukrs = p_bukrs
          AND kunnr = p_kunnr
          AND budat IN so_budat.
       SELECT bukrs kunnr belnr buzei bldat blart bschl shkzg dmbtr augdt augbl zuonr sgtxt zfbdt zterm zbd1t zbd2t zbd3t kkber
        bstat umskz FROM bsad
        APPENDING TABLE it_bsid
        WHERE bukrs = p_bukrs
         AND kunnr = p_kunnr
         AND budat IN so_budat.
       SELECT SINGLE name1 ort01 lifnr FROM kna1
         INTO (w_name1 , w_ort01 , w_lifnr)
         WHERE kunnr = p_kunnr.
       IF p_vendor IS NOT INITIAL.
         SELECT bukrs lifnr belnr buzei bldat blart bschl shkzg dmbtr augdt augbl zuonr sgtxt zfbdt zterm zbd1t zbd2t zbd3t
         bstat umskz   FROM bsik
         APPENDING TABLE it_bsik
         WHERE bukrs = p_bukrs
           AND lifnr = w_lifnr
           AND budat IN so_budat.
         SELECT bukrs lifnr belnr buzei bldat blart bschl shkzg dmbtr augdt augbl zuonr sgtxt zfbdt zterm zbd1t zbd2t zbd3t
         bstat umskz  FROM bsak
        APPENDING TABLE it_bsik
        WHERE bukrs = p_bukrs
          AND lifnr = w_lifnr
          AND budat IN so_budat.
       ENDIF.
       SORT it_bsid BY bschl.
       DELETE  it_bsid WHERE bschl = '04'.
       DELETE  it_bsid WHERE bschl = '07'.
       DELETE  it_bsid WHERE bschl = '17'.
       DELETE  it_bsid WHERE bschl = '34'.
       DELETE  it_bsid WHERE bschl = '27'.
       DELETE  it_bsid WHERE bschl = '37'.
       SORT it_bsik BY bschl.
       DELETE  it_bsik WHERE bschl = '04'.
       DELETE  it_bsik WHERE bschl = '07'.
       DELETE  it_bsik WHERE bschl = '17'.
       DELETE  it_bsik WHERE bschl = '34'.
       DELETE  it_bsik WHERE bschl = '27'.
       DELETE  it_bsik WHERE bschl = '37'.
    ENDFORM.                    " GET_DATA
    *&      Form  PROCESS_DATA
    *       text
    *  -->  p1        text
    *  <--  p2        text
    FORM process_data .
       DATA:okay       TYPE c VALUE space.
       w_month = '00'.
       w_year = '00'.
       SORT it_bsid BY bldat .
       LOOP AT it_bsid INTO wa_bsid.
         PERFORM check_item_ok  USING p_normal
                                      p_spl
                                      p_vendor
    *                               x_park
                                      wa_bsid
                                CHANGING okay.
         CHECK okay = 'X'.
         wa_final-belnr = wa_bsid-belnr.
         wa_final-bldat = wa_bsid-bldat.
         wa_final-blart = wa_bsid-blart.
         wa_final-txt = wa_bsid-sgtxt.
         wa_final-kkber = wa_bsid-kkber.
         wa_final-zterm = wa_bsid-zterm.
         wa_final-augbl = wa_bsid-augbl.
         wa_final-augdt = wa_bsid-augdt.
         wa_final-flag = 'C'.
         IF wa_bsid-blart = 'DZ'.
           wa_final-chq = wa_bsid-zuonr.
         ENDIF.
         IF wa_bsid-shkzg = 'S'.
           wa_final-debit = wa_bsid-dmbtr.
         ELSEIF wa_bsid-shkzg = 'H'.
           wa_final-credit = wa_bsid-dmbtr.
         ENDIF.
         w_credit = w_credit + wa_final-credit.
         w_debit = w_debit + wa_final-debit.
    ****** Net due  date
         IF wa_bsid-zbd1t IS NOT INITIAL.
           w_days = wa_bsid-zbd1t.
         ELSEIF wa_bsid-zbd2t IS NOT INITIAL.
           w_days = wa_bsid-zbd2t.
         ELSEIF wa_bsid-zbd3t IS NOT INITIAL.
           w_days = wa_bsid-zbd3t.
         ENDIF.
         IF w_days IS INITIAL.
           wa_final-date = wa_bsid-zfbdt.
         ELSE.
           CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
             EXPORTING
               date      = wa_bsid-zfbdt
               days      = w_days
               months    = w_month
               signum    = '+'
               years     = w_year
             IMPORTING
               calc_date = w_date.
           wa_final-date = w_date.
         ENDIF.
         APPEND wa_final TO it_final.
         CLEAR: w_days , w_date , wa_final .
       ENDLOOP.
       IF it_bsik IS NOT INITIAL.
         CLEAR: w_days , w_date.
         SORT it_bsik BY bldat.
         LOOP AT it_bsik INTO wa_bsik.
           wa_final-belnr = wa_bsik-belnr.
           wa_final-bldat = wa_bsik-bldat.
           wa_final-blart = wa_bsik-blart.
           wa_final-txt = wa_bsik-sgtxt.
    *    wa_final-kkber = wa_bsik-kkber.
           wa_final-zterm = wa_bsik-zterm.
           wa_final-augbl = wa_bsik-augbl.
           wa_final-augdt = wa_bsik-augdt.
           wa_final-flag = 'V'.
           IF wa_bsik-blart = 'DZ'.
             wa_final-chq = wa_bsik-zuonr.
           ENDIF.
           IF wa_bsik-shkzg = 'S'.
             wa_final-debit = wa_bsik-dmbtr.
           ELSEIF wa_bsik-shkzg = 'H'.
             wa_final-credit = wa_bsik-dmbtr.
           ENDIF.
           w_credit1 = w_credit1 + wa_final-credit.
           w_debit1 = w_debit1 + wa_final-debit.
    *******  Net Due date
           IF wa_bsik-zbd1t IS NOT INITIAL.
             w_days = wa_bsik-zbd1t.
           ELSEIF wa_bsik-zbd2t IS NOT INITIAL.
             w_days = wa_bsik-zbd2t.
           ELSEIF wa_bsik-zbd3t IS NOT INITIAL.
             w_days = wa_bsik-zbd3t.
           ENDIF.
           IF w_days IS INITIAL.
             wa_final-date = wa_bsik-zfbdt.
           ELSE.
             CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
               EXPORTING
                 date      = wa_bsik-zfbdt
                 days      = w_days
                 months    = w_month
                 signum    = '+'
                 years     = w_year
               IMPORTING
                 calc_date = w_date.
           ENDIF.
           wa_final-date = w_date.
           APPEND wa_final TO it_final.
           CLEAR: wa_final.
         ENDLOOP.
       ENDIF.
    ENDFORM.                    " PROCESS_DATA
    *&      Form  DISPLAY_DATA
    *       text
    *  -->  p1        text
    *  <--  p2        text
    FORM display_data .
       wa_fieldcat_alv-fieldname = 'BELNR'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-003.
       wa_fieldcat_alv-outputlen = '11'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'BLDAT'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-004.
       wa_fieldcat_alv-outputlen = '13'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'BLART'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-005.
       wa_fieldcat_alv-outputlen = '02'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'CHQ'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-006.
       wa_fieldcat_alv-outputlen = '09'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'DEBIT'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-007.
       wa_fieldcat_alv-outputlen = '15'.
       wa_fieldcat_alv-do_sum = 'X'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'CREDIT'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-008.
       wa_fieldcat_alv-outputlen = '15'.
       wa_fieldcat_alv-do_sum = 'X'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'TXT'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-009.
       wa_fieldcat_alv-outputlen = '50'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'DATE'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-010.
       wa_fieldcat_alv-outputlen = '12'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'KKBER'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-011.
       wa_fieldcat_alv-outputlen = '04'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'ZTERM'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-012.
       wa_fieldcat_alv-outputlen = '13'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'AUGBL'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-013.
       wa_fieldcat_alv-outputlen = '15'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'AUGDT'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-seltext_l = text-014.
       wa_fieldcat_alv-outputlen = '17'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       wa_fieldcat_alv-fieldname = 'FLAG'.
       wa_fieldcat_alv-tabname = 'IT_FINAL'.
       wa_fieldcat_alv-tech = 'X'.
       APPEND wa_fieldcat_alv TO it_fieldcat_alv.
       CLEAR wa_fieldcat_alv.
       CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
        EXPORTING
          i_callback_program             = sy-repid
          is_layout                      = wa_layout_alv
          it_fieldcat                    = it_fieldcat_alv
    *   IT_EXCLUDING                   =
    *   IT_SPECIAL_GROUPS              =
          it_sort                        = it_sort
          it_events                      = it_events
          i_save                            = 'A'
         TABLES
           t_outtab                       = it_final
        EXCEPTIONS
          program_error                  = 1
          OTHERS                         = 2
       IF sy-subrc <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
       ENDIF.
    *  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    *    EXPORTING
    *      i_callback_program                = sy-repid
    *     i_callback_top_of_page            = 'TOP_OF_PAGE'
    *      is_layout                         = wa_layout_alv
    *      it_fieldcat                       = it_fieldcat_alv
    *      it_sort                           = it_sort
    ***   I_DEFAULT                         = 'X'
    **      i_save                            = 'A'
    ***   IT_EVENTS                         =
    *     TABLES
    *       t_outtab                          = it_final
    *    EXCEPTIONS
    *      program_error                     = 1
    *      OTHERS                            = 2
    *  IF sy-subrc <> 0.
    *** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    ***         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    *  ENDIF.
    ENDFORM.                    " DISPLAY_DATA
    *&      Form  TOP_OF_PAGE
    *       Header at top of page.
    FORM top_of_page.
       SKIP 1.
       WRITE: AT 35 'Account Statement from' , so_budat-low , 'to' , so_budat-high.
       SKIP 2.
       WRITE: AT /5 'CUSTOMER:' , p_kunnr.
       WRITE: AT 35 'Name:' , w_name1.
       WRITE: AT /5 'Company:' , p_bukrs.
       WRITE: AT 35 'City:' , w_ort01.
       SKIP 1.
       WRITE: AT /5 'Opening Balance as on' , so_budat-low , '   ' ,  w_opbal LEFT-JUSTIFIED.
       SKIP 2.
    ENDFORM.                    "TOP_OF_PAGE
    *&      Form  END_OF_PAGE
    *       Footer at End of page.
    FORM end_of_page.
       SKIP 2.
       IF so_budat-high IS NOT INITIAL.
         WRITE: AT 5 'Closing Balance as on' , so_budat-high , '   ' ,  w_clobal LEFT-JUSTIFIED.
       ELSE.
         WRITE: AT 5 'Closing Balance  ' , w_clobal LEFT-JUSTIFIED.
       ENDIF.
    ENDFORM.                    "end_of_page
    *&      Form  CALCULATE_OPENBAL
    *       text
    *  -->  p1        text
    *  <--  p2        text
    FORM calculate_openbal .
       DATA:v_gjahr       TYPE bsid-gjahr.
       DATA: v_period LIKE  t009b-poper,v_monat LIKE t001-periv.
       CALL FUNCTION 'FI_PERIOD_DETERMINE'
              EXPORTING
                   i_budat        = so_budat-low
                   i_bukrs        = p_bukrs
    *           I_PERIV        = ' '
    *           I_GJAHR        = 0000
    *           I_MONAT        = 00
    *           X_XMO16        = ' '
              IMPORTING
                   e_gjahr        = v_gjahr
    *            e_monat        = v_monat
                   e_poper        = v_period.
       IF sy-subrc NE 0.
       ENDIF.
       DATA: f_date LIKE sy-datum.
       CALL FUNCTION 'FIRST_DAY_IN_PERIOD_GET'
         EXPORTING
           i_gjahr  = v_gjahr
           i_monmit = 00
           i_periv  = 'V3'
           i_poper  = v_period
         IMPORTING
           e_date   = f_date.
       period = v_period - 1.
       gjahr = v_gjahr.
       DATA wa_kna1 LIKE kna1.
       CALL FUNCTION 'READ_KNA1'
         EXPORTING
           xkunnr         = p_kunnr
         IMPORTING
           xkna1          = wa_kna1
         EXCEPTIONS
           key_incomplete = 1
           not_authorized = 2
           not_found      = 3
           OTHERS         = 4.
       IF sy-subrc <> 0.
         MESSAGE w023(zwww).
         CALL SCREEN 0010.
       ENDIF.
       MOVE-CORRESPONDING wa_kna1 TO t_kna1.
       APPEND t_kna1.
       SELECT kunnr FROM kna1 INTO TABLE ikunnr1
         WHERE kunnr = p_kunnr.
       SELECT bukrs FROM t001 INTO TABLE ibukrs
        FOR ALL ENTRIES IN t_knb1
        WHERE bukrs = t_knb1-bukrs.
       LOOP AT ikunnr1.
         LOOP AT ibukrs.
           ikunnr-kunnr = ikunnr1-kunnr.
           ikunnr-bukrs = ibukrs-bukrs.
           READ TABLE t_kna1 WITH  KEY kunnr = ikunnr1-kunnr.
           ikunnr-lifnr = t_kna1-lifnr.
           APPEND ikunnr.
         ENDLOOP.
       ENDLOOP.
       DELETE ikunnr WHERE bukrs NE p_bukrs.
       LOOP AT ikunnr.
         CLEAR: knc1,lfc1,f.
         IF NOT ( ikunnr-kunnr IS INITIAL ) AND NOT ( p_vendor IS INITIAL ).
           SELECT SINGLE * FROM lfc1
                  WHERE gjahr = gjahr AND bukrs = ikunnr-bukrs
                                      AND lifnr = ikunnr-lifnr.
         ENDIF.
         SELECT SINGLE * FROM knc1
           WHERE gjahr = gjahr AND bukrs = p_bukrs
                 AND kunnr = p_kunnr.
         IF sy-subrc = 0.
           CASE period .
             WHEN 12.
               open = knc1-umsav +
               knc1-um01s - knc1-um01h + knc1-um02s - knc1-um02h +
               knc1-um03s - knc1-um03h + knc1-um04s - knc1-um04h +
               knc1-um05s - knc1-um05h + knc1-um06s - knc1-um06h +
               knc1-um07s - knc1-um07h + knc1-um08s - knc1-um08h +
               knc1-um09s - knc1-um09h + knc1-um10s - knc1-um10h +
               knc1-um11s - knc1-um11h + knc1-um12s - knc1-um12h.
               IF NOT ( lfc1 IS INITIAL ).
                 open = open + lfc1-umsav +
                 lfc1-um01s - lfc1-um01h + lfc1-um02s - lfc1-um02h +
                 lfc1-um03s - lfc1-um03h + lfc1-um04s - lfc1-um04h +
                 lfc1-um05s - lfc1-um05h + lfc1-um06s - lfc1-um06h +
                 lfc1-um07s - lfc1-um07h + lfc1-um08s - lfc1-um08h +
                 lfc1-um09s - lfc1-um09h + lfc1-um10s - lfc1-um10h +
                 lfc1-um11s - lfc1-um11h + lfc1-um12s - lfc1-um12h.
               ENDIF.
             WHEN 11.
               open = knc1-umsav +
               knc1-um01s - knc1-um01h + knc1-um02s - knc1-um02h +
               knc1-um03s - knc1-um03h + knc1-um04s - knc1-um04h +
               knc1-um05s - knc1-um05h + knc1-um06s - knc1-um06h +
               knc1-um07s - knc1-um07h + knc1-um08s - knc1-um08h +
               knc1-um09s - knc1-um09h + knc1-um10s - knc1-um10h +
               knc1-um11s - knc1-um11h.
               IF NOT ( lfc1 IS INITIAL ) .
                 open = open + lfc1-umsav +
                 lfc1-um01s - lfc1-um01h + lfc1-um02s - lfc1-um02h +
                 lfc1-um03s - lfc1-um03h + lfc1-um04s - lfc1-um04h +
                 lfc1-um05s - lfc1-um05h + lfc1-um06s - lfc1-um06h +
                 lfc1-um07s - lfc1-um07h + lfc1-um08s - lfc1-um08h +
                 lfc1-um09s - lfc1-um09h + lfc1-um10s - lfc1-um10h +
                 lfc1-um11s - lfc1-um11h.
               ENDIF.
             WHEN 10.
               open = knc1-umsav +
               knc1-um01s - knc1-um01h + knc1-um02s - knc1-um02h +
               knc1-um03s - knc1-um03h + knc1-um04s - knc1-um04h +
               knc1-um05s - knc1-um05h + knc1-um06s - knc1-um06h +
               knc1-um07s - knc1-um07h + knc1-um08s - knc1-um08h +
               knc1-um09s - knc1-um09h + knc1-um10s - knc1-um10h .
               IF NOT ( lfc1 IS INITIAL ) .
                 open = open + lfc1-umsav +
                 lfc1-um01s - lfc1-um01h + lfc1-um02s - lfc1-um02h +
                 lfc1-um03s - lfc1-um03h + lfc1-um04s - lfc1-um04h +
                 lfc1-um05s - lfc1-um05h + lfc1-um06s - lfc1-um06h +
                 lfc1-um07s - lfc1-um07h + lfc1-um08s - lfc1-um08h +
                 lfc1-um09s - lfc1-um09h + lfc1-um10s - lfc1-um10h.
               ENDIF.
             WHEN 9.
               open = knc1-umsav +
               knc1-um01s - knc1-um01h + knc1-um02s - knc1-um02h +
               knc1-um03s - knc1-um03h + knc1-um04s - knc1-um04h +
               knc1-um05s - knc1-um05h + knc1-um06s - knc1-um06h +
               knc1-um07s - knc1-um07h + knc1-um08s - knc1-um08h +
               knc1-um09s - knc1-um09h .
               IF NOT ( lfc1 IS INITIAL ) .
                 open = open + lfc1-umsav +
                 lfc1-um01s - lfc1-um01h + lfc1-um02s - lfc1-um02h +
                 lfc1-um03s - lfc1-um03h + lfc1-um04s - lfc1-um04h +
                 lfc1-um05s - lfc1-um05h + lfc1-um06s - lfc1-um06h +
                 lfc1-um07s - lfc1-um07h + lfc1-um08s - lfc1-um08h +
                 lfc1-um09s - lfc1-um09h.
               ENDIF.
             WHEN 8.
               open = knc1-umsav +
               knc1-um01s - knc1-um01h + knc1-um02s - knc1-um02h +
               knc1-um03s - knc1-um03h + knc1-um04s - knc1-um04h +
               knc1-um05s - knc1-um05h + knc1-um06s - knc1-um06h +
               knc1-um07s - knc1-um07h + knc1-um08s - knc1-um08h.
               IF NOT ( lfc1 IS INITIAL ) .
                 open = open + lfc1-umsav +
                 lfc1-um01s - lfc1-um01h + lfc1-um02s - lfc1-um02h +
                 lfc1-um03s - lfc1-um03h + lfc1-um04s - lfc1-um04h +
                 lfc1-um05s - lfc1-um05h + lfc1-um06s - lfc1-um06h +
                 lfc1-um07s - lfc1-um07h + lfc1-um08s - lfc1-um08h .
               ENDIF.
             WHEN 7.
               open = knc1-umsav +
               knc1-um01s - knc1-um01h + knc1-um02s - knc1-um02h +
               knc1-um03s - knc1-um03h + knc1-um04s - knc1-um04h +
               knc1-um05s - knc1-um05h + knc1-um06s - knc1-um06h +
               knc1-um07s - knc1-um07h .
               IF NOT ( lfc1 IS INITIAL ) .
                 open = open + lfc1-umsav +
                 lfc1-um01s - lfc1-um01h + lfc1-um02s - lfc1-um02h +
                 lfc1-um03s - lfc1-um03h + lfc1-um04s - lfc1-um04h +
                 lfc1-um05s - lfc1-um

  • Reg:Opening and Closing Stock For Plant

    Hi All,
      i want to calculate the opening stock and closing stock for plant wise for the given Posting Period Date.
    Am having the opening balance and closing balance for all months but i need it as month wise.
    considering this example,
    the date is from 15.01.2010 to 15.03.2010 and for one plant
    considering this example,
    am having the opening stock from as 15.01.2010  to 15.03.2010 as 10,000 and the closing stock as 15,000.
    but i need in month wise like below,
    from 15.01.2010 to 31.01.2010 what is the opening stock and closing stock?
    from 01.02.2010 to 28.02.2010 what is the opening stock and closing stock?
    from 01.03.2010 to 15.03.2010 what is the opening stock and closing stock?
    this is purely based on plant wise and not material wise?
    could anybody say clearly how can i acheive this scenario?
    i have referred MB5b,mc.9 transaction programs but still confused how to go with plant wise for a particular period?
    Thanks & Regards,
    Suresh
    Edited by: suresh suresh on Mar 24, 2010 10:15 AM

    Hi,
    Refer to link below:
    http://help.sap.com/erp2005_ehp_04/helpdata/EN/c1/3766e7449a11d188fe0000e8322f96/frameset.htm
    Regards,
    Venkat.

  • Opening and closing balance values at storage location level

    Dear all,
    I want to know opening and closing balance values and receipts and issue values at storage location level for certain period.
    I tried with MB5B, it is not giving value at storage location level, it is giving quantities at storage location level and values at plant level. But requirement is: please see below example:
    All values are in USD          
    Storage location:      1001     1002
    Opening Balance:      2000     3000
    Receipts:                           1000      2000
    Issues:                            2000     3000
    Closing Balance:      1000     2000
    Please help in this regard (I want value of the stock only, not qty)
    Regads,
    Veerappa
    Edited by: Chinna Veerappa on Jul 27, 2010 5:27 PM
    Edited by: Chinna Veerappa on Jul 27, 2010 5:28 PM

    vlauation is at plant level (MBEW table) the storage location level does not carry values (table MARD).
    You may have to develope your own ZMB5B  program and calculate yourself what is not stored in SAP.

  • Intersperse daily totals with opening and closing balance

    Hi All
    Suppose we have a data set (simplified from my query but will suffice)
      select 1 as daynum, 'CR' as trantype, 200 as amount from dual union all
      select 1 as daynum, 'DR' as trantype, -300 as amount from dual union all
      select 2 as daynum, 'CR' as trantype, 800 as amount from dual union all
      select 2 as daynum, 'DR' as trantype, -200 as amount from dual union all
      select 3 as daynum, 'CR' as trantype, 900 as amount from dual union all
      select 3 as daynum, 'DR' as trantype, -500 as amount from dual
    )How would you go about getting a result set that looks like:
    1, Opening Balance, 0
    1, CR, 200
    1, DR, -300
    1, Closing Balance, -100
    2, Opening Balance, -100
    2, CR, 800
    2, DR, -200
    2, Closing Balance, 500
    3, Opening Balance, 500
    3, CR, 900
    3, DR, -500
    3, Closing Balance, 900
    4, Opening Balance, 900
    4, Closing Balance, 900
    I'm thinking:
    generate an arbitrary number of rows for the number of days I want to report over (in this case, 4)
    cross join these with 2 row set of constants "opening" and "closing" balanace
    -> I have now an opening and closing balance row for every day
    union it with my data so that "opening/CLosing balance" is a type too (and nulls in the amount column)
    case when the type is like '%Balance' then sum(amount) over(order by day, orderkind rows unbounded preceding) else amount
    (orderkind is a constant 0 for opening, 1 for all the datarows, 2 for closing)
    Is it right?
    Is there a better way?
    Edited by: charred on Apr 1, 2010 7:03 AM

    Unless I'm missing something, Tom's answer won't work for more than 4 days?
    with t as
        select 1 as daynum, 'CR' as trantype, 200 as amount from dual union all
        select 1 as daynum, 'DR' as trantype, -300 as amount from dual union all
        select 2 as daynum, 'CR' as trantype, 800 as amount from dual union all
        select 2 as daynum, 'DR' as trantype, -200 as amount from dual union all
        select 3 as daynum, 'CR' as trantype, 900 as amount from dual union all
        select 3 as daynum, 'DR' as trantype, -500 as amount from dual union all
        select 4 as daynum, 'CR' as trantype, 900 as amount from dual union all
        select 4 as daynum, 'DR' as trantype, -500 as amount from dual union all
        select 5 as daynum, 'CR' as trantype, 900 as amount from dual union all
        select 5 as daynum, 'DR' as trantype, -500 as amount from dual
      data
      as
      ( select daynum, r
          from (select level daynum from dual connect by level <= 4),
                   (select level r from dual connect by level <= 2 )
      select daynum,
             r,
             ttype,
             amt,
             case when ttype in ( 'close', 'open' )
                  then sum(samt) over (order by daynum, r)
              end theamount
        from (
      select data.daynum,
             data.r,
             case when (nvl(t.trantype,'CR') = 'CR' and data.r = 1 )
                  then 'open'
                  when (nvl(t.trantype,'DR') = 'DR' and data.r = 2)
                  then 'close'
                  else t.trantype
             end ttype,
             case when (t.trantype = 'CR' and r = 2) or (t.trantype = 'DR' and r=1)
                  then t.amount
              end amt,
             case when (t.trantype = 'DR' and r = 2)
                  then sum(case when r=1 then t.amount end) over (partition by data.daynum)
                  else 0
              end samt
      from   t, data
      where data.daynum (+) = t.daynum
      order by daynum, r, decode( ttype, 'open', 1, 'CR', 2, 'DR', 3, 'close', 4 );
        DAYNUM          R TTYPE        AMT  THEAMOUNT
             1          1 open                      0
             1          1 DR          -300          
             1          2 CR           200          
             1          2 close                  -100
             2          1 open                   -100
             2          1 DR          -200          
             2          2 CR           800          
             2          2 close                   500
             3          1 open                    500
             3          1 DR          -500          
             3          2 CR           900          
             3          2 close                   900
             4          1 open                    900
             4          1 DR          -500          
             4          2 CR           900          
             4          2 close                  1300
                          CR                        
                          DR  Here's a solution using partitioned outer join:
    with my_tab as (select 1 as daynum, 'CR' as trantype, 200 as amount from dual union all
                    select 1 as daynum, 'DR' as trantype, -300 as amount from dual union all
                    select 2 as daynum, 'CR' as trantype, 800 as amount from dual union all
                    select 2 as daynum, 'DR' as trantype, -200 as amount from dual union all
                    select 3 as daynum, 'CR' as trantype, 900 as amount from dual union all
                    select 3 as daynum, 'DR' as trantype, -500 as amount from dual union all
                    select 4 as daynum, 'CR' as trantype, 900 as amount from dual union all
                    select 4 as daynum, 'DR' as trantype, -500 as amount from dual union all
                    select 5 as daynum, 'CR' as trantype, 900 as amount from dual union all
                    select 5 as daynum, 'DR' as trantype, -500 as amount from dual),
          dummy as (select 1 id, 'Opening Balance' descr from dual union all
                    select 2 id, 'CR' descr from dual union all
                    select 3 id, 'DR' descr from dual union all
                    select 4 id, 'Closing Balance' descr from dual)
    select mt.daynum,
           d.descr,
           decode(d.id, 1, 0,
                        2, amount,
                        3, amount,
                        4, amount + lag(amount, 2) over (partition by mt.daynum order by d.id)) amount
    from   my_tab mt
           left outer join dummy d partition by (id, descr) on (mt.trantype = d.descr)
    where  (d.id = 1 and mt.trantype = 'CR')
    or     (d.id = 2 and mt.trantype = 'CR')
    or     (d.id = 3 and mt.trantype = 'DR')
    or     (d.id = 4 and mt.trantype = 'DR')
    order by mt.daynum, d.id;
        DAYNUM DESCR               AMOUNT
             1 Opening Balance          0
             1 CR                     200
             1 DR                    -300
             1 Closing Balance       -100
             2 Opening Balance          0
             2 CR                     800
             2 DR                    -200
             2 Closing Balance        600
             3 Opening Balance          0
             3 CR                     900
             3 DR                    -500
             3 Closing Balance        400
             4 Opening Balance          0
             4 CR                     900
             4 DR                    -500
             4 Closing Balance        400
             5 Opening Balance          0
             5 CR                     900
             5 DR                    -500
             5 Closing Balance        400ETA: And, if there can be any number of CR or DR rows:
    with my_tab as (select 1 as daynum, 'CR' as trantype, 200 as amount from dual union all
                    select 1 as daynum, 'DR' as trantype, -300 as amount from dual union all
                    select 1 as daynum, 'CR' as trantype, 400 as amount from dual union all
                    select 1 as daynum, 'DR' as trantype, -500 as amount from dual union all
                    select 2 as daynum, 'CR' as trantype, 800 as amount from dual union all
                    select 2 as daynum, 'DR' as trantype, -200 as amount from dual union all
                    select 3 as daynum, 'CR' as trantype, 900 as amount from dual union all
                    select 3 as daynum, 'DR' as trantype, -500 as amount from dual union all
                    select 4 as daynum, 'CR' as trantype, 900 as amount from dual union all
                    select 5 as daynum, 'CR' as trantype, 900 as amount from dual union all
                    select 5 as daynum, 'CR' as trantype, 200 as amount from dual union all
                    select 5 as daynum, 'DR' as trantype, -500 as amount from dual union all
                    select 6 as daynum, 'DR' as trantype, -200 as amount from dual),
          dummy as (select 1 id, 'Opening Balance' descr from dual union all
                    select 2 id, 'CR' descr from dual union all
                    select 3 id, 'DR' descr from dual union all
                    select 4 id, 'Closing Balance' descr from dual)
    select mt.daynum,
           d.descr,
           mt.trantype,
           decode(d.id, 1, 0,
                        2, amount,
                        3, amount,
                        4, sum(decode(d.id, 1, 0, 4, 0, amount)) over (partition by mt.daynum)) amount
    from   (select mt1.*, row_number() over (partition by mt1.daynum order by mt1.trantype) rn
            from   my_tab mt1) mt
           left outer join dummy d partition by (id, descr) on (mt.trantype = d.descr)
    where  case when d.id = 1 and rn = 1 then 1
                when d.id = 2 and mt.trantype = 'CR' then 1
                when d.id = 3 and mt.trantype = 'DR' then 1
                when d.id = 4 and rn = 1 then 1
                else 0
           end = 1
    order by mt.daynum, d.id;
        DAYNUM DESCR           TRANTYPE     AMOUNT
             1 Opening Balance CR                0
             1 CR              CR              400
             1 CR              CR              200
             1 DR              DR             -300
             1 DR              DR             -500
             1 Closing Balance CR             -200
             2 Opening Balance CR                0
             2 CR              CR              800
             2 DR              DR             -200
             2 Closing Balance CR              600
             3 Opening Balance CR                0
             3 CR              CR              900
             3 DR              DR             -500
             3 Closing Balance CR              400
             4 Opening Balance CR                0
             4 CR              CR              900
             4 Closing Balance CR              900
             5 Opening Balance CR                0
             5 CR              CR              900
             5 CR              CR              200
             5 DR              DR             -500
             5 Closing Balance CR              600
             6 Opening Balance DR                0
             6 DR              DR             -200
             6 Closing Balance DR             -200Edited by: Boneist on 16-Apr-2010 17:10

  • Open and closing balances std report

    Dear All,
    please suggest me open and closing balances std report and can i suggest my user below t.code for viewing open and closing balnces for India scenarion T.code:J3RFLVMOBVED
    Regards
    Gopal.S

    hi
    kindly chk MB5B it will fullfill your requirement
    regards
    praveen.k

Maybe you are looking for