use of com.axelor.apps.account.db.MoveLine in project axelor-open-suite by axelor.
the class MoveLineServiceImpl method populateMoveLineMap.
private void populateMoveLineMap(Map<List<Object>, Pair<List<MoveLine>, List<MoveLine>>> moveLineMap, List<MoveLine> reconciliableMoveLineList, boolean isCredit) {
for (MoveLine moveLine : reconciliableMoveLineList) {
Move move = moveLine.getMove();
List<Object> keys = new ArrayList<Object>();
keys.add(move.getCompany());
keys.add(moveLine.getAccount());
keys.add(moveLine.getPartner());
Pair<List<MoveLine>, List<MoveLine>> moveLineLists = moveLineMap.get(keys);
if (moveLineLists == null) {
moveLineLists = Pair.of(new ArrayList<>(), new ArrayList<>());
moveLineMap.put(keys, moveLineLists);
}
List<MoveLine> moveLineList = isCredit ? moveLineLists.getLeft() : moveLineLists.getRight();
moveLineList.add(moveLine);
}
}
use of com.axelor.apps.account.db.MoveLine in project axelor-open-suite by axelor.
the class MoveLineServiceImpl method createMoveLines.
/**
* Créer les lignes d'écritures comptables d'une facture.
*
* @param invoice
* @param move
* @param consolidate
* @return
*/
@Override
public List<MoveLine> createMoveLines(Invoice invoice, Move move, Company company, Partner partner, Account partnerAccount, boolean consolidate, boolean isPurchase, boolean isDebitCustomer) throws AxelorException {
log.debug("Création des lignes d'écriture comptable de la facture/l'avoir {}", invoice.getInvoiceId());
List<MoveLine> moveLines = new ArrayList<MoveLine>();
Set<AnalyticAccount> analyticAccounts = new HashSet<AnalyticAccount>();
int moveLineId = 1;
if (partner == null) {
throw new AxelorException(invoice, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.MOVE_LINE_1), invoice.getInvoiceId());
}
if (partnerAccount == null) {
throw new AxelorException(invoice, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.MOVE_LINE_2), invoice.getInvoiceId());
}
String origin = invoice.getInvoiceId();
if (InvoiceToolService.isPurchase(invoice)) {
origin = invoice.getSupplierInvoiceNb();
}
// Creation of partner move line
MoveLine moveLine1 = this.createMoveLine(move, partner, partnerAccount, invoice.getInTaxTotal(), invoice.getCompanyInTaxTotal(), null, isDebitCustomer, invoice.getInvoiceDate(), invoice.getDueDate(), invoice.getOriginDate(), moveLineId++, origin, null);
moveLines.add(moveLine1);
AnalyticMoveLineRepository analyticMoveLineRepository = Beans.get(AnalyticMoveLineRepository.class);
// Creation of product move lines for each invoice line
for (InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
BigDecimal companyExTaxTotal = invoiceLine.getCompanyExTaxTotal();
if (companyExTaxTotal.compareTo(BigDecimal.ZERO) != 0) {
analyticAccounts.clear();
Account account = invoiceLine.getAccount();
if (account == null) {
throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_4), invoiceLine.getName(), company.getName());
}
companyExTaxTotal = invoiceLine.getCompanyExTaxTotal();
log.debug("Traitement de la ligne de facture : compte comptable = {}, montant = {}", new Object[] { account.getName(), companyExTaxTotal });
if (invoiceLine.getAnalyticDistributionTemplate() == null && (invoiceLine.getAnalyticMoveLineList() == null || invoiceLine.getAnalyticMoveLineList().isEmpty()) && account.getAnalyticDistributionAuthorized() && account.getAnalyticDistributionRequiredOnInvoiceLines()) {
throw new AxelorException(move, TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.ANALYTIC_DISTRIBUTION_MISSING), invoiceLine.getName(), company.getName());
}
MoveLine moveLine = this.createMoveLine(move, partner, account, invoiceLine.getExTaxTotal(), companyExTaxTotal, null, !isDebitCustomer, invoice.getInvoiceDate(), null, invoice.getOriginDate(), moveLineId++, origin, invoiceLine.getProductName());
moveLine.setAnalyticDistributionTemplate(invoiceLine.getAnalyticDistributionTemplate());
if (invoiceLine.getAnalyticMoveLineList() != null && !invoiceLine.getAnalyticMoveLineList().isEmpty()) {
for (AnalyticMoveLine invoiceAnalyticMoveLine : invoiceLine.getAnalyticMoveLineList()) {
AnalyticMoveLine analyticMoveLine = analyticMoveLineRepository.copy(invoiceAnalyticMoveLine, false);
analyticMoveLine.setTypeSelect(AnalyticMoveLineRepository.STATUS_REAL_ACCOUNTING);
analyticMoveLine.setInvoiceLine(null);
analyticMoveLine.setAccount(moveLine.getAccount());
analyticMoveLine.setAccountType(moveLine.getAccount().getAccountType());
analyticMoveLineService.updateAnalyticMoveLine(analyticMoveLine, moveLine.getDebit().add(moveLine.getCredit()), moveLine.getDate());
moveLine.addAnalyticMoveLineListItem(analyticMoveLine);
}
} else {
generateAnalyticMoveLines(moveLine);
}
TaxLine taxLine = invoiceLine.getTaxLine();
if (taxLine != null) {
moveLine.setTaxLine(taxLine);
moveLine.setTaxRate(taxLine.getValue());
moveLine.setTaxCode(taxLine.getTax().getCode());
}
moveLines.add(moveLine);
}
}
// Creation of tax move lines for each invoice line tax
for (InvoiceLineTax invoiceLineTax : invoice.getInvoiceLineTaxList()) {
BigDecimal companyTaxTotal = invoiceLineTax.getCompanyTaxTotal();
if (companyTaxTotal.compareTo(BigDecimal.ZERO) != 0) {
Tax tax = invoiceLineTax.getTaxLine().getTax();
boolean hasFixedAssets = !invoiceLineTax.getSubTotalOfFixedAssets().equals(BigDecimal.ZERO);
boolean hasOtherAssets = !invoiceLineTax.getSubTotalExcludingFixedAssets().equals(BigDecimal.ZERO);
Account account;
MoveLine moveLine;
if (hasFixedAssets && invoiceLineTax.getCompanySubTotalOfFixedAssets().compareTo(BigDecimal.ZERO) != 0) {
account = taxAccountService.getAccount(tax, company, isPurchase, true);
if (account == null) {
throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_6), tax.getName(), company.getName());
}
moveLine = this.createMoveLine(move, partner, account, invoiceLineTax.getSubTotalOfFixedAssets(), invoiceLineTax.getCompanySubTotalOfFixedAssets(), null, !isDebitCustomer, invoice.getInvoiceDate(), null, invoice.getOriginDate(), moveLineId++, origin, null);
moveLine.setTaxLine(invoiceLineTax.getTaxLine());
moveLine.setTaxRate(invoiceLineTax.getTaxLine().getValue());
moveLine.setTaxCode(tax.getCode());
moveLines.add(moveLine);
}
if (hasOtherAssets && invoiceLineTax.getCompanySubTotalExcludingFixedAssets().compareTo(BigDecimal.ZERO) != 0) {
account = taxAccountService.getAccount(tax, company, isPurchase, false);
if (account == null) {
throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_6), tax.getName(), company.getName());
}
moveLine = this.createMoveLine(move, partner, account, invoiceLineTax.getSubTotalExcludingFixedAssets(), invoiceLineTax.getCompanySubTotalExcludingFixedAssets(), null, !isDebitCustomer, invoice.getInvoiceDate(), null, invoice.getOriginDate(), moveLineId++, origin, null);
moveLine.setTaxLine(invoiceLineTax.getTaxLine());
moveLine.setTaxRate(invoiceLineTax.getTaxLine().getValue());
moveLine.setTaxCode(tax.getCode());
moveLines.add(moveLine);
}
}
}
if (consolidate) {
this.consolidateMoveLines(moveLines);
}
return moveLines;
}
use of com.axelor.apps.account.db.MoveLine in project axelor-open-suite by axelor.
the class MoveLineServiceImpl method reconcileMoveLinesWithCacheManagement.
/**
* Method used to reconcile the move line list passed as a parameter
*
* @param moveLineList
*/
@Override
public void reconcileMoveLinesWithCacheManagement(List<MoveLine> moveLineList) {
List<MoveLine> reconciliableCreditMoveLineList = getReconciliableCreditMoveLines(moveLineList);
List<MoveLine> reconciliableDebitMoveLineList = getReconciliableDebitMoveLines(moveLineList);
Map<List<Object>, Pair<List<MoveLine>, List<MoveLine>>> moveLineMap = new HashMap<>();
populateCredit(moveLineMap, reconciliableCreditMoveLineList);
populateDebit(moveLineMap, reconciliableDebitMoveLineList);
Comparator<MoveLine> byDate = Comparator.comparing(MoveLine::getDate);
PaymentService paymentService = Beans.get(PaymentService.class);
for (Pair<List<MoveLine>, List<MoveLine>> moveLineLists : moveLineMap.values()) {
try {
moveLineLists = this.findMoveLineLists(moveLineLists);
this.useExcessPaymentOnMoveLinesDontThrow(byDate, paymentService, moveLineLists);
} catch (Exception e) {
TraceBackService.trace(e);
log.debug(e.getMessage());
} finally {
JPA.clear();
}
}
}
use of com.axelor.apps.account.db.MoveLine in project axelor-open-suite by axelor.
the class MoveLineServiceImpl method reconcileMoveLines.
@Override
@Transactional
public void reconcileMoveLines(List<MoveLine> moveLineList) {
List<MoveLine> reconciliableCreditMoveLineList = getReconciliableCreditMoveLines(moveLineList);
List<MoveLine> reconciliableDebitMoveLineList = getReconciliableDebitMoveLines(moveLineList);
Map<List<Object>, Pair<List<MoveLine>, List<MoveLine>>> moveLineMap = new HashMap<>();
populateCredit(moveLineMap, reconciliableCreditMoveLineList);
populateDebit(moveLineMap, reconciliableDebitMoveLineList);
Comparator<MoveLine> byDate = Comparator.comparing(MoveLine::getDate);
PaymentService paymentService = Beans.get(PaymentService.class);
for (Pair<List<MoveLine>, List<MoveLine>> moveLineLists : moveLineMap.values()) {
List<MoveLine> companyPartnerCreditMoveLineList = moveLineLists.getLeft();
List<MoveLine> companyPartnerDebitMoveLineList = moveLineLists.getRight();
companyPartnerCreditMoveLineList.sort(byDate);
companyPartnerDebitMoveLineList.sort(byDate);
paymentService.useExcessPaymentOnMoveLinesDontThrow(companyPartnerDebitMoveLineList, companyPartnerCreditMoveLineList);
}
}
use of com.axelor.apps.account.db.MoveLine in project axelor-open-suite by axelor.
the class MoveLineServiceImpl method autoTaxLineGenerate.
@Override
public void autoTaxLineGenerate(Move move) throws AxelorException {
List<MoveLine> moveLineList = move.getMoveLineList();
moveLineList.sort(new Comparator<MoveLine>() {
@Override
public int compare(MoveLine o1, MoveLine o2) {
if (o2.getSourceTaxLine() != null) {
return 0;
}
return -1;
}
});
Iterator<MoveLine> moveLineItr = moveLineList.iterator();
Map<String, MoveLine> map = new HashMap<>();
Map<String, MoveLine> newMap = new HashMap<>();
while (moveLineItr.hasNext()) {
MoveLine moveLine = moveLineItr.next();
TaxLine taxLine = moveLine.getTaxLine();
TaxLine sourceTaxLine = moveLine.getSourceTaxLine();
if (sourceTaxLine != null) {
String sourceTaxLineKey = moveLine.getAccount().getCode() + sourceTaxLine.getId();
moveLine.setCredit(BigDecimal.ZERO);
moveLine.setDebit(BigDecimal.ZERO);
map.put(sourceTaxLineKey, moveLine);
moveLineItr.remove();
continue;
}
if (taxLine != null) {
String accountType = moveLine.getAccount().getAccountType().getTechnicalTypeSelect();
if (accountType.equals(AccountTypeRepository.TYPE_DEBT) || accountType.equals(AccountTypeRepository.TYPE_CHARGE) || accountType.equals(AccountTypeRepository.TYPE_INCOME) || accountType.equals(AccountTypeRepository.TYPE_ASSET)) {
BigDecimal debit = moveLine.getDebit();
BigDecimal credit = moveLine.getCredit();
LocalDate date = moveLine.getDate();
Company company = move.getCompany();
MoveLine newOrUpdatedMoveLine = new MoveLine();
if (accountType.equals(AccountTypeRepository.TYPE_DEBT) || accountType.equals(AccountTypeRepository.TYPE_CHARGE)) {
newOrUpdatedMoveLine.setAccount(taxAccountService.getAccount(taxLine.getTax(), company, true, false));
} else if (accountType.equals(AccountTypeRepository.TYPE_INCOME)) {
newOrUpdatedMoveLine.setAccount(taxAccountService.getAccount(taxLine.getTax(), company, false, false));
} else if (accountType.equals(AccountTypeRepository.TYPE_ASSET)) {
newOrUpdatedMoveLine.setAccount(taxAccountService.getAccount(taxLine.getTax(), company, true, true));
}
Account newAccount = newOrUpdatedMoveLine.getAccount();
if (newAccount == null) {
throw new AxelorException(move, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MOVE_LINE_6), taxLine.getName(), company.getName());
}
String newSourceTaxLineKey = newAccount.getCode() + taxLine.getId();
if (!map.containsKey(newSourceTaxLineKey) && !newMap.containsKey(newSourceTaxLineKey)) {
newOrUpdatedMoveLine = this.createNewMoveLine(debit, credit, date, accountType, taxLine, newOrUpdatedMoveLine);
} else {
if (newMap.containsKey(newSourceTaxLineKey)) {
newOrUpdatedMoveLine = newMap.get(newSourceTaxLineKey);
} else if (!newMap.containsKey(newSourceTaxLineKey) && map.containsKey(newSourceTaxLineKey)) {
newOrUpdatedMoveLine = map.get(newSourceTaxLineKey);
}
newOrUpdatedMoveLine.setDebit(newOrUpdatedMoveLine.getDebit().add(debit.multiply(taxLine.getValue())));
newOrUpdatedMoveLine.setCredit(newOrUpdatedMoveLine.getCredit().add(credit.multiply(taxLine.getValue())));
}
newMap.put(newSourceTaxLineKey, newOrUpdatedMoveLine);
}
}
}
moveLineList.addAll(newMap.values());
}
Aggregations