<!--#include file="../Start.asp"-->
<!--#include file="Channel_Config.asp"-->
<!--#include file="../Include/PowerEasy.Cache.asp"-->
<!--#include file="../Include/PowerEasy.Channel.asp"-->
<!--#include file="../Include/PowerEasy.Common.Front.asp"-->
<!--#include file="../Include/PowerEasy.OrderFlow.asp"-->
<!--#include file="../Include/PowerEasy.SMS.asp"-->
<!--#include file="../Include/PowerEasy.MD5_New.asp"-->
<%
'**************************************************************
' Software name: PowerEasy SiteWeaver
' Web: http://www.powereasy.net
' Copyright (C) 2005－2008 佛山市动易网络科技有限公司 版权所有
'**************************************************************

'判断是否需要注册
If UserLogined = False And EnableGuestBuy = False Then
    Response.Write XmlText_Class("ShowTips_GuestBuy", "<br><br><p align='center'>请先<a href='../User/User_Login.asp'>登录</a>！</p>")
    Response.End
End If
    
ContacterName = PE_HTMLEncode(Trim(Request("ContacterName")))
Address = PE_HTMLEncode(Trim(Request("Address")))
ZipCode = PE_HTMLEncode(Trim(Request("ZipCode")))
Phone = PE_HTMLEncode(Trim(Request("Phone")))
Mobile = PE_HTMLEncode(Trim(Request("Mobile")))
Email = PE_HTMLEncode(Trim(Request("Email")))
AgentName = PE_HTMLEncode(Trim(Request("AgentName")))
PaymentType = Trim(Request("PaymentType"))
DeliverType = Trim(Request("DeliverType"))
NeedInvoice = Trim(Request("NeedInvoice"))
InvoiceContent = Trim(Request("InvoiceContent"))
Remark = Trim(Request("Remark"))
BeginDate = FormatDateTime(Date, 2) '开始日期
If ContacterName = "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>请输入收货人姓名！</li>"
End If
If Address = "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>请输入收货人地址！</li>"
End If
If ZipCode = "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>请输入收货人邮编！</li>"
End If
If Phone = "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>请输入收货人电话！</li>"
Else
    If IsValidPhone(Phone) = False Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>收货人电话格式错误！</li>"
    End If
End If
If Not IsNumeric(Mobile) And Mobile <> "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>收货人手机格式错误！</li>"
End If
If Email = "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>请输入收货人Email！</li>"
Else
    If IsValidEmail(Email) = False Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>收货人Email有错误！</li>"
    End If
End If
If PaymentType = "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>请指定付款方式！</li>"
Else
    PaymentType = PE_CLng(PaymentType)
    Set rsPaymentType = Conn.Execute("select * from PE_PaymentType where TypeID=" & PaymentType & "")
    If rsPaymentType.BOF And rsPaymentType.EOF Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到指定的付款方式！</li>"
    Else
        PaymentTypeName = rsPaymentType("TypeName")
        Discount_Payment = rsPaymentType("Discount")
    End If
    Set rsPaymentType = Nothing
End If
If DeliverType = "" Then
    FoundErr = True
    ErrMsg = ErrMsg & "<li>请指定送货方式！</li>"
Else
    DeliverType = PE_CLng(DeliverType)
    Set rsDeliverType = Conn.Execute("select * from PE_DeliverType where TypeID=" & DeliverType & "")
    If rsDeliverType.BOF And rsDeliverType.EOF Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到指定的送货方式！</li>"
    Else
        DeliverTypeName = rsDeliverType("TypeName")
        ChargeType = rsDeliverType("ChargeType")
        ReleaseType = PE_CLng(rsDeliverType("ReleaseType"))
        MinMoney1 = PE_CDbl(rsDeliverType("MinMoney1"))
        ReleaseCharge = PE_CDbl(rsDeliverType("ReleaseCharge"))
        MinMoney2 = PE_CDbl(rsDeliverType("MinMoney2"))
        MaxCharge = PE_CDbl(rsDeliverType("MaxCharge"))
        MinMoney3 = PE_CDbl(rsDeliverType("MinMoney3"))
        Charge_Min = PE_CDbl(rsDeliverType("Charge_Min"))
        Charge_Max = PE_CDbl(rsDeliverType("Charge_Max"))
        Charge_Percent = PE_CDbl(rsDeliverType("Charge_Percent"))
    End If
    Set rsDeliverType = Nothing
End If
If NeedInvoice = "Yes" Then
    NeedInvoice = True
Else
    NeedInvoice = False
End If
If FoundErr = True Then
    Response.Write ErrMsg
    Response.End
End If

