Ejemplo n.º 1
0
        public async Task <List <FreeProductHeaderModel> > GetFreeProductList()
        {
            List <FreeProductHeaderModel> freeProductList = new List <FreeProductHeaderModel>();
            FreeProductHeaderModel        freeProduct     = null;

            var freeItems = await _context.FreeProducts.Where(s => s.IsActive).ToListAsync();

            if (freeItems.Any())
            {
                freeItems.ForEach(s =>
                {
                    freeProduct = new FreeProductHeaderModel();
                    var parents = _context.FreeProductParents.Where(x => x.FreeProductId == s.Id).ToList();
                    parents.ForEach(p =>
                    {
                        var parentProduct = _context.Products.FirstOrDefault(x => x.Id == p.ProductId);
                        freeProduct.ParentProducts.Add(new FreeProductDetailModel
                        {
                            ProductId = p.ProductId,
                            Name      = parentProduct.Name,
                            Quantity  = p.RequiredQuantity
                        });
                    });

                    var children = _context.FreeProductChildren.Where(x => x.FreeProductId == s.Id).ToList();
                    children.ForEach(p =>
                    {
                        var childProduct = _context.Products.FirstOrDefault(x => x.Id == p.ProductId);
                        freeProduct.ChildProducts.Add(new FreeProductDetailModel
                        {
                            ProductId = p.ProductId,
                            Name      = childProduct.Name,
                            Quantity  = p.FreeQuantity
                        });
                    });

                    freeProductList.Add(freeProduct);
                });
            }

            return(freeProductList);
        }
        private PackingSlipHeader CheckAndAddFreeItems(PackingSlipHeader packingSlipHeader, FreeProductHeaderModel headerModel)
        {
            bool isApplicable = true;

            foreach (var parent in headerModel.ParentProducts)
            {
                var productExists = (from tmpLine in packingSlipHeader.PackingSlipItems where tmpLine.Name == parent.Name && tmpLine.Quantity >= parent.Quantity select tmpLine).FirstOrDefault();
                if (productExists == null)
                {
                    isApplicable = false;
                    break;
                }
            }

            if (isApplicable)
            {
                foreach (var child in headerModel.ChildProducts)
                {
                    packingSlipHeader.PackingSlipItems.Add(new PackingSlipItem
                    {
                        IsFreeItem = true,
                        Name       = child.Name,
                        ProductId  = child.ProductId,
                        Quantity   = child.Quantity
                    });
                }
            }

            return(packingSlipHeader);
        }