diff --git a/Controllers/GiriController.cs b/Controllers/GiriController.cs index b1b1b48..59139d6 100644 --- a/Controllers/GiriController.cs +++ b/Controllers/GiriController.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; using System.Diagnostics; using System.IdentityModel.Tokens.Jwt; +using System.Runtime.InteropServices; using System.Security.Claims; using System.Text; @@ -18,69 +19,108 @@ namespace ApiSoftway.Controllers { private readonly ILogger _logger; private readonly GESA_GIRI_DbContext _giri_context; + private readonly GESA_GIRICONSEGNE_DbContext _giricons_context; private readonly GESA_DESTINAZIONI_DbContext _destinazioni_context; private readonly GESA_CONSEGNE_DbContext _consegne_context; + private readonly GESA_GIRICONSEGNEVISTA_DbContext _consegnevista_context; + private readonly GESA_SBR_ORD_DbContext _sbrord_context; private readonly IConfiguration? _configuration; private DbSet? _giri; + private DbSet? _giriCons; + private DbSet? _giriConsView; private DbSet? _destinazioni; private DbSet? _consegne; + private DbSet? _sbrord; public GiriController(ILogger logger, IConfiguration? configuration, GESA_GIRI_DbContext giri_context, GESA_DESTINAZIONI_DbContext destinazioni_context - , GESA_CONSEGNE_DbContext consegne_context) + , GESA_CONSEGNE_DbContext consegne_context, GESA_GIRICONSEGNE_DbContext giricons_context, GESA_GIRICONSEGNEVISTA_DbContext consegnevista_context, GESA_SBR_ORD_DbContext sbrord_context) { _logger = logger; _configuration = configuration; _giri_context = giri_context; _destinazioni_context = destinazioni_context; _consegne_context = consegne_context; + _giricons_context = giricons_context; + _consegnevista_context = consegnevista_context; + _sbrord_context = sbrord_context; + } - //[HttpGet("listaGiri")] - //public async Task>> listaGiri(string token) - //{ + [HttpGet("listaGiri")] + public async Task>> listaGiri(string? autista, DateTime? data, bool aperto=true) + { - // List lst = new List(); - // string usr = getClaimValueByToken(token, "codice"); - // _giri = _giri_context.Giri; - // var r= await _giri.Where(t => t.Brautist != null && t.Brautist.Equals(usr)).OrderByDescending(t=>t.Brdatcar).Take(1).ToListAsync(); - // foreach(Giri giri in r) - // { - // Giri_out o=new Giri_out(); - // o.Autista = giri.Autista; - // o.Brdatcar = giri.Brdatcar; - // o.Brautist = giri.Brautist; - // o.num_dest=giri.num_dest; - // o.ItemList = formattaGiro(giri); - // lst.Add(o); - // } - // return lst; - //} - //[HttpGet("listaDestinazioni")] - //public async Task>> listaDestinazioni(string token, DateTime data) - //{ + List lst = new List(); + _giriConsView = _consegnevista_context.GiriView; + + if(aperto) + { + lst = await _giriConsView.Where(t => t.CodAutista != null && t.CodAutista.Equals(autista) && t.DataGiro != null && t.DataGiro == data && t.DataChiusura==null).OrderByDescending(t => t.DataGiro).ToListAsync(); + } + else + { + lst = await _giriConsView.Where(t => t.CodAutista != null && t.CodAutista.Equals(autista) && t.DataGiro != null && t.DataGiro == data && t.DataChiusura != null).OrderByDescending(t => t.DataGiro).ToListAsync(); + } + + return lst; + } + /// Salva i dati della consegna + [HttpPost] + [Route("addGiro")] + public async Task> addGiro(string? autista,int? bancali, decimal? importo, DateTime? data) + { + GiriConsegna_out tOut = new GiriConsegna_out(); + //string usr = getClaimValueByToken(token, "codice"); + try + { + //step 1 : calcolo il nuovo seriale + _giriConsView = _consegnevista_context.GiriView; + var ser= await _giriConsView.Take(1).OrderByDescending(t => t.SerialeGiro).ToListAsync(); + string ultSer = ser.First().SerialeGiro; + string newSer = calcolaNuovoSeriale(ultSer); + + //step 2 : inserisco in PIGIRCON + GiriConsegna gc=new GiriConsegna(); + gc.Piautist = autista; + gc.Pitbancar = bancali; + gc.Pidata = data; + gc.Pisergir = newSer; + gc.Pidarecu = importo; + using (var transactionGiri = _giricons_context.Database.BeginTransaction()) + { + await _giricons_context.GiriCons.AddAsync(gc); + await _giricons_context.SaveChangesAsync(); + transactionGiri.Commit(); + } + + //step 3 aggiorno con il seriale le righe delle destinazioni + _destinazioni = _destinazioni_context.Destinazioni; + var dest = await _destinazioni.Where(t => t.CodAutista != null && t.CodAutista.Equals(autista) && t.DataCarico != null && t.DataCarico == data && t.serialeGiro==null).OrderByDescending(t => t.DataCarico).ToListAsync(); + foreach(Destinazioni d in dest) + { + Sbr_ord sbr_Ord = new Sbr_ord(); + sbr_Ord.Brserial = d.Brserial; + sbr_Ord.Pisergir = newSer; + using (var transactionDest = _sbrord_context.Database.BeginTransaction()) + { + _sbrord_context.Entry(sbr_Ord).State = EntityState.Modified; + await _sbrord_context.SaveChangesAsync(); + transactionDest.Commit(); + } + } + + var giro = await _giriConsView.Where(t=>t.SerialeGiro.Equals(newSer)).ToListAsync(); + return StatusCode(StatusCodes.Status200OK, giro.First()); + } + catch (Exception ex) + { + string errmsg = ex.InnerException != null ? ex.InnerException.Message : ex.Message; + tOut.err_title = ex.Message; + tOut.err_detail = errmsg; + tOut.err_status_code = "500"; + return StatusCode(StatusCodes.Status500InternalServerError, tOut); + } + } - // List lst = new List(); - // string usr = getClaimValueByToken(token, "codice"); - // _destinazioni = _destinazioni_context.Destinazioni; - // var r = await _destinazioni.Where(t => t.CodAutista != null && t.CodAutista.Equals(usr)&& t.DataCarico != null && t.DataCarico == data).OrderByDescending(t => t.DataCarico).ToListAsync(); - // foreach(Destinazioni d in r) - // { - // Destinazioni_out o = new Destinazioni_out(); - // o.Autista = d.Autista; - // o.CodAutista = d.CodAutista; - // o.DescAutomezzo = d.DescAutomezzo; - // o.CodAutomezzo=d.CodAutomezzo; - // o.DataCarico = d.DataCarico; - // o.Brserial=d.Brserial; - // o.Brmerce=d.Brmerce; - // o.Brnote=d.Brnote; - // o.Cproword=d.Cproword; - // o.IndirizzoSede=d.IndirizzoSede; - // o.Sede=d.Sede; - // o.ItemList = formattaDestinazione(d); - // lst.Add(o); - // } - // return lst; - //} [HttpGet("listaDestinazioni")] public async Task>> listaDestinazioni2(string token) @@ -130,6 +170,7 @@ namespace ApiSoftway.Controllers o.CodSede = d.CodSede; o.ItemList = formattaDestinazione(d); o.ImportoDaRitirare = d.ImportoDaRitirare; + o.serialeGiro=d.serialeGiro; if (!string.IsNullOrEmpty(d.consFattaSerial)) { ConsegnaFatta cf=new ConsegnaFatta(); @@ -144,6 +185,7 @@ namespace ApiSoftway.Controllers cf.consFattaMezzo= d.consFattaMezzo; string nota2 = !string.IsNullOrEmpty(d.consFattaNotImp) ? d.consFattaNotImp.Trim() : string.Empty; cf.consFattaNotImp=nota2; + cf.consFattaFlagCons=d.consFattaFlagCons; o.ConsFatta=cf; } @@ -344,7 +386,6 @@ namespace ApiSoftway.Controllers return r; } - private Consegna fillConsegnaNonEffettuata(Consegna i, string token) { Consegna r = new Consegna(); @@ -429,6 +470,21 @@ namespace ApiSoftway.Controllers return r; } + private string calcolaNuovoSeriale(string ultSer) + { + string newSer = string.Empty; + + int ser=Convert.ToInt32(ultSer); + ser++; + newSer=Convert.ToString(ser); + + if (ser > 0) + { + newSer = newSer.PadLeft(10, '0'); + } + + return newSer; + } } } diff --git a/Models/Destinazioni.cs b/Models/Destinazioni.cs index ded3764..e7b51c6 100644 --- a/Models/Destinazioni.cs +++ b/Models/Destinazioni.cs @@ -35,6 +35,9 @@ namespace ApiSoftway.Models [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "decimal(18, 5)")] public decimal? ImportoDaRitirare { get; set; } + public string? consFattaFlagCons { get; set; } + + public string? serialeGiro { get; set; } } } diff --git a/Models/Destinazioni_out.cs b/Models/Destinazioni_out.cs index 447f948..41adeb3 100644 --- a/Models/Destinazioni_out.cs +++ b/Models/Destinazioni_out.cs @@ -24,6 +24,7 @@ namespace ApiSoftway.Models public decimal? ImportoDaRitirare { get; set; } public string? ItemList { get; set; } public ConsegnaFatta? ConsFatta { get; set; } + public string? serialeGiro { get; set; } } public class ConsegnaFatta { @@ -38,7 +39,8 @@ namespace ApiSoftway.Models public decimal? consFattaImpor { get; set; } public string? consFattaNotImp { get; set; } public string? consFattaNotBan { get; set; } - - + public string? consFattaFlagCons { get; set; } + + } } diff --git a/Models/Gesa_DbContext/GESA_GIRICONSEGNEVISTA_DbContext.cs b/Models/Gesa_DbContext/GESA_GIRICONSEGNEVISTA_DbContext.cs new file mode 100644 index 0000000..5ab8825 --- /dev/null +++ b/Models/Gesa_DbContext/GESA_GIRICONSEGNEVISTA_DbContext.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; + +namespace ApiSoftway.Models.Gesa_DbContext +{ + public class GESA_GIRICONSEGNEVISTA_DbContext : DbContext + { + public DbSet? GiriView { get; set; } + public GESA_GIRICONSEGNEVISTA_DbContext(DbContextOptions options) : base(options) + { + } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().ToView("API_GIRICONSEGNA"); + } + } +} diff --git a/Models/Gesa_DbContext/GESA_GIRICONSEGNE_DbContext.cs b/Models/Gesa_DbContext/GESA_GIRICONSEGNE_DbContext.cs new file mode 100644 index 0000000..f767b3d --- /dev/null +++ b/Models/Gesa_DbContext/GESA_GIRICONSEGNE_DbContext.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; + +namespace ApiSoftway.Models.Gesa_DbContext +{ + public class GESA_GIRICONSEGNE_DbContext: DbContext + { + /// + public DbSet? GiriCons { get; set; } + + /// + public GESA_GIRICONSEGNE_DbContext(DbContextOptions options) : base(options) + { + } + + /// + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().ToTable("GESAPIGIRCON"); + modelBuilder.Entity().HasKey(table => new { + table.Pisergir + }); + } + } +} diff --git a/Models/Gesa_DbContext/GESA_SBR_ORD_DbContext.cs b/Models/Gesa_DbContext/GESA_SBR_ORD_DbContext.cs new file mode 100644 index 0000000..af1348d --- /dev/null +++ b/Models/Gesa_DbContext/GESA_SBR_ORD_DbContext.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; + +namespace ApiSoftway.Models.Gesa_DbContext +{ + public class GESA_SBR_ORD_DbContext: DbContext + { + public DbSet? Cons { get; set; } + + /// + public GESA_SBR_ORD_DbContext(DbContextOptions options) : base(options) + { + } + + /// + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().ToTable("GESASBR_ORD"); + modelBuilder.Entity().HasKey(table => new { + table.Brserial + }); + } + } +} diff --git a/Models/GiriConsegna.cs b/Models/GiriConsegna.cs new file mode 100644 index 0000000..e8f0f98 --- /dev/null +++ b/Models/GiriConsegna.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; + +namespace ApiSoftway.Models +{ + public class GiriConsegna + { + [Key] + public string? Pisergir { get; set; } + public DateTime? Pidata { get; set; } + public string? Piautist { get; set; } + public int? Pitbancar { get; set; } + public int? Pitbanrec { get; set; } + + [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "decimal(18, 5)")] + public decimal? Pidarecu { get; set; } + + [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "decimal(18, 5)")] + public decimal? Pirecupe { get; set; } + + public DateTime? Pidatchi { get; set; } + + } +} diff --git a/Models/GiriConsegnaView.cs b/Models/GiriConsegnaView.cs new file mode 100644 index 0000000..93b19a5 --- /dev/null +++ b/Models/GiriConsegnaView.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; + +namespace ApiSoftway.Models +{ + [Keyless] + public class GiriConsegnaView + { + public string? SerialeGiro { get; set; } + public DateTime? DataGiro { get; set; } + public string? CodAutista { get; set; } + public string? Autista { get; set; } + public int? BancaliCaricati { get; set; } + public int? BancaliRecuperati { get; set; } + + [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "decimal(18, 5)")] + public decimal? ImportoDaRecuperare { get; set; } + + [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "decimal(18, 5)")] + public decimal? ImportoRecuperato { get; set; } + public DateTime? DataChiusura { get; set; } + + } +} diff --git a/Models/GiriConsegna_out.cs b/Models/GiriConsegna_out.cs new file mode 100644 index 0000000..86c8da8 --- /dev/null +++ b/Models/GiriConsegna_out.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; + +namespace ApiSoftway.Models +{ + [Keyless] + public class GiriConsegna_out + { + public string? Pisergir { get; set; } + public DateTime? Pidata { get; set; } + public string? Piautist { get; set; } + public int? Pitbancar { get; set; } + public int? Pitbanrec { get; set; } + + [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "decimal(18, 5)")] + public decimal? Pidarecu { get; set; } + + [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "decimal(18, 5)")] + public decimal? Pirecupe { get; set; } + public DateTime? Pidatchi { get; set; } + + /// errore titolo + public string? err_title { get; set; } + /// errore dettaglio + public string? err_detail { get; set; } + /// errore status code (200, 500) + public string? err_status_code { get; set; } + } +} diff --git a/Models/Sbr_ord.cs b/Models/Sbr_ord.cs new file mode 100644 index 0000000..c7744e5 --- /dev/null +++ b/Models/Sbr_ord.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace ApiSoftway.Models +{ + public class Sbr_ord + { + [Key] + public string? Brserial { get; set; } + public string? Pisergir { get; set; } + } +} diff --git a/Program.cs b/Program.cs index 00626b3..62f75b7 100644 --- a/Program.cs +++ b/Program.cs @@ -27,12 +27,18 @@ builder.Services.AddDbContext(options => options.Us builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("GESA") , options => { options.CommandTimeout(commandTimeoutInSeconds); } )); - builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("GESA") , options => { options.CommandTimeout(commandTimeoutInSeconds); } )); - - +builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("GESA") + , options => { options.CommandTimeout(commandTimeoutInSeconds); } + )); +builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("GESA") + , options => { options.CommandTimeout(commandTimeoutInSeconds); } + )); +builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("GESA") + , options => { options.CommandTimeout(commandTimeoutInSeconds); } + )); #endregion @@ -55,5 +61,5 @@ app.UseAuthorization(); app.MapControllers(); -app.Run(); -//app.Run("http://localhost:6000"); +//app.Run(); +app.Run("http://localhost:6000");