Beispiel #1
0
 public void Insert(Order order)
 {
     var date = DateTime.Now;
     var maxnumber =
         DbContext.Orders.Count(
             n => n.UpdateTime.Value.Year == date.Year && n.UpdateTime.Value.Month == date.Month &&
                  n.UpdateTime.Value.Day == date.Day) + 1;
     var code =
         $"SHFL{date.ToString("yyyyMMdd")}{(maxnumber.ToString().Length == 1 ? "0" + maxnumber : maxnumber.ToString())}";
     order.Code = code;
     DbContext.Orders.Add(order);
     Update();
 }
        public object Post(OrderModel model)
        {
            if (model == null || model.OrderDetailModels.Any() == false)
            {
                return Failed("合同不能为空");
            }
            if (model.OrderDetailModels.Any(n => n.Qty == 0))
            {
                return Failed("合同的每个料号都需要数量");
            }
            if (model.OrderDate == DateTime.MinValue)
            {
                return Failed("合同不能没有日期");
            }
            if (model.PayPeriodModel == null)
            {
                return Failed("请选择账期");
            }
            var payPeriod = _payPeriodService.GetPayPeriod(model.PayPeriodModel.Id);
            if (payPeriod == null)
            {
                return Failed("请选择账期");
            }
            var currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            var currentPosition =
                _employeesService.GetEmployee(HttpContext.Current.User.Identity.GetUser().EmployeeId)
                    .EmployeePostions.Where(
                        n => n.StartDate <= currentDate && (n.EndDate == null || n.EndDate >= currentDate) && n.IsDeleted == false)
                    .Select(n => n.Position)
                    .FirstOrDefault();
            if (currentPosition == null)
            {
                return Failed("没有权限");
            }
            if (model.OrderDetailModels.Select(n => n.EnquiryModel.CustomerModel.Id).Distinct().Count() != 1)
            {
                return Failed("一次合同只限一个客户");
            }
            var orderdetails = new List<OrderDetail>();
            foreach (var item in model.OrderDetailModels)
            {
                var enquiry = _enquiryService.GetEnquiry(item.EnquiryModel.Id);
                if (item.UnitPrice == null)
                {
                    return Failed("请填写单价");
                }
                if (item.Qty == null)
                {
                    return Failed("请填写数量");
                }
                if (enquiry == null || enquiry.IsDeleted || enquiry.PositionId != currentPosition.Id)
                {
                    return Failed("禁止提交");
                }
                if (enquiry.Price == null || item.UnitPrice < enquiry.Price ||
                    (Math.Abs(item.UnitPrice.Value) > Math.Abs(enquiry.Price.Value) * (decimal)1.05))
                {
                    return Failed("单价超出范围");
                }
                orderdetails.Add(new OrderDetail
                {
                    Id = Guid.NewGuid(),
                    Qty = item.Qty.Value,
                    EnquiryId = enquiry.Id,
                    TotalPrice = (decimal)(item.Qty * item.UnitPrice.Value),
                    UnitPrice = item.UnitPrice.Value
                });
            }
            var parentRole =
                _auditLevelService.GetAuditLevels()
                    .Where(n => n.RoleId == currentPosition.Role.Id)
                    .Select(n => n.ParentRole).FirstOrDefault();
            var customer = _customerService.GetCustomer(model.CustomerModel.Id);
            if (parentRole == null)
            {
                return Failed("找不到上级审核人");
            }
            if (customer == null)
            {
                return Failed("找不到客户");
            }
            try
            {
                var item = new Order
                {
                    Id = Guid.NewGuid(),
                    Remark = model.Remark,
                    OrderDate = model.OrderDate,
                    PayPeriodId = model.PayPeriodModel.Id,
                    EstimatedDeliveryDate = model.EstimatedDeliveryDate,
                    LastPayDate = model.OrderDate.AddDays(payPeriod.Days),
                    OrderReviews = new Collection<OrderReview>
                    {
                        new OrderReview
                        {
                            Id = Guid.NewGuid(),
                            SendToRoleId = parentRole.Id
                        }
                    },
                    ContractAmount = orderdetails.Sum(p => p.TotalPrice),
                    OrderDetails = orderdetails,
                    PositionId = currentPosition.Id,
                    CustomerId = customer.Id
                };
                _orderService.Insert(item);
                return Success();
            }
            catch (Exception ex)
            {

                return Failed(ex.Message);
            }


        }