public bool Delete(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (MaterialColorMngEntities context = CreateContext())
                {
                    MaterialColor dbItem = context.MaterialColor.FirstOrDefault(o => o.MaterialColorID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "MaterialColor not found!";
                        return(false);
                    }
                    else
                    {
                        var item = context.MaterialColorMng_MaterialColorCheck_View.Where(o => o.MaterialColorID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't delete because it used in item other!");
                        }
                        context.MaterialColor.Remove(dbItem);
                        context.SaveChanges();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;

                return(false);
            }
        }
        public bool Update(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            notification      = new Notification();
            notification.Type = NotificationType.Success;
            DTO.MaterialColorData dtoItems = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.MaterialColorData>();

            int    number;
            string indexName;

            try
            {
                using (var context = CreateContext())
                {
                    MaterialColor materialColor = null;

                    if (id == 0)
                    {
                        materialColor = new MaterialColor();

                        context.MaterialColor.Add(materialColor);
                        materialColor.UpdatedBy   = userId;
                        materialColor.UpdatedDate = DateTime.Now;
                    }
                    else
                    {
                        var item = context.MaterialColorMng_MaterialColorCheck_View.Where(o => o.MaterialColorID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't update because it used in item other!");
                        }
                        materialColor             = context.MaterialColor.FirstOrDefault(o => o.MaterialColorID == id);
                        materialColor.UpdatedBy   = userId;
                        materialColor.UpdatedDate = DateTime.Now;
                    }

                    if (materialColor == null)
                    {
                        notification.Message = "Material Color not found!";

                        return(false);
                    }
                    else
                    {
                        converter.DTO2BD_MaterialColor(dtoItems, ref materialColor);

                        if (id <= 0)
                        {
                            // Generate code.
                            using (var trans = context.Database.BeginTransaction())
                            {
                                context.Database.ExecuteSqlCommand("SELECT * FROM MaterialColor WITH (TABLOCKX, HOLDLOCK)");

                                try
                                {
                                    var newCode = context.MaterialColorMng_function_GenerateCode().FirstOrDefault();

                                    if (!"***".Equals(newCode))
                                    {
                                        materialColor.MaterialColorUD = newCode;

                                        context.SaveChanges();
                                    }
                                    else
                                    {
                                        notification.Type    = NotificationType.Error;
                                        notification.Message = "Auto generated code exceed maximum option: [ZZZ]";
                                    }
                                }
                                catch (Exception ex)
                                {
                                    trans.Rollback();
                                    throw ex;
                                }
                                finally
                                {
                                    trans.Commit();
                                }
                            }
                        }
                        else
                        {
                            context.SaveChanges();
                        }

                        dtoItem = GetEditData(materialColor.MaterialColorID, out notification).Data;

                        return(true);
                    }
                }
            }
            catch (DataException exData)
            {
                notification.Type = NotificationType.Error;
                ErrorHelper.DataExceptionParser(exData, out number, out indexName);

                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    if ("MaterialColorUDUnique".Equals(indexName))
                    {
                        notification.Message = "The Material Color Code is already exists.";
                    }
                }
                else
                {
                    notification.Message = exData.Message;
                }

                return(false);
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;

                return(false);
            }
        }
 public void DTO2BD_MaterialColor(DTO.MaterialColorData dtoItem, ref MaterialColor dbItem)
 {
     AutoMapper.Mapper.Map <DTO.MaterialColorData, MaterialColor>(dtoItem, dbItem);
 }