Beispiel #1
0
        public StringBuilder GenerateRecipeText(INVRIRP0 Recipe)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("04,");
            sb.Append(Recipe.STATUS.Trim() + ",");
            sb.Append(Recipe.RIRRID.Trim() + ",");
            if (Recipe.RIRMIC != null)
            {
                sb.Append(Recipe.RIRMIC.ToString() + ",");
            }
            else
            {
                sb.Append(",");
            }
            if (Recipe.RIRRIC != null)
            {
                sb.Append(Recipe.RIRRIC.ToString() + ",");
            }
            else
            {
                sb.Append(",");
            }
            if (Recipe.RIRVPC != null)
            {
                sb.Append(Recipe.RIRVPC.ToString() + ",");
            }
            else
            {
                sb.Append(",");
            }
            if (Recipe.RIRSFQ != null)
            {
                sb.Append(((double)Recipe.RIRSFQ).ToString("N2") + ",");
            }
            else
            {
                sb.Append("0.00,");
            }
            if (Recipe.RIRCWC != null)
            {
                sb.Append(Recipe.RIRCWC.Trim() + ",");
            }
            else
            {
                sb.Append(",");
            }
            if (Recipe.RIRSTA != null)
            {
                sb.Append(Recipe.RIRSTA.Trim() + ",");
            }
            else
            {
                sb.Append(",");
            }
            if (Recipe.RIRVST != null)
            {
                sb.Append(Recipe.RIRVST.Trim() + ",");
            }
            else
            {
                sb.Append(",");
            }
            if (Recipe.RIRUSR != null)
            {
                sb.Append(Recipe.RIRUSR.Trim() + ",");
            }
            else
            {
                sb.Append(",");
            }
            if (Recipe.RIRDAT == null)
            {
                sb.Append("null,");
            }
            else
            {
                sb.Append(Recipe.RIRDAT.ToString() + ",");
            }
            if (Recipe.RIRFLG != null)
            {
                sb.Append(Recipe.RIRFLG.ToString() + System.Environment.NewLine);
            }
            else
            {
                sb.Append(System.Environment.NewLine);
            }
            return(sb);
        }
        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);
            }
        }