Ejemplo n.º 1
0
  /**
   * Returns an order line with everything correctly initialized. It does not call plug-ins to set
   * the price
   *
   * @param order
   * @param language
   * @param userId
   * @param currencyId
   * @param precision
   * @return
   */
  private static void populateWithSimplePrice(
      OrderDTO order,
      OrderLineDTO line,
      Integer language,
      Integer userId,
      Integer currencyId,
      Integer itemId,
      Integer precision) {

    ItemBL itemBl = new ItemBL(itemId);
    ItemDTO item = itemBl.getEntity();

    // it takes the line type of the first category this item belongs too...
    // TODO: redo, when item categories are redone
    Integer type = item.getItemTypes().iterator().next().getOrderLineTypeId();
    Boolean editable = OrderBL.lookUpEditable(type);

    if (line.getDescription() == null) {
      line.setDescription(item.getDescription(language));
    }

    if (line.getQuantity() == null) {
      line.setQuantity(new BigDecimal(1));
    }

    if (line.getPrice() == null) {
      BigDecimal price = item.getPercentage();
      if (price == null) {
        Date pricingDate = order != null ? order.getPricingDate() : null;
        price =
            itemBl.getPriceByCurrency(
                pricingDate, item, userId, currencyId); // basic price, ignoring current usage and
        // and quantity purchased for price calculations
      }
      line.setPrice(price);
    }

    if (line.getAmount() == null) {
      BigDecimal additionAmount = item.getPercentage(); // percentage ignores the quantity
      if (additionAmount == null) {
        // normal price, multiply by quantity
        additionAmount = line.getPrice().setScale(precision, CommonConstants.BIGDECIMAL_ROUND);
        additionAmount = additionAmount.multiply(line.getQuantity());
      }
      line.setAmount(additionAmount.setScale(precision, CommonConstants.BIGDECIMAL_ROUND));
    }

    line.setCreateDatetime(null);
    line.setDeleted(0);
    line.setTypeId(type);
    line.setEditable(editable);
    line.setItem(item);
  }
  /* (non-Javadoc)
   * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
   */
  public int compare(InvoiceLineDTO perA, InvoiceLineDTO perB) {
    int retValue;

    // the line type should tell first
    if (perA.getOrderPosition() == perB.getOrderPosition()) {

      try {
        if (perA.getTypeId() == Constants.INVOICE_LINE_TYPE_SUB_ACCOUNT
            && perB.getTypeId() == Constants.INVOICE_LINE_TYPE_SUB_ACCOUNT) {
          // invoice lines have to be grouped by user
          // find out both users
          retValue = perA.getSourceUserId().compareTo(perB.getSourceUserId());
          /*
          Logger.getLogger(InvoiceLineComparator.class).debug(
                  "Testing two sub account lines. a.userid " +
                  perA.getSourceUserId() + " b.userid " + perB.getSourceUserId() +
                  " result " + retValue);
                  */
          if (retValue != 0) {
            // these are lines for two different users, so
            // they are different enough now
            return retValue;
          }
        }
        // use the number
        if (perA.getItem() != null && perB.getItem() != null) {
          ItemBL itemA = new ItemBL(perA.getItem());
          ItemBL itemB = new ItemBL(perB.getItem());
          if (itemA.getEntity().getNumber() == null && itemB.getEntity().getNumber() == null) {
            retValue =
                new Integer(perA.getItem().getId()).compareTo(new Integer(perB.getItem().getId()));
          } else if (itemA.getEntity().getNumber() == null) {
            retValue = 1;
          } else if (itemB.getEntity().getNumber() == null) {
            retValue = -1;
          } else {
            // none are null
            retValue = itemA.getEntity().getNumber().compareTo(itemB.getEntity().getNumber());
          }
        } else {
          retValue = 0;
        }
      } catch (Exception e) {
        Logger.getLogger(InvoiceLineComparator.class)
            .error("Comparing invoice lines " + perA + " " + perB, e);
        retValue = 0;
      }
    } else {
      retValue = new Integer(perA.getOrderPosition()).compareTo(perB.getOrderPosition());
    }
    /*
            Logger.getLogger(InvoiceLineComparator.class).debug(
                    "Comparing " + perA.getId() + " " + perB.getId() +
                    " result " + retValue);
    */
    return retValue;
  }
