public ActionResult CompanyBillingInfo(SubscriptionDetail details)
        {
            if (ModelState.IsValid)
             {

            ViewBag.SaveMessage = Resources.Global.settingWpConfigSavedSuccessfuly;
            var sd = context.SubscriptionDetails.Find(details.Id);
            context.Entry(sd).CurrentValues.SetValues(details);
            var primaryContact = context.Contacts.Find(details.PrimaryContact_Id);
            if (primaryContact != null)
            {
               context.Entry(primaryContact).CurrentValues.SetValues(details.PrimaryContact);
            }
            //in case they are pointing to same contact
            var secondaryContact = context.Contacts.Find(details.SecondaryContact_Id);

            if (secondaryContact != null)
            {
               context.Entry(secondaryContact).CurrentValues.SetValues(details.SecondaryContact);
            }
            context.SaveChanges();
             }

             return View(details);
        }
 public virtual MailMessage SpendingLimitReachedEmail(SubscriptionDetail sd, string toEmailAddress, string name, string surname)
 {
     var mailMessage = new MailMessage { Subject = "Spending limit reached for your company" };
      mailMessage.Priority = MailPriority.High;
      mailMessage.To.Add(new MailAddress(toEmailAddress, name + " " + surname));
      mailMessage.To.Add(toEmailAddress);
      ViewBag.CompanyName = sd.Companies.FirstOrDefault().Name;
      ViewBag.DearSir = "Mr./Ms. " + surname;
      var today = DateTime.Now;
      ViewBag.ResetDate = sd.GetNextBillingDate(today).ToLongDateString();
      ViewData.Model = sd;
      PopulateBody(mailMessage, viewName: "SpendingLimitReachedEmail");
      return mailMessage;
 }
 public void UpdateSMSForSubscription(String price, SubscriptionDetail sd, smsfeedbackEntities dbContext)
 {
     /**
     *RemainingSMS - if still > 0 decrease
     *SpentAmount - increase with price only if RemainingSMS == 0
     */
        bool saveFailed = true;
        do
        {
       saveFailed = false;
       try
       {
          if (sd.RemainingSMS > 0)
          {
             sd.RemainingSMS = sd.RemainingSMS - 1;
          }
          else
          {
             decimal priceAsDecimal = 0;
             try
              {
                 priceAsDecimal = Decimal.Parse(price, CultureInfo.InvariantCulture);
              }
             catch(Exception ex)
             {
                logger.ErrorFormat("Price not a valid decimal. ErrorType: {0}, ErrorMessage: {1}",ex.GetType().ToString(), ex.Message);
             }
             sd.SpentThisMonth += priceAsDecimal;
          }
          dbContext.SaveChanges();
       }
       catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException ex)
       {
          saveFailed = true;
          ex.Entries.Single().Reload();
       }
        } while (saveFailed);
 }
        public void GetNextBillingDate_ComparisonAfterBillingDay_AND_ComparisonDayIsInDecember_DateIsNextYear()
        {
            var compTime = new DateTime(2012, 12, 17);
             var billingDay = 15;
             var sd = new SubscriptionDetail() { BillingDay = billingDay };
             Company newCompany = new Company() { Name = "Random" };
             newCompany.Invoices.Add(new Invoice() { DateCreated = compTime.Subtract(new TimeSpan(35, 0, 0, 0)), AutoGenerated = true });
             sd.Companies.Add(newCompany);

             var nextBillingDate = sd.GetNextBillingDate(compTime);
             var expectedBillingDate = new DateTime(2013,01,billingDay);
             Assert.AreEqual(expectedBillingDate.Date, nextBillingDate.Date);
        }
 public void WarningsRequired_StillHaveRemainingSMS_returnsFalse()
 {
     var sd = new SubscriptionDetail();
      sd.RemainingSMS = 100;
      bool result = sd.WarningsRequired();
      Assert.IsFalse(result,"If we still have subscriptionSMS then no warnings are required");
 }
 public void WarningsRequired_NotEnoughToSendAnotherSMS_returnsTrue()
 {
     var sd = new SubscriptionDetail();
      sd.RemainingSMS = 0;
      sd.ExtraAddedCreditThisMonth = 0;
      sd.RemainingCreditFromPreviousMonth = 0;
      sd.SpentThisMonth = 1;
      sd.WarningLimit = 1.1m;
      bool result = sd.WarningsRequired();
      Assert.IsTrue(result, "If not enough to send another SMS then warnings are required");
 }
 public void WarningsRequired_NoRemainingSMS_stillHaveCreditFromLastMonth_returnsFalse()
 {
     var sd = new SubscriptionDetail();
      sd.RemainingSMS = 0;
      sd.ExtraAddedCreditThisMonth = 0;
      sd.RemainingCreditFromPreviousMonth = 0.1m;
      bool result = sd.WarningsRequired();
      Assert.IsFalse(result, "If we still have Credit no warnings are required");
 }
 public void WarningsRequired_NoRemainingSMS_noCredit_belowWarningLimit_returnsFalse()
 {
     var sd = new SubscriptionDetail();
      sd.RemainingSMS = 0;
      sd.ExtraAddedCreditThisMonth = 0;
      sd.RemainingCreditFromPreviousMonth = 0;
      sd.SpentThisMonth = 1;
      sd.WarningLimit = 1.3m;
      sd.SpendingLimit = 1.5m;
      bool result = sd.WarningsRequired();
      Assert.IsFalse(result, "No Warnings required if below warning limit");
 }
 public void WarningsRequired_NoRemainingSMS_noCredit_atWarningLimit_returnsTrue()
 {
     var sd = new SubscriptionDetail();
      sd.RemainingSMS = 0;
      sd.ExtraAddedCreditThisMonth = 0;
      sd.RemainingCreditFromPreviousMonth = 0;
      sd.SpentThisMonth = 1;
      sd.WarningLimit = 1;
      bool result = sd.WarningsRequired();
      Assert.IsTrue(result, "Warnings required if at warning limit");
 }
 public void Initialize()
 {
     sd = new SubscriptionDetail();
 }
 public void GetNextBillingDate_ComparisonOnBillingDay_AND_NoAutoInvoice_DateIsOnComparisonDay()
 {
     var compTime = new DateTime(2012, 01, 15);
      var billingDay = 15;
      var sd = new SubscriptionDetail() { BillingDay = billingDay };
      sd.Companies.Add(new Company() {Name= "Random"});
      var nextBillingDate = sd.GetNextBillingDate(compTime);
      var expectedBillingDate = compTime;
      Assert.AreEqual(expectedBillingDate.Date, nextBillingDate.Date);
 }
        public void GetNextBillingDate_ComparisonOnBillingDay_AND_AutoInvoiceOnThisDay_DateIsNextMonth()
        {
            var compTime = new DateTime(2012, 01, 15);
             var billingDay = 15;
             var sd = new SubscriptionDetail() { BillingDay = billingDay };
             Company newCompany = new Company() { Name = "Random" };
             newCompany.Invoices.Add(new Invoice() {DateCreated = compTime, AutoGenerated=true });
             newCompany.Invoices.Add(new Invoice() { DateCreated = compTime.Subtract(new TimeSpan(35,0,0,0)), AutoGenerated = true });
             sd.Companies.Add(newCompany);

             var nextBillingDate = sd.GetNextBillingDate(compTime);
             var expectedBillingDate = new DateTime(2012,02,billingDay);
             Assert.AreEqual(expectedBillingDate.Date, nextBillingDate.Date);
        }
 public void GetNextBillingDate_ComparisonIsBeforeBillingDay_DateIsThisMonth()
 {
     var compTime = new DateTime(2012, 01, 13);
      var billingDay = 14;
      var sd = new SubscriptionDetail() { BillingDay = billingDay };
      var nextBillingDate = sd.GetNextBillingDate(compTime);
      var expectedBillingDate = new DateTime(2012, 01, billingDay);
      Assert.AreEqual(expectedBillingDate.Date, nextBillingDate.Date);
 }
 public void SendSpendingLimitReachedEmail(string emailAddress)
 {
     var mailer = new WarningMailer();
      var sd = new SubscriptionDetail() { BillingDay = 1, DefaultCurrency = "EUR", SpentThisMonth = 2, SpendingLimit = 3 };
      sd.Companies.Add(new Company() { Name = "Ab Mobile Apps" });
      var msg = mailer.SpendingLimitReachedEmail(sd, emailAddress, "Dragos", "Andronic");
      msg.Send();
 }