public static void EditCompany(CompanyMVCModel company) { using (IDbConnection cnn = new SqlConnection(GlobalConfig.CnnString())) { if (company != null) { List <CompanyMVCModel> companies = LoadCompanies(); if (companies.Where(x => x.Id == company.Id).FirstOrDefault() != null) { var obj = new { PhoneNumber = company.PhoneNumber, EmailAddress = company.EmailAddress, Street = company.Street, City = company.City, PostalCode = company.PostalCode, NIP = company.NIP, CompanyName = company.CompanyName, REGON = company.REGON, Id = company.Id }; cnn.Execute("UPDATE Companies SET " + "NIP = @NIP, PostalCode = @PostalCode, " + "Street = @Street, City = @City, " + "PhoneNumber = @PhoneNumber, EmailAddress = @EmailAddress, " + "CompanyName = @CompanyName, REGON = @REGON " + "WHERE Id = @Id", obj); } } } }
public static CompanyModel ConvertToCompanyModel(this CompanyMVCModel model) { if (model == null) { return(new CompanyModel()); } return(new CompanyModel(model.Id, model.NIP, model.PostalCode, model.City, model.Street, model.EmailAddress, model.CompanyName, model.PhoneNumber, model.REGON)); }
// GET: Company/Edit/5 public ActionResult Edit(int id) { CompanyMVCModel model = SQLiteDataAccess.LoadCompanies().Where(x => x.Id == id).FirstOrDefault(); if (model != null) { return(View(model)); } return(RedirectToAction("Index")); }
private CompanyMVCModel GetCompanyInformation(CompanyMVCModel creationCompany, int selectedCompany) { CompanyMVCModel result = new CompanyMVCModel(); if (creationCompany.CompanyName != null) { SQLiteDataAccess.SaveCompany(creationCompany); result = creationCompany; } else { result = SQLiteDataAccess.LoadCompanies().Where(x => x.Id == selectedCompany).FirstOrDefault(); } return(result); }
public ActionResult Edit(int id, CompanyMVCModel company) { try { if (ModelState.IsValid) { SQLiteDataAccess.EditCompany(company); } return(RedirectToAction("Index")); } catch { return(RedirectToAction("Edit", id)); } }
public ActionResult Create(CompanyMVCModel company) { try { if (ModelState.IsValid) { SQLiteDataAccess.SaveCompany(company); } return(RedirectToAction("Index")); } catch { return(RedirectToAction("Create")); } }
public ActionResult Delete(int id, CompanyMVCModel company) { try { if (company != null) { SQLiteDataAccess.RemoveCompany(company.Id); } return(RedirectToAction("Index")); } catch { return(View(company)); } }
private CompanyMVCModel ConvertCompanyToMVCModel(CompanyModel company) { CompanyMVCModel output = new CompanyMVCModel { PostalCode = $"{ company.Address.PostalCode.Number }", City = $"{ company.Address.City }", Street = $"{ company.Address.Street }", EmailAddress = $"{ company.EmailAddress.Address }", PhoneNumber = $"{ company.PhoneNumber.Number }", NIP = $"{ company.NIP.Number}", REGON = $"{ company.REGON.Number }", CompanyName = $"{ company.CompanyName}", Id = company.Id }; return(output); }
public static void SaveCompany(CompanyMVCModel company) { using (IDbConnection cnn = new SqlConnection(GlobalConfig.CnnString())) { if (company != null) { var o = cnn.Query <object>($"SELECT * FROM Companies WHERE " + $"(NIP='{company.NIP}' AND NIP!='') OR " + $"(REGON='{company.REGON}' AND REGON!='') OR " + $"CompanyName='{company.CompanyName}' AND " + $"((EmailAddress='{company.EmailAddress}' AND EmailAddress!='') OR " + $"(Street='{company.Street}' AND City='{company.City}') OR " + $"(PhoneNumber='{company.PhoneNumber}' AND PhoneNumber!=''))", new DynamicParameters()); if (!o.Any()) { var obj = new { PhoneNumber = company.PhoneNumber, EmailAddress = company.EmailAddress, Street = company.Street, City = company.City, PostalCode = company.PostalCode, NIP = company.NIP, CompanyName = company.CompanyName, REGON = company.REGON, }; cnn.Execute("insert into Companies (NIP, PostalCode, Street, City, PhoneNumber, EmailAddress, CompanyName, REGON)" + " values (@NIP, @PostalCode, @Street, @City, @PhoneNumber, @EmailAddress, @CompanyName, @REGON)", obj); //If storing company into db was succesful, return new record's Id company.Id = cnn.Query <int>("select Id from Companies").OrderBy(x => x).Last(); } } } }
public ActionResult Create(CommissionMVCCreateModel commissionModel) { try { string zipFolderPath = $"{ Server.MapPath("~") }GeneratedZips"; string docsFolderPath = $"{ Server.MapPath("~") }GeneratedDocuments"; string documentsStorageFolder = Server.MapPath("~") + "GeneratedDocuments\\"; string zipPath = $"{ Server.MapPath("~") }GeneratedZips\\result.zip"; string uploadsFolderPath = $"{ Server.MapPath("~") }Uploads"; string uploadsStorageFolder = $"{uploadsFolderPath}\\"; if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } if (!Directory.Exists(docsFolderPath)) { Directory.CreateDirectory(docsFolderPath); } if (!Directory.Exists(zipFolderPath)) { Directory.CreateDirectory(zipFolderPath); } // Get Company information CompanyMVCModel documentCompany = GetCompanyInformation(commissionModel.CreationCompany, commissionModel.SelectedCompany); //Get Clients information List <ClientMVCModel> documentClients = GetClientsInformation(commissionModel.CreationClient, commissionModel.SelectedClients); //Get Products information List <ItemMVCModel> documentProducts = GetProductsInformation(commissionModel.SelectedProducts, commissionModel.ProductPrices, commissionModel.ProductQuantities); //Get Creator information CreatorMVCModel documentCreator = GetCreatorInformation(commissionModel.CommissionCreator, commissionModel.SelectedCreator); if (documentCompany == null || documentClients.Count == 0 || documentProducts.Count == 0 || documentCreator == null) { return(RedirectToAction("Create", "Commission")); } string fileName; // Saving uploaded file if (commissionModel.PostedFile != null) { fileName = Path.GetFileName(commissionModel.PostedFile.FileName); commissionModel.PostedFile.SaveAs(Server.MapPath("~") + "Uploads\\" + fileName); } if (documentClients.Count > 1) { foreach (var item in documentClients) { PersonalData personalData = new PersonalData(documentCompany.ConvertToCompanyModel(), item.ConvertToClientModel(), documentCreator.ConvertToCommissionCreatorModel()); string path = $"{documentsStorageFolder}{ documentCompany.CompanyName }_{ item.FullName.Replace("\"", "") }_{ DateTime.Today.ToShortDateString() }.docx"; path.Trim(); if (commissionModel.PostedFile != null) { DocumentHelper.GenerateDocumentFromTemplate($"{Server.MapPath("~")}\\Uploads\\{Path.GetFileName(commissionModel.PostedFile.FileName)}", path, personalData, documentProducts.ConvertToItemModel(), true); } else { DocumentHelper.GenerateNewDocument(path, personalData, documentProducts.ConvertToItemModel()); } } ZipFile.CreateFromDirectory(docsFolderPath, zipPath); // Append headers Response.AppendHeader("content-disposition", $"attachment; filename={ documentCompany.CompanyName }_{ DateTime.Today.ToShortDateString() }.zip"); // Open/Save dialog Response.ContentType = "application/octet-stream"; // Push it! Response.TransmitFile(zipPath); Response.End(); } else { PersonalData personalData = new PersonalData(documentCompany.ConvertToCompanyModel(), documentClients.First().ConvertToClientModel(), documentCreator.ConvertToCommissionCreatorModel()); string path = $"{documentsStorageFolder}{ documentCompany.CompanyName }_{ documentClients.First().FullName.Replace("\"", "") }_{ DateTime.Today.ToShortDateString() }.docx"; path.Trim(); if (commissionModel.PostedFile != null) { DocumentHelper.GenerateDocumentFromTemplate(uploadsStorageFolder + Path.GetFileName(commissionModel.PostedFile.FileName), path, personalData, documentProducts.ConvertToItemModel(), true); } else { DocumentHelper.GenerateNewDocument(path, personalData, documentProducts.ConvertToItemModel()); } // Append headers Response.AppendHeader("content-disposition", $"attachment; filename={ documentCompany.CompanyName }_{ documentClients.First().FullName.Replace("\"", "") }_{ DateTime.Today.ToShortDateString() }.docx"); // Open/Save dialog Response.ContentType = "application/octet-stream"; // Push it! Response.TransmitFile(path); Response.End(); } DeleteFiles(); return(RedirectToAction("Index", "Home")); } catch (Exception e) { DeleteFiles(); string msg = e.Message; return(RedirectToAction("Create", "Commission")); } }
// GET: Company/Create public ActionResult Create() { CompanyMVCModel input = new CompanyMVCModel(); return(View(input)); }