use of com.axelor.apps.account.db.PayVoucherElementToPay in project axelor-open-suite by axelor.
the class PaymentVoucherConfirmService method createMoveAndConfirm.
/**
* Confirm payment voucher and create move.
*
* @param paymentVoucher
* @throws AxelorException
*/
@Transactional(rollbackOn = { Exception.class })
public void createMoveAndConfirm(PaymentVoucher paymentVoucher) throws AxelorException {
Partner payerPartner = paymentVoucher.getPartner();
PaymentMode paymentMode = paymentVoucher.getPaymentMode();
Company company = paymentVoucher.getCompany();
BankDetails companyBankDetails = paymentVoucher.getCompanyBankDetails();
Journal journal = paymentModeService.getPaymentModeJournal(paymentMode, company, companyBankDetails);
LocalDate paymentDate = paymentVoucher.getPaymentDate();
boolean scheduleToBePaid = false;
Account paymentModeAccount = paymentModeService.getPaymentModeAccount(paymentMode, company, companyBankDetails);
// If paid by a moveline check if all the lines selected have the same account + company
// Excess payment
boolean allRight = paymentVoucherControlService.checkIfSameAccount(paymentVoucher.getPayVoucherElementToPayList(), paymentVoucher.getMoveLine());
// Check if allright=true (means companies and accounts in lines are all the same and same as in
// move line selected for paying
log.debug("allRight : {}", allRight);
if (allRight) {
scheduleToBePaid = this.toPayWithExcessPayment(paymentVoucher.getPayVoucherElementToPayList(), paymentVoucher.getMoveLine(), scheduleToBePaid, paymentDate);
}
if (paymentVoucher.getMoveLine() == null || (paymentVoucher.getMoveLine() != null && !allRight) || (scheduleToBePaid && !allRight && paymentVoucher.getMoveLine() != null)) {
// Manage all the cases in the same way. As if a move line (Excess payment) is selected, we
// cancel it first
Move move = moveService.getMoveCreateService().createMoveWithPaymentVoucher(journal, company, paymentVoucher, payerPartner, paymentDate, paymentMode, MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PAYMENT);
move.setPaymentVoucher(paymentVoucher);
move.setTradingName(paymentVoucher.getTradingName());
paymentVoucher.setGeneratedMove(move);
// Create move lines for payment lines
BigDecimal paidLineTotal = BigDecimal.ZERO;
int moveLineNo = 1;
boolean isDebitToPay = paymentVoucherToolService.isDebitToPay(paymentVoucher);
for (PayVoucherElementToPay payVoucherElementToPay : this.getPayVoucherElementToPayList(paymentVoucher)) {
MoveLine moveLineToPay = payVoucherElementToPay.getMoveLine();
log.debug("PV moveLineToPay debit : {}", moveLineToPay.getDebit());
log.debug("PV moveLineToPay amountPaid : {}", moveLineToPay.getAmountPaid());
BigDecimal amountToPay = payVoucherElementToPay.getAmountToPayCurrency();
if (amountToPay.compareTo(BigDecimal.ZERO) > 0) {
paidLineTotal = paidLineTotal.add(amountToPay);
this.payMoveLine(move, moveLineNo++, payerPartner, moveLineToPay, amountToPay, payVoucherElementToPay, isDebitToPay, paymentDate);
}
}
// Create move line for the payment amount
MoveLine moveLine = null;
// in the else case we create a classical balance on the bank account of the payment mode
if (paymentVoucher.getMoveLine() != null) {
moveLine = moveLineService.createMoveLine(move, paymentVoucher.getPartner(), paymentVoucher.getMoveLine().getAccount(), paymentVoucher.getPaidAmount(), isDebitToPay, paymentDate, moveLineNo++, paymentVoucher.getRef(), null);
Reconcile reconcile = reconcileService.createReconcile(moveLine, paymentVoucher.getMoveLine(), moveLine.getDebit(), !isDebitToPay);
if (reconcile != null) {
reconcileService.confirmReconcile(reconcile, true);
}
} else {
moveLine = moveLineService.createMoveLine(move, payerPartner, paymentModeAccount, paymentVoucher.getPaidAmount(), isDebitToPay, paymentDate, moveLineNo++, paymentVoucher.getRef(), null);
}
move.getMoveLineList().add(moveLine);
// Then Use Excess payment on old invoices / moveLines
if (paymentVoucher.getPaidAmount().compareTo(paidLineTotal) > 0) {
BigDecimal remainingPaidAmount = paymentVoucher.getRemainingAmount();
// TODO rajouter le process d'imputation automatique
// if(paymentVoucher.getHasAutoInput()) {
//
// List<MoveLine> debitMoveLines =
// Lists.newArrayList(pas.getDebitLinesToPay(contractLine,
// paymentVoucher.getPaymentScheduleToPay()));
// pas.createExcessPaymentWithAmount(debitMoveLines, remainingPaidAmount,
// move, moveLineNo,
// paymentVoucher.getPayerPartner(), company, contractLine, null,
// paymentDate, updateCustomerAccount);
// }
// else {
Account partnerAccount = Beans.get(AccountCustomerService.class).getPartnerAccount(payerPartner, company, paymentVoucherToolService.isPurchase(paymentVoucher));
moveLine = moveLineService.createMoveLine(move, paymentVoucher.getPartner(), partnerAccount, remainingPaidAmount, !isDebitToPay, paymentDate, moveLineNo++, paymentVoucher.getRef(), null);
move.getMoveLineList().add(moveLine);
if (isDebitToPay) {
reconcileService.balanceCredit(moveLine);
}
}
moveService.getMoveValidateService().validate(move);
paymentVoucher.setGeneratedMove(move);
}
paymentVoucher.setStatusSelect(PaymentVoucherRepository.STATUS_CONFIRMED);
deleteUnPaidLines(paymentVoucher);
}
use of com.axelor.apps.account.db.PayVoucherElementToPay in project axelor-open-suite by axelor.
the class PaymentVoucherConfirmService method toPayWithExcessPayment.
/**
* If paid by a moveline check if all the lines selected have the same account + company Excess
* payment Check if allright=true (means companies and accounts in lines are all the same and same
* as in move line selected for paying
*
* @param payVoucherElementToPayList Liste des paiement a réaliser
* @param creditMoveLine Le trop-perçu
* @param scheduleToBePaid
* @return Une échéance doit-elle être payée?
* @throws AxelorException
*/
public boolean toPayWithExcessPayment(List<PayVoucherElementToPay> payVoucherElementToPayList, MoveLine creditMoveLine, boolean scheduleToBePaid, LocalDate paymentDate) throws AxelorException {
boolean scheduleToBePaid2 = scheduleToBePaid;
List<MoveLine> debitMoveLines = new ArrayList<MoveLine>();
for (PayVoucherElementToPay payVoucherElementToPay : payVoucherElementToPayList) {
debitMoveLines.add(payVoucherElementToPay.getMoveLine());
}
List<MoveLine> creditMoveLines = new ArrayList<MoveLine>();
creditMoveLines.add(creditMoveLine);
paymentService.useExcessPaymentOnMoveLines(debitMoveLines, creditMoveLines);
return scheduleToBePaid2;
}
use of com.axelor.apps.account.db.PayVoucherElementToPay in project axelor-open-suite by axelor.
the class PaymentVoucherLoadService method createPayVoucherElementToPay.
public PayVoucherElementToPay createPayVoucherElementToPay(PaymentVoucher paymentVoucher, PayVoucherDueElement payVoucherDueElement, int sequence) throws AxelorException {
BigDecimal amountRemaining = paymentVoucher.getRemainingAmount();
LocalDate paymentDate = paymentVoucher.getPaymentDate();
PayVoucherElementToPay payVoucherElementToPay = new PayVoucherElementToPay();
payVoucherElementToPay.setSequence(sequence);
payVoucherElementToPay.setMoveLine(payVoucherDueElement.getMoveLine());
payVoucherElementToPay.setTotalAmount(payVoucherDueElement.getDueAmount());
payVoucherElementToPay.setRemainingAmount(payVoucherDueElement.getAmountRemaining());
payVoucherElementToPay.setCurrency(payVoucherDueElement.getCurrency());
BigDecimal amountRemainingInElementCurrency = currencyService.getAmountCurrencyConvertedAtDate(paymentVoucher.getCurrency(), payVoucherElementToPay.getCurrency(), amountRemaining, paymentDate).setScale(2, RoundingMode.HALF_UP);
BigDecimal amountImputedInElementCurrency = amountRemainingInElementCurrency.min(payVoucherElementToPay.getRemainingAmount());
BigDecimal amountImputedInPayVouchCurrency = currencyService.getAmountCurrencyConvertedAtDate(payVoucherElementToPay.getCurrency(), paymentVoucher.getCurrency(), amountImputedInElementCurrency, paymentDate).setScale(2, RoundingMode.HALF_UP);
payVoucherElementToPay.setAmountToPay(amountImputedInElementCurrency);
payVoucherElementToPay.setAmountToPayCurrency(amountImputedInPayVouchCurrency);
payVoucherElementToPay.setRemainingAmountAfterPayment(payVoucherElementToPay.getRemainingAmount().subtract(amountImputedInElementCurrency));
return payVoucherElementToPay;
}
use of com.axelor.apps.account.db.PayVoucherElementToPay in project axelor-open-suite by axelor.
the class PayVoucherElementToPayController method updateAmountToPayCurrency.
public void updateAmountToPayCurrency(ActionRequest request, ActionResponse response) {
try {
PayVoucherElementToPay elementToPayContext = request.getContext().asType(PayVoucherElementToPay.class);
PayVoucherElementToPay elementToPay = Beans.get(PayVoucherElementToPayRepository.class).find(elementToPayContext.getId());
Beans.get(PayVoucherElementToPayService.class).updateAmountToPayCurrency(elementToPay);
response.setReload(true);
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
Aggregations