public async Task <IActionResult> CreateBillReceives([FromBody] CreateBillReceivesDto createBillReceivesDto) { try { var result = await(_service as IBillReceiveService).CreateBillReceives(createBillReceivesDto); return(Ok(result)); } catch (NotSupportedException e) { return(SendBadRequest(e.Message.Split('|'))); } catch (Exception e) { return(SendBadRequest(e)); } }
public async Task <List <BillReceive> > CreateBillReceives(CreateBillReceivesDto createBillReceivesDto) { decimal sumOriginalValue = 0; string errors = ""; if (createBillReceivesDto.PurchaseDate < new DateTime(1900, 01, 01)) { errors = $"A data da venda não pode ser inferior a 01/01/1900"; } foreach (var billReceive in createBillReceivesDto.BillReceives) { sumOriginalValue += billReceive.OriginalValue; if (billReceive.DueDate < createBillReceivesDto.PurchaseDate) { if (errors != "") { errors += $"|A data do vencimento {billReceive.DueDate.ToString("dd/MM/yyyy")} da parcela {billReceive.Quota} é menor que a data da compra {createBillReceivesDto.PurchaseDate.ToString("dd/MM/yyyy")}"; } else { errors = $"A data do vencimento {billReceive.DueDate.ToString("dd/MM/yyyy")} da parcela {billReceive.Quota} é menor que a data da compra {createBillReceivesDto.PurchaseDate.ToString("dd/MM/yyyy")}"; } } } if (sumOriginalValue != createBillReceivesDto.OriginalValue) { if (errors != "") { errors += $"|A soma das parcelas (R$ {sumOriginalValue}) difere do valor do título (R$ {createBillReceivesDto.OriginalValue})"; } else { errors = $"A soma das parcelas (R$ {sumOriginalValue}) difere do valor do título (R$ {createBillReceivesDto.OriginalValue})"; } } if (errors != "") { throw new NotSupportedException(errors); } int nextCode = await(_repository as IBillReceiveRepository).NextCode(); foreach (var billReceive in createBillReceivesDto.BillReceives) { billReceive.ClientId = createBillReceivesDto.ClientId; billReceive.PurchaseDate = createBillReceivesDto.PurchaseDate; billReceive.Code = nextCode; billReceive.Vendor = createBillReceivesDto.Vendor; var ret = await(_repository as IBillReceiveRepository).AddAsync(billReceive); if (!string.IsNullOrWhiteSpace(ret)) { throw new NotSupportedException(ret); } } return(createBillReceivesDto.BillReceives); }