Ejemplo n.º 1
0
        public async Task Delete(Guid userId, Guid id)
        {
            var sale = await _saleRepository.GetById(id, x => x.Include(s => s.Details));

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

            foreach (var detail in sale.Details)
            {
                var product = await _productRepository.GetById(detail.ProductId);

                if (product == null)
                {
                    product = (await _productRepository.FindDeleted(x => x.Id == detail.ProductId)).FirstOrDefault();
                }
                if (product == null)
                {
                    throw new KeyNotFoundException($"Product with id: {detail.ProductId} not found.");
                }

                product.Stock += detail.Quantity;

                await _productRepository.Update(product);

                await _detailRepository.Delete(userId, detail.Id);
            }

            Payment payment = null;

            switch (sale.PaymentType)
            {
            case Util.Enums.ePaymentTypes.Cash:
                payment = await _cashRepository.Delete(userId, sale.PaymentId);

                break;

            case Util.Enums.ePaymentTypes.OwnFees:
                payment = await _ownFeesRepository.Delete(userId, sale.PaymentId);

                List <Fee> fees = await _feeRepository.Find(x => x.OwnFeesId == payment.Id);

                foreach (var fee in fees)
                {
                    await _feeRepository.Delete(userId, fee.Id);
                }
                break;

            case Util.Enums.ePaymentTypes.CreditCard:
                payment = await _creditCardRepository.Delete(userId, sale.PaymentId);

                break;

            case Util.Enums.ePaymentTypes.DebitCard:
                payment = await _debitCardRepository.Delete(userId, sale.PaymentId);

                break;

            case Util.Enums.ePaymentTypes.Cheques:
                payment = await _chequesPaymentRepository.Delete(userId, sale.PaymentId);

                List <Cheque> cheques = await _chequeRepository.Find(x => x.ChequesPaymentId == payment.Id);

                foreach (var cheque in cheques)
                {
                    await _chequeRepository.Delete(userId, cheque.Id);
                }
                break;

            default:
                break;
            }

            if (payment == null)
            {
                throw new KeyNotFoundException($"Payment with id: {payment.Id} not found.");
            }

            await _saleRepository.Delete(userId, id);

            await _saleRepository.CommitAsync();
        }