public async Task <GarmentDeliveryReturn> Handle(RemoveGarmentDeliveryReturnCommand request, CancellationToken cancellationToken)
        {
            var garmentDeliveryReturn = _garmentDeliveryReturnRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

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

            var garmentDeliveryReturnItems = _garmentDeliveryReturnItemRepository.Find(x => x.DRId == request.Id);

            foreach (var item in garmentDeliveryReturnItems)
            {
                item.Remove();
                await _garmentDeliveryReturnItemRepository.Update(item);

                if (item.ProductName == "FABRIC")
                {
                    var garmentPreparingItem = _garmentPreparingItemRepository.Find(o => o.Identity == Guid.Parse(item.PreparingItemId)).Single();

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

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

            garmentDeliveryReturn.Remove();

            await _garmentDeliveryReturnRepository.Update(garmentDeliveryReturn);

            _storage.Save();

            return(garmentDeliveryReturn);
        }
        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     = _garmentDeliveryReturnRepository.Read(order, select, filter);
            int    totalRows = query.Count();
            double totalQty  = query.Sum(a => a.GarmentDeliveryReturnItem.Sum(b => b.Quantity));
            var    garmentDeliveryReturnDto     = _garmentDeliveryReturnRepository.Find(query).Select(o => new GarmentDeliveryReturnDto(o)).OrderByDescending(x => x.LastModifiedDate).ToArray();
            var    garmentDeliveryReturnItemDto = _garmentDeliveryReturnItemRepository.Find(_garmentDeliveryReturnItemRepository.Query).Select(o => new GarmentDeliveryReturnItemDto(o)).ToList();

            Parallel.ForEach(garmentDeliveryReturnDto, itemDto =>
            {
                if (itemDto.Storage.Name == null)
                {
                    itemDto.Storage.Name = "-";
                }
                var garmentDeliveryReturnItems = garmentDeliveryReturnItemDto.Where(x => x.DRId == itemDto.Id).ToList();

                itemDto.Items = garmentDeliveryReturnItems;

                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))
            {
                garmentDeliveryReturnDto = garmentDeliveryReturnDto.Where(x => x.DRNo.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                          x.UnitDONo.Contains(keyword, StringComparison.OrdinalIgnoreCase) || x.Unit.Name.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                          x.Storage.Name.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToArray();
            }

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

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

            await Task.Yield();

            return(Ok(garmentDeliveryReturnDto, info: new
            {
                page,
                size,
                count = totalRows,
                totalQty
            }));
        }
        public async Task <GarmentDeliveryReturn> Handle(UpdateGarmentDeliveryReturnCommand request, CancellationToken cancellaitonToken)
        {
            var requestTempItems = request.Items.Where(item => item.IsSave != true);

            request.Items = request.Items.Where(item => item.IsSave == true).ToList();
            var garmentDeliveryReturn = _garmentDeliveryReturnRepository.Find(o => o.Identity == request.Identity).FirstOrDefault();

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

            garmentDeliveryReturn.setDRNo(request.DRNo);
            garmentDeliveryReturn.setRONo(request.RONo);
            garmentDeliveryReturn.setArticle(request.Article);
            garmentDeliveryReturn.setUnitDOId(request.UnitDOId);
            garmentDeliveryReturn.setUnitDONo(request.UnitDONo);
            garmentDeliveryReturn.setUENId(request.UENId);
            garmentDeliveryReturn.setPreparingId(request.PreparingId);
            garmentDeliveryReturn.setReturnDate(request.ReturnDate);
            garmentDeliveryReturn.setReturnType(request.ReturnType);
            garmentDeliveryReturn.setUnitId(new UnitDepartmentId(request.Unit.Id));
            garmentDeliveryReturn.setUnitCode(request.Unit.Code);
            garmentDeliveryReturn.setUnitName(request.Unit.Name);
            garmentDeliveryReturn.setStorageId(new StorageId(request.Storage.Id));
            garmentDeliveryReturn.setStorageCode(request.Storage.Code);
            garmentDeliveryReturn.setStorageName(request.Storage.Name);
            garmentDeliveryReturn.setIsUsed(request.IsUsed);

            var dbGarmentDeliveryReturnItem = _garmentDeliveryReturnItemRepository.Find(y => y.DRId == garmentDeliveryReturn.Identity);
            var updatedItems = request.Items.Where(x => dbGarmentDeliveryReturnItem.Any(y => y.DRId == garmentDeliveryReturn.Identity));
            var addedItems   = request.Items.Where(x => !dbGarmentDeliveryReturnItem.Any(y => y.DRId == garmentDeliveryReturn.Identity));


            foreach (var item in updatedItems)
            {
                var dbItem = dbGarmentDeliveryReturnItem.Find(x => x.Identity == item.Id);
                dbItem.setUnitDOItemId(item.UnitDOItemId);
                dbItem.setUENItemId(item.UENItemId);
                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.setRONo(item.RONo);

                dbItem.setUomId(new UomId(item.Uom.Id));
                dbItem.setUomUnit(item.Uom.Unit);

                if (item.Product.Name == "FABRIC")
                {
                    var garmentPreparingItem = _garmentPreparingItemRepository.Query.Where(x => x.Identity == Guid.Parse(item.PreparingItemId)).Select(s => new GarmentPreparingItem(s)).Single();
                    garmentPreparingItem.setRemainingQuantity(garmentPreparingItem.RemainingQuantity + dbItem.Quantity - item.Quantity);
                    garmentPreparingItem.SetModified();
                    await _garmentPreparingItemRepository.Update(garmentPreparingItem);
                }
                dbItem.setQuantity(item.Quantity);

                dbItem.SetModified();
                await _garmentDeliveryReturnItemRepository.Update(dbItem);
            }

            addedItems.Select(x => new GarmentDeliveryReturnItem(Guid.NewGuid(), garmentDeliveryReturn.Identity, x.UnitDOItemId, x.UENItemId, x.PreparingItemId, new ProductId(x.Product.Id), x.Product.Code, x.Product.Name, x.DesignColor, x.RONo, x.Quantity, new UomId(x.Uom.Id), x.Uom.Unit)).ToList()
            .ForEach(async x => await _garmentDeliveryReturnItemRepository.Update(x));

            foreach (var itemDeleted in requestTempItems)
            {
                var dbItemDeleted = dbGarmentDeliveryReturnItem.Find(x => x.Identity == itemDeleted.Id);
                var deletedItems  = dbGarmentDeliveryReturnItem.Find(x => x.Identity == itemDeleted.Id);
                if (itemDeleted.Product.Name == "FABRIC")
                {
                    var garmentPreparingItemDeleted = _garmentPreparingItemRepository.Query.Where(x => x.Identity == Guid.Parse(itemDeleted.PreparingItemId)).Select(s => new GarmentPreparingItem(s)).Single();
                    garmentPreparingItemDeleted.setRemainingQuantity(garmentPreparingItemDeleted.RemainingQuantity + dbItemDeleted.Quantity);
                    garmentPreparingItemDeleted.SetModified();
                    await _garmentPreparingItemRepository.Update(garmentPreparingItemDeleted);
                }
                deletedItems.Remove();
                await _garmentDeliveryReturnItemRepository.Update(deletedItems);
            }

            garmentDeliveryReturn.SetModified();

            await _garmentDeliveryReturnRepository.Update(garmentDeliveryReturn);

            _storage.Save();

            return(garmentDeliveryReturn);
        }