Exemplo n.º 1
0
        // GET: EstimateCosts/Create
        public async Task <IActionResult> Create(int id)
        {
            DateTime                    Today      = DateTime.UtcNow;
            EstimateController          controller = new EstimateController(_context, _config);
            EstimateCostCreateViewModel model      = new EstimateCostCreateViewModel {
                EstimateId = id,
                Estimate   = controller.GetEstimateById(id),
                Costs      = new List <EstimateCost>(),
                CostItems  = new List <CostItem>()
            };

            model.CostItems = await _context.CostItem
                              .Include(ci => ci.CostCategory)
                              .Include(ci => ci.UnitOfMeasure)
                              .OrderBy(ci => ci.ItemName)
                              .ToListAsync();

            EstimateCost Cost = new EstimateCost {
                EstimateId = id
            };

            model.Costs.Add(Cost);

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(EstimateCostCreateViewModel estimateCosts)
        {
            List <EstimateCost> EstimateCostsInContext = await _context.EstimateCost
                                                         .Where(ec => ec.EstimateId == estimateCosts.EstimateId).ToListAsync();

            List <EstimateCost> CostsEntered     = (estimateCosts.Costs?.Count > 0) ? estimateCosts.Costs : estimateCosts.RejectedEntries;
            List <EstimateCost> RejectecdEntries = new List <EstimateCost>();
            List <EstimateCost> UpdatedRecords   = new List <EstimateCost>();

            foreach (var cost in CostsEntered.ToList())
            {
                cost.EstimateId = estimateCosts.EstimateId;

                EstimateCost ExistingCost = EstimateCostsInContext
                                            .FirstOrDefault(ec => ec.EstimateId == cost.EstimateId && ec.CostItemId == cost.CostItemId);

                if (cost.Quantity <= 0 || cost.CostItemId == 0)
                {
                    CostsEntered.Remove(cost);
                    RejectecdEntries.Add(cost);
                }

                if (ExistingCost != null)
                {
                    ExistingCost.Quantity += cost.Quantity;
                    CostsEntered.Remove(cost);
                    UpdatedRecords.Add(ExistingCost);

                    _context.Update(ExistingCost);
                    await _context.SaveChangesAsync();
                }
            }

            foreach (var estimateCost in CostsEntered)
            {
                estimateCost.EstimateId = estimateCosts.EstimateId;
                _context.Add(estimateCost);
                await _context.SaveChangesAsync();
            }

            if (RejectecdEntries.Count > 0 || UpdatedRecords.Count > 0)
            {
                if (RejectecdEntries.Count > 0)
                {
                    foreach (var item in RejectecdEntries)
                    {
                        item.CostItem    = _context.CostItem.FirstOrDefault(ci => ci.Id == item.CostItemId);
                        item.CostPerUnit = _context.CostPerUnit.FirstOrDefault(cpu => cpu.CostItemId == item.CostItemId && cpu.EndDate == null).Cost;
                    }
                }

                if (UpdatedRecords.Count > 0)
                {
                    foreach (var item in UpdatedRecords)
                    {
                        item.CostItem    = _context.CostItem.FirstOrDefault(ci => ci.Id == item.CostItemId);
                        item.CostPerUnit = _context.CostPerUnit.FirstOrDefault(cpu => cpu.CostItemId == item.CostItemId && cpu.EndDate == null).Cost;
                    }
                }

                EstimateCostCreateViewModel viewModel = new EstimateCostCreateViewModel {
                    EstimateId      = estimateCosts.EstimateId,
                    CostItems       = new List <CostItem>(),
                    RejectedEntries = RejectecdEntries,
                    UpdatedRecords  = UpdatedRecords
                };

                viewModel.CostItems = _context.CostItem.Include(ci => ci.UnitOfMeasure).OrderBy(ci => ci.ItemName).ToList();

                return(View("CreateFinish", viewModel));
            }
            else
            {
                return(RedirectToAction("Details", "Estimate", new { id = estimateCosts.EstimateId }));
            }
        }