Search in sources :

Example 1 with DebtRecovery

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

the class DebtRecoveryService method createDebtRecovery.

@Transactional(rollbackOn = { Exception.class })
public DebtRecovery createDebtRecovery(AccountingSituation accountingSituation, TradingName tradingName) {
    DebtRecovery debtRecovery = new DebtRecovery();
    if (tradingName != null) {
        debtRecovery.setTradingNameAccountingSituation(accountingSituation);
        if (accountingSituation.getTradingNameDebtRecoveryList() != null) {
            accountingSituation.getTradingNameDebtRecoveryList().add(debtRecovery);
        } else {
            List<DebtRecovery> tradingNameDebtRecoveryList = new ArrayList<DebtRecovery>();
            tradingNameDebtRecoveryList.add(debtRecovery);
            accountingSituation.setTradingNameDebtRecoveryList(tradingNameDebtRecoveryList);
        }
    } else {
        debtRecovery.setAccountingSituation(accountingSituation);
        accountingSituation.setDebtRecovery(debtRecovery);
    }
    debtRecoveryRepo.save(debtRecovery);
    return debtRecovery;
}
Also used : ArrayList(java.util.ArrayList) DebtRecovery(com.axelor.apps.account.db.DebtRecovery) Transactional(com.google.inject.persist.Transactional)

Example 2 with DebtRecovery

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

the class DebtRecoveryController method runDebtRecovery.

public void runDebtRecovery(ActionRequest request, ActionResponse response) {
    DebtRecovery debtRecovery = request.getContext().asType(DebtRecovery.class);
    debtRecovery = Beans.get(DebtRecoveryRepository.class).find(debtRecovery.getId());
    try {
        if (debtRecoveryService.getAccountingSituation(debtRecovery) == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.DEBT_RECOVERY_1));
        }
        debtRecovery.setDebtRecoveryMethodLine(debtRecovery.getWaitDebtRecoveryMethodLine());
        Beans.get(DebtRecoveryActionService.class).runManualAction(debtRecovery);
        response.setReload(true);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) DebtRecovery(com.axelor.apps.account.db.DebtRecovery) AxelorException(com.axelor.exception.AxelorException) DebtRecoveryActionService(com.axelor.apps.account.service.debtrecovery.DebtRecoveryActionService)

Example 3 with DebtRecovery

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

the class AccountingSituationController method openDebtRecovery.

/**
 * Open Debt Recovery record in form view
 *
 * @param request
 * @param response
 */
public void openDebtRecovery(ActionRequest request, ActionResponse response) {
    AccountingSituation accountingSituation = request.getContext().asType(AccountingSituation.class);
    DebtRecovery debtRecovery = accountingSituation.getDebtRecovery();
    if (debtRecovery != null) {
        response.setView(ActionView.define(I18n.get("Debt Recovery")).model(DebtRecovery.class.getName()).add("grid", "debt-recovery-grid").add("form", "debt-recovery-form").param("forceEdit", "true").context("_showRecord", debtRecovery.getId()).map());
        response.setCanClose(true);
    }
}
Also used : AccountingSituation(com.axelor.apps.account.db.AccountingSituation) DebtRecovery(com.axelor.apps.account.db.DebtRecovery)

Example 4 with DebtRecovery

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

the class BatchDebtRecovery method debtRecoveryPartner.

