3 Aplicación de anticipo sobre una factura ya registrada, que genere NC

This commit is contained in:
2025-01-31 16:33:03 -07:00
parent 4fcbc0b6c3
commit 09d09eb6db

View File

@@ -5703,6 +5703,7 @@ Partial Class Ventas
VentaActual.PorcentajeIVA = DbRowVenta("PorcentajeIVA").ToString.ToDecimal
VentaActual.UsuarioActual = UsuarioActual
VentaActual.CveSubProyecto = DbRowVenta("CveSubProyecto").ToString.ToEntero
VentaActual.CveUsoCFDI = DbRowVenta("CveUsoCFDI").ToString.ToEntero
HdnAmortizacion.Value = LblAmort.Text.ToDouble.ToString()
HdnSubTotalDescuestos.Value = (TxtSubTotal.Text.Replace(",", "").ToDouble - LblDescuentos.Text.Replace(",", "").ToDouble - LblAmortizacion.Text.Trim.Replace(",", "").ToDouble - TxtRetISR.Text.Replace(",", "").ToDouble).FormatoNumero(2)
@@ -17525,6 +17526,123 @@ Partial Class Ventas
End If
End If
End Sub
Private Function GenerarNC(ImporteTotal As Decimal, CveVendedor As Integer, ByRef Venta As Venta, GvRowAnticipo As GridViewRow, ByRef ObjVentas As ClsVentas) As Modelos.Factura.MMensajeFactura
Dim Retorno As Modelos.Factura.MMensajeFactura = Nothing
Dim SbAlert As New StringBuilder()
Try
Dim ObjNC = Modelos.Factura.CrearVentaNueva(UsuarioActual, Venta.CveSubProyecto, Venta.CveCliente, Venta.CveMoneda, "Egs")
ObjNC.CveMoneda = Venta.CveMoneda
If ObjNC.CveMoneda = 2 Then
ObjNC.TipoCambio = GlobalTipoCambio(Now).ToString().ToDecimal()
Else
ObjNC.TipoCambio = 1
End If
Dim Total As Decimal = ImporteTotal
Dim subTotal As Decimal = (Total / (1D + Venta.PorcentajeIVA))
'Dim IVA = (subTotal * Factura.PorcentajeIVA).RoundSQL(2)
Dim PU As Decimal = subTotal
Dim Cantidad As Decimal = 1D
Dim Descuento As Decimal = 0
Dim Obs = ""
Dim CvePSAnticipo As Integer = 1
Dim DatarowPS = GlobalDTPS(UsuarioActual.CveEmpresa, SATClavePS:="84111506").FiltroPrimero("CodigoPS LIKE '%ANTICIPO%' OR CodigoPS LIKE '%anticipo%' OR CodigoPS LIKE '%Anticipo%'")
If DatarowPS IsNot Nothing Then
CvePSAnticipo = DatarowPS("CvePS").ToString().ToEntero()
End If
ObjNC.AgregarPS(CvePSAnticipo, Cantidad, PU, Descuento, Obs, CveSubProyecto:=Venta.CveSubProyecto, PorcentajeIVA:=Venta.PorcentajeIVA)
ObjNC.CalculaTotales()
Dim DtVentasNC = New DataTable
DtVentasNC.Columns.Add("CveVenta", GetType(Integer))
DtVentasNC.Columns.Add("Importe", GetType(Decimal))
DtVentasNC.Columns.Add("FolioFiscal", GetType(String))
DtVentasNC.Columns.Add(New DataColumn With {.ColumnName = "TC", .DataType = GetType(Decimal), .DefaultValue = 1})
DtVentasNC.Columns.Add(New DataColumn With {.ColumnName = "TCCambioValuacion", .DataType = GetType(Decimal), .DefaultValue = 1})
DtVentasNC.Columns.Add(New DataColumn With {.ColumnName = "CveEstatusFactura", .DataType = GetType(Integer), .DefaultValue = -1})
DtVentasNC.Columns.Add(New DataColumn With {.ColumnName = "Folio", .DataType = GetType(String), .DefaultValue = ""})
DtVentasNC.Rows.Add(Venta.CveVenta, Total, "")
ObjNC.DtVentasNC = DtVentasNC
Dim DtVentasRelacionadas As New DataTable()
DtVentasRelacionadas.Columns.Add("CveVenta", GetType(Integer))
DtVentasRelacionadas.Rows.Add(Venta.CveVenta)
ObjNC.DtVentasRelacionadas = DtVentasRelacionadas
Dim DtAnticipos As New DataTable()
DtAnticipos.Columns.Add("CveVenta", GetType(Integer))
DtAnticipos.Columns.Add("Importe", GetType(Decimal))
DtAnticipos.Columns.Add("CveConceptoContable", GetType(Integer))
DtAnticipos.Columns.Add("CveIE", GetType(Integer))
DtAnticipos.Columns.Add("FolioFiscal", GetType(String))
DtAnticipos.Columns.Add("Descuentos", GetType(Decimal))
DtAnticipos.Columns.Add("TotalAnticipo", GetType(Decimal))
DtAnticipos.Columns.Add("SubTotalAnticipo", GetType(Decimal))
DtAnticipos.Columns.Add("CveMoneda", GetType(Decimal))
DtAnticipos.Columns.Add("Fecha", GetType(DateTime))
DtAnticipos.Columns.Add("TipoCambioPago", GetType(Decimal))
DtAnticipos.Columns.Add("Folio", GetType(String))
DtAnticipos.Columns.Add("Tipo", GetType(String))
If GvRowAnticipo IsNot Nothing Then
Dim CveVenta = GvRowAnticipo.DataKey("CveVenta").ToEntero()
Dim Importe = GvRowAnticipo.FCTextBox(String.Format("TxtAnticipo{0}", GvRowAnticipo.RowIndex)).Text.ToDecimal()
Dim CveConceptoContable = GvRowAnticipo.DataKey("CveConceptoContable").ToEntero()
Dim CveIE = GvRowAnticipo.DataKey("CveIE").ToEntero()
Dim FolioFiscal = GvRowAnticipo.DataKey("FolioFiscal")
Dim Descuentos As Decimal = GvRowAnticipo.DataKey("Descuentos").ToDecimal()
Dim TotalAnticipo As Decimal = GvRowAnticipo.DataKey("TotalAnticipo").ToDecimal()
Dim SubTotalAnticipo As Decimal = GvRowAnticipo.DataKey("SubTotalAnticipo").ToDecimal()
Dim CveMoneda As Integer = GvRowAnticipo.DataKey("CveMoneda").ToEntero()
Dim Fecha As DateTime = GvRowAnticipo.DataKey("Fecha").ToDateTime()
Dim TipoCambioPago As Decimal = GvRowAnticipo.DataKey("TipoCambioPago").ToDecimal()
Dim Folio = GvRowAnticipo.DataKey("Folio")
Dim Tipo = GvRowAnticipo.DataKey("Tipo")
DtAnticipos.Rows.Add(CveVenta, Importe, CveConceptoContable, CveIE, FolioFiscal, Descuentos, TotalAnticipo, SubTotalAnticipo, CveMoneda, Fecha, TipoCambioPago, Folio, Tipo)
End If
ObjNC.EsNCAmortizacion = True
ObjNC.CveTipoAmortizacionAnticipo = 2
ObjNC.DtAnticiposAplicados = DtAnticipos
ObjNC.CveFormaPago = 21 'Aplicación de anticipos
ObjNC.CveMetodoPago = 1
ObjNC.CveUsoCFDI = Venta.CveUsoCFDI
ObjNC.CveVendedor = CveVendedor
ObjNC.CveTipoRelacion = 7
ObjNC.PreparaTimbre()
AjustarTimbreNC(ObjNC)
Retorno = ObjNC.EmitirFactura(False, Transac:=ObjVentas.IniciaTransacSQL(), Modulo:="Cobranza/GuardarVenta", MostrarURL:=True)
Retorno.Factura = ObjNC
If Retorno IsNot Nothing AndAlso Retorno.CveVenta > 0 Then
For Each msg In Retorno.Mensajes
SbAlert.AppendFormat("{0} <br/>", msg)
Next
SbAlert.AppendFormat("<ul><li><a class=""Texto"" href=""/Compras/PopUpVerFactura.aspx?P={0}"">Descargar PDF Factura</a></li></ul><br/>", Encripta(String.Format("OP=VTA&CVEVENTA={0}&PAG=1&GUARDAR=1", Retorno.CveVenta)))
Else
SbAlert.Append("Ocurrió un error al generar la nota de crédito")
End If
Catch ex As Exception
SbAlert.Append("Ocurrió un error al generar el documento")
GuardaLog(ex.ToString())
End Try
Return Retorno
End Function
Protected Sub BtnAceptarAnticipos_Click(sender As Object, e As System.EventArgs) Handles BtnAceptarAnticipos.Click
AplicaAnticipos()
End Sub
@@ -17554,6 +17672,8 @@ Partial Class Ventas
Dim CveIEAnticipo As Integer
Dim CvePlantillaPoliza As Integer = 10
Dim ListaNC As New List(Of Modelos.Factura.MMensajeFactura)
Try
CvePrePoliza = ObjConta.AddPrePolizas("Ventas/Anticipos", CvePlantillaPoliza, TxtTotal.Text.ToDecimal, TxtTotal.Text.ToDecimal, DdlMoneda.SelectedValue.ToEntero(), TxtFechaAnticipoCliente.Text.ToDate, String.Format("{0} {1} {2}-{3}", If(EsAnticipo2017Jam, "Aplicación Anticipo 2017", ""), NombreCliente, Serie, TxtFolio.Text), UsuarioActual.CveUsuario, UsuarioActual.CveEmpresa)
@@ -17616,27 +17736,46 @@ Partial Class Ventas
Select Case gvr.DataKey("CveConceptoContable").ToString()
Case "3", "295"
importeMovimiento = gvr.FCTextBox("TxtAnticipo" & gvr.RowIndex).Text.ToDecimal - ((gvr.FCTextBox("TxtAnticipo" & gvr.RowIndex).Text.ToDecimal / gvr.DataKey("TotalAnticipo").ToDecimal) * gvr.DataKey("IVAAnticipo").ToDecimal)
importeMN = gvr.FCTextBox("TxtAnticipo" & gvr.RowIndex).Text.ToDecimal
If UsuarioActual.Empresa.Funcionalidad(382) = "1" AndAlso DdlMoneda.SelectedValue.ToEntero() = 1 Then
Dim ImporteNC As Decimal = gvr.FCTextBox("TxtAnticipo" & gvr.RowIndex).Text.ToDecimal
If importeMovimiento <> 0 Then
Dim RespNC = GenerarNC(ImporteNC, DdlVendedor.SelectedValue.ToEntero(), Me.VentaActual, gvr, ObjVenta)
Dim Pago As New VentaPago
Pago.ImportePagoVenta = importeMN
Pago.ImportePago = importeMovimiento
Pago.CveIE = CveIEAnticipo
Pago.CveMonedaPago = gvr.DataKey("CveMoneda").ToEntero()
Pago.TipoPago = "A"
Pago.Fecha = TxtFechaAnticipoCliente.Text.ToDate 'TCPago = gvr.DataKey("TipoCambio").todecimal
Pago.CveVentaAmortizacion = gvr.DataKey("CveVenta").ToEntero()
Pago.TCPago = gvr.DataKey("TipoCambio").ToDecimal
Pago.AplicaTraslado = gvr.DataKey("RegresarImpuesto") = "1"
Pago.Referencia = gvr.DataKey("Folio")
Pago.AplicaConceptoImpuestoPendiente = (Not gvr.DataKey("RegresarImpuesto") = "1") AndAlso (VentaActual.IVA > 0 OrElse VentaActual.IEPS > 0)
If RespNC IsNot Nothing Then
If RespNC.TieneError Then
ErrorConta = True
Exit For
Else
ListaNC.Add(RespNC)
End If
Else
ErrorConta = True
End If
ErrorConta = AplicaPrePolizaPagoVenta(VentaActual, Pago, CvePrePoliza, ObjConta)
Else
importeMovimiento = gvr.FCTextBox("TxtAnticipo" & gvr.RowIndex).Text.ToDecimal - ((gvr.FCTextBox("TxtAnticipo" & gvr.RowIndex).Text.ToDecimal / gvr.DataKey("TotalAnticipo").ToDecimal) * gvr.DataKey("IVAAnticipo").ToDecimal)
importeMN = gvr.FCTextBox("TxtAnticipo" & gvr.RowIndex).Text.ToDecimal
If importeMovimiento <> 0 Then
Dim Pago As New VentaPago
Pago.ImportePagoVenta = importeMN
Pago.ImportePago = importeMovimiento
Pago.CveIE = CveIEAnticipo
Pago.CveMonedaPago = gvr.DataKey("CveMoneda").ToEntero()
Pago.TipoPago = "A"
Pago.Fecha = TxtFechaAnticipoCliente.Text.ToDate 'TCPago = gvr.DataKey("TipoCambio").todecimal
Pago.CveVentaAmortizacion = gvr.DataKey("CveVenta").ToEntero()
Pago.TCPago = gvr.DataKey("TipoCambio").ToDecimal
Pago.AplicaTraslado = gvr.DataKey("RegresarImpuesto") = "1"
Pago.Referencia = gvr.DataKey("Folio")
Pago.AplicaConceptoImpuestoPendiente = (Not gvr.DataKey("RegresarImpuesto") = "1") AndAlso (VentaActual.IVA > 0 OrElse VentaActual.IEPS > 0)
ErrorConta = AplicaPrePolizaPagoVenta(VentaActual, Pago, CvePrePoliza, ObjConta)
End If
End If
Case "12"
@@ -17743,26 +17882,64 @@ Partial Class Ventas
If ErrorConta Then
Alert("Ocurrió un error al amortizar el documento. Por favor verifique y vuelva a intentar.", TipoMensaje.MsgError)
ObjConta.RollBack()
ObjConta.Dispose()
ObjVenta.Dispose()
ObjIE.Dispose()
ObjCliente.Dispose()
Else
Dim Msg As New StringBuilder
Msg.Append("Se ha amortizado correctamente")
Msg.Append("Se ha amortizado correctamente. <br/>")
'AJUSTA POLIZA
If ObjConta.ContaFolio > 0 Then
Msg.AppendFormat("<br/><br/><a class=""Texto"" target='_blank' href=""/Contabilidad/RegPrePolizas.aspx?CvePrePoliza={0}"">Ver Póliza</a>", CvePrePoliza)
End If
ObjConta.Commit()
ObjConta.Dispose()
ObjVenta.Dispose()
ObjIE.Dispose()
ObjCliente.Dispose()
Dim MsgTimbrado As String = ""
Try
If ListaNC.Count > 0 Then
For Each respNC In ListaNC
Msg.AppendFormat("<span style=""font-size:14px;""> La nota de crédito <a class=""Texto"" target=""_blank"" style=""font-size:18px;font-weight:bold;"" href=""/Ventas/Ventas.aspx?CveVenta={0}"">{1}</a> se genero correctamente.</span> <br/>", respNC.CveVenta, respNC.SerieFolio)
Dim SeTimbroNC As Boolean = False
MsgTimbrado = TimbrarNC(respNC.Factura, SeTimbroNC)
If Not String.IsNullOrEmpty(MsgTimbrado) Then
Dim StrEr As String = $"<br/><div id = 'DivTipoCambioAdvertencia' class='alert alert-danger' style='padding: 5px; margin-bottom: 5px; padding-right: 25px;'><strong style = 'font-size: 20px; margin-right: 25px;' > Comprobante no emitido : </strong><br/> {MsgTimbrado}<br/>Por favor inténtelo de nuevo a través del botón 'Emitir' en ventas.</div>"
MsgTimbrado = StrEr
End If
If SeTimbroNC Then
Dim xml = String.Format("<ul><li><a class=""Texto"" target=""_blank"" href=""/Compras/PopUpVerFactura.aspx?P={0}"">Descargar XML</a></li></ul>", Encripta(String.Format("OP=FactVta&CVEVENTA={0}&EXT={1}&SERIEFOLIO={2}&TIPODOCTO={3}", respNC.CveVenta, "XML", respNC.SerieFolio, respNC.Factura.TipoDocto)))
Msg.AppendFormat("{0}", xml)
End If
Dim pdfNC = String.Format("<ul><li><a class=""Texto"" href=""/Compras/PopUpVerFactura.aspx?P={0}"">Descargar PDF</a></li></ul>", Encripta(String.Format("OP=VTA&CVEVENTA={0}&PAG=1", respNC.CveVenta)))
Msg.AppendFormat("{0}", pdfNC)
If Not String.IsNullOrEmpty(MsgTimbrado) Then
Msg.AppendFormat("{0}", MsgTimbrado)
End If
Next
End If
Catch ex As Exception
GuardaLogAD(ex.ToString())
Msg.AppendFormat("<br/>{0}<br/>", MsgTimbrado)
End Try
BtnAceptarAnticipos.Enabled = False
Alert(Msg.ToString(), TipoMensaje.MsgInformacion, JSBtnAceptar:="BtnRefrescarPagina();", ShowCloseButton:=False)
End If
ObjConta.Dispose()
ObjVenta.Dispose()
ObjIE.Dispose()
ObjCliente.Dispose()
Else
'cuando no se ha registrado la venta
@@ -17970,6 +18147,47 @@ Partial Class Ventas
End Sub
Private Sub AjustarTimbreNC(ByRef ObjNC As Modelos.Factura)
If LblSeTimbro.Text = "Si" Then
ObjNC.Timbre.CFDIRelacionados.UUID.Clear()
ObjNC.Timbre.CFDIRelacionados.UUID.Add(LblFolioFiscal.Text.Trim())
ObjNC.Timbre.CFDIRelacionados.TipoRelacion = "07"
ObjNC.Timbre.CFDIRelacionados.TipoRelacionTexto = "07-CFDI por aplicación de anticipo"
End If
ObjNC.Timbre.Comprobante.MetodoPago = GlobalDTVtaMetodoPago.FiltroPrimeroValor("ClaveMetodoPagoSAT", $"CveMetodoPago = {ObjNC.CveMetodoPago}") ' Obj.ClaveMetodoPagoSAT
ObjNC.Timbre.Comprobante.FormadePago = GlobalDTVtaFormaPago.FiltroPrimeroValor("ClaveFormaPagoSAT", $"CveFormaPago = {ObjNC.CveFormaPago}") 'Obj.ClaveFormaPagoSAT
ObjNC.Timbre.Receptor.UsoCFDI = GlobalDTSATUsosCFDI.FiltroPrimeroValor("UsoCFDI", $"CveUsoCFDI = {ObjNC.CveUsoCFDI}")
End Sub
Private Function TimbrarNC(ByRef ObjNC As Modelos.Factura, ByRef SeTimbroNC As Boolean) As String
Dim MsgErrorTimbrado As String = ""
'Dim SeTimbro As Boolean = False
'Dim TimbroNC As Boolean = False
Dim ObjTimbrado As New Timbrado
Try
If ObjNC.Timbre IsNot Nothing Then
Dim ObjTimbradoNC As New Timbrado
SeTimbroNC = ObjTimbradoNC.TimbrarFactura(UsuarioActual, ObjNC)
If Not SeTimbroNC Then
MsgErrorTimbrado = $"{MsgErrorTimbrado} {ObjTimbradoNC.MensajeErrorCFDI}"
End If
ObjTimbradoNC = Nothing
End If
Catch ex As Exception
OnError("Ocurrió un error al generar el timbrado", ex)
End Try
If Not String.IsNullOrEmpty(MsgErrorTimbrado) Then
GuardaLogAD(MsgErrorTimbrado, Archivo:="LogTimbrado")
End If
Return MsgErrorTimbrado
End Function
Private Function AgregarPrePolizasXTrasladar(ObjConta As ClsContabilidad, CvePrePoliza As Integer, PorcentajeAmortizacion As Decimal, TipoCambio As Decimal, ByRef CuentasContablesInvalidas As StringBuilder, ByRef CveCuentaContabilizacion As Integer, Importe13 As Decimal, Importe8 As Decimal, Importe9 As Decimal) As Boolean
Dim ImporteMovimiento As Decimal
Dim ImporteMX As Decimal
@@ -18793,7 +19011,7 @@ Partial Class Ventas
Dim ActualizarPrecios As Boolean = False
If UsuarioActual.Empresa.Funcionalidad(109) = "1" Then
ActualizarPrecios = true
ActualizarPrecios = True
End If
If UsuarioActual.Empresa.Funcionalidad(558) = "1" Then