How does the profit get calculated?

The current profit margin for product prices is determined by the difference between the retail price and the last purchase price.  The last purchase price can be found in the Edit Product screen. It is automatically updated if you book a Stock Order or Stock Receipt.

Profit margins are calculated using the actual sales price, which includes item discounts and the average purchase price at the time of sale for the warehouse from which the item was sold.  If, at the time of sale, you also have an additional cost set up in the product settings, this cost will be added to the purchase price.

You can review the average purchase price under Inventory > Warehouses. If you need to correct the average purchase price, you can do that by performing an inventory count under Inventory > Inventories.