324 lines
17 KiB
C#
324 lines
17 KiB
C#
using Hermes.Model;
|
|
using System;
|
|
using System.Data.Entity;
|
|
using System.Collections.Generic;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Data;
|
|
using System.IO;
|
|
using System.Text;
|
|
using Microsoft.Win32;
|
|
|
|
namespace Hermes {
|
|
public partial class MainWindow : Window {
|
|
private ModelContext dbContext = null;
|
|
private CollectionViewSource citoyenCollectionViewSource = null;
|
|
private bool isEnabledFilter = false;
|
|
|
|
public MainWindow() {
|
|
dbContext = ModelContext.Getinstance();
|
|
//dbContext.Database.Log = log => System.Console.WriteLine(log);
|
|
dbContext.Database.CreateIfNotExists();
|
|
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs e) {
|
|
dbContext.CitoyenSet.Load();
|
|
dbContext.Preferences.Load();
|
|
citoyenCollectionViewSource = (CollectionViewSource) this.FindResource("citoyenCollectionViewSource");
|
|
citoyenCollectionViewSource.Source = dbContext.CitoyenSet.Local;
|
|
|
|
if(dbContext.Preferences.Local.Count == 0) {
|
|
MessageBox.Show("Il s'agit du premier lancement de Hermes. Veuillez renseigner les informations sur votre commune.", "Premier lancement", MessageBoxButton.OK, MessageBoxImage.None);
|
|
PreferencesWindow preferencesWindow = new PreferencesWindow(this);
|
|
preferencesWindow.ShowDialog();
|
|
}
|
|
}
|
|
|
|
private void Options_Click(object sender, RoutedEventArgs e) {
|
|
PreferencesWindow preferencesWindow = new PreferencesWindow(this);
|
|
preferencesWindow.ShowDialog();
|
|
}
|
|
|
|
private void Ajouter_Click(object sender, RoutedEventArgs e) {
|
|
CitoyenWindow citoyenWindow = new CitoyenWindow(this);
|
|
citoyenWindow.ShowDialog();
|
|
}
|
|
|
|
private void GroupEdit_Batiment(object sender, RoutedEventArgs e) {
|
|
if(dgCitoyens.SelectedItems.Count == 0) {
|
|
MessageBox.Show("Aucun citoyen sélectionné.", "Édition multiple", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
return;
|
|
}
|
|
|
|
InputWindow inputBox = new InputWindow("Associer un bâtiment :", "Entrez le nom d'un bâtiment :", this);
|
|
if(inputBox.ShowDialog() == true) {
|
|
foreach(Citoyen citoyen in dgCitoyens.SelectedItems) {
|
|
citoyen.AdresseBatiment = inputBox.InputText;
|
|
citoyen.DateModification = DateTime.Now;
|
|
}
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
private void GroupEdit_Quartier(object sender, RoutedEventArgs e) {
|
|
if(dgCitoyens.SelectedItems.Count == 0) {
|
|
MessageBox.Show("Aucun citoyen sélectionné.", "Édition multiple", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
return;
|
|
}
|
|
|
|
InputWindow inputBox = new InputWindow("Associer un quartier :", "Entrez le nom d'un quartier :", this);
|
|
if(inputBox.ShowDialog() == true) {
|
|
foreach(Citoyen citoyen in dgCitoyens.SelectedItems) {
|
|
citoyen.Quartier = inputBox.InputText;
|
|
citoyen.DateModification = DateTime.Now;
|
|
}
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
private void GroupEdit_Adresse(object sender, RoutedEventArgs e) {
|
|
if(dgCitoyens.SelectedItems.Count == 0) {
|
|
MessageBox.Show("Aucun citoyen sélectionné.", "Édition multiple", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
return;
|
|
}
|
|
|
|
InputWindow inputBox = new InputWindow("Associer une adresse :", "Entrez un numéro et un nom de rue :", this);
|
|
if(inputBox.ShowDialog() == true) {
|
|
foreach(Citoyen citoyen in dgCitoyens.SelectedItems) {
|
|
citoyen.Adresse = inputBox.InputText;
|
|
citoyen.DateModification = DateTime.Now;
|
|
}
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
private void Supprimer_Click(object sender, RoutedEventArgs e) {
|
|
if(dgCitoyens.SelectedItems.Count > 0) {
|
|
MessageBoxResult result = MessageBox.Show("Voulez-vous supprimer ces citoyens ?", "Suppression", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
if(result == MessageBoxResult.Yes) {
|
|
List<Citoyen> clist = new List<Citoyen>();
|
|
foreach(Citoyen c in dgCitoyens.SelectedItems) {
|
|
clist.Add(c);
|
|
}
|
|
foreach(Citoyen c in clist) {
|
|
dbContext.CitoyenSet.Remove(c);
|
|
}
|
|
dbContext.SaveChanges();
|
|
}
|
|
} else {
|
|
MessageBox.Show("Aucun citoyen sélectionné.", "Suppression", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
}
|
|
|
|
private void Sms_Click(object sender, RoutedEventArgs e) {
|
|
if(dgCitoyens.SelectedItems.Count > 0) {
|
|
MessageBoxResult result = MessageBox.Show("Voulez-vous envoyer un SMS à ces citoyens ?", "Envoi de SMS", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
if(result == MessageBoxResult.Yes) {
|
|
bool noTel = false;
|
|
List<Citoyen> rcps = new List<Citoyen>();
|
|
foreach(Citoyen citoyen in dgCitoyens.SelectedItems) {
|
|
if(!String.IsNullOrWhiteSpace(citoyen.TelPort)) {
|
|
rcps.Add(citoyen);
|
|
} else {
|
|
noTel = true;
|
|
}
|
|
}
|
|
if(rcps.Count > 0) {
|
|
if(noTel) {
|
|
MessageBox.Show("Certains des citoyens sélectionnés ne disposent pas d'un numéro de mobile.", "Envoi de SMS", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
SmsWindow smsWindow = new SmsWindow(this, rcps);
|
|
smsWindow.ShowDialog();
|
|
} else {
|
|
MessageBox.Show("Aucun des citoyens sélectionnés ne disposent d'un numéro de mobile.", "Envoi de SMS", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
}
|
|
} else {
|
|
MessageBox.Show("Aucun citoyen sélectionné.", "Envoi de SMS", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
|
|
}
|
|
|
|
private void Courriel_Click(object sender, RoutedEventArgs e) {
|
|
if(dgCitoyens.SelectedItems.Count > 0) {
|
|
MessageBoxResult result = MessageBox.Show("Voulez-vous envoyer un courriel à ces citoyens ?", "Envoi de courriel", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
if(result == MessageBoxResult.Yes) {
|
|
bool noMail = false;
|
|
List<string> mails = new List<string>();
|
|
foreach(Citoyen citoyen in dgCitoyens.SelectedItems) {
|
|
if(!String.IsNullOrWhiteSpace(citoyen.Mail)) {
|
|
mails.Add(citoyen.Mail);
|
|
} else {
|
|
noMail = true;
|
|
}
|
|
}
|
|
if(mails.Count > 0) {
|
|
if(noMail) {
|
|
MessageBox.Show("Certains des citoyens sélectionnés ne disposent pas d'une adresse E-Mail.", "Envoi de courriel", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
|
|
Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
|
|
foreach(string mail in mails) {
|
|
Microsoft.Office.Interop.Outlook.Recipient rcp = mailItem.Recipients.Add(mail);
|
|
rcp.Type = (int) Microsoft.Office.Interop.Outlook.OlMailRecipientType.olBCC;
|
|
}
|
|
mailItem.Recipients.ResolveAll();
|
|
MessageBox.Show("Assurez-vous que Microsoft OutLook soit démarré avant de continuer.", "Envoi de courriel", MessageBoxButton.OK, MessageBoxImage.None);
|
|
mailItem.Display(true);
|
|
} else {
|
|
MessageBox.Show("Aucun des citoyens sélectionnés ne disposent d'une adresse E-Mail.", "Envoi de courriel", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
}
|
|
} else {
|
|
MessageBox.Show("Aucun citoyen sélectionné.", "Envoi de courriel", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
}
|
|
|
|
private void Publipostage_Click(object sender, RoutedEventArgs e) {
|
|
if(dgCitoyens.SelectedItems.Count > 0) {
|
|
MessageBoxResult result = MessageBox.Show("Voulez-vous réaliser une tâche de publipostage pour ces citoyens ?", "Publipostage", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
if(result == MessageBoxResult.Yes) {
|
|
string csvPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hermes_tmp_datasource.csv");
|
|
string csvHeader = "\"Civilité\";\"Nom\";\"Nom de naissance\";\"Prénom\";\"Profession\";\"Type de résidence\";\"E-Mail\";\"Téléphone\";\"Mobile\";\"Quartier\";\"Bâtiment\";\"Numéro appartement\";\"Adresse principale\";\"Code postal principal\";\"Ville principale\";\"Adresse secondaire\";\"Code postal secondaire\";\"Ville secondaire\"";
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.AppendLine(csvHeader);
|
|
foreach(Citoyen citoyen in dgCitoyens.SelectedItems) {
|
|
string civilite = citoyen.Civilite == null ? "" : citoyen.Civilite;
|
|
string nom = citoyen.Nom == null ? "" : citoyen.Nom;
|
|
string nomNaissance = citoyen.NomNaissance == null ? "" : citoyen.NomNaissance;
|
|
string prenom = citoyen.Prenom == null ? "" : citoyen.Prenom;
|
|
string profession = citoyen.Profession == null ? "" : citoyen.Profession;
|
|
string typeResidence = citoyen.TypeResidenceLabel == null ? "" : citoyen.TypeResidenceLabel;
|
|
string mail = citoyen.Mail == null ? "" : citoyen.Mail;
|
|
string tel = citoyen.Tel == null ? "" : citoyen.Tel;
|
|
string telPort = citoyen.TelPort == null ? "" : citoyen.TelPort;
|
|
string quartier = citoyen.Quartier == null ? "" : citoyen.Quartier;
|
|
string batiment = citoyen.AdresseBatiment == null ? "" : citoyen.AdresseBatiment;
|
|
string numeroBatiment = citoyen.AdresseNumeroBatiment == null ? "" : citoyen.AdresseNumeroBatiment;
|
|
string adressePrincipale = citoyen.AdressePrincipale == null ? "" : citoyen.AdressePrincipale;
|
|
string cpPrincipal = citoyen.AdressePrincipaleCP == null ? "" : citoyen.AdressePrincipaleCP;
|
|
string villePrincipale = citoyen.AdressePrincipaleVille == null ? "" : citoyen.AdressePrincipaleVille;
|
|
string adresseSecondaire = citoyen.AdresseSecondaire == null ? "" : citoyen.AdresseSecondaire;
|
|
string cpSecondaire = citoyen.AdresseSecondaireCP == null ? "" : citoyen.AdresseSecondaireCP;
|
|
string villeSecondaire = citoyen.AdresseSecondaireVille == null ? "" : citoyen.AdresseSecondaireVille;
|
|
|
|
sb.AppendLine($"\"{civilite}\";\"{nom}\";\"{nomNaissance}\";\"{prenom}\";\"{profession}\";\"{typeResidence}\";\"{mail}\";\"{tel}\";\"{telPort}\";\"{quartier}\";\"{batiment}\";\"{numeroBatiment}\";\"{adressePrincipale}\";\"{cpPrincipal}\";\"{villePrincipale}\";\"{adresseSecondaire}\";\"{cpSecondaire}\";\"{villeSecondaire}\"");
|
|
|
|
File.WriteAllText(csvPath, sb.ToString(), Encoding.GetEncoding("ISO-8859-1"));
|
|
}
|
|
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
ofd.Filter = "Document World|*.doc;*.docx;*.dotx|Tous les ficiers|*.*";
|
|
|
|
if(ofd.ShowDialog() == true) {
|
|
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
|
|
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ofd.FileName);
|
|
doc.MailMerge.OpenDataSource(csvPath, false, false, true);
|
|
app.Visible = true;
|
|
}
|
|
}
|
|
} else {
|
|
MessageBox.Show("Aucun citoyen sélectionné.", "Publipostage", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
}
|
|
|
|
private void Importer_Click(object sender, RoutedEventArgs e) {
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
ofd.Filter = "Classeur Excel|*.xls;*.xlsx;*.xlsm|Tous les ficiers|*.*";
|
|
if(ofd.ShowDialog() == true) {
|
|
ImportWindow importWindow = new ImportWindow(this, ofd.FileName);
|
|
importWindow.ShowDialog();
|
|
}
|
|
}
|
|
|
|
private void DgCitoyen_DoubleClick(object sender, MouseButtonEventArgs e) {
|
|
if(dgCitoyens.SelectedItem != null) {
|
|
CitoyenWindow citoyenWindow = new CitoyenWindow(this, (Citoyen) dgCitoyens.SelectedItem);
|
|
citoyenWindow.ShowDialog();
|
|
}
|
|
}
|
|
|
|
private void Rechercher_Click(object sender, RoutedEventArgs e) {
|
|
if(isEnabledFilter) {
|
|
citoyenCollectionViewSource.View.Refresh();
|
|
} else {
|
|
citoyenCollectionViewSource.Filter += new FilterEventHandler(CitoyenFilter);
|
|
isEnabledFilter = true;
|
|
}
|
|
}
|
|
|
|
private void Quitter_Click(object sender, RoutedEventArgs e) {
|
|
Close();
|
|
}
|
|
|
|
private void Reinitialiser_Click(object sender, RoutedEventArgs e) {
|
|
if(isEnabledFilter) {
|
|
citoyenCollectionViewSource.Filter -= new FilterEventHandler(CitoyenFilter);
|
|
isEnabledFilter = false;
|
|
}
|
|
ageOperationFilterComboBox.SelectedIndex = 0;
|
|
ageFilterTextBox.Text = null;
|
|
professionFilterTextBox.Text = null;
|
|
quartierFilterTextBox.Text = null;
|
|
adresseFilterTextBox.Text = null;
|
|
residenceFilterComboBox.SelectedIndex = 0;
|
|
}
|
|
|
|
protected override void OnClosed(EventArgs e) {
|
|
dbContext.Dispose();
|
|
base.OnClosed(e);
|
|
Application.Current.Shutdown();
|
|
}
|
|
|
|
private void CitoyenFilter(object sender, FilterEventArgs e) {
|
|
Citoyen citoyen = (Citoyen) e.Item;
|
|
e.Accepted = true;
|
|
if(citoyen != null) {
|
|
if(!String.IsNullOrEmpty(professionFilterTextBox.Text)) {
|
|
if(citoyen.Profession == null || !citoyen.Profession.ToLower().Contains(professionFilterTextBox.Text.ToLower())) {
|
|
e.Accepted = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
int ageFilter = Int32.Parse(ageFilterTextBox.Text);
|
|
if(!citoyen.AgeInt.HasValue
|
|
|| (ageOperationFilterComboBox.SelectedIndex == 0 && ageFilter != citoyen.AgeInt)
|
|
|| (ageOperationFilterComboBox.SelectedIndex == 1 && ageFilter >= citoyen.AgeInt)
|
|
|| (ageOperationFilterComboBox.SelectedIndex == 2 && ageFilter <= citoyen.AgeInt)) {
|
|
e.Accepted = false;
|
|
return;
|
|
}
|
|
} catch(Exception) { }
|
|
|
|
if(!String.IsNullOrEmpty(quartierFilterTextBox.Text)) {
|
|
if(citoyen.Quartier == null || !citoyen.Quartier.ToLower().Contains(quartierFilterTextBox.Text.ToLower())) {
|
|
e.Accepted = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if(!String.IsNullOrEmpty(adresseFilterTextBox.Text)) {
|
|
string addrNumBat = citoyen.AdresseNumeroBatiment == null ? "" : citoyen.AdresseNumeroBatiment;
|
|
string addrBat = citoyen.AdresseBatiment == null ? "" : citoyen.AdresseBatiment;
|
|
string addr = citoyen.Adresse == null ? "" : citoyen.Adresse;
|
|
string addrFull = $"{addr} {addrNumBat} {addrBat}".ToLower();
|
|
if(!addrFull.Contains(adresseFilterTextBox.Text.ToLower())) {
|
|
e.Accepted = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if((residenceFilterComboBox.SelectedIndex == 1 && citoyen.TypeResidence != false)
|
|
|| (residenceFilterComboBox.SelectedIndex == 2 && citoyen.TypeResidence != true)) {
|
|
e.Accepted = false;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|