Search in sources :

Example 6 with Company

use of com.axelor.apps.base.db.Company in project axelor-open-suite by axelor.

the class MoveLineExportServiceImpl method exportMoveLineTypeSelect1009FILE1.

/**
 * Méthode réalisant l'export SI - des en-têtes pour les journaux de type achat
 *
 * @param mlr
 * @param replay
 * @throws AxelorException
 * @throws IOException
 */
@SuppressWarnings("unchecked")
@Transactional(rollbackOn = { Exception.class })
public void exportMoveLineTypeSelect1009FILE1(AccountingReport accountingReport, boolean replay) throws AxelorException, IOException {
    log.info("In export service 1009 FILE 1:");
    Company company = accountingReport.getCompany();
    String dateQueryStr = String.format(" WHERE self.company = %s", company.getId());
    JournalType journalType = accountingReportService.getJournalType(accountingReport);
    if (accountingReport.getJournal() != null) {
        dateQueryStr += String.format(" AND self.journal = %s", accountingReport.getJournal().getId());
    } else {
        dateQueryStr += String.format(" AND self.journal.journalType = %s", journalType.getId());
    }
    if (accountingReport.getPeriod() != null) {
        dateQueryStr += String.format(" AND self.period = %s", accountingReport.getPeriod().getId());
    }
    if (replay) {
        dateQueryStr += String.format(" AND self.accountingOk = true AND self.accountingReport = %s", accountingReport.getId());
    } else {
        dateQueryStr += " AND self.accountingOk = false ";
    }
    dateQueryStr += " AND self.ignoreInAccountingOk = false AND self.journal.notExportOk = false ";
    dateQueryStr += String.format(" AND (self.statusSelect = %s OR self.statusSelect = %s) ", MoveRepository.STATUS_VALIDATED, MoveRepository.STATUS_ACCOUNTED);
    Query dateQuery = JPA.em().createQuery("SELECT self.date from Move self" + dateQueryStr + "group by self.date order by self.date");
    List<LocalDate> allDates = dateQuery.getResultList();
    log.debug("allDates : {}", allDates);
    List<String[]> allMoveData = new ArrayList<>();
    String companyCode = "";
    String reference = "";
    String moveQueryStr = "";
    String moveLineQueryStr = "";
    if (accountingReport.getRef() != null) {
        reference = accountingReport.getRef();
    }
    if (company != null) {
        companyCode = company.getCode();
        moveQueryStr += String.format(" AND self.company = %s", company.getId());
    }
    if (accountingReport.getPeriod() != null) {
        moveQueryStr += String.format(" AND self.period = %s", accountingReport.getPeriod().getId());
    }
    if (accountingReport.getDateFrom() != null) {
        moveLineQueryStr += String.format(" AND self.date >= '%s'", accountingReport.getDateFrom().toString());
    }
    if (accountingReport.getDateTo() != null) {
        moveLineQueryStr += String.format(" AND self.date <= '%s'", accountingReport.getDateTo().toString());
    }
    if (accountingReport.getDate() != null) {
        moveLineQueryStr += String.format(" AND self.date <= '%s'", accountingReport.getDate().toString());
    }
    if (replay) {
        moveQueryStr += String.format(" AND self.accountingOk = true AND self.accountingReport = %s", accountingReport.getId());
    } else {
        moveQueryStr += " AND self.accountingOk = false ";
    }
    moveQueryStr += String.format(" AND self.statusSelect = %s ", MoveRepository.STATUS_VALIDATED);
    LocalDate interfaceDate = accountingReport.getDate();
    for (LocalDate dt : allDates) {
        List<Journal> journalList = journalRepo.all().filter("self.journalType = ?1 AND self.notExportOk = false", journalType).fetch();
        if (accountingReport.getJournal() != null) {
            journalList = new ArrayList<>();
            journalList.add(accountingReport.getJournal());
        }
        for (Journal journal : journalList) {
            List<Move> moveList = moveRepo.all().filter("self.date = ?1 AND self.ignoreInAccountingOk = false AND self.journal.notExportOk = false AND self.journal = ?2" + moveQueryStr, dt, journal).fetch();
            String journalCode = journal.getExportCode();
            int moveListSize = moveList.size();
            if (moveListSize > 0) {
                int i = 0;
                for (Move move : moveList) {
                    List<MoveLine> moveLineList = moveLineRepo.all().filter("self.account.useForPartnerBalance = true AND self.credit != 0.00 AND self.move in ?1" + moveLineQueryStr, moveList).fetch();
                    if (!moveLineList.isEmpty()) {
                        String exportNumber = this.getPurchaseExportNumber(company);
                        String periodCode = move.getPeriod().getFromDate().format(DateTimeFormatter.ofPattern("yyyyMM"));
                        BigDecimal totalCredit = this.getSumCredit(moveLineList);
                        String invoiceId = "";
                        String dueDate = "";
                        if (move.getInvoice() != null) {
                            invoiceId = move.getInvoice().getInvoiceId();
                            dueDate = move.getInvoice().getDueDate().toString();
                        }
                        MoveLine firstMoveLine = moveLineList.get(0);
                        String[] items = new String[11];
                        items[0] = companyCode;
                        items[1] = journalCode;
                        items[2] = exportNumber;
                        items[3] = interfaceDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
                        items[4] = invoiceId;
                        items[5] = dueDate;
                        items[6] = firstMoveLine.getAccount().getCode();
                        items[7] = totalCredit.toString();
                        items[8] = reference;
                        items[9] = dt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
                        items[10] = periodCode;
                        allMoveData.add(items);
                        this.updateMove(move, accountingReport, interfaceDate, exportNumber);
                        if (i % 10 == 0) {
                            JPA.clear();
                        }
                        if (i++ % 100 == 0) {
                            log.debug("Process : {} / {}", i, moveListSize);
                        }
                    }
                }
            }
        }
    }
    String fileName = "entete" + appAccountService.getTodayDateTime().format(DateTimeFormatter.ofPattern(DATE_FORMAT_YYYYMMDDHHMMSS)) + "achats.dat";
    writeMoveLineToCsvFile(company, fileName, this.createHeaderForHeaderFile(accountingReport.getReportType().getTypeSelect()), allMoveData, accountingReport);
}
Also used : Company(com.axelor.apps.base.db.Company) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) Journal(com.axelor.apps.account.db.Journal) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) JournalType(com.axelor.apps.account.db.JournalType) Transactional(com.google.inject.persist.Transactional)

