use of com.axelor.apps.account.db.Reconcile in project axelor-open-suite by axelor.
the class PaymentVoucherConfirmService method payMoveLine.
/**
* @param paymentMove
* @param moveLineSeq
* @param payerPartner
* @param moveLineToPay
* @param amountToPay
* @param payVoucherElementToPay
* @return
* @throws AxelorException
*/
public MoveLine payMoveLine(Move paymentMove, int moveLineSeq, Partner payerPartner, MoveLine moveLineToPay, BigDecimal amountToPay, PayVoucherElementToPay payVoucherElementToPay, boolean isDebitToPay, LocalDate paymentDate) throws AxelorException {
String invoiceName = "";
if (moveLineToPay.getMove().getInvoice() != null) {
invoiceName = moveLineToPay.getMove().getInvoice().getInvoiceId();
} else {
invoiceName = payVoucherElementToPay.getPaymentVoucher().getRef();
}
MoveLine moveLine = moveLineService.createMoveLine(paymentMove, payerPartner, moveLineToPay.getAccount(), amountToPay, !isDebitToPay, paymentDate, moveLineSeq, invoiceName, null);
paymentMove.addMoveLineListItem(moveLine);
payVoucherElementToPay.setMoveLineGenerated(moveLine);
BigDecimal amountInCompanyCurrency = moveLine.getDebit().add(moveLine.getCredit());
Reconcile reconcile = reconcileService.createReconcile(moveLineToPay, moveLine, amountInCompanyCurrency, true);
if (reconcile != null) {
log.debug("Reconcile : : : {}", reconcile);
reconcileService.confirmReconcile(reconcile, true);
}
return moveLine;
}
use of com.axelor.apps.account.db.Reconcile in project axelor-open-suite by axelor.
the class BatchBankPaymentServiceImpl method createBankOrders.
@Transactional(rollbackOn = { Exception.class })
protected void createBankOrders(Batch batch, Collection<PaymentScheduleLine> paymentScheduleLines) throws AxelorException, JAXBException, IOException, DatatypeConfigurationException {
for (PaymentScheduleLine paymentScheduleLine : paymentScheduleLines) {
PaymentSchedule paymentSchedule = paymentScheduleLine.getPaymentSchedule();
MoveLine creditMoveLine = paymentScheduleLine.getAdvanceMoveLine();
for (Invoice invoice : paymentSchedule.getInvoiceSet()) {
MoveLine debitMoveLine = moveService.getMoveLineService().getDebitCustomerMoveLine(invoice);
Reconcile reconcile = reconcileRepo.findByMoveLines(debitMoveLine, creditMoveLine);
if (reconcile == null) {
continue;
}
createBankOrders(batch, reconcile);
}
}
}
use of com.axelor.apps.account.db.Reconcile 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.Reconcile in project axelor-open-suite by axelor.
the class PaymentServiceImpl method useExcessPaymentWithAmountConsolidated.
@SuppressWarnings("unchecked")
@Override
public int useExcessPaymentWithAmountConsolidated(List<MoveLine> creditMoveLines, BigDecimal remainingPaidAmount, Move move, int moveLineNo, Partner partner, Company company, Account account, LocalDate date, LocalDate dueDate) throws AxelorException {
log.debug("In useExcessPaymentWithAmount");
int moveLineNo2 = moveLineNo;
BigDecimal remainingPaidAmount2 = remainingPaidAmount;
List<Reconcile> reconcileList = new ArrayList<Reconcile>();
int i = creditMoveLines.size();
if (i != 0) {
Query q = JPA.em().createQuery("select new map(ml.account, SUM(ml.amountRemaining)) FROM MoveLine as ml " + "WHERE ml in ?1 group by ml.account");
q.setParameter(1, creditMoveLines);
List<Map<Account, BigDecimal>> allMap = new ArrayList<Map<Account, BigDecimal>>();
allMap = q.getResultList();
for (Map<Account, BigDecimal> map : allMap) {
Account accountMap = (Account) map.values().toArray()[0];
BigDecimal amountMap = (BigDecimal) map.values().toArray()[1];
BigDecimal amountDebit = amountMap.min(remainingPaidAmount2);
if (amountDebit.compareTo(BigDecimal.ZERO) > 0) {
MoveLine debitMoveLine = moveLineService.createMoveLine(move, partner, accountMap, amountDebit, true, date, dueDate, moveLineNo2, null, null);
move.getMoveLineList().add(debitMoveLine);
moveLineNo2++;
for (MoveLine creditMoveLine : creditMoveLines) {
if (creditMoveLine.getAccount().equals(accountMap)) {
Reconcile reconcile = null;
i--;
// Afin de pouvoir arrêter si il n'y a plus rien à payer
if (amountDebit.compareTo(BigDecimal.ZERO) <= 0) {
break;
}
BigDecimal amountToPay = amountDebit.min(creditMoveLine.getAmountRemaining());
// Gestion du passage en 580
if (i == 0) {
reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amountToPay, true);
} else {
reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amountToPay, false);
}
if (reconcile != null) {
remainingPaidAmount2 = remainingPaidAmount2.subtract(amountToPay);
amountDebit = amountDebit.subtract(amountToPay);
reconcileList.add(reconcile);
}
}
}
}
}
for (Reconcile reconcile : reconcileList) {
reconcileService.confirmReconcile(reconcile, true);
}
}
// Si il y a un restant à payer, alors on crée un dû.
if (remainingPaidAmount2.compareTo(BigDecimal.ZERO) > 0) {
MoveLine debitmoveLine = moveLineService.createMoveLine(move, partner, account, remainingPaidAmount2, true, date, dueDate, moveLineNo2, null, null);
move.getMoveLineList().add(debitmoveLine);
moveLineNo2++;
}
log.debug("End useExcessPaymentWithAmount");
return moveLineNo2;
}
use of com.axelor.apps.account.db.Reconcile in project axelor-open-suite by axelor.
the class PaymentServiceImpl method createReconcile.
/**
* Private method called by useExcessPaymentOnMoveLines used to lighten it and to create a
* reconcile
*
* @param debitMoveLine
* @param creditMoveLine
* @param debitTotalRemaining
* @param creditTotalRemaining
* @throws AxelorException
*/
@Transactional
private void createReconcile(MoveLine debitMoveLine, MoveLine creditMoveLine, BigDecimal debitTotalRemaining, BigDecimal creditTotalRemaining) throws AxelorException {
BigDecimal amount;
Reconcile reconcile;
if (debitMoveLine.getMaxAmountToReconcile() != null && debitMoveLine.getMaxAmountToReconcile().compareTo(BigDecimal.ZERO) > 0) {
amount = debitMoveLine.getMaxAmountToReconcile().min(creditMoveLine.getAmountRemaining());
debitMoveLine.setMaxAmountToReconcile(null);
} else {
amount = creditMoveLine.getAmountRemaining().min(debitMoveLine.getAmountRemaining());
}
log.debug("amount : {}", amount);
log.debug("debitTotalRemaining : {}", debitTotalRemaining);
log.debug("creditTotalRemaining : {}", creditTotalRemaining);
BigDecimal nextDebitTotalRemaining = debitTotalRemaining.subtract(amount);
BigDecimal nextCreditTotalRemaining = creditTotalRemaining.subtract(amount);
// Gestion du passage en 580
if (nextDebitTotalRemaining.compareTo(BigDecimal.ZERO) <= 0 || nextCreditTotalRemaining.compareTo(BigDecimal.ZERO) <= 0) {
log.debug("last loop");
reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amount, true);
} else {
reconcile = reconcileService.createReconcile(debitMoveLine, creditMoveLine, amount, false);
}
if (reconcile != null) {
reconcileService.confirmReconcile(reconcile, true);
debitTotalRemaining = debitTotalRemaining.subtract(amount);
creditTotalRemaining = creditTotalRemaining.subtract(amount);
log.debug("Réconciliation : {}", reconcile);
}
}
Aggregations