Exemplo n.º 1
0
        public async Task <SaleDto> GetOne(Guid id)
        {
            var sale = await _saleRepository.GetById(id, x => x.Include(s => s.Client).Include(s => s.Payment).Include(s => s.Details).ThenInclude(d => d.Product));

            if (sale == null)
            {
                throw new KeyNotFoundException($"Sale with id: {id} not found.");
            }

            sale.Details = sale.Details.Where(x => !x.IsDeleted).ToList();

            var saleDto = _mapper.Map <Sale, SaleDto>(sale);

            var payment = sale.Payment;

            switch (saleDto.PaymentType)
            {
            case Util.Enums.ePaymentTypes.Cash:
                saleDto.Cash = _mapper.Map <Cash, CashDto>((Cash)payment);
                break;

            case Util.Enums.ePaymentTypes.OwnFees:
                OwnFees owF = (OwnFees)payment;
                owF.FeeList     = (await _feeRepository.GetAll()).Where(x => x.OwnFeesId == owF.Id).OrderBy(x => x.ExpirationDate).ToList();
                saleDto.OwnFees = _mapper.Map <OwnFees, OwnFeesDto>(owF);
                break;

            case Util.Enums.ePaymentTypes.CreditCard:
                saleDto.CreditCard = _mapper.Map <CreditCard, CreditCardDto>((CreditCard)payment);
                break;

            case Util.Enums.ePaymentTypes.DebitCard:
                saleDto.DebitCard = _mapper.Map <DebitCard, DebitCardDto>((DebitCard)payment);
                break;

            case Util.Enums.ePaymentTypes.Cheques:
                saleDto.Cheques = _mapper.Map <ChequesPayment, ChequesPaymentDto>((ChequesPayment)payment);
                saleDto.Cheques.ListOfCheques =
                    _mapper.Map <IEnumerable <Cheque>, IEnumerable <ChequeDto> >(
                        (await _chequeRepository.GetAll()).Where(x => x.ChequesPaymentId == saleDto.Cheques.Id)
                        ).ToList();
                break;

            default:
                break;
            }

            return(saleDto);
        }
Exemplo n.º 2
0
        //public async Task Update(Guid id, SaleDto saleDto)
        //{
        //    var newProduct = await _productRepository.GetById(saleDto.ProductId);
        //    if (newProduct == null)
        //        throw new KeyNotFoundException($"Product with id: {saleDto.ProductId} not found.");

        //    Client client = null;
        //    if (saleDto.ClientId.HasValue)
        //    {
        //        client = await _clientRepository.GetById(saleDto.ClientId.Value);
        //        if (client == null)
        //            throw new KeyNotFoundException($"Client with id: {saleDto.ClientId} not found.");
        //    }

        //    var sale = await _saleRepository.GetById(id);
        //    if (sale == null)
        //        throw new KeyNotFoundException($"Sale with id: {id} not found.");

        //    var price = (await _priceRepository.GetAll())
        //        .OrderByDescending(x => x.DateTime)
        //        .FirstOrDefault(x => x.ProductId == saleDto.ProductId && x.DateTime <= saleDto.Date && !x.IsDeleted);

        //    if (saleDto.ProductId != sale.ProductId)
        //    {
        //        var oldProduct = await _productRepository.GetById(sale.ProductId);
        //        if (oldProduct == null)
        //            throw new KeyNotFoundException($"Product with id: {sale.ProductId} not found.");

        //        oldProduct.Stock += sale.Quantity;
        //        newProduct.Stock -= saleDto.Quantity;
        //        await _productRepository.Update(oldProduct);
        //        await _productRepository.Update(newProduct);
        //    }
        //    else
        //    {
        //        newProduct.Stock += sale.Quantity - saleDto.Quantity;
        //        await _productRepository.Update(newProduct);
        //    }

        //    sale.ProductId = saleDto.ProductId;
        //    sale.ClientId = saleDto.ClientId;
        //    sale.ClientName = client != null ? client.Name + " " + client.Lastname : saleDto.ClientName;
        //    sale.Date = saleDto.Date.ToLocalTime();
        //    sale.Quantity = saleDto.Quantity;
        //    //sale.Amount = price.Value * saleDto.Quantity;
        //    sale.LastModificationBy = saleDto.LastModificationBy;

        //    await _saleRepository.Update(sale);
        //    await _productRepository.CommitAsync();
        //    await _saleRepository.CommitAsync();
        //}

        private async Task <IEnumerable <SaleDto> > SetSalePayments(IEnumerable <Sale> sales, IEnumerable <SaleDto> salesDto)
        {
            foreach (var s in salesDto)
            {
                var payment = sales.FirstOrDefault(x => x.Id == s.Id).Payment;
                switch (s.PaymentType)
                {
                case Util.Enums.ePaymentTypes.Cash:
                    s.Cash = _mapper.Map <Cash, CashDto>((Cash)payment);
                    break;

                case Util.Enums.ePaymentTypes.OwnFees:
                    OwnFees owF = (OwnFees)payment;
                    owF.FeeList = (await _feeRepository.GetAll()).Where(x => x.OwnFeesId == owF.Id).OrderBy(x => x.ExpirationDate).ToList();
                    s.OwnFees   = _mapper.Map <OwnFees, OwnFeesDto>(owF);
                    break;

                case Util.Enums.ePaymentTypes.CreditCard:
                    s.CreditCard = _mapper.Map <CreditCard, CreditCardDto>((CreditCard)payment);
                    break;

                case Util.Enums.ePaymentTypes.DebitCard:
                    s.DebitCard = _mapper.Map <DebitCard, DebitCardDto>((DebitCard)payment);
                    break;

                case Util.Enums.ePaymentTypes.Cheques:
                    s.Cheques = _mapper.Map <ChequesPayment, ChequesPaymentDto>((ChequesPayment)payment);
                    s.Cheques.ListOfCheques =
                        _mapper.Map <IEnumerable <Cheque>, IEnumerable <ChequeDto> >(
                            (await _chequeRepository.GetAll()).Where(x => x.ChequesPaymentId == s.Cheques.Id)
                            ).ToList();
                    break;

                default:
                    break;
                }
            }
            return(salesDto);
        }
