private ActionResult ModifyState(Subscription s, SubscriptionState state)
        {
            if (ModelState.IsValid)
            {
                DateTime     time             = s.Timestamp;
                Subscription subscription     = db.Subscriptions.Find(s.SubscriptionId);
                long         stationAccountId = subscription.StationAccountId;
                subscription.State     = state;
                subscription.Timestamp = s.Timestamp;
                subscription.Notes     = s.Notes;

                db.Entry(subscription).State = EntityState.Modified;

                var subscriptionHistory = new SubscriptionHistory()
                {
                    UnitPrice      = subscription.UnitPrice,
                    State          = state,
                    Quantity       = subscription.CurrentQuantity,
                    Notes          = subscription.Notes,
                    SubscriptionId = subscription.SubscriptionId,
                    Timestamp      = time
                };

                db.SubscriptionHistories.Add(subscriptionHistory);

                db.SaveChanges();
                return(RedirectToAction("Index", new { accountId = stationAccountId }));
            }

            return(View("Invalid state."));
        }
        public ActionResult Create([Bind(Include = "SubscriptionId,StationAccountId,StartQuantity,UnitPrice,Notes,Timestamp")] Subscription subscription)
        {
            DateTime time = subscription.Timestamp;

            if (ModelState.IsValid)
            {
                subscription.Timestamp       = time;
                subscription.CurrentQuantity = subscription.StartQuantity;
                subscription.State           = SubscriptionState.Created;

                db.Subscriptions.Add(subscription);
                db.SaveChanges();

                var subscriptionHistory = new SubscriptionHistory()
                {
                    UnitPrice      = subscription.UnitPrice,
                    State          = SubscriptionState.Created,
                    Quantity       = subscription.StartQuantity,
                    Notes          = subscription.Notes,
                    SubscriptionId = subscription.SubscriptionId,
                    Timestamp      = time
                };

                db.SubscriptionHistories.Add(subscriptionHistory);
                db.SaveChanges();

                return(RedirectToAction("Index", new { accountId = subscription.StationAccountId }));
            }

            ViewBag.StationAccountId = new SelectList(db.StationAccounts, "StationAccountId", "StationName", subscription.StationAccountId);
            return(View(subscription));
        }
Beispiel #3
0
        public int UpdateSubscriptionHistoryMailSent(SubscriptionHistory subscriptionHistory)
        {
            using (DemsifyEntities dataContext = new DemsifyEntities())
            {
                ObjectParameter result = new ObjectParameter("Result", typeof(int));

                dataContext.SubscriptionHistoryMailSentUpdate(subscriptionHistory.SubscriptionHistoryId, subscriptionHistory.IsMailSentToUser, subscriptionHistory.IsMailSentToAdmin, subscriptionHistory.ModifiedBy, result);

                return(Convert.ToInt32(result.Value));
            }
        }
        public ActionResult PayConfirmed([Bind(Include = "SubscriptionId, Notes, Timestamp")] Subscription s)
        {
            if (ModelState.IsValid)
            {
                DateTime time = s.Timestamp;

                Subscription   subscription = db.Subscriptions.Find(s.SubscriptionId);
                long           accountId    = subscription.StationAccountId;
                StationAccount station      = db.StationAccounts.Find(accountId);


                subscription.State           = SubscriptionState.Paid;
                subscription.Timestamp       = time;
                db.Entry(subscription).State = EntityState.Modified;


                station.Deposite       -= subscription.UnitPrice * subscription.StartQuantity;
                db.Entry(station).State = EntityState.Modified;

                var subscriptionHistory = new SubscriptionHistory()
                {
                    UnitPrice      = subscription.UnitPrice,
                    State          = SubscriptionState.Paid,
                    Quantity       = subscription.StartQuantity,
                    Notes          = subscription.Notes,
                    SubscriptionId = subscription.SubscriptionId,
                    Timestamp      = time
                };

                db.SubscriptionHistories.Add(subscriptionHistory);

                var depositeHistory = new DepositeHistory()
                {
                    Amount           = subscription.UnitPrice * subscription.StartQuantity * -1,
                    ChangeType       = DepositeChangeType.Pay,
                    StationAccountId = accountId,
                    Description      = string.Format("Pay for Subscription: {0}.", subscription.SubscriptionId),
                    Timestamp        = time
                };

                db.DepositeHistories.Add(depositeHistory);

                db.SaveChanges();
                return(RedirectToAction("Index", new { accountId = accountId }));
            }

            return(View("The state is not valid."));
        }
        public async Task ProcessCryptoSubSuccess(CryptoPaymentInfo cryptoPaymentInfo)
        {
            var client = new HttpClient();
            var result = await client.PostAsJsonAsync($"{_configuration["Service:CryptoPayment:VerifyApiValidationToken"]}", cryptoPaymentInfo);

            if (result.IsSuccessStatusCode)
            {
                var payment = await _functionalUnitOfWork.PaymentRepository.FirstOrDefault(a => a.OrderNumber == cryptoPaymentInfo.OrderNumber);

                var subscription = await _functionalUnitOfWork.SubscriptionPackRepository.FirstOrDefault(x => x.Id == payment.SubscriptionId.Value);

                payment.IsValidated   = true;
                payment.ValidatedDate = DateTime.Now;
                payment.OrderNumber   = cryptoPaymentInfo.OrderNumber;
                payment.TokenId       = cryptoPaymentInfo.Token;

                SubscriptionHistory subHisto = new SubscriptionHistory();
                subHisto.Created            = DateTime.Now;
                subHisto.EndSubscription    = DateTime.Now.AddMonths(subscription.Duration);
                subHisto.Enable             = true;
                subHisto.UserId             = Guid.Parse(payment.UserId);
                subHisto.SubscriptionPackId = payment.SubscriptionId.Value;
                await _functionalUnitOfWork.SubscriptionHistoryRepository.Add(subHisto);

                var user = await _userManager.FindByIdAsync(payment.UserId);

                user.SubscriptionStartDate = user.SubscriptionStartDate.HasValue ? user.SubscriptionStartDate : subHisto.Created;
                if (user.SubscriptionEndDate.HasValue)
                {
                    if (user.SubscriptionEndDate.Value < DateTime.Now)
                    {
                        user.SubscriptionEndDate = DateTime.Now.AddMonths(subscription.Duration);
                    }
                    else
                    {
                        user.SubscriptionEndDate = user.SubscriptionEndDate.Value.AddMonths(subscription.Duration);
                    }
                }
                else
                {
                    user.SubscriptionEndDate = subHisto.EndSubscription;
                }

                await _userManager.UpdateAsync(user);
            }
        }
