MySQL ABM – Código Liberado VI

2015-05-10_183529
AbmMig.prg

/*
 *
 * MINIGUI - Harbour Win32 GUI library
 * Copyright 2002-2009 Roberto Lopez <[email protected]>
 * http://harbourminigui.googlepages.com/
 *
 * MySQL ABM v15.0518
 * Miguel Angel Juárez A. - 2009-2015 MigSoft <migsoft/at/oohg.org>
 *
 */

#include 'oohg.ch'
#include "tdolphin.ch"
#include "miniprint.ch"

PROCEDURE MigABM( oSrv_Mig, cTabla )

    Public Nuevo := .F., aName := {}, aCtrol := {}, nOpt := 1 , nReg1, aTxtBx := {}
    Public oQry_Mig := Nil, cTable := cTabla

    If !Empty( oSrv_Mig ) .AND. !Empty(cTabla)

        oQry_Mig := oSrv_Mig:Query( "select * from " + cTabla )

        nAlto := CalcHeight(oQry_Mig) ; nReg1 := oQry_Mig:RecNo()

        DEFINE WINDOW Win_2                                ;
            AT 0,0                                         ;
            WIDTH 685                                      ;
            HEIGHT 575                                     ;
            TITLE 'MySQL Table: '+cTable+' - Record View #'+Alltrim(Str(oQry_Mig:RecNo())) ;
            ICON "MAIN1"                                   ;
            MODAL                                          ;
            FONT 'ARIAL' SIZE 9                            ;
            ON INIT ( DesactivarEdicion(oQry_Mig), Actualizar2(oQry_Mig) )  ;
            ON RELEASE CerrarTablas()

            AbrirTablas()

            Define Splitbox

                MyToolBar( oSrv_Mig, oQry_Mig )

            End Splitbox

               @ 32,8 FRAME FRAME_51 OBJ oFrame WIDTH 657 HEIGHT 460
                      oFrame:Anchor := "TOPLEFTBOTTOMRIGHT"

                Define Window Win_1 OBJ oWin1                ;
                    at 40,10                                 ;
                    width 650                                ;
                    height 450                               ;
                    virtual height MAX(nAlto-70,451)         ;
                    Internal                                 ;
                    nocaption                                ;
                    font "ms sans serif" size 9              ;
                    focused

                    oWin1:Anchor := "TOPLEFTBOTTOMRIGHT"

                    CreaControles( oSrv_Mig, oQry_Mig )

                End Window


            @ 500,450 BUTTON ACEPTAR  OBJ oButt1 CAPTION 'O&K' ACTION AceptarEdicion(oQry_Mig)
                      oButt1:Anchor := "BOTTOMRIGHT"
            @ 500,550 BUTTON CANCELAR OBJ oButt2 CAPTION 'Ca&ncel' ACTION CancelarEdicion(oQry_Mig)
                      oButt2:Anchor := "BOTTOMRIGHT"

        END WINDOW

        CENTER WINDOW Win_2
        ACTIVATE WINDOW Win_2

    Endif

RETURN

*------------------------------------------------------------*
Function CalcHeight(oQry_Mig)
*------------------------------------------------------------*
    nAlto1 := 140

    For n := 1 to oQry_Mig:Fcount()
        If oQry_Mig:FieldType(n)=='M'
            nAlto1 := nAlto1 + 58
        Endif
    Next

    nAlto1 := nAlto1 + ( oQry_Mig:FCount() * 30 )

Return(nAlto1)

*------------------------------------------------------------*
PROCEDURE CreaControles( oSrv_Mig, oQry_Mig )
*------------------------------------------------------------*
    nFil := 10 ; nCol :=180 ; nMin := 80

    @ nFil,10 FRAME FRAME_1 OBJ oFrame1 WIDTH 620 HEIGHT MAX(nAlto - 90, 138)
      oFrame1:Anchor := "TOPLEFTBOTTOMRIGHT"

    aLabel := {}  ; aCtrol := {} ; aName := {} ; aTxtBx := {}

    For n := 1 to oQry_Mig:FCount()

        aadd( aLabel , "label_" + alltrim( str(n) ) )
        aadd( aCtrol , "Control_" + alltrim( str(n) ) )
        aadd( aName  , oQry_Mig:FieldName(n) )

        nLongTot := oQry_Mig:FieldLen(n)
        nLongDec := oQry_Mig:FieldDec(n)

        // AutoMsgInfo( oQry_Mig:FieldGet(n), oQry_Mig:FieldType(n) )

        @ nFil+(n*30),20 LABEL &(aLabel[n]) OBJ &(aLabel[n]) VALUE TokenUpper( Lower(aName[n]) )+" ("+alltrim(str(nLongTot,6,0))+")" WIDTH 120 //80
          &(aLabel[n]):Anchor := "TOPLEFT"

        If oQry_Mig:FieldType(n)=='L'
            @ nFil+(n*30) ,nCol CHECKBOX   &(aCtrol[n]) caption "Yes" value oQry_Mig:FieldGet(n)
          Aadd( aTxtBx, .F. )
        Elseif oQry_Mig:FieldType(n)=='D'
            @ nFil+(n*30) ,nCol DATEPICKER &(aCtrol[n]) value oQry_Mig:FieldGet(n) UpDown SHOWNONE
          Aadd( aTxtBx, .F. )
        Elseif oQry_Mig:FieldType(n)=='N'
            cInpMsk  := iif( nLongDec > 0, REPLICATE( "9", nLongTot - nLongDec -1 ) + "." + REPLICATE( "9", nLongDec ), REPLICATE( "9", nLongTot - nLongDec ) )
            nWidthF  := Min( iif(len(hb_Cstr(oQry_Mig:FieldGet(n)))*10<nMin,nMin,len(hb_Cstr(oQry_Mig:FieldGet(n)))*10),400 )
            @ nFil+(n*30) ,nCol TEXTBOX &(aCtrol[n]) WIDTH nWidthF VALUE oQry_Mig:FieldGet(n) NUMERIC INPUTMASK cInpMsk RIGHTALIGN
          Aadd( aTxtBx, .T. )
        Elseif oQry_Mig:FieldType(n)=='C'
            If oQry_Mig:FieldGet(n) == NIL
               @ nFil+(n*30) ,nCol TEXTBOX    &(aCtrol[n]) value "" width nMin
            Else
               @ nFil+(n*30) ,nCol TEXTBOX    &(aCtrol[n]) value oQry_Mig:FieldGet(n) width Min( iif(len(oQry_Mig:FieldGet(n))*10<nMin,nMin,len(oQry_Mig:FieldGet(n))*10),300 )
            Endif
          Aadd( aTxtBx, .T. )
        Elseif oQry_Mig:FieldType(n)=='M'
            @ nFil+(n*30) ,nCol EDITBOX    &(aCtrol[n]) value oQry_Mig:FieldGet(n) width 300 height 81
            nFil := nFil + 58
          Aadd( aTxtBx, .F. )
        Elseif oQry_Mig:FieldType(n)=='U'
            @ nFil+(n*30) ,nCol TEXTBOX    &(aCtrol[n]) value oQry_Mig:FieldGet(n) width Min( iif(len(oQry_Mig:FieldGet(n))*10<nMin,nMin,len(oQry_Mig:FieldGet(n))*10),300 )
          Aadd( aTxtBx, .T. )
        Else
            @ nFil+(n*30) ,nCol TEXTBOX    &(aCtrol[n]) value oQry_Mig:FieldGet(n) width Min( iif(len(oQry_Mig:FieldGet(n))*10<nMin,nMin,len(oQry_Mig:FieldGet(n))*10),300 )
          Aadd( aTxtBx, .T. )
        Endif

    Next

Return

*------------------------------------------------------------*
PROCEDURE MyToolbar( oSrv_Mig, oQry_Mig )
*------------------------------------------------------------*

    DEFINE TOOLBAR ToolBar_1 BUTTONSIZE 16,16 FONT "Arial" SIZE 9 FLAT

    BUTTON PRIMERO TOOLTIP '&First' PICTURE 'first1'	;
        ACTION ( oQry_Mig:GoTop() ,Actualizar2(oQry_Mig) )

    BUTTON ANTERIOR TOOLTIP '&Previous' PICTURE 'previous1'	;
        ACTION ( oQry_Mig:Skip( -1 ), Actualizar2(oQry_Mig) )

    BUTTON SIGUIENTE TOOLTIP '&Next' PICTURE 'next1'	;
        ACTION ( oQry_Mig:Skip( 1 ) , if ( oQry_Mig:Eof() , oQry_Mig:GoBottom() , Nil ) , Actualizar2(oQry_Mig) )

    BUTTON ULTIMO TOOLTIP '&Last' PICTURE 'last1'	;
        ACTION ( oQry_Mig:GoBottom(),Actualizar2(oQry_Mig)  )  SEPARATOR

    BUTTON BUSCAR TOOLTIP '&Find' PICTURE 'find1'	;
        ACTION Buscando( oSrv_Mig, oQry_Mig )

    BUTTON Nuevo TOOLTIP '&New' PICTURE 'new1'	;
        ACTION ( Nuevo := .T. , Nuevo(oQry_Mig) )

    BUTTON EDITAR TOOLTIP '&Edit' PICTURE 'format1'	;
        ACTION If ( BloquearRegistro(oQry_Mig) , ActivarEdicion(oQry_Mig) , Nil )

    BUTTON ELIMINAR TOOLTIP 'E&rase' PICTURE 'erase1'	;
        ACTION Eliminar(oQry_Mig)

    BUTTON IMPRIMIR TOOLTIP '&Print'PICTURE 'print1' ;
        ACTION Imprimir(oQry_Mig, .F.)

    BUTTON CERRAR TOOLTIP '&Close' PICTURE 'record1' ;
        ACTION Win_2.release

    END TOOLBAR

RETURN

*------------------------------------------------------------*
PROCEDURE Buscando( oSrv_Mig, oQry_Mig )
*------------------------------------------------------------*
    If ! oSrv_Mig:lError

        nAlto := CalcHeight(oQry_Mig)

        DEFINE WINDOW Win_Busca2                           ;
            AT 0,0                                         ;
            WIDTH 685                                      ;
            HEIGHT 550                                     ;
            TITLE 'MySQL - Record Find'                    ;
            ICON "MAIN1"                                   ;
            MODAL                                          ;
            FONT 'ARIAL' SIZE 9

                Define Window Win_Busca1                    ;
                    width 650                                ;
                    height 450                               ;
                    virtual height MAX(nAlto-70,451)         ;
                    INTERNAL                               ;
                    nocaption                                ;
                    font "ms sans serif" size 9              ;
                    focused

                    CreaControles( oSrv_Mig, oQry_Mig )
                    Nuevo2(oQry_Mig)

                End Window

            @ 470,420 BUTTON ACEPTAR  CAPTION 'O&K' ACTION AceptaBuscar(oQry_Mig)
            @ 470,530 BUTTON CANCELAR CAPTION 'Ca&ncel' ACTION CancelaBuscar(oQry_Mig)

        END WINDOW

        CENTER WINDOW Win_Busca2
        ACTIVATE WINDOW Win_Busca2

    Endif

Return
*------------------------------------------------------------*
PROCEDURE AbrirTablas
*------------------------------------------------------------*

RETURN
*------------------------------------------------------------*
PROCEDURE CerrarTablas
*------------------------------------------------------------*

    win_1.release

RETURN
*------------------------------------------------------------*
PROCEDURE DesactivarEdicion(oQry_Mig)
*------------------------------------------------------------*

    For n := 1 to oQry_Mig:FCount()
       If aTxtBx[n] == .T.
          Win_1.&(aCtrol[n]).ReadOnly := .T.
       Else
          Win_1.&(aCtrol[n]).Enabled := .F.
       Endif
    Next

    Win_2.Aceptar.Enabled		:= .F.
    Win_2.Cancelar.Enabled		:= .F.
    Win_2.ToolBar_1.Enabled		:= .T.

RETURN
*------------------------------------------------------------*
PROCEDURE ActivarEdicion(oQry_Mig)
*------------------------------------------------------------*

    For n := 1 to oQry_Mig:FCount()
       If aTxtBx[n] == .T.
          Win_1.&(aCtrol[n]).ReadOnly := .F.
       Else
          Win_1.&(aCtrol[n]).Enabled := .T.
       Endif
    Next

    Win_2.Aceptar.Enabled		:= .T.
    Win_2.Cancelar.Enabled		:= .T.
    Win_2.ToolBar_1.Enabled		:= .F.

    Win_1.&(aCtrol[1]).SetFocus

RETURN
*------------------------------------------------------------*
PROCEDURE CancelarEdicion(oQry_Mig)
*------------------------------------------------------------*

    DesactivarEdicion(oQry_Mig)
    Actualizar2(oQry_Mig)
    Nuevo := .F.

RETURN
*------------------------------------------------------------*
PROCEDURE AceptarEdicion( oQry_Mig )
*------------------------------------------------------------*

    DesactivarEdicion(oQry_Mig)

    If Nuevo == .T.
        oQry_Mig:GetBlankRow(.F.)
        Nuevo := .F.
    EndIf

    For n := 1 to oQry_Mig:FCount()
        oQry_Mig:FieldPut( n , Win_1.&(aCtrol[n]).Value )
    Next

    oQry_Mig:Save()
    oQry_Mig:Refresh()
    Actualizar2( oQry_Mig )

RETURN

*------------------------------------------------------------*
PROCEDURE CancelaBuscar()
*------------------------------------------------------------*

    Win_Busca2.Release

RETURN

*------------------------------------------------------------*
PROCEDURE AceptaBuscar(oQry_Mig)
*------------------------------------------------------------*

    Local aBusca := {} , aNomb := {}

    For n := 1 to oQry_Mig:FCount()
        If !Empty(Win_Busca1.&(aCtrol[n]).Value )
           Aadd( aBusca, AllTrim(Win_Busca1.&(aCtrol[n]).Value) )
           Aadd( aNomb, oQry_Mig:FieldName(n) )
        Endif
    Next

    nRec := oQry_Mig:Find( aBusca, aNomb, , , .T. )

    IF nRec > 0
       oQry_Mig:GoTo( nRec )
       Actualizar2(oQry_Mig)
    Else
       MsgExclamation( 'No records for your query!', "MySQL Table: "+cTable+"- Viewer" )
    EndIf

    Win_Busca2.Release

Return
*------------------------------------------------------------*
PROCEDURE Nuevo(oQry_Mig)
*------------------------------------------------------------*

    For n := 1 to oQry_Mig:FCount()
        aadd( aCtrol , "Control_" + alltrim( str(n) ) )
        aadd( aName  , oQry_Mig:FieldName(n) )

        If oQry_Mig:FieldType(n)=='L'
            Win_1.&(aCtrol[n]).value := .F.
        Elseif oQry_Mig:FieldType(n)=='D'
            Win_1.&(aCtrol[n]).value := ctod("  /  /  ")
        Elseif oQry_Mig:FieldType(n)=='N'
            Win_1.&(aCtrol[n]).value := 0
        Elseif oQry_Mig:FieldType(n)=='C'
            Win_1.&(aCtrol[n]).value := ''
        Elseif oQry_Mig:FieldType(n)=='M'
            Win_1.&(aCtrol[n]).value := ''
        Endif
    Next

    ActivarEdicion(oQry_Mig)

RETURN

*------------------------------------------------------------*
PROCEDURE Nuevo2(oQry_Mig)
*------------------------------------------------------------*

    For n := 1 to oQry_Mig:FCount()
        aadd( aCtrol , "Control_" + alltrim( str(n) ) )
        aadd( aName  , oQry_Mig:FieldName(n) )

        If oQry_Mig:FieldType(n)=='L'
            Win_Busca1.&(aCtrol[n]).value := .F.
        Elseif oQry_Mig:FieldType(n)=='D'
            Win_Busca1.&(aCtrol[n]).value := ctod("  /  /  ")
        Elseif oQry_Mig:FieldType(n)=='N'
            Win_Busca1.&(aCtrol[n]).value := 0
        Elseif oQry_Mig:FieldType(n)=='C'
            Win_Busca1.&(aCtrol[n]).value := ''
        Elseif oQry_Mig:FieldType(n)=='M'
            Win_Busca1.&(aCtrol[n]).value := ''
        Endif
    Next

RETURN

*------------------------------------------------------------*
PROCEDURE Actualizar2( oQry_Mig )
*------------------------------------------------------------*

    Win_2.TITLE := 'MySQL Table: '+cTable+' - Record View #'+Alltrim(Str(oQry_Mig:RecNo()))

    For n := 1 to oQry_Mig:FCount()
        Win_1.&(aCtrol[n]).Value := oQry_Mig:FieldGet(n)
    Next


Return
*------------------------------------------------------------*
Function BloquearRegistro()
*------------------------------------------------------------*
    Local RetVal := .T.

Return RetVal
*------------------------------------------------------------*
Procedure Eliminar(oQry_Mig)
*------------------------------------------------------------*

    If MsgYesNo ( 'Are you sure?', 'Delete' )

        oQry_Mig:Delete()
        oQry_Mig:Refresh()
        Actualizar2(oQry_Mig)

    EndIf

Return

