Beispiel #1
0
 public void Insert(Finance finance)
 {
     DbContext.Finances.Add(finance);
     Update();
 }
        public object Post(FinanceModel model)
        {
            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 == null)
            {
                return Failed("提交失败");
            }
            if (model.Amount == null)
            {
                return Failed("金额不得为空");
            }
            if (model.HasFaPiao)
            {
                if (string.IsNullOrEmpty(model.FaPiaoNumbers))
                {
                    return Failed("发票编号不得为空");
                }
            }
            if (model.PayDate == null)
            {
                return Failed("收款日期不得为空");
            }
            try
            {
                var finance = new Finance
                {
                    Id = Guid.NewGuid(),
                    Amount = model.Amount.Value,
                    HasFaPiao = model.HasFaPiao,
                    FaPiaos = model.HasFaPiao ? new List<FaPiao>() : null,
                    OrderId = model.OrderId,
                    PayDate = model.PayDate.Value,
                    PositionId = currentPosition.Id,
                    Remark = model.Remark
                };

                if (model.HasFaPiao)
                {
                    var fapiaonumbers = model.FaPiaoNumbers.Split(',').ToArray();
                    if (!fapiaonumbers.Any())
                    {
                        return Failed("发票编号不得为空");
                    }
                    if (fapiaonumbers.Any(p => p.Trim().Length != 8))
                    {
                        return Failed("发票编号必须得8位");
                    }
                    if (fapiaonumbers.Distinct().Count() != fapiaonumbers.Length)
                    {
                        return Failed("发票编号不得重复");
                    }
                    if (_faPiaoService.GetFaPiaos().Any(p => fapiaonumbers.Contains(p.Code)))
                    {
                        return Failed("发票编号已存在,不得重复");
                    }
                    foreach (var item in fapiaonumbers)
                    {
                        finance.FaPiaos.Add(new FaPiao
                        {
                            Id = Guid.NewGuid(),
                            Code = item.Trim()
                        });
                    }
                }
                _financeService.Insert(finance);
                return Success();
            }
            catch (Exception ex)
            {

                return Failed(ex.Message);
            }
        }