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

Dim ClientName, ShortedForm
Dim OrderType

Sub Execute()
    Dim rsClient
    Set rsClient = Conn.Execute("select ClientName,ShortedForm from PE_Client where ClientID=" & ClientID & "")
    If rsClient.BOF And rsClient.EOF Then
        ClientName = ""
        ShortedForm = ""
    Else
        ClientName = rsClient(0)
        ShortedForm = rsClient(1)
    End If
    rsClient.Close
    Set rsClient = Nothing

    SearchType = PE_CLng(Trim(Request("SearchType")))
    OrderType = PE_CLng(Trim(Request("OrderType")))
    strFileName = "User_Order.asp?SearchType=" & SearchType & "&OrderType=" & OrderType & "&Field=" & strField & "&Keyword=" & Keyword
  
    Call ShowJS_Main("订单")
    Response.Write "<br>"
    
    Select Case Action
    Case "ShowOrder"
        Call ShowOrder
    Case "AddPayment"
        Call AddPayment
    Case "SavePayment"
        Call SavePayment
    Case "DelOrder"
        Call DelOrder
    Case "Received"
        Call Received
    Case "Received2"
        Call Received2
    Case "AgentPayment"
        Call AgentPayment
    Case Else
        Call main
    End Select
    If FoundErr = True Then
        Call WriteErrMsg(ErrMsg, ComeUrl)
    End If
    Call CloseConn
End Sub

