Пример #1
0
        public async Task <IActionResult> Create(ProductKit_ViewModel model)
        {
            var productKit = new ProductKit();

            productKit.EventId     = model.EventId;
            productKit.Name        = model.Name.Replace(" ", "-");
            productKit.StyleId     = model.StyleId;
            productKit.TerritoryId = model.TerritoryId;

            if (ModelState.IsValid)
            {
                _context.Add(productKit);
                await _context.SaveChangesAsync();

                productKit.Name = productKit.Name + "-" + _productKitService.AddProductKitIdentifier(model, ref productKit);

                _context.Update(productKit);
                await _context.SaveChangesAsync();

                _productKitService.AddInstances(model, productKit, 0);

                await _context.SaveChangesAsync();

                //return RedirectToAction("Details", "Events", new { id = productKit.EventId });
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
        public void AddInstances(ProductKit_ViewModel model, ProductKit productKit, int numInstancesRemovedFromEdit)
        {
            for (int i = 0; i < model.ProductNames.Count(); i++)
            {
                var numberExistingInstancesOfthisProduct = _context.ProductInstance.Where(pi => pi.ProductId == model.ProductIds[i]).Count();

                for (int j = 0; j < model.Quantities[i]; j++)
                {
                    //var identifier = numberExistingInstancesOfthisProduct + j;
                    //identifier += numInstancesRemovedFromEdit;
                    //do something like find how many product kits with this territory have these instances and
                    //add to that number?
                    //maybe identifier only matters in the context of a given event, so just do this on events
                    _context.ProductInstance.Add(new ProductInstance
                    {
                        Name         = model.ProductNames[i] /*+ "-" + identifier*/,
                        ProductId    = model.ProductIds[i],
                        ProductKitId = productKit.Id,
                        CheckedOut   = false
                    });
                    _context.SaveChanges();
                    //need to save the changes and then modify the name of the instance to have the new identifier? no how do i find that instance again?
                    var thisInstance = _context.ProductInstance.LastOrDefault();//this is kind of a sketchy way of doing this?
                    thisInstance.Name = thisInstance.Name + "-" + AddProductInstanceIdentifier(thisInstance.ProductId, thisInstance.Id);
                    _context.Update(thisInstance);
                    _context.SaveChanges();
                }
            }
        }
        public string AddProductKitIdentifier(ProductKit_ViewModel model, ref ProductKit productKit)
        {
            //i think that this should only be existing productkits for this style, no need to make it territory exclusive because different territories get combined on an
            //event but their instances need exclusive names nonetheless, WAIT no that is the case for the instances but not for the proeuct kits, where is the instance identifier
            //creator?
            var existingProductKitsForThisStyleAndTerritory = _context.ProductKit.Where(pk => pk.TerritoryId == model.TerritoryId && pk.StyleId == model.StyleId).ToList();

            var existingProductKitIdentifiersForThisStyleAndTerritory = _context.ProductKitIdentifier.Where(pk => pk.TerritoryId == model.TerritoryId && pk.StyleId == model.StyleId).ToList();
            ProductKitIdentifier idToUse = new ProductKitIdentifier();

            if (existingProductKitIdentifiersForThisStyleAndTerritory.Count == 0)
            {
                _context.ProductKitIdentifier.Add(new ProductKitIdentifier
                {
                    Identifier   = 0,//if you want this to be 1 later then below count under if(!thereIsAnAvailableExistinIdentifier) needs to be plus 1
                    IsInUse      = true,
                    StyleId      = model.StyleId,
                    TerritoryId  = model.TerritoryId,
                    ProductKitId = productKit.Id
                });
                _context.SaveChanges();
                idToUse = _context.ProductKitIdentifier.FirstOrDefault();//if there are none add it and assign it to idtouse
            }
            else
            {
                var thereIsAnAvailableExistinIdentifier = false;
                foreach (var identifier in existingProductKitIdentifiersForThisStyleAndTerritory)
                {
                    if (identifier.IsInUse == false)
                    {
                        thereIsAnAvailableExistinIdentifier = true;
                        idToUse = identifier;
                        identifier.ProductKitId = productKit.Id;
                        identifier.IsInUse      = true;
                        _context.Update(identifier); //i think that this is necessary...
                        _context.SaveChanges();
                        break;                       //not sure if this continue is breaking only out of this if else? that's all i want it to do
                    }
                }
                if (!thereIsAnAvailableExistinIdentifier)
                {
                    _context.ProductKitIdentifier.Add(new ProductKitIdentifier
                    {
                        Identifier   = existingProductKitIdentifiersForThisStyleAndTerritory.Count(),
                        IsInUse      = true,
                        StyleId      = model.StyleId,
                        TerritoryId  = model.TerritoryId,
                        ProductKitId = productKit.Id
                    });
                    _context.SaveChanges();
                    idToUse = _context.ProductKitIdentifier.LastOrDefault();//set to last because we just added it
                }
            }

            return(idToUse.Identifier.ToString());
        }
Пример #4
0
        // GET: ProductKits/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var productKit = await _context.ProductKit
                             .SingleOrDefaultAsync(m => m.Id == id);

            if (productKit == null)
            {
                return(NotFound());
            }

            //
            var model = new ProductKit_ViewModel();

            model.Name         = productKit.Name;
            model.ProductNames = new List <string>();
            var products = _context.Product.Where(p => p.StyleId == productKit.StyleId).ToList();

            model.Quantities = new int[products.Count()];
            for (int i = 0; i < products.Count(); i++)
            {
                model.ProductNames.Add(products[i].Name);

                var instances = _context.ProductInstance.Where(pi => pi.ProductKitId == productKit.Id && pi.ProductId == products[i].Id).ToList();//AND of this product

                int instanceCount = 0;
                for (int j = 0; j < instances.Count(); j++)
                {
                    instanceCount++;
                }
                model.Quantities[i] = instanceCount;
            }
            //
            return(View(model));
        }
Пример #5
0
        // GET: Events/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var @event = await _context.Event
                         .SingleOrDefaultAsync(m => m.Id == id);

            if (@event == null)
            {
                return(NotFound());
            }

            var EventDetails_ViewModel = new EventDetails_ViewModel();

            EventDetails_ViewModel.Event_ViewModel        = _eventService.ConvertEventToEventViewModel(@event);
            EventDetails_ViewModel.Event_ViewModel.Budget = _context.Budget.FirstOrDefault(b => b.EventId == @event.Id);

            EventDetails_ViewModel.ProductKit_ViewModels = new List <ProductKit_ViewModel>();

            var associatedProductKits = _context.ProductKit.Where(pk => pk.EventId == @event.Id).ToList();
            //maybe starting from above i repackage all the product kis with the same style id back into associated products kits as one product kit
            HashSet <int> allStyleIdsRepresentedAtThisEvent = new HashSet <int>();

            foreach (var productKit in associatedProductKits)
            {
                allStyleIdsRepresentedAtThisEvent.Add(productKit.StyleId);
            }

            //foreach (var styleId in allStyleIdsRepresentedAtThisEvent)
            //{

            //}

            //need to break these product kits up by styleid and add up all the ones with the same style id into one representation on the details page

            //this needs to be for uniqe style ids among product kits not product kits
            foreach (var styleId in allStyleIdsRepresentedAtThisEvent)                     //this foreach will need another loop where it does all the below for all the kits of a particular style and adds up all that stuff and makes it into a single product kit view model
            {
                var products = _context.Product.Where(p => p.StyleId == styleId).ToList(); //this is already getting all of the right products
                products = products.OrderBy(p => p.SizeId).ToList();                       //added to order the product kits

                var kits             = _context.ProductKit.Where(k => k.EventId == @event.Id && k.StyleId == styleId).ToList();
                var productInstances = new List <ProductInstance>();//so this will be all the instances from all the product kits taht are at this event and of this style

                foreach (var kit in kits)
                {
                    var thisKitsInstances = _context.ProductInstance.Where(pi => pi.ProductKitId == kit.Id).ToList();//so these will automatically be the right style, and lots of diff products
                    productInstances.AddRange(thisKitsInstances);
                }
                //this will need to be all product instances for all the kits with the same style id and event id?
                //like do a foreach one of these kits with this styleid at this event add up all of their instances

                //this should be fine
                var productKit_ViewModel = new ProductKit_ViewModel();
                //and these 4
                productKit_ViewModel.ProductNames        = new List <string>();
                productKit_ViewModel.ProductIds          = new List <int>();
                productKit_ViewModel.Quantities          = new int[products.Count()];
                productKit_ViewModel.QuantitiesAvailable = new int[products.Count()];

                //this should be fine? dunno if this name should have its number cut off the end, look for where i do that on product name creation
                productKit_ViewModel.Name = _context.Style.FirstOrDefault(s => s.Id == styleId).Name;
                //
                //below could be an issue, maybe don't need to kit id anymore and instead the event and style id
                //productKit_ViewModel.Id = kit.Id;//this might be needed for the link to checkout?
                productKit_ViewModel.EventId = @event.Id;
                productKit_ViewModel.StyleId = styleId;


                int quantityIndex = 0;

                foreach (var product in products)
                {
                    string productName = product.Name;

                    //only size:
                    //int sizeLength = productName.Length - (productName.LastIndexOf('-') + 1);
                    //productName = productName.Substring(productName.LastIndexOf('-') + 1, sizeLength);

                    int sizeLength = productName.Length - (productName.IndexOf('-') + 1);
                    productName = productName.Substring(productName.IndexOf('-') + 1, sizeLength); //make it so when products are named spaces are replaced with nothing for the name part first and then the size color gender added and spaces replaced with dashes

                    productKit_ViewModel.ProductNames.Add(productName);                            //maybe change this to like just the size name? or just the size and color and gender?
                    //productKit_ViewModel.ProductNames.Add(_context.Size.FirstOrDefault(s => s.Id == product.SizeId).Name + "-" + _context.Gender.FirstOrDefault(s => s.Id == product.GenderId).Name + "-" + _context.Color.FirstOrDefault(s => s.Id == product.ColorId).Name);//this doesn't really work cuz isn't distinguishing between color and gender options, so does that mean enforce one color and gender option per product kit? that's a bit boring i guess but maybe it would work?
                    //also above has like a ridiculous number of potential null ref exceptions, should figure out some way to reduce the product.Name rather than regathering all of the color and gender and size names from the database
                    productKit_ViewModel.ProductIds.Add(product.Id);
                    var instances = productInstances.Where(i => i.ProductId == product.Id).ToList();

                    int instanceCount          = 0;
                    int instanceAvaliableCount = 0;
                    for (int i = 0; i < instances.Count(); i++)
                    {
                        instanceCount++;
                        if (instances[i].CheckedOut == false)
                        {
                            instanceAvaliableCount++;
                        }
                    }
                    productKit_ViewModel.Quantities[quantityIndex]          = instanceCount;
                    productKit_ViewModel.QuantitiesAvailable[quantityIndex] = instanceAvaliableCount;//not sure if working
                    quantityIndex++;
                }

                EventDetails_ViewModel.ProductKit_ViewModels.Add(productKit_ViewModel);
            }

            var swagItemsFromThisEvent  = _context.Event_SwagItem.Where(es => es.EventId == id).ToList();
            var numSwagItemsOnThisEvent = swagItemsFromThisEvent.Count();

            EventDetails_ViewModel.Event_SwagItems = new List <Event_SwagItem_ViewModel>();
            //model.Event_SwagItems is what it iterates through on edit, so thats where i will have to add newly created booth items
            //and swag items

            for (int i = 0; i < numSwagItemsOnThisEvent; i++)
            {
                EventDetails_ViewModel.Event_SwagItems.Add(new Event_SwagItem_ViewModel
                {
                    EventId                     = swagItemsFromThisEvent[i].EventId,    //this could probably just be id as well
                    SwagItemId                  = swagItemsFromThisEvent[i].SwagItemId, //changed this to swagitem id from id, might be wrong on booths
                    QuantityBroughtToEvent      = swagItemsFromThisEvent[i].QuantityBroughtToEvent,
                    QuantityGivenAway           = swagItemsFromThisEvent[i].QuantityGivenAway,
                    QuantityRemainingAfterEvent = swagItemsFromThisEvent[i].QuantityBroughtToEvent - swagItemsFromThisEvent[i].QuantityGivenAway,
                    SwagItemName                = _context.SwagItem.FirstOrDefault(s => s.Id == swagItemsFromThisEvent[i].SwagItemId).Name//potential null ref
                });
            }

            var boothItemsFromThisEvent  = _context.Event_BoothItem.Where(es => es.EventId == id).ToList();
            var numBoothItemsOnThisEvent = boothItemsFromThisEvent.Count();

            EventDetails_ViewModel.Event_BoothItems = new List <Event_BoothItem_ViewModel>();

            for (int i = 0; i < numBoothItemsOnThisEvent; i++)
            {
                EventDetails_ViewModel.Event_BoothItems.Add(new Event_BoothItem_ViewModel
                {
                    EventId         = boothItemsFromThisEvent[i].EventId,
                    BoothItemId     = boothItemsFromThisEvent[i].BoothItemId,
                    QuantityAtEvent = boothItemsFromThisEvent[i].QuantityAtEvent,
                    BoothItemName   = _context.BoothItem.FirstOrDefault(s => s.Id == boothItemsFromThisEvent[i].BoothItemId).Name//potential null ref
                });
            }


            return(View(EventDetails_ViewModel));
        }
