Generaracion y carga de excel para la generacion de prepoliza
This commit is contained in:
@@ -4,6 +4,7 @@ Imports System.Data
|
||||
Imports Extensiones
|
||||
Imports System.Linq
|
||||
Imports System.Web.Services
|
||||
Imports GemBox.Spreadsheet
|
||||
|
||||
Partial Class ContabilidadC_RegPrePoliza
|
||||
Inherits ClsPaginas
|
||||
@@ -171,6 +172,8 @@ Partial Class ContabilidadC_RegPrePoliza
|
||||
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
|
||||
|
||||
If Not Page.IsPostBack Then
|
||||
|
||||
|
||||
If UsuarioActual.Empresa.TipoContabilizacion = 1 OrElse UsuarioActual.Empresa.TipoContabilidad = 1 Then
|
||||
Response.Redirect(Request.Url.PathAndQuery.Replace("RegPrePoliza", "RegPrePolizaS"))
|
||||
End If
|
||||
@@ -223,8 +226,9 @@ Partial Class ContabilidadC_RegPrePoliza
|
||||
Contabilizar()
|
||||
End If
|
||||
Exit Sub
|
||||
LnkExcel.Visible = False
|
||||
Else
|
||||
|
||||
LnkExcel.Visible = True
|
||||
CreaDT()
|
||||
BtnEliminar.Visible = False
|
||||
|
||||
@@ -527,13 +531,20 @@ Partial Class ContabilidadC_RegPrePoliza
|
||||
|
||||
If UsuarioActual.CveEmpresa.Contiene(120) Then
|
||||
DdlPlantilla.DataSource = GlobalDTContaPlantillasPolizas(UsuarioActual.CveEmpresa, -1)
|
||||
DdlPlantilla2.DataSource = GlobalDTContaPlantillasPolizas(UsuarioActual.CveEmpresa, -1)
|
||||
|
||||
Else
|
||||
DdlPlantilla.DataSource = GlobalDTContaPlantillasPolizas(UsuarioActual.CveEmpresa, TipoPlantilla)
|
||||
DdlPlantilla2.DataSource = GlobalDTContaPlantillasPolizas(UsuarioActual.CveEmpresa, TipoPlantilla)
|
||||
|
||||
End If
|
||||
|
||||
|
||||
DdlPlantilla.DataBind()
|
||||
DdlPlantilla.Items.Insert(0, New ListItem("Plantilla Personalizada", "53"))
|
||||
|
||||
DdlPlantilla2.DataBind()
|
||||
DdlPlantilla2.Items.Insert(0, New ListItem("Plantilla Personalizada", "53"))
|
||||
End Sub
|
||||
|
||||
Private Sub CargaConceptos()
|
||||
@@ -801,6 +812,8 @@ Partial Class ContabilidadC_RegPrePoliza
|
||||
dr("PorcentajeImpuesto") = drt("PorcentajeImpuesto")
|
||||
dr("CveCuenta") = drt("CveCuenta")
|
||||
DtMovimientos.Rows.Add(dr)
|
||||
|
||||
|
||||
Next
|
||||
|
||||
|
||||
@@ -1754,5 +1767,252 @@ Partial Class ContabilidadC_RegPrePoliza
|
||||
End If
|
||||
ObjGral.Dispose()
|
||||
End Sub
|
||||
|
||||
Private Sub LnkExcel_Click(sender As Object, e As EventArgs) Handles LnkExcel.Click
|
||||
RegistraScript(String.Format("$('#{0}').modal();", "DivCargaExcel"))
|
||||
|
||||
End Sub
|
||||
|
||||
Protected Sub BtnDescargar_Click(sender As Object, e As EventArgs) Handles BtnDescargar.Click
|
||||
Dim NombreArchivo As String
|
||||
SpreadsheetInfo.SetLicense(ModExtensiones.SerialGemBoxSpreadsheet)
|
||||
Dim workbook As New ExcelFile()
|
||||
Dim worksheet = workbook.Worksheets.Add(DdlPlantilla2.SelectedItem.Text.ToString)
|
||||
|
||||
Dim CvePlantilla As Integer = DdlPlantilla2.SelectedValue.ToEntero
|
||||
Dim Dt As DataTable = GlobalDTContaPlantillasPolizasDetalle(CvePlantilla).FiltraParametros(String.Format("CveEmpresa={0}", UsuarioActual.CveEmpresa), "Orden")
|
||||
|
||||
Dim DtExcel As DataTable = (From row As DataRow In Dt.Rows
|
||||
Select New With
|
||||
{
|
||||
.ConceptoAdministrativo = row.Item("ConceptoContable"),
|
||||
.Cuenta = "",
|
||||
.NombreCuenta = "",
|
||||
.Cargo = "",
|
||||
.Abono = "",
|
||||
.Referencia = "",
|
||||
.Concepto = "",
|
||||
.SubProyecto = row.Item("SubProyecto"),
|
||||
.CveConceptoContable = row.Item("CveConcepto"),
|
||||
.CveSubProyecto = IIf(row.Item("CveSubProyecto").ToString.ToEntero > 0, row.Item("CveSubProyecto"), -1),
|
||||
.IdCtaContable = IIf(row.Item("IdCtaContable").ToString.ToEntero > 0, row.Item("IdCtaContable"), -1)
|
||||
}).QueryToDataTable(True)
|
||||
|
||||
worksheet.InsertDataTable(DtExcel, New InsertDataTableOptions() With
|
||||
{
|
||||
.ColumnHeaders = True,
|
||||
.StartRow = 0,
|
||||
.StartColumn = 0
|
||||
})
|
||||
|
||||
Dim columnCount = worksheet.CalculateMaxUsedColumns()
|
||||
For i = 0 To columnCount
|
||||
worksheet.Columns(i).AutoFit(1, worksheet.Rows(0), worksheet.Rows(worksheet.Rows.Count - 1))
|
||||
Next
|
||||
worksheet.Columns("I").Hidden = True
|
||||
worksheet.Columns("J").Hidden = True
|
||||
worksheet.Columns("K").Hidden = True
|
||||
|
||||
|
||||
NombreArchivo = DdlPlantilla2.SelectedItem.Text.ToString
|
||||
workbook.Save(Response, $"{NombreArchivo}.xlsx", SaveOptions.XlsxDefault)
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
Protected Sub BtnCargaExcel_Click(sender As Object, e As EventArgs) Handles BtnCargaExcel.Click
|
||||
If TxtExcel.getfiles().Archivos.Count > 0 Then
|
||||
Dim file As Archivo = TxtExcel.getfiles().Archivos(0)
|
||||
|
||||
Dim Directorio As String = Server.MapPath("~") & file.patch
|
||||
|
||||
Dim Tipo As String = If(file.Extencion.Contiene("xlsx", "xlsx", "xlsm", "xlsb"), "EXCEL", "CSV")
|
||||
CargarArchivo(Directorio, file.filename, Tipo)
|
||||
Else
|
||||
Alert("No existe el archivo.Favor de subir un archivo csv o excel a la plataforma", TipoMensaje.MsgError)
|
||||
End If
|
||||
End Sub
|
||||
Private Sub CargarArchivo(Ruta As String, FileName As String, Tipo As String)
|
||||
Dim dt As New DataTable
|
||||
' Conexion As Odbc.OdbcConnection = Nothing
|
||||
Dim ErrorArchivo = False
|
||||
Dim ErrorExcel = False
|
||||
|
||||
dt = ReadExcelFile(Ruta + FileName, True)
|
||||
|
||||
|
||||
If Not ErrorArchivo Then
|
||||
|
||||
If Not dt.Columns(0).ColumnName = "ConceptoAdministrativo" Then
|
||||
Alert("El nombre de la primera columna debe ser 'ConceptoAdmin'. Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
If Not dt.Columns(1).ColumnName = "Cuenta" Then
|
||||
Alert("El nombre de la segunda columna debe ser 'Cuenta'.Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
If Not dt.Columns(2).ColumnName = "NombreCuenta" Then
|
||||
Alert("El nombre de la tercera columna debe ser 'NombreCuenta'.Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
If Not dt.Columns(3).ColumnName = "Cargo" Then
|
||||
Alert("El nombre de la cuarta columna debe ser 'Cargo'. Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
If Not dt.Columns(4).ColumnName = "Abono" Then
|
||||
Alert("El nombre de la quinta columna debe ser 'Abono' .Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
If Not dt.Columns(5).ColumnName = "Referencia" Then
|
||||
Alert("El nombre de la sexta columna debe ser 'Referencia'. Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
If Not dt.Columns(6).ColumnName = "Concepto" Then
|
||||
Alert("El nombre de la séptima columna debe ser 'Concepto'.Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
If Not dt.Columns(7).ColumnName = "SubProyecto" Then
|
||||
Alert("El nombre de la octava columna debe ser 'SubProyecto'. Favor de corregirlo", TipoMensaje.MsgError)
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
|
||||
If Not ErrorArchivo Then
|
||||
|
||||
|
||||
Dim ObjConta As New ClsContabilidad()
|
||||
ObjConta.IniciaTransacSQL()
|
||||
Try
|
||||
Dim TieneError As Boolean = False
|
||||
Dim ContadorUpdate As Integer = 0
|
||||
Dim ContadorError As Integer = 0
|
||||
Dim NombreCuenta As String = ""
|
||||
Dim CvePrePoliza As Integer = 0
|
||||
Dim Cuenta As String = ""
|
||||
Dim Reporte As New StringBuilder
|
||||
Dim TotalCargos As Integer = 0
|
||||
Dim TotalAbonos As Integer = 0
|
||||
|
||||
|
||||
CvePrePoliza = ObjConta.AddPrePolizas("Contabilidad/RegPrePolizaExcel", DdlPlantilla2.SelectedValue.ToEntero, TotalCargos, TotalAbonos, 1, TxtFecha.Text.ToDate, DdlPlantilla2.SelectedItem.Text.ToString, UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa, CveTipoPoliza:=DdlTipoPolizaAdd.SelectedValue.ToEntero)
|
||||
|
||||
|
||||
For Each Row As DataRow In dt.Rows
|
||||
NombreCuenta = Row("NombreCuenta").ToString
|
||||
Cuenta = Row("Cuenta").ToString
|
||||
|
||||
Dim Importe As Decimal = If(Row("Cargo").ToString.ToDecimal <> 0, Row("Cargo").ToString.ToDecimal, Row("Abono").ToString.ToDecimal)
|
||||
Dim Concepto As String = Row("Concepto").ToString
|
||||
Dim Referencia As String = Row("Referencia").ToString
|
||||
Dim IdCuenta As Integer = -1
|
||||
Dim CveSubProyecto As Integer = Row("CveSubProyecto").ToString.ToEntero
|
||||
Dim TipoMovto As Boolean = If(Row("Cargo").ToString.ToDecimal <> 0, False, True)
|
||||
Dim YaExiste As Boolean = False
|
||||
Dim CveConceptoContable As Integer = Row("CveConceptoContable").ToString.ToEntero
|
||||
Dim IdCtaContable As Integer = Row("IdCtaContable").ToString.ToEntero
|
||||
|
||||
If ObjConta.GetCuentas(-1, Cuenta.Replace("-", ""), 1, UsuarioActual.CveEmpresa) Then
|
||||
If ObjConta.DtResultados.Rows.Count > 0 Then
|
||||
IdCuenta = ObjConta.DtResultados().FiltroPrimeroValor("CveCuenta").ToEntero
|
||||
'TipoCuenta = ObjConta.DtResultados().FiltroPrimeroValor("Tipo")
|
||||
Else
|
||||
Reporte.AppendLine($"{Cuenta} - {NombreCuenta} - {Importe} </br>")
|
||||
TieneError = True
|
||||
YaExiste = True
|
||||
End If
|
||||
Else
|
||||
TieneError = True
|
||||
Exit For
|
||||
End If
|
||||
|
||||
|
||||
'If TipoCuenta.Contiene("A", "C", "E") Then
|
||||
' CargoAbono = False
|
||||
'End If
|
||||
|
||||
If YaExiste = False Then
|
||||
If IdCuenta > 0 Then
|
||||
If Not ObjConta.AddPrePolizasDetalle(CvePrePoliza, TipoMovto, Importe, "", CveConceptoContable, Concepto, Now.ToDate, IdCtaContable, -1, ImporteMX:=Importe, CveSubProyecto:=CveSubProyecto, CveCuenta:=IdCuenta) Then
|
||||
TieneError = True
|
||||
Exit For
|
||||
End If
|
||||
Else
|
||||
IdCuenta = -2
|
||||
End If
|
||||
End If
|
||||
|
||||
|
||||
Next
|
||||
If Not TieneError Then
|
||||
ObjConta.Commit()
|
||||
Alert(String.Format("<br/><br/>Prepóliza: <b><a href = ""/Contabilidad/RegPrePoliza.aspx?CvePrePoliza={0}"" class=""texto"" target=""_blank"" >{1}</a></b>", CvePrePoliza, ObjConta.ContaFolio))
|
||||
|
||||
Alert($"Se agregó la póliza {ObjConta.ContaFolio}")
|
||||
|
||||
Else
|
||||
Alert($"No encontraron las siguientes cuentas: {Reporte.ToString} ", TipoMensaje.MsgError)
|
||||
ObjConta.RollBack()
|
||||
End If
|
||||
|
||||
|
||||
Catch ex As Exception
|
||||
ObjConta.RollBack()
|
||||
End Try
|
||||
|
||||
ObjConta.Dispose()
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Function ReadExcelFile(ByVal flatFilePath As String, ByVal firstRowHasHeaders As Boolean) As DataTable
|
||||
SpreadsheetInfo.SetLicense(ModExtensiones.SerialGemBoxSpreadsheet)
|
||||
Dim excelFile As ExcelFile = ExcelFile.Load(flatFilePath)
|
||||
|
||||
Dim unnamed As Integer = 0
|
||||
Dim cols As Integer
|
||||
Dim columns As String()
|
||||
Dim curRow As Integer = 0
|
||||
Dim curCol As Integer = 0
|
||||
Dim dataTable As DataTable = New DataTable()
|
||||
Dim worksheet As ExcelWorksheet = excelFile.Worksheets(0)
|
||||
|
||||
For cols = 0 To worksheet.Rows(0).AllocatedCells.Count - 1
|
||||
|
||||
If firstRowHasHeaders Then
|
||||
|
||||
If worksheet.Rows(0).Cells(cols).Value IsNot Nothing Then
|
||||
dataTable.Columns.Add(worksheet.Rows(0).Cells(cols).Value.ToString().Trim)
|
||||
Else
|
||||
dataTable.Columns.Add("Unnamed Column " & (System.Threading.Interlocked.Increment(unnamed)))
|
||||
End If
|
||||
|
||||
curRow = 1
|
||||
Else
|
||||
dataTable.Columns.Add("Column " & (cols + 1))
|
||||
End If
|
||||
|
||||
Next
|
||||
|
||||
While curRow < worksheet.Rows.Count
|
||||
|
||||
columns = New String(cols - 1) {}
|
||||
|
||||
For curCol = 0 To cols - 1
|
||||
|
||||
If worksheet.Rows(curRow).Cells(curCol).Value Is Nothing Then
|
||||
columns(curCol) = ""
|
||||
Else
|
||||
columns(curCol) = worksheet.Rows(curRow).Cells(curCol).Value.ToString()
|
||||
End If
|
||||
|
||||
Next
|
||||
|
||||
dataTable.Rows.Add(columns)
|
||||
curRow += 1
|
||||
|
||||
End While
|
||||
|
||||
Return dataTable
|
||||
|
||||
End Function
|
||||
End Class
|
||||
|
||||
|
||||
Reference in New Issue
Block a user