If ChargeType = 1 Then
    Dim rsCity, AreaType, strProvince, strPostCode
    Set rsCity = Conn.Execute("select Top 1 * from PE_City where left(PostCode,4)='" & Left(ZipCode, 4) & "'")
    If rsCity.BOF And rsCity.EOF Then
        strProvince = ""
        strPostCode = ""
    Else
        strProvince = rsCity("Province")
        strPostCode = rsCity("PostCode")
    End If
    rsCity.Close
    Set rsCity = Nothing
    
    If Left(MyPostcode, 4) = Left(ZipCode, 4) Then  '同城
        AreaType = 1
        Set rsDeliverType = Conn.Execute("select * from PE_DeliverCharge where AreaType=1 and DeliverTypeID=" & DeliverType & "")
        If Not (rsDeliverType.BOF And rsDeliverType.EOF) Then
            Charge_Min = PE_CDbl(rsDeliverType("Charge_Min"))
            Weight_Min = PE_CDbl(rsDeliverType("Weight_Min"))
            ChargePerUnit = PE_CDbl(rsDeliverType("ChargePerUnit"))
            WeightPerUnit = PE_CDbl(rsDeliverType("WeightPerUnit"))
            Charge_Max = PE_CDbl(rsDeliverType("Charge_Max"))
        End If
        Set rsDeliverType = Nothing
    Else '不同城
        If strProvince = MyProvince Then '同省
            Set rsDeliverType = Conn.Execute("select * from PE_DeliverCharge where AreaType=2 and DeliverTypeID=" & DeliverType & "")
            If Not (rsDeliverType.BOF And rsDeliverType.EOF) Then
                If FoundInArr(rsDeliverType("arrArea"), Left(strPostCode, 4), ",") = True Then '如果同区域
                    Charge_Min = PE_CDbl(rsDeliverType("Charge_Min"))
                    Weight_Min = PE_CDbl(rsDeliverType("Weight_Min"))
                    ChargePerUnit = PE_CDbl(rsDeliverType("ChargePerUnit"))
                    WeightPerUnit = PE_CDbl(rsDeliverType("WeightPerUnit"))
                    Charge_Max = PE_CDbl(rsDeliverType("Charge_Max"))
                    AreaType = 2
                Else  '同省但不同区域
                    AreaType = 3
                End If
            Else
                AreaType = 3
            End If
            Set rsDeliverType = Nothing
            If AreaType = 3 Then
                Set rsDeliverType = Conn.Execute("select * from PE_DeliverCharge where AreaType=3 and DeliverTypeID=" & DeliverType & "")
                If Not (rsDeliverType.BOF And rsDeliverType.EOF) Then
                    Charge_Min = PE_CDbl(rsDeliverType("Charge_Min"))
                    Weight_Min = PE_CDbl(rsDeliverType("Weight_Min"))
                    ChargePerUnit = PE_CDbl(rsDeliverType("ChargePerUnit"))
                    WeightPerUnit = PE_CDbl(rsDeliverType("WeightPerUnit"))
                    Charge_Max = PE_CDbl(rsDeliverType("Charge_Max"))
                End If
                Set rsDeliverType = Nothing
            End If
        Else '不同省
            AreaType = 5
            Set rsDeliverType = Conn.Execute("select * from PE_DeliverCharge where AreaType=4 and DeliverTypeID=" & DeliverType & "")
            If Not (rsDeliverType.BOF And rsDeliverType.EOF) Then
                Do While Not rsDeliverType.EOF
                    If FoundInArr(rsDeliverType("arrArea"), strProvince, ",") = True Then  '如果在指定省份列表中
                        Charge_Min = PE_CDbl(rsDeliverType("Charge_Min"))
                        Weight_Min = PE_CDbl(rsDeliverType("Weight_Min"))
                        ChargePerUnit = PE_CDbl(rsDeliverType("ChargePerUnit"))
                        WeightPerUnit = PE_CDbl(rsDeliverType("WeightPerUnit"))
                        Charge_Max = PE_CDbl(rsDeliverType("Charge_Max"))
                        AreaType = 4
                        Exit Do
                    End If
                    rsDeliverType.MoveNext
                Loop
            End If
            Set rsDeliverType = Nothing
            If AreaType = 5 Then
                Set rsCity = Conn.Execute("select Top 1 * from PE_City where left(PostCode,2)='" & Left(ZipCode, 2) & "'")
                If Not (rsCity.BOF And rsCity.EOF) Then
                    Set rsDeliverType = Conn.Execute("select Top 1 * from PE_DeliverCharge where AreaType=4 and DeliverTypeID=" & DeliverType & "")
                    If Not (rsDeliverType.BOF And rsDeliverType.EOF) Then
                        Charge_Min = PE_CDbl(rsDeliverType("Charge_Min"))
                        Weight_Min = PE_CDbl(rsDeliverType("Weight_Min"))
                        ChargePerUnit = PE_CDbl(rsDeliverType("ChargePerUnit"))
                        WeightPerUnit = PE_CDbl(rsDeliverType("WeightPerUnit"))
                        Charge_Max = PE_CDbl(rsDeliverType("Charge_Max"))
                        AreaType = 4
                    End If
                    rsDeliverType.Close
                    Set rsDeliverType = Nothing
                End If
                rsCity.Close
                Set rsCity = Nothing
            End If
            If AreaType = 5 Then
                Set rsDeliverType = Conn.Execute("select * from PE_DeliverCharge where AreaType=5 and DeliverTypeID=" & DeliverType & "")
                If Not (rsDeliverType.BOF And rsDeliverType.EOF) Then
                    Charge_Min = PE_CDbl(rsDeliverType("Charge_Min"))
                    Weight_Min = PE_CDbl(rsDeliverType("Weight_Min"))
                    ChargePerUnit = PE_CDbl(rsDeliverType("ChargePerUnit"))
                    WeightPerUnit = PE_CDbl(rsDeliverType("WeightPerUnit"))
                    Charge_Max = PE_CDbl(rsDeliverType("Charge_Max"))
                End If
                Set rsDeliverType = Nothing
            End If
        End If
    End If
    If WeightPerUnit <= 0 Then
        WeightPerUnit = 1
    End If
