示例#1
0
  public CachedRowSet getList(Integer orderId) throws SQLException, Exception {
    prepareStatement(InvoiceSQL.customerList);

    // find out the user from the order
    Integer userId;
    OrderBL order = new OrderBL(orderId);
    if (order.getEntity().getUser().getCustomer().getParent() == null) {
      userId = order.getEntity().getUser().getUserId();
    } else {
      userId = order.getEntity().getUser().getCustomer().getParent().getBaseUser().getUserId();
    }
    cachedResults.setInt(1, userId.intValue());
    execute();
    conn.close();
    return cachedResults;
  }
示例#2
0
  /**
   * Generates one single invoice for one single purchase order. This is meant to be called outside
   * the billing process.
   *
   * @param orderId
   * @return
   * @throws PluggableTaskException
   * @throws SessionInternalError
   */
  public InvoiceDTO generateInvoice(Integer orderId, Integer invoiceId)
      throws PluggableTaskException, SessionInternalError, SQLException {
    InvoiceDTO retValue = null;
    // find the order
    OrderBL order = new OrderBL(orderId);
    // define some data
    Integer entityId = order.getEntity().getUser().getEntity().getId();
    ConfigurationBL config = new ConfigurationBL(entityId);
    int maxPeriods = config.getEntity().getMaximumPeriods();
    boolean paymentApplication = config.getEntity().getAutoPaymentApplication() == 1;
    // The user could be the parent of a sub-account
    Integer userId = findUserId(order.getEntity());
    Date processDate = Calendar.getInstance().getTime();
    processDate = Util.truncateDate(processDate);
    // create the my invoice
    NewInvoiceDTO newInvoice = new NewInvoiceDTO();
    newInvoice.setDate(processDate);
    newInvoice.setIsReview(new Integer(0));
    // find the due date that applies
    TimePeriod period = order.getDueDate();
    newInvoice.setDueDatePeriod(period);
    // this is an isolated invoice that doesn't care about previous
    // overdue invoices
    newInvoice.setCarriedBalance(BigDecimal.ZERO);
    newInvoice.setInvoiceStatus(new InvoiceStatusDAS().find(Constants.INVOICE_STATUS_UNPAID));

    try {
      // put the order in the invoice using all the pluggable taks stuff
      addOrderToInvoice(entityId, order.getEntity(), newInvoice, processDate, maxPeriods);

      // this means that the user is trying to generate an invoice from
      // an order that the configurated tasks have rejected. Therefore
      // either this is the case an generating this invoice doesn't make
      // sense, or some business rules in the tasks have to be changed
      // (probably with a personalized task for this entity)
      if (newInvoice.getOrders().size() == 0) {
        return null;
      }

      // process events before orders added to invoice
      processOrderToInvoiceEvents(newInvoice, entityId);

      // generate the invoice lines
      composeInvoice(entityId, userId, newInvoice);

      // process events after orders added to invoice
      processOrderAddedOnInvoiceEvents(newInvoice, entityId);

      // put the resulting invoice in the database
      if (invoiceId == null) {
        // it is a new invoice from a singe order
        retValue =
            generateDBInvoice(userId, newInvoice, null, Constants.ORDER_PROCESS_ORIGIN_MANUAL);
        // try to get this new invioce paid by previously unlinked
        // payments
        if (paymentApplication) {
          PaymentBL pBL = new PaymentBL();
          pBL.automaticPaymentApplication(retValue);
        }
      } else {
        // it is an order going into an existing invoice
        InvoiceBL invoice = new InvoiceBL(invoiceId);
        boolean isUnpaid = invoice.getEntity().getToProcess() == 1;
        invoice.update(newInvoice);
        retValue = invoice.getEntity();
        createOrderProcess(newInvoice, retValue, null, Constants.ORDER_PROCESS_ORIGIN_MANUAL);
        eLogger.info(
            entityId,
            userId,
            invoiceId,
            EventLogger.MODULE_INVOICE_MAINTENANCE,
            EventLogger.INVOICE_ORDER_APPLIED,
            Constants.TABLE_INVOICE);
        // if the invoice is now not payable, take the user
        // out of ageing
        if (isUnpaid && retValue.getToProcess() == 0) {
          AgeingBL ageing = new AgeingBL();
          ageing.out(retValue.getBaseUser(), null);
        }
      }
    } catch (TaskException e) {
      // this means that the user is trying to generate an invoice from
      // an order that the configurated tasks have rejected. Therefore
      // either this is the case an generating this invoice doesn't make
      // sense, or some business rules in the tasks have to be changed
      // (probably with a personalized task for this entity)
      LOG.warn("Exception in generate invoice ", e);
    }

    if (retValue != null) {
      InvoicesGeneratedEvent generatedEvent = new InvoicesGeneratedEvent(entityId, null);
      generatedEvent.getInvoiceIds().add(retValue.getId());
      EventManager.process(generatedEvent);
    }

    return retValue;
  }