public void debtRecoveryPartner() {
    Company company = batch.getAccountingBatch().getCompany();
    Set<TradingName> tradingNameSet = // Get the trading names for which to operate the debt recovery process
    null;
    if (appBaseService.getAppBase().getEnableTradingNamesManagement() && batch.getAccountingBatch().getIsDebtRecoveryByTradingName()) {
        tradingNameSet = batch.getAccountingBatch().getTradingNameSet();
        if (tradingNameSet == null || tradingNameSet.isEmpty()) {
            tradingNameSet = company.getTradingNameSet();
        }
    }
    Query<Partner> query = partnerRepository.all().filter("self.isContact = false " + "AND :_company MEMBER OF self.companySet " + "AND self.accountingSituationList IS NOT EMPTY " + "AND self.isCustomer = true " + "AND self.id NOT IN (" + Beans.get(BlockingService.class).listOfBlockedPartner(company, BlockingRepository.REMINDER_BLOCKING) + ")").bind("_company", company).order("id");
    int offset = 0;
    List<Partner> partnerList;
    while (!(partnerList = query.fetch(FETCH_LIMIT, offset)).isEmpty()) {
        findBatch();
        for (Partner partner : partnerList) {
            ++offset;
            boolean remindedOk;
            // if recovery handled by trading name
            if (tradingNameSet != null && !tradingNameSet.isEmpty()) {
                boolean incrementPartner = false;
                for (TradingName tradingName : tradingNameSet) {
                    try {
                        remindedOk = debtRecoveryService.debtRecoveryGenerate(partner, company, tradingName);
                        if (remindedOk) {
                            DebtRecovery debtRecovery = debtRecoveryService.getDebtRecovery(partner, company, tradingName);
                            addBatchToModel(debtRecovery);
                            incrementPartner = true;
                        }
                    // Catching exceptions
                    } catch (AxelorException e) {
                        TraceBackService.trace(new AxelorException(e, e.getCategory(), I18n.get("Partner") + " %s, " + I18n.get("Trading name") + " %s", partner.getName(), tradingName.getName()), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
                        incrementAnomaly(partner);
                        break;
                    } catch (Exception e) {
                        TraceBackService.trace(new Exception(String.format(I18n.get("Partner") + " %s, " + I18n.get("Trading name") + " %s", partner.getName(), tradingName.getName()), e), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
                        incrementAnomaly(partner);
                        break;
                    }
                // \Catching exceptions
                }
                if (incrementPartner) {
                    incrementDone(partner);
                }
            } else {
                // if recovery handled by company
                try {
                    remindedOk = debtRecoveryService.debtRecoveryGenerate(partner, company, null);
                    if (remindedOk) {
                        DebtRecovery debtRecovery = debtRecoveryService.getDebtRecovery(partner, company);
                        addBatchToModel(debtRecovery);
                        incrementDone(partner);
                    }
                // Catching exceptions
                } catch (AxelorException e) {
                    TraceBackService.trace(new AxelorException(e, e.getCategory(), I18n.get("Partner") + " %s", partner.getName()), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
                    incrementAnomaly(partner);
                    break;
                } catch (Exception e) {
                    TraceBackService.trace(new Exception(String.format(I18n.get("Partner") + " %s", partner.getName()), e), ExceptionOriginRepository.DEBT_RECOVERY, batch.getId());
                    incrementAnomaly(partner);
                    break;
                }
            // \Catching exceptions
            }
        }
        JPA.clear();
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) TradingName(com.axelor.apps.base.db.TradingName) DebtRecovery(com.axelor.apps.account.db.DebtRecovery) Partner(com.axelor.apps.base.db.Partner) AxelorException(com.axelor.exception.AxelorException)

Example 5 with DebtRecovery

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

the class DebtRecoveryService method debtRecoveryGenerate.

/**
 * Handle the debt recovery process for a partner and company. Can optionally specify a trading
 * name.
 *
 * @param partner The partner that has debts to be recovered
 * @param company The company for which to recover the debts
 * @param tradingName (optional) A trading name of the company for which to recover the debts
 * @throws AxelorException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws ClassNotFoundException
 * @throws IOException
 */
@Transactional(rollbackOn = { Exception.class })
public boolean debtRecoveryGenerate(Partner partner, Company company, TradingName tradingName) throws AxelorException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, JSONException {
    boolean remindedOk = false;
    DebtRecovery debtRecovery = this.getDebtRecovery(partner, company, // getDebtRecovery if one already exists
    tradingName);
    BigDecimal balanceDue = accountCustomerService.getBalanceDue(partner, company, tradingName);
    if (balanceDue.compareTo(BigDecimal.ZERO) > 0) {
        log.debug("balanceDue : {} ", balanceDue);
        BigDecimal balanceDueDebtRecovery = accountCustomerService.getBalanceDueDebtRecovery(partner, company, tradingName);
        if (balanceDueDebtRecovery.compareTo(BigDecimal.ZERO) > 0) {
            log.debug("balanceDueDebtRecovery : {} ", balanceDueDebtRecovery);
            remindedOk = true;
            if (debtRecovery == null) {
                AccountingSituationRepository accSituationRepo = Beans.get(AccountingSituationRepository.class);
                AccountingSituation accountingSituation = accSituationRepo.all().filter("self.partner = ?1 and self.company = ?2", partner, company).fetchOne();
                debtRecovery = this.createDebtRecovery(accountingSituation, tradingName);
            }
            debtRecovery.setCompany(companyRepo.find(company.getId()));
            if (tradingName != null)
                debtRecovery.setTradingName(tradingNameRepo.find(tradingName.getId()));
            debtRecovery.setCurrency(partner.getCurrency());
            debtRecovery.setBalanceDue(balanceDue);
            List<MoveLine> moveLineList = this.getMoveLineDebtRecovery(partner, company, tradingName);
            this.updateInvoiceDebtRecovery(debtRecovery, this.getInvoiceList(moveLineList));
            this.updatePaymentScheduleLineDebtRecovery(debtRecovery, this.getPaymentScheduleList(moveLineList, partner));
            debtRecovery.setBalanceDueDebtRecovery(balanceDueDebtRecovery);
            Integer levelDebtRecovery = -1;
            if (debtRecovery.getDebtRecoveryMethodLine() != null) {
                levelDebtRecovery = debtRecovery.getDebtRecoveryMethodLine().getSequence();
            }
            LocalDate referenceDate = this.getReferenceDate(debtRecovery);
            if (referenceDate != null) {
                log.debug("date de référence : {} ", referenceDate);
                debtRecovery.setReferenceDate(referenceDate);
            } else {
                throw new AxelorException(debtRecovery, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, "%s :\n" + I18n.get("Partner") + " %s, " + I18n.get("Company") + " %s : " + tradingName != null ? I18n.get("Trading name") + " %s : " : "" + I18n.get(IExceptionMessage.DEBT_RECOVERY_2), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION), partner.getName(), company.getName());
            }
            if (debtRecovery.getDebtRecoveryMethod() == null) {
                DebtRecoveryMethod debtRecoveryMethod = debtRecoverySessionService.getDebtRecoveryMethod(debtRecovery);
                if (debtRecoveryMethod != null) {
                    debtRecovery.setDebtRecoveryMethod(debtRecoveryMethod);
                    debtRecoverySessionService.debtRecoverySession(debtRecovery);
                } else {
                    throw new AxelorException(debtRecovery, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, "%s :\n" + I18n.get("Partner") + " %s, " + I18n.get("Company") + " %s : " + tradingName != null ? I18n.get("Trading name") + " %s : " : "" + I18n.get(IExceptionMessage.DEBT_RECOVERY_3), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION), partner.getName(), company.getName());
                }
            } else {
                debtRecoverySessionService.debtRecoverySession(debtRecovery);
            }
            if (debtRecovery.getWaitDebtRecoveryMethodLine() == null) {
                // Si le niveau de relance a évolué
                if (debtRecovery.getDebtRecoveryMethodLine() != null && debtRecovery.getDebtRecoveryMethodLine().getSequence() > levelDebtRecovery) {
                    debtRecoveryActionService.runAction(debtRecovery);
                    DebtRecoveryHistory debtRecoveryHistory = debtRecoveryActionService.getDebtRecoveryHistory(debtRecovery);
                    if (CollectionUtils.isEmpty(messageRepo.findByRelatedTo(Math.toIntExact(debtRecoveryHistory.getId()), DebtRecoveryHistory.class.getCanonicalName()).fetch())) {
                        debtRecoveryActionService.runMessage(debtRecovery);
                    }
                }
            } else {
                log.debug("Tiers {}, Société {} - Niveau de relance en attente ", partner.getName(), company.getName());
                // TODO Alarm ?
                TraceBackService.trace(new AxelorException(debtRecovery, TraceBackRepository.CATEGORY_INCONSISTENCY, "%s :\n" + I18n.get("Partner") + " %s, " + I18n.get("Company") + " %s : " + I18n.get(IExceptionMessage.DEBT_RECOVERY_4), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION), partner.getName(), company.getName()));
            }
        } else {
            debtRecoverySessionService.debtRecoveryInitialization(debtRecovery);
        }
    } else {
        debtRecoverySessionService.debtRecoveryInitialization(debtRecovery);
    }
    return remindedOk;
}
Also used : AxelorException(com.axelor.exception.AxelorException) DebtRecoveryHistory(com.axelor.apps.account.db.DebtRecoveryHistory) DebtRecoveryMethod(com.axelor.apps.account.db.DebtRecoveryMethod) AccountingSituationRepository(com.axelor.apps.account.db.repo.AccountingSituationRepository) AccountingSituation(com.axelor.apps.account.db.AccountingSituation) MoveLine(com.axelor.apps.account.db.MoveLine) DebtRecovery(com.axelor.apps.account.db.DebtRecovery) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Aggregations

DebtRecovery (com.axelor.apps.account.db.DebtRecovery)6 AxelorException (com.axelor.exception.AxelorException)4 AccountingSituation (com.axelor.apps.account.db.AccountingSituation)3 AccountingSituationRepository (com.axelor.apps.account.db.repo.AccountingSituationRepository)2 Transactional (com.google.inject.persist.Transactional)2 DebtRecoveryHistory (com.axelor.apps.account.db.DebtRecoveryHistory)1 DebtRecoveryMethod (com.axelor.apps.account.db.DebtRecoveryMethod)1 MoveLine (com.axelor.apps.account.db.MoveLine)1 DebtRecoveryActionService (com.axelor.apps.account.service.debtrecovery.DebtRecoveryActionService)1 Company (com.axelor.apps.base.db.Company)1 Partner (com.axelor.apps.base.db.Partner)1 TradingName (com.axelor.apps.base.db.TradingName)1 BigDecimal (java.math.BigDecimal)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1