示例#1
0
  @Override
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public Boolean remove(Long id) throws IabakoActionForbiddenException {

    saleDAO.deleteAttachedObjects(id);

    Sale sale = saleDAO.findById(id);
    if (sale == null) {
      log.warn("No sale found with id :" + id);
      return false;
    }

    stockRollBack(sale);

    Client client = sale.getClient();

    boolean result = super.remove(id);

    if (client != null) {
      clientService.calculateStatus(client);
    }

    trackingService.addTrackingToUserSession(TrackingType.saleDelete, sale);

    return result;
  }
示例#2
0
 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
 public List<Installment> getCustomInstallmentsDefinition(Sale sale) {
   if (sale == null || sale.getId() == null) {
     return null;
   }
   return saleDAO.getCustomInstallmentsDefinition(sale);
 }
示例#3
0
 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
 public List<Payment> getPayments(Sale sale) {
   if (sale == null || sale.getId() == null) {
     return null;
   }
   return saleDAO.getPayments(sale);
 }
示例#4
0
 private Sale checkIfQuote(Sale sale) {
   if (sale.getSaleQuoteId() != null) {
     Sale saleQuote = saleDAO.findById(Long.parseLong(sale.getSaleQuoteId()));
     if (saleQuote != null && saleQuote.isQuote()) {
       saleQuote.setSaleQuoteId(
           sale.getStatus() == FinancialStatusSale._0_draft
               ? "_draft_" + sale.getId()
               : sale.getId() + "");
       saleQuote.setStatus(FinancialStatusSale._2_quote_transformed);
     } else {
       sale.setSaleQuoteId(null);
     }
     saleDAO.merge(saleQuote);
     return saleQuote;
   }
   return null;
 }
示例#5
0
  /**
   * This method is used by SaleModificationForm
   *
   * @param sale
   * @return
   * @throws IabakoActionForbiddenException
   */
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public Sale save(Sale sale)
      throws IabakoActionForbiddenException, IabakoUniqueConstraintException {

    Enterprise enterprise = getEnterpriseFromSessionUser();
    if (sale.getId() != null && enterprise.getAllRelatedEnterprises().size() > 1) {
      // Do not change Enterprise!
      sale.setEnterprise(saleDAO.findById(sale.getId()).getEnterprise());
    } else {
      sale.setEnterprise(enterprise);
    }

    if (enterprise != null && enterprise.isDemo()) {
      throw new IabakoActionForbiddenException(
          "action_forbidden_for_demo_title", "action_forbidden_for_demo_text", true);
    }

    if (!getDAO().isUniqueNumber(sale)) {
      throw new IabakoUniqueConstraintException(
          messages.getLabel("validation_sale_unique_constraint_error_title"),
          messages.getLabel("validation_sale_unique_constraint_error_text", sale.getNumber()),
          true,
          true);
    }

    Sale saleFromDB = saleDAO.findById(sale.getId());
    setAttributesFromDB(sale, saleFromDB);

    if (!enterprise.getPaymentRequestConfig().isEnabled()
        || (sale.getClient() != null && sale.getClient().isPaymentRequestDisabled())) {
      // If paymentRequest disable, then do not change initial value (it doesn't come from client
      // world)
      sale.setPaymentRequestDisabled(saleFromDB.isPaymentRequestDisabled());
    }

    sale.setDate(GenericTools.getDateFromString(sale.getDateString()));

    trackingService.addTrackingToUserSession(TrackingType.saleModify, sale);

    return rawSave(sale);
  }
示例#6
0
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void duplicateEnMasse(QueryDefinitionDTO saleList)
      throws IabakoUniqueConstraintException, IabakoActionForbiddenException,
          IabakoPackageForbiddenException, IabakoStockException {
    saleList.setPageSize(-1);
    saleList.excludeAllColumns();
    queryDefinitionDAO.executeQueryDefinition(saleList);
    Map<Long, QueryResultDTO> sales = saleList.getResults();

    for (Long idSale : sales.keySet()) {
      Sale sale = saleDAO.findById(idSale);
      duplicate(sale);
    }
  }
示例#7
0
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public Sale duplicate(Sale sale)
      throws IabakoUniqueConstraintException, IabakoPackageForbiddenException,
          IabakoActionForbiddenException, IabakoStockException {
    // get ProdSer from DB
    sale.setProductsServices(saleDAO.getProductServiceList(sale));

    if (sale.getInstallmentsDefinition() != null
        && sale.getInstallmentsDefinition().getInstallmentsFrequency()
            == InstallmentsFrequency.custom
        && GenericTools.isEmpty(sale.getInstallmentsDefinition().getInstallments())) {
      // initialize empty relation coming from GWT world before clone() call
      sale.getInstallmentsDefinition()
          .setInstallments(saleDAO.getCustomInstallmentsDefinition(sale));
    }
    Sale newSale = sale.customClone();

    newSale.setDate(new Date());
    newSale.setDateString(GenericTools.formatDateToString(newSale.getDate()));

    newSale.setClient(sale.getClient());
    if (sale.getStatus() != FinancialStatusSale._0_draft) {
      newSale.setSaleStep(SaleStep.confirmation);
    }

    newSale.getPayments().clear();
    newSale.getInstallments().clear();

    List<Installment> installments =
        newSale.getInstallmentsDefinition() != null
            ? newSale.getInstallmentsDefinition().getInstallments()
            : null;

    return save(
        true, newSale, newSale.getProductsServices(), newSale.getExpenses(), null, installments);
  }
示例#8
0
 public List<Expense> getExpenses(Sale sale) {
   return saleDAO.getExpenses(sale);
 }
