Dim CurrentControl As Integer
Dim CodeSearch As Boolean
Dim SearchString As String
Dim LastColumn As Integer
Dim Work As String
Dim RefNo As Long
Dim Emp1Refno As Long
Dim Emp2Refno As Long
Dim TotalRows As Long
Dim ObjDmg As New Damage
Dim TotalAmount As Double
Dim TypeCode As Long
Dim Sql As String
Dim code As Long
Dim ClAcCode As String
Dim PrhRate As Double
Dim ClientDistrict As String
Dim ClientCode As Long
Dim ClientAdd1 As String
Dim ClientAdd2 As String
Dim ClientAdd3 As String
Dim ClientState As String
Dim BillNo As String
Dim ProcessStart As Boolean
Dim StValue As Double
Dim StNetTotal As Double
Dim Emp1AcCode As String
Dim Emp2AcCode As String
Dim TotalWight As Double
Dim TotKg As Double
Dim OldEmp1Wage As Double
Dim OldEmp2Wage As Double
Dim TotalItem As Long
Dim BillPrint As String
Dim ItmCode As Long
Dim RptFromDate As Date
Dim RptToDate As Date
Dim DebitTotal As Double
Dim CreditTotal As Double
Dim OpBalDr As Double
Dim OpBalCr As Double
Dim BalAmt As Double
Dim PFlag As String
Dim RateOrg As Double

Dim ClSlno, ClName, ClRate, Clqty, ClTax, ClTaxAmt, ClDIs, ClDIsAmt, Clgross, ClNetTotal, Clitcd
 



Private Sub CmdAdd_Click()

CmdAdd.BackColor = &H2877B7
CmdModify.BackColor = BgArea.Cm_SaveClr
cmdDELETE.BackColor = BgArea.Cm_SaveClr

CmdSave.BackColor = BgArea.Cm_SaveClr
Work = "A"
Clear Me
TxtType = "E"
TxtBillNo = GetNextDmgNo("Select Max(Cast(St_InvNo as Numeric)) From Sls_Trn where st_fs='B'")
ClearFlex MsfDamage, TotalRows
MedBillDate = Date
LblCrDr.Caption = ""
MedBillDate.SetFocus
End Sub

Private Sub CmdView_Click()
Work = "V"
Clear Me
ClearFlex MsfDamage, 100



FraList.Visible = False

RsOpen ("Select Max(Cast(St_InvNo as Numeric)) From Sls_Trn where st_fs='B'")
If Trim(Rs.Fields(0)) <> "" Then
    TxtInvNo = Trim(Rs.Fields(0))
End If
Rs.Close

End Sub

Private Sub MsfList_DblClick()
MsfList_KeyDown 13, 0
End Sub
Private Sub CmdClose_Click()
 Unload Me
End Sub
Sub NextColumn()
With MsfDamage
 ' If Trim(.Text) = "" Then Exit Sub
      .Col = .Col + 1
End With
End Sub
Sub IncrementRow()
With MsfDamage
       If Trim(.Text) = "" Then Exit Sub

        .Rows = .Rows + 1
        .Row = .Row + 1
        .Col = 1
        If .Row > 12 Then
            .Row = .Row - 1
            SendKeys "{Down}"
        End If
End With
End Sub
Sub NextRow()
With MsfDamage
        If Trim(.Text) = "" Then Exit Sub
        .Row = .Row + 1
        .Col = 1
        If .Row > 14 Then
            .Row = .Row - 1
            SendKeys "{Down}"
        End If
End With
End Sub
Private Sub cmdDELETE_Click()
CmdAdd.BackColor = BgArea.Cm_SaveClr
CmdModify.BackColor = BgArea.Cm_SaveClr
cmdDELETE.BackColor = &H2877B7
CmdSave.BackColor = BgArea.Cm_SaveClr
Work = "D"
Clear Me
ClearFlex MsfDamage, TotalRows
CodeSearch = True
CurrentControl = 1
SearchString = ""
FlexSearch 32, CodeSearch, SearchString, Me, MsfList
FraList.Visible = True
MsfList.ColWidth(0) = 4500
MsfList.ColWidth(1) = 1300
MsfList.ColWidth(2) = 1300
MsfList.ColWidth(3) = 0
MsfList.ColWidth(4) = 2300
MsfList.ColWidth(5) = 1300
MsfList.Height = 3735
MsfList.Width = 11055
MsfList.Top = 240
''MsfList.Left = 120
FraList.Height = 4095
FraList.Width = 11295
FraList.Top = 1450
FraList.Left = 1500
MsfList.SetFocus

End Sub

Private Sub cmdDELETE_GotFocus()
CurrentControl = 3
End Sub

Private Sub CmdModify_Click()
CmdModify.BackColor = &H2877B7
CmdAdd.BackColor = BgArea.Cm_SaveClr
cmdDELETE.BackColor = BgArea.Cm_SaveClr
CmdSave.BackColor = BgArea.Cm_SaveClr
Work = "E"
Clear Me
ClearFlex MsfDamage, TotalRows
CodeSearch = True
CurrentControl = 1
SearchString = ""
FlexSearch 32, CodeSearch, SearchString, Me, MsfList
FraList.Visible = True
MsfList.ColWidth(0) = 4500
MsfList.ColWidth(1) = 1300
MsfList.ColWidth(2) = 1300
MsfList.ColWidth(3) = 0
MsfList.ColWidth(4) = 2300
MsfList.ColWidth(5) = 1300
MsfList.Height = 3735
MsfList.Width = 11055
MsfList.Top = 240
''MsfList.Left = 120
FraList.Height = 4095
FraList.Width = 11295
FraList.Top = 1450
FraList.Left = 1500
MsfList.SetFocus
End Sub
Sub ResizeMsfList()
 MsfList.Cols = 2
 If CurrentControl = 2 Then
    FraList.Left = 1080
    FraList.Top = 2880
    FraList.Width = 9400
    MsfList.Width = 9100
    MsfList.ColWidth(0) = 1000
    MsfList.ColWidth(1) = 5000
    MsfList.ColWidth(2) = 2000
    MsfList.ColWidth(3) = 0
    
    MsfList.FormatString = "<Code                  |< Item Name                                                                                      |>            Cost|>TaxPrh"
  ElseIf CurrentControl = 1 Then
  
  
  ElseIf CurrentControl = 3 Then
    FraList.Left = 2000
    FraList.Top = 3000
    MsfList.ColWidth(0) = 1000
    MsfList.ColWidth(1) = 4000
    MsfList.ColWidth(2) = 0
    MsfList.ColWidth(3) = 0
    FraList.Width = 5400
    MsfList.Width = 5100
    
   End If
End Sub

Private Sub CmdModify_GotFocus()
CurrentControl = 3
End Sub



Private Sub CmdSave_Click()
If Work = "A" Then
If TxtType = "E" Then
    ObjDmg.DtFs = "E"
    ObjDmg.DsFs = "E"
Else
    ObjDmg.DtFs = "B"
    ObjDmg.DsFs = "B"
End If
End If



If Trim(TxtBillNo) = "" Or Trim(TxtGross) = "" Then
    MsgBox "Cannot Save !.... Invalid Data...             ", vbCritical
    CmdAdd.BackColor = &H2877B7
    CmdModify.BackColor = BgArea.Cm_SaveClr
    cmdDELETE.BackColor = BgArea.Cm_SaveClr
    CmdSave.BackColor = BgArea.Cm_SaveClr
    TxtClientName.SetFocus
    Exit Sub
End If
'CalculateNetTotal
If ProcessStart = False Then
    ProcessStart = True
Else
    Exit Sub
End If
CmdSave.BackColor = &H40A5F0
Dim S As String

If Work = "A" Then
    If ObjDmg.DtFs = "B" Then
        BillNo = GetNextDmgNo("Select Max(Cast(St_InvNo as Numeric)) From Sls_Trn where st_fs='B'")
    Else
        BillNo = GetNextDmgNo("Select Max(Cast(St_InvNo as Numeric)) From Sls_Trn where st_fs='E'")
    End If
    ObjDmg.DtRefno = 0
    RefNo = 0
    S = "select * from Sls_Trn where Sls_Inv_No='" & Trim(TxtBillNo) & "'"
ElseIf Work = "E" Then
    ''CancelBilling
    S = "Select * from Sls_Trn Where Sls_Inv_No='" & Trim(TxtBillNo) & "' and Sls_Refno <>" & ObjDmg.DtRefno

    Sql = ""
    Sql = Sql + "'" & ObjDmg.DsRefno & "'"
    Con.Execute "Exec sp_delete_Sls_Sub" + Sql
        
 ElseIf Work = "D" Then
    If MsgBox("Delete the Damage (Y/N", vbYesNo + vbInformation) = vbNo Then Exit Sub
    ''CancelBilling
    '    Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(OldEmp1Wage) & " where Emp_AcCode='" & Emp1AcCode & "'"
    '   Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(OldEmp2Wage) & " where Emp_AcCode='" & Emp2AcCode & "'"
    
    Con.Execute "Delete From Trn_Sub Where Trs_Refno=" & ObjDmg.DtRefno
    Con.Execute "Delete From Trn_Mst Where Trn_Refno=" & ObjDmg.DtRefno
    Con.Execute "Delete From Trn_Sub Where Trs_Refno=" & ObjDmg.DtRefno + 30000000
    Con.Execute "Delete From Trn_Mst Where Trn_Refno=" & ObjDmg.DtRefno + 30000000
    
    Con.Execute "Delete From Trn_Sub Where Trs_Refno=" & ObjDmg.DtRefno + 10000000
    Con.Execute "Delete From Trn_Mst Where Trn_Refno=" & ObjDmg.DtRefno + 10000000
    Con.Execute "Delete From Trn_Sub Where Trs_Refno=" & ObjDmg.DtRefno + 20000000
    Con.Execute "Delete From Trn_Mst Where Trn_Refno=" & ObjDmg.DtRefno + 20000000
    
        Sql = ""
        Sql = Sql + "'" & ObjDmg.DtRefno & "'"
        Con.Execute "Exec sp_delete_Sls_Trn" + Sql
        
        Sql = ""
        Sql = Sql + "'" & ObjDmg.DsRefno & "'"
        Con.Execute "Exec sp_delete_Sls_Sub" + Sql
        
        MsgBox "Deleted Successfully"
        cmdDELETE_Click
        Clear Me
        ProcessStart = False
        Exit Sub
 End If
 

 If Work = "E" And TxtType = "B" And ObjDmg.DtFs = "E" Then
     ObjDmg.DtFs = "B"
     BillNo = GetNextDmgNo("Select Max(Cast(St_InvNo as Numeric)) From Sls_Trn where st_fs='B'")
 ElseIf Work = "E" And TxtType = "E" And ObjDmg.DtFs = "B" Then
     ObjDmg.DtFs = "E"
     BillNo = GetNextDmgNo("Select Max(Cast(St_InvNo as Numeric)) From Sls_Trn where st_fs='E'")

 End If
