Beispiel #1
0
 public ActionResult Edit(GoodsSubCategoryViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return(View(model));
     }
     try
     {
         using (var db = new SmDbContext())
         {
             var entity = db.GoodsSubCategories.FirstOrDefault(x => x.Id == model.Id);
             if (entity == null)
             {
                 entity = new GoodsSubCategory();
                 db.GoodsSubCategories.Add(entity);
             }
             entity.Name            = model.Name;
             entity.Description     = model.Description;
             entity.GoodsCategoryId = model.GoodsCategoryId;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("Name", ex);
         return(View(model));
     }
 }
Beispiel #2
0
 public ActionResult Edit(SupplierViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return(View(model));
     }
     try
     {
         using (var db = new SmDbContext())
         {
             var entity = db.Suppliers.FirstOrDefault(x => x.Id == model.Id);
             if (entity == null)
             {
                 entity = new Supplier();
                 db.Suppliers.Add(entity);
             }
             entity.Name        = model.Name;
             entity.Identifier  = model.Identifier;
             entity.Address     = model.Address;
             entity.Description = model.Description;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("Name", ex);
         return(View(model));
     }
 }
Beispiel #3
0
        public ActionResult Delete(int id)
        {
            try
            {
                using (var db = new SmDbContext())
                {
                    var tr = db.Database.BeginTransaction();

                    var entity = db.Sales.First(x => x.Id == id);
                    foreach (var item in db.SaleItems.Where(x => x.SaleId == id))
                    {
                        db.SaleItems.Remove(item);
                    }
                    db.Sales.Remove(entity);
                    db.SaveChanges();
                    tr.Commit();
                }

                return(SuccessJson());
            }
            catch (Exception ex)
            {
                return(FailJson(ex));
            }
        }
Beispiel #4
0
 private void SetRoles(string currentRole = "")
 {
     using (var db = new SmDbContext())
     {
         var roles = db.AspNetRoles.ToList();
         if (roles.Count() == 0)
         {
             roles.Add(new AspNetRoles {
                 Id = Guid.NewGuid().ToString(), Name = "Администратор"
             });
             roles.Add(new AspNetRoles {
                 Id = Guid.NewGuid().ToString(), Name = "Директор"
             });
             roles.Add(new AspNetRoles {
                 Id = Guid.NewGuid().ToString(), Name = "Менеджер"
             });
             roles.Add(new AspNetRoles {
                 Id = Guid.NewGuid().ToString(), Name = "Продавец"
             });
             roles.Add(new AspNetRoles {
                 Id = Guid.NewGuid().ToString(), Name = "Кассир"
             });
             db.AspNetRoles.AddRange(roles);
             db.SaveChanges();
         }
         ViewBag.Roles = roles.OrderBy(x => x.Name).Select(x => new SelectListItem {
             Value = x.Id, Text = x.Name, Selected = x.Id == currentRole
         }).ToList();
     }
 }