Example 7 with Company

use of com.axelor.apps.base.db.Company in project axelor-open-suite by axelor.

the class MoveLineExportServiceImpl method exportMoveLineAllTypeSelectFILE2.

/**
 * Méthode réalisant l'export SI - des fichiers détails
 *
 * @param mlr
 * @param fileName
 * @throws AxelorException
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public void exportMoveLineAllTypeSelectFILE2(AccountingReport accountingReport, String fileName) throws AxelorException, IOException {
    log.info("In export service FILE 2 :");
    Company company = accountingReport.getCompany();
    String companyCode = "";
    String moveLineQueryStr = "";
    int typeSelect = accountingReport.getReportType().getTypeSelect();
    if (company != null) {
        companyCode = company.getCode();
        moveLineQueryStr += String.format(" AND self.move.company = %s", company.getId());
    }
    if (accountingReport.getJournal() != null) {
        moveLineQueryStr += String.format(" AND self.move.journal = %s", accountingReport.getJournal().getId());
    } else {
        moveLineQueryStr += String.format(" AND self.move.journal.journalType = %s", accountingReportService.getJournalType(accountingReport).getId());
    }
    if (accountingReport.getPeriod() != null) {
        moveLineQueryStr += String.format(" AND self.move.period = %s", accountingReport.getPeriod().getId());
    }
    if (accountingReport.getDateFrom() != null) {
        moveLineQueryStr += String.format(" AND self.date >= '%s'", accountingReport.getDateFrom().toString());
    }
    if (accountingReport.getDateTo() != null) {
        moveLineQueryStr += String.format(" AND self.date <= '%s'", accountingReport.getDateTo().toString());
    }
    if (accountingReport.getDate() != null) {
        moveLineQueryStr += String.format(" AND self.date <= '%s'", accountingReport.getDate().toString());
    }
    if (typeSelect != 8) {
        moveLineQueryStr += " AND self.account.useForPartnerBalance = false ";
    }
    moveLineQueryStr += String.format("AND self.move.accountingOk = true AND self.move.ignoreInAccountingOk = false AND self.move.accountingReport = %s", accountingReport.getId());
    moveLineQueryStr += String.format(" AND (self.move.statusSelect = %s OR self.move.statusSelect = %s) ", MoveRepository.STATUS_VALIDATED, MoveRepository.STATUS_ACCOUNTED);
    Query queryDate = JPA.em().createQuery("SELECT self.date from MoveLine self where self.account != null AND (self.debit > 0 OR self.credit > 0) " + moveLineQueryStr + " group by self.date ORDER BY self.date");
    List<LocalDate> dates = queryDate.getResultList();
    log.debug("dates : {}", dates);
    List<String[]> allMoveLineData = new ArrayList<>();
    for (LocalDate localDate : dates) {
        Query queryExportRef = JPA.em().createQuery("SELECT DISTINCT self.move.exportNumber from MoveLine self where self.account != null " + "AND (self.debit > 0 OR self.credit > 0) AND self.date = '" + localDate.toString() + "'" + moveLineQueryStr);
        List<String> exportRefs = queryExportRef.getResultList();
        for (String exportRef : exportRefs) {
            if (exportRef != null && !exportRef.isEmpty()) {
                int sequence = 1;
                Query query = JPA.em().createQuery("SELECT self.account.id from MoveLine self where self.account != null AND (self.debit > 0 OR self.credit > 0) " + "AND self.date = '" + localDate.toString() + "' AND self.move.exportNumber = '" + exportRef + "'" + moveLineQueryStr + " group by self.account.id");
                List<Long> accountIds = query.getResultList();
                log.debug("accountIds : {}", accountIds);
                for (Long accountId : accountIds) {
                    if (accountId != null) {
                        String accountCode = accountRepo.find(accountId).getCode();
                        List<MoveLine> moveLines = moveLineRepo.all().filter("self.account.id = ?1 AND (self.debit > 0 OR self.credit > 0) AND self.date = '" + localDate.toString() + "' AND self.move.exportNumber = '" + exportRef + "'" + moveLineQueryStr, accountId).fetch();
                        log.debug("movelines  : {} ", moveLines);
                        if (!moveLines.isEmpty()) {
                            List<MoveLine> moveLineList = moveLineService.consolidateMoveLines(moveLines);
                            List<MoveLine> sortMoveLineList = this.sortMoveLineByDebitCredit(moveLineList);
                            for (MoveLine moveLine3 : sortMoveLineList) {
                                Journal journal = moveLine3.getMove().getJournal();
                                LocalDate date = moveLine3.getDate();
                                String[] items = null;
                                if (typeSelect == 9) {
                                    items = new String[13];
                                } else {
                                    items = new String[12];
                                }
                                items[0] = companyCode;
                                items[1] = journal.getExportCode();
                                items[2] = moveLine3.getMove().getExportNumber();
                                items[3] = String.format("%s", sequence);
                                sequence++;
                                items[4] = accountCode;
                                BigDecimal totAmt = moveLine3.getCredit().subtract(moveLine3.getDebit());
                                String moveLineSign = "C";
                                if (totAmt.compareTo(BigDecimal.ZERO) < 0) {
                                    moveLineSign = "D";
                                    totAmt = totAmt.negate();
                                }
                                items[5] = moveLineSign;
                                items[6] = totAmt.toString();
                                String analyticAccounts = "";
                                for (AnalyticMoveLine analyticDistributionLine : moveLine3.getAnalyticMoveLineList()) {
                                    analyticAccounts = analyticAccounts + analyticDistributionLine.getAnalyticAccount().getCode() + "/";
                                }
                                if (typeSelect == 9) {
                                    items[7] = "";
                                    items[8] = analyticAccounts;
                                    items[9] = String.format("%s DU %s", journal.getCode(), date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
                                } else {
                                    items[7] = analyticAccounts;
                                    items[8] = String.format("%s DU %s", journal.getCode(), date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
                                }
                                allMoveLineData.add(items);
                            }
                        }
                    }
                }
            }
        }
    }
    writeMoveLineToCsvFile(company, fileName, this.createHeaderForDetailFile(typeSelect), allMoveLineData, accountingReport);
}
Also used : Company(com.axelor.apps.base.db.Company) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) Journal(com.axelor.apps.account.db.Journal) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Example 8 with Company

use of com.axelor.apps.base.db.Company in project axelor-open-suite by axelor.

the class PaymentScheduleLineServiceImpl method createPaymentMove.

@Override
@Transactional(rollbackOn = { Exception.class })
public Move createPaymentMove(PaymentScheduleLine paymentScheduleLine, BankDetails companyBankDetails, PaymentMode paymentMode) throws AxelorException {
    Preconditions.checkNotNull(paymentScheduleLine);
    Preconditions.checkNotNull(companyBankDetails);
    PaymentSchedule paymentSchedule = paymentScheduleLine.getPaymentSchedule();
    Company company = paymentSchedule.getCompany();
    Partner partner = paymentSchedule.getPartner();
    Journal journal = paymentModeService.getPaymentModeJournal(paymentMode, company, companyBankDetails);
    BigDecimal amount = paymentScheduleLine.getInTaxAmount();
    String name = paymentScheduleLine.getName();
    LocalDate todayDate = appBaseService.getTodayDate(company);
    Account account = accountingSituationService.getCustomerAccount(partner, company);
    Move move = moveService.getMoveCreateService().createMove(journal, company, null, partner, paymentMode, MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PAYMENT);
    MoveLine creditMoveLine = moveService.getMoveLineService().createMoveLine(move, partner, account, amount, false, todayDate, 1, name, null);
    move.addMoveLineListItem(creditMoveLine);
    creditMoveLine = moveLineRepo.save(creditMoveLine);
    Account paymentModeAccount = paymentModeService.getPaymentModeAccount(paymentMode, company, companyBankDetails);
    MoveLine debitMoveLine = moveService.getMoveLineService().createMoveLine(move, partner, paymentModeAccount, amount, true, todayDate, 2, name, null);
    move.addMoveLineListItem(debitMoveLine);
    debitMoveLine = moveLineRepo.save(debitMoveLine);
    moveService.getMoveValidateService().validate(move);
    // Reconcile
    if (paymentSchedule.getTypeSelect() == PaymentScheduleRepository.TYPE_TERMS && paymentSchedule.getInvoiceSet() != null) {
        List<MoveLine> debitMoveLineList = paymentSchedule.getInvoiceSet().stream().sorted(Comparator.comparing(Invoice::getDueDate)).map(invoice -> moveService.getMoveLineService().getDebitCustomerMoveLine(invoice)).collect(Collectors.toList());
        if (moveToolService.isSameAccount(debitMoveLineList, account)) {
            List<MoveLine> creditMoveLineList = Lists.newArrayList(creditMoveLine);
            paymentService.useExcessPaymentOnMoveLines(debitMoveLineList, creditMoveLineList);
        }
    }
    paymentScheduleLine.setDirectDebitAmount(amount);
    paymentScheduleLine.setInTaxAmountPaid(amount);
    paymentScheduleLine.setAdvanceOrPaymentMove(move);
    paymentScheduleLine.setAdvanceMoveLine(creditMoveLine);
    paymentScheduleLine.setStatusSelect(PaymentScheduleLineRepository.STATUS_VALIDATED);
    paymentScheduleService.closePaymentScheduleIfAllPaid(paymentSchedule);
    return move;
}
Also used : Company(com.axelor.apps.base.db.Company) PaymentSchedule(com.axelor.apps.account.db.PaymentSchedule) Move(com.axelor.apps.account.db.Move) MoveService(com.axelor.apps.account.service.move.MoveService) PaymentService(com.axelor.apps.account.service.payment.PaymentService) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) PaymentModeService(com.axelor.apps.account.service.payment.PaymentModeService) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Lists(com.google.common.collect.Lists) AxelorException(com.axelor.exception.AxelorException) MoveLine(com.axelor.apps.account.db.MoveLine) PaymentScheduleLine(com.axelor.apps.account.db.PaymentScheduleLine) MoveToolService(com.axelor.apps.account.service.move.MoveToolService) PaymentScheduleRepository(com.axelor.apps.account.db.repo.PaymentScheduleRepository) RoundingMode(java.math.RoundingMode) Journal(com.axelor.apps.account.db.Journal) Logger(org.slf4j.Logger) MethodHandles(java.lang.invoke.MethodHandles) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) Invoice(com.axelor.apps.account.db.Invoice) Collectors(java.util.stream.Collectors) Account(com.axelor.apps.account.db.Account) SequenceService(com.axelor.apps.base.service.administration.SequenceService) List(java.util.List) PaymentScheduleLineRepository(com.axelor.apps.account.db.repo.PaymentScheduleLineRepository) LocalDate(java.time.LocalDate) PaymentMode(com.axelor.apps.account.db.PaymentMode) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) MoveLineRepository(com.axelor.apps.account.db.repo.MoveLineRepository) Partner(com.axelor.apps.base.db.Partner) MoveRepository(com.axelor.apps.account.db.repo.MoveRepository) BankDetails(com.axelor.apps.base.db.BankDetails) Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) PaymentSchedule(com.axelor.apps.account.db.PaymentSchedule) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) Journal(com.axelor.apps.account.db.Journal) Partner(com.axelor.apps.base.db.Partner) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 9 with Company

use of com.axelor.apps.base.db.Company in project axelor-open-suite by axelor.

the class IrrecoverableService method passInIrrecoverable.

/**
 * Procédure permettant de passer une facture en irrécouvrable
 *
 * @param invoice Une facture
 * @param generateEvent Un évènement à t'il déjà été créé par un autre objet ?
 * @throws AxelorException
 */