'''ObjDmg.DtRefno = Val(RefNo)
ObjDmg.DtInvNo = Trim(BillNo)
ObjDmg.DtGross = Val(TxtGross)
ObjDmg.DtVehNo = Trim(TxtVehNo)
ObjDmg.DtDisc = Val(TxtDiscount)
ObjDmg.DtOthAdd = Val(TxtOtherAdd)
ObjDmg.DtTax = Val(TxtTaxPer1)
ObjDmg.DtTaxAmt = Val(TxtTaxAmount)
ObjDmg.DtTotal = Val(TxtNetTotal)
ObjDmg.DtDate = MedBillDate
ObjDmg.DtClAccd = Trim(TxtClientName) 'Clientplier Name
ObjDmg.DtCmpCd = "" ''Trim(TxtTinNo)   'Tin no
ObjDmg.DtSupCode = ClientCode
ObjDmg.DtSupName = Trim(TxtClientName)
ObjDmg.DtAdd1 = Trim(TxtPlace)
ObjDmg.DtSupTin = Trim(ClientDistrict)
ObjDmg.DtGross4 = Val(TxtGross4)
ObjDmg.DtGross12 = Val(TxtGross12)
ObjDmg.DtGross1 = Val(TxtGross1)
ObjDmg.DtGross0 = Val(TxtGross0)
ObjDmg.DtIpt4 = Val(TxtIpt4)
ObjDmg.DtIpt12 = Val(TxtIpt12)
ObjDmg.DtIpt1 = Val(TxtIpt1)
ObjDmg.DtCess = Val(TxtCess)
ObjDmg.DtDisPer = Val(TxtDisPer)
ObjDmg.DtExp = Val(TxtExp)
ObjDmg.DtComPer = Val(TxtCommPer)
ObjDmg.DtComAmt = Val(TxtCommAmt)
ObjDmg.DtCash = Val(TxtDPayment)
ObjDmg.DtCrAmt = Val(TxtCrAmt)
ObjDmg.DtEmp1Cd = Val(Emp1Refno)
ObjDmg.DtEmp2Cd = Val(Emp2Refno)
ObjDmg.DtEmp1 = Trim(TxtEmp1)
ObjDmg.DtEmp2 = Trim(TxtEmp2)
ObjDmg.DtEmp1AcCd = Trim(Emp1AcCode)
ObjDmg.DtEmp2AcCd = Trim(Emp2AcCode)
ObjDmg.DtClAccd = ClAcCode
ObjDmg.DtEmp1Wages = Val(TxtLd1Wages)
ObjDmg.DtEmp2Wages = Val(TxtLd2Wages)
ObjDmg.DtFrightChar = Val(TxtFfiCharg)
If Work = "A" Then
    ObjDmg.DtPFlag = "N"
Else
    ObjDmg.DtPFlag = Trim(PFlag)
End If
ObjDmg.SaveDamageTrn
 
 Dim I As Long
    TotalWight = 0
    For I = 1 To MsfDamage.Rows - 1
          If Trim(MsfDamage.TextMatrix(I, 1)) = "" Then
            Exit For
          End If
            ObjDmg.DsRefno = Val(RefNo)
            ObjDmg.DsInvNo = Trim(BillNo)
            ObjDmg.DsSlNo = Val(MsfDamage.TextMatrix(I, ClSlno))
            ObjDmg.DsName = Trim(MsfDamage.TextMatrix(I, ClName))
            ObjDmg.DsRate = FmtNumber(MsfDamage.TextMatrix(I, ClRate))        'rate
            ObjDmg.DsQty = FmtNumber(MsfDamage.TextMatrix(I, Clqty))          'qty
            TotalWight = TotalWight + Val(MsfDamage.TextMatrix(I, Clqty))
            ObjDmg.DsTotal = FmtNumber(MsfDamage.TextMatrix(I, Clgross))        'total
            ObjDmg.DsItCd = Val(MsfDamage.TextMatrix(I, Clitcd))        'itcd
            ObjDmg.DsDate = MedBillDate                               'date.
            If Trim(MsfDamage.TextMatrix(I, ClTax)) = "" Then
                ObjDmg.DsTaxPer = 0
            Else
                ObjDmg.DsTaxPer = FmtNumber(MsfDamage.TextMatrix(I, ClTax))
            End If
            ObjDmg.DsTax = FmtNumber(MsfDamage.TextMatrix(I, ClTaxAmt))
            
            If Trim(MsfDamage.TextMatrix(I, ClDIs)) = "" Then
                ObjDmg.DsDisPer = 0
            Else
                ObjDmg.DsDisPer = FmtNumber(MsfDamage.TextMatrix(I, ClDIs))
            End If
            ObjDmg.DsDisAmt = FmtNumber(MsfDamage.TextMatrix(I, ClDIsAmt))
            
            ObjDmg.SaveDamageSub
    Next I
'UpdateItemQty

     
     Dim ObjTrn As New AccTran
      
      ObjTrn.TrnDate = MedBillDate
      ObjTrn.TrnAmt = 0
      ObjTrn.TrnBillNo = TxtBillNo
      ObjTrn.TrnVchNo = TxtVchno
      ObjTrn.TrnType = ""
      ObjTrn.TrnFS = FS
      ObjTrn.TrnUser = UserId
      ObjTrn.TrnBrCd = BranchCode
      
      'Trn Sub
      '**************************
      ObjTrn.TrsNarr1 = "INV NO / " + ObjDmg.DtInvNo + ", " + TxtVehNo
      ObjTrn.TrsNarr2 = ""
      ObjTrn.TrsNarr3 = ""
      ObjTrn.TrsNarr4 = ""
      ObjTrn.TrsChqNo = ""
      '***************************
     
      If Work = "A" Then
          
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal - " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp1AcCode & "' and Emp_CrDr='Dr.' "
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp1AcCode & "' and Emp_CrDr='Cr.' "
        
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal - " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp2AcCode & "' and Emp_CrDr='Dr.' "
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp2AcCode & "' and Emp_CrDr='Cr.' "
        
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal - " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp2AcCode & "'"
        
         S = ""
         S = "Select Max(Dt_Refno)From Dmg_Trn where Dt_HostName=Host_Name()"
         If RsOpen(S) = True Then
            ObjDmg.DtRefno = Val(RetField(Rs.Fields(0)))
            Rs.Close
         End If
         '''''ObjTrn.PostDamage True, Val(TxtDPayment), Val(TxtCrAmt), ObjDmg.DtRefno, ClAcCode, Emp1AcCode, Val(TxtLd1Wages), Emp2AcCode, Val(TxtLd2Wages), Val(TxtFfiCharg)
      ElseIf Work = "E" Then
      
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp1AcCode & "' and Emp_CrDr='Dr.' "
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal - " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp1AcCode & "' and Emp_CrDr='Cr.' "
        
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp2AcCode & "' and Emp_CrDr='Dr.' "
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal - " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp2AcCode & "' and Emp_CrDr='Cr.' "
        
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal - " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp1AcCode & "' and Emp_CrDr='Dr.' "
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp1AcCode & "' and Emp_CrDr='Cr.' "
        
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal - " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp2AcCode & "' and Emp_CrDr='Dr.' "
        'Con.Execute "Update Emp_Mst Set Emp_OpSal=Emp_OpSal + " & Val(TxtLd1Wages) & " where Emp_AcCode='" & Emp2AcCode & "' and Emp_CrDr='Cr.' "
        
         '''ObjTrn.PostDamage False, Val(TxtDPayment), Val(TxtCrAmt), ObjDmg.DtRefno, ClAcCode, Emp1AcCode, Val(TxtLd1Wages), Emp2AcCode, Val(TxtLd2Wages), Val(TxtFfiCharg)
      End If
      
     
        
If Work = "A" Then
    BillNo = GetNextDmgNo("Select Max(Cast(Dt_InvNo as Numeric)) From Dmg_Trn where Dt_fs='B'")
    CmdSave.BackColor = BgArea.Cm_SaveClr
    CmdAdd.BackColor = &H2877B7
    If ObjDmg.DtFs = "B" Then
       ' CreateBill
    Else
        'CreateDamage
    End If
    
    CreateDamage
    
    Clear Me
    ClearFlex MsfDamage, TotalRows
    ProcessStart = False
    TxtBillNo = GetNextDmgNo("Select Max(Cast(Dt_InvNo as Numeric)) From Dmg_Trn where Dt_fs='B'")
    'CmdAdd_Click
ElseIf Work = "E" Then
    CmdSave.BackColor = BgArea.Cm_SaveClr
    CmdModify.BackColor = &H2877B7
    If ObjDmg.DtFs = "B" Then
        CreateBill
        
    Else
     CreateDamage
    End If
    Clear Me
    ClearFlex MsfDamage, TotalRows
    ProcessStart = False
    'CmdModify_Click
End If
 Set ObjTrn = Nothing
End Sub
Sub UpdateItemQty()
Dim I As Integer
Dim Sql As String
Sql = ""
    For I = 1 To MsfDamage.Rows - 1
        Sql = "select Itm_CuStk,Itm_Code from Itm_Mst where Itm_Code=" & Val(MsfDamage.TextMatrix(I, 8))
            If RsOpen(Sql) = True Then
                Do While Not Rs.EOF
                    Con.Execute "Update Itm_Mst set Itm_CuStk= Itm_Custk + " & Val(MsfDamage.TextMatrix(I, 3)) & " where Itm_Code=" & Val(MsfDamage.TextMatrix(I, 8))
            Rs.MoveNext

            Loop
            End If
    Next I
End Sub
Sub CancelBilling()
Dim I As Integer
Dim S As String
S = ""
S = "select Ss_Refno,Ss_ItCd,Ss_Slno,Ss_Qty From Sls_sub where Ss_Refno=" & RefNo
    If RsOpen(S) = True Then
        Do While Not Rs.EOF
            Con.Execute "Update Itm_Mst set Itm_CuStk= Itm_CuStk - " & Rs!Ss_qty & " where Itm_Code=" & Rs!Ss_ItCd
            Rs.MoveNext
        Loop
    End If
        
End Sub



Private Sub CmdSave_GotFocus()
CmdSave.BackColor = BgArea.Cm_Bcf
End Sub

Private Sub Form_Load(): PaintControls Me
    ClSlno = 0: ClName = 1: ClRate = 2: Clqty = 3: ClTax = 4: ClTaxAmt = 8: ClDIs = 5: ClDIsAmt = 6: Clgross = 7: ClNetTotal = 9: Clitcd = 10
    TxtBillNo = GetNextDmgNo("Select Max(Cast(St_InvNo as Numeric)) From Sls_Trn where st_fs='B'")
    TotalRows = 100
    MsfDamage.Rows = TotalRows
    Me.Left = 100
    Me.Top = 0
    MedBillDate = Date
    MedLrDate = Date
    RptFromDate = MedBillDate
    RptToDate = MedBillDate
    Work = "A"
    MsfDamage.ColWidth(ClSlno) = 750        'slno
    MsfDamage.ColWidth(ClName) = 4500      'name
    MsfDamage.ColWidth(ClRate) = 1400       'rate
    MsfDamage.ColWidth(Clqty) = 800      'qty
    MsfDamage.ColWidth(ClTax) = 800       'tax%
    MsfDamage.ColWidth(ClDIs) = 800        'taxamt
    
    MsfDamage.ColWidth(ClDIsAmt) = 1000     'Disc%
    MsfDamage.ColWidth(Clgross) = 1400  'DiscAmt
    
    MsfDamage.ColWidth(ClTaxAmt) = 1000        'gross
    
    MsfDamage.ColWidth(ClNetTotal) = 1500       'nettotal
    MsfDamage.ColWidth(10) = 0         'itcd
    MsfDamage.ColWidth(11) = 0         'Ucode
'    CurrentControl = 3
'    FraList.Visible = True
'    MsfList.ColWidth(0) = 3750
'    MsfList.ColWidth(1) = 3750
'    MsfList.ColWidth(2) = 0
'    FraList.Width = 6815
'    MsfList.Width = 6515
'    FraList.Left = 3500
'    FraList.Top = 1450
'    MsfList.Col = 0
'    FlexSearch 32, CODESEARCH, SEARCHSTRING, Me, MsfList
    CmdAdd.BackColor = &H2877B7
    CmdModify.BackColor = BgArea.Cm_SaveClr
    cmdDELETE.BackColor = BgArea.Cm_SaveClr
    
    CmdSave.BackColor = BgArea.Cm_SaveClr
    Dim S As String
    StValue = 0
    S = "select * from Rm_PrinterSetup"
    If RsOpen(S) = True Then
        StValue = Val(Rs!Rm_StValue)
        BillPrint = Trim(Rs!Rm_SlsPrint)
    End If
End Sub

Private Sub msf1_GotFocus()
 TXTSRH.Visible = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
'If MsgBox("Are you sure to exit (Y/N", vbyesno + Vbinformation) = vbNo Then
'    Me.c
'Exit Sub
 ChangeColor , Me
End Sub

Private Sub MedBillDate_GotFocus()
 ChangeColor ActiveControl
 MedBillDate.SelStart = 0
End Sub

Private Sub MedBillDate_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
TxtType.SetFocus
ElseIf KeyCode = 38 Then
    TxtBillNo.SetFocus
End If
End Sub

Private Sub MsfAcc_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
    FraAcc.Visible = False
    Exit Sub
End If
If KeyCode = 13 Then
    TxtClientName.Text = Trim(MsfAcc.TextMatrix(MsfAcc.Row, 0))
    FraAcc.Visible = False
    TxtClientName.SetFocus
End If

    
End Sub

Private Sub MedBillDate_KeyPress(KeyAscii As Integer)
If KeyAscii >= 37 And KeyAscii <= 40 Then
Else
  ActiveControl.SelLength = 1
  ActiveControl.SetFocus
End If
End Sub

Private Sub MSFLIST_EnterCell()
' If MsfList.Col > 1 Then
'    MsfList.Col = 1
'
' End If
End Sub
Private Sub MsfList_GotFocus()
    CMDCLOSE.Cancel = False
End Sub
Private Sub MsfList_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 27 Then
    If CurrentControl = 4 Then
        TxtEmp1.SetFocus
    ElseIf CurrentControl = 5 Then
        TxtLd1Wages.SetFocus
    Else
    FraList.Visible = False
    MsfDamage.SetFocus
    Exit Sub
   End If
ElseIf KeyCode = 13 Then
    If Trim(MsfList.TextMatrix(MsfList.Row, MsfList.Col)) = "" Then Exit Sub
    If CurrentControl = 0 Then
        MsfList.Cols = 4
        If Val(MsfList.TextMatrix(MsfList.Row, 1)) <> 0 Then
            PrhRate = 0
            MsfDamage.TextMatrix(MsfDamage.Row, ClSlno) = MsfDamage.Row
            MsfDamage.TextMatrix(MsfDamage.Row, ClName) = Trim(MsfList.TextMatrix(MsfList.Row, 0))
            MsfDamage.TextMatrix(MsfDamage.Row, Clitcd) = Trim(MsfList.TextMatrix(MsfList.Row, 2))
            GetItem (Val(MsfList.TextMatrix(MsfList.Row, 2)))
            'MsfDamage.TextMatrix(MsfDamage.Row, ClRate) = Trim(MsfList.TextMatrix(MsfList.Row, 1))
            'MsfDamage.TextMatrix(MsfDamage.Row, 2) = FmtNumber(MsfList.TextMatrix(MsfList.Row, 1))
            'PrhRate = FmtNumber(MsfList.TextMatrix(MsfList.Row, 1))
            MsfDamage.TextMatrix(MsfDamage.Row, Clgross) = Val(MsfList.TextMatrix(MsfList.Row, 1))
            MsfDamage.Col = 2
            If Work = "A" Then
                GetPreviousRate ClAcCode, MsfDamage.TextMatrix(MsfDamage.Row, Clgross)
            Else
                GetPreviousRateModiyy ClAcCode, MsfDamage.TextMatrix(MsfDamage.Row, Clgross)
            End If
            
            MsfDamage.SetFocus
            
        Else
            MsgBox "No Record Found"
            MsfDamage.SetFocus
            Exit Sub
        End If
    ElseIf CurrentControl = 1 Then
        MsfList.Cols = 5
        If Val(MsfList.TextMatrix(MsfList.Row, 2)) <> 0 Then
            ObjDmg.DsRefno = 0
            ObjDmg.DtRefno = 0
            RefNo = 0
            TxtBillNo = Trim(MsfList.TextMatrix(MsfList.Row, 1))
            BillNo = Trim(MsfList.TextMatrix(MsfList.Row, 1))
            
            ObjDmg.DtRefno = Val(MsfList.TextMatrix(MsfList.Row, 3))
            ObjDmg.DsRefno = Val(MsfList.TextMatrix(MsfList.Row, 3))
            RefNo = Val(MsfList.TextMatrix(MsfList.Row, 3))
            GetRecord
            FraList.Visible = False
        Else
            MsgBox "No Record Found"
            MsfDamage.SetFocus
            Exit Sub
        End If
        MsfDamage.Col = 1
        If Work = "E" Then
            MedBillDate.BackColor = BgArea.Cm_Bcf
            MedBillDate.SetFocus
        ElseIf Work = "D" Then
            CmdSave.SetFocus
        End If
        Exit Sub
    ElseIf CurrentControl = 2 Then
        If Val(MsfList.TextMatrix(MsfList.Row, 1)) <> 0 Then
            TxtType = Trim(MsfList.TextMatrix(MsfList.Row, 0))
            TypeCode = Val(MsfList.TextMatrix(MsfList.Row, 1))
            FraList.Visible = False
            TxtType.SetFocus
        Else
            MsgBox "No Record Found"
            MsfDamage.SetFocus
            Exit Sub
        End If
    ElseIf CurrentControl = 3 Then
        LblCrDr.Caption = ""
        DebitTotal = 0: CreditTotal = 0: BalAmt = 0: OpBalCr = 0: OpBalDr = 0
        TxtClientName = Trim(MsfList.TextMatrix(MsfList.Row, 0))
        TxtPlace = Trim(MsfList.TextMatrix(MsfList.Row, 1))
        ClAcCode = Trim(MsfList.TextMatrix(MsfList.Row, 2))
        ClientCode = ClAcCode
        ClientAdd1 = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_Add1")
        ClientAdd2 = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_Add2")
        ClientAdd3 = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_City")
        ClientDistrict = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_District")
        ClientState = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_State")
        FraList.Visible = False
            GetOpBal ClAcCode, RptFromDate, RptToDate, 1, OpBalDr, OpBalCr
            DebitTotal = OpBalDr
            CreditTotal = OpBalCr
            BalAmt = DebitTotal - CreditTotal
            If Val(BalAmt) > 0 Then
                LblCrDr.Caption = FmtNumber(BalAmt) + " Dr."
            Else
                LblCrDr.Caption = FmtNumber(Abs(BalAmt)) + " Cr."
            End If
        CmdSave.SetFocus
        Exit Sub
    ElseIf CurrentControl = 4 Then
        Emp1Refno = 0
        'If Trim(MsfList.TextMatrix(MsfList.Row, 1)) = "" Then Exit Sub
            TxtEmp1 = Trim(MsfList.TextMatrix(MsfList.Row, 0))
            Emp1Refno = Val(MsfList.TextMatrix(MsfList.Row, 2))
            Emp1AcCode = Trim(MsfList.TextMatrix(MsfList.Row, 3))
            TxtLd1Wages = Val(MsfList.TextMatrix(MsfList.Row, 4))
            FraList.Visible = False
            TxtEmp2.SetFocus
        'End If
    ElseIf CurrentControl = 5 Then
        Emp2Refno = 0

        'If Trim(MsfList.TextMatrix(MsfList.Row, 1)) <> "" Then
            TxtEmp2 = Trim(MsfList.TextMatrix(MsfList.Row, 0))
            Emp2Refno = Val(MsfList.TextMatrix(MsfList.Row, 2))
            Emp2AcCode = Trim(MsfList.TextMatrix(MsfList.Row, 3))
            TxtLd2Wages = Val(MsfList.TextMatrix(MsfList.Row, 4))
            FraList.Visible = False
            TxtLd1Wages.SetFocus
        'End If
    End If
Else
    
    FlexSearch KeyCode, CodeSearch, SearchString, Me, MsfList

End If
End Sub
Function MsfSlTm(rw As Long, Cl As Long) As String
    With MsfDamage
     MsfSlTm = Trim(MsfDamage.TextMatrix(rw, Cl))
    End With
End Function
Function GetPreviousRate(ClAccCode As String, ItmCode As Long) As Double
Dim InvNo As String
Dim S As String

RsOpen ("Select Max(Cast(Ss_InvNo as Numeric)) From Sls_sub where  Ss_ClAcCd ='" & ClAccCode & "'")
If Trim(Rs.Fields(0)) <> "" Then
    InvNo = Trim(Rs.Fields(0))
End If
Rs.Close
S = ""

S = "select Ss_Rate from Sls_Sub where Ss_InvNo ='" & InvNo & "' and Ss_ItCd=" & ItmCode & " "
If RsOpen(S) = True Then

MsfDamage.TextMatrix(MsfDamage.Row, 11) = FmtNumber(Rs!Ss_Rate)
Else

End If

End Function
Function GetPreviousRateModiyy(ClAccCode As String, ItmCode As Long) As Double
Dim InvNo As String
Dim S As String

RsOpen ("Select Max(Cast(Ss_InvNo as Numeric)) From Sls_sub where  Ss_ClAcCd ='" & ClAccCode & "' and Ss_InvNo <> '" & TxtBillNo & "'")
If Trim(Rs.Fields(0)) <> "" Then
    InvNo = Trim(Rs.Fields(0))
End If
Rs.Close
S = ""
'InvNo = InvNo - 1
'MsgBox InvNo
S = "select Ss_Rate from Sls_Sub where Ss_InvNo ='" & InvNo & "' and Ss_ItCd=" & ItmCode & " "
If RsOpen(S) = True Then

MsfDamage.TextMatrix(MsfDamage.Row, 11) = FmtNumber(Rs!Ss_Rate)
Else

End If

End Function
Function MsfLsTm(rw As Long, Cl As Long) As String
    With MsfList
      'MsfLsTm = Trim(MsfList.TextMatrix(rw, Cl))
    End With
End Function
Private Sub MsfList_LostFocus()
 FraList.Visible = False
 CMDCLOSE.Cancel = True
End Sub
Private Sub MsfDamage_EnterCell()
If MsfDamage.Col = 3 Then
    PrhRate = 0
  PrhRate = Val(MsfDamage.TextMatrix(MsfDamage.Row, 2))
End If

LastColumn = MsfDamage.Col
End Sub
Private Sub MsfDamage_GotFocus()
CurrentControl = 0
End Sub
Sub CalculateTotal()
'Clslno = 0: ClName = 1: ClRate = 2: ClQty = 3: ClTax = 4: ClTaxAmt = 7: ClDIs = 5: ClDIsAmt = 6: Clgross = 8: ClNetTotal = 9: Clitcd = 10
 Dim I As Integer
 Dim Gtotal As Double
 StNetTotal = 0
 Dim Gross4 As Double, Gross12 As Double, Gross1 As Double, Gross0 As Double
 Dim IPT4 As Double, IPT12 As Double, IPT1 As Double, IPT0 As Double
 Dim NetTotal As Double, Tax As Double, cess As Double, Disc As Double
 NetTotal = 0: Tax = 0: cess = 0: Disc = 0
 For I = 1 To MsfDamage.Rows - 1
    Gtotal = Gtotal + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, Clgross))))
    If Val(MsfDamage.TextMatrix(I, ClTax)) = 4 Then
    Gross4 = Gross4 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, Clgross))))
    ElseIf Val(MsfDamage.TextMatrix(I, ClTax)) = 12.5 Then
    Gross12 = Gross12 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, Clgross))))
    ElseIf Val(MsfDamage.TextMatrix(I, ClTax)) = 1 Then
    Gross1 = Gross1 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, Clgross))))
    ElseIf Val(MsfDamage.TextMatrix(I, ClTax)) = 0 Then
    Gross0 = Gross0 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, Clgross))))
    End If
    If Val(MsfDamage.TextMatrix(I, ClTax)) = 4 Then
    IPT4 = IPT4 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, ClTaxAmt))))
    ElseIf Val(MsfDamage.TextMatrix(I, ClTax)) = 12.5 Then
    IPT12 = IPT12 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, ClTaxAmt))))
    ElseIf Val(MsfDamage.TextMatrix(I, ClTax)) = 1 Then
    IPT1 = IPT1 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, ClTaxAmt))))
    ElseIf Val(MsfDamage.TextMatrix(I, ClTax)) = 0 Then
    IPT0 = IPT0 + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, ClTaxAmt))))
    End If
    NetTotal = NetTotal + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, ClNetTotal))))
    Tax = Tax + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, ClTaxAmt))))
    StNetTotal = StNetTotal + Val(FmtNumber(Val(MsfDamage.TextMatrix(I, 11))))
Next I
 cess = Tax * 0.01
 NetTotal = NetTotal + cess
 TxtGross = FmtNumber(Gtotal)
 TxtGross4 = FmtNumber(Gross4)
 TxtGross12 = FmtNumber(Gross12)
 TxtGross1 = FmtNumber(Gross1)
 TxtGross0 = FmtNumber(Gross0)
 TxtGross = FmtNumber(Gtotal)
 TxtIpt4 = FmtNumber(IPT4)
 TxtIpt12 = FmtNumber(IPT12)
 TxtIpt1 = FmtNumber(IPT1)
 TxtIpt0 = FmtNumber(IPT0)
 TxtTaxAmount = FmtNumber(Tax)
 TxtCess = FmtNumber(cess)
 TxtNetTotal = FmtNumber(NetTotal)
End Sub
Private Sub MsfDamage_KeyDown(KeyCode As Integer, Shift As Integer)
'Clslno = 0: ClName = 1: ClRate = 2: ClQty = 3: ClTax = 4: ClTaxAmt = 5: ClDIs = 6: ClDIsAmt = 7: Clgross = 8: ClNetTotal = 9: Clitcd = 10
If KeyCode = 46 Then
    If Trim(MsfDamage.TextMatrix(MsfDamage.Row, ClName)) = "" Then
        DelRow MsfDamage.Row, MsfDamage
    Else
        MsfDamage.Text = ""
    End If
    Exit Sub
End If
    If MsfDamage.Col = ClRate Or MsfDamage.Col = Clqty Or MsfDamage.Col = ClTax Or MsfDamage.Col = ClDIs Or MsfDamage.Col = ClDIsAmt Then
            If LastColumn <> MsfDamage.Col And KeyCode <> 13 And Not (KeyCode >= 37 And KeyCode <= 40) Then
              MsfDamage.TextMatrix(MsfDamage.Row, MsfDamage.Col) = ""
              LastColumn = MsfDamage.Col
           End If
        MsfDamage.Text = MsfDamage.Text + KeyCodeToCharacter(KeyCode, False)
        CalculateItemTotal (MsfDamage.Row)
    End If
If KeyCode = 13 Then

     If MsfDamage.Col = ClName And Trim(MsfDamage.TextMatrix(MsfDamage.Row, MsfDamage.Col)) = "" And Work = "A" Then
             If Trim(MsfDamage.TextMatrix(MsfDamage.Row - 1, 1)) = "" Then Exit Sub
              If KeyCode = 13 Then
              FlexSearch 46, CodeSearch, SearchString, Me, MsfList
              CurrentControl = 0
              FraList.Visible = True
              CodeSearch = True
              MsfList.Col = 0
              MsfList.ColWidth(0) = 4600
              MsfList.ColWidth(1) = 1500
              MsfList.ColWidth(2) = 0
              MsfList.ColWidth(3) = 1000
              MsfList.ColWidth(4) = 1000
              MsfList.Width = 8475
              FraList.Width = 8675
              FraList.Top = 1560
              FraList.Left = 3000
              MsfList.SetFocus
              Exit Sub
              End If
     ElseIf MsfDamage.Col = ClName And Trim(MsfDamage.Text) <> "" Then
        GetItemTax (Val(MsfDamage.TextMatrix(MsfDamage.Row, Clitcd)))
        NextColumn
     ElseIf MsfDamage.Col = ClRate Then
        NextColumn
     ElseIf MsfDamage.Col = Clqty Then
       If Val(MsfDamage.TextMatrix(MsfDamage.Row, Clqty)) = 0 Then
        MsfDamage.TextMatrix(MsfDamage.Row, Clqty) = 1
       End If
       NextColumn
     ElseIf MsfDamage.Col = ClTax Then
        If TxtType = "B" And Val(MsfDamage.TextMatrix(MsfDamage.Row, ClTax)) = 0 Then
        
        Else
        NextColumn
        End If
     ElseIf MsfDamage.Col = ClTaxAmt Then
        NextColumn
     ElseIf MsfDamage.Col = ClDIs Then
        If MsfDamage.TextMatrix(MsfDamage.Row, ClDIs) = "" Then
        MsfDamage.TextMatrix(MsfDamage.Row, ClDIs) = 0
        End If
        RateOrg = getRateOrg(Val(MsfDamage.TextMatrix(MsfDamage.Row, ClRate)), Val(MsfDamage.TextMatrix(MsfDamage.Row, ClTax)))
        MsfDamage.TextMatrix(MsfDamage.Row, ClDIsAmt) = FormatNumber(RateOrg * MsfDamage.TextMatrix(MsfDamage.Row, ClDIs) / 100) * MsfDamage.TextMatrix(MsfDamage.Row, Clqty)
         NextColumn
     ElseIf MsfDamage.Col = ClDIsAmt Then
        'MsfDamage.TextMatrix(MsfDamage.Row, ClDIs) = FormatNumber((MsfDamage.TextMatrix(MsfDamage.Row, ClDIsAmt) / (RateOrg * MsfDamage.TextMatrix(MsfDamage.Row, ClQty)) * 100))
       CalculateItemTotal (MsfDamage.Row)
       CalculateTotal
       If MsfDamage.Row = MsfDamage.Rows - 1 Then
          MsfDamage.Text = FmtNumber(Val(MsfDamage.Text))
          IncrementRow
          MsfDamage.TextMatrix(MsfDamage.Row, ClSlno) = MsfDamage.Row
       Else
          MsfDamage.Text = FmtNumber(Val(MsfDamage.Text))
          NextRow

       End If
    End If
ElseIf KeyCode = 112 Then
    If MsfDamage.Col = 1 Then
        FlexSearch 46, CodeSearch, SearchString, Me, MsfList
        CurrentControl = 0
              FraList.Visible = True
              CodeSearch = True
              MsfList.Col = 0
              MsfList.ColWidth(0) = 4600
              MsfList.ColWidth(1) = 1500
              MsfList.ColWidth(2) = 0
              MsfList.ColWidth(3) = 1000
              MsfList.ColWidth(4) = 1000
              MsfList.Width = 8475
              FraList.Width = 8675
              FraList.Top = 1560
              FraList.Left = 3000
              MsfList.SetFocus
              Exit Sub
    End If
ElseIf KeyCode = 8 Then

 If MsfDamage.Col = Clqty Or MsfDamage.Col = ClTax Then
    FlexBackSpace MsfDamage, MsfDamage.Row, MsfDamage.Col
 End If
ElseIf KeyCode = vbKeyF8 And MsfDamage.Col < Clqty Then
    CalculateTotal
    TxtCessPer.SetFocus
End If
End Sub
Sub CalCulateTotalWeight()
Dim I As Long
    TotalWeight = 0
    For I = 1 To MsfDamage.Rows - 1
         If Trim(MsfDamage.TextMatrix(I, 3)) = "" Then Exit For
         TotalWeight = TotalWeight + Val(MsfDamage.TextMatrix(I, Clqty))
     Next I
         'MsfPurchase.TextMatrix(MsfPurchase.Row + 3, 3) = ""
'     MsfPurchase.TextMatrix(MsfPurchase.Row + 2, 3) = ""
'     MsfPurchase.TextMatrix(MsfPurchase.Row + 2, 1) = ""
     MsfDamage.TextMatrix(13, ClName) = "                                        Total Qty"
     MsfDamage.TextMatrix(13, Clqty) = Val(TotalWeight)
End Sub

Private Sub MsfDamage_LostFocus()
LastColumn = -1
End Sub

Private Sub MsfDamage_RowColChange()
 LastColumn = -1
End Sub
Sub UpdatePurchaseCost()
           Dim NewPurRate As Double
           NewPurRate = 0
           NewPurRate = Val(MsfDamage.TextMatrix(MsfDamage.Row, 2))
              'If MsgBox("Save New Cost?", vbyesno + Vbinformation) = vbYes Then
              S = "Update Itm_Mst set Itm_Rate=" & NewPurRate & " where Itm_Code=" & Val(MsfDamage.TextMatrix(MsfDamage.Row, 8))
                Con.Execute S
              'End If
           MsfDamage.SetFocus
End Sub
Private Sub TXTADD_DED_KeyDown(KeyCode As Integer, Shift As Integer)
If IsNumeric(TXTADD_DED) = True And KeyCode = 13 Then
 AddDeduct = Val(TXTADD_DED.Text)
 'CalculateItemValues
 CmdSave.SetFocus
ElseIf KeyCode = 13 Then
 TXTADD_DED = 0
' CalculateItemValues
 CmdSave.SetFocus
End If
End Sub
Function GetQuery() As String
Dim S As String
S = ""
    Select Case (CurrentControl)
        Case 0:
            If CodeSearch = True Then
            S = "Select Itm_Name 'Item Name',cast(Itm_rate as nvarchar(10)) 'Rate', Itm_Code,Itm_PrhTotal'Prh Rate',Itm_custk'Stock' from Itm_Mst where Itm_Name like '" & Trim(SearchString) & "%' order by Itm_Name"
            Else
            S = "Select Itm_Name 'Item Name',cast(Itm_rate as nvarchar(10)) 'Rate', Itm_Code,Itm_PrhTotal'Prh Rate',Itm_custk'Stock' from Itm_Mst where Itm_Rate like '" & Trim(SearchString) & "%'  order by Itm_name"
            End If
        Case 1:
            If CodeSearch = True Then
                S = "select Dt_ClientName 'CLIENT NAME',Dt_InvNo 'BILL NO', Convert(nvarchar(10),Dt_Date,103) 'Date',Dt_Refno,Dt_ClientCity 'CITY',Cast(Dt_NetAmt as nvarchar(10)) 'NetAmt' from Dmg_Trn where Dt_ClientName like '" & Trim(SearchString) & "%' order by cast(Dt_InvNo as numeric) desc, Dt_Date desc"
                
            Else
                S = "select Dt_ClientName 'CLIENT NAME',Dt_InvNo 'BILL NO', Convert(nvarchar(10),Dt_Date,103) 'Date',Dt_Refno,Dt_ClientCity 'CITY',Cast(Dt_NetAmt as nvarchar(10)) 'NetAmt' from Dmg_Trn where Dt_InvNo like '" & Trim(SearchString) & "%' order by Dt_InvNo desc"
            End If
        Case 2:
            S = "select T_Name 'Type',T_Code from Type_Mst where T_Name like '" & Trim(SearchString) & "%' order by T_Code"
        Case 3:
            S = "select Cl_Name 'CLIENT NAME',Cl_Add1 'Add1',Cl_AcCode from Client_Mst  where Cl_Type='N' and Cl_Name like '" & Trim(SearchString) & "%' order by Cl_Name"
        Case 4:
            S = "select Emp_Name 'Employee Name' ,Emp_Desg 'Site',Emp_RefNo,Emp_AcCode,Emp_Basic,Emp_OpSal from Emp_Mst where Emp_Name like '" & Trim(SearchString) & "%' order by Emp_Name"
        Case 5:
            S = "select Emp_Name 'Employee Name' ,Emp_Desg 'Site',Emp_RefNo,Emp_AcCode,Emp_Basic,Emp_OpSal from Emp_Mst where Emp_Name like '" & Trim(SearchString) & "%' order by Emp_Name"
    End Select
GetQuery = S
End Function

Function CalculateItemTotal(RwNumber As Long) As String
 DoEvents
 Dim ItemRate As Double
 Dim TotRate As Double
 Dim RateWithoutTax As Double
 Dim ItemQty As Double
 Dim ItemGrossTotal As Double
 Dim ItemTax As Double
 Dim ItemDis As Double
 Dim ItemNetTotal As Double
 Dim ItemTaxAmt As Double
 Dim StTot As Double
 
 ItemRate = 0: ItemQty = 0: ItemGrossTotal = 0: ItemTax = 0: ItemNetTotal = 0: ItemTaxAmt = 0: StTot = 0
  With MsfDamage
  
     ItemQty = Val(FmtNumber(Val(.TextMatrix(RwNumber, Clqty))))
     ItemTax = Val(FmtNumber(Val(.TextMatrix(RwNumber, ClTax))))
     ItemDis = Val(FmtNumber(Val(.TextMatrix(RwNumber, ClDIsAmt))))
     ItemRate = Val(FmtNumber(Val(.TextMatrix(RwNumber, ClRate))))
     
     RateOrg = getRateOrg(ItemRate, ItemTax)
     
     
     ItemGrossTotal = (RateOrg * ItemQty) - ItemDis
     
         
        
        'StTot = Val(FmtNumber(ItemQty) * Val(StValue))
        ItemTaxAmt = ItemGrossTotal * (ItemTax / 100)
        ItemNetTotal = ItemGrossTotal + ItemTaxAmt
            MsfDamage.TextMatrix(RwNumber, Clgross) = FmtNumber(ItemGrossTotal)
            Debug.Print FmtNumber(ItemGrossTotal)
            MsfDamage.TextMatrix(RwNumber, ClTaxAmt) = FmtNumber(ItemTaxAmt)
            MsfDamage.TextMatrix(RwNumber, ClNetTotal) = FmtNumber(ItemNetTotal)
            ''MsfDamage.TextMatrix(RwNumber, 11) = FmtNumber(StTot)
  End With
End Function


Private Sub TxtBillNo_GotFocus()
'  CurrentControl = 0
 ChangeColor ActiveControl
TxtBillNo.BackColor = BgArea.Cm_Bcf
End Sub

Private Sub TxtBillNo_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 And TxtBillNo.Text = "" Then
    TxtBillNo.SetFocus
ElseIf KeyCode = 13 Or KeyCode = 40 And TxtBillNo.Text <> "" Then
'  FlexSearch 46, CodeSearch, SearchString, Me, MsfList
'  CurrentControl = 0
'  FraList.Visible = True
'  CodeSearch = True
'  MsfList.COL = 0
'  MsfList.ColWidth(0) = 3500
'  MsfList.ColWidth(1) = 1500
'  MsfList.ColWidth(2) = 0
'  MsfList.Width = 6675
'  FraList.Width = 6875
'  FraList.Top = 1560
'  FraList.Left = 3000
MedBillDate.SetFocus
ElseIf KeyCode = 38 Then
 TxtClientName.SetFocus
End If
End Sub


Sub PrepareSerialNo()
For I = 1 To MsfDamage.Rows - 1
    MsfDamage.TextMatrix(I, 0) = I
    DoEvents
Next I
End Sub


Sub GetRecord()
ClearFlex MsfDamage, 100
Dim Sql As String
Dim SlNo As Integer
ClientCode = 0
Sql = "Select * from Sls_Trn where St_Refno = " & Val(RefNo)
 OldEmp1Wage = 0: OldEmp2Wage = 0
If RsOpen(Sql) = True Then

    ObjDmg.DtRefno = Val(Rs!St_Refno)
    TxtType = Trim(Rs!St_Fs)
    MedBillDate = Rs!St_Date
    TxtGross = FmtNumber(Rs!St_Gross)
    TxtDiscount = FmtNumber(Rs!St_disc)
    TxtOtherAdd = FmtNumber(Rs!St_OthAdd)
   ' TxtTaxPer1 = FmtNumber(Rs!St_tax)
    TxtTaxAmount = FmtNumber(Rs!St_TaxAmt)
    TxtNetTotal = FmtNumber(Rs!St_NetAmt)
    ClientCode = Val(Rs!St_ClientCode)
    TxtClientName = Trim(Rs!St_ClientName)
    TxtPlace = Trim(Rs!St_ClientCity)
    ClientAdd3 = Trim(Rs!St_ClientCity)
    TxtGross4 = FmtNumber(Rs!St_Gross4)
    TxtGross12 = FmtNumber(Rs!St_Gross12)
    TxtGross1 = FmtNumber(Rs!St_Gross1)
    TxtGross0 = FmtNumber(Rs!St_Gross0)
    TxtIpt4 = FmtNumber(Rs!St_ipt4)
    TxtIpt12 = FmtNumber(Rs!St_ipt12)
    TxtIpt1 = FmtNumber(Rs!St_ipt1)
    TxtIpt0 = 0
    TxtCess = FmtNumber(Rs!St_cess)
    TxtCessPer = 1
    TxtRountOff = 0
    TxtDisPer = FmtNumber(Rs!St_discPer)
    TxtExp = FmtNumber(Rs!St_Exp)
    TxtCommPer = FmtNumber(Rs!St_ComPer)
    TxtCommAmt = FmtNumber(Rs!St_ComAmt)
    TxtVehNo = Trim(Rs!St_VehNo)
    TxtDPayment = FmtNumber(Rs!St_Cash)
    TxtCrAmt = FmtNumber(Rs!St_Credit)
    PFlag = Trim(Rs!St_P_Flag)
    TxtEmp1 = Trim(Rs!St_Emp1)
    TxtEmp2 = Trim(Rs!St_Emp2)
    Emp1AcCode = Trim(Rs!St_Emp1AcCd)
    Emp2AcCode = Trim(Rs!St_Emp2AcCd)
    ClAcCode = Trim(Rs!St_ClAcCd)
    TxtFfiCharg = FmtNumber(Rs!St_FrightChar)
    TxtLd1Wages = FmtNumber(Rs!St_Emp1Wages)
    TxtLd2Wages = FmtNumber(Rs!St_Emp2Wages)
    OldEmp1Wage = FmtNumber(Rs!St_Emp1Wages)
    OldEmp2Wage = FmtNumber(Rs!St_Emp2Wages)
    
    ClientAdd1 = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_Add1")
    ClientAdd2 = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_Add2")
    ClientAdd3 = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_City")
    ClientDistrict = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_District")
    ClientState = FetchField("Cl_AcCode", ClAcCode, "Client_Mst", "Cl_State")
    ObjDmg.DtFs = Trim(Rs!St_Fs)
    
Else

    MsgBox "Invalid Bill No"
    Exit Sub
End If
Sql = ""
TotalWight = 0
TotalItem = 0
Sql = "Select * from Sls_Sub where Ss_Refno= " & Val(RefNo) & " order by Ss_Slno"
If RsOpen(Sql) Then
    SlNo = 1
    With MsfDamage
    Do While Not Rs.EOF
        .TextMatrix(SlNo, ClSlno) = SlNo
        .TextMatrix(SlNo, ClName) = Trim(Rs!Ss_ItName)
        .TextMatrix(SlNo, ClRate) = FmtNumber(Rs!Ss_Rate)
        .TextMatrix(SlNo, Clqty) = Val(Rs!Ss_qty)
        .TextMatrix(SlNo, ClTax) = FmtNumber(Rs!Ss_taxper)
        .TextMatrix(SlNo, ClTaxAmt) = FmtNumber(Rs!Ss_tax)
         .TextMatrix(SlNo, ClDIs) = FmtNumber(Rs!Ss_DisPer)
        .TextMatrix(SlNo, ClDIsAmt) = FmtNumber(Rs!Ss_Disamt)
        .TextMatrix(SlNo, Clgross) = FmtNumber(Rs!Ss_Gross)
        .TextMatrix(SlNo, ClNetTotal) = FmtNumber(Val(Rs!Ss_Gross) + Val(Rs!Ss_tax))
        .TextMatrix(SlNo, Clitcd) = Val(Rs!Ss_ItCd)
        If SlNo = .Rows - 1 Then
            .Rows = .Rows + 1
        End If

        SlNo = SlNo + 1
        TotalItem = TotalItem + 1
        Rs.MoveNext
    Loop
    Rs.Close
    End With
    'CalCulateTotalWeight
    If Trim(Work) = "D" Then
        CmdSave.Enabled = True
        CmdSave.SetFocus
    ElseIf Trim(Work) = "E" Then
        MedBillDate.SetFocus
    Else
    Exit Sub
    End If
Else
    MsgBox "Cannot Get Bill Details"
    Exit Sub
End If
LblCrDr.Caption = ""
DebitTotal = 0: CreditTotal = 0: BalAmt = 0: OpBalCr = 0: OpBalDr = 0
GetOpBal ClAcCode, RptFromDate, RptToDate, 1, OpBalDr, OpBalCr
DebitTotal = OpBalDr
CreditTotal = OpBalCr
BalAmt = DebitTotal - CreditTotal
If Val(BalAmt) > 0 Then
    LblCrDr.Caption = FmtNumber(BalAmt) + " Dr."
Else
    LblCrDr.Caption = FmtNumber(Abs(BalAmt)) + " Cr."
End If
End Sub


Private Sub TxtCess_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtCess_KeyDown(KeyCode As Integer, Shift As Integer)
FocusNext TxtDisPer, KeyCode, TxtGross
End Sub

Private Sub TxtCessPer_GotFocus()
 ChangeColor ActiveControl
 TxtCessPer = 1
End Sub

Private Sub TxtCessPer_KeyDown(KeyCode As Integer, Shift As Integer)
TxtCess.Text = Val(TxtTaxAmount.Text) * Val(TxtCessPer) / 100
TxtTotal = Val(TxtGross.Text) + Val(TxtTaxAmount.Text) + Val(TxtCess.Text)
FocusNext TxtDisPer, KeyCode, TxtGross
End Sub

Private Sub TxtCommAmt_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtCommAmt_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
    If Trim(TxtCommAmt) = "" Then
        TxtCommAmt = 0
    End If
    CalculateNetTotal
End If

End Sub

Private Sub TxtCommPer_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtCommPer_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
    TxtCommAmt = Val(TxtGross) * Val(TxtCommPer) / 100
    CalculateNetTotal
End If

End Sub

Private Sub TxtCrAmt_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtCrAmt_KeyDown(KeyCode As Integer, Shift As Integer)

End Sub

Private Sub TxtDiscount_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtDiscount_KeyDown(KeyCode As Integer, Shift As Integer)
CalculateNetTotal
FocusNext TxtNetTotal, KeyCode, TxtDisPer
End Sub

Private Sub TxtDisPer_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtDisPer_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
If Val(TxtDisPer.Text) > 0 Then
TxtDiscount = Val(TxtNetTotal.Text) * (Val(TxtDisPer.Text) / 100)
End If
CalculateNetTotal
End If
FocusNext TxtDiscount, KeyCode, TxtCessPer
End Sub

Private Sub TxtDPayment_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtDPayment_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
    If Val(TxtDPayment) < 1 Then
        TxtDPayment = 0
    End If
    TxtCrAmt = Val(TxtNetTotal) - Val(TxtDPayment)

    CalculateNetTotal
End If
FocusNext TxtNetTotal, KeyCode, TxtDisPer
End Sub



Private Sub TxtDriverNam_GotFocus()
ChangeColor ActiveControl
End Sub

Private Sub TxtDriverNam_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
    TxtBillNo = Trim(TxtInvNo)
    Dim s3 As String
    Dim r3 As New ADODB.Recordset
    s3 = "select *From Sls_Trn where St_InvNo='" & TxtBillNo & "'"
    r3.Open s3, Con, adOpenDynamic, adLockOptimistic
        r3!St_FrightAmt = Val(TxtFrigthTot)
        r3!St_Extra = Val(TxtExtraChar)
        r3!St_Oth = Val(TxtOther)
        r3.Update
    r3.Close
    If Work = "P" Then
        TotKg = 0
        If Trim(TxtTotalWeight) = "" Then
            TotKg = 0
        Else
            TotKg = Val(TxtTotalWeight)
        End If
        'ShowCheckPost
        Clear Me
        ClearFlex MsfDamage, TotalRows
    ElseIf Work = "V" Then
        RefNo = 0
        RsOpen ("select St_Refno from Sls_trn where St_InvNo= '" & Trim(TxtInvNo) & " ' ")
        RefNo = Val(Rs.Fields(0))
        Rs.Close
        GetRecord
        CreateBill
        Clear Me
        ClearFlex MsfDamage, TotalRows
    ElseIf Work = "F" Then
        RefNo = 0
        RsOpen ("select St_Refno from Sls_trn where St_InvNo= '" & Trim(TxtInvNo) & " ' ")
        RefNo = Val(Rs.Fields(0))
        Rs.Close
        GetRecord
   
        Clear Me
        ClearFlex MsfDamage, TotalRows
    ElseIf Work = "R" Then
        RefNo = 0
        RsOpen ("select St_Refno from Sls_trn where St_InvNo= '" & Trim(TxtInvNo) & " ' ")
        RefNo = Val(Rs.Fields(0))
        Rs.Close
        GetRecord
    
        Clear Me
        ClearFlex MsfDamage, TotalRows
    
    End If
ElseIf KeyCode = 38 Then
    TxtOwnerNam.SetFocus
ElseIf KeyCode = 40 Then
    TxtOwnerNam.SetFocus
End If
End Sub


Private Sub TxtGross_Click()
    CmdSave.Enabled = True
    CalculateTotal
End Sub

Private Sub TxtGross_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtGross_KeyDown(KeyCode As Integer, Shift As Integer)
FocusNext TxtTaxAmount, KeyCode, TxtGross
End Sub


Private Sub TxtNetTot_Change(Index As Integer)

End Sub

Private Sub TxtOthAdd_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
End If
End Sub

Private Sub TxtInvNo_GotFocus()
ChangeColor ActiveControl
End Sub
Private Sub TxtNetTotal_GotFocus()
 ChangeColor ActiveControl
 Dim RealPart As Double
  RealPart = Val(FmtNumber(Val(FmtNumber(TxtNetTotal)) - Int(TxtNetTotal)))
    If RealPart > 0 And RealPart < 0.5 Then
       TxtNetTotal = Int(TxtNetTotal)
       TxtRountOff = FormatNumber("-" + str(RealPart))
    ElseIf RealPart >= 0.5 And RealPart <= 0.99 Then
       TxtNetTotal = Int(TxtNetTotal) + 1
       TxtRountOff = FormatNumber(1 - RealPart, 2)
    End If
End Sub

Private Sub TxtNetTotal_KeyDown(KeyCode As Integer, Shift As Integer)
FocusNext TxtClientName, KeyCode, TxtDiscount
End Sub

Private Sub TxtOther_GotFocus()
ChangeColor ActiveControl
End Sub


Private Sub TxtOtherAdd_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtOtherAdd_KeyDown(KeyCode As Integer, Shift As Integer)
CalculateNetTotal
FocusNext TxtNetTotal, KeyCode, TxtDiscount
End Sub

Private Sub TxtClientName_GotFocus()
    ChangeColor ActiveControl
    CurrentControl = 3
End Sub

Private Sub TxtClientName_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 And Work = "A" Then
    'TxtBillNo = GetNextDmgNo("select Max(Sls_Inv_No)from Sls_Trn")
    If TxtClientName = "" And KeyCode = F1 Then
        CurrentControl = 3
        FraList.Visible = True
        MsfList.ColWidth(0) = 3750
        MsfList.ColWidth(1) = 3750
        MsfList.ColWidth(2) = 0
        FraList.Width = 6815
        MsfList.Width = 6515
        FraList.Left = 3500
        FraList.Top = 1450
        MsfList.Col = 0
        FlexSearch 32, CodeSearch, SearchString, Me, MsfList
        MsfList.SetFocus
    Else
        TxtPlace.SetFocus
    End If
ElseIf KeyCode = 13 And Work = "A" And Trim(TxtClientName) <> "" Then


    TxtPlace.SetFocus
ElseIf KeyCode = 13 And Work = "E" Then
LblCrDr.Caption = ""
DebitTotal = 0: CreditTotal = 0: BalAmt = 0: OpBalCr = 0: OpBalDr = 0
GetOpBal ClAcCode, RptFromDate, RptToDate, 1, OpBalDr, OpBalCr
DebitTotal = OpBalDr
CreditTotal = OpBalCr
BalAmt = DebitTotal - CreditTotal
If Val(BalAmt) > 0 Then
    LblCrDr.Caption = FmtNumber(BalAmt) + " Dr."
Else
    LblCrDr.Caption = FmtNumber(Abs(BalAmt)) + " Cr."
End If
    TxtPlace.SetFocus
ElseIf KeyCode = 13 Or KeyCode = 40 And Work = "E" And Trim(TxtClientName) <> "" Then
        TxtPlace.SetFocus

ElseIf KeyCode = 112 Then
    CurrentControl = 3
    FraList.Visible = True
    MsfList.ColWidth(0) = 3750
    MsfList.ColWidth(1) = 3750
    MsfList.ColWidth(2) = 0
    FraList.Width = 6815
    MsfList.Width = 6515
    FraList.Left = 3500
    FraList.Top = 1450
    MsfList.Col = 0
    FlexSearch 32, CodeSearch, SearchString, Me, MsfList
    CmdAdd.BackColor = &H2877B7
    CmdModify.BackColor = BgArea.Cm_SaveClr
    cmdDELETE.BackColor = BgArea.Cm_SaveClr
    CmdSave.BackColor = BgArea.Cm_SaveClr
    MsfList.SetFocus
End If
End Sub

Private Sub TxtTax_Change(Index As Integer)

End Sub

Private Sub TxtTax_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
    'TxtTaxAmount.SetFocus
End If
    
End Sub

Private Sub TxtTaxPer_KeyDown(KeyCode As Integer, Shift As Integer)
'If KeyCode = 13 Then
'
'CalculateNetTotal
'    TxtNetTotal.SetFocus
'End If
'FocusNext TxtNetTotal, KeyCode, TxtOtherAdd
End Sub
Sub CalculateNetTotal()
Dim NetTot As Double
CalculateTotal
NetTot = 0
TxtCess = FmtNumber((TxtTaxAmount * Val(TxtCessPer)) / 100)
NetTot = (Val(TxtGross.Text) + Val(TxtTaxAmount.Text) + Val(TxtCess.Text) - Val(TxtDiscount))
TxtNetTotal = FmtNumber(NetTot)
End Sub

Private Sub TxtOwnerNam_GotFocus()
ChangeColor ActiveControl
End Sub



Private Sub TxtOwnerNam_KeyPress(KeyAscii As Integer)
KeyAscii = ToUpper(KeyAscii)
End Sub

Private Sub TxtPlace_KeyDown(KeyCode As Integer, Shift As Integer)
FocusNext CmdSave, KeyCode, TxtClientName
End Sub

Private Sub TxtTaxAmount_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtTaxAmount_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
    TxtCessPer.SetFocus
End If
End Sub

Private Sub TxtTaxPer1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then

CalculateNetTotal
    TxtTaxAmount.SetFocus
End If
End Sub


Private Sub TxtTinNo_GotFocus()
 ChangeColor ActiveControl
End Sub

Private Sub TxtTinNo_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Or KeyCode = 40 Then
    TxtBillNo.SetFocus
ElseIf KeyCode = 38 Then
    TxtClientName.SetFocus
End If
End Sub

Private Sub TxtTotalWeight_GotFocus()
ChangeColor ActiveControl
End Sub


Function GetOpBal(AccCode As String, FrmDate As Date, ToDate As Date, BrnchCd As Integer, ByRef OpBalDr As Double, ByRef OpBalCr As Double) As Boolean
If BrnchCd = 1 Then
 'Head office
 S = "Select As_OpBalDr,As_OpBalCr from Ac_Sub where As_Code = '" & Trim(AccCode) & "' and As_Branch=1"
ElseIf BrnchCd = -1 Then
 'Consolidated
 S = "Select sum(As_OpBalDr),sum(As_OpBalCr) from Ac_Sub where As_Code = '" & Trim(AccCode) & "'"
Else
 'Branch
 S = "Select As_OpBalDr,As_OpBalCr from Ac_Sub where As_Code = '" & Trim(AccCode) & "' and As_Branch=" & BrnchCd
End If



If RsOpen(S) = True Then
    OpBalDr = RetField(Rs.Fields(0))
    OpBalCr = RetField(Rs.Fields(1))
    Rs.Close
End If

'*******************************************
If BrnchCd = 1 Then
   S = "Select Sum(Trn_Amt) from Trn_Mst where  Trn_DCode='" & Trim(AccCode) & "' and Trn_Date<'" & Format(FrmDate, "MM/dd/yyyy") & "' and Trn_BrCd=1"
ElseIf BrnchCd = -1 Then
  S = "Select Sum(Trn_Amt) from Trn_Mst where  Trn_DCode='" & Trim(AccCode) & "' and Trn_Date<'" & Format(FrmDate, "MM/dd/yyyy") & "'"
Else
  S = "Select Sum(Trn_Amt) from Trn_Mst where  Trn_DCode='" & Trim(AccCode) & "' and Trn_Date<'" & Format(FrmDate, "MM/dd/yyyy") & "' and Trn_BrCd=" & BrnchCd
End If

If RsOpen(S) = True Then
   OpBalDr = OpBalDr + RetFieldNum(Rs.Fields(0))
   Rs.Close
End If


DoEvents
'*******************************************
        If BrnchCd = 1 Then
           S = "Select Sum(Trn_Amt) from Trn_Mst where  Trn_CCode='" & Trim(AccCode) & "' and Trn_Date<'" & Format(FrmDate, "MM/dd/yyyy") & "' and Trn_BrCd=1"
        ElseIf BrnchCd = -1 Then
          S = "Select Sum(Trn_Amt) from Trn_Mst where  Trn_CCode='" & Trim(AccCode) & "' and Trn_Date<'" & Format(FrmDate, "MM/dd/yyyy") & "'"
        Else
          S = "Select Sum(Trn_Amt) from Trn_Mst where  Trn_CCode='" & Trim(AccCode) & "' and Trn_Date<'" & Format(FrmDate, "MM/dd/yyyy") & "' and Trn_BrCd=" & BrnchCd
        End If
        
        If RsOpen(S) = True Then
           OpBalCr = OpBalCr + RetFieldNum(Rs.Fields(0))
           Rs.Close
        End If
'********************************************
       GetOpBal = True
End Function
Sub GetItem(code)

S = "Select Itm_Name ,Itm_rate,itm_tax,itm_slsdis from Itm_Mst where Itm_code=" & code
If RsOpen(S) = True Then

MsfDamage.TextMatrix(MsfDamage.Row, ClRate) = Rs!Itm_rate
MsfDamage.TextMatrix(MsfDamage.Row, ClDIs) = Rs!Itm_slsDis

If TxtType = "B" Then
MsfDamage.TextMatrix(MsfDamage.Row, ClTax) = Rs!Itm_Tax
Else
MsfDamage.TextMatrix(MsfDamage.Row, ClTax) = 0
End If

End If
End Sub

Sub GetItemTax(code)




S = "Select Itm_Name ,Itm_rate,itm_tax,itm_slsdis from Itm_Mst where Itm_code=" & code
If RsOpen(S) = True Then

If TxtType = "B" Then
MsfDamage.TextMatrix(MsfDamage.Row, ClTax) = Rs!Itm_Tax
Else
MsfDamage.TextMatrix(MsfDamage.Row, ClTax) = 0
End If

End If
End Sub

Function getRateOrg(Rate, TaxPer) As Double
getRateOrg = (Rate * 100) / (TaxPer + 100)
End Function


Sub CreateBill()
    Dim LeftMargin As Integer
    Dim WClient As Integer
    Dim WBillNo As Integer
    Dim WDate As Integer
    Dim WAmtInW As Integer
    Dim WGross As Integer
    Dim totitmdis As Double
Dim WSlno As Integer '0
Dim WItm As Integer  '2
Dim WItmCode As Integer
Dim WRtax As Integer '7
Dim WRate As Integer '3
Dim WQty As Integer  '4
Dim WGrs As Integer  '9
Dim WNv As Integer   '10
Dim Wtax As Integer  '8
Dim WUn As Integer
Dim WTaxAmt As Integer
WSlno = 2: WItm = 22: Wtax = 4: WTaxAmt = 4: WQty = 4: WUn = 2: WRate = 6
WGrs = 14
Dim WTab1 As Integer, WTab2 As Integer, WTab3 As Integer, WTab4 As Integer, WTab5 As Integer, WTab6 As Integer
WTab1 = 1: WTab2 = 15: WTab3 = 30: WTab4 = 45: WTab5 = 60: WTab6 = 75

Dim TotLine As Long, BalLine As Double
TotLine = 0: BalLine = 0
'Wtax = 10: WNv = 10: WTotal = 10
TotalAmount = 0: totitmdis = 0
Open "C:\Damage.txt" For Output As #1
    FlName = "C:\Damage.txt"
    Print #1, Chr(27) + Chr(18)
    Dim I As Long, j As Long
    Dim S As String, Check As String
    Check = "Yes"
    Print #1, Chr(27) + "j" + Chr(255)
    'Print #1, Chr(15)
    Dim SlNo As Integer, CurLine As Integer

     
     
    Print #1, "Tin No. "; Tab(11); CompanyTin; Tab(50); "Phone :"; Tab(62); CompanyPhone1
    Print #1, "Cst No. "; Tab(11); Trim(CompanyCst)
    Print #1, CenterAlign("THE KERALA VALUE ADDED TAX RULES 2005", 80)
    Print #1, CenterAlign("TAX INVOICE", 80)
    Print #1, CenterAlign("FORM 8B", 80)
    Print #1, CenterAlign("See rule 58[10]", 80)
    Print #1, ""
    
    'Print #1, Chr(18) + Chr(20) + Chr(14)
    
    Print #1, Chr(27) + Chr(87) + Chr(49) + CenterAlign(CompanyName, 80 / 2) + Chr(27) + Chr(87) + Chr(48)
    'Print #1, Chr(18) + Chr(20)
    Print #1, CenterAlign(CompanyAddress1, 80)
    Print #1, CenterAlign(CompanyAddress2, 80)
    Print #1, CenterAlign(CompanyAddress3, 80)

    Print #1, ""
    'Print #1, CondenseStart
    Print #1, Tab(WTab1); "Purch.Order"; Tab(WTab2); "Deli.Note"; Tab(WTab3); "Desp.Doc"; Tab(WTab4); "Terms"; Tab(WTab5 - 5); "Invoice No."; Tab(WTab6 - 4); Trim(ObjDmg.DtInvNo)
    Print #1, Tab(WTab1); "& Date"; Tab(WTab2); "& Date"; Tab(WTab3); Tab(WTab4); "Deli."; Tab(WTab5 - 5); "Invoice Date"; Tab(WTab6 - 4); MedBillDate
    Print #1, ""
    Print #1, Tab(1); "To"; Tab(5); Trim(TxtClientName)
    Print #1, Tab(5); Trim(TxtPlace)
    'Print #1, Tab(5); Trim(CustAdd2)
    
    Print #1, Replicate("-", 79)
    
    'Print #1, FrmStrng("No", Wslno) + "|" + FrmStrng("Particulars", WItm) + "|" + FrmStrng("Tax%", Wtax) + "|" + FrmStrng("Qty", Wqty) + "|" + FrmStrng("Un", WUn) + "|" + FrmStrng("Rate", Wrate) + "|" + FrmStrng("Grs Amt", Wrate) + "|" + FrmStrng("Net Amt", Wrate) + "|" + FrmStrng("Tax Amt", Wrate) + "|" + FrmStrng("Total", Wrate) + "|"
    
    Print #1, FrmStrng("No", WSlno) + "|" + FrmStrng("Particulars", WItm) + "|" + FrmStrng("Tax%", Wtax) + "|" + FrmStrng("Qty", WQty) + "|" + FrmStrng("Un", WUn) + "|" + FrmStrng("Rate", WRate) + "|" + FrmStrng("Gross Amt", WRate) + "|" + FrmStrng("Dis ", WRate) + "|" + FrmStrng("Net Amt", WRate) + "|" + FrmStrng("Tax Amt", WTaxAmt) + "|" + FrmStrng("Total", WRate) + "|"
    
    Print #1, Replicate("-", 78)
    SlNo = 1: CurLine = 1
    'Print #1, Tab(1); "|"; Tab(6); "|"; Tab(63); "|"; Tab(80); "|"
        For I = 1 To MsfDamage.Rows - 1
            If MsfDamage.TextMatrix(I, 1) = "" Then
                Exit For
            End If
            DoEvents
            S = S + FrmStrng(MsfDamage.TextMatrix(I, ClSlno), WSlno, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClName), WItm)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClTax), Wtax, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, Clqty), WQty, True)
            S = S + "|" + FrmStrng("  ", WUn, True)
            Dim A As Double
            
            A = getRateOrg(MsfDamage.TextMatrix(I, ClRate), MsfDamage.TextMatrix(I, ClTax))
            'MsgBox FormatNumber(a)
            S = S + "|" + FrmStrng(FormatNumber(A), WRate, True)
            'S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClRate), Wrate, True)
            S = S + "|" + FrmStrng(FormatNumber(MsfDamage.TextMatrix(I, Clqty) * A), WRate, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClDIsAmt), WRate, True)
            totitmdis = totitmdis + Val(MsfDamage.TextMatrix(I, ClDIsAmt))
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, Clgross), WRate, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClTaxAmt), WTaxAmt, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClNetTotal), WRate, True) + "|"
            Print #1, S
            S = ""
            DoEvents
            CurLine = CurLine + 1
            SlNo = SlNo + 1
            If Val(CurLine) > 25 Then
                Print #1, Replicate("-", 79)
                  For j = 1 To 15
                     Print #1, ""
                  Next j
                   Print #1, Replicate("-", 79)
                  CurLine = 1
            End If
         Next I
      TotLine = 16 + 21 + Val(CurLine)
      If Val(TotLine) < 60 Then
         BalLine = 60 - Val(TotLine)
      Else
         BalLine = Val(TotLine) - 60
      End If
       For I = 1 To BalLine
       'Print #1, FrmStrng("", Wslno) + "|" + FrmStrng("", WItm) + "|" + FrmStrng("", Wtax) + "|" + FrmStrng("", Wqty) + "|" + FrmStrng("", WUn) + "|" + FrmStrng("", Wrate) + "|" + FrmStrng("", Wrate) + "|" + FrmStrng("", Wrate) + "|" + FrmStrng("", Wrate) + "|" + FrmStrng("", Wrate)
       Print #1, FrmStrng("|", WSlno + 1, True) + FrmStrng("|", WItm + 1, True) + FrmStrng("|", Wtax + 1, True) + FrmStrng("|", WQty + 1, True) + FrmStrng("|", WUn + 1, True) + FrmStrng("|", WRate + 1, True) + FrmStrng("|", WRate + 1, True) + FrmStrng("|", WRate + 1, True) + FrmStrng("|", WRate + 1, True) + FrmStrng("|", WTaxAmt + 1, True) + FrmStrng("|", WRate + 1, True)
        'Print #1, Tab(1); "|"; Tab(6); "|"; Tab(65); "|"; Tab(80); "|"
      Next I
         
    
    Print #1, Replicate("-", 78)
    Print #1, Tab(1); "Gross Total "; Tab(70); FrmStrng(FmtNumber(Val(TxtGross)), WRate + 2, True)
    Print #1, Tab(1); "KVAT "; Tab(70); FrmStrng(FmtNumber(ObjDmg.DtTaxAmt), WRate + 2, True)
    Print #1, Tab(1); "Cess @1% "; Tab(70); FrmStrng(FmtNumber(ObjDmg.DtCess), WRate + 2, True)
    Print #1, Tab(1); "Discount  "; Tab(70); FrmStrng(FmtNumber(Val(TxtDiscount)), WRate + 2, True)
    Print #1, Tab(1); "Round Off  "; Tab(70); FrmStrng(FmtNumber(TxtRountOff), WRate + 2, True)
    Print #1, Tab(1); "GRAND TOTAL "; Tab(70); FrmStrng(FmtNumber(Val(TxtNetTotal)), WRate + 2, True)
    Print #1, Replicate("-", 78)
    Print #1, Tab(1); "Total Discount  "; Tab(40); FrmStrng(FmtNumber(Val(TxtDiscount) + totitmdis), WRate + 2, True)
    Print #1, Chr(15)
    Print #1, Tab(1); "Amount In Words :"; Tab(20); NumbWords(Val(TxtNetTotal))
    Print #1, Chr(18)
    Print #1, ""
    Print #1, Tab(1); "E&D.E"; CenterAlign("DECLARATION", 75)
    Print #1, Tab(1); " Certified that all the particulars shown in the tax invoice are true & correct"
    Print #1, Tab(1); "in all respects and the goods on which the tax charged and collected are int ac-"
    Print #1, Tab(1); "crdance with the provisions of KVAT Act 2003 and the rules made there under. It"
    Print #1, Tab(1); "is also certified that my/our registration under KVAT Act 2003 is not subject to"
    Print #1, Tab(1); "any suspension/cancellation and it is valid as on the date of this bill."
    Print #1, ""
    Print #1, Tab(40); "For  "; Tab(50); CompanyName
    
    Print #1, ""
    
    Print #1, Tab(1); "Customer's Signature"; Tab(50); "Authorised Signatury"

    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    'Print #1, CondenseEnd
    
Close #1
'FileCopy "Quot.txt", "Body WorkShop"
FrmViewReport.show
FrmViewReport.SetFocus
End Sub

Sub CreateDamage()
    Dim LeftMargin As Integer
    Dim WClient As Integer
    Dim WBillNo As Integer
    Dim WDate As Integer
    Dim WAmtInW As Integer
    Dim WGross As Integer
    Dim totitmdis As Double
Dim WSlno As Integer '0
Dim WItm As Integer  '2
Dim WItmCode As Integer
Dim WRtax As Integer '7
Dim WRate As Integer '3
Dim WQty As Integer  '4
Dim WGrs As Integer  '9
Dim WNv As Integer   '10
Dim Wtax As Integer  '8
Dim WUn As Integer
Dim WTaxAmt As Integer
Dim GrossAmt As Double
Dim NetAmt As Double
WSlno = 2: WItm = 23: Wtax = 4: WTaxAmt = 4: WQty = 4: WUn = 2: WRate = 8
WGrs = 14
Dim WTab1 As Integer, WTab2 As Integer, WTab3 As Integer, WTab4 As Integer, WTab5 As Integer, WTab6 As Integer
WTab1 = 1: WTab2 = 15: WTab3 = 30: WTab4 = 45: WTab5 = 60: WTab6 = 75

Dim TotLine As Long, BalLine As Double
TotLine = 0: BalLine = 0
'Wtax = 10: WNv = 10: WTotal = 10
TotalAmount = 0
Open "C:\esti.txt" For Output As #1
    FlName = "C:\esti.txt"
    Print #1, Chr(27) + Chr(18)
    Dim I As Long, j As Long
    Dim S As String, Check As String
    Check = "Yes"
    Print #1, Chr(27) + "j" + Chr(255)
    'Print #1, Chr(15)
    Dim SlNo As Integer, CurLine As Integer
    
    Print #1, CenterAlign("Damage", 58)

    Print #1, ""
    Print #1, FrmStrng("Damage No:", WSlno + WItm - 10) + FrmStrng(ObjDmg.DtInvNo, WQty) + FrmStrng("Date:", WRate + 10, True) + FrmStrng(ObjDmg.DtDate, WRate + WRate - 3, True)
    Print #1, ""
    
    Print #1, FrmStrng("No", WSlno) + "|" + FrmStrng("Particulars", WItm) + "|" + FrmStrng(" Qty", WQty) + "|" + FrmStrng("   Rate", WRate) + "|" + FrmStrng("Discount", WRate) + "|" + FrmStrng("   Total", WRate) + "|"
    Print #1, Replicate("-", 58)
    SlNo = 1: CurLine = 1
    'Print #1, Tab(1); "|"; Tab(6); "|"; Tab(63); "|"; Tab(80); "|"
        For I = 1 To MsfDamage.Rows - 1
            If MsfDamage.TextMatrix(I, 1) = "" Then
                Exit For
            End If
            DoEvents
            S = S + FrmStrng(MsfDamage.TextMatrix(I, ClSlno), WSlno, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClName), WItm)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, Clqty), WQty, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClRate), WRate, True)
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClDIsAmt), WRate, True)
            totitmdis = totitmdis + Val(MsfDamage.TextMatrix(I, ClDIsAmt))
            S = S + "|" + FrmStrng(MsfDamage.TextMatrix(I, ClNetTotal), WRate, True) + "|"
            GrossAmt = GrossAmt + Val(MsfDamage.TextMatrix(I, ClRate) * MsfDamage.TextMatrix(I, Clqty))
            Print #1, S
            S = ""
            DoEvents
            CurLine = CurLine + 1
            SlNo = SlNo + 1

         Next I
    Print #1, Replicate("-", 58)
    
    If Val(TxtDiscount) > 0 Then
    Print #1, Tab(1); "Gross Total    : "; Tab(40); FrmStrng(FmtNumber(Val(TxtGross)), WRate + WRate + 3, True)
    Print #1, Tab(1); "Discount       :"; Tab(40); FrmStrng(FmtNumber(Val(TxtDiscount)), WRate + WRate + 3, True)

    End If
    Print #1, Tab(1); "Net Total      :"; Tab(40); FrmStrng(FmtNumber(FmtNumber(Val(TxtNetTotal))), WRate + WRate + 3, True)
    Print #1, Tab(1); "Total Discount :"; Tab(20); FrmStrng(FmtNumber(Val(TxtDiscount) + Val(totitmdis)), WRate + WRate + 3, True)
    
   ' Print #1, ""
    Print #1, Chr(15)
    Print #1, Tab(1); "Amount In Words :"; Tab(20); NumbWords(Val(TxtNetTotal))
    Print #1, Chr(18)
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    Print #1, ""
    'Print #1, CondenseEnd
    
Close #1
'FileCopy "Quot.txt", "Body WorkShop"
FrmViewReport.show
FrmViewReport.SetFocus

End Sub


Private Sub TxtType_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
    If TxtType = "" Then
        TxtType = "E"
    End If
    'TxtType = UCase(TxtType)
    PrepareSerialNo
    MsfDamage.Col = 1
    MsfDamage.SetFocus
ElseIf KeyCode = 38 Then
    MedBillDate.SetFocus
End If
End Sub

Private Sub TxtType_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyE Or KeyAscii - 32 = vbKeyE Or KeyAscii = vbKeyB Or KeyAscii - 32 = vbKeyB Then
If KeyAscii > 97 Then KeyAscii = KeyAscii - 32
ActiveControl.Text = ""
ActiveControl.Text = UCase(ActiveControl.Text)
ElseIf KeyAscii = 13 Then

Else
KeyAscii = 0
End If
End Sub

Function GetNextDmgNo(Query As String) As Long
 Dim re As New ADODB.Recordset
 re.Open Query, Con, adOpenDynamic, adLockOptimistic
 If Not re.EOF Then
    GetNextDmgNo = Val(RetField(re.Fields(0))) + 1
 End If
    re.Close
End Function