End If

'判断购物车是否为空
CartID = ReplaceBadChar(Trim(Request.Cookies("Cart" & Site_Sn)("CartID")))
ProductList = SelectCart(CartID, 0)
PresentList = SelectCart(CartID, 1)
If ProductList = "" Then       '如果购物车为空，转入提示界面
    Response.Write "<table height='100%' width='100%'><tr><td align='center'>您好！目前您的购物车中没有任何商品，是不是 <a href='ShowElite.asp'><b>继续购物</b></a>？</td></tr></table>"
    Response.End
Else
    ProductList = ReplaceBadChar(ProductList)
End If


'如果会员还不是客户，则添加相应的客户信息，并把会员变成客户
If UserID > 0 And ClientID = 0 Then
    Dim ClientName, ShortedForm, ClientType
    If UserType > 0 Then
        Dim rsCompany
        Set rsCompany = Conn.Execute("select CompanyName from PE_Company where CompanyID=" & CompanyID & "")
        ClientName = rsCompany(0)
        ShortedForm = Left(rsCompany(0), 6)
        ClientType = 0
        rsCompany.Close
        Set rsCompany = Nothing
    Else
        ClientName = ContacterName
        ShortedForm = ContacterName
        ClientType = 1
    End If

    ClientID = GetNewID("PE_Client", "ClientID")
    Dim rsClient, sqlClient
    sqlClient = "select top 1 * from PE_Client"
    Set rsClient = Server.CreateObject("adodb.recordset")
    rsClient.Open sqlClient, Conn, 1, 3
    rsClient.AddNew
    rsClient("ClientID") = ClientID
    rsClient("CreateTime") = Now()
    rsClient("Owner") = ""
    rsClient("ClientName") = ClientName
    rsClient("ClientNum") = GetNumString()
    rsClient("ShortedForm") = ShortedForm
    rsClient("ClientType") = ClientType
    rsClient("Area") = -1
    rsClient("ClientField") = -1
    rsClient("ValueLevel") = -1
    rsClient("CreditLevel") = -1
    rsClient("Importance") = -1
    rsClient("ConnectionLevel") = -1
    rsClient("SourceType") = -1
    rsClient("GroupID") = 0
    rsClient("ParentID") = 0
    rsClient("PhaseType") = -1
    rsClient("Remark") = ""
    rsClient("UpdateTime") = Now()
    rsClient.Update
    rsClient.Close
    Set rsClient = Nothing
    Conn.Execute ("update PE_User set ClientID=" & ClientID & " where UserID=" & UserID & "")
    If UserType > 0 Then
        Conn.Execute ("update PE_Company set ClientID=" & ClientID & " where CompanyID=" & CompanyID & "")
        Dim rsMember
        Set rsMember = Conn.Execute("select ContacterID from PE_User where CompanyID=" & CompanyID & "")
        Do While Not rsMember.EOF
            Conn.Execute ("update PE_Contacter set ClientID=" & ClientID & " where ContacterID=" & rsMember(0) & "")
            rsMember.MoveNext
        Loop
        rsMember.Close
        Set rsMember = Nothing
    End If
End If

