ManufacturingHistoryStore.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. namespace Comal.Stores
  7. {
  8. public class ManufacturingHistoryStore : BaseStore<ManufacturingHistory>
  9. {
  10. //private void ProcessHistory(params ManufacturingHistory[] entities)
  11. //{
  12. // TimeSpan start = TimeSpan.FromMinutes(Math.Floor(entity.Created.TimeOfDay.Subtract(entity.Window).TotalMinutes / 15.0F) * 15.0F);
  13. // TimeSpan finish = start.Add(new TimeSpan(0, 15, 0));
  14. //}
  15. protected override void BeforeSave(ManufacturingHistory entity)
  16. {
  17. base.BeforeSave(entity);
  18. var start = TimeSpan.FromMinutes(Math.Floor(entity.Created.TimeOfDay.Subtract(entity.Window).TotalMinutes / 15.0F) * 15.0F);
  19. var finish = start.Add(new TimeSpan(0, 15, 0));
  20. var sDescription = entity.LostTime.IsValid() ? entity.Description : "Manufacturing Time";
  21. var ass = Provider.Query(
  22. new Filter<Assignment>(x => x.EmployeeLink.ID).IsEqualTo(entity.Employee.ID)
  23. .And(x => x.Date).IsEqualTo(entity.Date)
  24. .And(x => x.Actual.Finish).IsGreaterThanOrEqualTo(start)
  25. .And(x => x.Actual.Finish).IsLessThanOrEqualTo(finish)
  26. //.And(x=>x.Description).BeginsWith(sDescription)
  27. .And(x => x.ActivityLink.ID).IsEqualTo(entity.Activity.ID),
  28. new Columns<Assignment>(
  29. x => x.ID,
  30. x => x.Description,
  31. x => x.Actual.Finish
  32. ),
  33. new SortOrder<Assignment>(x => x.Actual.Finish, SortDirection.Descending)
  34. ).Rows.Where(r => r.Get<Assignment, string>(c => c.Description).StartsWith(sDescription)).FirstOrDefault()?.ToObject<Assignment>();
  35. if (ass == null)
  36. {
  37. ass = new Assignment { Date = entity.Date };
  38. ass.EmployeeLink.ID = entity.Employee.ID;
  39. ass.Actual.Start = start;
  40. ass.Description = sDescription;
  41. ass.ActivityLink.ID = entity.Activity.ID;
  42. }
  43. if (!entity.LostTime.IsValid())
  44. {
  45. var bFound = false;
  46. var summary = new List<string>();
  47. var lines = ass.Description.Split('\n').ToList();
  48. foreach (var line in lines)
  49. if (line.Length > 8)
  50. {
  51. // Assumes a line format for [Serial] : HH:mm
  52. var ser = line.Substring(0, line.Length - 8);
  53. if (ser.Equals(entity.Description))
  54. {
  55. bFound = true;
  56. var sTime = line.Substring(line.Length - 5, 5);
  57. var tTime = TimeSpan.Parse(sTime) + entity.QADuration + entity.WorkDuration;
  58. summary.Add(string.Format("{0} : {1}", entity.Description, tTime.ToString(@"hh\:mm")));
  59. }
  60. else
  61. {
  62. summary.Add(line);
  63. }
  64. }
  65. else
  66. {
  67. summary.Add(line);
  68. }
  69. if (!bFound)
  70. summary.Add(string.Format("{0} : {1}", entity.Description, (entity.QADuration + entity.WorkDuration).ToString(@"hh\:mm")));
  71. ass.Description = string.Join("\n", summary);
  72. }
  73. ass.Actual.Finish = finish;
  74. if (ass.IsChanged())
  75. Provider.Save(ass);
  76. var row = Provider.Query(
  77. new Filter<ManufacturingHistory>(x => x.Employee.ID).IsEqualTo(entity.Employee.ID)
  78. .And(x => x.Packet.ID).IsEqualTo(entity.LostTime.ID)
  79. .And(x => x.Packet.ID).IsEqualTo(entity.Packet.ID)
  80. .And(x => x.Section.ID).IsEqualTo(entity.Section.ID)
  81. .And(x => x.Station).IsEqualTo(entity.Station)
  82. .And(x => x.Date).IsEqualTo(entity.Date),
  83. new Columns<ManufacturingHistory>(
  84. x => x.ID,
  85. x => x.QADuration,
  86. x => x.QACompleted,
  87. x => x.WorkDuration,
  88. x => x.WorkCompleted,
  89. x => x.Breakdown
  90. )
  91. ).Rows.FirstOrDefault();
  92. if (row != null)
  93. {
  94. var history = row.ToObject<ManufacturingHistory>();
  95. history.QADuration += entity.QADuration;
  96. history.QACompleted += entity.QACompleted;
  97. history.WorkDuration += entity.WorkDuration;
  98. history.WorkCompleted += entity.WorkCompleted;
  99. UpdateBreakdown(history, entity.Created, entity.QADuration + entity.WorkDuration);
  100. Provider.Save(history);
  101. }
  102. else
  103. {
  104. UpdateBreakdown(entity, entity.Created, entity.QADuration + entity.WorkDuration);
  105. Provider.Save(entity);
  106. }
  107. }
  108. private void UpdateBreakdown(ManufacturingHistory entity, DateTime date, TimeSpan timeSpan)
  109. {
  110. var breakdown = new Dictionary<string, TimeSpan>();
  111. if (!string.IsNullOrEmpty(entity.Breakdown))
  112. breakdown = Serialization.Deserialize<Dictionary<string, TimeSpan>>(entity.Breakdown);
  113. var slot = TimeSpan.FromMinutes(Math.Ceiling((date - date.Date).TotalMinutes / 15.0F) * 15.0F);
  114. var key = slot.ToString(@"hh\:mm");
  115. if (breakdown.ContainsKey(key))
  116. breakdown[key] = breakdown[key] + timeSpan;
  117. else
  118. breakdown[key] = timeSpan;
  119. entity.Breakdown = Serialization.Serialize(breakdown);
  120. }
  121. protected override void OnSave(ManufacturingHistory entity, ref string auditnote)
  122. {
  123. // Consolidated Saving happend in BeforeSave - nothing to see here
  124. //base.OnSave(entity);
  125. }
  126. protected override void OnSave(IEnumerable<ManufacturingHistory> entities, ref string auditnote)
  127. {
  128. //base.OnSave(entities);
  129. }
  130. }
  131. }