public static string Registration(string number, string input)
        {
            db = new SMSDataClassesDataContext();
            string returnMessage = replyMessage("NREG", "NOK0");

            if (checkNumber(number))
            {
                return "Oop, Your number already exist to our record!Pleas try a new number";
            }

            if (checkCode(populate(input)))
            {
                string refCode = "CIA" + config.generateReferenceNo(4) + "-" + DateTime.Now.Year;
                SMS_Member m = new SMS_Member();
                m.ReferenceNo = refCode;
                m.Group_Name = "BAYANIHAN";
                m.Account_Number = number;
                m.Family_Name = Lastname;
                m.First_Name = Firstname;
                m.Town = Town;
                m.Sponsor_ID = Sponsor;
                m.CP_Number  = number;
                m.DateReg = config.receivedDateTime();
                m.Status = true;
                m.monthOf = DateTime.Now.Month;
                m.yearOf = DateTime.Now.Year;

                SMS_eMoney e = new SMS_eMoney();
                e.Account = number;
                e.Amount = 100;
                e.Status = true;

                if (!checkRef(refCode))
                {
                    try
                    {
                        db.SMS_Members.InsertOnSubmit(m);
                        db.SMS_eMoneys.InsertOnSubmit(e);
                        db.SubmitChanges();
                        returnMessage = replyMessage("NREG", "OK", Firstname);
                    }
                    catch (Exception ex)
                    {
                        return "Sorry, our system has encountered an error, please try again later. thank you!";
                    }
                }
                else
                {
                    returnMessage = replyMessage("NREG", "NOK1");
                }
            }
            return returnMessage;
        }
 public static double current_amount(string account)
 {
     db = new SMSDataClassesDataContext();
     var acnt = (from i in db.SMS_eMoneys.Where(i => i.ID == id(account)) select i).Take(1).FirstOrDefault();
     if (acnt != null)
     {
         isNotExist = true;
         return (double)acnt.Amount;
     }
     isNotExist = false;
     return 0;
 }
 public static bool now()
 {
     db = new SMSDataClassesDataContext();
     var que = (from i in db.SMS_QueuedBoxes.Where(i => i.Status == false) select i).Take(1).FirstOrDefault();
     if (que != null)
     {
         if (config.getLastDigit(que.Number, config.outBound))
         {
             Id = que.ID;
             Number = que.Number;
             Message = config.decrypt(que.Message);
             return true;
         }
     }
     return false;
 }
        public static string now(string number, string input)
        {
            db = new SMSDataClassesDataContext();
            Sender = number;
            if (IsSender_Exist(Sender))
            {
                string[] split = input.Split(' ');
                Receiver = split[1];

                if (IsSender_Exist(Receiver))
                {
                    double d = Convert.ToDouble(split[2]);
                    ReferenceNo = config.generateReferenceNo(6);

                    if (balance.current_amount(number) >= d)
                    {
                        if (!IsRef_Exist(ReferenceNo))
                        {
                            balance.update(Sender, balance.deduct(Sender, d));
                            if (save("sender", (decimal)d, ReferenceNo))
                            {
                                report("sender", d.ToString());
                            }

                            balance.update(Receiver, balance.increase(Receiver, d));
                            if (save("receiver", (decimal)d, ReferenceNo))
                            {
                                report("receiver", ReferenceNo);
                            }
                        }
                    }
                    else
                    {
                        save("insufficient");
                    }
                }
                else
                {
                    save("receiver_notExist");
                }
            }
            else
            {
                save("Sender_notExist");
            }
            return "OK";
        }
 static string check_If_open(DateTime value)
 {
     db = new SMSDataClassesDataContext();
     try
     {
         var x = (from i in db.SMS_BayanihanSums.Where(i => i.open_started >= value && value <= i.closing_time)
                  select i).Take(1).FirstOrDefault();
         if (x != null)
         {
             return x.bayanihan_ref;
         }
         return "Closed";
     }
     catch (Exception ex)
     {}
     return "not available at this time!";
 }
        public static bool delete(long id)
        {
            db = new SMSDataClassesDataContext();
            var x = (from i in db.SMS_ReceivedMsgs.Where(i => i.Id == id) select i).Take(1).FirstOrDefault();

            try
            {
                db.SMS_ReceivedMsgs.DeleteOnSubmit(x);
                db.SubmitChanges();
                config.log("Successful deleted in inbox");
                return true;
            }
            catch (Exception ex)
            {
                config.log(ex.Message);
                return false;
            }
        }
        public static bool save(string number, string message)
        {
            if (message != "OK")
            {
                db = new SMSDataClassesDataContext();
                SMS_QueuedBox que = new SMS_QueuedBox();
                que.Message = config.encrypt(message);
                que.Number = number;
                que.Network = (short)config.prefixes(number);
                que.Status = false;

                try
                {
                    db.SMS_QueuedBoxes.InsertOnSubmit(que);
                    db.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    config.log(ex.Message);
                }
            }
            return false;
        }
        public static bool save(string number, string message)
        {
            db = new SMSDataClassesDataContext();
            SMS_Inbox inbox = new SMS_Inbox();
            SMS_ReceivedMsg receivedMsg = new SMS_ReceivedMsg();

            inbox.Sender = number;
            inbox.Message = message;
            inbox.DateReceived = config.receivedDateTime();
            inbox.MonthOf = DateTime.Now.Month;
            inbox.YearOf = DateTime.Now.Year;
            inbox.Status = false;

            receivedMsg.Sender = number;
            receivedMsg.Message = message;
            receivedMsg.DateReceived = config.receivedDateTime();
            receivedMsg.MonthOf = DateTime.Now.Month;
            receivedMsg.YearOf = DateTime.Now.Year;
            receivedMsg.Status = false;

            validatorMessage.save(number, "Sorry, your message cannot be processed at this time, " +
                    "but your message is on queue. and it will process later. thank you!");

            try
            {
                db.SMS_Inboxes.InsertOnSubmit(inbox);
                db.SMS_ReceivedMsgs.InsertOnSubmit(receivedMsg);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                config.log(ex.Message);
                return false;
            }
        }
 static string city_selected()
 {
     db = new SMSDataClassesDataContext();
     var g = (from i in db.SMS_WeatherForecasts.Where(i => i.selected == true) select i).Take(1).FirstOrDefault();
     if (g != null)
     {
         return g.weather_code;
     }
     return "NULL";
 }
        public static bool saveToSentItemThenDelete(long id, bool status)
        {
            db = new SMSDataClassesDataContext();
            SMS_SentItem sentItems = new SMS_SentItem();
            var x = (from i in db.SMS_QueuedBoxes.Where(i => i.ID == id) select i).Take(1).FirstOrDefault();
            if (x != null)
            {
                sentItems.Number = x.Number;
                sentItems.Message = x.Message;
                sentItems.DateTime = config.receivedDateTime();
                sentItems.Status = status;
                sentItems.MonthOf = DateTime.Now.Month;
                sentItems.YearOf = DateTime.Now.Year;

                try
                {
                    db.SMS_SentItems.InsertOnSubmit(sentItems);
                    db.SMS_QueuedBoxes.DeleteOnSubmit(x);
                    db.SubmitChanges();
                    config.log("Successful updated in inbox");
                    return true;
                }
                catch (Exception ex)
                {
                    config.log(ex.Message);
                }
            }
            return false;
        }
 static string replyMessage(string tag, string status, string optional = "")
 {
     string reply = "Sorry, our system has encountered an error, please try again later. thank you!";
     db = new SMSDataClassesDataContext();
     var rep = (from i in db.SMS_Replies.Where(i => i.Tagged_ID == tag && i.Status == status) select i).Take(1).FirstOrDefault();
     if (rep != null)
     {
         if (status == "OK")
         {
             reply = rep.Message.Replace("[Firstname]", optional);
         }
         else
         {
             reply = rep.Message;
         }
     }
     return reply;
 }
 static bool checkRef(string input)
 {
     db = new SMSDataClassesDataContext();
     var refNo = (from i in db.SMS_Members.Where(i => i.ReferenceNo == input) select i).Take(1).FirstOrDefault();
     if (refNo != null)
     {
         return true;
     }
     return false;
 }
 static bool checkNumber(string number)
 {
     db = new SMSDataClassesDataContext();
     var member = (from i in db.SMS_Members.Where(i => i.CP_Number == number) select i).Take(1).FirstOrDefault();
     var money = (from i in db.SMS_eMoneys.Where(i => i.Account == number) select i).Take(1).FirstOrDefault();
     if (member != null || money != null)
     {
         return true;
     }
     return false;
 }
 static bool checkCode(string input)
 {
     db = new SMSDataClassesDataContext();
     var code = (from i in db.SMS_ActivationCodes.Where(i => i.PINCode == input && i.Status == true) select i).Take(1).FirstOrDefault();
     if (code != null)
     {
         return code.Status;
     }
     return false;
 }
 static bool IsRef_Exist(string input)
 {
     db = new SMSDataClassesDataContext();
     var refNo = (from i in db.SMS_TransferFundReports.Where(i => i.ReferenceNo == input) select i).Take(1).FirstOrDefault();
     if (refNo != null)
     {
         return true;
     }
     return false;
 }