123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.WPF;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for SchedulePanel.xaml
- /// </summary>
- public partial class SchedulePanel : UserControl, IPanel<ScheduleBooking>
- {
- private List<ScheduleBooking> _bookings = new();
- //Dictionary<String,List<Tuple<String,Guid>>> Groups = new Dictionary<string,List<Tuple<String, Guid>>>();
- private string CurrentFactory = "";
- //private void DoChangeGroup(PanelAction obj)
- //{
- // Group = obj.Caption;
- // Refresh();
- //}
- private readonly Random generator = new(Guid.NewGuid().GetHashCode());
- private FactorySetup settings;
- private Dictionary<Guid, string> StepNames = new();
- public ObservableCollection<Tuple<string, BitmapImage>> FactoryList { get; private set; }
- public bool IsReady { get; set; }
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]> { { typeof(ScheduleBooking).EntityName(), new CoreRow[] { } } };
- }
- public void Setup()
- {
- settings = new GlobalConfiguration<FactorySetup>().Load();
- Filter<Document> imageFilter = null;
- foreach (var group in settings.Groups)
- if (group.Thumbnail.ID != Guid.Empty)
- {
- var newfilter = new Filter<Document>(x => x.ID).IsEqualTo(group.Thumbnail.ID);
- if (imageFilter == null)
- imageFilter = newfilter;
- else
- imageFilter.Ors.Add(newfilter);
- }
- var Images = new Client<Document>().Load(imageFilter);
- FactoryList = new ObservableCollection<Tuple<string, BitmapImage>>();
- FactoryList.Add(new Tuple<string, BitmapImage>("All Sections", PRSDesktop.Resources.factory.AsBitmapImage()));
- //FactorySetup settings = new GlobalConfiguration<FactorySetup>().Load();
- foreach (var group in settings.Groups)
- {
- var groups = new List<string>();
- if (!FactoryList.Any(x => x.Item1.Equals(group.Group)))
- {
- var bOK = false;
- foreach (var section in settings.Sections)
- if (section.Group.Equals(group.Group) && !section.Shared)
- bOK = true;
- if (bOK)
- {
- var image = Images.FirstOrDefault(x => x.ID.Equals(group.Thumbnail.ID));
- //byte[] data = image != null ? image.Data : null;
- BitmapImage img = null;
- if (image != null && image.Data != null && image.Data.Length > 0)
- {
- img = new BitmapImage();
- img.LoadImage(image.Data);
- }
- else
- {
- img = PRSDesktop.Resources.factory.AsBitmapImage();
- }
- FactoryList.Add(new Tuple<string, BitmapImage>(group.Group, img));
- }
- }
- }
- Factories.ItemsSource = FactoryList;
- Factories.SelectedIndex = Factories.Items.Count > 1 ? 1 : 0;
- //foreach (FactorySection section in settings.Sections)
- //{
- // StepNames[section.ID] = section.Name;
- // if (!section.Shared)
- // {
- // String group = String.IsNullOrEmpty(section.Group) ? "General" : section.Group;
- // if (!Groups.ContainsKey(group))
- // Groups[group] = new List<Tuple<String,Guid>>();
- // Groups[group].Add(new Tuple<String,Guid>(section.Name,section.ID));
- // }
- //}
- //if (Groups.Count > 0)
- // CurrentFactory = Groups.Keys.First();
- Refresh();
- }
- public void Shutdown()
- {
- }
- public void CreateToolbarButtons(IPanelHost host)
- {
- host.CreatePanelAction(new PanelAction
- { Caption = "Recalculate", OnExecute = DoRecalculateSchedule, Image = PRSDesktop.Resources.clock });
- //foreach (String group in Groups.Keys)
- // host.CreateToolbarButton(new PanelAction() { Caption = group, OnExecute = DoChangeGroup, Image = PRSDesktop.Resources.shared });
- }
- public event DataModelUpdateEvent OnUpdateDataModel;
- public void Refresh()
- {
- // ResizeIntervals(Schedule.ActualHeight);
- // //Client<Job> jobclient = new Client<Job>();
- // //Job[] jobs = jobclient.Load();
- // Schedule.Appointments.Clear();
- //_bookings.Clear();
- //ResourceType restyp = new ResourceType() { TypeName = "Stations" };
- //var sections = settings.Sections.Where(x => !x.Shared && (String.IsNullOrEmpty(CurrentFactory) || x.Group.Equals(CurrentFactory)));
- //foreach (var section in sections)
- //{
- // restyp.ResourceCollection.Add(new Resource() { DisplayName = section.Name, ResourceName = section.ID.ToString() });
- //}
- //Schedule.ScheduleResourceTypeCollection.Clear();
- //Schedule.ScheduleResourceTypeCollection.Add(restyp);
- //Schedule.DayViewColumnCount = restyp.ResourceCollection.Count;
- // CoreTable setouts = new Client<ManufacturingPacket>().Query(new Filter<ManufacturingPacket>(x => x.Completed).IsEqualTo(DateTime.MinValue));
- //if (setouts.Rows.Count() > 0)
- //{
- // Comal.Classes.Calendar calendar = new Client<Comal.Classes.Calendar>().Load(new Filter<Comal.Classes.Calendar>(x => x.IsActive).IsEqualTo(true)).FirstOrDefault();
- // if (calendar != null)
- // {
- // foreach (CalendarBooking booking in calendar.Bookings)
- // {
- // CoreRow packet = setouts.Rows.Where(x => x.Get<Guid>("ID").Equals(booking.Packet)).FirstOrDefault();
- // if (packet != null)
- // {
- // ScheduleBooking appt = new ScheduleBooking()
- // {
- // SetoutID = packet.Get<ManufacturingPacket, Guid>(x=>x.SetoutLink.ID),
- // StartTime = booking.Start,
- // EndTime = booking.End,
- // Subject = packet.Get<ManufacturingPacket, String>(x=>x.SetoutLink.Description),
- // //SetoutNumber = packet.Get<ManufacturingPacket, String>(x => x.Se),
- // Description = String.Format("{0} {1}", packet.Get<ManufacturingPacket, String>(x => x.SetoutLink.Number), packet.Get<ManufacturingPacket, String>(x => x.Serial)),
- // AllDay = false
- // };
- // if (booking.End > packet.Get<DateTime>("DueDate"))
- // {
- // appt.AppointmentBackground = CreateBrush(Colors.Firebrick, booking.IsShared);
- // appt.AppointmentForeground = CreateBrush(Colors.Yellow, false);
- // }
- // else if (booking.End > packet.Get<DateTime>("DueDate").Subtract(new TimeSpan(1, 0, 0, 0)))
- // {
- // appt.AppointmentBackground = CreateBrush(Colors.Orange, booking.IsShared);
- // appt.AppointmentForeground = CreateBrush(Colors.Black, false);
- // }
- // else
- // {
- // appt.AppointmentBackground = CreateBrush(Colors.LightGreen, booking.IsShared);
- // appt.AppointmentForeground = CreateBrush(Colors.Black, false);
- // }
- // appt.ResourceCollection.Add(new Resource() { ResourceName = booking.Station.ToString(), TypeName = "Stations" });
- // _bookings.Add(appt);
- // Schedule.Appointments.Add(appt);
- // }
- // }
- // }
- //}
- #region Old Code
- /*
-
- Factory factory = new Factory();
- foreach (FactorySection section in settings.Sections)
- {
- factory.Sections.Add(new Section(section.Name, section.ID, section.Stations));
- }
-
- if (factory.Sections.Count == 0)
- {
- System.Windows.MessageBox.Show("Factory has not been configured!");
- return;
- }
-
- Client<Setout> client = new Client<Setout>();
- Setout[] setouts = client.Load(
- new Filter<Setout>(x => x.ID, Operator.NotEqualTo, Guid.Empty),
- new SortOrder<Setout>(x => x.DueDate, SortOrder.Ascending)
- );
- foreach(Setout setout in setouts)
- {
- List<Step> steps = new List<Step>();
-
- Guid _stage = Guid.Empty;
- String _status = "";
- setout.GetStage(out _stage, out _status);
- if (!_stage.Equals(Guid.Empty))
- {
- foreach (SetoutStage stage in setout.Stages)
- {
- if (stage.Completed.IsEmpty())
- steps.Add(new Step(stage.SectionID, stage.Minutes * setout.Quantity, stage.Sequence));
- }
- }
- if (steps.Count > 0)
- {
- factory.Widgets.Add
- (
- new Widget(setout.Title,setout.Job.Name,setout.Number,setout.DueDate,steps.ToArray())
- );
- }
- }
- //for (int i = 0; i < 100; i++)
- //{
- // DateTime date = DateTime.Today.AddDays(GetRandom(10F, 90F, 1F));
- // double cut = GetRandom(10F, 480F, 10F);
- // double mac = GetRandom(30F, 90F, 10F);
- // double fab = GetRandom(30F, 180F, 10F);
- // double ass = GetRandom(60F, 180F, 10F);
- // factory.Widgets.Add
- // (
- // new Widget
- // (
- // String.Format("{0:D2}", i + 1),
- // date,
- // new Step[]
- // {
- // new Step(StepType.Cutting , cut),
- // new Step(StepType.Machining , mac),
- // new Step(StepType.Fabricating , fab),
- // new Step(StepType.Assembling , ass)
- // }
- // )
- // );
- //}
-
- StringBuilder summary = new StringBuilder();
-
- //summary.AppendLine("Widget Due Date Cut Mac Fab Ass ");
- //summary.AppendLine("====== ========== ===== ===== ===== =====");
-
- //List<Widget> Widgets = factory.Widgets.OrderBy(x => x.DueDate).ToList();
- //foreach (Widget Widget in Widgets)
- //{
- // summary.AppendLine(String.Format("{0,-6} {1:dd/MM/yyyy} {2,5:hh\\:mm} {3,5:hh\\:mm} {4,5:hh\\:mm} {5,5:hh\\:mm}",
- // Widget.Name,
- // Widget.DueDate,
- // Widget.Steps[0].Duration,
- // Widget.Steps[1].Duration,
- // Widget.Steps[2].Duration,
- // Widget.Steps[3].Duration
- // ));
- //}
- //summary.AppendLine();
-
- factory.ProcessWidgets(DateTime.Today.AddHours(7), summary);
- summary.AppendLine();
-
- foreach (Section section in factory.Sections)
- {
- bool bFound = false;
- foreach (Tuple<String,Guid> tuple in Groups[Group])
- {
- if (tuple.Item2.Equals(section.ID))
- {
- bFound = true;
- break;
- }
- }
- if (bFound)
- {
- int i = 1; ;
- foreach (Station station in section.Stations)
- {
- StringBuilder line = new StringBuilder();
- String Heading = String.Format("Booking Schedule for {0} Station {1:D2}", section.Name, i);
- summary.AppendLine(Heading);
- summary.AppendLine(new string('=', Heading.Length));
- List<Booking> bookings = station.Bookings.OrderBy(x => x.Start).ToList();
- foreach (Booking booking in bookings)
- {
- summary.AppendLine(String.Format("From {1:HH:mm} until {2:HH:mm}, process {0}", booking.Widget.Name, booking.Start, booking.End));
- //if (station.StepType == StepType.Machining)
- //{
- Classes.Calendar appt = new Classes.Calendar()
- {
- SetoutNumber = booking.Widget.SetoutNumber,
- JobName = booking.Widget.JobName,
- DueDate = booking.Widget.DueDate,
-
- StartTime = booking.Start,
- EndTime = booking.End,
- Subject = booking.Widget.Name,
- AllDay = false
- };
- if (booking.End > booking.Widget.DueDate)
- {
- appt.AppointmentBackground = CreateBrush(Colors.Firebrick, booking.Shared);
- appt.AppointmentForeground = CreateBrush(Colors.Yellow, false);
- }
- else if (booking.End > booking.Widget.DueDate.Subtract(new TimeSpan(1, 0, 0, 0)))
- {
- appt.AppointmentBackground = CreateBrush(Colors.Orange, booking.Shared);
- appt.AppointmentForeground = CreateBrush(Colors.Black, false);
- }
- else
- {
- appt.AppointmentBackground = CreateBrush(Colors.LightGreen, booking.Shared);
- appt.AppointmentForeground = CreateBrush(Colors.Black, false);
- }
- appt.ResourceCollection.Add(new Resource() { ResourceName = station.ID.ToString(), TypeName = "Stations" });
- Schedule.Appointments.Add(appt);
- //}
-
- }
- summary.AppendLine();
- i++;
- }
- }
- }
- File.WriteAllText("factory.txt", summary.ToString());
-
- */
- #endregion
- }
- public DataModel DataModel(Selection selection)
- {
- return new ScheduleDataModel();
- }
- public void Heartbeat(TimeSpan time)
- {
- }
- private void DoRecalculateSchedule(PanelAction obj)
- {
- // Progress.Show("Recalculating Calendar");
- //Comal.Classes.Calendar calendar = new Comal.Classes.Calendar();
- // calendar.IsActive = true;
- // Progress.Show("Loading Manufacturing Packets");
- // var packets = new Client<ManufacturingPacket>().Load(new Filter<ManufacturingPacket>(x => x.Issued).IsNotEqualTo(DateTime.MinValue).And(x => x.Completed).IsEqualTo(DateTime.MinValue), new SortOrder<ManufacturingPacket>(x => x.DueDate));
- // Progress.Show("Rebuilding Calendar");
- // calendar.Build(packets);
- // Progress.Show("Saving Calendar");
- // new Client<Comal.Classes.Calendar>().Save(calendar, "Calendar Manually Built");
- // Progress.Close();
- // MessageBox.Show("Calculation Complete");
- // Refresh();
- }
- private double GetRandom(double minimum, double maximum, double step)
- {
- var value = generator.NextDouble() * (maximum - minimum) + minimum;
- value = Math.Round(value / step, 0) * step;
- return value;
- }
- public Brush CreateBrush(Color color, bool shared)
- {
- if (shared)
- {
- double dia1 = 1.5F;
- double dia2 = 2.0F;
- var vb = new VisualBrush();
- vb.TileMode = TileMode.Tile;
- vb.Viewport = new Rect(0, 0, dia1, dia1);
- vb.ViewportUnits = BrushMappingMode.Absolute;
- vb.Viewbox = new Rect(0, 0, dia2, dia2);
- vb.ViewboxUnits = BrushMappingMode.Absolute;
- var ellipse = new Ellipse();
- ellipse.Stroke = new SolidColorBrush(color);
- ellipse.Fill = new SolidColorBrush(Colors.DimGray);
- ellipse.Width = dia1;
- ellipse.Height = dia1;
- vb.Visual = ellipse;
- return vb;
- }
- return new SolidColorBrush(color);
- }
- private void ResizeIntervals(double height)
- {
- Schedule.IntervalHeight = (height - 130F) / 18F;
- }
- public Type DataType()
- {
- return typeof(ScheduleBooking);
- }
- private void Schedule_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
- {
- e.Action = DragAction.Cancel;
- }
- private void ProgressSetout_Click(object sender, RoutedEventArgs e)
- {
- }
- private void Factories_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (e.AddedItems.Count == 0 || Factories.SelectedIndex == 0)
- {
- CurrentFactory = "";
- }
- else
- {
- var selected = (Tuple<string, BitmapImage>)e.AddedItems[0];
- CurrentFactory = selected.Item1;
- }
- Refresh();
- }
- private void Schedule_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- ResizeIntervals(e.NewSize.Height);
- }
- }
- }
|