public void UploadPermanentInfo(CheckOut_ViewModel model, ref List <PermanentCustomer_ProductAssociationTable> permanentEntries, int i)
        {
            var customer = _context.Customer.FirstOrDefault(c => c.Id == model.CustomerIds[i]);
            var product  = _context.Product.FirstOrDefault(p => p.Id == model.productInstances[i].ProductId);
            var evnt     = _context.Event.FirstOrDefault(e => e.Id == model.EventId);

            var color          = _context.Color.FirstOrDefault(c => c.Id == product.ColorId);
            var gender         = _context.Gender.FirstOrDefault(g => g.Id == product.GenderId);
            var size           = _context.Size.FirstOrDefault(s => s.Id == product.SizeId);
            var customerGender = _context.Gender.FirstOrDefault(g => g.Id == customer.GenderId);

            if (customer != null && product != null && evnt != null && color != null && size != null && gender != null && customerGender != null)
            {
                permanentEntries.Add(new PermanentCustomer_ProductAssociationTable
                {
                    OriginalCustomerId = model.CustomerIds[i],
                    FirstName          = customer.FirstName,
                    LastName           = customer.LastName,
                    Email             = customer.Email,
                    PhoneNumber       = customer.PhoneNumber,
                    Notes             = customer.Notes,
                    CustomerGender    = customerGender.Name,
                    Age               = customer.Age,
                    EventName         = evnt.Name,
                    OriginalProductId = product.Id,
                    ProductName       = product.Name,
                    Color             = color.Name,
                    Size              = size.Name,
                    ProductGender     = gender.Name,
                    AtRetailer        = evnt.IsRetailer
                });
            }
        }
        public void UploadPermanentInfo(CustomerCheckOut_ViewModel model, ref List <PermanentCustomer_ProductAssociationTable> permanentEntries, ProductInstance instance)
        {
            CheckOut_ViewModel checkOutModel = new CheckOut_ViewModel();

            checkOutModel.CustomerIds      = new int[1];
            checkOutModel.CustomerIds[0]   = model.CustomerId;
            checkOutModel.productInstances = new List <ProductInstance>();//_context.ProductInstance.Where(p => model.ProductInstanceIds.Contains(p.Id)).ToList();//works?
            checkOutModel.productInstances.Add(instance);
            int i = 0;

            checkOutModel.EventId = model.EventId;
            UploadPermanentInfo(checkOutModel, ref permanentEntries, i);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CheckOut(CheckOut_ViewModel model)
        {
            var permanentEntries = new List <PermanentCustomer_ProductAssociationTable>();


            //!!!the only thing successgully getting passed in and that I am finding is the instance in question, none of the customer info makes it to here
            //but really all I need is the customer id corresponding to each instance
            int eventId = 0;
            //first clear out all the instance_customer entries with this instances id
            var customer_ProductInstances_ForRemoval = new List <ProductInstance_Customer>();

            for (int i = 0; i < model.productInstances.Count(); i++)//this only loops through the number of product instances, what if the number of product instances has changed since the last time this was updated
            {
                //add the ProductInstance_Customer of this instance to clear later
                var CPIForRemoval = _context.ProductInstance_Customer.FirstOrDefault(pic => pic.ProductInstanceId == model.productInstances[i].Id);
                if (CPIForRemoval != null)
                {
                    customer_ProductInstances_ForRemoval.Add(CPIForRemoval);
                }
                else
                {
                    //no pre-existing PIC entry because never checked out AND...
                    if (model.productInstances[i].CheckedOut == false)//...the item is NOT being checked out now
                    {
                        //that is item has never been checked out and is not being checked out now.
                        //So, continue because nothing to update
                        continue;
                    }
                }

                //here find the existing productinsttance and update it
                var instance = _context.ProductInstance.FirstOrDefault(pi => pi.Id == model.productInstances[i].Id);//i don't know if this lines up right

                if (instance != null)
                {
                    instance.CheckedOut = model.productInstances[i].CheckedOut;// i have no idea if this lines up right
                }

                _context.Update(instance);

                _context.ProductInstance_Customer.Add(new ProductInstance_Customer
                {
                    ProductInstanceId = instance.Id,
                    CustomerId        = model.CustomerIds[i]
                });

                await _context.SaveChangesAsync();

                _productKitService.UploadPermanentInfo(model, ref permanentEntries, i);
            }

            _context.PermanentCustomer_ProductAssociationTable.AddRange(permanentEntries);
            _context.SaveChanges();

            eventId = model.EventId;

            if (customer_ProductInstances_ForRemoval.Count != 0)//not sure if this check is sufficient, what if the count is non-zero but its like some number greater or lesser than the ones that were added so i didn't get all of them in the above loop?
            {
                _context.ProductInstance_Customer.RemoveRange(customer_ProductInstances_ForRemoval);
            }

            await _context.SaveChangesAsync();

            if (ModelState.IsValid)
            {
                //try
                //{
                //    _context.Update(state);
                //    await _context.SaveChangesAsync();
                //}
                //catch (DbUpdateConcurrencyException)
                //{
                //    if (!StateExists(state.Id))
                //    {
                //        return NotFound();
                //    }
                //    else
                //    {
                //        throw;
                //    }
                //}
                return(RedirectToAction("Details", "Events", new { id = eventId }));
            }
            return(View(model));
        }
Ejemplo n.º 4
0
        // GET: ProductKits/Checkout/5
        public async Task <IActionResult> CheckOut(int?eventId, int?styleId, int?productId)  //checkout will have to be for all of the product kits with this style and event, not for one product kit, probably fixt this later
        {
            if (eventId == null || styleId == null || productId == null)
            {
                return(NotFound());
            }

            var evnt = await _context.Event.SingleOrDefaultAsync(m => m.Id == eventId);

            var style = await _context.Style.SingleOrDefaultAsync(m => m.Id == styleId);

            var product = await _context.Product.SingleOrDefaultAsync(m => m.Id == productId);

            if (evnt == null || style == null || product == null)
            {
                return(NotFound());
            }

            var model = new CheckOut_ViewModel();

            model.ProductName = product.Name;
            model.EventId     = evnt.Id;
            //model.productInstances = _context.ProductInstance.Where(i => i.ProductKitId == productKitId && i.ProductId == productId).ToList();

            var kits = _context.ProductKit.Where(k => k.EventId == eventId && k.StyleId == styleId).ToList();

            model.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 && pi.ProductId == productId).ToList();//so these will automatically be the right style, and lots of diff products
                model.productInstances.AddRange(thisKitsInstances);
            }

            //model.productKit = productKit;//not sure where this is needed in the view or what not having it will do?
            //model.products = _context.Product.Where(p => p.StyleId == productKit.StyleId).ToList();//what was i going to use this for? is it hidden in the view?
            model.CustomerIds = new int[model.productInstances.Count()];

            //i have to check here if there are instances in the productinstance_customer table for these productinstances and if so load in the correct customerids
            //to put as the default in the dropdowns i think
            for (int i = 0; i < model.productInstances.Count(); i++)
            {
                var customer_productInstance = _context.ProductInstance_Customer.FirstOrDefault(pic => pic.ProductInstanceId == model.productInstances[i].Id);//.CustomerId;//this should be all i need to get the customer ids in the right order here
                if (customer_productInstance != null)
                {
                    var customer = _context.Customer.FirstOrDefault(c => c.Id == customer_productInstance.CustomerId);
                    if (customer != null)
                    {
                        model.CustomerIds[i] = customer.Id;
                    }
                }

                //don't use below, instead need to use an independent identifer table to add unique identifiers upon instance creation
                //model.productInstances[i].Name = model.productInstances[i].Name + "-" + i;//this is not related to what else is going on in this for loop, this is just assigning ad
                //hoc instance identifiers, the issue with this is that there is no way to line it up with the instance names on customer checkin, except with some more complex logic maybe?
            }

            var customers = _context.Customer.OrderBy(c => c.LastName).Where(c => c.EventId == eventId).Select(x => new { Id = x.Id, Value = (x.LastName + ", " + x.FirstName) }); //not sure if this value works

            model.CustomerList = new SelectList(customers, "Id", "Value");                                                                                                         //should be able to put a 4th parameter in here as the default selected id but isn't working

            return(View(model));
        }