Пример #6
0
        public async Task <IActionResult> Edit(int id, ProductKit_ViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            ProductKit productKit = _context.ProductKit.FirstOrDefault(p => p.Id == id);

            productKit.EventId     = model.EventId;
            productKit.Name        = model.Name.Replace(" ", "-");
            productKit.StyleId     = model.StyleId;
            productKit.TerritoryId = model.TerritoryId;


            //remove all of the existing product instances and customer_productinstnace entries, maybe this could be bad in the future and loss of data? what if they try
            //to edit the number of instances in a product kit after they've already saved a bunch of check out and check in info
            var        productInstances   = _context.ProductInstance.Where(i => i.ProductKitId == productKit.Id).ToList();
            List <int> productInstanceIds = new List <int>();

            foreach (var instance in productInstances)
            {
                productInstanceIds.Add(instance.Id);
            }
            var customer_Instances = _context.ProductInstance_Customer.Where(pic => productInstanceIds.Contains(pic.ProductInstanceId)).ToList();

            //clear instance identifiers
            var instanceIdentifiers = _context.ProductInstanceIdentifier.Where(i => productInstanceIds.Contains((int)i.InstanceId)).ToList(); //AAAAAAAAAAAAAAAAAAAAAAAA

            foreach (var instanceIdentifer in instanceIdentifiers)                                                                            //AAAAAAAAAAA whole loop
            {
                instanceIdentifer.IsInUse    = false;
                instanceIdentifer.InstanceId = null;
            }
            //end clear instnace identifiers

            var numInstancesToBeRemoved = customer_Instances.Count();

            _context.ProductInstance.RemoveRange(productInstances);
            _context.ProductInstance_Customer.RemoveRange(customer_Instances);

            //editing the product kit here is actually change the number of instances entirely, so I may have to delete all instances associated with this kit and
            //add in altogether new ones, rather than upate the ones that remained, because the whole point of editing the product kit is the change the number of instances
            //because what I am changing is the quantity of instnaces it is not a quality of a given instance that is being changed, so yeah they will all have to be
            //deleted, meaning that their associations with customers will also have to be deleted

            //ADDED

            //END

            if (ModelState.IsValid)
            {
                _context.Update(productKit);
                await _context.SaveChangesAsync();

                _productKitService.AddInstances(model, productKit, numInstancesToBeRemoved);

                //for (int i = 0; i < model.ProductNames.Count(); i++)//for every product
                //{

                //    var numberExistingInstancesOfthisProduct = _context.ProductInstance.Where(pi => pi.ProductId == model.ProductIds[i]).Count();

                //    for (int j = 0; j < model.Quantities[i]; j++)
                //    {//change where this j starts to say make identifiers start with 1 if you want
                //        var identifier = numberExistingInstancesOfthisProduct + j;

                //        _context.ProductInstance.Add(new ProductInstance
                //        {
                //            Name = model.ProductNames[i] + "-" + identifier,
                //            ProductId = model.ProductIds[i],
                //            ProductKitId = productKit.Id,
                //            CheckedOut = false
                //        });
                //    }

                //}

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
                //return RedirectToAction("Details", "Events", new { id = model.EventId });
            }
            return(View(model));

            #region old
            //if (ModelState.IsValid)
            //{
            //    try
            //    {
            //        _context.Update(model);
            //        await _context.SaveChangesAsync();
            //    }
            //    catch (DbUpdateConcurrencyException)
            //    {
            //        if (!ProductKitExists(model.Id))
            //        {
            //            return NotFound();
            //        }
            //        else
            //        {
            //            throw;
            //        }
            //    }
            //    return RedirectToAction(nameof(Index));
            //}
            #endregion
        }
Пример #7
0
        // GET: ProductKits/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            //i wonder if this could take as an input the existing productkit_viewmodel that is on the events page already? no cuz can only pass in an id thorugh the link button?

            if (id == null)
            {
                return(NotFound());
            }

            var productKit = await _context.ProductKit.SingleOrDefaultAsync(m => m.Id == id);

            if (productKit == null)
            {
                return(NotFound());
            }

            var model = new ProductKit_ViewModel();

            model.EventId     = productKit.EventId;
            model.Id          = productKit.Id;
            model.Name        = productKit.Name;
            model.StyleId     = productKit.StyleId;
            model.TerritoryId = productKit.TerritoryId;

            var productsList = _context.Product.Where(s => s.StyleId == model.StyleId).ToList();//this will be drawing from the existing style id,

            //which may have been edited in the interim, so keep in mind if you edit a product kit after changing the style you will be left with products of the new style
            //also what effect will this have on say customer_instance associations if you are checking out products and creating customer_instances, then you
            //change the style, then you come in and edit the product kit to have the new style, then start checking stuff out again, will you have lost your old
            //product_instances? if you change the stlye in the interim then going to edit will throw things off, instead i guess if a style edit is made then
            //all product kits and their associations are deleted as well? that would be a bit extreme though...

            model.ProductIds   = new List <int>();
            model.ProductNames = new List <string>();

            foreach (var product in productsList)
            {
                model.ProductNames.Add(product.Name);
                model.ProductIds.Add(product.Id);
            }

            model.Quantities          = new int[model.ProductNames.Count];
            model.QuantitiesAvailable = new int[model.ProductNames.Count];

            //add the right quantity in each spot of quantities
            var productInstances = _context.ProductInstance.Where(pi => pi.ProductKitId == productKit.Id).ToList();//so these will automatically be the right style, and lots of diff products
            int quantityIndex    = 0;

            foreach (var product in productsList)
            {
                var instances              = productInstances.Where(i => i.ProductId == product.Id).ToList();
                int instanceCount          = 0;
                int instanceAvaliableCount = 0;
                for (int i = 0; i < instances.Count(); i++)
                {
                    instanceCount++;
                    if (instances[i].CheckedOut == false)
                    {
                        instanceAvaliableCount++;//this logic was added a bit later not sure if it works
                    }
                }
                model.Quantities[quantityIndex]          = instanceCount;
                model.QuantitiesAvailable[quantityIndex] = instanceAvaliableCount;//not sure if working
                quantityIndex++;
            }

            return(View(model));
        }