@Transactional(rollbackOn = { Exception.class })
public void passInIrrecoverable(Invoice invoice, boolean generateEvent) throws AxelorException {
    invoice.setIrrecoverableStatusSelect(InvoiceRepository.IRRECOVERABLE_STATUS_TO_PASS_IN_IRRECOUVRABLE);
    if (generateEvent) {
        Company company = invoice.getCompany();
        ManagementObject managementObject = this.createManagementObject("IRR", accountConfigService.getIrrecoverableReasonPassage(accountConfigService.getAccountConfig(company)));
        invoice.setManagementObject(managementObject);
        MoveLine moveLine = moveService.getMoveToolService().getCustomerMoveLineByQuery(invoice);
        if (moveLine == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.IRRECOVERABLE_3), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION), invoice.getInvoiceId());
        }
        this.passInIrrecoverable(moveLine, managementObject, false);
    }
    invoiceRepo.save(invoice);
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) ManagementObject(com.axelor.apps.account.db.ManagementObject) MoveLine(com.axelor.apps.account.db.MoveLine) Transactional(com.google.inject.persist.Transactional)

Example 10 with Company

use of com.axelor.apps.base.db.Company in project axelor-open-suite by axelor.

the class IrrecoverableService method passInIrrecoverable.

/**
 * Procédure permettant de passer un échéancier de lissage de paiement en irrécouvrable La
 * procédure passera aussi les lignes d'écriture de rejet d'échéance en irrécouvrable, ainsi que
 * les factures pas complètement payée selectionnées sur l'échéancier
 *
 * @param paymentSchedule Un échéancier de paiement
 * @throws AxelorException
 */
