ManufacturingPacket.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using InABox.Core;
  6. namespace Comal.Classes
  7. {
  8. public class ManufacturingPacketArea : IFormula<ManufacturingPacket, object>
  9. {
  10. public Expression<Func<ManufacturingPacket, object>> Value => x => x.Height;
  11. public Expression<Func<ManufacturingPacket, object>>[] Modifiers =>
  12. new Expression<Func<ManufacturingPacket, object>>[] { x => x.Width, x => 0.000001m };
  13. public FormulaOperator Operator => FormulaOperator.Multiply;
  14. public FormulaType Type => FormulaType.Virtual;
  15. }
  16. public class ManufacturingPacketVolume : IFormula<ManufacturingPacket, object>
  17. {
  18. public Expression<Func<ManufacturingPacket, object>> Value => x => x.Height;
  19. public Expression<Func<ManufacturingPacket, object>>[] Modifiers => new Expression<Func<ManufacturingPacket, object>>[]
  20. { x => x.Width, x => x.Length, x => 0.000000001m };
  21. public FormulaOperator Operator => FormulaOperator.Multiply;
  22. public FormulaType Type => FormulaType.Virtual;
  23. }
  24. public class ManufacturingPacketTime : CoreAggregate<ManufacturingPacket, ManufacturingPacketStage, TimeSpan>
  25. {
  26. public override Expression<Func<ManufacturingPacketStage, TimeSpan>> Aggregate => x => x.Time;
  27. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  28. public override Dictionary<Expression<Func<ManufacturingPacketStage, object>>, Expression<Func<ManufacturingPacket, object>>> Links =>
  29. new Dictionary<Expression<Func<ManufacturingPacketStage, object>>, Expression<Func<ManufacturingPacket, object>>>()
  30. {
  31. { ManufacturingPacketStage => ManufacturingPacketStage.Parent.ID, ManufacturingPacket => ManufacturingPacket.ID }
  32. };
  33. }
  34. public class ManufacturingPacketTimeRemaining : CoreAggregate<ManufacturingPacket, ManufacturingPacketStage, TimeSpan>
  35. {
  36. public override Expression<Func<ManufacturingPacketStage, TimeSpan>> Aggregate => x => x.TimeRemaining;
  37. public Expression<Func<ManufacturingPacketStage, Guid>> Link => x => x.Parent.ID;
  38. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  39. public override Dictionary<Expression<Func<ManufacturingPacketStage, object>>, Expression<Func<ManufacturingPacket, object>>> Links =>
  40. new Dictionary<Expression<Func<ManufacturingPacketStage, object>>, Expression<Func<ManufacturingPacket, object>>>()
  41. {
  42. { ManufacturingPacketStage => ManufacturingPacketStage.Parent.ID, ManufacturingPacket => ManufacturingPacket.ID }
  43. };
  44. }
  45. public class ManufacturingPacketActualTime : CoreAggregate<ManufacturingPacket, ManufacturingHistory, TimeSpan>
  46. {
  47. public override Expression<Func<ManufacturingHistory, TimeSpan>> Aggregate => x => x.WorkDuration;
  48. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  49. public override Dictionary<Expression<Func<ManufacturingHistory, object>>, Expression<Func<ManufacturingPacket, object>>> Links =>
  50. new Dictionary<Expression<Func<ManufacturingHistory, object>>, Expression<Func<ManufacturingPacket, object>>>()
  51. {
  52. { ManufacturingHistory => ManufacturingHistory.Packet.ID, ManufacturingPacket => ManufacturingPacket.ID }
  53. };
  54. }
  55. [UserTracking("Manufacturing")]
  56. [Caption("Manufacturing")]
  57. public class ManufacturingPacket : Entity, IPersistent, IRemotable, IOneToMany<Setout>, ILicense<ManufacturingLicense>, IIssues
  58. {
  59. [TextBoxEditor]
  60. [EditorSequence(1)]
  61. public string Title { get; set; }
  62. [CodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  63. [EditorSequence(2)]
  64. public string Serial { get; set; }
  65. [EditorSequence(3)]
  66. [EntityRelationship(DeleteAction.Cascade)]
  67. public SetoutLink SetoutLink { get; set; }
  68. [EditorSequence(4)]
  69. public JobITPLink ITP { get; set; }
  70. [TextBoxEditor]
  71. [EditorSequence(5)]
  72. public string Location { get; set; }
  73. // Used to calculate time & materials
  74. [EditorSequence(7)]
  75. [IntegerEditor]
  76. public int Quantity { get; set; }
  77. [EditorSequence(8)]
  78. [IntegerEditor]
  79. // Determines # of barcodes to print
  80. public int BarcodeQty { get; set; }
  81. [EditorSequence(9)]
  82. [TimestampEditor(Editable = Editable.Hidden)]
  83. public override DateTime Created
  84. {
  85. get => base.Created;
  86. set => base.Created = value;
  87. }
  88. [EditorSequence(10)]
  89. [DateTimeEditor]
  90. public DateTime DueDate { get; set; }
  91. [EditorSequence("Design", 101)]
  92. [EntityRelationship(DeleteAction.Cascade)]
  93. public PDFDocumentLink Drawing { get; set; }
  94. // To be overlaid over the PDF document
  95. [EditorSequence("Design", 102)]
  96. [TextBoxEditor]
  97. public string WaterMark { get; set; }
  98. [EditorSequence("Design", 103)]
  99. public double Height { get; set; }
  100. [EditorSequence("Design", 104)]
  101. public double Width { get; set; }
  102. [EditorSequence("Design", 105)]
  103. public double Length { get; set; }
  104. [EditorSequence("Manufacturing", 200)]
  105. public ManufacturingTemplateLink ManufacturingTemplateLink { get; set; }
  106. [EditorSequence("Manufacturing", 201)]
  107. [TimestampEditor(Editable = Editable.Disabled)]
  108. public DateTime Issued { get; set; }
  109. [EditorSequence("Manufacturing", 202)]
  110. [LoggableProperty]
  111. public bool Priority { get; set; }
  112. [EditorSequence("Manufacturing", 203)]
  113. [LoggableProperty]
  114. public bool Distributed { get; set; }
  115. [EditorSequence("Manufacturing", 204)]
  116. [TextBoxEditor(Editable = Editable.Disabled)]
  117. public string Trolleys { get; set; }
  118. [EditorSequence("Manufacturing", 205)]
  119. [TimestampEditor(Editable = Editable.Hidden)]
  120. public DateTime BarcodePrinted { get; set; }
  121. [EditorSequence("Manufacturing", 206)]
  122. [DateTimeEditor(Editable = Editable.Disabled)]
  123. [SecondaryIndex]
  124. public DateTime Completed { get; set; }
  125. [SecondaryIndex]
  126. [TimestampEditor]
  127. [EditorSequence("Manufacturing", 207)]
  128. public DateTime Archived { get; set; } = DateTime.MinValue;
  129. [CheckBoxEditor]
  130. [EditorSequence("Issues", 2)]
  131. [LoggableProperty]
  132. public bool OnHold { get; set; }
  133. [MemoEditor]
  134. [EditorSequence("Issues", 1)]
  135. [LoggableProperty]
  136. public string Issues { get; set; }
  137. public SetoutGroupLink SetoutGroup { get; set; }
  138. public override string ToString()
  139. {
  140. return string.Format("{0} {1}", SetoutLink.Number, Serial);
  141. }
  142. public static void Progress(IEnumerable<ManufacturingPacket> packets, ManufacturingPacketStage[] Stages)
  143. {
  144. //List<ManufacturingPacketStage> updates = new List<ManufacturingPacketStage>();
  145. foreach (var packet in packets.Where(x => !x.StageLink.Equals(CoreUtils.FullGuid)))
  146. {
  147. var stages = Stages.Where(x => x.Parent.ID.Equals(packet.ID));
  148. long sequence = 0;
  149. var stage = stages.FirstOrDefault(x => x.ID.Equals(packet.StageLink.ID));
  150. if (stage != null)
  151. {
  152. stage.Completed = DateTime.Now;
  153. stage.PercentageComplete = 100.0F;
  154. sequence = stage.Sequence;
  155. }
  156. // Update the pointer to the next stage
  157. stage = stages.Where(x => x.Sequence > sequence).FirstOrDefault();
  158. if (stage != null)
  159. {
  160. stage.QualityStatus = QualityStatus.NotChecked;
  161. stage.QualityNotes = "";
  162. stage.Station = 0;
  163. stage.Started = DateTime.MinValue;
  164. stage.Completed = DateTime.MinValue;
  165. stage.PercentageComplete = 0.0F;
  166. packet.StageLink.ID = stage.ID;
  167. packet.StageLink.Synchronise(stage);
  168. }
  169. else
  170. {
  171. packet.StageLink.ID = CoreUtils.FullGuid;
  172. }
  173. packet.Issued = !packet.StageLink.IsValid() ? DateTime.MinValue : packet.Issued.IsEmpty() ? DateTime.Now : packet.Issued;
  174. packet.Completed = packet.StageLink.ID.Equals(CoreUtils.FullGuid)
  175. ? packet.Completed.IsEmpty() ? DateTime.Now : packet.Completed
  176. : DateTime.MinValue;
  177. }
  178. }
  179. public static void Regress(IEnumerable<ManufacturingPacket> pkts, ManufacturingPacketStage[] stgs)
  180. {
  181. foreach(var packet in pkts)
  182. {
  183. var stages = stgs.Where(x => x.Parent.ID.Equals(packet.ID));
  184. var sequence = long.MaxValue;
  185. var stage = stages.FirstOrDefault(x => x.ID.Equals(packet.StageLink.ID));
  186. if (stage != null)
  187. {
  188. stage.Completed = DateTime.MinValue;
  189. stage.PercentageComplete = 0.0F;
  190. stage.QualityStatus = QualityStatus.NotChecked;
  191. stage.QualityNotes = "";
  192. sequence = stage.Sequence;
  193. }
  194. // Update the pointer to the previous stage
  195. stage = stages.Where(x => x.Sequence < sequence).LastOrDefault();
  196. if (stage != null)
  197. {
  198. stage.QualityStatus = QualityStatus.NotChecked;
  199. stage.QualityNotes = "";
  200. stage.Station = 0;
  201. stage.Started = DateTime.MinValue;
  202. stage.Completed = DateTime.MinValue;
  203. stage.PercentageComplete = 0.0F;
  204. }
  205. packet.StageLink.ID = stage == null ? Guid.Empty : stage.ID;
  206. packet.Issued = !packet.StageLink.IsValid() ? DateTime.MinValue : packet.Issued.IsEmpty() ? DateTime.Now : packet.Issued;
  207. packet.DueDate = packet.Issued.IsEmpty() ? DateTime.MinValue : packet.DueDate;
  208. packet.Completed = packet.StageLink.ID.Equals(CoreUtils.FullGuid)
  209. ? packet.Completed.IsEmpty() ? DateTime.Now : packet.Completed
  210. : DateTime.MinValue;
  211. }
  212. }
  213. #region Aggregates
  214. [EditorSequence(300)]
  215. [DoubleEditor(Editable = Editable.Hidden)]
  216. [Formula(typeof(ManufacturingPacketArea))]
  217. public double Area { get; set; }
  218. [EditorSequence(301)]
  219. [DoubleEditor(Editable = Editable.Hidden)]
  220. [Formula(typeof(ManufacturingPacketVolume))]
  221. public double Volume { get; set; }
  222. [EditorSequence(302)]
  223. [Aggregate(typeof(ManufacturingPacketTime))]
  224. public TimeSpan Time { get; set; }
  225. [EditorSequence(303)]
  226. [Aggregate(typeof(ManufacturingPacketTimeRemaining))]
  227. public TimeSpan TimeRemaining { get; set; }
  228. [EditorSequence(304)]
  229. [Aggregate(typeof(ManufacturingPacketActualTime))]
  230. public TimeSpan ActualTime { get; set; }
  231. #endregion
  232. #region Internal / NullEditor Properties
  233. [NullEditor]
  234. public ManufacturingPacketStageLink StageLink { get; set; }
  235. [NullEditor]
  236. public QAFormLink QAForm { get; set; }
  237. // I think ITPs will end up being linked to a stage,
  238. // So this might get obsoleted at some point
  239. [NullEditor]
  240. public JobStageLink JobStage { get; set; }
  241. [NullEditor]
  242. [EntityRelationship(DeleteAction.SetNull)]
  243. public PurchaseOrderItemLink OrderItem { get; set; }
  244. [NullEditor]
  245. public DateTime EstimatedDate { get; set; }
  246. #endregion
  247. #region Obsolete Properties
  248. // The code of the Linked Manufacturing Template
  249. // Suggested - set to obsolete?
  250. [NullEditor]
  251. [Obsolete("Replaced with ManufacturingTemplateLink.Code", true)]
  252. public string Code { get; set; }
  253. // The Factory to which this packet template belongs
  254. // Suggestion - Set to obsolete?
  255. [NullEditor]
  256. [Obsolete("Replaced with ManufacturingTemplateLink.FactoryLink.ID")]
  257. public string Group { get; set; }
  258. [NullEditor]
  259. [Obsolete("Replaced with BarcodeQty")]
  260. public bool GroupedBarcode { get; set; }
  261. private BarcodeType _barcodetype = BarcodeType.Unspecified;
  262. [NullEditor]
  263. [Obsolete("Replaced with BarcodeQty")]
  264. public BarcodeType BarcodeType
  265. {
  266. get => _barcodetype == BarcodeType.Unspecified ? GroupedBarcode ? BarcodeType.Grouped : BarcodeType.Individual : _barcodetype;
  267. set => _barcodetype = value;
  268. }
  269. // Comes from Setout.Title (Should be Reference)
  270. [NullEditor]
  271. [Obsolete("Replaced with SetoutLink.Reference")]
  272. public string Reference { get; set; }
  273. // Comes from Setout.Location
  274. [NullEditor]
  275. [Obsolete("Replaced with SetoutLink.Description")]
  276. public string Description { get; set; }
  277. [Obsolete("Replaced With ManufacturingTemplateLink")]
  278. [NullEditor]
  279. public Guid ManufacturingItemID { get; set; }
  280. [NullEditor]
  281. [Obsolete("Replaced with ManufacturingPacketLink.Code")]
  282. public string Template { get; set; }
  283. [Obsolete("Replaced With SetoutLink.JobLink")]
  284. [EntityRelationship(DeleteAction.Cascade)]
  285. [NullEditor]
  286. public JobLink JobLink { get; set; }
  287. #endregion
  288. #region Functions
  289. //public void MovePrevious()
  290. //{
  291. // bool bFound = false;
  292. // SetoutStage prev = null;
  293. // foreach (SetoutStage stage in Stages)
  294. // {
  295. // if (bFound)
  296. // {
  297. // stage.Started = DateTime.MinValue;
  298. // stage.Completed = DateTime.MinValue;
  299. // stage.PercentageComplete = 0.0F;
  300. // }
  301. // else if (stage.Completed.IsEmpty())
  302. // {
  303. // stage.Started = DateTime.MinValue;
  304. // stage.Completed = DateTime.MinValue;
  305. // stage.PercentageComplete = 0.0F;
  306. // bFound = true;
  307. // if (prev != null)
  308. // prev.Completed = DateTime.MinValue;
  309. // }
  310. // else
  311. // prev = stage;
  312. // }
  313. // if (prev == null)
  314. // Issued = DateTime.MinValue;
  315. // Completed = DateTime.MinValue;
  316. // Stage = CurrentStage();
  317. //}
  318. //public void MoveNext()
  319. //{
  320. // bool bFound = false;
  321. // bool bComplete = true;
  322. // if (Issued.IsEmpty())
  323. // Issued = DateTime.Now;
  324. // foreach (SetoutStage stage in Stages)
  325. // {
  326. // if (bFound)
  327. // {
  328. // stage.Started = DateTime.MinValue;
  329. // stage.Completed = DateTime.MinValue;
  330. // stage.PercentageComplete = 0.0F;
  331. // bComplete = false;
  332. // }
  333. // else if (stage.Started.IsEmpty())
  334. // {
  335. // //stage.Started = DateTime.Now;
  336. // stage.Completed = DateTime.MinValue;
  337. // stage.PercentageComplete = 0.0F;
  338. // bComplete = false;
  339. // bFound = true;
  340. // }
  341. // else if (stage.Completed.IsEmpty())
  342. // {
  343. // stage.Completed = DateTime.Now;
  344. // stage.PercentageComplete = 100.0F;
  345. // bFound = true;
  346. // }
  347. // }
  348. // //Completed = bComplete ? DateTime.Now : DateTime.MinValue;
  349. // bool bIsComplete = !Stages.Any(x => x.Completed.Equals(DateTime.MinValue));
  350. // if (bIsComplete && Completed.Equals(DateTime.MinValue))
  351. // Completed = DateTime.Now;
  352. // Stage = CurrentStage();
  353. //}
  354. //public Boolean IsComplete()
  355. //{
  356. // if ((!Issued.IsEmpty()) && (Stages != null))
  357. // {
  358. // foreach (SetoutStage stage in Stages)
  359. // {
  360. // if (stage.Completed.IsEmpty())
  361. // return false;
  362. // }
  363. // return true;
  364. // }
  365. // return false;
  366. //}
  367. //public String Status()
  368. //{
  369. // if ((Stages == null) || (!Stages.Any()))
  370. // return "No Template!";
  371. // if (Issued.IsEmpty())
  372. // return "To Be Issued";
  373. // var stage = GetCurrentStage();
  374. // if (stage != null)
  375. // {
  376. // if (!Archived.IsEmpty())
  377. // return "Cancelled";
  378. // else
  379. // return String.Format("{0} ({1:F2}%)", stage.Name, stage.PercentageComplete);
  380. // }
  381. // return "Complete";
  382. //}
  383. //public Guid CurrentStage()
  384. //{
  385. // if (Issued.IsEmpty())
  386. // return Guid.Empty;
  387. // SetoutStage stage = GetCurrentStage();
  388. // if (stage != null)
  389. // return stage.SectionID;
  390. // return CoreUtils.FullGuid;
  391. //}
  392. //public SetoutStage GetCurrentStage()
  393. //{
  394. // if ((!Issued.IsEmpty()) && (Stages != null))
  395. // {
  396. // foreach (SetoutStage stage in Stages)
  397. // {
  398. // if (stage.Completed.IsEmpty())
  399. // return stage;
  400. // }
  401. // }
  402. // return null;
  403. //}
  404. //public void SetStage(Guid id, bool complete = false)
  405. //{
  406. // bool bFound = id == Guid.Empty;
  407. // Issued = (id == Guid.Empty) ? DateTime.MinValue : Issued.IsEmpty() ? DateTime.Now : Issued;
  408. // if (id == CoreUtils.FullGuid)
  409. // {
  410. // foreach (SetoutStage stage in Stages)
  411. // {
  412. // stage.Started = stage.Started.IsEmpty() ? DateTime.Now : stage.Started;
  413. // stage.Completed = stage.Completed.IsEmpty() ? DateTime.Now : stage.Completed;
  414. // stage.PercentageComplete = 100.0F;
  415. // Completed = stage.Completed;
  416. // }
  417. // }
  418. // else
  419. // {
  420. // //Completed = DateTime.MinValue;
  421. // foreach (SetoutStage stage in Stages)
  422. // {
  423. // if (stage.SectionID.Equals(id))
  424. // {
  425. // bFound = true;
  426. // //stage.Started = stage.Started.IsEmpty() ? DateTime.Now : stage.Started;
  427. // stage.Completed = complete ? DateTime.Now : DateTime.MinValue;
  428. // stage.PercentageComplete = stage.Completed.IsEmpty() ? 0.0F : 100.0F;
  429. // }
  430. // else
  431. // {
  432. // if (!bFound)
  433. // {
  434. // // Stages Before this stage - Update Started and Completed if Empty
  435. // if (stage.Started.IsEmpty())
  436. // stage.Started = DateTime.Now;
  437. // if (stage.Completed.IsEmpty())
  438. // stage.Completed = DateTime.Now;
  439. // stage.PercentageComplete = 100.0F;
  440. // }
  441. // else
  442. // {
  443. // // Stages After This Stage - Blank out Started and Completed
  444. // stage.Started = DateTime.MinValue;
  445. // stage.Completed = DateTime.MinValue;
  446. // stage.PercentageComplete = 0.0F;
  447. // }
  448. // }
  449. // }
  450. // }
  451. // bool bIsComplete = !Stages.Any(x => x.Completed.Equals(DateTime.MinValue));
  452. // if (bIsComplete && Completed.Equals(DateTime.MinValue))
  453. // Completed = DateTime.Now;
  454. // Stage = CurrentStage();
  455. //}
  456. #endregion
  457. }
  458. }