/******************************************************************************
* Copyright (C) 2008 Elaine Tan
* Copyright (C) 2008 Idalica *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.webui.panel;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Vector;
import java.util.logging.Level;
import org.adempiere.webui.apps.AEnv;
import org.adempiere.webui.component.ConfirmPanel;
import org.adempiere.webui.component.Label;
import org.adempiere.webui.component.ListModelTable;
import org.adempiere.webui.component.ListboxFactory;
import org.adempiere.webui.component.Tab;
import org.adempiere.webui.component.Tabbox;
import org.adempiere.webui.component.Tabpanel;
import org.adempiere.webui.component.Tabpanels;
import org.adempiere.webui.component.Tabs;
import org.adempiere.webui.component.WListbox;
import org.adempiere.webui.component.Window;
import org.adempiere.webui.info.InfoProductWindow;
import org.adempiere.webui.theme.ThemeManager;
import org.adempiere.webui.util.ZKUpdateUtil;
import org.compiere.model.MDocType;
import org.compiere.model.MPriceList;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center;
import org.zkoss.zul.North;
import org.zkoss.zul.South;
/**
* Price History for BPartner/Product
* @author Elaine
*/
public class InvoiceHistory extends Window implements EventListener
{
/**
* generated serial id
*/
private static final long serialVersionUID = 8742214467478030802L;
protected boolean showDetailATP = false;
/**
* Show History
* @param parent
* @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 invoice-history-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 final 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;
/**
* Layout window
*/
protected 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")));
ZKUpdateUtil.setHeight(pricePane, "100%");
pricePane.appendChild(m_tablePrice);
tabpanels.appendChild(pricePane);
ZKUpdateUtil.setHeight(reservedPane, "100%");
reservedPane.appendChild(m_tableReserved);
tabpanels.appendChild(reservedPane);
ZKUpdateUtil.setHeight(orderedPane, "100%");
orderedPane.appendChild(m_tableOrdered);
tabpanels.appendChild(orderedPane);
ZKUpdateUtil.setHeight(unconfirmedPane, "100%");
unconfirmedPane.appendChild(m_tableUnconfirmed);
tabpanels.appendChild(unconfirmedPane);
if (m_M_Product_ID != 0)
{
ZKUpdateUtil.setHeight(atpPane, "100%");
atpPane.appendChild(m_tableAtp);
tabpanels.appendChild(atpPane);
}
tabbox.setSelectedIndex(0);
tabbox.addEventListener(Events.ON_SELECT, this);
confirmPanel.addActionListener(this);
Borderlayout borderlayout = new Borderlayout();
if (!ThemeManager.isUseCSSForWindowSize())
{
ZKUpdateUtil.setWindowWidthX(this, 700);
ZKUpdateUtil.setWindowHeightX(this, 400);
}
else
{
addCallback(AFTER_PAGE_ATTACHED, t-> {
ZKUpdateUtil.setCSSHeight(this);
ZKUpdateUtil.setCSSWidth(this);
this.invalidate();
});
}
borderlayout.setStyle("border: none; position: relative");
this.appendChild(borderlayout);
this.setClosable(true);
this.setSizable(true);
this.setMaximizable(true);
this.setBorder("normal");
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);
ZKUpdateUtil.setVflex(tabbox, "1");
ZKUpdateUtil.setHflex(tabbox, "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 columnNames = new Vector();
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> 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
return data.size() != 0;
} // dynInit
/**
* Get invoiced product details for a given Business Partner.
* @return invoiced product details
*/
private Vector> 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> 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 invoiced Business Partners details for a given Product
* @return invoiced Business Partners details
*/
private Vector> 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> 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 list
* @param sql
* @param parameter
* @return List of records
*/
private Vector> fillTable (String sql, int parameter)
{
if (log.isLoggable(Level.FINE)) log.fine(sql + "; Parameter=" + parameter);
Vector> data = new Vector>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, parameter);
rs = pstmt.executeQuery();
while (rs.next())
{
Vector