Commit inicial: ITCA_API_SAP_WMS (.NET Core 3.1)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
2b82744eb8
168 files changed
+11066
No files matched your search
@@ -0,0 +1,12 @@
|
||||
namespace Net.Data.WMS.Enums
|
||||
{
|
||||
public class EnumRecibo
|
||||
{
|
||||
public const int IMPORTACION = 1;
|
||||
public const int IMPORTACION_ORDEN = 1;
|
||||
public const int IMPORTACION_ORDEN_COMPRA = 2;
|
||||
public const int IMPORTACION_SOL_ENTRADA = 10;
|
||||
public const int NACIONAL = 2;
|
||||
public const int DEVOLUCION = 3;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.WMS.Inventario;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Inventario
|
||||
{
|
||||
public interface IInventarioRepository
|
||||
{
|
||||
Task<BE_ResultadoTransaccion<object>> Create(BE_Inventario entidad);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,601 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.ServicesLayer.InventoryGenEntries;
|
||||
using Net.Business.Entities.ServicesLayer.InventoryGenExits;
|
||||
using Net.Business.Entities.ServicesLayer.Items;
|
||||
using Net.Business.Entities.ServicesLayer.StockTransfer;
|
||||
using Net.Business.Entities.WMS.Inventario;
|
||||
using Net.Connection;
|
||||
using Net.CrossCotting;
|
||||
using Net.Data.HelperData;
|
||||
using Net.Data.HelperData.Inventario;
|
||||
using Net.Data.HelperData.Stock;
|
||||
using Net.Data.ServicesLayer.InventoryGenEntries;
|
||||
using Net.Data.ServicesLayer.InventoryGenExits;
|
||||
using Net.Data.ServicesLayer.StockTransfer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Inventario
|
||||
{
|
||||
public class InventarioRepository : RepositoryBase<object>, IInventarioRepository
|
||||
{
|
||||
#region Variables Globales
|
||||
private string _aplicacionName;
|
||||
private string _metodoName;
|
||||
private readonly Regex regex = new Regex(@"<(\w+)>.*");
|
||||
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
#endregion
|
||||
|
||||
public InventarioRepository(IConnectionSQL context, IHttpClientFactory clientFactory, IConfiguration configuration) : base(context)
|
||||
{
|
||||
_clientFactory = clientFactory;
|
||||
_configuration = configuration;
|
||||
_aplicacionName = GetType().Name;
|
||||
}
|
||||
|
||||
public async Task<BE_ResultadoTransaccion<object>> Create(BE_Inventario entidad)
|
||||
{
|
||||
_metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString();
|
||||
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>(_metodoName, _aplicacionName);
|
||||
|
||||
if (string.IsNullOrEmpty(entidad.TipoMovimiento))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El campo Tipo Movimiento no puede ser vacio", entidad);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(entidad.TipoMovimientoSAP))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El campo Tipo Movimiento SAP no puede ser vacio", entidad);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(entidad.TipoInventario))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El campo Tipo Inventario no puede ser vacio", entidad);
|
||||
}
|
||||
|
||||
//=== ASIGNA PRECIO PROMEDIO (solo aplica a entradas de mercancia)
|
||||
if (entidad.TipoMovimiento == "01")
|
||||
{
|
||||
foreach (var x in entidad.Detalle)
|
||||
{
|
||||
var Stock = HelperStock.GetConsolidado(x.CodigoArticulo, entidad.SubAlmacen);
|
||||
|
||||
// Un ingreso puede crear el primer stock del articulo: sin datos se conserva el precio del request
|
||||
if (Stock.Count > 0)
|
||||
{
|
||||
x.PrecioPromedio = Stock.FirstOrDefault().AvgPrice;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var ListaxLista = new List<BE_Inventario>();
|
||||
var lista = entidad.Detalle;
|
||||
for (int i = 0; i < lista.Count; i += 100)
|
||||
{
|
||||
var bloque = lista.Skip(i).Take(100).ToList();
|
||||
// Copia por bloque: antes se reutilizaba "entidad" y todos los bloques
|
||||
// terminaban apuntando al ultimo detalle (documentos repetidos y lineas perdidas)
|
||||
var Inventario = new BE_Inventario()
|
||||
{
|
||||
Fecha = entidad.Fecha,
|
||||
FechaVencimiento = entidad.FechaVencimiento,
|
||||
TipoMovimientoSAP = entidad.TipoMovimientoSAP,
|
||||
TipoMovimiento = entidad.TipoMovimiento,
|
||||
TipoInventario = entidad.TipoInventario,
|
||||
Almacen = entidad.Almacen,
|
||||
SubAlmacen = entidad.SubAlmacen,
|
||||
NroInventario = entidad.NroInventario,
|
||||
Detalle = bloque
|
||||
};
|
||||
ListaxLista.Add(Inventario);
|
||||
}
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
string CuentaContable = HelperInventario.Get(entidad.TipoMovimientoSAP);
|
||||
|
||||
if (string.IsNullOrEmpty(CuentaContable))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("No existe cuenta contable configurado para el tipo movimiento SAP", entidad);
|
||||
}
|
||||
|
||||
//=== OBTIENE PRECIO PROMEDIO (solo aplica a entradas de mercancia)
|
||||
if (entidad.TipoMovimiento == "01")
|
||||
{
|
||||
entidad.Detalle.ForEach(detalle =>
|
||||
{
|
||||
var ListaStockConsolidado = HelperStock.GetConsolidado(detalle.CodigoArticulo, entidad.Almacen);
|
||||
|
||||
// Un ingreso puede crear el primer stock del articulo: sin datos se conserva el precio del request
|
||||
if (ListaStockConsolidado.Count > 0)
|
||||
{
|
||||
detalle.PrecioPromedio = ListaStockConsolidado.FirstOrDefault().AvgPrice;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//=== TIPO DE INVENTARIO GENERAL
|
||||
if (entidad.TipoInventario == "01")
|
||||
{
|
||||
//=== INGRESO DE MERCANCIA
|
||||
if (entidad.TipoMovimiento == "01")
|
||||
{
|
||||
int DocEntry = 0;
|
||||
int DocNum = 0;
|
||||
foreach (var entidad_inventario in ListaxLista)
|
||||
{
|
||||
BE_InventoryGenEntries _InventoryGenEntries = new BE_InventoryGenEntries()
|
||||
{
|
||||
DocObjectCode = "oInventoryGenEntry",
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
PaymentGroupCode = "-2",
|
||||
DocCurrency = "S/",
|
||||
DocRate = 1,
|
||||
DocumentLines = new List<BE_InventoryGenEntriesLine>(),
|
||||
U_SYP_TPOENTME = entidad_inventario.TipoMovimientoSAP,
|
||||
U_ITC_WMS_NUMINV = entidad_inventario.NroInventario
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad_inventario.Detalle.ForEach(detalle =>
|
||||
{
|
||||
_InventoryGenEntries.DocumentLines.Add(new BE_InventoryGenEntriesLine()
|
||||
{
|
||||
Currency = "S/",
|
||||
ItemCode = detalle.CodigoArticulo,
|
||||
ItemDescription = detalle.DescripcionArticulo,
|
||||
Quantity = (double)detalle.Cantidad,
|
||||
LineNum = LineNum,
|
||||
PriceAfterVAT = detalle.PrecioPromedio,
|
||||
LineTotal = detalle.PrecioPromedio * (double)detalle.Cantidad,
|
||||
WarehouseCode = entidad.SubAlmacen,
|
||||
AccountCode = CuentaContable,
|
||||
CostingCode = detalle.CentroCosto ?? "",
|
||||
BatchNumbers = new List<BE_InventoryGenEntriesBatchNumbers>()
|
||||
{
|
||||
new BE_InventoryGenEntriesBatchNumbers()
|
||||
{
|
||||
BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
|
||||
Quantity = (double)detalle.Cantidad
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
InventoryGenEntriesRepository inventoryGenEntries = new InventoryGenEntriesRepository(_clientFactory, _configuration, context);
|
||||
var inventoryGenEntries_result = inventoryGenEntries.SetCreateInventoryGenEntries(_InventoryGenEntries, false).Result;
|
||||
if (inventoryGenEntries_result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(inventoryGenEntries_result.Message.ToString(), entidad);
|
||||
}
|
||||
|
||||
DocEntry = (int)inventoryGenEntries_result.Data.DocEntry;
|
||||
DocNum = (int)inventoryGenEntries_result.Data.DocNum;
|
||||
};
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(DocEntry, DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
|
||||
//=== SALIDA DE MERCANCIA
|
||||
if (entidad.TipoMovimiento == "02")
|
||||
{
|
||||
int DocEntry = 0;
|
||||
int DocNum = 0;
|
||||
foreach (var entidad_inventario in ListaxLista)
|
||||
{
|
||||
BE_InventoryGenExits _InventoryGenExits = new BE_InventoryGenExits()
|
||||
{
|
||||
DocObjectCode = "oInventoryGenExit",
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
PaymentGroupCode = "-2",
|
||||
DocCurrency = "S/",
|
||||
DocRate = 1,
|
||||
DocumentLines = new List<BE_InventoryGenExitsLine>(),
|
||||
U_SYP_TPOSALME = entidad_inventario.TipoMovimientoSAP,
|
||||
U_ITC_WMS_NUMINV = entidad_inventario.NroInventario
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad_inventario.Detalle.ForEach(detalle =>
|
||||
{
|
||||
_InventoryGenExits.DocumentLines.Add(new BE_InventoryGenExitsLine()
|
||||
{
|
||||
Currency = "S/",
|
||||
ItemCode = detalle.CodigoArticulo,
|
||||
ItemDescription = detalle.DescripcionArticulo,
|
||||
Quantity = (double)detalle.Cantidad,
|
||||
LineNum = LineNum,
|
||||
PriceAfterVAT = detalle.PrecioPromedio,
|
||||
LineTotal = detalle.PrecioPromedio * (double)detalle.Cantidad,
|
||||
WarehouseCode = entidad.SubAlmacen,
|
||||
AccountCode = CuentaContable,
|
||||
BatchNumbers = new List<BE_InventoryGenExitsBatchNumbers>(),
|
||||
CostingCode = detalle.CentroCosto ?? "",
|
||||
});
|
||||
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
_InventoryGenExits.DocumentLines.ForEach(line =>
|
||||
{
|
||||
#region CONSULTA A HANA
|
||||
|
||||
List<BE_ItemStock> ListaStock = HelperStock.Get(line.ItemCode, entidad.SubAlmacen);
|
||||
var CantidadPendiente = line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock.OrderBy(x => x.ExpDate).ToList();
|
||||
var cantidadStock = ListaStock.Sum(x => x.OnHandQty);
|
||||
if (cantidadStock < line.Quantity)
|
||||
{
|
||||
throw new Exception($"El producto {line.ItemCode} no cuenta con stock suficiente");
|
||||
}
|
||||
else
|
||||
{
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.OnHandQty >= CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_InventoryGenExitsBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.OnHandQty < CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_InventoryGenExitsBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = Lote.OnHandQty });
|
||||
CantidadPendiente -= Lote.OnHandQty;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
});
|
||||
|
||||
InventoryGenExitsRepository inventoryGenExits = new InventoryGenExitsRepository(_clientFactory, _configuration, context);
|
||||
var inventoryGenExits_result = inventoryGenExits.SetCreateInventoryGenExits(_InventoryGenExits, false).Result;
|
||||
if (inventoryGenExits_result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(inventoryGenExits_result.Message.ToString(), entidad);
|
||||
}
|
||||
DocEntry = (int)inventoryGenExits_result.Data.DocEntry;
|
||||
DocNum = (int)inventoryGenExits_result.Data.DocNum;
|
||||
};
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(DocEntry, DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
}
|
||||
|
||||
//=== TIPO DE INVENTARIO CICLICO
|
||||
if (entidad.TipoInventario == "02")
|
||||
{
|
||||
//=== INGRESO DE MERCANCIA
|
||||
if (entidad.TipoMovimiento == "01")
|
||||
{
|
||||
int DocEntry = 0;
|
||||
int DocNum = 0;
|
||||
foreach (var entidad_inventario in ListaxLista)
|
||||
{
|
||||
BE_InventoryGenEntries _InventoryGenEntries = new BE_InventoryGenEntries()
|
||||
{
|
||||
DocObjectCode = "oInventoryGenEntry",
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
PaymentGroupCode = "-2",
|
||||
DocCurrency = "S/",
|
||||
DocRate = 1,
|
||||
DocumentLines = new List<BE_InventoryGenEntriesLine>(),
|
||||
U_SYP_TPOENTME = entidad_inventario.TipoMovimientoSAP,
|
||||
U_ITC_WMS_NUMINV = entidad_inventario.NroInventario
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad_inventario.Detalle.ForEach(detalle =>
|
||||
{
|
||||
_InventoryGenEntries.DocumentLines.Add(new BE_InventoryGenEntriesLine()
|
||||
{
|
||||
Currency = "S/",
|
||||
ItemCode = detalle.CodigoArticulo,
|
||||
ItemDescription = detalle.DescripcionArticulo,
|
||||
Quantity = (double)detalle.Cantidad,
|
||||
LineNum = LineNum,
|
||||
PriceAfterVAT = detalle.PrecioPromedio,
|
||||
LineTotal = detalle.PrecioPromedio * (double)detalle.Cantidad,
|
||||
WarehouseCode = entidad.SubAlmacen,
|
||||
AccountCode = CuentaContable,
|
||||
CostingCode = detalle.CentroCosto ?? "",
|
||||
BatchNumbers = new List<BE_InventoryGenEntriesBatchNumbers>()
|
||||
{
|
||||
new BE_InventoryGenEntriesBatchNumbers()
|
||||
{
|
||||
BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
|
||||
Quantity = (double)detalle.Cantidad
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
InventoryGenEntriesRepository inventoryGenEntries = new InventoryGenEntriesRepository(_clientFactory, _configuration, context);
|
||||
var inventoryGenEntries_result = inventoryGenEntries.SetCreateInventoryGenEntries(_InventoryGenEntries).Result;
|
||||
if (inventoryGenEntries_result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(inventoryGenEntries_result.Message.ToString(), entidad);
|
||||
}
|
||||
DocEntry = (int)inventoryGenEntries_result.Data.DocEntry;
|
||||
DocNum = (int)inventoryGenEntries_result.Data.DocNum;
|
||||
};
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(DocEntry, DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
|
||||
//=== SALIDA DE MERCANCIA [GENERA TRANSFERENCIA]
|
||||
if (entidad.TipoMovimiento == "02")
|
||||
{
|
||||
string AlmacenDestino = "ALM12";
|
||||
//=== CREA INSTANCIA PARA CONECTAR A SAP
|
||||
StockTransferRepository stockTransferRepository = new StockTransferRepository(_clientFactory, _configuration);
|
||||
int DocEntry = 0;
|
||||
int DocNum = 0;
|
||||
foreach (var entidad_inventario in ListaxLista)
|
||||
{
|
||||
BE_StockTransfer StockTransfer = new BE_StockTransfer
|
||||
{
|
||||
DocDate = DateTime.Now,
|
||||
DueDate = DateTime.Now,
|
||||
TaxDate = DateTime.Now,
|
||||
FromWarehouse = entidad.SubAlmacen,
|
||||
ToWarehouse = AlmacenDestino,
|
||||
Comments = "Transferencia Generado desde Cubico - SAP",
|
||||
StockTransferLines = new List<BE_StockTransferLine>(),
|
||||
U_ITC_WMS_NUMINV = entidad.NroInventario,
|
||||
U_SYP_TPOENTME = entidad.TipoMovimientoSAP
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad_inventario.Detalle.ForEach(x =>
|
||||
{
|
||||
var Detalle = new BE_StockTransferLine
|
||||
{
|
||||
FromWarehouseCode = entidad.SubAlmacen,
|
||||
WarehouseCode = AlmacenDestino,
|
||||
ItemCode = x.CodigoArticulo,
|
||||
Quantity = (double)x.Cantidad,
|
||||
BaseLine = null,
|
||||
BaseEntry = null,
|
||||
BaseType = null,
|
||||
LineNum = LineNum,
|
||||
BatchNumbers = new List<BE_StockTransferLineBatchNumber>()
|
||||
};
|
||||
StockTransfer.StockTransferLines.Add(Detalle);
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
// == REALIZA DISTRIBUCION DE STOCK POR LOTE / ALMACEN
|
||||
StockTransfer.StockTransferLines.ForEach(line =>
|
||||
{
|
||||
#region CONSULTA A HANA
|
||||
|
||||
List<BE_ItemStock> ListaStock = HelperStock.Get(line.ItemCode, entidad.SubAlmacen);
|
||||
var CantidadPendiente = line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock.OrderBy(x => x.ExpDate).ToList();
|
||||
var cantidadStock = ListaStock.Sum(x => x.OnHandQty);
|
||||
if (cantidadStock < line.Quantity)
|
||||
{
|
||||
throw new Exception($"El producto {line.ItemCode} no cuenta con stock suficiente");
|
||||
}
|
||||
else
|
||||
{
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.OnHandQty >= CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_StockTransferLineBatchNumber() { BatchNumber = Lote.DistNumber, Quantity = CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.OnHandQty < CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_StockTransferLineBatchNumber() { BatchNumber = Lote.DistNumber, Quantity = Lote.OnHandQty });
|
||||
CantidadPendiente -= Lote.OnHandQty;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
});
|
||||
var jsonForma = Newtonsoft.Json.JsonConvert.SerializeObject(StockTransfer);
|
||||
var ResultTransaction = stockTransferRepository.StockTransfer(StockTransfer).Result;
|
||||
if (ResultTransaction.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ResultTransaction.Message.ToString(), entidad);
|
||||
}
|
||||
DocEntry = (int)ResultTransaction.Data.DocEntry;
|
||||
DocNum = (int)ResultTransaction.Data.DocNum;
|
||||
};
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(DocEntry, DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
}
|
||||
|
||||
//=== TIPO DE INVENTARIO POR CONSUMO
|
||||
if (entidad.TipoInventario == "03")
|
||||
{
|
||||
//=== INGRESO DE MERCANCIA
|
||||
if (entidad.TipoMovimiento == "01")
|
||||
{
|
||||
int DocEntry = 0;
|
||||
int DocNum = 0;
|
||||
foreach (var entidad_inventario in ListaxLista)
|
||||
{
|
||||
BE_InventoryGenEntries _InventoryGenEntries = new BE_InventoryGenEntries()
|
||||
{
|
||||
DocObjectCode = "oInventoryGenEntry",
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
PaymentGroupCode = "-2",
|
||||
DocCurrency = "S/",
|
||||
DocRate = 1,
|
||||
DocumentLines = new List<BE_InventoryGenEntriesLine>(),
|
||||
U_SYP_TPOENTME = entidad.TipoMovimientoSAP,
|
||||
U_ITC_WMS_NUMINV = entidad.NroInventario
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad_inventario.Detalle.ForEach(detalle =>
|
||||
{
|
||||
_InventoryGenEntries.DocumentLines.Add(new BE_InventoryGenEntriesLine()
|
||||
{
|
||||
Currency = "S/",
|
||||
ItemCode = detalle.CodigoArticulo,
|
||||
ItemDescription = detalle.DescripcionArticulo,
|
||||
Quantity = (double)detalle.Cantidad,
|
||||
LineNum = LineNum,
|
||||
PriceAfterVAT = detalle.PrecioPromedio,
|
||||
LineTotal = detalle.PrecioPromedio * (double)detalle.Cantidad,
|
||||
WarehouseCode = entidad.SubAlmacen,
|
||||
AccountCode = CuentaContable,
|
||||
CostingCode = detalle.CentroCosto ?? "",
|
||||
BatchNumbers = new List<BE_InventoryGenEntriesBatchNumbers>()
|
||||
{
|
||||
new BE_InventoryGenEntriesBatchNumbers()
|
||||
{
|
||||
BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
|
||||
Quantity = (double)detalle.Cantidad
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
InventoryGenEntriesRepository inventoryGenEntries = new InventoryGenEntriesRepository(_clientFactory, _configuration, context);
|
||||
var inventoryGenEntries_result = inventoryGenEntries.SetCreateInventoryGenEntries(_InventoryGenEntries, false).Result;
|
||||
if (inventoryGenEntries_result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(inventoryGenEntries_result.Message.ToString(), entidad);
|
||||
}
|
||||
DocEntry = (int)inventoryGenEntries_result.Data.DocEntry;
|
||||
DocNum = (int)inventoryGenEntries_result.Data.DocNum;
|
||||
};
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(DocEntry, DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
// return resultadoTransaccion.GetInformacionResultado((int)inventoryGenEntries_result.Data.DocEntry, inventoryGenEntries_result.Data.DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
|
||||
//=== SALIDA DE MERCANCIA
|
||||
if (entidad.TipoMovimiento == "02")
|
||||
{
|
||||
int DocEntry = 0;
|
||||
int DocNum = 0;
|
||||
foreach (var entidad_inventario in ListaxLista)
|
||||
{
|
||||
BE_InventoryGenExits _InventoryGenExits = new BE_InventoryGenExits()
|
||||
{
|
||||
DocObjectCode = "oInventoryGenExit",
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
PaymentGroupCode = "-2",
|
||||
DocCurrency = "S/",
|
||||
DocRate = 1,
|
||||
DocumentLines = new List<BE_InventoryGenExitsLine>(),
|
||||
U_SYP_TPOSALME = entidad.TipoMovimientoSAP,
|
||||
U_ITC_WMS_NUMINV = entidad.NroInventario
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad_inventario.Detalle.ForEach(detalle =>
|
||||
{
|
||||
_InventoryGenExits.DocumentLines.Add(new BE_InventoryGenExitsLine()
|
||||
{
|
||||
Currency = "S/",
|
||||
ItemCode = detalle.CodigoArticulo,
|
||||
ItemDescription = detalle.DescripcionArticulo,
|
||||
Quantity = (double)detalle.Cantidad,
|
||||
LineNum = LineNum,
|
||||
PriceAfterVAT = detalle.PrecioPromedio,
|
||||
LineTotal = detalle.PrecioPromedio * (double)detalle.Cantidad,
|
||||
WarehouseCode = entidad.SubAlmacen,
|
||||
AccountCode = CuentaContable,
|
||||
BatchNumbers = new List<BE_InventoryGenExitsBatchNumbers>(),
|
||||
CostingCode = detalle.CentroCosto ?? "",
|
||||
});
|
||||
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
_InventoryGenExits.DocumentLines.ForEach(line =>
|
||||
{
|
||||
#region CONSULTA A HANA
|
||||
|
||||
List<BE_ItemStock> ListaStock = HelperStock.Get(line.ItemCode, entidad.SubAlmacen);
|
||||
var CantidadPendiente = line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock.OrderBy(x => x.ExpDate).ToList();
|
||||
var cantidadStock = ListaStock.Sum(x => x.OnHandQty);
|
||||
if (cantidadStock < line.Quantity)
|
||||
{
|
||||
throw new Exception($"El producto {line.ItemCode} no cuenta con stock suficiente");
|
||||
}
|
||||
else
|
||||
{
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.OnHandQty >= CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_InventoryGenExitsBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.OnHandQty < CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_InventoryGenExitsBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = Lote.OnHandQty });
|
||||
CantidadPendiente -= Lote.OnHandQty;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
});
|
||||
InventoryGenExitsRepository inventoryGenExits = new InventoryGenExitsRepository(_clientFactory, _configuration, context);
|
||||
var inventoryGenExits_result = inventoryGenExits.SetCreateInventoryGenExits(_InventoryGenExits, false).Result;
|
||||
if (inventoryGenExits_result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(inventoryGenExits_result.Message.ToString(), entidad);
|
||||
}
|
||||
DocEntry = (int)inventoryGenExits_result.Data.DocEntry;
|
||||
DocNum = (int)inventoryGenExits_result.Data.DocNum;
|
||||
};
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(DocEntry, DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
//return resultadoTransaccion.GetInformacionResultado((int)inventoryGenExits_result.Data.DocEntry, inventoryGenExits_result.Data.DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
}
|
||||
return resultadoTransaccion.GetInformacionResultado(0, "", "Registrado Correctamente", entidad);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), entidad);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.ServicesLayer.Items;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Items
|
||||
{
|
||||
public interface IItemsRepository
|
||||
{
|
||||
Task<BE_ResultadoTransaccion<BE_ItemStock>> GetStockItem();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.ServicesLayer.Items;
|
||||
using Net.Connection;
|
||||
using Net.CrossCotting;
|
||||
using Sap.Data.Hana;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Items
|
||||
{
|
||||
public class ItemsRepository : RepositoryBase<object>, IItemsRepository
|
||||
{
|
||||
#region Variables Globales
|
||||
private string _aplicacionName;
|
||||
private string _metodoName;
|
||||
private readonly Regex regex = new Regex(@"<(\w+)>.*");
|
||||
|
||||
#endregion
|
||||
|
||||
public ItemsRepository(IConnectionSQL context, IHttpClientFactory clientFactory, IConfiguration configuration) : base(context)
|
||||
{
|
||||
_aplicacionName = GetType().Name;
|
||||
}
|
||||
|
||||
public async Task<BE_ResultadoTransaccion<BE_ItemStock>> GetStockItem()
|
||||
{
|
||||
_metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString();
|
||||
|
||||
ResultadoTransaccion<BE_ItemStock> resultadoTransaccion = new ResultadoTransaccion<BE_ItemStock>(_metodoName, _aplicacionName);
|
||||
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
List<BE_ItemStock> ListaStock = new List<BE_ItemStock>();
|
||||
DataTable dt = new DataTable();
|
||||
using (HanaConnection con = new HanaConnection(LocalData.ConnectionString))
|
||||
{
|
||||
con.Open();
|
||||
HanaCommand cmd = new HanaCommand($"CALL\"{LocalData.Schema}\".\"ITCA_WMS_SAP_GetStockConsolidado\"", con);
|
||||
HanaDataAdapter da = new HanaDataAdapter(cmd);
|
||||
if (da != null)
|
||||
{
|
||||
da.Fill(dt);
|
||||
}
|
||||
}
|
||||
|
||||
for (int x = 0; x < dt.Rows.Count; x++)
|
||||
{
|
||||
ListaStock.Add(new BE_ItemStock()
|
||||
{
|
||||
BinCode = dt.Rows[x]["BinCode"] != DBNull.Value ? Convert.ToString(dt.Rows[x]["BinCode"]) : string.Empty,
|
||||
DistNumber = dt.Rows[x]["DistNumber"] != DBNull.Value ? Convert.ToString(dt.Rows[x]["DistNumber"]) : string.Empty,
|
||||
ExpDate = dt.Rows[x]["ExpDate"] != DBNull.Value ? Convert.ToDateTime(dt.Rows[x]["ExpDate"]) : DateTime.Now,
|
||||
ItemCode = dt.Rows[x]["ItemCode"] != DBNull.Value ? Convert.ToString(dt.Rows[x]["ItemCode"]) : string.Empty,
|
||||
ItemName = dt.Rows[x]["ItemName"] != DBNull.Value ? Convert.ToString(dt.Rows[x]["ItemName"]) : string.Empty,
|
||||
OnHandQty = dt.Rows[x]["OnHandQty"] != DBNull.Value ? Convert.ToDouble(dt.Rows[x]["OnHandQty"]) : 0,
|
||||
WhsCode = dt.Rows[x]["WhsCode"] != DBNull.Value ? Convert.ToString(dt.Rows[x]["WhsCode"]) : string.Empty,
|
||||
WhsName = dt.Rows[x]["WhsName"] != DBNull.Value ? Convert.ToString(dt.Rows[x]["WhsName"]) : string.Empty,
|
||||
AvgPrice = dt.Rows[x]["AvgPrice"] != DBNull.Value ? Convert.ToDouble(dt.Rows[x]["AvgPrice"]) : 0,
|
||||
});
|
||||
}
|
||||
return resultadoTransaccion.GetInformacionResultado(0, 0, "Datos obtenidos Correctamente", ListaStock);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.WMS.HojaRuta;
|
||||
using Net.Business.Entities.WMS.Picking;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Picking
|
||||
{
|
||||
public interface IPickingRepository
|
||||
{
|
||||
Task<BE_ResultadoTransaccion<object>> Create(BE_PickingRequest entidad);
|
||||
Task<BE_ResultadoTransaccion<object>> CreateHojaRuta(BE_HojaRutaEntities entidad);
|
||||
Task<BE_ResultadoTransaccion<object>> CerrarHojaRuta(int docEntry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,794 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.ServicesLayer;
|
||||
using Net.Business.Entities.ServicesLayer.CreditNotes;
|
||||
using Net.Business.Entities.ServicesLayer.InventoryGenEntries;
|
||||
using Net.Business.Entities.ServicesLayer.InventoryGenExits;
|
||||
using Net.Business.Entities.ServicesLayer.Invoices;
|
||||
using Net.Business.Entities.ServicesLayer.Items;
|
||||
using Net.Business.Entities.ServicesLayer.ReturnRequest;
|
||||
using Net.Business.Entities.ServicesLayer.StockTransfer;
|
||||
using Net.Business.Entities.WMS.HojaRuta;
|
||||
using Net.Business.Entities.WMS.Picking;
|
||||
using Net.Business.Entities.WMS.ProcesoPicking;
|
||||
using Net.Connection;
|
||||
using Net.Connection.ServiceLayer;
|
||||
using Net.CrossCotting;
|
||||
using Net.Data.HelperData.Correlativo;
|
||||
using Net.Data.HelperData.HelperProcesoPermitido;
|
||||
using Net.Data.HelperData.Stock;
|
||||
using Net.Data.ServicesLayer;
|
||||
using Net.Data.ServicesLayer.CreditNotes;
|
||||
using Net.Data.ServicesLayer.GlobalData;
|
||||
using Net.Data.ServicesLayer.InventoryGenExits;
|
||||
using Net.Data.ServicesLayer.ReturnRequest;
|
||||
using Net.Data.ServicesLayer.StockTransfer;
|
||||
using Sap.Data.Hana;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Picking
|
||||
{
|
||||
public class PickingRepository : RepositoryBase<object>, IPickingRepository
|
||||
{
|
||||
#region Variables Globales
|
||||
private string _aplicacionName;
|
||||
private string _metodoName;
|
||||
private readonly Regex regex = new Regex(@"<(\w+)>.*");
|
||||
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
#endregion
|
||||
|
||||
public PickingRepository(IConnectionSQL context, IHttpClientFactory clientFactory, IConfiguration configuration) : base(context)
|
||||
{
|
||||
_clientFactory = clientFactory;
|
||||
_configuration = configuration;
|
||||
_aplicacionName = GetType().Name;
|
||||
}
|
||||
|
||||
public async Task<BE_ResultadoTransaccion<object>> Create(BE_PickingRequest entidad)
|
||||
{
|
||||
_metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString();
|
||||
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>(_metodoName, _aplicacionName);
|
||||
|
||||
if (entidad.DocEntry == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El campo docEntry no puede ser cero o vacio", entidad);
|
||||
}
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var Almacen = HelperProcesoPermitido.GetMotivoAlmacen(entidad.CodMotivo, out bool Reproceso, out string MotivoNotaCredito, out string DescripcionMotivo);
|
||||
|
||||
if (string.IsNullOrEmpty(entidad.Proceso))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Se necesita definir el proceso para continuar con el proceso.", entidad);
|
||||
}
|
||||
|
||||
if (entidad.IdEstadoCubico == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Se necesita indicar el IdEstado Cubico para continuar con el proceso.", entidad);
|
||||
}
|
||||
|
||||
int Existe = HelperProcesoPermitido.Get(entidad.Proceso, entidad.IdEstadoCubico, "PICKING", out List<BE_ProcesoPicking> ListaProceso);
|
||||
var Proceso = ListaProceso.FirstOrDefault();
|
||||
if (Existe == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El Proceso y estado no se encuentra configurado para este EndPoint", entidad);
|
||||
}
|
||||
|
||||
var TotalDetalle = 0;
|
||||
var TotalDetalle_Anulado = 0;
|
||||
var TotalDetalle_Parcial = 0;
|
||||
var TotalDetalle_Completo = 0;
|
||||
if (Existe > 1)
|
||||
{
|
||||
TotalDetalle = entidad.Detalle.Count();
|
||||
TotalDetalle_Anulado = entidad.Detalle.Where(x=> x.Estado == "A").Count();
|
||||
TotalDetalle_Parcial = entidad.Detalle.Where(x => x.Estado == "P").Count();
|
||||
TotalDetalle_Completo = entidad.Detalle.Where(x => x.Estado == "C").Count();
|
||||
if(TotalDetalle == TotalDetalle_Anulado)
|
||||
{
|
||||
Proceso = ListaProceso.FirstOrDefault(x => x.Code == "013");
|
||||
if (Reproceso)
|
||||
{
|
||||
Proceso = ListaProceso.FirstOrDefault(x => x.Code == "017");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Proceso = ListaProceso.FirstOrDefault(x => x.Code == "012");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TotalDetalle = entidad.Detalle.Count();
|
||||
TotalDetalle_Anulado = entidad.Detalle.Where(x => x.Estado == "A").Count();
|
||||
TotalDetalle_Parcial = entidad.Detalle.Where(x => x.Estado == "P").Count();
|
||||
TotalDetalle_Completo = entidad.Detalle.Where(x => x.Estado == "C").Count();
|
||||
}
|
||||
|
||||
//=== REALIZA PROCESO COMPLETO DE ACTUALIZACION
|
||||
HelperProcesoPiciking.Set(entidad.Proceso, entidad.IdEstadoCubico, entidad.DocEntry, entidad.CodMotivo, Proceso.Code);
|
||||
|
||||
// === NUEVO (Picking diurno/nocturno): "notificacion" distinta de "cierre"
|
||||
// (o ausente) es la notificacion preliminar en tiempo real - solo actualiza
|
||||
// estado en HANA (ya hecho arriba), NUNCA debe crear documentos en SAP.
|
||||
// Default seguro: cualquier valor que no sea "cierre" exacto cae aca.
|
||||
if (!string.Equals(entidad.Notificacion, "cierre", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(entidad.DocEntry, entidad.NumOrden, "Estado actualizado correctamente", entidad);
|
||||
}
|
||||
if(TotalDetalle == TotalDetalle_Completo && entidad.IdTipoDoc != 9)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(entidad.DocEntry, entidad.NumOrden, "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
|
||||
#region PROCESO DE NOTA DE CREDITO
|
||||
|
||||
if (entidad.Proceso.ToUpper() == "DESPACHO" && (entidad.IdEstadoCubico == 6 || entidad.IdEstadoCubico == 5))
|
||||
{
|
||||
if (entidad.IdTipoDoc != 9)
|
||||
{
|
||||
CreditNotesRepository _creditNotesRepository = new CreditNotesRepository(_clientFactory, _configuration, context);
|
||||
BE_CreditNotesNotes creditNotesNotes = new BE_CreditNotesNotes
|
||||
{
|
||||
DocObjectCode = "oCreditNotes",
|
||||
DocumentLines = new List<BE_CreditNotesLine>()
|
||||
};
|
||||
|
||||
InvoiceRepository invoicestRepository = new InvoiceRepository(_clientFactory, _configuration, context);
|
||||
BE_ResultadoTransaccion<BE_InvoicesConsulta> response_invoicesRepository = invoicestRepository.GetInvoice(entidad.DocEntry).Result;
|
||||
|
||||
if (response_invoicesRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: La Factura no se encuentra", entidad);
|
||||
}
|
||||
|
||||
|
||||
BE_InvoicesConsulta _invoices = response_invoicesRepository.ListData.FirstOrDefault();
|
||||
|
||||
if (_invoices.DocumentStatus != "bost_Open")
|
||||
{
|
||||
var Response_Reopen = invoicestRepository.ReOpen(_invoices.DocEntry).Result;
|
||||
if (Response_Reopen.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: La Factura no se puede reabrir", entidad);
|
||||
}
|
||||
}
|
||||
|
||||
string Serie = _invoices.U_SYP_MDTD == "03" ? "BC10" : "FC10";
|
||||
string Correlativo = HelperCorrelativo.Get(Serie);
|
||||
creditNotesNotes.DocCurrency = _invoices.DocCurrency;
|
||||
creditNotesNotes.CardCode = _invoices.CardCode;
|
||||
creditNotesNotes.CardName = _invoices.CardName;
|
||||
creditNotesNotes.PaymentGroupCode = _invoices.PaymentGroupCode;
|
||||
creditNotesNotes.DocDate = DateTime.Now;
|
||||
creditNotesNotes.DocDueDate = DateTime.Now;
|
||||
creditNotesNotes.U_SYP_MDTO = _invoices.U_SYP_MDTD;
|
||||
creditNotesNotes.U_SYP_MDCO = _invoices.U_SYP_MDCD;
|
||||
creditNotesNotes.U_SYP_MDSO = _invoices.U_SYP_MDSD;
|
||||
creditNotesNotes.U_SYP_MOTNC = DescripcionMotivo;
|
||||
creditNotesNotes.U_SYP_TPONC = MotivoNotaCredito;
|
||||
creditNotesNotes.U_SYP_MDCD = Correlativo;
|
||||
creditNotesNotes.U_SYP_MDSD = Serie;
|
||||
creditNotesNotes.U_SYP_MDTD = "07";
|
||||
creditNotesNotes.U_SYP_FECHAREF = _invoices.DocDate.ToString("yyyy-MM-dd");
|
||||
creditNotesNotes.NumAtCard = ("07-" + Serie + "-" + Correlativo);
|
||||
|
||||
entidad.Detalle.ForEach(x =>
|
||||
{
|
||||
var Item = _invoices.DocumentLines.ToList().FirstOrDefault(t => t.LineNum == x.Item);
|
||||
if (Item != null)
|
||||
{
|
||||
if (Item.Quantity - x.CantidadPedida > 0)
|
||||
{
|
||||
creditNotesNotes.DocumentLines.Add(new BE_CreditNotesLine()
|
||||
{
|
||||
Currency = _invoices.DocCurrency,
|
||||
BatchNumbers = new List<BE_CreditNotesBatchNumbers>(),
|
||||
ItemCode = Item.ItemCode,
|
||||
ItemDescription = Item.ItemDescription,
|
||||
PriceAfterVAT = Item.PriceAfterVAT,
|
||||
Quantity = Item.Quantity - x.CantidadPedida,
|
||||
WarehouseCode = Item.WarehouseCode,
|
||||
BaseEntry = entidad.DocEntry,
|
||||
BaseLine = x.Item,
|
||||
BaseType = 13,
|
||||
LineNum = x.Item,
|
||||
U_ITC_ALMDES = ((Almacen == "ALMPRO") ? Item.WarehouseCode : Almacen),
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// == REALIZA DISTRIBUCION DE STOCK POR LOTE / ALMACEN
|
||||
creditNotesNotes.DocumentLines.ForEach(line =>
|
||||
{
|
||||
#region CONSULTA A HANA
|
||||
|
||||
//List<BE_ItemStock> ListaStock = HelperStock.Get(line.ItemCode, entidad.CodigoAlmacen);
|
||||
var ReferenciaFactura = _invoices.DocumentLines.FirstOrDefault(t => t.LineNum == line.LineNum);
|
||||
var ListaStock = _invoices.DocumentLines.FirstOrDefault(InvoiceLine => InvoiceLine.LineNum == ReferenciaFactura.BaseLine).BatchNumbers;
|
||||
|
||||
double CantidadPendiente = (double)line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock;
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.Quantity >= CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = Lote.BatchNumber, Quantity = CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.Quantity < CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = Lote.BatchNumber, Quantity = Lote.Quantity });
|
||||
CantidadPendiente -= Lote.Quantity;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
#endregion
|
||||
});
|
||||
|
||||
if (creditNotesNotes.DocumentLines.Count > 0)
|
||||
{
|
||||
var result_creditNotesRepository = _creditNotesRepository.SetCreateCreditNotes(creditNotesNotes).Result;
|
||||
|
||||
if (result_creditNotesRepository.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(result_creditNotesRepository.Message.ToString(), entidad);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<string> ListaUnicaAlmacen = creditNotesNotes.DocumentLines.Select(x => x.WarehouseCode).Distinct().ToList();
|
||||
List<int> ListDocEntry = new List<int>();
|
||||
ListaUnicaAlmacen.ForEach(Almacen =>
|
||||
{
|
||||
List<string> ListaUnicaAlmDestino = creditNotesNotes.DocumentLines.Where(Linea => Linea.WarehouseCode == Almacen).ToList().Select(x => x.U_ITC_ALMDES).Distinct().ToList().Where(x => !string.IsNullOrEmpty(x)).ToList();
|
||||
ListaUnicaAlmDestino.ForEach(Destino =>
|
||||
{
|
||||
if (Almacen != Destino)
|
||||
{
|
||||
BE_StockTransferAutomatico Transfer = new BE_StockTransferAutomatico();
|
||||
Transfer.FromWarehouse = Almacen;
|
||||
Transfer.ToWarehouse = Destino;
|
||||
Transfer.DocDate = creditNotesNotes.DocDate;
|
||||
Transfer.TaxDate = creditNotesNotes.DocDate;
|
||||
Transfer.U_SYP_MDTS = "TSI";
|
||||
Transfer.U_SYP_MDMT = "24";
|
||||
Transfer.DueDate = creditNotesNotes.DocDueDate;
|
||||
Transfer.PriceList = -2;
|
||||
var ListaDetalle_PorAlmacen = creditNotesNotes.DocumentLines.Where(x => x.WarehouseCode == Almacen && x.U_ITC_ALMDES == Destino).ToList();
|
||||
var ListaUnicaArticulo = ListaDetalle_PorAlmacen.Select(x => x.ItemCode).Distinct().ToList();
|
||||
Transfer.StockTransferLines = new List<BE_StockTransferLine>();
|
||||
Transfer.DocumentReferences = new List<BE_StockTransferDocumentReferences>();
|
||||
ListaUnicaArticulo.ForEach(x =>
|
||||
{
|
||||
var ListLote = new List<BE_StockTransferLineBatchNumber>();
|
||||
ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).FirstOrDefault().BatchNumbers.ForEach(r =>
|
||||
{
|
||||
ListLote.Add(new BE_StockTransferLineBatchNumber()
|
||||
{
|
||||
BatchNumber = r.BatchNumber,
|
||||
Quantity = r.Quantity
|
||||
});
|
||||
});
|
||||
Transfer.StockTransferLines.Add(new BE_StockTransferLine()
|
||||
{
|
||||
|
||||
ItemCode = x,
|
||||
ItemDescription = ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).FirstOrDefault().ItemDescription,
|
||||
Quantity = Convert.ToDouble(ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).Sum(t => t.Quantity)),
|
||||
WarehouseCode = Destino,
|
||||
FromWarehouseCode = Almacen,
|
||||
BatchNumbers = ListLote
|
||||
});
|
||||
|
||||
|
||||
//=== AGREGA DOCUMENTO REFERENCIA
|
||||
Transfer.DocumentReferences = new List<BE_StockTransferDocumentReferences>
|
||||
{
|
||||
new BE_StockTransferDocumentReferences()
|
||||
{
|
||||
RefDocEntr = (int)result_creditNotesRepository.Data.DocEntry,
|
||||
RefObjType = "rot_SalesCreditNote",
|
||||
IssueDate = DateTime.Now,
|
||||
Remark = "Nota de Credito",
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
//=== CREA INSTANCIA PARA CONECTAR A SAP
|
||||
StockTransferRepository stockTransferRepository = new StockTransferRepository(_clientFactory, _configuration);
|
||||
var ResultTransfer = stockTransferRepository.StockTransfer(Transfer).Result;
|
||||
if (ResultTransfer.StatusCode == -1)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ListDocEntry.Add((int)ResultTransfer.Data.DocEntry);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
if (ListDocEntry.Count > 0)
|
||||
{
|
||||
var listaReferencia = new List<BE_CreditNotesDocumentReferences>();
|
||||
ListDocEntry.ForEach(x =>
|
||||
{
|
||||
listaReferencia.Add(new BE_CreditNotesDocumentReferences()
|
||||
{
|
||||
RefDocEntr = x,
|
||||
IssueDate = DateTime.Now,
|
||||
RefObjType = "rot_InventoryTransfer",
|
||||
Remark = "Transferencias Automatica"
|
||||
});
|
||||
});
|
||||
|
||||
var credit = new
|
||||
{
|
||||
DocumentReferences = listaReferencia
|
||||
};
|
||||
|
||||
var Actualizacion = _creditNotesRepository.UpdateCreateCreditNotes(credit, (int)result_creditNotesRepository.Data.DocEntry).Result;
|
||||
}
|
||||
return resultadoTransaccion.GetInformacionResultado((int)result_creditNotesRepository.Data.DocEntry, result_creditNotesRepository.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado((int)entidad.DocEntry, entidad.NumOrden.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//=== ASIGNA PRECIO PROMEDIO
|
||||
foreach (var x in entidad.Detalle)
|
||||
{
|
||||
var Stock = HelperStock.GetConsolidado(x.CodigoArticulo, entidad.CodigoAlmacen);
|
||||
if (Stock.Count == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("No existe stock disponible para el articulo " + x.CodigoArticulo, entidad);
|
||||
}
|
||||
x.PrecioPromedio = Stock.FirstOrDefault().AvgPrice;
|
||||
};
|
||||
|
||||
GlobalDataRepository globalDataRepository = new GlobalDataRepository(_clientFactory, _configuration, context);
|
||||
|
||||
var Data_SolicitudSalida = globalDataRepository.GetSolicitudSalida(entidad.DocEntry).Result.ListData.FirstOrDefault();
|
||||
|
||||
InventoryGenExitsRepository inventoryGenExitsRepository = new InventoryGenExitsRepository(_clientFactory, _configuration, context);
|
||||
BE_InventoryGenExits _InventoryGenExits = new BE_InventoryGenExits()
|
||||
{
|
||||
DocObjectCode = "oInventoryGenExit",
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
PaymentGroupCode = "-2",
|
||||
DocCurrency = "S/",
|
||||
DocRate = 1,
|
||||
DocumentLines = new List<BE_InventoryGenExitsLine>(),
|
||||
U_SYP_TPOSALME = Data_SolicitudSalida.U_ITC_MDTS,
|
||||
U_ITC_WMS_NUMINV = "",
|
||||
U_ARD_PERSOLIC = Data_SolicitudSalida.U_ITC_USRSOL,
|
||||
Comments = Data_SolicitudSalida.U_ITC_MDCM
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad.Detalle.ForEach(detalle =>
|
||||
{
|
||||
_InventoryGenExits.DocumentLines.Add(new BE_InventoryGenExitsLine()
|
||||
{
|
||||
Currency = "S/",
|
||||
ItemCode = detalle.CodigoArticulo,
|
||||
ItemDescription = Data_SolicitudSalida.ITCA_SOLSALMERDETCollection.FirstOrDefault(item_s => item_s.LineId == detalle.Item).U_ITC_ItemName,
|
||||
Quantity = (double)detalle.CantidadPedida,
|
||||
LineNum = LineNum,
|
||||
PriceAfterVAT = detalle.PrecioPromedio,
|
||||
LineTotal = detalle.PrecioPromedio * (double)detalle.CantidadPedida,
|
||||
WarehouseCode = entidad.CodigoAlmacen,
|
||||
AccountCode = Data_SolicitudSalida.ITCA_SOLSALMERDETCollection.FirstOrDefault(item_s => item_s.LineId == detalle.Item).U_ITC_AccCode,
|
||||
BatchNumbers = new List<BE_InventoryGenExitsBatchNumbers>(),
|
||||
CostingCode = Data_SolicitudSalida.ITCA_SOLSALMERDETCollection.FirstOrDefault(item_s => item_s.LineId == detalle.Item).U_ITC_CCOST,
|
||||
});
|
||||
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
|
||||
_InventoryGenExits.DocumentLines.ForEach(line =>
|
||||
{
|
||||
#region CONSULTA A HANA
|
||||
|
||||
List<BE_ItemStock> ListaStock = HelperStock.Get(line.ItemCode, entidad.CodigoAlmacen);
|
||||
var CantidadPendiente = line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock.OrderBy(x => x.ExpDate).ToList();
|
||||
var cantidadStock = ListaStock.Sum(x => x.OnHandQty);
|
||||
if (cantidadStock < line.Quantity)
|
||||
{
|
||||
throw new Exception($"El producto {line.ItemCode} no cuenta con stock suficiente");
|
||||
}
|
||||
else
|
||||
{
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.OnHandQty >= CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_InventoryGenExitsBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.OnHandQty < CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_InventoryGenExitsBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = Lote.OnHandQty });
|
||||
CantidadPendiente -= Lote.OnHandQty;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
});
|
||||
|
||||
var Result = inventoryGenExitsRepository.SetCreateInventoryGenExits(_InventoryGenExits, false).Result;
|
||||
if (Result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(Result.Message.ToString(), entidad);
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado((int)entidad.DocEntry, entidad.NumOrden.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
}
|
||||
|
||||
if (entidad.Proceso.ToUpper() == "PICKING" && entidad.IdEstadoCubico == 13)
|
||||
{
|
||||
CreditNotesRepository _creditNotesRepository = new CreditNotesRepository(_clientFactory, _configuration, context);
|
||||
BE_CreditNotesNotes creditNotesNotes = new BE_CreditNotesNotes
|
||||
{
|
||||
DocObjectCode = "oCreditNotes",
|
||||
DocumentLines = new List<BE_CreditNotesLine>()
|
||||
};
|
||||
|
||||
InvoiceRepository invoicestRepository = new InvoiceRepository(_clientFactory, _configuration, context);
|
||||
BE_ResultadoTransaccion<BE_InvoicesConsulta> response_invoicesRepository = invoicestRepository.GetInvoice(entidad.DocEntry).Result;
|
||||
|
||||
if (response_invoicesRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: La Factura no se encuentra", entidad);
|
||||
}
|
||||
BE_InvoicesConsulta _invoices = response_invoicesRepository.ListData.FirstOrDefault();
|
||||
|
||||
if (_invoices.DocumentStatus != "bost_Open")
|
||||
{
|
||||
var Response_Reopen = invoicestRepository.ReOpen(_invoices.DocEntry).Result;
|
||||
if (Response_Reopen.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: La Factura no se puede reabrir", entidad);
|
||||
}
|
||||
}
|
||||
|
||||
string Serie = _invoices.U_SYP_MDTD == "03" ? "BC10" : "FC10";
|
||||
string Correlativo = HelperCorrelativo.Get(Serie);
|
||||
|
||||
creditNotesNotes.DocCurrency = _invoices.DocCurrency;
|
||||
creditNotesNotes.CardCode = _invoices.CardCode;
|
||||
creditNotesNotes.CardName = _invoices.CardName;
|
||||
creditNotesNotes.PaymentGroupCode = _invoices.PaymentGroupCode;
|
||||
creditNotesNotes.DocDate = DateTime.Now;
|
||||
creditNotesNotes.DocDueDate = DateTime.Now;
|
||||
creditNotesNotes.U_SYP_MDTO = _invoices.U_SYP_MDTD;
|
||||
creditNotesNotes.U_SYP_MDCO = _invoices.U_SYP_MDCD;
|
||||
creditNotesNotes.U_SYP_MDSO = _invoices.U_SYP_MDSD;
|
||||
creditNotesNotes.U_SYP_MOTNC = DescripcionMotivo;
|
||||
creditNotesNotes.U_SYP_TPONC = MotivoNotaCredito;
|
||||
creditNotesNotes.U_SYP_MDCD = Correlativo;
|
||||
creditNotesNotes.U_SYP_MDSD = Serie;
|
||||
creditNotesNotes.U_SYP_MDTD = "07";
|
||||
creditNotesNotes.U_SYP_FECHAREF = _invoices.DocDate.ToString("yyyy-MM-dd");
|
||||
creditNotesNotes.NumAtCard = ("07-" + Serie+ "-" + Correlativo);
|
||||
|
||||
entidad.Detalle.ForEach(x =>
|
||||
{
|
||||
var Item = _invoices.DocumentLines.ToList().FirstOrDefault(t => t.LineNum == x.Item);
|
||||
if (Item != null)
|
||||
{
|
||||
creditNotesNotes.DocumentLines.Add(new BE_CreditNotesLine()
|
||||
{
|
||||
Currency = _invoices.DocCurrency,
|
||||
BatchNumbers = new List<BE_CreditNotesBatchNumbers>(),
|
||||
ItemCode = Item.ItemCode,
|
||||
ItemDescription = Item.ItemDescription,
|
||||
PriceAfterVAT = Item.PriceAfterVAT,
|
||||
Quantity = Item.Quantity - x.CantidadPedida,
|
||||
WarehouseCode = Item.WarehouseCode,
|
||||
BaseEntry = entidad.DocEntry,
|
||||
BaseLine = x.Item,
|
||||
BaseType = 13,
|
||||
LineNum = x.Item,
|
||||
U_ITC_ALMDES = ((Almacen == "ALMPRO") ? Item.WarehouseCode : Almacen),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
var _Invoice = response_invoicesRepository.ListData.FirstOrDefault();
|
||||
creditNotesNotes.DocumentLines.ForEach(line =>
|
||||
{
|
||||
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"), Quantity = (double)line.Quantity });
|
||||
});
|
||||
|
||||
if (creditNotesNotes.DocumentLines.Count > 0)
|
||||
{
|
||||
var result_creditNotesRepository = _creditNotesRepository.SetCreateCreditNotes(creditNotesNotes).Result;
|
||||
if (result_creditNotesRepository.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(result_creditNotesRepository.Message.ToString(), entidad);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
List<string> ListaUnicaAlmacen = creditNotesNotes.DocumentLines.Select(x => x.WarehouseCode).Distinct().ToList();
|
||||
List<int> ListDocEntry = new List<int>();
|
||||
ListaUnicaAlmacen.ForEach(Almacen =>
|
||||
{
|
||||
List<string> ListaUnicaAlmDestino = creditNotesNotes.DocumentLines.Where(Linea => Linea.WarehouseCode == Almacen).ToList().Select(x => x.U_ITC_ALMDES).Distinct().ToList().Where(x => !string.IsNullOrEmpty(x)).ToList();
|
||||
ListaUnicaAlmDestino.ForEach(Destino =>
|
||||
{
|
||||
if (Almacen != Destino)
|
||||
{
|
||||
BE_StockTransferAutomatico Transfer = new BE_StockTransferAutomatico();
|
||||
Transfer.FromWarehouse = Almacen;
|
||||
Transfer.ToWarehouse = Destino;
|
||||
Transfer.DocDate = creditNotesNotes.DocDate;
|
||||
Transfer.TaxDate = creditNotesNotes.DocDate;
|
||||
Transfer.DueDate = creditNotesNotes.DocDueDate;
|
||||
Transfer.U_SYP_MDTS = "TSI";
|
||||
Transfer.U_SYP_MDMT = "24";
|
||||
Transfer.PriceList = -2;
|
||||
var ListaDetalle_PorAlmacen = creditNotesNotes.DocumentLines.Where(x => x.WarehouseCode == Almacen && x.U_ITC_ALMDES == Destino).ToList();
|
||||
var ListaUnicaArticulo = ListaDetalle_PorAlmacen.Select(x => x.ItemCode).Distinct().ToList();
|
||||
|
||||
Transfer.StockTransferLines = new List<BE_StockTransferLine>();
|
||||
Transfer.DocumentReferences = new List<BE_StockTransferDocumentReferences>();
|
||||
|
||||
ListaUnicaArticulo.ForEach(x =>
|
||||
{
|
||||
var ListLote = new List<BE_StockTransferLineBatchNumber>();
|
||||
ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).FirstOrDefault().BatchNumbers.ForEach(r =>
|
||||
{
|
||||
ListLote.Add(new BE_StockTransferLineBatchNumber()
|
||||
{
|
||||
BatchNumber = r.BatchNumber,
|
||||
Quantity = r.Quantity
|
||||
});
|
||||
});
|
||||
Transfer.StockTransferLines.Add(new BE_StockTransferLine()
|
||||
{
|
||||
ItemCode = x,
|
||||
ItemDescription = ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).FirstOrDefault().ItemDescription,
|
||||
Quantity = Convert.ToDouble(ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).Sum(t => t.Quantity)),
|
||||
WarehouseCode = Destino,
|
||||
FromWarehouseCode = Almacen,
|
||||
BatchNumbers = ListLote
|
||||
});
|
||||
|
||||
//=== AGREGA DOCUMENTO REFERENCIA
|
||||
Transfer.DocumentReferences = new List<BE_StockTransferDocumentReferences>
|
||||
{
|
||||
new BE_StockTransferDocumentReferences()
|
||||
{
|
||||
RefDocEntr = (int)result_creditNotesRepository.Data.DocEntry,
|
||||
RefObjType = "rot_SalesCreditNote",
|
||||
IssueDate = DateTime.Now,
|
||||
Remark = "Nota de Credito",
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
//=== CREA INSTANCIA PARA CONECTAR A SAP
|
||||
StockTransferRepository stockTransferRepository = new StockTransferRepository(_clientFactory, _configuration);
|
||||
var ResultTransfer = stockTransferRepository.StockTransfer(Transfer).Result;
|
||||
if (ResultTransfer.StatusCode == -1)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ListDocEntry.Add((int)ResultTransfer.Data.DocEntry);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (ListDocEntry.Count > 0)
|
||||
{
|
||||
var listaReferencia = new List<BE_CreditNotesDocumentReferences>();
|
||||
ListDocEntry.ForEach(x =>
|
||||
{
|
||||
listaReferencia.Add(new BE_CreditNotesDocumentReferences()
|
||||
{
|
||||
RefDocEntr = x,
|
||||
IssueDate = DateTime.Now,
|
||||
RefObjType = "rot_InventoryTransfer",
|
||||
Remark = "Transferencias Automatica"
|
||||
});
|
||||
});
|
||||
|
||||
var credit = new
|
||||
{
|
||||
DocumentReferences = listaReferencia
|
||||
};
|
||||
var Actualizacion = _creditNotesRepository.UpdateCreateCreditNotes(credit, (int)result_creditNotesRepository.Data.DocEntry).Result;
|
||||
}
|
||||
return resultadoTransaccion.GetInformacionResultado((int)result_creditNotesRepository.Data.DocEntry, result_creditNotesRepository.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado((int)entidad.DocEntry, entidad.NumOrden.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
}
|
||||
if(entidad.Proceso.ToUpper() == "ULTIMA MILLA" && entidad.IdEstadoCubico == 23)
|
||||
{
|
||||
if(Proceso.U_ITCA_GNRPROC == "SOLICITUD DEV" && !Reproceso)
|
||||
{
|
||||
|
||||
if (string.IsNullOrEmpty(Almacen))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El motivo no tiene asignado un almacen de destino", entidad);
|
||||
}
|
||||
|
||||
ReturnRequestRepository returnRequestRepository = new ReturnRequestRepository(_clientFactory, _configuration);
|
||||
|
||||
InvoiceRepository invoicestRepository = new InvoiceRepository(_clientFactory, _configuration, context);
|
||||
BE_ResultadoTransaccion<BE_InvoicesConsulta> response_invoicesRepository = invoicestRepository.GetInvoice(entidad.DocEntry).Result;
|
||||
|
||||
if (response_invoicesRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: La Factura no se encuentra", entidad);
|
||||
}
|
||||
BE_InvoicesConsulta invoices = response_invoicesRepository.ListData.FirstOrDefault();
|
||||
BE_ReturnRequest _ReturnRequest = new BE_ReturnRequest()
|
||||
{
|
||||
CardCode = invoices.CardCode,
|
||||
CardName = invoices.CardName,
|
||||
DocCurrency = invoices.DocCurrency,
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
DocObjectCode = "oReturnRequest",
|
||||
PaymentGroupCode = invoices.PaymentGroupCode,
|
||||
U_SYP_MOTNC = DescripcionMotivo,
|
||||
U_SYP_TPONC = MotivoNotaCredito,
|
||||
U_SYP_MDTO = invoices.U_SYP_MDTD,
|
||||
U_SYP_MDCO = invoices.U_SYP_MDCD,
|
||||
U_SYP_MDSO = invoices.U_SYP_MDSD,
|
||||
U_SYP_FECHAREF = invoices.DocDate,
|
||||
DocumentLines = new List<BE_ReturnRequestLine>()
|
||||
};
|
||||
|
||||
var ListaDetalle = entidad.Detalle.Where(x => x.Estado != "C").ToList();
|
||||
ListaDetalle.ForEach(detalle =>
|
||||
{
|
||||
var Detalle_Factura = invoices.DocumentLines.FirstOrDefault(x => x.LineNum == detalle.Item);
|
||||
_ReturnRequest.DocumentLines.Add(new BE_ReturnRequestLine()
|
||||
{
|
||||
LineNum = detalle.Item,
|
||||
BaseEntry = entidad.DocEntry,
|
||||
BaseLine = Detalle_Factura.LineNum,
|
||||
BatchNumbers = new List<BE_ReturnRequestBatchNumbers>(),
|
||||
Currency = invoices.DocCurrency,
|
||||
ItemCode = Detalle_Factura.ItemCode,
|
||||
ItemDescription = Detalle_Factura.ItemDescription,
|
||||
PriceAfterVAT = Detalle_Factura.PriceAfterVAT,
|
||||
Quantity = (Detalle_Factura.Quantity - detalle.CantidadPedida),
|
||||
WarehouseCode = Detalle_Factura.WarehouseCode,
|
||||
BaseType = 13,
|
||||
U_ITC_ALMDES = ((Almacen == "ALMPRO") ? Detalle_Factura.WarehouseCode: Almacen),
|
||||
});
|
||||
});
|
||||
|
||||
var _returnRequestRepository_Result = returnRequestRepository.SetCreateReturnRequest(_ReturnRequest).Result;
|
||||
if (_returnRequestRepository_Result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(_returnRequestRepository_Result.Message.ToString(), entidad);
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado((int)_returnRequestRepository_Result.Data.DocEntry, _returnRequestRepository_Result.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
return resultadoTransaccion.GetInformacionResultado(entidad.DocEntry, entidad.NumOrden, "Registrado Correctamente", entidad);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), entidad);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<BE_ResultadoTransaccion<object>> CreateHojaRuta(BE_HojaRutaEntities entidad)
|
||||
{
|
||||
_metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString();
|
||||
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>(_metodoName, _aplicacionName);
|
||||
|
||||
if (entidad.DocEntry == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El campo docEntry no puede ser cero o vacio", entidad);
|
||||
}
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(entidad.Proceso))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Se necesita definir el proceso para continuar con el proceso.", entidad);
|
||||
}
|
||||
|
||||
if (entidad.IdEstadoCubico == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Se necesita indicar el IdEstado Cubico para continuar con el proceso.", entidad);
|
||||
}
|
||||
|
||||
int Existe = HelperProcesoPermitido.Get(entidad.Proceso, entidad.IdEstadoCubico, "HOJARUTA", out List<BE_ProcesoPicking> ListaProceso);
|
||||
if (Existe == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("El Proceso y estado no se encuentra configurado para este EndPoint", entidad);
|
||||
}
|
||||
|
||||
HelperProcesoPiciking.Set(entidad.Proceso, entidad.IdEstadoCubico, entidad.DocEntry, HelperProcesoPermitido.ConvertTimeToInteger(entidad.HoraInicio), HelperProcesoPermitido.ConvertTimeToInteger(entidad.FinRuta), entidad.CodMotivo, ListaProceso.FirstOrDefault().Code);
|
||||
if (entidad.Proceso == "Picking")
|
||||
{
|
||||
//=== IDENTIFICA DESCRIPCION DE ESTADO
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(entidad.DocEntry, entidad.DocEntry.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), entidad);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<BE_ResultadoTransaccion<object>> CerrarHojaRuta(int docEntry)
|
||||
{
|
||||
_metodoName = "CerrarHojaRuta";
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>(_metodoName, _aplicacionName);
|
||||
|
||||
try
|
||||
{
|
||||
// Hoja de Ruta es un UDO en SAP (ARD_PHR) - mismo patron que
|
||||
// SolicitudTransferencia/{docEntry}/cerrar (POST {recurso}({docEntry})/Close).
|
||||
var connectServiceLayer = new ConnectionServiceLayer(_configuration, _clientFactory);
|
||||
await connectServiceLayer.PostActionAsync($"ARD_PHR({docEntry})/Close");
|
||||
return resultadoTransaccion.GetInformacionResultado(docEntry, docEntry.ToString(), "Cerrado Correctamente", (object)null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), (object)null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.WMS.Recibo;
|
||||
using Net.Connection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Recibo
|
||||
{
|
||||
public interface IReciboRepository : IRepositoryBase<object>
|
||||
{
|
||||
Task<BE_ResultadoTransaccion<object>> Create(BE_ReciboRequest entidad);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,642 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.ServicesLayer;
|
||||
using Net.Business.Entities.ServicesLayer.CreditNotes;
|
||||
using Net.Business.Entities.ServicesLayer.InventoryGenEntries;
|
||||
using Net.Business.Entities.ServicesLayer.Invoices;
|
||||
using Net.Business.Entities.ServicesLayer.PurchaseDeliveryNotes;
|
||||
using Net.Business.Entities.ServicesLayer.PurchaseInvoices;
|
||||
using Net.Business.Entities.ServicesLayer.PurchaseOrders;
|
||||
using Net.Business.Entities.ServicesLayer.ReturnRequest;
|
||||
using Net.Business.Entities.ServicesLayer.StockTransfer;
|
||||
using Net.Business.Entities.WMS.Recibo;
|
||||
using Net.Connection;
|
||||
using Net.CrossCotting;
|
||||
using Net.Data.HelperData.Correlativo;
|
||||
using Net.Data.HelperData.Stock;
|
||||
using Net.Data.ServicesLayer;
|
||||
using Net.Data.ServicesLayer.CreditNotes;
|
||||
using Net.Data.ServicesLayer.GlobalData;
|
||||
using Net.Data.ServicesLayer.InventoryGenEntries;
|
||||
using Net.Data.ServicesLayer.ReturnRequest;
|
||||
using Net.Data.ServicesLayer.StockTransfer;
|
||||
using Net.Data.WMS.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Recibo
|
||||
{
|
||||
public class ReciboRepository : RepositoryBase<object>, IReciboRepository
|
||||
{
|
||||
#region Variables Globales
|
||||
|
||||
private string _aplicacionName;
|
||||
private string _metodoName;
|
||||
private readonly Regex regex = new Regex(@"<(\w+)>.*");
|
||||
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
#endregion
|
||||
|
||||
public ReciboRepository(IConnectionSQL context, IHttpClientFactory clientFactory, IConfiguration configuration) : base(context)
|
||||
{
|
||||
_clientFactory = clientFactory;
|
||||
_configuration = configuration;
|
||||
_aplicacionName = GetType().Name;
|
||||
}
|
||||
|
||||
public async Task<BE_ResultadoTransaccion<object>> Create(BE_ReciboRequest entidad)
|
||||
{
|
||||
_metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString();
|
||||
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>(_metodoName, _aplicacionName);
|
||||
|
||||
return await Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
//=== INICIALIZA OBJETO DE SAP
|
||||
PurchaseDeliveryNotesRepository _purchaseDeliveryNotes = new PurchaseDeliveryNotesRepository(_clientFactory, _configuration);
|
||||
CreditNotesRepository _creditNotesRepository = new CreditNotesRepository(_clientFactory, _configuration, context);
|
||||
|
||||
BusinessPartnersRepository businessPartnersRepository = new BusinessPartnersRepository(_clientFactory, _configuration, context);
|
||||
var response_businessPartnersRepository = await businessPartnersRepository.GetBusinessPartnersByFilter(new FE_BusinessPartners()
|
||||
{
|
||||
CardCode = entidad.CodigoERPProveedor
|
||||
});
|
||||
|
||||
if (response_businessPartnersRepository.ListData.Count() == 0)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: El Proveedor no se encuentra registrado en SAP", entidad);
|
||||
}
|
||||
|
||||
#region PROCESO POR COMPRA DE IMPORTACION
|
||||
|
||||
if (entidad.IdTipo == EnumRecibo.IMPORTACION)
|
||||
{
|
||||
//=== INICIALIZA OBJETO DE SAP
|
||||
BE_PurchaseDeliveryNotes purchaseDeliveryNotes = new BE_PurchaseDeliveryNotes
|
||||
{
|
||||
DocObjectCode = "oPurchaseDeliveryNotes",
|
||||
DocumentLines = new List<BE_PurchaseDeliveryNotesLine>()
|
||||
};
|
||||
|
||||
if (entidad.IdTipoDoc == EnumRecibo.IMPORTACION_ORDEN)
|
||||
{
|
||||
PurchaseInvoicesRepository purchaseInvoicesRepository = new PurchaseInvoicesRepository(_clientFactory, _configuration);
|
||||
BE_ResultadoTransaccion<BE_PurchaseInvoices> response_purchaseInvoicesRepository = purchaseInvoicesRepository.GetPurchaseInvoices(entidad.DocEntry).Result;
|
||||
if (response_purchaseInvoicesRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Factura de reserva por importaciones no se encuentra", entidad);
|
||||
}
|
||||
|
||||
BE_PurchaseInvoices purchaseOrders = response_purchaseInvoicesRepository.ListData.FirstOrDefault();
|
||||
purchaseDeliveryNotes.DocCurrency = purchaseOrders.DocCurrency;
|
||||
purchaseDeliveryNotes.CardCode = purchaseOrders.CardCode;
|
||||
purchaseDeliveryNotes.CardName = purchaseOrders.CardName;
|
||||
purchaseDeliveryNotes.PaymentGroupCode = purchaseOrders.PaymentGroupCode;
|
||||
purchaseDeliveryNotes.DocDate = DateTime.Now;
|
||||
purchaseDeliveryNotes.DocDueDate = DateTime.Now;
|
||||
purchaseDeliveryNotes.NumAtCard = entidad.DocImportacion;
|
||||
purchaseDeliveryNotes.U_SYP_MDCD = entidad.U_SYP_MDCD;
|
||||
purchaseDeliveryNotes.U_SYP_MDSD = entidad.U_SYP_MDSD;
|
||||
purchaseDeliveryNotes.U_SYP_MDTD = entidad.U_SYP_MDTD;
|
||||
entidad.Detalle.ForEach(x =>
|
||||
{
|
||||
if (x.CantidadPedida > 0)
|
||||
{
|
||||
var Item = purchaseOrders.DocumentLines.ToList().FirstOrDefault(t => t.LineNum == x.Item);
|
||||
if (Item != null)
|
||||
{
|
||||
purchaseDeliveryNotes.DocumentLines.Add(new BE_PurchaseDeliveryNotesLine()
|
||||
{
|
||||
Currency = purchaseOrders.DocCurrency,
|
||||
BatchNumbers = new List<BE_PurchaseDeliveryNotesBatchNumbers>
|
||||
{
|
||||
new BE_PurchaseDeliveryNotesBatchNumbers()
|
||||
{
|
||||
BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
|
||||
Quantity = (int)x.CantidadPedida
|
||||
},
|
||||
},
|
||||
ItemCode = Item.ItemCode,
|
||||
ItemDescription = Item.ItemDescription,
|
||||
PriceAfterVAT = Item.PriceAfterVAT,
|
||||
Quantity = (double)x.CantidadPedida,
|
||||
WarehouseCode = Item.WarehouseCode,
|
||||
BaseEntry = entidad.DocEntry,
|
||||
BaseLine = x.Item,
|
||||
BaseType = 18,
|
||||
LineNum = x.Item,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var result_purchaseDeliveryNotes = _purchaseDeliveryNotes.SetCreatePurchaseDeliveryNotes(purchaseDeliveryNotes).Result;
|
||||
if (result_purchaseDeliveryNotes.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(result_purchaseDeliveryNotes.Message, entidad);
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado((int)result_purchaseDeliveryNotes.Data.DocEntry, result_purchaseDeliveryNotes.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
|
||||
if (entidad.IdTipoDoc == EnumRecibo.IMPORTACION_ORDEN_COMPRA)
|
||||
{
|
||||
PurchaseOrdersRepository purchaseOrdersRepository = new PurchaseOrdersRepository(_clientFactory, _configuration);
|
||||
BE_ResultadoTransaccion<BE_PurchaseOrders> response_purchaseOrdersRepository = purchaseOrdersRepository.GetPurchaseInvoices(entidad.DocEntry).Result;
|
||||
|
||||
if (response_purchaseOrdersRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Orden de Compra no se encuentra", entidad);
|
||||
}
|
||||
|
||||
BE_PurchaseOrders purchaseOrders = response_purchaseOrdersRepository.ListData.FirstOrDefault();
|
||||
purchaseDeliveryNotes.DocCurrency = purchaseOrders.DocCurrency;
|
||||
purchaseDeliveryNotes.CardCode = purchaseOrders.CardCode;
|
||||
purchaseDeliveryNotes.CardName = purchaseOrders.CardName;
|
||||
purchaseDeliveryNotes.PaymentGroupCode = purchaseOrders.PaymentGroupCode;
|
||||
purchaseDeliveryNotes.DocDate = DateTime.Now;
|
||||
purchaseDeliveryNotes.DocDueDate = DateTime.Now;
|
||||
purchaseDeliveryNotes.NumAtCard = entidad.DocImportacion;
|
||||
purchaseDeliveryNotes.U_SYP_MDCD = entidad.U_SYP_MDCD;
|
||||
purchaseDeliveryNotes.U_SYP_MDSD = entidad.U_SYP_MDSD;
|
||||
purchaseDeliveryNotes.U_SYP_MDTD = entidad.U_SYP_MDTD;
|
||||
|
||||
entidad.Detalle.ForEach(x =>
|
||||
{
|
||||
var Item = purchaseOrders.DocumentLines.ToList().FirstOrDefault(t => t.LineNum == x.Item);
|
||||
if (Item != null)
|
||||
{
|
||||
purchaseDeliveryNotes.DocumentLines.Add(new BE_PurchaseDeliveryNotesLine()
|
||||
{
|
||||
Currency = purchaseOrders.DocCurrency,
|
||||
BatchNumbers = new List<BE_PurchaseDeliveryNotesBatchNumbers>
|
||||
{
|
||||
new BE_PurchaseDeliveryNotesBatchNumbers()
|
||||
{
|
||||
BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
|
||||
Quantity = (int)x.CantidadPedida
|
||||
},
|
||||
},
|
||||
ItemCode = Item.ItemCode,
|
||||
ItemDescription = Item.ItemDescription,
|
||||
PriceAfterVAT = Item.PriceAfterVAT,
|
||||
Quantity = (double)x.CantidadPedida,
|
||||
WarehouseCode = Item.WarehouseCode,
|
||||
BaseEntry = entidad.DocEntry,
|
||||
BaseLine = x.Item,
|
||||
BaseType = 22,
|
||||
LineNum = x.Item
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
var result_purchaseDeliveryNotes = _purchaseDeliveryNotes.SetCreatePurchaseDeliveryNotes(purchaseDeliveryNotes).Result;
|
||||
if (result_purchaseDeliveryNotes.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(result_purchaseDeliveryNotes.Message, entidad);
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado((int)result_purchaseDeliveryNotes.Data.DocEntry, result_purchaseDeliveryNotes.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
|
||||
if (entidad.IdTipoDoc == EnumRecibo.IMPORTACION_SOL_ENTRADA)
|
||||
{
|
||||
GlobalDataRepository globalDataRepository = new GlobalDataRepository(_clientFactory, _configuration, context);
|
||||
var Result_globalDataRepository = globalDataRepository.GetSolicitudIngreso(entidad.DocEntry).Result.ListData.FirstOrDefault();
|
||||
|
||||
BE_InventoryGenEntries _InventoryGenEntries = new BE_InventoryGenEntries()
|
||||
{
|
||||
DocObjectCode = "oInventoryGenEntry",
|
||||
DocDate = DateTime.Now,
|
||||
DocDueDate = DateTime.Now,
|
||||
PaymentGroupCode = "-2",
|
||||
DocCurrency = "S/",
|
||||
DocRate = 1,
|
||||
DocumentLines = new List<BE_InventoryGenEntriesLine>(),
|
||||
U_SYP_TPOENTME = Result_globalDataRepository.U_ITC_MDTI,
|
||||
U_ITC_WMS_NUMINV = "",
|
||||
U_ARD_PERSOLIC = Result_globalDataRepository.U_ITC_USRSOL,
|
||||
Comments = Result_globalDataRepository.U_ITC_MDCM
|
||||
};
|
||||
|
||||
int LineNum = 0;
|
||||
entidad.Detalle.ForEach(detalle =>
|
||||
{
|
||||
var LineaDetalle = Result_globalDataRepository.ITC_SOLINGMERDETCollection.FirstOrDefault(x => x.LineId == detalle.Item);
|
||||
|
||||
var Precio = HelperStock.GetConsolidado(detalle.CodigoArticulo, Result_globalDataRepository.U_ITC_MDWC).FirstOrDefault().AvgPrice;
|
||||
|
||||
_InventoryGenEntries.DocumentLines.Add(new BE_InventoryGenEntriesLine()
|
||||
{
|
||||
Currency = "S/",
|
||||
ItemCode = detalle.CodigoArticulo,
|
||||
ItemDescription = LineaDetalle.U_ITC_ITEMNAME,
|
||||
Quantity = (double)detalle.CantidadPedida,
|
||||
LineNum = LineNum,
|
||||
PriceAfterVAT = Precio,
|
||||
LineTotal = Precio * (double)detalle.CantidadPedida,
|
||||
WarehouseCode = Result_globalDataRepository.U_ITC_MDWC,
|
||||
AccountCode = LineaDetalle.U_ITC_ACCCODE,
|
||||
CostingCode = LineaDetalle.U_ITC_CCOSTO ?? "",
|
||||
BatchNumbers = new List<BE_InventoryGenEntriesBatchNumbers>()
|
||||
{
|
||||
new BE_InventoryGenEntriesBatchNumbers()
|
||||
{
|
||||
BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
|
||||
Quantity = (double)detalle.CantidadPedida
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
LineNum++;
|
||||
});
|
||||
|
||||
InventoryGenEntriesRepository inventoryGenEntries = new InventoryGenEntriesRepository(_clientFactory, _configuration, context);
|
||||
var inventoryGenEntries_result = inventoryGenEntries.SetCreateInventoryGenEntries(_InventoryGenEntries, false).Result;
|
||||
if (inventoryGenEntries_result.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(inventoryGenEntries_result.Message.ToString(), entidad);
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado((int)inventoryGenEntries_result.Data.DocEntry, inventoryGenEntries_result.Data.DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PROCESO POR COMPRA NACIONAL
|
||||
|
||||
if (entidad.IdTipo == EnumRecibo.NACIONAL)
|
||||
{
|
||||
//=== INICIALIZA OBJETO DE SAP
|
||||
BE_PurchaseDeliveryNotes purchaseDeliveryNotes = new BE_PurchaseDeliveryNotes
|
||||
{
|
||||
DocObjectCode = "oPurchaseDeliveryNotes",
|
||||
DocumentLines = new List<BE_PurchaseDeliveryNotesLine>()
|
||||
};
|
||||
|
||||
PurchaseOrdersRepository purchaseOrdersRepository = new PurchaseOrdersRepository(_clientFactory, _configuration);
|
||||
BE_ResultadoTransaccion<BE_PurchaseOrders> response_purchaseOrdersRepository = purchaseOrdersRepository.GetPurchaseInvoices(entidad.DocEntry).Result;
|
||||
|
||||
if (response_purchaseOrdersRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Orden de Compra no se encuentra", entidad);
|
||||
}
|
||||
|
||||
BE_PurchaseOrders purchaseOrders = response_purchaseOrdersRepository.ListData.FirstOrDefault();
|
||||
purchaseDeliveryNotes.DocCurrency = purchaseOrders.DocCurrency;
|
||||
purchaseDeliveryNotes.CardCode = purchaseOrders.CardCode;
|
||||
purchaseDeliveryNotes.CardName = purchaseOrders.CardName;
|
||||
purchaseDeliveryNotes.PaymentGroupCode = purchaseOrders.PaymentGroupCode;
|
||||
purchaseDeliveryNotes.DocDate = DateTime.Now;
|
||||
purchaseDeliveryNotes.DocDueDate = DateTime.Now;
|
||||
purchaseDeliveryNotes.NumAtCard = entidad.DocImportacion;
|
||||
purchaseDeliveryNotes.U_SYP_MDCD = entidad.U_SYP_MDCD;
|
||||
purchaseDeliveryNotes.U_SYP_MDSD = entidad.U_SYP_MDSD;
|
||||
purchaseDeliveryNotes.U_SYP_MDTD = entidad.U_SYP_MDTD;
|
||||
|
||||
entidad.Detalle.ForEach(x =>
|
||||
{
|
||||
var Item = purchaseOrders.DocumentLines.ToList().FirstOrDefault(t => t.LineNum == x.Item);
|
||||
if (Item != null)
|
||||
{
|
||||
purchaseDeliveryNotes.DocumentLines.Add(new BE_PurchaseDeliveryNotesLine()
|
||||
{
|
||||
Currency = purchaseOrders.DocCurrency,
|
||||
BatchNumbers = new List<BE_PurchaseDeliveryNotesBatchNumbers>
|
||||
{
|
||||
new BE_PurchaseDeliveryNotesBatchNumbers()
|
||||
{
|
||||
BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"),
|
||||
Quantity = (int)x.CantidadPedida
|
||||
},
|
||||
},
|
||||
ItemCode = Item.ItemCode,
|
||||
ItemDescription = Item.ItemDescription,
|
||||
PriceAfterVAT = Item.PriceAfterVAT,
|
||||
Quantity = (double)x.CantidadPedida,
|
||||
WarehouseCode = Item.WarehouseCode,
|
||||
BaseEntry = entidad.DocEntry,
|
||||
BaseLine = x.Item,
|
||||
BaseType = 22,
|
||||
LineNum = x.Item
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
var result_purchaseDeliveryNotes = _purchaseDeliveryNotes.SetCreatePurchaseDeliveryNotes(purchaseDeliveryNotes).Result;
|
||||
if (result_purchaseDeliveryNotes.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(result_purchaseDeliveryNotes.Message, entidad);
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado((int)result_purchaseDeliveryNotes.Data.DocEntry, result_purchaseDeliveryNotes.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PROCESO DE NOTA DE CREDITO
|
||||
|
||||
if (entidad.IdTipo == EnumRecibo.DEVOLUCION)
|
||||
{
|
||||
BE_CreditNotesNotes creditNotesNotes = new BE_CreditNotesNotes
|
||||
{
|
||||
DocObjectCode = "oCreditNotes",
|
||||
DocumentLines = new List<BE_CreditNotesLine>()
|
||||
};
|
||||
|
||||
ReturnRequestRepository returnRequestRepository = new ReturnRequestRepository(_clientFactory, _configuration);
|
||||
BE_ResultadoTransaccion<BE_ReturnRequest> response_purchaseOrdersRepository = returnRequestRepository.GetRequestReturn(entidad.DocEntry).Result;
|
||||
|
||||
if (response_purchaseOrdersRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: Solicitud de Devolucion no se encuentra", entidad);
|
||||
}
|
||||
|
||||
BE_ReturnRequest returnRequest = response_purchaseOrdersRepository.ListData.FirstOrDefault();
|
||||
|
||||
#region OBTIENE FACTURAS RELACIONADA A LA SOLICITUD DE DEVOLUCION
|
||||
|
||||
List<int> ListaDocEntryFactura = returnRequest.DocumentLines.Select(x => !x.BaseEntry.HasValue? 0: x.BaseEntry.Value).Distinct().Where(x => x > 0).ToList();
|
||||
BE_InvoicesConsulta _Invoice = null;
|
||||
ListaDocEntryFactura.ForEach(DocEntry =>
|
||||
{
|
||||
var _InvoiceRepository = new InvoiceRepository(_clientFactory, _configuration, context);
|
||||
|
||||
var _Invoice_Result = _InvoiceRepository.GetInvoice(DocEntry).Result;
|
||||
if (_Invoice_Result.ListData.Count() > 0)
|
||||
{
|
||||
_Invoice = _Invoice_Result.ListData.FirstOrDefault();
|
||||
}
|
||||
});
|
||||
string SerieOrigen = "";
|
||||
string U_SYP_MDTO = "";
|
||||
string U_SYP_MDCO = "";
|
||||
string U_SYP_MDSO = "";
|
||||
DateTime DocDate;
|
||||
|
||||
if (_Invoice == null)
|
||||
{
|
||||
SerieOrigen = returnRequest.U_SYP_MDSO;
|
||||
U_SYP_MDTO = returnRequest.U_SYP_MDTO;
|
||||
U_SYP_MDCO = returnRequest.U_SYP_MDCO;
|
||||
U_SYP_MDSO = returnRequest.U_SYP_MDSO;
|
||||
DocDate = returnRequest.U_SYP_FECHAREF;
|
||||
}
|
||||
else
|
||||
{
|
||||
SerieOrigen = _Invoice.U_SYP_MDSD;
|
||||
U_SYP_MDTO = _Invoice.U_SYP_MDTD;
|
||||
U_SYP_MDCO = _Invoice.U_SYP_MDCD;
|
||||
U_SYP_MDSO = _Invoice.U_SYP_MDSD;
|
||||
DocDate = _Invoice.DocDate;
|
||||
}
|
||||
|
||||
#endregion
|
||||
string Serie = SerieOrigen == "03" ? "BC10" : "FC10";
|
||||
string Correlativo = HelperCorrelativo.Get(Serie);
|
||||
creditNotesNotes.DocCurrency = returnRequest.DocCurrency;
|
||||
creditNotesNotes.CardCode = returnRequest.CardCode;
|
||||
creditNotesNotes.CardName = returnRequest.CardName;
|
||||
creditNotesNotes.PaymentGroupCode = returnRequest.PaymentGroupCode;
|
||||
creditNotesNotes.DocDate = DateTime.Now;
|
||||
creditNotesNotes.DocDueDate = DateTime.Now;
|
||||
//creditNotesNotes.NumAtCard = entidad.DocImportacion;
|
||||
creditNotesNotes.NumAtCard = ("07-" + Serie +"-" + Correlativo);
|
||||
creditNotesNotes.U_SYP_MDTO = U_SYP_MDTO;
|
||||
creditNotesNotes.U_SYP_MDCO = U_SYP_MDCO;
|
||||
creditNotesNotes.U_SYP_MDSO = U_SYP_MDSO;
|
||||
creditNotesNotes.U_SYP_MOTNC = returnRequest.U_SYP_MOTNC;
|
||||
creditNotesNotes.U_SYP_TPONC = returnRequest.U_SYP_TPONC;
|
||||
creditNotesNotes.U_SYP_MDCD = Correlativo;
|
||||
creditNotesNotes.U_SYP_MDSD = Serie;
|
||||
creditNotesNotes.U_SYP_MDTD = "07";
|
||||
creditNotesNotes.U_SYP_FECHAREF = DocDate.ToString("yyyy-MM-dd");
|
||||
entidad.Detalle.ForEach(x =>
|
||||
{
|
||||
var Item = returnRequest.DocumentLines.ToList().FirstOrDefault(t => t.LineNum == x.Item);
|
||||
if (Item != null)
|
||||
{
|
||||
creditNotesNotes.DocumentLines.Add(new BE_CreditNotesLine()
|
||||
{
|
||||
Currency = returnRequest.DocCurrency,
|
||||
BatchNumbers = new List<BE_CreditNotesBatchNumbers>
|
||||
{
|
||||
new BE_CreditNotesBatchNumbers()
|
||||
{
|
||||
BatchNumber = x.LoteProducto,
|
||||
Quantity = (int)x.CantidadPedida
|
||||
},
|
||||
},
|
||||
ItemCode = Item.ItemCode,
|
||||
ItemDescription = Item.ItemDescription,
|
||||
PriceAfterVAT = Item.PriceAfterVAT,
|
||||
Quantity = x.CantidadPedida,
|
||||
WarehouseCode = Item.WarehouseCode,
|
||||
BaseEntry = entidad.DocEntry,
|
||||
BaseLine = x.Item,
|
||||
BaseType = 234000031,
|
||||
LineNum = x.Item
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (_Invoice == null)
|
||||
{
|
||||
//=== REALIZA DISTRIBUCION DE STOCK POR LOTE
|
||||
creditNotesNotes.DocumentLines.ForEach(line =>
|
||||
{
|
||||
#region CONSULTA A HANA
|
||||
|
||||
var ListaStock = HelperStock.Get(line.ItemCode, line.WarehouseCode);
|
||||
if (ListaStock.Count > 0)
|
||||
{
|
||||
var CantidadPendiente = line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock.OrderBy(x => x.ExpDate).ToList();
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.OnHandQty >= (double)CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = (double)CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.OnHandQty < (double)CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = Lote.DistNumber, Quantity = (double)Lote.OnHandQty });
|
||||
CantidadPendiente -= (decimal)Lote.OnHandQty;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"), Quantity = (double)line.Quantity });
|
||||
}
|
||||
|
||||
#endregion
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
creditNotesNotes.DocumentLines.ForEach(line =>
|
||||
{
|
||||
var ReferenciaFactura = returnRequest.DocumentLines.FirstOrDefault(t => t.LineNum == line.LineNum);
|
||||
var ListaStock = _Invoice.DocumentLines.FirstOrDefault(InvoiceLine => InvoiceLine.LineNum == ReferenciaFactura.BaseLine).BatchNumbers;
|
||||
if (ListaStock.Count > 0)
|
||||
{
|
||||
var CantidadPendiente = line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock;
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.Quantity >= (double)CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = Lote.BatchNumber, Quantity = (double)CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.Quantity < (double)CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = Lote.BatchNumber, Quantity = (double)Lote.Quantity });
|
||||
CantidadPendiente -= (decimal)Lote.Quantity;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_CreditNotesBatchNumbers() { BatchNumber = DateTime.Now.ToString("yyyyMMddhhmmss"), Quantity = (double)line.Quantity });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var result_creditNotesRepository = _creditNotesRepository.SetCreateCreditNotes(creditNotesNotes).Result;
|
||||
if (result_creditNotesRepository.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(result_creditNotesRepository.Message, entidad);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<string> ListaUnicaAlmacen = creditNotesNotes.DocumentLines.Select(x => x.WarehouseCode).Distinct().ToList();
|
||||
List<int> ListDocEntry = new List<int>();
|
||||
ListaUnicaAlmacen.ForEach(Almacen =>
|
||||
{
|
||||
List<string> ListaUnicaAlmDestino = creditNotesNotes.DocumentLines.Where(Linea => Linea.WarehouseCode == Almacen).ToList().Select(x => x.U_ITC_ALMDES).Distinct().ToList().Where(x => !string.IsNullOrEmpty(x)).ToList();
|
||||
ListaUnicaAlmDestino.ForEach(Destino =>
|
||||
{
|
||||
if (Almacen != Destino)
|
||||
{
|
||||
BE_StockTransferAutomatico Transfer = new BE_StockTransferAutomatico();
|
||||
Transfer.FromWarehouse = Almacen;
|
||||
Transfer.ToWarehouse = Destino;
|
||||
Transfer.DocDate = creditNotesNotes.DocDate;
|
||||
Transfer.TaxDate = creditNotesNotes.DocDate;
|
||||
Transfer.DueDate = creditNotesNotes.DocDueDate;
|
||||
Transfer.U_SYP_MDTS = "TSI";
|
||||
Transfer.U_SYP_MDMT = "24";
|
||||
Transfer.PriceList = -2;
|
||||
var ListaDetalle_PorAlmacen = creditNotesNotes.DocumentLines.Where(x => x.WarehouseCode == Almacen && x.U_ITC_ALMDES == Destino).ToList();
|
||||
var ListaUnicaArticulo = ListaDetalle_PorAlmacen.Select(x => x.ItemCode).Distinct().ToList();
|
||||
|
||||
Transfer.StockTransferLines = new List<BE_StockTransferLine>();
|
||||
Transfer.DocumentReferences = new List<BE_StockTransferDocumentReferences>();
|
||||
|
||||
ListaUnicaArticulo.ForEach(x =>
|
||||
{
|
||||
var ListLote = new List<BE_StockTransferLineBatchNumber>();
|
||||
ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).FirstOrDefault().BatchNumbers.ForEach(r =>
|
||||
{
|
||||
ListLote.Add(new BE_StockTransferLineBatchNumber()
|
||||
{
|
||||
BatchNumber = r.BatchNumber,
|
||||
Quantity = r.Quantity
|
||||
});
|
||||
});
|
||||
Transfer.StockTransferLines.Add(new BE_StockTransferLine()
|
||||
{
|
||||
ItemCode = x,
|
||||
ItemDescription = ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).FirstOrDefault().ItemDescription,
|
||||
Quantity = Convert.ToDouble(ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).Sum(t => t.Quantity)),
|
||||
WarehouseCode = Destino,
|
||||
FromWarehouseCode = Almacen,
|
||||
BatchNumbers = ListLote
|
||||
});
|
||||
|
||||
//=== AGREGA DOCUMENTO REFERENCIA
|
||||
Transfer.DocumentReferences = new List<BE_StockTransferDocumentReferences>
|
||||
{
|
||||
new BE_StockTransferDocumentReferences()
|
||||
{
|
||||
RefDocEntr = (int)result_creditNotesRepository.Data.DocEntry,
|
||||
RefObjType = "rot_SalesCreditNote",
|
||||
IssueDate = DateTime.Now,
|
||||
Remark = "Nota de Credito",
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
//=== CREA INSTANCIA PARA CONECTAR A SAP
|
||||
StockTransferRepository stockTransferRepository = new StockTransferRepository(_clientFactory, _configuration);
|
||||
var ResultTransfer = stockTransferRepository.StockTransfer(Transfer).Result;
|
||||
if (ResultTransfer.StatusCode == -1)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ListDocEntry.Add((int)ResultTransfer.Data.DocEntry);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (ListDocEntry.Count > 0)
|
||||
{
|
||||
var listaReferencia = new List<BE_CreditNotesDocumentReferences>();
|
||||
ListDocEntry.ForEach(x =>
|
||||
{
|
||||
listaReferencia.Add(new BE_CreditNotesDocumentReferences()
|
||||
{
|
||||
RefDocEntr = x,
|
||||
IssueDate = DateTime.Now,
|
||||
RefObjType = "rot_InventoryTransfer",
|
||||
Remark = "Transferencias Automatica"
|
||||
});
|
||||
});
|
||||
|
||||
var credit = new
|
||||
{
|
||||
DocumentReferences = listaReferencia
|
||||
};
|
||||
var Actualizacion = _creditNotesRepository.UpdateCreateCreditNotes(credit, (int)result_creditNotesRepository.Data.DocEntry).Result;
|
||||
}
|
||||
return resultadoTransaccion.GetInformacionResultado((int)result_creditNotesRepository.Data.DocEntry, result_creditNotesRepository.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
|
||||
//return resultadoTransaccion.GetInformacionResultado((int)result_creditNotesRepository.Data.DocEntry, result_creditNotesRepository.Data.DocNum.ToString(), "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado(0, "", "Se registro correctamente...!!!", entidad);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), entidad);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.WMS.SolicitudTransferencia;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.SolicitudTransferencia
|
||||
{
|
||||
public interface ISolicitudTransferenciaRepository
|
||||
{
|
||||
Task<BE_ResultadoTransaccion<object>> Update(int docEntry, BE_SolicitudTransferenciaPatchRequest entidad);
|
||||
Task<BE_ResultadoTransaccion<object>> Cerrar(int docEntry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.ServicesLayer.InventoryTransferRequest;
|
||||
using Net.Business.Entities.WMS.SolicitudTransferencia;
|
||||
using Net.Connection.ServiceLayer;
|
||||
using Net.CrossCotting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.SolicitudTransferencia
|
||||
{
|
||||
// Modulo 100% nuevo (sin equivalente en el .NET original) - porte 1:1 de
|
||||
// itca_api_sap_wms_python/app/services/solicitud_transferencia_service.py.
|
||||
// Actualiza y cierra documentos InventoryTransferRequests (Solicitud de
|
||||
// Traslado) en SAP Business One via Service Layer. Ver
|
||||
// docs/SOLICITUD_TRANSFERENCIA.md para el detalle completo del contrato.
|
||||
public class SolicitudTransferenciaRepository : ISolicitudTransferenciaRepository
|
||||
{
|
||||
private readonly string _aplicacionName;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
|
||||
public SolicitudTransferenciaRepository(IHttpClientFactory clientFactory, IConfiguration configuration)
|
||||
{
|
||||
_aplicacionName = GetType().Name;
|
||||
_configuration = configuration;
|
||||
_clientFactory = clientFactory;
|
||||
}
|
||||
|
||||
// Devuelve null en exito (el caller arma un BE_ResultadoTransaccion 200 con
|
||||
// body - ver el controller; no responde 204 porque muchos clientes HTTP,
|
||||
// incluido HttpClient de .NET, no leen el body de un 204 - ver
|
||||
// docs/SOLICITUD_TRANSFERENCIA.md).
|
||||
//
|
||||
// El PATCH SOLO actualiza cantidades de linea existentes en SAP - no toca
|
||||
// fechas, almacenes ni ningun otro campo del documento - y ELIMINA una
|
||||
// linea cuando Cantidad = 0. Confirmado contra el Service Layer real de QA:
|
||||
// alcanza con mandar LineNum + Quantity por linea junto con el header
|
||||
// B1S-ReplaceCollectionsOnPatch: true, que le indica a Service Layer que
|
||||
// trate StockTransferLines como reemplazo COMPLETO del array, no un merge
|
||||
// parcial. SAP ademas RECHAZA una linea mandada explicita con Quantity: 0,
|
||||
// por eso se omite del array en vez de mandarla en cero.
|
||||
//
|
||||
// Al ser un reemplazo completo, SAP TAMBIEN exige el Rate real de cada
|
||||
// linea (hallazgo de QA, no cubierto en la primera tanda de pruebas) - por
|
||||
// eso se hace un GET previo para leerlo y reenviarlo, en vez de mandar
|
||||
// Rate: 0 fijo (pisaria la valorizacion real de la linea).
|
||||
public async Task<BE_ResultadoTransaccion<object>> Update(int docEntry, BE_SolicitudTransferenciaPatchRequest entidad)
|
||||
{
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>("Update", _aplicacionName);
|
||||
|
||||
try
|
||||
{
|
||||
var connectServiceLayer = new ConnectionServiceLayer(_configuration, _clientFactory);
|
||||
|
||||
var doc = (await connectServiceLayer.GetAsync<BE_InventoryTransferRequest>($"InventoryTransferRequests({docEntry})", 0, true)).FirstOrDefault();
|
||||
var ratesByLine = (doc?.StockTransferLines ?? new List<BE_InventoryTransferRequestLine>())
|
||||
.ToDictionary(l => l.LineNum, l => l.Rate);
|
||||
|
||||
var lines = new List<object>();
|
||||
foreach (var detalle in entidad.Detalle ?? new List<BE_SolicitudTransferenciaPatchDetalle>())
|
||||
{
|
||||
if (detalle.Cantidad == 0) continue;
|
||||
|
||||
if (!ratesByLine.TryGetValue(detalle.Linea, out var rate))
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado($"La linea {detalle.Linea} no existe en la Solicitud de Traslado {docEntry}", (object)entidad);
|
||||
}
|
||||
|
||||
lines.Add(new { LineNum = detalle.Linea, Quantity = detalle.Cantidad, Rate = rate });
|
||||
}
|
||||
|
||||
await connectServiceLayer.PatchNoContentAsync(
|
||||
$"InventoryTransferRequests({docEntry})",
|
||||
new { StockTransferLines = lines },
|
||||
new Dictionary<string, string> { ["B1S-ReplaceCollectionsOnPatch"] = "true" });
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), (object)entidad);
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el documento completo. Funcionalidad nueva (no existe en el .NET
|
||||
// original) - SAP Service Layer cierra documentos via POST a la sub-ruta
|
||||
// /Close, mismo patron que Picking/HojaRuta/Cerrar. Devuelve null en exito
|
||||
// (el caller arma el BE_ResultadoTransaccion 200 con body).
|
||||
public async Task<BE_ResultadoTransaccion<object>> Cerrar(int docEntry)
|
||||
{
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>("Cerrar", _aplicacionName);
|
||||
|
||||
try
|
||||
{
|
||||
var connectServiceLayer = new ConnectionServiceLayer(_configuration, _clientFactory);
|
||||
await connectServiceLayer.PostActionAsync($"InventoryTransferRequests({docEntry})/Close");
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), (object)null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.WMS.Transferencia;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Transferencia
|
||||
{
|
||||
public interface ITransferenciaRepository
|
||||
{
|
||||
Task<BE_ResultadoTransaccion<object>> Create(BE_TransferenciaRequest entidad);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
using iTextSharp.text;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Net.Business.Entities;
|
||||
using Net.Business.Entities.ServicesLayer;
|
||||
using Net.Business.Entities.ServicesLayer.Invoices;
|
||||
using Net.Business.Entities.ServicesLayer.Items;
|
||||
using Net.Business.Entities.ServicesLayer.StockTransfer;
|
||||
using Net.Business.Entities.WMS.Transferencia;
|
||||
using Net.Connection;
|
||||
using Net.CrossCotting;
|
||||
using Net.Data.HelperData.Invoices;
|
||||
using Net.Data.HelperData.Stock;
|
||||
using Net.Data.ServicesLayer;
|
||||
using Net.Data.ServicesLayer.StockTransfer;
|
||||
using Sap.Data.Hana;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Net.Data.WMS.Transferencia
|
||||
{
|
||||
public class TransferenciaRepository : RepositoryBase<object>, ITransferenciaRepository
|
||||
{
|
||||
#region Variables Globales
|
||||
private string _aplicacionName;
|
||||
private string _metodoName;
|
||||
private readonly Regex regex = new Regex(@"<(\w+)>.*");
|
||||
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
#endregion
|
||||
|
||||
public TransferenciaRepository(IConnectionSQL context, IHttpClientFactory clientFactory, IConfiguration configuration) : base(context)
|
||||
{
|
||||
_clientFactory = clientFactory;
|
||||
_configuration = configuration;
|
||||
_aplicacionName = GetType().Name;
|
||||
}
|
||||
|
||||
public async Task<BE_ResultadoTransaccion<object>> Create(BE_TransferenciaRequest entidad)
|
||||
{
|
||||
_metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString();
|
||||
|
||||
ResultadoTransaccion<object> resultadoTransaccion = new ResultadoTransaccion<object>(_metodoName, _aplicacionName);
|
||||
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
//=== CREA INSTANCIA PARA CONECTAR A SAP
|
||||
StockTransferRepository stockTransferRepository = new StockTransferRepository(_clientFactory, _configuration);
|
||||
|
||||
BE_StockTransfer StockTransfer = new BE_StockTransfer
|
||||
{
|
||||
DocDate = entidad.FechaOrden,
|
||||
DueDate = entidad.FechaOrden,
|
||||
TaxDate = entidad.FechaOrden,
|
||||
FromWarehouse = entidad.CodigoSubAlmacenOrigen,
|
||||
ToWarehouse = entidad.CodigoSubAlmacenDestino,
|
||||
Comments = "Transferencia Generado desde Cubico - SAP",
|
||||
StockTransferLines = new List<BE_StockTransferLine>()
|
||||
};
|
||||
|
||||
entidad.TransferenciaDetalles.ForEach(x =>
|
||||
{
|
||||
var Detalle = new BE_StockTransferLine
|
||||
{
|
||||
FromWarehouseCode = entidad.CodigoSubAlmacenOrigen,
|
||||
WarehouseCode = entidad.CodigoSubAlmacenDestino,
|
||||
ItemCode = x.CodigoArticulo,
|
||||
Quantity = (double)x.CantidadPedida,
|
||||
BaseLine = entidad.DocEntry == 0 ? null : (int?)x.Item,
|
||||
BaseEntry = entidad.DocEntry == 0 ? null : (int?)entidad.DocEntry,
|
||||
BaseType = entidad.DocEntry == 0 ? null : "InventoryTransferRequest",
|
||||
LineNum = x.Item,
|
||||
BatchNumbers = new List<BE_StockTransferLineBatchNumber>()
|
||||
};
|
||||
StockTransfer.StockTransferLines.Add(Detalle);
|
||||
});
|
||||
|
||||
// == REALIZA DISTRIBUCION DE STOCK POR LOTE / ALMACEN
|
||||
StockTransfer.StockTransferLines.ForEach(line =>
|
||||
{
|
||||
#region CONSULTA A HANA
|
||||
|
||||
List<BE_ItemStock> ListaStock = HelperStock.Get(line.ItemCode, line.FromWarehouseCode);
|
||||
var CantidadPendiente = line.Quantity;
|
||||
var ListOrdenadaFecha = ListaStock.OrderBy(x => x.ExpDate).ToList();
|
||||
ListOrdenadaFecha.ForEach(Lote =>
|
||||
{
|
||||
if (CantidadPendiente > 0)
|
||||
{
|
||||
if (Lote.OnHandQty >= CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_StockTransferLineBatchNumber() { BatchNumber = Lote.DistNumber, Quantity = CantidadPendiente });
|
||||
CantidadPendiente = 0;
|
||||
}
|
||||
else if (Lote.OnHandQty < CantidadPendiente)
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_StockTransferLineBatchNumber() { BatchNumber = Lote.DistNumber, Quantity = Lote.OnHandQty });
|
||||
CantidadPendiente -= Lote.OnHandQty;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
#endregion
|
||||
});
|
||||
|
||||
//=== SI EL ALMACEN ES ALM99, FUERZA EL AMARRE CON LA ENTRADA DE MERCANCIA
|
||||
if (entidad.CodigoSubAlmacenOrigen == "ALM99")
|
||||
{
|
||||
StockTransfer.U_SYP_MDMT = "21";
|
||||
int DocEntry_Entada = entidad.DocEntryEM; //HelperInvoice.Get(entidad.NumOrden);
|
||||
if(DocEntry_Entada > 0)
|
||||
{
|
||||
PurchaseDeliveryNotesRepository _purchaseDeliveryNotesRepository = new PurchaseDeliveryNotesRepository(_clientFactory, _configuration);
|
||||
var _purchaseDeliveryNotesRepository_Response = _purchaseDeliveryNotesRepository.GetPurchaseDeliveryNotes(DocEntry_Entada).Result.ListData.FirstOrDefault();
|
||||
|
||||
//=== RESETEA STOCK POR LOTES
|
||||
StockTransfer.StockTransferLines.ForEach(line =>
|
||||
{
|
||||
var Lines = _purchaseDeliveryNotesRepository_Response.DocumentLines.FirstOrDefault(line_purchaseDeliveryNotesRepository_Response => line_purchaseDeliveryNotesRepository_Response.BaseLine == line.LineNum);
|
||||
line.BatchNumbers = new List<BE_StockTransferLineBatchNumber>();
|
||||
|
||||
//=== AGREGA LOTES
|
||||
Lines.BatchNumbers.ForEach(lotes =>
|
||||
{
|
||||
line.BatchNumbers.Add(new BE_StockTransferLineBatchNumber()
|
||||
{
|
||||
BatchNumber = lotes.BatchNumber,
|
||||
Quantity = lotes.Quantity,
|
||||
});
|
||||
});
|
||||
|
||||
line.BaseLine = Lines.LineNum;
|
||||
line.BaseType = "PurchaseDeliveryNotes";
|
||||
line.BaseEntry = DocEntry_Entada;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var ResultTransaction = stockTransferRepository.StockTransfer(StockTransfer).Result;
|
||||
if (ResultTransaction.StatusCode == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ResultTransaction.Message.ToString(), entidad);
|
||||
}
|
||||
|
||||
// === SOLO SE EJECUTA CUANDO TIENE UN DOCUMENTO BASE [SOLICITUD DE TRASLADO]
|
||||
if (entidad.DocEntry != 0)
|
||||
{
|
||||
var Result_SolicitudTraslado = stockTransferRepository.GetInventoryTransferRequest(entidad.DocEntry).Result;
|
||||
if (Result_SolicitudTraslado.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: La solicitud no se encuentra", entidad);
|
||||
}
|
||||
var _SolicitudTraslado = Result_SolicitudTraslado.ListData.FirstOrDefault();
|
||||
|
||||
// === SOLO CIERRA CUANDO EL DOCUMENTO ESTA ABIERTO
|
||||
if (_SolicitudTraslado.DocumentStatus == "bost_Open")
|
||||
{
|
||||
var Response_Close = stockTransferRepository.CloseInventoryTransferRequest(entidad.DocEntry).Result;
|
||||
if (Response_Close.DocEntry == -1)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado("SAP: La Solicitud no se puede cerrar en automatico", entidad);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resultadoTransaccion.GetInformacionResultado((int)ResultTransaction.Data.DocEntry, ResultTransaction.Data.DocNum.ToString(), "Registrado Correctamente", entidad);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return resultadoTransaccion.GetInformacionResultado(ex.Message.ToString(), entidad);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user