コード例 #1
0
ファイル: InvoiceBL.java プロジェクト: rahith/ComtalkA-S
  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));
  }