public SubscriptionSmsStatus MarkMessageActivityInDB(        
    String from, String to, String conversationId, String text, Boolean readStatus,
    DateTime updateTime, String prevConvFrom, DateTime prevConvUpdateTime, bool isSmsBased, String XmppUser,  
    String price, String externalID, String direction, smsfeedbackEntities dbContext)
 {
     string updateAddConversationResult = UpdateOrAddConversation(from, to, conversationId, text, readStatus, updateTime, isSmsBased, dbContext);
     string addMessageResult = updateAddConversationResult;
     int newInsertedMessageID = -1;
        bool warningLimitReached = false;
        bool spendingLimitReached = false;
     if (updateAddConversationResult.Equals(JsonReturnMessages.OP_SUCCESSFUL))
     {
        //compute direction and
        ConversationUtilities.Direction dir = ConversationUtilities.Direction.from;
        if(direction == Constants.DIRECTION_OUT) { dir = ConversationUtilities.Direction.to;}
        newInsertedMessageID = AddMessage(from, to, conversationId, text, readStatus, updateTime,dir, prevConvFrom, prevConvUpdateTime, isSmsBased, XmppUser, price, externalID, dbContext);
     }
        //we added the message - now if SMS based, mark this
     if ((newInsertedMessageID > 0) && isSmsBased && (direction == Constants.DIRECTION_OUT)) {
        var sd = UpdateSMSstatusForCompany(from,price, dbContext);
        //if required emit warnings
        bool warningsRequired = sd.WarningsRequired();
        if (warningsRequired && sd != null)
        {
           var mailer = new SmsFeedback_Take4.Mailers.WarningMailer();
           var companyName = sd.Companies.FirstOrDefault().Name;
           if (sd.CanSendSMS)
           {
              warningLimitReached = true;
              //we need to send warnings
              System.Net.Mail.MailMessage msgPrimary = mailer.WarningEmail(sd, sd.PrimaryContact.Email, sd.PrimaryContact.Name, sd.PrimaryContact.Surname);
              msgPrimary.Send();
              System.Net.Mail.MailMessage msgSecondary = mailer.WarningEmail(sd, sd.SecondaryContact.Email, sd.SecondaryContact.Name, sd.SecondaryContact.Surname);
              msgSecondary.Send();
           }
           else
           {
              spendingLimitReached = true;
              //we need to send SpendingLimit reached emails
              System.Net.Mail.MailMessage msgPrimary = mailer.SpendingLimitReachedEmail(sd, sd.PrimaryContact.Email, sd.PrimaryContact.Name, sd.PrimaryContact.Surname);
              msgPrimary.Send();
              System.Net.Mail.MailMessage msgSecondary = mailer.SpendingLimitReachedEmail(sd, sd.PrimaryContact.Email, sd.SecondaryContact.Name, sd.SecondaryContact.Surname);
              msgSecondary.Send();
           }
        }
     }
     SubscriptionSmsStatus messageStatus = new SubscriptionSmsStatus(newInsertedMessageID, !spendingLimitReached, warningLimitReached, spendingLimitReached);
     return messageStatus;
 }
        private JsonResult SendSmsMessageAndUpdateDb(
           String from, String to, String convId, String text, String xmppUser, smsfeedbackEntities lContextPerRequest, 
           String prevConvFrom, DateTime prevConvUpdateTime)
        {
            SubscriptionSmsStatus messageStatus = new SubscriptionSmsStatus(-1, false, false, false);
            //var messageCanBeSent = SMSRepository.SendMessage(from, to, text, lContextPerRequest, (msgResponse) =>
            var status = SMSRepository.SendMessage(from, to, text,lContextPerRequest);
               //status will tell you if message was sent or not

               //the rest should come later....
            if (status != null) // null stands for  - could not sent SMS - no credit
            {
               //the idea is that a message can be sent but somewhere on Nexmo/Twilio there was a problem....
               messageStatus = mEFInterface.MarkMessageActivityInDB(from,
                      to,
                      convId,
                      text,
                      true,
                      status.DateSent,
                      prevConvFrom,
                      prevConvUpdateTime,
                      true,
                      xmppUser,
                      status.Price,
                      status.ExternalID,
                      Constants.DIRECTION_OUT, lContextPerRequest);
               messageStatus.MessageSent = status.MessageSent != null ? true : false;
            }

            //messageStatus = mEFInterface.MarkMessageActivityInDB(from,
            //           to,
            //           convId,
            //           text,
            //           true,
            //           new DateTime(2013,1,1,10,10,10,100),
            //           prevConvFrom,
            //           prevConvUpdateTime,
            //           true,
            //           xmppUser,
            //           "10",
            //           "115",
            //           Constants.DIRECTION_OUT, lContextPerRequest);
            return Json(messageStatus, JsonRequestBehavior.AllowGet);
        }
        public SubscriptionSmsStatus GetCompanySubscriptionSMSStatus(string loggedInUser, smsfeedbackEntities dbContext)
        {
            var user = (from usr in dbContext.Users where usr.UserName == loggedInUser select usr).FirstOrDefault();
               if (user != null)
               {
              Company company = user.Company;
              var sd = company.SubscriptionDetail;
              bool warningReached = false;
              bool spendingReached = false;
              SubscriptionSmsStatus status = new SubscriptionSmsStatus(0,false,warningReached,spendingReached);
              bool warningsRequired = sd.WarningsRequired();

              if (warningsRequired)
              {
                 if (sd.CanSendSMS)
                 {
                    status.WarningLimitReached = true;
                    status.WarningLimitReachedMessage = String.Format(Resources.Global.subscriptionWarningReached, sd.SpentThisMonth, sd.DefaultCurrency, sd.SpendingLimit, sd.GetNextBillingDate(DateTime.Now).ToLongDateString());
                 }
                 else {
                    status.SpendingLimitReached = true;
                    status.SpendingLimitReachedMessage = String.Format(Resources.Global.subscriptionSpendingReached, sd.SpendingLimit, sd.DefaultCurrency, sd.GetNextBillingDate(DateTime.Now).ToLongDateString());
                 }
              }
              return status;
               }
               logger.ErrorFormat("Invalid user id: {0}", loggedInUser);
               return null;
        }