机房收费系统(四)-结帐

【前言】
在写结帐代码以前,咱们要明白结帐的含义和思路。
含义:是谁来结帐?结的是谁的帐?这是必须弄明白的。
结帐:是管理员来结帐,管理员结的是操做员的帐。
以前老是听别人说结帐是个难点,因此内心有点抵触,而且不想去作,感受本身不会作,弄不明白。只有作过以后才知道它是否是很难,这是作完以后的感觉。因此什么事首先不要去顾虑太多,去作就行了。
【内容】
在结帐的过程当中,我认为稍微有些难度的就是汇总选项和结帐按钮了,思路很重要。下面是我对结帐的总结:
购卡(student表)
充值(Recharge表)
退卡(CancelCard表)
临时用户(student表)
汇总(student表、Recharge表、CancelCard表)

在这里插入图片描述
售卡张数:在student_info 表中当天该操做员未结帐的记录条数。
退卡张数:cancelcard_info 表中当天该操做员未结帐的记录条数。
充值金额:recharge_info 表中该操做员当天未结帐的金额总数。
临时收费金额:student表中当天该操做员对临时用户的收费金额。
退卡金额:cancelcard_info 表中操做员当天未结帐的金额总数。
总售卡张数:售卡张数-退卡张数
临时收费金额:包括在充值金额中!
应收总金额:充值金额(注册+充值)- 退卡金额
web

如下是个人结帐代码,望指点!
comboUserID点击事件svg

Private Sub comboUserID_Click()

Dim MsgText As String
Dim StuSQL As String
Dim ReChargeSQL As String
Dim CancelCardSQL As String
Dim mrcReCharge As ADODB.Recordset
Dim mrcCancelCard As ADODB.Recordset
Dim mrcStu As ADODB.Recordset

