private void AssetGrid_SaveEvent(object sender, EventArgs e)
        {
            using (var db = new AMSDB())
            {
                var dbassets = (from a in db.Assets
                                where a.Department == User.Department
                                select a).OrderBy(n => n.Name);

                foreach (Asset a in dbassets)
                {
                    foreach (Asset b in Assets)
                    {
                        if (a.ID == b.ID)
                        {
                            b.Update();
                            a.AttendedOn = b.AttendedOn;
                            break;
                        }
                    }
                }
                this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                            new Action(() =>
                {
                    AssetGrid.AssetDataGrid.DataContext = null;
                    AssetGrid.AssetDataGrid.DataContext = Assets;

                    MessageBox.Show("Assets Updated", "Update Info", MessageBoxButton.OK, MessageBoxImage.Information);
                }));
                db.SaveChanges();
            }
        }
        private void AddAssetControl_SaveEvent(object sender, Asset e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                        new Action(() =>
            {
                if (User != null)
                {
                    e.Department = User.Department;
                }

                using (var db = new AMSDB())
                {
                    if (db.Assets.Add(e) == null)
                    {
                        MessageBox.Show("Error Saving Asset. Please Retry", "Database Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        db.SaveChanges();
                        MessageBox.Show("Asset Saved", "Save Success", MessageBoxButton.OK, MessageBoxImage.Information);
                        Assets.Add(e);
                    }
                }
            }));
        }
        private void Login_LoginEvent(object sender, LoginArgs e)
        {
            using (var db = new AMSDB())
            {
                var users = from u in db.Users
                            where u.Username.Equals(e.Username) && u.Password.Equals(e.Password)
                            select u;
                User = users.SingleOrDefault();

                if (User == null)
                {
                    MessageBox.Show("Invalid Username / Password", "Authentication Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                IEnumerable <Asset> assets = null;

                if (User.Department == "Admin")
                {
                    assets = from a in db.Assets
                             select a;
                }
                else
                {
                    assets = (from a in db.Assets
                              where a.Department == User.Department
                              select a).OrderBy(n => n.Name);
                }


                Assets = new ObservableCollection <Asset>();

                foreach (Asset a in assets)
                {
                    a.Update();
                    Assets.Add(a);
                }

                this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                            new Action(() =>
                {
                    AssetGrid = new AssetGrid();
                    AssetGrid.AssetDataGrid.DataContext = Assets;
                    AssetGrid.AddEvent    += AssetGrid_AddEvent;
                    AssetGrid.DeleteEvent += AssetGrid_DeleteEvent;
                    AssetGrid.SaveEvent   += AssetGrid_SaveEvent;
                    MainGrid.Children.Clear();
                    MainGrid.Children.Add(AssetGrid);
                }));
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            DateTime reportingDate = DateTime.Now;

            if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday || ((DateTime.Now.Month == 10) && (DateTime.Now.Day == 2)))
            {
                return;
            }

            List <Receiver> Receivers = new List <Receiver>();

            //Receivers.Add(new Receiver { Email = "*****@*****.**", Level = 1 });
            //Receivers.Add(new Receiver { Email = "*****@*****.**", Level = 2 });
            //Receivers.Add(new Receiver { Email = "*****@*****.**", Level = 3 });
            Receivers.Add(new Receiver {
                Email = "*****@*****.**", Level = 1
            });

            List <Asset> Critical = new List <Asset>();
            List <Asset> High     = new List <Asset>();
            List <Asset> Normal   = new List <Asset>();

            String MailBody = String.Empty;


            using (var db = new AMSDB())
            {
                var assets = from a in db.Assets
                             where a.Department == "Maintenance"
                             select a;

                foreach (Asset a in assets)
                {
                    if ((a.AttendedOn.Value.AddDays(a.AttentionInterval.Value + 1) - DateTime.Now).Days < 0)
                    {
                        Critical.Add(a);
                    }



                    else if ((a.AttendedOn.Value.AddDays(a.AttentionInterval.Value + 1) - DateTime.Now).Days < 7)
                    {
                        High.Add(a);
                    }

                    else if ((a.AttendedOn.Value.AddDays(a.AttentionInterval.Value + 1) - DateTime.Now).Days < a.AlertInterval.Value)
                    {
                        Normal.Add(a);
                    }
                }

                if (Critical.Count > 0)
                {
                    List <Receiver> rx = new List <Receiver>();
                    MailBody = "<h1> CRITICAL PRIORITY </h1>" +
                               " <table border=" + 1 + " cellpadding=" + 0 + " cellspacing=" + 0 + " width = " + 400 + ">" +
                               "<tr bgcolor='#4da6ff'><th>Machine No</th><th> Location </th><th>Due Date</th></tr>";

                    foreach (Asset a in Critical)
                    {
                        MailBody += "<tr><td>" + "  " + a.Tag + "</td>"
                                    + "<td>  " + "  " + a.Location + "</td>"
                                    + "<td>  " + "  " + a.AttendedOn.Value.AddDays(a.AttentionInterval.Value + 1).ToShortDateString() + "</td>"
                                    + "</tr>";
                    }

                    foreach (Receiver r in Receivers)
                    {
                        if (r.Level == 3)
                        {
                            rx.Add(r);
                        }
                    }

                    MailBody += "</table>";

                    if (rx.Count > 0)
                    {
                        SendMail(MailBody, rx);
                    }
                }

                if (High.Count > 0)
                {
                    List <Receiver> rx = new List <Receiver>();
                    MailBody += "<h1> High PRIORITY </h1>" +
                                " <table border=" + 1 + " cellpadding=" + 0 + " cellspacing=" + 0 + " width = " + 400 + ">" +
                                "<tr bgcolor='#4da6ff'><th>Machine No</th><th> Location </th><th>Due Date</th></tr>";

                    foreach (Asset a in High)
                    {
                        MailBody += "<tr><td>" + "  " + a.Tag + "</td>"
                                    + "<td>  " + "  " + a.Location + "</td>"
                                    + "<td>  " + "  " + a.AttendedOn.Value.AddDays(a.AttentionInterval.Value + 1).ToShortDateString() + "</td>"
                                    + "</tr>";
                    }

                    foreach (Receiver r in Receivers)
                    {
                        if (r.Level == 2)
                        {
                            rx.Add(r);
                        }
                    }
                    MailBody += "</table>";
                    if (rx.Count > 0)
                    {
                        SendMail(MailBody, rx);
                    }
                }

                if (Normal.Count > 0)
                {
                    List <Receiver> rx = new List <Receiver>();
                    MailBody += "<h1> PRIORITY </h1>" +
                                " <table border=" + 1 + " cellpadding=" + 0 + " cellspacing=" + 0 + " width = " + 400 + ">" +
                                "<tr bgcolor='#4da6ff'><th>Machine No</th><th> Location </th><th>Due Date</th></tr>";

                    foreach (Asset a in Normal)
                    {
                        MailBody += "<tr><td>" + "  " + a.Tag + "</td>"
                                    + "<td>  " + "  " + a.Location + "</td>"
                                    + "<td>  " + "  " + a.AttendedOn.Value.AddDays(a.AttentionInterval.Value + 1).ToShortDateString() + "</td>"
                                    + "</tr>";
                    }

                    foreach (Receiver r in Receivers)
                    {
                        if (r.Level == 1)
                        {
                            rx.Add(r);
                        }
                    }

                    MailBody += "</table>";
                    if (rx.Count > 0)
                    {
                        SendMail(MailBody, rx);
                    }
                }
            }
        }