Пример #1
0
public class WOrderCompletedTips extends Window implements EventListener, IExceptionTips {

  /** */
  private static final long serialVersionUID = 883114344266765693L;
  /** Tab */
  private GridTab m_mTab;

  private boolean m_initOK = false;
  /** Logger */
  private static CLogger log = CLogger.getCLogger(WExceptionTips.class);

  private Panel mainPanel = new Panel();
  private Borderlayout mainLayout = new Borderlayout();
  private WListbox xTable = ListboxFactory.newDataTable();
  private ConfirmPanel confirmPanel = new ConfirmPanel(true);

  private String errorMessage = "";

  public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
  }

  public WOrderCompletedTips(int WindowNo, GridTab mTab, WButtonEditor button) {
    super();
    this.setTitle(Msg.getMsg(Env.getCtx(), "WT_CantComplete"));
    this.setAttribute("mode", "modal");
    m_mTab = mTab;
    try {
      zkInit();
      m_initOK = dynInit(button); // Null Pointer if order/invoice not
      // saved yet
    } catch (Exception ex) {
      setErrorMessage(ex.getLocalizedMessage());
      log.log(Level.SEVERE, "ExceptionTips", ex);
      m_initOK = false;
    }
    //
    this.setHeight("400px");
    this.setWidth("600px");
    this.setBorder("normal");
  }

  private void zkInit() throws Exception {
    this.appendChild(mainPanel);
    mainPanel.appendChild(mainLayout);
    mainPanel.setStyle("width: 100%; height: 100%; padding: 0; margin: 0");
    mainLayout.setHeight("100%");
    mainLayout.setWidth("100%");
    Center center = new Center();
    mainLayout.appendChild(center);
    Panel p = new Panel();
    p.appendChild(xTable);
    xTable.setWidth("100%");
    xTable.setHeight("100%");
    p.setStyle("width: 100%; height: 100%; padding: 0; margin: 0");
    center.appendChild(p);
    //
    South south = new South();
    south.setStyle("border: none");
    mainLayout.appendChild(south);
    south.appendChild(confirmPanel);
    confirmPanel.addActionListener(this);
  } // jbInit

  private boolean dynInit(WButtonEditor button) throws Exception {
    // 获取海外入库单产生的异常
    Vector<Vector> data = getBExceptionData();
    if (data.size() <= 0) {
      // 当卖家海外仓入库单内所有单品状态都变更为"海外仓上架完成"时,将卖家海外仓入库单状态更新为"海外仓入库完成"
      if (m_mTab.getAD_Table_ID() == MOrder.Table_ID) {
        MOrder order = new MOrder(Env.getCtx(), m_mTab.getRecord_ID(), null);
        order.setStatus(MOrder.STATUS_ForeignWarehouseInboundCompleted);
        order.saveEx();
      } else if (m_mTab.getAD_Table_ID() == MWorkOrder.Table_ID) {
        MWorkOrder order = new MWorkOrder(Env.getCtx(), m_mTab.getRecord_ID(), null);
        order.setStatus(MWorkOrder.STATUS_Completed);
        order.saveEx();
      }

      // 不显示业务异常提示窗口
      return false;
    }

    ColumnInfo[] layout =
        new ColumnInfo[] {
          new ColumnInfo(Msg.translate(Env.getCtx(), "WT_ExceptionType"), ".", String.class),
          new ColumnInfo(Msg.translate(Env.getCtx(), "WT_ExceptionID"), ".", String.class)
        };
    xTable.prepareTable(layout, "", "", false, "");
    xTable.loadTable(data);
    // 显示业务异常提示窗口
    return true;
  } // dynInit

  @Override
  public void onEvent(Event e) {
    // Finish
    if (e.getTarget().getId().equals(ConfirmPanel.A_OK)) {
      dispose();
    } else if (e.getTarget().getId().equals(ConfirmPanel.A_CANCEL)) dispose();
  }

  private Vector<Vector> getBExceptionData() {
    Vector<Vector> data = new Vector<Vector>();

    final String whereClause =
        MBExceptionDetail.COLUMNNAME_Record_ID
            + " = "
            + m_mTab.getRecord_ID()
            + " AND "
            + MBExceptionDetail.COLUMNNAME_Status
            + "<>"
            + DB.TO_STRING(MBExceptionDetail.STATUS_Completed);
    MBExceptionDetail[] bExceptionDetails =
        MBExceptionDetail.getBExceptionDetail(Env.getCtx(), whereClause);
    for (MBExceptionDetail ex : bExceptionDetails) {

      Vector<String> row = new Vector<String>();
      row.add(ex.getWT_BExceptionType().getName());
      row.add(ex.getValue());
      data.add(row);
    }

    return data;
  }

  public boolean isInitOK() {
    return m_initOK;
  } // isInitOK

  @Override
  public boolean needSave() {
    return true;
  }

  @Override
  public String getErrorMessage() {
    return this.errorMessage;
  }
}
Пример #2
0
/**
 * Create Manual Payments From (AP) Invoices or (AR) Credit Memos. Allows user to select Invoices
 * for payment. When Processed, PaySelection is created and optionally posted/generated and printed
 *
 * @author Jorg Janke
 * @version $Id: VPaySelect.java,v 1.3 2006/07/30 00:51:28 jjanke Exp $
 */
