package com.axelor.apps.supplychain.service;

import com.axelor.apps.base.db.Company;
import com.axelor.apps.stock.db.StockConfig;
import com.axelor.apps.stock.db.StockLocation;
import com.axelor.apps.stock.exception.IExceptionMessage;
import com.axelor.exception.AxelorException;
import com.axelor.exception.db.repo.TraceBackRepository;
import com.axelor.i18n.I18n;

public class StockConfigService {
    public StockConfig getStockConfig(Company company) throws AxelorException {

        StockConfig stockConfig = company.getStockConfig();

        if (stockConfig == null) {
            throw new AxelorException(
                    company,
                    TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
                    I18n.get(IExceptionMessage.STOCK_CONFIG_1),
                    company.getName());
        }

        return stockConfig;
    }

  public StockLocation getWarehouseNonCompliant(StockConfig stockConfig)
      throws AxelorException {
    if (stockConfig.getPickupDefaultStockLocation() == null) {
      throw new AxelorException(
          stockConfig,
          TraceBackRepository.CATEGORY_CONFIGURATION_ERROR,
          I18n.get(IExceptionMessage.STOCK_CONFIG_1),
          stockConfig.getCompany().getName());
    }
    return stockConfig.getWarehouseNonCompliant();
  }
}
