Search in sources :

Example 1 with AnalyticMoveLine

use of com.axelor.apps.account.db.AnalyticMoveLine 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 2 with AnalyticMoveLine

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

the class AnalyticMoveLineServiceImpl method createAnalyticMoveLine.

public AnalyticMoveLine createAnalyticMoveLine(AnalyticDistributionLine analyticDistributionLine, BigDecimal total, int typeSelect, LocalDate date) {
    AnalyticMoveLine analyticMoveLine = new AnalyticMoveLine();
    analyticMoveLine.setOriginalPieceAmount(total);
    analyticMoveLine.setAnalyticAccount(analyticDistributionLine.getAnalyticAccount());
    analyticMoveLine.setAnalyticAxis(analyticDistributionLine.getAnalyticAxis());
    analyticMoveLine.setAnalyticJournal(analyticDistributionLine.getAnalyticJournal());
    AnalyticJournal analyticJournal = analyticDistributionLine.getAnalyticJournal();
    Company company = analyticJournal == null ? null : analyticJournal.getCompany();
    if (company != null) {
        analyticMoveLine.setCurrency(company.getCurrency());
    }
    analyticMoveLine.setDate(date);
    analyticMoveLine.setPercentage(analyticDistributionLine.getPercentage());
    analyticMoveLine.setAmount(computeAmount(analyticMoveLine));
    analyticMoveLine.setTypeSelect(typeSelect);
    return analyticMoveLine;
}
Also used : Company(com.axelor.apps.base.db.Company) AnalyticJournal(com.axelor.apps.account.db.AnalyticJournal) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Example 3 with AnalyticMoveLine

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

the class AnalyticMoveLineMngtRepository method copy.

@Override
public AnalyticMoveLine copy(AnalyticMoveLine entity, boolean deep) {
    AnalyticMoveLine copy = super.copy(entity, deep);
    copy.setMoveLine(null);
    copy.setInvoiceLine(null);
    return copy;
}
Also used : AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Example 4 with AnalyticMoveLine

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

the class MoveManagementRepository method save.

@Override
public Move save(Move move) {
    try {
        if (move.getStatusSelect() == MoveRepository.STATUS_ACCOUNTED) {
            Beans.get(MoveValidateService.class).checkPreconditions(move);
        }
        Beans.get(MoveSequenceService.class).setDraftSequence(move);
        List<MoveLine> moveLineList = move.getMoveLineList();
        if (moveLineList != null) {
            for (MoveLine moveLine : moveLineList) {
                List<AnalyticMoveLine> analyticMoveLineList = moveLine.getAnalyticMoveLineList();
                if (analyticMoveLineList != null) {
                    for (AnalyticMoveLine analyticMoveLine : analyticMoveLineList) {
                        analyticMoveLine.setAccount(moveLine.getAccount());
                        analyticMoveLine.setAccountType(moveLine.getAccount().getAccountType());
                    }
                }
            }
        }
        return super.save(move);
    } catch (Exception e) {
        TraceBackService.traceExceptionFromSaveMethod(e);
        throw new PersistenceException(e.getMessage(), e);
    }
}
Also used : MoveValidateService(com.axelor.apps.account.service.move.MoveValidateService) MoveSequenceService(com.axelor.apps.account.service.move.MoveSequenceService) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) PersistenceException(javax.persistence.PersistenceException) AxelorException(com.axelor.exception.AxelorException) PersistenceException(javax.persistence.PersistenceException) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Example 5 with AnalyticMoveLine

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

the class ExpenseServiceImpl method createAndSetMove.