public class WPaySelect extends PaySelect
    implements IFormController, EventListener, WTableModelListener, ASyncProcess {
  /** @todo withholding */

  /** */
  private static final long serialVersionUID = -6031404894392912610L;

  private CustomForm form = new CustomForm();

  //
  private Panel mainPanel = new Panel();
  private Borderlayout mainLayout = new Borderlayout();
  private Panel parameterPanel = new Panel();
  private Label labelBankAccount = new Label();
  private Listbox fieldBankAccount = ListboxFactory.newDropdownListbox();
  private Grid parameterLayout = GridFactory.newGridLayout();
  private Label labelBankBalance = new Label();
  private Label labelCurrency = new Label();
  private Label labelBalance = new Label();
  private Checkbox onlyDue = new Checkbox();
  private Label labelBPartner = new Label();
  private Listbox fieldBPartner = ListboxFactory.newDropdownListbox();
  private Label dataStatus = new Label();
  private WListbox miniTable = ListboxFactory.newDataTable();
  private ConfirmPanel commandPanel =
      new ConfirmPanel(true, false, false, false, false, false, false);
  private Button bCancel = commandPanel.getButton(ConfirmPanel.A_CANCEL);
  private Button bGenerate = commandPanel.createButton(ConfirmPanel.A_PROCESS);
  private Button bRefresh = commandPanel.createButton(ConfirmPanel.A_REFRESH);
  private Label labelPayDate = new Label();
  private WDateEditor fieldPayDate = new WDateEditor();
  private Label labelPaymentRule = new Label();
  private Listbox fieldPaymentRule = ListboxFactory.newDropdownListbox();
  private Label labelDtype = new Label();
  private Listbox fieldDtype = ListboxFactory.newDropdownListbox();
  private Panel southPanel;

  @SuppressWarnings("unused")
  private ProcessInfo m_pi;

  private boolean m_isLock;

  /** Initialize Panel */
  public WPaySelect() {
    try {
      zkInit();
      dynInit();

      loadBankInfo();
      southPanel.appendChild(new Separator());
      southPanel.appendChild(commandPanel);
    } catch (Exception e) {
      log.log(Level.SEVERE, "", e);
    }
  } //	init

  /**
   * Static Init
   *
   * @throws Exception
   */
  private void zkInit() throws Exception {
    //
    form.appendChild(mainPanel);
    mainPanel.appendChild(mainLayout);
    mainPanel.setStyle("width: 100%; height: 100%; padding: 0; margin: 0");
    mainLayout.setHeight("100%");
    mainLayout.setWidth("99%");
    parameterPanel.appendChild(parameterLayout);
    //
    labelBankAccount.setText(Msg.translate(Env.getCtx(), "C_BankAccount_ID"));
    fieldBankAccount.addActionListener(this);
    labelBPartner.setText(Msg.translate(Env.getCtx(), "C_BPartner_ID"));
    fieldBPartner.addActionListener(this);
    bRefresh.addActionListener(this);
    labelPayDate.setText(Msg.translate(Env.getCtx(), "PayDate"));
    labelPaymentRule.setText(Msg.translate(Env.getCtx(), "PaymentRule"));
    fieldPaymentRule.addActionListener(this);
    labelDtype.setText(Msg.translate(Env.getCtx(), "C_DocType_ID"));
    fieldDtype.addActionListener(this);
    //
    labelBankBalance.setText(Msg.translate(Env.getCtx(), "CurrentBalance"));
    labelBalance.setText("0");
    onlyDue.setText(Msg.getMsg(Env.getCtx(), "OnlyDue"));
    dataStatus.setText(" ");
    dataStatus.setPre(true);
    //
    bGenerate.addActionListener(this);
    bCancel.addActionListener(this);
    //
    North north = new North();
    north.setStyle("border: none");
    mainLayout.appendChild(north);
    north.appendChild(parameterPanel);

    Rows rows = parameterLayout.newRows();
    Row row = rows.newRow();
    row.appendChild(labelBankAccount.rightAlign());
    row.appendChild(fieldBankAccount);
    row.appendChild(labelBankBalance.rightAlign());
    Panel balancePanel = new Panel();
    balancePanel.appendChild(labelCurrency);
    balancePanel.appendChild(labelBalance);
    row.appendChild(balancePanel);
    row.appendChild(new Space());

    row = rows.newRow();
    row.appendChild(labelBPartner.rightAlign());
    row.appendChild(fieldBPartner);
    row.appendChild(new Space());
    row.appendChild(onlyDue);
    row.appendChild(new Space());

    row = rows.newRow();
    row.appendChild(labelDtype.rightAlign());
    row.appendChild(fieldDtype);
    row.appendChild(new Space());
    row.appendChild(new Space());
    row.appendChild(new Space());

    row = rows.newRow();
    row.appendChild(labelPayDate.rightAlign());
    row.appendChild(fieldPayDate.getComponent());
    row.appendChild(labelPaymentRule.rightAlign());
    row.appendChild(fieldPaymentRule);
    row.appendChild(bRefresh);

    South south = new South();
    south.setStyle("border: none");
    mainLayout.appendChild(south);
    southPanel = new Panel();
    southPanel.appendChild(dataStatus);
    south.appendChild(southPanel);
    Center center = new Center();
    mainLayout.appendChild(center);
    center.appendChild(miniTable);
    //
    commandPanel.addButton(bGenerate);
    commandPanel.getButton(ConfirmPanel.A_OK).setVisible(false);
  } //  jbInit

  /** Dynamic Init. - Load Bank Info - Load BPartner - Init Table */
  private void dynInit() {
    ArrayList<BankInfo> bankAccountData = getBankAccountData();
    for (BankInfo bi : bankAccountData) fieldBankAccount.appendItem(bi.toString(), bi);

    if (fieldBankAccount.getItemCount() == 0) FDialog.error(m_WindowNo, form, "VPaySelectNoBank");
    else fieldBankAccount.setSelectedIndex(0);

    ArrayList<KeyNamePair> bpartnerData = getBPartnerData();
    for (KeyNamePair pp : bpartnerData) fieldBPartner.appendItem(pp.getName(), pp);
    fieldBPartner.setSelectedIndex(0);

    ArrayList<KeyNamePair> docTypeData = getDocTypeData();
    for (KeyNamePair pp : docTypeData) fieldDtype.appendItem(pp.getName(), pp);

    prepareTable(miniTable);

    miniTable.getModel().addTableModelListener(this);
    //
    fieldPayDate.setMandatory(true);
    fieldPayDate.setValue(new Timestamp(System.currentTimeMillis()));
  } //  dynInit

  /** Load Bank Info - Load Info from Bank Account and valid Documents (PaymentRule) */
  private void loadBankInfo() {
    BankInfo bi = (BankInfo) fieldBankAccount.getSelectedItem().getValue();
    if (bi == null) return;
    labelCurrency.setText(bi.Currency);
    labelBalance.setText(m_format.format(bi.Balance));

    //  PaymentRule
    fieldPaymentRule.removeAllItems();

    ArrayList<ValueNamePair> paymentRuleData = getPaymentRuleData(bi);
    for (ValueNamePair vp : paymentRuleData) fieldPaymentRule.appendItem(vp.getName(), vp);
    fieldPaymentRule.setSelectedIndex(0);
  } //  loadBankInfo

  /** Query and create TableInfo */
  private void loadTableInfo() {
    Timestamp payDate = (Timestamp) fieldPayDate.getValue();
    miniTable.setColorCompare(payDate);
    log.config("PayDate=" + payDate);

    BankInfo bi = (BankInfo) fieldBankAccount.getSelectedItem().getValue();

    ValueNamePair paymentRule = (ValueNamePair) fieldPaymentRule.getSelectedItem().getValue();
    KeyNamePair bpartner = (KeyNamePair) fieldBPartner.getSelectedItem().getValue();
    KeyNamePair docType = (KeyNamePair) fieldDtype.getSelectedItem().getValue();

    loadTableInfo(bi, payDate, paymentRule, onlyDue.isSelected(), bpartner, docType, miniTable);

    calculateSelection();
  } //  loadTableInfo

  /** Dispose */
  public void dispose() {
    SessionManager.getAppDesktop().closeActiveWindow();
  } //	dispose

  /**
   * ************************************************************************ ActionListener
   *
   * @param e event
   */
  public void onEvent(Event e) {
    //  Update Bank Info
    if (e.getTarget() == fieldBankAccount) loadBankInfo();

    //  Generate PaySelection
    else if (e.getTarget() == bGenerate) {
      generatePaySelect();
    } else if (e.getTarget() == bCancel) dispose();

    //  Update Open Invoices
    else if (e.getTarget() == fieldBPartner
        || e.getTarget() == bRefresh
        || e.getTarget() == fieldDtype) loadTableInfo();
  } //  actionPerformed

  /**
   * Table Model Listener
   *
   * @param e event
   */
  public void tableChanged(WTableModelEvent e) {
    if (e.getColumn() == 0) calculateSelection();
  } //  valueChanged

  /** Calculate selected rows. - add up selected rows */
  public void calculateSelection() {
    dataStatus.setText(calculateSelection(miniTable));
    //
    bGenerate.setEnabled(m_noSelected != 0);
  } //  calculateSelection

  /** Generate PaySelection */
  private void generatePaySelect() {
    if (miniTable.getRowCount() == 0) return;
    miniTable.setSelectedIndices(new int[] {0});
    calculateSelection();
    if (m_noSelected == 0) return;

    String msg =
        generatePaySelect(
            miniTable,
            (ValueNamePair) fieldPaymentRule.getSelectedItem().getValue(),
            new Timestamp(fieldPayDate.getComponent().getValue().getTime()),
            (BankInfo) fieldBankAccount.getSelectedItem().getValue());

    if (msg != null && msg.length() > 0) {
      FDialog.error(m_WindowNo, form, "SaveError", msg);
      return;
    }

    //  Ask to Post it
    if (!FDialog.ask(m_WindowNo, form, "VPaySelectGenerate?", "(" + m_ps.getName() + ")")) return;

    //  Prepare Process
    int AD_Proces_ID = 155; // 	C_PaySelection_CreatePayment

    //	Execute Process
    ProcessModalDialog dialog =
        new ProcessModalDialog(
            this,
            m_WindowNo,
            AD_Proces_ID,
            X_C_PaySelection.Table_ID,
            m_ps.getC_PaySelection_ID(),
            false);
    if (dialog.isValid()) {
      try {
        dialog.setWidth("500px");
        dialog.setVisible(true);
        dialog.setPage(form.getPage());
        dialog.doModal();
      } catch (SuspendNotAllowedException e) {
        log.log(Level.SEVERE, e.getLocalizedMessage(), e);
      } catch (InterruptedException e) {
        log.log(Level.SEVERE, e.getLocalizedMessage(), e);
      }
    }
  } //  generatePaySelect

  /** Lock User Interface Called from the Worker before processing */
  public void lockUI(ProcessInfo pi) {
    if (m_isLock) return;
    m_isLock = true;
    Clients.showBusy(null, true);
  } //  lockUI

  /** Unlock User Interface. Called from the Worker when processing is done */
  public void unlockUI(ProcessInfo pi) {
    if (!m_isLock) return;
    m_isLock = false;
    m_pi = pi;
    Clients.showBusy(null, false);

    // TODO: The response returned is always Cancel
    //		if (!FDialog.ask(0, form, "VPaySelectPrint?", "(" + m_pi.getSummary() + ")"))
    //		{
    //			dispose();
    //			return;
    //		}

    this.dispose();

    //  Start PayPrint
    int AD_Form_ID = 106; // 	Payment Print/Export
    ADForm form = SessionManager.getAppDesktop().openForm(AD_Form_ID);
    if (m_ps != null) {
      WPayPrint pp = (WPayPrint) form.getICustomForm();
      pp.setPaySelection(m_ps.getC_PaySelection_ID());
    }
  }

  public void executeASync(ProcessInfo pi) {}

  public boolean isUILocked() {
    return m_isLock;
  }

  public ADForm getForm() {
    return form;
  }
} //  VPaySelect
Пример #3
0
/**
 * Price History for BPartner/Product This class is based on org.compiere.apps.search.InvoiceHistory
 * written by Jorg Janke
 *
 * @author <a href="mailto:[email protected]">Elaine</a>
 */
