# Annual Leave Entitlement Proration Rounding Up to multiple of 0.5 in SF Time Off scenario

** Business Scenario**: Recently I was implementing time off solution for my client. While implementing Annual Leave Entitlement for my clients, I have encountered a little complex scenario that I would like to share with you today. Also, discuss on some specific business rule functionality.

Annual Leave entitlement is pretty straight forward to configure and well-articulated in the implementation guide. My focus today is to explain a typical Proration logic for Termination or Hire Annual Leave Proration Rule.

Annual Leave Entitlement to be retrieved (from a lookup table) based on years with company and Position. Proration logic is to round up the prorated leave balance to multiple of 0.5 for Hire and Termination Case.

Example Scenario:

For Hire Scenario, if Yearly Entitlement is 18 days leave, below proration logic to be implemented:

A calculated prorated leave entitlement of 12.75 to 13.24 to be rounded to 13

A calculated prorated leave entitlement of 13.25 to 13.74 to be rounded to 13.5

A calculated prorated leave entitlement of 13.75 to 14.24 to be rounded to 14

__Solution____:__

Here is the rule with simple rounding logic and optimized database hitting:

__Explanation:__

Trick I did is to perform the proration entitlement before actual rule execution, assigning it to a variable. Then apply the round rule on that variable.

** Note 1:** A variable used in any rule, can’t be deleted to modified, if that is used in actual rule par. So, to update a variable, one needs to remove it from the rule body part fiest.

** Note 2:** If a variable declared in any rule, but not used in the rule body, the variable will not get called at the run time

So, further frilling down on the var_calculatedamount assignment:

- Calculate the number of days an terminated employee will be eligible for leave calculation on the last day of work

**Get Number Of Calendar Days()**

** Start Date: Accrual Rule Parameters.Accruable Start Date**

** End Date: Accrual Rule Parameters.Accruable End Date**

- Get the number of days in the year calculation is happening. This function is used to take into account the extra day on Leap year

**Get Number Of Days For Year Of Date()**

** Date: Accrual Rule Parameters.Accruable End Date**

** **

**Divide**Days obtained from point 1 with Days obtained from point 2, to get the fraction**Fetch**the yearly leave day’s entitlement from “Your Lookup Table”, based on employee’s years of experience and Position Title (Consultant, Lead Consultant, Principal Consultant etc.)**Multiply**Yearly entitlement obtained from Point 4 with Fraction from Point 3 to get prorated entitlement for the year**Round**off the prorated data obtained from point 5 to 2 decimal places (Precision 2) and with threshold (0.5)

Now, we get the prorated value till 2 decimal. Example if the yearly entitlement is of 18 days for the whole year, prorated entitlement may be like 12.25, 13.01 or 12.06.

Now we shall use simple Modulo function to achieve desired rounded value. That is prorated leave balance in the range 12.75 to 13.24 to be rounded to 13 and so on.

Modulo function on a decimal number returns only the decimal part of it with divisor is 1.

For example MODULO ( Number = 12.35 Divisor = 1 ), will return value 0.35.

Then simply compare the decimal part against the rage and post the desired leave like below:

**If Modulo ( Number = 12.35 Division = 1 ) less than 0.25**

**Then Post Leave ( Number = 12.35 – Module ( Number = 12.35 ) ) = 12.35 – 0.35 = 12**

**Else if Modulo ( Number = 12.35 Division = 1 ) Greater than 0.24 and Less Than 0.75**

**Then Post Leave ( Number = 12.35 – Module ( Number = 12.35 ) + .5 ) = 12.35 – 0.35 + .5 = 12.5**

**Else Post Leave ( Number = 12.35 – Module ( Number = 12.35 ) + 1 ) = 12.35 – 0.35 + .5 = 13**

In the above example final prorated rounded value will be 12.5

__Thank You!__

Thank you for reading this blog. Hope you find this interesting. Request you to please share your comment and / or queries.

You can ask your query in community forum mentioned in the link below as well.