ManufacturingPacket.cs 21 KB

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