diff --git a/Ashx/E119.ashx b/Ashx/E119.ashx index 2b167fce5..2e7da80a0 100644 --- a/Ashx/E119.ashx +++ b/Ashx/E119.ashx @@ -427,36 +427,79 @@ Public Class E119 : Implements IHttpHandler If Not String.IsNullOrEmpty(UsoCFDI) Then CveUsoCFDI = ClsPaginas.GlobalDTSATUsosCFDI().FiltroPrimeroValor("CveUsoCFDI", $"UsoCFDI = '{UsoCFDI.Trim}'").ToEntero() End If + Else + TieneError = True + Resp.Mensajes.Add("no existe cliente definido") End If - 'BUSCAR SI ESTÁ REGISTRADO EL CLIENTE - If ObjClientes.GetClientes(-1, "-1", "-1", "-1", RFC, "-1", "-1", -1, CveEmpresa, 1, TipoCliente:=-1, TopSelect:=1) Then - If ObjClientes.DtResultados.Rows.Count > 0 Then - 'Resp.TieneError = False - CveCliente = ObjClientes.CveCliente - 'Resp.Mensajes.Add("El RFC ya existe en el sistema") + If Not TieneError Then + + + 'BUSCAR SI ESTÁ REGISTRADO EL CLIENTE + If ObjClientes.GetClientes(-1, "-1", "-1", "-1", RFC, "-1", "-1", -1, CveEmpresa, 1, TipoCliente:=-1, TopSelect:=1) Then + If ObjClientes.DtResultados.Rows.Count > 0 Then + 'Resp.TieneError = False + CveCliente = ObjClientes.CveCliente + 'Resp.Mensajes.Add("El RFC ya existe en el sistema") + End If End If - End If - MsgLog.AppendLine("1.4 verificar cliente") + MsgLog.AppendLine("1.4 verificar cliente.") - If CveCliente = 0 Then - ' 'DATOS DE BILLING - MsgLog.AppendLine("1.5 crear cliente") - If JsonRequest("billing") IsNot Nothing Then + If CveCliente = 0 Then + ' 'DATOS DE BILLING + MsgLog.AppendLine("1.5 crear cliente") + If JsonRequest("billing") IsNot Nothing Then + Dim CveEstado As Integer = -1 + Dim CveRegimenFiscal As Integer = -1 + + Dim Direccion As String = If(JsonRequest("billing")("address_1") Is Nothing, "", JsonRequest("billing")("address_1").ToString()) + Dim NumExt As String = "" + Dim NumInt As String = "" + Dim Colonia As String = "" + Dim Ciudad As String = If(JsonRequest("billing")("city") Is Nothing, "", JsonRequest("billing")("city").ToString()) + Dim Localidad As String = If(JsonRequest("billing")("city") Is Nothing, "", JsonRequest("billing")("city").ToString()) + Dim CP As String = If(JsonRequest("billing")("postcode") Is Nothing, "", JsonRequest("billing")("postcode").ToString()) + Dim Telefono As String = If(JsonRequest("billing")("phone") Is Nothing, "", JsonRequest("billing")("phone").ToString()) + Dim Correo As String = If(JsonRequest("billing")("email") Is Nothing, "", JsonRequest("billing")("email").ToString()) + Dim Estado As String = If(JsonRequest("billing")("state") Is Nothing, "", JsonRequest("billing")("state").ToString()) + If Not String.IsNullOrEmpty(Estado) Then + CveEstado = ClsPaginas.GlobalDTGralEstados("MEX").FiltroPrimeroValor("CveEstado", $"NombreEstado = '{Estado.Trim}'").ToEntero() + If CveEstado <= 0 Then 'SI NO ENCUENTRA EL ESTADO INGRESA EL DE SONORA + CveEstado = 26 + End If + Else + CveEstado = 26 + End If + If Not String.IsNullOrEmpty(RegimenCliente) Then + CveRegimenFiscal = ClsPaginas.GlobalDTRegimenesFiscales().FiltroPrimeroValor("CveRegimenFiscal", $"CodigoRegimenFiscal = '{RegimenCliente.Trim}'").ToEntero() + End If + + If ObjClientes.AgregarClienteNoFrecuente(NombreCliente, Direccion, NumExt, NumInt, Colonia, Localidad, Ciudad, CveEstado, RFC, CP, Telefono, Correo, CveSubProyecto, CveUsuario, CveEmpresa, "", -1, -1, CveRegimenFiscal:=CveRegimenFiscal, CveUsoCFDI:=CveUsoCFDI) Then + If ObjClientes.DtResultados.Rows.Count > 0 Then + CveCliente = ObjClientes.DtResultados(0)("CveCliente").ToString().ToEntero() + MsgLog.AppendLine("1.5.5 cliente creado") + End If + End If + Else + MsgLog.AppendLine("1.5.9 sin datos cliente") + TieneError = True + Resp.Mensajes.Add("no existen datos del cliente.") + End If + + End If + If JsonRequest("shipping") IsNot Nothing AndAlso Not TieneError Then Dim CveEstado As Integer = -1 - Dim CveRegimenFiscal As Integer = -1 - - Dim Direccion As String = If(JsonRequest("billing")("address_1") Is Nothing, "", JsonRequest("billing")("address_1").ToString()) + Dim Direccion As String = If(JsonRequest("shipping")("address_1") Is Nothing, "", JsonRequest("shipping")("address_1").ToString()) Dim NumExt As String = "" Dim NumInt As String = "" Dim Colonia As String = "" - Dim Ciudad As String = If(JsonRequest("billing")("city") Is Nothing, "", JsonRequest("billing")("city").ToString()) - Dim Localidad As String = If(JsonRequest("billing")("city") Is Nothing, "", JsonRequest("billing")("city").ToString()) - Dim CP As String = If(JsonRequest("billing")("postcode") Is Nothing, "", JsonRequest("billing")("postcode").ToString()) - Dim Telefono As String = If(JsonRequest("billing")("phone") Is Nothing, "", JsonRequest("billing")("phone").ToString()) - Dim Correo As String = If(JsonRequest("billing")("email") Is Nothing, "", JsonRequest("billing")("email").ToString()) - Dim Estado As String = If(JsonRequest("billing")("state") Is Nothing, "", JsonRequest("billing")("state").ToString()) + Dim Ciudad As String = If(JsonRequest("shipping")("city") Is Nothing, "", JsonRequest("shipping")("city").ToString()) + Dim Localidad As String = If(JsonRequest("shipping")("city") Is Nothing, "", JsonRequest("shipping")("city").ToString()) + Dim CP As String = If(JsonRequest("shipping")("postcode") Is Nothing, "", JsonRequest("shipping")("postcode").ToString()) + Dim Telefono As String = If(JsonRequest("shipping")("phone") Is Nothing, "", JsonRequest("shipping")("phone").ToString()) + Dim Correo As String = If(JsonRequest("shipping")("email") Is Nothing, "", JsonRequest("shipping")("email").ToString()) + Dim Estado As String = If(JsonRequest("shipping")("state") Is Nothing, "", JsonRequest("shipping")("state").ToString()) If Not String.IsNullOrEmpty(Estado) Then CveEstado = ClsPaginas.GlobalDTGralEstados("MEX").FiltroPrimeroValor("CveEstado", $"NombreEstado = '{Estado.Trim}'").ToEntero() If CveEstado <= 0 Then 'SI NO ENCUENTRA EL ESTADO INGRESA EL DE SONORA @@ -465,137 +508,100 @@ Public Class E119 : Implements IHttpHandler Else CveEstado = 26 End If - If Not String.IsNullOrEmpty(RegimenCliente) Then - CveRegimenFiscal = ClsPaginas.GlobalDTRegimenesFiscales().FiltroPrimeroValor("CveRegimenFiscal", $"CodigoRegimenFiscal = '{RegimenCliente.Trim}'").ToEntero() - End If - If ObjClientes.AgregarClienteNoFrecuente(NombreCliente, Direccion, NumExt, NumInt, Colonia, Localidad, Ciudad, CveEstado, RFC, CP, Telefono, Correo, CveSubProyecto, CveUsuario, CveEmpresa, "", -1, -1, CveRegimenFiscal:=CveRegimenFiscal, CveUsoCFDI:=CveUsoCFDI) Then - If ObjClientes.DtResultados.Rows.Count > 0 Then - CveCliente = ObjClientes.DtResultados(0)("CveCliente").ToString().ToEntero() - MsgLog.AppendLine("1.5.5 cliente creado") + If ObjClientes.DireccionEnvioGet(-1, CveCliente, -1) Then + Dim dtt As DataTable = ObjClientes.DtResultados + If dtt IsNot Nothing AndAlso dtt.Rows.Count > 0 Then + dtt = dtt.FiltraParametros($"EnvioDireccion = '{Direccion}' and EnvioCP = '{CP}'") + If dtt.Rows.Count > 0 Then + CveDireccionEnvio = dtt.FiltroPrimeroValor("CveDireccionEnvio").ToEntero + End If + Else + CveDireccionEnvio = ObjClientes.DireccionEnvioAdd(CveCliente, Direccion, NumExt, NumInt, Colonia, Ciudad, Estado, CP, "", 100, 1) End If End If + Else MsgLog.AppendLine("1.5.9 sin datos cliente") TieneError = True Resp.Mensajes.Add("no existen datos del cliente") End If - End If - If JsonRequest("shipping") IsNot Nothing AndAlso Not TieneError Then - Dim CveEstado As Integer = -1 - Dim Direccion As String = If(JsonRequest("shipping")("address_1") Is Nothing, "", JsonRequest("shipping")("address_1").ToString()) - Dim NumExt As String = "" - Dim NumInt As String = "" - Dim Colonia As String = "" - Dim Ciudad As String = If(JsonRequest("shipping")("city") Is Nothing, "", JsonRequest("shipping")("city").ToString()) - Dim Localidad As String = If(JsonRequest("shipping")("city") Is Nothing, "", JsonRequest("shipping")("city").ToString()) - Dim CP As String = If(JsonRequest("shipping")("postcode") Is Nothing, "", JsonRequest("shipping")("postcode").ToString()) - Dim Telefono As String = If(JsonRequest("shipping")("phone") Is Nothing, "", JsonRequest("shipping")("phone").ToString()) - Dim Correo As String = If(JsonRequest("shipping")("email") Is Nothing, "", JsonRequest("shipping")("email").ToString()) - Dim Estado As String = If(JsonRequest("shipping")("state") Is Nothing, "", JsonRequest("shipping")("state").ToString()) - If Not String.IsNullOrEmpty(Estado) Then - CveEstado = ClsPaginas.GlobalDTGralEstados("MEX").FiltroPrimeroValor("CveEstado", $"NombreEstado = '{Estado.Trim}'").ToEntero() - If CveEstado <= 0 Then 'SI NO ENCUENTRA EL ESTADO INGRESA EL DE SONORA - CveEstado = 26 - End If - Else - CveEstado = 26 + MsgLog.AppendLine("1.6 inicio generar venta") + ObjFactura = Modelos.Factura.CrearVentaNueva(Usuario, CveSubProyecto, CveCliente, CveMoneda, "Rem") + MsgLog.AppendFormat("1.6.0 objeto creado {0}", If(ObjFactura IsNot Nothing, "si", "no")) + + If JsonRequest("shiping_lines") IsNot Nothing Then + ' shiping + 'shiping_lines/ method_id: free_shipping/local_pickup + Dim Entrega As String = If(JsonRequest("shiping_lines")("method_id") Is Nothing, "", JsonRequest("shiping_lines")("method_id").ToString()) + ObjFactura.Alfa1 = Entrega End If - If ObjClientes.DireccionEnvioGet(-1, CveCliente, -1) Then - Dim dtt As DataTable = ObjClientes.DtResultados - If dtt IsNot Nothing AndAlso dtt.Rows.Count > 0 Then - dtt = dtt.FiltraParametros($"EnvioDireccion = '{Direccion}' and EnvioCP = '{CP}'") - If dtt.Rows.Count > 0 Then - CveDireccionEnvio = dtt.FiltroPrimeroValor("CveDireccionEnvio").ToEntero + ObjFactura.CveDireccionEnvio = CveDireccionEnvio + ObjFactura.CveVentaTerminal = IdEcommerce + ObjFactura.CveUsoCFDI = CveUsoCFDI + + If JsonRequest("line_items") IsNot Nothing Then + Dim ListaPS As JArray = JArray.Parse(JsonRequest("line_items").ToString()) + Dim Consec As Integer = 1 + MsgLog.AppendLine("1.6.1 recorrer ps") + For Each JsonPS As JObject In ListaPS + Cantidad = JsonPS("quantity").ToString.ToDecimal() + PU = JsonPS("price").ToString.ToDecimal() + CvePS = ClsPaginas.GlobalDTPSSimplificado(CveEmpresa, Codigo:=JsonPS("sku").ToString()).FiltroPrimeroValor("CvePS").ToEntero() + + ObjFactura.AgregarPS(CvePS, Cantidad, PU, Descuento, Obs, CveSubProyecto:=CveSubProyecto) + MsgLog.AppendLine("1.6.1 agregar ps a venta") + ''VALIDAR QUE SEA EL MISMO PRODUCTO + If CvePS <= 0 Then + TieneError = True + 'Retorno.Mensajes.Add(String.Format("No se encontró el producto: {0}", JsonPS("name").ToString())) + Exit For End If - Else - CveDireccionEnvio = ObjClientes.DireccionEnvioAdd(CveCliente, Direccion, NumExt, NumInt, Colonia, Ciudad, Estado, CP, "", 100, 1) - End If - End If - - Else - MsgLog.AppendLine("1.5.9 sin datos cliente") - TieneError = True - Resp.Mensajes.Add("no existen datos del cliente") - End If - - MsgLog.AppendLine("1.6 inicio generar venta") - ObjFactura = Modelos.Factura.CrearVentaNueva(Usuario, CveSubProyecto, CveCliente, CveMoneda, "Rem") - MsgLog.AppendFormat("1.6.0 objeto creado {0}", If(ObjFactura IsNot Nothing, "si", "no")) - - If JsonRequest("shiping_lines") IsNot Nothing Then - ' shiping - 'shiping_lines/ method_id: free_shipping/local_pickup - Dim Entrega As String = If(JsonRequest("shiping_lines")("method_id") Is Nothing, "", JsonRequest("shiping_lines")("method_id").ToString()) - ObjFactura.Alfa1 = Entrega - End If - - ObjFactura.CveDireccionEnvio = CveDireccionEnvio - ObjFactura.CveVentaTerminal = IdEcommerce - ObjFactura.CveUsoCFDI = CveUsoCFDI - - If JsonRequest("line_items") IsNot Nothing Then - Dim ListaPS As JArray = JArray.Parse(JsonRequest("line_items").ToString()) - Dim Consec As Integer = 1 - MsgLog.AppendLine("1.6.1 recorrer ps") - For Each JsonPS As JObject In ListaPS - Cantidad = JsonPS("quantity").ToString.ToDecimal() - PU = JsonPS("price").ToString.ToDecimal() - CvePS = ClsPaginas.GlobalDTPSSimplificado(CveEmpresa, Codigo:=JsonPS("sku").ToString()).FiltroPrimeroValor("CvePS").ToEntero() - - ObjFactura.AgregarPS(CvePS, Cantidad, PU, Descuento, Obs, CveSubProyecto:=CveSubProyecto) - MsgLog.AppendLine("1.6.1 agregar ps a venta") - ''VALIDAR QUE SEA EL MISMO PRODUCTO - If CvePS <= 0 Then - TieneError = True - 'Retorno.Mensajes.Add(String.Format("No se encontró el producto: {0}", JsonPS("name").ToString())) - Exit For - End If - Consec += 1 - Next - ObjFactura.CalculaTotales() - Else - ObjFactura = Nothing - TieneError = True - Resp.Mensajes.Add("no existen productos definidos") - End If - - - If ObjFactura IsNot Nothing AndAlso Not TieneError Then - MsgLog.AppendLine("1.7 agregar la remision") - Resp = ObjFactura.EmitirFactura(False, Transac:=ObjClientes.IniciaTransacSQL()) - - If Resp.CveVenta > 0 Then - MsgLog.AppendLine("2.0 remisión registrada") - Dim data As New JArray - Dim objSucursal As New JObject( - New JProperty("Cve", Resp.CveVenta), - New JProperty("SerieFolio", Resp.SerieFolio) - ) - data.Add(objSucursal) - Respuesta.status = "success" - Respuesta.data = data + Consec += 1 + Next + ObjFactura.CalculaTotales() Else - MsgLog.AppendLine("1.8 error al agregar la remision") + ObjFactura = Nothing TieneError = True + Resp.Mensajes.Add("no existen productos definidos") End If - If TieneError Then + If ObjFactura IsNot Nothing AndAlso Not TieneError Then + MsgLog.AppendLine("1.7 agregar la remision") + Resp = ObjFactura.EmitirFactura(False, Transac:=ObjClientes.IniciaTransacSQL()) + + If Resp.CveVenta > 0 Then + MsgLog.AppendLine("2.0 remisión registrada") + Dim data As New JArray + Dim objSucursal As New JObject( + New JProperty("Cve", Resp.CveVenta), + New JProperty("SerieFolio", Resp.SerieFolio) + ) + data.Add(objSucursal) + Respuesta.status = "success" + Respuesta.data = data + Else + MsgLog.AppendLine("1.8 error al agregar la remision.") + TieneError = True + End If + + If TieneError Then + Dim sb As New StringBuilder() + For Each msg As String In Resp.Mensajes + sb.AppendFormat("{0}. ", msg) + Next + RespuestaError.message = sb.ToString() + End If + Else + TieneError = True Dim sb As New StringBuilder() For Each msg As String In Resp.Mensajes sb.AppendFormat("{0}. ", msg) Next RespuestaError.message = sb.ToString() End If - Else - TieneError = True - Dim sb As New StringBuilder() - For Each msg As String In Resp.Mensajes - sb.AppendFormat("{0}. ", msg) - Next - RespuestaError.message = sb.ToString() End If If TieneError Then