From 033ac21ce0d05a60b17d4f79ef2c51604d47cf9a Mon Sep 17 00:00:00 2001 From: rrene Date: Tue, 31 Oct 2023 13:14:21 -0700 Subject: [PATCH] -- mejora --- Ventas/FacturacionAcarreos.aspx | 10 +- Ventas/FacturacionAcarreos.aspx.vb | 193 ++++++++++++++++++----------- 2 files changed, 125 insertions(+), 78 deletions(-) diff --git a/Ventas/FacturacionAcarreos.aspx b/Ventas/FacturacionAcarreos.aspx index d071388a9..47151b845 100644 --- a/Ventas/FacturacionAcarreos.aspx +++ b/Ventas/FacturacionAcarreos.aspx @@ -19,6 +19,9 @@
  • Eliminar seleccionados
  • +
  • + Excel +
  • @@ -90,7 +93,7 @@
    + DataKeyNames="CveFacturacionAcarreo,CvePSGeneral,Cantidad,CveCliente,CveSubProyecto,CvePSRelacionado,Folio,FolioAdicional" CssClass="table table-striped table-advance table-hover Widthall ScrollAD mysmallbtn" data-sort="true" GridLines="None" Width="99%"> @@ -99,8 +102,7 @@ - - + @@ -108,6 +110,8 @@ + + diff --git a/Ventas/FacturacionAcarreos.aspx.vb b/Ventas/FacturacionAcarreos.aspx.vb index 014548d4b..c500fcf03 100644 --- a/Ventas/FacturacionAcarreos.aspx.vb +++ b/Ventas/FacturacionAcarreos.aspx.vb @@ -1,13 +1,15 @@ - -Option Explicit On +Option Explicit On Option Strict On -Imports System.Data +Imports System.Data Imports System.IO +Imports System.IO.Compression +Imports System.Net.Http Imports ADSTimbrado Imports Extensiones Imports GemBox.Spreadsheet + Partial Class Ventas_FacturacionAcarreos Inherits ClsPaginas @@ -15,6 +17,11 @@ Partial Class Ventas_FacturacionAcarreos If Not Page.IsPostBack Then CargaDatos() End If + If IfNull(Request("OP"), "").ToString = "File" Then + + DescargaArchivoDirecto(Request("File").ToString) + + End If End Sub Private Sub CargaDatos() @@ -41,17 +48,18 @@ Partial Class Ventas_FacturacionAcarreos .Add(New DataColumn("Sucursal", GetType(String))) .Add(New DataColumn("RFC", GetType(String))) .Add(New DataColumn("Cantidad", GetType(Decimal))) + .Add(New DataColumn("Folio", GetType(String))) .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("Chofer", GetType(String))) + '.Add(New DataColumn("Remolque1", GetType(String))) + '.Add(New DataColumn("Remolque2", GetType(String))) .Add(New DataColumn("Distancia", GetType(Decimal))) + .Add(New DataColumn("HojaServicio", GetType(String))) End With SpreadsheetInfo.SetLicense(ModExtensiones.SerialGemBoxSpreadsheet) @@ -119,15 +127,30 @@ Partial Class Ventas_FacturacionAcarreos Dim FechaLlegada As DateTime = MinFecha If row("FechaSalida").ToString().ToDateTime() > FechaSalida Then - FechaSalida = row("FechaSalida").ToString().ToDateTime() + FechaSalida = row("FechaSalida").ToString().ToDateTime().AddHours(9) End If If row("FechaLlegada").ToString().ToDateTime() > FechaLlegada Then - FechaLlegada = row("FechaLlegada").ToString().ToDateTime() - End If - + FechaLlegada = row("FechaLlegada").ToString().ToDateTime().AddHours(14) + End If + + Dim Chofer, Configuracion, Remolque1, Remolque2 As String + Dim dtu As DataRow = GlobalDTInvCamiones(UsuarioActual.CveEmpresa).FiltroPrimero($"Placa = '{row("Unidad")}'") + If dtu IsNot Nothing Then + 'Chofer = dtu("Chofer").ToString + 'Configuracion = dtu("Configuracion").ToString + Remolque1 = dtu("Remolque1").ToString + Remolque2 = dtu("Remolque2").ToString + Else + Chofer = "" : Configuracion = "" : Remolque1 = "" : Remolque2 = "" + End If + + 'temporal + Chofer = row("Folio").ToString + Configuracion = row("HojaServicio").ToString + 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) + row("Destino").ToString(), FechaSalida, FechaLlegada, row("Unidad").ToString(), Chofer, Configuracion, + Remolque1, Remolque2, row("Distancia").ToString().ToDecimal(), UsuarioActual.CveEmpresa) If msg = "Correcto" Then RowsCargados += 1 Else @@ -163,60 +186,8 @@ Partial Class Ventas_FacturacionAcarreos 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 + End Sub - 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) @@ -227,9 +198,6 @@ Partial Class Ventas_FacturacionAcarreos 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 @@ -238,10 +206,17 @@ Partial Class Ventas_FacturacionAcarreos End If Dim msg As New StringBuilder() + Dim msgDescarga As New StringBuilder() Dim NumFacturasGeneradas As Integer = 0 Dim NumFacturasTimbradas As Integer = 0 + Dim Archivo As String = "" + Dim NombreArchivo As String = String.Format("Acarreos{0}{1}.zip", NowADS().ToString("yyyyMMddhhmmss"), UsuarioActual.CveUsuario.ToString) + Dim ArchivoDestino As String = String.Format("{0}Temp/{1}", UsuarioActual.Empresa.Ruta, NombreArchivo) + Dim HuboInexistentes As Boolean = False + Dim archiveZip As ZipArchive = ZipFile.Open(ArchivoDestino, ZipArchiveMode.Create) + For Each GvRow As GridViewRow In GvDatos.Rows If GvRow.RowType = DataControlRowType.DataRow Then If GvRow.FCCheckBox("chk").Checked Then @@ -251,10 +226,10 @@ Partial Class Ventas_FacturacionAcarreos Dim CveMoneda As Integer = 1 Dim CvePS As Integer = GvRow.DataKey("CvePSGeneral").ToEntero() - Dim Cantidad As Decimal = GvRow.DataKey("Cantidad").ToEntero() + Dim Cantidad As Decimal = GvRow.DataKey("Cantidad").ToDecimal() Dim PU As Decimal = -1 Dim Descuento As Decimal = 0 - Dim Obs As String = "" + Dim Obs As String = $"DE ACARREO DE MINERAL DE HIERRO AQUILA SINALOA, MÉXICO, FOLIO: { GvRow.DataKey("Folio")} HOJA DE SERVICIO: {GvRow.DataKey("FolioAdicional")}" Dim CvePSMercancia As Integer = GvRow.DataKey("CvePSRelacionado").ToEntero() @@ -274,8 +249,19 @@ Partial Class Ventas_FacturacionAcarreos msg.AppendFormat("{0}
    ", m) Next - If resp.TieneError AndAlso resp.Mensajes.Count = 0 Then - msg.Append("Ocurrió un error al generar la factura.
    ") + If Not resp.TieneError Then + If resp.SerieFolio <> String.Empty Then + msgDescarga.AppendFormat("{0},", resp.SerieFolio) + DescargarFactura(resp.CveVenta, UsuarioActual.CveEmpresa, UsuarioActual.CveUsuario) + Archivo = String.Format("{0}PDF/ING/{1}.pdf", UsuarioActual.Empresa.Ruta.Replace("\", "/"), resp.SerieFolio) + If File.Exists(Archivo) Then + archiveZip.CreateEntryFromFile(Archivo, resp.SerieFolio & ".pdf") + End If + Archivo = String.Format("{0}XML/ING/{1}.xml", UsuarioActual.Empresa.Ruta.Replace("\", "/"), resp.SerieFolio) + If File.Exists(Archivo) Then + archiveZip.CreateEntryFromFile(Archivo, resp.SerieFolio & ".xml") + End If + End If End If If resp.CveVenta > 0 AndAlso Not resp.TieneError Then @@ -287,9 +273,14 @@ Partial Class Ventas_FacturacionAcarreos End If End If Next - + archiveZip.Dispose() If NumFacturasGeneradas > 0 Then - Alert($"Se generaron {NumFacturasGeneradas} y se timbraron {NumFacturasTimbradas} facturas.
    " + msg.ToString(), TipoMensaje.MsgInformacion) + Dim stt As String = $"Se generaron {NumFacturasGeneradas} y se timbraron {NumFacturasTimbradas} facturas.
    " + If msgDescarga.Length > 0 Then + stt += String.Format("Descarga zip", NombreArchivo) + End If + Alert(stt, TipoMensaje.MsgInformacion) + Else Alert($"No se pudieron generar facturas.
    " + msg.ToString(), TipoMensaje.MsgError) End If @@ -367,4 +358,56 @@ Partial Class Ventas_FacturacionAcarreos Protected Sub BtnConsultar_Click(sender As Object, e As EventArgs) CargaDatos() End Sub + + Private Sub ImgExcel_Click(sender As Object, e As EventArgs) Handles ImgExcel.Click + GvDatos.Columns(0).Visible = False + GvDatos.ExportToExcel("Acarreos_" & NowADS().ToShortDateString & ".xls") + GvDatos.Columns(0).Visible = True + End Sub + Private Function DescargarFactura(CveVenta As Integer, CveEmpresa As Integer, CveUsuarioCreo As Integer, Optional GetBytes As Boolean = True, Optional Regenerar As Boolean = False, Optional strURLHOST As String = "") As Byte() + + If String.IsNullOrEmpty(strURLHOST) Then + strURLHOST = String.Format("{0}://{1}:{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Port) + End If + + Dim Url As New Uri(String.Format("{0}/Compras/PopUpVerFactura.aspx?P={1}", strURLHOST, Encripta(String.Format("OP=VTA&CVEVENTA={0}&PAG=1&GUARDAR=1®ENERAR={1}", CveVenta, If(Regenerar, "1", "0"))))) + Dim File As Byte() = Nothing + Try + Using httpClient As New HttpClient() + httpClient.Timeout = New TimeSpan(0, 2, 0) + Using request As New HttpRequestMessage(HttpMethod.Get, Url) + request.Headers.Add("ADSToken", Encripta(String.Format("CVEEMPRESA={0}&TICKS={1}&CveUsuario={2}", CveEmpresa, DateTime.UtcNow.Ticks, CveUsuarioCreo))) + Dim response As HttpResponseMessage = httpClient.SendAsync(request).Result + If GetBytes AndAlso response.StatusCode = Net.HttpStatusCode.OK Then + File = response.Content.ReadAsByteArrayAsync().Result + End If + End Using + End Using + Catch ex As Exception + File = Nothing + OnError("Ocurrió un error al descargar la factura", ex) + End Try + + Return File + End Function + + Private Sub DescargaArchivoDirecto(NombreArchivo As String) + If NombreArchivo <> "" Then + + NombreArchivo = String.Format("{0}Temp\{1}", UsuarioActual.Empresa.Ruta, NombreArchivo) + Dim Archivo As New System.IO.FileInfo(NombreArchivo) + + If Archivo.Exists Then + HttpContext.Current.Response.Clear() + HttpContext.Current.Response.ClearContent() + HttpContext.Current.Response.ContentType = "application/zip" + HttpContext.Current.Response.AppendHeader("Content-disposition", String.Format("attachment; filename={0}", Archivo.Name)) + HttpContext.Current.Response.TransmitFile(NombreArchivo) + HttpContext.Current.Response.Flush() + HttpContext.Current.Response.End() + End If + Archivo = Nothing + End If + End Sub + End Class