<%@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 ProductList,DeliveryMoney,ExpressCompany
		Private TotalPrice,RealPrice,Price_Original,Discount,Amount,RealTotalPrice,TotalWeight
		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	
		           ProductList = KS.FilterIDs(Session("ProductList"))
		           If KS.Setting(123)="" Then Response.Write "请先到""基本信息设置->模板绑定""进行模板绑定操作!":response.end
				   FileContent = KSRFObj.LoadTemplate(KS.Setting(123))
				   FCls.RefreshType = "ShoppingPreview" '设置刷新类型，以便取得当前位置导航等
				   FCls.RefreshFolderID = "0"        '设置当前刷新目录ID 为"0" 以取得通用标签
				   If Trim(FileContent) = "" Then FileContent = "商城订单预览模板不存在!"
				   If Request("tocity")="" Then KS.Die "<script>alert('请选择发往城市!');history.back();</script>"
				   
				   
Set RS=Server.CreateObject("ADODB.RecordSet") 

		 Dim ProBuyAttr
		 If ProductList<>"" Then
		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,I.Weight,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"
	'删除没有在当前购物车内的捆绑商品
	conn.execute("delete from KS_ShopBundleSelect where username='" & getuserid & "' and proid not in(" & ProductList &")")
         Else
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,I.Weight,L.LimitBuyBeginTime,L.LimitBuyEndTime from Product I Left Join KS_ShopLimitBuy L On I.LimitBuyTaskID=L.Id where 1=0 order by I.IsChangedBuy,I.ID"		 End If

RS.open strsql,conn,1,1
	CartStr=CartStr&"      <table border=""0"" cellspacing=""1"" cellpadding=""4"" 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
	
Amount = 1 : TotalWeight=0
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 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

If IsNumerIc(RS("Weight")) Then TotalWeight=TotalWeight+RS("Weight")*amount
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

          CartStr=CartStr & GetPackage(false)          '礼包

     Dim PaymentDiscount:PayMentDiscount=KS.ReturnPayment(KS.S("PaymentType"),1)
	 
	 '===========================计算运费===============================
	 DeliveryMoney=KS.GetFreight(KS.ChkClng(KS.S("DeliverType")),KS.R(KS.S("ToCity")),totalweight,ExpressCompany)
	 If DeliveryMoney=-1 Then		
	  KS.AlertHintScript "对不起，您选择的线路还没有开通此送货方式！"
	  KS.Die ""
	 End If

	 
	 Dim TaxRate:TaxRate=KS.Setting(65)
	 Dim IncludeTax:IncludeTax=KS.Setting(64)
	 Dim TaxMoney
	 If IncludeTax=1 Or KS.ChkClng(KS.S("NeedInvoice"))=0 Then TaxMoney=1 Else TaxMoney=1+Taxrate/100
	 TotalPrice=Round(TotalPrice,2)
	 '总金额 = (总价*付费方式折扣+运费)*(1+税率)
	 RealTotalPrice=Round((TotalPrice*PayMentDiscount/100+DeliveryMoney)*TaxMoney,2)
	CartStr=CartStr&"<tr class='tdbg'> " & vbcrlf
	CartStr=CartStr&" <td colspan=""5"" align=""center"">" & vbcrlf             
    CartStr=CartStr&" 付款方式折扣率：" &PayMentDiscount& "% "
	If TotalWeight<>0 Then  CartStr=CartStr & " 重量：" & TotalWeight & " KG"
	CartStr=CartStr & " 运费：" &DeliveryMoney& " 元"
	
	If KS.ChkClng(KS.S("NeedInvoice"))=1 Then   '显示税率信息
	CartStr=CartStr & "     价格含税："
	If IncludeTax=1 Then CartStr=CartStr&"是" Else CartStr=CartStr&"不含税&nbsp;&nbsp;税率：" & TaxRate & "%" 
	End If
	
	CartStr=CartStr&"<br>实际金额：(" & TotalPrice &"×" & PaymentDiscount & "%＋" &DeliveryMoney & ")×"
	If IncludeTax=1 Or KS.ChkClng(KS.S("NeedInvoice"))=0 Then CartStr=CartStr&"100%" Else CartStr=CartStr&"(1+" & TaxRate & "%)"
	CartStr=CartStr&"＝" & RealTotalPrice&" 元"
    CartStr=CartStr&"  </td>" &vbcrlf
	CartStr=CartStr&"<td colspan=""2"" height=""25""><input type='hidden' name='TRealTotalPrice' id='TRealTotalPrice' value='" & RealTotalPrice & "'><input type='hidden' name='RealTotalPrice' id='RealTotalPrice' value='" & RealTotalPrice & "'>本单合计：<font color=""#FF6600"">￥" & RealTotalPrice & "</font>&nbsp;元<span  id=""ORDER_COSTS""></span></td>" & vbcrlf
    CartStr=CartStr&"    </tr>" & vbcrlf
	
	IF Cbool(KSUser.UserLoginChecked)=true Then
	End If
	
	
    CartStr=CartStr&"</table>" & vbcrlf
			 	 
		   FileContent = Replace(FileContent,"{$ShowShoppingCart}",CartStr)
		   If Cbool(KSUser.UserLoginChecked)=False Then 
		   FileContent = Replace(FileContent,"{$ShowLoginTips}","<strong><font color=ff6600>温馨提示：您还没有注册或登录。享受更多会员优惠，请先<a href=""../user/login/"" target=""_blank"">登录</a>或<a href=""../?do=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 GetCouponOptionList()
    Dim RS,SQL,I,Str,Param,SQLStr
	Param=Param & " and b.status=1 and datediff(" & DataPart_S & ",b.enddate," & SqlNowString & ")<0 And datediff(" & DataPart_S & ",b.begindate," & SqlNowString & ")>0"
	SQLStr="Select a.id,b.title,b.facevalue,a.AvailableMoney From KS_ShopCouponUser A inner Join KS_ShopCoupon B on a.couponid=b.id where AvailableMoney>0 and a.username='" & KSUser.UserName & "'" & Param	   
	Set RS=Server.CreateObject("ADODB.RECORDSET")
	RS.Open SQLStr,conn,1,1
	If Not RS.Eof Then
	  SQL=RS.GetRows(-1)
	End If
	RS.Close:Set RS=Nothing
	Str="<script language='JavaScript' src='../Inc/jquery.js'></script>"
	
	Str=Str & "<select name=""couponid"" onchange=""changeCoupon(this.value)"" style=""width:250px"">"
	Str=Str & "<option value='0'>---请选择---</option>"
	If IsArray(SQL) Then
	 For I=0 To  Ubound(SQL,2)
	  Str=Str & "<option value='" & SQL(0,I) & "'>" & SQL(1,I) & "[面值￥" & round(SQL(2,I),2) &"元 余额￥" & formatnumber(SQL(3,I),2,-1) & "元]</option>"
	 Next
	End If
	Str=Str & "</select>"
	GetCouponOptionList=Str
  End Function

  Function  ReplaceUserInfo(FileContent)
     Dim ContactMan:ContactMan=KS.S("ContactMan"):Session("Payment_ContactMan")=ContactMan
	 Dim Address:Address=KS.S("Address"):Session("Payment_Address")=Address
	 Dim ZipCode:ZipCode=KS.S("ZipCode"):Session("Payment_ZipCode")=ZipCode
	 Dim Phone:Phone=KS.S("Phone"):Session("Payment_Phone")=Phone
	 Dim Email:Email=KS.S("Email"):Session("Payment_Email")=Email
	 Dim Mobile:Mobile=KS.S("Mobile"):Session("Payment_Mobile")=Mobile
	 Dim QQ:QQ=KS.S("QQ"):Session("Payment_QQ")=QQ
	 Dim PaymentType:PaymentType=KS.S("PaymentType"):Session("Payment_PaymentType")=PaymentType
	 Dim DeliverType:DeliverType=KS.S("DeliverType"):Session("Payment_DeliverType")=DeliverType
	 Dim InvoiceContent:InvoiceContent=KS.S("InvoiceContent"):Session("Payment_InvoiceContent")=InvoiceContent
	 Dim NeedInvoice:NeedInvoice=KS.ChkClng(KS.S("NeedInvoice")):Session("Payment_NeedInvoice")=NeedInvoice
	 Dim Remark:Remark=KS.S("Remark"):Session("Payment_Remark")=Remark
	 Dim ToCity:ToCity=KS.S("ToCity"):Session("ToCity")=ToCity
	 Session("Weight")=TotalWeight
	 Session("DeliveryMoney")=DeliveryMoney
	 If ToCity<>"" Then ToCity=" 订单发往：" & ToCity 
	 If DeliveryMoney<>0 Then ToCity=ToCity & " 运费：" & FormatNumber(DeliveryMoney,2,-1,0,0) & " 元"
     FileContent=Replace(FileContent,"{$ContactMan}",ContactMan)
     FileContent=Replace(FileContent,"{$Address}",Address)
     FileContent=Replace(FileContent,"{$ZipCode}",ZipCode)
     FileContent=Replace(FileContent,"{$Phone}",Phone)
     FileContent=Replace(FileContent,"{$Email}",Email)
     FileContent=Replace(FileContent,"{$Mobile}",Mobile)
     FileContent=Replace(FileContent,"{$QQ}",QQ)
	 FileContent=Replace(FileContent,"{$PaymentType}",KS.ReturnPayment(PaymentType,0))
	 FileContent=Replace(FileContent,"{$DeliverType}","快递公司：" & ExpressCompany & ToCity)
	 If NeedInvoice="0" Then 
	     FileContent=Replace(FileContent,"{$InvoiceContent}","<font color=red>不需要发票</font>")
	 Else
	 	 FileContent=Replace(FileContent,"{$InvoiceContent}","<font color=red><strong>需要开据发票，发票信息如下：</strong></font><br>" & InvoiceContent)
	 End If
	 FileContent=Replace(FileContent,"{$Remark}",Remark)
	 ReplaceUserInfo=FileContent
  End Function
  
End Class
%>
