public ActionResult CreateRequestForm(ProductGapDetailViewModel model)
        {
            List <CompetitorGapViewModel> CompetitorAccounts = model.Competitors.Where(c => c.Accounts.Any(a => a.Request)).ToList();

            int productID = model.ProductId;
            //Diccionario donde la clave es el competidor y el string son las cuentas separadas por punto y coma
            Dictionary <int, string> selectedCompetitors = CompetitorAccounts
                                                           .Select(c => new
            {
                c.ProductId,
                accounts = c.Accounts
                           .Where(a => a.Request)
                           .Select(a => a.Account.Id.ToString())
                           .Aggregate((cur, acc) => cur + ";" + acc)
            })
                                                           .ToDictionary(c => c.ProductId, c => c.accounts);

            List <PriceRequestViewModel> ListPR = commonRepository.GetPriceRequest(GetClienteSeleccionado(), productID, selectedCompetitors)
                                                  .Select(p => new PriceRequestViewModel()
            {
                Account         = p.Account,
                AccountId       = p.AccountId,
                Competitor      = new ProductPR(p.Competitor),
                Product         = new ProductPR(p.Product),
                IdealGap        = p.IdealGap,
                NetAsp          = 0,
                NetAspCondition = 0,
                PriceGap        = p.PriceGap
            }).ToList();
            PriceRequestFormViewModel modelOut = new PriceRequestFormViewModel()
            {
                Editable      = true,
                GUID          = "",
                PriceRequests = ListPR,
                WaitAprrobe   = false
            };

            return(View("PriceRequestForm", modelOut));
        }
        public ActionResult Index()
        {
            //REHACER TODA ESTA MIERDA!!!
            int clienteId = GetClienteSeleccionado();
            List <ProductGapDetailViewModel> _model = new List <ProductGapDetailViewModel>();
            GapModel gm = commonRepository.GetGap(clienteId);

            if (gm.Detail.Count == 0)
            {
                return(View("_emptyPriceGap"));
            }


            foreach (GapModelDetail det in gm.Detail)
            {
                if (_model.Any(p => p.ProductId == det.ProductId))
                {
                    var oldProd_ix = _model.FindIndex(p => p.ProductId == det.ProductId);
                    _model[oldProd_ix].Inventory += det.Inventory;
                    _model[oldProd_ix].SellIn    += det.SellIn;

                    // EL valor mas NEGATIVO es el PEOR
                    if (_model[oldProd_ix].OrderMath > det.OrderMath)
                    {
                        _model[oldProd_ix].PriceGap = det.PriceGap;
                        _model[oldProd_ix].Color    = det.Color;
                    }

                    if (_model[oldProd_ix].Competitors.Any(c => c.ProductId == det.CompetitorId))
                    {
                        var oldComp_ix = _model[oldProd_ix].Competitors.FindIndex(c => c.ProductId == det.CompetitorId);

                        //Para mantener la integridad deberia comparar si existe la cuenta y lanzar un error
                        _model[oldProd_ix].Competitors[oldComp_ix].Accounts.Add(new CompetitorAccountViewModel()
                        {
                            Account = new AccountViewModel()
                            {
                                Id    = det.AccountId,
                                Name  = gm.Accounts.FirstOrDefault(a => a.Id == det.AccountId).Name,
                                Image = gm.Accounts.FirstOrDefault(a => a.Id == det.AccountId).Image
                            },
                            AccountGap          = det.PriceGap,
                            Color               = det.Color,
                            Inventory           = det.Inventory,
                            SellIn              = det.SellIn,
                            SelfPrice           = det.SelfPrice,
                            CompetitorPrice     = det.CompetitorPrice,
                            Request             = det.Request,
                            SelfProductId       = det.ProductId,
                            CompetitorProductId = det.CompetitorId
                        });
                    }
                    else//no existe el competidor
                    {
                        CompetitorAccountViewModel _newAccount = new CompetitorAccountViewModel()
                        {
                            Account = new AccountViewModel()
                            {
                                Id    = det.AccountId,
                                Name  = gm.Accounts.FirstOrDefault(a => a.Id == det.AccountId).Name,
                                Image = gm.Accounts.FirstOrDefault(a => a.Id == det.AccountId).Image
                            },
                            AccountGap          = det.PriceGap,
                            Color               = det.Color,
                            Inventory           = det.Inventory,
                            SellIn              = det.SellIn,
                            SelfPrice           = det.SelfPrice,
                            CompetitorPrice     = det.CompetitorPrice,
                            Request             = det.Request,
                            SelfProductId       = det.ProductId,
                            CompetitorProductId = det.CompetitorId
                        };

                        CompetitorGapViewModel _newCompetitor = new CompetitorGapViewModel()
                        {
                            ProductId   = det.CompetitorId,
                            ProductName = gm.Products.FirstOrDefault(p => p.Id == det.CompetitorId).Name
                        };
                        _newCompetitor.Accounts.Add(_newAccount);

                        _model[oldProd_ix].Competitors.Add(_newCompetitor);
                    }
                    // deberian ir al final, una vez que se inserto el nuevo Item en competencia y account
                    _model[oldProd_ix].TotalCompetitors = _model[oldProd_ix].Competitors.Distinct().Count();
                    _model[oldProd_ix].TotalStores      = _model[oldProd_ix].Competitors
                                                          .SelectMany(c => c.Accounts
                                                                      .Select(a => a.Account.Id))
                                                          .Distinct()
                                                          .Count();
                }
                else
                {
                    //Si no existe Creo el producto, con la competencia y la cadena
                    CompetitorAccountViewModel _newAccount = new CompetitorAccountViewModel()
                    {
                        Account = new AccountViewModel()
                        {
                            Id    = det.AccountId,
                            Name  = gm.Accounts.FirstOrDefault(a => a.Id == det.AccountId).Name,
                            Image = gm.Accounts.FirstOrDefault(a => a.Id == det.AccountId).Image
                        },
                        AccountGap          = det.PriceGap,
                        Color               = det.Color,
                        Inventory           = det.Inventory,
                        SellIn              = det.SellIn,
                        SelfPrice           = det.SelfPrice,
                        CompetitorPrice     = det.CompetitorPrice,
                        Request             = det.Request,
                        SelfProductId       = det.ProductId,
                        CompetitorProductId = det.CompetitorId
                    };

                    CompetitorGapViewModel _newCompetitor = new CompetitorGapViewModel()
                    {
                        ProductId   = det.CompetitorId,
                        ProductName = gm.Products.FirstOrDefault(p => p.Id == det.CompetitorId).Name
                    };
                    _newCompetitor.Accounts.Add(_newAccount);

                    ProductGapDetailViewModel _newGapDet = new ProductGapDetailViewModel()
                    {
                        ProductId   = det.ProductId,
                        ProductName = gm.Products.FirstOrDefault(p => p.Id == det.ProductId).Name,
                        Image       = gm.Products.FirstOrDefault(p => p.Id == det.ProductId).Image,
                        Inventory   = det.Inventory,
                        SellIn      = det.SellIn,

                        PriceGap         = det.PriceGap,
                        Color            = det.Color,
                        TotalCompetitors = 1,
                        TotalStores      = 1,
                        OrderMath        = det.OrderMath
                    };
                    _newGapDet.Competitors.Add(_newCompetitor);

                    _model.Add(_newGapDet);
                }
            }
            return(View(_model));
        }