use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.
the class InvoicePaymentCreateServiceImpl method createInvoicePayment.
/**
* @param invoice
* @param amount
* @param paymentDate
* @param paymentMove
* @return
*/
public InvoicePayment createInvoicePayment(Invoice invoice, BigDecimal amount, Move paymentMove) throws AxelorException {
LocalDate paymentDate = paymentMove.getDate();
BigDecimal amountConverted = currencyService.getAmountCurrencyConvertedAtDate(paymentMove.getCompanyCurrency(), paymentMove.getCurrency(), amount, paymentDate);
int typePaymentMove = this.determineType(paymentMove);
Currency currency = paymentMove.getCurrency();
if (currency == null) {
currency = paymentMove.getCompanyCurrency();
}
PaymentMode paymentMode;
InvoicePayment invoicePayment;
if (typePaymentMove == InvoicePaymentRepository.TYPE_REFUND_INVOICE || typePaymentMove == InvoicePaymentRepository.TYPE_INVOICE) {
paymentMode = null;
} else {
paymentMode = paymentMove.getPaymentMode();
}
invoicePayment = this.createInvoicePayment(invoice, amountConverted, paymentDate, currency, paymentMode, typePaymentMove);
invoicePayment.setMove(paymentMove);
invoicePayment.setStatusSelect(InvoicePaymentRepository.STATUS_VALIDATED);
PaymentVoucher paymentVoucher = paymentMove.getPaymentVoucher();
if (paymentVoucher != null) {
invoicePayment.setCompanyBankDetails(paymentVoucher.getCompanyBankDetails());
} else if (invoice.getSchedulePaymentOk() && invoice.getPaymentSchedule() != null) {
BankDetails companyBankDetails = invoice.getPaymentSchedule().getCompanyBankDetails();
invoicePayment.setCompanyBankDetails(companyBankDetails);
}
computeAdvancePaymentImputation(invoicePayment, paymentMove);
invoice.addInvoicePaymentListItem(invoicePayment);
invoicePaymentToolService.updateAmountPaid(invoice);
invoicePaymentRepository.save(invoicePayment);
return invoicePayment;
}
use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.
the class InvoicePaymentCreateServiceImpl method computeAdvancePaymentImputation.
protected void computeAdvancePaymentImputation(InvoicePayment invoicePayment, Move paymentMove) {
// check if the payment is an advance payment imputation
Invoice advanceInvoice = determineIfReconcileFromInvoice(paymentMove);
if (advanceInvoice != null) {
List<InvoicePayment> invoicePaymentList = advanceInvoice.getInvoicePaymentList();
if (invoicePaymentList != null && !invoicePaymentList.isEmpty()) {
// set right type
invoicePayment.setTypeSelect(InvoicePaymentRepository.TYPE_ADV_PAYMENT_IMPUTATION);
// create link between advance payment and its imputation
InvoicePayment advancePayment = advanceInvoice.getInvoicePaymentList().get(0);
advancePayment.setImputedBy(invoicePayment);
invoicePaymentRepository.save(advancePayment);
// set the imputed payment currency
invoicePayment.setCurrency(advancePayment.getCurrency());
BigDecimal currentImputedAmount = invoicePayment.getAmount();
// we force the payment amount to be equal to the advance
// invoice amount, so we get the right amount in the
// right currency.
BigDecimal totalAmountInAdvanceInvoice = advancePayment.getInvoice().getCompanyInTaxTotal();
BigDecimal convertedImputedAmount = currentImputedAmount.multiply(advancePayment.getAmount()).divide(totalAmountInAdvanceInvoice, 2, RoundingMode.HALF_UP);
invoicePayment.setAmount(convertedImputedAmount);
}
}
}
use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.
the class InvoicePaymentCreateServiceImpl method determineIfReconcileFromInvoice.
/**
* We try to get to the status of the invoice from the reconcile to see if this move was created
* from a payment for an advance payment invoice.
*
* @param move
* @return the found advance invoice if the move is from a payment that comes from this invoice.
* null in other cases
*/
protected Invoice determineIfReconcileFromInvoice(Move move) {
List<MoveLine> moveLineList = move.getMoveLineList();
if (moveLineList == null || moveLineList.size() != 2) {
return null;
}
InvoicePaymentRepository invoicePaymentRepo = Beans.get(InvoicePaymentRepository.class);
for (MoveLine moveLine : moveLineList) {
// search for the reconcile between the debit line
if (moveLine.getDebit().compareTo(BigDecimal.ZERO) > 0) {
Reconcile reconcile = Beans.get(ReconcileRepository.class).all().filter("self.debitMoveLine = ?", moveLine).fetchOne();
if (reconcile == null) {
return null;
}
// associated payment
if (reconcile.getCreditMoveLine() == null || reconcile.getCreditMoveLine().getMove() == null) {
continue;
}
Move candidatePaymentMove = reconcile.getCreditMoveLine().getMove();
InvoicePayment invoicePayment = invoicePaymentRepo.all().filter("self.move = :_move").bind("_move", candidatePaymentMove).fetchOne();
// payment, then return true.
if (invoicePayment != null && invoicePayment.getInvoice() != null && invoicePayment.getInvoice().getOperationSubTypeSelect() == InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE) {
return invoicePayment.getInvoice();
}
}
}
return null;
}
use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.
the class InvoicePaymentCreateServiceImpl method createInvoicePayment.
@Transactional
public InvoicePayment createInvoicePayment(Invoice invoice, PaymentMode paymentMode, BankDetails companyBankDetails, LocalDate paymentDate, LocalDate bankDepositDate, String chequeNumber) {
InvoicePayment invoicePayment = createInvoicePayment(invoice, invoice.getInTaxTotal().subtract(invoice.getAmountPaid()), paymentDate, invoice.getCurrency(), paymentMode, InvoicePaymentRepository.TYPE_PAYMENT);
invoicePayment.setCompanyBankDetails(companyBankDetails);
invoicePayment.setBankDepositDate(bankDepositDate);
invoicePayment.setChequeNumber(chequeNumber);
return invoicePaymentRepository.save(invoicePayment);
}
use of com.axelor.apps.account.db.InvoicePayment in project axelor-open-suite by axelor.
the class InvoicePaymentToolServiceImpl method getCreditMoveLinesFromPayments.
@Override
public List<MoveLine> getCreditMoveLinesFromPayments(List<InvoicePayment> payments) {
List<MoveLine> moveLines = new ArrayList<>();
for (InvoicePayment payment : payments) {
Move move = payment.getMove();
if (move == null || move.getMoveLineList() == null || move.getMoveLineList().isEmpty()) {
continue;
}
moveLines.addAll(moveToolService.getToReconcileCreditMoveLines(move));
}
return moveLines;
}
Aggregations