Exemplo n.º 1
0
        private void InsertOrUpdateUserPhone(List <TB_R_USER_PHONE> dbPhoneList, string phoneNo, int order, TB_R_USER user, DateTime transactionDateTime)
        {
            var dbPhone = dbPhoneList.FirstOrDefault(x => x.ORDER == order);

            if (dbPhone == null)
            {
                // Insert
                dbPhone             = new TB_R_USER_PHONE();
                dbPhone.TB_R_USER   = user;
                dbPhone.PHONE_NO    = phoneNo;
                dbPhone.ORDER       = Convert.ToInt16(order);
                dbPhone.CREATE_DATE = transactionDateTime;
                dbPhone.UPDATE_DATE = transactionDateTime;
                _context.TB_R_USER_PHONE.Add(dbPhone);
            }
            else
            {
                // Update
                if (phoneNo != dbPhone.PHONE_NO)
                {
                    dbPhone.PHONE_NO    = phoneNo;
                    dbPhone.UPDATE_DATE = transactionDateTime;
                }

                SetEntryStateModified(dbPhone);
            }
        }
Exemplo n.º 2
0
        public InsertOrUpdateUserResponse InsertOrUpdateUser(InsertOrUpdateUserRequest request, int?supervisorUserId, int roleId, int branchId)
        {
            _context.Configuration.AutoDetectChangesEnabled = false;

            try
            {
                var result = new InsertOrUpdateUserResponse();

                TB_R_USER dbUser = null;

                var transactionDateTime = DateTime.Now;

                //if (request.ActionType == 1)
                //{
                //    // Insert
                //    dbUser = null;
                //}
                //else
                //{
                //    // Update
                //    dbUser = _context.TB_R_USER.Where(user => user.EMPLOYEE_CODE.Trim().ToUpper() == request.EmployeeCodeOld.Trim().ToUpper()).FirstOrDefault();

                //    if (dbUser == null)
                //    {
                //        result.IsSuccess = false;
                //        result.ErrorCode = "8";
                //        result.ErrorMessage = "ไม่สามารถอัพเดตข้อมูล User เนื่องจากไม่พบข้อมูล User (Employee Code Old='" + request.EmployeeCodeOld + "')";
                //        return result;
                //    }
                //}

                if (!string.IsNullOrEmpty(request.EmployeeCodeOld))
                {
                    dbUser = _context.TB_R_USER.Where(user => user.EMPLOYEE_CODE.Trim().ToUpper() == request.EmployeeCodeOld.Trim().ToUpper()).FirstOrDefault();
                }

                if (dbUser == null)
                {
                    dbUser           = new TB_R_USER();
                    result.IsNewUser = true;
                }
                else
                {
                    result.IsNewUser = false;

                    // IF (Change Branch) Or (Change Status 1 to 0)
                    if (dbUser.BRANCH_ID != branchId || (dbUser.STATUS == 1 && request.Status == 0))
                    {
                        #region == Validate Job On Hand ==

                        var userId = dbUser.USER_ID;

                        var activeStatuses = new int[]
                        {
                            Constants.SRStatusId.Open,
                            Constants.SRStatusId.InProgress,
                            Constants.SRStatusId.WaitingCustomer,
                            Constants.SRStatusId.RouteBack,
                        };

                        var countJobOnHand = _context.TB_T_SR.Count(sr =>
                                                                    (
                                                                        (sr.OWNER_USER_ID == userId || sr.DELEGATE_USER_ID == userId) &&
                                                                        activeStatuses.Contains(sr.SR_STATUS_ID.Value)
                                                                    )
                                                                    ||
                                                                    (
                                                                        sr.CREATE_USER == userId &&
                                                                        sr.SR_STATUS_ID == Constants.SRStatusId.Draft
                                                                    )
                                                                    );

                        if (countJobOnHand > 0)
                        {
                            result.IsSuccess    = false;
                            result.ErrorCode    = "7";
                            result.ErrorMessage = "ไม่สามารถอัพเดตข้อมูล User ได้เนื่องจากมีงานค้างในมือ (ServiceRequest)";
                            return(result);
                        }

                        #endregion
                    }
                }

                var actionUser = _context.TB_R_USER.Where(user => user.USERNAME.Trim().ToUpper() == request.ActionUsername.Trim().ToUpper()).FirstOrDefault();
                if (actionUser == null)
                {
                    //if (dbUser == null)
                    //{
                    //    result.IsSuccess = false;
                    //    result.ErrorCode = "9";
                    //    result.ErrorMessage = "ไม่สามารถอัพเดตข้อมูล User เนื่องจากไม่พบข้อมูล Action User (Action Username='******')";
                    //    return result;
                    //}

                    actionUser = _context.TB_R_USER.Where(user => user.USERNAME.Trim().ToUpper() == Constants.SystemUserName).FirstOrDefault();
                }

                dbUser.BRANCH_ID      = branchId;
                dbUser.SUPERVISOR_ID  = supervisorUserId;
                dbUser.FIRST_NAME     = ValueOrDefault(request.FirstName);
                dbUser.LAST_NAME      = ValueOrDefault(request.LastName);
                dbUser.USERNAME       = ValueOrDefault(request.WindowsUsername);
                dbUser.STATUS         = Convert.ToInt16(request.Status);
                dbUser.ROLE_ID        = roleId;
                dbUser.EMPLOYEE_CODE  = ValueOrDefault(request.EmployeeCodeNew);
                dbUser.UPDATE_DATE    = transactionDateTime;
                dbUser.IS_GROUP       = request.IsGroup;
                dbUser.POSITION_CODE  = ValueOrDefault(request.PositionCode);
                dbUser.MARKETING_CODE = ValueOrDefault(request.MarketingCode);
                dbUser.EMAIL          = ValueOrDefault(request.Email);
                dbUser.ROLE_SALE      = ValueOrDefault(request.RoleSale);
                dbUser.MARKETING_TEAM = ValueOrDefault(request.MarketingTeam);
                dbUser.LINE           = ValueOrDefault(request.Line);
                dbUser.RANK           = ValueOrDefault(request.Rank);
                dbUser.EMPLOYEE_TYPE  = ValueOrDefault(request.EmployeeType);
                dbUser.COMPANY_NAME   = ValueOrDefault(request.CompanyName);
                dbUser.TELESALE_TEAM  = ValueOrDefault(request.TelesaleTeam);
                dbUser.MARKETING_FLAG = request.MarketingFlag;
                dbUser.EXPORT_DATE    = null;

                if (result.IsNewUser)
                {
                    for (var i = 0; i < 3; i++)
                    {
                        var phone = new TB_R_USER_PHONE();
                        phone.TB_R_USER   = dbUser;
                        phone.PHONE_NO    = i == 0 ? request.Phone1 : i == 1 ? request.Phone2 : request.Phone3;
                        phone.ORDER       = Convert.ToInt16(i + 1);
                        phone.CREATE_DATE = transactionDateTime;
                        phone.UPDATE_DATE = transactionDateTime;
                        _context.TB_R_USER_PHONE.Add(phone);
                    }
                }
                else
                {
                    var dbPhoneList = dbUser.TB_R_USER_PHONE.OrderBy(p => p.ORDER).Take(10).ToList();
                    InsertOrUpdateUserPhone(dbPhoneList, request.Phone1, 1, dbUser, transactionDateTime);
                    InsertOrUpdateUserPhone(dbPhoneList, request.Phone2, 2, dbUser, transactionDateTime);
                    InsertOrUpdateUserPhone(dbPhoneList, request.Phone3, 3, dbUser, transactionDateTime);
                }

                if (result.IsNewUser)
                {
                    dbUser.CREATE_USERNAME = ValueOrDefault(request.ActionUsername);
                    dbUser.UPDATE_USERNAME = ValueOrDefault(request.ActionUsername);
                    dbUser.CREATE_DATE     = transactionDateTime;
                    dbUser.UPDATE_DATE     = transactionDateTime;

                    _context.TB_R_USER.Add(dbUser);
                }
                else
                {
                    dbUser.UPDATE_USERNAME = ValueOrDefault(request.ActionUsername);
                    dbUser.UPDATE_DATE     = transactionDateTime;
                    SetEntryStateModified(dbUser);
                }

                this.Save();

                if (!request.IsGroup)
                {
                    var _customerDataAccess = new CustomerDataAccess(_context);

                    CustomerEntity customerEntity;

                    bool isCreateCustomer;
                    if (result.IsNewUser)
                    {
                        isCreateCustomer = true;
                        customerEntity   = new CustomerEntity();
                    }
                    else
                    {
                        customerEntity = _customerDataAccess.GetCustomerByEmployeeID(dbUser.USER_ID);

                        // If Update but no have Customer for this user >> It will create
                        if (customerEntity == null)
                        {
                            customerEntity   = new CustomerEntity();
                            isCreateCustomer = true;
                        }
                        else
                        {
                            isCreateCustomer = false;
                        }
                    }

                    // Create/Update Customer for this user

                    if (isCreateCustomer)
                    {
                        // Create Customer
                        var _commonDataAccess = new CommonDataAccess(_context);
                        var subscriptionType  = _commonDataAccess.GetSubscriptTypeByCode(Constants.DefaultSubscriptionTypeForUser);

                        customerEntity.CustomerType  = Constants.CustomerType.Employee;
                        customerEntity.FirstNameThai = request.FirstName;
                        customerEntity.LastNameThai  = request.LastName;
                        customerEntity.SubscriptType = subscriptionType;
                        customerEntity.CardNo        = request.EmployeeCodeNew;
                        customerEntity.Email         = request.Email;
                        customerEntity.EmployeeId    = dbUser.USER_ID;

                        customerEntity.TitleThai    = new TitleEntity();
                        customerEntity.TitleEnglish = new TitleEntity();

                        customerEntity.CreateUser = new UserEntity()
                        {
                            UserId = actionUser.USER_ID
                        };
                        customerEntity.UpdateUser = new UserEntity()
                        {
                            UserId = actionUser.USER_ID
                        };

                        customerEntity.PhoneList = new List <PhoneEntity>();

                        if (!string.IsNullOrEmpty(request.Phone1))
                        {
                            customerEntity.PhoneList.Add(new PhoneEntity()
                            {
                                PhoneNo = request.Phone1
                            });
                        }

                        if (!string.IsNullOrEmpty(request.Phone2))
                        {
                            customerEntity.PhoneList.Add(new PhoneEntity()
                            {
                                PhoneNo = request.Phone2
                            });
                        }

                        if (!string.IsNullOrEmpty(request.Phone3))
                        {
                            customerEntity.PhoneList.Add(new PhoneEntity()
                            {
                                PhoneNo = request.Phone3
                            });
                        }

                        var isSaveCustomerSuccess = _customerDataAccess.SaveCustomer(customerEntity);

                        if (!isSaveCustomerSuccess)
                        {
                            return(new InsertOrUpdateUserResponse()
                            {
                                IsSuccess = false,
                                ErrorCode = "6",
                                ErrorMessage = "Cannot create customer for this user.",
                            });
                        }
                    }
                    else
                    {
                        customerEntity.FirstNameThai = request.FirstName;
                        customerEntity.LastNameThai  = request.LastName;
                        customerEntity.CardNo        = request.EmployeeCodeNew;
                        customerEntity.Email         = request.Email;

                        customerEntity.TitleThai    = new TitleEntity();
                        customerEntity.TitleEnglish = new TitleEntity();

                        customerEntity.CreateUser = new UserEntity()
                        {
                            UserId = actionUser.USER_ID
                        };
                        customerEntity.UpdateUser = new UserEntity()
                        {
                            UserId = actionUser.USER_ID
                        };

                        if (customerEntity.PhoneList == null)
                        {
                            customerEntity.PhoneList = new List <PhoneEntity>();
                        }

                        var phones = new List <string>();

                        if (!string.IsNullOrEmpty(request.Phone1))
                        {
                            phones.Add(request.Phone1);
                        }

                        if (!string.IsNullOrEmpty(request.Phone2))
                        {
                            phones.Add(request.Phone2);
                        }

                        if (!string.IsNullOrEmpty(request.Phone3))
                        {
                            phones.Add(request.Phone3);
                        }

                        if (phones.Count > customerEntity.PhoneList.Count)
                        {
                            var diff = (phones.Count - customerEntity.PhoneList.Count);
                            for (int i = 0; i < diff; i++)
                            {
                                customerEntity.PhoneList.Add(new PhoneEntity());
                            }
                        }
                        else if (phones.Count < customerEntity.PhoneList.Count)
                        {
                            var diff = (customerEntity.PhoneList.Count - phones.Count);
                            for (int i = 0; i < diff; i++)
                            {
                                customerEntity.PhoneList.RemoveAt(customerEntity.PhoneList.Count - 1);
                            }
                        }

                        for (int i = 0; i < phones.Count; i++)
                        {
                            customerEntity.PhoneList[i].PhoneNo = phones[i];
                        }

                        var isSaveCustomerSuccess = _customerDataAccess.SaveCustomer(customerEntity);

                        if (!isSaveCustomerSuccess)
                        {
                            return(new InsertOrUpdateUserResponse()
                            {
                                IsSuccess = false,
                                ErrorCode = "6",
                                ErrorMessage = "Cannot update customer for this user.",
                            });
                        }
                    }
                }

                result.IsSuccess = true;
                return(result);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);

                return(new InsertOrUpdateUserResponse()
                {
                    IsSuccess = false,
                    ErrorCode = "1",
                    ErrorMessage = ex.Message,
                });
            }
            finally
            {
                _context.Configuration.AutoDetectChangesEnabled = false;
            }
        }