5b357e2cea
- gestione dati x fattura not mapped
605 lines
23 KiB
C#
605 lines
23 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using SHERPA.Data.DbModels;
|
|
|
|
namespace SHERPA.Data.Controllers
|
|
{
|
|
public class SInManController : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public SInManController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
Log.Info("Avviata classe BBMController.SHERPAFattContext");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public bool AccDocUpdate(AccoDocModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var currData = dbCtx
|
|
.DbSetAccoDocs
|
|
.Where(x => x.IdxFatt == updItem.IdxFatt)
|
|
.FirstOrDefault();
|
|
if (currData != null)
|
|
{
|
|
currData.Anno = updItem.Anno;
|
|
currData.Num = updItem.Num;
|
|
currData.IdxCli = updItem.IdxCli;
|
|
currData.Emesso = updItem.Emesso;
|
|
currData.Ritenuta = updItem.Ritenuta;
|
|
currData.Scadenza = updItem.Scadenza;
|
|
currData.Tipo = updItem.Tipo;
|
|
currData.NewId = updItem.NewId;
|
|
currData.Token = updItem.Token;
|
|
dbCtx.Entry(currData).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
dbCtx
|
|
.DbSetAccoDocs
|
|
.Add(updItem);
|
|
}
|
|
dbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in AccDocUpdate{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina item AccoDoc (Accounting Document)
|
|
/// </summary>
|
|
/// <param name="FattId"></param>
|
|
/// <returns></returns>
|
|
public bool AccoDocDelete(int FattId)
|
|
{
|
|
bool done = false;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var item2del = dbCtx
|
|
.DbSetAccoDocs
|
|
.Where(x => x.IdxFatt == FattId)
|
|
.FirstOrDefault();
|
|
if (item2del != null)
|
|
{
|
|
dbCtx.DbSetAccoDocs.Remove(item2del);
|
|
dbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in AccoDocDelete{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera AccoDoc (Accounting Document) da Key
|
|
/// </summary>
|
|
/// <param name="newItem"></param>
|
|
/// <returns></returns>
|
|
public AccoDocModel? AccoDocGetByKey(int DocId)
|
|
{
|
|
AccoDocModel? currDoc = new AccoDocModel();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
currDoc = dbCtx
|
|
.DbSetAccoDocs
|
|
.Include(d => d.RigheFattNav)
|
|
.ThenInclude(r => r.VatNav)
|
|
.Where(x => x.IdxFatt == DocId)
|
|
.FirstOrDefault();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in AccoDocGetByKey{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return currDoc;
|
|
}
|
|
|
|
/// <returns></returns>
|
|
public List<AccoDocModel> AccoDocGetFilt(int anno, int numero, string tipoDoc, bool soloMod, string searchVal)
|
|
{
|
|
List<AccoDocModel> dbResult = new List<AccoDocModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetAccoDocs
|
|
.Where(x => (x.Anno == anno || anno == 0) && (x.Num == numero || numero == 0) && ((x.Changed > 0 && soloMod) || !soloMod) && (x.Tipo == tipoDoc || string.IsNullOrEmpty(tipoDoc)) && ((x.CustomerNav != null && x.CustomerNav.RagSoc != null && x.CustomerNav.RagSoc.Contains(searchVal)) || string.IsNullOrEmpty(searchVal)))
|
|
.Include(d => d.RigheFattNav)
|
|
.Include(d => d.CustomerNav)
|
|
.Include(d => d.ContributiNav)
|
|
.Include(d => d.ScadenzeNav)
|
|
.Include(d => d.PagamentiNav)
|
|
.OrderByDescending(x => x.Anno)
|
|
.ThenByDescending(x => x.Num)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco ultimi AccoDoc (Accounting Document)
|
|
/// </summary>
|
|
/// <param name="numRecord"></param>
|
|
/// <returns></returns>
|
|
public List<AccoDocModel> AccoDocGetLastDesc(int numRecord)
|
|
{
|
|
List<AccoDocModel> dbResult = new List<AccoDocModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
// se numRecord = 0 --> passo tutti
|
|
if (numRecord == 0)
|
|
{
|
|
numRecord = dbCtx.DbSetAccoDocs.Count();
|
|
}
|
|
dbResult = dbCtx
|
|
.DbSetAccoDocs
|
|
.OrderByDescending(x => x.Anno)
|
|
.ThenByDescending(x => x.Num)
|
|
.Take(numRecord)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista configurazione
|
|
/// </summary>
|
|
/// <param name="dtStart"></param>
|
|
/// <param name="dtEnd"></param>
|
|
/// <returns></returns>
|
|
public List<ConfigModel> ConfigGetAll()
|
|
{
|
|
List<ConfigModel> dbResult = new List<ConfigModel>();
|
|
using (SHERPAFattContext localDbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetConfig
|
|
.OrderBy(x => x.Chiave)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public int CustomersCount()
|
|
{
|
|
int answ = 0;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
answ = dbCtx
|
|
.DbSetCustomers
|
|
.Count();
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Customers
|
|
/// </summary>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public List<CustomerModel> CustomersGetAll(string searchVal)
|
|
{
|
|
List<CustomerModel> dbResult = new List<CustomerModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetCustomers
|
|
.Where(x => (x.RagSoc.Contains(searchVal) || x.CodUnivoco.Contains(searchVal) || string.IsNullOrEmpty(searchVal)))
|
|
.OrderBy(x => x.RagSoc)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
/// <summary>
|
|
/// Ricerca Customer da idx
|
|
/// </summary>
|
|
/// <param name="idxCli"></param>
|
|
/// <returns></returns>
|
|
public CustomerModel? CustomersGetByKey(int idxCli)
|
|
{
|
|
CustomerModel? dbResult = new CustomerModel();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetCustomers
|
|
.Where(x => (x.IdxCli== idxCli))
|
|
.FirstOrDefault();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Customers che richiedono sync tramite stored
|
|
/// </summary>
|
|
/// <param name="tipoDoc">Tipo doc, * = tutti</param>
|
|
/// <param name="anno">Anno doc, 0=tutti</param>
|
|
/// <param name="notUpl">Indica se solo non caricati (1 = NON caricati)</param>
|
|
/// <returns></returns>
|
|
public List<CustomerModel> CustomersToSync(string tipoDoc, int anno, bool notUpl)
|
|
{
|
|
List<CustomerModel> dbResult = new List<CustomerModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
SqlParameter Tipo = new SqlParameter("@tipo", tipoDoc);
|
|
SqlParameter Anno = new SqlParameter("@anno", anno);
|
|
SqlParameter NotUpl = new SqlParameter("@notUpl", notUpl);
|
|
|
|
dbResult = dbCtx
|
|
.DbSetCustomers
|
|
.FromSqlRaw("EXEC stp_clienti_getByAnno @tipo,@anno,@notUpl", Tipo, Anno, NotUpl)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiorna un record Cliente
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public bool CustomerUpdate(CustomerModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var currData = dbCtx
|
|
.DbSetCustomers
|
|
.Where(x => x.IdxCli == updItem.IdxCli)
|
|
.FirstOrDefault();
|
|
if (currData != null)
|
|
{
|
|
currData.Attivo = updItem.Attivo;
|
|
currData.Cap = updItem.Cap;
|
|
currData.CFisc = updItem.CFisc;
|
|
currData.CodiceCliente = updItem.CodiceCliente;
|
|
currData.CodUnivoco = updItem.CodUnivoco;
|
|
currData.IdExt = updItem.IdExt;
|
|
currData.IdxGruppo = updItem.IdxGruppo;
|
|
currData.Localita = updItem.Localita;
|
|
currData.Note = updItem.Note;
|
|
currData.Pagamento = updItem.Pagamento;
|
|
currData.Pec = updItem.Pec;
|
|
currData.PIva = updItem.PIva;
|
|
currData.Provincia = updItem.Provincia;
|
|
currData.RagSoc = updItem.RagSoc;
|
|
currData.Valuta = updItem.Valuta;
|
|
currData.Via = updItem.Via;
|
|
dbCtx.Entry(currData).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
dbCtx
|
|
.DbSetCustomers
|
|
.Add(updItem);
|
|
}
|
|
dbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in CustomerUpdate{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
GC.Collect();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco AccoDoc (Accounting Document) filtrati
|
|
/// </summary>
|
|
/// <param name="anno">anno di riferimento (0: tutti)</param>
|
|
/// <param name="numero">num documento (0: tutti)</param>
|
|
/// <param name="tipoDoc">tipo Doc</param>
|
|
/// <param name="soloMod">Solo documenti modificati (da inviare in cloud)</param>
|
|
/// <param name="searchVal">Ricerca libera</param>
|
|
/// <summary>
|
|
/// Aggiunge un nuovo item Doc
|
|
/// </summary>
|
|
/// <param name="newItem"></param>
|
|
/// <returns></returns>
|
|
public bool DocsAddNew(AccoDocModel newItem)
|
|
{
|
|
bool done = false;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
dbCtx
|
|
.DbSetAccoDocs
|
|
.Add(newItem);
|
|
dbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in DocsAddNew{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Conteggio documenti x criteri
|
|
/// </summary>
|
|
/// <param name="anno"></param>
|
|
/// <param name="tipoDoc"></param>
|
|
/// <returns></returns>
|
|
public int DocsCount(int anno, string tipoDoc)
|
|
{
|
|
int answ = 0;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
answ = dbCtx
|
|
.DbSetAccoDocs
|
|
.Where(x => (x.Anno == anno || anno == 0) && (x.Tipo == tipoDoc || string.IsNullOrEmpty(tipoDoc)))
|
|
.Count();
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiorna un item Doc
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public bool DocUpdate(AccoDocModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var currData = dbCtx
|
|
.DbSetAccoDocs
|
|
.Where(x => x.IdxFatt == updItem.IdxFatt)
|
|
.FirstOrDefault();
|
|
if (currData != null)
|
|
{
|
|
currData.Anno = updItem.Anno;
|
|
currData.Num = updItem.Num;
|
|
currData.DtEvent = updItem.DtEvent;
|
|
currData.IdxCli = updItem.IdxCli;
|
|
currData.Tipo = updItem.Tipo;
|
|
currData.Ritenuta = updItem.Ritenuta;
|
|
currData.Scelta = updItem.Scelta;
|
|
currData.Token = updItem.Token;
|
|
currData.NewId = updItem.NewId;
|
|
// segno modificato!
|
|
dbCtx.Entry(currData).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
dbCtx
|
|
.DbSetAccoDocs
|
|
.Add(updItem);
|
|
}
|
|
dbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in DocUpdate{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco AccoDoc (Accounting Document) filtrati
|
|
/// </summary>
|
|
/// <param name="anno">anno di riferimento (0: tutti)</param>
|
|
/// <param name="idxCli">Idx Cliente (0: tutti)</param>
|
|
/// <param name="tipoDoc">tipo Doc (*=tutti)</param>
|
|
/// <param name="idxGruppo">Idx Gruppo (0: tutti)</param>
|
|
/// <param name="ragSoc">tipo Doc (*=tutti)</param>
|
|
/// <param name="inizio">Data inizio ricerca (emissione)</param>
|
|
/// <param name="fine">Data inizio ricerca (emissione)</param>
|
|
/// <param name="aperta">0 = tutto / 1 = aperta, NON pagata</param>
|
|
/// <param name="searchVal">Ricerca libera</param>
|
|
/// <returns></returns>
|
|
public List<vDocsExplModel> VDocExplGetFilt(int anno, int idxCli, string tipoDoc, string idxGruppo, string ragSoc, DateTime inizio, DateTime fine, int aperta, string searchVal)
|
|
{
|
|
List<vDocsExplModel> dbResult = new List<vDocsExplModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
SqlParameter Anno = new SqlParameter("@anno", anno);
|
|
SqlParameter IdxCli = new SqlParameter("@idxCli", idxCli);
|
|
SqlParameter Tipo = new SqlParameter("@tipo", tipoDoc);
|
|
SqlParameter IdxGruppo = new SqlParameter("@idxGruppo", idxGruppo);
|
|
SqlParameter RagSoc = new SqlParameter("@ragSoc", ragSoc);
|
|
SqlParameter DataFrom = new SqlParameter("@inizio", inizio);
|
|
SqlParameter DataTo = new SqlParameter("@fine", fine);
|
|
SqlParameter Aperta = new SqlParameter("@aperta", aperta);
|
|
SqlParameter Search = new SqlParameter("@search", searchVal);
|
|
|
|
dbResult = dbCtx
|
|
.DbSetDocExpl
|
|
.FromSqlRaw("EXEC stp_docs_getFilt @anno,@idxCli,@tipo,@idxGruppo,@ragSoc,@inizio,@fine,@aperta,@search", Anno, IdxCli, Tipo, IdxGruppo, RagSoc, DataFrom, DataTo, Aperta, Search)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<vSelCliModel> VSelCliGetAll()
|
|
{
|
|
List<vSelCliModel> dbResult = new List<vSelCliModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetSelCli
|
|
.OrderBy(x => x.label)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<vSelGruppiModel> VSelGruppiGetAll()
|
|
{
|
|
List<vSelGruppiModel> dbResult = new List<vSelGruppiModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetSelGruppi
|
|
.OrderBy(x => x.label)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public List<vSelTipoModel> VSelTipoGetAll()
|
|
{
|
|
List<vSelTipoModel> dbResult = new List<vSelTipoModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetSelTipo
|
|
.OrderBy(x => x.value)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Elenco VAT / CIva
|
|
/// </summary>
|
|
/// <param name="searchVal"></param>
|
|
/// <returns></returns>
|
|
public List<VatModel> VATGetAll(string searchVal)
|
|
{
|
|
List<VatModel> dbResult = new List<VatModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetVat
|
|
.Where(x => (x.Descrizione.Contains(searchVal) || string.IsNullOrEmpty(searchVal)))
|
|
.OrderBy(x => x.Descrizione)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
/// <summary>
|
|
/// Ricerca VAT / CIva da idx
|
|
/// </summary>
|
|
/// <param name="idxVat"></param>
|
|
/// <returns></returns>
|
|
public VatModel? VATGetByKey(int idxVat)
|
|
{
|
|
VatModel? dbResult = new VatModel();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetVat
|
|
.Where(x => (x.IdxVat == idxVat))
|
|
.FirstOrDefault();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco VAT / CIva che richiedono sync tramite stored
|
|
/// </summary>
|
|
/// <param name="tipoDoc">Tipo doc, * = tutti</param>
|
|
/// <param name="anno">Anno doc, 0=tutti</param>
|
|
/// <param name="notUpl">Indica se solo non caricati (1 = NON caricati)</param>
|
|
/// <returns></returns>
|
|
public List<VatModel> VATToSync(string tipoDoc, int anno, bool notUpl)
|
|
{
|
|
List<VatModel> dbResult = new List<VatModel>();
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
SqlParameter Tipo = new SqlParameter("@tipo", tipoDoc);
|
|
SqlParameter Anno = new SqlParameter("@anno", anno);
|
|
SqlParameter NotUpl = new SqlParameter("@notUpl", notUpl);
|
|
|
|
dbResult = dbCtx
|
|
.DbSetVat
|
|
.FromSqlRaw("EXEC stp_VAT_getByAnno @tipo,@anno,@notUpl", Tipo, Anno, NotUpl)
|
|
.ToList();
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiorna un record VAT / CIva
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public bool VATUpdate(VatModel updItem)
|
|
{
|
|
bool done = false;
|
|
using (SHERPAFattContext dbCtx = new SHERPAFattContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
var currData = dbCtx
|
|
.DbSetVat
|
|
.Where(x => x.IdxVat == updItem.IdxVat)
|
|
.FirstOrDefault();
|
|
if (currData != null)
|
|
{
|
|
currData.Iva = updItem.Iva;
|
|
currData.Descrizione = updItem.Descrizione;
|
|
currData.Enabled= updItem.Enabled;
|
|
currData.SplitPay= updItem.SplitPay;
|
|
currData.id_ext = updItem.id_ext;
|
|
dbCtx.Entry(currData).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
dbCtx
|
|
.DbSetVat
|
|
.Add(updItem);
|
|
}
|
|
dbCtx.SaveChanges();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in VATUpdate{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |