package com.cim.idm.mwmsextend.materialpackingstockin.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cim.idm.wmsextend.generic.GenericServiceProxy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.util.xml.JdomUtils;
import com.cim.idm.mwmsextend.generic.util.CommonValidation;
import com.cim.idm.wmsextend.generic.util.SMessageUtil;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;
import com.cim.idm.wmspackage.materialundo.info.MaterialUnDoinfo;
import com.cim.idm.wmspackage.name.NameServiceProxy;

public class MaterialPackingStockInforCXImpl {
	
	private static Log log = LogFactory.getLog(MaterialPackingStockInforCXImpl.class);
	static String factory = System.getProperty("factory");
	
	/**
	 * 
	 * @author: wsq
	 * @Company: ZXY System Inc.
	 * @date: 2021年4月8日
	 * @Description:
	 * @param toerplist
	 * @param eventInfo
	 * @param messageid
	 * @return
	 * @throws Exception
	 */
	public List<MaterialUnDoinfo> generateNormalStockInMessageForCX(
			List<MaterialStockInClass> toerplist, EventInfo eventInfo,String messageid)
			throws Exception {

		List<MaterialUnDoinfo> UnDoList = new ArrayList<MaterialUnDoinfo>();
		List<MaterialPacking> materialPackingList=new ArrayList<MaterialPacking>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String date = df.format(new Date());
		//声明虚拟过账返回凭证信息List
		List<MaterialUnDoinfo> NGUnDoList = new ArrayList<MaterialUnDoinfo>();
		Date uodiddate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		try {
			String Interface="";
			if(factory.equals("B5"))
				Interface= GenericServiceProxy.getConstantMap().B5_POCount;
			else 
				Interface=GenericServiceProxy.getConstantMap().New_POCount;			
			//Document doc = SMessageUtil.createERPXmlDocument(Interface, ""); // 信息名INT_WMS11自己写着玩，具体名字要看ERP怎么设定
			Document doc = SMessageUtil.createERPXmlDocumentForBadt();
			//String nodePath = "//Message/Body";
			String nodePath = "IV_REQUEST";
			Element parentElement = JdomUtils.getNode(doc, nodePath);
			CommonValidation cv = new CommonValidation();
			for (MaterialStockInClass ms : toerplist) {
				//获取BOX集合信息， Add by WH 2017/6/9
				List<MaterialPacking> mplist = new ArrayList<MaterialPacking>();
				if (ms.getPreGroupName() == null
						|| ms.getPreGroupName().equals("")){
					// 20180406 10091872 香港1900平台判断
					if(ms.getErpfactory().equals("1900")){  	
						mplist = new MaterialPackingStockInUtil()
						.GetOQADetailGroupList1ForHK(ms);
					}else{
						mplist = new MaterialPackingStockInUtil()
						.GetOQADetailGroupList1(ms);
					}
				} else {

					if(ms.getErpfactory().equals("1900")){  	
						mplist = new MaterialPackingStockInUtil()
						.GetOQADetailGroupList2ForHK(ms);
					}else{
						mplist = new MaterialPackingStockInUtil()
						.GetOQADetailGroupList2(ms);
					}
				}
				
				String sitename = ms.getSiteName();
				String rrn = ms.getReceiveRequestName();
				String rrdn = ms.getReceiveRequestDetailName();
				String ran = ms.getReceiveActNo();
				String mgn = ms.getGroupName();
				String mkd = "";
				if (ms.getMakedate() != null) {
					mkd = ms.getMakedate().toString();
				}
				String pmgn = "";
				if (ms.getPreGroupName() != null) {
					pmgn = ms.getPreGroupName();
				}

				String rq = ms.getReceiveQuantity();
				String at = ms.getActtype();
				String cd = ms.getCustomdate();
				String cf = ms.getCustomform();
				String cfs = ms.getCustomformstate();
				String erplocation = ms.getErplocation();
				String msn = ms.getMaterialSpecName();
				// String mt = ms.getMaterialtype();
				String rt = ms.getReceiveTime();
				
				rt = cv.getDate(rt);
				String tax = ms.getTaxid();
				String tf = ms.getTransportformno();
				int seqno = ms.getSeqno();

				//Element childElemrnt = new Element("item");
				Element childElemrnt = new Element("BKPF");
				parentElement.addContent(childElemrnt);
				SMessageUtil
						.createElement(nodePath, childElemrnt, "EBELN", rrn);
				SMessageUtil.createElement(nodePath, childElemrnt, "EBELP",
						rrdn);
				SMessageUtil.createElement(nodePath, childElemrnt, "POSNR", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "BWART",
						"101");
				SMessageUtil.createElement(nodePath, childElemrnt, "BLDAT", date);
				SMessageUtil.createElement(nodePath, childElemrnt, "BUDAT", rt);
				SMessageUtil
						.createElement(nodePath, childElemrnt, "BKTXT", ran);
				String str = "";
				if (cv.GetPOType(sitename,rrn).equals("Z")
						|| cv.GetPOType(sitename,rrn).equals("E")) {
					str = cf;

				} else {
					str = "";
				}
				if(factory.equals("B1")&&(cv.GetPOType(sitename,rrn).equals("Z")))
				{
						SMessageUtil.createElement(nodePath, childElemrnt, "XBLNR", tax);
						SMessageUtil.createElement(nodePath, childElemrnt, "FRBNR", str);
						SMessageUtil.createElement(nodePath, childElemrnt, "XABLN", "");
				}
				else
				{
						SMessageUtil.createElement(nodePath, childElemrnt, "XBLNR", str);
						if(tf.length() > 16)
						{
								tf = tf.substring(0, 16);
						}
						SMessageUtil.createElement(nodePath, childElemrnt, "FRBNR", tf);
						if(tax.length() > 10)
						{
								tax = tax.substring(tax.length() - 10, tax.length());
						}
								SMessageUtil.createElement(nodePath, childElemrnt, "XABLN", tax);
				}
				
				SMessageUtil.createElement(nodePath, childElemrnt, "MATNR", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "MEINS", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "MENGE", rq);
				SMessageUtil.createElement(nodePath, childElemrnt, "WERKS",
						ms.getErpfactory());//
				SMessageUtil.createElement(nodePath, childElemrnt, "LGORT",
						erplocation);// erplocation
				SMessageUtil.createElement(nodePath, childElemrnt, "CHARG",
						ms.getCharge());// mgn ms.getCharge()
				SMessageUtil
						.createElement(nodePath, childElemrnt, "HSDAT", mkd);// mkd
				SMessageUtil.createElement(nodePath, childElemrnt, "VFDAT", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "GRUND", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "INSMK",
						ms.getIcqflag() == null ? "" : ms.getIcqflag());
				SMessageUtil.createElement(nodePath, childElemrnt, "MBLNR", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "MJAHR", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "ZEILE", "");
				SMessageUtil.createElement(nodePath, childElemrnt, "PRUEFLOS",
						"");// ?
				//ERPSTATE状态为ERPSUCCESS，则为ERP过账成功，MWSM处理失败，需要将messageid+undodetailname作为seq号发给ERP,否则将Messageid+流水号作为SEQ号   Add by wh 2017/6/9
				SMessageUtil.createElement(nodePath, childElemrnt, "SEQ",mplist.get(0).getErpState().equalsIgnoreCase("ERPSUCCESS")?mplist.get(0).getMessageId()+mplist.get(0).getUnDoDetailName():
						messageid+Integer.toString(seqno));
				if(mplist.get(0).getErpState().equalsIgnoreCase("ERPSUCCESS"))
				{
					ms.setSeqno(Integer.parseInt( mplist.get(0).getUnDoDetailName()));
				}
				SMessageUtil.createElement(nodePath, childElemrnt, "FLAG", "1");					
				if(!mplist.get(0).getErpState().equalsIgnoreCase("ERPSUCCESS"))
				{
					for(MaterialPacking mp : mplist)
					{						
						mp.setUnDoDetailName(Integer.toString(seqno));
						mp.setMessageId(messageid);
						mp.setErpState("ERPSUCCESS");
						mp.setLastEventName(eventInfo.getEventName()+"+TB");
						materialPackingList.add(mp);
					}
					/*MaterialPackingServiceProxy.getMaterialPackingService().update(mplist);*/
				}
				//虚拟过账失败UndoInfo
				MaterialUnDoinfo NGUndoInfo = new MaterialUnDoinfo();
				NGUndoInfo.setUnDoID(messageid);
				NGUndoInfo.setOqaname("NGOQA");//非PO过账不需要此字段
				NGUndoInfo.setUnDoDetailName(mplist.get(0).getErpState().equalsIgnoreCase("ERPSUCCESS")?mplist.get(0).getMessageId()+mplist.get(0).getUnDoDetailName():
					messageid+Integer.toString(seqno));
				NGUndoInfo.setSeq(mplist.get(0).getErpState().equalsIgnoreCase("ERPSUCCESS")?mplist.get(0).getMessageId()+mplist.get(0).getUnDoDetailName():
					messageid+Integer.toString(seqno));
				NGUndoInfo.setUnDoIdDate( sdf.format(uodiddate));
				NGUnDoList.add(NGUndoInfo);
			}

			UnDoList = NGUnDoList;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.getMessage();
			throw e;
		}
		return UnDoList;

	}
	
	/**
	 * 
	 * @author: wsq
	 * @Company: ZXY System Inc.
	 * @date: 2021年4月8日
	 * @Description:
	 * @param msilist
	 * @param num
	 * @return
	 * @throws ParseException
	 */
	public List<MaterialStockInClass> GetToERPList(
			List<MaterialStockInClass> msilist, int num) throws ParseException {
		List<MaterialStockInClass> toerplist = new ArrayList<MaterialStockInClass>();
		int seqno = 1;
		CommonValidation commonVal = new CommonValidation();
		String sql2 = "SELECT MAX(UNDODETAILNAME) AS SEQ FROM materialpacking "
				+ "WHERE sitename = :sitename and receiveActNo=:receiveActNo and erpstate='ERPSUCCESS' ";

		Map<String, Object> bindMap3 = new HashMap<String, Object>();
		bindMap3.put("sitename", msilist.get(0).getSiteName());
		bindMap3.put("receiveActNo", msilist.get(0).getReceiveActNo());

		List<Map<String, Object>> queryResult2 = IDMFrameServiceProxy
				.getSqlTemplate().queryForList(sql2, bindMap3);
		if(!(queryResult2.size()<=0||queryResult2.get(0).get("SEQ")==null||queryResult2.get(0).get("SEQ").toString().equals("")))
		{
			seqno=Integer.parseInt(queryResult2.get(0).get("SEQ").toString())+seqno;
		}
		for (MaterialStockInClass ms : msilist) {

			// 查询该物料是否根据ERP批次进行管理
			String ERPChargeFlag = "N";
			ERPChargeFlag = commonVal.getMaterialSpecCharge(ms.getErpfactory(),
					ms.getMaterialSpecName());
			if (ms.getOqaflag().equals("Y")) {
				List<MaterialStockInClass> oqalist = GetOQAGroupList(ms,
						ms.getMaterialOQAnum(), seqno);
				String makedate = "";
				String charge = ms.getCharge();

				for (MaterialStockInClass oqa : oqalist) {

					oqa.setCustomdate(ms.getCustomdate());
					oqa.setCustomform(ms.getCustomform());
					oqa.setReceiveTime(ms.getReceiveTime());
					oqa.setCustomformstate(ms.getCustomformstate());
					oqa.setMaterialOQAnum(ms.getMaterialOQAnum());
					oqa.setTaxid(ms.getTaxid());
					oqa.setTransportformno(ms.getTransportformno());
					oqa.setErplocation(ms.getErplocation());
					oqa.setErpfactory(ms.getErpfactory());
					oqa.setMaterialSpecName(ms.getMaterialSpecName());
					oqa.setMaterialtype(ms.getMaterialtype());
					oqa.setSiteName(ms.getSiteName());
					oqa.setIcqflag(ms.getIcqflag());
					oqa.setCharge(charge);
					/**
					 * 根据ERP分批标准来进行，不再需要根据接收时间创建批次
					 * 用BS_MATERIALSPECCHARGEFLAG表中数据来确定是否需要进行批次管理；如果不需要进行批次管理的，
					 * 那么就不用在入库的时候创建批次 ，不用在退库的时候选择批次；还有一个问题，是PO的行项目中如果有charge的话，
					 * 那么在入库的时候就不用生成批次。
					 */
					if (ERPChargeFlag.equals("Y") && ms.getCharge().equals("")) {
						if (!oqa.getMakedate().equals(makedate)) {
							List<String> sqldata = new ArrayList<String>();
							sqldata.add(oqa.getErpfactory());
							String spec = oqa.getMaterialSpecName()
									+ "************";
							sqldata.add(spec.substring(0, 10));
							String str = oqa.getMakedate().substring(0, 4)
									+ oqa.getMakedate().substring(5, 7)
									+ oqa.getMakedate().substring(8);
							String time = str + "00";
							sqldata.add(time.substring(2, 8));
							charge = NameServiceProxy.getNameRuleDefService()
									.generateName("ChargeNo", sqldata, 1)
									.get(0);
							
							oqa.setCharge(charge.substring(14));
							makedate = oqa.getMakedate();
						} else {
							oqa.setCharge(charge.substring(14));
						}
					}

				}
				toerplist.addAll(oqalist);
				seqno = seqno + oqalist.size();
			} else {

				List<MaterialStockInClass> oqalist = new MaterialPackingStockInUtil()
						.GetOQAMakedateGroupList(ms.getReceiveRequestName(),
								ms.getReceiveRequestDetailName(),
								ms.getReceiveActNo(), ms.getSiteName());
				String makedate = "";
				String charge = ms.getCharge();
				for (MaterialStockInClass oqa : oqalist) {
					oqa.setCustomdate(ms.getCustomdate());
					oqa.setCustomform(ms.getCustomform());
					oqa.setReceiveTime(ms.getReceiveTime());
					oqa.setCustomformstate(ms.getCustomformstate());
					oqa.setMaterialOQAnum(ms.getMaterialOQAnum());
					oqa.setTaxid(ms.getTaxid());
					oqa.setTransportformno(ms.getTransportformno());
					oqa.setErplocation(ms.getErplocation());
					oqa.setErpfactory(ms.getErpfactory());
					oqa.setMaterialSpecName(ms.getMaterialSpecName());
					oqa.setMaterialtype(ms.getMaterialtype());
					oqa.setSiteName(ms.getSiteName());
					oqa.setSeqno(seqno++);
					oqa.setIcqflag(ms.getIcqflag());
					oqa.setCharge(charge);
					if (ERPChargeFlag.equals("Y") && ms.getCharge().equals("")) {
						if (!oqa.getMakedate().equals(makedate)) {
							List<String> sqldata = new ArrayList<String>();
							sqldata.add(oqa.getErpfactory());
							String spec = oqa.getMaterialSpecName()
									+ "************";
							sqldata.add(spec.substring(0, 10));
							String str = oqa.getMakedate().substring(0, 4)
									+ oqa.getMakedate().substring(5, 7)
									+ oqa.getMakedate().substring(8);
							
							String time = str + "00";
							sqldata.add(time.substring(2, 8));
							charge = NameServiceProxy.getNameRuleDefService()
									.generateName("ChargeNo", sqldata, 1)
									.get(0);
							oqa.setCharge(charge.substring(14));
							
							makedate = oqa.getMakedate();
						} else {
							oqa.setCharge(charge.substring(14));
						}
					}

				}
				toerplist.addAll(oqalist);
			}

		}

		return toerplist;
	}
	
