private void btnNew_Click(object sender, EventArgs e)
        {
            _Item       = null;
            _FileToLoad = null;
            GotoReadOnly();

            btnUpload.Enabled = true;

            cbBuilding.Enabled   = true;
            dtpEventDate.Enabled = true;
            dtpEventTime.Enabled = true;
            cbEvent.Enabled      = true;
            cbRoom.Enabled       = true;
            tbVenue.Enabled      = true;

            btnSave.Visible   = true;
            btnCancel.Visible = true;

            btnNew.Visible  = false;
            dgItems.Enabled = false;

            dtpEventDate.Value = DateTime.Today;
            dtpEventTime.Value = DateTime.Now;

            dtpEventToDate.Value = DateTime.Today;
            dtpEventToTime.Value = DateTime.Now;

            dtpEventToDate.Enabled = true;
            dtpEventToTime.Enabled = true;

            cbNotifyTrustees.Enabled = true;
            tbBCC.Enabled            = true;
            tbSubject.Enabled        = true;
            tbBodyContent.Enabled    = true;
        }
        private void LoadGrid()
        {
            if (_DisableGridLoad)
            {
                return;
            }

            _Data = null;
            _Item = null;
            LoadCalendarData();
            GotoReadOnly();
        }
        private bool CheckDoubleBooking(DataContext context, BuildingCalendarEntry editItem)
        {
            //first find out which rooms to load.
            if (editItem.MeetingRoomId == null)
            {
                return(false);
            }

            var roomList = context.MeetingRoomSet.ToList();

            var selectedRoom = roomList.Where(a => a.id == editItem.MeetingRoomId).FirstOrDefault();

            if (selectedRoom == null)
            {
                return(false);
            }

            if (selectedRoom.Name.Contains("A-B-C")) //we selected room A B and C
            {
                foreach (var room in roomList)
                {
                    if (HasMeetingOverlap(context, room, editItem.EntryDate, editItem.EventToDate))
                    {
                        Controller.HandleError("Meeting double booked for room " + room.ToString(), "Validation Error");
                        return(true);
                    }
                }
            }
            else if (selectedRoom.Name.Contains("B-C"))
            {
                foreach (var room in roomList.Where(a => a.Name.Contains("B") || a.Name.Contains("C")))
                {
                    if (HasMeetingOverlap(context, room, editItem.EntryDate, editItem.EventToDate))
                    {
                        Controller.HandleError("Meeting double booked for room " + selectedRoom.ToString(), "Validation Error");
                        return(true);
                    }
                }
            }
            else
            {
                if (HasMeetingOverlap(context, selectedRoom, editItem.EntryDate, editItem.EventToDate))
                {
                    Controller.HandleError("Meeting double booked for room " + selectedRoom.ToString(), "Validation Error");
                    return(true);
                }
            }
            return(false);
        }
Example #4
0
        private void ScheduleBuildingMeeting(tblBuilding building)
        {
            //find a calendare entry for next month.

            DateTime dt      = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            var      dtStart = dt.AddMonths(1); // next month
            var      dtEnd   = dt.AddMonths(2).AddDays(-1);

            var entryCount = _Context.BuildingCalendarEntrySet.Where(a => a.BuildingId == building.id &&
                                                                     a.EventToDate >= dtStart &&
                                                                     a.EventToDate <= dtEnd &&
                                                                     a.Event == building.FinancialMeetingEvent).Count();

            if (entryCount > 0)
            {
                return; //already scheduled
            }
            //find the day to use

            var day = building.FinancialMeetingDayOfMonth;

            if (day == null)
            {
                return;
            }

            DateTime meetingDate = dtStart;

            try
            {
                meetingDate = new DateTime(dtStart.Year, dtStart.Month, day.Value);
            }
            catch
            {
                meetingDate = dtEnd; //must be end of month gone past the end of the month
            }

            //now find closest work day.
            var workDay = ClosestWorkDay(meetingDate);

            //find allocated PM
            var monthFin = _Context.tblMonthFins.Where(a => a.buildingID == building.Code && a.findate == dtStart).FirstOrDefault();

            if (monthFin == null)
            {
                DoRandomAllocations(dtStart);
            }


            monthFin = _Context.tblMonthFins.Where(a => a.buildingID == building.Code && a.findate == dtStart).FirstOrDefault();
            if (monthFin != null)
            {
                var meetingStart = new DateTime(workDay.Year, workDay.Month, workDay.Day, building.FinancialMeetingStartTime.Value.Hour, building.FinancialMeetingStartTime.Value.Minute, 1);
                var meetingEnd   = new DateTime(workDay.Year, workDay.Month, workDay.Day, building.FinancialMeetingEndTime.Value.Hour, building.FinancialMeetingEndTime.Value.Minute, 1);
                //Schedule the meeting
                var calEntry = new BuildingCalendarEntry()
                {
                    CalendarEntryType = CalendarEntryType.Financial,
                    BuildingId        = building.id,
                    UserId            = monthFin.userID,
                    EntryDate         = meetingStart,
                    EventToDate       = meetingEnd,
                    Event             = building.FinancialMeetingEvent,
                    Venue             = building.FinancialMeetingVenue,
                    NotifyTrustees    = building.FinancialMeetingSendInviteToAllTrustees,
                    BCCEmailAddress   = building.FinancialMeetingBCC,
                    InviteSubject     = building.FinancialMeetingSubject,
                    InviteBody        = building.FinancialMeetingBodyText
                };
                _Context.BuildingCalendarEntrySet.Add(calEntry);
                _Context.SaveChanges();
                SendInviteOut(calEntry);
            }
        }
Example #5
0
        private void SendInviteOut(BuildingCalendarEntry entry)
        {
            string status;
            var    pm       = _Context.tblUsers.Single(a => a.id == entry.UserId);
            var    building = _Context.tblBuildings.Single(a => a.id == entry.BuildingId.Value);

            if (string.IsNullOrWhiteSpace(pm.email))
            {
                return;
            }

            var calendarInvite = CreateCalendarInvite(building.Building + " - " + entry.Event, "", entry.Venue, entry.EntryDate, entry.EventToDate);
            Dictionary <string, byte[]> attachments = new Dictionary <string, byte[]>();

            attachments.Add("Appointment.ics", calendarInvite);
            string subject = entry.InviteSubject;

            if (String.IsNullOrWhiteSpace(subject))
            {
                subject = building.Building + " - " + entry.Event;
            }

            string bodyContent = entry.InviteBody;

            if (String.IsNullOrWhiteSpace(bodyContent))
            {
                bodyContent = "";
            }

            bodyContent = bodyContent + Environment.NewLine + Environment.NewLine;

            bodyContent += "Kind Regards" + Environment.NewLine;
            bodyContent += pm.name + Environment.NewLine;
            bodyContent += "Tel: 011 867 3183" + Environment.NewLine;
            bodyContent += "Fax: 011 867 3163" + Environment.NewLine;
            bodyContent += "Direct Fax: 086 657 6199" + Environment.NewLine;
            bodyContent += "BEE Level 4 Contributor" + Environment.NewLine;

            bodyContent += "FOR AND ON BEHALF OF ASTRODON(PTY) LTD" + Environment.NewLine;
            bodyContent += "The information contained in this communication is confidential and may be legally privileged.It is intended solely for the use of the individual or entity to whom it is addressed and others authorized to receive it.If you are not the intended recipient you are hereby notified that any disclosure, copying, distribution or taking action in reliance of the contents of this information is strictly prohibited and may be unlawful.The company is neither liable for proper, complete transmission of the information contained in this communication nor any delay in its receipt." + Environment.NewLine;

            string bccEmail = entry.BCCEmailAddress;

            if (bccEmail == string.Empty)
            {
                bccEmail = null;
            }

            //add aditional attachments
            var attach = _Context.CalendarEntryAttachmentSet.Where(a => a.BuildingCalendarEntryId == entry.id).ToList();

            foreach (var a in attach)
            {
                attachments.Add(a.FileName, a.FileData);
            }

            List <string> toAddress = new List <string>();

            toAddress.Add(pm.email);


            if (!Mailer.SendMailWithAttachments("*****@*****.**", toAddress.Distinct().ToArray(),
                                                subject, bodyContent,
                                                false, false, false, out status, attachments, bccEmail))
            {
                Console.WriteLine("Error seding email " + status, "Email error");
            }

            //disabled until tested 100%

            /*
             * if (entry.NotifyTrustees && entry.CalendarEntryType == CalendarEntryType.Financial)
             * {
             *  var customers = Controller.pastel.AddCustomers(entry.BuildingAbreviation, entry.BuildingDataPath);
             *  var trustees = customers.Where(a => a.IsTrustee).ToList();
             *  if (trustees.Count() > 0 && Controller.AskQuestion("Are you sure you want to send the invite to " + trustees.Count().ToString() + " trustees?"))
             *  {
             *      foreach (var trustee in trustees)
             *      {
             *          if (trustee.Email != null && trustee.Email.Length > 0)
             *          {
             *              if (!Mailer.SendMailWithAttachments("*****@*****.**", trustee.Email,
             *                   subject, bodyContent, false, false, false, out status, attachments, bccEmail))
             *              {
             *                  Controller.HandleError("Error seding email " + status, "Email error");
             *              }
             *          }
             *      }
             *
             *      var itm = context.BuildingCalendarEntrySet.Single(a => a.id == entry.Id);
             *      itm.TrusteesNotified = true;
             *      entry.TrusteesNotified = true;
             *      context.SaveChanges();
             *      BindDataGrid();
             *  }
             * }*/
        }
        private void EditItem(int id)
        {
            _DisableGridLoad = true;
            try
            {
                using (var context = SqlDataHandler.GetDataContext())
                {
                    _Item = context.BuildingCalendarEntrySet.Single(a => a.id == id);
                    if (_Item.CalendarEntryType == CalendarEntryType.Financial)
                    {
                        rbFinancial.Checked = true;
                    }
                    else
                    {
                        rbStaff.Checked = true;
                    }
                    _FileToLoad = null;

                    cbBuilding.SelectedItem = _Buildings.Where(a => a.ID == _Item.BuildingId);
                    dtpEventDate.Value      = _Item.EntryDate;
                    dtpEventTime.Value      = _Item.EntryDate;
                    if (_Item != null)
                    {
                        dtpEventToDate.Value = _Item.EventToDate;
                        dtpEventToTime.Value = _Item.EventToDate;
                    }

                    cbEvent.Text             = _Item.Event;
                    tbVenue.Text             = _Item.Venue;
                    cbNotifyTrustees.Checked = _Item.NotifyTrustees;
                    tbBCC.Text         = _Item.BCCEmailAddress;
                    tbSubject.Text     = _Item.InviteSubject;
                    tbBodyContent.Text = _Item.InviteBody;

                    if (_Item.MeetingRoomId != null)
                    {
                        cbRoom.SelectedValue = _MeetingRooms.Where(a => a.Id == _Item.MeetingRoomId).FirstOrDefault();
                    }
                    else
                    {
                        cbRoom.SelectedIndex = -1;
                    }


                    var attachmentName = context.CalendarEntryAttachmentSet.Where(a => a.BuildingCalendarEntryId == id).Select(a => a.FileName).FirstOrDefault();

                    if (!String.IsNullOrWhiteSpace(attachmentName))
                    {
                        tbAttachment.Text = attachmentName;
                    }
                    else
                    {
                        tbAttachment.Text = string.Empty;
                    }

                    var usrInvates = context.CalendarUserInviteSet.Where(a => a.CalendarEntryId == id).ToList();
                    for (int x = 0; x < cbUserInvites.Items.Count; x++)
                    {
                        var usr = cbUserInvites.Items[x] as tblUser;
                        var c   = usrInvates.Where(a => a.UserId == usr.id).FirstOrDefault();
                        if (c != null)
                        {
                            cbUserInvites.SetItemChecked(x, true);
                        }
                    }


                    btnUpload.Enabled = true;

                    cbBuilding.Enabled   = true;
                    dtpEventDate.Enabled = true;
                    dtpEventTime.Enabled = true;
                    cbEvent.Enabled      = true;
                    tbVenue.Enabled      = true;
                    cbRoom.Enabled       = true;

                    dtpEventToDate.Enabled = true;
                    dtpEventToTime.Enabled = true;

                    cbNotifyTrustees.Enabled = true;
                    tbBCC.Enabled            = true;
                    tbSubject.Enabled        = true;
                    tbBodyContent.Enabled    = true;


                    btnSave.Visible   = true;
                    btnCancel.Visible = true;

                    btnNew.Visible        = false;
                    dgItems.Enabled       = false;
                    cbUserInvites.Enabled = true;


                    if (rbStaff.Checked)
                    {
                        cbBuilding.Visible       = false;
                        cbPM.Visible             = false;
                        cbFilterPM.Visible       = false;
                        label3.Visible           = false;
                        cbUserInvites.Visible    = true;
                        label4.Visible           = false;
                        label8.Visible           = false;
                        cbNotifyTrustees.Visible = false;
                        cbNotifyTrustees.Checked = false;
                    }
                    else
                    {
                        cbBuilding.Visible    = true;
                        cbPM.Visible          = true;
                        cbFilterPM.Visible    = true;
                        label3.Visible        = true;
                        cbUserInvites.Visible = false;

                        label4.Visible           = true;
                        label8.Visible           = true;
                        cbNotifyTrustees.Visible = true;
                    }
                }
            }
            finally
            {
                _DisableGridLoad = false;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            Building selectedBuilding = null;

            if (String.IsNullOrWhiteSpace(cbEvent.Text) ||
                String.IsNullOrWhiteSpace(tbVenue.Text)
                )
            {
                Controller.HandleError("Please complete all fields", "Validation Error");
                return;
            }

            if (rbFinancial.Checked)
            {
                selectedBuilding = (cbBuilding.SelectedItem as Building);
                if (selectedBuilding == null)
                {
                    Controller.HandleError("Building required", "Validation Error");
                    return;
                }
            }
            var minTime = new TimeSpan(05, 00, 00);
            var maxTime = new TimeSpan(23, 59, 00);

            if (dtpEventTime.Value.TimeOfDay < minTime || dtpEventTime.Value.TimeOfDay > maxTime)
            {
                Controller.HandleError("Event time must be > 05:00 and < 23:59", "Validation Error");
                return;
            }


            if (dtpEventToTime.Value.TimeOfDay < minTime || dtpEventToTime.Value.TimeOfDay > maxTime)
            {
                Controller.HandleError("Event time must be > 05:00 and < 23:59", "Validation Error");
                return;
            }

            if (dtpEventDate.Value.Date < DateTime.Today)
            {
                Controller.HandleError("Cannot schedule an event in the past.", "Validation Error");
                return;
            }

            if (!string.IsNullOrWhiteSpace(tbBCC.Text))
            {
                if (!tbBCC.Text.Contains("@") || !tbBCC.Text.Contains("."))
                {
                    Controller.HandleError("BCC not a valid email address.", "Validation Error");
                    return;
                }
            }

            DateTime fromDate = dtpEventDate.Value.Date + dtpEventTime.Value.TimeOfDay;
            DateTime toDate   = dtpEventToDate.Value.Date + dtpEventToTime.Value.TimeOfDay;

            if (fromDate >= toDate)
            {
                Controller.HandleError("To Date/Time cannot be more than the From Date/Time.", "Validation Error");
                return;
            }

            using (var context = SqlDataHandler.GetDataContext())
            {
                BuildingCalendarEntry editItem = null;
                if (_Item == null)
                {
                    editItem = new BuildingCalendarEntry();
                    context.BuildingCalendarEntrySet.Add(editItem);
                }
                else
                {
                    editItem = context.BuildingCalendarEntrySet.Include(a => a.UserInvites).Single(a => a.id == _Item.id);
                    foreach (var itm in editItem.UserInvites.ToList())
                    {
                        context.CalendarUserInviteSet.Remove(itm);
                    }
                }

                if (selectedBuilding != null)
                {
                    editItem.BuildingId = selectedBuilding.ID;
                }
                else
                {
                    editItem.BuildingId = null;
                }
                if (rbStaff.Checked)
                {
                    editItem.CalendarEntryType = CalendarEntryType.Staff;
                }
                else
                {
                    editItem.CalendarEntryType = CalendarEntryType.Financial;
                }

                if (rbFinancial.Checked)
                {
                    var pm = (from b in context.tblBuildings
                              join u in context.tblUsers on b.pm equals u.email
                              where b.id == editItem.BuildingId
                              select u).SingleOrDefault();

                    if (pm != null)
                    {
                        editItem.UserId = pm.id;
                    }
                    else if (editItem.id == 0)
                    {
                        editItem.UserId = Controller.user.id;
                    }
                    editItem.UserInvites = new List <CalendarUserInvite>();
                }
                else
                {
                    editItem.UserId      = Controller.user.id;
                    editItem.UserInvites = new List <CalendarUserInvite>();
                    foreach (var checkedItem in cbUserInvites.CheckedItems)
                    {
                        var usr = (tblUser)checkedItem;
                        editItem.UserInvites.Add(new CalendarUserInvite()
                        {
                            CalendarEntry = editItem,
                            UserId        = usr.id
                        });
                    }
                }

                editItem.EntryDate       = fromDate;
                editItem.EventToDate     = toDate;
                editItem.Event           = cbEvent.Text;
                editItem.Venue           = tbVenue.Text;
                editItem.NotifyTrustees  = cbNotifyTrustees.Checked;
                editItem.InviteSubject   = tbSubject.Text;
                editItem.BCCEmailAddress = tbBCC.Text;
                editItem.InviteBody      = tbBodyContent.Text;

                if (cbRoom.SelectedItem != null)
                {
                    editItem.MeetingRoomId = (cbRoom.SelectedItem as IdValue).Id;
                }
                else
                {
                    editItem.MeetingRoomId = null;
                }

                if (CheckDoubleBooking(context, editItem))
                {
                    return;
                }

                //check for attachments
                if (_FileToLoad != null)
                {
                    CalendarEntryAttachment fileItem = null;

                    if (editItem.id > 0)
                    {
                        fileItem = context.CalendarEntryAttachmentSet.Where(a => a.BuildingCalendarEntryId == editItem.id).FirstOrDefault();
                    }

                    if (fileItem == null)
                    {
                        fileItem = new CalendarEntryAttachment()
                        {
                            CalendarEntry = editItem
                        };
                        context.CalendarEntryAttachmentSet.Add(fileItem);
                    }

                    fileItem.FileData = _FileToLoad;
                    fileItem.FileName = tbAttachment.Text;
                }

                context.SaveChanges();
            }

            LoadGrid();
        }