using AppData; using System; using System.Collections.Generic; using System.Data; using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Web; using System.Web.UI; namespace NKC_WF.WebUserControls { public partial class cmp_DailyStatsList : BaseUserControl { #region Protected Fields protected string dateFormat = "yyyy-MM-dd"; #endregion Protected Fields #region Private Properties private string fullPath { get { string dirPath = HttpContext.Current.Server.MapPath("~/temp/"); return $"{dirPath}\\{FileName}"; } } #endregion Private Properties #region Protected Properties protected DateTime DateEnd { get { DateTime answ = DateTime.Today.AddDays(1); DateTime.TryParseExact(txtDateEnd.Text, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out answ); return answ; } set { txtDateEnd.Text = $"{value:yyyy-MM-dd}"; } } protected DateTime DateStart { get { DateTime answ = DateTime.Today.AddDays(-7); DateTime.TryParseExact(txtDateStart.Text, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out answ); return answ; } set { txtDateStart.Text = $"{value:yyyy-MM-dd}"; } } protected bool fileExist { get { return File.Exists(fullPath); } } #endregion Protected Properties #region Public Properties public string FileName { get { return hfFileName.Value; } set { hfFileName.Value = value; } } #endregion Public Properties #region Private Methods private void clearFile() { if (fileExist) { File.Delete(fullPath); } } private void Cmp_numRow_eh_doRefresh(object sender, EventArgs e) { // recupero num righe ed aggiorno... grView.PageSize = cmp_numRow.numRow; grView.DataBind(); } private void updateGraph() { string PlaceCod = ddlMachine.SelectedValue != "*" ? ddlMachine.SelectedValue : "[ALL]"; cmp_DailyStatsPlot.PlaceCod = PlaceCod; cmp_DailyStatsPlot.PlotType = ddlType.SelectedValue; cmp_DailyStatsPlot.Legend = traduci($"rep_{ddlType.SelectedValue}"); cmp_DailyStatsPlot.DateStart = DateStart; cmp_DailyStatsPlot.DateEnd = DateEnd; } #endregion Private Methods #region Protected Methods protected void chkPlotGraph_CheckedChanged(object sender, EventArgs e) { doUpdate(); } protected void ddlMachine_SelectedIndexChanged(object sender, EventArgs e) { clearFile(); doUpdate(); } protected void ddlPlaces_SelectedIndexChanged(object sender, EventArgs e) { } protected void ddlType_SelectedIndexChanged(object sender, EventArgs e) { doUpdate(); } protected void grView_PageIndexChanged(object sender, EventArgs e) { } protected void lbtExportCsv_Click(object sender, EventArgs e) { DataLayer DLMan = new DataLayer(); string PlaceCod = ddlMachine.SelectedValue; DS_App.ProductionStatsDayDataTable currRecords = DLMan.taDayStats.getFilt(DateStart, DateEnd, PlaceCod); var rowList = currRecords .Select(x => new DayStats() { DTime = x.DataRif, Machine = x.PlaceCod, AvailTime = (double)x.MinDisp / 60, RunTime = (double)x.MinRun / 60, PartProd = x.ItmProd, PartScrap = x.ItmScrap }) .ToList(); utils.SaveToCsv(rowList, fullPath); hlDownload.NavigateUrl = $"~/temp/{FileName}"; lbtExportCsv.Visible = !fileExist; hlDownload.Visible = fileExist; } /// /// Caricamento pagina /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { cmp_numRow.numRow = 25; grView.PageSize = cmp_numRow.numRow; divGraph.Visible = false; divType.Visible = false; DateEnd = DateTime.Today.AddDays(1); DateStart = DateEnd.AddDays(-10); FileName = "DailyStats.csv"; clearFile(); lbtExportCsv.Visible = !fileExist; hlDownload.Visible = fileExist; } cmp_numRow.eh_doRefresh += Cmp_numRow_eh_doRefresh; } protected void txtDateEnd_TextChanged(object sender, EventArgs e) { clearFile(); doUpdate(); } protected void txtDateStart_TextChanged(object sender, EventArgs e) { clearFile(); doUpdate(); } protected void txtNumShow_TextChanged(object sender, EventArgs e) { doUpdate(); } #endregion Protected Methods #region Public Methods public void doUpdate() { divGraph.Visible = chkPlotGraph.Checked; divType.Visible = chkPlotGraph.Checked; if (chkPlotGraph.Checked) { updateGraph(); } grView.DataBind(); lbtExportCsv.Visible = !fileExist; hlDownload.Visible = fileExist; } public double ratioCalc(object _num, object _den) { double answ = 0; double num = 0; double den = 1; double.TryParse($"{_num}", out num); double.TryParse($"{_den}", out den); den = den != 0 ? den : 1; try { answ = (num / den); } catch { } return answ; } public string ratioFix(object _num, object _den) { string answ = ""; double num = 0; double den = 1; double.TryParse($"{_num}", out num); double.TryParse($"{_den}", out den); den = den != 0 ? den : 1; try { answ = $"{(num / den):P2}"; } catch { } return answ; } #endregion Public Methods } }