/** Initialize component & values from m_mAssignment */
  private void setDisplay() {
    m_setting = true;

    //	Set Resource
    int S_Resource_ID = m_mAssignment.getS_Resource_ID();
    KeyNamePair[] resources = new KeyNamePair[m_lookup.size()];
    m_lookup.keySet().toArray(resources);
    for (int i = 0; i < resources.length; i++) {
      if (resources[i].getKey() == S_Resource_ID) {
        fResource.setSelectedIndex(i);
        break;
      }
    }
    ListItem listItem = fResource.getSelectedItem();
    KeyNamePair check = new KeyNamePair((Integer) listItem.getValue(), listItem.getLabel());
    if (check == null || check.getKey() != S_Resource_ID) {
      if (m_mAssignment.getS_ResourceAssignment_ID() == 0) // 	new record select first
      fResource.setSelectedItem(fResource.getSelectedItem()); // 	initiates UOM display
      else log.log(Level.SEVERE, "Resource not found ID=" + S_Resource_ID);
    }

    //	Set Date, Qty
    fDateFrom.setValue(m_mAssignment.getAssignDateFrom());
    fTimeFrom.setValue(m_mAssignment.getAssignDateFrom());
    fQty.setValue(m_mAssignment.getQty());

    //	Name, Description
    fName.setValue(m_mAssignment.getName());
    fDescription.setValue(m_mAssignment.getDescription());

    //	Set Editor to R/O if confirmed
    boolean readWrite = true;
    if (m_mAssignment.isConfirmed()) readWrite = false;
    confirmPanel.getButton("Cancel").setVisible(readWrite);
    fResource.setEnabled(readWrite);
    fDateFrom.setReadonly(!readWrite);
    fQty.setEnabled(readWrite);

    m_setting = false;
  } //	dynInit
  @Override
  public String getSQLWhere() {
    StringBuffer sql = new StringBuffer();
    if (txtDocumentNo.getText().length() > 0) sql.append(" AND UPPER(o.DocumentNo) LIKE ?");
    if (txtDescription.getText().length() > 0) sql.append(" AND UPPER(o.Description) LIKE ?");
    if (txtOrderRef.getText().length() > 0) sql.append(" AND UPPER(o.POReference) LIKE ?");
    //
    if (editorBPartner.getValue() != null) sql.append(" AND o.C_BPartner_ID=?");
    //
    Date fromDate = null;
    Date toDate = null;
    try {
      fromDate = dateFrom.getValue();
    } catch (WrongValueException e) {

    }
    try {
      toDate = dateTo.getValue();
    } catch (WrongValueException e) {

    }
    if (fromDate == null && toDate != null) {
      sql.append(" AND TRUNC(o.DateOrdered) <= ?");
    } else if (fromDate != null && toDate == null) {
      sql.append(" AND TRUNC(o.DateOrdered) >= ?");
    } else if (fromDate != null && toDate != null) {
      sql.append(" AND TRUNC(o.DateOrdered) BETWEEN ? AND ?");
    }
    //
    Double fromAmount = null;
    Double toAmount = null;
    if (amountFrom.getText() != null && amountFrom.getText().trim().length() > 0) {
      try {
        fromAmount = Double.parseDouble(amountFrom.getText());
      } catch (NumberFormatException e) {

      }
    }
    if (amountTo.getText() != null && amountTo.getText().trim().length() > 0) {
      try {
        toAmount = Double.parseDouble(amountTo.getText());
      } catch (NumberFormatException e) {

      }
    }
    if (fromAmount == null && toAmount != null) {
      sql.append(" AND o.GrandTotal <= ?");
    } else if (fromAmount != null && toAmount == null) {
      sql.append(" AND o.GrandTotal >= ?");
    } else if (fromAmount != null && toAmount != null) {
      sql.append(" AND o.GrandTotal BETWEEN ? AND ?");
    }
    sql.append(" AND o.IsSOTrx=?");

    log.finer(sql.toString());
    return sql.toString();
  }
 /**
  * Check availability and insert record
  *
  * @return true if saved/updated
  */
 private boolean cmd_save() {
   log.config("");
   //	Set AssignDateTo
   Calendar date = new GregorianCalendar();
   getDateAndTimeFrom(date);
   Timestamp assignDateFrom = new Timestamp(date.getTimeInMillis());
   BigDecimal qty = fQty.getValue();
   KeyNamePair uom = (KeyNamePair) m_lookup.get(fResource.getSelectedItem());
   int minutes = MUOMConversion.convertToMinutes(Env.getCtx(), uom.getKey(), qty);
   Timestamp assignDateTo = TimeUtil.addMinutess(assignDateFrom, minutes);
   m_mAssignment.setAssignDateTo(assignDateTo);
   //
   //	m_mAssignment.dump();
   return m_mAssignment.save();
 } //	cmdSave
  public void onEvent(Event e) throws Exception {
    if (m_setting) return;
    //	Update Assignment
    ListItem listItem = fResource.getSelectedItem();
    KeyNamePair resource =
        listItem != null
            ? new KeyNamePair((Integer) listItem.getValue(), listItem.getLabel())
            : null;
    if (resource != null) {
      int S_Resource_ID = resource.getKey();
      m_mAssignment.setS_Resource_ID(S_Resource_ID);
    }

    Calendar date = new GregorianCalendar();
    getDateAndTimeFrom(date);

    Timestamp assignDateFrom = new Timestamp(date.getTimeInMillis());
    if (assignDateFrom != null) m_mAssignment.setAssignDateFrom(assignDateFrom);
    if (fQty.getValue() != null) {
      BigDecimal qty = fQty.getValue();
      m_mAssignment.setQty(qty);
    }
    m_mAssignment.setName((String) fName.getValue());
    m_mAssignment.setDescription((String) fDescription.getValue());

    //	Resource - Look up UOM
    if (e.getTarget() == fResource) {
      Object o = m_lookup.get(fResource.getSelectedItem());
      if (o == null) lUOM.setValue(" ? ");
      else lUOM.setValue(o.toString());
    }

    //	Zoom - InfoResource
    else if (e.getTarget().getId().equals("Zoom")) {
      InfoSchedule is = new InfoSchedule(m_mAssignment, true);
      if (is.getMResourceAssignment() != null) {
        m_mAssignment = is.getMResourceAssignment();
        //	setDisplay();
        detach();
      }
      is = null;
    }

    //	cancel - return
    else if (e.getTarget().getId().equals("Cancel")) {
      m_cancel = true;
      detach();
    }

    //	delete - delete and return
    else if (e.getTarget().getId().equals("Delete")) {
      if (m_mAssignment.delete(true)) {
        m_mAssignment = null;
        detach();
      } else FDialog.error(0, this, "ResourceAssignmentNotDeleted");
    }

    //	OK - Save
    else if (e.getTarget().getId().equals("Ok")) {
      if (cmd_save()) detach();
    }
  }
  @Override
  protected void setParameters(PreparedStatement pstmt, boolean forCount) throws SQLException {
    int index = 1;
    if (txtDocumentNo.getText().length() > 0) pstmt.setString(index++, getSQLText(txtDocumentNo));
    if (txtDescription.getText().length() > 0) pstmt.setString(index++, getSQLText(txtDescription));
    if (txtOrderRef.getText().length() > 0) pstmt.setString(index++, getSQLText(txtOrderRef));
    //
    if (editorBPartner.getValue() != null) {
      Integer bp = (Integer) editorBPartner.getValue();
      pstmt.setInt(index++, bp.intValue());
      log.fine("BPartner=" + bp);
    }
    //

    Date fromD = null;
    Date toD = null;
    Timestamp from = null;
    Timestamp to = null;
    try {
      if (dateFrom.getValue() != null) {
        fromD = dateFrom.getValue();
        from = new Timestamp(fromD.getTime());
      }
    } catch (WrongValueException e) {

    }
    try {
      if (dateTo.getValue() != null) {
        toD = dateTo.getValue();
        to = new Timestamp(toD.getTime());
      }
    } catch (WrongValueException e) {

    }

    log.fine("Date From=" + from + ", To=" + to);
    if (from == null && to != null) {
      pstmt.setTimestamp(index++, to);
    } else if (from != null && to == null) {
      pstmt.setTimestamp(index++, from);
    } else if (from != null && to != null) {
      pstmt.setTimestamp(index++, from);
      pstmt.setTimestamp(index++, to);
    }

    //
    BigDecimal fromBD = null;
    BigDecimal toBD = null;
    Double fromAmt = null;
    Double toAmt = null;

    if (amountFrom.getText() != null && amountFrom.getText().trim().length() > 0) {
      try {
        fromAmt = Double.parseDouble(amountFrom.getText());
        fromBD = BigDecimal.valueOf(fromAmt);
      } catch (Exception e) {

      }
    }

    if (amountTo.getText() != null && amountTo.getText().trim().length() > 0) {
      try {
        toAmt = Double.parseDouble(amountTo.getText());
        toBD = BigDecimal.valueOf(toAmt);
      } catch (Exception e) {

      }
    }

    if (fromBD == null && toBD != null) {
      pstmt.setBigDecimal(index++, toBD);
    } else if (fromBD != null && toBD == null) {
      pstmt.setBigDecimal(index++, fromBD);
    } else if (fromBD != null && toBD != null) {
      pstmt.setBigDecimal(index++, fromBD);
      pstmt.setBigDecimal(index++, toBD);
    }

    pstmt.setString(index++, isSoTrx.isChecked() ? "Y" : "N");
  }
  private void initLayout() {
    txtDocumentNo.setWidth("100%");
    txtDescription.setWidth("100%");
    txtOrderRef.setWidth("100%");
    dateFrom.setWidth("165px");
    dateTo.setWidth("165px");
    amountFrom.getDecimalbox().setWidth("155px");
    amountTo.getDecimalbox().setWidth("155px");

    Grid grid = GridFactory.newGridLayout();

    Rows rows = new Rows();
    grid.appendChild(rows);

    Row row = new Row();
    rows.appendChild(row);
    row.appendChild(lblDocumentNo.rightAlign());
    row.appendChild(txtDocumentNo);
    row.appendChild(editorBPartner.getLabel().rightAlign());
    row.appendChild(editorBPartner.getComponent());
    row.appendChild(isSoTrx);

    row = new Row();
    row.setSpans("1, 1, 1, 2");
    rows.appendChild(row);
    row.appendChild(lblDescription.rightAlign());
    row.appendChild(txtDescription);
    row.appendChild(lblDateOrdered.rightAlign());
    Hbox hbox = new Hbox();
    hbox.appendChild(dateFrom);
    hbox.appendChild(new Label("-"));
    hbox.appendChild(dateTo);
    row.appendChild(hbox);

    row = new Row();
    row.setSpans("1, 1, 1, 2");
    rows.appendChild(row);
    row.appendChild(lblOrderRef.rightAlign());
    row.appendChild(txtOrderRef);
    row.appendChild(lblGrandTotal.rightAlign());
    hbox = new Hbox();
    hbox.appendChild(amountFrom);
    hbox.appendChild(new Label("-"));
    hbox.appendChild(amountTo);
    row.appendChild(hbox);

    layout = new Borderlayout();
    layout.setWidth("100%");
    layout.setHeight("100%");
    if (!isLookup()) {
      layout.setStyle("position: absolute");
    }
    this.appendChild(layout);

    North north = new North();
    layout.appendChild(north);
    north.appendChild(grid);

    Center center = new Center();
    layout.appendChild(center);
    center.setFlex(true);
    Div div = new Div();
    div.appendChild(contentPanel);
    if (isLookup()) contentPanel.setWidth("99%");
    else contentPanel.setStyle("width: 99%; margin: 0px auto;");
    contentPanel.setVflex(true);
    div.setStyle("width :100%; height: 100%");
    center.appendChild(div);

    South south = new South();
    layout.appendChild(south);
    southBody = new Vbox();
    southBody.setWidth("100%");
    south.appendChild(southBody);
    southBody.appendChild(confirmPanel);
    southBody.appendChild(new Separator());
    southBody.appendChild(statusBar);
  }