⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vcreatefromstatement.java

📁 Java写的ERP系统
💻 JAVA
字号:
/******************************************************************************
 * 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 org.compiere.apps.*;
import org.compiere.util.*;
import org.compiere.model.*;
import org.compiere.grid.ed.*;

/**
 *  Create Transactions for Bank Statements
 *
 *  @author Jorg Janke
 *  @version  $Id: VCreateFromStatement.java,v 1.13 2003/03/20 06:50:59 jjanke Exp $
 */
public class VCreateFromStatement extends VCreateFrom implements VetoableChangeListener
{
	/**
	 *  Protected Constructor
	 *  @param mTab MTab
	 */
	VCreateFromStatement(MTab mTab)
	{
		super (mTab);
		Log.trace(Log.l1_User, "VCreateFromStatement");
	}   //  VCreateFromStatement

	/**
	 *  Dynamic Init
	 *  @throws Exception if Lookups cannot be initialized
	 *  @return true if initialized
	 */
	protected boolean dynInit() throws Exception
	{
		Log.trace(Log.l3_Util, "VCreateFromStatement.dynInit");

		if (m_mTab.getValue("C_BankStatement_ID") == null)
		{
			ADialog.error(0, this, "SaveErrorRowNotFound");
			return false;
		}

		setTitle(Msg.translate(Env.getCtx(), "C_BankStatement_ID") + " .. " + Msg.translate(Env.getCtx(), "CreateFrom"));
		parameterStdPanel.setVisible(false);

		int AD_Column_ID = 4917;        //  C_BankStatement.C_BankAccount_ID
		MLookup lookup = MLookupFactory.create (Env.getCtx(), AD_Column_ID, m_WindowNo,
			DisplayType.TableDir, false);
		bankAccountField = new VLookup ("C_BankAccount_ID", true, false, true,
			lookup, DisplayType.TableDir, m_WindowNo);
		bankAccountField.addVetoableChangeListener(this);
		//  Set Default
		int C_BankAccount_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_BankAccount_ID");
		bankAccountField.setValue(new Integer(C_BankAccount_ID));
		//  initial Loading
		loadBankAccount(C_BankAccount_ID);

		return true;
	}   //  dynInit

	/**
	 *  Init Details (never called)
	 *  @param C_BPartner_ID BPartner
	 */
	protected void initBPDetails(int C_BPartner_ID)
	{
	}   //  initDetails

	/**
	 *  Change Listener
	 *  @param e event
	 */
	public void vetoableChange (PropertyChangeEvent e)
	{
		Log.trace(Log.l3_Util, "VCreateFromStatement.vetoableChange " + e.getPropertyName() + "=" + e.getNewValue());

		//  BankAccount
		if (e.getPropertyName() == "C_BankAccount_ID")
		{
			int C_BankAccount_ID = ((Integer)e.getNewValue()).intValue();
			loadBankAccount(C_BankAccount_ID);
		}
		tableChanged(null);
	}   //  vetoableChange

