Search in sources :

Example 1 with PriceList

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

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

the class PriceListService method historizePriceList.

@Transactional
public PriceList historizePriceList(PriceList priceList) {
    PriceList historizedPriceList = priceListRepo.copy(priceList, false);
    historizedPriceList.setIsActive(false);
    List<PriceListLine> priceListLineList = priceList.getPriceListLineList();
    for (PriceListLine priceListLine : priceListLineList) {
        PriceListLine newPriceListLine = priceListLineRepo.copy(priceListLine, false);
        newPriceListLine.setPriceList(null);
        historizedPriceList.addPriceListLineListItem(newPriceListLine);
    }
    priceListRepo.save(historizedPriceList);
    priceList.addHistorizedPriceListItem(historizedPriceList);
    priceListRepo.save(priceList);
    return priceList;
}
Also used : PriceListLine(com.axelor.apps.base.db.PriceListLine) PriceList(com.axelor.apps.base.db.PriceList) Transactional(com.google.inject.persist.Transactional)

Example 3 with PriceList

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

the class PartnerPriceListServiceImpl method checkDates.

@Override
public void checkDates(PartnerPriceList partnerPriceList) throws AxelorException {
    Set<PriceList> priceListSet = partnerPriceList.getPriceListSet();
    if (priceListSet == null) {
        return;
    }
    Set<PriceList> sortedPriceListSet = priceListSet.stream().sorted(Comparator.comparing(priceList -> priceList.getApplicationBeginDate() != null ? priceList.getApplicationBeginDate() : LocalDate.MIN)).collect(Collectors.toSet());
    LocalDate beginDate;
    LocalDate previousEndDate = LocalDate.MIN;
    String previousTitle = "";
    for (PriceList priceList : sortedPriceListSet) {
        beginDate = priceList.getApplicationBeginDate() != null ? priceList.getApplicationBeginDate() : LocalDate.MIN;
        if (beginDate.compareTo(previousEndDate) < 0) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, String.format(I18n.get(IExceptionMessage.PARTNER_PRICE_LIST_DATE_INCONSISTENT), previousTitle.replace("%", "%%"), priceList.getTitle().replace("%", "%%")), partnerPriceList);
        }
        previousEndDate = priceList.getApplicationEndDate() != null ? priceList.getApplicationEndDate() : LocalDate.MAX;
        previousTitle = priceList.getTitle();
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) PriceList(com.axelor.apps.base.db.PriceList) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList) LocalDate(java.time.LocalDate)

Example 4 with PriceList

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

the class TimesheetServiceImpl method createInvoiceLines.

@Override
public List<InvoiceLine> createInvoiceLines(Invoice invoice, List<TimesheetLine> timesheetLineList, int priority) throws AxelorException {
    List<InvoiceLine> invoiceLineList = new ArrayList<>();
    int count = 0;
    DateFormat ddmmFormat = new SimpleDateFormat("dd/MM");
    HashMap<String, Object[]> timeSheetInformationsMap = new HashMap<>();
    // Check if a consolidation by product and user must be done
    boolean consolidate = appHumanResourceService.getAppTimesheet().getConsolidateTSLine();
    for (TimesheetLine timesheetLine : timesheetLineList) {
        Object[] tabInformations = new Object[5];
        tabInformations[0] = timesheetLine.getProduct();
        tabInformations[1] = timesheetLine.getUser();
        // Start date
        tabInformations[2] = timesheetLine.getDate();
        // End date, useful only for consolidation
        tabInformations[3] = timesheetLine.getDate();
        tabInformations[4] = timesheetLine.getHoursDuration();
        String key = null;
        if (consolidate) {
            key = timesheetLine.getProduct().getId() + "|" + timesheetLine.getUser().getId();
            if (timeSheetInformationsMap.containsKey(key)) {
                tabInformations = timeSheetInformationsMap.get(key);
                // Update date
                if (timesheetLine.getDate().compareTo((LocalDate) tabInformations[2]) < 0) {
                    // If date is lower than start date then replace start date by this one
                    tabInformations[2] = timesheetLine.getDate();
                } else if (timesheetLine.getDate().compareTo((LocalDate) tabInformations[3]) > 0) {
                    // If date is upper than end date then replace end date by this one
                    tabInformations[3] = timesheetLine.getDate();
                }
                tabInformations[4] = ((BigDecimal) tabInformations[4]).add(timesheetLine.getHoursDuration());
            } else {
                timeSheetInformationsMap.put(key, tabInformations);
            }
        } else {
            key = String.valueOf(timesheetLine.getId());
            timeSheetInformationsMap.put(key, tabInformations);
        }
        timesheetLine.setInvoiced(true);
    }
    for (Object[] timesheetInformations : timeSheetInformationsMap.values()) {
        String strDate = null;
        Product product = (Product) timesheetInformations[0];
        User user = (User) timesheetInformations[1];
        LocalDate startDate = (LocalDate) timesheetInformations[2];
        LocalDate endDate = (LocalDate) timesheetInformations[3];
        BigDecimal hoursDuration = (BigDecimal) timesheetInformations[4];
        PriceList priceList = Beans.get(PartnerPriceListService.class).getDefaultPriceList(invoice.getPartner(), PriceListRepository.TYPE_SALE);
        if (consolidate) {
            strDate = ddmmFormat.format(startDate) + " - " + ddmmFormat.format(endDate);
        } else {
            strDate = ddmmFormat.format(startDate);
        }
        invoiceLineList.addAll(this.createInvoiceLine(invoice, product, user, strDate, hoursDuration, priority * 100 + count, priceList));
        count++;
    }
    return invoiceLineList;
}
Also used : User(com.axelor.auth.db.User) TimesheetLine(com.axelor.apps.hr.db.TimesheetLine) PartnerPriceListService(com.axelor.apps.base.service.PartnerPriceListService) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(com.axelor.apps.base.db.Product) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) PriceList(com.axelor.apps.base.db.PriceList)

Example 5 with PriceList

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

the class PriceListController method historizePriceList.

public void historizePriceList(ActionRequest request, ActionResponse response) {
    PriceList priceList = request.getContext().asType(PriceList.class);
    priceList = Beans.get(PriceListRepository.class).find(priceList.getId());
    priceList = Beans.get(PriceListService.class).historizePriceList(priceList);
    response.setReload(true);
}
Also used : PriceList(com.axelor.apps.base.db.PriceList)

Aggregations

PriceList (com.axelor.apps.base.db.PriceList)24 AxelorException (com.axelor.exception.AxelorException)13 ArrayList (java.util.ArrayList)12 Company (com.axelor.apps.base.db.Company)11 Partner (com.axelor.apps.base.db.Partner)11 List (java.util.List)11 Currency (com.axelor.apps.base.db.Currency)9 PriceListLine (com.axelor.apps.base.db.PriceListLine)7 Wizard (com.axelor.apps.base.db.Wizard)7 ActionViewBuilder (com.axelor.meta.schema.actions.ActionView.ActionViewBuilder)7 LocalDate (java.time.LocalDate)7 Map (java.util.Map)7 BigDecimal (java.math.BigDecimal)6 Invoice (com.axelor.apps.account.db.Invoice)5 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)5 PaymentMode (com.axelor.apps.account.db.PaymentMode)5 SaleOrder (com.axelor.apps.sale.db.SaleOrder)5 PaymentCondition (com.axelor.apps.account.db.PaymentCondition)4 PartnerPriceList (com.axelor.apps.base.db.PartnerPriceList)4 Product (com.axelor.apps.base.db.Product)4