'如果会员还没有填写详细的联系信息，则把订单中的信息保存到会员的详细信息中
If UserID > 0 Then
    Dim rsContacter, sqlContacter
    If ContacterID = 0 Then
        ContacterID = GetNewID("PE_Contacter", "ContacterID")
        sqlContacter = "select top 1 * from PE_Contacter"
        Set rsContacter = Server.CreateObject("adodb.recordset")
        rsContacter.Open sqlContacter, Conn, 1, 3
        rsContacter.AddNew
        rsContacter("ContacterID") = ContacterID
        rsContacter("ClientID") = ClientID
        rsContacter("ParentID") = 0
        rsContacter("UserType") = 0
        rsContacter("Family") = ""
        rsContacter("CreateTime") = Now()
        rsContacter("Owner") = ""

        rsContacter("TrueName") = ContacterName
        rsContacter("Country") = ""
        rsContacter("Province") = ""
        rsContacter("City") = ""
        rsContacter("ZipCode") = ZipCode
        rsContacter("Address") = Address
        rsContacter("Mobile") = Mobile
        rsContacter("OfficePhone") = Phone
        rsContacter("HomePhone") = Phone
        rsContacter("PHS") = ""
        rsContacter("Fax") = ""
        rsContacter("Homepage") = ""
        rsContacter("Email") = Email
        rsContacter("QQ") = ""
        rsContacter("MSN") = ""
        rsContacter("ICQ") = ""
        rsContacter("Yahoo") = ""
        rsContacter("UC") = ""
        rsContacter("Aim") = ""
        rsContacter("Company") = ""
        rsContacter("CompanyAddress") = ""
        rsContacter("Department") = ""
        rsContacter("Position") = ""
        rsContacter("Title") = ""
        rsContacter("BirthDay") = Date
        rsContacter("IDCard") = ""
        rsContacter("NativePlace") = ""
        rsContacter("Nation") = ""
        rsContacter("Sex") = 0
        rsContacter("Marriage") = 0
        rsContacter("Education") = -1
        rsContacter("GraduateFrom") = ""
        rsContacter("InterestsOfLife") = ""
        rsContacter("InterestsOfCulture") = ""
        rsContacter("InterestsOfAmusement") = ""
        rsContacter("InterestsOfSport") = ""
        rsContacter("InterestsOfOther") = ""
        rsContacter("Income") = -1
        rsContacter("UpdateTime") = Now()
        rsContacter.Update
        rsContacter.Close
        Set rsContacter = Nothing
        Conn.Execute ("update PE_User set ContacterID=" & ContacterID & " where UserID=" & UserID & "")
    Else
        sqlContacter = "select * from PE_Contacter where ContacterID=" & ContacterID
        Set rsContacter = Server.CreateObject("adodb.recordset")
        rsContacter.Open sqlContacter, Conn, 1, 3
        If Not (rsContacter.BOF And rsContacter.EOF) Then
            If rsContacter("ClientID") <= 0 Then
                rsContacter("ClientID") = ClientID
            End If
            If rsContacter("TrueName") & "" = "" Then
                rsContacter("TrueName") = ContacterName
            End If
            If rsContacter("ZipCode") & "" = "" Then
                rsContacter("ZipCode") = ZipCode
            End If
            If rsContacter("Address") & "" = "" Then
                rsContacter("Address") = Address
            End If
            If rsContacter("Mobile") & "" = "" Then
                rsContacter("Mobile") = Mobile
            End If
            'rsContacter("OfficePhone") = Phone
            If rsContacter("HomePhone") & "" = "" Then
                rsContacter("HomePhone") = Phone
            End If
            If rsContacter("Email") & "" = "" Then
                rsContacter("Email") = Email
            End If
            rsContacter.Update
        End If
        rsContacter.Close
        Set rsContacter = Nothing
    End If
End If

PresentMoney = 0
PresentExp = 0
PresentPoint = 0
Dim sqlCart, rsItem, SaleType, trs
Dim Charge_Deliver_Original
Dim OrderFormID, OrderFormNum

OrderFormID = GetNewID("PE_OrderForm", "OrderFormID")
OrderFormNum = Prefix_OrderFormNum & GetNumString()

