Search in sources :

Example 1 with PartnerPriceList

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

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

the class PartnerPriceListController method checkDates.

/**
 * Called from partner price list form view call {@link
 * PartnerPriceListService#checkDates(PartnerPriceList)} return a warning with the response if
 * dates validation fails for the price list set.
 *
 * @param request
 * @param response
 */
public void checkDates(ActionRequest request, ActionResponse response) {
    PartnerPriceList partnerPriceList;
    Class partnerOrPriceLists = request.getContext().getContextClass();
    if (partnerOrPriceLists.equals(Partner.class)) {
        partnerPriceList = request.getContext().asType(Partner.class).getSalePartnerPriceList();
    } else if (partnerOrPriceLists.equals(PartnerPriceList.class)) {
        partnerPriceList = request.getContext().asType(PartnerPriceList.class);
    } else {
        return;
    }
    try {
        Beans.get(PartnerPriceListService.class).checkDates(partnerPriceList);
    } catch (Exception e) {
        response.setAlert(e.getMessage());
    }
}
Also used : PartnerPriceListService(com.axelor.apps.base.service.PartnerPriceListService) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList)

Example 3 with PartnerPriceList

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

the class PartnerPriceListServiceImpl method getDefaultPriceList.

@Override
public PriceList getDefaultPriceList(Partner partner, int priceListTypeSelect) {
    if (partner == null) {
        return null;
    }
    partner = Beans.get(PartnerRepository.class).find(partner.getId());
    PartnerPriceList partnerPriceList = getPartnerPriceList(partner, priceListTypeSelect);
    if (partnerPriceList == null) {
        return null;
    }
    Set<PriceList> priceListSet = partnerPriceList.getPriceListSet();
    if (priceListSet == null) {
        return null;
    }
    List<PriceList> priceLists = priceListSet.stream().filter(priceList -> (priceList.getApplicationBeginDate() == null || priceList.getApplicationBeginDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) <= 0) && (priceList.getApplicationEndDate() == null || priceList.getApplicationEndDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) >= 0)).collect(Collectors.toList());
    if (priceLists.size() == 1) {
        return priceLists.get(0);
    } else {
        return null;
    }
}
Also used : StringTool(com.axelor.apps.tool.StringTool) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) Inject(com.google.inject.Inject) Set(java.util.Set) PartnerRepository(com.axelor.apps.base.db.repo.PartnerRepository) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) Collectors(java.util.stream.Collectors) List(java.util.List) AxelorException(com.axelor.exception.AxelorException) Beans(com.axelor.inject.Beans) PartnerPriceListRepository(com.axelor.apps.base.db.repo.PartnerPriceListRepository) IExceptionMessage(com.axelor.apps.base.exceptions.IExceptionMessage) LocalDate(java.time.LocalDate) PriceList(com.axelor.apps.base.db.PriceList) I18n(com.axelor.i18n.I18n) Optional(java.util.Optional) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList) Comparator(java.util.Comparator) Partner(com.axelor.apps.base.db.Partner) PriceListRepository(com.axelor.apps.base.db.repo.PriceListRepository) AuthUtils(com.axelor.auth.AuthUtils) User(com.axelor.auth.db.User) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList) PriceList(com.axelor.apps.base.db.PriceList) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList)

Example 4 with PartnerPriceList

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

the class PartnerPriceListServiceImpl method getPriceListDomain.

