public int ImportCustomer(Stream csvFile) { StreamReader textReader = new StreamReader(csvFile); CsvReader CSV = new CsvReader(textReader); CSV.Configuration.IsHeaderCaseSensitive = false; CSV.Configuration.TrimFields = true; CSV.Configuration.WillThrowOnMissingField = false; int count = 0; List<Customer> list = new List<Customer>(); Dictionary<decimal, int> GSTDictionary = gstSvr.GetAll().ToDictionary(t => t.Rate, t => t.Id); RegexUtilities util = new RegexUtilities(); while (CSV.Read()) { string name = CSV.GetField<string>("name"); string phone = CSV.GetField<string>("phone"); string fax = CSV.GetField<string>("fax"); string email = CSV.GetField<string>("email"); string contactName = CSV.GetField<string>("contactName"); string address = CSV.GetField<string>("address"); string address_line_1 = CSV.GetField<string>("address_line_1"); string address_line_2 = CSV.GetField<string>("address_line_2"); string address_line_3 = CSV.GetField<string>("address_line_3"); string address_line_4 = CSV.GetField<string>("address_line_4"); string GSTRate = CSV.GetField<string>("GSTRate"); decimal _GSTRate = decimal.Parse(GSTRate); string _address; if (!string.IsNullOrEmpty(address)) { _address = address.Replace("|", System.Environment.NewLine); } else { _address = string.Join(System.Environment.NewLine, new string[] { address_line_1, address_line_2, address_line_3, address_line_4 }); } int GSTId; //Get GST by Rate if (GSTDictionary.ContainsKey(_GSTRate)) { GSTId = GSTDictionary[_GSTRate]; } else { GSTId = GSTDictionary.First().Value; } if (!util.IsValidEmail(email)) { email = null; } if (custSvr.IsCustomerNameExist(name)) { //update record? } else { Customer customer = new Customer { Name = name, Phone = phone, Fax = fax, Address = _address, idmas_GST = GSTId, Email = email, ContactName = contactName }; //check duplicate name in the list if (!list.Exists(c => c.Name.ToUpper() == customer.Name.ToUpper())) { list.Add(customer); } } } try { custSvr.Save(list); } catch (Exception e) { //throw new Exception("Save Customer Error", e); } return count; }
public ActionResult Create() { ViewBag.idmas_GST = new SelectList(gstSvc.GetAll(), "Id", "Code"); Customer _customer = new Customer(); return View(_customer); }
public ActionResult Create(Customer customer, FormCollection collection) { if (ModelState.IsValid) { this.UpdateModel(customer); svc.Save(customer); return RedirectToAction("Details", new { id = customer.Id }); } ViewBag.idmas_GST = new SelectList(gstSvc.GetAll(), "Id", "Code"); return View(customer); }
public ActionResult Edit(int id,Customer customer, FormCollection collection) { Customer _entity = svc.GetById(id); if (ModelState.IsValid) { _entity.Name = collection["Name"]; _entity.Address = collection["Address"]; _entity.Phone = collection["Phone"]; _entity.Fax = collection["Fax"]; _entity.Email = string.IsNullOrEmpty(collection["Email"])?null:collection["Email"]; _entity.ContactName = collection["ContactName"]; _entity.idmas_GST =int.Parse(collection["idmas_GST"]); try { svc.Update(_entity); } catch (Exception e) { } return RedirectToAction("Details", new { id = _entity.Id }); } ViewBag.idmas_GST = new SelectList(gstSvc.GetAll(), "Id", "Code", _entity.idmas_GST); return View(_entity); }