Esempio n. 1
0
 private void stockRollBack(Sale sale) {
   for (SaleProductService saleProductService : sale.getProductsServices()) {
     if (saleProductService.getProduct() != null) {
       Product product = saleProductService.getProduct();
       product.setQuantity(product.getQuantity() + saleProductService.getQuantity());
       productDAO.merge(product);
     }
   }
 }
Esempio n. 2
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);
  }
Esempio n. 3
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);
      }
    }
  }