Files
SIG/Contabilidad/ContabilidadElectronica.aspx.vb

281 lines
12 KiB
VB.net

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}", NowADS().Year)
'TxtFechaI.Text = NowADS().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 = NowADS().Year.ToString
DdlPeriodo.SelectedValue = NowADS().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 As Date = 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("<?xml version=""1.0"" encoding=""UTF-8""?>")
Str.Append("<catalogocuentas:Catalogo xsi:schemaLocation=""www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas/CatalogoCuentas_1_3.xsd"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Version=""1.3"" ")
Str.AppendFormat("RFC=""{0}"" ", UsuarioActual.Empresa.RFC)
Str.AppendFormat("Mes=""{0}"" ", DdlPeriodo.SelectedValue)
Str.AppendFormat("Anio=""{0}"" ", DdlEjercicio.SelectedValue)
Str.AppendFormat("Sello=""{0}"" ", "123")
Str.AppendFormat("noCertificado=""{0}"" ", "123")
Str.AppendFormat("Certificado=""{0}"" ", "1234567890")
Str.Append("xmlns:catalogocuentas=""www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas"">")
'DETALLES
For Each Dr As DataRow In DtCuentas.Rows
Str.Append("<catalogocuentas:Ctas ")
If UsuarioActual.Empresa.TipoContabilidad = 1 Then
Str.AppendFormat("CodAgrup=""{0}"" ", Dr("CodigoSAT").ToString)
Str.AppendFormat("NumCta=""{0}"" ", Dr("Codigo").ToString)
Str.AppendFormat("Desc=""{0}"" ", HttpUtility.HtmlEncode(Dr("Nombre").ToString))
Str.AppendFormat("SubCtaDe=""{0}"" ", Dr("CodigoPadre").ToString)
Str.AppendFormat("Nivel=""{0}"" ", Dr("Nivel").ToString)
Str.AppendFormat("Natur=""{0}"" ", Dr("Tipo").ToString)
Else
Str.AppendFormat("CodAgrup=""{0}"" ", Dr("CodAgrup").ToString)
Str.AppendFormat("NumCta=""{0}"" ", Dr("NumCta").ToString)
Str.AppendFormat("Desc=""{0}"" ", HttpUtility.HtmlEncode(Dr("Desc").ToString))
Str.AppendFormat("SubCtaDe=""{0}"" ", Dr("SubCtaDe").ToString)
Str.AppendFormat("Nivel=""{0}"" ", Dr("Nivel").ToString)
Str.AppendFormat("Natur=""{0}"" ", Dr("Natur").ToString)
End If
Str.Append("/>")
Next
DtCuentas.Dispose()
'FIN
Str.Append("</catalogocuentas:Catalogo>")
'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)))
ArchivoCuentas = $"{UsuarioActual.Empresa.Ruta}/ContabilidadElectronica/{ UsuarioActual.Empresa.RFC}{DdlEjercicio.SelectedValue}{DdlPeriodo.SelectedValue.PadLeft(2, "0"c)}CT.xml"
'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 As Date = String.Format("01/{0}/{1}", DdlPeriodo.SelectedValue.PadLeft(2, "0"c), DdlEjercicio.SelectedValue).ToDate
Dim FechaF As Date = 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, -1, UsuarioActual.CveEmpresa, 0) Then
DtCuentas = objconta.DtResultados
End If
objconta.Dispose()
If DtCuentas IsNot Nothing Then
Dim Deudor As Decimal = DtCuentas.SUM("SaldoAD")
Dim Acreedor As Decimal = DtCuentas.SUM("SaldoAA")
If Deudor <> Acreedor Then
Alert("los Saldos Actuales no cuadran.", TipoMensaje.MsgError)
Exit Sub
End If
End If
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("<?xml version=""1.0"" encoding=""UTF-8""?>")
Str.Append("<BCE:Balanza xsi:schemaLocation=""www.sat.gob.mx/esquemas/ContabilidadE/1_3/BalanzaComprobacion/BalanzaComprobacion_1_3.xsd"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Version=""1.3"" ")
Str.AppendFormat("RFC=""{0}"" ", UsuarioActual.Empresa.RFC)
Str.AppendFormat("Mes=""{0}"" ", DdlPeriodo.SelectedValue)
Str.AppendFormat("Anio=""{0}"" ", DdlEjercicio.SelectedValue)
Str.AppendFormat("TipoEnvio=""{0}"" ", "C")
Str.AppendFormat("FechaModBal=""{0}"" ", FechaF.ToString("yyyy-MM-dd"))
Str.AppendFormat("Sello=""{0}"" ", "123")
Str.AppendFormat("noCertificado=""{0}"" ", "123")
Str.AppendFormat("Certificado=""{0}"" ", "1234567890")
Str.Append("xmlns:BCE=""www.sat.gob.mx/esquemas/ContabilidadE/1_3/BalanzaComprobacion"">")
'DETALLES
For Each Dr As DataRow In DtCuentas.Rows
Str.Append("<BCE:Ctas ")
If UsuarioActual.Empresa.TipoContabilidad = 1 Then
Str.AppendFormat("NumCta=""{0}"" ", Dr("Codigo").ToString)
Str.AppendFormat("SaldoIni=""{0}"" ", Dr("SaldoInicial").ToString)
Str.AppendFormat("Debe=""{0}"" ", Dr("Cargos").ToString)
Str.AppendFormat("Haber=""{0}"" ", Dr("Abonos").ToString)
Str.AppendFormat("SaldoFin=""{0}"" ", Dr("SaldoActual").ToString)
Else
Str.AppendFormat("NumCta=""{0}"" ", Dr("NumCta").ToString)
Str.AppendFormat("SaldoIni=""{0}"" ", Dr("SaldoIni").ToString)
Str.AppendFormat("Debe=""{0}"" ", Dr("Debe").ToString)
Str.AppendFormat("Haber=""{0}"" ", Dr("Haber").ToString)
Str.AppendFormat("SaldoFin=""{0}"" ", Dr("SaldoFin").ToString)
End If
Str.Append("/>")
Next
DtCuentas.Dispose()
'FIN
Str.Append("</BCE:Balanza>")
'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)))
Dim tDir As String = Server.MapPath(String.Format("~/Empresas/{0}/ContabilidadElectronica", UsuarioActual.CveEmpresa, UsuarioActual.Empresa.RFC))
'SOBREESCRIBE
If Not System.IO.Directory.Exists(tDir) Then System.IO.Directory.CreateDirectory(tDir)
'SOBREESCRIBE
If System.IO.File.Exists(ArchivoBalanza) Then System.IO.File.Delete(ArchivoBalanza)
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