public async Task <GarmentScrapTransaction> Handle(UpdateGarmentScrapTransactionCommand request, CancellationToken cancellationToken)
        {
            var scrapTransaction = _garmentScrapTransactionRepository.Query.Where(o => o.Identity == request.Identity).Select(o => new GarmentScrapTransaction(o)).Single();

            _garmentScrapTransactionItemRepository.Find(o => o.ScrapTransactionId == scrapTransaction.Identity).ForEach(async item =>
            {
                var gStock = _garmentScrapStockRepository.Query.Where(s => s.ScrapDestinationId == scrapTransaction.ScrapDestinationId && s.ScrapClassificationId == item.ScrapClassificationId).Select(i => new GarmentScrapStock(i)).Single();
                var items  = request.Items.Where(o => o.Id == item.Identity).Single();
                if (scrapTransaction.TransactionType == "IN")
                {
                    gStock.SetQuantity(gStock.Quantity - item.Quantity + items.Quantity);
                    await _garmentScrapTransactionItemRepository.Update(item);
                    gStock.Modify();
                    await _garmentScrapStockRepository.Update(gStock);
                }
                else
                {
                    gStock.SetQuantity(gStock.Quantity + item.Quantity - items.Quantity);
                    await _garmentScrapTransactionItemRepository.Update(item);
                    gStock.Modify();
                    await _garmentScrapStockRepository.Update(gStock);
                }
                item.SetQuantity(items.Quantity);
                item.SetDescription(items.Description);
                item.Modify();
                await _garmentScrapTransactionItemRepository.Update(item);
            });
            scrapTransaction.SetTransactionDate(request.TransactionDate);
            scrapTransaction.Modify();
            await _garmentScrapTransactionRepository.Update(scrapTransaction);

            _storage.Save();
            return(scrapTransaction);
        }
        public async Task <GarmentScrapTransaction> Handle(PlaceGarmentScrapTransactionCommand request, CancellationToken cancellationToken)
        {
            if (request.TransactionType == "IN")
            {
                request.Items = request.Items.Where(item => item.Quantity > 0).ToList();

                Guid identity = Guid.NewGuid();
                GarmentScrapTransaction transaction = new GarmentScrapTransaction(
                    identity,
                    GenerateTransactionNo(request),
                    request.TransactionType,
                    request.TransactionDate,
                    request.ScrapSourceId,
                    request.ScrapSourceName,
                    request.ScrapDestinationId,
                    request.ScrapDestinationName
                    );
                await _garmentScrapTransactionRepository.Update(transaction);

                foreach (var item in request.Items)
                {
                    if (item.Quantity > 0)
                    {
                        Guid itemIdentity = Guid.NewGuid();
                        GarmentScrapTransactionItem garmentScrapTransactionItem = new GarmentScrapTransactionItem(
                            itemIdentity,
                            transaction.Identity,
                            item.ScrapClassificationId,
                            item.ScrapClassificationName,
                            item.Quantity,
                            item.UomId,
                            item.UomUnit,
                            item.Description
                            );
                        var stock = _garmentScrapStockRepository.Query.Where(s => s.ScrapClassificationId == item.ScrapClassificationId && s.ScrapDestinationId == request.ScrapDestinationId).Select(s => new GarmentScrapStock(s)).FirstOrDefault();
                        if (stock != null)
                        {
                            stock.SetQuantity(stock.Quantity + item.Quantity);
                            stock.Modify();
                            await _garmentScrapStockRepository.Update(stock);
                        }
                        else
                        {
                            Guid stockIdentity = Guid.NewGuid();
                            GarmentScrapStock garmentScrapStock = new GarmentScrapStock(
                                stockIdentity,
                                transaction.ScrapDestinationId,
                                transaction.ScrapDestinationName,
                                item.ScrapClassificationId,
                                item.ScrapClassificationName,
                                item.Quantity,
                                item.UomId,
                                item.UomUnit
                                );
                            await _garmentScrapStockRepository.Update(garmentScrapStock);
                        }

                        await _garmentScrapTransactionItemRepository.Update(garmentScrapTransactionItem);

                        _storage.Save();
                    }
                }
                return(transaction);
            }
            else
            {
                request.Items = request.Items.Where(item => item.Quantity > 0).ToList();

                Guid identity = Guid.NewGuid();
                GarmentScrapTransaction transaction = new GarmentScrapTransaction(
                    identity,
                    GenerateTransactionNoOut(request),
                    request.TransactionType,
                    request.TransactionDate,
                    request.ScrapSourceId,
                    request.ScrapSourceName,
                    request.ScrapDestinationId,
                    request.ScrapDestinationName
                    );
                await _garmentScrapTransactionRepository.Update(transaction);

                foreach (var item in request.Items)
                {
                    if (item.Quantity > 0)
                    {
                        Guid itemIdentity = Guid.NewGuid();
                        GarmentScrapTransactionItem garmentScrapTransactionItem = new GarmentScrapTransactionItem(
                            itemIdentity,
                            transaction.Identity,
                            item.ScrapClassificationId,
                            item.ScrapClassificationName,
                            item.Quantity,
                            item.UomId,
                            item.UomUnit,
                            item.Description
                            );
                        var stock = _garmentScrapStockRepository.Query.Where(s => s.ScrapClassificationId == item.ScrapClassificationId && s.ScrapDestinationId == request.ScrapDestinationId).Select(s => new GarmentScrapStock(s)).FirstOrDefault();
                        if (stock != null)
                        {
                            stock.SetQuantity(stock.Quantity - item.Quantity);
                            stock.Modify();
                            await _garmentScrapStockRepository.Update(stock);
                        }
                        else
                        {
                            Guid stockIdentity = Guid.NewGuid();
                            GarmentScrapStock garmentScrapStock = new GarmentScrapStock(
                                stockIdentity,
                                transaction.ScrapDestinationId,
                                transaction.ScrapDestinationName,
                                item.ScrapClassificationId,
                                item.ScrapClassificationName,
                                item.Quantity,
                                item.UomId,
                                item.UomUnit
                                );
                            await _garmentScrapStockRepository.Update(garmentScrapStock);
                        }

                        await _garmentScrapTransactionItemRepository.Update(garmentScrapTransactionItem);

                        _storage.Save();
                    }
                }
                return(transaction);
            }
        }