Пример #1
0
        public async Task <IActionResult> Update(CreateContainerViewModel containerVM)
        {
            if (ModelState.IsValid == false)
            {
                return(View(await CreateViewModelWithPopulatedFormInputs()));
            }

            var container = await _context.Containers.FindAsync(containerVM.ContainerId);

            if (container == null)
            {
                return(NotFound("Container record not found"));
            }

            container.RefNum           = containerVM.RefNum;
            container.ExpTimeOfArrival = containerVM.ExpTimeOfArrival;
            container.ExpNumOfUnits    = containerVM.ExpNumOfUnits;
            container.ExpNumOfPallets  = containerVM.ExpNumOfPallets;
            container.ActTimeOfArrival = containerVM.ActTimeOfArrival;
            container.ActNumOfUnits    = containerVM.ActNumOfUnits;
            container.ActNumOfPallets  = containerVM.ActNumOfPallets;
            container.ArrivalDate      = DateTime.Today;
            container.DepartmentId     = containerVM.ContainerDepartmentId;
            container.TypeId           = containerVM.ContainerTypeId;
            container.BayId            = containerVM.BayId;
            container.DoorId           = containerVM.DoorId;
            container.StatusId         = containerVM.ContainerStatusId;
            container.ItemDiscrepancy  = (containerVM.ExpNumOfUnits.HasValue && containerVM.ActNumOfUnits.HasValue) ? (containerVM.ExpNumOfUnits.Value - containerVM.ActNumOfUnits.Value) : (int?)null;

            _context.Update(container);

            foreach (var comment in containerVM.Comments)
            {
                if (comment.Id == 0)
                {
                    var containerComment = new ContainerComments()
                    {
                        ContainerId = container.Id,
                        Comment     = comment.Comment
                    };

                    _context.Add(containerComment);
                }
                else if (comment.Id != 0)
                {
                    _context.Update(comment);
                }
            }

            if (containerVM.SupplierIds.Count() > 0)
            {
                var savedSuppliers = await _context.ContainerSuppliers.Where(cs => cs.ContainerId == container.Id).ToListAsync();

                var suppliersToDel = new List <ContainerSuppliers>(savedSuppliers);

                foreach (var supplierId in containerVM.SupplierIds)
                {
                    bool addThisSupplier = true;
                    foreach (var savedSupplier in savedSuppliers)
                    {
                        if (savedSupplier.SupplierId == supplierId)
                        {
                            addThisSupplier = false;
                            suppliersToDel.Remove(savedSupplier);
                        }
                    }

                    if (addThisSupplier)
                    {
                        var containerSuppliers = new ContainerSuppliers()
                        {
                            ContainerId = container.Id,
                            SupplierId  = supplierId.Value
                        };

                        _context.Add(containerSuppliers);
                    }
                }

                _context.RemoveRange(suppliersToDel);
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Пример #2
0
        public async Task <IActionResult> Create(CreateContainerViewModel containerVM)
        {
            if (ModelState.IsValid == false)
            {
                return(View(await CreateViewModelWithPopulatedFormInputs()));
            }

            int?itemD = null;

            if (containerVM.ExpNumOfUnits != null && containerVM.ActNumOfUnits != null)
            {
                itemD = containerVM.ExpNumOfUnits.Value - containerVM.ActNumOfUnits.Value;
            }

            var container = new Containers()
            {
                RefNum           = containerVM.RefNum,
                ExpTimeOfArrival = containerVM.ExpTimeOfArrival,
                ExpNumOfUnits    = containerVM.ExpNumOfUnits,
                ExpNumOfPallets  = containerVM.ExpNumOfPallets,
                ActTimeOfArrival = containerVM.ActTimeOfArrival,
                ActNumOfUnits    = containerVM.ActNumOfUnits,
                ActNumOfPallets  = containerVM.ActNumOfPallets,
                ArrivalDate      = DateTime.Today,
                DepartmentId     = containerVM.ContainerDepartmentId,
                TypeId           = containerVM.ContainerTypeId,
                BayId            = containerVM.BayId,
                DoorId           = containerVM.DoorId,
                StatusId         = containerVM.ContainerStatusId,
                ItemDiscrepancy  = (containerVM.ExpNumOfUnits.HasValue && containerVM.ActNumOfUnits.HasValue) ? (containerVM.ExpNumOfUnits.Value - containerVM.ActNumOfUnits.Value) : (int?)null
            };

            _context.Add(container);

            await _context.SaveChangesAsync();

            foreach (var comment in containerVM.Comments)
            {
                var containerComment = new ContainerComments()
                {
                    ContainerId = container.Id,
                    Comment     = comment.Comment
                };

                _context.Add(containerComment);
            }

            if (containerVM.SupplierIds?.Count() > 0)
            {
                foreach (var supplierId in containerVM.SupplierIds)
                {
                    var containerSuppliers = new ContainerSuppliers()
                    {
                        ContainerId = container.Id,
                        SupplierId  = supplierId.Value
                    };

                    _context.Add(containerSuppliers);
                }
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }