Ejemplo n.º 1
0
        public dynamic PutPermission(PermissionPVM p)
        {
            var Permission = db.HR_Leave_Order.Find(p.orderId);


            ////check before update if this order active and it's time pass
            //if (Permission.IS_Active==true && Permission.Order_Date < DateTime.Now)
            //{
            //    return new { result = " غير مسموح بتعديل فى اذن تم" };
            //}

            //check if this permission taked action on it
            if (Permission.OrderStatusId != (int)OrderStatus.Order)
            {
                return(new { result = " غير مسموح بتعديل فى اذن تم اخذ القرار فيه " });
            }

            //select this permission type
            var permissionType = db.PermissionTypes.Where(m => m.PermissionType_ID == p.leaveTypeId).FirstOrDefault();

            //select the previous permission for this permission type taked by this employee in this year
            var takedPermissions = db.HR_Leave_Order.
                                   Where(b => b.LeaveType_ID == p.leaveTypeId && b.Employee_ID == p.empId && b.Year == p.year && b.OrderStatusId == (int)OrderStatus.AcceptedByHr).ToList();


            //order hours
            int hours = p.toHour - p.fromHour;
            //order minutes
            int minutes = p.toMinute - p.fromMinute;
            //total order time in hours
            float totalTime = hours + (minutes / 60);



            //check if this employee have hours to take in this permission type or not
            if (totalTime +
                (takedPermissions.Sum(v => v.Leave_Hours))
                - Permission.Leave_Hours
                > (permissionType.Hours_Count))
            {
                return(new { result = " تخطى عدد الساعات المسموح" });
            }


            //check if this employee finished the permitted hours in this month or not
            if (permissionType.Max_Times != 0)
            {
                var totalTakedHours = (takedPermissions.Where(m => m.Month == p.month).Sum(v => v.Leave_Hours)) + hours;

                //if not change month
                if (p.month == Permission.Month)
                {
                    totalTakedHours = totalTakedHours - Permission.Leave_Hours;
                }
                //if  change month
                else
                {
                    totalTakedHours = totalTakedHours;
                }

                if (totalTakedHours > permissionType.Max_Times)
                {
                    return new { result = "تخطى عدد الساعات المسموح بها للاذن " + permissionType.PermissionType_Name + " خلال الشهر" + p.month }
                }
                ;
            }



            Permission.Order_Date        = p.orderDate;
            Permission.LeaveType_ID      = p.leaveTypeId;
            Permission.Employee_ID       = p.empId;
            Permission.Leave_Hours       = totalTime;
            Permission.From_Hour         = p.fromHour;
            Permission.From_Minute       = p.fromMinute;
            Permission.To_Hour           = p.toHour;
            Permission.To_Minute         = p.toMinute;
            Permission.Month             = p.month;
            Permission.Year              = p.year;
            Permission.OrderStatusId     = Convert.ToInt32(OrderStatus.Order);
            Permission.Permission_Causes = p.permissionCause;
            Permission.DayOrNight        = p.dayOrNight;
            Permission.User_ID           = p.userId;
            Permission.Last_Update       = DateTime.Now;



            var result = db.SaveChanges() > 0 ? true : false;

            return(new
            {
                result = result
            });
        }
Ejemplo n.º 2
0
        public dynamic PostPermission(PermissionPVM p)
        {
            //select this permission type
            var permissionType = db.PermissionTypes.Where(m => m.PermissionType_ID == p.leaveTypeId).FirstOrDefault();
            //select the previous permission for this permission type taked by this employee in this year
            var takedPermissions = db.HR_Leave_Order.
                                   Where(b => b.LeaveType_ID == p.leaveTypeId && b.Employee_ID == p.empId && b.Year == p.year && b.OrderStatusId == (int)OrderStatus.AcceptedByHr).ToList();

            //order hours
            int hours = p.toHour - p.fromHour;
            //order minutes
            int minutes = p.toMinute - p.fromMinute;
            //total order time in hours
            float totalTime = hours + (minutes / 60);


            //check if this employee have hours to take in this permission type or not
            if (totalTime + (takedPermissions.Sum(v => v.Leave_Hours)) > (permissionType.Hours_Count))
            {
                return(new { result = " تخطى عدد الساعات المسموح" });
            }


            //check if this employee finished the permitted hours in this month or not
            if (permissionType.Max_Times != 0)
            {
                if (((takedPermissions.Where(m => m.Month == p.month).Sum(v => v.Leave_Hours)) + hours) > permissionType.Max_Times)
                {
                    return new { result = "تخطى عدد الساعات المسموح بها للاذن " + permissionType.PermissionType_Name + " خلال الشهر" + p.month }
                }
                ;
            }



            var hrLeaveOrder = db.HR_Leave_Order.Add(new HR_Leave_Order
            {
                Order_Date   = p.orderDate,
                LeaveType_ID = p.leaveTypeId,
                Employee_ID  = p.empId,
                Leave_Hours  = totalTime,
                //Is_Agreed=false,
                //AccpetedBy_ID=acceptedById,
                From_Hour         = p.fromHour,
                From_Minute       = p.fromMinute,
                To_Hour           = p.toHour,
                To_Minute         = p.toMinute,
                Month             = p.month,
                Year              = p.year,
                Permission_Causes = p.permissionCause,
                DayOrNight        = p.dayOrNight,
                OrderStatusId     = Convert.ToInt32(OrderStatus.Order),
                //Approv_Date=approveDate,
                //IS_Active=false,
                User_ID     = p.userId,
                Last_Update = DateTime.Now
            });
            var result = db.SaveChanges() > 0 ? true : false;

            return(new
            {
                result = result,
                hrLeaveOrderId = hrLeaveOrder.Order_ID
            });
        }