public CreditDeductionType GetCreditSMSDeductionDetail()
 {
     using (DataContext context = new DataContext())
     {
         CreditDeductionType creditDeductiontype = context.CreditDeductionTypes.FirstOrDefault(x => x.IsActive == true && x.Id == (int)Constant.CreditDeductionType.SMSCharges);
         return(creditDeductiontype);
     }
 }
        protected override void Seed(CabicsSubscription.Service.DataContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.


            /// Defualt Client Insertion
            Client client = new Client();

            client.Id              = 1;
            client.Name            = "Cabics";
            client.Description     = "Cabics";
            client.encryptedstring = Guid.NewGuid().ToString();
            context.Clients.AddOrUpdate(client);
            context.SaveChanges();

            /// Defualt Client Insertion
            CreditDeductionType dailyCreditDeductionType = new CreditDeductionType();

            dailyCreditDeductionType.Id          = 1;
            dailyCreditDeductionType.Name        = "Daily charges";
            dailyCreditDeductionType.Credit      = 50;
            dailyCreditDeductionType.IsActive    = true;
            dailyCreditDeductionType.CreatedDate = DateTime.Now;
            context.CreditDeductionTypes.AddOrUpdate(dailyCreditDeductionType);
            context.SaveChanges();

            /// Per Job Client Insertion
            CreditDeductionType jobCreditDeductionType = new CreditDeductionType();

            jobCreditDeductionType.Id          = 2;
            jobCreditDeductionType.Name        = "Per Job charges";
            jobCreditDeductionType.Credit      = 3;
            jobCreditDeductionType.IsActive    = true;
            jobCreditDeductionType.CreatedDate = DateTime.Now;
            context.CreditDeductionTypes.AddOrUpdate(jobCreditDeductionType);
            context.SaveChanges();

            /// Per SMS Client Insertion
            CreditDeductionType smsCreditDeductionType = new CreditDeductionType();

            smsCreditDeductionType.Id          = 3;
            smsCreditDeductionType.Name        = "SMS Credit charges";
            smsCreditDeductionType.Credit      = 6;
            smsCreditDeductionType.IsActive    = true;
            smsCreditDeductionType.CreatedDate = DateTime.Now;
            context.CreditDeductionTypes.AddOrUpdate(smsCreditDeductionType);
            context.SaveChanges();
        }
        //public UtilizeSubscriptionResponse UtilizeSubscription(UtilizeSubscriptionModel utilizeSubscriptionModel)
        public async Task <IHttpActionResult> UtilizeSubscription(UtilizeSubscriptionModel utilizeSubscriptionModel)
        {
            UtilizeSubscriptionDto utilizeSubscriptionResponse = new UtilizeSubscriptionDto();

            try
            {
                //AccountService accountService = new AccountService();
                //SubscriptionService subscriptionService = new SubscriptionService();

                Account account     = null;
                string  email       = "";
                int     cabOfficeId = 0;
                if (Request.Headers.Contains("CabOfficeEmail"))
                {
                    IEnumerable <string> headerValues = Request.Headers.GetValues("CabOfficeEmail");
                    email = headerValues.FirstOrDefault();
                }

                if (Request.Headers.Contains("CabOfficeId"))
                {
                    IEnumerable <string> headerValues = Request.Headers.GetValues("CabOfficeId");
                    cabOfficeId = Convert.ToInt32(headerValues.FirstOrDefault());
                }

                account = accountService.getCabOfficeByEmailAndCabOfficeId(email, cabOfficeId);



                if (account == null)
                {
                    utilizeSubscriptionResponse.Result    = false;
                    utilizeSubscriptionResponse.IsSuccess = true;
                    utilizeSubscriptionResponse.Error     = Constant.APIError.AccountNotFound.ToString();
                    utilizeSubscriptionResponse.ErrorCode = (int)Constant.APIError.AccountNotFound;
                    return(Ok(utilizeSubscriptionResponse));
                }

                Subscription subscription = subscriptionService.GetSubscriptionBySubscriptionId(Convert.ToInt32(account.CurrentSubscriptionId));

                if (subscription == null)
                {
                    utilizeSubscriptionResponse.Result    = false;
                    utilizeSubscriptionResponse.IsSuccess = true;
                    utilizeSubscriptionResponse.Error     = Constant.APIError.NoSubscriptionFound.ToString();
                    utilizeSubscriptionResponse.ErrorCode = (int)Constant.APIError.NoSubscriptionFound;
                    return(Ok(utilizeSubscriptionResponse));
                }

                int subscriptionType = subscription.SubscriptionTypeId;

                if (subscriptionType == (int)Constant.SubscriptionType.Monthly)
                {
                    if (subscription.EndDate < DateTime.Now)
                    {
                        utilizeSubscriptionResponse.Result    = false;
                        utilizeSubscriptionResponse.IsSuccess = true;
                        utilizeSubscriptionResponse.Error     = Constant.APIError.SubscriptionExpired.ToString();
                        utilizeSubscriptionResponse.ErrorCode = (int)Constant.APIError.SubscriptionExpired;
                        return(Ok(utilizeSubscriptionResponse));
                    }

                    if (utilizeSubscriptionModel.CreditUtilizationType == (int)Constant.CreditDeductionType.SMSCharges)
                    {
                        if (subscription.RemainingSmsCreditPurchase <= 0)
                        {
                            utilizeSubscriptionResponse.Result    = false;
                            utilizeSubscriptionResponse.IsSuccess = true;
                            utilizeSubscriptionResponse.Error     = Constant.APIError.NotEnoughMonthlySMSCredit.ToString();
                            utilizeSubscriptionResponse.ErrorCode = (int)Constant.APIError.NotEnoughMonthlySMSCredit;
                            return(Ok(utilizeSubscriptionResponse));
                        }

                        CreditDeductionType smsCreditDeduction = subscriptionService.GetCreditSMSDeductionDetail();
                        subscriptionService.UpdateSubscriptionRemainingMonthlySMSCredit(subscription.Id, smsCreditDeduction.Credit, (int)Constant.CreditDeductionType.SMSCharges);
                    }
                }
                else
                {
                    if (subscription.RemainingCredit <= 0)
                    {
                        utilizeSubscriptionResponse.Result    = false;
                        utilizeSubscriptionResponse.IsSuccess = true;
                        utilizeSubscriptionResponse.Error     = Constant.APIError.NotEnoughCredit.ToString();
                        utilizeSubscriptionResponse.ErrorCode = (int)Constant.APIError.NotEnoughCredit;
                        return(Ok(utilizeSubscriptionResponse));
                    }

                    if (utilizeSubscriptionModel.CreditUtilizationType == (int)Constant.CreditDeductionType.PerJobCharges)
                    {
                        CreditDeductionType jobCreditDeduction = subscriptionService.GetCreditJobDeductionDetail();
                        subscriptionService.UpdateSubscriptionCredit(subscription.Id, jobCreditDeduction.Credit, (int)Constant.CreditDeductionType.PerJobCharges);
                    }
                    else if (utilizeSubscriptionModel.CreditUtilizationType == (int)Constant.CreditDeductionType.SMSCharges)
                    {
                        CreditDeductionType smsCreditDeduction = subscriptionService.GetCreditSMSDeductionDetail();
                        subscriptionService.UpdateSubscriptionCredit(subscription.Id, smsCreditDeduction.Credit, (int)Constant.CreditDeductionType.SMSCharges);
                    }
                }



                utilizeSubscriptionResponse.Error     = "";
                utilizeSubscriptionResponse.ErrorCode = 0;
                utilizeSubscriptionResponse.IsSuccess = true;
                utilizeSubscriptionResponse.Result    = true;
                return(Ok(utilizeSubscriptionResponse));
            }
            catch (Exception ex)
            {
                utilizeSubscriptionResponse.Error     = ex.ToString();
                utilizeSubscriptionResponse.ErrorCode = (int)Constant.APIError.Exception;
                utilizeSubscriptionResponse.IsSuccess = false;
                utilizeSubscriptionResponse.Result    = false;
                return(Ok(utilizeSubscriptionResponse));
            }
        }