Example #1
0
        public OrderAddResponseDTO Add(OrderDTO orderDto, string role)
        {
            var order = new Order
            {
                EmployeeId  = orderDto.EmployeeId,
                IsDelivered = orderDto.IsDelivered,
                PlanId      = orderDto.PlanId,
                Shift       = orderDto.Shift,
                Plan        = _planRepository.GetById(orderDto.PlanId)
            };

            if ((DateTime.Now > order.Plan.EditableFrom && DateTime.Now <= order.Plan.EditableTo.AddHours(23).AddMinutes(59).AddSeconds(59)))
            {
                var orderDb = _orderRepository.GetByDateAndEmployee(order);
                if (orderDb == null)
                {
                    _orderRepository.Add(order);
                    return(new OrderAddResponseDTO {
                        Message = ""
                    });
                }
                else
                {
                    order.Id = orderDb.Id;
                    var differentShift = order.Shift != orderDb.Shift;

                    _orderRepository.Update(order);

                    if (differentShift)
                    {
                        return(new OrderAddResponseDTO {
                            Message = "Променет оброк за истиот датум од друга смена"
                        });
                    }
                    else
                    {
                        return(new OrderAddResponseDTO {
                            Message = ""
                        });
                    }
                }
            }
            else if (role == "HR")
            {
                var orderDb  = _orderRepository.GetByDateAndEmployee(order);
                var employee = _employeeRepository.GetById(order.EmployeeId);

                if (orderDb == null)
                {
                    _orderRepository.Add(order);
                    var emailBody = _emailManager.PrepareAddEmail(employee.Rfid, order.Plan.Date, order.Plan.Meal.Name, employee.Company.Name);
                    _emailManager.SendEmail("Додаден оброк", emailBody, employee.User.Email);
                    _emailManager.SendEmail("Додаден оброк", emailBody, "*****@*****.**");
                }
                else
                {
                    order.Id = orderDb.Id;
                    var oldMeal = orderDb.Plan.Meal.Name;

                    _orderRepository.Update(order);
                    var emailBody = _emailManager.PrepareEditEmail(employee.Rfid, order.Plan.Date, oldMeal, order.Plan.Meal.Name, employee.Company.Name);
                    _emailManager.SendEmail("Променет оброк", emailBody, employee.User.Email);
                    _emailManager.SendEmail("Додаден оброк", emailBody, "*****@*****.**");
                }


                return(new OrderAddResponseDTO {
                    Message = "Променет оброк надвор од периодот за промени"
                });
            }
            else
            {
                throw new Exception("Моментално сте надвор од периодот за избирање на овој оброк");
            }
        }