Dim rsOrder, sqlOrder, ItemID
sqlOrder = "select top 1 * from PE_OrderForm"
Set rsOrder = Server.CreateObject("adodb.recordset")
rsOrder.Open sqlOrder, Conn, 1, 3
rsOrder.AddNew
rsOrder("OrderFormID") = OrderFormID
rsOrder("OrderFormNum") = OrderFormNum
rsOrder("UserName") = UserName
rsOrder("ClientID") = ClientID
rsOrder("ContacterName") = ContacterName
rsOrder("Address") = Address
rsOrder("ZipCode") = ZipCode
rsOrder("Mobile") = Mobile
rsOrder("Phone") = Phone
rsOrder("Email") = Email
rsOrder("AgentName") = AgentName
rsOrder("PaymentType") = PaymentType
rsOrder("DeliverType") = DeliverType
rsOrder("NeedInvoice") = NeedInvoice
rsOrder("InvoiceContent") = InvoiceContent
rsOrder("Invoiced") = False
rsOrder("Remark") = Remark
rsOrder("MoneyTotal") = 0
rsOrder("MoneyGoods") = 0
rsOrder("PresentMoney") = 0
rsOrder("PresentExp") = 0
rsOrder("PresentPoint") = 0
rsOrder("MoneyReceipt") = 0
rsOrder("BeginDate") = BeginDate
rsOrder("InputTime") = Now()
rsOrder("Status") = 1
rsOrder("DeliverStatus") = 1
rsOrder("EnableDownload") = False
rsOrder("Discount_Payment") = Discount_Payment
rsOrder("Charge_Deliver") = 0  '先把运费设为0，在下面的代码中会对运费计算后再修改
rsOrder.Update
rsOrder.Close
Set rsOrder = Nothing
        
Set rsItem = Server.CreateObject("adodb.recordset")
rsItem.Open "select top 1 * from PE_OrderFormItem", Conn, 1, 3

