public ActionResult Upload(HttpPostedFileBase uploadFile, CustomerImport imp, FormCollection fc) { string importstatus; importstatus = "The import was the successful.."; SemplestModel.Semplest dbcontext = new SemplestModel.Semplest(); { StringBuilder strValidations = new StringBuilder(string.Empty); try { if (uploadFile.ContentLength > 0) { string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(uploadFile.FileName)); uploadFile.SaveAs(filePath); FileHelperEngine engine = new FileHelperEngine(typeof(CustomerImportData)); engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; CustomerImportData[] customersimported = (CustomerImportData[])engine.ReadFile(filePath); if (engine.ErrorManager.HasErrors) foreach (ErrorInfo err in engine.ErrorManager.Errors) { string s; s = err.LineNumber + " / " + err.RecordString + " / " + err.ExceptionInfo.ToString(); //Console.WriteLine(err.LineNumber); //Console.WriteLine(err.RecordString); //Console.WriteLine(err.ExceptionInfo.ToString()); } if (customersimported.Count() < 1) throw new Exception("No customers could be imported from csv file"); /////////// /////////// foreach (CustomerImportData cust in customersimported) { CustomerAccount m = new CustomerAccount(); #region //cust.ContactCompanyName; //cust.ContactEmail; //cust.ContactFirstName; //cust.ContactLastName; //cust.ContactMiddleInitial; //cust.ContactPhoneNumber; //cust.ContactAddressLine1; //cust.ContactAddressLine2; //cust.ContactCity; //cust.ContactState; //cust.ContactStatus; //cust.ContactZip; //cust.FixedMonthlybudget; //cust.InternalCustomerID; /////// //cust.LocalTargetAddress1; //cust.LocalTargetAddress2; //cust.LocalTargetAddress3; //cust.LocalTargetProximity1; //cust.LocalTargetProximity1; //cust.LocalTargetProximity2; //cust.LocalTargetProximity3; //cust.ParentCompanyName; //ca.Address1; //ca.Address2; //ca.City; //ca.Customer; //ca.Email; //ca.FirstName; //ca.MiddleInitial; //ca.Phone; //ca.State; //ca.UserID; //ca.UserPassword; //ca.Zip; //ca.isActive; #endregion /* //cust.ContactStatus; cust.FixedMonthlybudget; //cust.InternalCustomerID; ///// cust.LocalTargetAddress1; cust.LocalTargetAddress2; cust.LocalTargetAddress3; cust.LocalTargetProximity1; cust.LocalTargetProximity2; cust.LocalTargetProximity3; cust.ParentCompanyName; ///// */ m.internalID = cust.InternalCustomerID; m.Address1 = cust.ContactAddressLine1; m.Address2 = cust.ContactAddressLine2; m.City = cust.ContactCity; m.Customer = cust.ContactCompanyName; m.Email = cust.ContactEmail; m.FirstName = cust.ContactFirstName; m.MiddleInitial = cust.ContactMiddleInitial; m.LastName = cust.ContactLastName; m.Phone = cust.ContactPhoneNumber; m.State = cust.ContactState; m.UserID = cust.ContactEmail; m.UserPassword = Semplest.SharedResources.Helpers.RandomPassword.Generate(8, 10); m.Zip = cust.ContactZip; m.isActive = true; var emailtemplate = (from et in dbcontext.EmailTemplates where et.EmailTemplatePK.Equals(13) select et).FirstOrDefault(); try { var existing = (from ex in dbcontext.Customers where ex.InternalCustomerId.Equals(cust.InternalCustomerID) && ex.Name.Equals(m.Customer) select ex).ToList(); if (existing.Count() > 0) throw new Exception(); ProductGroupCycleType pgct = dbcontext.ProductGroupCycleTypes.First(p => p.ProductGroupCycleType1 == "Product Group Cycle 30"); //Customer c = dbcontext.Customers.Add(new Customer { Name = m.Customer, BillTypeFK = imp.SelectedBillTypeID, ProductGroupCycleType = pgct, InternalCustomerId = cust.InternalCustomerID }); var c = new Customer { Name = m.Customer, BillTypeFK = imp.SelectedBillTypeID, ProductGroupCycleType = pgct, InternalCustomerId = cust.InternalCustomerID }; dbcontext.Customers.Add(c); var u = new User { Customer = c, Email = m.Email, FirstName = m.FirstName, LastName = m.LastName, MiddleInitial = m.MiddleInitial, IsActive = m.isActive }; dbcontext.Users.Add(u); var cr = new Credential { User = u, UsersFK = u.UserPK, Username = m.UserID, Password = m.UserPassword }; dbcontext.Credentials.Add(cr); PhoneType pt = dbcontext.PhoneTypes.First(p => p.PhoneType1 == "Business"); // --- phone types --- !!!! Phone ph = new Phone { Phone1 = m.Phone, PhoneType = pt }; dbcontext.Phones.Add(ph); var cpa = new CustomerPhoneAssociation { Customer = c, Phone = ph }; dbcontext.CustomerPhoneAssociations.Add(cpa); StateCode sc = dbcontext.StateCodes.First(p => p.StateAbbr == m.State); AddressType at = dbcontext.AddressTypes.First(p => p.AddressType1 == "H"); // --- address types --- !!! var a = new Address { Address1 = m.Address1, Address2 = m.Address2, City = m.City, ZipCode = m.Zip, StateCode = sc }; dbcontext.Addresses.Add(a); var caa = new CustomerAddressAssociation { Address = a, Customer = c, AddressType = at }; dbcontext.CustomerAddressAssociations.Add(caa); var cn = new CustomerNote { Customer = c, Note = m.CustomerNote }; dbcontext.CustomerNotes.Add(cn); var r = dbcontext.Roles.First(p => p.RolePK == imp.SelectedRoleID); var ura = new UserRolesAssociation { Role = r, User = u }; dbcontext.UserRolesAssociations.Add(ura); //default to the parent's rep and salesperson var parentrepandsales = (from prs in dbcontext.EmployeeCustomerAssociations where prs.CustomerFK.Equals(imp.ParentID) select prs).ToList(); foreach (EmployeeCustomerAssociation eca in parentrepandsales) { var addrepandsales = new EmployeeCustomerAssociation { Customer = c, EmployeeFK = eca.EmployeeFK }; dbcontext.EmployeeCustomerAssociations.Add(addrepandsales); } //add child to parent in customerhierarchy var ch = new CustomerHierarchy { CustomerFK = c.CustomerPK, CustomerParentFK = imp.ParentID }; dbcontext.CustomerHierarchies.Add(ch); dbcontext.SaveChanges(); if (fc["sendcustomeremail"] != null) { /////////////////////////////////////////////////////////////// //FOR SENDING OUT EMAILS to child customers /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// var parentdetails = from usr in dbcontext.Users join cus in dbcontext.Customers on usr.CustomerFK equals cus.CustomerPK where usr.CustomerFK == imp.ParentID select new { usr.CustomerFK, usr.Email, cus.Name }; //send mail //revisit string from = "*****@*****.**"; string to = u.Email; string body = emailtemplate.EmailBody; string subject = emailtemplate.EmailSubject; body = body.Replace("[ChildCustomerFirstLast]", u.FirstName.ToString() + " " + u.LastName.ToString()); body = body.Replace("[ParentCustomerName]", parentdetails.FirstOrDefault().Name.ToString()); body = body.Replace("[FAQs]", "http://faq"); body = body.Replace("[ChildCustomerUserID]", cr.Username.ToString()); body = body.Replace("[ChildCustomerPassword]", cr.Password.ToString()); body = body.Replace("[INSERT LINK]", "http://encrypto"); //SendEmail bool sent = false; ServiceClientWrapper scw = new ServiceClientWrapper(); sent = scw.SendEmail(subject, from, to, body); } } catch (Exception ex) { //Console.WriteLine(ex.TargetSite); SemplestModel.Semplest _dbContext = new SemplestModel.Semplest(); SemplestModel.Error er = new SemplestModel.Error(); er.ErrorMessage = ex.Message+" \r\n "+ex.InnerException +" \r\n "+ex.StackTrace +" \r\n "+ex.Source+" \r\n "+ex.TargetSite; //filterContext.RequestContext.HttpContext.Session //if (HttpContext.Current.Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID] == null) // er.UsersFK = 1; //else // er.UsersFK = ((Credential)HttpContext.Current.Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID]) == null ? 1 : ((Credential)HttpContext.Current.Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID]).UsersFK; er.CreatedDate = DateTime.Now; _dbContext.Errors.Add(er); _dbContext.SaveChanges(); var scw = new ServiceClientWrapper(); scw.SendEmail("WebSite Error Message", "*****@*****.**", _dbContext.Configurations.First().OnErrorEmail, er.ErrorMessage); } }//endforeach } else throw new Exception(); } catch (Exception ex) { importstatus = "An error has occured. Please check the file for errors and try again.."; //Console.WriteLine(ex.TargetSite); SemplestModel.Semplest _dbContext = new SemplestModel.Semplest(); SemplestModel.Error er = new SemplestModel.Error(); er.ErrorMessage = ex.Message + " \r\n " + ex.InnerException + " \r\n " + ex.StackTrace + " \r\n " + ex.Source + " \r\n " + ex.TargetSite; //filterContext.RequestContext.HttpContext.Session //if (HttpContext.Current.Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID] == null) // er.UsersFK = 1; //else // er.UsersFK = ((Credential)HttpContext.Current.Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID]) == null ? 1 : ((Credential)HttpContext.Current.Session[Semplest.SharedResources.SEMplestConstants.SESSION_USERID]).UsersFK; er.CreatedDate = DateTime.Now; _dbContext.Errors.Add(er); _dbContext.SaveChanges(); var scw = new ServiceClientWrapper(); scw.SendEmail("WebSite Error Message", "*****@*****.**", _dbContext.Configurations.First().OnErrorEmail, er.ErrorMessage); } /* * public Boolean SendEmail(String subject, String from, String recipient, String msgTxt) { var jsonHash = new Dictionary<string, string>(); jsonHash.Add("subject", subject); jsonHash.Add("from", from); jsonHash.Add("recipient", recipient); jsonHash.Add("msgTxt", msgTxt); string jsonstr = JsonConvert.SerializeObject(jsonHash); string returnData = runMethod(_baseURLTest, MAILSERVICEOFFERED, "SendEmail", jsonstr, timeoutMS); var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(returnData); string boolResult = dict.Values.First(); return Convert.ToBoolean(boolResult); } */ //scope.Complete(); } //return View(m); ImportResultModel res = new ImportResultModel(); res.Importresult = importstatus; return RedirectToAction("CustomerImportResult", "AccountService", res ); }
public ActionResult CustomerImport(int id) { SemplestModel.Semplest dbcontext = new SemplestModel.Semplest(); CustomerImport x = new CustomerImport(); ///////////////////////////////////////////////////////////////////////////////// //for billtype dropdown ///////////////////////////////////////////////////////////////////////////////// var allbilltypes = (from a in dbcontext.BillTypes select a).ToList(); x.SelectedBillTypeID = 1; x.BillTypes = allbilltypes.Select(r => new SelectListItem { Value = r.BillTypePK.ToString(), Text = r.BillType1.ToString() }); //for roles dropdown ///////////////////////////////////////////////////////////////////////////////// var roles = (from r in dbcontext.Roles select r).ToList().OrderBy(r => r.RoleName); x.Roles = roles.Select(r => new SelectListItem { Value = r.RolePK.ToString(), Text = r.RoleName.ToString() }); x.ParentID = id; return View(x); }