1. 程式人生 > >機房收費系統——結賬

機房收費系統——結賬

結賬這部分算是機房收費系統的一個難點,之所以它難,是因為這個窗體不光涉及到程式碼,而且還涉及到算數的問題,還和報表有關,所以它是個難點,但是隻要把思路弄清楚了,再難的東西也不怕啦!

思維導圖:

在這裡插入圖片描述
思路有沒有清楚一點呢?

程式碼片段

先讓操作員的名字和label框實現同步:

 txtsql = "select * from user_info where userid= '" & Trim(cboOpUserID.Text) & "'"
    Set mrc_u = ExecuteSQL(txtsql, msgtext)
    
    If mrc_u.EOF Then
        mrc_u.Close
        Exit Sub
    Else
        lblusername.Caption = mrc_u.fields(3)
    mrc_u.Close
    
    End If

以一個為例:

        txtsql = "select * from student_info where ischeck= '未結賬'and userid= '" & Trim(cboOpUserID.Text) & "'"
        Set mrc = ExecuteSQL(txtsql, msgtext)

       Do While mrc.EOF = False
        With MSHFlexGrid1
       .CellAlignment = 4
        .Rows = 2
                .CellAlignment = 4

                .TextMatrix(.Rows - 1, 0) = Trim(mrc.fields(0))
                .TextMatrix(.Rows - 1, 1) = Trim(mrc.fields(1))
                .TextMatrix(.Rows - 1, 2) = Trim(mrc.fields(12))
                .TextMatrix(.Rows - 1, 3) = Trim(mrc.fields(13))
                mrc.MoveNext
        
        
         End With
         Loop
         
         mrc.Close

重頭戲來了,讓我們看一下結賬部分:
首先我們要先弄清楚以下幾個關係:
1、售卡張數=student_info表中的總數
2、退卡張數=cancelcard_info表中的總數
3、總售卡張數=售卡張數-退卡張數
4、臨時使用者金額=當天未結賬的臨時使用者金額
5、應收金額=充值金額-退卡金額(充值金額=充值金額+註冊金額)

下面我們來看一下具體的程式碼應該怎麼寫:

Private Sub cmdAccount_Click()


'充值部分
Dim txtsql1 As String
Dim msgtext1 As String
Dim mrc1 As ADODB.Recordset  '連線recharge_info表

'退卡部分
Dim txtsql2 As String
Dim msgtext2 As String
Dim mrc2 As ADODB.Recordset  '連線cancel_info表

'臨時使用者部分
Dim txtsql3 As String
Dim msgtext3 As String
Dim mrc3 As ADODB.Recordset   '連線student_info表

Dim mrc4 As ADODB.Recordset    '連線為結賬的學生表

Dim mrc_line As ADODB.Recordset '連線line表

Dim mrc_c As ADODB.Recordset  '連線checkday表



'退卡金額
txtsql2 = "select sum(cancelcash) from cancelcard_info where userid= '" & Trim(cboOpUserID) & "' and status='" & "未結賬" & "'and date= '" & Date & " '"
Set mrc2 = ExecuteSQL(txtsql2, msgtext2)
    If IsNull(mrc2.fields(0)) Then
        txtBackCardMoney.Text = "0"
    Else
    
    txtBackCardMoney.Text = mrc2.fields(0)
    End If


'充值金額
txtsql1 = "select sum(addmoney) from recharge_info where userid= '" & Trim(cboOpUserID) & "'and status='" & "未結賬" & "'and date='" & Date & " '"

Set mrc1 = ExecuteSQL(txtsql1, msgtext1)
    If IsNull(mrc1.fields(0)) Then
    
        txtRecharge.Text = "0"
    Else
        txtRecharge.Text = mrc1.fields(0)
    End If
    

'臨時金額
txtsql3 = "select sum(cash) from student_info where userid= '" & Trim(cboOpUserID) & "'and type='" & "臨時使用者" & "'and ischeck='" & "未結賬" & "'and date='" & Date & "'"
Set mrc3 = ExecuteSQL(txtsql3, msgtext3)
    If IsNull(mrc3.fields(0)) Then
        txtTmpRecharge.Text = "0"
    Else
        txtTmpRecharge.Text = mrc3.fields(0)
    End If

'應收金額
'應收金額=充值金額-退卡金額,也就是掙得的錢,充值金額包括充值金額+註冊金額
txtCollectMoney.Text = Val(txtRecharge.Text) + Val(txtTmpRecharge.Text) - Val(txtBackCardMoney.Text)

