Search in sources :

Example 21 with Company

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

the class LogisticalFormStockRepository method save.

@Override
public LogisticalForm save(LogisticalForm logisticalForm) {
    try {
        Company company = logisticalForm.getCompany();
        if (company != null) {
            if (Strings.isNullOrEmpty(logisticalForm.getDeliveryNumberSeq())) {
                String sequenceNumber = Beans.get(SequenceService.class).getSequenceNumber("logisticalForm", logisticalForm.getCompany());
                if (Strings.isNullOrEmpty(sequenceNumber)) {
                    throw new AxelorException(Sequence.class, TraceBackRepository.CATEGORY_NO_VALUE, I18n.get(IExceptionMessage.LOGISTICAL_FORM_MISSING_SEQUENCE), logisticalForm.getCompany().getName());
                }
                logisticalForm.setDeliveryNumberSeq(sequenceNumber);
            }
            if (!logisticalForm.getIsEmailSent()) {
                StockConfig stockConfig = Beans.get(StockConfigService.class).getStockConfig(company);
                if (stockConfig.getLogisticalFormAutomaticEmail()) {
                    Template template = stockConfig.getLogisticalFormMessageTemplate();
                    if (template == null) {
                        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LOGISTICAL_FORM_MISSING_TEMPLATE), logisticalForm);
                    }
                    Beans.get(TemplateMessageService.class).generateAndSendMessage(logisticalForm, template);
                    logisticalForm.setIsEmailSent(true);
                }
            }
        }
        return super.save(logisticalForm);
    } catch (Exception e) {
        TraceBackService.traceExceptionFromSaveMethod(e);
        throw new PersistenceException(e.getMessage(), e);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) StockConfig(com.axelor.apps.stock.db.StockConfig) TemplateMessageService(com.axelor.apps.message.service.TemplateMessageService) PersistenceException(javax.persistence.PersistenceException) SequenceService(com.axelor.apps.base.service.administration.SequenceService) StockConfigService(com.axelor.apps.stock.service.config.StockConfigService) AxelorException(com.axelor.exception.AxelorException) PersistenceException(javax.persistence.PersistenceException) Template(com.axelor.apps.message.db.Template)

Example 22 with Company

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

the class ProductStockRepository method save.

