VCreateFromShipment:

* fix ORA-12704: character set mismatch or SQL query from loadInvoice and loadOrder
* fix NPE when we deselect the BPartner field
* organized imports
* fixed indentation
* improved readability
* close ResultSet and PreparedStatements correctly
This commit is contained in:
teo_sarca 2008-12-09 11:17:03 +00:00
parent b8d7f60427
commit 903f073e50
1 changed files with 198 additions and 150 deletions

View File

@ -16,20 +16,46 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.grid; package org.compiere.grid;
import java.awt.event.*; import java.awt.Component;
import java.beans.*; import java.awt.event.ActionEvent;
import java.math.*; import java.beans.PropertyChangeEvent;
import java.sql.*; import java.beans.VetoableChangeListener;
import java.util.*; import java.math.BigDecimal;
import java.util.logging.*; import java.sql.PreparedStatement;
import javax.swing.*; import java.sql.ResultSet;
import java.awt.*; import java.sql.SQLException;
import javax.swing.event.*; import java.util.List;
import javax.swing.table.*; import java.util.Vector;
import java.util.logging.Level;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import org.adempiere.plaf.AdempierePLAF; import org.adempiere.plaf.AdempierePLAF;
import org.compiere.grid.ed.*; import org.compiere.grid.ed.VLocator;
import org.compiere.model.*; import org.compiere.model.GridTab;
import org.compiere.util.*; import org.compiere.model.MInOut;
import org.compiere.model.MInOutLine;
import org.compiere.model.MInvoice;
import org.compiere.model.MInvoiceLine;
import org.compiere.model.MLocator;
import org.compiere.model.MLocatorLookup;
import org.compiere.model.MOrder;
import org.compiere.model.MOrderLine;
import org.compiere.model.MProduct;
import org.compiere.model.MRMA;
import org.compiere.model.MRMALine;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;
/** /**
* Create Shipments Transactions - from PO Orders or AP Invoices * Create Shipments Transactions - from PO Orders or AP Invoices
@ -39,14 +65,16 @@ import org.compiere.util.*;
*/ */
public class VCreateFromShipment extends VCreateFrom implements VetoableChangeListener public class VCreateFromShipment extends VCreateFrom implements VetoableChangeListener
{ {
private static final long serialVersionUID = 1L;
/** /**
* Cell editor specific for the MLocator in this form's table. * Cell editor specific for the MLocator in this form's table.
* *
*/ */
public class InnerLocatorTableCellEditor extends AbstractCellEditor implements TableCellEditor, CellEditorListener { public class InnerLocatorTableCellEditor extends AbstractCellEditor
implements TableCellEditor, CellEditorListener
{
private static final long serialVersionUID = 1L;
private KeyNamePair m_locatorKey; private KeyNamePair m_locatorKey;
private VLocator v_loc; private VLocator v_loc;
private JTable m_table; private JTable m_table;
@ -399,8 +427,10 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
else if (e.getSource().equals(sameWarehouseCb)) else if (e.getSource().equals(sameWarehouseCb))
{ {
initBPartnerOIS(((Integer)bPartnerField.getValue()).intValue(), false); initBPartnerOIS(((Integer)bPartnerField.getValue()).intValue(), false);
} else if (e.getSource().equals(upcField)) { }
checkProductUsingUpc(); else if (e.getSource().equals(upcField))
{
checkProductUsingUPC();
} }
} // actionPerformed } // actionPerformed
@ -410,19 +440,18 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
* Checks the UPC value and checks if the UPC matches any of the products in the * Checks the UPC value and checks if the UPC matches any of the products in the
* list. * list.
*/ */
private void checkProductUsingUpc() { private void checkProductUsingUPC()
{
String upc = upcField.getText(); String upc = upcField.getText();
DefaultTableModel model = (DefaultTableModel)dataTable.getModel(); DefaultTableModel model = (DefaultTableModel)dataTable.getModel();
// Lookup UPC // Lookup UPC
java.util.List<MProduct> products = MProduct.getByUPC(Env.getCtx(), upc, null); List<MProduct> products = MProduct.getByUPC(Env.getCtx(), upc, null);
MProduct product; for (MProduct product : products)
int row; {
BigDecimal qty; int row = findProductRow(product.get_ID());
for (Iterator<MProduct> it = products.iterator(); it.hasNext();) { if (row >= 0)
product = it.next(); {
row = findProductRow(product.get_ID()); BigDecimal qty = (BigDecimal)model.getValueAt(row, COL_QTY);
if (row>=0) {
qty = (BigDecimal)model.getValueAt(row, COL_QTY);
model.setValueAt(qty, row, COL_QTY); model.setValueAt(qty, row, COL_QTY);
model.setValueAt(Boolean.TRUE, row, COL_SELECT); model.setValueAt(Boolean.TRUE, row, COL_SELECT);
model.fireTableRowsUpdated(row, row); model.fireTableRowsUpdated(row, row);
@ -439,7 +468,8 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
* @return Row of the product or -1 if non existing. * @return Row of the product or -1 if non existing.
* *
*/ */
private int findProductRow(int M_Product_ID) { private int findProductRow(int M_Product_ID)
{
DefaultTableModel model = (DefaultTableModel)dataTable.getModel(); DefaultTableModel model = (DefaultTableModel)dataTable.getModel();
KeyNamePair kp; KeyNamePair kp;
for (int i=0; i<model.getRowCount(); i++) { for (int i=0; i<model.getRowCount(); i++) {
@ -462,7 +492,9 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
// BPartner - load Order/Invoice/Shipment // BPartner - load Order/Invoice/Shipment
if (e.getPropertyName().equals("C_BPartner_ID")) if (e.getPropertyName().equals("C_BPartner_ID"))
{ {
int C_BPartner_ID = ((Integer)e.getNewValue()).intValue(); int C_BPartner_ID = 0;
if (e.getNewValue() instanceof Number)
C_BPartner_ID = ((Number)e.getNewValue()).intValue();
initBPartnerOIS (C_BPartner_ID, false); initBPartnerOIS (C_BPartner_ID, false);
} }
tableChanged(null); tableChanged(null);
@ -489,12 +521,12 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
log.config("C_Order_ID=" + C_Order_ID); log.config("C_Order_ID=" + C_Order_ID);
p_order = new MOrder (Env.getCtx(), C_Order_ID, null); // save p_order = new MOrder (Env.getCtx(), C_Order_ID, null); // save
Vector<Vector> data = new Vector<Vector>(); Vector<Vector<?>> data = new Vector<Vector<?>>();
StringBuffer sql = new StringBuffer("SELECT " StringBuffer sql = new StringBuffer("SELECT "
+ "l.QtyOrdered-SUM(COALESCE(m.Qty,0))," // 1 + "l.QtyOrdered-SUM(COALESCE(m.Qty,0))," // 1
+ "CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END," // 2 + "CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END," // 2
+ " l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name)," // 3..4 + " l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name)," // 3..4
+ " p.M_Locator_ID, COALESCE(loc.value,''), " // 5..6 + " p.M_Locator_ID, loc.Value, " // 5..6
+ " COALESCE(l.M_Product_ID,0),COALESCE(p.Name,c.Name), " // 7..8 + " COALESCE(l.M_Product_ID,0),COALESCE(p.Name,c.Name), " // 7..8
+ " po.VendorProductNo, " // 9 + " po.VendorProductNo, " // 9
+ " l.C_OrderLine_ID,l.Line " // 10..11 + " l.C_OrderLine_ID,l.Line " // 10..11
@ -514,16 +546,18 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
// //
sql.append(" WHERE l.C_Order_ID=? " // #1 sql.append(" WHERE l.C_Order_ID=? " // #1
+ "GROUP BY l.QtyOrdered,CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END, " + "GROUP BY l.QtyOrdered,CASE WHEN l.QtyOrdered=0 THEN 0 ELSE l.QtyEntered/l.QtyOrdered END, "
+ "l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name), p.m_locator_id, COALESCE(loc.value,''), po.VendorProductNo, " + "l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name), p.M_Locator_ID, loc.Value, po.VendorProductNo, "
+ "l.M_Product_ID,COALESCE(p.Name,c.Name), l.Line,l.C_OrderLine_ID " + "l.M_Product_ID,COALESCE(p.Name,c.Name), l.Line,l.C_OrderLine_ID "
+ "ORDER BY l.Line"); + "ORDER BY l.Line");
// //
log.finer(sql.toString()); log.finer(sql.toString());
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(sql.toString(), null); pstmt = DB.prepareStatement(sql.toString(), null);
pstmt.setInt(1, C_Order_ID); pstmt.setInt(1, C_Order_ID);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) while (rs.next())
{ {
Vector<Object> line = new Vector<Object>(); Vector<Object> line = new Vector<Object>();
@ -547,12 +581,16 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
line.add(null); // 8-Invoice line.add(null); // 8-Invoice
data.add(line); data.add(line);
} }
rs.close();
pstmt.close();
} }
catch (Exception e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql.toString(), e); log.log(Level.SEVERE, sql.toString(), e);
//throw new DBException(e, sql.toString());
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
loadTableOIS (data); loadTableOIS (data);
} // LoadOrder } // LoadOrder
@ -571,17 +609,18 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
* ShipmentLine - 7 * ShipmentLine - 7
* InvoiceLine - 8 * InvoiceLine - 8
*/ */
private void loadInvoice(int C_Invoice_ID) { private void loadInvoice(int C_Invoice_ID)
{
log.config("C_Invoice_ID=" + C_Invoice_ID); log.config("C_Invoice_ID=" + C_Invoice_ID);
m_invoice = new MInvoice(Env.getCtx(), C_Invoice_ID, null); // save m_invoice = new MInvoice(Env.getCtx(), C_Invoice_ID, null); // save
p_order = null; p_order = null;
m_rma = null; m_rma = null;
Vector<Vector> data = new Vector<Vector>(); Vector<Vector<?>> data = new Vector<Vector<?>>();
StringBuffer sql = new StringBuffer("SELECT " // Entered UOM StringBuffer sql = new StringBuffer("SELECT " // Entered UOM
+ "l.QtyInvoiced-SUM(NVL(mi.Qty,0)),l.QtyEntered/l.QtyInvoiced," + "l.QtyInvoiced-SUM(NVL(mi.Qty,0)),l.QtyEntered/l.QtyInvoiced,"
+ " l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name)," // 3..4 + " l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name)," // 3..4
+ " p.M_Locator_ID, COALESCE(loc.value,''), " // 5..6 + " p.M_Locator_ID, loc.Value, " // 5..6
+ " l.M_Product_ID,p.Name, po.VendorProductNo, l.C_InvoiceLine_ID,l.Line," // 7..11 + " l.M_Product_ID,p.Name, po.VendorProductNo, l.C_InvoiceLine_ID,l.Line," // 7..11
+ " l.C_OrderLine_ID " // 12 + " l.C_OrderLine_ID " // 12
+ " FROM C_InvoiceLine l "); + " FROM C_InvoiceLine l ");
@ -600,15 +639,18 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
.append(" WHERE l.C_Invoice_ID=? AND l.QtyInvoiced<>0 ") .append(" WHERE l.C_Invoice_ID=? AND l.QtyInvoiced<>0 ")
.append("GROUP BY l.QtyInvoiced,l.QtyEntered/l.QtyInvoiced," .append("GROUP BY l.QtyInvoiced,l.QtyEntered/l.QtyInvoiced,"
+ "l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name)," + "l.C_UOM_ID,COALESCE(uom.UOMSymbol,uom.Name),"
+ "p.M_Locator_ID, COALESCE(loc.value,''), " + "p.M_Locator_ID, loc.Value, "
+ "l.M_Product_ID,p.Name, po.VendorProductNo, l.C_InvoiceLine_ID,l.Line,l.C_OrderLine_ID ") + "l.M_Product_ID,p.Name, po.VendorProductNo, l.C_InvoiceLine_ID,l.Line,l.C_OrderLine_ID ")
.append("ORDER BY l.Line"); .append("ORDER BY l.Line");
PreparedStatement pstmt = null;
try { ResultSet rs = null;
PreparedStatement pstmt = DB.prepareStatement(sql.toString(), null); try
{
pstmt = DB.prepareStatement(sql.toString(), null);
pstmt.setInt(1, C_Invoice_ID); pstmt.setInt(1, C_Invoice_ID);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next())
{
Vector<Object> line = new Vector<Object>(7); Vector<Object> line = new Vector<Object>(7);
line.add(new Boolean(false)); // 0-Selection line.add(new Boolean(false)); // 0-Selection
BigDecimal qtyInvoiced = rs.getBigDecimal(1); BigDecimal qtyInvoiced = rs.getBigDecimal(1);
@ -634,10 +676,16 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
line.add(pp); // 8-Invoice line.add(pp); // 8-Invoice
data.add(line); data.add(line);
} }
rs.close(); }
pstmt.close(); catch (SQLException e)
} catch (SQLException e) { {
log.log(Level.SEVERE, sql.toString(), e); log.log(Level.SEVERE, sql.toString(), e);
//throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
loadTableOIS(data); loadTableOIS(data);
} // loadInvoice } // loadInvoice
@ -653,7 +701,7 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
m_rma = new MRMA(Env.getCtx(), M_RMA_ID, null); m_rma = new MRMA(Env.getCtx(), M_RMA_ID, null);
Vector<Vector> data = new Vector<Vector>(); Vector<Vector<?>> data = new Vector<Vector<?>>();
StringBuffer sqlStmt = new StringBuffer(); StringBuffer sqlStmt = new StringBuffer();
sqlStmt.append("SELECT rl.M_RMALine_ID, rl.line, rl.Qty - rl.QtyDelivered, iol.M_Product_ID, p.Name, uom.C_UOM_ID, COALESCE(uom.UOMSymbol,uom.Name) "); sqlStmt.append("SELECT rl.M_RMALine_ID, rl.line, rl.Qty - rl.QtyDelivered, iol.M_Product_ID, p.Name, uom.C_UOM_ID, COALESCE(uom.UOMSymbol,uom.Name) ");
sqlStmt.append("FROM M_RMALine rl INNER JOIN M_InOutLine iol ON rl.M_InOutLine_ID=iol.M_InOutLine_ID "); sqlStmt.append("FROM M_RMALine rl INNER JOIN M_InOutLine iol ON rl.M_InOutLine_ID=iol.M_InOutLine_ID ");
@ -804,7 +852,7 @@ public class VCreateFromShipment extends VCreateFrom implements VetoableChangeLi
C_InvoiceLine_ID = pp.getKey(); C_InvoiceLine_ID = pp.getKey();
if (C_InvoiceLine_ID != 0) if (C_InvoiceLine_ID != 0)
il = new MInvoiceLine (Env.getCtx(), C_InvoiceLine_ID, null); il = new MInvoiceLine (Env.getCtx(), C_InvoiceLine_ID, null);
boolean isInvoiced = (C_InvoiceLine_ID != 0); //boolean isInvoiced = (C_InvoiceLine_ID != 0);
// Precision of Qty UOM // Precision of Qty UOM
int precision = 2; int precision = 2;
if (M_Product_ID != 0) if (M_Product_ID != 0)