Ejemplo n.º 1
0
        public ActionResult ManageContactRlat(ContactEditViewModel contactEditVM)
        {
            Logger.Info(_logMsg.Clear().SetPrefixMsg("Edit Contact").Add("FirstNameThai", contactEditVM.FirstNameThai).ToInputLogString());

            try
            {
                #region "Validate CardNo"

                if (!string.IsNullOrEmpty(contactEditVM.SubscriptType))
                {
                    _commonFacade = new CommonFacade();
                    var subscriptTypePersonal =
                        _commonFacade.GetSubscriptTypeByCode(Constants.SubscriptTypeCode.Personal);
                    if (string.IsNullOrEmpty(contactEditVM.CardNo))
                    {
                        ModelState.AddModelError("CardNo", string.Format(CultureInfo.InvariantCulture, Resource.ValErr_RequiredField, Resource.Lbl_CardNo_Passport));
                    }
                    else if (contactEditVM.SubscriptType.ToNullable <int>() == subscriptTypePersonal.SubscriptTypeId)
                    {
                        if (!ApplicationHelpers.ValidateCardNo(contactEditVM.CardNo))
                        {
                            ModelState.AddModelError("CardNo", Resource.ValErr_InvalidCardNo);
                        }
                    }
                }

                #endregion

                #region "Validate FirstName"

                if (string.IsNullOrEmpty(contactEditVM.FirstNameThai) &&
                    string.IsNullOrEmpty(contactEditVM.FirstNameEnglish))
                {
                    ModelState.AddModelError("FirstNameThai", string.Format(CultureInfo.InvariantCulture, Resource.ValErr_RequiredField, Resource.Lbl_FirstNameThai_Contact));
                }


                #endregion

                #region  "Validate Phone"

                if (contactEditVM.IsEdit == "1")
                {
                    if (string.IsNullOrEmpty(contactEditVM.PhoneType2))
                    {
                        ModelState.Remove("PhoneNo2");
                    }
                    if (string.IsNullOrEmpty(contactEditVM.PhoneType3))
                    {
                        ModelState.Remove("PhoneNo3");
                    }

                    // Check duplicate phoneNo
                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo1) && !string.IsNullOrEmpty(contactEditVM.PhoneNo2))
                    {
                        if (contactEditVM.PhoneNo1.Equals(contactEditVM.PhoneNo2))
                        {
                            ModelState.AddModelError("PhoneNo1", Resource.ValError_DuplicatePhoneNo);
                            ModelState.AddModelError("PhoneNo2", Resource.ValError_DuplicatePhoneNo);
                        }
                    }

                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo1) && !string.IsNullOrEmpty(contactEditVM.PhoneNo3))
                    {
                        if (contactEditVM.PhoneNo1.Equals(contactEditVM.PhoneNo3))
                        {
                            ModelState.AddModelError("PhoneNo1", Resource.ValError_DuplicatePhoneNo);
                            ModelState.AddModelError("PhoneNo3", Resource.ValError_DuplicatePhoneNo);
                        }
                    }

                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo2) && !string.IsNullOrEmpty(contactEditVM.PhoneNo3))
                    {
                        if (contactEditVM.PhoneNo2.Equals(contactEditVM.PhoneNo3))
                        {
                            ModelState.AddModelError("PhoneNo2", Resource.ValError_DuplicatePhoneNo);
                            ModelState.AddModelError("PhoneNo3", Resource.ValError_DuplicatePhoneNo);
                        }
                    }
                }
                else
                {
                    // Phone
                    ModelState.Remove("PhoneType1");
                    ModelState.Remove("PhoneNo1");
                    ModelState.Remove("PhoneNo2");
                    ModelState.Remove("PhoneNo3");

                    // Name
                    ModelState.Remove("FirstNameThai");
                    ModelState.Remove("FirstNameEnglish");
                    ModelState.Remove("LastNameThai");
                    ModelState.Remove("LastNameEnglish");
                }

                #endregion

                if (!string.IsNullOrEmpty(contactEditVM.BirthDate) && !contactEditVM.BirthDateValue.HasValue)
                {
                    ModelState.AddModelError("BirthDate", Resource.ValErr_InvalidDate);
                }
                else if (!string.IsNullOrEmpty(contactEditVM.BirthDate) && contactEditVM.BirthDateValue.HasValue)
                {
                    if (contactEditVM.BirthDateValue.Value > DateTime.Now.Date)
                    {
                        ModelState.AddModelError("BirthDate", Resource.ValErr_InvalidDate_MustLessThanToday);
                    }
                }

                if (ModelState.IsValid)
                {
                    _customerFacade = new CustomerFacade();

                    #region "Check Duplicate PhoneNo"

                    List <string> lstPhoneNo = new List <string>();
                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo1))
                    {
                        lstPhoneNo.Add(contactEditVM.PhoneNo1);
                    }
                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo2))
                    {
                        lstPhoneNo.Add(contactEditVM.PhoneNo2);
                    }
                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo3))
                    {
                        lstPhoneNo.Add(contactEditVM.PhoneNo3);
                    }

                    if (contactEditVM.IsEdit != "0")
                    {
                        contactEditVM.ContactList = _customerFacade.GetContactByPhoneNo(contactEditVM.ContactId,
                                                                                        contactEditVM.FirstNameThai, contactEditVM.LastNameThai
                                                                                        , contactEditVM.FirstNameEnglish, contactEditVM.LastNameEnglish, lstPhoneNo);
                    }

                    if (contactEditVM.ContactList != null && contactEditVM.ContactList.Any())
                    {
                        if (contactEditVM.IsConfirm == "0") // Case New
                        {
                            // Show DuplicateList
                            return(PartialView("~/Views/Contact/_ContactDuplicateList.cshtml", contactEditVM));
                        }
                        else // Case Edit
                        {
                            // Alert Duplicate
                            return(Json(new
                            {
                                Valid = false,
                                Error = Resource.ValError_DuplicateContact,
                                Errors = string.Empty
                            }));
                        }
                    }

                    #endregion

                    ContactEntity contactEntity = new ContactEntity
                    {
                        ContactId     = contactEditVM.ContactId,
                        SubscriptType = new SubscriptTypeEntity
                        {
                            SubscriptTypeId = contactEditVM.SubscriptType.ToNullable <int>()
                        },
                        CardNo    = contactEditVM.CardNo,
                        BirthDate = contactEditVM.BirthDateValue,
                        TitleThai = new TitleEntity
                        {
                            TitleId = contactEditVM.TitleThai.ToNullable <int>()
                        },
                        FirstNameThai = contactEditVM.FirstNameThai,
                        LastNameThai  = contactEditVM.LastNameThai,
                        TitleEnglish  = new TitleEntity
                        {
                            TitleId = contactEditVM.TitleEnglish.ToNullable <int>()
                        },
                        FirstNameEnglish = contactEditVM.FirstNameEnglish,
                        LastNameEnglish  = contactEditVM.LastNameEnglish,
                        Email            = contactEditVM.Email,
                        CreateUser       = new UserEntity
                        {
                            UserId = this.UserInfo.UserId
                        },
                        UpdateUser = new UserEntity
                        {
                            UserId = this.UserInfo.UserId
                        },
                        CustomerId = contactEditVM.CustomerId // for add CustomerLog
                    };

                    #region "Phone & Fax"

                    // Phone & Fax
                    contactEntity.PhoneList = new List <PhoneEntity>();
                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo1))
                    {
                        contactEntity.PhoneList.Add(new PhoneEntity {
                            PhoneTypeId = contactEditVM.PhoneType1.ToNullable <int>(), PhoneNo = contactEditVM.PhoneNo1
                        });
                    }
                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo2))
                    {
                        contactEntity.PhoneList.Add(new PhoneEntity {
                            PhoneTypeId = contactEditVM.PhoneType2.ToNullable <int>(), PhoneNo = contactEditVM.PhoneNo2
                        });
                    }
                    if (!string.IsNullOrEmpty(contactEditVM.PhoneNo3))
                    {
                        contactEntity.PhoneList.Add(new PhoneEntity {
                            PhoneTypeId = contactEditVM.PhoneType3.ToNullable <int>(), PhoneNo = contactEditVM.PhoneNo3
                        });
                    }
                    // Fax
                    if (!string.IsNullOrEmpty(contactEditVM.Fax))
                    {
                        _commonFacade = new CommonFacade();
                        var phoneTypeFax = _commonFacade.GetPhoneTypeByCode(Constants.PhoneTypeCode.Fax);
                        contactEntity.PhoneList.Add(new PhoneEntity {
                            PhoneTypeId = phoneTypeFax.PhoneTypeId, PhoneNo = contactEditVM.Fax
                        });
                    }
                    #endregion

                    // Save
                    var contactRelationship = contactEditVM.ContactRelationshipList.Where(x => x.IsEdit == true).ToList();

                    bool isSuccess = false;
                    if (contactEditVM.IsConfirm == "0")
                    {
                        isSuccess = _customerFacade.SaveContact(contactEntity, null); // save contact only
                    }
                    else
                    {
                        #region "Validate ContactRelationship"

                        if (contactEditVM.ContactRelationshipList.Count(x => x.CustomerId == contactEditVM.CustomerId) == 0)
                        {
                            // Alert
                            return(Json(new
                            {
                                Valid = false,
                                Error = Resource.Msg_PleaseInputRelationship,
                                Errors = string.Empty
                            }));
                        }


                        #endregion

                        isSuccess = _customerFacade.SaveContact(contactEntity, contactRelationship); // save contact & relationship
                    }

                    if (isSuccess)
                    {
                        return(Json(new
                        {
                            Valid = true,
                            contactId = contactEntity.ContactId,
                        }));
                    }
                    else
                    {
                        return(Json(new
                        {
                            Valid = false,
                            Error = Resource.Error_System,
                            Errors = string.Empty
                        }));
                    }
                }

                return(Json(new
                {
                    Valid = false,
                    Error = string.Empty,
                    Errors = GetModelValidationErrors()
                }));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Edit Contact").Add("Error Message", ex.Message).ToFailLogString());
                return(Json(new
                {
                    Valid = false,
                    Error = Resource.Error_System,
                    Errors = string.Empty
                }));
            }
        }