EchoTex_Payroll/HRM.BO/Common/PayrollGlobalFunctions.cs
2024-10-14 10:01:49 +06:00

531 lines
21 KiB
C#

using Ease.Core.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using static Ease.Core.Utility.Global;
namespace HRM.BO
{
public enum enumfractionCalculatinType
{
Monthly = 1,
Yearly = 2,
ThirtyDaysMonth = 3,
ThirtyDaysMonthActual = 4
}
public static class PayrollGlobalFunctions
{
public static DateTime PayrollFirstDateOfMonth(this DateTime date)
{
return Ease.Core.Utility.Global.DateFunctions.FirstDateOfMonth(date);
}
public static DateTime PayrollLastDateOfMonth(this DateTime date)
{
return Ease.Core.Utility.Global.DateFunctions.LastDateOfMonth(date);
}
public static DateTime FirstDateOfYear(DateTime date)
{
return new DateTime(date.Year, 1, 1);
}
public static DateTime LastDateOfYear(DateTime date)
{
return new DateTime(date.Year, 12, 31);
}
public static double GetFraction(DateTime StartDate, DateTime EndDate)
{
double WorkDays;
double MonDays;
double TotFract;
double nFraction = 1;
TotFract = 0;
// bool isYearly = ConfigurationManager.GetBoolValue("root", "fractionalcalculationyearly", EnumConfigurationType.Logic);
//bool isYearly = ConfigurationManager.GetBoolValue("root", "fractionalcalculation", EnumConfigurationType.Logic);
enumfractionCalculatinType ntype = getFractionateCalType();
WorkDays = DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
switch (ntype)
{
case enumfractionCalculatinType.Monthly:
while (StartDate <= EndDate)
{
WorkDays = DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
MonDays = DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(StartDate),
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
if (StartDate.Month == EndDate.Month &&
EndDate != PayrollGlobalFunctions.PayrollLastDateOfMonth(EndDate))
{
WorkDays = DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(EndDate), EndDate) + 1;
}
TotFract = TotFract + WorkDays / MonDays;
StartDate = PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate).AddDays(1);
}
nFraction = TotFract;
break;
case enumfractionCalculatinType.Yearly:
//0.032876712328767123287671232876712
//0.0328767123
//if (StartDate == GlobalFunctions.FirstDateOfMonth(StartDate) && EndDate == GlobalFunctions.LastDateOfMonth(EndDate))
//{
// WorkDays = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("m", StartDate, EndDate) + 1;
// nFraction = WorkDays;
//}
//else
//{
// WorkDays = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d", StartDate, EndDate) + 1;
// double yearlyFraction = 0.0328767123; // which mean 12/365
// nFraction = yearlyFraction * WorkDays;
//}
nFraction = 0;
if (DateFunctions.DateDiff("m", PayrollGlobalFunctions.PayrollFirstDateOfMonth(StartDate),
PayrollGlobalFunctions.PayrollLastDateOfMonth(EndDate)) > 2)
{
nFraction = DateFunctions.DateDiff("m",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(StartDate.AddMonths(1)),
PayrollGlobalFunctions.PayrollLastDateOfMonth(EndDate.AddMonths(-1)));
nFraction = nFraction + (DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1) * 0.0328767123;
nFraction = nFraction + (DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(EndDate),
EndDate) + 1) * 0.0328767123;
}
else
{
if (StartDate.Month == EndDate.Month && StartDate.Year == EndDate.Year) // if cross two month
{
if (StartDate == PayrollFirstDateOfMonth(StartDate) &&
EndDate == PayrollLastDateOfMonth(EndDate))
nFraction = 1;
else
nFraction = nFraction + (DateFunctions.DateDiff("d", StartDate,
EndDate) + 1) * 0.0328767123;
}
else
{
if (StartDate == PayrollFirstDateOfMonth(StartDate) &&
EndDate == PayrollLastDateOfMonth(EndDate))
nFraction = 1;
else
nFraction = nFraction + (DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1) * 0.0328767123;
if (EndDate == PayrollLastDateOfMonth(EndDate))
nFraction = nFraction + 1;
else
nFraction = nFraction + (DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(EndDate),
EndDate) + 1) * 0.0328767123;
}
}
//while (StartDate <= EndDate)
//{
// if (StartDate.Day != 1 )
// {
// nFraction = nFraction + ((double)(Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d",
// GlobalFunctions.FirstDateOfMonth(EndDate), EndDate) + 1)) * 0.0328767123;
// }
// WorkDays = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d", StartDate,
// GlobalFunctions.LastDateOfMonth(StartDate)) + 1;
// MonDays = Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d",
// GlobalFunctions.FirstDateOfMonth(StartDate),
// GlobalFunctions.LastDateOfMonth(StartDate)) + 1;
// if (StartDate.Month == EndDate.Month && EndDate != GlobalFunctions.LastDateOfMonth(EndDate))
// {
// nFraction = nFraction + ((double) (Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("d",
// GlobalFunctions.FirstDateOfMonth(EndDate), EndDate) + 1)) * 0.0328767123;
// }
// TotFract = TotFract + WorkDays / MonDays;
// StartDate = GlobalFunctions.LastDateOfMonth(StartDate).AddDays(1);
//}
break;
case enumfractionCalculatinType.ThirtyDaysMonth:
WorkDays = DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
while (StartDate <= EndDate)
{
WorkDays = DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
MonDays = DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(StartDate),
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
int tempDaya = DateFunctions.DateDiff("d", PayrollFirstDateOfMonth(StartDate),
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
if (tempDaya == 31) WorkDays = WorkDays - 1;
else if (tempDaya < 30)
if (tempDaya == 28) WorkDays = WorkDays + 2;
else WorkDays = WorkDays + 1;
MonDays = 30;
if (StartDate.Month == EndDate.Month &&
EndDate != PayrollGlobalFunctions.PayrollLastDateOfMonth(EndDate))
{
WorkDays = DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(EndDate), EndDate) + 1;
MonDays = 30;
}
TotFract = TotFract + WorkDays / MonDays;
StartDate = PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate).AddDays(1);
}
nFraction = TotFract;
break;
case enumfractionCalculatinType.ThirtyDaysMonthActual:
WorkDays = DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
while (StartDate <= EndDate)
{
WorkDays = DateFunctions.DateDiff("d", StartDate,
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
MonDays = DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(StartDate),
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
int tempDaya = DateFunctions.DateDiff("d", PayrollFirstDateOfMonth(StartDate),
PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate)) + 1;
MonDays = 30;
if (StartDate.Month == EndDate.Month &&
EndDate != PayrollGlobalFunctions.PayrollLastDateOfMonth(EndDate))
{
WorkDays = DateFunctions.DateDiff("d",
PayrollGlobalFunctions.PayrollFirstDateOfMonth(EndDate), EndDate) + 1;
MonDays = 30;
}
double temp = WorkDays / MonDays;
if (temp > 1) temp = 1;
TotFract = TotFract + temp;
StartDate = PayrollGlobalFunctions.PayrollLastDateOfMonth(StartDate).AddDays(1);
}
nFraction = TotFract;
break;
default:
break;
}
return nFraction;
}
public static DateTime AttendanceMonthStart(DateTime date)
{
if (date.Day > 20)
return new DateTime(date.Year, date.Month, 21);
else if (date.Month == 1)
return new DateTime(date.Year - 1, 12, 21);
else return new DateTime(date.Year, date.AddMonths(-1).Month, 21);
}
public static DateTime AttendanceMonthEnd(DateTime date)
{
if (date.Day < 20)
return new DateTime(date.Year, date.Month, 20);
else if (date.Month == 12)
return new DateTime(date.Year + 1, 1, 20);
else return new DateTime(date.Year, date.AddMonths(1).Month, 20);
}
public static enumfractionCalculatinType getFractionateCalType()
{
enumfractionCalculatinType type = enumfractionCalculatinType.Monthly;
string
caltype = "THIRTYDAYS"; //ConfigurationManager.GetStringValue("root", "monthfraction", EnumConfigurationType.Logic);
if (caltype.ToUpper() == "THIRTYDAYS")
type = enumfractionCalculatinType.ThirtyDaysMonth;
else if (caltype.ToUpper() == "THIRTYDAYSACTUAL")
type = enumfractionCalculatinType.ThirtyDaysMonthActual;
else if (caltype.ToUpper() == "YEARLY")
type = enumfractionCalculatinType.Yearly;
else
type = enumfractionCalculatinType.Monthly;
return type;
}
}
public static class GlobalExtensions
{
public static string GetFriendlyNames(this EnumLeaveStatus leveStatus)
{
string friendlyStatus = string.Empty;
switch (leveStatus)
{
case EnumLeaveStatus.Drafted:
case EnumLeaveStatus.Reverted:
case EnumLeaveStatus.Declined:
friendlyStatus = leveStatus.ToString();
break;
case EnumLeaveStatus.Approved:
case EnumLeaveStatus.Availed:
friendlyStatus = "Approved";
break;
case EnumLeaveStatus.OnApproval:
friendlyStatus = "Pending";
break;
case EnumLeaveStatus.Cancel_Request:
friendlyStatus = "Requested for cancel";
break;
case EnumLeaveStatus.Cancel:
friendlyStatus = "Cancelled";
break;
case EnumLeaveStatus.Cancelled_by_user:
friendlyStatus = leveStatus.ToString().Replace('_', ' ');
break;
default:
break;
}
return friendlyStatus;
}
public static HolidayDayOfWeek ToHolidayDayOfWeek(this DayOfWeek dayOfWeek)
{
HolidayDayOfWeek oHolidayDayOfWeek = HolidayDayOfWeek.None;
switch (dayOfWeek)
{
case DayOfWeek.Sunday:
oHolidayDayOfWeek = HolidayDayOfWeek.Sunday;
break;
case DayOfWeek.Monday:
oHolidayDayOfWeek = HolidayDayOfWeek.Monday;
break;
case DayOfWeek.Tuesday:
oHolidayDayOfWeek = HolidayDayOfWeek.Tuesday;
break;
case DayOfWeek.Wednesday:
oHolidayDayOfWeek = HolidayDayOfWeek.Wednesday;
break;
case DayOfWeek.Thursday:
oHolidayDayOfWeek = HolidayDayOfWeek.Thursday;
break;
case DayOfWeek.Friday:
oHolidayDayOfWeek = HolidayDayOfWeek.Friday;
break;
case DayOfWeek.Saturday:
oHolidayDayOfWeek = HolidayDayOfWeek.Saturday;
break;
default:
break;
}
return oHolidayDayOfWeek;
}
public static string CommaSeparatedIDs<T>(this List<T> objCollections) where T : ObjectTemplate
{
return objCollections.Aggregate(new StringBuilder(),
(acc, x) => acc.AppendFormat("{0},", x.ID),
acc => acc.ToString().Trim(','));
}
public static string CommaSeparatedIDs<T>(this List<T> objCollections, string propertyName) where T : class
{
return objCollections.Aggregate(new StringBuilder(),
(acc, x) => acc.AppendFormat("{0},", x.GetType().GetProperty(propertyName).GetValue(x)),
acc => acc.ToString().Trim(','));
}
public static DateTime RemoveSecondsAndMiliseconds(this DateTime dt)
{
return dt.AddSeconds(-dt.Second).AddMilliseconds(-dt.Millisecond);
}
public static DateTime FirstDateOfMonth(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, 1);
}
public static DateTime LastDateOfMonth(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);
}
public static int TotalDaysInMonth(this DateTime dt)
{
return (int)(dt.LastDateOfMonth().Subtract(dt.FirstDateOfMonth()).TotalDays + 1);
}
public static string ToColorCode(this Color clr)
{
return System.Drawing.ColorTranslator.ToHtml(clr);
}
public static Color ColorCodeToColor(this string hex)
{
try
{
Color clr = System.Drawing.ColorTranslator.FromHtml(hex);
return clr;
}
catch (Exception gEx)
{
if (gEx.InnerException != null && gEx.InnerException is FormatException)
{
throw new Exception(
"ColorCode String could not be converted to color\n\r This may happen due to invalid HTML Color code or Hex code",
gEx.InnerException);
}
throw new Exception("An Error Occurred", gEx);
}
}
public static string BloodGroupToFriendlyName(this EnumBloodGroup eBloodGrp)
{
string str = string.Empty;
switch (eBloodGrp)
{
case EnumBloodGroup.None:
str = "NA";
break;
case EnumBloodGroup.APos:
str = "A+";
break;
case EnumBloodGroup.ANeg:
str = "A-";
break;
case EnumBloodGroup.BPos:
str = "B+";
break;
case EnumBloodGroup.BNeg:
str = "B-";
break;
case EnumBloodGroup.OPos:
str = "O+";
break;
case EnumBloodGroup.ONeg:
str = "O-";
break;
case EnumBloodGroup.ABPos:
str = "AB+";
break;
case EnumBloodGroup.ABNeg:
str = "AB-";
break;
default:
eBloodGrp.ToString();
break;
}
return str;
}
public static string ToFriendlyString(this EnumExceptionType eExceptionType)
{
string str = string.Empty;
switch (eExceptionType)
{
case EnumExceptionType.Error:
str = "Error";
break;
case EnumExceptionType.Warning:
str = "Warning";
break;
case EnumExceptionType.Informational:
str = "Information";
break;
case EnumExceptionType.Validation:
str = "Invalid Info";
break;
default:
break;
}
return str;
}
public static string BanglaMonth(this DateTime dt)
{
string str = string.Empty;
switch (dt.Month)
{
case 1:
return "Rvbyqvwi";
case 2:
return "‡deªyqvix";
case 3:
return "gvP©";
case 4:
return "GwcÖj";
case 5:
return "‡g";
case 6:
return "Ryb";
case 7:
return "RyjvB";
case 8:
return "AvMó";
case 9:
return "‡m‡Þ¤^i";
case 10:
return "A‡±vei";
case 11:
return "b‡f¤^i";
case 12:
return "wW‡m¤^i";
default:
return string.Empty;
}
}
public static bool IsNullOrWhiteSpace(this string rawString)
{
bool isNullOrWhiteSpace = false;
if (string.IsNullOrEmpty(rawString) || rawString.Trim() == string.Empty)
{
isNullOrWhiteSpace = true;
}
return isNullOrWhiteSpace;
}
}
public class CustomException : Exception
{
public EnumExceptionType ExceptionType { get; set; }
public CustomException(EnumExceptionType exceptionType, string message)
: base(message)
{
ExceptionType = exceptionType;
}
public CustomException(EnumExceptionType exceptionType, string message, Exception inner)
: base(message, inner)
{
ExceptionType = exceptionType;
}
}
public class CustomMessage
{
public string Caption { get; set; }
public string Message { get; set; }
}
}