Buenas tardes, me podrian apoyar.
Estoy programando en C# para crear un traslado de stock en base a una solicitud de traslado usando SERVICE LAYER.
Actualmente cuento con el codigo para crear una solicitud de traslado, pero no se como pasarla a traslado de stock.
Me podrian ayudar, gracias.
Les comparto mi codigo para insertar la solicitud por si a alguien le ayuda de igual manera.
public async Task<string> CrearSolicitudTraslado(encabezadoSolicitudTraslado datos)
{
// Configurar TLS y aceptar certificados no confiables (como en LoginAsync)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
// Obtener el B1SESSION
string b1Session = await LoginAsync(datos.empresa);
if (string.IsNullOrEmpty(b1Session))
throw new Exception("No se pudo obtener la sesión de SAP.");
string baseUrl = _configuration.GetSection(datos.empresa.ToUpper())["BaseUrl"];
string url = $"{baseUrl}/InventoryTransferRequests";
// Armar el JSON de la solicitud
var bodyObj = new
{
DocDate = DateTime.Now.ToString("yyyy-MM-dd"),
FromWarehouse = datos.almacenOri,
ToWarehouse = datos.almacenDest,
StockTransferLines = datos.detalleST.Select(d => new
{
ItemCode = d.itemcode,
Quantity = d.cantidad,
}).ToList()
};
string jsonBody = JsonConvert.SerializeObject(bodyObj);
// Crear la petición
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Cookie", $"B1SESSION={b1Session}");
// Enviar el JSON
using (var streamWriter = new StreamWriter(await request.GetRequestStreamAsync()))
{
streamWriter.Write(jsonBody);
}
try
{
using (WebResponse response = await request.GetResponseAsync())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string result = await reader.ReadToEndAsync();
var jsonResult = JObject.Parse(result);
return jsonResult["DocEntry"]?.ToString(); // Regresamos el DocEntry de la solicitud creada
}
}
catch (WebException ex)
{
using (var errorResponse = (HttpWebResponse)ex.Response)
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
string errorText = await reader.ReadToEndAsync();
throw new Exception($"Error al crear la solicitud de traslado: {errorText}");
}
}
}