Sub main()
    Dim rsOrderList, sql, sqlMoney
    Dim MoneyTotal, MoneyReceipt
    MoneyTotal = 0
    MoneyReceipt = 0
        
    Response.Write "       <table width='100%'  border='0' cellspacing='1' cellpadding='2' class='border2'>"
    Response.Write "         <tr align='center' class='title'>"
    Response.Write "           <td width='110'>订单编号</td>"
    Response.Write "           <td>客户名称</td>"
    Response.Write "           <td>用户名</td>"
    Response.Write "           <td width='120'>下单时间</td>"
    Response.Write "           <td width='70'>订单金额</td>"
    Response.Write "           <td width='70'>收款金额</td>"
    Response.Write "           <td width='30'>需要<br>发票</td>"
    Response.Write "           <td width='30'>已开<br>发票</td>"
    Response.Write "           <td width='50'>订单状态</td>"
    Response.Write "           <td width='50'>付款状态</td>"
    Response.Write "           <td width='50'>物流状态</td>"
    Response.Write "         </tr>"
    sql = "select  O.OrderFormID,O.OrderFormNum,O.InputTime,O.UserName,O.ClientID,C.ShortedForm as ClientName,O.MoneyTotal,O.MoneyReceipt,O.NeedInvoice,O.Invoiced,O.Status,O.DeliverStatus from PE_OrderForm O left join PE_Client C on O.ClientID=C.ClientID "
    If OrderType = 1 Then
        sql = sql & " where AgentName='" & UserName & "' order by OrderFormID desc"
    Else
        sql = sql & " where UserName='" & UserName & "' order by OrderFormID desc"
    End If
    Set rsOrderList = Server.CreateObject("Adodb.RecordSet")
    rsOrderList.Open sql, Conn, 1, 1
    If rsOrderList.BOF And rsOrderList.EOF Then
        totalPut = 0
        Response.Write "<tr class='tdbg' height='50'><td colspan='20' align='center'>没有任何符合条件的订单！</td></tr>"
    Else
        totalPut = rsOrderList.RecordCount
        If CurrentPage < 1 Then
            CurrentPage = 1
        End If
        If (CurrentPage - 1) * MaxPerPage > totalPut Then
            If (totalPut Mod MaxPerPage) = 0 Then
                CurrentPage = totalPut \ MaxPerPage
            Else
                CurrentPage = totalPut \ MaxPerPage + 1
            End If
        End If
        If CurrentPage > 1 Then
            If (CurrentPage - 1) * MaxPerPage < totalPut Then
                rsOrderList.Move (CurrentPage - 1) * MaxPerPage
            Else
                CurrentPage = 1
            End If
        End If

        Dim i
        i = 0
        Do While Not rsOrderList.EOF
            Response.Write "         <tr align='center' class='tdbg' onmouseout=""this.className='tdbg'"" onmouseover=""this.className='tdbgmouseover'"">"
            Response.Write "           <td width='110'>"
            Response.Write "<a href='User_Order.asp?Action=ShowOrder&OrderType=" & OrderType & "&OrderFormID=" & rsOrderList("OrderFormID") & "'>" & rsOrderList("OrderFormNum") & "</a></td>"
            Response.Write "           <td>" & rsOrderList("ClientName") & "</td>"
            Response.Write "           <td>" & rsOrderList("UserName") & "</td>"
            Response.Write "           <td width='120'>" & rsOrderList("InputTime") & "</td>"
            Response.Write "           <td width='70' align='right'>" & FormatNumber(rsOrderList("MoneyTotal"), 2, vbTrue, vbFalse, vbTrue) & "</td>"
            Response.Write "           <td width='70' align='right'>"
            If rsOrderList("MoneyReceipt") < rsOrderList("MoneyTotal") Then
                Response.Write "<font color='red'>" & FormatNumber(rsOrderList("MoneyReceipt"), 2, vbTrue, vbFalse, vbTrue) & "</font>"
            Else
                Response.Write FormatNumber(rsOrderList("MoneyReceipt"), 2, vbTrue, vbFalse, vbTrue)
            End If
            Response.Write "</td>"
            Response.Write "           <td width='30'>"
            If rsOrderList("NeedInvoice") = True Then
                Response.Write "<font color='red'>√</font>"
            End If
            Response.Write "</td>"
            Response.Write "           <td width='30'>"
            If rsOrderList("NeedInvoice") = True Then
                If rsOrderList("Invoiced") = True Then
                    Response.Write "√"
                Else
                    Response.Write "<font color='red'>×</font>"
                End If
            End If
            Response.Write "</td>"
            Response.Write "           <td width='50'>"
            Select Case rsOrderList("Status")
            Case 0, 1
                Response.Write "<font color='red'>等待确认</font>"
            Case 2, 3
                Response.Write "<font color='blue'>已经确认</font>"
            Case 4
                Response.Write "<font color='gray'>已结清</font>"
            End Select
            Response.Write "</td>"
            Response.Write "           <td width='50'>"
            If rsOrderList("MoneyTotal") > rsOrderList("MoneyReceipt") Then
                If rsOrderList("MoneyReceipt") > 0 Then
                    Response.Write "<font color='green'>已收定金</font>"
                Else
                    Response.Write "<font color='red'>等待汇款</font>"
                End If
            Else
                Response.Write "<font color='blue'>已经付清</font>"
            End If
            Response.Write "</td>"
            Response.Write "           <td width='50'>"
            Select Case rsOrderList("DeliverStatus")
            Case 0, 1
                Response.Write "<font color='red'>配送中</font>"
            Case 2
                Response.Write "<font color='blue'>已发货</font>"
            Case 3
                Response.Write "<font color='green'>已签收</font>"
            End Select
            Response.Write "</td>"
             
            MoneyTotal = MoneyTotal + rsOrderList("MoneyTotal")
            MoneyReceipt = MoneyReceipt + rsOrderList("MoneyReceipt")
            i = i + 1
            If i >= MaxPerPage Then Exit Do
            rsOrderList.MoveNext
        Loop
    End If
    rsOrderList.Close
    Set rsOrderList = Nothing
    
    
    Response.Write "         <tr align='center' class='tdbg' onmouseout=""this.className='tdbg'"" onmouseover=""this.className='tdbgmouseover'"">"
    Response.Write "           <td colspan='4' align='right'><b>本页合计：</b></td>"
    Response.Write "           <td align='right'>" & FormatNumber(MoneyTotal, 2, vbTrue, vbFalse, vbTrue) & "</td>"
    Response.Write "           <td align='right'>" & FormatNumber(MoneyReceipt, 2, vbTrue, vbFalse, vbTrue) & "</td>"
    Response.Write "           <td colspan='5'>&nbsp;</td>"
    Response.Write "         </tr>"
    
    Dim rsCount
    If OrderType = 1 Then
        Set rsCount = Conn.Execute("select sum(MoneyTotal),sum(MoneyReceipt) from PE_OrderForm where AgentName='" & UserName & "'")
    Else
        Set rsCount = Conn.Execute("select sum(MoneyTotal),sum(MoneyReceipt) from PE_OrderForm where UserName='" & UserName & "'")
    End If
    If IsNull(rsCount(0)) Then
        MoneyTotal = 0
    Else
        MoneyTotal = rsCount(0)
    End If
    If IsNull(rsCount(1)) Then
        MoneyReceipt = 0
    Else
        MoneyReceipt = rsCount(1)
    End If
    Set rsCount = Nothing
    Response.Write "         <tr align='center' class='tdbg' onmouseout=""this.className='tdbg'"" onmouseover=""this.className='tdbgmouseover'"">"
    Response.Write "           <td colspan='4' align='right'><b>所有总计：</b></td>"
    Response.Write "           <td align='right'>" & FormatNumber(MoneyTotal, 2, vbTrue, vbFalse, vbTrue) & "</td>"
    Response.Write "           <td align='right'>" & FormatNumber(MoneyReceipt, 2, vbTrue, vbFalse, vbTrue) & "</td>"
    Response.Write "           <td colspan='5'>&nbsp;</td>"
    Response.Write "         </tr>"
    Response.Write "       </table>"
    Response.Write ShowPage(strFileName, totalPut, MaxPerPage, CurrentPage, True, True, "个订单", True)
