123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores
- {
- public class ManufacturingHistoryStore : BaseStore<ManufacturingHistory>
- {
- //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<Assignment>(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<Assignment>(
- x => x.ID,
- x => x.Description,
- x => x.Actual.Finish
- ),
- new SortOrder<Assignment>(x => x.Actual.Finish, SortDirection.Descending)
- ).Rows.Where(r => r.Get<Assignment, string>(c => c.Description).StartsWith(sDescription)).FirstOrDefault()?.ToObject<Assignment>();
- 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<string>();
- 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<ManufacturingHistory>(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<ManufacturingHistory>(
- 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<ManufacturingHistory>();
- 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<string, TimeSpan>();
- if (!string.IsNullOrEmpty(entity.Breakdown))
- breakdown = Serialization.Deserialize<Dictionary<string, TimeSpan>>(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<ManufacturingHistory> entities, ref string auditnote)
- {
- //base.OnSave(entities);
- }
- }
- }
|