public class InvoiceHistory extends Window implements EventListener<Event> {
  /** */
  private static final long serialVersionUID = 8742214467478030802L;

  boolean showDetailATP = false;

  /**
   * Show History
   *
   * @param C_BPartner_ID partner
   * @param M_Product_ID product
   * @param M_Warehouse_ID warehouse
   * @param M_AttributeSetInstance_ID ASI
   */
  public InvoiceHistory(
      Window parent,
      int C_BPartner_ID,
      int M_Product_ID,
      int M_Warehouse_ID,
      int M_AttributeSetInstance_ID) {
    super();
    setTitle(Msg.getMsg(Env.getCtx(), "PriceHistory"));
    if (log.isLoggable(Level.CONFIG))
      log.config(
          "C_BPartner_ID="
              + C_BPartner_ID
              + ", M_Product_ID="
              + M_Product_ID
              + ", M_Warehouse_ID="
              + M_Warehouse_ID
              + ", M_AttributeSetInstance_ID="
              + M_AttributeSetInstance_ID);
    m_C_BPartner_ID = C_BPartner_ID;
    m_M_Product_ID = M_Product_ID;
    m_M_Warehouse_ID = M_Warehouse_ID;
    m_M_AttributeSetInstance_ID = M_AttributeSetInstance_ID;
    try {
      jbInit();
      dynInit();
    } catch (Exception ex) {
      log.log(Level.SEVERE, "", ex);
    }

    this.setSclass("popup-dialog");
    AEnv.showCenterWindow(parent, this);
    if (parent instanceof InfoProductWindow)
      showDetailATP = ((InfoProductWindow) parent).isShowDetailATP();
  } //	InvoiceHistory

