package core.stlmes.server.sale.service.balance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import core.stlmes.server.sale.po.SlmBusinessOpeInfoBean;

import java.util.*;

import com.steering.comm.tools.EntityTool;
import com.steering.pss.glbusiness.SlmGlAccrued;
import com.steering.pss.sale.order.model.CrmSlmOrder;
import com.steering.pss.sale.order.model.SlmOrderHeadEntity;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import core.stlmes.server.sale.dao.DaoFactory;
import core.stlmes.server.sale.dao.SlmBalanceInfoDao;
import core.stlmes.server.sale.dao.SlmBalanceInfoOrgDao;
import core.stlmes.server.sale.dao.SlmBalanceTransMoneyDao;
import core.stlmes.server.sale.dao.YdmZcBillMDao;
import core.stlmes.server.sale.dbhelp.DbTrans;
import core.stlmes.server.sale.po.SlmBalanceDetailAllBean;
import core.stlmes.server.sale.po.SlmBalanceInfoBean;
import core.stlmes.server.sale.po.SlmOrderLineTgBean;
import core.stlmes.server.sale.po.SlmOrderPriceListBean;
import core.stlmes.server.sale.po.YdmZcBillMBean;
import core.stlmes.server.sale.service.baseservice.BaseBll;
import core.stlmes.server.sale.po.SlmBalanceTransMoneyBean;
import core.stlmes.server.sale.dao.*;
import core.stlmes.server.sale.po.*;

public class SlmBalanceTickets extends BaseBll
{

	public SlmBalanceTickets()
	{

	}

	public void acceptBalaneDetailSignal(String askPlanId, String billTp,
			java.sql.Connection conn, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();

		DaoFactory fact = new DaoFactory();
		core.stlmes.server.sale.dao.SlmBalanceDetailAllDao slmBalanceDetailAllDao = null;

		int index = 0;

		core.stlmes.server.sale.dao.YdmZcBillMDao ydmZcBillMDao = null;
		ydmZcBillMDao = new core.stlmes.server.sale.dao.YdmZcBillMDao();

		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();

		YdmZcLoadvehicleMatDao ydmZcLoadvehicleMatDao = new YdmZcLoadvehicleMatDao();
		slmBalanceDetailAllDao = new SlmBalanceDetailAllDao();
		String realTicketType = "1";

		List<SlmBalanceDetailAllBean> listInsert = ydmZcLoadvehicleMatDao
				.getSlmBalanceDetailFromLoadVehicleByBillNo(askPlanId,conn);
		if (listInsert == null)
		{
			throw new java.lang.Exception(this.AskPlanIdSendException);
		}
		if (fact.getSlmBalanceDetailAllDao().isAskPlanIdExists(askPlanId))
		{
			throw new java.lang.Exception(this.AskPlanIdHadSend);
		}

		String billType = billTp;

		for (SlmBalanceDetailAllBean bean : listInsert)
		{
			if(bean.getSteelcodekey()==null)
				bean.setSteelcodekey("/");
			cro = slmBalanceDetailAllDao.insert(conn, bean);

		}
		// RealTickets
		if (billType.equals(realTicketType))
		{
			ydmZcBillMDao.updateBillStatusToAccept(conn, askPlanId);
			ydmZcLoadvehicleMatDao.updateYdmZcLoadVehicleMatToAccept(conn,
					askPlanId);
		}
		// VirtualTickets
		else
		{
			ydmZcBillMDao.updateVirtualBillStatusToAccept(conn, askPlanId);
		}

		List<SlmBalanceInfoBean> bean = slmBalanceDetailAllDao
				.getSelBalanceInfo(conn, askPlanId);

		for (SlmBalanceInfoBean slm : bean)
		{
			if(slm.getSteelcodekey()==null)
				slm.setSteelcodekey("/");
			slmBalanceInfoDao.insert(conn, slm);

		}

		slmBalanceDetailAllDao.updateDetailStatusToAccept(conn, askPlanId);

		core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean1 = new SlmBusinessOpeInfoBean(
				this.AcceptAskPlanIdSuccess, "", "", askPlanId, "", person,
				askPlanId + "SUCCESS");
		super.logBusinessInfo(bean1);

	}

