public Task <CommandResult> Handle(AddChequeCommand command, CancellationToken cancellationToken) { if (!command.IsValid()) { NotifyCommandErrors(command); return(Response()); } if (_chequeRepository.CheckNumeroChequeExists(command.NumeroCheque, command.ClienteId, command.BancoId, command.Id)) { NotifyCommandError(RequestErrorsMessageConstants.ChequeAlreadyExists); return(Response()); } Banco banco = _bancoRepository.GetById(command.BancoId, loadRelatedData: false); Cliente cliente = _clienteRepository.GetById(command.ClienteId, loadRelatedData: false); Cheque cheque = new Cheque(banco, command.Agencia, command.Conta, command.NumeroCheque, cliente, command.DataEmissao, command.DataVencimento, command.Valor); _chequeRepository.Add(cheque); if (Commit()) { _mediator.Publish(new AddedChequeEvent()); } return(Response()); }
public HttpResponseMessage Post([FromBody] ChequeDto cheque) { if (cheque == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new BaseApiResponse { Code = InternalApiStatusCode.Error, Message = "Invalid cheque object" })); } if (string.IsNullOrEmpty(cheque.LastName) || string.IsNullOrEmpty(cheque.FirstName)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new BaseApiResponse { Code = InternalApiStatusCode.FailedRequestValidation, Message = "Invalid cheque object, name is not provided" })); } if (cheque.Amount <= 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new BaseApiResponse { Code = InternalApiStatusCode.FailedRequestValidation, Message = "Invalid cheque object, amount should be greater than zero" })); } var chequeResposne = new ChequeResponse(); try { var chequeResult = _chequeRepository.Add(cheque); chequeResposne.Cheque = chequeResult; chequeResposne.Code = InternalApiStatusCode.Success; return(Request.CreateResponse(HttpStatusCode.Created, chequeResposne)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
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; } }