'开始向订单明细表中录入数据
dblSubtotal = 0
dblTotal = 0
TotalWeight = 0
Set rsCart = Conn.Execute("select P.*,C.Quantity from PE_Product P inner join PE_ShoppingCarts C on C.ProductID=P.ProductID where C.CartID='" & CartID & "' and C.PresentID=0 and P.ProductType<>4 order by C.CartItemID desc")
Do While Not rsCart.EOF
    dblAmount = rsCart("Quantity")
    If dblAmount <= 0 Then dblAmount = 1
    
    dblPrice = rsCart("Price_Original")
    
    If PE_CLng(UserSetting(30)) = 1 And rsCart("EnableWholesale") = True And dblAmount >= rsCart("Number_Wholesale1") Then
        SaleType = 4 '批发
        If dblAmount < rsCart("Number_Wholesale2") Then
            dblTruePrice = rsCart("Price_Wholesale1")
        Else
            If dblAmount < rsCart("Number_Wholesale3") Then
                dblTruePrice = rsCart("Price_Wholesale2")
            Else
                dblTruePrice = rsCart("Price_Wholesale3")
            End If
        End If
    Else
        SaleType = 1 '正常销售
        Select Case GroupType
        Case 0, 1  '未登录
            dblTruePrice = rsCart("Price")
        Case 2, 3   '注册会员
            Select Case rsCart("ProductType")
            Case 1, 2
                If rsCart("Price_Member") > 0 Then '如果指定了会员价
                    dblTruePrice = rsCart("Price_Member")
                Else
                    dblTruePrice = rsCart("Price") * Discount_Member / 100
                End If
            Case 3, 5
                If rsCart("ProductType") = 3 Then
                    If Date < rsCart("BeginDate") Or Date > rsCart("EndDate") Then
                        dblTempPrice = dblPrice
                    Else
                        dblTempPrice = rsCart("Price")
                    End If
                Else
                    dblTempPrice = rsCart("Price")
                End If
                
                If rsCart("Price_Member") > 0 Then '如果指定了会员价
                    If rsCart("Price_Member") <= dblTempPrice Then
                        dblTruePrice = rsCart("Price_Member")
                    Else
                        dblTruePrice = dblTempPrice
                    End If
                Else
                    If PE_CLng(UserSetting(12)) = 1 Then '如可以享受折上折优惠
                        dblTruePrice = dblTempPrice * Discount_Member / 100
                    Else
                        If dblPrice * Discount_Member / 100 >= dblTempPrice Then
                            dblTruePrice = dblTempPrice
                        Else
                            dblTruePrice = dblPrice * Discount_Member / 100
                        End If
                    End If
                End If
            End Select
        Case 4  '代理商
            dblTempPrice = rsCart("Price")
            If rsCart("Price_Agent") > 0 Then '如果指定了代理价
                dblTruePrice = rsCart("Price_Agent")
            Else
                If Discount_Member = 100 Then
                    dblTruePrice = dblTempPrice
                Else
                    If PE_CLng(UserSetting(12)) = 1 Then '如可以享受折上折优惠
                        dblTruePrice = dblTempPrice * Discount_Member / 100
                    Else
                        If rsCart("Price_Original") * Discount_Member / 100 <= dblTempPrice Then
                            dblTruePrice = rsCart("Price_Original") * Discount_Member / 100
                        Else
                            dblTruePrice = dblTempPrice
                        End If
                    End If
                End If
            End If
        Case Else
            dblTruePrice = rsCart("Price")
        End Select
    End If

    Conn.Execute ("update PE_Product set OrderNum=OrderNum+" & dblAmount & " where ProductID=" & rsCart("ProductID") & "")

    dblSubtotal = dblTruePrice * dblAmount
    dblTotal = dblTotal + dblSubtotal
    TotalWeight = TotalWeight + PE_CDbl(rsCart("Weight")) * dblAmount
    
    '添加进订单详细表中
    ItemID = GetNewID("PE_OrderFormItem", "ItemID")
    rsItem.AddNew
    rsItem("ItemID") = ItemID
    rsItem("OrderFormID") = OrderFormID
    rsItem("ProductID") = rsCart("ProductID")
    rsItem("SaleType") = SaleType
    rsItem("Price_Original") = dblPrice
    rsItem("Price") = dblTruePrice
    rsItem("TruePrice") = dblTruePrice
    rsItem("Amount") = dblAmount
    rsItem("Subtotal") = dblSubtotal
    rsItem("Remark") = ""
    rsItem("BeginDate") = BeginDate
    rsItem("ServiceTerm") = rsCart("ServiceTerm")
    rsItem("PresentExp") = rsCart("PresentExp")
    rsItem("PresentMoney") = rsCart("PresentMoney")
    rsItem("PresentPoint") = rsCart("PresentPoint")
    rsItem.Update

    If rsCart("SalePromotionType") > 0 And dblAmount >= rsCart("MinNumber") Then
        Dim PresentNumber
        If rsCart("SalePromotionType") = 3 Or rsCart("SalePromotionType") = 4 Then
            PresentNumber = rsCart("PresentNumber")
        Else
            PresentNumber = Int(dblAmount / rsCart("MinNumber")) * rsCart("PresentNumber")
        End If
        If rsCart("SalePromotionType") = 1 Or rsCart("SalePromotionType") = 3 Then
            If FoundInArr(PresentList, rsCart("ProductID"), ",") = True Then
                '添加进订单详细表中
                ItemID = GetNewID("PE_OrderFormItem", "ItemID")
                rsItem.AddNew
                rsItem("ItemID") = ItemID
                rsItem("OrderFormID") = OrderFormID
                rsItem("ProductID") = rsCart("ProductID")
                rsItem("SaleType") = 3 '赠送
                rsItem("Price_Original") = dblPrice
                rsItem("Price") = 0
                rsItem("TruePrice") = 0
                rsItem("Amount") = PresentNumber
                rsItem("Subtotal") = 0
                If rsCart("SalePromotionType") = 3 Then
                    rsItem("Remark") = "买就送" & rsCart("PresentNumber") & "同样商品"
                Else
                    rsItem("Remark") = "买" & rsCart("MinNumber") & "送" & rsCart("PresentNumber") & "同样商品"
                End If
                rsItem("BeginDate") = BeginDate
                rsItem("ServiceTerm") = rsCart("ServiceTerm")
                rsItem("PresentExp") = 0
                rsItem("PresentMoney") = 0
                rsItem("PresentPoint") = 0
                rsItem.Update
                
                TotalWeight = TotalWeight + PE_CDbl(rsCart("Weight")) * PresentNumber
                
                Conn.Execute ("update PE_Product set OrderNum=OrderNum+" & PresentNumber & " where ProductID=" & rsCart("ProductID") & "")
                
            End If
        Else
            Dim rsPresent, sqlPresent, strPresentType
            sqlPresent = "select * from PE_Product where ProductNum='" & rsCart("PresentID") & "' and ProductType=4"
            Set rsPresent = Server.CreateObject("adodb.recordset")
            rsPresent.Open sqlPresent, Conn, 1, 3
            If Not (rsPresent.BOF And rsPresent.EOF) Then
                If FoundInArr(PresentList, rsPresent("ProductID"), ",") = True Then
                    If rsPresent("Price") > 0 Then
                        SaleType = 2
                    Else
                        SaleType = 3
                    End If
                    '添加进订单详细表中
                    ItemID = GetNewID("PE_OrderFormItem", "ItemID")
                    rsItem.AddNew
                    rsItem("ItemID") = ItemID
                    rsItem("OrderFormID") = OrderFormID
                    rsItem("ProductID") = rsPresent("ProductID")
                    rsItem("SaleType") = SaleType
                    rsItem("Price_Original") = rsPresent("Price_Original")
                    rsItem("Price") = rsPresent("Price")
                    rsItem("TruePrice") = rsPresent("Price")
                    rsItem("Amount") = PresentNumber
                    rsItem("Subtotal") = 0
                    If rsCart("SalePromotionType") = 4 Then
                        rsItem("Remark") = "买就送/换购" & rsCart("PresentNumber") & "指定商品"
                    Else
                        rsItem("Remark") = "买" & rsCart("MinNumber") & "送/换购" & rsCart("PresentNumber") & "指定商品"
                    End If
                    rsItem("BeginDate") = BeginDate
                    rsItem("ServiceTerm") = rsPresent("ServiceTerm")
                    rsItem("PresentExp") = 0
                    rsItem("PresentPoint") = 0
                    rsItem("PresentMoney") = 0
                    rsItem.Update
                    dblTotal = dblTotal + rsPresent("Price") * PresentNumber
                    rsPresent("OrderNum") = rsPresent("OrderNum") + PresentNumber
                    rsPresent.Update
                    
                    TotalWeight = TotalWeight + PE_CDbl(rsPresent("Weight")) * PresentNumber
                End If
            End If
            rsPresent.Close
            Set rsPresent = Nothing
        End If
    End If
    rsCart.MoveNext
