130 lines
7.1 KiB
C#
130 lines
7.1 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Net.Business.Entities.ServicesLayer.CreditNotes;
|
|
using Net.Business.Entities.ServicesLayer.StockTransfer;
|
|
using Net.Data.ServicesLayer.CreditNotes;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
|
|
namespace Net.Data.ServicesLayer.StockTransfer
|
|
{
|
|
// Transferencias de stock automaticas de una nota de credito (Picking DESPACHO/PICKING y Recibo Devolucion).
|
|
// Antes este bloque estaba copiado 3 veces.
|
|
public static class TransferenciaAutomaticaNC
|
|
{
|
|
// Por cada almacen de origen (WarehouseCode) y almacen destino (U_ITC_ALMDES) distintos de las lineas de la NC
|
|
// crea una transferencia de stock en SAP, con las cantidades y lotes de esas lineas y la NC como referencia.
|
|
// Luego agrega a la NC una referencia por cada transferencia creada.
|
|
// La NC ya esta creada, asi que un fallo aqui NO la deshace ni responde error (el consumidor reintentaria y
|
|
// duplicaria la NC). Devuelve el texto de advertencia para el mensaje de la respuesta ("" si todo salio bien):
|
|
// transferencias que SAP rechazo y/o la referencia que no se pudo agregar a la NC.
|
|
public static string Crear(BE_CreditNotesNotes creditNotesNotes, int docEntryNC, IHttpClientFactory clientFactory, IConfiguration configuration, CreditNotesRepository creditNotesRepository)
|
|
{
|
|
List<string> ListaUnicaAlmacen = creditNotesNotes.DocumentLines.Select(x => x.WarehouseCode).Distinct().ToList();
|
|
List<int> ListDocEntry = new List<int>();
|
|
List<string> Advertencias = new List<string>();
|
|
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>();
|
|
|
|
//=== AGREGA DOCUMENTO REFERENCIA
|
|
Transfer.DocumentReferences = new List<BE_StockTransferDocumentReferences>
|
|
{
|
|
new BE_StockTransferDocumentReferences()
|
|
{
|
|
RefDocEntr = docEntryNC,
|
|
RefObjType = "rot_SalesCreditNote",
|
|
IssueDate = DateTime.Now,
|
|
Remark = "Nota de Credito",
|
|
}
|
|
};
|
|
|
|
ListaUnicaArticulo.ForEach(x =>
|
|
{
|
|
var ListLote = new List<BE_StockTransferLineBatchNumber>();
|
|
// Lotes de todas las lineas del articulo (si el mismo lote esta en varias lineas se suma)
|
|
ListaDetalle_PorAlmacen.Where(t => t.ItemCode == x).SelectMany(t => t.BatchNumbers).GroupBy(r => r.BatchNumber).ToList().ForEach(g =>
|
|
{
|
|
ListLote.Add(new BE_StockTransferLineBatchNumber()
|
|
{
|
|
BatchNumber = g.Key,
|
|
Quantity = g.Sum(r => 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
|
|
});
|
|
});
|
|
|
|
//=== CREA INSTANCIA PARA CONECTAR A SAP
|
|
StockTransferRepository stockTransferRepository = new StockTransferRepository(clientFactory, configuration);
|
|
var ResultTransfer = stockTransferRepository.StockTransfer(Transfer).Result;
|
|
if (ResultTransfer.StatusCode != -1)
|
|
{
|
|
ListDocEntry.Add((int)ResultTransfer.Data.DocEntry);
|
|
}
|
|
else
|
|
{
|
|
Advertencias.Add($"no se creo la transferencia {Almacen} -> {Destino}: {ResultTransfer.Message}");
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
// Referencia las transferencias creadas en la NC
|
|
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, docEntryNC).Result;
|
|
if (Actualizacion.StatusCode == -1)
|
|
{
|
|
Advertencias.Add($"se crearon las transferencias {string.Join(", ", ListDocEntry)} pero no se pudo agregar su referencia a la NC: {Actualizacion.Message}");
|
|
}
|
|
}
|
|
|
|
return Advertencias.Count == 0
|
|
? ""
|
|
: " ADVERTENCIA: la nota de credito se creo, pero " + string.Join("; ", Advertencias) + ".";
|
|
}
|
|
}
|
|
}
|