Procedure Imprimir( oQry_Mig, lCuadro )

   lCuadro := Iif(Empty(lCuadro),.F.,lCuadro)

   cTituloImp    := "MySQLRep"
   nFieldsLimit  := 6

   SELECT PRINTER DEFAULT TO lSuccess PREVIEW

   IF lSuccess

      START PRINTDOC
      START PRINTPAGE

      oQry_Mig:GoTop()

      PAG := 0
      LIN := 0

      DO WHILE .NOT. oQry_Mig:Eof()

         IF LIN>=260 .OR. PAG=0
            cMsgPie := "Continúa en la Hoja: "
            IF PAG <> 0  // Pie de página
               @ LIN+5,220/2-(Len(cMsgPie)/2) PRINT cMsgPie + Ltrim(Str( PAG + 1 ))
               END PRINTPAGE
               START PRINTPAGE
            ENDIF

            PAG++

            // Encabezado
            cHead1 := "MySQL Reporte"

            //@ 5,5    PRINT "0"
            //@ 5,210  PRINT "|"

            @ 20,20  PRINT cHead1
            @ 20,190 PRINT "Hoja: "+LTRIM(STR(PAG)) RIGHT

            cHead2 := "Tabla: "+cTable    // Nombre de la tabla
            nCol2  := 220/2  - ( Len( "25/05/2015"+cHead2+"17:30:00" ) / 2 )

            @ 25,20    PRINT DATE()
            @ 25,nCol2 PRINT cHead2 SIZE 12 BOLD
            @ 25,190   PRINT Hb_Cstr(TIME()) RIGHT

            nCol3 := 200/2 - Len(cTituloImp)/2
            @ 35,nCol3 PRINT cTituloImp FONT "Verdana" SIZE 14 BOLD
            @ 40,20  PRINT 'desde: '
            @ 45,20  PRINT 'hasta: '

            LIN:=55
            nLimit := iif( oQry_Mig:FCount() > nFieldsLimit, nFieldsLimit, oQry_Mig:FCount() )

            IF lCuadro = .T.
               @ LIN, 19 PRINT RECTANGLE TO LIN+5, 39
               @ LIN, 39 PRINT RECTANGLE TO LIN+5,109

               @ LIN,109 PRINT RECTANGLE TO LIN+5,141
            ELSE
               @ LIN+4,20 PRINT LINE TO LIN+4, nLimit * 32 PENWIDTH .01
            ENDIF

            COL := 20
            For n := 1 to nLimit   // Nombre de Campos
                @ LIN,COL PRINT oQry_Mig:FieldName( n ) BOLD CENTER
                COL := COL + Iif( MIN(oQry_Mig:FieldLen(n),50)< 30, 30, MIN(oQry_Mig:FieldLen(n),50) )
            Next

            LIN := LIN + 5

         ENDIF

         IF lCuadro = .T.
            @ LIN, 19 PRINT RECTANGLE TO LIN+5, 39
            @ LIN, 39 PRINT RECTANGLE TO LIN+5,109
            @ LIN,109 PRINT RECTANGLE TO LIN+5,141
         ENDIF

         COL := 20
         For n := 1 to nLimit  // Registros
             If oQry_Mig:FieldType( n )=="N"
                cRegData := TRANSFORM( oQry_Mig:FieldGet( n ), Mascara(oQry_Mig) )
             Else
                cRegData := oQry_Mig:FieldGet( n )
             Endif
             @ LIN,COL PRINT Left( Hb_CStr(cRegData), 15 )
             COL := COL + Iif( MIN(oQry_Mig:FieldLen(n),50)< 30, 30, MIN(oQry_Mig:FieldLen(n),50) )
         Next

         LIN := LIN + 5

         oQry_Mig:Skip()

      ENDDO

      END PRINTPAGE
      END PRINTDOC

   ELSE

      MsgStop( "Couldn't select default printer !", "ERROR !" )

   ENDIF

Return

Function Mascara( oQry_Mig )
   nLongTot := oQry_Mig:FieldLen( n )
   nLongDec := iif(Right( lTrim( oQry_Mig:FieldGet( n ) ),3)=".",2,0)
   cInpMsk  := iif( nLongDec > 0, REPLICATE( "9", nLongTot - nLongDec -1 ) + "." + ;
               REPLICATE( "9", nLongDec ), REPLICATE( "9", nLongTot - nLongDec ) )
Return( cInpMsk )