Where/How is Commission Calculated?

On the sales order, how are these fields calculated? I did some searching online and in the forums as well as looked into the doctype and customization, but can’t find out how these fields are filled. (I am trying to make a custom commission calculator similar to price rules where a partner can have rules assigned and the rules apply different commission percentages or fixed amounts based on item, group, etc.)