Ejemplo n.º 1
0
 public static int InsertEvent(Guid OrgID, int DeptId, int CreatedByUserId, NotificationRules.TicketEvent tktEvent, string ObjectStateNew, string ObjectStateOld, FileItem[] EventFiles, int ScheduledTicketId, DateTime RunTime)
 {
     SqlParameter _pId=new SqlParameter("@Id", SqlDbType.Int);
     _pId.Direction=ParameterDirection.InputOutput;
     _pId.Value=DBNull.Value;
     SqlParameter _pObjectStateNew = new SqlParameter("@ObjectStateNew", SqlDbType.NText);
     if (ObjectStateNew.Length > 0) _pObjectStateNew.Value = ObjectStateNew;
     else _pObjectStateNew.Value = DBNull.Value;
     SqlParameter _pObjectStateOld = new SqlParameter("@ObjectStateOld", SqlDbType.NText);
     if (ObjectStateOld.Length > 0) _pObjectStateOld.Value = ObjectStateOld;
     else _pObjectStateOld.Value = DBNull.Value;
     SqlParameter _pScheduledTicketId = new SqlParameter("@ScheduledTicketId", SqlDbType.Int);
     if (ScheduledTicketId != 0) _pScheduledTicketId.Value = ScheduledTicketId;
     else _pScheduledTicketId.Value = DBNull.Value;
     SqlParameter _pRunTime = new SqlParameter("@RunTime", SqlDbType.SmallDateTime);
     if (RunTime == DateTime.MinValue) _pRunTime.Value = DBNull.Value;
     else _pRunTime.Value = RunTime;
     UpdateData("sp_UpdateNotificationEventsQueue", new SqlParameter[] { _pId, new SqlParameter("@DId", DeptId), new SqlParameter("@CreatedByUserId", CreatedByUserId), new SqlParameter("@EventType", (int)tktEvent), _pObjectStateNew, _pObjectStateOld, _pScheduledTicketId, _pRunTime}, OrgID);
     int _id = (int)_pId.Value;
     if (EventFiles == null) return _id;
     foreach (FileItem _file in EventFiles) InsertFile(OrgID, DeptId, _id, _file);
     return _id;
 }
Ejemplo n.º 2
0
 public static int InsertEvent(Guid OrgID, int DeptId, int CreatedByUserId, NotificationRules.TicketEvent tktEvent, string ObjectState, FileItem[] EventFiles)
 {
     return InsertEvent(OrgID, DeptId, CreatedByUserId, tktEvent, ObjectState, string.Empty, EventFiles, 0, DateTime.MinValue);
 }
Ejemplo n.º 3
0
 public static int InsertEvent(int DeptId, int CreatedByUserId, NotificationRules.TicketEvent tktEvent, string ObjectState)
 {
     return InsertEvent(Guid.Empty, DeptId, CreatedByUserId, tktEvent, ObjectState, string.Empty, null, 0, DateTime.MinValue);
 }
Ejemplo n.º 4
0
 public static int InsertEvent(int DeptId, int CreatedByUserId, NotificationRules.TicketEvent tktEvent, string ObjectState, FileItem[] EventFiles)
 {
     return InsertEvent(Guid.Empty, DeptId, CreatedByUserId, tktEvent, ObjectState, EventFiles);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Forces creation of new ticket from Scheduled ticket
        /// </summary>
        /// <returns>Id of the created ticket</returns>
        public static int RunScheduledTicket(int DepartmentId, int ScheduledTicketId, bool IsManualLaunch, out int initialPostId)
        {
            int error = 0;

            initialPostId = 0;

            CustomNames customNames = CustomNames.GetCustomNames(DepartmentId);

            DataRow rScheduledTicket = SelectOne(DepartmentId, ScheduledTicketId);

            int      ownerId            = 0;
            int      userId             = 0;
            int      technicianId       = 0;
            int      locationId         = 0;
            int      classId            = 0;
            int      priorityId         = 0;
            int      levelId            = 0;
            int      creationCategoryId = 0;
            string   assetSerial        = "";
            string   idMethod           = "";
            string   customXML          = "";
            DateTime now                       = DateTime.UtcNow;
            string   rTicketSubjsect           = "";
            string   rTicketText               = "";
            byte     rTicketEndCount           = 0;
            bool     rTicketEnabled            = false;
            DateTime rTicketNext               = DateTime.MinValue;
            string   rTicketRecurringOn        = "";
            int      rTicketRecurringFrequency = 0;
            string   rTicketEndMethod          = "";
            DateTime rTicketStop               = DateTime.MinValue;
            int      rTicketAccountId          = 0;
            int      rTicketAccountLocationId  = 0;
            int      rProjectId                = 0;
            int      rFolderId                 = 0;


            if (rScheduledTicket["intOwnerId"] != DBNull.Value)
            {
                ownerId = (int)rScheduledTicket["intOwnerId"];
            }

            if (rScheduledTicket["intUserId"] != DBNull.Value)
            {
                userId = (int)rScheduledTicket["intUserId"];
            }

            if (rScheduledTicket["intTechId"] != DBNull.Value)
            {
                technicianId = (int)rScheduledTicket["intTechId"];
            }

            if (rScheduledTicket["LocationId"] != DBNull.Value)
            {
                locationId = (int)rScheduledTicket["LocationId"];
            }

            if (rScheduledTicket["intClassId"] != DBNull.Value)
            {
                classId = (int)rScheduledTicket["intClassId"];
            }

            if (rScheduledTicket["intPriorityId"] != DBNull.Value)
            {
                priorityId = (int)rScheduledTicket["intPriorityId"];
            }

            if (rScheduledTicket["tintLevel"] != DBNull.Value)
            {
                levelId = (byte)rScheduledTicket["tintLevel"];
            }

            if (rScheduledTicket["CreationCatsId"] != DBNull.Value)
            {
                creationCategoryId = (int)rScheduledTicket["CreationCatsId"];
            }

            if (rScheduledTicket["vchAssetSerial"] != DBNull.Value)
            {
                assetSerial = (string)rScheduledTicket["vchAssetSerial"];
            }

            if (rScheduledTicket["vchIdMethod"] != DBNull.Value)
            {
                idMethod = (string)rScheduledTicket["vchIdMethod"];
            }

            if (rScheduledTicket["CustomXML"] != DBNull.Value)
            {
                customXML = (string)rScheduledTicket["CustomXML"];
            }

            if (rScheduledTicket["vchSubject"] != DBNull.Value)
            {
                rTicketSubjsect = (string)rScheduledTicket["vchSubject"];
            }

            if (rScheduledTicket["vchText"] != DBNull.Value)
            {
                rTicketText = (string)rScheduledTicket["vchText"];
            }
            rTicketText += "<br><br>This " + customNames.Ticket.FullSingular + " was created via Scheduled " + customNames.Ticket.FullPlural + ".";

            if (rScheduledTicket["tintEndCount"] != DBNull.Value)
            {
                rTicketEndCount = (byte)rScheduledTicket["tintEndCount"];
            }

            if (rScheduledTicket["btEnabled"] != DBNull.Value)
            {
                rTicketEnabled = (bool)rScheduledTicket["btEnabled"];
            }

            if (rScheduledTicket["dtNext"] != DBNull.Value)
            {
                rTicketNext = (DateTime)rScheduledTicket["dtNext"];
            }

            if (rScheduledTicket["vchRecurringOn"] != DBNull.Value)
            {
                rTicketRecurringOn = (string)rScheduledTicket["vchRecurringOn"];
            }

            if (rScheduledTicket["tintRecurringFeq"] != DBNull.Value)
            {
                rTicketRecurringFrequency = (byte)rScheduledTicket["tintRecurringFeq"];
            }

            if (rScheduledTicket["vchEndMethod"] != DBNull.Value)
            {
                rTicketEndMethod = (string)rScheduledTicket["vchEndMethod"];
            }

            if (rScheduledTicket["dtStop"] != DBNull.Value)
            {
                rTicketStop = (DateTime)rScheduledTicket["dtStop"];
            }

            if (rScheduledTicket["IntAcctId"] != DBNull.Value)
            {
                rTicketAccountId = (int)rScheduledTicket["IntAcctId"];
            }

            if (rScheduledTicket["AccountLocationId"] != DBNull.Value)
            {
                rTicketAccountLocationId = (int)rScheduledTicket["AccountLocationId"];
            }

            if (!rScheduledTicket.IsNull("ProjectID"))
            {
                rProjectId = (int)rScheduledTicket["ProjectID"];
            }

            if (!rScheduledTicket.IsNull("FolderID"))
            {
                rFolderId = (int)rScheduledTicket["FolderID"];
            }


            switch (rTicketEndMethod)
            {
            case "Date":
                if (IsManualLaunch && rTicketStop < rTicketNext)
                {
                    error = -1;
                }
                else if (!IsManualLaunch && rTicketStop < now)
                {
                    error = -1;
                }
                break;

            case "Times":
                if (rTicketEndCount > 0)
                {
                    rTicketEndCount--;
                }
                else
                {
                    error = -2;
                }
                rTicketEnabled = rTicketEndCount > 0;
                break;
            }

            if (rTicketRecurringOn != "no")
            {
                string daysOfWeek      = rTicketRecurringOn.Substring(1);
                int    totalDifference = daysOfWeek.Length > 0 ? Functions.GetWeekdayIndex(rTicketNext) - int.Parse(daysOfWeek[0].ToString()) : 0;

                if (IsManualLaunch)
                {
                    switch (rTicketRecurringOn[0])
                    {
                    case 'd':
                        rTicketNext = rTicketNext.AddDays(rTicketRecurringFrequency);
                        break;

                    case 'w':
                        int differenceToNextDay = DifferenceToNextDayOfWeek(daysOfWeek, Functions.GetWeekdayIndex(rTicketNext));

                        if (differenceToNextDay == -1)
                        {
                            rTicketNext     = rTicketNext.AddDays(rTicketRecurringFrequency * 7 - totalDifference);
                            totalDifference = 0;
                        }
                        else
                        {
                            rTicketNext      = rTicketNext.AddDays(differenceToNextDay);
                            totalDifference += differenceToNextDay;
                        }
                        break;

                    case 'm':
                        rTicketNext = rTicketNext.AddMonths(rTicketRecurringFrequency);
                        break;

                    default:
                        error          = -3;
                        rTicketEnabled = false;
                        break;
                    }
                }
                else
                {
                    while (rTicketNext <= now)
                    {
                        switch (rTicketRecurringOn[0])
                        {
                        case 'd':
                            rTicketNext = rTicketNext.AddDays(rTicketRecurringFrequency);
                            break;

                        case 'w':
                            int differenceToNextDay = DifferenceToNextDayOfWeek(daysOfWeek, Functions.GetWeekdayIndex(rTicketNext));

                            if (differenceToNextDay == -1)
                            {
                                rTicketNext     = rTicketNext.AddDays(rTicketRecurringFrequency * 7 - totalDifference);
                                totalDifference = 0;
                            }
                            else
                            {
                                rTicketNext      = rTicketNext.AddDays(differenceToNextDay);
                                totalDifference += differenceToNextDay;
                            }
                            break;

                        case 'm':
                            rTicketNext = rTicketNext.AddMonths(rTicketRecurringFrequency);
                            break;

                        default:
                            error          = -3;
                            rTicketEnabled = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                rTicketEnabled = false;
            }

            rTicketEnabled = error == 0;

            if (error < 0)
            {
                UpdateDates(DepartmentId, ScheduledTicketId, rTicketEnabled, rTicketNext, rTicketEndCount);
                if (!IsManualLaunch && !rTicketEnabled)
                {
                    Data.NotificationEventsQueue.DeleteEvents(DepartmentId, ScheduledTicketId);
                }
                return(error);
            }

            int newTicketId = Data.Tickets.CreateNew(Guid.Empty, DepartmentId, ownerId, technicianId, userId, DateTime.MinValue, rTicketAccountId, rTicketAccountLocationId, false, locationId,
                                                     classId, levelId, "Scheduled/Recurring Ticket", false, creationCategoryId, true, priorityId, DateTime.MinValue, "", assetSerial, SelectTicketAssets(DepartmentId, ScheduledTicketId), idMethod, customXML,
                                                     rTicketSubjsect, rTicketText, null, "Open", out initialPostId, rProjectId, rFolderId, ScheduledTicketId, -1);

            if (newTicketId > 0)
            {
                UpdateDates(DepartmentId, ScheduledTicketId, rTicketEnabled, rTicketNext, rTicketEndCount);
                if (!IsManualLaunch)
                {
                    FileItem[] fileItems = NotificationEventsQueue.SelectNotificationEventFilesToArray(DepartmentId, ScheduledTicketId);
                    if (rTicketEnabled)
                    {
                        NotificationRules.RaiseNotificationEvent(DepartmentId, userId, ScheduledTicketId, rTicketNext, fileItems);
                    }
                    else
                    {
                        NotificationEventsQueue.DeleteEvents(DepartmentId, ScheduledTicketId);
                    }
                }
            }

            return(newTicketId);
        }