public ActionResult AddMergedTable(FloorTable table, List<long> selectedTables)
        {
            ModelState.Clear();

            try
            {
                var tbl = new MergedFloorTable();

                CopyHelper.Copy(typeof(FloorTable), table, typeof(MergedFloorTable), tbl);

                tbl.TTop = table.TTop = "150px";
                tbl.TLeft = table.TLeft = "36px";
                tbl.TableDesign = " ";
                tbl.IsTemporary = table.IsTemporary = false;
                tbl.CreatedOn = table.CreatedOn = DateTime.UtcNow.ToDefaultTimeZone(User.Identity.GetDatabaseName());
                tbl.CreatedBy = User.Identity.GetUserId<long>();

                var originalTables = db.tabFloorTables.Where(t => selectedTables.Contains(t.FloorTableId)).ToList();

                foreach (var origionalTable in originalTables)
                {
                    var mergedTables = new MergedTableOrigionalTable();
                    mergedTables.MergedFloorTableId = tbl.FloorTableId;
                    mergedTables.FloorTableId = origionalTable.FloorTableId;

                    db.tabMergedTableOrigionalTables.Add(mergedTables);
                }

                db.tabMergedFloorTables.Add(tbl);
                db.SaveChanges();

                table.FloorTableId = tbl.FloorTableId;

                ViewBag.SelectedTables = selectedTables;

                tbl.TableDesign = this.RenderPartialViewToString("~/Views/MergeTable/MergedTableTemplate.cshtml", table);

                return Json(new
                {
                    Status = ResponseStatus.Success,
                    ItemId = tbl.FloorTableId,
                    Template = tbl.TableDesign,
                    TablesToRemove = string.Join(",#", originalTables.Select(t => t.HtmlId))
                });
            }
            catch (Exception)
            {
                return Json(new { Status = ResponseStatus.Fail, ItemId = 0 });
            }
        }
        public ActionResult AddMergedTableNew(List<long> selectedTables, int covers, int? PIN)
        {
            ModelState.Clear();

            var tbl = new MergedFloorTable();

            var tableName = string.Empty;
            var tableMinCovers = 0;
            var tableMaxCovers = 0;
            var floorId = 0L;

            var tables = db.tabFloorTables.Where(t => selectedTables.Contains(t.FloorTableId)).ToList();

            foreach (var table in tables)
            {
                tableName += table.TableName + " & ";
                tableMinCovers += table.MinCover;
                tableMaxCovers += table.MaxCover;
            }

            tableName = tableName.Remove(tableName.Length - 3);
            floorId = tables.First().FloorPlanId;

            //if (tableMaxCovers < covers)
            //{
            //    return Json(new
            //    {
            //        Status = ResponseStatus.Fail,
            //        ItemId = 0,
            //        Message = "Max Covers should be more than " + covers + "."
            //    });
            //}
            //else if (tableMinCovers > covers)
            //{
            //    return Json(new
            //    {
            //        Status = ResponseStatus.Fail,
            //        ItemId = 0,
            //        Message = "Min Covers should be less than or equal to " + covers + "."
            //    });
            //}

            tbl.FloorPlanId = floorId;
            tbl.HtmlId = "table" + Guid.NewGuid().ToString("N");
            tbl.TableName = tableName;
            tbl.Angle = 0;
            tbl.MinCover = tableMinCovers;
            tbl.MaxCover = tableMaxCovers;
            tbl.Size = "random";
            tbl.Shape = "RANDOM";
            tbl.TTop = "0px";
            tbl.TLeft = "0px";
            tbl.TableDesign = " ";
            tbl.IsTemporary = false;
            tbl.CreatedOn = DateTime.UtcNow.ToDefaultTimeZone(User.Identity.GetDatabaseName());
            tbl.CreatedBy = User.Identity.GetUserId<long>();

            var originalTables = db.tabFloorTables.Where(t => selectedTables.Contains(t.FloorTableId)).ToList();

            foreach (var origionalTable in originalTables)
            {
                var mergedTables = new MergedTableOrigionalTable();
                mergedTables.MergedFloorTableId = tbl.FloorTableId;
                mergedTables.FloorTableId = origionalTable.FloorTableId;

                db.tabMergedTableOrigionalTables.Add(mergedTables);
            }

            db.tabMergedFloorTables.Add(tbl);
            db.SaveChanges();

            ViewBag.SelectedTables = originalTables;

            return Json(new
            {
                Status = ResponseStatus.Success,
                ItemId = tbl.FloorTableId,
                PIN = PIN
            });
        }
        public ActionResult UpdateMergedTableNew(Int64 MergedFloorTableId, List<long> selectedTables, int covers, int? PIN)
        {
            ModelState.Clear();
            var tableName = string.Empty;
            var tableMinCovers = 0;
            var tableMaxCovers = 0;

            var tables = db.tabFloorTables.Where(t => selectedTables.Contains(t.FloorTableId)).ToList();

            foreach (var table in tables)
            {
                tableName += table.TableName + " & ";
                tableMinCovers += table.MinCover;
                tableMaxCovers += table.MaxCover;
            }

            tableName = tableName.Remove(tableName.Length - 3);

            //if (tableMaxCovers < covers)
            //{
            //    return Json(new
            //    {
            //        Status = ResponseStatus.Fail,
            //        ItemId = 0,
            //        Message = "Max Covers should be more than " + covers + "."
            //    });
            //}
            //else if (tableMinCovers > covers)
            //{
            //    return Json(new
            //    {
            //        Status = ResponseStatus.Fail,
            //        ItemId = 0,
            //        Message = "Min Covers should be less than or equal to " + covers + "."
            //    });
            //}

            var tbl = db.tabMergedFloorTables.Find(MergedFloorTableId);
            tbl.TableName = tableName;
            tbl.MinCover = tableMinCovers;
            tbl.MaxCover = tableMaxCovers;

            var mtblMap = tbl.OrigionalTables.ToList();
            var mtblTables = mtblMap.Select(mt => mt.FloorTable).ToList();

            foreach (var origionalTable in tables)
            {
                if (!mtblTables.Contains(origionalTable))
                {
                    var mergedTables = new MergedTableOrigionalTable();
                    mergedTables.MergedFloorTableId = tbl.FloorTableId;
                    mergedTables.FloorTableId = origionalTable.FloorTableId;

                    db.tabMergedTableOrigionalTables.Add(mergedTables);
                }
            }

            foreach (var oldMergedtable in mtblTables)
            {
                if (!tables.Contains(oldMergedtable))
                {
                    db.tabMergedTableOrigionalTables.Remove(mtblMap.Where(ot => ot.FloorTableId == oldMergedtable.FloorTableId).Single());
                }
            }

            db.Entry(tbl).State = EntityState.Modified;
            db.SaveChanges();

            ViewBag.SelectedTables = tables;

            return Json(new
            {
                Status = ResponseStatus.Success,
                ItemId = tbl.FloorTableId,
                PIN = PIN
            });
        }