Loop
rsCart.Close
Set rsCart = Nothing

'检查促销方案
Dim PresentID2
PresentID2 = PE_CLng(Trim(Request("PresentID2")))
Set rsPresent = Conn.Execute("select * from PE_PresentProject where MinMoney<=" & dblTotal & " and MaxMoney>" & dblTotal & " and MaxMoney>" & dblTotal & " and BeginDate<=" & PE_Now & " and EndDate>=" & PE_Now & "")
If Not (rsPresent.BOF And rsPresent.EOF) Then
    If FoundInArr(rsPresent("PresentContent"), "1", ",") And PresentID2 > 0 Then
        Set rsCart = Server.CreateObject("adodb.recordset")
        rsCart.Open "select * from PE_Product where ProductID=" & PresentID2 & " and ProductType=4", Conn, 1, 3
        If Not (rsCart.BOF And rsCart.EOF) Then
            '添加进订单详细表中
            ItemID = GetNewID("PE_OrderFormItem", "ItemID")
            rsItem.AddNew
            rsItem("ItemID") = ItemID
            rsItem("OrderFormID") = OrderFormID
            rsItem("ProductID") = rsCart("ProductID")
            rsItem("SaleType") = 2
            rsItem("Price_Original") = rsCart("Price_Original")
            rsItem("Price") = rsPresent("Price")
            rsItem("TruePrice") = rsPresent("Price")
            rsItem("Amount") = 1
            rsItem("Subtotal") = rsPresent("Price")
            rsItem("Remark") = "超值换购"
            rsItem("BeginDate") = BeginDate
            rsItem("ServiceTerm") = rsCart("ServiceTerm")
            rsItem("PresentExp") = 0
            rsItem("PresentMoney") = 0
            rsItem("PresentPoint") = 0
            rsItem.Update
            dblTotal = dblTotal + rsPresent("Price")
            rsCart("OrderNum") = rsCart("OrderNum") + 1
            rsCart.Update
            
            TotalWeight = TotalWeight + PE_CDbl(rsCart("Weight"))
        End If
        rsCart.Close
        Set rsCart = Nothing
    End If

    If FoundInArr(rsPresent("PresentContent"), "2", ",") = True Then
        PresentMoney = rsPresent("Cash")
    End If
    If FoundInArr(rsPresent("PresentContent"), "3", ",") = True Then
        PresentExp = rsPresent("PresentExp")
    End If
    If FoundInArr(rsPresent("PresentContent"), "4", ",") = True Then
        PresentPoint = rsPresent("PresentPoint")
    End If

End If
Set rsPresent = Nothing
rsItem.Close
Set rsItem = Nothing

dblTotal2 = dblTotal
If Discount_Payment > 0 And Discount_Payment < 100 Then
    dblTotal = dblTotal * Discount_Payment / 100
End If

Select Case ChargeType
Case 0
    Charge_Deliver = 0
Case 1
    If TotalWeight > 0 Then
        If TotalWeight > Weight_Min Then
            Dim iTemp
            iTemp = (TotalWeight - Weight_Min) / WeightPerUnit
            If iTemp > Fix(iTemp) Then
                iTemp = Fix(iTemp) + 1
            End If
            Charge_Deliver = Charge_Min + iTemp * ChargePerUnit
            If Charge_Deliver > Charge_Max Then
                Charge_Deliver = Charge_Max
            End If
        Else
            Charge_Deliver = Charge_Min
        End If
    Else
        Charge_Deliver = 0
    End If
Case 2
    Charge_Deliver = dblTotal2 * Charge_Percent / 100 + Charge_Min
    If Charge_Deliver > Charge_Max Then
        Charge_Deliver = Charge_Max
    End If
End Select
        