End Sub

Sub ShowOrder()
    Dim OrderFormID
    OrderFormID = Trim(Request("OrderFormID"))
    If OrderFormID = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定OrderFormID！</li>"
        Exit Sub
    Else
        OrderFormID = PE_CLng(OrderFormID)
    End If

    Response.Write GetOrderInfo(OrderFormID, UserName, True, OrderType)
End Sub

Sub AddPayment()
    Dim OrderFormID, Money, trs, Remark
    Dim rsOrderForm, sqlOrderForm
    OrderFormID = Trim(Request("OrderFormID"))
    If OrderFormID = "" Then
        OrderFormID = 0
    Else
        OrderFormID = PE_CLng(OrderFormID)
    End If
    
    If Balance <= 0 Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>您的资金余额不足！不能使用虚拟货币支付。</li>"
    End If
    
    If FoundErr = True Then Exit Sub
    
    sqlOrderForm = "select OrderFormNum,Status,MoneyTotal,MoneyReceipt from PE_OrderForm where OrderFormID=" & OrderFormID
    Set rsOrderForm = Conn.Execute(sqlOrderForm)
    If rsOrderForm.BOF And rsOrderForm.EOF Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到指定的订单！</li>"
        rsOrderForm.Close
        Set rsOrderForm = Nothing
        Exit Sub
    End If
    If rsOrderForm("MoneyTotal") <= rsOrderForm("MoneyReceipt") Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>此订单已经付清，无需再支付！</li>"
        rsOrderForm.Close
        Set rsOrderForm = Nothing
        Exit Sub
    End If
    Money = rsOrderForm("MoneyTotal") - rsOrderForm("MoneyReceipt")
    If Balance <= Money Then
        Money = Balance
    End If

    Response.Write "<form name='form4' method='post' action='User_Order.asp' onsubmit=""return confirm('确定所输入的信息都完全正确吗？一旦确认就不可更改哦！')"">"
    Response.Write "  <table width='100%' border='0' cellpadding='2' cellspacing='1' class='border2'>"
    Response.Write "    <tr align='center' class='title'>"
    Response.Write "      <td height='22' colspan='2'><b>使用虚拟货币支付订单</b></td>"
    Response.Write "    </tr>"
    Response.Write "    <tr class='tdbg'>"
    Response.Write "      <td align='right'><b>用 户 名：</b></td>"
    Response.Write "      <td align='left'><input name='UserName' type='text' id='UserName' value='" & UserName & "' size='30' maxlength='50' disabled></td>"
    Response.Write "    </tr>"
    Response.Write "    <tr class='tdbg'>"
    Response.Write "      <td align='right'><b>客户名称：</b></td>"
    Response.Write "      <td align='left'><input name='ClientName' type='text' id='ClientName' value='" & ClientName & "' size='30' maxlength='50' disabled></td>"
    Response.Write "    </tr>"
    Response.Write "    <tr class='tdbg'>"
    Response.Write "      <td align='right'><b>资金余额：</b></td>"
    Response.Write "      <td align='left'><input name='Balance' type='text' id='Balance' value='" & Balance & "' size='30' maxlength='50' disabled></td>"
    Response.Write "    </tr>"
    Response.Write "    <tr class='tdbg'>"
    Response.Write "      <td align='right'><b>支付内容：</b></td>"
    Response.Write "      <td align='left'><table  border='0' cellspacing='2' cellpadding='0'>"
    Response.Write "        <tr>"
    Response.Write "          <td align='right'>订单编号：</td>"
    Response.Write "          <td align='left'><input name='OrderFormNum' type='text' id='OrderFormNum' value='" & rsOrderForm("OrderFormNum") & "' size='30' maxlength='30' disabled></td>"
    Response.Write "        </tr>"
    Response.Write "        <tr>"
    Response.Write "          <td align='right'>订单金额：</td>"
    Response.Write "          <td align='left'><input name='MoneyTotal' type='text' id='MoneyTotal' value='" & rsOrderForm("MoneyTotal") & "元' size='30' maxlength='30' disabled></td>"
    Response.Write "        </tr>"
    Response.Write "        <tr>"
    Response.Write "          <td align='right'>已 付 款：</td>"
    Response.Write "          <td align='left'><input name='MoneyTotal' type='text' id='MoneyTotal' value='" & rsOrderForm("MoneyReceipt") & "元' size='30' maxlength='30' disabled></td>"
    Response.Write "        </tr>"
    Response.Write "      </table></td>"
    Response.Write "    </tr>"
    Response.Write "    <tr class='tdbg'>"
    Response.Write "      <td align='right'><b>支出金额：</b></td>"
    Response.Write "      <td align='left'><input name='Money' type='text' id='Money' value='" & Money & "' size='10' maxlength='20'> 元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='#0000FF'>支付成功后，将从您的资金余额中扣除相应款项。</font></td>"
    Response.Write "    </tr>"
    Response.Write "    <tr class='tdbg'>"
    Response.Write "      <td height='30' colspan='2'><b><font color='#FF0000'>注意：支出信息一旦录入，就不能再修改或修改！所以在保存之前确认输入无误！</font></b></td>"
    Response.Write "    </tr>"
    Response.Write "    <tr align='center' class='tdbg'>"
    Response.Write "      <td height='30' colspan='2'><input name='Action' type='hidden' id='Action' value='SavePayment'>"
    If OrderType = 1 Then
        Response.Write "  <input name='OrderType' type='hidden' id='OrderType' value='1'>"
    End If
    Response.Write "      <input name='OrderFormID' type='hidden' id='OrderFormID' value='" & OrderFormID & "'>"
    Response.Write "      <input type='submit' name='Submit' value=' 确认支付 '></td>"
    Response.Write "    </tr>"
    Response.Write "  </table>"
    Response.Write "</form>"
    rsOrderForm.Close
    Set rsOrderForm = Nothing
End Sub

Sub DelOrder()
    Dim OrderFormID
    Dim rsOrder, sqlOrder
    OrderFormID = Trim(Request("OrderFormID"))
    If OrderFormID = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定订单ID！</li>"
        Exit Sub
    Else
        OrderFormID = PE_CLng(OrderFormID)
    End If
    
    If OrderType = 1 Then
        sqlOrder = "select * from PE_OrderForm where AgentName='" & UserName & "' and OrderFormID=" & OrderFormID & ""
    Else
        sqlOrder = "select * from PE_OrderForm where UserName='" & UserName & "' and OrderFormID=" & OrderFormID & ""
    End If

    Set rsOrder = Server.CreateObject("Adodb.RecordSet")
    rsOrder.Open sqlOrder, Conn, 1, 3
    If rsOrder.BOF And rsOrder.EOF Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到指定的订单！</li>"
        rsOrder.Close
        Set rsOrder = Nothing
        Exit Sub
    End If
    If rsOrder("Status") > 1 Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>此订单已经确认，不能再删除！</li>"
    End If
    If rsOrder("MoneyReceipt") > 0 Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>此订单已经付款，不能再删除！</li>"
    End If
    If rsOrder("DeliverStatus") > 1 Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>此订单已经发货，不能再删除！</li>"
    End If
    If FoundErr = True Then
        rsOrder.Close
        Set rsOrder = Nothing
        Exit Sub
    End If
    
    Dim rsOrderItem, sqlOrderItem
    sqlOrderItem = "select ProductID,Amount from PE_OrderFormItem where OrderFormID=" & OrderFormID
    Set rsOrderItem = Server.CreateObject("Adodb.RecordSet")
    rsOrderItem.Open sqlOrderItem, Conn, 1, 3
    Do While Not rsOrderItem.EOF
        Conn.Execute ("update PE_Product set OrderNum=OrderNum-" & rsOrderItem("Amount") & " where ProductID=" & rsOrderItem("ProductID") & "")
        rsOrderItem.Delete
        rsOrderItem.MoveNext
    Loop
    rsOrderItem.Close
    Set rsOrderItem = Nothing
    
    rsOrder.Delete
    rsOrder.Update
    rsOrder.Close
    Set rsOrder = Nothing
    Call CloseConn
    Response.Redirect "User_Order.asp?OrderType=" & OrderType
