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

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import CoreFS.SA06.CoreReturnObject;
import core.stlmes.server.sale.dao.DaoFactory;
import core.stlmes.server.sale.dao.SlmBalanceInfoDao;
import core.stlmes.server.sale.dao.SlmBalanceMnySubtDao;
import core.stlmes.server.sale.dao.SlmBalanceTransMoneyDao;
import core.stlmes.server.sale.dao.SlmOrderHeadTgDao;
import core.stlmes.server.sale.dao.SlmOrderLineTgDao;
import core.stlmes.server.sale.dao.SlmOrderPriceDao;
import core.stlmes.server.sale.dbhelp.DbTrans;
import core.stlmes.server.sale.po.SlmBalanceInfoBean;
import core.stlmes.server.sale.po.SlmBusinessOpeInfoBean;
import core.stlmes.server.sale.po.SlmOrderLineTgBean;
import core.stlmes.server.sale.po.SlmOrderHeadTgBean;
import core.stlmes.server.sale.service.baseservice.BaseBll;
import core.stlmes.server.sale.dao.*;

public class SlmBalanceTicketsOrg extends BaseBll
{
	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();
		SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = new SlmBalanceInfoOrgDao();
		try
		{
			SlmBalanceInfoBean bean = slmBalanceInfoOrgDao
					.getSlmBalanceInfoByPrimaryKey(orderNo, orderSeq,
							orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
							steelCodeKey);
			if (bean == null) return cro;
			if (bean.getBalancestatus().equals("3"))
			{
				super.setCroErrMsg(cro, "");
				return cro;
			}

			slmBalanceInfoOrgDao.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 = "";
		core.stlmes.server.sale.dao.DaoFactory fact = new DaoFactory();
		core.stlmes.server.sale.dbhelp.DbTrans trans = new DbTrans();

		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);
			SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = fact
					.getSlmBalanceInfoOrgDao();
			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[13];
				if (balancestatus.equals("3") || balancestatus.equals("2")) continue;
				slmBalanceInfoOrgDao.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();
		SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = new SlmBalanceInfoOrgDao();

		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 ("货款".equals(moneyType))
				{
					SlmBalanceInfoBean t = slmBalanceInfoDao
							.getSlmBalanceInfoByPrimaryKey(orderNo, orderSeq,
									orderDlvSeq, askPlanId, askPlanIdSeq,
									spetKey, steelCodeKey);

					// if (balancestatus.equals("3")) continue;
					if (!"0".equals(t.getBalancestatus_org())) continue;
					// if ("2".equals(t.getBalancestatus())) continue;
					String price = t.getPrice_org() + "";
					if (core.stlmes.server.sale.util.Convert.getDouble(price) == 0)
					{
						super.setCroErrMsg(cro, orderNo + "/" + orderSeq
								+ "  当前结算价格0");
						return cro;
					}

					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());
					}
					// 当前结算价格无效
					if ("0".equals(orderLIn.getBalnce_price_org_sts())
							&& (!"国际贸易部".equals(t.getForecorpcode()) ))
					{
						super.setCroErrMsg(cro, orderNo + "/" + orderSeq
								+ "  当前结算价格无效,无法编卡");
						return cro;
					}
					String sql = slmBalanceInfoOrgDao
							.updateBalanceStatusToAffrimStr(orderNo, orderSeq,
									orderDlvSeq, askPlanId, askPlanIdSeq,
									spetKey, steelCodeKey, person);
					listSql.add(sql);
					String sqlTrans = slmBalanceTransMoneyDao
							.affrimAskPlanIdFroTransSqlOrg(askPlanId, person);
					listSql.add(sqlTrans);

				}
				else
				{

					String sqlTrans = slmBalanceTransMoneyDao
							.affrimAskPlanIdFroTransSqlOrg(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 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);
			SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = fact
					.getSlmBalanceInfoOrgDao();
			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[13];

				String sqlB = fact.getSlmBalanceTransMoneyDao()
						.unaffrimAskPlanIdFroTransSqlOrg(askPlanId, person);

				listSql.add(sqlB);

				if (balancestatus.equals("3") || balancestatus.equals("0")) continue;
				String sqlA = slmBalanceInfoOrgDao
						.updateBalanceStatusTounAffrimStr(orderNo, orderSeq,
								orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
								steelCodeKey, person);

				listSql.add(sqlA);

			}

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

		}

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

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

	}

	public synchronized CoreReturnObject makeInvoiceLightly(
			java.util.ArrayList list, String forecode, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();
		SlmBalanceInfoDao slmBalanceInfoDao = fact.getSlmBalanceInfoDao();
		SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = new SlmBalanceInfoOrgDao();
		SlmBalanceTransMoneyDao slmBalanceTransMoneyDao = fact
				.getSlmBalanceTransMoneyDao();
		SlmOrderHeadTgDao slmOrderHeadTgDao = new SlmOrderHeadTgDao();
		if (list == null) return cro;
		if (list.size() == 0) return cro;
		int maxinvoiceNum = 2000;
		String[] temp = null;

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

		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);
			slmBalanceInfoOrgDao.setTrans(trans);
			String buyerAndOrder = "";
			java.util.Hashtable hsKey = new Hashtable();
			java.util.Hashtable sortKey = new Hashtable();
			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 moneyType = "";
			String contax = "0";
			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];
				buyercode = temp[16];

				moneyType = temp[19];

				if (!"货款".equals(moneyType))
				{
					if ("出库费".equals(moneyType))
					{
						if (!listAsk.contains(askPlanId)) listAsk
								.add(askPlanId);
					}
					continue;
				}

				SlmBalanceInfoBean t = slmBalanceInfoDao
						.getSlmBalanceInfoByPrimaryKey(orderNo, orderSeq,
								orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
								steelCodeKey);

				if (this.BalanceStatusOrg_Invoice.equals(t
						.getBalancestatus_org())) continue;
				if (this.BalanceStatusOrg_Init.equals(t.getBalancestatus_org())) continue;
				
				SlmOrderHeadTgBean orderHead = slmOrderHeadTgDao.getSlmOrderHeadTgByOrdPk(t.getOrd_pk().toString());
				if(orderHead.getSupply_unit().equals("120504")){
					maxinvoiceNum=10;			
				}
				
				contax = t.getContain_tax();
				invoiceKey = new SlmInvoiceKey();
				invoiceKey.setBuyercode(buyercode + contax);
				invoiceKey.setOrd_no(orderNo + orderSeq);
				invoiceKey.setPrice(price);
				invoiceKey.setSpetkey(spetKey);
				invoiceKey.setSteelcodekey(steelCodeKey);
				invoicek = invoiceKey.toString();
				listArray = new ArrayList<String[]>();

				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)
			{
				java.util.Collections.sort(listseq);

				SlmInvoiceKey invoiceFirst = ( SlmInvoiceKey)sortKey
						.get(listseq.get(0));
				String buyercodetemp = invoiceFirst.getBuyercode();
				String orderNotemp = invoiceFirst.getOrd_no();
				String invoiceNo = slmBalanceInfoOrgDao.getmaxInvoiceNo();
				invoiceNo = this.getNextInvoiceNo(invoiceNo);
				// trans.beginTransaction();
				// fact.setTrans(trans);
				int index = 1;

				for (String sort : listseq)
				{

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

					if (!buyercodetemp.equals(invoice.getBuyercode()))

					{
						invoiceNo = this.getNextInvoiceNo(slmBalanceInfoOrgDao
								.getmaxInvoiceNo());
						index = 1;

					}

					if (index > maxinvoiceNum)
					{
						invoiceNo = this.getNextInvoiceNo(slmBalanceInfoOrgDao
								.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];

						String sqlInvoice = slmBalanceInfoOrgDao
								.makeInvoiceSqlStr(orderNo, orderSeq,
										orderDlvSeq, askPlanId, askPlanIdSeq,
										spetKey, steelCodeKey, invoiceNo, index
												+ "", person);

						String sqlTrs = slmBalanceTransMoneyDao
								.updateTransMoneyInvoiceNoOrgSqlStr(invoiceNo,
										person, askPlanId);

						String sqlOutStock = slmBalanceTransMoneyDao
								.updateOutStockMoneyInvoiceNoOgrSqlStr(
										invoiceNo, person, askPlanId);

						sqlList.add(sqlInvoice);
						sqlList.add(sqlTrs);
						sqlList.add(sqlOutStock);
						cro = super.ExcuteNonQueryIgnoreTrans(sqlList);
					}
					buyercodetemp = invoice.getBuyercode();
					orderNotemp = invoice.getOrd_no();
					index++;
				}
				trans.commit();
				fact.cleantTrans();

			}
			SlmBalanceTickets tick=new SlmBalanceTickets();
			// 单独出库费用处理
			if (listAsk.size() > 0) makeOutStockMoneyInvoiceSignal(listAsk,
					person);
			if(listInvoice.size()>0){
				String message=tick.insertGlInfo(listInvoice,person,"1");
			}
		}

		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 = null;
		}
		return cro;

	}

	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 ("3".equals(sbi.getBalancestatus_org()))
					{
						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
						.updateOutStockMoneyInvoiceNoOgrSqlStr(invoiceNo,
								person, s.toString());
				listSql.add(strtran);

			}

			if (listSql.size() > 0)

			super.ExcuteNonQueryIgnoreTrans(listSql);

			trans.commit();

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

		}

	}

	public CoreReturnObject rollBackInvoiceNo(String invoiceNo, String person)
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		DbTrans trans = new DbTrans();
		SlmBalanceMnySubtDao SlmBalanceMnySubtDao = fact
				.getSlmBalanceMnySubtDao();
		SlmBalanceInfoOrgDao SlmBalanceInfoOrgDao = fact
				.getSlmBalanceInfoOrgDao();
		SlmBalanceTransMoneyDao SlmBalanceTransMoneyDao = fact
				.getSlmBalanceTransMoneyDao();
		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);
			SlmBalanceInfoOrgDao.unmakeInvoice(invoiceNo);
			SlmBalanceTransMoneyDao.rollbackInvoiceNoOrg(invoiceNo, person);
			SlmBalanceMnySubtDao.deleteGlInfo(invoiceNo);
			// SlmBalanceMnySubtDao.rollbackInvoiceNo(invoiceNo);
			trans.commit();
			fact.cleantTrans();
			core.stlmes.server.sale.po.SlmBusinessOpeInfoBean bean = new SlmBusinessOpeInfoBean(
					"退结算单", "", "", "", invoiceNo, person, invoiceNo);
			super.logBusinessInfo(bean);
		}
		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;
		}
		return cro;

	}

	public CoreReturnObject credit(java.util.ArrayList list, String person)
			throws SQLException,Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		DaoFactory fact = new DaoFactory();
		SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = fact
				.getSlmBalanceInfoOrgDao();
		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];
				slmBalanceInfoOrgDao.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 setBalPriceOrg(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();

		java.util.Hashtable hsOrderPrice = new Hashtable();
		java.util.Hashtable<String, SlmOrderHeadTgBean> hsOrderHead = new Hashtable<String, SlmOrderHeadTgBean>();
		String balPrice = "0";

		ArrayList sqlList = new ArrayList();

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

		try
		{
			trans.beginTransaction();
			fact.setTrans(trans);
			core.stlmes.server.sale.dao.SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = fact
					.getSlmBalanceInfoOrgDao();
			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[13];

				try
				{
					ordPk = temp[10];
					ordLnPk = temp[11];
					if (!arrOrdPklist.contains(ordPk))
					{
						List<SlmOrderLineTgBean> listOrder = slmOrderLineTgDao
								.getSlmOrderLineTgByOrdPk(ordPk);
						if (listOrder == null)
						{
						}
						else
						{
							for (SlmOrderLineTgBean line : listOrder)
							{
								if (line.getBalnce_price_org_sts().equals("1"))
								{
									if (!hsOrderPrice.containsKey(ordPk
											+ line.getOrd_ln_pk()))
									{
										hsOrderPrice.put(ordPk
												+ line.getOrd_ln_pk(), line);
									}
								}

							}
						}

						arrOrdPklist.add(ordPk);

					}

					if (!hsOrderHead.containsKey(ordPk))
					{
						SlmOrderHeadTgBean head = slmOrderHeadTgDao
								.getSlmOrderHeadTgByOrdPk(ordPk);
						if (head == null)
						{
						}
						else
						{
							hsOrderHead.put(ordPk, head);

						}

					}

				}
				catch (java.sql.SQLException ex)
				{
					System.out.println(ex.getMessage());

				}
				catch (java.lang.Exception ex)
				{

				}

				if (balancestatus.equals("3")) continue;
				if (balancestatus.equals("2")) continue;
				balPrice = "0";
				boolean isGm = false;
				if (hsOrderPrice.containsKey(ordPk + ordLnPk))
				{
					SlmOrderLineTgBean bean = ( SlmOrderLineTgBean)hsOrderPrice
							.get(ordPk + ordLnPk);

					if (hsOrderHead.containsKey(ordPk))
					{
						SlmOrderHeadTgBean tgbean = hsOrderHead.get(ordPk);

						if ("100102".equals(tgbean.getSale_org()))
						{
							balPrice = bean.getBalnce_price().toString();
							isGm = true;

						}
						else
						{
							balPrice = bean.getOut_price().toString();
							isGm = false;
						}
						if("120504".equals(tgbean.getSupply_unit()))
						{
							balPrice=bean.getBalnce_price_org().toString();	
						}

					}

				}

				double corpPrice = 0.0;

				String sql = "";
				// 国贸的首次结算都是按吨结算
				if (isGm)
				{
					sql = slmBalanceInfoOrgDao.insertBalancePriceSqlStrForGm(
							orderNo, orderSeq, orderDlvSeq, askPlanId,
							askPlanIdSeq, spetKey, steelCodeKey, balPrice,
							person);
				}
				else

				{
					sql = slmBalanceInfoOrgDao.insertBalancePriceSqlStr(
							orderNo, orderSeq, orderDlvSeq, askPlanId,
							askPlanIdSeq, spetKey, steelCodeKey, balPrice,
							person);
				}

				sqlList.add(sql);

				// slmBalanceInfoOrgDao.insertBalancePrice(orderNo, orderSeq,
				// orderDlvSeq, askPlanId, askPlanIdSeq, spetKey,
				// steelCodeKey, balPrice, person);

			}

			cro = super.ExcuteNonQueryIgnoreTrans(sqlList);
			trans.commit();
			return cro;

		}

		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 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 = "";
		core.stlmes.server.sale.dao.SlmBalanceInfoOrgDao slmBalanceInfoOrgDao = null;
		slmBalanceInfoOrgDao = new SlmBalanceInfoOrgDao();
		for (Object s : list)
		{
			temp = ( String[])s;
			invoiceNo = temp[0];
			realInvoiceDate = temp[1];
			sql = slmBalanceInfoOrgDao.updateRealInvoiceDate(invoiceNo,
					realInvoiceDate, person);
			listSql.add(sql);

		}

		if (listSql.size() > 0)
		{
			cro = super.ExcuteNonQueryIgnoreTrans(listSql);
		}
		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 String BalanceStatusOrg_Init = "0";
	private String BalanceStatusOrg_Affrim = "2";
	private String BalanceStatusOrg_Invoice = "3";

}
