<%@ page contentType="text/html; charset=gbk" language="java" %>
<%@ page import="mediastore.web.global.*"%>
<%@ page import="mediastore.web.form.*"%>
<%@ page import="java.util.*"%>

<body onload="document.form1.goodsid.focus()">
<script language="JavaScript">
//去掉空格
function Trim(str){
 if(str.charAt(0) == " "){
  str = str.slice(1);
  str = Trim(str); 
 }
 return str;
}

//判断是否是空
function isEmpty(pObj,errMsg){
 var obj = eval(pObj);
 if( obj == null || Trim(obj.value) == ""){
  if (errMsg == null || errMsg =="")
   alert("输入为空");
  else
   alert(errMsg); 
  obj.focus(); 
  return false;
 }
 return true;
}

//判断是否是整数
function isNumber(pObj,errMsg){
 var obj = eval(pObj);
 strRef = "1234567890";
 if(!isEmpty(pObj,errMsg))return false;
 for (i=0;i<obj.value.length;i++) {
  tempChar= obj.value.substring(i,i+1);
  if (strRef.indexOf(tempChar,0)==-1) {
   if (errMsg == null || errMsg =="")
    alert("数据不符合要求，请检查");
   else
    alert(errMsg);
   if(obj.type=="text") 
    obj.focus(); 
   return false; 
  }
 }
 return true;
}
//判断是否是小数
function isFloat(pObj,errMsg){
 var obj = eval(pObj);
 strRef = "1234567890.";
 if(!isEmpty(pObj,errMsg))return false;
 for (i=0;i<obj.value.length;i++) {
  tempChar= obj.value.substring(i,i+1);
  if (strRef.indexOf(tempChar,0)==-1) {
   if (errMsg == null || errMsg =="")
    alert("数据不符合要求，请检查");
   else
    alert(errMsg);
   if(obj.type=="text") 
    obj.focus(); 
   return false; 
  }
 }
 return true;
}


function verify(form){

	if (Trim(form.goodsid.value) == "" | form.goodsid.value == null) 
	{ 
		alert("请输入货物标识"); 
		form.goodsid.value=""
		form.goodsid.focus(); 
		return (false); 
	} 
}


function updateExportAmount(goodsid, newexportamount, repertoryamount)
{
	//用于数据校验的临时对象
	document.form2.obj4numbercheck.value = newexportamount;

	if(Number(newexportamount) > Number(repertoryamount))
	{
		alert("售货数量不可大于库存数量");
		return '1';
	}
	else if(isNumber(document.form2.obj4numbercheck)==false)
	{}
	else if(document.form2.obj4numbercheck.value == null || document.form2.obj4numbercheck.value == '')
	{
		alert("售货数量不可为空");
	}
	else
	{
		document.form2.action = "exportGoodsBillUdtEAmount.do?updategoodsid="+goodsid+"&newexportamount="+newexportamount+"&";
		document.form2.submit();
	}
}

function deleteATempExportGoods(goodsid)
{
	if(confirm("确定要删除该货物吗？")) 
	{
		document.form2.action = "exportGoodsBillGoodsDelete.do?deletegoodsid="+goodsid+"&";
		document.form2.submit();
	}
}


function updateMemberId(newmemberid)
{
	//用于数据校验的临时对象
	if(newmemberid==null||newmemberid=="")
	{
		document.form2.obj4numbercheck.value = 0;
	}
	else
	{
		document.form2.obj4numbercheck.value = newmemberid;
	}

	if(isNumber(document.form2.obj4numbercheck)==false)
	{}
	else if(document.form2.obj4numbercheck.value == null || document.form2.obj4numbercheck.value == '')
	{
		document.form2.action = "exportGoodsBillUptMembId.do?newmemberid=0&";
		document.form2.submit();
	}
	else
	{
		document.form2.action = "exportGoodsBillUptMembId.do?newmemberid="+newmemberid+"&";
		document.form2.submit();
	}
}









function deleteAllGoods()
{
	try{
		var goods_num = document.form2.goodsid.length;
		if(goods_num == null || document.form2.goodsid == null)
		{//
			//alert("空列表");
		}
	}catch(e)
	{
		alert("货物列表目前为空");
		document.form1.goodsid.focus();
		return;
	}
	
	if(confirm("确定要清空该货物列表吗？")) 
	{
		document.form2.action = "exportGoodsBillGoodsDelAll.do";
		document.form2.submit();
	}
}