End Sub

Sub SavePayment()
    Dim OrderFormID, trs, ItemID, rsBankroll
    Dim rsOrderForm, sqlOrderForm
    Dim Money, Remark
    Dim HaveOtherProduct
    
    OrderFormID = Trim(Request("OrderFormID"))
    Money = Trim(Request("Money"))
    If OrderFormID = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定订单ID！"
    Else
        OrderFormID = PE_CLng(OrderFormID)
    End If
    If Money = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请输入支付金额！</li>"
    Else
        Money = Abs(PE_CDbl(Money))
    End If
    
    If FoundErr = True Then Exit Sub
    
    If Balance <= 0 Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>您的资金余额不足！不能使用虚拟货币支付。</li>"
    End If
    If Balance < Money Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>您输入的支付金额超过了您的资金余额，无效支付！</li>"
    End If
    
    If FoundErr = True Then Exit Sub
    
    Set rsOrderForm = Server.CreateObject("Adodb.RecordSet")
    sqlOrderForm = "select OrderFormNum,ContacterName,MoneyTotal,MoneyReceipt,EnableDownload,Status,DeliverStatus from PE_OrderForm where OrderFormID=" & OrderFormID
    rsOrderForm.Open sqlOrderForm, Conn, 1, 3
    If rsOrderForm.BOF And rsOrderForm.EOF Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到指定的订单！</li>"
        rsOrderForm.Close
        Set rsOrderForm = Nothing
        Exit Sub
    End If
    If rsOrderForm("MoneyTotal") <= rsOrderForm("MoneyReceipt") Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>此订单已经付清，无需再支付！</li>"
        rsOrderForm.Close
        Set rsOrderForm = Nothing
        Exit Sub
    End If
    If rsOrderForm("MoneyReceipt") + Money >= rsOrderForm("MoneyTotal") Then
        rsOrderForm("EnableDownload") = True
        
        HaveOtherProduct = False
        Dim rsOrderItem, rsCard, sqlCard
        Set rsCard = Server.CreateObject("Adodb.Recordset")
        Set rsOrderItem = Conn.Execute("select I.ItemID,P.ProductID,P.ProductName,P.ProductKind,I.Amount from PE_OrderFormItem I inner join PE_Product P on I.ProductID=P.ProductID where I.OrderFormID=" & OrderFormID & " order by I.ItemID")
        Do While Not rsOrderItem.EOF
            If rsOrderItem("ProductKind") = 3 Then
                Set trs = Conn.Execute("select top 1 CardID from PE_Card where ProductID=" & rsOrderItem("ProductID") & " and OrderFormItemID=" & rsOrderItem("ItemID") & "")
                If trs.BOF And trs.EOF Then
                    sqlCard = "select top " & rsOrderItem("Amount") & " CardID,ProductID,OrderFormItemID from PE_Card where ProductID=" & rsOrderItem("ProductID") & " and OrderFormItemID=0 order by CardID"
                    rsCard.Open sqlCard, Conn, 1, 3
                    If rsCard.RecordCount >= rsOrderItem("Amount") Then
                        Do While Not rsCard.EOF
                            rsCard(2) = rsOrderItem(0)
                            rsCard.Update
                            rsCard.MoveNext
                        Loop
                        Conn.Execute ("update PE_Product set Stocks=Stocks-" & rsOrderItem("Amount") & ",OrderNum=OrderNum-" & rsOrderItem("Amount") & " where ProductID=" & rsOrderItem("ProductID") & "")
                    Else
                        HaveOtherProduct = True
                    End If
                    rsCard.Close
                End If
            Else
                HaveOtherProduct = True
            End If
            rsOrderItem.MoveNext
        Loop
        Set rsOrderItem = Nothing
        Set rsCard = Nothing
        If HaveOtherProduct = False Then
            rsOrderForm("DeliverStatus") = 2
        End If

    End If
    rsOrderForm("MoneyReceipt") = rsOrderForm("MoneyReceipt") + Money
    If rsOrderForm("Status") <= 2 Then
        rsOrderForm("Status") = 3
    End If
    rsOrderForm.Update
    
    '给客户的资金余额中减去相应款项
    Conn.Execute ("update PE_User set Balance=Balance-" & Money & " where UserName='" & UserName & "'")
                
    '向资金明细表中添加支出记录
    Call AddBankrollItem("System", UserName, ClientID, Money, 4, "", 0, 2, OrderFormID, 0, "支付订单费用，订单号：" & rsOrderForm("OrderFormNum"), Now())
    
    rsOrderForm.Close
    Set rsOrderForm = Nothing

    Call CloseConn
    Response.Redirect "User_Order.asp?Action=ShowOrder&OrderType=" & OrderType & "&OrderFormID=" & OrderFormID