  private int m_C_BPartner_ID;
  private int m_M_Product_ID;
  private int m_M_Warehouse_ID;
  private int m_M_AttributeSetInstance_ID;

  /** Logger */
  private static CLogger log = CLogger.getCLogger(InvoiceHistory.class);

  private Label label = new Label();
  //
  private ConfirmPanel confirmPanel = new ConfirmPanel();
  private Tabbox tabbox = new Tabbox();
  //
  private Tabpanel pricePane = new Tabpanel();
  private WListbox m_tablePrice = ListboxFactory.newDataTable();
  private ListModelTable m_modelPrice = null;

  private Tabpanel reservedPane = new Tabpanel();
  private WListbox m_tableReserved = ListboxFactory.newDataTable();
  private ListModelTable m_modelReserved = null;

  private Tabpanel orderedPane = new Tabpanel();
  private WListbox m_tableOrdered = ListboxFactory.newDataTable();
  private ListModelTable m_modelOrdered = null;

  private Tabpanel unconfirmedPane = new Tabpanel();
  private WListbox m_tableUnconfirmed = ListboxFactory.newDataTable();
  private ListModelTable m_modelUnconfirmed = null;

  private Tabpanel atpPane = new Tabpanel();
  private WListbox m_tableAtp = ListboxFactory.newDataTable();
  private ListModelTable m_modelAtp = null;

  /** Ststic Init */
  void jbInit() throws Exception {
    label.setText("Label");

    Tabs tabs = new Tabs();
    tabbox.appendChild(tabs);

    Tabpanels tabpanels = new Tabpanels();
    tabbox.appendChild(tabpanels);

    tabs.appendChild(new Tab(Msg.getMsg(Env.getCtx(), "PriceHistory")));
    tabs.appendChild(new Tab(Msg.translate(Env.getCtx(), "QtyReserved")));
    tabs.appendChild(new Tab(Msg.translate(Env.getCtx(), "QtyOrdered")));
    tabs.appendChild(new Tab(Msg.getMsg(Env.getCtx(), "QtyUnconfirmed")));

    if (m_M_Product_ID != 0) tabs.appendChild(new Tab(Msg.getMsg(Env.getCtx(), "ATP")));

    pricePane.setHeight("100%");
    pricePane.appendChild(m_tablePrice);
    tabpanels.appendChild(pricePane);

    reservedPane.setHeight("100%");
    reservedPane.appendChild(m_tableReserved);
    tabpanels.appendChild(reservedPane);

    orderedPane.setHeight("100%");
    orderedPane.appendChild(m_tableOrdered);
    tabpanels.appendChild(orderedPane);

    unconfirmedPane.setHeight("100%");
    unconfirmedPane.appendChild(m_tableUnconfirmed);
    tabpanels.appendChild(unconfirmedPane);

    if (m_M_Product_ID != 0) {
      atpPane.setHeight("100%");
      atpPane.appendChild(m_tableAtp);
      tabpanels.appendChild(atpPane);
    }

    tabbox.setSelectedIndex(0);
    tabbox.addEventListener(Events.ON_SELECT, this);
    confirmPanel.addActionListener(this);

    Borderlayout borderlayout = new Borderlayout();
    this.setWidth("700px");
    this.setHeight("400px");
    borderlayout.setStyle("border: none; position: relative");
    this.appendChild(borderlayout);
    this.setClosable(true);

    North north = new North();
    north.setStyle("border: none");
    borderlayout.appendChild(north);
    north.appendChild(label);

    Center center = new Center();
    center.setSclass("dialog-content");
    center.setAutoscroll(true);
    borderlayout.appendChild(center);
    center.appendChild(tabbox);
    tabbox.setVflex("1");
    tabbox.setHflex("1");

    South south = new South();
    south.setSclass("dialog-footer");
    borderlayout.appendChild(south);
    south.appendChild(confirmPanel);
  } //	jbInit