public Product save(Product product) {
    WeightedAveragePriceService weightedAveragePriceService = Beans.get(WeightedAveragePriceService.class);
    Set<MetaField> specificProductFieldSet = appBaseService.getAppBase().getCompanySpecificProductFieldsSet();
    if (!specificProductFieldSet.isEmpty() && appBaseService.getAppBase().getEnableMultiCompany()) {
        ArrayList<Company> productCompanyList = new ArrayList<>();
        if (product.getProductCompanyList() != null) {
            for (ProductCompany productCompany : product.getProductCompanyList()) {
                productCompanyList.add(productCompany.getCompany());
            }
        }
        Mapper mapper = Mapper.of(Product.class);
        List<StockConfig> stockConfigList = Beans.get(StockConfigRepository.class).all().fetch();
        for (StockConfig stockConfig : stockConfigList) {
            if (stockConfig.getCompany() != null && !productCompanyList.contains(stockConfig.getCompany()) && stockConfig.getReceiptDefaultStockLocation() != null && (stockConfig.getCompany().getArchived() == null || !stockConfig.getCompany().getArchived())) {
                ProductCompany productCompany = new ProductCompany();
                for (MetaField specificField : specificProductFieldSet) {
                    mapper.set(productCompany, specificField.getName(), mapper.get(product, specificField.getName()));
                }
                // specific case for avgPrice per company
                productCompany.setAvgPrice(weightedAveragePriceService.computeAvgPriceForCompany(product, stockConfig.getCompany()));
                productCompany.setCompany(stockConfig.getCompany());
                productCompany.setProduct(product);
                product.addProductCompanyListItem(productCompany);
            }
        }
    }
    return super.save(product);
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Company(com.axelor.apps.base.db.Company) ProductCompany(com.axelor.apps.base.db.ProductCompany) StockConfig(com.axelor.apps.stock.db.StockConfig) MetaField(com.axelor.meta.db.MetaField) WeightedAveragePriceService(com.axelor.apps.stock.service.WeightedAveragePriceService) ArrayList(java.util.ArrayList) ProductCompany(com.axelor.apps.base.db.ProductCompany)

Example 23 with Company

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

the class SaleOrderController method fillCompanyBankDetails.

/**
 * Called on partner, company or payment change. Fill the bank details with a default value.
 *
 * @param request
 * @param response
 * @throws AxelorException
 */
public void fillCompanyBankDetails(ActionRequest request, ActionResponse response) throws AxelorException {
    SaleOrder saleOrder = request.getContext().asType(SaleOrder.class);
    PaymentMode paymentMode = (PaymentMode) request.getContext().get("paymentMode");
    Company company = saleOrder.getCompany();
    Partner partner = saleOrder.getClientPartner();
    if (company == null) {
        return;
    }
    if (partner != null) {
        partner = Beans.get(PartnerRepository.class).find(partner.getId());
    }
    BankDetails defaultBankDetails = Beans.get(BankDetailsService.class).getDefaultCompanyBankDetails(company, paymentMode, partner, null);
    response.setValue("companyBankDetails", defaultBankDetails);
}
Also used : BankDetailsService(com.axelor.apps.base.service.BankDetailsService) Company(com.axelor.apps.base.db.Company) BankDetails(com.axelor.apps.base.db.BankDetails) SaleOrder(com.axelor.apps.sale.db.SaleOrder) Partner(com.axelor.apps.base.db.Partner) PaymentMode(com.axelor.apps.account.db.PaymentMode)

Example 24 with Company

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

the class InvoiceGenerator method createInvoiceHeader.

protected Invoice createInvoiceHeader() throws AxelorException {
    Invoice invoice = new Invoice();
    invoice.setCompany(company);
    invoice.setOperationTypeSelect(operationType);
    if (partner == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.INVOICE_GENERATOR_2), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION));
    }
    if (Beans.get(BlockingService.class).getBlocking(partner, company, BlockingRepository.INVOICING_BLOCKING) != null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.INVOICE_VALIDATE_BLOCKING));
    }
    invoice.setPartner(partner);
    AccountingSituation accountingSituation = Beans.get(AccountingSituationService.class).getAccountingSituation(partner, company);
    if (accountingSituation != null) {
        invoice.setInvoiceAutomaticMail(accountingSituation.getInvoiceAutomaticMail());
        invoice.setInvoiceMessageTemplate(accountingSituation.getInvoiceMessageTemplate());
        invoice.setPfpValidatorUser(accountingSituation.getPfpValidatorUser());
    }
    if (paymentCondition == null) {
        paymentCondition = InvoiceToolService.getPaymentCondition(invoice);
    }
    invoice.setPaymentCondition(paymentCondition);
    if (paymentMode == null) {
        paymentMode = InvoiceToolService.getPaymentMode(invoice);
    }
    invoice.setPaymentMode(paymentMode);
    if (mainInvoicingAddress == null) {
        mainInvoicingAddress = Beans.get(PartnerService.class).getInvoicingAddress(partner);
    }
    invoice.setAddress(mainInvoicingAddress);
    invoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(invoice.getAddress()));
    invoice.setContactPartner(contactPartner);
    if (currency == null) {
        currency = partner.getCurrency();
    }
    if (currency == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.INVOICE_GENERATOR_6), I18n.get(com.axelor.apps.base.exceptions.IExceptionMessage.EXCEPTION));
    }
    invoice.setCurrency(currency);
    invoice.setStatusSelect(InvoiceRepository.STATUS_DRAFT);
    invoice.setPriceList(priceList);
    invoice.setInternalReference(internalReference);
    invoice.setExternalReference(externalReference);
    invoice.setPrintingSettings(Beans.get(TradingNameService.class).getDefaultPrintingSettings(null, company));
    invoice.setTradingName(tradingName);
    if (groupProductsOnPrintings == null) {
        groupProductsOnPrintings = partner.getGroupProductsOnPrintings();
    }
    invoice.setGroupProductsOnPrintings(groupProductsOnPrintings);
    // Set ATI mode on invoice
    AccountConfigService accountConfigService = Beans.get(AccountConfigService.class);
    AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
    int atiChoice = accountConfig.getInvoiceInAtiSelect();
    if (inAti == null) {
        invoice.setInAti(accountConfigService.getInvoiceInAti(accountConfig));
    } else if (atiChoice == AccountConfigRepository.INVOICE_ATI_DEFAULT || atiChoice == AccountConfigRepository.INVOICE_WT_DEFAULT) {
        invoice.setInAti(inAti);
    } else if (atiChoice == AccountConfigRepository.INVOICE_ATI_ALWAYS) {
        invoice.setInAti(true);
    } else {
        invoice.setInAti(false);
    }
    if (partner.getFactorizedCustomer() && accountConfig.getFactorPartner() != null) {
        List<BankDetails> bankDetailsList = accountConfig.getFactorPartner().getBankDetailsList();
        companyBankDetails = bankDetailsList.stream().filter(bankDetails -> bankDetails.getIsDefault()).findFirst().orElse(null);
    } else if (accountingSituation != null) {
        if (paymentMode != null) {
            if (paymentMode.equals(partner.getOutPaymentMode())) {
                companyBankDetails = accountingSituation.getCompanyOutBankDetails();
            } else if (paymentMode.equals(partner.getInPaymentMode())) {
                companyBankDetails = accountingSituation.getCompanyInBankDetails();
            }
        }
    }
    if (companyBankDetails == null) {
        companyBankDetails = company.getDefaultBankDetails();
        List<BankDetails> allowedBDs = Beans.get(PaymentModeService.class).getCompatibleBankDetailsList(paymentMode, company);
        if (!allowedBDs.contains(companyBankDetails)) {
            companyBankDetails = null;
        }
    }
    invoice.setCompanyBankDetails(companyBankDetails);
    if (partner.getBankDetailsList() != null && invoice.getBankDetails() == null) {
        invoice.setBankDetails(partner.getBankDetailsList().stream().filter(b -> b.getActive() && b.getIsDefault()).findFirst().orElse(null));
    }
    if (partner != null && !Strings.isNullOrEmpty(partner.getInvoiceComments())) {
        invoice.setNote(partner.getInvoiceComments());
    }
    invoice.setInvoicesCopySelect(getInvoiceCopy());
    initCollections(invoice);
    return invoice;
}
Also used : Company(com.axelor.apps.base.db.Company) AppAccountService(com.axelor.apps.account.service.app.AppAccountService) AccountConfig(com.axelor.apps.account.db.AccountConfig) PartnerService(com.axelor.apps.base.service.PartnerService) LoggerFactory(org.slf4j.LoggerFactory) PaymentCondition(com.axelor.apps.account.db.PaymentCondition) PaymentModeService(com.axelor.apps.account.service.payment.PaymentModeService) AddressService(com.axelor.apps.base.service.AddressService) ArrayList(java.util.ArrayList) InvoiceLineTax(com.axelor.apps.account.db.InvoiceLineTax) IExceptionMessage(com.axelor.apps.account.exception.IExceptionMessage) Strings(com.google.common.base.Strings) BigDecimal(java.math.BigDecimal) AccountingSituation(com.axelor.apps.account.db.AccountingSituation) AxelorException(com.axelor.exception.AxelorException) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) BlockingService(com.axelor.apps.base.service.BlockingService) ContextEntity(com.axelor.rpc.ContextEntity) I18n(com.axelor.i18n.I18n) AccountConfigRepository(com.axelor.apps.account.db.repo.AccountConfigRepository) TradingName(com.axelor.apps.base.db.TradingName) Logger(org.slf4j.Logger) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) InvoiceLineRepository(com.axelor.apps.account.db.repo.InvoiceLineRepository) MethodHandles(java.lang.invoke.MethodHandles) Invoice(com.axelor.apps.account.db.Invoice) TradingNameService(com.axelor.apps.base.service.TradingNameService) Currency(com.axelor.apps.base.db.Currency) InvoiceToolService(com.axelor.apps.account.service.invoice.InvoiceToolService) BlockingRepository(com.axelor.apps.base.db.repo.BlockingRepository) List(java.util.List) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Beans(com.axelor.inject.Beans) AccountConfigService(com.axelor.apps.account.service.config.AccountConfigService) LocalDate(java.time.LocalDate) PriceList(com.axelor.apps.base.db.PriceList) PaymentMode(com.axelor.apps.account.db.PaymentMode) AccountingSituationService(com.axelor.apps.account.service.AccountingSituationService) Address(com.axelor.apps.base.db.Address) Partner(com.axelor.apps.base.db.Partner) BankDetails(com.axelor.apps.base.db.BankDetails) TaxInvoiceLine(com.axelor.apps.account.service.invoice.generator.tax.TaxInvoiceLine) AxelorException(com.axelor.exception.AxelorException) PaymentModeService(com.axelor.apps.account.service.payment.PaymentModeService) Invoice(com.axelor.apps.account.db.Invoice) BankDetails(com.axelor.apps.base.db.BankDetails) AccountingSituation(com.axelor.apps.account.db.AccountingSituation) AccountConfigService(com.axelor.apps.account.service.config.AccountConfigService) AccountingSituationService(com.axelor.apps.account.service.AccountingSituationService) AccountConfig(com.axelor.apps.account.db.AccountConfig)

Example 25 with Company

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

the class InvoiceLineGenerator method computeCompanyTotal.

public void computeCompanyTotal(InvoiceLine invoiceLine) throws AxelorException {
    if (typeSelect == InvoiceLineRepository.TYPE_TITLE) {
        return;
    }
    Company company = invoice.getCompany();
    Currency companyCurrency = company.getCurrency();
    if (companyCurrency == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICE_LINE_GENERATOR_2), company.getName());
    }
    invoiceLine.setCompanyExTaxTotal(currencyService.getAmountCurrencyConvertedAtDate(invoice.getCurrency(), companyCurrency, exTaxTotal, today).setScale(AppBaseService.DEFAULT_NB_DECIMAL_DIGITS, RoundingMode.HALF_UP));
    invoiceLine.setCompanyInTaxTotal(currencyService.getAmountCurrencyConvertedAtDate(invoice.getCurrency(), companyCurrency, inTaxTotal, today).setScale(AppBaseService.DEFAULT_NB_DECIMAL_DIGITS, RoundingMode.HALF_UP));
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) Currency(com.axelor.apps.base.db.Currency)

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