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

📄 doc.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			m_fact[index].balanceAccounting();

		return DocVO.STATUS_Posted;
	}   //  postLogic

	/**
	 *  Post Commit.
	 *  Save Facts & Document
	 *  @param status status
	 *  @return Posting Status
	 */
	private final String postCommit (String status)
	{
		log.info("postCommit Sta=" + status + " DT=" + p_vo.DocumentType + " ID=" +  p_vo.Record_ID);
		p_vo.Status = status;

		Connection con = getConnection();
		try
		{
		//  *** Transaction Start       ***
			//  Commit Facts
			if (status.equals(DocVO.STATUS_Posted))
			{
				for (int i = 0; i < m_fact.length; i++)
				{
					if (m_fact[i] != null && m_fact[i].save(con))
						;
					else
					{
						con.rollback();
						unlock();
						return p_vo.STATUS_Error;
					}
				}
			}
			//  Commit Doc
			if (!save(con))     //  contains unlock
			{
				con.rollback();
				unlock();
				return p_vo.STATUS_Error;
			}
			con.commit();
		//  *** Transaction End         ***
		}
		catch (Exception e)
		{
			log.error("postCommit", e);
			status = p_vo.STATUS_Error;
			try
			{
				con.rollback();
			}
			catch (SQLException e2)
			{
			}
			unlock();
		}
		return status;
	}   //  postCommit

	/**
	 *  Unlock Document
	 */
	private void unlock()
	{
		StringBuffer sql = new StringBuffer ("UPDATE ");
		sql.append(getTableName()).append( " SET Processing='N' WHERE ")
			.append(getTableName()).append("_ID=").append(p_vo.Record_ID);
		DB.executeUpdate(sql.toString());
	}   //  unlock

	/*************************************************************************/
	//  General Document Methods

	/**
	 *	Load Generic Document Information and then document specific info.
	 *
	 *  @param  rs  document to load from current record (SELECT * FROM documentTable)
	 * 	@param force if true delete existing accounting
	 * 	@return true if document loaded correctly
	 */
	private boolean loadDocument (ResultSet rs, boolean force)
	{
		log.debug("loadDocument - " + getTableName());

		p_vo = new DocVO (getAD_Table_ID());
		p_vo.Status = DocVO.STATUS_Error;
		String Name = null;

		try
		{
			String key = getTableName() + "_ID";
			ResultSetMetaData rsmd = rs.getMetaData();
			for (int i = 1; i <= rsmd.getColumnCount(); i++)
			{
				String col = rsmd.getColumnName(i);
				if (col.equalsIgnoreCase(key))
					p_vo.Record_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("AD_Client_ID"))
					p_vo.AD_Client_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("AD_Org_ID"))
					p_vo.AD_Org_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("C_BPartner_ID"))
					p_vo.C_BPartner_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("M_Product_ID"))
					p_vo.M_Product_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("AD_OrgTrx_ID"))
					p_vo.AD_OrgTrx_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("C_SalesRegion_ID"))
					p_vo.C_SalesRegion_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("C_Project_ID"))
					p_vo.C_Project_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("C_Campaign_ID"))
					p_vo.C_Campaign_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("C_Activity_ID"))
					p_vo.C_Activity_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("C_LocFrom_ID"))
					p_vo.C_LocFrom_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("C_LocTo_ID"))
					p_vo.C_LocTo_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("User1_ID"))
					p_vo.User1_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("User2_ID"))
					p_vo.User2_ID = rs.getInt(i);
				//
				else if (col.equalsIgnoreCase("DocumentNo"))
					p_vo.DocumentNo = rs.getString(i);
				else if (col.equalsIgnoreCase("Name"))
					Name = rs.getString(i);
				else if (col.equalsIgnoreCase("DateAcct"))
					p_vo.DateAcct = rs.getTimestamp(i);
				else if (col.equalsIgnoreCase("DateDoc"))
					p_vo.DateDoc = rs.getTimestamp(i);
				else if (col.equalsIgnoreCase("C_Period_ID"))
					p_vo.C_Period_ID = rs.getInt(i);
				//
				else if (col.equalsIgnoreCase("C_Currency_ID"))
					p_vo.C_Currency_ID = rs.getInt(i);
				//
				else if (col.equalsIgnoreCase("C_DocType_ID"))
					p_vo.C_DocType_ID = rs.getInt(i);
				//  Special Document Fields
				else if (col.equalsIgnoreCase("C_Charge_ID"))
					p_vo.C_Charge_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("ChargeAmt"))
					p_vo.ChargeAmt = rs.getBigDecimal(i);
				else if (col.equalsIgnoreCase("C_BankAccount_ID"))
					p_vo.C_BankAccount_ID = rs.getInt(i);
				else if (col.equalsIgnoreCase("M_Warehouse_ID"))
					p_vo.M_Warehouse_ID = rs.getInt(i);
				//
				else if (col.equalsIgnoreCase("Posted"))
					p_vo.Posted = "Y".equals(rs.getString(i));
			}   //  for all columns
			p_vo.Status = DocVO.STATUS_NotPosted;
		}
		catch (SQLException e)
		{
			log.error("loadDocument", e);
		}

		//  Call Document Specific Info
		if (!loadDocumentDetails (rs))
			loadDocumentType();

		//  Fill Acct/Trx Date
		if (p_vo.DateAcct == null & p_vo.DateDoc != null)
			p_vo.DateAcct = p_vo.DateDoc;
		else if (p_vo.DateDoc == null & p_vo.DateAcct != null)
			p_vo.DateDoc = p_vo.DateAcct;
		//  DocumentNo (or Name)
		if (p_vo.DocumentNo == null || p_vo.DocumentNo.length() == 0)
			p_vo.DocumentNo = Name;
		if (p_vo.DocumentNo == null || p_vo.DocumentNo.length() == 0)
			p_vo.DocumentNo = "";

		//  Check Mandatory Info
		String error = "";
		if (p_vo.AD_Table_ID == 0)
			error += " AD_Table_ID";
		if (p_vo.Record_ID == 0)
			error += " Record_ID";
		if (p_vo.AD_Client_ID == 0)
			error += " AD_Client_ID";
		if (p_vo.AD_Org_ID == 0)
			error += " AD_Org_ID";
		if (p_vo.C_Currency_ID == 0)
			error += " C_Currency_ID";
		if (p_vo.DateAcct == null)
			error += " DateAcct";
		if (p_vo.DateDoc == null)
			error += " DateDoc";
		if (error.length() > 0)
		{
			log.error("loadDocument - " + toString() + " - Mandatory info missing: " + error);
			return false;
		}

		//  Delete existing Accounting
		if (force)
		{
			if (p_vo.Posted && !isPeriodOpen())	//	already posted - don't delete if period closed
			{
				log.error("loadDocument - " + toString() + " - Period Closed for already posed document");
				return false;
			}

			//	delete it
			StringBuffer sql = new StringBuffer ("DELETE Fact_Acct "
				+ "WHERE AD_Table_ID=");
			sql.append(getAD_Table_ID()).append(" AND Record_ID=").append(p_vo.Record_ID);
			int no = DB.executeUpdate(sql.toString());
			log.info("post - deleted=" + no);
		}
		else if (p_vo.Posted)
		{
			log.error("loadDocument - " + toString() + " - Document already posted");
			return false;
		}
		return true;
	}	//	loadDocument

	/**
	 *  Load Document Type and GL Info
	 */
	protected void loadDocumentType()
	{
		//  No Document Type defined
		if (p_vo.DocumentType == null && p_vo.C_DocType_ID != 0)
		{
			String sql = "SELECT DocBaseType, GL_Category_ID FROM C_DocType WHERE C_DocType_ID=?";
			try
			{
				PreparedStatement pstmt = DB.prepareStatement(sql);
				pstmt.setInt(1, p_vo.C_DocType_ID);
				ResultSet rsDT = pstmt.executeQuery();
				if (rsDT.next())
				{
					p_vo.DocumentType = rsDT.getString(1);
					p_vo.GL_Category_ID = rsDT.getInt(2);
				}
				rsDT.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				log.error("loadDocumentType-1", e);
			}
		}

		//  We have a document Type, but no GL info - search for DocType
		if (p_vo.GL_Category_ID == 0)
		{
			String sql = "SELECT GL_Category_ID FROM C_DocType "
				+ "WHERE AD_Client_ID=? AND DocBaseType=?";
			try
			{
				PreparedStatement pstmt = DB.prepareStatement(sql);
				pstmt.setInt(1, p_vo.AD_Client_ID);
				pstmt.setString(2, p_vo.DocumentType);
				ResultSet rsDT = pstmt.executeQuery();
				if (rsDT.next())
					p_vo.GL_Category_ID = rsDT.getInt(1);
				rsDT.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				log.error("loadDocumentType-2", e);
			}
		}

		if (p_vo.DocumentType == null)
			log.error("loadDocumentType - No DocType for GL Info - " + toString());

		//  Still no GL_Category - get Default GL Category
		if (p_vo.GL_Category_ID == 0)
		{
			String sql = "SELECT GL_Category_ID FROM GL_Category "
				+ "WHERE AD_Client_ID=? "
				+ "ORDER BY IsDefault DESC";
			try
			{
				PreparedStatement pstmt = DB.prepareStatement(sql);
				pstmt.setInt(1, p_vo.AD_Client_ID);
				ResultSet rsDT = pstmt.executeQuery();
				if (rsDT.next())
					p_vo.GL_Category_ID = rsDT.getInt(1);
				rsDT.close();
				pstmt.close();
			}
			catch (SQLException e)
			{
				log.error("loadDocumentType-3", e);
			}
		}
		if (p_vo.GL_Category_ID == 0)
			log.error("loadDocumentType - No GL_Category - " + toString());

		//  Budget
		p_vo.GL_Budget_ID = 0;
	}   //  loadDocumentType

	/*************************************************************************/

	/**
	 *  Is the Source Document Balanced
	 *  @return true if (source) baanced
	 */
	public boolean isBalanced()
	{
		//  Multi-Currency documents are source balanced by definition
		if (p_vo.MultiCurrency)
			return true;
		//
		boolean retValue = getBalance().compareTo(Env.ZERO) == 0;
		if (retValue)
			log.debug("isBalanced - " + p_vo.toString());
		else
			log.warn("isBalanced NO - " + p_vo.toString());
		return retValue;
	}	//	isBalanced

	/**
	 *  Is Document convertible to currency and Conversion Type
	 *  @param acctSchema accounting schema
	 *  @return true, if vonvertable to accounting currency
	 */
	public boolean isConvertible (AcctSchema acctSchema)
	{
		//  No Currency in document
		if (p_vo.C_Currency_ID == NO_CURRENCY)
		{
			log.debug ("isConvertible (none) - " + p_vo.toString());
			return true;
		}
		//  Get All Currencies
		HashSet set = new HashSet();
		set.add(new Integer(p_vo.C_Currency_ID));
		for (int i = 0; p_lines != null && i < p_lines.length; i++)
		{
			int currency = p_lines[i].getC_Currency_ID();
			set.add(new Integer(currency));
		}

		//  just one and the same
		if (set.size() == 1 && acctSchema.getC_Currency_ID() == p_vo.C_Currency_ID)
		{
			log.debug ("isConvertible (same) Cur=" + p_vo.C_Currency_ID + " - " + p_vo.toString());
			return true;
		}

		boolean convertible = true;
		Iterator it = set.iterator();
		while (it.hasNext() && convertible)
		{
			int currency = ((Integer)it.next()).intValue();
			if (currency != acctSchema.getC_Currency_ID())
			{
				BigDecimal amt = DB.getConvertedAmt(new BigDecimal(1.0), currency, acctSchema.getC_Currency_ID(),
					p_vo.DateAcct, acctSchema.getCurrencyRateType(), p_vo.AD_Client_ID, p_vo.AD_Org_ID);
				if (amt == null)
				{
					convertible = false;
					log.warn ("isConvertible NOT from " + currency +  " - " + p_vo.toString());
				}
				else
					log.debug ("isConvertible from " + currency);
			}
		}

		log.debug ("isConvertible=" + convertible + ", AcctSchemaCur=" + acctSchema.getC_Currency_ID() + " - " + p_vo.toString());
		return convertible;
	}	//	isConvertible


	/**
	 *  Calculate Period ID.
	 *  Set to -1 if no period open, 0 if no period control
	 */
	public void setC_Period_ID()
	{
		if (p_vo.C_Period_ID != DocVO.PERIOD_UNDEFINED)
			return;
		//
		try
		{

⌨️ 快捷键说明

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