	/**
	 * 
	 * @author: wsq
	 * @Company: ZXY System Inc.
	 * @date: 2021年4月8日
	 * @Description:
	 * @param msi
	 * @param num
	 * @param seqno
	 * @return
	 * @throws ParseException
	 */
	public List<MaterialStockInClass> GetOQAGroupList(MaterialStockInClass msi,
			double num, int seqno) throws ParseException {

		// 这里查到的 MaterialStockInClass 的列表要得到所有信息，这个要重新看看怎么写SQL
		List<MaterialStockInClass> oqalist = new MaterialPackingStockInUtil()
				.GetOQAMakedateGroupList(msi.getReceiveRequestName(),
						msi.getReceiveRequestDetailName(),
						msi.getReceiveActNo(), msi.getSiteName());
		List<MaterialStockInClass> oplist = new ArrayList<MaterialStockInClass>();
		// List <MaterialPacking> mp
		for (MaterialStockInClass ms : oqalist) {
			List<MaterialStockInClass> testlist = GroupOQAList2(ms, num, seqno);
			seqno = seqno + testlist.size();// 这里的SEQ号也要随着list变化累加，因为传过来和传过去的是形参，GroupOQAList2方法中SEQ变化后不会反映到这个function中，所以要手动累加一下
			oplist.addAll(testlist);
		}

		return oplist;
	}
	