Beispiel #6
0
        public async Task <IActionResult> Select(SubscriptionPack subscriptionPack)
        {
            var user = await _userManager.FindByNameAsync(User.UserName());

            if (user == null)
            {
                return(BadRequest());
            }

            SubscriptionHistory newSub = new SubscriptionHistory();

            newSub.Enable             = true;
            newSub.Created            = DateTime.Now;
            newSub.EndSubscription    = DateTime.MaxValue;
            newSub.SubscriptionPackId = subscriptionPack.Id;
            newSub.UserId             = Guid.Parse(user.Id);

            await _functionalUnitOfWork.SubscriptionHistoryRepository.Add(newSub);

            return(Ok());
        }
Beispiel #7
0
        public IHttpActionResult UpdateSubscription(UpdateSubscriptionRequest updateSubscriptionRequest)
        {
            var responses = new Responses();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (Utility.UserId < 0)
                {
                    return(BadRequest(Utility.INVALID_USER));
                }

                var Subscription = new Subscription()
                {
                    SubscriptionId = updateSubscriptionRequest.SubscriptionId,
                    StartDate      = updateSubscriptionRequest.StartDate,
                    PaymentDate    = updateSubscriptionRequest.PaymentDate,
                    IsExpired      = updateSubscriptionRequest.IsExpired,
                    IsActive       = updateSubscriptionRequest.IsActive,
                    ModifiedBy     = Utility.UserId
                };
                int result = iSubscription.UpdateSubscription(Subscription);
                switch (result)
                {
                case -2:
                    responses.Status      = Utility.ERROR_STATUS_RESPONSE;
                    responses.Description = "Subscription already exists.";
                    break;

                case -3:
                    responses.Status      = Utility.ERROR_STATUS_RESPONSE;
                    responses.Description = "Subscription doesn't exist.";
                    break;

                default:
                    if (result > 0)
                    {
                        string userName    = string.Empty;
                        var    userProfile = new UserProfile()
                        {
                            UserId = updateSubscriptionRequest.UserId
                        };
                        var userProfiles = iUserProfile.GetUserProfile(userProfile);
                        if (userProfiles != null)
                        {
                            userName = userProfiles.First().UserName;

                            var SubscriptionEmailHtmlCode = System.IO.File.ReadAllText(string.Format("{0}", HttpContext.Current.Server.MapPath(string.Format("{0}{1}", ConfigurationManager.AppSettings["EmailTemplatePath"], (updateSubscriptionRequest.StartDate != null ? ConfigurationManager.AppSettings["SubscriptionUpdateEmailTemplateForUser"] : ConfigurationManager.AppSettings["UnsubscriptionEmailTemplateForUser"])))));

                            var mainTemplateHtmlCode = System.IO.File.ReadAllText(string.Format("{0}", HttpContext.Current.Server.MapPath(string.Format("{0}{1}", ConfigurationManager.AppSettings["EmailTemplatePath"], ConfigurationManager.AppSettings["MainEmailTemplate"]))));
                            mainTemplateHtmlCode = mainTemplateHtmlCode.Replace("[SITEURL]", ConfigurationManager.AppSettings["SiteUrl"]);
                            mainTemplateHtmlCode = mainTemplateHtmlCode.Replace("[SITENAME]", ConfigurationManager.AppSettings["SiteName"]);
                            mainTemplateHtmlCode = mainTemplateHtmlCode.Replace("[PAGECONTENT]", SubscriptionEmailHtmlCode);

                            string subject          = "Subscription | Demystify Fema";
                            string body             = mainTemplateHtmlCode;
                            string displayName      = ConfigurationManager.AppSettings["SiteName"];
                            string emailTo          = userName;
                            bool   isMailSentToUser = Utility.SendMail(emailTo, string.Empty, string.Empty, subject, body, displayName, string.Empty, true);

                            try
                            {
                                var subscriptionHistory = new SubscriptionHistory()
                                {
                                    SubscriptionHistoryId = result,
                                    IsMailSentToUser      = isMailSentToUser,
                                    ModifiedBy            = Utility.UserId
                                };
                                iSubscription.UpdateSubscriptionHistoryMailSent(subscriptionHistory);
                            }
                            catch (Exception ex)
                            {
                                Utility.WriteLog("AddSubscription", updateSubscriptionRequest, "Error while updating Subscription History mail sent. (SubscriptionUserController)", ex.ToString());
                            }
                        }

                        responses.Status      = Utility.SUCCESS_STATUS_RESPONSE;
                        responses.Description = "Subscription updated successfully.";
                    }
                    else
                    {
                        responses.Status      = Utility.ERROR_STATUS_RESPONSE;
                        responses.Description = "Error while updating Subscription.";
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                responses.Status      = Utility.ERROR_STATUS_RESPONSE;
                responses.Description = "Error while updating Subscription.";

                Utility.WriteLog("UpdateSubscription", updateSubscriptionRequest, "Error while updating Subscription. (SubscriptionAdminController)", ex.ToString());
            }
            return(Ok(responses));
        }
        public ActionResult DeliverConfirmed([Bind(Include = "SubscriptionId, Quantity,ClientSubscriptionId, Notes, Timestamp")] SubscriptionDeliverViewModel model)
        {
            if (ModelState.IsValid)
            {
                DateTime           time         = model.Timestamp;
                Subscription       subscription = db.Subscriptions.Find(model.SubscriptionId);
                ClientSubscription cs           = db.ClientSubscriptions.Find(model.ClientSubscriptionId);
                long csId = cs.ClientSubscriptionId;

                if (cs.State != ClientSubscriptionState.Paid)
                {
                    if (cs.State != ClientSubscriptionState.PartialDelivered)
                    {
                        throw new Exception("The client subscription state must be 'paid'.");
                    }
                }

                if (cs.StationAccountId != subscription.StationAccountId)
                {
                    throw new Exception("The station does not match.");
                }

                string clientAccountName = cs.Client.ClientAccountName;
                string vesselName        = cs.VesselName;


                long accountId = subscription.StationAccountId;

                float leftQuantity = subscription.CurrentQuantity - model.Quantity;

                if (leftQuantity == 0)
                {
                    subscription.State = SubscriptionState.Delivered;
                }
                else
                {
                    subscription.State = SubscriptionState.PartialDelivered;
                }

                subscription.CurrentQuantity = leftQuantity;
                subscription.Timestamp       = time;
                subscription.Notes           = model.Notes;

                db.Entry(subscription).State = EntityState.Modified;

                var subscriptionHistory = new SubscriptionHistory()
                {
                    UnitPrice            = subscription.UnitPrice,
                    State                = subscription.State,
                    Quantity             = model.Quantity,
                    Notes                = model.Notes,
                    ClientSubscriptionId = model.ClientSubscriptionId,
                    ClientAccountName    = clientAccountName,
                    VesselName           = vesselName,
                    SubscriptionId       = subscription.SubscriptionId,
                    Timestamp            = time
                };

                db.SubscriptionHistories.Add(subscriptionHistory);

                float csLeftQuantity = cs.CurrentQuantity - model.Quantity;

                if (csLeftQuantity == 0)
                {
                    cs.State = ClientSubscriptionState.Delivered;
                }
                else
                {
                    cs.State = ClientSubscriptionState.PartialDelivered;
                }

                cs.CurrentQuantity = csLeftQuantity;
                cs.Timestamp       = time;
                cs.Notes           = model.Notes;

                db.Entry(cs).State = EntityState.Modified;

                var clientSubscriptionHistory = new ClientSubscriptionHistory()
                {
                    UnitPrice            = subscription.UnitPrice,
                    State                = cs.State,
                    Quantity             = model.Quantity,
                    Notes                = model.Notes,
                    VesselName           = vesselName,
                    ClientSubscriptionId = cs.ClientSubscriptionId,
                    Timestamp            = time
                };

                db.ClientSubscriptionHistories.Add(clientSubscriptionHistory);

                db.SaveChanges();
                return(RedirectToAction("Index", new { accountId = accountId }));
            }

            return(View("Invalid state."));
        }
 public Task Remove(SubscriptionHistory network)
 {
     return(Task.Run(() => FunctionalDbContext.SubscriptionHistories.Remove(network.Id)));
 }
 public Task <SubscriptionHistory> Add(SubscriptionHistory SubscriptionHistory)
 {
     return(Task.Run(() => FunctionalDbContext.SubscriptionHistories.Add(SubscriptionHistory)));
 }