Files
SIG/Contratos/RepContratosObraAvancesDetalle.aspx.vb
2020-11-13 19:02:03 -07:00

436 lines
20 KiB
VB.net

Imports Extensiones
Imports System.Data
Imports System.IO
Imports ClosedXML.Excel
Partial Class RepContratosObraAvancesDetalle
Inherits ClsPaginas
Property CveContrato As Integer
Get
If ViewState("CveContrato") Is Nothing Then
Return -1
Else
Return ViewState("CveContrato").ToString.ToEntero
End If
End Get
Set(value As Integer)
ViewState("CveContrato") = value
End Set
End Property
Property NumeroContrato As String
Get
If ViewState("NumeroContrato") Is Nothing Then
Return ""
Else
Return ViewState("NumeroContrato").ToString
End If
End Get
Set(value As String)
ViewState("NumeroContrato") = value
End Set
End Property
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
CveContrato = If(Request("CveContrato") IsNot Nothing AndAlso Request("CveContrato").ToString.ToEntero > 0, Request("CveContrato").ToString.ToEntero, -1)
NumeroContrato = If(Request("NumeroContrato") IsNot Nothing AndAlso Request("NumeroContrato").ToString.Length > 0, $"{Request("NumeroContrato").ToString}", "")
LblNumeroContrato.Text = If(Request("NumeroContrato") IsNot Nothing AndAlso Request("NumeroContrato").ToString.Length > 0, $"Estimaciones del Contrato: {Request("NumeroContrato").ToString}", "")
If CveContrato > 0 Then
CargaDatos()
End If
End If
End Sub
Private Sub CargaDatos()
Dim Con As New ClsContratos
Dim DTDatos As DataTable = New DataTable
Dim DTContrato As DataTable = New DataTable
If Con.GetContratosObraRepAvancesDetalle(CveContrato, UsuarioActual.CveEmpresa) Then
GVDatos.DataSource = Con.DtResultados
DTDatos = Con.DtResultados
If Con.DtResultados.Rows.Count = 0 Then
Alert("No se encontraron estimaciones.", TipoMensaje.MsgAdvertencia)
End If
Else
GVDatos.DataSource = Nothing
End If
If Con.GetContratosObraRepAvances(MinFecha, MaxFecha, -1, CveContrato, UsuarioActual.CveEmpresa) Then
DTContrato = Con.DtResultados
If DTContrato.Rows.Count > 0 Then
Indicadores(DTContrato, DTDatos.Rows.Count)
End If
End If
Con.Dispose()
GVDatos.DataBind()
UpDatos.Update()
End Sub
Private Sub Indicadores(DTDatos As DataTable, NumeroEstimaciones As Integer)
LtlGrafNumeroEstimaciones.Text = NumeroEstimaciones
Dim AvanceImporte As Decimal = DTDatos(0)("AvanceImporte").ToString.ToDecimal
Dim TotalContratos As Decimal = DTDatos(0)("Importe").ToString.ToDecimal
Dim TotalAvancePorcentaje As Decimal = (AvanceImporte * 100) / TotalContratos
Dim TotalFaltantePorcentaje As Decimal = 100 - TotalAvancePorcentaje
Dim FechaFinal As Date = DTDatos(0)("FechaFinal").ToString.ToDate
Dim Dias As Integer = (FechaFinal - Now).Days
Dias = If(Dias < 0, 0, Dias)
LtlAvanceImporte.Text = $"$ {AvanceImporte.ToString("###,###,###.00")}"
LtlAvanceImporteProcentaje1.Text = $"{TotalAvancePorcentaje.ToString("###.00")} %"
LtlAvanceImporteProcentaje2.Text = $"{TotalAvancePorcentaje.ToString("###.00")}%"
SPAvance1.Attributes.Add("style", $"width:{TotalAvancePorcentaje.ToString.ToEntero}%")
LtlFechaFin.Text = FechaFinal.ToString("dd/MM/yyyy")
LtlDiasRestantes.Text = Dias.ToString("###,###")
UpGrafica.Update()
End Sub
Protected Sub GV_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GVDatos.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim HplNumeroEstimacion As HyperLink = e.Row.FCHyperLink("HplNumeroEstimacion")
HplNumeroEstimacion.NavigateUrl = $"./RepContratosObraAvancesPartida.aspx?CveEstimacion={e.Row.DataKey("CveEstimacion")}&NumeroEstimacion={ e.Row.DataKey("NumeroEstimacion")}&CveContrato={ CveContrato }&NumeroContrato={NumeroContrato}"
Dim LnkAnexo As LinkButton = e.Row.FCLinkButton("LnkAnexo")
ScriptManager.GetCurrent(Me).RegisterPostBackControl(LnkAnexo)
' NavigateUrl='<%# Eval("CveEstimacion", "RepContratosObraAvancesPartida.aspx?CveEstimacion={0}")%>'
End If
End Sub
Protected Sub LnkAnexo_Click(sender As Object, e As EventArgs)
Dim GvRow As GridViewRow = DirectCast((DirectCast(sender, LinkButton)).NamingContainer, GridViewRow)
Dim CveEstimacion As Integer = GvRow.DataKey("CveEstimacion").ToEntero
Dim NumeroEstimacion As Integer = GvRow.DataKey("NumeroEstimacion").ToEntero
Dim FechaEstimacion As Date = GvRow.DataKey("FechaEstimacion").ToDate
Dim NumeroContrato As String = GvRow.DataKey("NumeroContrato")
If CveEstimacion > 0 Then
GeneraAnexo(CveEstimacion, NumeroEstimacion, FechaEstimacion, NumeroContrato)
End If
End Sub
Private Sub GeneraAnexo(CveEstimacion As Integer, NumeroEstimacion As Integer, FechaEstimacion As Date, NumeroContrato As String)
Dim PlantillaAnexo As String = $"{UsuarioActual.Empresa.Ruta}\Formatos\Anexo.xlsx"
If Not System.IO.File.Exists(PlantillaAnexo) Then
Alert("No se encuentra la platilla de anexos.", TipoMensaje.MsgAdvertencia)
Exit Sub
End If
Dim DtControl As New DataTable
Dim ObjContratos As New ClsContratos
Dim Lugar As String = ""
Dim DescripcionContrato As String = ""
If ObjContratos.GetContratosObraAvancesControlAcumulativo(CveContrato, CveEstimacion, UsuarioActual.CveEmpresa) Then
DtControl = ObjContratos.DtResultados
If DtControl.Rows.Count > 0 Then
Lugar = DtControl(0)("Lugar").ToString
DescripcionContrato = DtControl(0)("DescripcionContrato").ToString
End If
End If
ObjContratos.Dispose()
If DtControl.Rows.Count > 0 Then
Dim WBAnexo = New ClosedXML.Excel.XLWorkbook(PlantillaAnexo)
Dim WBAnexoSalvar = New ClosedXML.Excel.XLWorkbook
Dim WSControl = WBAnexo.Worksheet(1)
Dim WSDetalle = WBAnexo.Worksheet(2)
Dim CuerrentRow As Integer = 28
WSControl.Cell(17, 2).Value = NumeroEstimacion 'Estimacion
WSControl.Cell(20, 9).Value = FechaEstimacion.ToString("dd/MM/yyyy") 'Fecha estimacion
WSControl.Cell(19, 8).Value = "$ " & DtControl.SUM("Importe").ToString("###,###,###.00") 'Total
WSControl.Cell(19, 8).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right)
WSControl.Cell(20, 6).Value = NumeroContrato
WSControl.Cell(18, 2).Value = Lugar
WSControl.Cell(18, 2).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left)
WSControl.Cell(18, 2).Style.Alignment.SetVertical(XLAlignmentVerticalValues.Top)
WSControl.Cell(17, 6).Value = "" 'Periodo Fecha Inicial
WSControl.Cell(17, 7).Value = "" 'Periuodo Fecha Final
WSControl.Cell(20, 2).Value = DescripcionContrato
WSControl.Cell(20, 2).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left)
WSControl.Cell(20, 2).Style.Alignment.SetVertical(XLAlignmentVerticalValues.Top)
WSControl.Cell(22, 7).Value = "" 'Presupuesto segun contrato
WSControl.Cell(19, 5).Value = "" 'Contrtatista
Dim RowCountControl As Integer = 1
For Each Drow As DataRow In DtControl.Rows
WSControl.Cell(CuerrentRow, 1).Value = Drow("Codigo").ToString
WSControl.Cell(CuerrentRow, 2).Value = Drow("PS").ToString
WSControl.Cell(CuerrentRow, 3).Value = Drow("Abreviatura").ToString
WSControl.Cell(CuerrentRow, 4).Value = Drow("Cantidad").ToString.ToDecimal
WSControl.Cell(CuerrentRow, 5).Value = Drow("AcumuladoTotal").ToString.ToDecimal
WSControl.Cell(CuerrentRow, 6).Value = Drow("AcumuladoAnterior").ToString.ToDecimal
WSControl.Cell(CuerrentRow, 7).Value = Drow("CantidadActual").ToString.ToDecimal
WSControl.Cell(CuerrentRow, 8).Value = Drow("PU").ToString.ToDecimal
WSControl.Cell(CuerrentRow, 9).Value = Drow("Total").ToString.ToDecimal
WSControl.Cell(CuerrentRow, 9).Style.NumberFormat.Format = "$ ###,###,###.00"
WSControl.Cell(CuerrentRow, 1).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 2).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 3).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 4).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 5).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 6).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 7).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 8).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 9).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 1).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 2).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 3).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 4).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 5).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 6).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 7).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 8).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow, 9).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Medium
'ultimo Row
If DtControl.Rows.Count = RowCountControl Then
WSControl.Cell(CuerrentRow + 1, 1).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 2).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 3).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 4).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 5).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 6).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 7).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 8).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 9).Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 1).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 2).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 3).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 4).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 5).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 6).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 7).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 8).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
WSControl.Cell(CuerrentRow + 1, 9).Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Medium
WSControl.Cell(CuerrentRow + 1, 8).Value = "Total:"
WSControl.Cell(CuerrentRow + 1, 8).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Right)
WSControl.Cell(CuerrentRow + 1, 8).Style.Font.Bold = True
WSControl.Cell(CuerrentRow + 1, 9).Value = DtControl.SUM("Total")
WSControl.Cell(CuerrentRow + 1, 9).Style.Font.Bold = True
WSControl.Cell(CuerrentRow + 1, 9).Style.NumberFormat.Format = "$ ###,###,###.00"
Dim range = WSControl.Range($"A{CuerrentRow + 3}:D{CuerrentRow + 3}")
range.Merge()
range.Style.Border.TopBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
range = WSControl.Range($"E{CuerrentRow + 3}:I{CuerrentRow + 3}")
range.Merge()
range.Style.Border.TopBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Medium
range.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
range = WSControl.Range($"A{CuerrentRow + 4}:D{CuerrentRow + 4}")
range.Merge()
range.Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
range.Value = "ING. WALTER ROBERTO VILLEGAS LARES"
range = WSControl.Range($"E{CuerrentRow + 4}:I{CuerrentRow + 4}")
range.Merge()
range.Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Medium
range.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
range.Value = "C. SALVADOR SUAREZ"
range = WSControl.Range($"A{CuerrentRow + 5}:D{CuerrentRow + 5}")
range.Merge()
range.Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
range.Value = "SERVICIOS ELECTRICOS Y CONTROL DE HERMOSILLO S.A. DE C.V."
range.Style.Font.Bold = True
range = WSControl.Range($"E{CuerrentRow + 5}:I{CuerrentRow + 5}")
range.Merge()
range.Style.Border.BottomBorder = ClosedXML.Excel.XLBorderStyleValues.Thin
range.Style.Border.RightBorder = ClosedXML.Excel.XLBorderStyleValues.Medium
range.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
range.Value = "GASERVICIO MATAMOROS SA DE CV"
range.Style.Font.Bold = True
End If
CuerrentRow = CuerrentRow + 1
RowCountControl = RowCountControl + 1
Next
WBAnexo.Worksheet(1).CopyTo(WBAnexoSalvar, "CONTROL")
For Each Drow As DataRow In DtControl.Rows
If Drow("CantidadActual").ToString.ToDecimal > 0 Then
WSDetalle.Cell(3, 6).Value = NumeroContrato
WSDetalle.Cell(4, 9).Value = "" 'Fecha de
WSDetalle.Cell(4, 10).Value = "" 'Fecha a
WSDetalle.Cell(4, 11).Value = NumeroEstimacion
WSDetalle.Cell(6, 6).Value = DescripcionContrato
WSDetalle.Cell(14, 2).Value = Drow("Codigo").ToString
WSDetalle.Cell(14, 3).Value = Drow("PS").ToString
WSDetalle.Cell(14, 4).Value = Drow("Abreviatura").ToString
WSDetalle.Cell(12, 3).Value = "" 'Concepto
WSDetalle.Cell(14, 10).Value = Drow("CantidadActual").ToString.ToDecimal
WSDetalle.Cell(14, 11).Value = Drow("CantidadActual").ToString.ToDecimal
'WSDetalle.Cell(61, 6).Value = "" 'Empresario
WSDetalle.Cell(61, 9).Value = Drow("Nombre").ToString 'Cliente
Dim objContrato As New ClsContratos
Dim DTArchivos As New DataTable
If objContrato.GetContratosAvanceAnexos(Drow("CveContratoDetalle").ToString.ToEntero, Drow("Fecha").ToString, UsuarioActual.CveUsuario, CveEstimacion:=CveEstimacion) Then
DTArchivos = objContrato.DtResultados
End If
objContrato.Dispose()
Dim Imagenes As New List(Of ClosedXML.Excel.Drawings.IXLPicture)
Dim CurrectRowImg As Integer = 19 'Row 19 primera imggen
Dim RowH As Double = WSDetalle.Row(19).Height
For Each drowArchivos In DTArchivos.Rows
Dim Archivo As String = drowArchivos("Archivo").ToString
Dim RutaArchivo As String = $"{UsuarioActual.Empresa.Ruta}\Contrato\{CveContrato}\Anexos\{Archivo}"
If File.Exists(RutaArchivo) Then
Dim Imagen = WSDetalle.AddPicture(RutaArchivo).MoveTo(WSDetalle.Cell(CurrectRowImg, 3))
Imagen.Scale(WSDetalle.Column(3).Width / (Imagen.OriginalWidth * 0.145D))
Dim ImgWidth As Double = Imagen.Height
Dim nRowmas As Integer = (ImgWidth / RowH).ToEntero
CurrectRowImg = CurrectRowImg + nRowmas
Imagenes.Add(Imagen)
End If
Next
WBAnexo.Worksheet(2).CopyTo(WBAnexoSalvar, If(Drow("Codigo").ToString.Trim.Length > 0, Drow("Codigo").ToString, Drow("CvePS").ToString))
For Each img As ClosedXML.Excel.Drawings.IXLPicture In Imagenes
img.Delete()
Next
End If
Next
Dim msB = New MemoryStream()
WBAnexoSalvar.SaveAs(msB)
Dim BB As Byte() = msB.ToArray
msB.Dispose()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment; filename=Anexo_Estimacion" & NumeroEstimacion & ".xlsx")
Response.ContentType = "application/vnd.ms-excel"
Response.BinaryWrite(BB)
Response.End()
Else
Alert("No se encontraron datos des control", TipoMensaje.MsgAdvertencia)
End If
End Sub
End Class