Customization on TOTAL Form

Hi Guys,

We would like to monitor the gross margin of each sales transaction by gathering data of the last purchase price of the items being sold. We will monitor it by customizing the Totals Form in the Sales Order as shown below:

Example, three units of Item A was purchased for USD2500 each on Oct 1. Two more units of item A was purchased on Oct 2 for Php3000 each.
On Oct.3, we would like to sell 1 unit of ITEM A for USD 5,000. Hence, the customization on the form should be able to show the following:
Cost Value based on last purchase price: 3,000
Margin: 2,000

By default, the WA will also appear as follow:
Cost 2,700
Margin: 2,300

FYI, we are currently using Weighted Average as the item model group for all the products.See below:

I need help, anyone please

Thank you.