KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > VCreateFrom


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.grid;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import javax.swing.table.*;
20 import javax.swing.event.*;
21
22 import java.sql.*;
23 import java.util.*;
24 import java.beans.*;
25 import java.math.*;
26 import java.text.*;
27
28 import org.compiere.util.*;
29 import org.compiere.model.*;
30 import org.compiere.apps.*;
31 import org.compiere.grid.ed.*;
32 import org.compiere.minigrid.*;
33 import org.compiere.plaf.*;
34 import org.compiere.swing.*;
35
36 /**
37  * CreateFrom (Called from GridController.startProcess)
38  *
39  * @author Jorg Janke
40  * @version $Id: VCreateFrom.java,v 1.27 2003/10/10 00:59:45 jjanke Exp $
41  */

42 public abstract class VCreateFrom extends JDialog
43     implements ActionListener, TableModelListener
44 {
45     /**
46      * Factory - called from APanel
47      * @param mTab Model Tab for the trx
48      * @return JDialog
49      */

50     public static VCreateFrom create (MTab mTab)
51     {
52         // dynamic init preparation
53
int AD_Table_ID = Env.getContextAsInt(Env.getCtx(), mTab.getWindowNo(), "BaseTable_ID");
54
55         VCreateFrom retValue = null;
56         if (AD_Table_ID == 392) // C_BankStatement
57
retValue = new VCreateFromStatement (mTab);
58         else if (AD_Table_ID == 318) // C_Invoice
59
retValue = new VCreateFromInvoice (mTab);
60         else if (AD_Table_ID == 319) // M_InOut
61
retValue = new VCreateFromShipment (mTab);
62         else if (AD_Table_ID == 426) // C_PaySelection
63
return null; // ignore - will call process C_PaySelection_CreateFrom
64
else // Not supported CreateFrom
65
{
66             Log.error("VCreateFrom.create - Unsupported Table=" + AD_Table_ID);
67             return null;
68         }
69         return retValue;
70     } // create
71

72     /*************************************************************************/
73
74     /**
75      * Protected super class Constructor
76      * @param mTab MTab
77      */

78     VCreateFrom (MTab mTab)
79     {
80         super(Env.getWindow(mTab.getWindowNo()), true);
81         Log.trace(Log.l1_User, "VCreateFrom");
82         p_WindowNo = mTab.getWindowNo();
83         p_mTab = mTab;
84
85         try
86         {
87             if (!dynInit())
88                 return;
89             jbInit();
90             confirmPanel.addActionListener(this);
91             // Set status
92
statusBar.setStatusDB("");
93             tableChanged(null);
94             p_initOK = true;
95         }
96         catch(Exception JavaDoc e)
97         {
98             Log.error("VCreateFrom", e);
99             p_initOK = false;
100         }
101         AEnv.positionCenterWindow(Env.getWindow(p_WindowNo), this);
102     } // VCreateFrom
103

104     /** Window No */
105     protected int p_WindowNo;
106     /** Model Tab */
107     protected MTab p_mTab;
108
109     private boolean p_initOK = false;
110
111     /** Loaded Order */
112     protected int p_C_Order_ID = 0;
113
114     //
115
private CPanel parameterPanel = new CPanel();
116     protected CPanel parameterBankPanel = new CPanel();
117     private BorderLayout parameterLayout = new BorderLayout();
118     private JLabel bankAccountLabel = new JLabel();
119     protected CPanel parameterStdPanel = new CPanel();
120     private JLabel bPartnerLabel = new JLabel();
121     protected VLookup bankAccountField;
122     private GridBagLayout parameterStdLayout = new GridBagLayout();
123     private GridBagLayout parameterBankLayout = new GridBagLayout();
124     protected VLookup bPartnerField;
125     private JLabel orderLabel = new JLabel();
126     protected JComboBox orderField = new JComboBox();
127     protected JLabel invoiceLabel = new JLabel();
128     protected JComboBox invoiceField = new JComboBox();
129     protected JLabel shipmentLabel = new JLabel();
130     protected JComboBox shipmentField = new JComboBox();
131     private JScrollPane dataPane = new JScrollPane();
132     private CPanel southPanel = new CPanel();
133     private BorderLayout southLayout = new BorderLayout();
134     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
135     protected StatusBar statusBar = new StatusBar();
136     protected MiniTable dataTable = new MiniTable();
137     protected JLabel locatorLabel = new JLabel();
138     protected VLocator locatorField = new VLocator();
139
140     /**
141      * Static Init.
142      * <pre>
143      * parameterPanel
144      * parameterBankPanel
145      * parameterStdPanel
146      * bPartner/order/invoice/shopment/licator Label/Field
147      * dataPane
148      * southPanel
149      * confirmPanel
150      * statusBar
151      * </pre>
152      * @throws Exception
153      */

154     private void jbInit() throws Exception JavaDoc
155     {
156         CompiereColor.setBackground(this);
157         parameterPanel.setLayout(parameterLayout);
158         parameterStdPanel.setLayout(parameterStdLayout);
159         parameterBankPanel.setLayout(parameterBankLayout);
160         //
161
bankAccountLabel.setText(Msg.translate(Env.getCtx(), "C_BankAccount_ID"));
162         bPartnerLabel.setText(Msg.getElement(Env.getCtx(), "C_BPartner_ID"));
163         orderLabel.setText(Msg.getElement(Env.getCtx(), "C_Order_ID", false));
164         invoiceLabel.setText(Msg.getElement(Env.getCtx(), "C_Invoice_ID", false));
165         shipmentLabel.setText(Msg.getElement(Env.getCtx(), "M_InOut_ID", false));
166         locatorLabel.setText(Msg.translate(Env.getCtx(), "M_Locator_ID"));
167         //
168
this.getContentPane().add(parameterPanel, BorderLayout.NORTH);
169         parameterPanel.add(parameterBankPanel, BorderLayout.NORTH);
170         parameterBankPanel.add(bankAccountLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
171             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
172         if (bankAccountField != null)
173             parameterBankPanel.add(bankAccountField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
174                 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
175         parameterPanel.add(parameterStdPanel, BorderLayout.CENTER);
176         parameterStdPanel.add(bPartnerLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
177             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
178         if (bPartnerField != null)
179             parameterStdPanel.add(bPartnerField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
180                 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
181         parameterStdPanel.add(orderLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
182             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
183         parameterStdPanel.add(orderField, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
184             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
185         parameterStdPanel.add(invoiceLabel, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
186             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
187         parameterStdPanel.add(invoiceField, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
188             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
189         parameterStdPanel.add(shipmentLabel, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
190             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
191         parameterStdPanel.add(shipmentField, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
192             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
193         parameterStdPanel.add(locatorLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
194             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
195         parameterStdPanel.add(locatorField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
196             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 5), 0, 0));
197         this.getContentPane().add(dataPane, BorderLayout.CENTER);
198         dataPane.getViewport().add(dataTable, null);
199         //
200
this.getContentPane().add(southPanel, BorderLayout.SOUTH);
201         southPanel.setLayout(southLayout);
202         southPanel.add(confirmPanel, BorderLayout.CENTER);
203         southPanel.add(statusBar, BorderLayout.SOUTH);
204     } // jbInit
205

206     /**
207      * Init OK to be able to make changes?
208      * @return on if initialized
209      */

210     public boolean isInitOK()
211     {
212         return p_initOK;
213     } // isInitOK
214

215     /**
216      * Dynamic Init
217      * @throws Exception if Lookups cannot be initialized
218      * @return true if initialized
219      */

220     abstract boolean dynInit() throws Exception JavaDoc;
221
222     /**
223      * Init Business Partner Details
224      * @param C_BPartner_ID BPartner
225      */

226     abstract void initBPDetails(int C_BPartner_ID);
227
228     /**
229      * Add Info
230      */

231     abstract void info();
232
233     /**
234      * Save & Insert Data
235      * @return true if saved
236      */

237     abstract boolean save();
238
239     /*************************************************************************/
240
241     /**
242      * Action Listener
243      * @param e event
244      */

245     public void actionPerformed(ActionEvent e)
246     {
247         Log.trace(Log.l3_Util, "VCreateFrom.actionPerformed " + e.getActionCommand());
248     // if (m_action)
249
// return;
250
// m_action = true;
251

252         // OK - Save
253
if (e.getActionCommand().equals(ConfirmPanel.A_OK))
254         {
255             if (save())
256                 dispose();
257         }
258         // Cancel
259
else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
260         {
261             dispose();
262         }
263     // m_action = false;
264
} // actionPerformed
265

266     /**
267      * Table Model Listener
268      * @param e event
269      */

270     public void tableChanged (TableModelEvent e)
271     {
272         int type = -1;
273         if (e != null)
274         {
275             type = e.getType();
276             if (type != TableModelEvent.UPDATE)
277                 return;
278         }
279         Log.trace(Log.l3_Util, "VCreateFrom.tableChanged " + type);
280         info();
281     } // tableChanged
282

283     /*************************************************************************/
284
285     /**
286      * Load BPartner Field
287      * @param forInvoice true if Invoices are to be created, false receipts
288      * @throws Exception if Lookups cannot be initialized
289      */

290     protected void initBPartner (boolean forInvoice) throws Exception JavaDoc
291     {
292         // load BPartner
293
int AD_Column_ID = 3499; // C_Invoice.C_BPartner_ID
294
MLookup lookup = MLookupFactory.get (Env.getCtx(), p_WindowNo, 0, AD_Column_ID, DisplayType.Search);
295         bPartnerField = new VLookup ("C_BPartner_ID", true, false, true,
296             lookup, DisplayType.Search, p_WindowNo);
297         //
298
int C_BPartner_ID = Env.getContextAsInt(Env.getCtx(), p_WindowNo, "C_BPartner_ID");
299         bPartnerField.setValue(new Integer JavaDoc(C_BPartner_ID));
300
301         // initial loading
302
initBPartnerOIS(C_BPartner_ID, forInvoice);
303     } // initBPartner
304

305     /**
306      * Load PBartner dependent Order/Invoice/Shipment Field.
307      * @param C_BPartner_ID BPartner
308      * @param forInvoice for invoice
309      */

310     protected void initBPartnerOIS (int C_BPartner_ID, boolean forInvoice)
311     {
312         Log.trace(Log.l3_Util, "VCreateFrom.initBPartnerOIS BPartner=" + C_BPartner_ID);
313         KeyNamePair pp = new KeyNamePair(0,"");
314
315         // load PO Orders - Closed, Completed
316
orderField.removeActionListener(this);
317         orderField.removeAllItems();
318         orderField.addItem(pp);
319         // Display
320
StringBuffer JavaDoc display = new StringBuffer JavaDoc("o.DocumentNo||' - ' ||")
321             .append(DB.TO_CHAR("o.DateOrdered", DisplayType.Date, Env.getAD_Language(Env.getCtx())))
322             .append("||' - '||")
323             .append(DB.TO_CHAR("o.GrandTotal", DisplayType.Amount, Env.getAD_Language(Env.getCtx())));
324         //
325
String JavaDoc column = "m.M_InOutLine_ID";
326         if (forInvoice)
327             column = "m.C_InvoiceLine_ID";
328         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT o.C_Order_ID,").append(display)
329             .append(" FROM C_Order o "
330             + "WHERE o.C_BPartner_ID=? AND o.IsSOTrx='N' AND o.DocStatus IN ('CL','CO')"
331             + " AND o.C_Order_ID IN "
332                   + "(SELECT ol.C_Order_ID FROM C_OrderLine ol"
333                   + " LEFT OUTER JOIN M_MatchPO m ON (ol.C_OrderLine_ID=m.C_OrderLine_ID) "
334                   + "GROUP BY ol.C_Order_ID,ol.C_OrderLine_ID, ol.QtyOrdered,").append(column)
335                   .append(" HAVING (ol.QtyOrdered <> SUM(m.Qty) AND ").append(column)
336                   .append(" IS NOT NULL) OR ").append(column).append(" IS NULL) "
337             + "ORDER BY o.DateOrdered");
338         try
339         {
340             PreparedStatement pstmt = DB.prepareStatement(sql.toString());
341             pstmt.setInt(1, C_BPartner_ID);
342             ResultSet rs = pstmt.executeQuery();
343             while (rs.next())
344             {
345                 pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
346                 orderField.addItem(pp);
347             }
348             rs.close();
349             pstmt.close();
350         }
351         catch (SQLException e)
352         {
353             Log.error ("VCreateFrom.initIOS - Order\nSQL=" + sql.toString(), e);
354         }
355         orderField.setSelectedIndex(0);
356         orderField.addActionListener(this);
357
358         initBPDetails(C_BPartner_ID);
359     } // initBPartnerOIS
360

361     /**
362      * Load Data - Order
363      * @param C_Order_ID Order
364      * @param forInvoice true if for invoice vs. delivery qty
365      */

366     protected void loadOrder (int C_Order_ID, boolean forInvoice)
367     {
368         /**
369          * Selected - 0
370          * Qty - 1
371          * C_UOM_ID - 2
372          * M_Product_ID - 3
373          * OrderLine - 4
374          * ShipmentLine - 5
375          * InvoiceLine - 6
376          */

377         Log.trace(Log.l3_Util, "VCreateFrom.loadOrder - " + C_Order_ID);
378         p_C_Order_ID = C_Order_ID; // save
379

380         Vector data = new Vector();
381         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT l.QtyOrdered-SUM(NVL(m.Qty,0)),"
382             + " l.C_UOM_ID, uom.UOMSymbol," // 2..3
383
+ " COALESCE(l.M_Product_ID,0),COALESCE(p.Name,c.Name)," // 4..5
384
+ " l.C_OrderLine_ID,l.Line " // 6..7
385
+ "FROM C_OrderLine l"
386             + " LEFT OUTER JOIN M_MatchPO m ON (l.C_OrderLine_ID=m.C_OrderLine_ID AND ");
387         sql.append(forInvoice ? "m.C_InvoiceLine_ID" : "m.M_InOutLine_ID");
388         sql.append(" IS NOT NULL)")
389             .append(" LEFT OUTER JOIN M_Product p ON (l.M_Product_ID=p.M_Product_ID)"
390             + " LEFT OUTER JOIN C_Charge c ON (l.C_Charge_ID=c.C_Charge_ID)");
391         if (Env.isBaseLanguage(Env.getCtx(), "C_UOM"))
392             sql.append(" LEFT OUTER JOIN C_UOM uom ON (l.C_UOM_ID=uom.C_UOM_ID)");
393         else
394             sql.append(" LEFT OUTER JOIN C_UOM_Trl uom ON (l.C_UOM_ID=uom.C_UOM_ID AND uom.AD_Language='")
395                 .append(Env.getAD_Language(Env.getCtx())).append("')");
396         //
397
sql.append(" WHERE l.C_Order_ID=? " // #1
398
+ "GROUP BY l.QtyOrdered, l.C_UOM_ID,uom.UOMSymbol, l.M_Product_ID,p.Name,c.Name, l.Line,l.C_OrderLine_ID "
399             + "ORDER BY l.Line");
400         //
401
Log.trace(Log.l6_Database, "VCreateFrom.loadOrder", sql);
402         try
403         {
404             PreparedStatement pstmt = DB.prepareStatement(sql.toString());
405             pstmt.setInt(1, C_Order_ID);
406             ResultSet rs = pstmt.executeQuery();
407             while (rs.next())
408             {
409                 Vector line = new Vector();
410                 line.add(new Boolean JavaDoc(false)); // 0-Selection
411
line.add(new Double JavaDoc(rs.getDouble(1))); // 1-Qty
412
KeyNamePair pp = new KeyNamePair(rs.getInt(2), rs.getString(3).trim());
413                 line.add(pp); // 2-UOM
414
pp = new KeyNamePair(rs.getInt(4), rs.getString(5));
415                 line.add(pp); // 3-Product
416
pp = new KeyNamePair(rs.getInt(6), rs.getString(7));
417                 line.add(pp); // 4-Order
418
line.add(null); // 5-Ship
419
line.add(null); // 6-Invoice
420
data.add(line);
421             }
422             rs.close();
423             pstmt.close();
424         }
425         catch (SQLException e)
426         {
427             Log.error("VCreateFrom.loadOrder\nSQL=" + sql.toString(), e);
428         }
429         loadTableOIS (data);
430     } // LoadOrder
431

432
433     /**
434      * Load Order/Invoice/Shipment data into Table
435      * @param data data
436      */

437     protected void loadTableOIS (Vector data)
438     {
439         // Header Info
440
Vector columnNames = new Vector(7);
441         columnNames.add(Msg.getMsg(Env.getCtx(), "Select"));
442         columnNames.add(Msg.translate(Env.getCtx(), "Quantity"));
443         columnNames.add(Msg.translate(Env.getCtx(), "C_UOM_ID"));
444         columnNames.add(Msg.translate(Env.getCtx(), "M_Product_ID"));
445         columnNames.add(Msg.getElement(Env.getCtx(), "C_Order_ID", false));
446         columnNames.add(Msg.getElement(Env.getCtx(), "M_InOut_ID", false));
447         columnNames.add(Msg.getElement(Env.getCtx(), "C_Invoice_ID", false));
448
449         // Remove previous listeners
450
dataTable.getModel().removeTableModelListener(this);
451         // Set Model
452
DefaultTableModel model = new DefaultTableModel(data, columnNames);
453         model.addTableModelListener(this);
454         dataTable.setModel(model);
455         //
456
dataTable.setColumnClass(0, Boolean JavaDoc.class, false); // 0-Selection
457
dataTable.setColumnClass(1, Double JavaDoc.class, true); // 1-Qty
458
dataTable.setColumnClass(2, String JavaDoc.class, true); // 2-UOM
459
dataTable.setColumnClass(3, String JavaDoc.class, true); // 3-Product
460
dataTable.setColumnClass(4, String JavaDoc.class, true); // 4-Order
461
dataTable.setColumnClass(5, String JavaDoc.class, true); // 5-Ship
462
dataTable.setColumnClass(6, String JavaDoc.class, true); // 6-Invoice
463
// Table UI
464
dataTable.autoSize();
465     } // loadOrder
466

467 } // VCreateFrom
468
Popular Tags