I've asked for help but, in the meantime, this seems to work:

J2: =IF(MAX(0,MIN($F2,J$1)-$E2)-SUM($I2:I2)+IF(AND($E2=$F2,MONTH($E2)=MONTH(J$1)),1,0)<0,0,MAX(0,MIN($F2,J$1)-$E2)-SUM($I2:I2)+IF(AND($E2=$F2,MONTH($E2)=MONTH(J$1)),1,0))

copied down and across.

I note that row 27 is wrong but I guess that's because of the DAYS360

Regards, TMS