Dim RechargeCash As Variant      '用于存储,充值的 全部金额
Dim CancelCash As Variant        '用于存储,退钱的 全部金额
Dim TmpCash As Variant           '临时用户金额

    '购卡(student表)
    If SSTab1.Caption = "购卡" Then
        StuSQL = "select * from student_Info where UserID='" & comboUserID.Text & "' and status='未结帐'"
        Set mrcStu = ExecuteSQL(StuSQL, MsgText)
        MSFlexGrid1.Rows = mrcStu.RecordCount + 1
    
        With MSFlexGrid1
            .Rows = 1
            .CellAlignment = 4  '居中
            .TextMatrix(0, 0) = "学号"
            .TextMatrix(0, 1) = "卡号"
            .TextMatrix(0, 2) = "日期"
            .TextMatrix(0, 3) = "时间"
            .TextMatrix(0, 4) = "使用状态"
            .TextMatrix(0, 5) = "结帐状况"
            
        While mrcStu.EOF = False
            .Rows = .Rows + 1
            .CellAlignment = 4
            .TextMatrix(.Rows - 1, 0) = Trim(mrcStu.Fields(1))
            .TextMatrix(.Rows - 1, 1) = Trim(mrcStu.Fields(0))
            .TextMatrix(.Rows - 1, 2) = Trim(mrcStu.Fields(12))
            .TextMatrix(.Rows - 1, 3) = Trim(mrcStu.Fields(13))
            .TextMatrix(.Rows - 1, 4) = Trim(mrcStu.Fields(10))
            .TextMatrix(.Rows - 1, 5) = Trim(mrcStu.Fields(11))
            mrcStu.MoveNext
        Wend
        End With
    End If
 
    '充值(ReCharge表)
    If SSTab1.Caption = "充值" Then
        ReChargeSQL = "select * from ReCharge_Info where UserID='" & Trim(comboUserID.Text) & "' and status = '" & "未结帐" & "'"
        Set mrcReCharge = ExecuteSQL(ReChargeSQL, MsgText)
        RechargeCash = 0
        With MSFlexGrid2
            .Rows = 1
            .CellAlignment = 5
            .TextMatrix(0, 0) = "卡号"
            .TextMatrix(0, 1) = "学号"
            .TextMatrix(0, 2) = "充值金额"
            .TextMatrix(0, 3) = "日期"
            .TextMatrix(0, 4) = "时间"
            
        Do While Not mrcReCharge.EOF
            .Rows = .Rows + 1
            .TextMatrix(.Rows - 1, 0) = mrcReCharge.Fields(2)
            .TextMatrix(.Rows - 1, 1) = mrcReCharge.Fields(1)
            .TextMatrix(.Rows - 1, 2) = mrcReCharge.Fields(3)
            .TextMatrix(.Rows - 1, 3) = mrcReCharge.Fields(4)
            .TextMatrix(.Rows - 1, 4) = mrcReCharge.Fields(5)
            mrcReCharge.MoveNext
        Loop
        End With
    End If
    
    
    '退卡(CancelCard表)
    If SSTab1.Caption = "退卡" Then
        CancelCardSQL = "select * from CancelCard_Info where status='未结帐'and UserID='" & comboUserID.Text & "'"
        Set mrcCancelCard = ExecuteSQL(CancelCardSQL, MsgText)
        CancelCash = 0
        With MSFlexGrid3
            .Rows = 1
            .CellAlignment = 4      '居中
            .TextMatrix(0, 0) = "学号"  'studentid
            .TextMatrix(0, 1) = "卡号"  'cardid
            .TextMatrix(0, 2) = "日期"  'cash
            .TextMatrix(0, 3) = "时间"  'date
            .TextMatrix(0, 4) = "退卡金额"
            .TextMatrix(0, 5) = "结帐状况"
        
        While Not mrcCancelCard.EOF
            .Rows = .Rows + 1
            .CellAlignment = 4
            .TextMatrix(.Rows - 1, 0) = Trim(mrcCancelCard.Fields(0))  'studentno
            .TextMatrix(.Rows - 1, 1) = Trim(mrcCancelCard.Fields(1))   'cardno
            .TextMatrix(.Rows - 1, 2) = Trim(mrcCancelCard.Fields(3))   'date
            .TextMatrix(.Rows - 1, 3) = Trim(mrcCancelCard.Fields(4))   'time
            .TextMatrix(.Rows - 1, 4) = Trim(mrcCancelCard.Fields(2))   'cancelcash
            .TextMatrix(.Rows - 1, 5) = Trim(mrcCancelCard.Fields(6))   'cancelcash
            CancelCash = CancelCash + mrcCancelCard.Fields(2)
            mrcCancelCard.MoveNext
        Wend
        End With
    End If
    
    '临时用户(student表)
    If SSTab1.Caption = "退卡" Then
        StuSQL = "select * from student_Info where status='使用' and UserID='" & comboUserID.Text & "'and type='临时用户'and Ischeck='未结帐'"
        Set mrcStu = ExecuteSQL(StuSQL, MsgText)
        TmpCash = 0
        With MSFlexGrid4
            .Rows = 1
            .CellAlignment = 4
            .TextMatrix(0, 0) = "学号"
            .TextMatrix(0, 1) = "卡号"
            .TextMatrix(0, 2) = "日期"
            .TextMatrix(0, 3) = "时间"
            .TextMatrix(0, 4) = "结帐状况"
        
        While mrcStu.EOF = False
            .Rows = .Rows + 1
            .CellAlignment = 4
            .TextMatrix(.Rows - 1, 0) = Trim(mrcStu.Fields(1))
            .TextMatrix(.Rows - 1, 1) = Trim(mrcStu.Fields(0))
            .TextMatrix(.Rows - 1, 2) = Trim(mrcStu.Fields(12))
            .TextMatrix(.Rows - 1, 3) = Trim(mrcStu.Fields(13))
            .TextMatrix(.Rows - 1, 4) = Trim(mrcStu.Fields(11))
            TmpCash = mrcStu.Fields(7)
            mrcStu.MoveNext
        Wend
        End With
    End If
    
    If MSFlexGrid4.Rows = 1 Then
        RechargeCash = "0"
    Else
        ReChargeSQL = "select * from ReCharge_Info where status='未结帐' and UserID='" & comboUserID.Text & "'"
        Set mrcReCharge = ExecuteSQL(ReChargeSQL, MsgText)
        Do While Not mrcReCharge.EOF
            RechargeCash = RechargeCash + mrcReCharge.Fields(3)
            mrcReCharge.MoveNext
        Loop
    End If
    
    '汇总(student表)
    If SSTab1.Caption = "汇总" Then
    
        '计算售卡张数
        StuSQL = "select * from student_info where UserID = '" & Trim(comboUserID.Text) & "'and ischeck = '" & "未结帐" & "'"
        Set mrcStu = ExecuteSQL(StuSQL, MsgText)
        txtSCard.Text = mrcStu.RecordCount
   
        '计算退卡张数
        CancelCardSQL = "select * from CancelCard_info where UserID='" & Trim(comboUserID.Text) & "' and status = '" & "未结帐" & "'"
        Set mrcCancelCard = ExecuteSQL(CancelCardSQL, MsgText)
        txtBCard.Text = mrcCancelCard.RecordCount
        
        '总售卡数=售卡张数-退卡张数
        txtAllSCard = txtSCard - txtBCard
        
        '计算充值金额(不区分固定仍是临时用户)
        ReChargeSQL = "select sum(addmoney) from ReCharge_Info where UserID='" & Trim(comboUserID.Text) & "' and status = '" & "未结帐" & "'"
        Set mrcReCharge = ExecuteSQL(ReChargeSQL, MsgText)
        If IsNull(mrcReCharge.Fields(0)) Then   '无记录
            txtChargeMoney.Text = "0"
        Else
            txtChargeMoney.Text = mrcReCharge.Fields(3)
        End If

        '计算退卡金额
        CancelCardSQL = "select sum(CancelCash) from CancelCard_Info where UserID = '" & Trim(comboUserID.Text) & "'and status = '" & "未结帐" & "'"
        Set mrcCancelCard = ExecuteSQL(CancelCardSQL, MsgText)
        If IsNull(mrcCancelCard.Fields(0)) Then '无记录
            txtBMoney.Text = "0"
        Else
            txtBMoney.Text = mrcCancelCard.Fields(2)
        End If

        '计算临时收费金额
        ReChargeSQL = "select sum(addmoney) from ReCharge_Info where UserID = '" & Trim(comboUserID.Text) & "'and status = '未使用'"
        Set mrcReCharge = ExecuteSQL(ReChargeSQL, MsgText)
        If IsNull(mrcReCharge.Fields(0)) Then   '无记录
            txtTemporaryMoney.Text = "0"
        Else
            txtTemporaryMoney.Text = mrcReCharge.Fields(3)
        End If

        '计算应收金额
        txtAllCollectMoney.Text = Val(txtChargeMoney.Text) - Val(txtBMoney.Text)
        mrcStu.Close         '关闭释放空间
        mrcReCharge.Close
        mrcCancelCard.Close
    End If
    
    '退出
    If SSTab1.Caption = "退出" Then
        Unload Me
    End If
