public Task <bool> Handle(RegisterNewSalesLineCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.FromResult(false));
            }

            var SalesLine = new SalesLine(Guid.NewGuid(), message.IdSales, message.IdDisc, "", message.Quantity, message.PriceUnit, message.SalesPrice, message.Cashback);

            if (_SalesRepository.GetById(SalesLine.IdSales) != null)
            {
                Bus.RaiseEvent(new DomainNotification(message.MessageType, "Ordem de Venda não existe"));
                return(Task.FromResult(false));
            }

            if (_DiscMusicRepository.GetById(SalesLine.IdItem) != null)
            {
                Bus.RaiseEvent(new DomainNotification(message.MessageType, "O disco não existe"));
                return(Task.FromResult(false));
            }

            _SalesLineRepository.Add(SalesLine);

            if (Commit())
            {
                Bus.RaiseEvent(new SalesLineRegisteredEvent(SalesLine.Id, SalesLine.IdSales, SalesLine.IdItem, SalesLine.Quantity, SalesLine.PriceUnit, SalesLine.SalesPrice, SalesLine.Cashback));
            }

            return(Task.FromResult(false));
        }
Exemplo n.º 2
0
        public Task <bool> Handle(RegisterNewSalesCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.FromResult(false));
            }

            var Sales = new Sales(Guid.NewGuid(), message.SalesDate, message.TotalAmount, message.TotalCashback);

            decimal totalAmount   = 0;
            decimal totalCashback = 0;

            Sales.Lines = new List <SalesLine>();

            foreach (var item in message.SalesLines)
            {
                var disc = _discMusicRepository.GetById(item.IdDisc);
                if (disc == null)
                {
                    NotifyValidationErrors(message);
                    return(Task.FromResult(false));
                }
                var cashback = _cashbackRepository.GetByWeekDay(disc.IdGender, Sales.SalesDate);

                var salesPrice     = item.Quantity * disc.Price;
                var cashbackAmount = salesPrice * cashback.Percent;

                totalAmount   += salesPrice;
                totalCashback += cashbackAmount;

                Sales.Lines.Add(new SalesLine(Guid.NewGuid(), Sales.Id, item.IdDisc, disc.Name, item.Quantity, disc.Price, salesPrice, cashbackAmount));
            }

            Sales.TotalAmount   = totalAmount;
            Sales.TotalCashback = totalCashback;

            _salesRepository.Add(Sales);

            if (Commit())
            {
                Bus.RaiseEvent(new SalesRegisteredEvent(
                                   Sales.Id,
                                   Sales.SalesDate,
                                   Sales.TotalAmount,
                                   Sales.TotalCashback,
                                   Sales.Lines.ToList().ConvertAll(x =>
                                                                   new SalesLineRegisteredEvent(
                                                                       x.Id,
                                                                       x.IdSales,
                                                                       x.IdItem,
                                                                       x.Quantity,
                                                                       x.PriceUnit,
                                                                       x.SalesPrice,
                                                                       x.Cashback))));
            }

            return(Task.FromResult(false));
        }
Exemplo n.º 3
0
 public DiscMusicViewModel GetById(Guid id)
 {
     return(_mapper.Map <DiscMusicViewModel>(_repository.GetById(id)));
 }