1 14 package org.compiere.apps.search; 15 16 import java.awt.BorderLayout ; 17 import java.awt.Dialog ; 18 import java.awt.FlowLayout ; 19 import java.awt.event.ActionEvent ; 20 import java.awt.event.ActionListener ; 21 import java.math.BigDecimal ; 22 import java.sql.PreparedStatement ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Timestamp ; 26 import java.util.Vector ; 27 28 import javax.swing.JDialog ; 29 import javax.swing.JLabel ; 30 import javax.swing.JScrollPane ; 31 import javax.swing.JTabbedPane ; 32 import javax.swing.WindowConstants ; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 import javax.swing.table.DefaultTableModel ; 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 53 public class InvoiceHistory extends JDialog implements ActionListener , ChangeListener 54 { 55 58 public InvoiceHistory (Dialog 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 ex) 72 { 73 Log.error("InvoiceHistory", ex); 74 } 75 AEnv.positionCenterWindow(frame, this); 76 } 78 private int m_C_BPartner_ID; 79 private int m_M_Product_ID; 80 81 private CPanel mainPanel = new CPanel(); 82 private BorderLayout mainLayout = new BorderLayout (); 83 private CPanel northPanel = new CPanel(); 84 private JLabel label = new JLabel (); 85 private FlowLayout northLayout = new FlowLayout (); 86 private MiniTable m_tablePrice = new MiniTable(); 88 private DefaultTableModel m_modelPrice = null; 89 private MiniTable m_tableReserved = new MiniTable(); 90 private DefaultTableModel m_modelReserved = null; 91 private MiniTable m_tableOrdered = new MiniTable(); 92 private DefaultTableModel m_modelOrdered = null; 93 private ConfirmPanel confirmPanel = new ConfirmPanel(); 95 private JTabbedPane centerTabbedPane = new JTabbedPane (); 96 private JScrollPane pricePane = new JScrollPane (); 97 private JScrollPane reservedPane = new JScrollPane (); 98 private JScrollPane orderedPane = new JScrollPane (); 99 100 103 void jbInit() throws Exception 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 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 } 126 129 private boolean dynInit() 130 { 131 Vector columnNames = new Vector (); 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 Vector data = null; 142 if (m_C_BPartner_ID == 0) 143 data = queryBPartner(); else 145 data = queryProduct(); 147 m_modelPrice = new DefaultTableModel (data, columnNames); 149 m_tablePrice.setModel(m_modelPrice); 150 m_tablePrice.setColumnClass(0, String .class, true); m_tablePrice.setColumnClass(1, BigDecimal .class, true); m_tablePrice.setColumnClass(2, Double .class, true); m_tablePrice.setColumnClass(3, BigDecimal .class, true); m_tablePrice.setColumnClass(4, String .class, true); m_tablePrice.setColumnClass(5, Timestamp .class, true); m_tablePrice.autoSize(); 159 return data.size() != 0; 161 } 163 164 167 private Vector queryProduct () 168 { 169 String 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 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 } 185 188 private Vector queryBPartner () 189 { 190 String sql = "SELECT bp.Name,l.PriceActual,l.PriceList,l.QtyInvoiced," + "i.DateInvoiced,dt.PrintName || ' ' || i.DocumentNo As DocumentNo " + "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 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 } 206 209 private Vector fillTable (String sql, int parameter) 210 { 211 Log.trace(Log.l6_Database, "SQL=" + sql + "; Parameter=" + parameter); 212 Vector data = new Vector (); 213 try 214 { 215 PreparedStatement pstmt = DB.prepareStatement(sql); 216 pstmt.setInt(1, parameter); 217 ResultSet rs = pstmt.executeQuery(); 218 while (rs.next()) 219 { 220 Vector line = new Vector (6); 221 line.add(rs.getString(1)); line.add(rs.getBigDecimal(2)); line.add(new Double (rs.getDouble(4))); BigDecimal discountBD = Env.ZERO; 226 try { 228 double discountD = (rs.getDouble(3)-rs.getDouble(2)) / rs.getDouble(3) * 100; 229 discountBD = new BigDecimal (discountD); 230 } 231 catch (Exception e) 232 { 233 discountBD = Env.ZERO; 234 } 235 line.add(discountBD); line.add(rs.getString(6)); line.add(rs.getTimestamp(5)); data.add(line); 239 } 240 rs.close(); 241 pstmt.close(); 242 } 243 catch (SQLException e) 244 { 245 Log.error("InvoiceHistory.fillTable - SQL=" + sql, e); 246 } 247 Log.trace(Log.l5_DData, "fillTable #" + data.size()); 248 return data; 249 } 251 255 private void fillLabel (String sql, int parameter) 256 { 257 Log.trace(Log.l6_Database, "SQL=" + sql + "; Parameter=" + parameter); 258 try 259 { 260 PreparedStatement pstmt = DB.prepareStatement(sql); 261 pstmt.setInt(1, parameter); 262 ResultSet rs = pstmt.executeQuery(); 263 if (rs.next()) 264 label.setText(rs.getString(1)); 265 rs.close(); 266 pstmt.close(); 267 } 268 catch (SQLException e) 269 { 270 Log.error("InvoiceHistory.fillLabel - SQL=" + sql, e); 271 } 272 } 274 275 278 public void actionPerformed(ActionEvent e) 279 { 280 if (e.getActionCommand().equals(ConfirmPanel.A_OK)) 281 dispose(); 282 } 284 285 289 public void stateChanged(ChangeEvent e) 290 { 291 if (centerTabbedPane.getSelectedIndex() == 1) 292 initAdditionalTab(true); 293 else if (centerTabbedPane.getSelectedIndex() == 2) 294 initAdditionalTab(false); 295 } 297 300 private void initAdditionalTab (boolean reserved) 301 { 302 if (reserved && m_modelReserved != null) 304 return; 305 if (!reserved && m_modelOrdered != null) 306 return; 307 308 Vector columnNames = new Vector (); 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 Vector data = null; 319 if (m_C_BPartner_ID == 0) 320 { 321 String 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); } 333 else 334 { 335 String 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); } 347 348 MiniTable table = null; 350 if (reserved) 351 { 352 m_modelReserved = new DefaultTableModel (data, columnNames); 353 m_tableReserved.setModel(m_modelReserved); 354 table = m_tableReserved; 355 } 356 else 357 { 358 m_modelOrdered = new DefaultTableModel (data, columnNames); 359 m_tableOrdered.setModel(m_modelOrdered); 360 table = m_tableOrdered; 361 } 362 table.setColumnClass(0, String .class, true); table.setColumnClass(1, BigDecimal .class, true); table.setColumnClass(2, Double .class, true); table.setColumnClass(3, BigDecimal .class, true); table.setColumnClass(4, String .class, true); table.setColumnClass(5, Timestamp .class, true); table.autoSize(); 371 } 373 } | Popular Tags |