public OutputModel CheckUser(string token, [FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();

            try
            {
                var data = db.tblM_User.Where(p =>
                                              p.Phone == usr.Phone &&
                                              p.isVerified == 1).Select(p => p).FirstOrDefault();

                if (data != null)
                {
                    if (data.isBanned == 0)
                    {
                        output.status  = "failed";
                        output.message = "User has been banned by admin";
                        output.data    = null;
                        return(output);
                    }
                    if (usr.Password == data.Password)
                    {
                        if (!db.tblM_User_DeviceID.Any(u => u.DeviceID.Contains(token) && u.UserID == data.UserID))
                        {
                            var dids = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).Count();
                            if (dids != 0)
                            {
                                var didsz = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).SingleOrDefault();
                                didsz = didsz + ", " + token;
                                tblM_User_DeviceID update = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Single <tblM_User_DeviceID>();
                                update.DeviceID     = didsz;
                                update.DateModified = DateTime.Now;
                                db.SaveChanges();
                            }
                            else
                            {
                                tblM_User_DeviceID add = new tblM_User_DeviceID();
                                add.UserID       = data.UserID;
                                add.DeviceID     = token;
                                add.DateModified = DateTime.Now;
                                db.tblM_User_DeviceID.Add(add);
                                db.SaveChanges();
                            }
                        }
                        output.status  = "success";
                        output.message = String.Empty;
                        output.data    = data;
                    }
                    else
                    {
                        output.status  = "failed";
                        output.message = "Password has changed";
                        output.data    = null;
                        return(output);
                    }
                }
                else
                {
                    //cm.activity(Convert.ToInt32(usr.UserID), "Log In failed");
                    output.status  = "failed";
                    output.message = "User not found";
                    output.data    = null;
                    return(output);
                }
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(usr.UserID), "Log In failed");
                output.status  = "error";
                output.message = ex.Message;
                output.data    = null;
            }

            return(output);
        }
        public OutputModel RegisterWithPhone(string deviceid, [FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();
            string      uid    = "";

            try
            {
                string ph = checkPhone(usr.Phone);
                if (ph == "yes")
                {
                    output.status  = "failed";
                    output.message = "Phone number already exist";
                    output.data    = String.Empty;
                    return(output);
                }
                usr.isVerified = 0;
                usr.isActive   = 0;
                usr.SMSCount   = 1;
                usr.DateJoin   = DateTime.Now;
                db.tblM_User.Add(usr);
                db.SaveChanges();

                var data = db.tblM_User.Where(p => p.Phone == usr.Phone).Select(p => p).Single();

                uid = data.UserID.ToString();

                if (deviceid != null)
                {
                    tblM_User_DeviceID add = new tblM_User_DeviceID();
                    add.UserID       = data.UserID;
                    add.DeviceID     = deviceid;
                    add.DateModified = DateTime.Now;
                    db.tblM_User_DeviceID.Add(add);
                    db.SaveChanges();
                }

                Random r = new Random();
                var    x = r.Next(0, 1000000);
                string s = x.ToString("000000");

                DateTime        exp  = DateTime.Now.AddMinutes(10);
                tblT_User_Token temp = new tblT_User_Token();
                temp.UserID      = data.UserID;
                temp.Token       = s;
                temp.DateExpired = exp;
                db.tblT_User_Token.Add(temp);

                //sendSMS(usr.Phone, temp.Token);

                db.SaveChanges();

                output.status  = "success";
                output.message = "User Successfully Inserted";
                output.data    = temp;
                cm.activity(Convert.ToInt32(data.UserID), "Register success");
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(uid), "Register failed");

                output.status  = "error";
                output.message = ex.Message;
                output.data    = String.Empty;
            }

            return(output);
        }
        public OutputModel Login(string deviceid, [FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();

            try
            {
                var data = db.tblM_User.Where(p =>
                                              p.Phone == usr.Phone &&
                                              p.Password == usr.Password
                                              ).Select(p => p).FirstOrDefault();

                if (data == null)
                {
                    data = db.tblM_User.Where(p =>
                                              p.Phone == usr.Phone &&
                                              p.isVerified == 1 &&
                                              p.isActive == 1).Select(p => p).FirstOrDefault();
                    if (data == null)
                    {
                        output.status  = "error";
                        output.message = "Account not found.";
                        output.data    = null;
                        return(output);
                    }
                    else
                    {
                        data = db.tblM_User.Where(p =>
                                                  p.Phone == usr.Phone &&
                                                  p.Password == usr.Password &&
                                                  p.isVerified == 1 &&
                                                  p.isActive == 1).Select(p => p).FirstOrDefault();
                        if (data == null)
                        {
                            output.status  = "error";
                            output.message = "Wrong password.";
                            output.data    = null;
                            return(output);
                        }
                    }
                }

                if (data != null)
                {
                    if (data.isActive == 1 && data.isVerified == 1)
                    {
                        if (!db.tblM_User_DeviceID.Any(u => u.DeviceID.Contains(deviceid) && u.UserID == data.UserID))
                        {
                            var dids = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).Count();
                            if (dids != 0)
                            {
                                var didsz = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).SingleOrDefault();
                                didsz = didsz + ", " + deviceid;
                                tblM_User_DeviceID update = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Single <tblM_User_DeviceID>();
                                update.DeviceID     = didsz;
                                update.DateModified = DateTime.Now;
                                db.SaveChanges();
                            }
                            else
                            {
                                tblM_User_DeviceID add = new tblM_User_DeviceID();
                                add.UserID       = data.UserID;
                                add.DeviceID     = deviceid;
                                add.DateModified = DateTime.Now;
                                db.tblM_User_DeviceID.Add(add);
                                db.SaveChanges();
                            }
                        }

                        output.status  = "success";
                        output.message = String.Empty;
                        output.data    = data;
                        //cm.activity(Convert.ToInt32(data.UserID), "Log In");
                    }
                    else if (data.isVerified == 0 && data.isActive == 0)
                    {
                        var cd = db.tblT_User_Token.Where(p =>
                                                          p.UserID == data.UserID
                                                          ).Select(p => p).Single();

                        Random r = new Random();
                        var    x = r.Next(0, 1000000);
                        string s = x.ToString("000000");

                        cd.Token       = s;
                        cd.DateExpired = DateTime.Now.AddMinutes(10);

                        db.SaveChanges();

                        //sendSMS(data.Phone, s);

                        output.status  = "verify";
                        output.message = "Please verify your account first.";
                        output.data    = cd;
                    }
                    else if (data.isVerified == 1 && data.isActive == 0)
                    {
                        output.status  = "banned";
                        output.message = "User has been banned by admin";
                        output.data    = null;
                    }
                }
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(usr.UserID), "Log In failed");
                output.status  = "error";
                output.message = ex.Message;
                output.data    = null;
            }

            return(output);
        }
        public OutputModel CheckUser([FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();

            try
            {
                var data = db.tblM_User.Where(p => p.Phone == usr.Phone && p.isVerified == 1).FirstOrDefault();

                if (data != null)
                {
                    if (data.isBanned == 1)
                    {
                        output.status  = "failed";
                        output.message = "Akun Anda telah dinonaktifkan oleh Admin";
                        output.data    = null;
                        return(output);
                    }
                    if (usr.Password == data.Password)
                    {
                        if (!db.tblM_User_DeviceID.Any(u => u.DeviceID.Contains(usr.DeviceID) && u.UserID == data.UserID))
                        {
                            var dids = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).Count();
                            if (dids != 0)
                            {
                                var didsz = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).SingleOrDefault();
                                didsz = didsz + "," + usr.DeviceID;
                                tblM_User_DeviceID update = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Single <tblM_User_DeviceID>();
                                update.DeviceID     = didsz;
                                update.DateModified = DateTime.Now;
                                db.SaveChanges();
                            }
                            else
                            {
                                tblM_User_DeviceID add = new tblM_User_DeviceID();
                                add.UserID       = data.UserID;
                                add.DeviceID     = usr.DeviceID;
                                add.DateModified = DateTime.Now;
                                db.tblM_User_DeviceID.Add(add);
                                db.SaveChanges();
                            }
                        }
                        output.status  = "success";
                        output.message = String.Empty;
                        output.data    = data;
                    }
                    else
                    {
                        output.status  = "failed";
                        output.message = "Password Anda sudah dirubah sebelumnya. Silahkan login kembali menggunakan password baru Anda.";
                        output.data    = null;
                        return(output);
                    }
                }
                else
                {
                    //cm.activity(Convert.ToInt32(usr.UserID), "Log In failed");
                    output.status  = "failed";
                    output.message = "Akun Anda tidak ditemukan";
                    output.data    = null;
                    return(output);
                }
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(usr.UserID), "Log In failed");
                output.status  = "error";
                output.message = ex.Message;
                output.data    = null;
            }

            return(output);
        }
        public OutputModel RegisterWithPhone([FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();
            string      uid    = "";

            try
            {
                string ph = checkPhone(usr.Phone);
                if (ph == "yes")
                {
                    output.status  = "failed";
                    output.message = "Phone number already exist";
                    output.data    = String.Empty;
                    return(output);
                }
                usr.isVerified = 0;
                usr.isActive   = 0;
                usr.isBanned   = 0;
                usr.isMerchant = 0;
                usr.Balance    = 0;
                usr.SMSCount   = 1;
                usr.DateJoin   = DateTime.Now;
                db.tblM_User.Add(usr);
                db.SaveChanges();

                var data = db.tblM_User.Where(p => p.Phone == usr.Phone).Select(p => p).Single();

                uid = data.UserID.ToString();

                if (usr.DeviceID != null)
                {
                    tblM_User_DeviceID add = new tblM_User_DeviceID();
                    add.UserID       = data.UserID;
                    add.DeviceID     = usr.DeviceID;
                    add.DateModified = DateTime.Now;
                    db.tblM_User_DeviceID.Add(add);
                    db.SaveChanges();
                }

                db.SaveChanges();

                Xsight ver = new Xsight();
                ver = cm.SMSOTP(uid, cm.phoneNumberFormat(data.Phone), "");

                //string sub = data.Phone.Substring(0, 1);
                //string subs = data.Phone.Substring(1, data.Phone.Length - 1);
                //if (sub == "0")
                //{
                //    ver = SMSOTP(tokens, uid, "+62"+subs, "");
                //}
                //else
                //{
                //    ver = SMSOTP(tokens, uid, data.Phone, "");
                //}

                if (ver.status)
                {
                    Random r = new Random();
                    var    x = r.Next(0, 1000000);
                    string s = x.ToString("000000");

                    DateTime        exp  = DateTime.Now.AddMinutes(10);
                    tblT_User_Token temp = new tblT_User_Token();
                    temp.UserID      = data.UserID;
                    temp.Token       = s;
                    temp.DateExpired = exp;
                    db.tblT_User_Token.Add(temp);

                    //sendSMS(usr.Phone, temp.Token);

                    db.SaveChanges();

                    output.status  = "success";
                    output.message = "User Successfully Inserted";
                    output.data    = data;
                }
                else
                {
                    output.status  = "failed";
                    output.message = "User failed to add";
                    output.data    = data;
                }

                //Random r = new Random();
                //var x = r.Next(0, 1000000);
                //string s = x.ToString("000000");

                //DateTime exp = DateTime.Now.AddMinutes(10);
                //tblT_User_Token temp = new tblT_User_Token();
                //temp.UserID = data.UserID;
                //temp.Token = s;
                //temp.DateExpired = exp;
                //db.tblT_User_Token.Add(temp);

                //sendSMS(usr.Phone, temp.Token);

                //db.SaveChanges();

                //output.status = "success";
                //output.message = "User Successfully Inserted";
                //output.data = temp;
                //cm.activity(Convert.ToInt32(data.UserID), "Register success");
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(uid), "Register failed");

                output.status  = "error";
                output.message = ex.Message;
                output.data    = String.Empty;
            }

            return(output);
        }
        public OutputModel Login([FromBody] tblM_User usr)
        {
            OutputModel output = new OutputModel();

            try
            {
                var data = db.tblM_User.Where(p =>
                                              p.Phone == usr.Phone &&
                                              p.Password == usr.Password
                                              ).Select(p => p).FirstOrDefault();

                if (data == null)
                {
                    data = db.tblM_User.Where(p =>
                                              p.Phone == usr.Phone &&
                                              p.isVerified == 1 &&
                                              p.isActive == 1).Select(p => p).FirstOrDefault();
                    if (data == null)
                    {
                        output.status  = "error";
                        output.message = "Akun Anda tidak ditemukan.";
                        output.data    = null;
                        return(output);
                    }
                    else
                    {
                        data = db.tblM_User.Where(p =>
                                                  p.Phone == usr.Phone &&
                                                  p.Password == usr.Password &&
                                                  p.isVerified == 1 &&
                                                  p.isActive == 1).Select(p => p).FirstOrDefault();
                        if (data == null)
                        {
                            output.status  = "error";
                            output.message = "Password yang ANda masukkan salah.";
                            output.data    = null;
                            return(output);
                        }
                    }
                }

                if (data != null)
                {
                    if (data.isActive == 1 && data.isVerified == 1)
                    {
                        if (!db.tblM_User_DeviceID.Any(u => u.DeviceID.Contains(usr.DeviceID) && u.UserID == data.UserID))
                        {
                            var dids = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).Count();
                            if (dids != 0)
                            {
                                var didsz = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Select(p => p.DeviceID).SingleOrDefault();
                                didsz = didsz + "," + usr.DeviceID;
                                tblM_User_DeviceID update = db.tblM_User_DeviceID.Where(p => p.UserID == data.UserID).Single <tblM_User_DeviceID>();
                                update.DeviceID     = didsz;
                                update.DateModified = DateTime.Now;
                                db.SaveChanges();
                            }
                            else
                            {
                                tblM_User_DeviceID add = new tblM_User_DeviceID();
                                add.UserID       = data.UserID;
                                add.DeviceID     = usr.DeviceID;
                                add.DateModified = DateTime.Now;
                                db.tblM_User_DeviceID.Add(add);
                                db.SaveChanges();
                            }
                        }

                        if (data.isMerchant == 1)
                        {
                            var loc = db.tblT_Merchant_Delivery_Location.Where(p => p.UserID == data.UserID && p.isActive == 1).ToList();
                            for (int i = 0; i < loc.Count; i++)
                            {
                                long locId   = (long)loc[i].KabupatenID;
                                var  locName = db.tblM_Kabupaten.Where(p => p.KabupatenID == locId).FirstOrDefault();
                                if (i == 0)
                                {
                                    data.DeliveryLocation = locName.Nama;
                                }
                                else
                                {
                                    data.DeliveryLocation = data.DeliveryLocation + ", " + locName.Nama;
                                }
                            }
                        }

                        output.status  = "success";
                        output.message = String.Empty;
                        output.data    = data;
                        //cm.activity(Convert.ToInt32(data.UserID), "Log In");
                    }
                    else if (data.isVerified == 0 && data.isActive == 0)
                    {
                        var cd = db.tblT_User_Token.Where(p =>
                                                          p.UserID == data.UserID
                                                          ).Select(p => p).Single();

                        Random r = new Random();
                        var    x = r.Next(0, 1000000);
                        string s = x.ToString("000000");

                        cd.Token       = s;
                        cd.DateExpired = DateTime.Now.AddHours(12);

                        db.SaveChanges();

                        sendSMS(data.Phone, s);

                        output.status  = "verify";
                        output.message = "Please verify your account first.";
                        output.data    = cd;
                    }
                    else if ((data.isVerified == 1 && data.isActive == 0) || data.isBanned == 1)
                    {
                        output.status  = "banned";
                        output.message = "Akun Anda sudah dinonaktifkan oleh Admin. Silahkan buat akun baru.";
                        output.data    = null;
                    }
                }
            }
            catch (Exception ex)
            {
                //cm.activity(Convert.ToInt32(usr.UserID), "Log In failed");
                output.status  = "error";
                output.message = ex.Message;
                output.data    = null;
            }

            return(output);
        }