function submitAllGoods()
{
	//查看该单是否是空列表，空列表就不提交
	try{
		var goods_num = document.form2.goodsid.length;
		if(goods_num == null || document.form2.goodsid == null)
		{//
			//alert("空列表");
		}
	}catch(e)
	{
		alert("请添加货物后再提交售货单");
		document.form1.goodsid.focus();
		return;
	}
	
	//检查数量和单价是否都为数字
	var errMsg;
	
	var amount_num;
	try{
		amount_num = document.form2.exportamount.length;
	}catch(e)
	{
		alert("该售货单中不存在有库存货物");
		document.form1.goodsid.focus();
		return;
	}
	
	document.form2.obj4numbercheck.value = document.form2.memberid.value;
	if(document.form2.memberid.value==""||document.form2.memberid.value==null)
	{
		document.form2.obj4numbercheck.value = 0;
	}

	//amount_num有库存货物的数量，amount_num>0
	if(amount_num == null)
	{//只有一项
		var isNumberFlage = 'yes';//默认为“是数字”
		
		if(isNumber(document.form2.exportamount)==false)
		{
			isNumberFlage = 'no';
		}
		
		if(isNumber(document.form2.obj4numbercheck)==false)
		{
			isNumberFlage = 'no';
		}
		
		if(isNumberFlage=='yes')
		{//数字
			if(confirm("确定要提交该售货单吗？")) 
			{
				if(document.form2.memberid.value==""||document.form2.memberid.value==null)
				{
					document.form2.memberid.value = 0;
				}
				document.form2.action = "exportGoodsBillGoodsSubmit.do";
				document.form2.submit();
			}
		}
		else
		{//非数字
			document.form2.totaldisprice.value = "请修改售货数量或会员标识";
			document.form2.totaldisprice_value.value = "请修改售货数量或会员标识";
			document.form2.totalprice.value = "请修改售货数量或会员标识";
			document.form2.totalprice_value.value = "请修改售货数量或会员标识";
		}
	}
	else
	{//两项以上
	
		var isNumberFlage = 'yes';//默认为“是数字”
		for(iii=0; iii<amount_num; iii++)
		{
			if(isNumber(document.form2.exportamount[iii])==false)
			{
				isNumberFlage = 'no';
			}
		}
		
		if(isNumber(document.form2.obj4numbercheck)==false)
		{
			isNumberFlage = 'no';
		}

		if(isNumberFlage=='yes')
		{//数字
			if(confirm("确定要提交该售货单吗？")) 
			{
				if(document.form2.memberid.value==""||document.form2.memberid.value==null)
				{
					document.form2.memberid.value = 0;
				}
				document.form2.action = "exportGoodsBillGoodsSubmit.do";
				document.form2.submit();
			}
		}
		else
		{//非数字
			document.form2.totaldisprice.value = "请修改售货数量或会员标识";
			document.form2.totaldisprice_value.value = "请修改售货数量或会员标识";
			document.form2.totalprice.value = "请修改售货数量或会员标识";
			document.form2.totalprice_value.value = "请修改售货数量或会员标识";
		}
	}
}

function showNewBill()
{
	document.form2.action = "exportGoodsBillShowNew.do";
	document.form2.submit();
}


</script>


<table width="768" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> 
    <td width="5" rowspan="4" class="trtable"></td>
    <td width="6" bgcolor="#214984" height="1"></td>
    <td rowspan="4" bgcolor="#214984" width="1" height="1"></td>
    <td bgcolor="#214984" class="10p" colspan="2" height="1"></td>
    <td rowspan="4" width="1" bgcolor="#214984" height="1"></td>
    <td bgcolor="#214984" width="5" height="1"></td>
    <td width="5" rowspan="4" class="trtable"></td>
  </tr>
  <tr> 
    <td width="6"></td>
    <td width="379" height="20" class="mainhead">售货单录入</td>
    <td class="mainhead" width="379" height="20">&nbsp;</td>
    <td width="5"></td>
  </tr>
<tr> 
    <td width="6"></td>
    <td valign="top" colspan="2"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        
        <tr> 
          <td height="300" align="center" valign="top">   
          
<%
	ExportGoodsBillFB egbfb = (ExportGoodsBillFB)request.getAttribute(Globals.REQUEST_EXPORTGOODSBILL);
	int maxBillId = egbfb.getMaxBillId();
	List goodsList = egbfb.getGoodsList();
	float discount = egbfb.getDiscount();
