$saleAffiliateId is not being applied to blank invoice

When I create a blank invoice the Affiliate ID does not get inserted to the blank invoice.
The invoice gets created without any errors, but no Sale Referral Partner is set. I have checked what gets inserted before creating and if I do not have an affiliate_id setup in my session I simply add a 0, but I know there is an affiliate id there.

It was my understanding that if I add the affiliate ContactId when I create the blank invoice then when the Invoice has all items added and then marked as paid the affiliate would get a commission. Have I got this concept correct?

Edit: After some more tests I hardcoded the $saleAffiliateId into the function and still does not get inserted to the invoice.

I cannot find any information about this anywhere. I am honestly starting to doubt so much of the documentation. It’s really tiring trying to figurte out anything. It takes days to find out the true way to do anything. Is this by design?