@Transactional(rollbackOn = { Exception.class })
public void passInIrrecoverable(PaymentSchedule paymentSchedule) throws AxelorException {
    Company company = paymentSchedule.getCompany();
    paymentSchedule.setIrrecoverableStatusSelect(PaymentScheduleRepository.IRRECOVERABLE_STATUS_TO_PASS_IN_IRRECOUVRABLE);
    ManagementObject managementObject = this.createManagementObject("IRR", accountConfigService.getIrrecoverableReasonPassage(accountConfigService.getAccountConfig(company)));
    paymentSchedule.setManagementObject(managementObject);
    List<MoveLine> paymentScheduleLineRejectMoveLineList = new ArrayList<MoveLine>();
    for (PaymentScheduleLine paymentScheduleLine : paymentSchedule.getPaymentScheduleLineList()) {
        if (paymentScheduleLine.getRejectMoveLine() != null && paymentScheduleLine.getRejectMoveLine().getAmountRemaining().compareTo(BigDecimal.ZERO) > 0) {
            paymentScheduleLineRejectMoveLineList.add(paymentScheduleLine.getRejectMoveLine());
        }
    }
    for (MoveLine moveLine : paymentScheduleLineRejectMoveLineList) {
        this.passInIrrecoverable(moveLine, managementObject, true);
    }
    for (Invoice invoice : paymentSchedule.getInvoiceSet()) {
        if (invoice.getCompanyInTaxTotalRemaining().compareTo(BigDecimal.ZERO) > 0) {
            this.passInIrrecoverable(invoice, managementObject);
        }
    }
    paymentScheduleService.cancelPaymentSchedule(paymentSchedule);
    paymentScheduleRepo.save(paymentSchedule);
}
Also used : Company(com.axelor.apps.base.db.Company) Invoice(com.axelor.apps.account.db.Invoice) ManagementObject(com.axelor.apps.account.db.ManagementObject) PaymentScheduleLine(com.axelor.apps.account.db.PaymentScheduleLine) MoveLine(com.axelor.apps.account.db.MoveLine) ArrayList(java.util.ArrayList) Transactional(com.google.inject.persist.Transactional)

Aggregations

Company (com.axelor.apps.base.db.Company)213 Transactional (com.google.inject.persist.Transactional)72 Partner (com.axelor.apps.base.db.Partner)68 AxelorException (com.axelor.exception.AxelorException)65 BigDecimal (java.math.BigDecimal)54 Move (com.axelor.apps.account.db.Move)35 MoveLine (com.axelor.apps.account.db.MoveLine)35 LocalDate (java.time.LocalDate)35 ArrayList (java.util.ArrayList)31 PaymentMode (com.axelor.apps.account.db.PaymentMode)28 AccountConfig (com.axelor.apps.account.db.AccountConfig)27 Journal (com.axelor.apps.account.db.Journal)26 Account (com.axelor.apps.account.db.Account)25 Invoice (com.axelor.apps.account.db.Invoice)25 BankDetails (com.axelor.apps.base.db.BankDetails)22 Currency (com.axelor.apps.base.db.Currency)19 Product (com.axelor.apps.base.db.Product)17 StockLocation (com.axelor.apps.stock.db.StockLocation)17 StockMove (com.axelor.apps.stock.db.StockMove)15 List (java.util.List)15