End Sub

Sub AgentPayment()
    Dim OrderFormID, rsOrder, dblTotal, Discount_Payment, Charge_Deliver
    Dim Money, Margin, AgentBalance
    Dim rsOrderItem, rsBankroll, ItemID, dblAmount, dblSubtotal, Price_Agent
    Dim rsAgent, AgentClientID, Discount_Member, AgentGroupSetting, IsRepeatDiscount
    OrderFormID = Trim(Request("OrderFormID"))
    If OrderFormID = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定订单ID！</li>"
    Else
        OrderFormID = PE_CLng(OrderFormID)
    End If
    If FoundErr = True Then Exit Sub

    Set rsOrder = Server.CreateObject("adodb.recordset")
    rsOrder.Open "select * from PE_OrderForm where OrderFormID=" & OrderFormID & "", Conn, 1, 3
    If Not (rsOrder.BOF And rsOrder.EOF) Then
        If rsOrder("AgentName") <> "" And rsOrder("MoneyReceipt") < rsOrder("MoneyTotal") Then
            Set rsAgent = Conn.Execute("select U.Balance,U.ClientID,G.GroupSetting,U.SpecialPermission,U.UserSetting from PE_User U inner join PE_UserGroup G On U.GroupID = G.GroupID where UserName='" & rsOrder("AgentName") & "'")
            If rsAgent.BOF And rsAgent.EOF Then
                FoundErr = True
                ErrMsg = ErrMsg & "<li>找不到指定的代理商！</li>"
            Else
                AgentBalance = rsAgent("Balance")
                AgentClientID = rsAgent("ClientID")
                If rsAgent("SpecialPermission") Then
                    AgentGroupSetting = Split(rsAgent("UserSetting"), ",")
                Else
                    AgentGroupSetting = Split(rsAgent("GroupSetting"), ",")
                End If
                Discount_Member = PE_CDbl(AgentGroupSetting(11))
                IsRepeatDiscount = PE_CLng(AgentGroupSetting(12))
            End If
            Set rsAgent = Nothing
            If rsOrder("AgentName") <> UserName Then
                FoundErr = True
                ErrMsg = ErrMsg & "<li>对不起，你没有此项操作的权限！</li>"
            End If
            If FoundErr = True Then
                Call WriteErrMsg(ErrMsg, "User_Order.asp?Action=ShowOrder&OrderType=1&OrderFormID=" & OrderFormID & "")
                rsOrder.Close
                Set rsOrder = Nothing
                FoundErr = False
                Exit Sub
            End If

            '计算代理差额
            If rsOrder("AgentName") <> rsOrder("UserName") Then
                Set rsOrderItem = Conn.Execute("select I.SaleType,P.Price_Original,P.Price,I.TruePrice,I.Amount,I.SubTotal,P.Price_Agent from PE_OrderFormItem I inner join PE_Product P on I.ProductID=P.ProductID where I.OrderFormID=" & OrderFormID & " order by I.ItemID")
                Do While Not rsOrderItem.EOF
                    dblAmount = rsOrderItem("Amount")
                    If rsOrderItem("SaleType") = 1 Then
                        If rsOrderItem("Price_Agent") > 0 Then '如果指定了代理价
                            Price_Agent = rsOrderItem("Price_Agent")
                        Else
                            If Discount_Member = 100 Then
                                Price_Agent = rsOrderItem("Price")
                            Else
                                If IsRepeatDiscount = 1 Then '如可以享受折上折优惠
                                    Price_Agent = rsOrderItem("Price") * Discount_Member / 100
                                Else
                                    If rsOrderItem("Price_Original") * Discount_Member / 100 <= rsOrderItem("Price") Then
                                        Price_Agent = rsOrderItem("Price_Original") * Discount_Member / 100
                                    Else
                                        Price_Agent = rsOrderItem("Price")
                                    End If
                                End If
                            End If
                        End If
                        dblSubtotal = Price_Agent * dblAmount
                    Else
                        dblSubtotal = rsOrderItem("SubTotal")
                    End If
                    dblTotal = dblTotal + dblSubtotal
                    rsOrderItem.MoveNext
                Loop
                Set rsOrderItem = Nothing
                Discount_Payment = rsOrder("Discount_Payment")
                Charge_Deliver = rsOrder("Charge_Deliver")
                If Discount_Payment > 0 And Discount_Payment < 100 Then
                    dblTotal = dblTotal * Discount_Payment / 100
                End If
                dblTotal = dblTotal + Charge_Deliver
                If IncludeTax = True Then
                    If rsOrder("NeedInvoice") <> True Then
                        dblTotal = dblTotal * (100 - TaxRate) / 100
                    End If
                Else
                    If rsOrder("NeedInvoice") = True Then
                        dblTotal = dblTotal * (100 + TaxRate) / 100
                    End If
                End If
                dblTotal = Round(dblTotal, 2)
                Margin = rsOrder("MoneyTotal") - dblTotal
            Else
                Margin = 0
            End If
            If Margin < 0 Then Margin = 0

            Money = rsOrder("MoneyTotal") - rsOrder("MoneyReceipt")
            If AgentBalance < Money - Margin Then
                FoundErr = True
                ErrMsg = ErrMsg & "<li>代理商资金余额小于支出金额！</li>"
            End If
            If FoundErr = True Then
                Call WriteErrMsg(ErrMsg, "User_Order.asp?Action=ShowOrder&OrderType=1&OrderFormID=" & OrderFormID & "")
                rsOrder.Close
                Set rsOrder = Nothing
                FoundErr = False
                Exit Sub
            End If

            rsOrder("MoneyReceipt") = rsOrder("MoneyTotal")
            If rsOrder("Status") <= 2 Then
                rsOrder("Status") = 3
            End If
            rsOrder.Update
            Conn.Execute ("update PE_User set Balance=Balance-" & Money & " where UserName='" & rsOrder("AgentName") & "'")
                    
            '向资金明细表中添加支出记录
            Call AddBankrollItem("System", rsOrder("AgentName"), AgentClientID, Money, 4, "", 0, 2, OrderFormID, 0, "支付订单费用，订单号：" & rsOrder("OrderFormNum"), Now())


            If Margin <> 0 Then
                Conn.Execute ("update PE_User set Balance=Balance+" & Margin & " where UserName='" & rsOrder("AgentName") & "'")
                ' 向资金明细表中添加收入记录
                Call AddBankrollItem("System", rsOrder("AgentName"), AgentClientID, Margin, 4, "", 0, 1, OrderFormID, 0, "返还代理订单差额，订单号：" & rsOrder("OrderFormNum"), Now())
            End If
        End If
    End If
    Dim strMsg
    strMsg = "代理商余额支付成功！"
    rsOrder.Close
    Set rsOrder = Nothing
    Call WriteSuccessMsg(strMsg, "User_Order.asp?Action=ShowOrder&OrderType=1&OrderFormID=" & OrderFormID & "")
