public JsonResult GetCustomerDetail(int CRef)
 {
     var db = new NSMMSEntities();
     CustomerViewModel t = new CustomerViewModel();
     t = db.Customer.Where(x => x.CustomerRef == CRef).Select(c => new CustomerViewModel
     {
         CustomerStatus = c.CustomerStatus,
         CustomerRef = c.CustomerRef,
         FullName = c.FullName,
         ShortName = c.ShortName,
         PostalCode = c.PostalCode,
         Addr = c.Addr,
         Phone1 = c.Phone1,
         Phone2 = c.Phone2,
         Fax = c.Fax,
         ContactPersonName = c.ContactPersonName,
         ContactPersonPhone = c.ContactPersonPhone,
         UniformNumbers = c.UniformNumbers,
         Notes = c.Notes,
         GroupSW = c.GroupSW,
         GroupID = c.GroupID,
         BranchID = c.BranchID,
         ResponsibleID = c.ResponsibleID,
         RegionID = c.RegionID,
         DefaultUserName = c.SubArea.ResponsibleClass.UserData2.UserName
     }).First();            
     
     var jsonResult = Json(t, JsonRequestBehavior.AllowGet);
     jsonResult.MaxJsonLength = int.MaxValue;
     return jsonResult;
     //return Json(lookupList, JsonRequestBehavior.AllowGet);
 }
        public IQueryable<CustomerViewModel> CustomerSearch(string GroupID, string CustomerID, string FullName, string ShortName, string FieldClass, string ContentClass)
        {
            List<CustomerViewModel> ret = new List<CustomerViewModel>();
            var qry = db.Customer.Where(x => x.CustomerRef != 0);
            if (FieldClass == "0")
            { 
                if(GroupID != "" || CustomerID != "" || FullName != "" || ShortName != "")
                    qry = qry.Where(x => (x.GroupID == GroupID && GroupID != "") || (x.CustomerID.Contains(CustomerID) && CustomerID != "") || (x.FullName.Contains(FullName) && FullName != "") || (x.ShortName.Contains(ShortName) && ShortName != ""));
            }
            else if (FieldClass == "1")
            {
                if(GroupID != "")
                    qry = qry.Where(x => x.GroupID == GroupID);
                if (CustomerID != "")
                    qry = qry.Where(x => x.CustomerID.Contains(CustomerID));
                if (FullName != "")
                    qry = qry.Where(x => x.FullName.Contains(FullName));
                if (ShortName != "")
                    qry = qry.Where(x => x.ShortName.Contains(ShortName));
            }

            foreach (Customer o in qry)
            {
                CustomerViewModel c = new CustomerViewModel();


                c.Addr = o.Addr;
                c.BranchID = o.BranchID;
                c.ContactPersonName = o.ContactPersonName;
                c.ContactPersonPhone = o.ContactPersonPhone;
                c.CustomerID = o.CustomerID;
                c.CustomerRef = o.CustomerRef;
                c.CustomerStatus = o.CustomerStatus;
                if (c.CustomerStatus == "0")
                    c.CustomerStatusStr = "停用";
                else if (c.CustomerStatus == "1")
                    c.CustomerStatusStr = "使用";
                else if (c.CustomerStatus == "9")
                    c.CustomerStatusStr = "暫用";
                c.Fax = o.Fax;
                c.FullName = o.FullName;
                c.GroupID = o.GroupID;
                c.GroupSW = o.GroupSW;
                c.Notes = o.Notes;
                c.Phone1 = o.Phone1;
                c.Phone2 = o.Phone2;
                c.PostalCode = o.PostalCode;
                c.RegionID = o.RegionID;
                c.RegionIDStr = o.SubArea.Name;
                c.ResponsibleID = o.ResponsibleID;
                c.ShortName = o.ShortName;
                c.UniformNumbers = o.UniformNumbers;

                //new
                c.AddrNumber = (o.AddrNumber == null) ? "" :  o.AddrNumber;
                //c.CreateDT = o.CreateDateTime.Value;
                c.CreateDTStr = "";
                if (o.CreateDateTime != null)
                {
                    c.CreateDT = o.CreateDateTime.Value;
                    c.CreateDTStr = o.CreateDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                }
                c.CreateUserName = (o.CreateUserID == null) ? "" : o.UserData.UserName;
                c.CreateUserID = o.CreateUserID;
                //if (o.CreateUserID != null)
                //    c.CreateUserName = o.UserData.UserName;
                //c.History = o.History;
                c.History = (o.History == null) ? "" : o.History;
                //c.ModifyDT = o.ModifyDateTime.Value;
                c.ModifyDTStr = "";
                if (o.ModifyDateTime != null)
                {
                    c.ModifyDT = o.ModifyDateTime.Value;
                    c.ModifyDTStr = o.ModifyDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                }

                c.ModifyUserID = o.ModifyUserID;
                c.ModifyUserName = (o.ModifyUserID == null) ? "" : o.UserData1.UserName;
                //if (o.ModifyUserID != null)
                //    c.ModifyUserName = o.UserData1.UserName;
                ret.Add(c);
            }


            return ret.AsQueryable();
        }
 public bool Delete(CustomerViewModel customerViewModel)
 {
     bool ret = false;
     try
     {
         Customer c = db.Customer.Find(customerViewModel.CustomerRef);
         db.Customer.Remove(c);
         db.SaveChanges();
         ret = true;
     }
     catch
     {
     }
     return ret;
 }
        public bool Update(CustomerViewModel customerViewModel)
        {
            bool ret = false;
            try
            {
                Customer c = db.Customer.Find(customerViewModel.CustomerRef);

                c.Addr = customerViewModel.Addr;
                c.BranchID = customerViewModel.BranchID;
                c.ContactPersonName = customerViewModel.ContactPersonName;
                c.ContactPersonPhone = customerViewModel.ContactPersonPhone;
                c.CustomerID = customerViewModel.CustomerID;
                c.CustomerStatus = customerViewModel.CustomerStatus;
                c.Fax = customerViewModel.Fax;
                c.FullName = customerViewModel.FullName;
                c.GroupID = customerViewModel.GroupID;
                c.GroupSW = customerViewModel.GroupSW;
                c.Notes = customerViewModel.Notes;
                c.Phone1 = customerViewModel.Phone1;
                c.Phone2 = customerViewModel.Phone2;
                c.PostalCode = customerViewModel.PostalCode;
                c.RegionID = customerViewModel.RegionID;
                c.ResponsibleID = customerViewModel.ResponsibleID;
                c.ShortName = customerViewModel.ShortName;
                c.UniformNumbers = customerViewModel.UniformNumbers;

                //new
                c.AddrNumber = customerViewModel.AddrNumber;
                c.History = customerViewModel.History;
                c.CreateDateTime = DateTime.Now;
                c.ModifyDateTime = DateTime.Now;
                //c.CreateUserID = customerViewModel.CreateUserID;
                c.ModifyUserID = customerViewModel.ModifyUserID;

                db.SaveChanges();
                ret = true;
            }
            catch (DbUpdateConcurrencyException ex)
            {
                // Update the values of the entity that failed to save from the store
                ex.Entries.Single().Reload();
            }
            catch (DbEntityValidationException ex)
            {
                var errorMessages = ex.EntityValidationErrors
                         .SelectMany(x => x.ValidationErrors)
                         .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
            return ret;
        }
        public bool Create(CustomerViewModel customerViewModel)
        {
            bool ret = false;
            try
            {

                Customer c = new Customer();

                c.CustomerRef = db.Customer.AsEnumerable().Select(x => x.CustomerRef).DefaultIfEmpty(0).Max(x => x) + 1;

                c.Addr = customerViewModel.Addr;
                c.BranchID = customerViewModel.BranchID;
                c.ContactPersonName = customerViewModel.ContactPersonName;
                c.ContactPersonPhone = customerViewModel.ContactPersonPhone;                
                c.CustomerID = customerViewModel.CustomerID;
                c.CustomerStatus = customerViewModel.CustomerStatus;
                c.Fax = customerViewModel.Fax;
                c.FullName = customerViewModel.FullName;
                c.GroupID = customerViewModel.GroupID;
                c.GroupSW = customerViewModel.GroupSW;
                c.Notes = customerViewModel.Notes;
                c.Phone1 = customerViewModel.Phone1;
                c.Phone2 = customerViewModel.Phone2;
                c.PostalCode = customerViewModel.PostalCode;
                c.RegionID = customerViewModel.RegionID;
                c.ResponsibleID = customerViewModel.ResponsibleID;
                c.ShortName = customerViewModel.ShortName;
                c.UniformNumbers = customerViewModel.UniformNumbers;

                //newsdata.UpdUserID = "AAA";
                //newsdata.LastUpdDT = DateTime.Now;
                //new
                c.AddrNumber = customerViewModel.AddrNumber;
                c.History = customerViewModel.History;
                c.CreateDateTime = DateTime.Now;
                c.ModifyDateTime = DateTime.Now;
                c.CreateUserID = customerViewModel.CreateUserID;
                c.ModifyUserID = customerViewModel.ModifyUserID;

                db.Customer.Add(c);
                db.SaveChanges();


                //bulletinViewModel.SeqNo = bulletin.SeqNo;
                ret = true;
            }
            catch
            {
            }
            return ret;
        }
        public IQueryable<CustomerViewModel> Read()
        {
            List<CustomerViewModel> ret = new List<CustomerViewModel>();
            var qry = db.Customer;
            foreach (Customer o in qry)
            {
                CustomerViewModel c = new CustomerViewModel();

                c.Addr = o.Addr;
                c.BranchID = o.BranchID;
                c.ContactPersonName = o.ContactPersonName;
                c.ContactPersonPhone = o.ContactPersonPhone;
                c.CustomerID = o.CustomerID;
                c.CustomerRef = o.CustomerRef;
                c.CustomerStatus = o.CustomerStatus;
                if (c.CustomerStatus == "0")
                    c.CustomerStatusStr = "停用";
                else if (c.CustomerStatus == "1")
                    c.CustomerStatusStr = "使用";
                else if (c.CustomerStatus == "9")
                    c.CustomerStatusStr = "暫用";
                c.Fax = o.Fax;
                c.FullName = o.FullName;
                c.GroupID = o.GroupID;
                c.GroupSW = o.GroupSW;
                c.Notes = o.Notes;
                c.Phone1 = o.Phone1;
                c.Phone2 = o.Phone2;
                c.PostalCode = o.PostalCode;
                c.RegionID = o.RegionID;
                c.RegionIDStr = o.SubArea.Name;
                c.ResponsibleID = o.ResponsibleID;
                c.ShortName = o.ShortName;
                c.UniformNumbers = o.UniformNumbers;
                
                //new
                c.AddrNumber = o.AddrNumber;
                //c.CreateDT = o.CreateDateTime.Value;
                c.CreateDTStr = "";
                if (o.CreateDateTime != null) {
                    c.CreateDT = o.CreateDateTime.Value;
                    c.CreateDTStr = o.CreateDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                }
                    
                c.CreateUserID = o.CreateUserID;
                if (o.CreateUserID != null)
                    c.CreateUserName = o.UserData.UserName;
                c.History = o.History;
                //c.ModifyDT = o.ModifyDateTime.Value;
                c.ModifyDTStr = "";
                if (o.ModifyDateTime != null) {
                    c.ModifyDT = o.ModifyDateTime.Value;
                    c.ModifyDTStr = o.ModifyDateTime.Value.ToString("yyyy/MM/dd HH:mm");
                }
                    
                c.ModifyUserID = o.ModifyUserID;
                if (o.ModifyUserID != null)
                    c.ModifyUserName = o.UserData1.UserName;


                ret.Add(c);
            }
            return ret.AsQueryable();
        }