2023-10-27 14:37:15 -07:00

153 lines
5.0 KiB
C#

using ReportingServices.Shared.Models.ProductionReport;
namespace ReportingServices.Shared.ViewModels.ProductionReport;
public class YieldStatistics
{
public DateTime StartDate { get; set; }
public List<ReactorOutsByDay> OutsByDay { get; set; }
public List<ScrapByDay> ScrapByDay { get; set; }
public OutsAndScrapTotal QTDOutsAndScrap { get; set; }
public int DailyPlanWafers { get; set; }
public int DailyWafersForQTR { get; set; }
public bool IsCurrentWeek { get; set; }
public YieldStatistics(DateTime startDate, bool isCurrentWeek)
{
StartDate = startDate;
IsCurrentWeek = isCurrentWeek;
}
public void SetYieldInformation(YieldInformation yieldInformation, QuarterlyTargets targets, DateTime qtrStartDate, int yieldedOuts)
{
OutsByDay = GetReactorOutsByDay(yieldInformation.Outs);
ScrapByDay = GetReactorScrapByDay(yieldInformation.Scrap);
DailyPlanWafers = targets.Yield_Outs / targets.PlanWorkingDays;
int daysRemainingInQtr = (int)(qtrStartDate.AddDays(targets.PlanWorkingDays) - StartDate).TotalDays;
if (daysRemainingInQtr == targets.PlanWorkingDays)
DailyWafersForQTR = DailyPlanWafers;
else
DailyWafersForQTR = (targets.Yield_Outs - yieldedOuts) / daysRemainingInQtr;
}
public static List<string> GetDistinctDatesFromReactorOuts(List<ReactorOutsByRDS> outs)
{
List<string> dates = new();
foreach (ReactorOutsByRDS rout in outs)
{
if (!dates.Contains(DateTime.Parse(rout.EndProcessTime).Date.ToString()))
dates.Add(DateTime.Parse(rout.EndProcessTime).Date.ToString());
}
List<DateTime> dateTimes = new List<DateTime>();
foreach (string date in dates)
{
dateTimes.Add(DateTime.Parse(date));
}
dateTimes.Sort();
dates.Sort();
DateTime currentDateTime = dateTimes.ElementAt(0);
for (int i = 1; i < dateTimes.Count; i++)
{
DateTime nextDateTime = dateTimes[i];
int dayDiff = (nextDateTime - currentDateTime).Days;
if (dayDiff > 1)
{
dateTimes.Insert(i, currentDateTime.AddDays(1));
dates.Insert(i, dateTimes.ElementAt(i).ToString("MM/dd/yyyy hh:mm:ss tt"));
}
currentDateTime = dateTimes[i];
}
return dates;
}
public static List<ReactorOutsByDay> GetReactorOutsByDay(List<ReactorOutsByRDS> outs)
{
List<ReactorOutsByDay> outsByDay = new();
List<string> dates = GetDistinctDatesFromReactorOuts(outs);
foreach (string date in dates)
{
int waferCount = 0;
foreach (ReactorOutsByRDS rout in outs)
{
if (DateTime.Parse(rout.EndProcessTime).Date.ToString() == date && !string.IsNullOrEmpty(rout.Units))
waferCount += (int)float.Parse(rout.Units);
}
outsByDay.Add(new ReactorOutsByDay { StartDate = date, TotalWafers = waferCount });
}
return outsByDay;
}
public static List<string> GetDistinctDatesFromReactorScrap(List<ScrapByDay> scraps)
{
List<string> dates = new();
foreach (ScrapByDay scrap in scraps)
{
if (!dates.Contains(DateTime.Parse(scrap.StartDate).Date.ToString()))
dates.Add(DateTime.Parse(scrap.StartDate).Date.ToString());
}
List<DateTime> dateTimes = new List<DateTime>();
foreach (string date in dates)
{
dateTimes.Add(DateTime.Parse(date));
}
dateTimes.Sort();
dates.Sort();
DateTime currentDateTime = dateTimes.ElementAt(0);
for (int i = 1; i < dateTimes.Count; i++)
{
DateTime nextDateTime = dateTimes[i];
int dayDiff = (nextDateTime - currentDateTime).Days;
if (dayDiff > 1)
{
dateTimes.Insert(i, currentDateTime.AddDays(1));
dates.Insert(i, dateTimes.ElementAt(i).ToString("MM/dd/yyyy hh:mm:ss tt"));
}
currentDateTime = dateTimes[i];
}
return dates;
}
public static List<ScrapByDay> GetReactorScrapByDay(List<ScrapByDay> scrap)
{
List<ScrapByDay> sortedScrap = scrap.OrderBy(s => s.StartDate).ToList();
List<ScrapByDay> scrapByDay = new();
List<string> dates = GetDistinctDatesFromReactorScrap(scrap);
int scrapIdx = 0;
for (int i = 0; i < dates.Count; i++)
{
ScrapByDay currentScrap = sortedScrap.ElementAtOrDefault(scrapIdx);
string currentDate = dates[i];
if (currentScrap != null && currentScrap.StartDate == currentDate)
{
scrapByDay.Add(currentScrap);
scrapIdx++;
}
else
{
scrapByDay.Add(new ScrapByDay() { StartDate = dates[i] });
}
}
return scrapByDay;
}
}