Files
2025-05-29 08:58:28 +02:00

751 lines
23 KiB
C#

using MapoDb;
using Newtonsoft.Json;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Web.UI;
namespace MP_ADM.WebUserControls
{
public partial class mod_barcode : BaseUserControl
{
#region Protected Fields
/// <summary>
/// dictionary comandi ammessi
/// </summary>
protected Dictionary<string, string> _comandi = new Dictionary<string, string>();
/// <summary>
/// dictionary dei valori ammessi
/// </summary>
protected Dictionary<string, string> _tabValori = new Dictionary<string, string>();
/// <summary>
/// RegExp x Find --> replace (se vettore è | delimited)
/// </summary>
protected string regExp_Find = memLayer.ML.cdv("regExp_Find");
/// <summary>
/// RegExp x SAVE KIT
/// </summary>
protected string regExp_KitSave = memLayer.ML.cdv("regExp_KitSave");
/// <summary>
/// RegExp x START KIT
/// </summary>
protected string regExp_KitStart = memLayer.ML.cdv("regExp_KitStart");
/// <summary>
/// RegExp x RESET / CANCEL
/// </summary>
protected string regExp_KO = memLayer.ML.cdv("regExp_KO");
/// <summary>
/// RegExp x CONFERMA
/// </summary>
protected string regExp_OK = memLayer.ML.cdv("regExp_OK");
/// <summary>
/// RegExp x Replace da Find (se vettore è | delimited)
/// </summary>
protected string regExp_Replace = memLayer.ML.cdv("regExp_Replace");
#endregion Protected Fields
#region Public Events
/// <summary>
/// evento comando registrato
/// </summary>
public event EventHandler eh_comandoRegistrato;
/// <summary>
/// evento lettura dati (grezza) effettuata
/// </summary>
public event EventHandler eh_dataRead;
#endregion Public Events
#region Protected Properties
/// <summary>
/// oggetto comando locale alla classe
/// </summary>
protected inputComando comando
{
get
{
inputComando answ;
if (memLayer.ML.isInSessionObject("barcodeCmd"))
{
answ = (inputComando)memLayer.ML.objSessionObj("barcodeCmd");
}
else
{
answ = new inputComando();
}
return answ;
}
set
{
memLayer.ML.setSessionVal("barcodeCmd", value, false);
}
}
#endregion Protected Properties
#region Public Properties
public string BCodeVal
{
get
{
return txtInput.Text.Trim().ToUpper();
}
set
{
txtInput.Text = value;
}
}
/// <summary>
/// Verifica se il cod ordine è OK:
/// coerenza tra ordine e iniziale ordine (OPR/KIT)
/// </summary>
public bool checkOrdOk
{
get
{
return codOrd.StartsWith(codOrdPre);
}
}
public string codArt
{
get
{
return memLayer.ML.StringSessionObj(string.Format("codArt_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("codArt_{0}", uid), value);
}
}
public string codGruppo
{
get
{
return memLayer.ML.StringSessionObj(string.Format("codGruppo_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("codGruppo_{0}", uid), value);
}
}
public string codOrd
{
get
{
return memLayer.ML.StringSessionObj(string.Format("codOrd_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("codOrd_{0}", uid), value);
}
}
/// <summary>
/// Valore validazione codici ORDINE come STARTING (es: OPR,KIT)
/// </summary>
public string codOrdPre
{
get
{
return memLayer.ML.StringSessionObj(string.Format("codOrdPre_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("codOrdPre_{0}", uid), value);
}
}
/// <summary>
/// elenco dei comandi riconosciuti
/// </summary>
public Dictionary<string, string> comandiAmmessi
{
get
{
return _comandi;
}
set
{
_comandi = value;
}
}
/// <summary>
/// comando registrato dal barcode
/// </summary>
public inputComando comandoRegistrato
{
get
{
return comando;
}
}
public string descArt
{
get
{
return memLayer.ML.StringSessionObj(string.Format("descArt_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("descArt_{0}", uid), value);
}
}
public string idxMacc
{
get
{
return memLayer.ML.StringSessionObj(string.Format("idxMacc_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("idxMacc_{0}", uid), value);
}
}
public string kitCode
{
get
{
return memLayer.ML.StringSessionObj(string.Format("kitCode_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("kitCode_{0}", uid), value);
}
}
public string nomeMacc
{
get
{
return memLayer.ML.StringSessionObj(string.Format("nomeMacc_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("nomeMacc_{0}", uid), value);
}
}
public int qta
{
get
{
return memLayer.ML.IntSessionObj(string.Format("qta_{0}", uid));
}
set
{
memLayer.ML.setSessionVal(string.Format("qta_{0}", uid), value);
}
}
/// <summary>
/// oggetto comando locale alla classe
/// </summary>
public string rawInput
{
get
{
string answ;
if (memLayer.ML.isInSessionObject("barcodeRaw"))
{
answ = memLayer.ML.StringSessionObj("barcodeRaw");
}
else
{
answ = "";
}
return answ;
}
set
{
memLayer.ML.setSessionVal("barcodeRaw", value, false);
}
}
/// <summary>
/// tabella di valori ammissibili
/// </summary>
public Dictionary<string, string> tabValori
{
get
{
return _tabValori;
}
set
{
_tabValori = value;
}
}
/// <summary>
/// scrive nella label input
/// </summary>
public string txtInput2show
{
set
{
lblInput.Text = value;
}
}
/// <summary>
/// scrive nella label richiesta
/// </summary>
public string txtRich2show
{
set
{
lblRichiesta.Text = value;
}
}
/// <summary>
/// scrive nella label valore
/// </summary>
public string txtVal2show
{
set
{
lblValore.Text = value;
}
}
#endregion Public Properties
#region Private Methods
private bool checkComandi()
{
bool answ = false;
// controllo eventuali comandi CUSTOM
if (BCodeVal == regExp_KO)
{
comando.isValid = true;
answ = true;
resetData();
}
else if (BCodeVal == regExp_KitStart)
{
comando.isValid = true;
answ = true;
resetData();
}
else if (BCodeVal == regExp_KitSave)
{
comando.isValid = true;
answ = true;
resetData();
}
else if (BCodeVal == regExp_OK)
{
// comando valido SE ho ordine coerente...
if (checkOrdOk)
{
comando.isValid = true;
// recupero il codGruppo da macchina... (primo)
if (codGruppo == "")
{
var tabAG = DataLayerObj.taAG.getFaseByIdxMacc(idxMacc);
if (tabAG.Rows.Count > 0)
{
codGruppo = tabAG[0].CodGruppo;
}
}
// controllo se ho tutti i dati necessari...
if (codOrd != "" && codArt != "" && codGruppo != "" && idxMacc != "" && qta > 0)
{
// setup parametri
bool trovato = false;
decimal TCiclo = 0;
int pzPallet = 1;
DS_ProdTempi.ODLDataTable tabODL = new DS_ProdTempi.ODLDataTable();
DS_ProdTempi.PromesseODLDataTable tabPODL = new DS_ProdTempi.PromesseODLDataTable();
if (memLayer.ML.cdvb("mpAdm_TcFromPODL"))
{
// controllo se richiesto ricerca preliminare x articolo + macchina
if (memLayer.ML.cdvb("mpAdm_TcByMachine"))
{
// recupero TC promessa da ultimo per articolo/macchina
tabPODL = DataLayerObj.taPODL.getByMaccArt(idxMacc, codArt, "", false);
}
// se non lo trovo (o non ho cercato...) prendo da SOLO ARTICOLO
if (tabPODL.Rows.Count == 0)
{
tabPODL = DataLayerObj.taPODL.getByMaccArt("", codArt, "", false);
}
// recupero TCiclo
if (tabPODL.Rows.Count > 0)
{
TCiclo = tabPODL[0].TCAssegnato;
pzPallet = tabPODL[0].PzPallet;
trovato = true;
}
}
else
{
// controllos e richiesto ricerca preliminare x articolo + macchina
if (memLayer.ML.cdvb("mpAdm_TcByMachine"))
{
// recupero TC promessa da ultimo per articolo/macchina
tabODL = DataLayerObj.taODL.getByMacchinaArticolo(codArt, idxMacc);
}
// se non lo trovo (o non ho cercato...) prendo da SOLO ARTICOLO
if (tabODL.Rows.Count == 0)
{
tabODL = DataLayerObj.taODL.getByMacchinaArticolo(codArt, "");
}
// recupero TCiclo
if (tabODL.Rows.Count > 0)
{
TCiclo = tabODL[0].TCAssegnato;
pzPallet = tabODL[0].PzPallet;
trovato = true;
}
}
// se non lo trovo è valore default da config DB (oppure 59...)
if (!trovato)
{
TCiclo = memLayer.ML.CRI("TCicloStd");
}
if (TCiclo == 0)
{
TCiclo = 59;
}
// creo nuova PROMESSA ODL...
DataLayerObj.taPODL.insertQuery(codOrd, codOrd, true, codArt, codGruppo, idxMacc, qta, TCiclo, DateTime.Now, 1, pzPallet, descArt);
}
}
// resetto ricaricando
Response.Redirect(titolo);
}
return answ;
}
private bool checkIntServ()
{
bool answ = false;
// IN PRIMIS se c'è in config preprocesso BCode x Find-Replace
string BCodeFilt = BCodeVal;
if (regExp_Find != "")
{
// splitto i vettori
var vettFind = regExp_Find.Split('|');
var vettRepl = regExp_Replace.Split('|');
// se ho tanti find quanti replace procedo...
if (vettFind.Length == vettRepl.Length)
{
for (int i = 0; i < vettFind.Length; i++)
{
BCodeFilt = BCodeFilt.Replace(vettFind[i], vettRepl[i]);
}
}
else
{
logger.lg.scriviLog(string.Format("Attenzione: problema in decodifica barcode find/replace, i parametri non corrispondono una volta esplosi i vettori:{0}find: {1}{0}replace: {2}", Environment.NewLine, regExp_Find, regExp_Replace));
}
}
// cerco esplicitamente sul DB IntegrationServices...
var risultato = DataLayerObj.taIS_TrDati.getOrdini(BCodeFilt);
if (risultato.Rows.Count > 0)
{
var rRes = risultato[0];
// verifico da config COME decodificare IS...
string BCodeIS_DType = memLayer.ML.cdv("BCodeIS_DType");
// verifico corrispondano i formati...
if (rRes.DataType == BCodeIS_DType)
{
// cerco i dati e decodifico... togliendo eventuale VETTORE "[..]"
var jsonData = risultato[0].ValueOUT.Replace("[", "").Replace("]", "");
try
{
if (BCodeIS_DType == "Colcom_Order")
{
// a seconda del tipo decodifico...
ISTD_OrderColcom currVal = JsonConvert.DeserializeObject<ISTD_OrderColcom>(jsonData);
comando.isValid = true;
answ = true;
// salvo ordine / articolo / qta
codOrd = currVal.CodOrdine;
codArt = currVal.CodArticolo;
descArt = currVal.DescrArticolo;
kitCode = currVal.KitCode;
qta = (int)currVal.Qta;
comando.valore = jsonData;// string.Format("{0}#{1}#{2:N0}", codOrd, codArt, qta);
comando.currCmdIn = "OrdArtQta";
}
}
catch (Exception exc)
{
logger.lg.scriviLog(string.Format("Errore in decodifica obj JSON:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
}
}
}
return answ;
}
private bool checkMacc()
{
bool answ = false;
// verifico se il barcode è di una macchina...
var risultato = DataLayerObj.taMacchine.GetByIdx(BCodeVal);
if (risultato.Rows.Count > 0)
{
idxMacc = BCodeVal;
comando.valore = BCodeVal;
comando.isValid = true;
answ = true;
// recupero altri dati
var rRes = risultato[0];
nomeMacc = rRes.Descrizione;
// imposto il gruppo cercandolo...
var gruppi = DataLayerObj.taAG.getByIdxMacc(BCodeVal);
if (gruppi.Rows.Count > 0)
{
// salvo primo...
codGruppo = gruppi[0].CodGruppo;
}
}
return answ;
}
/// <summary>
/// verifica parametri in sessione x i valori eventualmente da mettere al comando...
/// </summary>
private void checkParam()
{
// ho macchina ed articolo?
bool hasAllData = (idxMacc != "" && codArt != "");
string valOut = "";
if (codArt != "")
{
valOut += string.Format("<b>{0}</b><br/>Art: {1} | qta: {2:N0} | kit: {3}<br/>{4}", codOrd, codArt, qta, kitCode, descArt);
}
if (idxMacc != "")
{
valOut += string.Format("<br>{0} | {1}", idxMacc, nomeMacc);
}
// imposto...
comando.valoreTrad = valOut;
// controllo se ho tutto...
comando.descrComando = "";
if (idxMacc == "")
{
comando.descrComando += " - Manca Impianto - ";
}
if (codArt == "" || codOrd == "")
{
comando.descrComando += " - Manca Ordine Produzione - ";
}
if (!checkOrdOk)
{
comando.descrComando += " - Tipo Ordine errato - ";
}
if (hasAllData && checkOrdOk)
{
comando.descrComando = "Confermare caricamento";
}
}
/// <summary>
/// verifica quale browser usato e applica css corretto al div attorno al box
/// </summary>
private void DetectAgent()
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
if (browser.Browser == "IE")
{
pnlBarcodeBox.CssClass = "barcodeBoxIE";
}
else
{
pnlBarcodeBox.CssClass = "barcodeBoxOther";
}
}
/// <summary>
/// verifica se il comando inserito sia valido
/// </summary>
private void isInputEvent()
{
if (_comandi.ContainsKey(BCodeVal)) // verifico se il comando digitato esista...
{
comando.isValid = true;
// salvo comando precedente (se c'è...)
comando.prevCmdIn = comando.currCmdIn;
comando.descrComandoPrev = comando.descrComando;
comando.currCmdIn = BCodeVal;
_comandi.TryGetValue(BCodeVal, out comando.descrComando);
}
}
/// <summary>
/// verifico se sia un valore compreso nell'elenco fornito
/// </summary>
private void isValore()
{
if (_tabValori.ContainsKey(BCodeVal)) // verifico se il comando digitato esista...
{
comando.isValid = true;
comando.valore = BCodeVal;
_tabValori.TryGetValue(BCodeVal, out comando.valoreTrad);
}
}
/// <summary>
/// inizializzazione specifica barcode
/// </summary>
private void myInitialize()
{
if (!Page.IsPostBack)
{
lblInput.Text = traduci("PregoInserireBarcode");
comando = new inputComando();
}
}
private void resetData()
{
// resetto
idxMacc = "";
nomeMacc = "";
codArt = "";
descArt = "";
codGruppo = "";
codOrd = "";
qta = 0;
}
#endregion Private Methods
#region Protected Methods
/// <summary>
/// al caricamento della pagina...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
resetData();
}
DetectAgent();
myInitialize();
}
/// <summary>
/// barcode completato con invio...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void txtInput_TextChanged(object sender, EventArgs e)
{
processInput();
}
#endregion Protected Methods
#region Public Methods
/// <summary>
/// effettua la lettura da sessione del comando cliccato e lo inserisce come fosse barcode
/// </summary>
public void loadBtnClickComando()
{
BCodeVal = memLayer.ML.StringSessionObj("btnCmdPress");
memLayer.ML.emptySessionVal("btnCmdPress");
processInput();
}
public void processInput()
{
comando.isValid = false;
bool gotIt = false;
gotIt = checkComandi();
if (!gotIt)
{
gotIt = checkMacc();
}
if (!gotIt)
{
gotIt = checkIntServ();
}
// ora check globale...
checkParam();
// verifico se c'è stato input evento
if (comando.isValid)
{
if (comando.currCmdIn != "" || comando.descrComando != "")
{
txtInput2show = comando.descrComando;
}
else
{
txtInput2show = "---";
}
if (comando.valore != "")
{
txtVal2show = comando.valoreTrad;
}
else
{
txtVal2show = "---";
}
if (eh_comandoRegistrato != null)
{
eh_comandoRegistrato(this, new EventArgs());
}
}
else
{
lblInput.Text = traduci("ComandoSconosciuto");
lblValore.Text = BCodeVal;
comando = new inputComando();
rawInput = BCodeVal;
if (eh_dataRead != null)
{
eh_dataRead(this, new EventArgs());
}
}
BCodeVal = "";
}
/// <summary>
/// reset del controllo
/// </summary>
public void resetMe()
{
comando = new inputComando();
txtInput2show = "Prego inserire barcode";
txtVal2show = "";
txtRich2show = "";
}
#endregion Public Methods
}
}