KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > search > InvoiceHistory


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps.search;
15
16 import java.awt.BorderLayout JavaDoc;
17 import java.awt.Dialog JavaDoc;
18 import java.awt.FlowLayout JavaDoc;
19 import java.awt.event.ActionEvent JavaDoc;
20 import java.awt.event.ActionListener JavaDoc;
21 import java.math.BigDecimal JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Timestamp JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import javax.swing.JDialog JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import javax.swing.JTabbedPane JavaDoc;
32 import javax.swing.WindowConstants JavaDoc;
33 import javax.swing.event.ChangeEvent JavaDoc;
34 import javax.swing.event.ChangeListener JavaDoc;
35 import javax.swing.table.DefaultTableModel JavaDoc;
36
37 import org.compiere.apps.AEnv;
38 import org.compiere.apps.ConfirmPanel;
39 import org.compiere.minigrid.MiniTable;
40 import org.compiere.plaf.CompiereColor;
41 import org.compiere.swing.CPanel;
42 import org.compiere.util.DB;
43 import org.compiere.util.Env;
44 import org.compiere.util.Log;
45 import org.compiere.util.Msg;
46
47 /**
48  * Price History for BPartner/Product
49  *
50  * @author Jorg Janke
51  * @version $Id: InvoiceHistory.java,v 1.6 2003/10/04 03:52:36 jjanke Exp $
52  */