示例#9
0
  public void addProductsServicesToSale(
      boolean isDraft, Sale saleFromDB, List<SaleProductService> productsServices)
      throws IabakoStockException {

    Enterprise enterprise = getEnterpriseFromSessionUser();
    String productsServicesAsString = "";

    Map<Product, Double> stockReductionMap = new HashMap<Product, Double>();

    // 1. Create productService as String (for table results)
    saleFromDB.setProductsServicesAsString(null);

    saleDAO.cleanProductService(saleFromDB);

    for (SaleProductService productService : productsServices) {

      String quantityString =
          productService.getQuantity() == 0d
                  || productService.getQuantity()
                          / new Double(productService.getQuantity()).intValue()
                      == 1
              ? new Double(productService.getQuantity()).intValue() + ""
              : ServerTools.formatAmount(
                  saleFromDB.getEnterprise().getLanguage(), productService.getQuantity());

      if (productService.getProduct() != null) {

        if (!productService.getProduct().isStockDisabled()) {
          Double quantity = stockReductionMap.get(productService.getProduct());
          stockReductionMap.put(
              productService.getProduct(),
              quantity == null
                  ? productService.getQuantity()
                  : quantity + productService.getQuantity());
        }

        quantityString +=
            productService.getProduct().getPriceUnit() == PriceUnit.unit
                ? ""
                : messages.getLabel(productService.getProduct().getPriceUnit().getLabelKey());

        productsServicesAsString +=
            productService.getProduct().getName() + " (" + quantityString + ")\n";
      } else {
        productsServicesAsString +=
            productService.getService().getName() + " (" + quantityString + ")\n";
      }
    }
    if (!GenericTools.isEmpty(productsServicesAsString)) {
      productsServicesAsString =
          productsServicesAsString.substring(0, productsServicesAsString.lastIndexOf("\n"));
      productsServicesAsString =
          productsServicesAsString.length() > 1000
              ? productsServicesAsString.substring(0, 1000)
              : productsServicesAsString;
      saleFromDB.setProductsServicesAsString(productsServicesAsString);
      saleFromDB.setProductsServices(productsServices);
    }

    // 2. Reset Product/Service Enterprise (and do not change Enterprise!)
    boolean enterpriseHasChildren = enterprise.getAllRelatedEnterprises().size() > 1;
    Product prod;
    Service service;
    for (SaleProductService saleProductService : saleFromDB.getProductsServices()) {
      if ((prod = saleProductService.getProduct()) != null
          && saleProductService.getProduct().getEnterprise() == null) {
        saleProductService
            .getProduct()
            .setEnterprise(
                enterpriseHasChildren
                    ? productDAO.findById(prod.getId()).getEnterprise()
                    : enterprise);

      } else if ((service = saleProductService.getService()) != null
          && saleProductService.getService().getEnterprise() == null) {
        saleProductService
            .getService()
            .setEnterprise(
                enterpriseHasChildren
                    ? serviceDAO.findById(service.getId()).getEnterprise()
                    : enterprise);
      }
    }

    // 3. Stock reduction
    if (isDraft) {
      return;
    }

    if (stockReductionMap.keySet().isEmpty()) {
      return;
    }

    ProductStockModification productStockModification =
        productDAO.persistProductStockModification(false);

    String errorMessage = "";
    for (Product product : stockReductionMap.keySet()) {
      if (product.getQuantity() < stockReductionMap.get(product)) {
        String unit = messages.getLabel(product.getPriceUnit().getLabelKey());
        unit = GenericTools.isEmpty(unit) ? " " + messages.getLabel("stock_unit") : unit;

        String stringQuantity =
            product.getQuantity() == 0d
                    || product.getQuantity() / new Double(product.getQuantity()).intValue() == 1
                ? new Double(product.getQuantity()).intValue() + ""
                : ServerTools.formatAmount(
                    saleFromDB.getEnterprise().getLanguage(), product.getQuantity());

        errorMessage +=
            "\n"
                + messages.getLabel(
                    "stock_sale_not_enough_text", product.getName(), stringQuantity, unit);
      }

      product.setQuantity(
          ServerTools.round(product.getQuantity() - stockReductionMap.get(product)));
      productDAO.merge(product);

      productDAO.trackStockModification(
          product,
          product.getQuantity(),
          stockReductionMap.get(product) * -1,
          productStockModification);
    }
    if (!GenericTools.isEmpty(errorMessage)) {
      errorMessage += messages.getLabel("stock_sale_not_enough_resolve_text");
      throw new IabakoStockException("stock_sale_not_enough_title", errorMessage, true);
    }

    // 4. Tag management
    for (SaleProductService saleProductService : saleFromDB.getProductsServices()) {
      if ((prod = saleProductService.getProduct()) != null
          && saleProductService.getProduct().getBusinessTag() != null) {
        BusinessTagSale businessTagSale = new BusinessTagSale();
        businessTagSale.setBusinessTag(prod.getBusinessTag());
        businessTagSale.setProduct(prod);
        businessTagSale.setSale(saleFromDB);
        saleFromDB.getBusinessTagList().add(businessTagSale);

      } else if ((service = saleProductService.getService()) != null
          && saleProductService.getService().getBusinessTag() != null) {
        BusinessTagSale businessTagSale = new BusinessTagSale();
        businessTagSale.setBusinessTag(service.getBusinessTag());
        businessTagSale.setService(service);
        businessTagSale.setSale(saleFromDB);
        saleFromDB.getBusinessTagList().add(businessTagSale);
      }
    }
  }
示例#10
0
 public List<SaleProductService> getProductServiceList(Sale sale) {
   return saleDAO.getProductServiceList(sale);
 }