330 lines
12 KiB
C#
330 lines
12 KiB
C#
using ApiSoftway.Models;
|
|
using ApiSoftway.Models.Gesa_DbContext;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Diagnostics;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
|
|
namespace ApiSoftway.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class GiriController : ControllerBase
|
|
{
|
|
private readonly ILogger<LoginController> _logger;
|
|
private readonly GESA_GIRI_DbContext _giri_context;
|
|
private readonly GESA_DESTINAZIONI_DbContext _destinazioni_context;
|
|
private readonly GESA_CONSEGNE_DbContext _consegne_context;
|
|
private readonly IConfiguration? _configuration;
|
|
private DbSet<Giri>? _giri;
|
|
private DbSet<Destinazioni>? _destinazioni;
|
|
private DbSet<Consegna>? _consegne;
|
|
public GiriController(ILogger<LoginController> logger, IConfiguration? configuration, GESA_GIRI_DbContext giri_context, GESA_DESTINAZIONI_DbContext destinazioni_context
|
|
, GESA_CONSEGNE_DbContext consegne_context)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_giri_context = giri_context;
|
|
_destinazioni_context = destinazioni_context;
|
|
_consegne_context = consegne_context;
|
|
}
|
|
|
|
//[HttpGet("listaGiri")]
|
|
//public async Task<ActionResult<IEnumerable<Giri_out>>> listaGiri(string token)
|
|
//{
|
|
|
|
// List<Giri_out> lst = new List<Giri_out>();
|
|
// 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<ActionResult<IEnumerable<Destinazioni_out>>> listaDestinazioni(string token, DateTime data)
|
|
//{
|
|
|
|
// List<Destinazioni_out> lst = new List<Destinazioni_out>();
|
|
// 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<ActionResult<IEnumerable<Destinazioni_out>>> listaDestinazioni2(string token)
|
|
{
|
|
//step 1: ricavo la data a partire dal login
|
|
string usr = getClaimValueByToken(token, "codice");
|
|
_giri = _giri_context.Giri;
|
|
var g = await _giri.Where(t => t.Brautist != null && t.Brautist.Equals(usr)).OrderByDescending(t => t.Brdatcar).Take(1).ToListAsync();
|
|
DateTime? data = g.First().Brdatcar;
|
|
|
|
|
|
List<Destinazioni_out> lst = new List<Destinazioni_out>();
|
|
_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.CodCommittente = d.CodCommittente;
|
|
o.Committente = d.Committente;
|
|
o.CodSede = d.CodSede;
|
|
o.ItemList = formattaDestinazione(d);
|
|
if(!string.IsNullOrEmpty(d.consFattaSerial))
|
|
{
|
|
ConsegnaFatta cf=new ConsegnaFatta();
|
|
cf.consFattaSerial= d.consFattaSerial;
|
|
cf.consFattaRow= d.consFattaRow;
|
|
cf.consFattaAut= d.consFattaAut;
|
|
cf.consFattaBanSca = d.consFattaBanSca;
|
|
cf.consFattaBanCar= d.consFattaBanCar;
|
|
string nota = !string.IsNullOrEmpty(d.consFattaNotBan) ? d.consFattaNotBan.Trim() : string.Empty;
|
|
cf.consFattaNotBan= nota;
|
|
cf.consFattaImpor=d.consFattaImpor;
|
|
cf.consFattaMezzo= d.consFattaMezzo;
|
|
string nota2 = !string.IsNullOrEmpty(d.consFattaNotImp) ? d.consFattaNotImp.Trim() : string.Empty;
|
|
cf.consFattaNotImp=nota2;
|
|
|
|
o.ConsFatta=cf;
|
|
}
|
|
|
|
lst.Add(o);
|
|
}
|
|
return lst;
|
|
}
|
|
|
|
/// <summary>Salva i dati della consegna</summary>
|
|
[HttpPost]
|
|
[Route("destinazione/salva")]
|
|
public async Task<ActionResult<Consegna_out>> timbrature_salva([FromBody] Consegna model, string token)
|
|
{
|
|
Consegna_out tOut = new Consegna_out();
|
|
string usr = getClaimValueByToken(token, "codice");
|
|
try
|
|
{
|
|
|
|
//string tecnico = getClaimValueByToken(token, "tccodice");
|
|
if (await checkConsegnaPresente(token, model) == 0)
|
|
{
|
|
Consegna t = fillConsegna(model, token);
|
|
using (var transaction = _consegne_context.Database.BeginTransaction())
|
|
{
|
|
await _consegne_context.Cons.AddAsync(t);
|
|
await _consegne_context.SaveChangesAsync();
|
|
transaction.Commit();
|
|
}
|
|
|
|
tOut = fillConsegna_out(model, token);
|
|
tOut.err_status_code = "200";
|
|
return StatusCode(StatusCodes.Status200OK, tOut);
|
|
}
|
|
else
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "timbratura presente.");
|
|
}
|
|
|
|
//return tOut;
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
private async Task<int> checkConsegnaPresente(string token, Consegna model)
|
|
{
|
|
int trovati = 0;
|
|
_consegne = _consegne_context.Cons;
|
|
var ti = await _consegne.Where(t => t.Serial.Equals(model.Serial) && t.Cprownum == model.Cprownum ).ToListAsync();
|
|
if (ti.Any())
|
|
{
|
|
trovati = ti.Count();
|
|
}
|
|
|
|
return trovati;
|
|
}
|
|
private string getClaimValueByToken(string token, string claimName)
|
|
{
|
|
string t = string.Empty;
|
|
|
|
var handler = new JwtSecurityTokenHandler();
|
|
var jwtSecurityToken = handler.ReadJwtToken(token);
|
|
if (jwtSecurityToken != null)
|
|
{
|
|
var id = jwtSecurityToken.Claims.First(claim => claim.Type == claimName).Value;
|
|
t = id;
|
|
}
|
|
return t;
|
|
}
|
|
private string formattaGiro(Giri g)
|
|
{
|
|
string item = string.Empty;
|
|
if (g != null)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
var dateString1 = g.Brdatcar.Value.ToString("dd-MM-yyyy");
|
|
sb.AppendLine("Giro del "+dateString1);
|
|
sb.AppendLine("Assegnato a "+g.Autista);
|
|
sb.AppendLine("Numero consegne " + g.num_dest.ToString());
|
|
item=sb.ToString();
|
|
sb = null;
|
|
}
|
|
|
|
return item;
|
|
}
|
|
private string formattaDestinazione(Destinazioni d)
|
|
{
|
|
string item = string.Empty;
|
|
if (d != null)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
//var dateString1 = d.DataCarico.Value.ToString("dd-MM-yyyy");
|
|
string comm = !string.IsNullOrEmpty(d.Committente) ? d.Committente.Trim() : string.Empty;
|
|
|
|
sb.Append("<b>Committente</b> " + comm);
|
|
sb.Append("<br> ");
|
|
string sed = !string.IsNullOrEmpty(d.Sede) ? d.Sede.Trim() : string.Empty;
|
|
sb.Append("<b>Sede Consegna</b> " + sed);
|
|
sb.Append("<br> ");
|
|
string ind = !string.IsNullOrEmpty(d.IndirizzoSede) ? d.IndirizzoSede.Trim() : string.Empty;
|
|
sb.Append("<b>Indirizzo</b> " + ind);
|
|
sb.Append("<br> ");
|
|
item = sb.ToString();
|
|
sb = null;
|
|
}
|
|
|
|
return item;
|
|
}
|
|
private Consegna fillConsegna(Consegna i, string token)
|
|
{
|
|
Consegna r = new Consegna();
|
|
|
|
string usr = getClaimValueByToken(token, "codice");
|
|
|
|
r.Piautist = usr;
|
|
r.Cprownum = i.Cprownum;
|
|
r.Ccddd = i.Ccddd;
|
|
r.cpccchk = i.cpccchk;
|
|
r.Ccda = i.Ccda;
|
|
r.Dcda = i.Dcda;
|
|
r.Pinotban = i.Pinotban;
|
|
r.Ccadd = i.Ccadd;
|
|
r.Ccda = i.Ccda;
|
|
r.Ccddd=i.Ccddd;
|
|
r.Datdoc= i.Datdoc;
|
|
r.Dcda=i.Dcda;
|
|
r.Descb=i.Descb;
|
|
r.Nbanc = i.Nbanc;
|
|
r.Ndoc = i.Ndoc;
|
|
r.Pibansca = i.Pibansca;
|
|
r.Pinotban = i.Pinotban;
|
|
r.Pinotimp = i.Pinotimp;
|
|
r.Tipob1 = i.Tipob1;
|
|
r.Tipob2 = i.Tipob2;
|
|
r.Tipob =i.Tipob;
|
|
r.Sdoc = i.Sdoc;
|
|
r.Serial = i.Serial;
|
|
r.Tcda=i.Tcda;
|
|
r.Tca = i.Tca;
|
|
r.Cca = i.Cca;
|
|
r.Dca = i.Dca;
|
|
r.Piconseg = i.Piconseg;
|
|
r.Pimezzo = i.Pimezzo;
|
|
r.Piimport= i.Piimport;
|
|
|
|
|
|
|
|
return r;
|
|
}
|
|
private Consegna_out fillConsegna_out(Consegna i, string token)
|
|
{
|
|
Consegna_out r = new Consegna_out();
|
|
|
|
string usr = getClaimValueByToken(token, "codice");
|
|
|
|
r.Piautist = usr;
|
|
r.Cprownum = i.Cprownum;
|
|
r.Ccddd = i.Ccddd;
|
|
r.cpccchk = i.cpccchk;
|
|
r.Ccda = i.Ccda;
|
|
r.Dcda = i.Dcda;
|
|
r.Pinotban = i.Pinotban;
|
|
r.Ccadd = i.Ccadd;
|
|
r.Ccda = i.Ccda;
|
|
r.Ccddd = i.Ccddd;
|
|
r.Datdoc = i.Datdoc;
|
|
r.Dcda = i.Dcda;
|
|
r.Descb = i.Descb;
|
|
r.Nbanc = i.Nbanc;
|
|
r.Ndoc = i.Ndoc;
|
|
r.Pibansca = i.Pibansca;
|
|
r.Pinotban = i.Pinotban;
|
|
r.Pinotimp = i.Pinotimp;
|
|
r.Tipob1 = i.Tipob1;
|
|
r.Tipob2 = i.Tipob2;
|
|
r.Tipob = i.Tipob;
|
|
r.Sdoc = i.Sdoc;
|
|
r.Serial = i.Serial;
|
|
r.Tcda = i.Tcda;
|
|
r.Tca = i.Tca;
|
|
r.Cca = i.Cca;
|
|
r.Dca = i.Dca;
|
|
r.Piconseg = i.Piconseg;
|
|
r.Pimezzo = i.Pimezzo;
|
|
r.Piimport = i.Piimport;
|
|
|
|
|
|
|
|
return r;
|
|
}
|
|
}
|
|
|
|
}
|