You can have a new Stock table (LastQuantity, ProductID, CurrentPrice, Margin) and use triggers on both tables to update that table. That would make FIFO simple.
I worked on FIFO problem a while back and found it to be quite complex. My article on this topic
https://social.technet.microsoft.com/wiki/contents/articles/18711.t-sql-fifo-inventory-problem-cost-of-goods-sold.aspx
More interesting article
https://www.red-gate.com/simple-talk/databases/sql-server/performance-sql-server/t-sql-window-function-speed-phreakery-the-fifo-stock-inventory-problem/