public void Withdraw(string cardNumber, decimal amount) { var card = _cardRepository.Get(cardNumber); var feeAmount = _withdrawalFeeCalculator.Calculate(amount); var amountTotal = amount + feeAmount; if (card.Balance - amountTotal < 0) { throw new InsufficientFundsException(); } card.Balance -= amountTotal; var fee = _feeFactory.Create(cardNumber, feeAmount); _feeRepository.Add(fee); }
public async Task <SaleDto> Create(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, 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, Quantity = detailFC.Quantity, UnitPrice = price.Value, FeeRuleId = feeRule?.Id, CreatedBy = userId }; product.Stock -= detailFC.Quantity; detail = await _detailRepository.Add(detail); await _productRepository.Update(product); } 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 }; await _cashRepository.Add((Cash)payment); break; case Util.Enums.ePaymentTypes.OwnFees: var ownFeesDto = (OwnFeesForCreationDto)saletForCreationDto.OwnFees; payment = new OwnFees(ownFeesDto.ExpirationDate, total, ownFeesDto.Quantity, userId) { SaleId = sale.Id, }; payment = await _ownFeesRepository.Add((OwnFees)payment); foreach (var fee in ((OwnFees)payment).FeeList) { await _feeRepository.Add(fee); } 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 }; await _creditCardRepository.Add((CreditCard)payment); 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 }; await _debitCardRepository.Add((DebitCard)payment); 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 }; await _chequesPaymentRepository.Add((ChequesPayment)payment); foreach (var c in chequesDto.ListOfCheques) { var cheque = new Cheque() { ChequesPaymentId = payment.Id, Bank = c.Bank, Nro = c.Nro, Value = c.Value, CreatedBy = userId }; await _chequeRepository.Add(cheque); } break; default: break; } sale.PaymentId = payment.Id; sale = await _saleRepository.Add(sale); await _saleRepository.CommitAsync(); return(_mapper.Map <Sale, SaleDto>(sale)); } catch (Exception e) { throw e; } }