Option Explicit On
Option Strict On
Imports System.Data
Imports System.IO
Imports Extensiones
Partial Class Contabilidad_ContabilidadElectronica
Inherits ClsPaginas
#Region " PROPIEDADES "
Private Property ArchivoCuentas As String
Get
ArchivoCuentas = ViewState("ArchivoCuentas").ToString
End Get
Set(value As String)
ViewState("ArchivoCuentas") = value
End Set
End Property
Private Property ArchivoBalanza As String
Get
ArchivoBalanza = ViewState("ArchivoBalanza").ToString
End Get
Set(value As String)
ViewState("ArchivoBalanza") = value
End Set
End Property
#End Region
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
InicializaValores()
End If
End Sub
#Region " INICIALIZA VALORES "
Private Sub InicializaValores()
'TxtFechaI.Text = String.Format("01/01/{0}", Now.Year)
'TxtFechaI.Text = Now.ToShortDateString
'If Not UsuarioActual.Empresa.UsaContabilidad Then
' BtnGenerar.Enabled = False
' Alert("Este módulo requiere el uso de contabilidad", TipoMensaje.MsgAdvertencia)
' Exit Sub
'End If
If UsuarioActual.Empresa.TipoContabilidad = 1 Then
Dim objconta As New ClsContabilidad
If objconta.GetEjercicios(UsuarioActual.CveEmpresa) Then
DdlEjercicio.DataSource = objconta.DtResultados
Else
DdlEjercicio.DataSource = Nothing
End If
objconta.Dispose()
DdlEjercicio.DataBind()
Else
Dim ObjConpaq As New ClsContPAQi(UsuarioActual.Empresa)
If ObjConpaq.GetEjercicios() Then
DdlEjercicio.DataSource = ObjConpaq.DtResultados
Else
DdlEjercicio.DataSource = Nothing
End If
ObjConpaq.Dispose()
DdlEjercicio.DataBind()
End If
DdlEjercicio.SelectedValue = Now.Year.ToString
DdlPeriodo.SelectedValue = Now.Month.ToString
End Sub
#End Region
#Region " GENERA XML "
Protected Sub BtnGenerar_Click(sender As Object, e As EventArgs)
'CREA DIRECTORIO SI NO EXISTE
DirectoryExistsCreate(String.Format("{0}ContabilidadElectronica", UsuarioActual.Empresa.Ruta))
'LIMPIA VALORES
ArchivoCuentas = ""
ArchivoBalanza = ""
If DdlTipoReporte.SelectedValue = "1" Then
GeneraCatalogo()
ElseIf DdlTipoReporte.SelectedValue = "2" Then
GeneraBalanza()
End If
End Sub
Private Sub GeneraCatalogo()
Dim FechaF = String.Format("01/{0}/{1}", DdlPeriodo.SelectedValue.PadLeft(2, "0"c), DdlEjercicio.SelectedValue).ToDate.LastDayOfMonth
Dim DtCuentas As New DataTable
If UsuarioActual.Empresa.TipoContabilidad = 1 Then
Dim objconta As New ClsContabilidad
If objconta.GetCuentas(-1, "-1", -1, UsuarioActual.CveEmpresa) Then
DtCuentas = objconta.DtResultados
End If
objconta.Dispose()
Else
Dim ObjConpaq As New ClsContPAQi(UsuarioActual.Empresa)
If ObjConpaq.GetCuentasElectronica(MinFecha, FechaF) Then
DtCuentas = ObjConpaq.DtResultados
End If
ObjConpaq.Dispose()
End If
If DtCuentas IsNot Nothing AndAlso DtCuentas.Rows.Count > 0 Then
Dim Str As New StringBuilder
'CABECERA
Str.Append("")
Str.Append("")
'DETALLES
For Each Dr As DataRow In DtCuentas.Rows
Str.Append("")
Next
DtCuentas.Dispose()
'FIN
Str.Append("")
'GUARDA ARHIVO
ArchivoCuentas = Server.MapPath(String.Format("~/Empresas/{0}/ContabilidadElectronica/{1}{2}{3}CT.xml", UsuarioActual.CveEmpresa, UsuarioActual.Empresa.RFC, DdlEjercicio.SelectedValue, DdlPeriodo.SelectedValue.PadLeft(2, "0"c)))
'SOBREESCRIBE
If System.IO.File.Exists(ArchivoCuentas) Then
System.IO.File.Delete(ArchivoCuentas)
End If
Dim fs As FileStream = File.Create(ArchivoCuentas)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(Str.ToString)
fs.Write(info, 0, info.Length)
fs.Close()
'DESCARGA XML
Dim DescargaZIP As Ionic.Zip.ZipFile = New Ionic.Zip.ZipFile()
If IO.File.Exists(ArchivoCuentas) Then
DescargaZIP.AddFile(ArchivoCuentas, "")
End If
DescargaZIP.Save(Response.OutputStream)
Response.AddHeader("content-Disposition", String.Format("attachment; filename={0}{1}{2}CT.zip", UsuarioActual.Empresa.RFC, DdlEjercicio.SelectedValue, DdlPeriodo.SelectedValue.PadLeft(2, "0"c)))
Response.ContentType = "application/zip"
Response.End()
End If
End Sub
Private Sub GeneraBalanza()
Dim FechaI = String.Format("01/{0}/{1}", DdlPeriodo.SelectedValue.PadLeft(2, "0"c), DdlEjercicio.SelectedValue).ToDate
Dim FechaF = FechaI.LastDayOfMonth
Dim DtCuentas As New DataTable
If UsuarioActual.Empresa.TipoContabilidad = 1 Then
Dim objconta As New ClsContabilidad
If objconta.GetBalanzaComprobacion(FechaI, FechaF, 1, -1, "-1", UsuarioActual.CveEmpresa, 0) Then
DtCuentas = objconta.DtResultados
End If
objconta.Dispose()
Else
Dim ObjConpaq As New ClsContPAQi(UsuarioActual.Empresa)
If ObjConpaq.GetBalanzaElectronica(FechaI, FechaF) Then
DtCuentas = ObjConpaq.DtResultados
End If
ObjConpaq.Dispose()
End If
If DtCuentas IsNot Nothing AndAlso DtCuentas.Rows.Count > 0 Then
Dim Str As New StringBuilder
'CABECERA
Str.Append("")
Str.Append("")
'DETALLES
For Each Dr As DataRow In DtCuentas.Rows
Str.Append("")
Next
DtCuentas.Dispose()
'FIN
Str.Append("")
'GUARDA ARHIVO
ArchivoBalanza = Server.MapPath(String.Format("~/Empresas/{0}/ContabilidadElectronica/{1}{2}{3}BN.xml", UsuarioActual.CveEmpresa, UsuarioActual.Empresa.RFC, DdlEjercicio.SelectedValue, DdlPeriodo.SelectedValue.PadLeft(2, "0"c)))
'SOBREESCRIBE
If System.IO.File.Exists(ArchivoBalanza) Then
System.IO.File.Delete(ArchivoBalanza)
End If
Dim fs As FileStream = File.Create(ArchivoBalanza)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(Str.ToString)
fs.Write(info, 0, info.Length)
fs.Close()
'DESCARGA XML
Dim DescargaZIP As Ionic.Zip.ZipFile = New Ionic.Zip.ZipFile()
If IO.File.Exists(ArchivoBalanza) Then
DescargaZIP.AddFile(ArchivoBalanza, "")
End If
DescargaZIP.Save(Response.OutputStream)
Response.AddHeader("content-Disposition", String.Format("attachment; filename={0}{1}{2}BN.zip", UsuarioActual.Empresa.RFC, DdlEjercicio.SelectedValue, DdlPeriodo.SelectedValue.PadLeft(2, "0"c)))
Response.ContentType = "application/zip"
Response.End()
End If
End Sub
#End Region
End Class