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 { /// /// Interaction logic for SchedulePanel.xaml /// public partial class SchedulePanel : UserControl, IPanel { private List _bookings = new(); //Dictionary>> Groups = new Dictionary>>(); 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 StepNames = new(); public ObservableCollection> FactoryList { get; private set; } public bool IsReady { get; set; } public Dictionary Selected() { return new Dictionary { { typeof(ScheduleBooking).EntityName(), new CoreRow[] { } } }; } public void Setup() { settings = new GlobalConfiguration().Load(); Filter imageFilter = null; foreach (var group in settings.Groups) if (group.Thumbnail.ID != Guid.Empty) { var newfilter = new Filter(x => x.ID).IsEqualTo(group.Thumbnail.ID); if (imageFilter == null) imageFilter = newfilter; else imageFilter.Ors.Add(newfilter); } var Images = new Client().Load(imageFilter); FactoryList = new ObservableCollection>(); FactoryList.Add(new Tuple("All Sections", PRSDesktop.Resources.factory.AsBitmapImage())); //FactorySetup settings = new GlobalConfiguration().Load(); foreach (var group in settings.Groups) { var groups = new List(); 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(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>(); // Groups[group].Add(new Tuple(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 jobclient = new Client(); // //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().Query(new Filter(x => x.Completed).IsEqualTo(DateTime.MinValue)); //if (setouts.Rows.Count() > 0) //{ // Comal.Classes.Calendar calendar = new Client().Load(new Filter(x => x.IsActive).IsEqualTo(true)).FirstOrDefault(); // if (calendar != null) // { // foreach (CalendarBooking booking in calendar.Bookings) // { // CoreRow packet = setouts.Rows.Where(x => x.Get("ID").Equals(booking.Packet)).FirstOrDefault(); // if (packet != null) // { // ScheduleBooking appt = new ScheduleBooking() // { // SetoutID = packet.Get(x=>x.SetoutLink.ID), // StartTime = booking.Start, // EndTime = booking.End, // Subject = packet.Get(x=>x.SetoutLink.Description), // //SetoutNumber = packet.Get(x => x.Se), // Description = String.Format("{0} {1}", packet.Get(x => x.SetoutLink.Number), packet.Get(x => x.Serial)), // AllDay = false // }; // if (booking.End > packet.Get("DueDate")) // { // appt.AppointmentBackground = CreateBrush(Colors.Firebrick, booking.IsShared); // appt.AppointmentForeground = CreateBrush(Colors.Yellow, false); // } // else if (booking.End > packet.Get("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 client = new Client(); Setout[] setouts = client.Load( new Filter(x => x.ID, Operator.NotEqualTo, Guid.Empty), new SortOrder(x => x.DueDate, SortOrder.Ascending) ); foreach(Setout setout in setouts) { List steps = new List(); 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 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 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 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().Load(new Filter(x => x.Issued).IsNotEqualTo(DateTime.MinValue).And(x => x.Completed).IsEqualTo(DateTime.MinValue), new SortOrder(x => x.DueDate)); // Progress.Show("Rebuilding Calendar"); // calendar.Build(packets); // Progress.Show("Saving Calendar"); // new Client().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)e.AddedItems[0]; CurrentFactory = selected.Item1; } Refresh(); } private void Schedule_SizeChanged(object sender, SizeChangedEventArgs e) { ResizeIntervals(e.NewSize.Height); } } }