예제 #1
0
        private void DoRandomAllocations(DateTime dtStart)
        {
            var dt      = new DateTime(dtStart.Year, dtStart.Month, 1);
            var context = _Context;

            //create a list of building id and user id
            var userIdList = context.tblUsers
                             .Where(a => a.ProcessCheckLists == true && a.Active == true)
                             .Select(a => a.id).ToList();

            var buildingCodeList = context.tblBuildings.Where(a => a.BuildingFinancialsEnabled == true && a.BuildingDisabled == false).Select(a => a.Code).ToList();

            Dictionary <string, int> randomAllocations = new Dictionary <string, int>();

            //load the current allocations
            foreach (var itm in context.tblMonthFins.Where(a => a.findate == dt)
                     .Select(a => new { a.buildingID, a.userID }).ToList())
            {
                randomAllocations.Add(itm.buildingID, itm.userID);
                if (buildingCodeList.Contains(itm.buildingID))
                {
                    buildingCodeList.Remove(itm.buildingID);
                }
            }

            //get last months allocations
            Dictionary <string, int> lastMonthsAllocations = new Dictionary <string, int>();
            var lastMonth = dt.AddMonths(-1);

            foreach (var itm in context.tblMonthFins.Where(a => a.findate == lastMonth)
                     .Select(a => new { a.buildingID, a.userID }).ToList())
            {
                lastMonthsAllocations.Add(itm.buildingID, itm.userID);
            }

            var userProcesslist = userIdList.ToList();
            var random          = new Random();

            //bublle through buildings to build random dictionary
            #region Random Allocations
            while (buildingCodeList.Count > 0)
            {
                int userId       = 0;
                var buildingCode = buildingCodeList[0];

                if (userProcesslist.Count > 1)
                {
                    var idx = random.Next(0, userProcesslist.Count);

                    userId = userProcesslist[idx];
                    userProcesslist.Remove(userId);
                }
                else
                {
                    if (userProcesslist.Count == 1)
                    {
                        userId = userProcesslist[0];
                        userProcesslist.Remove(userId);
                    }
                }

                if (userProcesslist.Count < 1)
                {
                    userProcesslist = userIdList.ToList();
                }

                if (userId > 0)
                {
                    if (lastMonthsAllocations.ContainsKey(buildingCode) && lastMonthsAllocations[buildingCode] == userId)
                    {
                        if (userProcesslist.Count > 1)
                        {
                            var idx       = random.Next(0, userProcesslist.Count);
                            int newUserId = userProcesslist[idx];
                            userProcesslist.Remove(newUserId);
                            userProcesslist.Add(userId);
                            if (newUserId > 0)
                            {
                                randomAllocations.Add(buildingCode, newUserId);
                                buildingCodeList.Remove(buildingCode);
                            }
                        }
                    }
                    else
                    {
                        if (userId > 0)
                        {
                            randomAllocations.Add(buildingCode, userId);
                            buildingCodeList.Remove(buildingCode);
                        }
                    }
                }
            }


            #endregion

            //Process dictionary to tblMonthFin

            foreach (var key in randomAllocations.Keys)
            {
                var curr = context.tblMonthFins.Where(a => a.buildingID == key && a.findate == dt).SingleOrDefault();
                if (curr == null)
                {
                    curr = new Data.tblMonthFin()
                    {
                        buildingID = key,
                        findate    = dt,
                        finPeriod  = dt.Month,
                        year       = dt.Year
                    };
                    context.tblMonthFins.Add(curr);
                }
                curr.userID = randomAllocations[key];
            }

            context.SaveChanges();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var selectedYear = cmbYear.SelectedItem as IdValue;

            if (selectedYear == null)
            {
                return;
            }

            var selectedMonth = cmbMonth.SelectedItem as IdValue;

            if (selectedMonth == null)
            {
                return;
            }

            var dt = new DateTime((cmbYear.SelectedItem as IdValue).Id, (cmbMonth.SelectedItem as IdValue).Id, 1);


            if (!Controller.AskQuestion("Are you sure you want to create the allocation for " + dt.ToString("MMM yyyy") + "?"))
            {
                return;
            }

            this.Cursor = Cursors.WaitCursor;
            try
            {
                using (var context = SqlDataHandler.GetDataContext())
                {
                    //clear current allocations
                    var currentAllocations = context.tblMonthFins.Where(a => a.completeDate == null && a.findate == dt).ToList();
                    context.tblMonthFins.RemoveRange(currentAllocations);
                    context.SaveChanges();

                    //create a list of building id and user id
                    var userIdList = context.tblUsers
                                     .Where(a => a.ProcessCheckLists == true && a.Active == true)
                                     .Select(a => a.id).ToList();

                    var buildingCodeList = context.tblBuildings.Where(a => a.BuildingFinancialsEnabled == true && a.BuildingDisabled == false).Select(a => a.Code).ToList();

                    Dictionary <string, int> randomAllocations = new Dictionary <string, int>();

                    //load the current allocations
                    foreach (var itm in context.tblMonthFins.Where(a => a.findate == dt)
                             .Select(a => new { a.buildingID, a.userID }).ToList())
                    {
                        randomAllocations.Add(itm.buildingID, itm.userID);
                        if (buildingCodeList.Contains(itm.buildingID))
                        {
                            buildingCodeList.Remove(itm.buildingID);
                        }
                    }

                    //get last months allocations
                    Dictionary <string, int> lastMonthsAllocations = new Dictionary <string, int>();
                    var lastMonth = dt.AddMonths(-1);
                    foreach (var itm in context.tblMonthFins.Where(a => a.findate == lastMonth)
                             .Select(a => new { a.buildingID, a.userID }).ToList())
                    {
                        lastMonthsAllocations.Add(itm.buildingID, itm.userID);
                    }

                    var userProcesslist = userIdList.ToList();
                    var random          = new Random();
                    //bublle through buildings to build random dictionary
                    #region Random Allocations
                    while (buildingCodeList.Count > 0)
                    {
                        int userId       = 0;
                        var buildingCode = buildingCodeList[0];

                        if (userProcesslist.Count > 1)
                        {
                            var idx = random.Next(0, userProcesslist.Count);

                            userId = userProcesslist[idx];
                            userProcesslist.Remove(userId);
                        }
                        else
                        {
                            if (userProcesslist.Count == 1)
                            {
                                userId = userProcesslist[0];
                                userProcesslist.Remove(userId);
                            }
                        }

                        if (userProcesslist.Count < 1)
                        {
                            userProcesslist = userIdList.ToList();
                        }

                        if (userId > 0)
                        {
                            if (lastMonthsAllocations.ContainsKey(buildingCode) && lastMonthsAllocations[buildingCode] == userId)
                            {
                                if (userProcesslist.Count > 1)
                                {
                                    var idx       = random.Next(0, userProcesslist.Count);
                                    int newUserId = userProcesslist[idx];
                                    userProcesslist.Remove(newUserId);
                                    userProcesslist.Add(userId);
                                    if (newUserId > 0)
                                    {
                                        randomAllocations.Add(buildingCode, newUserId);
                                        buildingCodeList.Remove(buildingCode);
                                    }
                                }
                            }
                            else
                            {
                                if (userId > 0)
                                {
                                    randomAllocations.Add(buildingCode, userId);
                                    buildingCodeList.Remove(buildingCode);
                                }
                            }
                        }
                    }


                    #endregion

                    //Process dictionary to tblMonthFin

                    foreach (var key in randomAllocations.Keys)
                    {
                        var curr = context.tblMonthFins.Where(a => a.buildingID == key && a.findate == dt).SingleOrDefault();
                        var old  = currentAllocations.Where(a => a.buildingID == key && a.findate == dt).FirstOrDefault();

                        var prev = context.tblMonthFins.Where(a => a.buildingID == key && a.findate < dt).OrderByDescending(a => a.findate).FirstOrDefault();

                        if (curr == null)
                        {
                            curr = new Data.tblMonthFin()
                            {
                                buildingID         = key,
                                findate            = dt,
                                finPeriod          = dt.Month,
                                year               = dt.Year,
                                AdditionalComments = prev != null ? prev.AdditionalComments : null
                            };
                            context.tblMonthFins.Add(curr);
                        }
                        curr.userID = randomAllocations[key];
                    }

                    context.SaveChanges();
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            LoadReport();

            Controller.ShowMessage("Random allocations completed");
        }