public bool CopyProduct(String ProductID, String CopyCode, String CopyName)
        {
            bool ret = false;
            try
            {
                ProductClass productdata = new ProductClass();

                productdata.Code = CopyCode;
                productdata.Note = CopyName;
                
                db.ProductClass.Add(productdata);
                db.SaveChanges();
                List<int> oldIIDList = new List<int>();
                List<int> nweIIDList = new List<int>();
                int newPID = db.ProductClass.AsEnumerable().Where(x => x.Code == CopyCode).Select(x => x.ProductClassID).First();
                int pID = Int32.Parse(ProductID);
                var qry = db.ItemClass.Where(x => x.ProductClassID == pID);
                foreach (ItemClass i in qry)
                {
                    int ID = db.ItemClass.AsEnumerable().Select(x => x.ItemClassID).DefaultIfEmpty(0).Max(x => x) + 1;
                    ItemClass itemdata = new ItemClass();

                    itemdata.Code = i.Code;
                    itemdata.Note = i.Note;
                    itemdata.ProductClassID = newPID;
                    itemdata.ItemClassID = ID;

                    db.ItemClass.Add(itemdata);
                    //db.SaveChanges();
                    //int newIID = db.ItemClass.AsEnumerable().Where(x => x.Code == CopyCode && x.ProductClassID == newPID).Select(x => x.ItemClassID).First();
                    //nweIIDList.Add(ID);
                    oldIIDList.Add(i.ItemClassID);
                }
                db.SaveChanges();
                var qry3 = db.ItemClass.Where(x => x.ProductClassID == newPID).OrderBy(x=>x.ItemClassID);
                foreach (ItemClass i in qry3)
                {
                    nweIIDList.Add(i.ItemClassID);
                }

                
                for (int i = 0; i < oldIIDList.Count; i++)
                {
                    int iid = oldIIDList[i];
                    var qry2 = db.QuestionClass.Where(x => x.ItemClassID == iid);
                    foreach (QuestionClass q in qry2)
                    {
                        QuestionClass questiondata = new QuestionClass();

                        questiondata.Code = q.Code;
                        questiondata.Note = q.Note;
                        questiondata.ItemClassID = nweIIDList[i];
                        questiondata.QuestionGrade = q.QuestionGrade;
                        questiondata.QuestionServiceHours = q.QuestionServiceHours;

                        db.QuestionClass.Add(questiondata);
                        
                    }
                    db.SaveChanges();
                }
                //db.SaveChanges();

                
                ret = true;
            }
            catch
            {
                ret = false;
            }
            return ret;

        }
        public bool CopyItem(String ItemID, String CopyCode, String CopyName)
        {
            bool ret = false;
            try
            {
                int iID = Int32.Parse(ItemID);
                int pID = db.ItemClass.AsEnumerable().Where(x => x.ItemClassID == iID).Select(x => x.ProductClassID).First().Value;
                ItemClass itemdata = new ItemClass();

                itemdata.Code = CopyCode;
                itemdata.Note = CopyName;
                itemdata.ProductClassID = pID;

                db.ItemClass.Add(itemdata);
                db.SaveChanges();

                int newIID = db.ItemClass.AsEnumerable().Where(x => x.Code == CopyCode && x.ProductClassID == pID).Select(x => x.ItemClassID).First();

                var qry = db.QuestionClass.Where(x => x.ItemClassID == iID);
                foreach (QuestionClass q in qry)
                {

                    QuestionClass questiondata = new QuestionClass();

                    questiondata.Code = q.Code;
                    questiondata.Note = q.Note;
                    questiondata.ItemClassID = newIID;
                    questiondata.QuestionGrade = q.QuestionGrade;
                    questiondata.QuestionServiceHours = q.QuestionServiceHours;

                    db.QuestionClass.Add(questiondata);
                    
                }
                db.SaveChanges();


                ret = true;
            }
            catch
            {
                ret = false;
            }
            return ret;

        }
        public bool CreateItem(ViewModels.ItemClassViewModel itemClassViewModel)
        {
            bool ret = false;
            try
            {
                ItemClass itemdata = new ItemClass();

                itemdata.Code = itemClassViewModel.Code;
                itemdata.Note = itemClassViewModel.Name;
                itemdata.ProductClassID = Int32.Parse(itemClassViewModel.PID);

                db.ItemClass.Add(itemdata);
                db.SaveChanges();

                ret = true;
            }
            catch
            {
            }
            return ret;
        }