# The Initial Stock Calculation for MB5B at Plant/Storage Location Level

After the new plant has been created and the initial stock has been set at the plant/storage location level, the user finds that the initial stock calculation for MB5B at the Storage location level is not correct as they expected.

As per the current understanding, the new plant should have zero initial stock on 01-01-0000 (without any specific selection date being input at the selection screen of MB5B ) as it belongs to a new company code/plant and no stock ahead of the first 561 movements happened at this Match 06.

There’re totally 4 quantities in the result header part:

• <Qty1> Stock  on 01-01-0000 (Begin Stock)
• <Qty2> Total Receipts
• <Qty3> Total Issues
• <Qty4> Stock on 12-31-9999 (End Stock)

### 1. Notes related to the wrong beginning quantity for MB5B

There’re lots of notes mentioned about the wrong beginning quantity for MB5B:

• 3005740 – MB5B: Wrong beginning quantity because of ML period closing documents
• 3295513 – Wrong opening values for stock value/quantity at MB5B
• 2963312 – MB5B: Wrong calculation of beginning stock value/quantity

But most of them are talking about the option “Valuated Stock” instead of “Storage location/batch stock”. And for this case, the opening stock is correct for “Valuated Stock” even without above mentioned notes.

Maybe the new patch already covers those old notes, but still need to provide how MB5B calculates the current opening stock. And the branch logic to deal with Valuated Stock” or “Storage location/batch stock” lies at includes program RM07MLBD_FORM_01.

The final formula to calculate the opening /beginning quantity is:

Begin stock = Ending stock – Total Receipts + Total Issues.

In short, it is Qty 1 = Qty 4 + Qty3 – Qty2.

### 2.  How to calculate Ending Stock <Qty 4: Stock on 12-31-9999>

The ending stock or the newest current stock is stored at the internal table BESTAND. There are various sources depending on the user inputs. For this specific case, it’s collected during the loop of IMARD.

Here we can see the ending quantity is field <bestandendmenge> which is calculated by:

``````        bestand-endmenge = imard-labst + imard-insme + imard-speme
+ imard-einme + imard-retme - weg_mat-menge.``````

Table IMARD is fetched from MARD directly according to plant, storage location, and material.

### 3.  How to calculate Total Receipts& Total Issues <Qty 2& Qty 3>

Qty 2 is BESTAND-SOLL and Qty 3 is BESTAND-HABEN which is calculated inside the internal table sum_mat. When has SUM_MAT been filled?

Then the question is how the internal table: G_T_MSEG_LEAN has been filled? The answer is RM07MLBD_FORM_02.

1. Get material document entry by material number, plant, and Posting Date from the MSEG table.
2. The sum of quantity (MSEG-MENGE) group by material/plant and Debit/Credit Indicator (MSEG-SHKZG), against Qty2 and Qty 3 (Total Receipts/ Total Issues) respectively.

### Final. Calculate the begin stock <Qty 1: Stock  on 01-01-0000>

Begin stock = Ending stock – Total Receipts + Total Issues. <Qty 1 = Qty 4 + Qty3 – Qty2>

So from the report point of view, seems like the program calculates the beginning stock correctly based on current table entries (MARD+MSEG) for this specific case. Could be a data issue at the transaction level. No idea why report S_P00_07000139  can provide the correct result 😛

As MB5B has very complex calculation logic at different levels for various scenarios, here just record the checking procedure just FYI and in case need to check the same in the future. Hope it could help if someone faces the same issue.

### Assigned Tags

You must be Logged on to comment or reply to a post.

That is a very thorough analysis. Have you brought that up to SAP support? It should be fixed in standard with a correction note. Your analysis is a perfect test case for OSS incident.

Jigang Zhang 张吉刚
Blog Post Author

Thanks for the comments. Haven't raised a ticket yet, will update you if any.

Jigang Zhang 张吉刚
Blog Post Author

Dominik Tylczynski