public async Task <IActionResult> AddCard(int id) { var addedClient = _context.Client.Where(x => x.IdClient == id).FirstOrDefault(); var newCard = new LoyalityCard() { IdClient = addedClient.IdClient, ActualPoints = 0 }; _context.Add(newCard); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(ClientList))); }
public async Task <IActionResult> AddNewClient([Bind("IdClient,Name,FirstName,Surname,NIP,Street,HouseNumber,ApartmentNumber,Postcode,Locality")] Client client, bool addCard) { if (ModelState.IsValid) { if ((client.NIP == null && client.Name != null) || (client.NIP != null && client.Name == null) || (client.FirstName == null && client.Surname != null) || (client.FirstName != null && client.Surname == null) || (client.FirstName == null && client.Surname == null && client.NIP == null && client.Name == null)) { ViewBag.ErrorClient = "Invalid customer data"; return(View(client)); } if (client.NIP != null && client.Name != null && (client.FirstName != null || client.Surname != null)) { ViewBag.ErrorClient = "Invalid customer data"; return(View(client)); } if (client.FirstName != null && client.Surname != null && (client.NIP != null || client.Name != null)) { ViewBag.ErrorClient = "Invalid customer data"; return(View(client)); } _context.Add(client); await _context.SaveChangesAsync(); //get added client from database if (addCard) { var addedClient = _context.Client.Where(x => x.IdClient == client.IdClient).FirstOrDefault(); var newCard = new LoyalityCard() { IdClient = addedClient.IdClient, ActualPoints = 0 }; _context.Add(newCard); await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(ClientList))); } return(View(client)); }
public JsonResult Save(LoyalityCardViewModel model) { Response response; var currentUser = GetAuthenticatedUser(); try { using (var db = new KiaGalleryContext()) { if (model.id > 0) { } else { List <string> codeList = new List <string>(); for (int i = 1; i <= model.count; i++) { string code; while (true) { code = RandomString(8); LoyalityCard loyalityCard = db.LoyalityCard.FirstOrDefault(x => x.Code == code); if (loyalityCard == null && codeList.Count(x => x == code) == 0) { break; } } codeList.Add(code); var entity = new LoyalityCard() { Code = code, CardType = model.cardType, CardStatus = LoyalityCardStatus.Register, CreateUserId = currentUser.Id, ModifyUserId = currentUser.Id, CreateDate = DateTime.Now, ModifyDate = DateTime.Now, Ip = Request.UserHostAddress }; var log = new LoyalityCardLog() { LoyalityCard = entity, CardStatus = LoyalityCardStatus.Register, CreateUserId = currentUser.Id, CreateDate = DateTime.Now, Ip = Request.UserHostAddress }; db.LoyalityCardLog.Add(log); db.LoyalityCard.Add(entity); var barcode = new Barcode(code, TypeBarcode.Code128C); var bar128 = barcode.Encode(TypeBarcode.Code128C, code, 886, 142); string serverPath = Server.MapPath("~/Upload/LoyalityCard/"); if (!Directory.Exists(serverPath)) { Directory.CreateDirectory(serverPath); } bar128.Save(serverPath + code + ".jpg"); } } db.SaveChanges(); } response = new Response() { status = 200, message = "کارت با موفقیت ایجاد شد." }; } catch (Exception ex) { response = Core.GetExceptionResponse(ex); } return(Json(response, JsonRequestBehavior.AllowGet)); }