?? vcreatefromshipment.java
字號(hào):
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.grid;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
import java.math.*;
import java.text.*;
import java.beans.*;
import java.awt.event.*;
import org.compiere.util.*;
import org.compiere.model.*;
import org.compiere.plaf.*;
import org.compiere.grid.ed.*;
/**
* Create Transactions for Shipments - from PO Orders or AP Invoices
*
* @author Jorg Janke
* @version $Id: VCreateFromShipment.java,v 1.12 2003/01/27 06:14:49 jjanke Exp $
*/
public class VCreateFromShipment extends VCreateFrom implements VetoableChangeListener
{
/**
* Protected Constructor
* @param mTab MTab
*/
VCreateFromShipment(MTab mTab)
{
super (mTab);
// Log.trace(Log.l1_User, "VCreateFromShipment");
} // VCreateFromShipment
/** Loaded Invoice */
private int m_C_Invoice_ID = 0;
/**
* Dynamic Init
* @throws Exception if Lookups cannot be initialized
* @return true if initialized
*/
protected boolean dynInit() throws Exception
{
Log.trace(Log.l3_Util, "VCreateFromShipment.dynInit");
setTitle(Msg.getElement(Env.getCtx(), "M_InOut_ID", false) + " .. " + Msg.translate(Env.getCtx(), "CreateFrom"));
parameterBankPanel.setVisible(false);
shipmentLabel.setVisible(false);
shipmentField.setVisible(false);
// load Locator
int AD_Column_ID = 3537; // M_InOut.M_Locator_ID
MLocator locator = new MLocator(Env.getCtx(), m_WindowNo);
locatorField = new VLocator ("M_Locator_ID", true, false, true, locator);
initBPartner(false);
bPartnerField.addVetoableChangeListener(this);
return true;
} // dynInit
/**
* Init Details - load invoices not shipped
* @param C_BPartner_ID BPartner
*/
protected void initBPDetails(int C_BPartner_ID)
{
Log.trace(Log.l3_Util, "VCreateFromShipment.initBPDetails");
// load AP Invoice closed or complete
invoiceField.removeActionListener(this);
invoiceField.removeAllItems();
// None
KeyNamePair pp = new KeyNamePair(0,"");
invoiceField.addItem(pp);
StringBuffer display = new StringBuffer("i.DocumentNo||' - '||")
.append(DB.TO_CHAR("DateInvoiced", DisplayType.Date, Env.getAD_Language(Env.getCtx())))
.append("|| ' - ' ||")
.append(DB.TO_CHAR("GrandTotal", DisplayType.Amount, Env.getAD_Language(Env.getCtx())));
//
StringBuffer sql = new StringBuffer("SELECT i.C_Invoice_ID,").append(display)
.append(" FROM C_Invoice i "
+ "WHERE i.C_BPartner_ID=? AND i.IsSOTrx='N' AND i.DocStatus IN ('CL','CO')"
+ " AND i.C_Invoice_ID IN "
+ "(SELECT il.C_Invoice_ID FROM C_InvoiceLine il"
+ " LEFT OUTER JOIN M_MatchInv mi ON (il.C_InvoiceLine_ID=mi.C_InvoiceLine_ID) "
+ "GROUP BY il.C_Invoice_ID,mi.C_InvoiceLine_ID,il.QtyInvoiced "
+ "HAVING (il.QtyInvoiced<>SUM(mi.Qty) AND mi.C_InvoiceLine_ID IS NOT NULL)"
+ " OR mi.C_InvoiceLine_ID IS NULL) "
+ "ORDER BY i.DateInvoiced");
try
{
PreparedStatement pstmt = DB.prepareStatement(sql.toString());
pstmt.setInt(1, C_BPartner_ID);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
invoiceField.addItem(pp);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error ("VCreateFromShipment.initBPDetails\nSQL=" + sql, e);
}
invoiceField.setSelectedIndex(0);
invoiceField.addActionListener(this);
} // initBPDetails
/**
* Action Listener
* @param e event
*/
public void actionPerformed(ActionEvent e)
{
super.actionPerformed(e);
Log.trace(Log.l3_Util, "VCreateFromShipment.actionPerformed " + e.getActionCommand());
// Order
if (e.getSource().equals(orderField))
{
KeyNamePair pp = (KeyNamePair)orderField.getSelectedItem();
if (pp == null || pp.getKey() == 0)
;
else
{
int C_Order_ID = pp.getKey();
// set Invoice and Shipment to Null
invoiceField.setSelectedIndex(-1);
shipmentField.setSelectedIndex(-1);
loadOrder(C_Order_ID, false);
}
}
// Invoice
else if (e.getSource().equals(invoiceField))
{
KeyNamePair pp = (KeyNamePair)invoiceField.getSelectedItem();
if (pp == null || pp.getKey() == 0)
;
else
{
int C_Invoice_ID = pp.getKey();
// set Order and Shipment to Null
orderField.setSelectedIndex(-1);
shipmentField.setSelectedIndex(-1);
loadInvoice(C_Invoice_ID);
}
}
} // actionPerformed
/**
* Change Listener
* @param e event
*/
public void vetoableChange (PropertyChangeEvent e)
{
Log.trace(Log.l3_Util, "VCreateFromShipment.vetoableChange " + e.getPropertyName() + "=" + e.getNewValue());
// BPartner - load Order/Invoice/Shipment
if (e.getPropertyName() == "C_BPartner_ID")
{
int C_BPartner_ID = ((Integer)e.getNewValue()).intValue();
initBPartnerOIS (C_BPartner_ID, false);
}
tableChanged(null);
} // vetoableChange
/**
* Load Data - Invoice
* @param C_Invoice_ID Invoice
*/
private void loadInvoice (int C_Invoice_ID)
{
Log.trace(Log.l3_Util, "VCreateFromShipment.loadInvoice - " + C_Invoice_ID);
m_C_Invoice_ID = C_Invoice_ID; // save
Vector data = new Vector();
StringBuffer sql = new StringBuffer("SELECT l.QtyInvoiced-SUM(NVL(mi.Qty,0)),"
+ " l.C_UOM_ID,uom.UOMSymbol," // 2..3
+ " l.M_Product_ID,p.Name, l.C_InvoiceLine_ID,l.Line," // 4..7
+ " l.C_OrderLine_ID "); // 8
if (Env.isBaseLanguage(Env.getCtx(), "C_UOM"))
{
sql.append("FROM C_UOM uom, C_InvoiceLine l, M_Product p, M_MatchInv mi ");
sql.append("WHERE l.C_UOM_ID=uom.C_UOM_ID");
}
else
{
sql.append("FROM C_UOM_Trl uom, C_InvoiceLine_v l, M_Product p, M_MatchInv mi ");
sql.append("WHERE l.C_UOM_ID=uom.C_UOM_ID AND uom.AD_Language='").append(Env.getAD_Language(Env.getCtx())).append("'");
}
sql.append(" AND l.M_Product_ID=p.M_Product_ID"
+ " AND l.C_InvoiceLine_ID=mi.C_InvoiceLine_ID(+)"
+ " AND l.C_Invoice_ID=? " // #1
+ "GROUP BY l.QtyInvoiced,l.C_UOM_ID,uom.UOMSymbol,l.M_Product_ID,p.Name, l.C_InvoiceLine_ID,l.Line,l.C_OrderLine_ID "
+ "ORDER BY l.Line");
try
{
PreparedStatement pstmt = DB.prepareStatement(sql.toString());
pstmt.setInt(1, C_Invoice_ID);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
Vector line = new Vector(7);
line.add(new Boolean(false)); // 0-Selection
line.add(new Double(rs.getDouble(1))); // 1-Qty
KeyNamePair pp = new KeyNamePair(rs.getInt(2), rs.getString(3).trim());
line.add(pp); // 2-UOM
pp = new KeyNamePair(rs.getInt(4), rs.getString(5));
line.add(pp); // 3-Product
int i = rs.getInt(8);
if (rs.wasNull())
line.add(null); // 4-Order
else
line.add(new KeyNamePair(i," "));
line.add(null); // 5-Ship
pp = new KeyNamePair(rs.getInt(6), rs.getString(7));
line.add(pp); // 6-Invoice
data.add(line);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error ("VCreateFromShipment.loadInvoice", e);
}
loadTableOIS (data);
} // loadInvoice
/**
* List number of rows selected
*/
protected void info()
{
TableModel model = dataTable.getModel();
int rows = model.getRowCount();
int count = 0;
for (int i = 0; i < rows; i++)
{
if (((Boolean)model.getValueAt(i, 0)).booleanValue())
count++;
}
statusBar.setStatusLine(String.valueOf(count));
} // info
/**
* Save - create Shipments
* @return true if saved
*/
protected boolean save()
{
Log.trace(Log.l3_Util, "VCreateFromShipment.save");
// fixed values
Integer loc = (Integer)locatorField.getValue();
if (loc == null || loc.intValue() == 0)
{
locatorField.setBackground(CompierePLAF.getFieldBackground_Error());
return false;
}
int M_Locator_ID = loc.intValue();
//
int AD_Client_ID = ((Integer)m_mTab.getValue("AD_Client_ID")).intValue();
int AD_Org_ID = ((Integer)m_mTab.getValue("AD_Org_ID")).intValue();
int CreatedBy = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");
int M_InOut_ID = ((Integer)m_mTab.getValue("M_InOut_ID")).intValue();
Log.trace( Log.l4_Data, "Client=" + AD_Client_ID + ", Org=" + AD_Org_ID
+ ", User=" + CreatedBy + ", InOut=" + M_InOut_ID + ", Locator=" + M_Locator_ID);
/**
* Selected - 0
* Qty - 1
* C_UOM_ID - 2
* M_Product_ID - 3
* OrderLine - 4
* ShipmentLine - 5
* InvoiceLine - 6
*/
// Lines
TableModel model = dataTable.getModel();
int rows = model.getRowCount();
for (int i = 0; i < rows; i++)
{
if (((Boolean)model.getValueAt(i, 0)).booleanValue())
{
// variable values
int M_InOutLine_ID = DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "M_InOutLine");
Double d = (Double)model.getValueAt(i, 1); // 1-Qty
BigDecimal MovementQty = new BigDecimal(d.doubleValue());
KeyNamePair pp = (KeyNamePair)model.getValueAt(i, 2); // 2-UOM
int C_UOM_ID = pp.getKey();
pp = (KeyNamePair)model.getValueAt(i, 3); // 3-Product
int M_Product_ID = pp.getKey();
int C_OrderLine_ID = 0;
pp = (KeyNamePair)model.getValueAt(i, 4); // 4-OrderLine
if (pp != null)
C_OrderLine_ID = pp.getKey();
int C_InvoiceLine_ID = 0;
pp = (KeyNamePair)model.getValueAt(i, 6); // 6-InvoiceLine
if (pp != null)
C_InvoiceLine_ID = pp.getKey();
boolean isInvoiced = (C_InvoiceLine_ID != 0);
//
Log.trace(Log.l5_DData, "Line=" + M_InOutLine_ID + ", Qty=" + MovementQty
+ ", Product=" + M_Product_ID + ", OrderLine=" + C_OrderLine_ID + ", InvoiceLine=" + C_InvoiceLine_ID);
//
String Description = null;
StringBuffer sql = new StringBuffer("INSERT INTO M_InOutLine");
sql.append("(M_InOutLine_ID,M_InOut_ID,");
sql.append("AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,");
sql.append("Line,Description,");
sql.append("M_Product_ID,C_UOM_ID,MovementQty,");
sql.append("C_OrderLine_ID,M_Locator_ID,IsInvoiced)");
sql.append(" VALUES (");
//
sql.append(M_InOutLine_ID).append(",").append(M_InOut_ID).append(",");
sql.append(AD_Client_ID).append(",").append(AD_Org_ID).append(",'Y',");
sql.append("SysDate,").append(CreatedBy).append(",SysDate,").append(CreatedBy).append(",");
// Line,
sql.append("(SELECT (NVL(Max(Line),0))+10 FROM M_InOutLine WHERE M_InOut_ID=").append(M_InOut_ID).append("),");
// Description
if (Description == null || Description.length() == 0)
sql.append("NULL,");
else
sql.append("'").append(Description).append("',");
// M_Product_ID,C_UOM_ID,MovementQty,
sql.append(M_Product_ID).append(",").append(C_UOM_ID).append(",").append(MovementQty).append(",");
// C_OrderLine_ID,M_Locator_ID,IsInvoiced
if (C_OrderLine_ID == 0)
sql.append("NULL,");
else
sql.append(C_OrderLine_ID).append(",");
sql.append(M_Locator_ID).append(",");
sql.append(isInvoiced ? "'Y')" : "'N')");
//
int no = DB.executeUpdate(sql.toString());
if (no != 1)
Log.error("VCreateFromShipment.save - Line NOT created #" + no);
} // if selected
} // for all rows
/**
* Update Header
* - if linked to another order/invoice - remove link
* - if no link set it
*/
if (m_C_Order_ID != 0)
{
String sql = "UPDATE M_InOut SET C_Order_ID=NULL"
+ " WHERE M_InOut_ID=" + M_InOut_ID
+ " AND C_Order_ID IS NOT NULL AND C_Order_ID <> " + m_C_Order_ID;
int no = DB.executeUpdate(sql);
if (no == 0)
{
sql = "UPDATE M_InOut SET C_Order_ID=" + m_C_Order_ID
+ " WHERE M_InOut_ID=" + M_InOut_ID;
no = DB.executeUpdate(sql);
}
}
if (m_C_Invoice_ID != 0)
{
String sql = "UPDATE M_InOut SET C_Invoice_ID=NULL"
+ " WHERE M_InOut_ID=" + M_InOut_ID
+ " AND C_Invoice_ID IS NOT NULL AND C_Invoice_ID <> " + m_C_Invoice_ID;
int no = DB.executeUpdate(sql);
if (no == 0)
{
sql = "UPDATE M_InOut SET C_Invoice_ID=" + m_C_Invoice_ID
+ " WHERE M_InOut_ID=" + M_InOut_ID;
no = DB.executeUpdate(sql);
}
}
return true;
} // save
} // VCreateFromShipment
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -