Selling to dealer, need to track invoice with end customer

We sometimes sell directly to customers and then give a sales rep a commission, but we sometimes sell to a sales rep at a lower cost, and they then sell to the customer.

How should we handle this? If we were a normal wholesaler, the sales rep would be the customer. But if we set the sales rep as the customer, the invoices won’t show under the end customer, which we need.

Any thoughts on how to handle this?