	/**
	 * 
	 * @author: wsq
	 * @Company: ZXY System Inc.
	 * @date: 2021年4月8日
	 * @Description:
	 * @param ms
	 * @param num
	 * @param seqno
	 * @return
	 */
	public List<MaterialStockInClass> GroupOQAList2(MaterialStockInClass ms,
													double num, int seqno) {

		List<MaterialPacking> mplist = new MaterialPackingStockInUtil()
				.GetOQADetailGroupList(ms);
		List<MaterialStockInClass> result = new ArrayList<MaterialStockInClass>();

		int first = 0;
		 DecimalFormat df=new DecimalFormat("#.##");
		int end = mplist.size() - 1;// int groupname = seqno;
		if (mplist.get(first).getMaterialQuantity() >= num) {
			for (MaterialPacking p : mplist) {

				MaterialStockInClass mg = new MaterialStockInClass();// ms;
				mg.setCustomdate(ms.getCustomdate());
				mg.setCustomform(ms.getCustomform());
				mg.setReceiveTime(ms.getReceiveTime());
				mg.setCustomformstate(ms.getCustomformstate());
				mg.setMaterialOQAnum(ms.getMaterialOQAnum());
				mg.setTaxid(ms.getTaxid());
				mg.setTransportformno(ms.getTransportformno());
				mg.setErplocation(ms.getErplocation());
				mg.setErpfactory(ms.getErpfactory());
				mg.setMaterialSpecName(ms.getMaterialSpecName());
				mg.setMaterialtype(ms.getMaterialtype());
				mg.setSeqno(seqno);
				mg.setReceiveActNo(ms.getReceiveActNo());
				mg.setReceiveRequestDetailName(ms.getReceiveRequestDetailName());
				mg.setReceiveRequestName(ms.getReceiveRequestName());
				mg.setPreGroupName(Integer.toString(seqno));
				mg.setMakedate(ms.getMakedate());
				mg.setSiteName(ms.getSiteName());
				mg.setIcqflag(ms.getIcqflag());
				mg.setShipRequestDetailType(ms.getShipRequestDetailType());
				mg.setReceiveQuantity(df.format((p.getMaterialQuantity())));
				p.setMaterialProcessGroupName(Integer.toString(seqno));
				result.add(mg);
				seqno++;
			}
			MaterialPackingServiceProxy.getMaterialPackingService().update(
					mplist);
		} else if (mplist.get(first).getMaterialQuantity() < num
				&& mplist.get(end).getMaterialQuantity() >= num) {
			// mplist.get(first).getMaterialQuantity()<num &&
			// mplist.get(end).getMaterialQuantity()>num-mplist.get(first).getMaterialQuantity()
			for (; end > first && mplist.get(end).getMaterialQuantity() >= num; end--) {
				MaterialStockInClass mg = new MaterialStockInClass();// ms;
				mg.setCustomdate(ms.getCustomdate());
				mg.setCustomform(ms.getCustomform());
				mg.setReceiveTime(ms.getReceiveTime());
				mg.setCustomformstate(ms.getCustomformstate());
				mg.setMaterialOQAnum(ms.getMaterialOQAnum());
				mg.setTaxid(ms.getTaxid());
				mg.setTransportformno(ms.getTransportformno());
				mg.setErplocation(ms.getErplocation());
				mg.setErpfactory(ms.getErpfactory());
				mg.setMaterialSpecName(ms.getMaterialSpecName());
				mg.setMaterialtype(ms.getMaterialtype());
				mg.setSeqno(seqno);
				mg.setReceiveActNo(ms.getReceiveActNo());
				mg.setReceiveRequestDetailName(ms.getReceiveRequestDetailName());
				mg.setReceiveRequestName(ms.getReceiveRequestName());
				mg.setPreGroupName(Integer.toString(seqno));
				mg.setMakedate(ms.getMakedate());
				mg.setSiteName(ms.getSiteName());
				mg.setIcqflag(ms.getIcqflag());
				mg.setShipRequestDetailType(ms.getShipRequestDetailType());
				mg.setReceiveQuantity(df.format((mplist.get(end)
						.getMaterialQuantity())));
				mplist.get(end).setMaterialProcessGroupName(
						Integer.toString(seqno));
				result.add(mg);
				seqno++;
			}
			// MaterialPackingServiceProxy.getMaterialPackingService().update(mplist);
			result.addAll(GroupOQA(mplist, ms, first, end, seqno, num));

		} else {
			result.addAll(GroupOQA(mplist, ms, first, end, seqno, num));
		}
		return result;
	}
	
