using System; using System.Collections.Generic; using System.Linq; using Comal.Classes; using InABox.Core; namespace Comal.Stores { public class ManufacturingHistoryStore : BaseStore { //private void ProcessHistory(params ManufacturingHistory[] entities) //{ // TimeSpan start = TimeSpan.FromMinutes(Math.Floor(entity.Created.TimeOfDay.Subtract(entity.Window).TotalMinutes / 15.0F) * 15.0F); // TimeSpan finish = start.Add(new TimeSpan(0, 15, 0)); //} protected override void BeforeSave(ManufacturingHistory entity) { base.BeforeSave(entity); var start = TimeSpan.FromMinutes(Math.Floor(entity.Created.TimeOfDay.Subtract(entity.Window).TotalMinutes / 15.0F) * 15.0F); var finish = start.Add(new TimeSpan(0, 15, 0)); var sDescription = entity.LostTime.IsValid() ? entity.Description : "Manufacturing Time"; var ass = Provider.Query( new Filter(x => x.EmployeeLink.ID).IsEqualTo(entity.Employee.ID) .And(x => x.Date).IsEqualTo(entity.Date) .And(x => x.Actual.Finish).IsGreaterThanOrEqualTo(start) .And(x => x.Actual.Finish).IsLessThanOrEqualTo(finish) //.And(x=>x.Description).BeginsWith(sDescription) .And(x => x.ActivityLink.ID).IsEqualTo(entity.Activity.ID), new Columns( x => x.ID, x => x.Description, x => x.Actual.Finish ), new SortOrder(x => x.Actual.Finish, SortDirection.Descending) ).Rows.Where(r => r.Get(c => c.Description).StartsWith(sDescription)).FirstOrDefault()?.ToObject(); if (ass == null) { ass = new Assignment { Date = entity.Date }; ass.EmployeeLink.ID = entity.Employee.ID; ass.Actual.Start = start; ass.Description = sDescription; ass.ActivityLink.ID = entity.Activity.ID; } if (!entity.LostTime.IsValid()) { var bFound = false; var summary = new List(); var lines = ass.Description.Split('\n').ToList(); foreach (var line in lines) if (line.Length > 8) { // Assumes a line format for [Serial] : HH:mm var ser = line.Substring(0, line.Length - 8); if (ser.Equals(entity.Description)) { bFound = true; var sTime = line.Substring(line.Length - 5, 5); var tTime = TimeSpan.Parse(sTime) + entity.QADuration + entity.WorkDuration; summary.Add(string.Format("{0} : {1}", entity.Description, tTime.ToString(@"hh\:mm"))); } else { summary.Add(line); } } else { summary.Add(line); } if (!bFound) summary.Add(string.Format("{0} : {1}", entity.Description, (entity.QADuration + entity.WorkDuration).ToString(@"hh\:mm"))); ass.Description = string.Join("\n", summary); } ass.Actual.Finish = finish; if (ass.IsChanged()) Provider.Save(ass); var row = Provider.Query( new Filter(x => x.Employee.ID).IsEqualTo(entity.Employee.ID) .And(x => x.Packet.ID).IsEqualTo(entity.LostTime.ID) .And(x => x.Packet.ID).IsEqualTo(entity.Packet.ID) .And(x => x.Section.ID).IsEqualTo(entity.Section.ID) .And(x => x.Station).IsEqualTo(entity.Station) .And(x => x.Date).IsEqualTo(entity.Date), new Columns( x => x.ID, x => x.QADuration, x => x.QACompleted, x => x.WorkDuration, x => x.WorkCompleted, x => x.Breakdown ) ).Rows.FirstOrDefault(); if (row != null) { var history = row.ToObject(); history.QADuration += entity.QADuration; history.QACompleted += entity.QACompleted; history.WorkDuration += entity.WorkDuration; history.WorkCompleted += entity.WorkCompleted; UpdateBreakdown(history, entity.Created, entity.QADuration + entity.WorkDuration); Provider.Save(history); } else { UpdateBreakdown(entity, entity.Created, entity.QADuration + entity.WorkDuration); Provider.Save(entity); } } private void UpdateBreakdown(ManufacturingHistory entity, DateTime date, TimeSpan timeSpan) { var breakdown = new Dictionary(); if (!string.IsNullOrEmpty(entity.Breakdown)) breakdown = Serialization.Deserialize>(entity.Breakdown); var slot = TimeSpan.FromMinutes(Math.Ceiling((date - date.Date).TotalMinutes / 15.0F) * 15.0F); var key = slot.ToString(@"hh\:mm"); if (breakdown.ContainsKey(key)) breakdown[key] = breakdown[key] + timeSpan; else breakdown[key] = timeSpan; entity.Breakdown = Serialization.Serialize(breakdown); } protected override void OnSave(ManufacturingHistory entity, ref string auditnote) { // Consolidated Saving happend in BeforeSave - nothing to see here //base.OnSave(entity); } protected override void OnSave(IEnumerable entities, ref string auditnote) { //base.OnSave(entities); } } }