	public CoreReturnObject acceptBalaneDetailBySignTicket(String askPlanId,
			java.util.ArrayList list, String billType, String billck,
			String outStockCk, String transCk, String person)
	{

		CoreReturnObject cro = new CoreReturnObject();
		java.sql.Connection conn = null;
		SlmBalanceTransMgt slmBalanceTransMgt = null;
		YdmZcBillMDao ydmZcBillMDao = null;
		slmBalanceTransMgt = new SlmBalanceTransMgt();
		ydmZcBillMDao = new YdmZcBillMDao();
		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
		String checkSignal = "1";

		String listnumber = billType + askPlanId + billck + outStockCk
				+ transCk;

		conn = this.getDao(super.daoName).getConnection();
		try
		{
			conn.setAutoCommit(false);

			if (checkSignal.equals(billck))
			{
				acceptBalaneDetailSignal(askPlanId, billType, conn, person);
				ydmZcBillMDao.updateBillTicketStatusToAccept(askPlanId, person,
						conn);

			}

			if (checkSignal.equals(outStockCk))
			{

				slmBalanceTransMgt.SaveOutStockMnyByAlineTicket(conn, list,
						person);
				ydmZcBillMDao.updateRealOutStockTicketStatusToAccept(askPlanId,
						person, conn);

			}

			if (checkSignal.equals(transCk))
			{

				ydmZcBillMDao.updateRealTransmnyTicketStatusToAccept(askPlanId,
						person, conn);

			}

			if (checkSignal.equals(billck))
			{
				slmBalanceInfoDao.updateTicketListnumber(conn, askPlanId,
						listnumber);

			}
			if (checkSignal.equals(outStockCk))
			{
				slmBalanceInfoDao.updateOutStockmneyListNumber(conn, askPlanId,
						listnumber);

			}
			if (checkSignal.equals(transCk))
			{
				slmBalanceInfoDao.updateTransmneyListNumber(conn, askPlanId,
						listnumber);

			}

			conn.commit();

		}
		catch (java.sql.SQLException ex)
		{
			try
			{
				conn.rollback();
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			super.setCroErrMsg(cro, ex.getMessage());

		}
		catch (java.lang.Exception ex)
		{
			try
			{
				conn.rollback();
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			super.setCroErrMsg(cro, ex.getMessage());

		}

		finally
		{
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return cro;

	}

	public CoreReturnObject acceptRollBackAndCreditRecords(String askPlanId,
			java.util.ArrayList list)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceDetailAllDao slmBalanceDetailAllDao = null;
		SlmBalanceInfoDao slmBalanceInfoDao = null;
		core.stlmes.server.sale.dbhelp.DbTrans trans = null;
		try
		{
			slmBalanceDetailAllDao = new SlmBalanceDetailAllDao();
			slmBalanceInfoDao = new SlmBalanceInfoDao();
			trans = new DbTrans();
			trans.beginTransaction();
			slmBalanceDetailAllDao.setTrans(trans);
			slmBalanceInfoDao.setTrans(trans);

			List<SlmBalanceDetailAllBean> listd = null;

			String pk = "";
			String[] temp = null;

			String ordPk = "";
			String ordLnPk = "";
			String ordDlyPk = "";
			String stoveNO = "";
			String batchNo = "";
			String BATCH_GROUD_NO = "";
			String OUTSTOCK_SQE = "";
			String act_wgt = "";
			String sendNum = "";

			SlmBalanceDetailAllBean tempd = null;
			String newAskPlanId = askPlanId + "TH";
			List<SlmBalanceDetailAllBean> listSl = new java.util.ArrayList<SlmBalanceDetailAllBean>();
			for (Object s : list)
			{
				listd = slmBalanceDetailAllDao
						.getSlmBalanceDetailAllByAskPlanId(askPlanId);
				if (listd == null) return cro;

				temp = ( String[])s;
				ordPk = temp[0];
				ordLnPk = temp[1];
				ordDlyPk = temp[2];
				stoveNO = temp[3];
				batchNo = temp[4];
				BATCH_GROUD_NO = temp[5];
				OUTSTOCK_SQE = temp[6];
				act_wgt = temp[7];
				sendNum = temp[8];
				tempd = new SlmBalanceDetailAllBean();
				for (SlmBalanceDetailAllBean bean : listd)
				{
					pk = bean.getOrd_pk() + bean.getOrd_ln_pk()
							+ bean.getOrd_ln_dly_pk();
					if ( (ordPk + ordLnPk + ordDlyPk ).equals(pk))
					{
						tempd = bean;
						break;

					}
				}
				tempd.setBatch_no(stoveNO + "/" + batchNo);
				tempd.setPrd_id(OUTSTOCK_SQE);
				tempd.setStatus_cd("0");
				tempd.setAskplan_id(newAskPlanId);
				tempd.setInv_no(BATCH_GROUD_NO);
				tempd.setPrd_qty(-1.00);
				tempd.setAct_wgt(core.stlmes.server.sale.util.Convert
						.getDouble(act_wgt));
				tempd.setSend_num(core.stlmes.server.sale.util.Convert
						.getDouble(sendNum));

				listSl.add(tempd);

			}

			if (listSl.size() == 0) return cro;

			for (SlmBalanceDetailAllBean bean : listSl)
			{

				List<SlmBalanceDetailAllBean> listtt = slmBalanceDetailAllDao
						.getSlmBalanceDetailAllByProdId(bean.getPrd_id());
				if (listtt != null && listtt.size() > 0)
				{
					super.setCroErrMsg(cro, bean.getPrd_id() + "Repeat Data");
					trans.rollback();
					return cro;
				}
				slmBalanceDetailAllDao.insert(bean);

			}

			List<SlmBalanceInfoBean> bean = slmBalanceDetailAllDao
					.getSelBalanceInfo(newAskPlanId);

			for (SlmBalanceInfoBean slm : bean)
			{
				slm.setListnumber(newAskPlanId);
				slm.setTicketstatus("1");
				slm.setTicketperson("");
				slm.setTicketdate(new Date());
				slmBalanceInfoDao.insert(slm);

			}

			slmBalanceDetailAllDao.updateDetailStatusToAccept(newAskPlanId);
			trans.commit();

			core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean1 = new SlmBusinessOpeInfoBean(
					AcceptRollBackData, "", "", newAskPlanId, "", "System",
					newAskPlanId + "SUCCESS");
			super.logBusinessInfo(bean1);
			return cro;

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;

		}
		finally
		{
			trans.commit();

		}

	}

	public CoreReturnObject rollBackTicketsAndOutStockMnyAndTransMnyByAksplid(
			String askPlanId, String billType, String billck,
			String outStockCk, String transCk, String person)
	{
		java.util.ArrayList list = new java.util.ArrayList();
		java.util.ArrayList listTemp = null;
		CoreReturnObject cro = new CoreReturnObject();
		YdmZcBillMDao ydmZcBillMDao = new YdmZcBillMDao();
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		String checkFlag = "1";
		String statusCd_InvoiceStats = "2";
		try
		{
			if (checkFlag.equals(billck))
			{
				listTemp = this.rollBackBillTicketByAskPlanId(askPlanId,
						billType, person);
				String rollT = ydmZcBillMDao.updateBillTicketStatusToNoAccept(
						askPlanId, person);
				list.addAll(listTemp);
				list.add(rollT);

			}

			if (checkFlag.equals(outStockCk))
			{
				List<SlmBalanceTransMoneyBean> lisot = slmBalanceTransMoneyDao
						.getSlmBalanceTransMoneyByAskPlanId(askPlanId);
				if (lisot != null && lisot.size() > 0)
				{
					for (SlmBalanceTransMoneyBean s : lisot)
					{
						if (statusCd_InvoiceStats.equals(s.getStatus_cd())
								|| statusCd_InvoiceStats.equals(s
										.getStatus_cd_org())
								&& OutStockMoneyName.equals(s.getMoney_type()))
						{
							super.setCroErrMsg(cro,
									OutStockMoneyNameInvoiceDMsg);
							return cro;

						}

					}
				}
				String sql = new SlmBalanceTransMoneyDao()
						.deleteOutStockMnyByBillNoSql(askPlanId);
				String rollTt = ydmZcBillMDao
						.updateRealOutStockTicketStatusToNoAcceptSql(askPlanId,
								person);
				list.add(sql);
				list.add(rollTt);

			}
			if (checkFlag.equals(transCk))
			{

				String sqlT = new SlmBalanceTransMoneyDao()
						.deleteTranskMnyByBillNoSql(askPlanId);

				String rollTt = ydmZcBillMDao
						.updateRealTransmnyTicketStatusToNoAcceptSql(askPlanId,
								person);
				String sqlTS = ydmZcBillMDao
						.updateRealTransmnyTicketStatusToNoAline(askPlanId, "");

				list.add(sqlT);

				list.add(rollTt);
				list.add(sqlTS);
			}
			if (list.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(list);
				if (checkFlag.equals(billck))
				{

					core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean = new SlmBusinessOpeInfoBean(
							RollBackAskPlanIdName, "", "", askPlanId, "",
							person, askPlanId);
					super.logBusinessInfo(bean);
				}
			}

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;

		}

		return cro;

	}

	public CoreReturnObject rollBackAskPlanId(String askPlanId, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();
		java.util.ArrayList sqlList = new java.util.ArrayList();
		try
		{

			if (!this.isAskPlanIdCanBeRollBack(askPlanId))
			{
				super.setCroErrMsg(cro, AskPlanIdNotAllowRollbackMsg);
				return cro;

			}
			core.stlmes.server.sale.dao.YdmZcBillMDao ydmZcBillMDao = null;
			ydmZcBillMDao = new core.stlmes.server.sale.dao.YdmZcBillMDao();

			String billType = "1";

			List<SlmBalanceInfoBean> list = (new SlmBalanceInfoDao() )
					.getSlmBalanceInfoByAskPlanId(askPlanId);
			if (list == null || list.size() == 0) return cro;

			if (TruthName.equals(list.get(0).getIspreclose()))
			{
				billType = "2";

			}

			String sqlDeleteBal = "";
			String sqlDeleteBalDetail = "";
			String sqlupdatests = "";
			String sqlupdatestsmat = "";
			String sqlupdatevirtsmat = "";

			sqlDeleteBal = fact.getSlmBalanceInfoDao().deleteSql(askPlanId);
			sqlDeleteBalDetail = fact.getSlmBalanceDetailAllDao()
					.deleteByAskPlanIdSql(askPlanId);
			sqlList.add(sqlDeleteBal);
			sqlList.add(sqlDeleteBalDetail);

			if (billType.equals("1"))
			{
				sqlupdatests = fact.getYdmZcBillMDao()
						.updateAcceptBillStatusToNoAcceptSql(askPlanId);
				sqlupdatestsmat = fact.getYdmZcLoadvehicleMatDao()
						.updateYdmZcLoadVehicleMatToUnAcceptSql(askPlanId);
				sqlList.add(sqlupdatests);
				sqlList.add(sqlupdatestsmat);
			}
			else
			{
				sqlupdatevirtsmat = fact.getYdmZcBillMDao()
						.updateVirtualAcceptBillStatusToNoAcceptSql(askPlanId);
				sqlList.add(sqlupdatevirtsmat);

			}

			if (sqlList.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(sqlList);
			}

			core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean = new SlmBusinessOpeInfoBean(
					RollBackAskPlanIdName, "", "", askPlanId, "", person,
					askPlanId);
			super.logBusinessInfo(bean);

			try
			{
				(new SlmBalanceTransMgt() ).deleteOutStockMnyByAksPlanId(
						askPlanId, person);
			}
			catch (java.lang.Exception ex)
			{
			}

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;

		}
		finally
		{
			fact.cleantTrans();
			trans = null;
		}
		return cro;

	}

	public java.util.ArrayList rollBackBillTicketByAskPlanId(String askPlanId,
			String billTp, String person) throws java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();
		java.util.ArrayList sqlList = new java.util.ArrayList();

		if (!this.isAskPlanIdCanBeRollBackWithTicketStatus(askPlanId))
		{
			throw new Exception(TicketNotAllowedCancel);

		}
		core.stlmes.server.sale.dao.YdmZcBillMDao ydmZcBillMDao = null;
		ydmZcBillMDao = new core.stlmes.server.sale.dao.YdmZcBillMDao();

		String billType = billTp;

		String sqlDeleteBal = "";
		String sqlDeleteBalDetail = "";
		String sqlupdatests = "";
		String sqlupdatestsmat = "";
		String sqlupdatevirtsmat = "";

		sqlDeleteBal = fact.getSlmBalanceInfoDao().deleteSql(askPlanId);
		sqlDeleteBalDetail = fact.getSlmBalanceDetailAllDao()
				.deleteByAskPlanIdSql(askPlanId);
		sqlList.add(sqlDeleteBal);
		sqlList.add(sqlDeleteBalDetail);

		if (billType.equals("1"))
		{
			sqlupdatests = fact.getYdmZcBillMDao()
					.updateAcceptBillStatusToNoAcceptSql(askPlanId);
			sqlupdatestsmat = fact.getYdmZcLoadvehicleMatDao()
					.updateYdmZcLoadVehicleMatToUnAcceptSql(askPlanId);

			sqlList.add(sqlupdatests);
			sqlList.add(sqlupdatestsmat);
		}
		else
		{
			sqlupdatevirtsmat = fact.getYdmZcBillMDao()
					.updateVirtualAcceptBillStatusToNoAcceptSql(askPlanId);
			sqlList.add(sqlupdatevirtsmat);

		}

		return sqlList;

	}

	public CoreReturnObject updatePrice(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String price,
			String adjustreason, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		try
		{
			SlmBalanceInfoBean bean = slmBalanceInfoDao
					.getSlmBalanceInfoByPrimaryKey(orderNo, orderSeq,
							orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
							steelCodeKey);
			if (bean == null) return cro;
			if (bean.getBalancestatus().equals(BalanceStatus_Invoice))
			{

				return cro;
			}
			slmBalanceInfoDao.updateBalancePrice(orderNo, orderSeq,
					orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
					steelCodeKey, price, adjustreason, person);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

		}
		return cro;

	}

	public CoreReturnObject inputPrice(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String price, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		try
		{
			SlmBalanceInfoBean bean = slmBalanceInfoDao
					.getSlmBalanceInfoByPrimaryKey(orderNo, orderSeq,
							orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
							steelCodeKey);
			if (bean == null) return cro;
			if (bean.getBalancestatus().equals(BalanceStatus_Invoice))
			{
				return cro;
			}

			slmBalanceInfoDao.insertBalancePrice(orderNo, orderSeq,
					orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
					steelCodeKey, price, person);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

		}
		return cro;

	}

	public CoreReturnObject affrim(ArrayList list, String person)
	{
		if (true)
		{
			return affrimTicketAndTrans(list, person);
		}

		CoreReturnObject cro = new CoreReturnObject();
		if (list == null) return cro;
		if (list.size() == 0) return cro;

		String[] temp = null;
		String orderNo = "";
		String orderSeq = "";
		String orderDlvSeq = "";
		String askPlanId = "";
		String askPlanIdSeq = "";
		String spetKey = "";
		String steelCodeKey = "";
		String balancestatus = "";
		String ordPk = "";
		String ordLnPk = "";
		SlmOrderPriceDao slmOrderPriceDao = new SlmOrderPriceDao();

		SlmOrderHeadTgDao slmOrderHeadTgDao = new SlmOrderHeadTgDao();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		List arrOrdPklist = new ArrayList();

		java.util.Hashtable hsOrderPrice = new Hashtable();
		String balPrice = "0";

		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();

		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);
			core.stlmes.server.sale.dao.SlmBalanceInfoDao slmBalanceInfoDao = fact
					.getSlmBalanceInfoDao();
			for (Object s : list)
			{
				temp = ( String[])s;
				orderNo = temp[0];
				orderSeq = temp[1];
				orderDlvSeq = temp[2];
				askPlanId = temp[3];
				askPlanIdSeq = temp[4];
				spetKey = temp[5];
				steelCodeKey = temp[6];
				balancestatus = temp[9];

				if (balancestatus.equals(BalanceStatus_Invoice)) continue;

				slmBalanceInfoDao.updateBalanceStatusToAffrim(orderNo,
						orderSeq, orderDlvSeq, askPlanId, askPlanIdSeq,
						spetKey, steelCodeKey, person);
			}

			trans.commit();

		}

		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();

		}
		finally
		{
			fact.cleantTrans();
			trans = null;
		}

		return cro;

	}

	public CoreReturnObject affrimTicketAndTrans(ArrayList list, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (list == null) return cro;
		if (list.size() == 0) return cro;
		String[] temp = null;
		String orderNo = "";
		String orderSeq = "";
		String orderDlvSeq = "";
		String askPlanId = "";
		String askPlanIdSeq = "";
		String spetKey = "";
		String steelCodeKey = "";
		String balancestatus = "";
		String ordPk = "";
		String ordLnPk = "";
		String moneyType = "";
		SlmOrderPriceDao slmOrderPriceDao = new SlmOrderPriceDao();

		SlmOrderHeadTgDao slmOrderHeadTgDao = new SlmOrderHeadTgDao();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		List arrOrdPklist = new ArrayList();

		java.util.Hashtable hsOrderPrice = new Hashtable();
		java.util.ArrayList arrsBuyercodeAndForeCode = new java.util.ArrayList();
		java.util.Hashtable<String, SlmOrderLineTgBean> listOrdLnAndOrdbean = new java.util.Hashtable<String, SlmOrderLineTgBean>();
		java.util.ArrayList listOrdPk = new java.util.ArrayList();
		String balPrice = "0";

		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();

		java.util.ArrayList listSql = new java.util.ArrayList();

		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);
			core.stlmes.server.sale.dao.SlmBalanceInfoDao slmBalanceInfoDao = fact
					.getSlmBalanceInfoDao();
			slmOrderHeadTgDao.setTrans(trans);
			slmOrderLineTgDao.setTrans(trans);
			for (Object s : list)
			{
				temp = ( String[])s;
				orderNo = temp[0];
				orderSeq = temp[1];
				orderDlvSeq = temp[2];
				askPlanId = temp[3];
				askPlanIdSeq = temp[4];
				spetKey = temp[5];
				steelCodeKey = temp[6];
				balancestatus = temp[9];
				moneyType = temp[19];
				if (TicketTypeGoods.equals(moneyType))
				{
					SlmBalanceInfoBean t = slmBalanceInfoDao
							.getSlmBalanceInfoByPrimaryKey(orderNo, orderSeq,
									orderDlvSeq, askPlanId, askPlanIdSeq,
									spetKey, steelCodeKey);

					// if (balancestatus.equals("3")) continue;
					if (!BalanceStatus_Init.equals(t.getBalancestatus())) continue;

					if (!listOrdPk.contains(t.getOrd_pk().toString()))
					{
						List<SlmOrderLineTgBean> listorder = slmOrderLineTgDao
								.getSlmOrderLineTgByOrdPk(t.getOrd_pk()
										.toString());
						if (listorder == null || listorder.size() == 0)
						{

						}
						else
						{
							for (SlmOrderLineTgBean bean : listorder)
							{
								if (!listOrdLnAndOrdbean.containsKey(bean
										.getOrd_ln_pk()))
								{
									listOrdLnAndOrdbean.put(
											bean.getOrd_ln_pk(), bean);
								}

							}

						}

					}

					SlmOrderLineTgBean orderLIn = null;
					if (listOrdLnAndOrdbean.containsKey(t.getOrd_ln_pk()))
					{
						orderLIn = ( SlmOrderLineTgBean)listOrdLnAndOrdbean
								.get(t.getOrd_ln_pk());
					}
					
					SlmOrderHeadTgBean orderHead = slmOrderHeadTgDao.getSlmOrderHeadTgByOrdPk(t.getOrd_pk().toString());
					
					String flag="0";
					if(orderHead.getSupply_unit().equals("120504")){
						if(!orderHead.getFst_drawee_unit().equals("120607")){
							flag="1";
						}
						
					}
					if(!flag.equals("1")){
						if ("0".equals(orderLIn.getBalnce_price_sts()))
						{
							super.setCroErrMsg(cro, orderNo + "/" + orderSeq
									+ CannotSetCardsWithNoValidPrice);
							return cro;
						}
					}       
					
					String sql = slmBalanceInfoDao
							.updateBalanceStatusToAffrimStr(orderNo, orderSeq,
									orderDlvSeq, askPlanId, askPlanIdSeq,
									spetKey, steelCodeKey, person);
					listSql.add(sql);
					String sqlTrans = slmBalanceTransMoneyDao
							.affrimAskPlanIdFroTransSql(askPlanId, person);
					listSql.add(sqlTrans);

				}
				else
				{

					String sqlTrans = slmBalanceTransMoneyDao
							.affrimAskPlanIdFroTransSql(askPlanId, person);
					listSql.add(sqlTrans);
				}

			}

			if (listSql.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(listSql);
			}

			trans.commit();

		}

		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();

		}
		finally
		{
			fact.cleantTrans();
			trans.commit();
			trans = null;
		}

		return cro;

	}

	public CoreReturnObject setBalPrice(ArrayList list, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();

		cro = setBalPricet(list, person);
		return cro;

	}

	public CoreReturnObject setBalPricet(ArrayList list, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (list == null) return cro;
		if (list.size() == 0) return cro;
		String[] temp = null;
		String orderNo = "";
		String orderSeq = "";
		String orderDlvSeq = "";
		String askPlanId = "";
		String askPlanIdSeq = "";
		String spetKey = "";
		String steelCodeKey = "";
		String balancestatus = "";
		String ordPk = "";
		String ordLnPk = "";
		SlmOrderPriceDao slmOrderPriceDao = new SlmOrderPriceDao();
		SlmOrderHeadTgDao slmOrderHeadTgDao = new SlmOrderHeadTgDao();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		List arrOrdPklist = new ArrayList();
		ArrayList sqlList = new ArrayList();

		java.util.Hashtable hsOrderPrice = new Hashtable();
		String balPrice = "0";

		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();
		String conTax = "0";

		core.stlmes.server.sale.dao.SlmBalanceInfoDao slmBalanceInfoDao = fact
				.getSlmBalanceInfoDao();
		try
		{
			trans.beginTransaction();
			slmOrderLineTgDao.setTrans(trans);
			slmOrderHeadTgDao.setTrans(trans);
			for (Object s : list)
			{
				temp = ( String[])s;
				orderNo = temp[0];
				orderSeq = temp[1];
				orderDlvSeq = temp[2];
				askPlanId = temp[3];
				askPlanIdSeq = temp[4];
				spetKey = temp[5];
				steelCodeKey = temp[6];
				balancestatus = temp[9];

				try
				{

					ordPk = temp[10];
					ordLnPk = temp[11];
					SlmOrderHeadTgBean ordTg = slmOrderHeadTgDao
							.getSlmOrderHeadTgByOrdPk(ordPk);
					if (ordTg == null) conTax = "0";
					else
						conTax = ordTg.getContain_tax();
					if (!arrOrdPklist.contains(ordPk))
					{
						List<SlmOrderLineTgBean> listOrder = slmOrderLineTgDao
								.getSlmOrderLineTgByOrdPk(ordPk);

						if (listOrder == null)
						{
						}
						else
						{
							for (SlmOrderLineTgBean line : listOrder)
							{
								if ("1".equals(line.getBalnce_price_sts()))
								{
									if (!hsOrderPrice.containsKey(ordPk
											+ line.getOrd_ln_pk()))
									{
										hsOrderPrice.put(ordPk
												+ line.getOrd_ln_pk(), line);
									}
								}

							}
						}
						arrOrdPklist.add(ordPk);

					}

				}
				catch (java.lang.Exception ex)
				{

				}

				// if (balancestatus.equals("3")) continue;
				if ("2".equals(balancestatus)) continue;
				if ("3".equals(balancestatus)) continue;
				balPrice = "0";

				if (hsOrderPrice.containsKey(ordPk + ordLnPk))
				{
					SlmOrderLineTgBean bean = ( SlmOrderLineTgBean)hsOrderPrice
							.get(ordPk + ordLnPk);
					balPrice = bean.getBalnce_price().toString();

				}

				double corpPrice = 0.0;

				String ustr = slmBalanceInfoDao.insertBalancePriceStr(orderNo,
						orderSeq, orderDlvSeq, askPlanId, askPlanIdSeq,
						spetKey, steelCodeKey, balPrice, conTax, person);
				sqlList.add(ustr);

			}
		}
		catch (java.sql.SQLException ex)
		{
			trans.rollback();

		}
		finally
		{
			trans.commit();

		}

		cro = super.ExcuteNonQueryIgnoreTrans(sqlList);

		return cro;

	}

	public CoreReturnObject unaffrim(java.util.ArrayList list, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (list == null) return cro;
		if (list.size() == 0) return cro;
		String[] temp = null;
		String orderNo = "";
		String orderSeq = "";
		String orderDlvSeq = "";
		String askPlanId = "";
		String askPlanIdSeq = "";
		String spetKey = "";
		String steelCodeKey = "";
		String balancestatus = "";
		core.stlmes.server.sale.dao.DaoFactory fact = new DaoFactory();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new DbTrans();
		java.util.ArrayList listSql = new java.util.ArrayList();

		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);
			for (Object s : list)
			{
				temp = ( String[])s;
				orderNo = temp[0];
				orderSeq = temp[1];
				orderDlvSeq = temp[2];
				askPlanId = temp[3];
				askPlanIdSeq = temp[4];
				spetKey = temp[5];
				steelCodeKey = temp[6];
				balancestatus = temp[9];
				if (balancestatus.equals(this.BalanceStatus_Invoice)
						|| balancestatus.equals(BalanceStatus_Init)) continue;
				String sqlB = fact.getSlmBalanceInfoDao()
						.updateBalanceStatusToUnAffrimStr(orderNo, orderSeq,
								orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
								steelCodeKey, person);
				String sqlT = fact.getSlmBalanceTransMoneyDao()
						.unAffrimAskPlanIdFroTransSql(askPlanId, person);
				listSql.add(sqlB);
				listSql.add(sqlT);

			}

			if (listSql.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(listSql);
			}

			trans.commit();

		}

		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();

		}
		finally
		{
			fact.cleantTrans();
			trans = null;
		}
		return cro;

	}

	@SuppressWarnings("unchecked")
	public synchronized CoreReturnObject makeInvoiceLightly(
			java.util.ArrayList list, String forecode, String person)
	{
		CoreReturnObject cro = null;
		DaoFactory fact = null;
		core.stlmes.server.sale.dbhelp.DbTrans trans = null;
		SlmBalanceInfoDao slmBalanceInfoDao = null;
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = null;
		SlmOrderLineTgDao slmOrderLineTgDao = null;
		String[] temp = null;
		@SuppressWarnings("unused")
		java.util.ArrayList arrsBuyercodeAndForeCode = null;
		java.util.Hashtable<String, SlmOrderLineTgBean> listOrdLnAndOrdbean = null;
		java.util.ArrayList listOrdPk = null;
		java.util.ArrayList listAsk = null;
		java.util.Hashtable hsKey = null;
		java.util.Hashtable sortKey = null;
		java.util.Hashtable hsASk = null;

		cro = new CoreReturnObject();
		fact = new DaoFactory();
		trans = new DbTrans();
		slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		slmBalanceTransMoneyDao = fact.getSlmBalanceTransMoneyDao();
		slmOrderLineTgDao = new SlmOrderLineTgDao();
		arrsBuyercodeAndForeCode = new java.util.ArrayList();
		listOrdLnAndOrdbean = new java.util.Hashtable<String, SlmOrderLineTgBean>();
		listOrdPk = new java.util.ArrayList();
		listAsk = new java.util.ArrayList();
		hsKey = new Hashtable();
		sortKey = new Hashtable();
		hsASk = new Hashtable();

		if (list == null) return cro;
		if (list.size() == 0) return cro;
		int maxinvoiceNum = 8;

		try
		{
			// Begin the transaction
			trans.beginTransaction();
			fact.setTrans(trans);
			slmOrderLineTgDao.setTrans(trans);
			String buyerAndOrder = "";

			List<SlmBalanceInfoBean> listBean = null;
			List<String[]> listArray = null;
			SlmInvoiceKey invoiceKey = null;
			String invoicek = "";
			String orderNo = "";
			String orderSeq = "";
			String orderDlvSeq = "";
			String askPlanId = "";
			String askPlanIdSeq = "";
			String spetKey = "";
			String steelCodeKey = "";
			String buyercode = "";
			String price = "";
			String customerNo = "";
			String salearea = "";
			String moneyType = "";
			String contax = "0";
			SlmOrderHeadTgDao slmOrderHeadTgDao = new SlmOrderHeadTgDao();
			// Get all the primary key
			for (Object s : list)
			{
				contax = "0";
				temp = ( String[])s;
				orderNo = temp[0];
				orderSeq = temp[1];
				orderDlvSeq = temp[2];
				askPlanId = temp[3];
				askPlanIdSeq = temp[4];
				spetKey = temp[5];
				steelCodeKey = temp[6];
				buyercode = temp[7];
				price = temp[8];
				customerNo = temp[14];
				salearea = temp[15];
				moneyType = temp[19];
				if (!TicketTypeGoods.equals(moneyType))
				{
					if (OutStockMoneyName.equals(moneyType))
					{
						if (!listAsk.contains(askPlanId)) listAsk
								.add(askPlanId);
					}
					continue;
				}
				// invoiceKey: Get the Group by Value
				invoiceKey = new SlmInvoiceKey();
				invoiceKey.setBuyercode(buyercode);
				SlmBalanceInfoBean t = slmBalanceInfoDao
						.getSlmBalanceInfoByPrimaryKey(orderNo, orderSeq,
								orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
								steelCodeKey);

				if (this.BalanceStatus_Invoice.equals(t.getBalancestatus())) continue;
				if (this.BalanceStatus_Init.equals(t.getBalancestatus())) continue;

				SlmOrderHeadTgBean orderHead = slmOrderHeadTgDao.getSlmOrderHeadTgByOrdPk(t.getOrd_pk().toString());
				String flag="0";
				if(orderHead.getSupply_unit().equals("120504")){
					if(!orderHead.getFst_drawee_unit().equals("120607")){
						flag="1";
					}
					else{
						maxinvoiceNum=10;
					}				
				}
				if(!flag.equals("1")){
					if (core.stlmes.server.sale.util.Convert.getDouble(price) == 0) continue;
				}  

				
				contax = t.getContain_tax();

				if (!listOrdPk.contains(t.getOrd_pk().toString()))
				{
					// Get orderInfo
					List<SlmOrderLineTgBean> listorder = slmOrderLineTgDao
							.getSlmOrderLineTgByOrdPk(t.getOrd_pk().toString());
					if (listorder == null || listorder.size() == 0)
					{

					}
					else
					{
						for (SlmOrderLineTgBean bean : listorder)
						{
							if(orderHead.getSupply_unit().equals("120504")){
								if(!"120607".equals(orderHead.getFst_drawee_unit())){
									if(bean.getBalnce_price_org()==null){
										super.setCroErrMsg(cro, orderNo + "/" + orderSeq
												+ "请先维护内部结算价");
										return cro;
									}
									if(bean.getBalnce_price_org()==0.0){
										super.setCroErrMsg(cro, orderNo + "/" + orderSeq
												+ "内部结算价不能为0");
										return cro;
									}
								}
							}
							if (!listOrdLnAndOrdbean.containsKey(bean
									.getOrd_ln_pk()))
							{
								listOrdLnAndOrdbean.put(bean.getOrd_ln_pk(),
										bean);
							}		
						}

					}

				}

				SlmOrderLineTgBean orderLIn = null;
				if (listOrdLnAndOrdbean.containsKey(t.getOrd_ln_pk()))
				{
					orderLIn = ( SlmOrderLineTgBean)listOrdLnAndOrdbean.get(t
							.getOrd_ln_pk());
				}
				if(!flag.equals("1")){
					if ("0".equals(orderLIn.getBalnce_price_sts()))
					{
						super.setCroErrMsg(cro, orderNo + "/" + orderSeq
								+ CannotSetInvoiceWithNoValidPrice);
						return cro;
					}
				}

				// Set the GroupBy Value
				// customerNo,salearea,contax,buyercode,spetKey,steelCodeKey

				invoiceKey.setOrd_no(customerNo + salearea + contax);
				invoiceKey.setPrice(price + orderNo + orderSeq);
				invoiceKey.setSpetkey(spetKey);
				invoiceKey.setSteelcodekey(steelCodeKey);
				invoicek = invoiceKey.toString();
				listArray = new ArrayList<String[]>();

				String buyercandFor = t.getBuyercode() + t.getForecorpcode();
				String askplandd = t.getAskplan_id();
				if (!"".equals(askplandd))
				{
					if (!hsASk.containsKey(t.getAskplan_id()))
					{
						hsASk.put(askplandd, buyercandFor);
					}

				}
				// Map Key with GroupBy value And records
				if (hsKey.containsKey(invoicek))
				{
					listArray = ( List<String[]>)hsKey.get(invoicek);
					listArray.add(temp);
					hsKey.remove(invoicek);

				}
				else
					listArray.add(temp);

				if (sortKey.containsKey(invoicek))
				{
					sortKey.remove(invoicek);
				}

				hsKey.put(invoicek, listArray);
				sortKey.put(invoicek, invoiceKey);

			}

			java.util.Set s = sortKey.keySet();
			List<String> listseq = new ArrayList<String>(s);
			List<String> listInvoice = new ArrayList<String>();
			if (listseq.size() > 0)
			{
				// Sort the GroupBy Value
				java.util.Collections.sort(listseq);
				// Get the GroupBy Value
				SlmInvoiceKey invoiceFirst = ( SlmInvoiceKey)sortKey
						.get(listseq.get(0));
				String buyercodetemp = invoiceFirst.getBuyercode();
				String orderNotemp = invoiceFirst.getOrd_no();
				String invoiceNo = slmBalanceInfoDao.getmaxInvoiceNo();
				invoiceNo = this.getNextInvoiceNo(invoiceNo);

				int index = 1;

				for (String sort : listseq)
				{

					SlmInvoiceKey invoice = ( SlmInvoiceKey)sortKey.get(sort);

					// Different GroupBy value with diffenent InvoiceNo
					if (!buyercodetemp.equals(invoice.getBuyercode())
							|| !orderNotemp.equals(invoice.getOrd_no()))
					{
						invoiceNo = this.getNextInvoiceNo(slmBalanceInfoDao
								.getmaxInvoiceNo());
						index = 1;

					}

					// If the detail greater than the define Number,
					// Get a new InvoiceNo

					if (index > maxinvoiceNum)
					{
						invoiceNo = this.getNextInvoiceNo(slmBalanceInfoDao
								.getmaxInvoiceNo());
						index = 1;

					}
					if(!listInvoice.contains(invoiceNo))
						listInvoice.add(invoiceNo);
					List<String[]> bal = ( List<String[]>)hsKey.get(sort);
					for (String[] ba : bal)
					{
						ArrayList sqlList = new ArrayList();

						orderNo = ba[0];
						orderSeq = ba[1];
						orderDlvSeq = ba[2];
						askPlanId = ba[3];
						askPlanIdSeq = ba[4];
						spetKey = ba[5];
						steelCodeKey = ba[6];
						// Update Invoice Status
						String strin = slmBalanceInfoDao.makeInvoiceSqlStr(
								orderNo, orderSeq, orderDlvSeq, askPlanId,
								askPlanIdSeq, spetKey, steelCodeKey, invoiceNo,
								index + "", person);
						sqlList.add(strin);
						String strtran = slmBalanceTransMoneyDao
								.updateOutStockMoneyInvoiceNoSqlStr(invoiceNo,
										person, askPlanId);
						sqlList.add(strtran);
						String strUpdateBillM = slmBalanceInfoDao.upateBillM(askPlanId);
						sqlList.add(strUpdateBillM);
						String strtmny = "";
						if (hsASk.containsKey(askPlanId))
						{
							String buyerandfore = hsASk.get(askPlanId)
									.toString();
							strtmny = slmBalanceTransMoneyDao
									.updateTransMoneyInvoiceNoByBuyerCodeSqlStr(
											invoiceNo, person, buyerandfore);
							if (strtmny.length() > 0)
							{
								sqlList.add(strtmny);

							}

						}

						cro = super.ExcuteNonQueryIgnoreTrans(sqlList);

					}

					buyercodetemp = invoice.getBuyercode();
					orderNotemp = invoice.getOrd_no();
					index++;
				}
			}
			trans.commit();
			if (listAsk.size() > 0) makeOutStockMoneyInvoiceSignal(listAsk,
					person);
			if(listInvoice.size()>0){
				String message=insertGlInfo(listInvoice,person,"2");
			}	
		}

		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			System.out.println(ex.getMessage());
			trans.rollback();

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			System.out.println(ex.getMessage());
			trans.rollback();

		}
		finally
		{
			fact.cleantTrans();
			trans.commit();

		}
		return cro;

	}
	

	
	  /**
     * 插入关联交易表
     * @param parm 合同头参数
     * @param flag 1 首次编单 2 末次编单
     * @return
     */
    public String insertGlInfo(List<String> list,String userId,String flag) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            SlmGlAccrued gl =new SlmGlAccrued();
            String insertGlInfo = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.insertGlInfo"); // 新增到crm出库表
			PreparedStatement pstaGlInfo = con.prepareStatement(insertGlInfo);
			SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
	        String yyMMdd = f.format(new Date());
	        String strInvId = "JS" + yyMMdd;
            for(String invoice : list){
            	List<SlmOrderHeadEntity> listBalance = new ArrayList<SlmOrderHeadEntity>();
                String sql = CoreXmlSqlParsersFactory
                        .getSql("GlOrderManage.getBalanceInfo");
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, flag);
                ps.setObject(2, invoice);
                ResultSet rs = ps.executeQuery();
                listBalance = EntityTool
                        .getEntitysByRs(rs, SlmOrderHeadEntity.class);
                for(SlmOrderHeadEntity balance:listBalance){
                	if(flag.equals("1") && "120607".equals(balance.getFstDraweeUnit())){
                		return "";
                	}
                	if(flag.equals("2") && !"120607".equals(balance.getFstDraweeUnit())){
                		return "";
                	}
                	pstaGlInfo.setString(1,strInvId);
                	pstaGlInfo.setString(2,balance.getOrderNo());
                	pstaGlInfo.setString(3,balance.getOrderTyp());
                	pstaGlInfo.setString(4,balance.getOrderTypDesc());
                	pstaGlInfo.setString(5,"123401");
                	pstaGlInfo.setString(6,"应收");
                	pstaGlInfo.setString(7,"123301");
                	pstaGlInfo.setString(8,"货款");
                	pstaGlInfo.setString(9,"0");
                	pstaGlInfo.setString(10,balance.getSupplyUnit());
                	if("120607".equals(balance.getFstDraweeUnit())){
                		pstaGlInfo.setString(11,balance.getCustomerNo());
                    	pstaGlInfo.setString(12,balance.getCustomerNm());
                    	pstaGlInfo.setString(13,balance.getStlmtUnitNo());
                    	pstaGlInfo.setString(14,balance.getStlmtUnitNm());
                    	pstaGlInfo.setString(18,balance.getOrderMnyNtax());
                		pstaGlInfo.setString(20,"2");
                	}
                	else{
                		pstaGlInfo.setString(11,balance.getFstDraweeUnit());
                    	pstaGlInfo.setString(12,balance.getFstDraweeName());
                    	pstaGlInfo.setString(13,balance.getFstDraweeUnit());
                    	pstaGlInfo.setString(14,balance.getFstDraweeName());
                    	pstaGlInfo.setString(18,balance.getOrderMnyTax());
                		pstaGlInfo.setString(20,"3");
                	}
                	
                	pstaGlInfo.setString(15,balance.getSignDate());
                	pstaGlInfo.setString(16,balance.getOrderYear());
                	pstaGlInfo.setString(17,userId);
                	pstaGlInfo.setString(19,invoice);
                	
					pstaGlInfo.executeUpdate();
                }
            	
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
        }
        return "";
    }
    

	private void makeOutStockMoneyInvoiceSignal(ArrayList list, String person)
	{
		core.stlmes.server.sale.dbhelp.DbTrans trans = new DbTrans();
		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		ArrayList listSignalInvoice = new ArrayList();
		ArrayList listSql = new ArrayList();
		// 需要是单独开票的提单号
		try
		{
			trans.beginTransaction();
			slmBalanceInfoDao.setTrans(trans);
			slmBalanceTransMoneyDao.setTrans(trans);
			String askPlanId = "";
			for (Object s : list)
			{
				askPlanId = s.toString();
				List<SlmBalanceInfoBean> listBal = slmBalanceInfoDao
						.getSlmBalanceInfoByAskPlanId(askPlanId);
				if (listBal == null || listBal.size() == 0)
				{
					// 如果有出库费无货款，等
					continue;
				}
				for (SlmBalanceInfoBean sbi : listBal)
				{
					if (this.BalanceStatus_Invoice.equals(sbi
							.getBalancestatus()))
					{
						if (!listSignalInvoice.contains(askPlanId))
						{
							listSignalInvoice.add(askPlanId);
						}
						continue;

					}

				}

			}

			if (listSignalInvoice.size() == 0) return;
			for (Object s : listSignalInvoice)
			{
				String invoiceNo = core.stlmes.server.sale.util.Convert
						.getFormDate(new Date());
				invoiceNo = invoiceNo + super.getRadomChar(3);
				String strtran = slmBalanceTransMoneyDao
						.updateOutStockMoneyInvoiceNoSqlStr(invoiceNo, person,
								s.toString());
				listSql.add(strtran);

			}

			if (listSql.size() > 0)

			super.ExcuteNonQueryIgnoreTrans(listSql);

			trans.commit();

		}
		catch (java.lang.Exception ex)
		{
			trans.rollback();

		}

	}

	/*
	 * 单独开运费or出库费，传入trans_pk，由外端判断只能传同结算单位，同运费or同出库费数据
	 */

	public CoreReturnObject makeLastInvoiceForTransMny(ArrayList list,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String invoiceNo = core.stlmes.server.sale.util.Convert
				.getFormDate(new Date());
		invoiceNo = invoiceNo + super.getRadomChar();
		if (list == null || list.size() == 0) return cro;
		ArrayList sqlList = new ArrayList();
		String sql = "";
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		for (Object s : list)
		{
			sql = slmBalanceTransMoneyDao
					.updateTransMoneyAndStockMnyInvoiceNoByBusiseq(invoiceNo,
							person, s.toString());
			sqlList.add(sql);

		}

		if (sqlList.size() > 0) cro = super.ExcuteNonQueryIgnoreTrans(sqlList);
		return cro;

	}

	public CoreReturnObject makeFirstInvoiceForTransMny(ArrayList list,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		String invoiceNo = core.stlmes.server.sale.util.Convert
				.getFormDate(new Date());
		invoiceNo = invoiceNo + super.getRadomChar();
		if (list == null || list.size() == 0) return cro;
		ArrayList sqlList = new ArrayList();
		String sql = "";
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		for (Object s : list)
		{
			sql = slmBalanceTransMoneyDao
					.updateTransMoneyAndStockMnyInvoiceNoOrgByBusiseq(
							invoiceNo, person, s.toString());
			sqlList.add(sql);

		}

		if (sqlList.size() > 0) cro = super.ExcuteNonQueryIgnoreTrans(sqlList);
		return cro;

	}

	public CoreReturnObject rollBackInvoiceNo(String invoiceNo, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();
		SlmBalanceInfoDao SlmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		SlmBalanceTransMoneyDao SlmBalanceTransMoneyDao = fact
				.getSlmBalanceTransMoneyDao();
		SlmBalanceMnySubtDao SlmBalanceMnySubtDao = fact
				.getSlmBalanceMnySubtDao();
		// new SlmBalanceTransMoneyDao().rollbackInvoiceNo(invoiceNo, person);
		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);

			java.util.List<SlmBalanceInfoBean> beanList = SlmBalanceInfoDao
					.getSlmBalanceInfoByInvoiceNo(invoiceNo);

			if (beanList == null || beanList.size() == 0) return cro;
			SlmBalanceInfoBean bs = beanList.get(0);
			if (bs.getRealinvoiceno() != null)
			{
				if (!"".equals(bs.getRealinvoiceno()))
				{
					super.setCroErrMsg(cro,
							NotAllowRollInvoiceWithRealInvoiceNo);
					return cro;
				}

			}

			SlmBalanceInfoDao.rollBackBillM(invoiceNo);
			
			SlmBalanceInfoDao.unmakeInvoice(invoiceNo);
			SlmBalanceTransMoneyDao.rollbackInvoiceNo(invoiceNo, person);
			SlmBalanceMnySubtDao.rollbackInvoiceNo(invoiceNo);
			SlmBalanceMnySubtDao.deleteGlInfo(invoiceNo);
			trans.commit();
			core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean = new SlmBusinessOpeInfoBean(
					RollBackInvoiceNo, "", "", "", invoiceNo, person, invoiceNo);
			super.logBusinessInfo(bean);
			fact.cleantTrans();
		}
		catch (java.sql.SQLException ex)
		{
			trans.rollback();
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			trans.rollback();
			super.setCroErrMsg(cro, ex.getMessage());
			return cro;
		}
		finally
		{
			trans.rollback();

		}

		return cro;

	}


	public CoreReturnObject affrimTicket(String askPlanid, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new core.stlmes.server.sale.dbhelp.DbTrans();

		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);

			slmBalanceInfoDao.affrimTicket(askPlanid, person);
			slmBalanceInfoDao.updateVirtualAskplanIdToAffrim(askPlanid);

			cro = (new SlmBalanceTransMgt() )
					.saveOutStockMny(askPlanid, person);
			if (cro.getV_errMsg().length() > 0)
			{
				trans.rollback();
				return cro;

			}
			cro = (new SlmBalanceTransMgt() ).affrimTransMnyByAskPlanId(
					askPlanid, person);
			if (cro.getV_errMsg().length() > 0)
			{
				trans.rollback();
				return cro;
			}
			trans.commit();

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		return cro;

	}

	public CoreReturnObject affrimTicketByListNumber(ArrayList listaskPlanid,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new core.stlmes.server.sale.dbhelp.DbTrans();
		if (listaskPlanid == null || listaskPlanid.size() == 0) return cro;
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = null;
		slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		ArrayList sqlList = new ArrayList();
		try
		{
			String sqlB = "";
			String sqlTrans = "";
			for (Object s : listaskPlanid)
			{
				sqlB = slmBalanceInfoDao.affrimTicketSqlByListNumber(s
						.toString(), person);
				sqlTrans = slmBalanceTransMoneyDao.affrimTransTickets(s
						.toString(), person);
				sqlList.add(sqlB);
				sqlList.add(sqlTrans);

			}

			if (sqlList.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(sqlList);

			}

		}

		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

		}
		return cro;

	}

	public CoreReturnObject unaffrimTicketByListNumber(String listnumber,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new core.stlmes.server.sale.dbhelp.DbTrans();

		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = null;
		slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		ArrayList sqlList = new ArrayList();
		try
		{
			String sqlB = "";
			String sqlTrans = "";

			if (!isListNumberCanBeRollBack(listnumber))
			{
				super.setCroErrMsg(cro, AskPlanIdNotAllowRollbackMsg);
				return cro;

			}

			sqlB = slmBalanceInfoDao.unaffrimTicketSqlByListNumber(listnumber,
					person);
			sqlTrans = slmBalanceTransMoneyDao.unaffrimTransTickets(listnumber,
					person);
			sqlList.add(sqlB);
			sqlList.add(sqlTrans);

			if (sqlList.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(sqlList);

			}

		}

		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

		}
		return cro;

	}

	public CoreReturnObject unAffrimTicket(String askPlanid, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new core.stlmes.server.sale.dbhelp.DbTrans();
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		try
		{
			if (!this.isAskPlanIdCanBeRollBack(askPlanid))
			{
				super.setCroErrMsg(cro, AskPlanIdNotAllowRollbackMsg);
				return cro;

			}
			trans.beginTransaction();
			slmBalanceTransMoneyDao.setTrans(trans);
			fact.setTrans(trans);
			slmBalanceInfoDao.unAffrimTicket(askPlanid, person);
			slmBalanceInfoDao
					.updateVirtualAskplanIdWhenCancelAffrimTickets(askPlanid);
			slmBalanceTransMoneyDao.unaffrimAskPlanIdFroTransMny(askPlanid,
					person);
			List<SlmBalanceTransMoneyBean> list = slmBalanceTransMoneyDao
					.getunInvoiceOutStockMnySlmBalanceTransMoneyByAskPlanId(askPlanid);
			if (list == null)
			{
			}
			else
			{
				for (SlmBalanceTransMoneyBean bean : list)
				{
					slmBalanceTransMoneyDao.delete(bean.getTrans_pk());

				}
			}

			trans.commit();
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		return cro;

	}

	private String getNextInvoiceNo(String invoiceNo)
	{
		java.math.BigDecimal bd = new java.math.BigDecimal(invoiceNo);
		java.math.BigDecimal bd1 = new java.math.BigDecimal(1);
		bd = bd.add(bd1);
		return bd.toString();
	}

	private boolean isAskPlanIdCanBeRollBack(String askPlanId)
			throws java.sql.SQLException
	{
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		List<SlmBalanceInfoBean> bean = null;
		bean = slmBalanceInfoDao.getSlmBalanceInfoByAskPlanId(askPlanId);

		if (bean == null) return true;
		for (SlmBalanceInfoBean ba : bean)
		{
			if (ba.getBalancestatus() == null) continue;
			// if (ba.getBalancestatus().equals(this.BalanceStatus_Invoice))
			// return false;
			if (this.BalanceStatus_Invoice.equals(ba.getBalancestatus())) return false;
			// if (ba.getBalancestatus().equals(this.BalanceStatus_Affrim))
			// return false;
			// if (ba.getBalancestatus_org().equals(this.BalanceStatus_Affrim))
			// return false;
			// if (ba.getBalancestatus_org().equals(this.BalanceStatus_Invoice))
			// return false;
			if (this.BalanceStatus_Invoice.equals(ba.getBalancestatus_org())) return false;

		}

		return true;

	}

	private boolean isListNumberCanBeRollBack(String listnumber)
			throws java.sql.SQLException
	{
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		List<SlmBalanceInfoBean> bean = null;
		List<SlmBalanceTransMoneyBean> tbean = null;
		bean = slmBalanceInfoDao.getSlmBalanceInfoByListnumber(listnumber);
		tbean = fact.getSlmBalanceTransMoneyDao()
				.getOutStockMnySlmBalanceTransMoneyByListnumber(listnumber);

		if (tbean != null)
		{

			for (SlmBalanceTransMoneyBean s : tbean)
			{
				if ("1".equals(s.getStatus_cd())
						|| "2".equals(s.getStatus_cd()))
				{
					return false;

				}
			}

		}

		if (bean != null)
		{
			for (SlmBalanceInfoBean ba : bean)
			{
				if (ba.getBalancestatus() == null) continue;

				if (this.BalanceStatus_Invoice.equals(ba.getBalancestatus())) return false;
				if (this.BalanceStatus_Affrim.equals(ba.getBalancestatus())) return false;
				if (this.BalanceStatus_Invoice
						.equals(ba.getBalancestatus_org())) return false;
				if (this.BalanceStatus_Affrim.equals(ba.getBalancestatus_org())) return false;

			}
		}

		return true;

	}

	private boolean isAskPlanIdCanBeRollBackWithTicketStatus(String askPlanId)
			throws java.sql.SQLException
	{
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		List<SlmBalanceInfoBean> bean = null;
		bean = slmBalanceInfoDao.getSlmBalanceInfoByAskPlanId(askPlanId);

		if (bean == null) return true;
		for (SlmBalanceInfoBean ba : bean)
		{
			if (!this.BalanceStatus_Init.equals(ba.getBalancestatus())) return false;
			if (!BalanceStatus_Init.equals(ba.getBalancestatus_org())) return false;
			// if ("1".equals(ba.getTicketstatus())) return false;

		}

		return true;

	}

	public CoreReturnObject updateRealInvoiceNo(String invoiceno,
			String realInvoiceNo, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		try
		{
			cro = slmBalanceInfoDao.updateRealInvoiceNo(invoiceno,
					realInvoiceNo, person);
			slmBalanceTransMoneyDao.updateRealInvoiceNo(invoiceno,
					realInvoiceNo, person);
			updaterealInvoiceNoForLastInvoce(invoiceno, realInvoiceNo, person);

			slmBalanceInfoDao.UpdateProcessOrderMByUpdateRealInvoiceNo(
					invoiceno, realInvoiceNo);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;

	}

	private void updaterealInvoiceNoForLastInvoce(String invoiceno,
			String realInvoiceNo, String person)
	{
		try
		{
			SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
			java.util.ArrayList arraySql = new java.util.ArrayList();
			List<SlmBalanceInfoBean> list = slmBalanceInfoDao
					.getSlmBalanceInfoByInvoiceNo(invoiceno);
			if (list == null || list.size() == 0) return;
			String sql = "";
			YdmZcBillMDao ydmZcBillMDao = new YdmZcBillMDao();
			for (SlmBalanceInfoBean s : list)
			{
				sql = ydmZcBillMDao
						.updateRealInvoiceToBillBySavingRealInvoiceForVirtualBill(
								realInvoiceNo, s.getAskplan_id(),
								s.getOrd_pk(), s.getOrd_ln_pk(), s
										.getOrd_ln_dly_pk());
				arraySql.add(sql);

			}

			if (arraySql.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(arraySql);
			}

		}
		catch (java.sql.SQLException ex)
		{

		}

	}

	private void updaterealInvoiceNoForFirstInvoce(String invoiceno,
			String realInvoiceNo, String person)
	{
		try
		{
			SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
			java.util.ArrayList arraySql = new java.util.ArrayList();
			List<SlmBalanceInfoBean> list = slmBalanceInfoDao
					.getSlmBalanceInfoByInvoiceNoOrg(invoiceno);
			if (list == null || list.size() == 0) return;
			String sql = "";
			YdmZcBillMDao ydmZcBillMDao = new YdmZcBillMDao();
			for (SlmBalanceInfoBean s : list)
			{
				sql = ydmZcBillMDao
						.updateRealInvoiceToBillBySavingRealFristInvoiceForVirtualBill(
								realInvoiceNo, s.getAskplan_id(),
								s.getOrd_pk(), s.getOrd_ln_pk(), s
										.getOrd_ln_dly_pk());
				arraySql.add(sql);

			}

			if (arraySql.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(arraySql);
			}

		}
		catch (java.sql.SQLException ex)
		{

		}

	}

	public CoreReturnObject updateRealInvoiceNoOrg(String invoiceno,
			String realInvoiceNo, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		try
		{
			cro = slmBalanceInfoDao.updateRealInvoiceNoOrg(invoiceno,
					realInvoiceNo, person);
			slmBalanceTransMoneyDao.updateRealInvoiceNoOrg(invoiceno,
					realInvoiceNo, person);
			updaterealInvoiceNoForFirstInvoce(invoiceno, realInvoiceNo, person);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;

	}

	public CoreReturnObject updateFinallBalPrice(String ordLnPk, String price,
			String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		try
		{

			slmOrderLineTgDao.updateFinallBalPrice(ordLnPk + "Cannot", price,
					person);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;
	}

	public CoreReturnObject updateFinallBalPriceOrg(String ordLnPk,
			String price, String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		try
		{
			slmOrderLineTgDao.updateFinallBalPriceOrg(ordLnPk, price, person);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;
	}

	public CoreReturnObject affrimFinallBalPrice(String orderNo, String person)
			throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		try
		{
			slmOrderLineTgDao.affrimFinallBalPrice(orderNo, person);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;
	}

	public CoreReturnObject unAffrimFinallBalPrice(String orderNo, String person)
			throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		try
		{
			slmOrderLineTgDao.unAffrimFinallBalPrice(orderNo, person);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;
	}

	public CoreReturnObject affrimFinallBalPriceOrg(String orderNo,
			String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		try
		{
			slmOrderLineTgDao.affrimFinallBalPriceOrg(orderNo, person);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;
	}

	public CoreReturnObject unAffrimFinallBalPriceOrg(String orderNo,
			String person) throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
		try
		{
			slmOrderLineTgDao.unAffrimFinallBalPriceOrg(orderNo, person);
		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

			return cro;
		}
		return cro;
	}

	public CoreReturnObject updateSendNum(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String sendNum, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
		try
		{
			slmBalanceInfoDao.updateSendNum(sendNum, orderNo, orderSeq,
					orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
					steelCodeKey, person);

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());

		}
		return cro;

	}

	public CoreReturnObject updateSendNum(String orderNo, String orderSeq,
			String orderDlvSeq, String askPlanId, String askPlanIdSeq,
			String spetKey, String steelCodeKey, String sendNum,
			java.util.ArrayList list, String person)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
		SlmBalanceDetailAllDao slmBalanceDetailAllDao = new SlmBalanceDetailAllDao();
		String batchNo = "";
		String Num = "";
		String[] temp = null;
		core.stlmes.server.sale.dbhelp.DbTrans trans = new core.stlmes.server.sale.dbhelp.DbTrans();

		try
		{
			if (list == null || list.size() == 0) return cro;
			trans.beginTransaction();
			slmBalanceDetailAllDao.setTrans(trans);
			slmBalanceInfoDao.setTrans(trans);
			for (Object array : list)
			{
				temp = ( String[])array;
				batchNo = temp[0];
				Num = temp[1];
				slmBalanceDetailAllDao.updateSendNum(orderNo, orderSeq,
						orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
						steelCodeKey, batchNo, Num);

			}
			slmBalanceInfoDao.updateSendNum(sendNum, orderNo, orderSeq,
					orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
					steelCodeKey, person);
			trans.commit();

		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();

		}
		return cro;

	}

	public CoreReturnObject creditByAddNewTicketsWithSameWgt(String invoiceNo,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		List<SlmBalanceInfoBean> listbalInfo = null;
		List<SlmBalanceDetailAllBean> listDetail = null;
		List<SlmBalanceDetailAllBean> listAddDetail = null;
		List<SlmBalanceDetailAllBean> listCreditDetail = null;
		List<SlmBalanceInfoBean> listAddinfo = null;
		List<SlmBalanceInfoBean> listcreditinfo = null;

		List<SlmBalanceTransMoneyBean> listtransAdd = null;
		List<SlmBalanceTransMoneyBean> listtransCredit = null;
		SlmBalanceInfoDao slmBalanceInfoDao = null;
		SlmBalanceDetailAllDao slmBalanceDetailAllDao = null;
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = null;
		slmBalanceInfoDao = new SlmBalanceInfoDao();
		slmBalanceDetailAllDao = new SlmBalanceDetailAllDao();
		slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		listAddDetail = new ArrayList<SlmBalanceDetailAllBean>();
		listCreditDetail = new ArrayList<SlmBalanceDetailAllBean>();
		listAddinfo = new ArrayList<SlmBalanceInfoBean>();
		listcreditinfo = new ArrayList<SlmBalanceInfoBean>();

		listtransAdd = new ArrayList<SlmBalanceTransMoneyBean>();
		listtransCredit = new ArrayList<SlmBalanceTransMoneyBean>();
		DbTrans trans = new DbTrans();
		try
		{

			trans.beginTransaction();
			slmBalanceInfoDao.setTrans(trans);
			slmBalanceDetailAllDao.setTrans(trans);
			slmBalanceTransMoneyDao.setTrans(trans);
			listbalInfo = slmBalanceInfoDao
					.getSlmBalanceInfoByRealInvoiceNo(invoiceNo);

			List<SlmBalanceTransMoneyBean> list = slmBalanceTransMoneyDao
					.getunInvoiceOutStockMnySlmBalanceTransMoneyByRealInvoiceNo(invoiceNo);
			if (listbalInfo == null || listbalInfo.size() == 0) return cro;
			for (SlmBalanceInfoBean bean : listbalInfo)
			{
				listDetail = slmBalanceDetailAllDao
						.getAcceptSlmBalanceDetailAllByAskPlanId(bean
								.getAskplan_id());
				for (SlmBalanceDetailAllBean detail : listDetail)
				{
					if ( (detail.getAskplan_id() + detail.getAskplan_id_seq()
							+ detail.getOrd_no() + detail.getOrd_seq()
							+ detail.getOrd_ln_dly() + detail.getSteelcodekey() + detail
							.getSpetkey() ).equals(bean.getAskplan_id()
							+ bean.getAskplan_id_seq() + bean.getOrd_no()
							+ bean.getOrd_seq() + bean.getOrd_ln_dly()
							+ bean.getSteelcodekey() + bean.getSpetkey()))
					{
						// listAddDetail.add(detail);
						listCreditDetail.add(detail);
					}

				}

				listcreditinfo.add(bean);
				// listAddinfo.add(bean);

			}

			if (list == null || list.size() == 0)
			{

			}
			else
			{
				listtransAdd.addAll(list);
				listtransCredit.addAll(list);

			}

			String randomCredit = super.getRadomChar(2) + "C";
			String randomAdd = "C";
			
			String currentMonth = new SimpleDateFormat("yyyyMM").format(new Date()); // add by ym 20160308

			// 新增负记录
			for (SlmBalanceDetailAllBean detail : listCreditDetail)
			{
				detail.setPrd_id(detail.getPrd_id() + randomCredit);
				detail.setAskplan_id_seq(detail.getAskplan_id_seq()
						+ randomCredit);
				detail.setAct_wgt(0 - detail.getAct_wgt());
				detail.setPrd_qty(0 - detail.getPrd_qty());
				detail.setSend_num(0 - detail.getSend_num());
				slmBalanceDetailAllDao.insert(detail);
				// 正
				detail.setPrd_id(detail.getPrd_id() + randomAdd);
				detail.setAskplan_id_seq(detail.getAskplan_id_seq()+ randomAdd);
				detail.setAct_wgt(0 - detail.getAct_wgt());
				detail.setPrd_qty(0 - detail.getPrd_qty());
				detail.setSend_num(0 - detail.getSend_num());
				slmBalanceDetailAllDao.insert(detail);

			}
			for (SlmBalanceInfoBean bean : listcreditinfo)
			{
				// 负记录
				bean.setAskplan_id_seq(bean.getAskplan_id_seq() + randomCredit);
				bean.setWeight(0 - bean.getWeight());
				bean.setQuantity(0 - bean.getQuantity());
				bean.setSend_num(0 - bean.getSend_num());
				bean.setMoney(0 - bean.getMoney());
				bean.setMoney_org(0 - bean.getMoney_org());
				
			    bean.setInvoice_month(currentMonth);// add by ym 20160308，设置记录时间月份为当前月份
                bean.setInvoice_sts("0");// add by ym 20160308，设置为未结转
                
				slmBalanceInfoDao.insert(bean);

				// 正记录
				bean.setAskplan_id_seq(bean.getAskplan_id_seq() + randomAdd);
				bean.setWeight(0 - bean.getWeight());
				bean.setQuantity(0 - bean.getQuantity());
				bean.setSend_num(0 - bean.getSend_num());
				bean.setMoney(0 - bean.getMoney());
				bean.setMoney_org(0 - bean.getMoney_org());
				bean.setRealinvoiceno("");
				bean.setTicketstatus("1");
				bean.setBalancestatus("0");
				bean.setInvoiceno("");
				bean.setInvoice_sts("0");
				
				bean.setInvoice_month(currentMonth); // add by ym 20160308
				
				slmBalanceInfoDao.insert(bean);

			}

			for (SlmBalanceTransMoneyBean bean : listtransCredit)
			{
				bean.setWeight(0 - bean.getWeight());
				bean.setMoney(0 - bean.getMoney());
				bean.setTicketnum(0 - bean.getTicketnum());
				
				bean.setInvoice_month(currentMonth); // add by ym 20160308
                bean.setInvoice_sts("0"); // add by ym 20160308
                
				slmBalanceTransMoneyDao.insert(bean);

				bean.setWeight(0 - bean.getWeight());
				bean.setMoney(0 - bean.getMoney());
				bean.setTicketnum(0 - bean.getTicketnum());
				bean.setStatus_cd("0");
				bean.setRealinvoiceno("");
				bean.setInvoiceno("");
				bean.setInvoice_sts("0");
				
                bean.setInvoice_month(currentMonth);// add by ym 20160308
                
				slmBalanceTransMoneyDao.insert(bean);

			}

			trans.commit();

			core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean1 = new SlmBusinessOpeInfoBean(
					"Credit", "", "", "", invoiceNo, person, "SUCCESS");
			super.logBusinessInfo(bean1);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		finally
		{
			trans.commit();
		}
		return cro;

	}

	public CoreReturnObject creditByAddNewTicketsWithSameWgtOrg(
			String invoiceNo, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		List<SlmBalanceInfoBean> listbalInfo = null;
		List<SlmBalanceDetailAllBean> listDetail = null;
		List<SlmBalanceDetailAllBean> listAddDetail = null;
		List<SlmBalanceDetailAllBean> listCreditDetail = null;
		List<SlmBalanceInfoBean> listAddinfo = null;
		List<SlmBalanceInfoBean> listcreditinfo = null;
		List<SlmBalanceTransMoneyBean> listtransCredit = null;

		SlmBalanceInfoDao slmBalanceInfoDao = null;
		SlmBalanceDetailAllDao slmBalanceDetailAllDao = null;
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = null;
		slmBalanceInfoDao = new SlmBalanceInfoDao();
		slmBalanceDetailAllDao = new SlmBalanceDetailAllDao();
		slmBalanceTransMoneyDao = new SlmBalanceTransMoneyDao();
		listAddDetail = new ArrayList<SlmBalanceDetailAllBean>();
		listCreditDetail = new ArrayList<SlmBalanceDetailAllBean>();
		listAddinfo = new ArrayList<SlmBalanceInfoBean>();
		listcreditinfo = new ArrayList<SlmBalanceInfoBean>();
		listtransCredit = new ArrayList<SlmBalanceTransMoneyBean>();
		DbTrans trans = new DbTrans();
		try
		{
			trans.beginTransaction();

			slmBalanceInfoDao.setTrans(trans);
			slmBalanceDetailAllDao.setTrans(trans);
			slmBalanceTransMoneyDao.setTrans(trans);
			listbalInfo = slmBalanceInfoDao
					.getSlmBalanceInfoByRealInvoiceNoOrg(invoiceNo);

			if (listbalInfo == null || listbalInfo.size() == 0) return cro;
			for (SlmBalanceInfoBean bean : listbalInfo)
			{
				listDetail = slmBalanceDetailAllDao
						.getAcceptSlmBalanceDetailAllByAskPlanId(bean
								.getAskplan_id());
				for (SlmBalanceDetailAllBean detail : listDetail)
				{
					if ( (detail.getAskplan_id() + detail.getAskplan_id_seq()
							+ detail.getOrd_no() + detail.getOrd_seq()
							+ detail.getOrd_ln_dly() + detail.getSteelcodekey() + detail
							.getSpetkey() ).equals(bean.getAskplan_id()
							+ bean.getAskplan_id_seq() + bean.getOrd_no()
							+ bean.getOrd_seq() + bean.getOrd_ln_dly()
							+ bean.getSteelcodekey() + bean.getSpetkey()))
					{
						// listAddDetail.add(detail);
						listCreditDetail.add(detail);
					}

				}

				listcreditinfo.add(bean);
				// listAddinfo.add(bean);

			}

			String randomCredit = super.getRadomChar(2) + "C";
			String randomAdd = "C";
			
			String currentMonth = new SimpleDateFormat("yyyyMM").format(new Date()); // add by ym 20160308

			// 新增负记录
			for (SlmBalanceDetailAllBean detail : listCreditDetail)
			{
				detail.setPrd_id(detail.getPrd_id() + randomCredit);
				detail.setAskplan_id_seq(detail.getAskplan_id_seq()
						+ randomCredit);
				detail.setAct_wgt(0 - detail.getAct_wgt());
				detail.setPrd_qty(0 - detail.getPrd_qty());
				detail.setSend_num(0 - detail.getSend_num());
				slmBalanceDetailAllDao.insert(detail);
				// 正
				detail.setPrd_id(detail.getPrd_id() + randomAdd);
				detail
						.setAskplan_id_seq(detail.getAskplan_id_seq()
								+ randomAdd);
				detail.setAct_wgt(0 - detail.getAct_wgt());
				detail.setPrd_qty(0 - detail.getPrd_qty());
				detail.setSend_num(0 - detail.getSend_num());
				slmBalanceDetailAllDao.insert(detail);

			}
			for (SlmBalanceInfoBean bean : listcreditinfo)
			{
				// 负记录
				bean.setAskplan_id_seq(bean.getAskplan_id_seq() + randomCredit);
				bean.setWeight(0 - bean.getWeight());
				bean.setQuantity(0 - bean.getQuantity());
				bean.setSend_num(0 - bean.getSend_num());
				bean.setMoney(0 - bean.getMoney());
				bean.setMoney_org(0 - bean.getMoney_org());
				
				bean.setInvoice_month(currentMonth); // add by ym 20160308
                bean.setInvoice_sts("0"); // add by ym 20160308
				
				slmBalanceInfoDao.insert(bean);

				// 正记录
				bean.setAskplan_id_seq(bean.getAskplan_id_seq() + randomAdd);
				bean.setWeight(0 - bean.getWeight());
				bean.setQuantity(0 - bean.getQuantity());
				bean.setSend_num(0 - bean.getSend_num());
				bean.setMoney(0 - bean.getMoney());
				bean.setMoney_org(0 - bean.getMoney_org());

				bean.setTicketstatus("1");
				bean.setBalancestatus_org("0");
				bean.setInvoiceno_org("");
				bean.setRealinvoiceno_org("");
				bean.setInvoice_sts("0");
				
                bean.setInvoice_month(currentMonth); // add by ym 20160308
                
				slmBalanceInfoDao.insert(bean);

			}

			for (SlmBalanceTransMoneyBean bean : listtransCredit)
			{
				bean.setWeight(0 - bean.getWeight());
				bean.setMoney(0 - bean.getMoney());
				bean.setTicketnum(0 - bean.getTicketnum());
				
                bean.setInvoice_sts("0");// add by ym 20160308
                bean.setInvoice_month(currentMonth); // add by ym 20160308
                
				slmBalanceTransMoneyDao.insert(bean);

				bean.setWeight(0 - bean.getWeight());
				bean.setMoney(0 - bean.getMoney());
				bean.setTicketnum(0 - bean.getTicketnum());
				bean.setStatus_cd_org("0");
				bean.setRealinvoiceno_org("");
				bean.setInvoice_sts("0");
				
                bean.setInvoice_month(currentMonth); // add by ym 20160308
				
				slmBalanceTransMoneyDao.insert(bean);

			}

			trans.commit();

			core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean1 = new SlmBusinessOpeInfoBean(
					"Credit", "", "", "", invoiceNo, person, "SUCCESS");
			super.logBusinessInfo(bean1);

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		finally
		{
			trans.commit();
		}
		return cro;

	}

	public CoreReturnObject credit(java.util.ArrayList list, String person)
			throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		String[] temp = null;

		String invoiceno = "";
		String invoiceseq = "";
		String price = "";
		String money = "";
		String sub_seq = "";
		DbTrans trans = new DbTrans();

		try
		{

			if (list == null || list.size() == 0) return cro;

			trans.beginTransaction();
			fact.setTrans(trans);
			for (Object array : list)
			{
				temp = ( String[])array;
				invoiceno = temp[0];
				invoiceseq = temp[1];
				price = temp[2];
				money = temp[3];
				sub_seq = temp[4];
				slmBalanceInfoDao.creditInvoicePrice(invoiceno + invoiceseq,
						price, money, person);

			}
			fact.getSlmBalanceMnySubtDao().updateInvoiceNo(sub_seq, invoiceno);
			trans.commit();
			fact.cleantTrans();

		}
		catch (java.sql.SQLException ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		catch (java.lang.Exception ex)
		{
			super.setCroErrMsg(cro, ex.getMessage());
			trans.rollback();
			return cro;
		}
		return cro;
	}

	// 调整发票时间
	public CoreReturnObject updateRealInvoiceDate(java.util.ArrayList list,
			String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (list == null || list.size() == 0) return cro;
		String[] temp = null;
		String invoiceNo = "";
		String realInvoiceDate = "";
		java.util.ArrayList listSql = new java.util.ArrayList();
		String sql = "";

		SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
		for (Object s : list)
		{
			temp = ( String[])s;
			invoiceNo = temp[0];
			realInvoiceDate = temp[1];
			sql = slmBalanceInfoDao.updateRealInvoiceDate(invoiceNo,
					realInvoiceDate, person);
			listSql.add(sql);

		}

		if (listSql.size() > 0)
		{
			cro = super.ExcuteNonQueryIgnoreTrans(listSql);
		}
		return cro;

	}

	public CoreReturnObject updateInvoiceStatusToLocked(
			java.util.ArrayList list, String person) throws SQLException,
			Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		String[] temp = null;
		String invoiceno = "";
		java.util.ArrayList sqlList = new java.util.ArrayList();
		try
		{

			if (list == null || list.size() == 0) return cro;
			for (Object s : list)
			{
				invoiceno = s.toString();
				sqlList.add(slmBalanceInfoDao.updateInvoiceStatusToLocked(
						invoiceno, person));

			}

			if (sqlList.size() > 0)
			{
				cro = super.ExcuteNonQueryIgnoreTrans(sqlList);
			}

		}

		catch (java.lang.Exception ex)
		{
			return cro;
		}
		return cro;
	}

	public void updateInvoicePrice(String ordPk)
	{
		try
		{
			SlmOrderLineTgDao slmOrderLineTgDao = new SlmOrderLineTgDao();
			List<SlmOrderLineTgBean> list = slmOrderLineTgDao
					.getSlmOrderLineTgByOrdPk(ordPk);
			ArrayList listSql = new ArrayList();
			SlmBalanceInfoDao slmBalanceInfoDao = new SlmBalanceInfoDao();
			SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = new SlmBalanceInfoOrgDao();
			if (list == null || list.size() == 0)
			{
				return;
			}
			String sql = "";
			String sqlOrg = "";

			for (SlmOrderLineTgBean order : list)
			{

				sql = "";
				sqlOrg = "";
				if ("1".equals(order.getBalnce_price_sts()))
				{
					sql = slmBalanceInfoDao.insertBalancePriceByOrdLnPk(ordPk,
							order.getOrd_ln_pk(), order.getBalnce_price() + "",
							"System");
					listSql.add(sql);
				}
				else
				{
					sql = slmBalanceInfoDao.insertBalancePriceByOrdLnPk(ordPk,
							order.getOrd_ln_pk(), 0 + "", "System");
					listSql.add(sql);

				}

				if ("1".equals(order.getBalnce_price_org_sts()))
				{
					sqlOrg = slmBalanceInfoOrgDao
							.insertBalancePriceSqlByOrdPkAndLnPk(ordPk, order
									.getOrd_ln_pk(), order.getOut_price() + "",
									"System");
					listSql.add(sqlOrg);
				}
				else
				{
					sqlOrg = slmBalanceInfoOrgDao
							.insertBalancePriceSqlByOrdPkAndLnPk(ordPk, order
									.getOrd_ln_pk(), 0 + "", "System");
					listSql.add(sqlOrg);

				}

			}
			if (listSql.size() > 0)
			{
				super.ExcuteNonQueryIgnoreTrans(listSql);
			}

		}
		catch (java.lang.Exception ex)
		{

		}
	}

	private String BalanceStatus_Init = "0";
	private String BalanceStatus_Affrim = "2";
	private String BalanceStatus_Invoice = "3";
	private String AskPlanIdNotAllowRollbackMsg = "提单有部分数据已经结算或确认结算,不允许回退";
	private String AcceptAskPlanIdSuccess = "接收提单成功";
	private String AskPlanIdSendException = "货款发送异常";
	private String AskPlanIdHadSend = "货款已经发送";
	private String AcceptRollBackData = "接收退货记录";
	private String OutStockMoneyName = "出库费";
	private String OutStockMoneyNameInvoiceDMsg = "出库费用已经编单，不允许取消";
	private String RollBackAskPlanIdName = "退提单";
	private String TruthName = "是";
	private String TicketNotAllowedCancel = "单据已经处于结算状态，不允许取消";
	private String TicketTypeGoods = "货款";
	private String CannotSetCardsWithNoValidPrice = "当前结算价格无效,无法编卡";
	private String CannotSetInvoiceWithNoValidPrice = "当前结算价格无效,无法编单";
	private String RollBackInvoiceNo = "退结算单";
	private String NotAllowRollInvoiceWithRealInvoiceNo = "该结算单已经开具正式放票，请先取消发票号再回退";

}
