Exemplo n.º 1
0
 public GarmentScrapStockDto(GarmentScrapStock transaction)
 {
     Id = transaction.Identity;
     ScrapClassificationId   = transaction.ScrapClassificationId;
     ScrapClassificationName = transaction.ScrapClassificationName;
     ScrapDestinationId      = transaction.ScrapDestinationId;
     ScrapDestinationName    = transaction.ScrapDestinationName;
     UomId    = transaction.UomId;
     UomUnit  = transaction.UomUnit;
     Quantity = transaction.Quantity;
 }
        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);
            }
        }