Пример #8
0
        // GET: ProductKits/Create
        public IActionResult CreateGet(ChooseStyleForNewProductKit_ViewModel chooseModel, int evntId) //need to set the routing up to pass this in on create like you normally do on an edit, and that create button needs to be on an event details page,
        {                                                                                             //but where will the checkout page be? perhaps on event, details as well, and then press a button for check in and check out
            //style id will have to come from a dropdown
            var model = new ProductKit_ViewModel();

            model.EventId     = chooseModel.EventId;//maybe this works instead of above
            model.StyleId     = chooseModel.StyleId;
            model.TerritoryId = chooseModel.TerritoryId;

            //im still passing in evntId even though I don't need to so that routing knows to choose this method instead of the create post method because the only difference is the signature parameters
            //styleId isn't getting passed in from choose style

            string eventName = "";
            var    evnt      = _context.Event.FirstOrDefault(e => e.Id == model.EventId);

            if (evnt != null)
            {
                eventName = evnt.Name;
            }

            string styleName = "";
            var    style     = _context.Style.FirstOrDefault(s => s.Id == model.StyleId);

            if (style != null)
            {
                styleName = style.Name;
            }

            string territoryName = "";
            var    territory     = _context.Territory.FirstOrDefault(s => s.Id == model.TerritoryId);

            if (territory != null)
            {
                territoryName = territory.Name;
            }

            styleName     = styleName.Replace(" ", "-");
            territoryName = territoryName.Replace(" ", "-");
            eventName     = eventName.Replace(" ", "-");

            //


            if (style != null && evnt != null && territory != null) //wait no the identifier has to take into account the style and territory combination
            {
                model.Name = styleName + "-" + territoryName;       //* + "-" + idToUse.Identifier*/;//last part moved to post
                //* + "-" + existingProductKitsForThisStyleAndTerritory.Count*/;//here i need to start adding the identifiers, need to go through all existing product kits
                //with both this style and this terriotry and increment up once for each entry, not sure how above works out if/when i start deleting product kits, the count shoule
                //workout i think
            }
            else
            {
                model.Name = "Unnamed Product Kit";
            }

            var productsList = _context.Product.Where(s => s.StyleId == model.StyleId).ToList();

            productsList       = productsList.OrderBy(p => p.SizeId).ToList();//ADDED FOR ORDER test to make sure it does not throw off alignemnt in for loops with name
            model.ProductIds   = new List <int>();
            model.ProductNames = new List <string>();

            foreach (var product in productsList)
            {
                model.ProductNames.Add(product.Name);
                model.ProductIds.Add(product.Id);
            }
            model.Quantities = new int[model.ProductNames.Count];
            for (int i = 0; i < model.Quantities.Length; i++)
            {
                model.Quantities[i] = 1;
            }
            model.QuantitiesAvailable = new int[model.ProductNames.Count];


            return(View(model));
        }