%>

    	<form name="form1" method="post" action="exportGoodsBillGoodsInsert.do" onSubmit="javascript:return verify(this);">
    	<font color=#215385><b>添加货物</b></font>
    	<IMG src="images/line1.gif" border=0>
    	<br>
    	<input type="hidden" name="maxBillId"  value="<%=maxBillId%>">
    	<TABLE width="100%" border="1" cellpadding="3" cellspacing="0" bordercolor="#FFFFFF" class="mailtable"> 

	  <tr>
            <td  align="right" width=20%> 
                <b>售货单号:</b>
            </td>
            <td width=20%>
            	<INPUT class=none  name="billid" type="text" size="30" value="<%=maxBillId%>" disabled>
            </td>
            <td width=30%>&nbsp;</td>
          </tr>
          <tr>
            <td  align="right" width=20%> 
                <b>货物标识:</b>
            </td>
            <td width=20%>
            	<INPUT class=none  name="goodsid" type="text" size="30" value="" >
            </td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name=submit type=submit value="添加" ></td>
          </tr>

	</TABLE>
	</form>
	
	<br>
    	<br>
	
	<form name="form2" method="post" action="" >
	<font color=#215385><b>货物列表</b></font>
	<IMG src="images/line1.gif" border=0>
	<input type="hidden" name="curBillId"  value="<%=maxBillId%>">
	<input type="hidden" name="obj4numbercheck" value="">
	<TABLE width="100%" border="1" cellpadding="3" cellspacing="0" bordercolor="#FFFFFF" class="mailtable">  
		<%
			if(goodsList.size()>0)
			{
		%>
		<tr bgcolor="#C2CEDC">
		<th></th>
		<th>货物标识</th>
		<th>货物名称</th>
		<th>目前库存</th>
		<th>售货数量</th>
		<th>售货单价</th>
		<th>销售人员</th>
		<th>删除</th>
		</tr>
		<%
			}
		%>
		
		<%
		    	int index = 0;
	      		GoodsExportGoodsInfo tmpInfo = null;
	      		float totalPrice = 0;
	      	
	      	for (index=0; index<goodsList.size(); index++)
		{
	      		tmpInfo = (GoodsExportGoodsInfo)goodsList.get(index);
	      		totalPrice = totalPrice + (tmpInfo.getExportAmount() * tmpInfo.getExportUnitPrice());
	      	%>
	        <tr <%if((index%2)==1){%>bgcolor="#CAE4F4"<%}%> >
	        <td align='center' width=2%><a href="<%= request.getContextPath() + "/goodsInfoView.do?goodsid=" + tmpInfo.getGoodsId() %>" target='goodsinfo'><IMG src="images/goods.gif" border=0 alt='查看货物信息'></a></td>
	        <td align='center'><%= tmpInfo.getGoodsId() %>
	        <input type="hidden" name="goodsid"  value="<%= tmpInfo.getGoodsId() %>">
	        </td>
	        <td align='center'>
	        	<%= tmpInfo.getGoodsName() %>
	        </td>
	        <td align='center'>
	        	<%
	        		if(tmpInfo.getCurRepertoryAmount()>0)
	        		{
	        	%>
	        			<%= tmpInfo.getCurRepertoryAmount() %>
	        	<%
	        		}
	        		else
	        		{
	        	%>	
	        		无库存
	        	<%
	        		}
	        	%>
	        </td>
	        <td align='center'>
	        	<%
	        		if(tmpInfo.getCurRepertoryAmount()>0)
	        		{
	        	%>
	        			<input type="text" name="exportamount"  value="<%= tmpInfo.getExportAmount() %>" size=5 onChange="if(updateExportAmount('<%=tmpInfo.getGoodsId()%>', this.value, '<%= tmpInfo.getCurRepertoryAmount() %>')=='1'){this.value='<%= tmpInfo.getExportAmount() %>'}">
	        	<%
	        		}
	        		else
	        		{
	        	%>	
	        			<%= tmpInfo.getExportAmount() %>
	        	<%
	        		}
	        	%>
	        </td>
		<td align='center'>
	        	<%= tmpInfo.getExportUnitPrice() %>
	        	<%
	        		if("1".equals(tmpInfo.getIsSpecialUnitPrice()))
	        		{
	        	%>
	        		<font color=red><b>特价</b></font>
	        	<%
	        		}
	        	%>
		</td>
	        <td align='center'><%= tmpInfo.getSalesPerson() %><input type="hidden" name="salesperson"  value="<%= tmpInfo.getSalesPerson() %>"></td>
	        <td align='center'>
	        	<a href="javascript:deleteATempExportGoods('<%= tmpInfo.getGoodsId() %>') "><IMG src="images/delto.gif" border=0></a>
	        </td>
	        </tr>
	        <%
	
	      	}//for
	        %>
	</TABLE>
	
    	
    	<TABLE width="100%" border="1" cellpadding="3" cellspacing="0" bordercolor="#FFFFFF" class="mailtable"> 

	  <tr>
            <td  align="right" > 
                <b>售货单号:</b>
            </td>
            <td >
            	<INPUT class=none  name="billid" type="text" size="30" value="<%=maxBillId%>" disabled>
            </td>
            <td  align="right" > 
                <b>会员标识:</b>
            </td>
            <td >
            	<%
            		
            		//同一张单上的会员标识是一样的，所以这里只取最后项的memberid
            		if(goodsList.size()>=1)
            		{//至少有一项

            			String memberid = tmpInfo.getMemberId()+"";
            			if("0".equals(memberid))
            			{//无会员标识
            				discount=1;
            	%>
            				<INPUT class=none  name="memberid" type="text" size="30" value="" onChange="updateMemberId(this.value)">
            	<%
            			}else
            			{
            	%>
            				<INPUT class=none  name="memberid" type="text" size="30" value="<%=memberid%>" onChange="updateMemberId(this.value)">
           	<%
           			}
           		}
           		else
           		{//无会员标识
           			discount=1;
           	%>
           			<INPUT class=none  name="memberid" type="text" size="30" value="" onChange="updateMemberId(this.value)">
           	<%
           		}
           	%>
            </td>
          </tr>
          <tr>
            <td  align="right" > 
                <b>会员折扣:</b>
            </td>
            <td >
            	<%
            		
            		//同一张单上的会员标识是一样的，所以这里只取最后项的memberid
            		if(goodsList.size()>=1)
            		{//至少有一项

            			String memberid = tmpInfo.getMemberId()+"";
            			if("0".equals(memberid))
            			{//无会员标识
            				discount=1;
            	%>
            				<INPUT class=none  name="discount" type="text" size="30" value="1" disabled>
            				<INPUT class=none  name="discount_value" type="hidden" size="30" value="1" >
            	<%
            			}else
            			{//含会员标识
            	%>
            				<INPUT class=none  name="discount" type="text" size="30" value="<%=discount%>" disabled>
            				<INPUT class=none  name="discount_value" type="hidden" size="30" value="<%=discount%>" >
           	<%
           			}
           		}
           		else
           		{//无会员标识
           			discount=1;
           	%>
           			<INPUT class=none  name="discount" type="text" size="30" value="1" disabled>
           			<INPUT class=none  name="discount_value" type="hidden" size="30" value="1" >
           	<%
           		}
           	%>
            	
            </td>
            <td  align="right" > 
                <b>折前总价:</b>
            </td>
            <td >
            	<INPUT class=none  name="totalprice" type="text" size="30" value="<%=totalPrice%>" disabled>
            	<INPUT class=none  name="totalprice_value" type="hidden" size="30" value="<%=totalPrice%>" >
            </td>
          </tr>
          <tr>
            <td  align="right" > 
                <b>实收总价:</b>
            </td>
            <td >
            	<INPUT class=none  name="totaldisprice" type="text" size="30" value="<%=totalPrice*discount%>" disabled>
            	<INPUT class=none  name="totaldisprice_value" type="hidden" size="30" value="<%=totalPrice*discount%>" >
            </td>
            <td  align="right" > 
                <b>是否付款:</b>
            </td>
            <td >
            	&nbsp;
            	<input type="radio" name="payornot" value="1" checked>已付款
            	&nbsp;
            	<input type="radio" name="payornot" value="0" >未付款
            </td>
          </tr>

	</TABLE>
	</form>
	
	<p><center>
	<input name=newbill type=button value="新售货单" onClick="showNewBill()">
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input name=deleteall type=button value="清空列表" onClick="deleteAllGoods()">
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input name=submitall type=button value="确认提交" onClick="submitAllGoods()">
    	</center><p>
	

	</td>
        </tr>
        <tr> 
          <td valign="middle" align="center">&nbsp;</td>
        </tr>

      </table>
      
    </td>
    <td width="5"></td>
  </tr>
  <tr> 
    <td width="6"></td>
    <td colspan="2" class="mainhead">&nbsp;</td>
    <td width="5"></td>
  </tr>
  <tr> 
    <td colspan="8" bgcolor="#214984" height="2"></td>
  </tr>
</table>
</body>