千萬要記住一定要把記錄同步到日結賬表和周結賬表中

 '將記錄同步到checkday_info表中
    Dim mrcStudent As ADODB.Recordset
    Dim mrcline As ADODB.Recordset
    Dim mrccheckday As ADODB.Recordset
    Dim mrcrecharge As ADODB.Recordset
    Dim mrccancel As ADODB.Recordset
    Dim txtsql As String
    Dim msgtext As String
    Dim lastRecharge As Integer
    Dim LastExitcash As Integer
    Dim RemainCash As Integer
    Dim RechargeCash As Integer
    Dim ConsumeCash As Integer
    Dim CancelCash As Integer
    
    '計算上期充值卡餘額(上期就是昨天的錢=充值卡的錢-退卡的錢)
    '計算昨天充值卡里面的餘額
        txtsql = "select sum(addmoney) from recharge_info where date= '" & Date - 1 & "'"
        Set mrcrecharge = ExecuteSQL(txtsql, msgtext)
    '判斷充值卡里面的錢是否為空
        If IsNull(mrcrecharge.fields(0)) = True Then
            lastRecharge = "0"
        Else
    '引用sum函式,預設的addmoney列為field(0)
            lastRecharge = mrcrecharge.fields(0)
    '計算昨天退卡表裡面的錢
            
        txtsql = "select  sum(cancelcash) from cancelcard_info where date= '" & Date - 1 & "'"
        Set mrccancel = ExecuteSQL(txtsql, msgtext)
            If IsNull(mrccancel.fields(0)) = True Then
                LastExitcash = "0"
            Else
                RemainCash = lastRecharge - LastExitcash
            End If
        End If
     '計算當日充值金額
        txtsql = "select sum(addmoney) from recharge_info where date='" & Date & "'"
        Set mrcrecharge = ExecuteSQL(txtsql, msgtext)
            If IsNull(mrcrecharge.fields(0)) Then
                RechargeCash = "0"
            Else
                RechargeCash = mrcrecharge.fields(0)
            End If
    '計算當日消費金額
        txtsql = "select sum(consume) from line_info where offdate= '" & Date & "'"
        Set mrcline = ExecuteSQL(txtsql, msgtext)
        
        If IsNull(mrcline.fields(0)) Then
            ConsumeCash = "0"
        Else
            ConsumeCash = mrcline.fields(0)
        End If
    '計算當日退還金額
        txtsql = "select sum(cancelcash) from cancelcard_info where date= '" & Date & "'"
        Set mrccancel = ExecuteSQL(txtsql, msgtext)
        If IsNull(mrccancel.fields(0)) Then
            CancelCash = "0"
        Else
            CancelCash = mrccancel.fields(0)
        End If
    '清楚當天記錄
        txtsql = "delete checkday_info where date= '" & Date & "'"
        Set mrccheckday = ExecuteSQL(txtsql, msgtext)
    '將資料同步到checkday_info 表中
        txtsql = "select * from checkday_info "
        Set mrccheckday = ExecuteSQL(txtsql, msgtext)
        
        mrccheckday.AddNew
        
        mrccheckday!RemainCash = Val(RemainCash)
        mrccheckday!RechargeCash = Val(RechargeCash)
        mrccheckday!ConsumeCash = Val(ConsumeCash)
        mrccheckday!CancelCash = Val(CancelCash)
        mrccheckday!allcash = Val(RechargeCash) - Val(CancelCash)
        mrccheckday!Date = Date
        mrccheckday.Update
        mrccheckday.Close
        
        MsgBox "結賬成功!", 0 + 46, "提示"
        
        txtsql = "select * from student_info where date = '" & Date & "'and ischeck= '" & "未結賬" & "'"
        Set mrcStudent = ExecuteSQL(txtsql, msgtext)
        
        Do While Not mrcStudent.EOF
            mrcStudent!ischeck = "結賬"
            mrcStudent.Update
            mrcStudent.MoveNext
            
        Loop
            mrcStudent.Close
            
        txtsql = "select * from recharge_info where  date= '" & Date & " 'and status= '" & "未結賬" & "'"
        Set mrcrecharge = ExecuteSQL(txtsql, msgtext)
        
        Do While Not mrcrecharge.EOF
            mrcrecharge!status = "結賬"
            mrcrecharge.Update
            mrcrecharge.MoveNext
            
        Loop
            mrcrecharge.Close
            
        txtsql = "select * from cancelcard_info where date = '" & Date & "'and status='" & "未結賬" & "'"
        Set mrccancel = ExecuteSQL(txtsql, msgtext)
        
        Do While Not mrccancel.EOF
            mrccancel!status = "結賬"
            mrccancel.Update
            mrccancel.MoveNext
            
        Loop
            mrccancel.Close
            
    
        
    '將資料同步到checkweek表中
    Dim mrccheckweek As ADODB.Recordset
    Dim mrc23 As ADODB.Recordset
    
    '刪除原先的記錄
        txtsql = "delete checkweek_info"
        Set mrccheckweek = ExecuteSQL(txtsql, msgtext)
        
        txtsql = "insert into checkweek_info select * from checkday_info"
        Set mrc23 = ExecuteSQL(txtsql, msgtext)
        
    
    End Sub