예제 #1
0
  public static InvoiceWS getWS(InvoiceDTO i) {
    if (i == null) {
      return null;
    }
    InvoiceWS retValue = new InvoiceWS();
    retValue.setId(i.getId());
    retValue.setCreateDateTime(i.getCreateDatetime());
    retValue.setCreateTimeStamp(i.getCreateTimestamp());
    retValue.setLastReminder(i.getLastReminder());
    retValue.setDueDate(i.getDueDate());
    retValue.setTotal(i.getTotal());
    retValue.setToProcess(i.getToProcess());
    retValue.setStatusId(i.getInvoiceStatus().getId());
    retValue.setBalance(i.getBalance());
    retValue.setCarriedBalance(i.getCarriedBalance());
    retValue.setInProcessPayment(i.getInProcessPayment());
    retValue.setDeleted(i.getDeleted());
    retValue.setPaymentAttempts(i.getPaymentAttempts());
    retValue.setIsReview(i.getIsReview());
    retValue.setCurrencyId(i.getCurrency().getId());
    retValue.setCustomerNotes(i.getCustomerNotes());
    retValue.setNumber(i.getPublicNumber());
    retValue.setOverdueStep(i.getOverdueStep());
    retValue.setUserId(i.getBaseUser().getId());

    Integer delegatedInvoiceId = i.getInvoice() == null ? null : i.getInvoice().getId();
    Integer userId = i.getBaseUser().getId();
    Integer payments[] = new Integer[i.getPaymentMap().size()];
    com.sapienter.jbilling.server.entity.InvoiceLineDTO invoiceLines[] =
        new com.sapienter.jbilling.server.entity.InvoiceLineDTO[i.getInvoiceLines().size()];
    Integer orders[] = new Integer[i.getOrderProcesses().size()];

    int f;
    f = 0;
    for (PaymentInvoiceMapDTO p : i.getPaymentMap()) {
      payments[f++] = p.getPayment().getId();
    }
    f = 0;
    for (OrderProcessDTO orderP : i.getOrderProcesses()) {
      orders[f++] = orderP.getPurchaseOrder().getId();
    }
    f = 0;
    for (InvoiceLineDTO line : i.getInvoiceLines()) {
      invoiceLines[f++] =
          new com.sapienter.jbilling.server.entity.InvoiceLineDTO(
              line.getId(),
              line.getDescription(),
              line.getAmount(),
              line.getPrice(),
              line.getQuantity(),
              line.getDeleted(),
              line.getItem() == null ? null : line.getItem().getId(),
              line.getSourceUserId(),
              line.getIsPercentage());
    }

    retValue.setDelegatedInvoiceId(delegatedInvoiceId);
    retValue.setUserId(userId);
    retValue.setPayments(payments);
    retValue.setInvoiceLines(invoiceLines);
    retValue.setOrders(orders);

    retValue.setMetaFields(MetaFieldBL.convertMetaFieldsToWS(new UserBL().getEntityId(userId), i));

    return retValue;
  }
예제 #2
0
  /**
   * This will remove all the records (sql delete, not just flag them). It will also update the
   * related orders if applicable
   */
  public void delete(Integer executorId) throws SessionInternalError {
    if (invoice == null) {
      throw new SessionInternalError("An invoice has to be set before delete");
    }

    // prevent a delegated Invoice from being deleted
    if (invoice.getDelegatedInvoiceId() != null && invoice.getDelegatedInvoiceId().intValue() > 0) {
      SessionInternalError sie =
          new SessionInternalError("A carried forward Invoice cannot be deleted");
      sie.setErrorMessages(
          new String[] {"InvoiceDTO,invoice,invoice.error.fkconstraint," + invoice.getId()});
      throw sie;
    }
    // start by updating purchase_order.next_billable_day if applicatble
    // for each of the orders included in this invoice
    for (OrderProcessDTO orderProcess : (Collection<OrderProcessDTO>) invoice.getOrderProcesses()) {
      OrderDTO order = orderProcess.getPurchaseOrder();
      if (order.getNextBillableDay() == null) {
        // the next billable day doesn't need updating
        if (order.getStatusId().equals(Constants.ORDER_STATUS_FINISHED)) {
          OrderBL orderBL = new OrderBL(order);
          orderBL.setStatus(null, Constants.ORDER_STATUS_ACTIVE);
        }
        continue;
      }
      // only if this invoice is the responsible for the order's
      // next billable day
      if (order.getNextBillableDay().equals(orderProcess.getPeriodEnd())) {
        order.setNextBillableDay(orderProcess.getPeriodStart());
        if (order.getStatusId().equals(Constants.ORDER_STATUS_FINISHED)) {
          OrderBL orderBL = new OrderBL(order);
          orderBL.setStatus(null, Constants.ORDER_STATUS_ACTIVE);
        }
      }
    }

    // go over the order process records again just to delete them
    // we are done with this order, delete the process row
    for (OrderProcessDTO orderProcess : (Collection<OrderProcessDTO>) invoice.getOrderProcesses()) {
      OrderDTO order = orderProcess.getPurchaseOrder();
      OrderProcessDAS das = new OrderProcessDAS();
      order.getOrderProcesses().remove(orderProcess);
      das.delete(orderProcess);
    }
    invoice.getOrderProcesses().clear();

    // get rid of the contact associated with this invoice
    try {
      ContactBL contact = new ContactBL();
      if (contact.setInvoice(invoice.getId())) {
        contact.delete();
      }
    } catch (Exception e1) {
      LOG.error("Exception deleting the contact of an invoice", e1);
    }

    // remove the payment link/s
    PaymentBL payment = new PaymentBL();
    Iterator<PaymentInvoiceMapDTO> it = invoice.getPaymentMap().iterator();
    while (it.hasNext()) {
      PaymentInvoiceMapDTO map = it.next();
      payment.removeInvoiceLink(map.getId());
      invoice.getPaymentMap().remove(map);
      // needed because the collection has changed
      it = invoice.getPaymentMap().iterator();
    }

    // log that this was deleted, otherwise there will be no trace
    if (executorId != null) {
      eLogger.audit(
          executorId,
          invoice.getBaseUser().getId(),
          Constants.TABLE_INVOICE,
          invoice.getId(),
          EventLogger.MODULE_INVOICE_MAINTENANCE,
          EventLogger.ROW_DELETED,
          null,
          null,
          null);
    }

    // before delete the invoice most delete the reference in table
    // PAYMENT_INVOICE
    new PaymentInvoiceMapDAS().deleteAllWithInvoice(invoice);

    Set<InvoiceDTO> invoices = invoice.getInvoices();
    if (invoices.size() > 0) {
      for (InvoiceDTO delegate : invoices) {
        // set status to unpaid as against carried
        delegate.setInvoiceStatus(new InvoiceStatusDAS().find(Constants.INVOICE_STATUS_UNPAID));
        // remove delegated invoice link
        delegate.setInvoice(null);
        getHome().save(delegate);
      }
    }

    // now delete the invoice itself
    getHome().delete(invoice);
    getHome().flush();
  }