using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; namespace MyCompagnyCalendar { public partial class CalendarIHM : Form { public static Collection employees; public static Collection contacts; public static Collection services; public static Collection rdvs; private static bool view_contact=false; private static bool view_employee=false; public CalendarIHM() { InitializeComponent(); } public void write(string ca) { LogBox.Text += ca; LogBox.Text += "\r\n"; } private void Error_Message(string msg) { MessageBox.Show(msg,"Erreur"); } private void Calendar_Load(object sender, EventArgs e) { employees=new Collection(); contacts = new Collection(); services= new Collection(); rdvs = new Collection(); try { StreamReader reader = new StreamReader("employees.txt"); string line; while ((line = reader.ReadLine()) != null) { string[] result; result = line.Split(' '); int ID = Convert.ToInt32(result[0]); int service = Convert.ToInt32(result[3]); Employee emp = new Employee(ID, result[2], result[1], service); employees.Add(emp); } } catch (Exception a) { // Let the user know what went wrong. Error_Message("Impossible de lire employees.txt"); write(a.Message); } try { StreamReader reader = new StreamReader("contacts.txt"); string line; while ((line = reader.ReadLine()) != null) { string[] result; result = line.Split(' '); int ID = Convert.ToInt32(result[0]); Contact cont = new Contact(ID, result[3], result[2], result[1]); contacts.Add(cont); } } catch (Exception a) { // Let the user know what went wrong. Error_Message("Impossible de lire contacts.txt"); write(a.Message); } try { StreamReader reader = new StreamReader("services.txt"); string line; while ((line = reader.ReadLine()) != null) { string[] result; result = line.Split(' '); int ID = Convert.ToInt32(result[0]); Service ser= new Service(ID, result[1]); services.Add(ser); } } catch (Exception a) { // Let the user know what went wrong. Error_Message("Impossible de lire services.txt"); write(a.Message); } load_rdvs(); //Crée la liste des employées dans la combobox foreach (Employee p in employees) { string re; re = p.name + ' ' + p.firstname; Employees_Box.Items.Add(re); } //Crée la liste des contacts dans la combobox foreach (Service p in services) { Services_Box.Items.Add(p.name); } Services_Box.Items.Add(" "); //Crée la liste des contacts dans la combobox foreach (Contact p in contacts) { string re; re = p.name + ' ' + p.firstname; Contacts_Box.Items.Add(re); } } public abstract class Person { /** Variables **/ private string _name, _firstname; private int _id; /** Getters/Setters **/ public string name { get { return _name; } set { _name = value; } } public string firstname { get { return _firstname; } set { _firstname = value; } } public int id { get { return _id; } set { _id = value; } } } public class Employee : Person { /** Variables **/ private int _service; //ID du service auquel il est affecté private int _holiday;// nombre de jour de vacance utilisé /** Constructeurs **/ public Employee(int ident, string nom, string prenom, int service, int holiday) { id = ident; name = nom; firstname = prenom; _service = service; _holiday = holiday; } public Employee(int ident, string nom, string prenom, int servicee) { id = ident; name = nom; firstname = prenom; _service = servicee; _holiday = 0; } /** Getters/Setters **/ public int service { get { return _service; } set { _service = value; } } public int holiday { get { return _holiday; } set { _holiday += value; } } } public class Contact : Person { private string _enterprise; //0=supplier, 1=client /** Constructeurs **/ public Contact(int ident, string nom, string prenom, string enterprisee) { id = ident; name = nom; firstname = prenom; enterprise = enterprisee; } /** Getters/Setters **/ public string enterprise { get { return _enterprise; } set { _enterprise = value; } } } public class Service { private int _id; private string _name; public Service(int iid, string nom) { _id = iid; _name = nom; } /** Getters/Setters **/ public string name { get { return _name; } set { _name = value; } } public int id { get { return _id; } set { _id = value; } } } public class Rdv { /** Variables **/ private DateTime _begining; //fin du rendez vous private DateTime _end;//Début du rendez vous private bool _vac; private int _employee; private int _contact; /** Constructeurs **/ public Rdv(DateTime debut, DateTime fin, int emp, int cont, bool vac) { _begining = debut; _end = fin; _employee=emp; _contact=cont; _vac = vac; } /** Getters/Setters **/ public DateTime begining { get { return _begining; } set { _begining = value; } } public DateTime end { get { return _end; } set { _end = value; } } public bool IsVacation { get { return _vac; } set { _vac = value; } } public int employee { get { return _employee; } set { _employee = value; } } public int contact { get { return _contact; } set { _contact = value; } } } private void label1_Click(object sender, EventArgs e) { } private void label6_Click(object sender, EventArgs e) { } private void Services_Box_SelectedIndexChanged(object sender, EventArgs e) { Employees_Box.Items.Clear(); // on vide la boite avant de la re-remplire if (Services_Box.SelectedItem.ToString() == " ")//si on veut afficher tous les contacts { foreach (Employee p in employees) { string re; re = p.name + ' ' + p.firstname; Employees_Box.Items.Add(re); } } else //sinon, choix par service { foreach (Employee p in employees) { if (p.service == Get_Service_ID(Services_Box.SelectedItem.ToString())) { string re; re = p.name + ' ' + p.firstname; Employees_Box.Items.Add(re); } } } Employees_Box.Text = Employees_Box.Items[0].ToString(); Employees_Box.Select(); } int Get_Service_ID(string service) { int i = 0; while (i<=services.Count && services[i].name!=service) { i++; } return services[i].id; } int Get_Employee_ID(string emp) { string[] re; re=emp.Split(' '); int i = 0; while (i<=employees.Count && employees[i].name!=re[0] && employees[i].firstname!=re[1]) { i++; } if (i >= employees.Count) { return -1; } else { return employees[i].id; } } Employee Get_Employee_by_ID(int emp) { int i = 0; while (i <= employees.Count && employees[i].id != emp) { i++; } if (i >= employees.Count) { return null; } else { return employees[i]; } } Contact Get_Contact_by_ID(int cont) { int i = 0; while (i <= contacts.Count && contacts[i].id != cont) { i++; } if (i >= contacts.Count) { return null; } else { return contacts[i]; } } Service Get_Service_by_ID(int ser) { int i = 0; while (i <= services.Count && services[i].id != ser) { i++; } if (i >= services.Count) { return null; } else { return services[i]; } } int Get_Contact_ID(string emp) { string[] re; re = emp.Split(' '); int i = 0; while (i <= contacts.Count && contacts[i].name != re[0] && contacts[i].firstname != re[1]) { i++; } if (i >= contacts.Count) { return -1; } else { return contacts[i].id; } } private void app_Click(object sender, EventArgs e) { int emp = -1, cont = -1; DateTime start, end; try { emp = Get_Employee_ID(Employees_Box.SelectedItem.ToString()); cont = Get_Contact_ID(Contacts_Box.SelectedItem.ToString()); } catch (Exception ) { // Let the user know what went wrong. Error_Message("Sélectionnez un employé et un contact "); } start = dateTimePicker_app_beg.Value; end = dateTimePicker_app_end.Value; if (emp != -1 && cont != -1) //si on connait l'employé, le contact et que la date de début et antérieur à la date de fin { if (start.CompareTo(end) < 0) { if (NoWE(start, end)) { if (!IsRdvBetween_forContact(cont, start, end))//pas de rdv si le contact n'est pas dispo { if (!IsRdvBetween(emp, start, end))//pas de rdv si l'employée n'est pas dispo { Rdv nouv = new Rdv(start, end, emp, cont, false); rdvs.Add(nouv); write(Get_Employee_by_ID(nouv.employee).firstname + " " + Get_Employee_by_ID(nouv.employee).name + " with " + Get_Contact_by_ID(nouv.contact).firstname + " " + Get_Contact_by_ID(nouv.contact).name + " : " + nouv.begining.ToShortDateString() + "-->" + nouv.end.ToShortDateString()); write("Nouveau rendez-vous ajouté"); if (view_employee) { MaJ_affichage(true, Get_Employee_ID(Employees_Box.SelectedItem.ToString())); } if (view_contact) { MaJ_affichage(false, Get_Contact_ID(Contacts_Box.SelectedItem.ToString())); } } else { Error_Message("Employé indisponible : L'employé n'est pas disponible, il est soit en vacance, soit en rendez-vous"); } } else { Error_Message("Contact indisponible : Le contact séléctionné n'est pas disponible, il est soit en vacance, soit en rendez-vous"); } } else { Error_Message("Un rendez-vous ne peut pas avoir lieu le W-E"); } } else { Error_Message(" La date de fin est anterieur à la date de début"); } } else { Error_Message("L'employé ou le contact séléctionné n'est pas valide"); } } private void vac_Click(object sender, EventArgs e) { int emp=-1, cont=-1; DateTime start, end; start = Vac_beg.Value; end = Vac_end.Value; bool allowed = false; if (radioButton_contact.Checked) // si déclaration pour un contact { try { cont = Get_Contact_ID(Contacts_Box.SelectedItem.ToString()); } catch { Error_Message(" Sélectionnez un contact valable"); } emp = -2; } else if (radioButton_employee.Checked)// si pour un employé { try { emp = Get_Employee_ID(Employees_Box.SelectedItem.ToString()); } catch { Error_Message("Sélectionnez un employé valide!"); } cont = -2; } int nb_jour=0; if (!(!radioButton_contact.Checked && !radioButton_employee.Checked)) { if (emp != -1 && cont != -1) //si on connait l'employé, le contact et que la date de début et antérieur à la date de fin { if (start.CompareTo(end) < 0) { if (radioButton_contact.Checked) { allowed = Delete_rdv_contact_between(cont, start, end); //on supprime les rdvs } else if (radioButton_employee.Checked) { if (!IsRdvBetween(emp, start, end)) { if (NbPresentBetween(Get_Employee_by_ID(emp).service, start, end) >= 2)//si pas de rdv entre et assez de monde { if (MoreThanOneWeek(start, end)) { nb_jour=nb_jour_entre(start, end); if (Get_Employee_by_ID(emp).holiday + nb_jour <= 25) //5*5jours ouvrés = 25 jours de vacances { allowed = true; } else { Error_Message("Quota de vacances dépassé pour cet employé"); } } else { Error_Message("Durée minimale des vacances : Une semaine"); } } else { Error_Message("Impossible : manque de personnel"); } } else { Error_Message("Il y a des rendez-vous ou des vacances de prévu dans la plage de vacance selectionné"); } } else { Error_Message("Ces vacancances concerne un employé ou un contact?"); } } else { Error_Message("La date de fin est anterieur à la date de début !"); } if (allowed)// si on peut (rdv effacé pour le contact et pas de contre-indication pour un employé { Rdv nouv = new Rdv(start, end, emp, cont, true); rdvs.Add(nouv); if (radioButton_employee.Checked) { Get_Employee_by_ID(nouv.employee).holiday = nb_jour;//MaJ Du quota de vacances write(Get_Employee_by_ID(nouv.employee).firstname + " " + Get_Employee_by_ID(nouv.employee).name + " : " + nouv.begining.ToShortDateString() + "-->" + nouv.end.ToShortDateString()); } else { write(Get_Contact_by_ID(nouv.contact).firstname + " " + Get_Contact_by_ID(nouv.contact).name + " : " + nouv.begining.ToShortDateString() + "-->" + nouv.end.ToShortDateString()); } write("Vacances ajouté"); if (radioButton_employee.Checked) { MaJ_affichage(true, Get_Employee_ID(Employees_Box.SelectedItem.ToString())); } if (radioButton_contact.Checked) { MaJ_affichage(false, Get_Contact_ID(Contacts_Box.SelectedItem.ToString())); } } else { write("Impossible !"); } } } else { Error_Message("Ces vacancances concerne un employé ou un contact?"); } } bool IsRdvBetween(int emp, DateTime start, DateTime end)// Renvoi vrai si on a un rendez vous pour cet employé dans la plage indiqué { bool ok = true; int i = 0; while (i=0) { ok = false; } if (start.CompareTo(rdvs[i].begining) <= 0 && end.CompareTo(rdvs[i].begining) >= 0) { ok = false; } if (start.CompareTo(rdvs[i].end) <= 0 && end.CompareTo(rdvs[i].end) >= 0)//si le début d'un rdv existant est dans l'intervale des vacs ou si la fin d'un rdv est dans l'interval de vac (=si un rdv interfère avec les vacs) ou si le rdv est dans la plage complète { ok = false; } } i++; } return !ok; } bool IsRdvBetween_forContact(int cont, DateTime start, DateTime end)// Revoi vrai si on a un rendez vous pour cet employé dans la plage indiqué { bool ok = true; int i = 0; while (i < rdvs.Count && ok) { if (rdvs[i].contact == cont) { if ((start.CompareTo(rdvs[i].end) <= 0 && rdvs[i].end.CompareTo(end) <= 0) || (start.CompareTo(rdvs[i].begining) <= 0 && start.CompareTo(rdvs[i].end) <= 0) || (start.CompareTo(rdvs[i].begining) >= 0 && end.CompareTo(rdvs[i].end) <= 0))//si le début d'un rdv existant est dans l'intervale des vacs ou si la fin d'un rdv est dans l'interval de vac (=si un rdv interfère avec les vacs) { ok = false; } } i++; } return !ok; } int NbPresentBetween(int service, DateTime start, DateTime end) { int nb_service=0, max=0, tmp=0; foreach (Employee p in employees) { if (p.service==service) { nb_service++; } } //on récupère tous les rdvs qui concerne ce service pour faciliter l'analyse Collection s; s = new Collection(); foreach( Rdv r in rdvs) { if (r.employee != -2) { int t = Get_Employee_by_ID(r.employee).service; if (r.IsVacation && service == t) { s.Add(r); } } } DateTime DT_tmp; DT_tmp = start; while (DT_tmp.CompareTo(end) < 0) { tmp = 0; foreach (Rdv app in s) { if (app.IsVacation && app.begining.CompareTo(DT_tmp) <= 0 && app.end.CompareTo(DT_tmp) >= 0) { tmp++; } } if (tmp > max) { max = tmp; } DT_tmp=DT_tmp.AddDays(1); } return (nb_service - max); } bool Delete_rdv_contact_between(int cont,DateTime start, DateTime end) { int i = 0; while (i" + rdvs[i].end.ToShortDateString()); write("rdv supprimé"); rdvs.RemoveAt(i);//si on efface un élément, alors on racourci d 1 le tableau donc pas besoin d'incrémenter l'index } else { i++; }//si on a pas effacé, on regarde le suivant } else { i++; } } return true; } void save_rdvs() { try { using (StreamWriter writer = new StreamWriter("rdvs.txt")) foreach (Rdv r in rdvs) { string line; line = r.begining + " " + r.end + " " + r.employee + " " + r.contact + " " + r.IsVacation; // write(line); writer.WriteLine(line); } write("rdvs.txt enregistré"); } catch (Exception a) { // Let the user know what went wrong. Error_Message("Impossible d'écrire le fichier rdvs.txt"); write(a.Message); } return; } void load_rdvs() { StreamReader reader=null; try { reader = new StreamReader("rdvs.txt"); string line; while ((line = reader.ReadLine()) != null) { string[] r,date,hour; int d, M, y, h, m, s; DateTime start, end; r = line.Split(' '); //Récupération de la date de début date = r[0].Split('/'); hour = r[1].Split(':'); d=Convert.ToInt32(date[0]); M=Convert.ToInt32(date[1]); y=Convert.ToInt32(date[2]); h=Convert.ToInt32(hour[0]); m = Convert.ToInt32(hour[1]); s = Convert.ToInt32(hour[2]); start = new DateTime(y, M, d, h, m, s); //Récupération de la date de fin date = r[2].Split('/'); hour = r[3].Split(':'); d = Convert.ToInt32(date[0]); M = Convert.ToInt32(date[1]); y = Convert.ToInt32(date[2]); h = Convert.ToInt32(hour[0]); m = Convert.ToInt32(hour[1]); s = Convert.ToInt32(hour[2]); end = new DateTime(y, M, d, h, m, s); int emp = Convert.ToInt32(r[4]); int cont = Convert.ToInt32(r[5]); bool vac; if (r[6] == "True") { vac = true; } else { vac = false; } Rdv nouv_rdv = new Rdv(start, end, emp, cont, vac); rdvs.Add(nouv_rdv); if (nouv_rdv.IsVacation && nouv_rdv.employee!=-2) { Get_Employee_by_ID(nouv_rdv.employee).holiday = nb_jour_entre(start, end);//MaJ Du quota de vacances } } } catch (Exception a) { // Let the user know what went wrong. Error_Message("Impossible de lire rdvs.txt"); write(a.Message); } reader.Close(); } private void button1_Click(object sender, EventArgs e) { save_rdvs(); } void affiche_emp(int emp) { write("-----"); write(Get_Employee_by_ID(emp).firstname + " " + Get_Employee_by_ID(emp).name); write(Get_Service_by_ID(Get_Employee_by_ID(emp).service).name + " / Nombre de jour de congés utilisé : " + Get_Employee_by_ID(emp).holiday + " "); write("-----"); foreach (Rdv r in rdvs) { if (r.employee == emp) { if (r.IsVacation) { write("Vacances : " + r.begining.ToShortDateString() + "-->" + r.end.ToShortDateString()); } else { write("Rdv avec " + Get_Contact_by_ID(r.contact).firstname + " " + Get_Contact_by_ID(r.contact).name + " : " + r.begining.ToShortDateString() + "-->" + r.end.ToShortDateString()); } } } } void affiche_contact(int contact) { write("-----"); write(Get_Contact_by_ID(contact).firstname + " " + Get_Contact_by_ID(contact).name); write(Get_Contact_by_ID(contact).enterprise); write("-----"); foreach (Rdv r in rdvs) { if (r.contact == contact) { if (r.IsVacation) { write("- Vacances : " + r.begining.ToShortDateString() + "-->" + r.end.ToShortDateString()); } else { write("- Rdv avec " + Get_Employee_by_ID(r.employee).firstname + " " + Get_Contact_by_ID(r.employee).name + " : " + r.begining.ToShortDateString() + "-->" + r.end.ToShortDateString()); } } } } private void Employee_info_Click(object sender, EventArgs e) { try { MaJ_affichage(true,Get_Employee_ID(Employees_Box.SelectedItem.ToString())); } catch (Exception ) { Error_Message("selectionnez un employé"); } } void MaJ_affichage(bool employee, int id) { if (employee) { view_contact = false; view_employee = true; affiche_emp(id); string name = Get_Employee_by_ID(id).firstname + " " + Get_Employee_by_ID(id).name; Info.Clear(); Info.Text = "Employee : " + name + "\r\nService : " + Get_Service_by_ID(Get_Employee_by_ID(id).service).name + " \r\nCongés : " + Get_Employee_by_ID(id).holiday+" Jours utilisés"; DataGridViewRowCollection rowsR = this.Rdv_Viewer.Rows; DataGridViewRowCollection rowsV = this.Vac_Viewer.Rows; rowsR.Clear(); rowsV.Clear(); foreach (Rdv r in rdvs) { if (r.employee == id) { if (!r.IsVacation) { string[] ligne = { Get_Contact_by_ID(r.contact).firstname + " " + Get_Contact_by_ID(r.contact).name, r.begining.ToShortDateString() + " " + r.begining.ToShortTimeString(), r.end.ToShortDateString() + " " + r.end.ToShortTimeString() }; rowsR.Add(ligne); } else { string[] ligne = { r.begining.ToShortDateString(), r.end.ToShortDateString() }; rowsV.Add(ligne); } } } } else { view_contact = true; view_employee = false; affiche_contact(id); string name = Get_Contact_by_ID(id).firstname + " " + Get_Contact_by_ID(id).name; Info.Clear(); Info.Text = "Contact : " + name + "\r\n\r\n" + Get_Contact_by_ID(id).enterprise; DataGridViewRowCollection rowsR = this.Rdv_Viewer.Rows; DataGridViewRowCollection rowsV = this.Vac_Viewer.Rows; rowsR.Clear(); rowsV.Clear(); foreach (Rdv r in rdvs) { if (r.contact == id) { if (!r.IsVacation) { string[] ligne = { Get_Employee_by_ID(r.employee).firstname + " " + Get_Employee_by_ID(r.employee).name, r.begining.ToLongDateString(), r.end.ToLongDateString() }; rowsR.Add(ligne); } else { string[] ligne = { r.begining.ToShortDateString(), r.end.ToShortDateString() }; rowsV.Add(ligne); } } } } } private void Contact_informations_Click(object sender, EventArgs e) { try{ MaJ_affichage(false,Get_Contact_ID(Contacts_Box.SelectedItem.ToString())); } catch (Exception ) { Error_Message("Sélectionnez un contact"); } } int nb_jour_entre(DateTime s, DateTime e) { int j = 0; while (s.CompareTo(e) <= 0) { if (s.DayOfWeek != DayOfWeek.Saturday && s.DayOfWeek != DayOfWeek.Sunday) { j++; } s = s.AddDays(1); } return j; } bool NoWE(DateTime s, DateTime e) { bool week = true; while (week && s.CompareTo(e) <= 0) { if (s.DayOfWeek == DayOfWeek.Saturday || s.DayOfWeek == DayOfWeek.Sunday) { week = false; } s = s.AddDays(1); } return week; } bool MoreThanOneWeek(DateTime s, DateTime e) { if (e.Subtract(s).Days >= 6) { return true; } else { return false; } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void LogMode_CheckedChanged(object sender, EventArgs e) { if (LogMode.Checked) { Rdv_Viewer.Hide(); Vac_Viewer.Hide(); L_app.Hide(); L_Vac.Hide(); Info.Hide(); L_infos.Hide(); LogBox.Show(); L_log.Show(); } else { Rdv_Viewer.Show(); Vac_Viewer.Show(); L_app.Show(); L_Vac.Show(); Info.Show(); L_infos.Show(); LogBox.Hide(); L_log.Hide(); } } } }