<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%option explicit%>
<!--#include file="../Conn.asp"-->
<!--#include file="../KS_Cls/Kesion.MemberCls.asp"-->
<!--#include file="../KS_Cls/Kesion.Label.CommonCls.asp"-->
<%

Dim KSCls
Set KSCls = New ShoppingCart
KSCls.Kesion()
Set KSCls = Nothing

Class ShoppingCart
        Private KS, KSRFObj,KSUser,DomainStr
		Private TotalPrice,RealPrice,Price_Original,Discount,Amount
		Private ProductList
		Private Sub Class_Initialize()
		  Set KS=New PublicCls
		  DomainStr=KS.GetDomain
		  Set KSUser = New UserCls
		  Set KSRFObj = New Refresh
		End Sub
        Private Sub Class_Terminate()
		 Call CloseConn()
		 Set KS=Nothing
		 Set KSUser=Nothing
		End Sub
		%>
		<!--#include file="PackCart.asp"-->
		<%
		Public Sub Kesion()
		  Dim FileContent,Products,i,RS,strsql,CartStr	
		  
		    Products = Split(Replace(KS.S("id")," ",""), ",")
			For I=0 To UBound(Products)
				   PutToShopBag Products(I), ProductList,I
			Next
			ProductList=KS.FilterIDs(ProductList)
			Session("ProductList")=ProductList
			
			If Cint(KS.Setting(63))=0 And Cbool(KSUser.UserLoginChecked)=false Then Response.Write "<script>alert('本商城设置注册用户才可购买，请先登录!');location.href='" &  DomainStr  & "user/login/';</script>"

		           If KS.Setting(122)="" Then Response.Write "请先到""基本信息设置->模板绑定""进行模板绑定操作!":response.end
				   FileContent = KSRFObj.LoadTemplate(KS.Setting(122))
				   FCls.RefreshType = "ShoppingPayMent" '设置刷新类型，以便取得当前位置导航等
				   Fcls.RefreshFolderID = "0"        '设置当前刷新目录ID 为"0" 以取得通用标签
				   If Trim(FileContent) = "" Then FileContent = "商城购物车模板不存在!"
	
	Dim packstr:packstr=GetPackage(false)          '礼包		
				   
    Set RS=Server.CreateObject("ADODB.RecordSet") 
	If ProductList<>"" Then
	Dim ProBuyAttr
	'删除没有在当前购物车内的捆绑商品
	conn.execute("delete from KS_ShopBundleSelect where username='" & getuserid & "' and proid not in(" & ProductList &")")

	strsql="select I.ID,I.Title,I.ProductType,I.Price_Original,I.Price,I.Price_Member,I.Discount,I.TotalNum,I.GroupPrice,I.photourl,I.unit,I.IsLimitBuy,I.LimitBuyPrice,I.LimitBuyAmount,L.LimitBuyBeginTime,L.LimitBuyEndTime from Product I Left Join KS_ShopLimitBuy L On I.LimitBuyTaskID=L.Id  where I.ID in ("&ProductList&") order by I.IsChangedBuy,I.ID"
	Call SetBundleSaleAmount()  '设置捆绑促销购买数量
	ElseIf packstr="" Then
	Response.Write "<script>alert('您的购物车中没有商品!');history.back();</script>":response.end
	End If

	CartStr="<form action=""ShoppingCart.asp"" method=""POST"" name=""check"">"&vbcrlf
	CartStr=CartStr&"      <table border=""0"" cellspacing=""1"" cellpadding=""1"" align=""center"" width=""100%"" class=""border"">" & vbcrlf
	CartStr=CartStr&"            <tr class='title' height=""25"">" & vbcrlf 
	CartStr=CartStr&"            <td width=""6%"" align=""center"">编 号</td>" &vbcrlf
	CartStr=CartStr&"            <td align=""center"">商 品 名 称</td>" & vbcrlf
	CartStr=CartStr&"            <td width=""10%"" align=""center"">数 量</td>" & vbcrlf
	CartStr=CartStr&"			   <td width=""10%"" align=""center"">原 价</td>" & vbcrlf
	CartStr=CartStr&"			   <td width=""10%"" align=""center"">折 扣</td>"&vbcrlf
	CartStr=CartStr&"			   <td width=""10%"" align=""center"">实 价</td>" & vbcrlf
	CartStr=CartStr&"			   <td width=""12%"" align=""center"">总 计</td>" & vbcrlf
	CartStr=CartStr&"          </tr>"&vbcrlf
	
	If strSql<>"" Then
			RS.open strsql,conn,1,1
			Amount = 1
			Do While Not RS.EOF
				 Amount = KS.ChkClng(KS.S( "Q_" & RS("ID")))
				 If Amount <= 0 Then 
					Amount = KS.ChkClng(Session("Amount"&RS("ID")))
					If Amount <= 0 Then Amount = 1
				 End If
			Session("Amount"&RS("ID")) = Amount
			
			IF RS("TotalNum") < Amount Then
				Amount = 1
				Session("Amount"&RS("ID")) = 1
				response.write "<script language=javascript>alert('对不起，["&RS("Title")&"]暂时库存不足，最多只能购买" & RS("TotalNum") & RS("unit") & "！');history.back(-1);</script>" 
				response.End()
			End IF
			Dim ProDiscount:ProDiscount=""
			IF RS("IsLimitBuy")<>"0" and RS("LimitBuyAmount") < Amount Then
				Amount = 1
				Session("Amount"&RS("ID")) = 1
				response.write "<script language=javascript>alert('对不起，["&RS("Title")&"]还剩" & RS("LimitBuyAmount") & RS("unit") & "供抢购！');history.back(-1);</script>" 
				rs.close:set rs=nothing
				response.End()
			End If
			ProBuyAttr=""

			If Trim(RS("ID"))=trim(Session("ChangeBuyID")) Then
			   RealPrice=Session("ChangeBuyPrice")
			ElseIf RS("IsLimitBuy")="1" And Now>RS("LimitBuyBeginTime") And RS("LimitBuyEndTime")>Now And RS("LimitBuyAmount")>0 Then
			   RealPrice=RS("LimitBuyPrice")
			   ProBuyAttr="<span style='color:green'>(限时抢购)</span>"
			   ProDiscount="---"
			ElseIf RS("IsLimitBuy")="2" And RS("LimitBuyAmount")>0 Then
			   RealPrice=RS("LimitBuyPrice")
			   ProBuyAttr="<span style='color:blue'>(限量抢购)</span>"
			   ProDiscount="---"
			ElseIf Not Conn.Execute("Select price From KS_ShopBundleSelect Where Pid=" & RS("ID") &" And ProID in(" &ProductList& ") AND UserName='" & GetUserID & "'").EOF Then
			   RealPrice=Conn.Execute("Select price From KS_ShopBundleSelect Where Pid=" & RS("ID") &" And ProID in(" &ProductList & ") AND UserName='" & GetUserID & "'")(0)
			   ProBuyAttr="<span style='color:blue'>(捆绑促销)</span>"
			   ProDiscount="---"
			   
			ElseIF Cbool(KSUser.UserLoginChecked)=true Then
			  If RS("GroupPrice")=0 Then
			   RealPrice=RS("Price_Member")
			  Else
			   Dim RSP:Set RSP=Conn.Execute("Select top 1 Price From KS_ProPrice Where GroupID=" & KSUser.GroupID & " And ProID=" & RS("ID"))
			   If RSP.Eof Then
				 RealPrice=RS("Price_Member")
			   Else
				 RealPrice=RSp(0)
			   End If
			   RSP.Close:Set RSP=Nothing
			  End If
			Else
			  RealPrice=RS("Price")
			End If
			TotalPrice=TotalPrice+Round(RealPrice*Amount,2)
			Dim PhotoUrl:PhotoUrl=RS("PhotoUrl")
			If KS.IsNul(PhotoUrl) Then PhotoUrl="/images/nopic.gif"
			
					  CartStr=CartStr&"<tr class='tdbg' height=""25"" align=""center""> " & vbcrlf
					  CartStr=CartStr&"  <td>" & RS("ID") & "</td>" & vbcrlf
					  CartStr=CartStr&"  <td align=""left""><img src=""" & PhotoUrl & """ alt=""" & RS("Title") & """ width=""50"" height=""50"" align=""left"" style=""border:1px solid #ccc;padding:2px""/><a href=""/item/show.asp?m=5&d=" & RS("id") & """ target=""_blank"">" & RS("Title") 
					  If Trim(RS("ID"))=trim(Session("ChangeBuyID")) Then
					  CartStr=CartStr& "(换购)"
					  Else
					  CartStr=CartStr& ProBuyAttr
					  End If
					  CartStr=CartStr&"</a><br/>" & Session("AttributeCart"&RS("ID"))  & "</td>" & vbcrlf
					  CartStr=CartStr&"  <td>" & Amount & "</td>" & vbcrlf
					  CartStr=CartStr&"	<td>￥" & FormatNumber(RS("Price_Original"),2,-1) & "</td>" & vbcrlf
					  If Trim(RS("ID"))=trim(Session("ChangeBuyID")) Then
					  CartStr=CartStr&"	<td align='center'>换购</td>" & vbcrlf
					  ElseIf ProDiscount<>"" Then
					  CartStr=CartStr&"	<td align='center'>" & ProDiscount & "</td>" & vbcrlf
					  ElseIf RS("Discount")=10 Then
					  CartStr=CartStr&"	<td align='center'>无折扣</td>" & vbcrlf
					  Else
					  CartStr=CartStr&"	<td>" & RS("Discount") & "折</td>" & vbcrlf
					  End If
					  CartStr=CartStr&"	<td>￥" & FormatNumber(RealPrice,2,-1) & "</td>" & vbcrlf
					  CartStr=CartStr&"	<td>￥" & FormatNumber(Round(RealPrice*Amount,2),2,-1)  & "</td>" & vbcrlf
					  CartStr=CartStr&"</tr>" & vbcrlf
		              CartStr=CartStr & GetBundleSalePro(RS("ID"),false)   '获得捆绑促销的商品
					  
				 RS.MoveNext
				 Loop
			RS.close:set RS=nothing
    End If
	
	
	CartStr=CartStr & packstr      '礼包

	CartStr=CartStr&"<tr class='tdbg'> " & vbcrlf
	CartStr=CartStr&" <td colspan=""4"" align=""center"">" & vbcrlf             
    CartStr=CartStr&" <font color=red><strong>以上是您购物车中的商品信息，请核对正确无误后下单!</strong>&nbsp;</font>点此<a href=""ShoppingCart.asp"">修改订单</a>" & vbcrlf
    CartStr=CartStr&"  </td>" &vbcrlf
	CartStr=CartStr&"<td colspan=""3"" height=""25"">合计：<font color=""#FF6600"">￥" & Round(TotalPrice,2) & "</font>&nbsp;元</td>" & vbcrlf
    CartStr=CartStr&"    </tr>" & vbcrlf
    CartStr=CartStr&"</table>" & vbcrlf
    CartStr=CartStr&"</form>"&vbcrlf
			 	 
		   FileContent = Replace(FileContent,"{$ShowShoppingCart}",CartStr)
		   If Cbool(KSUser.UserLoginChecked)=False Then 
		   FileContent = Replace(FileContent,"{$ShowLoginTips}","<strong><font color=ff6600>温馨提示：您还没有注册或登录。享受更多会员优惠，请先<a href=""../user/login/"">登录</a>或<a href=""../?dp=reg"" target=""_blank"">注册</a>成为商城会员！</font></strong>")
           Else
		   FileContent = Replace(FileContent,"{$ShowLoginTips}","亲爱的" & KSUser.UserName &"! 您的个人信息-> 用户组："&KS.GetUserGroupName(KSUser.GroupID)&"&nbsp;可用资金：&nbsp;<font color=""green"">" & FormatNumber(KSUser.GetUserInfo("Money"),2,-1,0,0) & "</font>&nbsp;元 " & KS.Setting(45) & "：&nbsp;<font color=green>" & KSUser.GetUserInfo("Point") & "</font>&nbsp;" & KS.Setting(46)&" 积分：&nbsp;<font color=""green"">" & KSUser.GetUserInfo("Score") & "</font>&nbsp;分")
		   End If
		   FileContent = ReplaceUserInfo(FileContent)
		   FileContent=KSRFObj.KSLabelReplaceAll(FileContent)
		   Response.Write FileContent  