If Charge_Deliver > 0 And ReleaseType > 0 And dblTotal2 >= MinMoney1 Then
    Charge_Deliver_Original = Charge_Deliver
    If Charge_Deliver <= ReleaseCharge Then
        Charge_Deliver = 0
    Else
        Charge_Deliver = Charge_Deliver - ReleaseCharge
    End If
    If dblTotal2 >= MinMoney2 Then
        If dblTotal2 >= MinMoney3 Then
            Charge_Deliver = 0
        Else
            If Charge_Deliver_Original <= MaxCharge Then
                Charge_Deliver = 0
            End If
        End If
    End If
End If

dblTotal = dblTotal + Charge_Deliver
   
If IncludeTax = True Then
    If NeedInvoice = False Then
        dblTotal = dblTotal * (100 - TaxRate) / 100
    End If
Else
    If NeedInvoice = True Then
        dblTotal = dblTotal * (100 + TaxRate) / 100
    End If
End If

Conn.Execute ("update PE_OrderForm set  MoneyGoods=" & dblTotal2 & ",Charge_Deliver=" & Charge_Deliver & ",MoneyTotal=" & Round(dblTotal, 2) & ",PresentMoney=" & PresentMoney & ",PresentExp=" & PresentExp & ",PresentPoint=" & PresentPoint & " where OrderFormID=" & OrderFormID & "")

'给管理员发送手机短信
If EnableSMS = True And SendMessageToAdminWhenOrder = True And Mobiles <> "" And MessageOfOrder <> "" Then
    MessageOfOrder = Replace(MessageOfOrder, "{$OrderFormID}", OrderFormID)
    MessageOfOrder = Replace(MessageOfOrder, "{$OrderFormNum}", OrderFormNum)
    MessageOfOrder = Replace(MessageOfOrder, "{$MoneyTotal}", FormatNumber(dblTotal, 2, vbTrue, vbFalse, vbTrue))
    MessageOfOrder = Replace(MessageOfOrder, "{$MoneyReceipt}", "0")
    MessageOfOrder = Replace(MessageOfOrder, "{$MoneyNeedPay}", FormatNumber(dblTotal, 2, vbTrue, vbFalse, vbTrue))
    MessageOfOrder = Replace(MessageOfOrder, "{$InputTime}", Now())
    MessageOfOrder = Replace(MessageOfOrder, "{$UserName}", UserName)
    MessageOfOrder = Replace(MessageOfOrder, "{$ContacterName}", ContacterName)
    MessageOfOrder = Replace(MessageOfOrder, "{$Address}", Address)
    MessageOfOrder = Replace(MessageOfOrder, "{$ZipCode}", ZipCode)
    MessageOfOrder = Replace(MessageOfOrder, "{$Mobile}", Mobile)
    MessageOfOrder = Replace(MessageOfOrder, "{$Phone}", Phone)
    MessageOfOrder = Replace(MessageOfOrder, "{$Email}", Email)
    MessageOfOrder = Replace(MessageOfOrder, "{$PaymentType}", PaymentTypeName)
    MessageOfOrder = Replace(MessageOfOrder, "{$DeliverType}", DeliverTypeName)
    MessageOfOrder = Replace(MessageOfOrder, "{$OrderStatus}", "等待确认")
    MessageOfOrder = Replace(MessageOfOrder, "{$PayStatus}", "等待付款")
    MessageOfOrder = Replace(MessageOfOrder, "{$DeliverStatus}", "配送中")
    MessageOfOrder = Replace(MessageOfOrder, "{$Charge_Deliver}", Charge_Deliver)
    MessageOfOrder = Replace(MessageOfOrder, "{$PresentMoney}", PresentMoney)
    MessageOfOrder = Replace(MessageOfOrder, "{$PresentExp}", PresentExp)
    MessageOfOrder = Replace(MessageOfOrder, "{$PresentPoint}", PresentPoint)
    MessageOfOrder = Replace(MessageOfOrder, "{$Discount_Payment}", Discount_Payment)

    Call PostSMS(Mobiles, MessageOfOrder, "")
End If

strHtml = GetTemplate(ChannelID, 12, 0)
Call ReplaceCommon
Call ReplaceUserInfo

strHtml = Replace(strHtml, "{$OrderFormID}", OrderFormID)
strHtml = Replace(strHtml, "{$OrderFormNum}", OrderFormNum)
strHtml = Replace(strHtml, "{$TotalMoney}", FormatNumber(dblTotal, 2, vbTrue, vbFalse, vbTrue))

strHtml = Replace(strHtml, "{$ShowCart}", ShowCart2(True))

Response.Write strHtml
Call DelCart(CartID)
Call CloseConn
%>
