use of com.axelor.apps.account.db.Account in project axelor-open-suite by axelor.
the class AccountingCutOffServiceImpl method generateProductMoveLine.
protected MoveLine generateProductMoveLine(Move move, StockMoveLine stockMoveLine, String origin, boolean isPurchase, boolean recoveredTax, boolean ati, String moveDescription, boolean isReverse, LocalDate originDate) throws AxelorException {
SaleOrderLine saleOrderLine = stockMoveLine.getSaleOrderLine();
PurchaseOrderLine purchaseOrderLine = stockMoveLine.getPurchaseOrderLine();
Company company = move.getCompany();
LocalDate moveDate = move.getDate();
Partner partner = move.getPartner();
boolean isFixedAssets = false;
BigDecimal amountInCurrency = null;
BigDecimal totalQty = null;
BigDecimal notInvoicedQty = null;
if (isPurchase && purchaseOrderLine != null) {
totalQty = purchaseOrderLine.getQty();
notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), purchaseOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), purchaseOrderLine.getProduct());
isFixedAssets = purchaseOrderLine.getFixedAssets();
if (ati && !recoveredTax) {
amountInCurrency = purchaseOrderLine.getInTaxTotal();
} else {
amountInCurrency = purchaseOrderLine.getExTaxTotal();
}
}
if (!isPurchase && saleOrderLine != null) {
totalQty = saleOrderLine.getQty();
notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), saleOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), saleOrderLine.getProduct());
if (ati) {
amountInCurrency = saleOrderLine.getInTaxTotal();
} else {
amountInCurrency = saleOrderLine.getExTaxTotal();
}
}
if (totalQty == null || BigDecimal.ZERO.compareTo(totalQty) == 0) {
return null;
}
BigDecimal qtyRate = notInvoicedQty.divide(totalQty, 10, RoundingMode.HALF_UP);
amountInCurrency = amountInCurrency.multiply(qtyRate).setScale(2, RoundingMode.HALF_UP);
if (amountInCurrency == null || amountInCurrency.compareTo(BigDecimal.ZERO) == 0) {
return null;
}
Product product = stockMoveLine.getProduct();
Account account = accountManagementAccountService.getProductAccount(product, company, partner.getFiscalPosition(), isPurchase, isFixedAssets);
boolean isDebit = false;
if ((isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == 1) || !isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == -1) {
isDebit = true;
}
if (isReverse) {
isDebit = !isDebit;
}
MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, amountInCurrency, isDebit, originDate, ++counter, origin, moveDescription);
moveLine.setDate(moveDate);
moveLine.setDueDate(moveDate);
getAndComputeAnalyticDistribution(product, move, moveLine);
move.addMoveLineListItem(moveLine);
if (recoveredTax) {
TaxLine taxLine = accountManagementAccountService.getTaxLine(originDate, product, company, partner.getFiscalPosition(), isPurchase);
if (taxLine != null) {
moveLine.setTaxLine(taxLine);
moveLine.setTaxRate(taxLine.getValue());
moveLine.setTaxCode(taxLine.getTax().getCode());
if (taxLine.getValue().compareTo(BigDecimal.ZERO) != 0) {
generateTaxMoveLine(move, moveLine, origin, isPurchase, isFixedAssets, moveDescription);
}
}
}
return moveLine;
}
use of com.axelor.apps.account.db.Account 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.Account 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());
}
use of com.axelor.apps.account.db.Account in project axelor-open-suite by axelor.
the class MoveServiceImpl method createMoveUseDebit.
@Override
public Move createMoveUseDebit(Invoice invoice, List<MoveLine> debitMoveLines, MoveLine invoiceCustomerMoveLine) throws AxelorException {
Company company = invoice.getCompany();
Partner partner = invoice.getPartner();
Account account = invoice.getPartnerAccount();
Journal journal = accountConfigService.getAutoMiscOpeJournal(accountConfigService.getAccountConfig(company));
log.debug("Création d'une écriture comptable O.D. spécifique à l'emploie des trop-perçus {} (Société : {}, Journal : {})", new Object[] { invoice.getInvoiceId(), company.getName(), journal.getCode() });
BigDecimal remainingAmount = invoice.getInTaxTotal().abs();
log.debug("Montant à payer avec l'avoir récupéré : {}", remainingAmount);
Move oDmove = moveCreateService.createMove(journal, company, null, partner, invoice.getInvoiceDate(), null, MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PAYMENT);
if (oDmove != null) {
BigDecimal totalDebitAmount = moveToolService.getTotalDebitAmount(debitMoveLines);
BigDecimal amount = totalDebitAmount.min(invoiceCustomerMoveLine.getCredit());
// Création de la ligne au débit
MoveLine debitMoveLine = moveLineService.createMoveLine(oDmove, partner, account, amount, true, appAccountService.getTodayDate(company), 1, invoice.getInvoiceId(), null);
oDmove.getMoveLineList().add(debitMoveLine);
// Emploie des dûs sur les lignes de credit qui seront créées au fil de l'eau
paymentService.createExcessPaymentWithAmount(debitMoveLines, amount, oDmove, 2, partner, company, null, account, appAccountService.getTodayDate(company));
moveValidateService.validate(oDmove);
// Création de la réconciliation
Reconcile reconcile = reconcileService.createReconcile(debitMoveLine, invoiceCustomerMoveLine, amount, false);
if (reconcile != null) {
reconcileService.confirmReconcile(reconcile, true);
}
}
return oDmove;
}
use of com.axelor.apps.account.db.Account in project axelor-open-suite by axelor.
the class MoveServiceImpl method createMoveUseExcessPayment.
@Override
public void createMoveUseExcessPayment(Invoice invoice) throws AxelorException {
Company company = invoice.getCompany();
// Récupération des acomptes de la facture
List<MoveLine> creditMoveLineList = moveExcessPaymentService.getAdvancePaymentMoveList(invoice);
AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
// Récupération des trop-perçus
creditMoveLineList.addAll(moveExcessPaymentService.getExcessPayment(invoice));
if (creditMoveLineList != null && creditMoveLineList.size() != 0) {
Partner partner = invoice.getPartner();
Account account = invoice.getPartnerAccount();
MoveLine invoiceCustomerMoveLine = moveToolService.getCustomerMoveLineByLoop(invoice);
Journal journal = accountConfigService.getAutoMiscOpeJournal(accountConfig);
// Si c'est le même compte sur les trop-perçus et sur la facture, alors on lettre directement
if (moveToolService.isSameAccount(creditMoveLineList, account)) {
List<MoveLine> debitMoveLineList = new ArrayList<MoveLine>();
debitMoveLineList.add(invoiceCustomerMoveLine);
paymentService.useExcessPaymentOnMoveLines(debitMoveLineList, creditMoveLineList);
} else // Sinon on créée une O.D. pour passer du compte de la facture à un autre compte sur les
// trop-perçus
{
log.debug("Création d'une écriture comptable O.D. spécifique à l'emploie des trop-perçus {} (Société : {}, Journal : {})", new Object[] { invoice.getInvoiceId(), company.getName(), journal.getCode() });
Move move = moveCreateService.createMove(journal, company, null, partner, invoice.getInvoiceDate(), null, MoveRepository.TECHNICAL_ORIGIN_AUTOMATIC, MoveRepository.FUNCTIONAL_ORIGIN_PAYMENT);
if (move != null) {
BigDecimal totalCreditAmount = moveToolService.getTotalCreditAmount(creditMoveLineList);
BigDecimal amount = totalCreditAmount.min(invoiceCustomerMoveLine.getDebit());
// Création de la ligne au crédit
MoveLine creditMoveLine = moveLineService.createMoveLine(move, partner, account, amount, false, appAccountService.getTodayDate(company), 1, invoice.getInvoiceId(), null);
move.getMoveLineList().add(creditMoveLine);
// Emploie des trop-perçus sur les lignes de debit qui seront créées au fil de l'eau
paymentService.useExcessPaymentWithAmountConsolidated(creditMoveLineList, amount, move, 2, partner, company, account, invoice.getInvoiceDate(), invoice.getDueDate());
moveValidateService.validate(move);
// Création de la réconciliation
Reconcile reconcile = reconcileService.createReconcile(invoiceCustomerMoveLine, creditMoveLine, amount, false);
if (reconcile != null) {
reconcileService.confirmReconcile(reconcile, true);
}
}
}
invoice.setCompanyInTaxTotalRemaining(moveToolService.getInTaxTotalRemaining(invoice));
}
}
Aggregations