End Sub

  Function  ReplaceUserInfo(FileContent)
     FileContent=Replace(FileContent,"{$ContactMan}",KSUser.GetUserInfo("RealName"))
     FileContent=Replace(FileContent,"{$Address}",KSUser.GetUserInfo("Address"))
     FileContent=Replace(FileContent,"{$ZipCode}",KSUser.GetUserInfo("Zip"))
     FileContent=Replace(FileContent,"{$Phone}",KSUser.GetUserInfo("OfficeTel"))
     FileContent=Replace(FileContent,"{$Email}",KSUser.GetUserInfo("Email"))
     FileContent=Replace(FileContent,"{$Mobile}",KSUser.GetUserInfo("Mobile"))
     FileContent=Replace(FileContent,"{$QQ}",KSUser.GetUserInfo("QQ"))
	 FileContent=Replace(FileContent,"{$PaymentType}",GetPaymentTypeStr)
	 FileContent=Replace(FileContent,"{$DeliveryType}",GetDeliveryTypeStr)
	 ReplaceUserInfo=FileContent
  End Function
  '付款方式
  Function GetPaymentTypeStr()
   Dim DiscountStr,SQL,I,RS:Set RS=Server.CreateObject("ADODB.RECORDSET")
   RS.Open "select TypeID,TypeName,IsDefault,Discount from KS_PaymentType order by orderid",conn,1,1
   If Not RS.Eof Then
     SQL=RS.GetRows(-1)
   End IF
   RS.Close:Set RS=Nothing
   GetPaymentTypeStr="<select name='PaymentType'>"
   For I=0 To UBound(SQL,2)
     If SQL(3,I)<>100 Then
	  DiscountStr="折扣率 " & SQL(3,I) & "%"
	 Else
	  DiscountStr=""
	 End iF
     If SQL(2,I)=1 Then
    GetPaymentTypeStr=GetPaymentTypeStr& "<option value='" & SQL(0,I) & "' selected>"  &SQL(1,I) & " " & DiscountStr & "</option>"
	 Else
    GetPaymentTypeStr=GetPaymentTypeStr& "<option value='" & SQL(0,I) & "'>"  &SQL(1,I) & " " & DiscountStr & "</option>"
	End If
   Next
   GetPaymentTypeStr=GetPaymentTypeStr & "</select>"
  End Function
  
  '发货方式
  Function GetDeliveryTypeStr()
   Dim typeid,j,rss,rsss
   Dim DiscountStr,SQL,I,RS
    set rsss=conn.execute("select expressid from KS_Delivery where isDefault=1 order by orderid")
	if not rsss.eof then 
		 typeid=rsss(0)
	end if
	rsss.close:set rsss=nothing

   Set RS=Server.CreateObject("ADODB.RECORDSET")
   RS.Open "select TypeID,TypeName,IsDefault from KS_DeliveryType order by orderid,TypeID",conn,1,1
   If Not RS.Eof Then
     SQL=RS.GetRows(-1)
   End IF
   RS.Close:Set RS=Nothing
   GetDeliveryTypeStr="<strong>快递公司：</strong><select name='DeliverType' id='DeliverType'>"
   For I=0 To UBound(SQL,2)
     If trim(typeid)=trim(sql(0,i)) Then
    GetDeliveryTypeStr=GetDeliveryTypeStr& "<option value='" & SQL(0,I) & "' selected>"  &SQL(1,I) & "</option>"
	 Else
    GetDeliveryTypeStr=GetDeliveryTypeStr& "<option value='" & SQL(0,I) & "'>"  &SQL(1,I) & "</option>"
	End If
   Next
   GetDeliveryTypeStr=GetDeliveryTypeStr & "</select>"
   

   GetDeliveryTypeStr=GetDeliveryTypeStr & " <input type=""hidden"" name=""tocity"" id=""tocity""/> <span style='position:relative'><input class=""tocity"" style='text-align;left' name='' id='choosecity' type='button' value='选择送货地点'  onclick=""$('#showprovn').show();if(this.getBoundingClientRect().top>300){showprovn.style.top=(this.offsetHeight-showprovn.offsetHeight)}else{showprovn.style.top='0'}""><span id='showprovn' onclick=""this.style.display='none'"" class='showcity'>"&_
			 "<table width='92%' align='center' border='0' cellspacing='0' cellpadding='0'>"&vbcrlf
			        dim pxml,node,pnode
			        set rss=conn.execute("select id,City,parentid from KS_Province order by orderid asc,id")
					if not rss.eof then
					  set pxml=KS.RsToXml(rss,"row","")
					end if
					rss.close  : Set RSS=Nothing
					If IsObject(Pxml) Then
	  				 For Each Node In pxml.DocumentElement.SelectNodes("row[@parentid=0]")
					    GetDeliveryTypeStr=GetDeliveryTypeStr&"<tr><td colspan='5' class='provincename'><strong>" & Node.SelectSingleNode("@city").text &"</td></tr>"
						j=1
						For Each pnode in Pxml.DocumentElement.SelectNodes("row[@parentid=" & Node.SelectSingleNode("@id").text & "]")
						IF (j MOD 5) = 1 THEN GetDeliveryTypeStr=GetDeliveryTypeStr&"<tr>"&vbcrlf
						GetDeliveryTypeStr=GetDeliveryTypeStr&"<td id='ccity' onclick=""$('#choosecity').val(this.innerHTML);ajshowdata(this.innerHTML)"" style='cursor:hand' onmouseover=""this.style.color='red'"" onmouseout=""this.style.color=''"">"&pnode.selectsinglenode("@city").text&"</td>"&vbcrlf
						if (j mod 5)=0 then GetDeliveryTypeStr=GetDeliveryTypeStr&"</tr>"&vbcrlf
						j=j+1
						Next
						
					 Next
					End If
 
			        
					 
			 GetDeliveryTypeStr=GetDeliveryTypeStr&"</table>"&vbcrlf&_
			"</span></span>"&_
		"<br/><strong>价格信息：</strong><span id='jgxx' class='jgxx'>选择送货路线确定运费！</span>"&vbcrlf
   
  End Function
  
  	Sub PutToShopBag( Prodid, ProductList ,I)
		   if KS.S("action")="set" then
			   If i = 0 Then
				  ProductList =Prodid
			   ElseIf KS.FoundInArr( ProductList, Prodid,",")=false Then
				  ProductList = ProductList&", "&Prodid &""
			   End If
		   else
			   If Len(ProductList) = 0 Then
				  ProductList =Prodid
			   ElseIf KS.FoundInArr( ProductList, Prodid,",")=false Then
				  ProductList = ProductList&", "&Prodid &""
			   End If
		  end if
      End Sub

End Class
%>