Ejemplo n.º 3
0
  public void createLines(NewInvoiceDTO newInvoice) {
    Collection invoiceLines = invoice.getInvoiceLines();

    // Now create all the invoice lines, from the lines in the DTO
    // put there by the invoice composition pluggable tasks
    InvoiceLineDAS invoiceLineDas = new InvoiceLineDAS();

    // get the result DTO lines
    Iterator dueInvoiceLines = newInvoice.getResultLines().iterator();
    // go over the DTO lines, creating one invoice line for each

    // #2196 - GET Invoice Rounding Preference for entity entityId
    PreferenceBL pref = new PreferenceBL();
    Integer entityId = newInvoice.getEntityId();
    if (null == entityId) {
      entityId = newInvoice.getBaseUser().getEntity().getId();
    }

    int decimals = Constants.BIGDECIMAL_SCALE;
    try {
      pref.set(entityId, Constants.PREFERENCE_INVOICE_DECIMALS);
      decimals = pref.getInt();
    } catch (EmptyResultDataAccessException e) {
      // ignore
    }
    // #2196

    while (dueInvoiceLines.hasNext()) {
      InvoiceLineDTO lineToAdd = (InvoiceLineDTO) dueInvoiceLines.next();
      // define if the line is a percentage or not
      lineToAdd.setIsPercentage(0);
      if (lineToAdd.getItem() != null) {
        try {
          ItemBL item = new ItemBL(lineToAdd.getItem());
          if (item.getEntity().getPercentage() != null) {
            lineToAdd.setIsPercentage(1);
          }
        } catch (SessionInternalError e) {
          LOG.error("Could not find item to create invoice line " + lineToAdd.getItem().getId());
        }
      }

      // #2196 - Use Invoice Rounding Preference to round Invoice Lines
      if (null != lineToAdd.getAmount()) {
        lineToAdd.setAmount(lineToAdd.getAmount().setScale(decimals, Constants.BIGDECIMAL_ROUND));
      }
      // #2196

      // create the database row
      InvoiceLineDTO newLine =
          invoiceLineDas.create(
              lineToAdd.getDescription(),
              lineToAdd.getAmount(),
              lineToAdd.getQuantity(),
              lineToAdd.getPrice(),
              lineToAdd.getTypeId(),
              lineToAdd.getItem(),
              lineToAdd.getSourceUserId(),
              lineToAdd.getIsPercentage());

      // update the invoice-lines relationship
      newLine.setInvoice(invoice);
      invoiceLines.add(newLine);
    }
    getHome().save(invoice);
    EventManager.process(new NewInvoiceEvent(invoice));
  }
Ejemplo n.º 4
0
  protected void processRules(OrderDTO newOrder) throws TaskException {
    // now we have the line with good defaults, the order and the item
    // These have to be visible to the rules
    KnowledgeBase knowledgeBase;
    try {
      knowledgeBase = readKnowledgeBase();
    } catch (Exception e) {
      throw new TaskException(e);
    }
    session = knowledgeBase.newStatefulKnowledgeSession();
    List<Object> rulesMemoryContext = new ArrayList<Object>();
    rulesMemoryContext.add(helperOrder);

    // add OrderDTO to rules memory context
    newOrder.setCurrency(new CurrencyDAS().find(newOrder.getCurrency().getId()));
    if (newOrder.getCreateDate() == null) {
      newOrder.setCreateDate(new Date());
    }
    rulesMemoryContext.add(newOrder);

    for (OrderLineDTO line : newOrder.getLines()) {
      if (line.getItem() != null) {
        ItemBL item = new ItemBL(line.getItemId());
        rulesMemoryContext.add(
            item.getDTO(
                helperOrder.getLanguage(),
                helperOrder.getUserId(),
                helperOrder.getEntityId(),
                helperOrder.getCurrencyId()));
      }
      rulesMemoryContext.add(line);
    }

    if (newOrder.getPricingFields() != null && newOrder.getPricingFields().size() > 0) {
      for (PricingField pf : newOrder.getPricingFields()) {
        rulesMemoryContext.add(pf);
      }
    }
    try {
      Integer userId = newOrder.getBaseUserByUserId().getId();
      UserDTOEx user = DTOFactory.getUserDTOEx(userId);
      rulesMemoryContext.add(user);
      ContactBL contact = new ContactBL();
      contact.set(userId);
      ContactDTOEx contactDTO = contact.getDTO();
      rulesMemoryContext.add(contactDTO);

      // Add the subscriptions
      OrderBL order = new OrderBL();
      for (OrderDTO myOrder : order.getActiveRecurringByUser(userId)) {
        for (OrderLineDTO myLine : myOrder.getLines()) {
          rulesMemoryContext.add(new Subscription(myLine));
        }
      }
    } catch (Exception e) {
      throw new TaskException(e);
    }
    session.setGlobal("order", helperOrder);
    // then execute the rules

    executeStatefulRules(session, rulesMemoryContext);
  }