184 lines
9.3 KiB
C#
184 lines
9.3 KiB
C#
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);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|