  /** Dynamic Init for Price Tab */
  private boolean dynInit() {
    //	Header
    Vector<String> columnNames = new Vector<String>();
    columnNames.add(
        Msg.translate(Env.getCtx(), m_C_BPartner_ID == 0 ? "C_BPartner_ID" : "M_Product_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), "PriceActual"));
    columnNames.add(Msg.translate(Env.getCtx(), "C_Currency_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), "QtyInvoiced"));
    columnNames.add(Msg.translate(Env.getCtx(), "Discount"));
    columnNames.add(Msg.translate(Env.getCtx(), "DocumentNo"));
    columnNames.add(Msg.translate(Env.getCtx(), "DateInvoiced"));
    columnNames.add(Msg.translate(Env.getCtx(), "AD_Org_ID"));

    //	Fill Data
    Vector<Vector<Object>> data = null;
    if (m_C_BPartner_ID == 0) data = queryBPartner(); // 	BPartner of Product
    else data = queryProduct(); // 	Product of BPartner

    //  Table
    m_modelPrice = new ListModelTable(data);
    m_tablePrice.setData(m_modelPrice, columnNames);
    //
    m_tablePrice.setColumnClass(0, String.class, true); //  Product/Partner
    m_tablePrice.setColumnClass(1, Double.class, true); //  Price
    m_tablePrice.setColumnClass(2, String.class, true); //  Currency
    m_tablePrice.setColumnClass(3, Double.class, true); //  Quantity
    m_tablePrice.setColumnClass(4, BigDecimal.class, true); //  Discount (%) to limit precision
    m_tablePrice.setColumnClass(5, String.class, true); //  DocNo
    m_tablePrice.setColumnClass(6, Timestamp.class, true); //  Date
    m_tablePrice.setColumnClass(7, String.class, true); //  Org
    //
    m_tablePrice.autoSize();
    //

    return data.size() != 0;
  } //	dynInit

  /** Get Info for Product for given Business Parner */
  private Vector<Vector<Object>> queryProduct() {
    String sql =
        "SELECT p.Name,l.PriceActual,c.Iso_Code,l.PriceList,l.QtyInvoiced," //  1,2,3,4,5
            + "i.DateInvoiced,dt.PrintName || ' ' || i.DocumentNo As DocumentNo," //  6,7
            + "o.Name, " //  8
            + "NULL, i.M_PriceList_ID " //  9,10
            + "FROM C_Invoice i"
            + " INNER JOIN C_InvoiceLine l ON (i.C_Invoice_ID=l.C_Invoice_ID)"
            + " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)"
            + " INNER JOIN AD_Org o ON (i.AD_Org_ID=o.AD_Org_ID)"
            + " INNER JOIN M_Product p  ON (l.M_Product_ID=p.M_Product_ID) "
            + " INNER JOIN C_Currency c ON (i.C_Currency_ID=c.C_Currency_ID) "
            + "WHERE i.C_BPartner_ID=? "
            + "ORDER BY i.DateInvoiced DESC";

    Vector<Vector<Object>> data = fillTable(sql, m_C_BPartner_ID);

    sql = "SELECT Name from C_BPartner WHERE C_BPartner_ID=?";
    fillLabel(sql, m_C_BPartner_ID);
    return data;
  } //  queryProduct

  /** Get Info for Business Partners for given Product */
  private Vector<Vector<Object>> queryBPartner() {
    String sql =
        "SELECT bp.Name,l.PriceActual,c.Iso_Code,l.PriceList,l.QtyInvoiced," //	1,2,3,4,5
            + "i.DateInvoiced,dt.PrintName || ' ' || i.DocumentNo As DocumentNo," //	6,7
            + "o.Name," //  8
            + "NULL, i.M_PriceList_ID" //  9,10
            + " FROM C_Invoice i"
            + " INNER JOIN C_InvoiceLine l ON (i.C_Invoice_ID=l.C_Invoice_ID)"
            + " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)"
            + " INNER JOIN AD_Org o ON (i.AD_Org_ID=o.AD_Org_ID)"
            + " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID) "
            + " INNER JOIN C_Currency c ON (i.C_Currency_ID=c.C_Currency_ID) "
            + "WHERE l.M_Product_ID=? "
            + "ORDER BY i.DateInvoiced DESC";

    Vector<Vector<Object>> data = fillTable(sql, m_M_Product_ID);

    sql = "SELECT Name from M_Product WHERE M_Product_ID=?";
    fillLabel(sql, m_M_Product_ID);
    return data;
  } //	qyeryBPartner

  /** Fill Table */
  private Vector<Vector<Object>> fillTable(String sql, int parameter) {
    if (log.isLoggable(Level.FINE)) log.fine(sql + "; Parameter=" + parameter);
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, null);
      pstmt.setInt(1, parameter);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        Vector<Object> line = new Vector<Object>(6);
        //	0-Name, 1-PriceActual, 2-QtyInvoiced, 3-Discount, 4-DocumentNo, 5-DateInvoiced
        line.add(rs.getString(1)); //  Name
        line.add(rs.getBigDecimal(2)); // 	Price
        line.add(rs.getString(3)); // 	Currency
        line.add(new Double(rs.getDouble(5))); //  Qty
        BigDecimal discountBD = rs.getBigDecimal(9);
        if (discountBD == null) {
          double priceList = rs.getDouble(4);
          double priceActual = rs.getDouble(2);
          if (priceList != 0) {
            discountBD = BigDecimal.valueOf((priceList - priceActual) / priceList * 100);
            // Rounding:
            int precision = MPriceList.getStandardPrecision(Env.getCtx(), rs.getInt(10));
            if (discountBD.scale() > precision)
              discountBD = discountBD.setScale(precision, RoundingMode.HALF_UP);
          } else discountBD = Env.ZERO;
        }
        line.add(discountBD); //  Discount
        line.add(rs.getString(7)); //  DocNo
        line.add(rs.getTimestamp(6)); //  Date
        line.add(rs.getString(8)); // 	Org/Warehouse
        data.add(line);
      }
    } catch (SQLException e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.close(rs, pstmt);
      rs = null;
      pstmt = null;
    }
    if (log.isLoggable(Level.FINE)) log.fine("#" + data.size());
    return data;
  } //	fillTable

  /** Set Label to product or bp name */
  private void fillLabel(String sql, int parameter) {
    if (log.isLoggable(Level.FINE)) log.fine(sql + "; Parameter=" + parameter);
    String retValue = DB.getSQLValueString(null, sql, parameter);
    if (retValue != null) label.setText(retValue);
  } //	fillLabel

  public void onEvent(Event e) throws Exception {
    Component component = e.getTarget();

    if (component.equals(confirmPanel.getButton(ConfirmPanel.A_OK))) dispose();
    else if (component instanceof Tab) {
      if (tabbox.getSelectedIndex() == 1) initReservedOrderedTab(true);
      else if (tabbox.getSelectedIndex() == 2) initReservedOrderedTab(false);
      else if (tabbox.getSelectedIndex() == 3) initUnconfirmedTab();
      else if (tabbox.getSelectedIndex() == 4) initAtpTab();
    }
  }

  /**
   * Query Reserved/Ordered
   *
   * @param reserved po/so
   */
  private void initReservedOrderedTab(boolean reserved) {
    //	Done already
    if (reserved && m_modelReserved != null) return;
    if (!reserved && m_modelOrdered != null) return;

    //	Header
    Vector<String> columnNames = new Vector<String>();
    columnNames.add(
        Msg.translate(Env.getCtx(), m_C_BPartner_ID == 0 ? "C_BPartner_ID" : "M_Product_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), "PriceActual"));
    columnNames.add(Msg.translate(Env.getCtx(), "C_Currency_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), reserved ? "QtyReserved" : "QtyOrdered"));
    columnNames.add(Msg.translate(Env.getCtx(), "Discount"));
    columnNames.add(Msg.translate(Env.getCtx(), "DocumentNo"));
    columnNames.add(Msg.translate(Env.getCtx(), "DateOrdered"));
    columnNames.add(Msg.translate(Env.getCtx(), "M_Warehouse_ID"));

    //	Fill Data
    Vector<Vector<Object>> data = null;
    if (m_C_BPartner_ID == 0) {
      String sql =
          "SELECT bp.Name, ol.PriceActual,c.Iso_Code,ol.PriceList,ol.QtyReserved,"
              + "o.DateOrdered,dt.PrintName || ' ' || o.DocumentNo As DocumentNo, "
              + "w.Name,"
              + "ol.Discount, 0 " // 8,9=M_PriceList_ID
              + "FROM C_Order o"
              + " INNER JOIN C_OrderLine ol ON (o.C_Order_ID=ol.C_Order_ID)"
              + " INNER JOIN C_DocType dt ON (o.C_DocType_ID=dt.C_DocType_ID)"
              + " INNER JOIN M_Warehouse w ON (ol.M_Warehouse_ID=w.M_Warehouse_ID)"
              + " INNER JOIN C_BPartner bp  ON (o.C_BPartner_ID=bp.C_BPartner_ID) "
              + " INNER JOIN C_Currency c ON (o.C_Currency_ID=c.C_Currency_ID) "
              + "WHERE ol.QtyReserved<>0"
              + " AND ol.M_Product_ID=?"
              + " AND o.IsSOTrx="
              + (reserved ? "'Y'" : "'N'")
              + " ORDER BY o.DateOrdered";
      data = fillTable(sql, m_M_Product_ID); // 	Product By BPartner
    } else {
      String sql =
          "SELECT p.Name, ol.PriceActual,c.Iso_Code,ol.PriceList,ol.QtyReserved,"
              + "o.DateOrdered,dt.PrintName || ' ' || o.DocumentNo As DocumentNo, "
              + "w.Name,"
              + "ol.Discount, 0 " // 8,9=M_PriceList_ID
              + "FROM C_Order o"
              + " INNER JOIN C_OrderLine ol ON (o.C_Order_ID=ol.C_Order_ID)"
              + " INNER JOIN C_DocType dt ON (o.C_DocType_ID=dt.C_DocType_ID)"
              + " INNER JOIN M_Warehouse w ON (ol.M_Warehouse_ID=w.M_Warehouse_ID)"
              + " INNER JOIN M_Product p  ON (ol.M_Product_ID=p.M_Product_ID) "
              + " INNER JOIN C_Currency c ON (o.C_Currency_ID=c.C_Currency_ID) "
              + "WHERE ol.QtyReserved<>0"
              + " AND o.C_BPartner_ID=?"
              + " AND o.IsSOTrx="
              + (reserved ? "'Y'" : "'N'")
              + " ORDER BY o.DateOrdered";
      data = fillTable(sql, m_C_BPartner_ID); // 	Product of BP
    }

    //  Table
    if (reserved) {
      m_modelReserved = new ListModelTable(data);
      m_tableReserved.setData(m_modelReserved, columnNames);
      //
      m_tableReserved.setColumnClass(0, String.class, true); //  Product/Partner
      m_tableReserved.setColumnClass(1, BigDecimal.class, true); //  Price
      m_tableReserved.setColumnClass(2, String.class, true); //  Currency
      m_tableReserved.setColumnClass(3, Double.class, true); //  Quantity
      m_tableReserved.setColumnClass(4, BigDecimal.class, true); //  Discount (%)
      m_tableReserved.setColumnClass(5, String.class, true); //  DocNo
      m_tableReserved.setColumnClass(6, Timestamp.class, true); //  Date
      m_tableReserved.setColumnClass(7, String.class, true); //  Warehouse
      //
      m_tableReserved.autoSize();
    } else {
      m_modelOrdered = new ListModelTable(data);
      m_tableOrdered.setData(m_modelOrdered, columnNames);
      //
      m_tableOrdered.setColumnClass(0, String.class, true); //  Product/Partner
      m_tableOrdered.setColumnClass(1, BigDecimal.class, true); //  Price
      m_tableOrdered.setColumnClass(2, String.class, true); //  Currency
      m_tableOrdered.setColumnClass(3, Double.class, true); //  Quantity
      m_tableOrdered.setColumnClass(4, BigDecimal.class, true); //  Discount (%)
      m_tableOrdered.setColumnClass(5, String.class, true); //  DocNo
      m_tableOrdered.setColumnClass(6, Timestamp.class, true); //  Date
      m_tableOrdered.setColumnClass(7, String.class, true); //  Warehouse
      //
      m_tableOrdered.autoSize();
    }
  } //	initReservedOrderedTab

  /** Query Unconfirmed */
  private void initUnconfirmedTab() {
    //	Done already
    if (m_modelUnconfirmed != null) return;

    //	Header
    Vector<String> columnNames = new Vector<String>();
    columnNames.add(
        Msg.translate(Env.getCtx(), m_C_BPartner_ID == 0 ? "C_BPartner_ID" : "M_Product_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), "MovementQty"));
    columnNames.add(Msg.translate(Env.getCtx(), "MovementDate"));
    columnNames.add(Msg.translate(Env.getCtx(), "IsSOTrx"));
    columnNames.add(Msg.translate(Env.getCtx(), "DocumentNo"));
    columnNames.add(Msg.translate(Env.getCtx(), "M_Warehouse_ID"));

    //	Fill Data
    String sql = null;
    int parameter = 0;
    if (m_C_BPartner_ID == 0) {
      sql =
          "SELECT bp.Name,"
              + " CASE WHEN io.IsSOTrx='Y' THEN iol.MovementQty*-1 ELSE iol.MovementQty END AS MovementQty,"
              + " io.MovementDate,io.IsSOTrx,"
              + " dt.PrintName || ' ' || io.DocumentNo As DocumentNo,"
              + " w.Name "
              + "FROM M_InOutLine iol"
              + " INNER JOIN M_InOut io ON (iol.M_InOut_ID=io.M_InOut_ID)"
              + " INNER JOIN C_BPartner bp  ON (io.C_BPartner_ID=bp.C_BPartner_ID)"
              + " INNER JOIN C_DocType dt ON (io.C_DocType_ID=dt.C_DocType_ID)"
              + " INNER JOIN M_Warehouse w ON (io.M_Warehouse_ID=w.M_Warehouse_ID)"
              + " INNER JOIN M_InOutLineConfirm lc ON (iol.M_InOutLine_ID=lc.M_InOutLine_ID) "
              + "WHERE iol.M_Product_ID=?"
              + " AND lc.Processed='N' "
              + "ORDER BY io.MovementDate,io.IsSOTrx";
      parameter = m_M_Product_ID;
    } else {
      sql =
          "SELECT p.Name,"
              + " CASE WHEN io.IsSOTrx='Y' THEN iol.MovementQty*-1 ELSE iol.MovementQty END AS MovementQty,"
              + " io.MovementDate,io.IsSOTrx,"
              + " dt.PrintName || ' ' || io.DocumentNo As DocumentNo,"
              + " w.Name "
              + "FROM M_InOutLine iol"
              + " INNER JOIN M_InOut io ON (iol.M_InOut_ID=io.M_InOut_ID)"
              + " INNER JOIN M_Product p  ON (iol.M_Product_ID=p.M_Product_ID)"
              + " INNER JOIN C_DocType dt ON (io.C_DocType_ID=dt.C_DocType_ID)"
              + " INNER JOIN M_Warehouse w ON (io.M_Warehouse_ID=w.M_Warehouse_ID)"
              + " INNER JOIN M_InOutLineConfirm lc ON (iol.M_InOutLine_ID=lc.M_InOutLine_ID) "
              + "WHERE io.C_BPartner_ID=?"
              + " AND lc.Processed='N' "
              + "ORDER BY io.MovementDate,io.IsSOTrx";
      parameter = m_C_BPartner_ID;
    }
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, null);
      pstmt.setInt(1, parameter);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        Vector<Object> line = new Vector<Object>(6);
        //	1-Name, 2-MovementQty, 3-MovementDate, 4-IsSOTrx, 5-DocumentNo
        line.add(rs.getString(1)); //  Name
        line.add(new Double(rs.getDouble(2))); //  Qty
        line.add(rs.getTimestamp(3)); //  Date
        line.add(new Boolean("Y".equals(rs.getString(4)))); //  IsSOTrx
        line.add(rs.getString(5)); //  DocNo
        line.add(rs.getString(6)); //  Warehouse
        data.add(line);
      }
    } catch (SQLException e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.close(rs, pstmt);
      rs = null;
      pstmt = null;
    }
    if (log.isLoggable(Level.FINE)) log.fine("#" + data.size());

    //  Table
    m_modelUnconfirmed = new ListModelTable(data);
    m_tableUnconfirmed.setData(m_modelUnconfirmed, columnNames);
    //
    m_tableUnconfirmed.setColumnClass(0, String.class, true); //  Product/Partner
    m_tableUnconfirmed.setColumnClass(1, Double.class, true); //  MovementQty
    m_tableUnconfirmed.setColumnClass(2, Timestamp.class, true); //  MovementDate
    m_tableUnconfirmed.setColumnClass(3, Boolean.class, true); //  IsSOTrx
    m_tableUnconfirmed.setColumnClass(4, String.class, true); //  DocNo
    //
    m_tableUnconfirmed.autoSize();
  } //	initUnconfirmedTab

  /** Query ATP */
  private void initAtpTab() {
    //	Done already
    if (m_modelAtp != null) return;

    //	Header
    Vector<String> columnNames = new Vector<String>();
    columnNames.add(Msg.translate(Env.getCtx(), "Date"));
    columnNames.add(Msg.translate(Env.getCtx(), "QtyOnHand"));
    columnNames.add(Msg.translate(Env.getCtx(), "C_BPartner_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), "QtyOrdered"));
    columnNames.add(Msg.translate(Env.getCtx(), "QtyReserved"));
    columnNames.add(Msg.translate(Env.getCtx(), "M_Locator_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), "M_AttributeSetInstance_ID"));
    columnNames.add(Msg.translate(Env.getCtx(), "DocumentNo"));
    columnNames.add(Msg.translate(Env.getCtx(), "M_Warehouse_ID"));

    //	Fill Storage Data
    String sql =
        "SELECT s.QtyOnHand, s.QtyReserved, s.QtyOrdered,"
            + " productAttribute(s.M_AttributeSetInstance_ID), s.M_AttributeSetInstance_ID,";
    if (!showDetailATP)
      sql =
          "SELECT SUM(s.QtyOnHand), SUM(s.QtyReserved), SUM(s.QtyOrdered),"
              + " productAttribute(s.M_AttributeSetInstance_ID), 0,";
    sql +=
        " w.Name, l.Value "
            + "FROM M_Storage s"
            + " INNER JOIN M_Locator l ON (s.M_Locator_ID=l.M_Locator_ID)"
            + " LEFT JOIN M_LocatorType lt ON (l.M_LocatorType_ID=lt.M_LocatorType_ID)"
            + " INNER JOIN M_Warehouse w ON (l.M_Warehouse_ID=w.M_Warehouse_ID) "
            + "WHERE M_Product_ID=?";
    if (m_M_Warehouse_ID != 0) sql += " AND l.M_Warehouse_ID=?";
    if (m_M_AttributeSetInstance_ID > 0) sql += " AND s.M_AttributeSetInstance_ID=?";
    sql += " AND (s.QtyOnHand<>0 OR s.QtyReserved<>0 OR s.QtyOrdered<>0)";
    sql += " AND COALESCE(lt.IsAvailableForReservation,'Y')='Y'";
    if (!showDetailATP)
      sql += " GROUP BY productAttribute(s.M_AttributeSetInstance_ID), w.Name, l.Value";
    sql += " ORDER BY l.Value";

    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    double qty = 0;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, null);
      pstmt.setInt(1, m_M_Product_ID);
      if (m_M_Warehouse_ID != 0) pstmt.setInt(2, m_M_Warehouse_ID);
      if (m_M_AttributeSetInstance_ID > 0) pstmt.setInt(3, m_M_AttributeSetInstance_ID);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        Vector<Object> line = new Vector<Object>(9);
        line.add(null); //  Date
        double qtyOnHand = rs.getDouble(1);
        qty += qtyOnHand;
        line.add(new Double(qtyOnHand)); //  Qty
        line.add(null); //  BPartner
        line.add(new Double(rs.getDouble(3))); //  QtyOrdered
        line.add(new Double(rs.getDouble(2))); //  QtyReserved
        line.add(rs.getString(7)); //  Locator
        String asi = rs.getString(4);
        if (showDetailATP && (asi == null || asi.length() == 0)) asi = "{" + rs.getInt(5) + "}";
        line.add(asi); //  ASI
        line.add(null); //  DocumentNo
        line.add(rs.getString(6)); // 	Warehouse
        data.add(line);
      }
    } catch (SQLException e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.close(rs, pstmt);
      rs = null;
      pstmt = null;
    }

    //	Orders
    sql =
        "SELECT o.DatePromised, ol.QtyReserved,"
            + " productAttribute(ol.M_AttributeSetInstance_ID), ol.M_AttributeSetInstance_ID,"
            + " dt.DocBaseType, bp.Name,"
            + " dt.PrintName || ' ' || o.DocumentNo As DocumentNo, w.Name "
            + "FROM C_Order o"
            + " INNER JOIN C_OrderLine ol ON (o.C_Order_ID=ol.C_Order_ID)"
            + " INNER JOIN C_DocType dt ON (o.C_DocType_ID=dt.C_DocType_ID)"
            + " INNER JOIN M_Warehouse w ON (ol.M_Warehouse_ID=w.M_Warehouse_ID)"
            + " INNER JOIN C_BPartner bp  ON (o.C_BPartner_ID=bp.C_BPartner_ID) "
            + "WHERE ol.QtyReserved<>0"
            + " AND ol.M_Product_ID=?";
    if (m_M_Warehouse_ID != 0) sql += " AND ol.M_Warehouse_ID=?";
    if (m_M_AttributeSetInstance_ID > 0) sql += " AND ol.M_AttributeSetInstance_ID=?";
    sql += " ORDER BY o.DatePromised";
    try {
      pstmt = DB.prepareStatement(sql, null);
      pstmt.setInt(1, m_M_Product_ID);
      if (m_M_Warehouse_ID != 0) pstmt.setInt(2, m_M_Warehouse_ID);
      if (m_M_AttributeSetInstance_ID > 0) pstmt.setInt(3, m_M_AttributeSetInstance_ID);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        Vector<Object> line = new Vector<Object>(9);
        line.add(rs.getTimestamp(1)); //  Date
        double oq = rs.getDouble(2);
        String DocBaseType = rs.getString(5);
        Double qtyReserved = null;
        Double qtyOrdered = null;
        if (MDocType.DOCBASETYPE_PurchaseOrder.equals(DocBaseType)) {
          qtyOrdered = new Double(oq);
          qty += oq;
        } else {
          qtyReserved = new Double(oq);
          qty -= oq;
        }
        line.add(new Double(qty)); //  Qty
        line.add(rs.getString(6)); //  BPartner
        line.add(qtyOrdered); //  QtyOrdered
        line.add(qtyReserved); //  QtyReserved
        line.add(null); //  Locator
        String asi = rs.getString(3);
        if (showDetailATP && (asi == null || asi.length() == 0)) asi = "{" + rs.getInt(4) + "}";
        line.add(asi); //  ASI
        line.add(rs.getString(7)); //  DocumentNo
        line.add(rs.getString(8)); // 	Warehouse
        data.add(line);
      }
    } catch (SQLException e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.close(rs, pstmt);
      rs = null;
      pstmt = null;
    }

    //  Table
    m_modelAtp = new ListModelTable(data);
    m_tableAtp.setData(m_modelAtp, columnNames);
    //
    m_tableAtp.setColumnClass(0, Timestamp.class, true); //  Date
    m_tableAtp.setColumnClass(1, Double.class, true); //  Quantity
    m_tableAtp.setColumnClass(2, String.class, true); //  Partner
    m_tableAtp.setColumnClass(3, Double.class, true); //  Quantity
    m_tableAtp.setColumnClass(4, Double.class, true); //  Quantity
    m_tableAtp.setColumnClass(5, String.class, true); //  Locator
    m_tableAtp.setColumnClass(6, String.class, true); //  ASI
    m_tableAtp.setColumnClass(7, String.class, true); //  DocNo
    m_tableAtp.setColumnClass(8, String.class, true); //  Warehouse
    //
    m_tableAtp.autoSize();
  } //	initAtpTab
} //	InvoiceHistory