ManufacturingPacket.cs 21 KB

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