Calculate Leave Encashment based on past gross_pay


is there any way that I can calculate leave encashment based on the gross_pay of selected past months.

If there are salary structures without a base salary, only based on timesheets the leave encashment has to be calculated based on the average daily earnings of a number of selected month.

Can anyone help here?
Thanks and have a great day.