Beispiel #5
0
        public ActionResult Edit(UserViewModel model)
        {
            if (string.IsNullOrEmpty(model.Id) && string.IsNullOrEmpty(model.Password))
            {
                ModelState.AddModelError("Password", "Требуется поле Пароль.");
            }

            if (!ModelState.IsValid)
            {
                SetRoles(model.Role);
                return(View(model));
            }
            try
            {
                using (var db = new SmDbContext())
                {
                    var user = db.AspNetUsers.FirstOrDefault(x => x.Id == model.Id);
                    if (user == null)
                    {
                        user = new AspNetUsers {
                            Id = Guid.NewGuid().ToString()
                        };
                        db.AspNetUsers.Add(user);
                        user.UserName      = model.UserName;
                        user.SecurityStamp = Guid.NewGuid().ToString();
                    }
                    user.Email       = model.Email;
                    user.Lastname    = model.Lastname;
                    user.Firstname   = model.Firstname;
                    user.PhoneNumber = model.PhoneNumber;
                    if (!string.IsNullOrEmpty(model.Password))
                    {
                        var ph = new Microsoft.AspNet.Identity.PasswordHasher();
                        user.PasswordHash = ph.HashPassword(model.Password);
                    }
                    if (!string.IsNullOrEmpty(model.Role))
                    {
                        var role = db.AspNetRoles.FirstOrDefault(x => x.Id == model.Role);
                        if (role != null)
                        {
                            user.AspNetRoles.Clear();
                            user.AspNetRoles.Add(role);
                        }
                    }
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("UserName", ex);

                SetRoles(model.Role);
                return(View(model));
            }
        }
Beispiel #6
0
        private Guid GreateRunTrace()
        {
            ScheduleTraceEntity entity = new ScheduleTraceEntity();

            entity.TraceId    = Guid.NewGuid();
            entity.ScheduleId = _sid;
            entity.Node       = node;
            entity.StartTime  = DateTime.Now;
            entity.Result     = (int)ScheduleRunResult.Null;
            _db.ScheduleTraces.Add(entity);
            if (_db.SaveChanges() > 0)
            {
                return(entity.TraceId);
            }
            return(Guid.Empty);
        }
Beispiel #7
0
        public ActionResult Delete(int Id)
        {
            try
            {
                using (var db = new SmDbContext())
                {
                    var entity = db.Suppliers.First(x => x.Id == Id);
                    db.Suppliers.Remove(entity);
                    db.SaveChanges();

                    return(SuccessJson());
                }
            }
            catch (Exception ex)
            {
                return(FailJson(ex));
            }
        }
Beispiel #8
0
        public ActionResult Delete(string Id)
        {
            try
            {
                using (var db = new SmDbContext())
                {
                    var user = db.AspNetUsers.First(x => x.Id == Id);
                    db.AspNetUsers.Remove(user);
                    db.SaveChanges();

                    return(SuccessJson());
                }
            }
            catch (Exception ex)
            {
                return(FailJson(ex));
            }
        }
Beispiel #9
0
        private void SaveModel(GoodsViewModel model)
        {
            using (var db = new SmDbContext())
            {
                var entity = db.Goods.FirstOrDefault(x => x.Id == model.Id);
                if (entity == null)
                {
                    entity = new Goods();
                    db.Goods.Add(entity);
                }
                entity.Brand              = model.Brand;
                entity.Description        = model.Description;
                entity.GoodsCategoryId    = model.GoodsCategoryId;
                entity.GoodsSubCategoryId = model.GoodsSubCategoryId;
                entity.Name        = model.Name;
                entity.Price       = model.Price;
                entity.ProductCode = model.ProductCode;

                db.SaveChanges();
            }
        }
Beispiel #10
0
        public ActionResult Edit(SaleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var pos = 0;
                foreach (var item in model.SaleItems)
                {
                    item.pos = pos++;
                }
                return(View(model));
            }

            try
            {
                using (var db = new SmDbContext())
                {
                    var tr     = db.Database.BeginTransaction();
                    var entity = db.Sales.FirstOrDefault(x => x.Id == model.Id);
                    if (entity == null)
                    {
                        entity = new Sale();
                        db.Sales.Add(entity);
                    }
                    if (entity.Processed)
                    {
                        throw new Exception("Документ уже проведен!");
                    }
                    entity.OrderId         = model.OrderId;
                    entity.Paytype         = model.Paytype;
                    entity.OperatorId      = db.AspNetUsers.First(x => x.UserName == User.Identity.Name).Id;
                    entity.RealizationDate = model.RealisationDate.Value;
                    entity.Processed       = model.Processed ?? false;
                    db.SaveChanges();

                    var existedIds = model.SaleItems.Select(x => x.Id);
                    foreach (var item in db.SaleItems.Where(x => x.SaleId == model.Id && !existedIds.Contains(x.Id)))
                    {
                        db.SaleItems.Remove(item);
                    }

                    foreach (var modelItem in model.SaleItems)
                    {
                        var entityItem = db.SaleItems.FirstOrDefault(x => x.Id == modelItem.Id);
                        if (entityItem == null)
                        {
                            entityItem = new SaleItem {
                                SaleId = entity.Id
                            };
                            db.SaleItems.Add(entityItem);
                        }
                        entityItem.Discount    = modelItem.Discount;
                        entityItem.OrderItemId = modelItem.OrderItemId;
                        entityItem.GoodsId     = modelItem.GoodsId;
                        entityItem.Amount      = modelItem.Amount;
                        entityItem.Price       = modelItem.Price;
                        entityItem.Discount    = modelItem.Discount;
                    }

                    db.SaveChanges();

                    // Если документ проведен, то обновим наличие на складе
                    if (model.Processed == true)
                    {
                        foreach (var item in model.SaleItems)
                        {
                            var goods = db.Goods.First(x => x.Id == item.GoodsId);
                            goods.Quantity -= item.Amount;
                            if (goods.Quantity < 0)
                            {
                                throw new Exception("Недостаточно наличия \"" + goods.Name + "\" на складе!");
                            }
                        }
                        db.SaveChanges();
                    }

                    tr.Commit();

                    //var t = 0;
                    //var a = 5 / t;
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Id", ex);
                return(View());
            }
        }
Beispiel #11
0
        public ActionResult Edit(IncomeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                using (var db = new SmDbContext())
                {
                    ViewBag.Suppliers = db.Suppliers.Select(x => new SelectListItem
                    {
                        Value = x.Id.ToString(),
                        Text  = x.Name
                    }).ToList();
                }
                var pos = 0;
                foreach (var item in model.IncomeItems)
                {
                    item.pos = pos++;
                }
                return(View(model));
            }

            try
            {
                using (var db = new SmDbContext())
                {
                    var tr     = db.Database.BeginTransaction();
                    var entity = db.Incomes.FirstOrDefault(x => x.Id == model.Id);
                    if (entity == null)
                    {
                        entity = new Income();
                        db.Incomes.Add(entity);
                    }
                    if (entity.Processed)
                    {
                        throw new Exception("Документ уже проведен!");
                    }
                    entity.DocumentNumber = model.DocumentNumber;
                    entity.IncomeDate     = model.IncomeDate;
                    entity.OperatorId     = db.AspNetUsers.First(x => x.UserName == User.Identity.Name).Id;
                    entity.SupplierId     = model.SupplierId;
                    entity.Processed      = model.Processed ?? false;
                    db.SaveChanges();

                    var existedIds = model.IncomeItems.Select(x => x.Id);
                    foreach (var item in db.IncomeItems.Where(x => x.IncomeId == model.Id && !existedIds.Contains(x.Id)))
                    {
                        db.IncomeItems.Remove(item);
                    }

                    foreach (var modelItem in model.IncomeItems)
                    {
                        var entityItem = db.IncomeItems.FirstOrDefault(x => x.Id == modelItem.Id);
                        if (entityItem == null)
                        {
                            entityItem = new IncomeItem {
                                IncomeId = entity.Id
                            };
                            db.IncomeItems.Add(entityItem);
                        }
                        entityItem.GoodsId = modelItem.GoodsId;
                        entityItem.Amount  = modelItem.Amount;
                        entityItem.Price   = modelItem.Price;
                    }

                    db.SaveChanges();

                    // Если документ проведен, то обновим наличие на складе
                    if (model.Processed == true)
                    {
                        foreach (var item in model.IncomeItems)
                        {
                            var goods = db.Goods.First(x => x.Id == item.GoodsId);
                            goods.Quantity += item.Amount;
                        }
                        db.SaveChanges();
                    }

                    tr.Commit();

                    //var t = 0;
                    //var a = 5 / t;
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
Beispiel #12
0
        public ActionResult Edit(OrderViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var pos = 0;
                foreach (var item in model.OrderItems)
                {
                    item.pos = pos++;
                }
                return(View(model));
            }

            try
            {
                using (var db = new SmDbContext())
                {
                    var tr     = db.Database.BeginTransaction();
                    var entity = db.Orders.FirstOrDefault(x => x.Id == model.Id);
                    if (entity == null)
                    {
                        entity = new Order();
                        db.Orders.Add(entity);
                    }
                    if (entity.Sales.Any())
                    {
                        throw new Exception("По данному заказу уже оформлена продажа!");
                    }
                    entity.ClientAddress     = model.ClientAddress;
                    entity.ClientDescription = model.ClientDescription;
                    entity.ClientIdentifier  = model.ClientIdentifier;
                    entity.ClientName        = model.ClientName;
                    entity.OrderDate         = model.OrderDate;
                    entity.OperatorId        = db.AspNetUsers.First(x => x.UserName == User.Identity.Name).Id;
                    db.SaveChanges();

                    var existedIds = model.OrderItems.Select(x => x.Id);
                    foreach (var item in db.OrderItems.Where(x => x.OrderId == model.Id && !existedIds.Contains(x.Id)))
                    {
                        db.OrderItems.Remove(item);
                    }

                    foreach (var modelItem in model.OrderItems)
                    {
                        var entityItem = db.OrderItems.FirstOrDefault(x => x.Id == modelItem.Id);
                        if (entityItem == null)
                        {
                            entityItem = new OrderItem {
                                OrderId = entity.Id
                            };
                            db.OrderItems.Add(entityItem);
                        }
                        entityItem.GoodsId = modelItem.GoodsId;
                        entityItem.Amount  = modelItem.Amount;
                    }

                    db.SaveChanges();

                    tr.Commit();
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Id", ex);
                return(View());
            }
        }