コード例 #1
0
        public async Task <IActionResult> CreateCustomer(CustomerViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //get user id from access token i.e authorized user
                    string Id = User.Claims.First(c => c.Type == "UserID").Value;
                    //if customer created by sub-admin then customer linked to parent admin (i.e bussiness)
                    string parentUserId = Id;
                    var    user         = await _userManager.FindByIdAsync(Id);

                    if (User.IsInRole(Constants.isSubAdmin))
                    {
                        parentUserId = user.ParentUserId;
                    }
                    var    userstatus = user.UserStatus;
                    string accNo      = "1000" + CommonMethods.Generate13UniqueDigits();
                    var    customers  = new Customers()
                    {
                        FirstName      = model.FirstName,
                        LastName       = model.LastName,
                        BussinessName  = model.BussinessName,
                        Phone          = model.Phone,
                        Fax            = model.Fax,
                        Mobile         = model.Mobile,
                        Address1       = model.Address1,
                        Address2       = model.Address2,
                        BillingAddress = model.BillingAddress,
                        MailingAddress = model.MailingAddress,
                        CountryId      = model.CountryId,
                        StateId        = model.StateId,
                        City           = model.City,
                        Postalcode     = model.Postalcode,
                        PersonalEmail  = model.PersonalEmail,
                        BussinessEmail = model.BussinessEmail,
                        Gender         = model.Gender,
                        Dob            = (!string.IsNullOrWhiteSpace(model.Dob))? DateTime.ParseExact(model.Dob, "dd/MM/yyyy", null):(DateTime?)null,
                        Gstin          = model.Gstin,
                        AccountNumber  = Convert.ToInt64(accNo), //Convert.ToInt64(model.AccountNumber),
                        PosoNumber     = model.PosoNumber,
                        Website        = model.Website,
                        IsActive       = true,
                        UserId         = parentUserId,
                        CreatedBy      = Id,
                        CreatedDate    = DateTime.Now
                    };
                    var retId = await _managementService.AddCustomer(customers);

                    if (retId > 0)
                    {
                        return(Ok(new { status = StatusCodes.Status200OK, success = true, message = "customer" + ResponseMessages.msgCreationSuccess, userstatus }));
                    }
                    else if (retId < 0)
                    {
                        return(Ok(new { status = StatusCodes.Status400BadRequest, success = false, message = ResponseMessages.msgEmailAlreadyUsed, userstatus = false }));
                    }
                    else
                    {
                        return(Ok(new { status = StatusCodes.Status404NotFound, success = false, message = ResponseMessages.msgDbConnectionError, userstatus = false }));
                    }
                }
                catch (Exception ex)
                {
                    return(Ok(new { status = StatusCodes.Status500InternalServerError, success = false, message = ResponseMessages.msgSomethingWentWrong + ex.Message, userstatus = false }));
                }
            }
            return(Ok(new { status = StatusCodes.Status406NotAcceptable, success = false, message = ResponseMessages.msgParametersNotCorrect, userstatus = false }));
        }