	/**
	 *  Load Data - Bank Account
	 *  @param C_BankAccount_ID Bank Account
	 */
	private void loadBankAccount (int C_BankAccount_ID)
	{
		Log.trace(Log.l3_Util, "VCreateFromStatement.loadBankAccount - " + C_BankAccount_ID);
		/**
		 *  Selected        - 0
		 *  Date            - 1
		 *  C_Payment_ID    - 2
		 *  C_Currenncy     - 3
		 *  Amt             - 4
		 */
		Vector data = new Vector();
		String sql = "SELECT p.DateTrx,p.C_Payment_ID,p.DocumentNo, p.C_Currency_ID,c.ISO_Code, p.PayAmt,"
			+ "C_Currency_Convert(p.PayAmt,p.C_Currency_ID,ba.C_Currency_ID,?,null,p.AD_Client_ID,p.AD_Org_ID) "   //  #1
			+ "FROM C_BankAccount ba, C_Payment_v p, C_Currency c "
			+ "WHERE p.C_BankAccount_ID=ba.C_BankAccount_ID"
			+ " AND p.C_Currency_ID=c.C_Currency_ID"
			+ " AND p.C_BankAccount_ID=?"                                               //  #2
			+ " AND p.Processed='Y' AND p.IsReconciled='N'"
			+ " AND NOT EXISTS (SELECT * FROM C_BankStatementLine l WHERE p.C_Payment_ID=l.C_Payment_ID)";

		//  Get StatementDate
		Timestamp ts = (Timestamp)m_mTab.getValue("StatementDate");
		if (ts == null)
			ts = new Timestamp(System.currentTimeMillis());

		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql.toString());
			pstmt.setTimestamp(1, ts);
			pstmt.setInt(2, C_BankAccount_ID);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next())
			{
				Vector line = new Vector(6);
				line.add(new Boolean(false));       //  0-Selection
				line.add(rs.getTimestamp(1));       //  1-DateTrx
				KeyNamePair pp = new KeyNamePair(rs.getInt(2), rs.getString(3));
				line.add(pp);                       //  2-C_Payment_ID
				pp = new KeyNamePair(rs.getInt(4), rs.getString(5));
				line.add(pp);                       //  3-Currency
				line.add(rs.getBigDecimal(6));      //  4-PayAmt
				line.add(rs.getBigDecimal(7));      //  5-Conv Amt
				data.add(line);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			Log.error ("VCreateFromStatement.loadBankAccount", e);
		}
		//  Header Info
		Vector columnNames = new Vector(6);
		columnNames.add(Msg.getMsg(Env.getCtx(), "Select"));
		columnNames.add(Msg.translate(Env.getCtx(), "Date"));
		columnNames.add(Msg.getElement(Env.getCtx(), "C_Payment_ID"));
		columnNames.add(Msg.translate(Env.getCtx(), "C_Currency_ID"));
		columnNames.add(Msg.translate(Env.getCtx(), "Amount"));
		columnNames.add(Msg.translate(Env.getCtx(), "ConvertedAmount"));

		//  Remove previous listeners
		dataTable.getModel().removeTableModelListener(this);
		//  Set Model
		DefaultTableModel model = new DefaultTableModel(data, columnNames);
		model.addTableModelListener(this);
		dataTable.setModel(model);
		//
		dataTable.setColumnClass(0, Boolean.class, false);      //  0-Selection
		dataTable.setColumnClass(1, Timestamp.class, true);     //  1-TrxDate
		dataTable.setColumnClass(2, String.class, true);        //  2-Payment
		dataTable.setColumnClass(3, String.class, true);        //  3-Currency
		dataTable.setColumnClass(4, BigDecimal.class, true);    //  4-Amount
		dataTable.setColumnClass(5, BigDecimal.class, true);    //  5-ConvAmount
		//  Table UI
		dataTable.autoSize();
	}   //  loadBankAccount

	/**
	 *  List total amount
	 */
	protected void info()
	{
		DecimalFormat format = DisplayType.getNumberFormat(DisplayType.Amount);

		TableModel model = dataTable.getModel();
		BigDecimal total = new BigDecimal(0.0);
		int rows = model.getRowCount();
		int count = 0;
		for (int i = 0; i < rows; i++)
		{
			if (((Boolean)model.getValueAt(i, 0)).booleanValue())
			{
				total = total.add((BigDecimal)model.getValueAt(i, 4));
				count++;
			}
		}
		statusBar.setStatusLine(String.valueOf(count) + " - " + Msg.getMsg(Env.getCtx(), "Sum") + "  " + format.format(total));
	}   //  infoStatement

	/**
	 *  Save Statement - Insert Data
	 *  @return true if saved
	 */
	protected boolean save()
	{
		Log.trace(Log.l3_Util, "VCreateFromStatement.save");
		TableModel model = dataTable.getModel();
		int rows = model.getRowCount();
		if (rows == 0)
			return false;

		//  fixed values
		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 C_BankStatement_ID = ((Integer)m_mTab.getValue("C_BankStatement_ID")).intValue();;
		Log.trace(Log.l4_Data, "Client=" + AD_Client_ID + ", Org=" + AD_Org_ID
			+ ", User=" + CreatedBy + ", BankStatement=" + C_BankStatement_ID);

		//  Lines
		for (int i = 0; i < rows; i++)
		{
			if (((Boolean)model.getValueAt(i, 0)).booleanValue())
			{
				//  variable values
				int C_BankStatementLine_ID = DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "C_BankStatementLine");
				Timestamp trxDate = (Timestamp)model.getValueAt(i, 1);  //  1-DateTrx
				KeyNamePair pp = (KeyNamePair)model.getValueAt(i, 2);   //  2-C_Payment_ID
				int C_Payment_ID = pp.getKey();
				pp = (KeyNamePair)model.getValueAt(i, 3);               //  3-Currency
				int C_Currency_ID = pp.getKey();
				BigDecimal TrxAmt = (BigDecimal)model.getValueAt(i, 4); //  4-PayAmt
				BigDecimal StmtAmt = (BigDecimal)model.getValueAt(i, 5);//  5-Conv Amt
				//
				Log.trace(Log.l5_DData, "Line=" + C_BankStatementLine_ID + ", Date=" + trxDate
					+ ", Payment=" + C_Payment_ID + ", Currency=" + C_Currency_ID + ", Amt=" + TrxAmt);
				//
				StringBuffer sql = new StringBuffer("INSERT INTO C_BankStatementLine");
				sql.append("(C_BankStatementLine_ID,");
				sql.append("AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,");
				sql.append("C_BankStatement_ID,DateAcct,ValutaDate,");
				sql.append("Line,Description,IsReversal,");
				sql.append("C_Payment_ID,TrxAmt,");
				sql.append("C_Charge_ID,ChargeAmt,InterestAmt,");
				sql.append("C_Currency_ID,StmtAmt)");
				sql.append(" VALUES (");
				//
				sql.append(C_BankStatementLine_ID).append(",");
				sql.append(AD_Client_ID).append(",").append(AD_Org_ID).append(",'Y',");
				sql.append("SysDate,").append(CreatedBy).append(",SysDate,").append(CreatedBy).append(",");
				//  C_BankStatement_ID,DateAcct,ValutaDate,
				sql.append(C_BankStatement_ID).append(",");
				sql.append(DB.TO_DATE(trxDate)).append(",").append(DB.TO_DATE(trxDate)).append(",");
				//  Line,Description,IsReversal,
				sql.append("(SELECT (NVL(Max(Line),0))+10 FROM C_BankStatementLine WHERE C_BankStatement_ID=").append(C_BankStatement_ID).append("),");
				sql.append("NULL,'N',");
				//  C_Payment_ID,TrxAmt,
				sql.append(C_Payment_ID).append(",").append(TrxAmt).append(",");
				sql.append("NULL,0,0,");
				//  C_Currency_ID,StmtAmt
				sql.append(C_Currency_ID).append(",").append(StmtAmt);
				sql.append(")");
				//
				int no = DB.executeUpdate(sql.toString());
				if (no != 1)
					Log.error("VCreateFromStatement.save - Line created #" + no);
			}   //   if selected
		}   //  for all rows
		return true;
	}   //  save

}   //  VCreateFromStatement

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -