#include "menu1100.ch"
#include "set.ch"
#include "myinkey.ch"
#include "clientes.ch"

#define  tCOND       Chr( 15 )
#define  tNORMAL     Chr( 18 )
#define  t10CPI      Chr( 27 ) + 'P'
#define  t12CPI      Chr( 27 ) + 'M'
#define  tINITPRN    Chr( 27 ) + '@'       + ;                // inicializo
                     t10CPI                + ;                // 10 cpi
                     tNORMAL               + ;                // nomal
                     Chr( 27 ) + 'C' + Chr( 0 ) + Chr( 6 )    // formulario de 6"
#define  tFIN_PRN    Chr( 27 ) + '@'       + ;                // inicializo
                     t10CPI                + ;                // 10 cpi
                     tNORMAL               + ;                // normal
                     Chr( 27 ) + 'C' + Chr( 0 ) + Chr( 12 )   // formulario de 12"

/***
        -    1    Function   FacturaEpson
        -    2    Function   Det_Impre
        -    3    Function   Val_1
        -    4    Function   Letras1
        -    5    Function   InitCoord
*/
Function FacturaEpson( xPrn       , dFEC       , cVEND      , aLeyPagos , lN_CRED    , lCANCELAR,;
                       nTOTAL     , nSU_PAGO   , aDatClient , aDatFact  , CONTROLADOR, nDescGral,;
                       cLeyBonDesc, aLeyRecDesc, nDescUsados, cLeyUsados, nCopias    , nLineas   ,;
                       aLEYENDAS  , nOPE       , aFpagos    , oFis      , aTarj           )
*Begin
   Local T1, T2, Y, R, G, i, j, cFec, nLen, cAux, cTipOpe
   If ( Det_Impre( xPrn ) != K_ESC )
      InitCoord( @oFis )
      Set( _SET_DEVICE, 'PRINTER' )
      Set( _SET_PRINTFILE, xPrn )
      nCopias := If( (Valtype( nCopias  ) = 'N'), nCopias, 2 )
      cFec    := DToC( dFec )
      j       := aScan( aFPAGOS, { | x | x[FPAGOS_MON] != 0 } )
      j       := If( (j = 0), 1, j )
      cTipOpe := If( lN_CRED, 'NOTA DE CREDITO', 'FACTURA' )
      SetPRC( 0, 0 )
      @ 0,0 Say tINITPRN
      For i :=1 To nCopias
         SetPRC( 0, 0 )
         @  oFis:yCI  ,oFis:xCI   Say 'C.I.' + Str( nOPE, 8 )
         @  oFis:yTOp ,oFis:xTOp  Say cTipOpe
         @  oFis:yFec ,oFis:xFec  Say cFec
         @  oFis:yHora,oFis:xHora Say Left( Time(), 5 )

         @  oFis:yNCli,oFis:xNCli Say aDatClient[cliNOMBRE]         // nombre
         @  oFis:yTR  ,oFis:xTR   Say aDatClient[cliTIPRES]         // tipo responsable

         @  oFis:yDCli,oFis:xDCli Say aDatClient[cliDOMICI]         // domicilio
         @  oFis:yCUI ,oFis:xCUI  Say aDatClient[cliCUIT__]         // CUIT

         cAux := aFPAGOS[j,FPAGOS_DES]
         If ( Len( aTarj ) > 0 )
            cAux += ' - '
            For R :=1 To Len( aTarj[1,2] )
               If ( aTarj[1,2,R,2] != 0 )
                  cAux +=Trim( aTarj[1,2,R,1] ) + '/' + aTarj[1,2,R,5] + ' /'
               EndIF
            Next Y

         EndIF
         @ oFis:yFP1,oFis:xFP1 Say cAux
         Y     := oFis:yLib
         nLen  := Len( aDatFact )
         @ Y, 0 Say ''
         For R :=1 To nLineas
            If ( R <= nLen .And. Len( aDatFact[R] ) >= 10 )
               @ Y+R,oFis:xCan Say Val( aDatFact[R,2] ) Picture '999'               // cantidad
               @ Y+R,oFis:xTit Say aDatFact[R,1]                                    // titulo 30 char
               @ Y+R,oFis:xCod Say aDatFact[R,9]                                    // EAN
               @ Y+R,oFis:xPU  Say Val_1( aDatFact[R,12] ) Picture '9999.99'        // precio unitario y abajo, total
               @ Y+R,oFis:xDes Say ''                                               // descuento
               @ Y+R,oFis:xTot Say (Val( aDatFact[R,2] ) * Val_1( aDatFact[R,12] )) Picture '9999.99'
            ElseIf ( R <= nLen )
               @ Y+R,oFis:xTit Say aDatFact[R,1]
               @ Y+R,oFis:xTot Say ( aDatFact[R,2] ) Picture '9999.99'
            EndIF                          //
         Next R
         Y := oFis:yLey                    // fijo tope
         If ( ValType( aLEYENDAS ) = 'A' ) // descuento segun las leyendas que tenga
            aEval( aLEYENDAS, { | x | Y -= If( (x[2] = 'S'), 1, 0 ) } )
            For R :=1 To Len( aLEYENDAS )
               If ( aLEYENDAS[R,2] = 'S' )
                  @ ++Y, 7 Say ConvToAnsiCP( aLEYENDAS[R,1] )
               EndIF
            Next R
         EndIF
         @ oFis:yLet     ,oFis:xLet   Say 'Son Pesos ' + Letras1( Round( nTOTAL, 2 ) )
         @ oFis:yTotG    ,oFis:xTotG  Say nTOTAL Picture '99,999.99'
         @ oFis:yTotG + 1, 0 Say t10CPI
         @ oFis:yTotG + 1, 0          Say Chr( 12 )             // salto la hoja
      Next i
      @ PRow(),PCol() Say tFIN_PRN
      Set( _SET_DEVICE, 'SCREEN' )
      Set( _SET_PRINTFILE, '' )
   EndIF                                   //
   Return ( .t. )
*End;
*
*   -  2 -
Static Function Det_Impre( xPORT )                 // detecta la prescencia de la
*                                          // impresora
*Begin
   Local nRET, oErr, cDev, cPort
   If ( ValType( xPORT ) != 'C' )
      xPORT :='LPT1'
   EndIF
   nRET :=1
   oErr :=ErrorBlock( { | e | Break( e ) } )
   BEGIN SEQUENCE
   cDev  :=Set( _SET_DEVICE, 'PRINTER' )
   cPort :=Set( _SET_PRINTFILE, xPORT )
   SetPRC( 0, 0 )                          // intento inicializar impresora
   DevOut( Chr( 27 ) + Chr( 64 ) )         // solo para epson
   SetPRC( 0, 0 )                          // intento inicializar impresora
   Set( _SET_PRINTFILE, cPort )
   Set( _SET_DEVICE, cDev )
   RECOVER 
   nRET :=K_ESC
   MsgBox( 'Impresora No Conectada En El Puerto ' + xPORT, 'Error  125' )
   END SEQUENCE
   ErrorBlock( oErr )
   Return ( nRET )                         // devuelvo codigo de pulsacion
*End;
*
*   -  3 -
Function Val_1( cName )
*
*Begin
   Local RET :=0
   If ( ValType( cName ) = 'C' )
      RET :=Val( StrTran( cName, ',', '.' ) )
   ElseIf ( ValType( cName ) = 'N' )
      RET :=cName
   EndIF
   Return ( RET )
*End;
*
*   -  4 -
Function Letras1( NUM__ )
*
*Begin
   Local A, B, C, D, F1, F2, RET, R, AUX, S, V, U
   A :={ "un ", "dos ", "tres ", "cuatro ", "cinco ", "seis ", "siete ", "ocho ", "nueve " }
   B :={ "once ", "doce ", "trece ", "catorce ", "quince ", "dieciseis ", "diecisiete ", "dieciocho ", "diecinueve " }
   C :={ "diez ", "veinte ", "treinta ", "cuarenta ", "cincuenta ", "sesenta ", "setenta ", "ochenta ", "noventa " }
   D :={ "cien ", "doscientos ", "trescientos ", "cuatrocientos ", "quinientos ", "seiscientos ", "setecientos ", "ochocientos ", "novecientos " }
   ////E1 ="mil "; E2 ="miles ";
   F1 ="millon "; F2 ="milones "
   RET :=''
   NUM__ :=If( (ValType( NUM__ ) = 'N'), NUM__, 0 )
   AUX :=Str( Int( NUM__ ), 7, 0 )
   R := 1
   While ( R < 8 )
      S :=SubStr( AUX, R, 1 )
      V :=Val( S )
      If ( R = 1 .And. V != 0 )            // por millon
         RET +=A[ V ] + If( (V > 1), F2, F1 )
      ElseIf ( R = 2 .And. V != 0 )        // por cien mil
         If ( V = 1 .And. SubStr( AUX, 3, 1 ) = '0' .And. SubStr( AUX, 4, 1 ) = '0' )
            RET += 'un '
         End
         RET +=D[ V ]
         If ( V = 1 .And. ( SubStr( AUX, 3, 1 ) != '0' .Or. SubStr( AUX, 4, 1 ) != '0' ) )
            RET := Stuff( RET, Len( RET ), 1, 'to' )
         End
      ElseIf ( R = 3 .And. V != 0 )        // por diez mil
         U :=SubStr( AUX, 4, 1 )
         If ( V = 2 .And. U != '0')
            RET += 'veinti'
         ElseIf ( V = 1 )
            RET += If( (U = '0'), 'diez ', B[ Val( U ) ] )
            AUX :=Stuff( AUX, 4, 1, '0' )
         Else
            RET += C[V] + If( (U != '0'), 'y ', '' )
         End
      ElseIf ( R = 4 .And. S != ' ' )      // por mil
         If ( SubStr( AUX, 2, 1 ) != '0' .Or. SubStr( AUX, 3, 1 ) != '0' )
            RET +=If( (V != 0), A[V], '' ) + 'mil '
         End
      ElseIf ( R = 5 .And. V != 0 )        // por cien
         RET += D[ V ]
         If ( V = 1 .And. ( SubStr( AUX, 6, 1 ) != '0' .Or. SubStr( AUX, 7, 1 ) != '0' ) )
            RET := Stuff( RET, Len( RET ), 1, 'to ' )
         End
      ElseIf ( R = 6 .And. V != 0 )        // por diez
         U :=SubStr( AUX, 7, 1 )
         If ( V = 2 .And. U != '0')
            RET += 'veinti'
         ElseIf ( V = 1 )
            RET += If( (U = '0'), 'diez ', B[ Val( U ) ] )
            AUX :=Stuff( AUX, 7, 1, ' ' )
         Else
            RET += C[V] + If( (U != '0'), 'y ', '' )
         End
      ElseIf ( R = 7 .And. V != 0  )       // por unidad
         RET += A[ V ]
         If ( V = 1 )
            RET :=Stuff( RET, Len( RET ), 1, 'o' )
         End
      End                                  // del If R = 7 .And. ...
      R += 1
   End                                     // del While R > 0
   RET :=If( (NUM__ < 0), 'Menos ' + RET, RET )
   If ( Int( NUM__ ) != Round( NUM__, 2 ) )
      RET +=', Con ' + Right( Str( NUM__, 12, 2 ), 2 ) + '/100'
   End
   V :=Upper( Left( RET, 1 ) )
   RET :=Stuff( RET, 1, 1, V )
   Return ( RET )