End Sub

结帐按钮:oop

Private Sub cmdAmount_Click()

Dim MsgText As String
Dim StuSQL As String
Dim ReChargeSQL As String
Dim CancelCardSQL As String
Dim checkWeekSQL As String
Dim CheckDaySQL As String
Dim LineSQL As String
Dim mrcStu As ADODB.Recordset          '表明学生表(student_info)
Dim mrcReCharge As ADODB.Recordset     '表明充值表(recharge_info)
Dim mrcCancelCard As ADODB.Recordset   '表明退卡表(cancelcard_info)
Dim mrcCheckDay As ADODB.Recordset     '表明日结帐单(checkday_info)
Dim mrcLine As ADODB.Recordset         '表明line表(Line_info)

Dim remaincash As String               '上期金额
Dim RechargeCash As String             '充值金额
Dim consumecash As String              '消费金额
Dim CancelCash As String               '退卡金额
Dim allcash As String                  '汇总金额
    
    '判断是否已选择操做员
    If comboUserID.Text = "" Then
        MsgBox "请选择操做员后再结帐!", 48, "警告"
        Exit Sub
    End If

    '更新学生表
    StuSQL = "select * from student_info where UserID = '" & Trim(comboUserID.Text) & "'and ischeck = '" & "未结帐" & "'"
    Set mrcStu = ExecuteSQL(StuSQL, MsgText)
    Do While Not mrcStu.EOF
        mrcStu!ischeck = "结帐"
        mrcStu.Update
        mrcStu.MoveNext
    Loop
    mrcStu.Close

    '更新充值表
    ReChargeSQL = "select * from ReCharge_Info where UserID='" & Trim(comboUserID.Text) & "' and status = '" & "未结帐" & "'"
    Set mrcReCharge = ExecuteSQL(ReChargeSQL, MsgText)
    Do While Not mrcReCharge.EOF
        mrcReCharge!Status = "结帐"
        mrcReCharge.Update
        mrcReCharge.MoveNext
    Loop
        mrcReCharge.Close
    
    '更新退卡表
    CancelCardSQL = "select * from CancelCard_Info where UserID='" & Trim(comboUserID.Text) & "' and status = '" & "未结帐" & "'"
    Set mrcCancelCard = ExecuteSQL(CancelCardSQL, MsgText)
    Do While Not mrcCancelCard.EOF
        mrcCancelCard!Status = "结帐"
        mrcCancelCard.Update
        mrcCancelCard.MoveNext
    Loop
        mrcCancelCard.Close

    '更新日结帐单表
    '计算上期充值卡余额(remaincash)
    CheckDaySQL = "select max(date) from CheckDay_Info"   'max(date)就是最近的一天
    Set mrcCheckDay = ExecuteSQL(CheckDaySQL, MsgText)
    If mrcCheckDay.BOF Then
        Exit Sub
        MaxDate = mrcCheckDay.Fields(5)
        CheckDaySQL = "select * from CheckDay_Info where date ='" & MaxDate & "'"
        Set mrcCheckDay = ExecuteSQL(CheckDaySQL, MsgText)
        If IsNull(mrcCheckDay.Fields(0)) Then
            remaincash = "0"
            Exit Sub
        Else
            remaincash = mrcCheckDay.Fields(0)
        End If
    End If
    
    '更新当天充值金额
    '充值金额是今天的值班的任意个操做员充值的金额,而且是已经结完帐的
    '若是今天充值了一些金额,可是没有结帐,则在计算日结帐单时,不计入
    ReChargeSQL = "select sum(addmoney) from ReCharge_info where status = '结帐' and date  = '" & Date & "'" '表明今天
    Set mrcReCharge = ExecuteSQL(ReChargeSQL, MsgText)
    If IsNull(mrcReCharge.Fields(0)) Then
        RechargeCash = "0"
    Else
        RechargeCash = mrcReCharge.Fields(0)
    End If

    '计算当日消费金额
    LineSQL = "select sum(consume) from Line_Info where offdate='" & Date & "'"
    Set mrcLine = ExecuteSQL(LineSQL, MsgText)
    If IsNull(mrcLine.Fields(0)) Then
        consumecash = "0"
    Else
        consumecash = mrcLine.Fields(0)
    End If

   '更新计算当天的退卡金额(cancelcash)
   CancelCardSQL = "select sum(cancelcash) from CancelCard_Info where date='" & Date & "'and status = '结帐'"
   Set mrcCancelCard = ExecuteSQL(CancelCardSQL, MsgText)
   If IsNull(mrcCancelCard.Fields(0)) Then
       CancelCash = "0"
   Else
       CancelCash = mrcCancelCard.Fields(0)
   End If

    '更新CheckDay表
    CheckDaySQL = "select * from CheckDay_info "
    Set mrcCheckDay = ExecuteSQL(CheckDaySQL, MsgText)
    
    '判断当天是否已经结过账
    CheckDaySQL = "select * from CheckDay_info where date='" & Date & "'"
    Set mrcCheckDay = ExecuteSQL(CheckDaySQL, MsgText)
    If mrcCheckDay.EOF = False Then
        mrcCheckDay!remaincash = Val(remaincash)
        mrcCheckDay!RechargeCash = Val(RechargeCash)
        mrcCheckDay!consumecash = Val(consumecash)
        mrcCheckDay!CancelCash = Val(CancelCash)
        mrcCheckDay!allcash = Val(remaincash) + Val(RechargeCash) - Val(CancelCash) - Val(consumecash)
        mrcCheckDay!Date = Date
        mrcCheckDay.Update
        mrcCheckDay.Close
    Else
        mrcCheckDay.AddNew
        mrcCheckDay!remaincash = Val(remaincash)
        mrcCheckDay!RechargeCash = Val(RechargeCash)
        mrcCheckDay!consumecash = Val(consumecash)
        mrcCheckDay!CancelCash = Val(CancelCash)
        mrcCheckDay!allcash = Val(remaincash) + Val(RechargeCash) - Val(CancelCash)
        mrcCheckDay!Date = Date
        mrcCheckDay.Update
        mrcCheckDay.Close
    End If
    
    '更新周结帐单
    checkWeekSQL = "delete checkWeek_info"
    Set mrccheckWeek = ExecuteSQL(checkWeekSQL, MsgText)
    
    checkWeekSQL = "insert into checkWeek_info select * from CheckDay_info"
    Set mrccheckWeek = ExecuteSQL(checkWeekSQL, MsgText)
    
    '清空文本框显示的信息
    txtSCard.Text = "0"
    txtBCard.Text = "0"
    txtChargeMoney.Text = "0"
    txtTemporaryMoney.Text = "0"
    txtBMoney.Text = "0"
    txtAllSCard.Text = "0"
    txtAllCollectMoney.Text = "0"
    MsgBox "结帐成功!", 64, "舒适提示"
   
End Sub