public async Task <ProviderAC> GetProviderById(long id)
        {
            ProviderAC providerAC = new ProviderAC();

            TeleBillingUtility.Models.Provider provider = await _dbTeleBilling_V01Context.Provider.FirstAsync(x => x.Id == id);

            providerAC = _mapper.Map <ProviderAC>(provider);

            List <Providerservice> listOfService = await _dbTeleBilling_V01Context.Providerservice.Where(x => x.ProviderId == id && !x.IsDelete).Include(x => x.ServiceType).ToListAsync();

            providerAC.ServiceTypes = new List <DrpResponseAC>();
            foreach (var item in listOfService)
            {
                DrpResponseAC serviceType = new DrpResponseAC();
                serviceType.Id   = item.ServiceTypeId;
                serviceType.Name = item.ServiceType.Name;

                providerAC.ServiceTypes.Add(serviceType);
            }

            List <Providercontactdetail> providerContactDetails = await _dbTeleBilling_V01Context.Providercontactdetail.Where(x => x.ProviderId == provider.Id && !x.IsDeleted).ToListAsync();

            providerAC.ProviderContactDetailACList = new List <ProviderContactDetailAC>();
            providerAC.ProviderContactDetailACList = _mapper.Map(providerContactDetails, providerAC.ProviderContactDetailACList);

            return(providerAC);
        }
        public async Task <IActionResult> EditProvider(ProviderAC providerAC)
        {
            string userId   = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "user_id").Value;
            string fullname = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "fullname").Value;

            return(Ok(await _iProviderRepository.UpdateProvider(Convert.ToInt64(userId), providerAC, fullname)));
        }
        public async Task <ResponseAC> AddProvider(long userId, ProviderAC providerAC, string loginUserName)
        {
            ResponseAC responeAC = new ResponseAC();

            if (!await _dbTeleBilling_V01Context.Provider.AnyAsync(x => x.ContractNumber.ToLower() == providerAC.ContractNumber.ToLower() && !x.IsDelete))
            {
                TeleBillingUtility.Models.Provider provider = new TeleBillingUtility.Models.Provider();
                provider               = _mapper.Map <TeleBillingUtility.Models.Provider>(providerAC);
                provider.IsActive      = true;
                provider.CreatedDate   = DateTime.Now;
                provider.CreatedBy     = userId;
                provider.TransactionId = _iLogManagement.GenerateTeleBillingTransctionID();

                await _dbTeleBilling_V01Context.AddAsync(provider);

                await _dbTeleBilling_V01Context.SaveChangesAsync();


                foreach (var serviceType in providerAC.ServiceTypes)
                {
                    Providerservice providerService = new Providerservice();
                    providerService.ProviderId    = provider.Id;
                    providerService.ServiceTypeId = serviceType.Id;
                    await _dbTeleBilling_V01Context.AddAsync(providerService);

                    await _dbTeleBilling_V01Context.SaveChangesAsync();
                }

                #region Added Provider Contact Detail
                List <Providercontactdetail> providerContactDetails = new List <Providercontactdetail>();
                foreach (var item in providerAC.ProviderContactDetailACList)
                {
                    Providercontactdetail providerContectDatail = new Providercontactdetail();
                    providerContectDatail            = _mapper.Map <Providercontactdetail>(item);
                    providerContectDatail.ProviderId = provider.Id;
                    providerContactDetails.Add(providerContectDatail);
                }

                await _dbTeleBilling_V01Context.AddRangeAsync(providerContactDetails);

                await _dbTeleBilling_V01Context.SaveChangesAsync();

                #endregion

                await _iLogManagement.SaveAuditActionLog((int)EnumList.AuditLogActionType.AddProvider, loginUserName, userId, "Provider(" + provider.Name + ")", (int)EnumList.ActionTemplateTypes.Add, provider.Id);

                responeAC.Message    = _iStringConstant.ProviderAddedSuccessfully;
                responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Success);
            }
            else
            {
                responeAC.Message    = _iStringConstant.ContractNumberExists;
                responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
            }
            return(responeAC);
        }
        public async Task <ResponseAC> UpdateProvider(long userId, ProviderAC providerAC, string loginUserName)
        {
            ResponseAC responeAC = new ResponseAC();

            if (await _dbTeleBilling_V01Context.Provider.FirstOrDefaultAsync(x => x.Id != providerAC.Id && x.ContractNumber.ToLower() == providerAC.ContractNumber.ToLower() && !x.IsDelete) == null)
            {
                TeleBillingUtility.Models.Provider provider = await _dbTeleBilling_V01Context.Provider.FirstOrDefaultAsync(x => x.Id == providerAC.Id);

                if (provider != null)
                {
                    #region Transaction Log Entry
                    if (provider.TransactionId == null)
                    {
                        provider.TransactionId = _iLogManagement.GenerateTeleBillingTransctionID();
                    }

                    var jsonSerailzeObj = JsonConvert.SerializeObject(provider);
                    await _iLogManagement.SaveRequestTraseLog(Convert.ToInt64(provider.TransactionId), userId, Convert.ToInt64(EnumList.TransactionTraseLog.UpdateRecord), jsonSerailzeObj);

                    #endregion

                    provider             = _mapper.Map(providerAC, provider);
                    provider.UpdatedBy   = userId;
                    provider.UpdatedDate = DateTime.Now;

                    _dbTeleBilling_V01Context.Update(provider);
                    await _dbTeleBilling_V01Context.SaveChangesAsync();

                    #region Update Provider Service List
                    List <Providerservice> providerServiceList = await _dbTeleBilling_V01Context.Providerservice.Where(x => x.ProviderId == providerAC.Id && !x.IsDelete).ToListAsync();

                    if (providerServiceList.Any())
                    {
                        List <Providerservice> dummyProviderServices = new List <Providerservice>();
                        foreach (var item in providerServiceList)
                        {
                            item.IsDelete = true;
                            dummyProviderServices.Add(item);
                        }

                        _dbTeleBilling_V01Context.UpdateRange(dummyProviderServices);
                        _dbTeleBilling_V01Context.SaveChanges();
                    }

                    providerServiceList = new List <Providerservice>();
                    foreach (var serviceType in providerAC.ServiceTypes)
                    {
                        Providerservice providerService = new Providerservice();
                        providerService.ServiceTypeId = serviceType.Id;
                        providerService.ProviderId    = providerAC.Id;
                        providerServiceList.Add(providerService);
                    }

                    await _iLogManagement.SaveAuditActionLog((int)EnumList.AuditLogActionType.EditProvider, loginUserName, userId, "Provider(" + provider.Name + ")", (int)EnumList.ActionTemplateTypes.Edit, provider.Id);

                    await _dbTeleBilling_V01Context.AddRangeAsync(providerServiceList);

                    await _dbTeleBilling_V01Context.SaveChangesAsync();

                    #endregion

                    #region Update Provider Contact Details
                    List <Providercontactdetail> providerContactDetails = await _dbTeleBilling_V01Context.Providercontactdetail.Where(x => x.ProviderId == provider.Id && !x.IsDeleted).ToListAsync();

                    if (providerContactDetails.Any())
                    {
                        List <Providercontactdetail> providerContactDetailList = new List <Providercontactdetail>();
                        foreach (var providerContact in providerContactDetails)
                        {
                            providerContact.IsDeleted = true;
                            providerContactDetailList.Add(providerContact);
                        }
                    }


                    #region Added Provider Contact Detail
                    providerContactDetails = new List <Providercontactdetail>();
                    foreach (var item in providerAC.ProviderContactDetailACList)
                    {
                        Providercontactdetail providerContectDatail = new Providercontactdetail();
                        providerContectDatail            = _mapper.Map <Providercontactdetail>(item);
                        providerContectDatail.Id         = 0;
                        providerContectDatail.ProviderId = provider.Id;
                        providerContactDetails.Add(providerContectDatail);
                    }

                    await _dbTeleBilling_V01Context.AddRangeAsync(providerContactDetails);

                    await _dbTeleBilling_V01Context.SaveChangesAsync();

                    #endregion

                    #endregion

                    responeAC.Message    = _iStringConstant.ProviderUpdatedSuccessfully;
                    responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Success);
                }
            }
            else
            {
                responeAC.Message    = _iStringConstant.ContractNumberExists;
                responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
            }
            return(responeAC);
        }