Exemplo n.º 3
0
        public async Task <SaleDto> PreCreation(Guid userId, SaleForCreationDto saletForCreationDto)
        {
            try
            {
                Client client = null;
                if (saletForCreationDto.ClientId.HasValue)
                {
                    client = await _clientRepository.GetById(saletForCreationDto.ClientId.Value);

                    if (client == null)
                    {
                        throw new KeyNotFoundException($"Client with id: {saletForCreationDto.ClientId} not found.");
                    }
                }

                var sale = new Sale()
                {
                    ClientId    = saletForCreationDto.ClientId,
                    Client      = client,
                    ClientName  = client != null ? client.Name + " " + client.Lastname : saletForCreationDto.ClientName,
                    Date        = saletForCreationDto.Date.ToLocalTime(),
                    PaymentType = saletForCreationDto.PaymentType,
                    CreatedBy   = userId
                };

                decimal total = 0;
                foreach (var detailFC in saletForCreationDto.Details)
                {
                    var product = await _productRepository.GetById(detailFC.ProductId);

                    if (product == null)
                    {
                        throw new KeyNotFoundException($"Product with id: {detailFC.ProductId} not found.");
                    }

                    var price = (await _priceRepository.GetAll())
                                .OrderByDescending(x => x.DateTime)
                                .FirstOrDefault(
                        x => x.ProductId == detailFC.ProductId &&
                        x.DateTime.ToLocalTime() <= saletForCreationDto.Date.ToLocalTime() &&
                        x.PriceType == ePriceTypes.SalePrice &&
                        !x.IsDeleted
                        );

                    FeeRule feeRule = null;
                    if (sale.PaymentType == ePaymentTypes.OwnFees)
                    {
                        feeRule = (await _feeRuleRepository.Find(x => x.ProductId == detailFC.ProductId))
                                  .OrderByDescending(x => x.Date).ThenBy(x => x.FeesAmountTo)
                                  .FirstOrDefault(x => x.Date <= sale.Date && x.FeesAmountTo >= saletForCreationDto.OwnFees.Quantity);
                        if (feeRule == null)
                        {
                            throw new KeyNotFoundException($"Fee Rule not found.");
                        }

                        decimal percentage = feeRule.Percentage * saletForCreationDto.OwnFees.Quantity / 100;
                        total += price.Value * detailFC.Quantity * (1 + percentage);
                    }
                    else
                    {
                        total += price.Value * detailFC.Quantity;
                    }

                    var detail = new Detail()
                    {
                        SaleId    = sale.Id,
                        ProductId = detailFC.ProductId,
                        Product   = product,
                        Quantity  = detailFC.Quantity,
                        UnitPrice = price.Value,
                        FeeRuleId = feeRule?.Id,
                        CreatedBy = userId
                    };

                    sale.Details.Add(detail);
                }

                Payment payment = null;
                switch (saletForCreationDto.PaymentType)
                {
                case Util.Enums.ePaymentTypes.Cash:
                    var cashDto = (CashForCreationDto)saletForCreationDto.Cash;
                    payment = new Cash(total, cashDto.Discount)
                    {
                        SaleId    = sale.Id,
                        CreatedBy = userId
                    };
                    break;

                case Util.Enums.ePaymentTypes.OwnFees:
                    var ownFeesDto = (OwnFeesForCreationDto)saletForCreationDto.OwnFees;
                    payment = new OwnFees(ownFeesDto.ExpirationDate, total, ownFeesDto.Quantity, userId)
                    {
                        SaleId = sale.Id,
                    };
                    break;

                case Util.Enums.ePaymentTypes.CreditCard:
                    var creditCardDto = (CreditCardForCreationDto)saletForCreationDto.CreditCard;
                    payment = new CreditCard(total, creditCardDto.Discount, creditCardDto.Surcharge)
                    {
                        SaleId    = sale.Id,
                        CardType  = creditCardDto.CardType,
                        Bank      = creditCardDto.Bank,
                        CreatedBy = userId
                    };
                    break;

                case Util.Enums.ePaymentTypes.DebitCard:
                    var debitCardDto = (DebitCardForCreationDto)saletForCreationDto.DebitCard;
                    payment = new DebitCard(total, debitCardDto.Discount, debitCardDto.Surcharge)
                    {
                        SaleId    = sale.Id,
                        CardType  = debitCardDto.CardType,
                        Bank      = debitCardDto.Bank,
                        CreatedBy = userId
                    };
                    break;

                case Util.Enums.ePaymentTypes.Cheques:
                    var chequesDto = (ChequesPaymentForCreationDto)saletForCreationDto.Cheques;

                    if (chequesDto.ListOfCheques.Sum(x => x.Value) != total)
                    {
                        throw new InvalidOperationException("The sum of cheques list is different of amount.");
                    }

                    payment = new ChequesPayment()
                    {
                        SaleId    = sale.Id,
                        Amount    = Math.Ceiling(total * 100) / 100,
                        CreatedBy = userId
                    };

                    foreach (var c in chequesDto.ListOfCheques)
                    {
                        var cheque = new Cheque()
                        {
                            ChequesPaymentId = payment.Id,
                            Bank             = c.Bank,
                            Nro       = c.Nro,
                            Value     = c.Value,
                            CreatedBy = userId
                        };

                        ((ChequesPayment)payment).ListOfCheques.Add(cheque);
                    }
                    break;

                default:
                    break;
                }

                sale.PaymentId = payment.Id;
                sale.Payment   = payment;

                var saleDto = _mapper.Map <Sale, SaleDto>(sale);

                switch (saleDto.PaymentType)
                {
                case Util.Enums.ePaymentTypes.Cash:
                    saleDto.Cash = _mapper.Map <Cash, CashDto>((Cash)payment);
                    break;

                case Util.Enums.ePaymentTypes.OwnFees:
                    OwnFees owF = (OwnFees)payment;
                    saleDto.OwnFees = _mapper.Map <OwnFees, OwnFeesDto>(owF);
                    break;

                case Util.Enums.ePaymentTypes.CreditCard:
                    saleDto.CreditCard = _mapper.Map <CreditCard, CreditCardDto>((CreditCard)payment);
                    break;

                case Util.Enums.ePaymentTypes.DebitCard:
                    saleDto.DebitCard = _mapper.Map <DebitCard, DebitCardDto>((DebitCard)payment);
                    break;

                case Util.Enums.ePaymentTypes.Cheques:
                    saleDto.Cheques = _mapper.Map <ChequesPayment, ChequesPaymentDto>((ChequesPayment)payment);
                    break;

                default:
                    break;
                }

                return(saleDto);
            }
            catch (Exception e)
            {
                throw e;
            }
        }