Ejemplo n.º 1
0
        public async Task <IActionResult> EditOffer(OfferViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("offermodel", "Form is not valid!");
                return(View(model));
            }
            try
            {
                ICollection <EstimateConnects> Connect;
                ICollection <Offers>           offers;


                using (var context = new DataBaseContext())
                {
                    offers = context.Offer
                             .Include(offermodel => offermodel.Estimate)
                             .Include(users => users.CreatedBy)
                             .Include(users => users.UpdatedBy)
                             .ToList();

                    Connect = context.EstimateConnects
                              .Include(line => line.EstimateLines)
                              .ToList();
                }

                Offers offer = await OfferRepository.FindAsync(model.Id);

                var   dboffer  = offers.FirstOrDefault(x => x.Id.Equals(model.Id));
                Users user     = UserRepository.FindUserByEmail(User.Identity.Name);
                var   estimate = await EstimateRepository.FindAsync(dboffer.Estimate.Id);

                var connectlines = Connect.Where(x => x.Estimate.Id.Equals(dboffer.Estimate.Id)).ToList();
                if (model.EstimateLines != null)
                {
                    foreach (var item in model.EstimateLines)
                    {
                        if (EstimateLinesRepository.AnyLineExist(item.Id) == true)
                        {
                            var line = await EstimateLinesRepository.FindAsync(item.Id);

                            line.Specification = item.Specification;
                            line.Hours         = item.Hours;
                            line.HourCost      = item.HourCost;
                            line.TotalCost     = item.TotalCost;
                            await EstimateLinesRepository.SaveChangesAsync();
                        }
                        else
                        {
                            var newline = new EstimateLines
                            {
                                Specification = item.Specification,
                                Hours         = item.Hours,
                                HourCost      = item.HourCost,
                                TotalCost     = item.TotalCost
                            };
                            await EstimateLinesRepository.AddAsync(newline);

                            var newconnect = new EstimateConnects
                            {
                                Estimate      = estimate,
                                EstimateLines = newline
                            };
                            await EstimateConnectsRepository.AddAsync(newconnect);
                        }
                    }
                    foreach (var connectline in connectlines)
                    {
                        if (!model.EstimateLines.Any(x => x.Id == connectline.EstimateLines.Id))
                        {
                            await EstimateConnectsRepository.RemoveAsync(connectline.Id);
                        }
                    }
                }

                offer.IndexContent    = model.IndexContent;
                offer.LastUpdatedAt   = DateTime.Now;
                offer.UpdatedBy       = user;
                offer.ProjectName     = model.ProjectName;
                offer.DocumentVersion = offer.DocumentVersion + 1;
                //offer.DebtorNumber = model.DebtorNumber;

                await OfferRepository.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                ICollection <Offers>           offers;
                ICollection <EstimateConnects> Connect;
                using (var context = new DataBaseContext())
                {
                    offers = context.Offer
                             .Include(offermodel => offermodel.Estimate)
                             .Include(user => user.CreatedBy)
                             .Include(user => user.UpdatedBy)
                             .ToList();

                    Connect = context.EstimateConnects
                              .Include(line => line.EstimateLines)
                              .ToList();
                }

                var offer    = offers.FirstOrDefault(x => x.Id.Equals(model.Id));
                var estimate = await EstimateRepository.FindAsync(offer.Estimate.Id.ToString());

                var lines = Connect.Where(x => x.Estimate.Id == estimate.Id).ToList();

                ViewData["estimatelines"] = lines.Select(x => new EstimateConnectViewModel
                {
                    EstimateLines = x.EstimateLines
                }).ToList();
                return(View(model));
            }
        }