53 public class InvoiceHistory extends JDialog JavaDoc implements ActionListener JavaDoc, ChangeListener JavaDoc
54 {
55     /**
56      * Show Price History
57      */

58     public InvoiceHistory (Dialog JavaDoc frame, int C_BPartner_ID, int M_Product_ID)
59     {
60         super(frame, Msg.getMsg(Env.getCtx(), "PriceHistory"), true);
61         CompiereColor.setBackground(this);
62         Log.trace(Log.l3_Util, "InvoiceHistory - C_BPartner_ID=" + C_BPartner_ID
63             + ", M_Product_ID=" + M_Product_ID);
64         m_C_BPartner_ID = C_BPartner_ID;
65         m_M_Product_ID = M_Product_ID;
66         try
67         {
68             jbInit();
69             dynInit();
70         }
71         catch(Exception JavaDoc ex)
72         {
73             Log.error("InvoiceHistory", ex);
74         }
75         AEnv.positionCenterWindow(frame, this);
76     } // InvoiceHistory
77

78     private int m_C_BPartner_ID;
79     private int m_M_Product_ID;
80
81     private CPanel mainPanel = new CPanel();
82     private BorderLayout JavaDoc mainLayout = new BorderLayout JavaDoc();
83     private CPanel northPanel = new CPanel();
84     private JLabel JavaDoc label = new JLabel JavaDoc();
85     private FlowLayout JavaDoc northLayout = new FlowLayout JavaDoc();
86     //
87
private MiniTable m_tablePrice = new MiniTable();
88     private DefaultTableModel JavaDoc m_modelPrice = null;
89     private MiniTable m_tableReserved = new MiniTable();
90     private DefaultTableModel JavaDoc m_modelReserved = null;
91     private MiniTable m_tableOrdered = new MiniTable();
92     private DefaultTableModel JavaDoc m_modelOrdered = null;
93     //
94
private ConfirmPanel confirmPanel = new ConfirmPanel();
95     private JTabbedPane JavaDoc centerTabbedPane = new JTabbedPane JavaDoc();
96     private JScrollPane JavaDoc pricePane = new JScrollPane JavaDoc();
97     private JScrollPane JavaDoc reservedPane = new JScrollPane JavaDoc();
98     private JScrollPane JavaDoc orderedPane = new JScrollPane JavaDoc();
99
100     /**
101      * Ststic Init
102      */

103     void jbInit() throws Exception JavaDoc
104     {
105         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
106         mainPanel.setLayout(mainLayout);
107         label.setText("Label");
108         northPanel.setLayout(northLayout);
109         northLayout.setAlignment(FlowLayout.LEFT);
110         getContentPane().add(mainPanel);
111         mainPanel.add(northPanel, BorderLayout.NORTH);
112         northPanel.add(label, null);
113         mainPanel.add(confirmPanel, BorderLayout.SOUTH);
114         mainPanel.add(centerTabbedPane, BorderLayout.CENTER);
115         centerTabbedPane.addChangeListener(this);
116         centerTabbedPane.add(pricePane, Msg.getMsg(Env.getCtx(), "PriceHistory"));
117         centerTabbedPane.add(reservedPane, Msg.translate(Env.getCtx(), "QtyReserved"));
118         centerTabbedPane.add(orderedPane, Msg.translate(Env.getCtx(), "QtyOrdered"));
119         //
120
pricePane.getViewport().add(m_tablePrice, null);
121         reservedPane.getViewport().add(m_tableReserved, null);
122         orderedPane.getViewport().add(m_tableOrdered, null);
123         confirmPanel.addActionListener(this);
124     } // jbInit
125

126     /**
127      * Dynamic Init for Price Tab
128      */

129     private boolean dynInit()
130     {
131         // Header
132
Vector JavaDoc columnNames = new Vector JavaDoc();
133         columnNames.add(Msg.translate(Env.getCtx(), m_C_BPartner_ID == 0 ? "C_BPartner_ID" : "M_Product_ID"));
134         columnNames.add(Msg.translate(Env.getCtx(), "PriceActual"));
135         columnNames.add(Msg.translate(Env.getCtx(), "QtyInvoiced"));
136         columnNames.add(Msg.translate(Env.getCtx(), "Discount"));
137         columnNames.add(Msg.translate(Env.getCtx(), "DocumentNo"));
138         columnNames.add(Msg.translate(Env.getCtx(), "DateInvoiced"));
139
140         // Fill Data
141
Vector JavaDoc data = null;
142         if (m_C_BPartner_ID == 0)
143             data = queryBPartner(); // BPartner of Product
144
else
145             data = queryProduct(); // Product of BPartner
146

147         // Table
148
m_modelPrice = new DefaultTableModel JavaDoc(data, columnNames);
149         m_tablePrice.setModel(m_modelPrice);
150         //
151
m_tablePrice.setColumnClass(0, String JavaDoc.class, true); // Product/Partner
152
m_tablePrice.setColumnClass(1, BigDecimal JavaDoc.class, true); // Price
153
m_tablePrice.setColumnClass(2, Double JavaDoc.class, true); // Quantity
154
m_tablePrice.setColumnClass(3, BigDecimal JavaDoc.class, true); // Discount (%)
155
m_tablePrice.setColumnClass(4, String JavaDoc.class, true); // DocNo
156
m_tablePrice.setColumnClass(5, Timestamp JavaDoc.class, true); // Date
157
//
158
m_tablePrice.autoSize();
159         //
160
return data.size() != 0;
161     } // dynInit
162

163
164     /**
165      * Get Info for Product for given Business Parner
166      */

167     private Vector JavaDoc queryProduct ()
168     {
169         String JavaDoc sql = "SELECT p.Name,l.PriceActual,l.PriceList,l.QtyInvoiced,"
170             + "i.DateInvoiced,dt.PrintName || ' ' || i.DocumentNo As DocumentNo "
171             + "FROM C_Invoice i"
172             + " INNER JOIN C_InvoiceLine l ON (i.C_Invoice_ID=l.C_Invoice_ID)"
173             + " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)"
174             + " INNER JOIN M_Product p ON (l.M_Product_ID=p.M_Product_ID) "
175             + "WHERE i.C_BPartner_ID=? "
176             + "ORDER BY i.DateInvoiced DESC";
177
178         Vector JavaDoc data = fillTable (sql, m_C_BPartner_ID);
179
180         sql = "SELECT Name from C_BPartner WHERE C_BPartner_ID=?";
181         fillLabel (sql, m_C_BPartner_ID);
182         return data;
183     } // queryProduct
184

185     /**
186      * Get Info for Business Partners for given Product
187      */

188     private Vector JavaDoc queryBPartner ()
189     {
190         String JavaDoc sql = "SELECT bp.Name,l.PriceActual,l.PriceList,l.QtyInvoiced," // 1,2,3,4
191
+ "i.DateInvoiced,dt.PrintName || ' ' || i.DocumentNo As DocumentNo " // 5,6
192
+ "FROM C_Invoice i"
193             + " INNER JOIN C_InvoiceLine l ON (i.C_Invoice_ID=l.C_Invoice_ID)"
194             + " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)"
195             + " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID) "
196             + "WHERE l.M_Product_ID=? "
197             + "ORDER BY i.DateInvoiced DESC";
198
199         Vector JavaDoc data = fillTable (sql, m_M_Product_ID);
200
201         sql = "SELECT Name from M_Product WHERE M_Product_ID=?";
202         fillLabel (sql, m_M_Product_ID);
203         return data;
204     } // qyeryBPartner
205

206     /**
207      * Fill Table
208      */

209     private Vector JavaDoc fillTable (String JavaDoc sql, int parameter)
210     {
211         Log.trace(Log.l6_Database, "SQL=" + sql + "; Parameter=" + parameter);
212         Vector JavaDoc data = new Vector JavaDoc();
213         try
214         {
215             PreparedStatement JavaDoc pstmt = DB.prepareStatement(sql);
216             pstmt.setInt(1, parameter);
217             ResultSet JavaDoc rs = pstmt.executeQuery();
218             while (rs.next())
219             {
220                 Vector JavaDoc line = new Vector JavaDoc(6);
221                 // 0-Name, 1-PriceActual, 2-QtyInvoiced, 3-Discount, 4-DocumentNo, 5-DateInvoiced
222
line.add(rs.getString(1)); // Name
223
line.add(rs.getBigDecimal(2)); // Price
224
line.add(new Double JavaDoc(rs.getDouble(4))); // Qty
225
BigDecimal JavaDoc discountBD = Env.ZERO;
226                 try // discoint can be indefinate
227
{
228                     double discountD = (rs.getDouble(3)-rs.getDouble(2)) / rs.getDouble(3) * 100;
229                     discountBD = new BigDecimal JavaDoc(discountD);
230                 }
231                 catch (Exception JavaDoc e)
232                 {
233                     discountBD = Env.ZERO;
234                 }
235                 line.add(discountBD); // Discount
236
line.add(rs.getString(6)); // DocNo
237
line.add(rs.getTimestamp(5)); // Date
238
data.add(line);
239             }
240             rs.close();
241             pstmt.close();
242         }
243         catch (SQLException JavaDoc e)
244         {
245             Log.error("InvoiceHistory.fillTable - SQL=" + sql, e);
246         }
247         Log.trace(Log.l5_DData, "fillTable #" + data.size());
248         return data;
249     } // fillTable
250

251     /**
252      * Set Label
253      * to product or bp name
254      */

255     private void fillLabel (String JavaDoc sql, int parameter)
256     {
257         Log.trace(Log.l6_Database, "SQL=" + sql + "; Parameter=" + parameter);
258         try
259         {
260             PreparedStatement JavaDoc pstmt = DB.prepareStatement(sql);
261             pstmt.setInt(1, parameter);
262             ResultSet JavaDoc rs = pstmt.executeQuery();
263             if (rs.next())
264                 label.setText(rs.getString(1));
265             rs.close();
266             pstmt.close();
267         }
268         catch (SQLException JavaDoc e)
269         {
270             Log.error("InvoiceHistory.fillLabel - SQL=" + sql, e);
271         }
272     } // fillLabel
273

274
275     /**
276      * Action Listener
277      */

278     public void actionPerformed(ActionEvent JavaDoc e)
279     {
280         if (e.getActionCommand().equals(ConfirmPanel.A_OK))
281             dispose();
282     } // actionPerformed
283

284
285     /**
286      * Tab Changed
287      * @param e event
288      */

289     public void stateChanged(ChangeEvent JavaDoc e)
290     {
291         if (centerTabbedPane.getSelectedIndex() == 1)
292             initAdditionalTab(true);
293         else if (centerTabbedPane.getSelectedIndex() == 2)
294             initAdditionalTab(false);
295     } // stateChanged
296

297     /**
298      * Query Reserved/Ordered
299      */

300     private void initAdditionalTab (boolean reserved)
301     {
302         // Done already
303
if (reserved && m_modelReserved != null)
304             return;
305         if (!reserved && m_modelOrdered != null)
306             return;
307             
308         // Header
309
Vector JavaDoc columnNames = new Vector JavaDoc();
310         columnNames.add(Msg.translate(Env.getCtx(), m_C_BPartner_ID == 0 ? "C_BPartner_ID" : "M_Product_ID"));
311         columnNames.add(Msg.translate(Env.getCtx(), "PriceActual"));
312         columnNames.add(Msg.translate(Env.getCtx(), reserved ? "QtyReserved" : "QtyOrdered"));
313         columnNames.add(Msg.translate(Env.getCtx(), "Discount"));
314         columnNames.add(Msg.translate(Env.getCtx(), "DocumentNo"));
315         columnNames.add(Msg.translate(Env.getCtx(), "DateOrdered"));
316
317         // Fill Data
318
Vector JavaDoc data = null;
319         if (m_C_BPartner_ID == 0)
320         {
321             String JavaDoc sql = "SELECT bp.Name, ol.PriceActual,ol.PriceList,ol.QtyReserved,"
322                 + "o.DateOrdered,dt.PrintName || ' ' || o.DocumentNo As DocumentNo "
323                 + "FROM C_Order o"
324                 + " INNER JOIN C_OrderLine ol ON (o.C_Order_ID=ol.C_Order_ID)"
325                 + " INNER JOIN C_DocType dt ON (o.C_DocType_ID=dt.C_DocType_ID)"
326                 + " INNER JOIN C_BPartner bp ON (o.C_BPartner_ID=bp.C_BPartner_ID) "
327                 + "WHERE ol.QtyReserved<>0"
328                 + " AND ol.M_Product_ID=?"
329                 + " AND o.IsSOTrx=" + (reserved ? "'Y'" : "'N'")
330                 + " ORDER BY o.DateOrdered";
331             data = fillTable (sql, m_M_Product_ID); // Product By BPartner
332
}
333         else
334         {
335             String JavaDoc sql = "SELECT p.Name, ol.PriceActual,ol.PriceList,ol.QtyReserved,"
336                 + "o.DateOrdered,dt.PrintName || ' ' || o.DocumentNo As DocumentNo "
337                 + "FROM C_Order o"
338                 + " INNER JOIN C_OrderLine ol ON (o.C_Order_ID=ol.C_Order_ID)"
339                 + " INNER JOIN C_DocType dt ON (o.C_DocType_ID=dt.C_DocType_ID)"
340                 + " INNER JOIN M_Product p ON (ol.M_Product_ID=p.M_Product_ID) "
341                 + "WHERE ol.QtyReserved<>0"
342                 + " AND o.C_BPartner_ID=?"
343                 + " AND o.IsSOTrx=" + (reserved ? "'Y'" : "'N'")
344                 + " ORDER BY o.DateOrdered";
345             data = fillTable (sql, m_C_BPartner_ID);// Product of BP
346
}
347
348         // Table
349
MiniTable table = null;
350         if (reserved)
351         {
352             m_modelReserved = new DefaultTableModel JavaDoc(data, columnNames);
353             m_tableReserved.setModel(m_modelReserved);
354             table = m_tableReserved;
355         }
356         else
357         {
358             m_modelOrdered = new DefaultTableModel JavaDoc(data, columnNames);
359             m_tableOrdered.setModel(m_modelOrdered);
360             table = m_tableOrdered;
361         }
362         //
363
table.setColumnClass(0, String JavaDoc.class, true); // Product/Partner
364
table.setColumnClass(1, BigDecimal JavaDoc.class, true); // Price
365
table.setColumnClass(2, Double JavaDoc.class, true); // Quantity
366
table.setColumnClass(3, BigDecimal JavaDoc.class, true); // Discount (%)
367
table.setColumnClass(4, String JavaDoc.class, true); // DocNo
368
table.setColumnClass(5, Timestamp JavaDoc.class, true); // Date
369
//
370
table.autoSize();
371     } // initAdditionalTab
372

373 } // InvoiceHistory
374
Popular Tags