Search in sources :

Example 31 with Account

use of com.axelor.apps.account.db.Account 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);
}
Also used : Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) AccountCustomerService(com.axelor.apps.account.service.AccountCustomerService) BankDetails(com.axelor.apps.base.db.BankDetails) Journal(com.axelor.apps.account.db.Journal) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) PayVoucherElementToPay(com.axelor.apps.account.db.PayVoucherElementToPay) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) Partner(com.axelor.apps.base.db.Partner) PaymentMode(com.axelor.apps.account.db.PaymentMode) Reconcile(com.axelor.apps.account.db.Reconcile) Transactional(com.google.inject.persist.Transactional)

Example 32 with Account

use of com.axelor.apps.account.db.Account in project axelor-open-suite by axelor.

the class PaymentVoucherConfirmService method confirmPaymentVoucher.

/**
 * Confirms the payment voucher if the selected lines PiToPay 2nd O2M belongs to different
 * companies -> error I - Payment with an amount If we pay a classical moveLine (invoice, reject
 * ..) -> just create a payment If we pay a schedule 2 payments are created 1st reconciled with
 * the invoice and the second reconciled with the schedule II - Payment with an excess Payment If
 * we pay a moveLine having the same account, we just reconcile If we pay a with different account
 * -> 1- switch money to the good account 2- reconcile then
 *
 * @param paymentVoucher
 */
@Transactional(rollbackOn = { Exception.class })
public void confirmPaymentVoucher(PaymentVoucher paymentVoucher) throws AxelorException {
    log.debug("In confirmPaymentVoucherService ....");
    paymentVoucherSequenceService.setReference(paymentVoucher);
    PaymentMode paymentMode = paymentVoucher.getPaymentMode();
    Company company = paymentVoucher.getCompany();
    BankDetails companyBankDetails = paymentVoucher.getCompanyBankDetails();
    Journal journal = paymentModeService.getPaymentModeJournal(paymentMode, company, companyBankDetails);
    Account paymentModeAccount = paymentModeService.getPaymentModeAccount(paymentMode, company, companyBankDetails);
    paymentVoucherControlService.checkPaymentVoucherField(paymentVoucher, company, paymentModeAccount, journal);
    if (paymentVoucher.getRemainingAmount().compareTo(BigDecimal.ZERO) > 0 && !journal.getExcessPaymentOk()) {
        throw new AxelorException(paymentVoucher, TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PAYMENT_AMOUNT_EXCEEDING), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION));
    }
    // TODO VEIRIFER QUE LES ELEMENTS A PAYER NE CONCERNE QU'UNE SEULE DEVISE
    // TODO RECUPERER DEVISE DE LA PREMIERE DETTE
    // Currency currencyToPay = null;
    AppAccountService appAccountService = Beans.get(AppAccountService.class);
    if (appAccountService.getAppAccount().getPaymentVouchersOnInvoice() && paymentVoucher.getPaymentMode().getValidatePaymentByDepositSlipPublication()) {
        waitForDepositSlip(paymentVoucher);
    } else {
        createMoveAndConfirm(paymentVoucher);
    }
    paymentVoucherSequenceService.setReceiptNo(paymentVoucher, company, journal);
    paymentVoucherRepository.save(paymentVoucher);
}
Also used : Account(com.axelor.apps.account.db.Account) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) BankDetails(com.axelor.apps.base.db.BankDetails) Journal(com.axelor.apps.account.db.Journal) PaymentMode(com.axelor.apps.account.db.PaymentMode) AppAccountService(com.axelor.apps.account.service.app.AppAccountService) Transactional(com.google.inject.persist.Transactional)

Example 33 with Account

use of com.axelor.apps.account.db.Account in project axelor-open-suite by axelor.

the class InvoiceLineController method getAccount.

public void getAccount(ActionRequest request, ActionResponse response) {
    try {
        InvoiceLine invoiceLine = request.getContext().asType(InvoiceLine.class);
        if (invoiceLine != null) {
            Product product = invoiceLine.getProduct();
            Invoice invoice = this.getInvoice(request.getContext());
            if (product != null) {
                Account account = Beans.get(AccountManagementServiceAccountImpl.class).getProductAccount(product, invoice.getCompany(), invoice.getPartner().getFiscalPosition(), InvoiceToolService.isPurchase(invoice), invoiceLine.getFixedAssets());
                response.setValue("account", account);
            }
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Account(com.axelor.apps.account.db.Account) AccountManagementServiceAccountImpl(com.axelor.apps.account.service.AccountManagementServiceAccountImpl) Invoice(com.axelor.apps.account.db.Invoice) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) Product(com.axelor.apps.base.db.Product) AxelorException(com.axelor.exception.AxelorException)

Example 34 with Account

use of com.axelor.apps.account.db.Account in project axelor-open-suite by axelor.

the class MoveValidateService method freezeAccountAndPartnerFieldsOnMoveLines.

/**
 * Method that freeze the account and partner fields on move lines
 *
 * @param move
 */
public void freezeAccountAndPartnerFieldsOnMoveLines(Move move) {
    for (MoveLine moveLine : move.getMoveLineList()) {
        Account account = moveLine.getAccount();
        moveLine.setAccountId(account.getId());
        moveLine.setAccountCode(account.getCode());
        moveLine.setAccountName(account.getName());
        Partner partner = moveLine.getPartner();
        if (partner != null) {
            moveLine.setPartnerId(partner.getId());
            moveLine.setPartnerFullName(partner.getFullName());
            moveLine.setPartnerSeq(partner.getPartnerSeq());
        }
        if (moveLine.getTaxLine() != null) {
            moveLine.setTaxRate(moveLine.getTaxLine().getValue());
            moveLine.setTaxCode(moveLine.getTaxLine().getTax().getCode());
        }
    }
}
Also used : Account(com.axelor.apps.account.db.Account) MoveLine(com.axelor.apps.account.db.MoveLine) Partner(com.axelor.apps.base.db.Partner)

Example 35 with Account

use of com.axelor.apps.account.db.Account 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;
}
Also used : Account(com.axelor.apps.account.db.Account) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) MoveLine(com.axelor.apps.account.db.MoveLine) Map(java.util.Map) BigDecimal(java.math.BigDecimal) Reconcile(com.axelor.apps.account.db.Reconcile)

Aggregations

Account (com.axelor.apps.account.db.Account)59 MoveLine (com.axelor.apps.account.db.MoveLine)27 Company (com.axelor.apps.base.db.Company)26 BigDecimal (java.math.BigDecimal)26 Partner (com.axelor.apps.base.db.Partner)25 Move (com.axelor.apps.account.db.Move)21 AxelorException (com.axelor.exception.AxelorException)20 Journal (com.axelor.apps.account.db.Journal)18 Transactional (com.google.inject.persist.Transactional)18 AccountConfig (com.axelor.apps.account.db.AccountConfig)14 LocalDate (java.time.LocalDate)12 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)10 ArrayList (java.util.ArrayList)10 TaxLine (com.axelor.apps.account.db.TaxLine)8 Invoice (com.axelor.apps.account.db.Invoice)7 Reconcile (com.axelor.apps.account.db.Reconcile)7 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)6 BankDetails (com.axelor.apps.base.db.BankDetails)6 PaymentMode (com.axelor.apps.account.db.PaymentMode)5 Product (com.axelor.apps.base.db.Product)5