public ActionResult Edit(CompanyViewModel model) { if (model.CompanyId == 0) { var random = new Random(); var numver = random.Next(100000, 999999); var company = new Company { CompanyId = model.CompanyId, Name = model.Name, City = model.City, Address = model.Address, Bulstat = model.Eik, LeftNumber = $"DT{numver}", RigthNumber = $"02{numver}", Description = model.Description != null ? model.Description : "" }; dc.Companies.Add(company); dc.SaveChanges(); } else { var company = dc.Companies.Where(c => c.CompanyId == model.CompanyId).SingleOrDefault(); company.Address = model.Address; company.City = model.City; company.Name = model.Name; company.Bulstat = model.Eik; company.Description = model.Description != null ? model.Description : ""; dc.SaveChanges(); } return(RedirectToAction("list")); }
public ActionResult DeleteReceipt(int id) { var receipt = dc.Receipts.Where(r => r.ReceiptId == id).SingleOrDefault(); dc.Receipts.Remove(receipt); dc.SaveChanges(); return(null); }
public int Save(ReceiptViewModel model) { if (model.ReceiptId == 0) { var userID = User.Identity.GetUserId(); var _user = dc.Users.Where(u => u.Id == userID).SingleOrDefault(); var company = dc.Companies.Where(c => c.CompanyId == model.CompanyId).SingleOrDefault(); Receipt.DataContext.Receipt receipt = new Receipt.DataContext.Receipt() { Date = (DateTime)model.Date, Company = company, OperatorS = model.Operator, Operator = _user, UniqueNumber = Hellper.Hellper.GenerateBigNumber() }; dc.Receipts.Add(receipt); foreach (var a in model.Articles) { Article artcle = new Article() { Name = a.ArticleName, Price = a.Price }; receipt.Articles.Add(artcle); } var wl = dc.WorkLists.Where(w => w.User.Id == userID && w.IsActive).SingleOrDefault(); if (wl == null) { wl = new WorkList() { Date = DateTime.Now, Name = "", User = _user, IsActive = true }; } receipt.WorkList = wl; dc.SaveChanges(); model.ReceiptId = receipt.ReceiptId; } else { Receipt.DataContext.Receipt receipt = dc.Receipts.Where(r => r.ReceiptId == model.ReceiptId).SingleOrDefault(); var company = dc.Companies.Where(c => c.CompanyId == model.CompanyId).SingleOrDefault(); receipt.Company = company; receipt.Date = (DateTime)model.Date; receipt.OperatorS = model.Operator; receipt.Articles.ToList().ForEach(a => { receipt.Articles.Remove(a); }); foreach (var a in model.Articles) { Article artcle = new Article() { Name = a.ArticleName, Price = a.Price }; receipt.Articles.Add(artcle); } dc.SaveChanges(); } return(model.ReceiptId); }