How to subtract

I have two sheets for warehouse inventory, in first one subtotals of materials with total quantity and the second the same materials with batchs ie in first sheet the material in one row and in the second in many columns
If i have deficit or increase i want it automatically take the difference from the first batch and if run out take from the next etc warehouse inv.xlsx

The exact operations that you are trying to perform are obviously difficult to describe in words alone. It would be a good idea if you gave us an example, a sample spreadsheet (not a screen capture) showing the values in cells and the calculations that you want to have done.

Uploaded
I need in real stock in sheet2 take the difference from sheet1 and put the values of each batch after calculating the difference by FEFO

Just to be clear, can you fill the expected results for the “real stock” in sheet 2, and upload again.
Thanks.

Hi
Have a look at the attached sheet, see if it helps, You will need to create a pivot table each time you need to use it exactly as mine and then copy the columns (GtoN) to do the calculations. I change 2 columns in your sheet which I coloured blue again you will need to re-create both each time used.
Regards. HowToSubtract_20190403.ods