Ejemplo n.º 1
0
        /// <summary>
        /// Calculate the total number of leave days as of the requested date.
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="numDays"></param>
        /// <param name="reqDate"></param>
        public static void ApproveLeaveRequest(int eid, decimal numDays, DateTime reqDate)
        {
            var proxy    = new LeaveProxy();
            var employee = proxy.Context.Employees.FirstOrDefault(ep => ep.EmployeeId == eid);
            //ty - this year.
            var ty = reqDate.Year;
            //sy - service year. sy holds only the year part ignoring the month part.
            int sy = reqDate.Subtract(Convert.ToDateTime(employee.HireDate)).Days / 365;
            //fy - the last service year just befor the request year.
            DateTime fy = new DateTime();

            fy = sy > 0 ? Convert.ToDateTime(employee.HireDate).AddYears(sy - 1) : Convert.ToDateTime(employee.HireDate);
            //fyv - the first year remaining leave value
            decimal fyv = 0;

            if (fy == Convert.ToDateTime(employee.HireDate))
            {
                fyv = 0;
            }
            else
            {
                fyv = GetRemaingLeaveAmount(fy, eid);
            }

            var     fullYear = Convert.ToDateTime(employee.HireDate).AddYears(sy);
            decimal rate     = Convert.ToDecimal(GetTotalAnnualLeave(reqDate, eid)) / 12;
            decimal md       = Math.Round(Convert.ToDecimal(reqDate.Subtract(fullYear).Days / (362.25 / 12)));
            var     v        = Math.Round(md * rate) - GetAmountTaken(DateTime.Today, eid);
            decimal tyv      = 0;

            if (v >= 0)
            {
                tyv = v;
            }
            var x = numDays;

            if (fyv > 0 && x > 0)
            {
                if (fyv >= x)
                {
                    var obj = new EmployeePtoTransaction()
                    {
                        ApprovedBy   = "####",
                        ApprovedDate = DateTime.Today,
                        EmployeeId   = eid,
                        Year         = fy.Year,
                        NoDays       = x
                    };
                    var obj2 = proxy.Context.EmployeePtoStatus.FirstOrDefault(ep => ep.EmployeeId == eid);
                    if (obj2 != null)
                    {
                        obj2.PrevCount    = fyv - x;
                        obj2.CurrentCount = tyv;
                    }
                    else
                    {
                        obj2 = new EmployeePtoStatus()
                        {
                            EmployeeId = eid, CurrentCount = tyv, PrevCount = fyv - x
                        };
                        proxy.Context.EmployeePtoStatus.AddObject(obj2);
                    }
                    proxy.Context.EmployeePtoTransactions.AddObject(obj);
                    employee.CurrentLeaveDays = (fyv - x) + tyv;
                    proxy.Save();
                    return;
                }
                else
                {
                    x = x - fyv;
                    var obj = new EmployeePtoTransaction()
                    {
                        ApprovedBy   = "####",
                        ApprovedDate = DateTime.Today,
                        EmployeeId   = eid,
                        Year         = fy.Year,
                        NoDays       = fyv
                    };
                    proxy.Context.EmployeePtoTransactions.AddObject(obj);
                }
            }
            if (tyv > 0 && x > 0)
            {
                if (x >= tyv) // case of Overridden
                {
                    var obj = new EmployeePtoTransaction()
                    {
                        ApprovedBy   = "#####",
                        ApprovedDate = DateTime.Today,
                        EmployeeId   = eid,
                        Year         = ty,
                        NoDays       = tyv
                    };
                    var obj2 = proxy.Context.EmployeePtoStatus.FirstOrDefault(ep => ep.EmployeeId == eid);
                    if (obj2 != null)
                    {
                        obj2.PrevCount    = 0;
                        obj2.CurrentCount = 0;
                    }
                    else
                    {
                        obj2 = new EmployeePtoStatus()
                        {
                            EmployeeId = eid, PrevCount = 0, CurrentCount = 0
                        };
                        proxy.Context.EmployeePtoStatus.AddObject(obj2);
                    }
                    proxy.Context.EmployeePtoTransactions.AddObject(obj);
                    employee.CurrentLeaveDays = 0;
                    proxy.Save();
                    return;
                }
                else
                {
                    var obj = new EmployeePtoTransaction()
                    {
                        ApprovedBy   = "#####",
                        ApprovedDate = DateTime.Today,
                        EmployeeId   = eid,
                        Year         = ty,
                        NoDays       = x
                    };
                    var obj2 = proxy.Context.EmployeePtoStatus.FirstOrDefault(ep => ep.EmployeeId == eid);
                    if (obj2 != null)
                    {
                        obj2.PrevCount    = 0;
                        obj2.CurrentCount = tyv - x;
                    }
                    else
                    {
                        obj2 = new EmployeePtoStatus()
                        {
                            EmployeeId = eid, PrevCount = 0, CurrentCount = tyv - x
                        };
                        proxy.Context.EmployeePtoStatus.AddObject(obj2);
                    }
                    proxy.Context.EmployeePtoTransactions.AddObject(obj);
                    employee.CurrentLeaveDays = tyv - x;
                    proxy.Save();
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add the intial leave amount all employees.
        /// </summary>
        /// <param name="employees"></param>
        public static void AddInitialLeave(List <EmployeeInfo> employees)
        {
            var proxy = new LeaveProxy();

            foreach (var e in employees)
            {
                var      hd   = Convert.ToDateTime(e.HireDate);
                var      eid  = e.EmployeeId;
                int      sy   = DateTime.Today.Subtract(hd).Days / 365;
                var      emp  = proxy.Context.Employees.FirstOrDefault(ep => ep.EmployeeId == eid);
                decimal  fyv  = 0;
                decimal  tv   = 0;
                DateTime fy   = sy > 0 ? Convert.ToDateTime(hd).AddYears(sy - 1) : Convert.ToDateTime(hd);
                decimal  fyav = GetTotalAnnualLeave(fy, eid);
                // if the use enter a null value for the previous amount we will assum the employee hasn't taken any leave in previous year.
                // if the fy ==  hd  the employee hasn't have 1 year service year, so he willn't have transaction in the previous year.
                if (e.PrevValue != null && fy != hd)
                {
                    fyv = fyav - Convert.ToDecimal(e.PrevValue);
                    var obj = new EmployeePtoTransaction()
                    {
                        Year         = fy.Year,
                        EmployeeId   = eid,
                        NoDays       = fyv,
                        ApprovedBy   = "#####",
                        ApprovedDate = DateTime.Today
                    };
                    proxy.Context.EmployeePtoTransactions.AddObject(obj);
                }
                if (e.CurrentValue != null)
                {
                    var     fullYear = hd.AddYears(sy);
                    decimal rate     = Convert.ToDecimal(GetTotalAnnualLeave(DateTime.Today, eid)) / 12;
                    decimal md       = Math.Round(Convert.ToDecimal(DateTime.Today.Subtract(fullYear).Days / (362.25 / 12)));
                    //tyv - total leave value for the this year.
                    decimal tyv = Math.Round(md * rate);
                    tv = tyv - Convert.ToDecimal(e.CurrentValue);
                    if (tv > 0)
                    {
                        var obj = new EmployeePtoTransaction()
                        {
                            Year         = DateTime.Today.Year,
                            EmployeeId   = eid,
                            NoDays       = tv,
                            ApprovedBy   = "#####",
                            ApprovedDate = DateTime.Today
                        };
                        proxy.Context.EmployeePtoTransactions.AddObject(obj);
                    }
                }


                if (e.PrevValue != null && e.CurrentValue != null)
                {
                    emp.CurrentLeaveDays = Math.Round(Convert.ToDecimal(e.PrevValue) + Convert.ToDecimal(e.CurrentValue));
                    var status = proxy.Context.EmployeePtoStatus.FirstOrDefault(es => es.EmployeeId == eid);
                    if (status != null)
                    {
                        status.PrevCount    = e.PrevValue;
                        status.CurrentCount = e.CurrentValue;
                    }
                    else
                    {
                        status = new EmployeePtoStatus()
                        {
                            EmployeeId = eid, PrevCount = e.PrevValue, CurrentCount = e.CurrentValue
                        };
                        proxy.Context.EmployeePtoStatus.AddObject(status);
                    }
                }
            }
            proxy.Save();
        }