End Sub

Sub Received()
    Dim DeliverID, rsDeliver, OrderFormID
    DeliverID = Trim(Request("DeliverID"))
    If DeliverID = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定DeliverID！</li>"
        Exit Sub
    Else
        DeliverID = PE_CLng(DeliverID)
    End If
    Set rsDeliver = Server.CreateObject("adodb.recordset")
    rsDeliver.Open "select OrderFormID,Received from PE_DeliverItem where DeliverID=" & DeliverID & "", Conn, 1, 3
    If rsDeliver.BOF And rsDeliver.EOF Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>找不到指定的订单！</li>"
    Else
        OrderFormID = rsDeliver("OrderFormID")
        rsDeliver("Received") = True
        rsDeliver.Update
    End If
    rsDeliver.Close
    Set rsDeliver = Nothing
    
    If FoundErr = True Then Exit Sub
    
    Conn.Execute ("update PE_OrderForm set DeliverStatus=3 where OrderFormID=" & OrderFormID & "")
    Call CloseConn
    Response.Redirect "User_Order.asp?Action=ShowOrder&OrderType=" & OrderType & "&OrderFormID=" & OrderFormID
End Sub

Sub Received2()
    Dim rsDeliver, OrderFormID
    OrderFormID = Trim(Request("OrderFormID"))
    If OrderFormID = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定OrderFormID！</li>"
    Else
        OrderFormID = PE_CLng(OrderFormID)
    End If
    If FoundErr = True Then Exit Sub
    
    Set rsDeliver = Server.CreateObject("adodb.recordset")
    rsDeliver.Open "select Received from PE_DeliverItem where OrderFormID=" & OrderFormID & " and Received=" & PE_False & "", Conn, 1, 3
    If Not (rsDeliver.BOF And rsDeliver.EOF) Then
        rsDeliver("Received") = True
        rsDeliver.Update
    End If
    rsDeliver.Close
    Set rsDeliver = Nothing
    
    If FoundErr = True Then Exit Sub
    
    If OrderType = 1 Then
        Conn.Execute ("update PE_OrderForm set DeliverStatus=3 where AgentName='" & UserName & "' and OrderFormID=" & OrderFormID & "")
    Else
        Conn.Execute ("update PE_OrderForm set DeliverStatus=3 where UserName='" & UserName & "' and OrderFormID=" & OrderFormID & "")
    End If
    Call CloseConn
    Response.Redirect "User_Order.asp?Action=ShowOrder&OrderType=" & OrderType & "&OrderFormID=" & OrderFormID
End Sub
%>
