- Asignen sus valores desde un form
- la función SetTiket recibe como parámetros dos tablas
El formato de las tablas que recibe es la siguiente:
Dim tablaProductos As New DataTable
With tablaProductos.Columns
.Add("CANTIDAD")
.Add("NOMBRE")
.Add("PRECIO")
.Add("TOTAL")
End With
Dim tablaTiposPagos As New DataTable
With tablaTiposPagos.Columns
.Add("DESCRIPCION")
.Add("MONTO")
.Add("REFERENCIA")
End With
Ya escrito el archivo. solo falta mandarlo a imprimir con la siguiente instrucción
Shell("print /d:lpt1 c:\temp.txt", vbNormalFocus)
' para abrir la caja registradora, ya se incluyo en la clase
Lo he probado con las miniprinter EPSON y la caja registradora que se conecta a ellas mediante RJ-11
CLASE
Public Class Ticket
Public RutaArchivoTMP As String
Public NombreEmpresa As String
Public Slogan As String
Public DireccionParte1 As String
Public DireccionParte2 As String
Public DireccionParte3 As String
Public NumeroTicket As String
Public fecha As String
Public Telefono As String
Public EMail As String
Public Caja As String
Public Cajero As String
Public subTotal As String
Public descuento As String
Public otroDescuento As String
Public total As String
Public sucambio As String
Public SimboloOtroDescuento As String
Public LengthLineaPrint As Integer
Sub New(ByVal RutaFileTMP As String)
RutaArchivoTMP = RutaFileTMP
End Sub
Public Sub SetTiket(ByVal tablaProductos As DataTable, ByVal tablaTiposPagos As DataTable)
Dim intFileNo As Integer = FreeFile()
Dim tabla As New DataTable
With tabla.Columns
.Add("CANTIDAD") : .Add("NOMBRE") : .Add("PRECIO") : .Add("TOTAL")
End With
With tabla.Rows
'============================================================Cabecera conceptos
.Add("FECH", "A: " & fecha, "Folio: ", NumeroTicket)
.Add("", "", "", "")
.Add("CAN", "PRODUCTO ", "PRECIO", "TOTAL")
.Add("----", "--------------------", "--------", "--------")
'============================================================Detalle conceptos
Dim lector As DataTableReader
lector = tablaProductos.CreateDataReader
If lector.HasRows Then
While lector.Read
.Add(lector(0), lector(1), lector(2), lector(3))
End While
End If
.Add("----", "--------------------", "--------", "--------")
'============================================================Totales
.Add("", "SUBTOTAL", "= $", subTotal)
.Add("", "DESCUENTO", "= $", descuento)
.Add("", "OTRO DESCUENTO", "= " & SimboloOtroDescuento, otroDescuento)
.Add("", "", "", "________")
.Add("", "TOTAL", "= $", total)
.Add("....", "....................", "........", "........")
'============================================================Cabecera Tipos de pago
.Add("FORM", "A DE PAGO", "MONTO($)", "REFER")
.Add("----", "--------------------", "--------", "--------")
'============================================================Detalles Tipos de pago
Dim lectorTwo As DataTableReader
lectorTwo = tablaTiposPagos.CreateDataReader
If lectorTwo.HasRows Then
While lectorTwo.Read
.Add("", lectorTwo(0), lectorTwo(1), lectorTwo(2))
End While
End If
.Add("----", "--------------------", "--------", "--------")
.Add("", "SU CAMBIO........$", sucambio, "")
End With
Dim lista As New ArrayList
'Antes de pasar a la funcion reeplazar el # por ?
For Each pro As DataRow In tabla.Rows
pro(0) = Replace(pro(0), "#", "?") : pro(1) = Replace(pro(1), "#", "?")
pro(2) = Replace(pro(2), "#", "?") : pro(3) = Replace(pro(3), "#", "?")
lista.Add(formateaProductos(pro(0) & "#" & pro(1) & "#" & pro(2) & "#" & pro(3)))
Next
FileOpen(1, RutaArchivoTMP, OpenMode.Output)
PrintLine(1, Centrar(NombreEmpresa))
PrintLine(1, " ")
PrintLine(1, Centrar(Slogan))
PrintLine(1, Centrar(DireccionParte1))
PrintLine(1, Centrar(DireccionParte2))
PrintLine(1, Centrar(DireccionParte3))
For Each producto As String In lista
PrintLine(1, producto)
Next
PrintLine(1, " *Solo por pago en efectivo* ")
PrintLine(1, "........................................")
PrintLine(1, Centrar(Telefono))
PrintLine(1, Centrar(EMail))
PrintLine(1, Centrar(Caja))
PrintLine(1, Centrar(Cajero))
PrintLine(1, Centrar("***COPIA DEL CLIENTE***"))
'Abrimos CAJA
PrintLine(1, Chr(27) & "p" & Chr(0) & Chr(25) & Chr(250))
FileClose(1)
End Sub
Function formateaProductos(ByVal line As String) As String
Dim Array As Array
Dim op As Integer = 0
Dim newStr As String = ""
Array = line.Split("#")
For i As Integer = 0 To Array.Length - 1
If i = 0 Or i = 1 Then op = 1
If i = 2 Or i = 3 Then op = 2
formatoValor(Array(i), i, op)
Next
For i As Integer = 0 To Array.Length - 1
newStr += Array(i)
Next
Return newStr
End Function
''' <summary>
'''
''' </summary>
''' <param name="valor"></param>
''' <param name="op">1=a la derecha,2=a la izquierda</param>
''' <remarks></remarks>
Sub formatoValor(ByRef valor As String, ByVal index As Integer, ByVal op As Integer)
'CANTIDAD MAXLENG = 4
'NOMBRE MAXLENG = 20
'PRECIO MAXLENG = 8
'TOTAL MAXLENG = 8
Select Case index
Case 0 : index = 4
Case 1
index = 20
If valor = "--------------------" Or valor = "====================" Or valor = "...................." Or valor = "____________________" Or valor = "- -- -- -- -- -" Or valor = ". .. .. .. .. ." Then
Else
If valor.Length >= 20 Then valor = Mid(valor, 1, 19) 'truncamos el nombre a 19
End If
Case 2 : index = 8
Case 3 : index = 8
End Select
valor = Trim(valor)
Dim valorAux As String = valor
Select Case op
Case 1 'derecho
For i = Len(valor) To index - 1
valorAux += " "
Next
Case 2 'izquierdo
For i = Len(valor) To index - 1
valorAux = " " + valorAux
Next
End Select
valor = valorAux
End Sub
Function Centrar(ByVal Texto As String) As String
Dim CharsTotalforLine As Integer = LengthLineaPrint
Dim PosInicial As Integer = (CharsTotalforLine - Len(Texto)) / 2
Dim res As String = ""
For i As Integer = 1 To PosInicial
res += " "
Next
For Each caracter As String In Texto
res += caracter
Next
For i As Integer = 1 To PosInicial
res += " "
Next
Return Mid(res, 1, LengthLineaPrint - 1)
End Function
End Class