Ejemplo n.º 1
0
  protected void processRules(OrderDTO order, Integer userId) throws TaskException {

    rulesMemoryContext.add(order);

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

    // needed for calls to 'rateOrder'
    if (order.getPricingFields() != null) {
      for (PricingField field : order.getPricingFields()) {
        rulesMemoryContext.add(field);
      }
    }

    try {
      UserDTOEx user = DTOFactory.getUserDTOEx(userId);
      rulesMemoryContext.add(user);
      ContactBL contact = new ContactBL();
      contact.set(userId);
      ContactDTOEx contactDTO = contact.getDTO();
      rulesMemoryContext.add(contactDTO);
    } catch (Exception e) {
      throw new TaskException(e);
    }

    executeRules();
  }
Ejemplo n.º 2
0
  /**
   * @param userId
   * @param newInvoice
   * @param process It can be null.
   */
  public void create(
      Integer userId, NewInvoiceDTO newInvoice, BillingProcessDTO process, Integer executorUserId) {
    // find out the entity id
    PreferenceBL pref = new PreferenceBL();
    UserBL user = null;
    Integer entityId;
    if (process != null) {
      entityId = process.getEntity().getId();
    } else {
      // this is a manual invoice, there's no billing process
      user = new UserBL(userId);
      entityId = user.getEntityId(userId);
    }

    // verify if this entity is using the 'continuous invoice date'
    // preference
    try {
      pref.set(entityId, Constants.PREFERENCE_CONTINUOUS_DATE);

      if (StringUtils.isNotBlank(pref.getString())) {
        Date lastDate = com.sapienter.jbilling.common.Util.parseDate(pref.getString());
        LOG.debug("Last date invoiced: " + lastDate);

        if (lastDate.after(newInvoice.getBillingDate())) {
          LOG.debug(
              "Due date is before the last recorded date. Moving due date forward for continuous invoice dates.");
          newInvoice.setBillingDate(lastDate);

        } else {
          // update the lastest date only if this is not a review
          if (newInvoice.getIsReview() == null || newInvoice.getIsReview() == 0) {
            pref.createUpdateForEntity(
                entityId,
                Constants.PREFERENCE_CONTINUOUS_DATE,
                com.sapienter.jbilling.common.Util.parseDate(newInvoice.getBillingDate()));
          }
        }
      }
    } catch (EmptyResultDataAccessException e) {
      // not interested, ignore
    }

    // in any case, ensure that the due date is => that invoice date
    if (newInvoice.getDueDate().before(newInvoice.getBillingDate())) {
      LOG.debug("Due date before billing date, moving date up to billing date.");
      newInvoice.setDueDate(newInvoice.getBillingDate());
    }

    // ensure that there are only so many decimals in the invoice
    int decimals = Constants.BIGDECIMAL_SCALE;
    try {
      pref.set(entityId, Constants.PREFERENCE_INVOICE_DECIMALS);
      decimals = pref.getInt();
    } catch (EmptyResultDataAccessException e) {
      // not interested, ignore
    }

    LOG.debug("Rounding " + newInvoice.getTotal() + " to " + decimals + " decimals");
    if (newInvoice.getTotal() != null) {
      newInvoice.setTotal(newInvoice.getTotal().setScale(decimals, Constants.BIGDECIMAL_ROUND));
    }
    if (newInvoice.getBalance() != null) {
      newInvoice.setBalance(newInvoice.getBalance().setScale(decimals, Constants.BIGDECIMAL_ROUND));
    }

    // some API calls only accept ID's and do not pass meta-fields
    // update and validate meta-fields if they've been populated
    if (newInvoice.getMetaFields() != null && !newInvoice.getMetaFields().isEmpty()) {
      newInvoice.updateMetaFieldsWithValidation(entityId, newInvoice);
    }

    // create the invoice row
    invoice = invoiceDas.create(userId, newInvoice, process);

    // add delegated/included invoice links
    if (newInvoice.getIsReview() == 0) {
      for (InvoiceDTO dto : newInvoice.getInvoices()) {
        dto.setInvoice(invoice);
      }
    }

    // add the customer notes if it applies
    try {
      pref.set(entityId, Constants.PREFERENCE_SHOW_NOTE_IN_INVOICE);
    } catch (EmptyResultDataAccessException e) {
      // use the default then
    }

    if (pref.getInt() == 1) {
      if (user == null) {
        user = new UserBL(userId);
      }
      if (user.getEntity().getCustomer() != null
          && user.getEntity().getCustomer().getNotes() != null) {
        // append the notes if there's some text already there
        newInvoice.setCustomerNotes(
            (newInvoice.getCustomerNotes() == null)
                ? user.getEntity().getCustomer().getNotes()
                : newInvoice.getCustomerNotes() + " " + user.getEntity().getCustomer().getNotes());
      }
    }
    // notes might come from the customer, the orders, or both
    if (newInvoice.getCustomerNotes() != null && newInvoice.getCustomerNotes().length() > 0) {
      invoice.setCustomerNotes(newInvoice.getCustomerNotes());
    }

    // calculate/compose the number
    String numberStr = null;
    if (newInvoice.getIsReview() != null && newInvoice.getIsReview() == 1) {
      // invoices for review will be seen by the entity employees
      // so the entity locale will be used
      EntityBL entity = new EntityBL(entityId);
      ResourceBundle bundle = ResourceBundle.getBundle("entityNotifications", entity.getLocale());
      numberStr = bundle.getString("invoice.review.number");
    } else if (newInvoice.getPublicNumber() == null || newInvoice.getPublicNumber().length() == 0) {
      String prefix;
      try {
        pref.set(entityId, Constants.PREFERENCE_INVOICE_PREFIX);
        prefix = pref.getString();
        if (prefix == null) {
          prefix = "";
        }
      } catch (EmptyResultDataAccessException e) {
        prefix = "";
      }
      int number;
      try {
        pref.set(entityId, Constants.PREFERENCE_INVOICE_NUMBER);
        number = pref.getInt();
      } catch (EmptyResultDataAccessException e1) {
        number = 1;
      }

      numberStr = prefix + number;
      // update for the next time
      number++;
      pref.createUpdateForEntity(entityId, Constants.PREFERENCE_INVOICE_NUMBER, number);
    } else { // for upload of legacy invoices
      numberStr = newInvoice.getPublicNumber();
    }

    invoice.setPublicNumber(numberStr);

    // set the invoice's contact info with the current user's primary
    ContactBL contact = new ContactBL();
    contact.set(userId);
    contact.createForInvoice(contact.getDTO(), invoice.getId());

    // add a log row for convenience
    if (null != executorUserId) {
      eLogger.audit(
          executorUserId,
          userId,
          Constants.TABLE_INVOICE,
          invoice.getId(),
          EventLogger.MODULE_INVOICE_MAINTENANCE,
          EventLogger.ROW_CREATED,
          null,
          null,
          null);
    } else {
      eLogger.auditBySystem(
          entityId,
          userId,
          Constants.TABLE_INVOICE,
          invoice.getId(),
          EventLogger.MODULE_INVOICE_MAINTENANCE,
          EventLogger.ROW_CREATED,
          null,
          null,
          null);
    }
  }
Ejemplo n.º 3
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);
  }