Our first step here is introduce the notion of a Tax Code to MSCRM via a new custom entity – this could be a simple additional screen comprising of a simple ‘Name’, ‘Rate’ and ‘Is Default for Organisation?’ fields to form a short entity data dictionary:
Entity Relationship Diagram showing how the new Tax Code Entity may relate to the other system Entities in MSCRM
This then gives several methods for deducing a default tax code for an Invoice or Invoice Line:
(1) The Product’s Default Tax Code for an Invoice Line
(2) The Tax Code assigned for an Invoice based on the Customer Account
(3) The Default Tax Code for the MSCRM Deployment Organisation
This list is in order of typical priority – such that certain Products sold by an Organisation will have different Tax Levels (such as 0% Sales Tax on Services for example), and Accounts working in different countries or different currencies may have different default Tax Codes as a result.
Once the Tax Code entity has been published and a list of Tax Codes defined – our next step is to add a layer of logic to MSCRM that will automatically assign a Tax Code to a new Invoice (based on Customer Account and then the Default), and similarly assign a Tax Code to each Invoice Line (based on Product and then Invoice).
List of Tax Codes in Settings area of MSCRM
This is where we would need a pair of custom Plugins to support this for assigning a Tax Code to an Invoice and Invoice Line accordingly.
Invoice Plugin to assign Tax Code on creation of a new Invoice
The following Plugin code would then assign a Tax Code to a new Invoice when an Invoice is first created, this Plugin is envisaged to only fire on Create as any subsequent changes to an Invoice or the Customer paying the Invoice would be intended to be manually updated by the CRM System User.
NOTE: This code (and all the Plugin examples detailed here) is dependent on a separate ‘PluginMethods’ class for invoking Data Access operations against the MSCRM Web Service – the code for this is posted in the third post for handling Tax Codes.
This Plugin code could then be registered as a Pre-Event Step on the Create Message of the Invoice entity
in the Parent Pipeline.
Invoice Line Plugin to assign Tax Code on create or update of an Invoice Line
The following Plugin code is an idea of how to assign a Tax Code to an Invoice Line when the Invoice Line is created or updated – this Plugin then also gives the Invoice Line a % Tax Rate based on the Tax Code assigned.
This percentage is the first step to extending MSCRM to automatically calculating the amount of tax for the Invoice Line and so giving the Net and Gross figures for each Invoice Line based on the Tax Code assigned to the line based on the Product or Invoice involved.
This Plugin code could then be registered as a Pre-Event Step on both the Create and Update Messages of the Invoice Detail entity in the Parent Pipeline.
Invoice Line Plugin to calculate the amount of Tax on create or update of an Invoice Line
The following Plugin code takes the Tax Code and Rate assigned to an Invoice Line and then calculates the amount of Tax based on the (calculated by MSCRM default business logic) Base Amount of the Invoice Line. This logic must take place on the Post step of the Create or Update of the Invoice Line as the Base Amount or Price per Unit fields will not have been populated by the MSCRM Platform Layer until the Post step – this does leave the Plugin triggering on Update of an event whilst also updating the same event, so we must be careful of accidentally triping an infinite loop here.
MSCRM’s default business logic will then take the updated amount of Tax and include this figure to produce a revised Extended Amount for the Invoice Line – and therefore giving the Invoice Line Net (Base Amount) and Gross (Extended Amount) figures.
This Plugin code could then be registered as a Post-Event Step on both the Create and Update Messages of the Invoice Detail entity in the Parent Pipeline. To avoid the infinite loop risk, we should register the Update Step to only fire on certain field updates:
Registering the Tax Code Calculation Plugin using the MSCRM Plugin Registration Tool on certain fields to avoid creating an infinite loop
This, whilst quite simple in itself, actually builds a relatively detailed process flow for recording a new Invoice Line.