ManufacturingPacket.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 override string ToString()
  138. {
  139. return string.Format("{0} {1}", SetoutLink.Number, Serial);
  140. }
  141. public static void Progress(IEnumerable<ManufacturingPacket> packets, ManufacturingPacketStage[] Stages)
  142. {
  143. //List<ManufacturingPacketStage> updates = new List<ManufacturingPacketStage>();
  144. foreach (var packet in packets.Where(x => !x.StageLink.Equals(CoreUtils.FullGuid)))
  145. {
  146. var stages = Stages.Where(x => x.Parent.ID.Equals(packet.ID));
  147. long sequence = 0;
  148. var stage = stages.FirstOrDefault(x => x.ID.Equals(packet.StageLink.ID));
  149. if (stage != null)
  150. {
  151. stage.Completed = DateTime.Now;
  152. stage.PercentageComplete = 100.0F;
  153. sequence = stage.Sequence;
  154. }
  155. // Update the pointer to the next stage
  156. stage = stages.Where(x => x.Sequence > sequence).FirstOrDefault();
  157. if (stage != null)
  158. {
  159. stage.QualityStatus = QualityStatus.NotChecked;
  160. stage.QualityNotes = "";
  161. stage.Station = 0;
  162. stage.Started = DateTime.MinValue;
  163. stage.Completed = DateTime.MinValue;
  164. stage.PercentageComplete = 0.0F;
  165. packet.StageLink.ID = stage.ID;
  166. packet.StageLink.Synchronise(stage);
  167. }
  168. else
  169. {
  170. packet.StageLink.ID = CoreUtils.FullGuid;
  171. }
  172. packet.Issued = !packet.StageLink.IsValid() ? DateTime.MinValue : packet.Issued.IsEmpty() ? DateTime.Now : packet.Issued;
  173. packet.Completed = packet.StageLink.ID.Equals(CoreUtils.FullGuid)
  174. ? packet.Completed.IsEmpty() ? DateTime.Now : packet.Completed
  175. : DateTime.MinValue;
  176. }
  177. }
  178. public static void Regress(IEnumerable<ManufacturingPacket> pkts, ManufacturingPacketStage[] stgs)
  179. {
  180. foreach(var packet in pkts)
  181. {
  182. var stages = stgs.Where(x => x.Parent.ID.Equals(packet.ID));
  183. var sequence = long.MaxValue;
  184. var stage = stages.FirstOrDefault(x => x.ID.Equals(packet.StageLink.ID));
  185. if (stage != null)
  186. {
  187. stage.Completed = DateTime.MinValue;
  188. stage.PercentageComplete = 0.0F;
  189. stage.QualityStatus = QualityStatus.NotChecked;
  190. stage.QualityNotes = "";
  191. sequence = stage.Sequence;
  192. }
  193. // Update the pointer to the previous stage
  194. stage = stages.Where(x => x.Sequence < sequence).LastOrDefault();
  195. if (stage != null)
  196. {
  197. stage.QualityStatus = QualityStatus.NotChecked;
  198. stage.QualityNotes = "";
  199. stage.Station = 0;
  200. stage.Started = DateTime.MinValue;
  201. stage.Completed = DateTime.MinValue;
  202. stage.PercentageComplete = 0.0F;
  203. }
  204. packet.StageLink.ID = stage == null ? Guid.Empty : stage.ID;
  205. packet.Issued = !packet.StageLink.IsValid() ? DateTime.MinValue : packet.Issued.IsEmpty() ? DateTime.Now : packet.Issued;
  206. packet.DueDate = packet.Issued.IsEmpty() ? DateTime.MinValue : packet.DueDate;
  207. packet.Completed = packet.StageLink.ID.Equals(CoreUtils.FullGuid)
  208. ? packet.Completed.IsEmpty() ? DateTime.Now : packet.Completed
  209. : DateTime.MinValue;
  210. }
  211. }
  212. #region Aggregates
  213. [EditorSequence(300)]
  214. [DoubleEditor(Editable = Editable.Hidden)]
  215. [Formula(typeof(ManufacturingPacketArea))]
  216. public double Area { get; set; }
  217. [EditorSequence(301)]
  218. [DoubleEditor(Editable = Editable.Hidden)]
  219. [Formula(typeof(ManufacturingPacketVolume))]
  220. public double Volume { get; set; }
  221. [EditorSequence(302)]
  222. [Aggregate(typeof(ManufacturingPacketTime))]
  223. public TimeSpan Time { get; set; }
  224. [EditorSequence(303)]
  225. [Aggregate(typeof(ManufacturingPacketTimeRemaining))]
  226. public TimeSpan TimeRemaining { get; set; }
  227. [EditorSequence(304)]
  228. [Aggregate(typeof(ManufacturingPacketActualTime))]
  229. public TimeSpan ActualTime { get; set; }
  230. #endregion
  231. #region Internal / NullEditor Properties
  232. [NullEditor]
  233. public ManufacturingPacketStageLink StageLink { get; set; }
  234. [NullEditor]
  235. public QAFormLink QAForm { get; set; }
  236. // I think ITPs will end up being linked to a stage,
  237. // So this might get obsoleted at some point
  238. [NullEditor]
  239. public JobStageLink JobStage { get; set; }
  240. [NullEditor]
  241. [EntityRelationship(DeleteAction.SetNull)]
  242. public PurchaseOrderItemLink OrderItem { get; set; }
  243. [NullEditor]
  244. public DateTime EstimatedDate { get; set; }
  245. #endregion
  246. #region Obsolete Properties
  247. // The code of the Linked Manufacturing Template
  248. // Suggested - set to obsolete?
  249. [NullEditor]
  250. [Obsolete("Replaced with ManufacturingTemplateLink.Code", true)]
  251. public string Code { get; set; }
  252. // The Factory to which this packet template belongs
  253. // Suggestion - Set to obsolete?
  254. [NullEditor]
  255. [Obsolete("Replaced with ManufacturingTemplateLink.FactoryLink.ID")]
  256. public string Group { get; set; }
  257. [NullEditor]
  258. [Obsolete("Replaced with BarcodeQty")]
  259. public bool GroupedBarcode { get; set; }
  260. private BarcodeType _barcodetype = BarcodeType.Unspecified;
  261. [NullEditor]
  262. [Obsolete("Replaced with BarcodeQty")]
  263. public BarcodeType BarcodeType
  264. {
  265. get => _barcodetype == BarcodeType.Unspecified ? GroupedBarcode ? BarcodeType.Grouped : BarcodeType.Individual : _barcodetype;
  266. set => _barcodetype = value;
  267. }
  268. // Comes from Setout.Title (Should be Reference)
  269. [NullEditor]
  270. [Obsolete("Replaced with SetoutLink.Reference")]
  271. public string Reference { get; set; }
  272. // Comes from Setout.Location
  273. [NullEditor]
  274. [Obsolete("Replaced with SetoutLink.Description")]
  275. public string Description { get; set; }
  276. [Obsolete("Replaced With ManufacturingTemplateLink")]
  277. [NullEditor]
  278. public Guid ManufacturingItemID { get; set; }
  279. [NullEditor]
  280. [Obsolete("Replaced with ManufacturingPacketLink.Code")]
  281. public string Template { get; set; }
  282. [Obsolete("Replaced With SetoutLink.JobLink")]
  283. [EntityRelationship(DeleteAction.Cascade)]
  284. [NullEditor]
  285. public JobLink JobLink { get; set; }
  286. #endregion
  287. #region Functions
  288. //public void MovePrevious()
  289. //{
  290. // bool bFound = false;
  291. // SetoutStage prev = null;
  292. // foreach (SetoutStage stage in Stages)
  293. // {
  294. // if (bFound)
  295. // {
  296. // stage.Started = DateTime.MinValue;
  297. // stage.Completed = DateTime.MinValue;
  298. // stage.PercentageComplete = 0.0F;
  299. // }
  300. // else if (stage.Completed.IsEmpty())
  301. // {
  302. // stage.Started = DateTime.MinValue;
  303. // stage.Completed = DateTime.MinValue;
  304. // stage.PercentageComplete = 0.0F;
  305. // bFound = true;
  306. // if (prev != null)
  307. // prev.Completed = DateTime.MinValue;
  308. // }
  309. // else
  310. // prev = stage;
  311. // }
  312. // if (prev == null)
  313. // Issued = DateTime.MinValue;
  314. // Completed = DateTime.MinValue;
  315. // Stage = CurrentStage();
  316. //}
  317. //public void MoveNext()
  318. //{
  319. // bool bFound = false;
  320. // bool bComplete = true;
  321. // if (Issued.IsEmpty())
  322. // Issued = DateTime.Now;
  323. // foreach (SetoutStage stage in Stages)
  324. // {
  325. // if (bFound)
  326. // {
  327. // stage.Started = DateTime.MinValue;
  328. // stage.Completed = DateTime.MinValue;
  329. // stage.PercentageComplete = 0.0F;
  330. // bComplete = false;
  331. // }
  332. // else if (stage.Started.IsEmpty())
  333. // {
  334. // //stage.Started = DateTime.Now;
  335. // stage.Completed = DateTime.MinValue;
  336. // stage.PercentageComplete = 0.0F;
  337. // bComplete = false;
  338. // bFound = true;
  339. // }
  340. // else if (stage.Completed.IsEmpty())
  341. // {
  342. // stage.Completed = DateTime.Now;
  343. // stage.PercentageComplete = 100.0F;
  344. // bFound = true;
  345. // }
  346. // }
  347. // //Completed = bComplete ? DateTime.Now : DateTime.MinValue;
  348. // bool bIsComplete = !Stages.Any(x => x.Completed.Equals(DateTime.MinValue));
  349. // if (bIsComplete && Completed.Equals(DateTime.MinValue))
  350. // Completed = DateTime.Now;
  351. // Stage = CurrentStage();
  352. //}
  353. //public Boolean IsComplete()
  354. //{
  355. // if ((!Issued.IsEmpty()) && (Stages != null))
  356. // {
  357. // foreach (SetoutStage stage in Stages)
  358. // {
  359. // if (stage.Completed.IsEmpty())
  360. // return false;
  361. // }
  362. // return true;
  363. // }
  364. // return false;
  365. //}
  366. //public String Status()
  367. //{
  368. // if ((Stages == null) || (!Stages.Any()))
  369. // return "No Template!";
  370. // if (Issued.IsEmpty())
  371. // return "To Be Issued";
  372. // var stage = GetCurrentStage();
  373. // if (stage != null)
  374. // {
  375. // if (!Archived.IsEmpty())
  376. // return "Cancelled";
  377. // else
  378. // return String.Format("{0} ({1:F2}%)", stage.Name, stage.PercentageComplete);
  379. // }
  380. // return "Complete";
  381. //}
  382. //public Guid CurrentStage()
  383. //{
  384. // if (Issued.IsEmpty())
  385. // return Guid.Empty;
  386. // SetoutStage stage = GetCurrentStage();
  387. // if (stage != null)
  388. // return stage.SectionID;
  389. // return CoreUtils.FullGuid;
  390. //}
  391. //public SetoutStage GetCurrentStage()
  392. //{
  393. // if ((!Issued.IsEmpty()) && (Stages != null))
  394. // {
  395. // foreach (SetoutStage stage in Stages)
  396. // {
  397. // if (stage.Completed.IsEmpty())
  398. // return stage;
  399. // }
  400. // }
  401. // return null;
  402. //}
  403. //public void SetStage(Guid id, bool complete = false)
  404. //{
  405. // bool bFound = id == Guid.Empty;
  406. // Issued = (id == Guid.Empty) ? DateTime.MinValue : Issued.IsEmpty() ? DateTime.Now : Issued;
  407. // if (id == CoreUtils.FullGuid)
  408. // {
  409. // foreach (SetoutStage stage in Stages)
  410. // {
  411. // stage.Started = stage.Started.IsEmpty() ? DateTime.Now : stage.Started;
  412. // stage.Completed = stage.Completed.IsEmpty() ? DateTime.Now : stage.Completed;
  413. // stage.PercentageComplete = 100.0F;
  414. // Completed = stage.Completed;
  415. // }
  416. // }
  417. // else
  418. // {
  419. // //Completed = DateTime.MinValue;
  420. // foreach (SetoutStage stage in Stages)
  421. // {
  422. // if (stage.SectionID.Equals(id))
  423. // {
  424. // bFound = true;
  425. // //stage.Started = stage.Started.IsEmpty() ? DateTime.Now : stage.Started;
  426. // stage.Completed = complete ? DateTime.Now : DateTime.MinValue;
  427. // stage.PercentageComplete = stage.Completed.IsEmpty() ? 0.0F : 100.0F;
  428. // }
  429. // else
  430. // {
  431. // if (!bFound)
  432. // {
  433. // // Stages Before this stage - Update Started and Completed if Empty
  434. // if (stage.Started.IsEmpty())
  435. // stage.Started = DateTime.Now;
  436. // if (stage.Completed.IsEmpty())
  437. // stage.Completed = DateTime.Now;
  438. // stage.PercentageComplete = 100.0F;
  439. // }
  440. // else
  441. // {
  442. // // Stages After This Stage - Blank out Started and Completed
  443. // stage.Started = DateTime.MinValue;
  444. // stage.Completed = DateTime.MinValue;
  445. // stage.PercentageComplete = 0.0F;
  446. // }
  447. // }
  448. // }
  449. // }
  450. // bool bIsComplete = !Stages.Any(x => x.Completed.Equals(DateTime.MinValue));
  451. // if (bIsComplete && Completed.Equals(DateTime.MinValue))
  452. // Completed = DateTime.Now;
  453. // Stage = CurrentStage();
  454. //}
  455. #endregion
  456. }
  457. }