public String getPriceListDomain(Partner partner, int priceListTypeSelect) {
    if (partner == null) {
        return "self.id IN (0)";
    }
    // get all non exclusive partner price lists
    List<PartnerPriceList> partnerPriceLists = Beans.get(PartnerPriceListRepository.class).all().filter("self.typeSelect = :_priceListTypeSelect " + "AND self.isExclusive = false").bind("_priceListTypeSelect", priceListTypeSelect).fetch();
    // get (maybe exclusive) list for the partner
    PartnerPriceList partnerPriceList = getPartnerPriceList(partner, priceListTypeSelect);
    if (partnerPriceList != null && partnerPriceList.getIsExclusive()) {
        partnerPriceLists.add(partnerPriceList);
    }
    if (partnerPriceLists.isEmpty()) {
        return "self.id IN (0)";
    }
    List<PriceList> priceLists = partnerPriceLists.stream().flatMap(partnerPriceList1 -> partnerPriceList1.getPriceListSet().stream()).filter(priceList -> priceList.getIsActive() && (priceList.getApplicationBeginDate() == null || priceList.getApplicationBeginDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) <= 0) && (priceList.getApplicationEndDate() == null || priceList.getApplicationEndDate().compareTo(appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))) >= 0)).collect(Collectors.toList());
    return "self.id IN (" + StringTool.getIdListString(priceLists) + ")";
}
Also used : StringTool(com.axelor.apps.tool.StringTool) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) Inject(com.google.inject.Inject) Set(java.util.Set) PartnerRepository(com.axelor.apps.base.db.repo.PartnerRepository) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) Collectors(java.util.stream.Collectors) List(java.util.List) AxelorException(com.axelor.exception.AxelorException) Beans(com.axelor.inject.Beans) PartnerPriceListRepository(com.axelor.apps.base.db.repo.PartnerPriceListRepository) IExceptionMessage(com.axelor.apps.base.exceptions.IExceptionMessage) LocalDate(java.time.LocalDate) PriceList(com.axelor.apps.base.db.PriceList) I18n(com.axelor.i18n.I18n) Optional(java.util.Optional) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList) Comparator(java.util.Comparator) Partner(com.axelor.apps.base.db.Partner) PriceListRepository(com.axelor.apps.base.db.repo.PriceListRepository) AuthUtils(com.axelor.auth.AuthUtils) User(com.axelor.auth.db.User) User(com.axelor.auth.db.User) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList) PriceList(com.axelor.apps.base.db.PriceList) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList)

Example 5 with PartnerPriceList

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

the class PartnerServiceImpl method getSalePriceList.

/**
 * Search for the sale price list for the current date in the partner.
 *
 * @param partner
 * @return the sale price list for the partner null if no active price list has been found
 */
@Override
public PriceList getSalePriceList(Partner partner) {
    PartnerPriceList partnerPriceList = partner.getSalePartnerPriceList();
    if (partnerPriceList == null) {
        return null;
    }
    Set<PriceList> priceListSet = partnerPriceList.getPriceListSet();
    if (priceListSet == null) {
        return null;
    }
    LocalDate today = Beans.get(AppBaseService.class).getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null));
    List<PriceList> candidatePriceListList = new ArrayList<>();
    for (PriceList priceList : priceListSet) {
        LocalDate beginDate = priceList.getApplicationBeginDate() != null ? priceList.getApplicationBeginDate() : LocalDate.MIN;
        LocalDate endDate = priceList.getApplicationEndDate() != null ? priceList.getApplicationEndDate() : LocalDate.MAX;
        if (beginDate.compareTo(today) <= 0 && today.compareTo(endDate) <= 0) {
            candidatePriceListList.add(priceList);
        }
    }
    // if we found multiple price list, then the user will have to select one
    if (candidatePriceListList.size() == 1) {
        return candidatePriceListList.get(0);
    } else {
        return null;
    }
}
Also used : User(com.axelor.auth.db.User) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList) ArrayList(java.util.ArrayList) PartnerPriceList(com.axelor.apps.base.db.PartnerPriceList) PriceList(com.axelor.apps.base.db.PriceList) LocalDate(java.time.LocalDate)

Aggregations

PartnerPriceList (com.axelor.apps.base.db.PartnerPriceList)5 PriceList (com.axelor.apps.base.db.PriceList)4 LocalDate (java.time.LocalDate)4 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)3 User (com.axelor.auth.db.User)3 AxelorException (com.axelor.exception.AxelorException)3 Partner (com.axelor.apps.base.db.Partner)2 PartnerPriceListRepository (com.axelor.apps.base.db.repo.PartnerPriceListRepository)2 PartnerRepository (com.axelor.apps.base.db.repo.PartnerRepository)2 PriceListRepository (com.axelor.apps.base.db.repo.PriceListRepository)2 IExceptionMessage (com.axelor.apps.base.exceptions.IExceptionMessage)2 StringTool (com.axelor.apps.tool.StringTool)2 AuthUtils (com.axelor.auth.AuthUtils)2 TraceBackRepository (com.axelor.exception.db.repo.TraceBackRepository)2 I18n (com.axelor.i18n.I18n)2 Beans (com.axelor.inject.Beans)2 Inject (com.google.inject.Inject)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Optional (java.util.Optional)2