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, 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> Create(BE_PickingRequest entidad) { _metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString(); ResultadoTransaccion resultadoTransaccion = new ResultadoTransaccion(_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 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); // === Solo para el proceso "ULTIMA MILLA": "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. // DESPACHO, PICKING y demas procesos no dependen de este campo. if (string.Equals(entidad.Proceso, "ULTIMA MILLA", StringComparison.OrdinalIgnoreCase) && !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() }; InvoiceRepository invoicestRepository = new InvoiceRepository(_clientFactory, _configuration, context); BE_ResultadoTransaccion 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(), 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 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 ListaUnicaAlmacen = creditNotesNotes.DocumentLines.Select(x => x.WarehouseCode).Distinct().ToList(); List ListDocEntry = new List(); ListaUnicaAlmacen.ForEach(Almacen => { List 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(); Transfer.DocumentReferences = new List(); ListaUnicaArticulo.ForEach(x => { var ListLote = new List(); 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 { 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(); 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(), 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(), 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 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() }; InvoiceRepository invoicestRepository = new InvoiceRepository(_clientFactory, _configuration, context); BE_ResultadoTransaccion 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(), 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 ListaUnicaAlmacen = creditNotesNotes.DocumentLines.Select(x => x.WarehouseCode).Distinct().ToList(); List ListDocEntry = new List(); ListaUnicaAlmacen.ForEach(Almacen => { List 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(); Transfer.DocumentReferences = new List(); ListaUnicaArticulo.ForEach(x => { var ListLote = new List(); 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 { 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(); 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 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() }; 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(), 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> CreateHojaRuta(BE_HojaRutaEntities entidad) { _metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString(); ResultadoTransaccion resultadoTransaccion = new ResultadoTransaccion(_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 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> CerrarHojaRuta(int docEntry) { _metodoName = "CerrarHojaRuta"; ResultadoTransaccion resultadoTransaccion = new ResultadoTransaccion(_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); } } } }