TYPE RECTA
 Left As Long
 Top As Long
 Right As Long
 Bottom As Long 
END TYPE 
CONST LVM_FIRST = &H1000
Const LVM_GETHEADER = (LVM_FIRST + 31)
Const HDM_FIRST = &H1200
'dim r as rectA
dim r as QRect
dim IDcolumn as integer
dim ccount as integer
const bOK = 0 		
const bOKCan = 1
const bAbRetIg = 2
const bYesNoCan = 3
const bYesNo = 4
const bRetCan = 5
const iCritMsg = 16
const iWarnQuery = 32
const iWarnMsg = 48
const iInfMsg = 64
const dFst = 0		
const dSec = 256
const dTrd = 512
const dFth = 768


'$TYPECHECK ON
$INCLUDE <rapidq2.inc>
'$INCLUDE "APIs.inc
$INCLUDE "mysql.inc

'------- SUB & FORM DECLARATIONS -----------

Declare Function SendMessagea Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As LONG) As Long
'Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECTA)AS Long
'Declare Function GetParent Lib "user32" Alias "GetParent" (ByVal hwnd As Long) As Long
Declare Function MsgDlg (MsgDlgText As String, MsgDlgTitle As String, MsgDlgButtons As Long) As Long
Declare Function MDlg Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

DECLARE SUB OK(COLUMN%,s as qlistview)
DECLARE SUB BTNK
declare sub principio
declare sub registrosiguiente
declare sub registroanterior
declare sub ultimoregistro
declare sub busqueda
DECLARE SUB Finalizar 
DECLARE SUB FillList
DECLARE SUB LoadEditFields
declare sub validardatos 
DECLARE SUB AgregaRegistro
DECLARE SUB ModificaRegistro
DECLARE SUB BorraRegistro
Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (hWnd As Long) As Long 
Declare sub Limpiadatos

declare sub cargarlistview
Dim Columna as long
dim k as  integer
dim fuente as qfont
fuente.color=qbcolor(15)
DIM MySQL AS QMYSQL
DIM Host$ as STRING
DIM User$ as STRING
DIM Password$ as STRING
dim Fecha$ as string
dim rec as integer
dim bandera as integer
Function MsgDlg (MsgDlgText As String, MsgDlgTitle As String, MsgDlgButtons As Long) As Long
   Result = MDlg (Form.Handle,MsgDlgText,MsgDlgTitle,MsgDlgButtons)
End Function


CREATE Form AS QFORM
    Caption = "List of Taxes"
    Width = 788
    Height = 478
    Center
    CREATE Image1 AS QIMAGE
        BMP = "Degradee.bmp"
        Width = 750
        Height = 450
        Autosize=1
    END CREATE
    
   CREATE Label1 AS QLABEL
        Caption = "Tax"
        Font=fuente
        Transparent=1
        Left = 12
        Top = 245
        Width = 88
    END CREATE
    CREATE Label2 AS QLABEL
        Caption = "Quota"                                                                                                   
        Font=fuente
        Left = 12
        Transparent=1
        Top = 275
        Width = 72
    END CREATE
    CREATE Label3 AS QLABEL
        Caption = "Expiration Date"                                                                                                                  
        Font=fuente
        Transparent=1
        Left = 12
        Top = 305
        Width = 88
    END CREATE
    CREATE Label4 AS QLABEL
        Caption = "Amount"                                                                                                    
        Font=fuente
        Left = 12
        Transparent=1
        Top = 335
        Width = 88
    END CREATE
    CREATE Label5 AS QLABEL
        Caption = "Payment Date"                                                                                                        
        Font=fuente
        Left = 12
        Transparent=1
        Top = 365
        Width = 78
    END CREATE
    CREATE Label6 AS QLABEL
        Caption = "Payment Place"                                                                                                        
        Font=fuente
        Left = 12
        Transparent=1
        Top = 395
        Width = 78
    END CREATE
    CREATE Edit1 AS QEdit
        Color=14344934
        Text = ""
        Left = 116
        Top = 245
        Width = 257
        Charcase=1
        MaxLength = 40
        Taborder=1
    END CREATE
    CREATE Edit2 AS QEDIT
        Color=14344934
        Text = ""
        Left = 116
        Top = 275
        Charcase=1
        Width = 257
        TabOrder = 2
    END CREATE
    CREATE Edit3 AS QEDIT
        Color=14344934
        Text = ""
        Left = 116
        Top = 305
        Width = 257
        Charcase=1
        InputMask="##/##/####"
        TabOrder = 3
        'Onkeyup = tecla
    END CREATE
    CREATE Edit4 AS QEDIT
        Color=14344934
        Left = 116
        Top = 335
        Width = 257
        Charcase=1
        TabOrder = 4
    END CREATE
    CREATE Edit5 AS QEDIT
        Color=14344934
        Left = 116
        Top = 365
        Width = 257
        InputMask="##/##/####"
        Charcase=1
        TabOrder = 5
    END CREATE
    CREATE Edit6 AS QEDIT
        Color=14344934
        Text = ""
        Left = 116
        Top = 395
        width = 257
        MaxLength = 40
        Charcase=1
        Taborder=6
    END CREATE
    CREATE BtnOK AS QBUTTON
        Caption = "&Exit"
'        BMP="salida.bmp"
        Left = 650
        Top = 374
        Height = 50
        Width = 110
        OnClick = Finalizar
    END CREATE
    CREATE BtnAdd AS QButton
            Caption = "ADD"
            Bmp="ingresar.bmp"
            Left = 470
            Top = 310
            Width = 110
            Height = 50
            OnClick = AgregaRegistro
        END CREATE
        CREATE BtnChange AS QButton
            Caption = "MODIFY"
            'Bmp="\rapidq\bmp\reintentar1.bmp"
            Left = 650
            Top = 310
            Width = 110
            Height = 50
            OnClick = ModificaRegistro
        END CREATE
        CREATE BtnDelete AS QBUTTON
            Caption = "ERASE"
            'Bmp="\rapidq\bmp\BORRAR1.bmp"
            Left = 650
            Top = 245
            Width = 110
            Height = 50
            OnClick = BorraRegistro
        END CREATE
        CREATE primero AS QBUTTON
            Caption = ""
            Bmp="primero.bmp"
            Left = 470
            Top = 245
            Showhint=1
            Hint="First Registry"
            Width = 30
            Height = 50
            OnClick = Principio
        END CREATE
        CREATE anterior AS QBUTTON
            Caption = ""
            Bmp="anterior.bmp"
            Left = 510
            Top = 245
            Showhint=1
            Hint="Previous Registry"
            Width = 30
            Height = 50
            OnClick = RegistroAnterior
        END CREATE
        CREATE posterior AS QBUTTON
            Caption = ""
            Bmp="posterior.bmp"
            Left = 550
            Top = 245
            Showhint=1
            Hint="Next Registry"
            Width = 30
            Height = 50
            OnClick = RegistroSiguiente
        END CREATE
        CREATE ultimo AS QBUTTON
            Caption = ""
            Bmp="ultimo.bmp"
            Left = 590
            Showhint=1
            Hint="Last Registry"
            Top = 245
            Width = 30
            Height = 50
            OnClick = UltimoRegistro
        END CREATE
        CREATE Imprimir AS QButton
            Caption = "Print"
'            Bmp="impresora.bmp"
            Left = 470
            Top = 374
            Width = 110
            Height = 50
        END CREATE

CREATE ListView AS QListView
        Top=16
        Left=10
        HideSelection=0
        Width=750
        Height=200
        ViewStyle=vsReport
        showcolumnheaders=1
        Color=14344934
        AddColumns "Id_Tax","Tax","Quota","Expiration_Date","Amount","Payment_Date","Payment_Place"
        Column(0).Width=50
        Column(1).Width=200
        Column(2).Width=65
        Column(3).Width=100
        Column(4).Width=70
        Column(5).Width=100
        Column(6).Width=180
        Multiselect=True
        Gridlines=True
        Rowselect=True
        OnClick=LoadEditFields
        ONCOLUMNCLICK=OK
    END CREATE    
END CREATE

'--------- INITIALISATION ---------------

Host$=""
User$=""
Password$=""
MySQL.Connect(Host$,User$,Password$)

IF MySQL.Connected THEN
    cargarListview
ELSE
Showmessage "asdfs"
    Form.Caption="No Conection to a Server"
    END
End IF
Form.ShowModal


SUB LoadEditFields(A$)
    dim Puntero$ as string
    MySQL.SelectDB("Taxes")
    DIM x AS INTEGER
    DIM a$ AS STRING
    Puntero$ = ListView.Item(Listview.ItemIndex).Caption
    if MySQL.Query("SELECT * from datos WHERE Id_Tax = '" + Puntero$ + "'") = 0 THEN
       ShowMessage("Don't call the Registry")
       EXIT SUB
    ELSE
       WHILE MySQL.FetchRow
       MySQL.FieldSeek(0)
       validardatos
       SetFocus(edit1.handle)
    WEND
    END IF
END SUB 

SUB AgregaRegistro
'   MySQL.SelectDB("Taxes")
   bandera=0
   if bandera =1 then 
       limpiadatos
       setfocus(Edit1.handle)
       exit sub
   end if
   if Edit1.text="" then
       Showmessage "Don't Add with blanck"
       setfocus(edit1.handle)
       exit sub
   end if    
   if MySQL.Query ("Insert into datos(Tax,Quota,Expiration_Date,Amount,Payment_Date,Payment_Place) values ('" + Edit1.Text + "','" + Edit2.Text + "','" + Edit3.Text + "','" + Edit4.Text + "','" + Edit5.Text + "','" + Edit6.Text + "')") =0 then
      ShowMessage("Failed to write register")
   ELSE
      LimpiaDatos
      cargarListview
   END IF

END SUB

