Microsoft Gold Cloud CRM and Gold Cloud Platform Partner

Imagine that you are looking over your ticketing queue, and along comes a ticket: “ITPerson!  The invoice for Customer is wrong!  The amount they are supposed to pay is way off!  This is critical!”.  So, the first thing you think is lets check out the Invoice in question.  You know that the invoice has a field, Total Amount, that holds the final price for the invoice, but you aren’t sure how it gets there.  After some digging you find that each InvoiceDetail has a field called PricePerUnit and a field called Quantity.  A little more digging reveals that the Invoice.TotalAmount is calculated by adding together all the individual InvoiceDetails amounts, which is calculated by multiplying the InvoiceDetails.PricePerUnit times the InvoiceDetails.Quantity.  So the Invoice.TotalAmount is calculated as follows :

Invoice.TotalAmount = Sum(InvoiceDetails.PricePerUnit * InvoiceDetails.Quantity)

Whew, well that helps, but where does the PricePerUnit originate?  A little more digging and you find that it depends on the PriceList that was selected for the customer and Product.  So there are a couple of ways that the TotalAmount could be off on the Invoice.  The user could set the wrong PriceList, the wrong number of units could be set on the InvoiceDetails, or there could be a plugin somewhere that is changing the price on the product so that it doesn’t match the PriceList.

After reviewing the InvoiceDetails, you find that someone had changed the Quantity to 3 from 1 on one of the invoice detail lines.  That caused the TotalAmount for that Invoice to be way too high.  Easy fix, just change the units back to 1.

Then another ticket pops up.  “ITPerson, We now need to calculate sales tax for all Invoices.  It should be set to 6% of the Total Amount.”  Ok, well good news, there is a field for TotalTax on the Invoice entity.  Bad news is that field is locked.  However, there is a field on the InvoiceDetails for tax that you can manually enter a dollar amount into.  Bad news is this requires the user to calculate tax and manually enter it and you want it to be automatically set to a percent of the InvoiceDetails.Amount field.

There are a couple of options.  You can use a third party tax calculation resource (there are several out there) or you can write a plugin that will do it for you.

If you are going to write your own plugin, you would need to modify one of the entities to put a tax percent field in place, either at the Invoice level or the PriceList level.  If would really be driven by if your PriceLists are specific to tax zones.  If your price lists span tax areas, then it would probably be best to have it set on the Invoice level as this will allow each user to specify exactly what the tax rate is for this invoice.  If you needed further granularity from that, you could add it to the InvoiceDetail entity and then have the plugin trigger off of either the new field being updated or off of the InvoiceDetail.Amount being updated.  Your plugin would then set InvoiceDetails.Tax field to be TaxRate (from wherever you had set it) times InvoiceDetail.Amount.  Once that amount is set on the InvoiceDetails, it should roll up to the Invoice and be included in the TotalTax field on the Invoice.  you could also use this to automatically set a Manual Discount, though price lists are usually a better choice when running any sort of sale.

Hopefully that helps clarify a bit about dynamics invoicing and how prices are set and calculated.