payment.setStatus(PaymentStatus.ANULADO);
List<InvoicePayment> invoicePayments = invoiceManager.findInvoiceByPayment(payment);
for (InvoicePayment detail : invoicePayments) {
Invoice invoice = detail.getInvoice();
Float totalAmountPaid = 0F;
if (payment.getCurrency() != null && payment.getCurrency().isLocal()) {
totalAmountPaid = invoice.getLocalAmountPaid() - detail.getLocalAmount();
invoice.setLocalAmountPaid(totalAmountPaid);
invoice.setAmountPaid(totalAmountPaid);
// Balance increase with anulation
Float companyLocalBalance = payment.getCompany().getBalanceLocal() + detail.getLocalAmount();
payment.getCompany().setBalanceLocal(companyLocalBalance);
Float foreignAmount = detail.getForeignAmount();
Float totalForeignAmount = invoice.getForeignAmountPaid() - foreignAmount;
invoice.setForeignAmountPaid(totalForeignAmount);
// Balance increase with anulation
Float companyBalance = payment.getCompany().getBalance() + foreignAmount;
payment.getCompany().setBalance(companyBalance);
Float pendingBalance = invoice.getLocalAmount() - (invoice.getLocalAmountPaid() != null ? invoice.getLocalAmountPaid() : 0f);
if (pendingBalance == 0f) {
invoice.setStatus(DocumentStatus.PAGADA);
} else if (pendingBalance.equals(invoice.getLocalAmount())) {
invoice.setStatus(DocumentStatus.ACTIVA);
} else if (pendingBalance > 0f) {
invoice.setStatus(DocumentStatus.PAGADA_PARCIAL);
}
} else if (payment.getCurrency() != null && !payment.getCurrency().isLocal()) {
totalAmountPaid = invoice.getForeignAmountPaid() - detail.getForeignAmount();
invoice.setForeignAmountPaid(totalAmountPaid);
invoice.setAmountPaid(totalAmountPaid);
// Balance increase with anulation
Float companyBalance = payment.getCompany().getBalance() + detail.getForeignAmount();
payment.getCompany().setBalance(companyBalance);
Float localAmount = detail.getLocalAmount();
Float totalLocalAmount = invoice.getLocalAmountPaid() - localAmount;
invoice.setLocalAmountPaid(totalLocalAmount);
// Balance increase with anulation
Float companyLocalBalance = payment.getCompany().getBalanceLocal() + localAmount;
payment.getCompany().setBalanceLocal(companyLocalBalance);
Float pendingBalance = invoice.getForeignAmount() - (invoice.getForeignAmountPaid() != null ? invoice.getForeignAmountPaid() : 0f);
if (pendingBalance == 0f) {
invoice.setStatus(DocumentStatus.PAGADA);
} else if (pendingBalance.equals(invoice.getForeignAmount())) {
invoice.setStatus(DocumentStatus.ACTIVA);
} else if (pendingBalance > 0f) {
invoice.setStatus(DocumentStatus.PAGADA_PARCIAL);
}
}
companyManager.save(payment.getCompany());
invoice.setCurrentPayment(null);
invoiceManager.save(invoice);
}
paymentManager.save(payment);
String key = "payment.updated";