	/**
	 * 
	 * @author: wsq
	 * @Company: ZXY System Inc.
	 * @date: 2021年4月8日
	 * @Description:
	 * @param mp
	 * @param ms
	 * @param first
	 * @param end
	 * @param seqno
	 * @param num
	 * @return
	 */
	public List<MaterialStockInClass> GroupOQA(List<MaterialPacking> mp,
			MaterialStockInClass ms, int first, int end, int seqno, double num) {

		List<MaterialStockInClass> result2 = new ArrayList<MaterialStockInClass>();
		DecimalFormat df=new DecimalFormat("#.##");
		Double sum = (double) 0.0;
		int count = 0;
		while (first <= end) {// 死循环了

			if (sum < num) {
				double nu = num - sum;
				double nu1 = mp.get(end).getMaterialQuantity();
				if (num - sum >= mp.get(end).getMaterialQuantity()) {
					sum = sum + mp.get(end).getMaterialQuantity();
					mp.get(end--).setMaterialProcessGroupName(
							Integer.toString(seqno));

					count++;
				} else if (num - sum >= mp.get(first).getMaterialQuantity()) {
					sum = sum + mp.get(first).getMaterialQuantity();
					mp.get(first++).setMaterialProcessGroupName(
							Integer.toString(seqno));
					count++;
				} else {
					MaterialStockInClass mg = new MaterialStockInClass();// ms;
					mg.setCustomdate(ms.getCustomdate());
					mg.setCustomform(ms.getCustomform());
					mg.setReceiveTime(ms.getReceiveTime());
					mg.setCustomformstate(ms.getCustomformstate());
					mg.setMaterialOQAnum(ms.getMaterialOQAnum());
					mg.setTaxid(ms.getTaxid());
					mg.setTransportformno(ms.getTransportformno());
					mg.setErplocation(ms.getErplocation());
					mg.setErpfactory(ms.getErpfactory());
					mg.setMaterialSpecName(ms.getMaterialSpecName());
					mg.setMaterialtype(ms.getMaterialtype());
					mg.setSiteName(ms.getSiteName());
					mg.setSeqno(seqno);
					mg.setIcqflag(ms.getIcqflag());
					mg.setReceiveActNo(ms.getReceiveActNo());
					mg.setReceiveRequestDetailName(ms
							.getReceiveRequestDetailName());
					mg.setReceiveRequestName(ms.getReceiveRequestName());
					mg.setMakedate(ms.getMakedate());
					mg.setShipRequestDetailType(ms.getShipRequestDetailType());
					mg.setPreGroupName(Integer.toString(seqno));
					mg.setSeqno(seqno);
					mg.setReceiveQuantity(sum.toString());
					result2.add(mg);
					seqno++;
					count = 0;
					sum = (double) 0;
				}
				if (first > end) {// youwenti meiyou =
					MaterialStockInClass mg = new MaterialStockInClass();// ms;
					mg.setCustomdate(ms.getCustomdate());
					mg.setCustomform(ms.getCustomform());
					mg.setReceiveTime(ms.getReceiveTime());
					mg.setCustomformstate(ms.getCustomformstate());
					mg.setMaterialOQAnum(ms.getMaterialOQAnum());
					mg.setTaxid(ms.getTaxid());
					mg.setSiteName(ms.getSiteName());
					mg.setTransportformno(ms.getTransportformno());
					mg.setErplocation(ms.getErplocation());
					mg.setErpfactory(ms.getErpfactory());
					mg.setMaterialSpecName(ms.getMaterialSpecName());
					mg.setMaterialtype(ms.getMaterialtype());
					mg.setSeqno(seqno);
					mg.setIcqflag(ms.getIcqflag());
					mg.setReceiveActNo(ms.getReceiveActNo());
					mg.setReceiveRequestDetailName(ms
							.getReceiveRequestDetailName());
					mg.setReceiveRequestName(ms.getReceiveRequestName());
					mg.setMakedate(ms.getMakedate());
					mg.setShipRequestDetailType(ms.getShipRequestDetailType());
					mg.setPreGroupName(Integer.toString(seqno));
					mg.setSeqno(seqno);
					mg.setReceiveQuantity(sum.toString());
					result2.add(mg);
					seqno++;
					count = 0;
					sum = (double) 0;
				}

			} else {
				MaterialStockInClass mg = new MaterialStockInClass();// ms;
				mg.setCustomdate(ms.getCustomdate());
				mg.setCustomform(ms.getCustomform());
				mg.setReceiveTime(ms.getReceiveTime());
				mg.setCustomformstate(ms.getCustomformstate());
				mg.setMaterialOQAnum(ms.getMaterialOQAnum());
				mg.setTaxid(ms.getTaxid());
				mg.setTransportformno(ms.getTransportformno());
				mg.setErplocation(ms.getErplocation());
				mg.setErpfactory(ms.getErpfactory());
				mg.setMaterialSpecName(ms.getMaterialSpecName());
				mg.setMaterialtype(ms.getMaterialtype());
				mg.setSiteName(ms.getSiteName());
				mg.setSeqno(seqno);
				mg.setIcqflag(ms.getIcqflag());
				mg.setReceiveActNo(ms.getReceiveActNo());
				mg.setReceiveRequestDetailName(ms.getReceiveRequestDetailName());
				mg.setReceiveRequestName(ms.getReceiveRequestName());
				mg.setMakedate(ms.getMakedate());
				mg.setShipRequestDetailType(ms.getShipRequestDetailType());
				mg.setPreGroupName(Integer.toString(seqno));
				mg.setSeqno(seqno);
				mg.setReceiveQuantity(sum.toString());
				result2.add(mg);
				seqno++;
				count = 0;
				sum = (double) 0;
			}
		}

		MaterialPackingServiceProxy.getMaterialPackingService().update(mp);
		return result2;
	}
	
