What's confusing folks here is that this sample datainsert into #sample
select '11003291001-----NOB30000','0V003','A00A00',269,285,305,1 union all
select '11003291001-----NOB30000','03003','A00A00',287,30,305,2
is both an order and a description of the available stock. Split them out and the problem should become a lot easier to understand and code up.
Also [aging stock] and [days]: you're indicating you have 305 units at 269 days and 305 at 287 days, but you've actually got 305 units, correct? This is confusing and requires clarification.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden