/*
         * Combined Create and Update Store profile method.
         * Creates a Store_Profile instance (which will be a new table row)
         * and instantiates each SPViewModel property to the respective property of the former.
         * Also checks if the given Store profile number is already in the table,
         * if true, the method performs an update, otherwise, creation.
         *
         * Returns true if the operation is successful.
         * */
        public bool UpdateStore(StoreProfileViewModel SPViewModel)
        {
            using (CFMMCDEntities db = new CFMMCDEntities())
            {
                Store_Profile SPRow;
                bool          isUpdating = false;
                if (db.Store_Profile.Where(o => o.STORE_NO.ToString().Equals(SPViewModel.STORE_NO)).Any())
                {
                    SPRow      = db.Store_Profile.Single(o => o.STORE_NO.ToString().Equals(SPViewModel.STORE_NO));
                    isUpdating = true;
                }
                else
                {
                    SPRow = new Store_Profile();
                }

                if (SPViewModel.STORE_NO != null && !SPViewModel.STORE_NO.Equals(""))
                {
                    SPRow.STORE_NO = int.Parse(SPViewModel.STORE_NO);
                }
                else
                {
                    return(false);
                }

                if (SPViewModel.STORE_NAME != null && !SPViewModel.STORE_NAME.Equals(""))
                {
                    SPRow.STORE_NAME = SPViewModel.STORE_NAME;
                }
                else
                {
                    return(false);
                }

                if (SPViewModel.OWNERSHIP != null)
                {
                    SPRow.OWNERSHIP = int.Parse(SPViewModel.OWNERSHIP);
                }
                if (SPViewModel.BREAKFAST_PRICE_TIER != null)
                {
                    SPRow.BREAKFAST_PRICE_TIER = int.Parse(SPViewModel.BREAKFAST_PRICE_TIER);
                }
                if (SPViewModel.REGULAR_PRICE_TIER != null)
                {
                    SPRow.REGULAR_PRICE_TIER = int.Parse(SPViewModel.REGULAR_PRICE_TIER);
                }
                if (SPViewModel.DC_PRICE_TIER != null)
                {
                    SPRow.DC_PRICE_TIER = int.Parse(SPViewModel.DC_PRICE_TIER);
                }
                if (SPViewModel.MDS_PRICE_TIER != null)
                {
                    SPRow.MDS_PRICE_TIER = int.Parse(SPViewModel.MDS_PRICE_TIER);
                }
                if (SPViewModel.MCCAFE_LEVEL_2_PRICE_TIER != null)
                {
                    SPRow.MCCAFE_LEVEL_2_PRICE_TIER = int.Parse(SPViewModel.MCCAFE_LEVEL_2_PRICE_TIER);
                }
                if (SPViewModel.MCCAFE_LEVEL_3_PRICE_TIER != null)
                {
                    SPRow.MCCAFE_LEVEL_3_PRICE_TIER = int.Parse(SPViewModel.MCCAFE_LEVEL_3_PRICE_TIER);
                }
                if (SPViewModel.MCCAFE_BISTRO_PRICE_TIER != null)
                {
                    SPRow.MCCAFE_BISTRO_PRICE_TIER = int.Parse(SPViewModel.MCCAFE_BISTRO_PRICE_TIER);
                }
                if (SPViewModel.PROJECT_GOLD_PRICE_TIER != null)
                {
                    SPRow.PROJECT_GOLD_PRICE_TIER = int.Parse(SPViewModel.PROJECT_GOLD_PRICE_TIER);
                }
                if (SPViewModel.BusinessExtList != null)
                {
                    SPRow.BET = SetBusinessExtention(SPViewModel.BET, SPViewModel.BusinessExtList);
                }
                if (SPViewModel.PROFIT_CENTER != null)
                {
                    SPRow.PROFIT_CENTER = int.Parse(SPViewModel.PROFIT_CENTER);
                }
                if (SPViewModel.REGION != null)
                {
                    SPRow.REGION = SPViewModel.REGION;
                }
                if (SPViewModel.PROVINCE != null)
                {
                    SPRow.PROVINCE = SPViewModel.PROVINCE;
                }
                if (SPViewModel.LOCATION != null && !SPViewModel.LOCATION.Equals(""))
                {
                    SPRow.LOCATION = int.Parse(SPViewModel.LOCATION);
                }
                if (SPViewModel.ADDRESS != null)
                {
                    SPRow.ADDRESS = SPViewModel.ADDRESS;
                }
                if (SPViewModel.CITY != null)
                {
                    SPRow.CITY = SPViewModel.CITY;
                }
                if (SPViewModel.FRESH_OR_FROZEN != null)
                {
                    SPRow.FRESH_OR_FROZEN = SPViewModel.FRESH_OR_FROZEN;
                }
                if (SPViewModel.PAPER_OR_PLASTIC != null)
                {
                    SPRow.PAPER_OR_PLASTIC = SPViewModel.PAPER_OR_PLASTIC;
                }
                if (SPViewModel.SOFT_SERVE_OR_VANILLA_POWDER_MIX != null)
                {
                    SPRow.SOFT_SERVE_OR_VANILLA_POWDER_MIX = SPViewModel.SOFT_SERVE_OR_VANILLA_POWDER_MIX;
                }
                if (SPViewModel.SIMPLOT_OR_MCCAIN != null)
                {
                    SPRow.SIMPLOT_OR_MCCAIN = SPViewModel.SIMPLOT_OR_MCCAIN;
                }
                if (SPViewModel.MCCORMICK_OR_GSF != null)
                {
                    SPRow.MCCORMICK_OR_GSF = SPViewModel.MCCORMICK_OR_GSF;
                }
                if (SPViewModel.FRESHB_OR_FROZENB != null)
                {
                    SPRow.FRESHB_OR_FROZENB = SPViewModel.FRESHB_OR_FROZENB;
                }
                SPRow.STATUS = "A";
                // Group
                if (db.ITMGRPs.Where(o => o.Item_Code.ToString().Equals(SPViewModel.STORE_NO)).Any())
                {
                    if (SPViewModel.Group == 0)
                    {
                        int val = db.ITMGRPs.FirstOrDefault(o => o.Item_Code.ToString().Equals(SPViewModel.STORE_NO)).Id;
                        ItemGroupManager.DeleteItem(val);
                    }
                    else
                    {
                        db.ITMGRPs.Single(o => o.Item_Code.ToString().Equals(SPViewModel.STORE_NO)).Item_Code  = int.Parse(SPViewModel.STORE_NO);
                        db.ITMGRPs.Single(o => o.Item_Code.ToString().Equals(SPViewModel.STORE_NO)).Item_Name  = SPViewModel.STORE_NAME;
                        db.ITMGRPs.Single(o => o.Item_Code.ToString().Equals(SPViewModel.STORE_NO)).Group_Id   = SPViewModel.Group;
                        db.ITMGRPs.Single(o => o.Item_Code.ToString().Equals(SPViewModel.STORE_NO)).Group_Name = db.ITMGRPs.FirstOrDefault(o => o.Group_Id == SPViewModel.Group).Group_Name;
                    }
                }
                else
                {
                    if (SPViewModel.Group != 0)
                    {
                        ItemGroupViewModel IGRow = new ItemGroupViewModel();
                        IGRow.GroupName = db.ITMGRPs.FirstOrDefault(o => o.Group_Id == SPViewModel.Group).Group_Name;
                        IGRow.GroupId   = SPViewModel.Group;
                        IGRow.ItemCode  = int.Parse(SPViewModel.STORE_NO);
                        IGRow.ItemName  = SPViewModel.STORE_NAME;
                        IGRow.ItemType  = 3;
                        IGRow.GroupType = 3;
                        ItemGroupManager.UpdateGroup(IGRow);
                    }
                }
                SPRow.Group = SPViewModel.Group;
                try
                {
                    // Check if STORE_NO already exists in the database, perform an update if true
                    if (isUpdating)
                    {
                        SPRow.STATUS = "E";
                    }
                    else
                    {
                        db.Store_Profile.Add(SPRow);
                    }
                    db.SaveChanges();
                    return(true);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Source);
                    System.Diagnostics.Debug.WriteLine(e.Message);
                    System.Diagnostics.Debug.WriteLine(e.StackTrace);
                    System.Diagnostics.Debug.WriteLine(e.InnerException);
                    Exception f = e.InnerException;
                    while (f != null)
                    {
                        System.Diagnostics.Debug.WriteLine("INNER:");
                        System.Diagnostics.Debug.WriteLine(f.Message);
                        System.Diagnostics.Debug.WriteLine(f.Source);
                        f = f.InnerException;
                    }
                    System.Diagnostics.Debug.WriteLine(e.Data);
                    return(false);
                }
            }
        }
        public static bool UpdateMenuItem(MenuRecipeViewModel MRViewModel, string user)
        {
            using (CFMMCDEntities db = new CFMMCDEntities())
            {
                if (MRViewModel.RIRMIC == null || MRViewModel.RIRMIC.Equals(""))
                {
                    return(false);
                }
                // Existing row
                foreach (var v in MRViewModel.MenuRecipeList)
                {
                    if (!v.PreviousRIRRIC.Equals(v.RIRRIC))
                    {
                        INVRIRP0 MRRowToDelete = db.INVRIRP0.Single(o => o.RIRRID.Equals(v.RIRRID));
                        db.INVRIRP0.Remove(MRRowToDelete);
                        db.SaveChanges();
                    }
                    if (!db.INVRIMP0.Where(o => o.RIMRIC.ToString().Equals(v.RIRRIC)).Any())
                    {
                        continue;
                    }
                    INVRIRP0 MRRow;
                    if (db.INVRIRP0.Where(o => o.RIRRID.Equals(MRViewModel.RIRMIC + v.RIRRIC)).Any())
                    {
                        MRRow = db.INVRIRP0.Single(o => o.RIRRID.Equals(MRViewModel.RIRMIC + v.RIRRIC));
                    }
                    else
                    {
                        MRRow = new INVRIRP0();
                    }

                    MRRow.RIRRID   = MRViewModel.RIRMIC + v.RIRRIC;
                    MRRow.RIRMIC   = int.Parse(MRViewModel.RIRMIC);
                    MRRow.RIRRIC   = int.Parse(v.RIRRIC);
                    MRRow.RIRVPC   = 0;
                    MRRow.LongName = MRViewModel.MIMLON;
                    if (v.RIRSFQ != null && !v.RIRSFQ.Equals(""))
                    {
                        MRRow.RIRSFQ = double.Parse(v.RIRSFQ);
                    }
                    MRRow.RIRCWC = v.RIRCWC;
                    MRRow.RIRSTA = "0";
                    MRRow.RIRVST = "";
                    MRRow.RIRUSR = user.Substring(0, 3).ToUpper();
                    MRRow.RIRDAT = DateTime.Now;
                    MRRow.RIRFLG = false;
                    // Group
                    var IGRowLookup = db.ITMGRPs.Where(o => o.Item_Code.ToString().Equals(MRViewModel.RIRMIC));
                    IGRowLookup = IGRowLookup.Where(o => o.Item_Type == 4);
                    if (IGRowLookup.Any())
                    {
                        if (MRViewModel.Group == 0)
                        {
                            int val = IGRowLookup.FirstOrDefault().Id;
                            ItemGroupManager.DeleteItem(val);
                        }
                        else
                        {
                            IGRowLookup.FirstOrDefault().Item_Code  = int.Parse(MRViewModel.RIRMIC);
                            IGRowLookup.FirstOrDefault().Item_Name  = MRViewModel.MIMDSC;
                            IGRowLookup.FirstOrDefault().Group_Id   = MRViewModel.Group;
                            IGRowLookup.FirstOrDefault().Group_Name = db.ITMGRPs.FirstOrDefault(o => o.Group_Id == MRViewModel.Group).Group_Name;
                        }
                    }
                    else
                    {
                        if (MRViewModel.Group != 0)
                        {
                            ItemGroupViewModel IGRow = new ItemGroupViewModel();
                            IGRow.GroupName = db.ITMGRPs.FirstOrDefault(o => o.Group_Id == MRViewModel.Group).Group_Name;
                            IGRow.GroupId   = MRViewModel.Group;
                            IGRow.ItemCode  = int.Parse(MRViewModel.RIRMIC);
                            IGRow.ItemName  = MRViewModel.MIMDSC;
                            IGRow.ItemType  = 4;
                            IGRow.GroupType = 4;
                            ItemGroupManager.UpdateGroup(IGRow);
                        }
                    }
                    MRRow.Group = MRViewModel.Group;
                    try
                    {
                        if (db.INVRIRP0.Where(o => o.RIRRID == MRRow.RIRRID).Any())
                        {
                            MRRow.STATUS = "E";
                        }
                        else
                        {
                            MRRow.STATUS = "A";
                            db.INVRIRP0.Add(MRRow);
                        }
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.Source);
                        System.Diagnostics.Debug.WriteLine(e.Message);
                        System.Diagnostics.Debug.WriteLine(e.StackTrace);
                        System.Diagnostics.Debug.WriteLine(e.InnerException);
                        Exception f = e.InnerException;
                        while (f != null)
                        {
                            System.Diagnostics.Debug.WriteLine("INNER:");
                            System.Diagnostics.Debug.WriteLine(f.Message);
                            System.Diagnostics.Debug.WriteLine(f.Source);
                            f = f.InnerException;
                        }
                        System.Diagnostics.Debug.WriteLine(e.Data);
                        return(false);
                    }
                }
                // New created row
                for (int i = 0; i < MRViewModel.RIMRID.Count(); i++)
                {
                    if (MRViewModel.RIRRIC[i] == null || MRViewModel.RIRRIC[i].Equals(""))
                    {
                        continue;
                    }
                    INVRIRP0 MRRow = new INVRIRP0();
                    MRRow.RIRRID   = MRViewModel.RIRMIC + MRViewModel.RIRRIC[i];
                    MRRow.RIRMIC   = int.Parse(MRViewModel.RIRMIC);
                    MRRow.RIRRIC   = int.Parse(MRViewModel.RIRRIC[i]);
                    MRRow.RIRVPC   = 0;
                    MRRow.LongName = MRViewModel.MIMLON;
                    if (MRViewModel.RIRSFQ[i] != null && !MRViewModel.RIRSFQ[i].Equals(""))
                    {
                        MRRow.RIRSFQ = double.Parse(MRViewModel.RIRSFQ[i]);
                    }
                    MRRow.RIRCWC = MRViewModel.RIRCWC[i];
                    MRRow.RIRSTA = "0";
                    MRRow.RIRVST = "";
                    MRRow.RIRUSR = user.Substring(0, 3).ToUpper();
                    MRRow.RIRDAT = DateTime.Now;
                    MRRow.RIRFLG = false;
                    // Group
                    var IGRowLookup = db.ITMGRPs.Where(o => o.Item_Code.ToString().Equals(MRViewModel.RIRMIC));
                    IGRowLookup = IGRowLookup.Where(o => o.Item_Type == 4);
                    if (IGRowLookup.Any())
                    {
                        if (MRViewModel.Group == 0)
                        {
                            int val = IGRowLookup.FirstOrDefault().Id;
                            ItemGroupManager.DeleteItem(val);
                        }
                        else
                        {
                            IGRowLookup.FirstOrDefault().Item_Code  = int.Parse(MRViewModel.RIRMIC);
                            IGRowLookup.FirstOrDefault().Item_Name  = MRViewModel.MIMDSC;
                            IGRowLookup.FirstOrDefault().Group_Id   = MRViewModel.Group;
                            IGRowLookup.FirstOrDefault().Group_Name = db.ITMGRPs.FirstOrDefault(o => o.Group_Id == MRViewModel.Group).Group_Name;
                        }
                    }
                    else
                    {
                        if (MRViewModel.Group != 0)
                        {
                            ItemGroupViewModel IGRow = new ItemGroupViewModel();
                            IGRow.GroupName = db.ITMGRPs.FirstOrDefault(o => o.Group_Id == MRViewModel.Group).Group_Name;
                            IGRow.GroupId   = MRViewModel.Group;
                            IGRow.ItemCode  = int.Parse(MRViewModel.RIRMIC);
                            IGRow.ItemName  = MRViewModel.MIMDSC;
                            IGRow.ItemType  = 4;
                            IGRow.GroupType = 4;
                            ItemGroupManager.UpdateGroup(IGRow);
                        }
                    }
                    MRRow.Group = MRViewModel.Group;
                    try
                    {
                        if (db.INVRIRP0.Where(o => o.RIRRID == MRRow.RIRRID).Any())
                        {
                            INVRIRP0 rowToDelete = db.INVRIRP0.Single(o => o.RIRRID == MRRow.RIRRID);
                            db.INVRIRP0.Remove(rowToDelete);
                            MRRow.STATUS = "E";
                            db.INVRIRP0.Add(MRRow);
                        }
                        else
                        {
                            MRRow.STATUS = "A";
                            db.INVRIRP0.Add(MRRow);
                        }
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.Source);
                        System.Diagnostics.Debug.WriteLine(e.Message);
                        System.Diagnostics.Debug.WriteLine(e.StackTrace);
                        System.Diagnostics.Debug.WriteLine(e.InnerException);
                        Exception f = e.InnerException;
                        while (f != null)
                        {
                            System.Diagnostics.Debug.WriteLine("INNER:");
                            System.Diagnostics.Debug.WriteLine(f.Message);
                            System.Diagnostics.Debug.WriteLine(f.Source);
                            f = f.InnerException;
                        }
                        System.Diagnostics.Debug.WriteLine(e.Data);
                        return(false);
                    }
                }
                List <INVRIRP0> MRRowList = db.INVRIRP0.Where(o => o.RIRMIC.ToString().Equals(MRViewModel.RIRMIC)).ToList();
                if (MRRowList.Count() == 0)
                {
                    var RIRRow = db.ITMGRPs.Where(o => o.Item_Code.ToString().Equals(MRViewModel.RIRMIC));
                    RIRRow = RIRRow.Where(o => o.Item_Type == 4);
                    if (RIRRow.Any())
                    {
                        db.ITMGRPs.RemoveRange(RIRRow);
                        db.SaveChanges();
                    }
                }
                return(true);
            }
        }