*End
*
*   -  5 -
Function InitCoord( oFis )
*
*Begin
   Static oCoord
   Local oErr, lA1
   lA1  :=IsMemberVar( oFis, 'xCI' )
   If ( oFis = NIL .Or. !lA1 )
      If ( oCoord = NIL )
         oCoord := IniciaCoor():New()
      EndIF
      oFis :=oCoord
   ElseIf ( oCoord = NIL )
      oCoord := oFis
   EndIF
   oFis:yCI     := If( (oFis:yCI    = NIL),   2, oFis:yCI    )  ;  oFis:xCI     := If( (oFis:xCI    = NIL),  45, oFis:xCI    )
   oFis:yTOp    := If( (oFis:yTOp   = NIL),   4, oFis:yTOp   )  ;  oFis:xTOp    := If( (oFis:xTOp   = NIL),  45, oFis:xTOp   )
   oFis:yFec    := If( (oFis:yFec   = NIL),   5, oFis:yFec   )  ;  oFis:xFec    := If( (oFis:xFec   = NIL),  45, oFis:xFec   )
   oFis:yHora   := If( (oFis:yHora  = NIL),   5, oFis:yHora  )  ;  oFis:xHora   := If( (oFis:xHora  = NIL),  45, oFis:xHora  )

   oFis:yNCli   := If( (oFis:yNCli  = NIL),   7, oFis:yNCli  )  ;  oFis:xNCli   := If( (oFis:xNCli  = NIL),   8, oFis:xNCli  )
   oFis:yTR     := If( (oFis:yTR    = NIL),   7, oFis:yTR    )  ;  oFis:xTR     := If( (oFis:xTR    = NIL),  55, oFis:xTR    )
   oFis:yDCli   := If( (oFis:yDCli  = NIL),   8, oFis:yDCli  )  ;  oFis:xDCli   := If( (oFis:xDCli  = NIL),   8, oFis:xDCli  )
   oFis:yCUI    := If( (oFis:yCUI   = NIL),   8, oFis:yCUI   )  ;  oFis:xCUI    := If( (oFis:xCUI   = NIL),  55, oFis:xCUI   )
   oFis:yFP1    := If( (oFis:yFP1   = NIL),  10, oFis:yFP1   )  ;  oFis:xFP1    := If( (oFis:xFP1   = NIL),  14, oFis:xFP1   )
   oFis:yFP2    := If( (oFis:yFP2   = NIL),  11, oFis:yFP2   )  ;  oFis:xFP2    := If( (oFis:xFP2   = NIL),  14, oFis:xFP2   )

   oFis:yLib    := If( (oFis:yLib   = NIL),  11, oFis:yLib   )  ;  oFis:xCan    := If( (oFis:xCan   = NIL),   1, oFis:xCan   )
                                                                   oFis:xCod    := If( (oFis:xCod   = NIL),   6, oFis:xCod   )
                                                                   oFis:xTit    := If( (oFis:xTit   = NIL),  20, oFis:xTit   )
                                                                   oFis:xPU     := If( (oFis:xPU    = NIL),  55, oFis:xPU    )
                                                                   oFis:xDes    := If( (oFis:xDes   = NIL),  63, oFis:xDes   )
                                                                   oFis:xTot    := If( (oFis:xTot   = NIL),  70, oFis:xTot   )
   oFis:yLey    := If( (oFis:yLey   = NIL),  28, oFis:yLey   )
   oFis:yLet    := If( (oFis:yLet   = NIL),  30, oFis:yLet   )  ;  oFis:xLet    := If( (oFis:xLet   = NIL),   3, oFis:xLet   )
   oFis:yTotG   := If( (oFis:yTotG  = NIL),  31, oFis:yTotG  )  ;  oFis:xTotG   := If( (oFis:xTotG  = NIL),  68, oFis:xTotG  )
   Return ( nil )
*End;
*
*   -
CLASS IniciaCoor
   EXPORTED:
   Var yHora, xHora
   Var xCI, xFec, xNCli, xDCli, xCUI, xTR, xFP1, xFP2, xTOp
   Var yCI, yFec, yNCli, yDCli, yCUI, yTR, yFP1, yFP2, yTOp
   Var yLib, xCod, xTit, xCan, xPU, xTot, xDes
   Var xLet, yLet
   Var xTotG, yTotG
   Var yLey

   INLINE METHOD Init
      ::yCI     :=  3           ;        ::xCI     := 45
      ::yTOp    :=  4           ;        ::xTOp    := 45
      ::yFec    :=  6           ;        ::xFec    := 45
      ::yHora   :=  7           ;        ::xHora   := 45

      ::yNCli   :=  8           ;        ::xNCli   :=  8
      ::yTR     :=  8           ;        ::xTR     := 55

      ::yDCli   :=  9           ;        ::xDCli   :=  8      // letra condensada
      ::yCUI    :=  9           ;        ::xCUI    := 55

      ::yFP1    := 11           ;        ::xFP1    := 14
      ::yFP2    := 11           ;        ::xFP2    := 14

      ::yLib    := 13           ;        ::xCan    :=  1
                                         ::xCod    :=  6
                                         ::xTit    := 20
                                         ::xPU     := 55
                                         ::xDes    := 63
                                         ::xTot    := 70
      ::yLey    := 28
      ::yLet    := 30           ;        ::xLet    :=  3
      ::yTotG   := 31           ;        ::xTotG   := 68
   Return ( self )
ENDCLASS
*