Ejemplo n.º 1
0
        public async Task <IActionResult> Get(int page = 1, int size = 25, string order = "{}", [Bind(Prefix = "Select[]")] List <string> select = null, string keyword = null, string filter = "{}")
        {
            VerifyUser();
            var    query                     = _garmentAvalProductRepository.Read(order, select, filter);
            int    totalRows                 = query.Count();
            double totalQty                  = query.Sum(a => a.GarmentAvalProductItem.Sum(b => b.Quantity));
            var    garmentAvalProductDto     = _garmentAvalProductRepository.Find(query).Select(o => new GarmentAvalProductDto(o)).OrderByDescending(x => x.LastModifiedDate).ToArray();
            var    garmentAvalProductItemDto = _garmentAvalProductItemRepository.Find(_garmentAvalProductItemRepository.Query).Select(o => new GarmentAvalProductItemDto(o)).ToList();

            Parallel.ForEach(garmentAvalProductDto, itemDto =>
            {
                var garmentPreparingItems = garmentAvalProductItemDto.Where(x => x.APId == itemDto.Id).ToList();

                itemDto.Items = garmentPreparingItems;

                //Parallel.ForEach(itemDto.Items, orderItem =>
                //{
                //    var selectedProduct = GetGarmentProduct(orderItem.Product.Id, WorkContext.Token);
                //    var selectedUom = GetUom(orderItem.Uom.Id, WorkContext.Token);

                //    if (selectedProduct != null && selectedProduct.data != null)
                //    {
                //        orderItem.Product.Name = selectedProduct.data.Name;
                //        orderItem.Product.Code = selectedProduct.data.Code;
                //    }

                //    if (selectedUom != null && selectedUom.data != null)
                //    {
                //        orderItem.Uom.Unit = selectedUom.data.Unit;
                //    }
                //});

                itemDto.Items = itemDto.Items.OrderBy(x => x.Id).ToList();
            });

            if (!string.IsNullOrEmpty(keyword))
            {
                garmentAvalProductDto = garmentAvalProductDto.Where(x => x.RONo.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                    x.Article.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToArray();
            }

            if (order != "{}")
            {
                Dictionary <string, string> OrderDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(order);
                garmentAvalProductDto = QueryHelper <GarmentAvalProductDto> .Order(garmentAvalProductDto.AsQueryable(), OrderDictionary).ToArray();
            }

            garmentAvalProductDto = garmentAvalProductDto.Take(size).Skip((page - 1) * size).ToArray();

            await Task.Yield();

            return(Ok(garmentAvalProductDto, info: new
            {
                page,
                size,
                count = totalRows,
                totalQty
            }));
        }
        public async Task <GarmentAvalProduct> Handle(RemoveGarmentAvalProductCommand request, CancellationToken cancellationToken)
        {
            var garmentAvalProduct = _garmentAvalProductRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

            if (garmentAvalProduct == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Id));
            }

            var garmentAvalProductItems = _garmentAvalProductItemRepository.Find(x => x.APId == request.Id);

            foreach (var item in garmentAvalProductItems)
            {
                item.Remove();
                await _garmentAvalProductItemRepository.Update(item);

                var garmentPreparingItem = _garmentPreparingItemRepository.Find(o => o.Identity == Guid.Parse(item.PreparingItemId.Value)).Single();

                garmentPreparingItem.setRemainingQuantityZeroValue(garmentPreparingItem.RemainingQuantity + item.Quantity);

                garmentPreparingItem.SetModified();
                await _garmentPreparingItemRepository.Update(garmentPreparingItem);
            }

            garmentAvalProduct.Remove();

            await _garmentAvalProductRepository.Update(garmentAvalProduct);

            _storage.Save();

            return(garmentAvalProduct);
        }
        public async Task <GarmentAvalProduct> Handle(UpdateGarmentAvalProductCommand request, CancellationToken cancellaitonToken)
        {
            var garmentAvalProduct = _garmentAvalProductRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

            if (garmentAvalProduct == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Id));
            }

            garmentAvalProduct.SetRONo(request.RONo);
            garmentAvalProduct.SetArticle(request.Article);
            garmentAvalProduct.SetAvalDate(request.AvalDate);

            var dbGarmentAvalProduct = _garmentAvalProductItemRepository.Find(y => y.APId == garmentAvalProduct.Identity);
            var updatedItems         = request.Items.Where(x => dbGarmentAvalProduct.Any(y => y.APId == garmentAvalProduct.Identity));
            var addedItems           = request.Items.Where(x => !dbGarmentAvalProduct.Any(y => y.APId == garmentAvalProduct.Identity));
            var deletedItems         = dbGarmentAvalProduct.Where(x => !request.Items.Any(y => y.APId == garmentAvalProduct.Identity));

            foreach (var item in updatedItems)
            {
                var dbItem = dbGarmentAvalProduct.Find(x => x.Identity == item.Identity);
                dbItem.setPreparingId(item.PreparingId);
                dbItem.setPreparingItemId(item.PreparingItemId);
                dbItem.setProductId(new ProductId(item.Product.Id));
                dbItem.setProductCode(item.Product.Code);
                dbItem.setProductName(item.Product.Name);
                dbItem.setDesignColor(item.DesignColor);
                dbItem.setQuantity(item.Quantity);
                dbItem.setUomId(new UomId(item.Uom.Id));
                dbItem.setUomUnit(item.Uom.Unit);
                await _garmentAvalProductItemRepository.Update(dbItem);
            }

            addedItems.Select(x => new GarmentAvalProductItem(Guid.NewGuid(), garmentAvalProduct.Identity, x.PreparingId, x.PreparingItemId, new ProductId(x.Product.Id), x.Product.Code, x.Product.Name, x.DesignColor, x.Quantity, new UomId(x.Uom.Id), x.Uom.Unit, x.BasicPrice, x.IsReceived)).ToList()
            .ForEach(async x => await _garmentAvalProductItemRepository.Update(x));

            foreach (var item in deletedItems)
            {
                item.SetDeleted();
                await _garmentAvalProductItemRepository.Update(item);
            }


            garmentAvalProduct.SetModified();

            await _garmentAvalProductRepository.Update(garmentAvalProduct);

            _storage.Save();

            return(garmentAvalProduct);
        }