Example of Date Calculation According to Business Calendar
This article shows an example of calculating a date and a period between dates according to the business calendar.
You can read about scripts for working with the business calendar in this article.
The business calendar defines the weekends and holidays and the working hours. To learn more, read this Help section.
In contracts and other documents, the terms are usually specified in business days, for example, " ...within 14 business days after payment."
To automatically control these terms, use calculation according to the business calendar.
You can also use the business calendar to automatically determine the task execution time. You cannot simply take away the start date from the end date, because in this case the days off, lunch break and non-working hours are not taken into account.
Calculating the “within 14 business days” period
Consider a situation when a customer must pay for a service within 14 working days after signing the contract. A simplified business process map would look like this:
The system waits for 14 days for the customer to make the payment.
The script for calculating the due date is recorded in the NextDate variable and looks as follows:
Script with PublicAPI
Namespace:
using EleWise.ELMA.API;
Script text:
context.NextDate = PublicAPI.Services.ProductionCalendar.EvalTargetTime(context.PeriodStart.Value, TimeSpan.FromDays(14));
Script without PublicAPI
Namespaces:
using EleWise.ELMA.Services;
using EleWise.ELMA.Scheduling;
Script text:
var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
context.NextDate = calendar.EvalTargetTime(context.PeriodStart.Value, TimeSpan.FromDays(14));
Determining working time for task execution
Consider the Emergency Call process, where the operator receives a phone call, registers it and hands the order to a technical support specialist. The specialist receives the order in form of a task and starts working. Once the task is complete, the employee closes the task and ELMA automatically calculates the time spent on the task, and registers it in the system.
Here is what the script for calculating the working time spent on the order looks like:
Script with PublicAPI
Namespace:
using EleWise.ELMA.API;
Script text:
public void WorkDays(Context context)
{
//get the task start and due dates
DateTime startTime = context.Task.StartDate.Value;
DateTime endTime = context.Task.EndWorkDate.Value;
//get the business calendar service
//calculate the interval with the EvalWorkTimeDifference() method
context.ExecutionTime= PublicAPI.Services.ProductionCalendar.EvalWorkTimeDifference(startTime, endTime);
}
Script without PublicAPI
Namespaces:
using EleWise.ELMA.Services;
using EleWise.ELMA.Scheduling;
Script text:
public void WorkDays(Context context)
{
//get the start and end date and time
DateTime startTime = context.Task.StartWorkDate.Value;
DateTime endTime = context.Task.EndWorkDate.Value;
//get business calendar service
var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
//calculate the interval with the EvalWorkTimeDifference() method
context.ExecutionTime= calendar.EvalWorkTimeDifference(startTime, endTime);
}
To invoke the Request Execution task, the following script is used. It is executed when the task is created in the process and adds the task to the Task context variable.
Namespace:
using EleWise.ELMA.Tasks.Models;
Script text:
public override void OnTaskCreate(ITaskBase task,
EleWise.ELMA.Model.Entities.ProcessContext.P_EmergencySupport context)
{
//select only the required taks by operation name
if (task.Subject == "Request Execution")
{
//record the task in the context variable
context.Task=(TaskBase)task;
}
}
To learn more about such scripts, read this article.
Scripts with PublicAPI
A script for calculating the previous working day:
private DateTime GetPrevWorkDay(Context context, DateTime selectedDay)
{
return PublicAPI.Services.ProductionCalendar.EvalTargetTime(selectedDay.Date.AddMinutes(1), -1).Date;
}
A script for calculating the next working day:
private DateTime GetNextWorkDay(Context context, DateTime selectedDay)
{
return PublicAPI.Services.ProductionCalendar.EvalTargetTime(selectedDay.Date.AddHours(23).AddMinutes(59), 1).Date;
}
Script without PublicAPI
A script for calculating the previous working day:
1 private DateTime GetPrevWorkDay(Context context, DateTime selectedDay)
2 {
3 var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
4 return calendar.EvalTargetTime(selectedDay.Date.AddMinutes(1), -1).Date;
5 }
A script for calculating the next working day:
1 private DateTime GetNextWorkDay(Context context, DateTime selectedDay)
2 {
3 var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
4 return calendar.EvalTargetTime(selectedDay.Date.AddHours(23).AddMinutes(59), 1).Date;
5 }