using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Net.Business.Entities; using Net.Business.Entities.ServicesLayer; using Net.Business.Entities.ServicesLayer.CreditNotes; using Net.Connection; using Net.Connection.ServiceLayer; using Net.CrossCotting; using Net.Data.HelperData.Log; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Net.Data.ServicesLayer.CreditNotes { public class CreditNotesRepository : ICreditNotesRepository { #region Variables Globales private string _aplicacionName; private string _metodoName; private readonly Regex regex = new Regex(@"<(\w+)>.*"); //Services Layer private readonly ConnectionServiceLayer _connectServiceLayer; private readonly IConnectionSQL _context; #endregion public CreditNotesRepository(IHttpClientFactory clientFactory, IConfiguration configuration, IConnectionSQL context) { _aplicacionName = this.GetType().Name; _connectServiceLayer = new ConnectionServiceLayer(configuration, clientFactory); _context = context; } public async Task> SetCreateCreditNotes(BE_CreditNotesNotes entidad) { _metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString(); ResultadoTransaccion resultadoTransaccion = new ResultadoTransaccion(_metodoName, _aplicacionName); try { //var cadena = "Drafts"; var cadena = "CreditNotes"; var data = await _connectServiceLayer.PostAsync(cadena, entidad); if (!data.Exito) { // Solo si SAP rechaza: guarda el JSON enviado al Service Layer para poder revisarlo HelperLogServiceLayer.AddError("NotaCredito Error", DocEntryOrigen(entidad), entidad, data.Mensaje); return resultadoTransaccion.GetInformacionResultado(-1, -1, data.Mensaje); } return resultadoTransaccion.GetInformacionResultado(0, "", "Se Listo correctamento...!!!", data); } catch (Exception ex) { HelperLogServiceLayer.AddError("NotaCredito Error", DocEntryOrigen(entidad), entidad, ex.Message); return resultadoTransaccion.GetInformacionResultado(-1, -1, ex.Message.ToString()); } } // DocEntry del documento base (factura o solicitud de devolucion) de la primera linea private static string DocEntryOrigen(BE_CreditNotesNotes entidad) { return entidad?.DocumentLines?.Count > 0 ? entidad.DocumentLines[0].BaseEntry?.ToString() : null; } public async Task> UpdateCreateCreditNotes(dynamic entidad, int DocEntry) { _metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString(); ResultadoTransaccion resultadoTransaccion = new ResultadoTransaccion(_metodoName, _aplicacionName); try { //var cadena = "Drafts"; var cadena = $"CreditNotes({DocEntry})"; var data = await _connectServiceLayer.PatchAsyncSBA(cadena, entidad); if (!data.Exito) { return resultadoTransaccion.GetInformacionResultado(-1, -1, data.Mensaje); } return resultadoTransaccion.GetInformacionResultado(0, "", "Se Listo correctamento...!!!", data); } catch (Exception ex) { return resultadoTransaccion.GetInformacionResultado(-1, -1, ex.Message.ToString()); } } } }