This is a complete 'mishmash' of several different processes.
If a customer orders products from sales organization AAAA belonging to company code AAA, but receives the products from plant B belonging to company code BBB - this is intercompany salesprocess. You have 1 sales order, 1 delivery and 2 billing documents - F2 for end customer and IV for billing between company code BB and AA. There is no physical transfer of products between plant B and any plant in company code AAA.
In case you wish to execute ATP for more than one plant and also have the possibility to suggest substitute products in the case of unavailability of a certain product in specific locations, then you can consider rule-based ATP check functionality in APO.
In case all you need a simple functionality to substitute material A with material B when ordering from different locations - use material determination with a suitable substitution reason as suggested by Prashant Sharma.
In case you need to move products between plants assigned to different company codes - this is intercompany stock transfer. In this case consider production repacking either in the sending or in the receiving location. (Production order with a different production BOM to get as a result material B).
I assume that there is some difference between the two material codes - at least in the labels, otherwise there is no logical business or any other reason to keep them as separate numbers in the system.
As TW Typewriter already pointed out, it is not possible in standard to receive a different material code with stock transport order than the product sent.