	/**
	 * 
	 * @author: wsq
	 * @Company: ZXY System Inc.
	 * @date: 2021年4月8日
	 * @Description:
	 * @param document
	 * @return
	 * @throws Exception
	 */
	public List<MaterialStockInClass> GetNormalStockInList(
			org.jdom.Document document) throws Exception {

		List<MaterialStockInClass> mplist = new ArrayList<MaterialStockInClass>();
		Element root = document.getDocument().getRootElement();
		Element bodyElement = root.getChild("Body");
		Element requestlist = bodyElement.getChild("DATALIST");

		try {
			if (requestlist != null) {
				for (Iterator<?> iterator = requestlist.getChildren()
						.iterator(); iterator.hasNext();) {
					Element element = (Element) iterator.next();
					String sitename = element.getChild("SITENAME").getText();
					String erpfactory = element.getChildText("ERPFACTORYNAME");
					String materialSpecName = element.getChild(
							"MATERIALSPECNAME").getText();
					String receiveRequestName = element.getChild(
							"RECEIVEREQUESTNAME").getText();
					String receiveRequestDetailName = element.getChild(
							"RECEIVEREQUESTDETAILNAME").getText();
					String receiveActNo = element.getChild("RECEIVEACTNO")
							.getText();
					String erplocation = element.getChild("ERPLOCATION")
							.getText();
					double materialOQAnum = 0;
					if (element.getChild("MATERIALOQANUM").getText() != null) {
						materialOQAnum = Double.valueOf(element.getChild(
								"MATERIALOQANUM").getText());
					} else {
						materialOQAnum = 0;
					}
					CommonValidation cv = new CommonValidation();
					String receiveTime = cv.getDate(element.getChild(
							"RECEIVETIME").getText());
					String erptime = "";
					if (new CommonValidation().checkTime(receiveTime)) {
						erptime = receiveTime;
					} else {
						throw (new Exception(
								"Server StockInDate more than system Date !"));
					}
					String receiveQuantity = element
							.getChild("RECEIVEQUANTITY").getText();
					String acttype = element.getChild("ACTTYPE").getText();
					String customform = element.getChild("CUSTOMFORM")
							.getText();
					String customstate = element.getChild("CUSTOMSTATE")
							.getText();
					// String customdate =
					// element.getChild("CUSTOMDATE").getText();
					String transportformno = element
							.getChild("TRANSPORTFORMNO").getText();
					String taxid = element.getChild("TAXID").getText();
					String icqflag = element.getChild("IQCFLAG") == null ? ""
							: element.getChild("IQCFLAG").getText();
					String oqaflag = element.getChild("OQAFLAG").getText();
					String charge = element.getChild("CHARGE").getText();
					String remark = element.getChild("REMARK").getText();
					// String inspectp = element.getChild("INSPECTP").getText();
					String suppliername = element.getChild("SUPPLIERNAME").getText();//B2新增

					MaterialStockInClass msc = new MaterialStockInClass();
					if(factory.equals("B2")) msc.setSuppliername(suppliername);//B2新增
					msc.setErplocation(erplocation);
					msc.setMaterialSpecName(materialSpecName);
					msc.setErpfactory(erpfactory);
					msc.setReceiveActNo(receiveActNo);
					msc.setReceiveQuantity(receiveQuantity);
					msc.setMaterialOQAnum(materialOQAnum);
					msc.setReceiveRequestDetailName(receiveRequestDetailName);
					msc.setReceiveRequestName(receiveRequestName);
					msc.setReceiveTime(erptime);
					msc.setSiteName(sitename);
					msc.setActtype(acttype);
					// msc.setCustomdate(customdate);
					msc.setCustomform(customform);
					msc.setCustomformstate(customstate);
					msc.setTaxid(taxid);
					msc.setOqaflag(oqaflag);
					msc.setCharge(charge);
					msc.setIcqflag(icqflag);
					msc.setTransportformno(transportformno);
					msc.setRemark(remark);
					mplist.add(msc);
				}
			}

			
		} catch (Exception e) {
			e.getMessage();
			throw e;
		}
		return mplist;
	}

}