SUB ModificaRegistro
   if MySQL.Query ("Update datos SET Tax= '" + Edit1.Text + "',_
                   Quota='" + Edit2.Text + "',_
                   Expiration_Date='" + Edit3.Text + "',_
                   Amount='" + Edit4.Text + "',_
                   Payment_Date='" + Edit5.Text + "',_
                   Payment_Place='" + Edit6.Text + "' where Id_Tax='" + Listview.Item(Listview.ItemIndex).Caption+ "'") =0 then
                   ShowMessage("Error al realizar la modificación")
   ELSE
                   CargarListview
   END IF
END SUB

SUB BorraRegistro
   opcion=0
   opcion=MsgDlg ("Sure to erase this "+ Edit1.text,"Eliminated a Register",1+16+0)
   select case opcion
   case 1   
           segundaopcion=MsgDlg ("Sure ? "+ Edit1.text,"Segunda Opcion Para Eliminar un Proveedor",1+16+0)
           select case segundaopcion
           case 1
           if MySQL.Query("DELETE from datos WHERE Tax = '" + Edit1.text + "'") = 0 THEN
              ShowMessage("I don't erase")
           End If
           case 2   
           Limpiadatos
           cargarlistview
           SetFocus(Edit1.handle)   
   End Select
   case 2   
           CargarListview
           Limpiadatos
           SetFocus(Edit1.handle)   
   End Select   
End SUB   

sub Limpiadatos
       Edit1.Text=""
       Edit2.Text=""
       Edit3.Text=""
       Edit4.Text=""
       Edit5.Text=""
       Edit6.Text=""
end sub

sub validardatos       
       Edit1.Text=MySQL.Row(1)
       Edit2.Text=MySQL.Row(2)
       Edit3.Text=MySQL.Row(3)
       Edit4.Text=MySQL.Row(4)
       Edit5.Text=MySQL.Row(5)
       Edit6.Text=MySQL.Row(6)
      
end sub                   

SUB Finalizar(sender as Qbutton)
  
Resultado=MsgDlg ("Cancel this Job ?","Your Option",1+16+0)
       select case resultado     
       case=1
           MySQL.Close
           End
                  case=2 
           setfocus(Edit1.handle)
       end select        
END SUB

sub principio
    rec=1
    if MySQL.Query("SELECT * from datos WHERE id_Tax="+str$(rec))=0 then
       ShowMessage("I don't call the registry")
       EXIT SUB
    ELSE
           WHILE MySQL.FetchRow
       MySQL.FieldSeek(0)
       validardatos
       SetFocus(edi2.handle)
       wend
    end if   
end sub  

sub ultimoregistro
            MySQL.Query("Select Count(*) from datos") 
            MySQL.FetchRow
            MySQL.FieldSeek(0)
            rec = val(MySQL.Row(0))
    if MySQL.Query("SELECT * from datos WHERE id_Tax="+str$(rec))=0 then
       ShowMessage("I don't call the registry")
       EXIT SUB
    ELSE
       WHILE MySQL.FetchRow
           MySQL.FieldSeek(0)
           validardatos
           SetFocus(edi2.handle)
       wend
    end if   
end sub           

SUB registrosiguiente
    dim ultimo as integer     
    MySQL.Query("Select Count(*) from datos") 
    MySQL.FetchRow
    MySQL.FieldSeek(0)
    ultimo = val(MySQL.Row(0))
    rec=rec+1
    if rec>ultimo then rec=ultimo
    if MySQL.Query("SELECT * from datos WHERE id_Tax="+str$(rec))=0 then
       ShowMessage("I don't call the registry")
       EXIT SUB
    ELSE
       WHILE MySQL.FetchRow
           MySQL.FieldSeek(0)
           validardatos
           SetFocus(edi2.handle)
       wend
    end if   
        
end sub           

SUB registroanterior
    rec=rec-1
        if rec<1 then rec=1
    if MySQL.Query("SELECT * from datos WHERE id_Tax="+str$(rec))=0 then
       ShowMessage("I don't call the registry")
       EXIT SUB
    ELSE
       WHILE MySQL.FetchRow
           MySQL.FieldSeek(0)
           validardatos
           SetFocus(edi2.handle)
       wend
    end if   
end sub           

SUB OK(COLUMN%,s as qlistview)
 defint mx,my,i,cw,lvh,cc,hh,parw
 mx=screen.mousex
 my=screen.mousey
 lvh=ListView.Handle
 hh=sendMESSAGEa(lvh, (LVM_GETHEADER),0,0)'get handler of header
 ccount=sendMESSAGEa(hh, (HDM_FIRST + 0),0,0 )'get number of columns
 parw=getparent(lvh) 
 getWINDOWrect(parw,R)
 cc=0:cw=0
  for i=0 to ccount-1
 cw=s.column(i).width
 cc=cw+cc
 if s.left+cc+R.left>mx then exit for
 next
 idcolumn=i+
 parametro$=s.column(idcolumn).caption
 MySQL.SelectDB("Taxes")
 MySQL.Query("SELECT * FROM datos ORDER BY "+ Parametro$ +";") 
 j=0
 i=0
 Listview.clear
   WHILE MySQL.FetchRow                 
      MySQL.FieldSeek(0)                
      ListView.Additems MySQL.Row(0)    
      j=1
      DO
         ListView.Addsubitem i, MySQL.Row(j)  
         j++
      LOOP until j>MySQL.ColCount-1
      i++
   WEND  
END SUB

sub cargarlistview
   MySQL.SelectDB("Taxes")
   MySQL.Query("SELECT * from Datos order by Expiration_Date")
   dim j as integer
   dim i as integer
   j=0
   i=0
   Listview.clear
   WHILE MySQL.FetchRow                 
      MySQL.FieldSeek(0)                
      Listview.Additems MySQL.Row(0)    
      j=1
      DO
         ListView.Addsubitem i,MySQL.Row(j)  
         j++
      LOOP until j>MySQL.ColCount-1
      i++
   WEND  
end sub