protected Move createAndSetMove(Expense expense) throws AxelorException {
    LocalDate moveDate = expense.getMoveDate();
    if (moveDate == null) {
        moveDate = appAccountService.getTodayDate(expense.getCompany());
        expense.setMoveDate(moveDate);
    }
    Company company = expense.getCompany();
    Partner partner = expense.getUser().getPartner();
    Account account = null;
    AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
    if (partner == null) {
        throw new AxelorException(expense, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(com.axelor.apps.account.exception.IExceptionMessage.USER_PARTNER), expense.getUser().getName());
    }
    Move move = moveService.getMoveCreateService().createMove(accountConfigService.getExpenseJournal(accountConfig), company, null, partner, moveDate, partner.getInPaymentMode(), MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PURCHASE);
    List<MoveLine> moveLines = new ArrayList<>();
    Set<AnalyticAccount> analyticAccounts = new HashSet<>();
    BigDecimal exTaxTotal = null;
    int moveLineId = 1;
    int expenseLineId = 1;
    Account employeeAccount = accountingSituationService.getEmployeeAccount(partner, company);
    moveLines.add(moveLineService.createMoveLine(move, partner, employeeAccount, expense.getInTaxTotal(), false, moveDate, moveDate, moveLineId++, expense.getExpenseSeq(), expense.getFullName()));
    for (ExpenseLine expenseLine : getExpenseLineList(expense)) {
        analyticAccounts.clear();
        Product product = expenseLine.getExpenseProduct();
        account = accountManagementService.getProductAccount(product, company, partner.getFiscalPosition(), true, false);
        if (account == null) {
            throw new AxelorException(expense, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(com.axelor.apps.account.exception.IExceptionMessage.MOVE_LINE_4), expenseLineId, company.getName());
        }
        exTaxTotal = expenseLine.getUntaxedAmount();
        MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, exTaxTotal, true, moveDate, moveDate, moveLineId++, expense.getExpenseSeq(), expenseLine.getComments() != null ? expenseLine.getComments().replaceAll("(\r\n|\n\r|\r|\n)", " ") : "");
        for (AnalyticMoveLine analyticDistributionLineIt : expenseLine.getAnalyticMoveLineList()) {
            AnalyticMoveLine analyticDistributionLine = Beans.get(AnalyticMoveLineRepository.class).copy(analyticDistributionLineIt, false);
            analyticDistributionLine.setExpenseLine(null);
            moveLine.addAnalyticMoveLineListItem(analyticDistributionLine);
        }
        moveLines.add(moveLine);
        expenseLineId++;
    }
    moveLineService.consolidateMoveLines(moveLines);
    account = accountConfigService.getExpenseTaxAccount(accountConfig);
    BigDecimal taxTotal = BigDecimal.ZERO;
    for (ExpenseLine expenseLine : getExpenseLineList(expense)) {
        exTaxTotal = expenseLine.getTotalTax();
        taxTotal = taxTotal.add(exTaxTotal);
    }
    if (taxTotal.signum() != 0) {
        MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, taxTotal, true, moveDate, moveDate, moveLineId++, expense.getExpenseSeq(), expense.getFullName());
        moveLines.add(moveLine);
    }
    move.getMoveLineList().addAll(moveLines);
    moveService.getMoveValidateService().validate(move);
    expense.setMove(move);
    return move;
}
Also used : AnalyticAccount(com.axelor.apps.account.db.AnalyticAccount) Account(com.axelor.apps.account.db.Account) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) ArrayList(java.util.ArrayList) Product(com.axelor.apps.base.db.Product) AnalyticAccount(com.axelor.apps.account.db.AnalyticAccount) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AccountConfig(com.axelor.apps.account.db.AccountConfig) AnalyticMoveLineRepository(com.axelor.apps.account.db.repo.AnalyticMoveLineRepository) Move(com.axelor.apps.account.db.Move) MoveLine(com.axelor.apps.account.db.MoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) Partner(com.axelor.apps.base.db.Partner) HashSet(java.util.HashSet) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine)

Aggregations

AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)30 MoveLine (com.axelor.apps.account.db.MoveLine)7 Transactional (com.google.inject.persist.Transactional)6 BigDecimal (java.math.BigDecimal)6 AnalyticMoveLineRepository (com.axelor.apps.account.db.repo.AnalyticMoveLineRepository)5 ArrayList (java.util.ArrayList)5 Account (com.axelor.apps.account.db.Account)4 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)4 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)4 AxelorException (com.axelor.exception.AxelorException)4 TaxPaymentMoveLine (com.axelor.apps.account.db.TaxPaymentMoveLine)3 Company (com.axelor.apps.base.db.Company)3 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)3 LocalDate (java.time.LocalDate)3 AnalyticAccount (com.axelor.apps.account.db.AnalyticAccount)2 Move (com.axelor.apps.account.db.Move)2 TaxLine (com.axelor.apps.account.db.TaxLine)2 AnalyticMoveLineService (com.axelor.apps.account.service.AnalyticMoveLineService)2 InvoiceLineService (com.axelor.apps.account.service.invoice.InvoiceLineService)2 Partner (com.axelor.apps.base.db.Partner)2