491 lines
19 KiB
C#
491 lines
19 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.Runtime.InteropServices;
|
|
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_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>? _giri;
|
|
private DbSet<GiriConsegna>? _giriCons;
|
|
private DbSet<GiriConsegnaView>? _giriConsView;
|
|
private DbSet<Destinazioni>? _destinazioni;
|
|
private DbSet<Consegna>? _consegne;
|
|
private DbSet<Sbr_ord>? _sbrord;
|
|
public GiriController(ILogger<LoginController> logger, IConfiguration? configuration, GESA_GIRI_DbContext giri_context, GESA_DESTINAZIONI_DbContext destinazioni_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<ActionResult<IEnumerable<GiriConsegnaView>>> listaGiri(string? autista, DateTime? data, bool aperto=true)
|
|
{
|
|
|
|
List<GiriConsegnaView> lst = new List<GiriConsegnaView>();
|
|
_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;
|
|
}
|
|
/// <summary>Salva i dati della consegna</summary>
|
|
[HttpPost]
|
|
[Route("addGiro")]
|
|
public async Task<ActionResult<GiriConsegna_out>> 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);
|
|
}
|
|
}
|
|
|
|
|
|
[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();
|
|
string _aut= !string.IsNullOrEmpty(d.Autista) ? d.Autista.Trim() : string.Empty;
|
|
o.Autista = _aut;
|
|
|
|
string _autCod = !string.IsNullOrEmpty(d.CodAutista) ? d.CodAutista.Trim() : string.Empty;
|
|
o.CodAutista = _autCod;
|
|
|
|
string _automezzo = !string.IsNullOrEmpty(d.DescAutomezzo) ? d.DescAutomezzo.Trim() : string.Empty;
|
|
o.DescAutomezzo = _automezzo;
|
|
|
|
string _automezzoCod = !string.IsNullOrEmpty(d.CodAutomezzo) ? d.CodAutomezzo.Trim() : string.Empty;
|
|
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.Cprownum=d.Cprownum;
|
|
|
|
string _sedeInd = !string.IsNullOrEmpty(d.IndirizzoSede) ? d.IndirizzoSede.Trim() : string.Empty;
|
|
o.IndirizzoSede = _sedeInd;
|
|
|
|
string _sede = !string.IsNullOrEmpty(d.Sede) ? d.Sede.Trim() : string.Empty;
|
|
o.Sede = _sede;
|
|
o.CodCommittente = d.CodCommittente;
|
|
|
|
string _committente = !string.IsNullOrEmpty(d.Committente) ? d.Committente.Trim() : string.Empty;
|
|
o.Committente = _committente;
|
|
|
|
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();
|
|
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;
|
|
cf.consFattaFlagCons=d.consFattaFlagCons;
|
|
|
|
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>> destinazione_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);
|
|
}
|
|
}
|
|
|
|
/// <summary>Salva i dati della consegna</summary>
|
|
[HttpPost]
|
|
[Route("destinazione/non_effettuata")]
|
|
public async Task<ActionResult<Consegna_out>> destinazione_non_effettuata([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 = fillConsegnaNonEffettuata(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.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= DateTime.Now;
|
|
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.Tcda = "S";
|
|
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 fillConsegnaNonEffettuata(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 = DateTime.Now;
|
|
r.Dcda = i.Dcda;
|
|
r.Descb = "Consegna non effetuata";
|
|
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.Tcda = "N";
|
|
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.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 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;
|
|
}
|
|
}
|
|
|
|
}
|