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

            _garmentScrapTransactionItemRepository.Find(o => o.ScrapTransactionId == scrapTransaction.Identity).ForEach(async item =>
            {
                if (scrapTransaction.TransactionType == "IN")
                {
                    var gStock = _garmentScrapStockRepository.Query.Where(s => s.ScrapDestinationId == scrapTransaction.ScrapDestinationId && s.ScrapClassificationId == item.ScrapClassificationId).Select(i => new GarmentScrapStock(i)).FirstOrDefault();
                    gStock.SetQuantity(gStock.Quantity - item.Quantity);
                    gStock.Modify();
                    await _garmentScrapStockRepository.Update(gStock);
                }
                else
                {
                    var gStock = _garmentScrapStockRepository.Query.Where(s => s.ScrapDestinationId == scrapTransaction.ScrapDestinationId && s.ScrapClassificationId == item.ScrapClassificationId).Select(i => new GarmentScrapStock(i)).FirstOrDefault();
                    gStock.SetQuantity(gStock.Quantity + item.Quantity);
                    gStock.Modify();
                    await _garmentScrapStockRepository.Update(gStock);
                }
                item.Remove();
                await _garmentScrapTransactionItemRepository.Update(item);
            });
            scrapTransaction.Remove();
            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);
            }
        }