370 lines
15 KiB
VB.net
370 lines
15 KiB
VB.net
|
|
Option Explicit On
|
|
Option Strict On
|
|
|
|
Imports System.Data
|
|
Imports System.IO
|
|
Imports Extensiones
|
|
Imports GemBox.Spreadsheet
|
|
|
|
Partial Class Ventas_FacturacionAcarreos
|
|
Inherits ClsPaginas
|
|
|
|
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
|
|
If Not Page.IsPostBack Then
|
|
CargaDatos()
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub CargaDatos()
|
|
Dim CveCliente As Integer = -1
|
|
If DdlClientes.SelectedValue.ToEntero() > 0 Then
|
|
CveCliente = DdlClientes.SelectedValue.ToEntero()
|
|
End If
|
|
Using ObjVentas As New ClsVentas
|
|
If ObjVentas.GetFacturacionAcarreos(-1, CveCliente, -1, DdlEstatus.SelectedValue.ToEntero(), UsuarioActual.CveEmpresa) Then
|
|
GvDatos.DataSource = ObjVentas.DtResultados
|
|
End If
|
|
End Using
|
|
GvDatos.DataBind()
|
|
|
|
UPDatos.Update()
|
|
End Sub
|
|
|
|
|
|
#Region "C A R G A R E X C E L "
|
|
Private Sub BtnDescargarFormato_Click(sender As Object, e As EventArgs) Handles BtnDescargarFormato.Click
|
|
|
|
Dim DtExcel As DataTable = New DataTable("Facturas")
|
|
With DtExcel.Columns
|
|
.Add(New DataColumn("Sucursal", GetType(String)))
|
|
.Add(New DataColumn("RFC", GetType(String)))
|
|
.Add(New DataColumn("Cantidad", GetType(Decimal)))
|
|
.Add(New DataColumn("ProductoGeneral", GetType(String)))
|
|
.Add(New DataColumn("Origen", GetType(String)))
|
|
.Add(New DataColumn("Destino", GetType(String)))
|
|
.Add(New DataColumn("FechaSalida", GetType(DateTime)))
|
|
.Add(New DataColumn("FechaLlegada", GetType(DateTime)))
|
|
.Add(New DataColumn("Unidad", GetType(String)))
|
|
.Add(New DataColumn("Chofer", GetType(String)))
|
|
.Add(New DataColumn("Configuración", GetType(String)))
|
|
.Add(New DataColumn("Remolque1", GetType(String)))
|
|
.Add(New DataColumn("Remolque2", GetType(String)))
|
|
.Add(New DataColumn("Distancia", GetType(Decimal)))
|
|
End With
|
|
|
|
SpreadsheetInfo.SetLicense(ModExtensiones.SerialGemBoxSpreadsheet)
|
|
|
|
Dim FileExcel As New ExcelFile()
|
|
|
|
Dim worksheet = FileExcel.Worksheets.Add(DtExcel.TableName)
|
|
worksheet.InsertDataTable(DtExcel, New InsertDataTableOptions() With
|
|
{
|
|
.ColumnHeaders = True,
|
|
.StartRow = 0,
|
|
.StartColumn = 0
|
|
})
|
|
worksheet.Rows(0).Style.FillPattern.SetPattern(FillPatternStyle.Solid, SpreadsheetColor.FromArgb(75, 119, 190), SpreadsheetColor.FromArgb(255, 255, 255))
|
|
worksheet.Rows(0).Style.Font.Color = SpreadsheetColor.FromArgb(255, 255, 255)
|
|
|
|
Dim columnCount = worksheet.CalculateMaxUsedColumns()
|
|
For i = 0 To columnCount
|
|
worksheet.Columns(i).AutoFit(1, worksheet.Rows(0), worksheet.Rows(worksheet.Rows.Count - 1))
|
|
Next
|
|
|
|
FileExcel.Save(Response, "FormatoFacturas.xlsx", SaveOptions.XlsxDefault)
|
|
|
|
End Sub
|
|
Private 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 = AppDomain.CurrentDomain.BaseDirectory & file.path
|
|
CargarArchivo(Directorio, file.filename)
|
|
Else
|
|
Alert("No existe el archivo. Favor de subir un archivo excel a la plataforma.", TipoMensaje.MsgError)
|
|
End If
|
|
End Sub
|
|
Private Sub CargarArchivo(Ruta As String, FileName As String)
|
|
SpreadsheetInfo.SetLicense(ModExtensiones.SerialGemBoxSpreadsheet)
|
|
Dim dt As DataTable = Nothing
|
|
Dim ErrorArchivo = False
|
|
|
|
Try
|
|
dt = ReadExcelFile(Ruta + FileName, True)
|
|
Catch ex As Exception
|
|
ErrorArchivo = True
|
|
OnError("Ocurrio un error al leer el archivo <br/>" & ex.Message, ex)
|
|
End Try
|
|
If Not ErrorArchivo AndAlso dt IsNot Nothing Then
|
|
|
|
dt.TableName = "Datos"
|
|
Dim MsgFacturacionAcarreos As New StringBuilder()
|
|
Dim RowsCargados As Integer = 0
|
|
Dim RowsNoCargados As Integer = 0
|
|
|
|
Dim ObjVentas As New ClsVentas
|
|
ObjVentas.IniciaTransacSQL()
|
|
Dim TieneError As Boolean = False
|
|
Try
|
|
If dt.Rows.Count = 0 Then
|
|
TieneError = True
|
|
MsgFacturacionAcarreos.Append("No hay datos para procesar")
|
|
RowsNoCargados += 1
|
|
End If
|
|
|
|
For Each row As DataRow In dt.Rows
|
|
Dim FechaSalida As DateTime = MinFecha
|
|
Dim FechaLlegada As DateTime = MinFecha
|
|
|
|
If row("FechaSalida").ToString().ToDateTime() > FechaSalida Then
|
|
FechaSalida = row("FechaSalida").ToString().ToDateTime()
|
|
End If
|
|
If row("FechaLlegada").ToString().ToDateTime() > FechaLlegada Then
|
|
FechaLlegada = row("FechaLlegada").ToString().ToDateTime()
|
|
End If
|
|
|
|
Dim msg As String = ObjVentas.AddFacturacionAcarreos(row("Sucursal").ToString(), row("RFC").ToString(), row("Cantidad").ToString().ToDecimal(), row("ProductoGeneral").ToString(), row("Origen").ToString(),
|
|
row("Destino").ToString(), FechaSalida, FechaLlegada, row("Unidad").ToString(), row("Chofer").ToString(), row("Configuración").ToString(),
|
|
row("Remolque1").ToString(), row("Remolque2").ToString(), row("Distancia").ToString().ToDecimal(), UsuarioActual.CveEmpresa)
|
|
If msg = "Correcto" Then
|
|
RowsCargados += 1
|
|
Else
|
|
TieneError = True
|
|
MsgFacturacionAcarreos.AppendFormat("Ocurrió un error al procesar la fila: {0}<br/>{1}", row("NoExcel"), msg)
|
|
RowsNoCargados += 1
|
|
Exit For
|
|
End If
|
|
Next
|
|
Catch ex As Exception
|
|
OnError("Ocurrió un error al cargar los datos.", ex)
|
|
Finally
|
|
Dim MensajeFinal As New StringBuilder()
|
|
If RowsNoCargados = 0 AndAlso RowsCargados > 0 Then
|
|
MensajeFinal.Append("Los datos se cargaron correctamente. <br/><br/>")
|
|
End If
|
|
|
|
If RowsNoCargados > 0 Then
|
|
MensajeFinal.Append(MsgFacturacionAcarreos.ToString())
|
|
End If
|
|
|
|
If TieneError Then
|
|
ObjVentas.RollBack()
|
|
Alert(MensajeFinal.ToString(), TipoMensaje.MsgError)
|
|
Else
|
|
ObjVentas.Commit()
|
|
Alert(MensajeFinal.ToString())
|
|
End If
|
|
ObjVentas.Dispose()
|
|
|
|
End Try
|
|
|
|
CargaDatos()
|
|
|
|
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())
|
|
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
|
|
|
|
dataTable.Columns.Add("NoExcel", GetType(String))
|
|
|
|
While curRow < worksheet.Rows.Count
|
|
columns = New String(cols) {}
|
|
|
|
For curCol = 0 To cols
|
|
|
|
If curCol = cols Then
|
|
columns(curCol) = worksheet.Rows(curRow).Name
|
|
Else
|
|
If worksheet.Rows(curRow).Cells(curCol).Value Is Nothing Then
|
|
columns(curCol) = ""
|
|
Else
|
|
columns(curCol) = worksheet.Rows(curRow).Cells(curCol).Value.ToString()
|
|
End If
|
|
End If
|
|
Next
|
|
|
|
dataTable.Rows.Add(columns)
|
|
curRow += 1
|
|
End While
|
|
|
|
Return dataTable
|
|
End Function
|
|
#End Region
|
|
|
|
Protected Sub GvDatos_RowDataBound(sender As Object, e As GridViewRowEventArgs)
|
|
If e.Row.RowType = DataControlRowType.DataRow Then
|
|
If e.Row.DBCampo("SerieFolio").Length > 0 Then
|
|
e.Row.FCCheckBox("chk").Visible = False
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
Protected Sub LnkFacturarSeleccionados_Click(sender As Object, e As EventArgs)
|
|
|
|
If GvDatos.Rows.Cast(Of GridViewRow)().Count(Function(gvr) gvr.FCCheckBox("chk").Checked) <= 0 Then
|
|
Alert($"Debe seleccionar un acarreo para facturar", TipoMensaje.MsgAdvertencia)
|
|
Exit Sub
|
|
End If
|
|
|
|
Dim msg As New StringBuilder()
|
|
|
|
Dim NumFacturasGeneradas As Integer = 0
|
|
Dim NumFacturasTimbradas As Integer = 0
|
|
|
|
For Each GvRow As GridViewRow In GvDatos.Rows
|
|
If GvRow.RowType = DataControlRowType.DataRow Then
|
|
If GvRow.FCCheckBox("chk").Checked Then
|
|
|
|
Dim CveCliente As Integer = GvRow.DataKey("CveCliente").ToEntero()
|
|
Dim CveSubProyecto As Integer = GvRow.DataKey("CveSubProyecto").ToEntero()
|
|
Dim CveMoneda As Integer = 1
|
|
|
|
Dim CvePS As Integer = GvRow.DataKey("CvePSGeneral").ToEntero()
|
|
Dim Cantidad As Decimal = GvRow.DataKey("Cantidad").ToEntero()
|
|
Dim PU As Decimal = -1
|
|
Dim Descuento As Decimal = 0
|
|
Dim Obs As String = ""
|
|
|
|
Dim CvePSMercancia As Integer = GvRow.DataKey("CvePSRelacionado").ToEntero()
|
|
|
|
Dim ObjFactura = Modelos.Factura.CrearVentaNueva(UsuarioActual, CveSubProyecto, CveCliente, CveMoneda)
|
|
ObjFactura.EmitirCartaPorte = True
|
|
ObjFactura.CveFacturacionAcarreo = GvRow.DataKey("CveFacturacionAcarreo").ToEntero()
|
|
|
|
ObjFactura.AgregarPS(CvePS, Cantidad, PU, Descuento, Obs)
|
|
|
|
ObjFactura.DtMercancias = CreaDTMercancias(CvePSMercancia, Cantidad)
|
|
ObjFactura.CalculaTotales()
|
|
ObjFactura.PreparaTimbre()
|
|
|
|
Dim resp = ObjFactura.EmitirFactura(Timbrar:=True, Modulo:="FacturacionAcarreos")
|
|
|
|
For Each m As String In resp.Mensajes
|
|
msg.AppendFormat("{0}<br/>", m)
|
|
Next
|
|
|
|
If resp.TieneError AndAlso resp.Mensajes.Count = 0 Then
|
|
msg.Append("Ocurrió un error al generar la factura.<br/>")
|
|
End If
|
|
|
|
If resp.CveVenta > 0 AndAlso Not resp.TieneError Then
|
|
NumFacturasGeneradas += 1
|
|
If resp.SeTimbro Then
|
|
NumFacturasTimbradas += 1
|
|
End If
|
|
End If
|
|
End If
|
|
End If
|
|
Next
|
|
|
|
If NumFacturasGeneradas > 0 Then
|
|
Alert($"Se generaron {NumFacturasGeneradas} y se timbraron {NumFacturasTimbradas} facturas. <br/>" + msg.ToString(), TipoMensaje.MsgInformacion)
|
|
Else
|
|
Alert($"No se pudieron generar facturas. <br/>" + msg.ToString(), TipoMensaje.MsgError)
|
|
End If
|
|
|
|
CargaDatos()
|
|
End Sub
|
|
|
|
Protected Sub LnkEliminarSeleccionados_Click(sender As Object, e As EventArgs)
|
|
|
|
If GvDatos.Rows.Cast(Of GridViewRow)().Count(Function(gvr) gvr.FCCheckBox("chk").Checked) <= 0 Then
|
|
Alert($"Debe seleccionar un acarreo para eliminar", TipoMensaje.MsgAdvertencia)
|
|
Exit Sub
|
|
End If
|
|
|
|
Dim msg As New StringBuilder()
|
|
Dim TieneError As Boolean = False
|
|
Dim ObjVentas As New ClsVentas
|
|
ObjVentas.IniciaTransacSQL()
|
|
|
|
Try
|
|
For Each GvRow As GridViewRow In GvDatos.Rows
|
|
If GvRow.RowType = DataControlRowType.DataRow Then
|
|
If GvRow.FCCheckBox("chk").Checked Then
|
|
|
|
If Not ObjVentas.RemoveFacturacionAcarreo(GvRow.DataKey("CveFacturacionAcarreo").ToEntero(), UsuarioActual.CveEmpresa) Then
|
|
TieneError = True
|
|
Exit For
|
|
End If
|
|
|
|
End If
|
|
End If
|
|
Next
|
|
Catch ex As Exception
|
|
TieneError = True
|
|
End Try
|
|
|
|
If TieneError Then
|
|
ObjVentas.RollBack()
|
|
Alert("Ocurrió un error al eliminar los registros.", TipoMensaje.MsgError)
|
|
Else
|
|
ObjVentas.Commit()
|
|
Alert("Los registros se eliminaron correctamente.", TipoMensaje.MsgInformacion)
|
|
End If
|
|
ObjVentas.Dispose()
|
|
|
|
CargaDatos()
|
|
End Sub
|
|
|
|
Private Function CreaDTMercancias(CvePS As Integer, Cantidad As Decimal) As DataTable
|
|
Dim DtMercancias = New DataTable()
|
|
DtMercancias.Columns.Add(New DataColumn("Consec", GetType(Integer)))
|
|
DtMercancias.Columns.Add(New DataColumn("Cantidad", GetType(Decimal)))
|
|
DtMercancias.Columns.Add(New DataColumn("CvePS", GetType(Integer)))
|
|
DtMercancias.Columns.Add(New DataColumn("CveUnidadMedida", GetType(Integer)))
|
|
DtMercancias.Columns.Add(New DataColumn("CodigoPS", GetType(String)))
|
|
|
|
Dim DrPS As DataRow = GlobalDTPSSimplificado(CveEmpresa:=UsuarioActual.CveEmpresa, CvePS:=CvePS).FiltroPrimero("")
|
|
If DrPS Is Nothing Then
|
|
Alert("No se encontró la mercancía", TipoMensaje.MsgAdvertencia)
|
|
Return Nothing
|
|
End If
|
|
|
|
Dim NewRow As DataRow = DtMercancias.NewRow
|
|
NewRow("Consec") = DtMercancias.MAX("Consec").ToEntero() + 1
|
|
NewRow("Cantidad") = Cantidad
|
|
NewRow("CvePS") = DrPS("CvePS")
|
|
NewRow("CveUnidadMedida") = DrPS("CveUnidadMedida")
|
|
NewRow("CodigoPS") = DrPS("PSCodigo")
|
|
|
|
DtMercancias.Rows.Add(NewRow)
|
|
|
|
Return DtMercancias
|
|
End Function
|
|
|
|
Protected Sub BtnConsultar_Click(sender As Object, e As EventArgs)
|
|
CargaDatos()
|
|
End Sub
|
|
End Class
|