Exemplo n.º 1
0
 public ActionResult SaveResult(string SubID, string Description)
 {
     if (asset.View)
     {
         if (string.IsNullOrEmpty(SubID) || string.IsNullOrEmpty(Description))
         {
             return(Json(new { success = false, error = "Please input data" }));
         }
         else
         {
             using (var dbConn = Helpers.OrmliteConnection.openConn())
                 using (var dbTrans = dbConn.OpenTransaction(IsolationLevel.ReadCommitted))
                 {
                     try
                     {
                         var    data   = new DC_Telesales_ResultList();
                         var    lastId = DC_Telesales_ResultList.GetLastID();
                         string a      = "";
                         if (lastId == null)
                         {
                             a = "R000";
                         }
                         else
                         {
                             var num = lastId.Id.Substring(2, lastId.Id.ToString().Length - 2);
                             var id  = Int16.Parse(num.ToString()) + 1;
                             a = "R" + String.Format("{0:000}", id);
                         }
                         data.Id                 = a;
                         data.Active             = true;
                         data.SubId              = SubID;
                         data.Description        = Description;
                         data.RowCreatedTime     = DateTime.Now;
                         data.RowCreatedUser     = currentUser.UserName;
                         data.RowLastUpdatedTime = DateTime.Parse("1900-01-01");
                         data.RowLastUpdatedUser = "";
                         dbConn.Insert(data);
                         dbTrans.Commit();
                         return(Json(new { success = true }));
                     }
                     catch (Exception e)
                     {
                         dbTrans.Rollback();
                         return(Json(new { success = false, error = e.Message }));
                     }
                 }
         }
     }
     else
     {
         ModelState.AddModelError("", "You don't have permission to create record ");
         return(Json(new { success = false, error = "You don't have permission to create record" }));
     }
 }
Exemplo n.º 2
0
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable <DC_Telesales_ResultList> listResult)
        {
            if (asset.View)
            {
                using (var dbConn = Helpers.OrmliteConnection.openConn())
                    using (var dbTrans = dbConn.OpenTransaction(IsolationLevel.ReadCommitted))
                    {
                        try
                        {
                            if (listResult != null && ModelState.IsValid)
                            {
                                foreach (var item in listResult)
                                {
                                    if (String.IsNullOrEmpty(item.Description))
                                    {
                                        ModelState.AddModelError("", "Please input field [Description]");
                                        return(Json(listResult.ToDataSourceResult(request, ModelState)));
                                    }

                                    var checkDescription = dbConn.Select <DC_Telesales_ResultList>("SELECT * FROM DC_Telesales_ResultList WHERE Description = '" + item.Description + "'");
                                    if (checkDescription.Count > 0)
                                    {
                                        ModelState.AddModelError("", "[Description] exist in system");
                                        return(Json(listResult.ToDataSourceResult(request, ModelState)));
                                    }

                                    var    lastId = DC_Telesales_ResultList.GetLastID();
                                    string a      = "";
                                    if (lastId == null)
                                    {
                                        a = "R000";
                                    }
                                    else
                                    {
                                        var num = lastId.Id.Substring(2, lastId.Id.ToString().Length - 2);
                                        var id  = Int16.Parse(num.ToString()) + 1;
                                        a = "R" + String.Format("{0:000}", id);
                                    }

                                    if (!string.IsNullOrEmpty(item.SubId))
                                    {
                                        item.SubId = item.SubId;
                                    }
                                    else
                                    {
                                        item.SubId = "0";
                                    }

                                    if (item.Id == item.SubId)
                                    {
                                        ModelState.AddModelError("", "Can't update this record: Self -own");
                                        return(Json(listResult.ToDataSourceResult(request, ModelState)));
                                    }

                                    item.Active             = item.Active;
                                    item.Id                 = a;
                                    item.Description        = item.Description;
                                    item.RowCreatedUser     = currentUser.UserName;
                                    item.RowCreatedTime     = DateTime.Now;
                                    item.RowLastUpdatedTime = DateTime.Parse("1900-01-01");
                                    item.RowLastUpdatedUser = "";
                                    dbConn.Insert(item);

                                    dbTrans.Commit();
                                    return(Json(new { success = true }));
                                }
                                return(Json(listResult.ToDataSourceResult(request, ModelState)));
                            }
                            else
                            {
                                ModelState.AddModelError("error", "");
                                return(Json(new { success = false }));
                            }
                        }
                        catch (Exception e)
                        {
                            dbTrans.Rollback();
                            return(Json(new { success = false, error = e.Message }));
                        }
                    }
            }
            else
            {
                ModelState.AddModelError("", "You don't have permission to create record ");
                return(Json(listResult.ToDataSourceResult(request, ModelState)));
            }
        }