//constructor
 public ControllerAudit(ControllerParent calling, FormAudit view, ArrayList list_customers, ArrayList list_licenseinventories, ArrayList list_systeminventories, ArrayList list_audits, ArrayList list_licenses) : base(calling, list_customers)
 {
     //connect controller to its view
     this.view = view;
     this.list_licenseInventories = list_licenseinventories;
     this.list_systemInventories = list_systeminventories;
     this.list_audits = list_audits;
     this.list_allAvailableLicenses = list_licenses;
     currentAudit = null;
     currentLicenseInventory = null;
     currentSystemInventory = null;
 }
        public override void SelectedCustomerChanged(Object customer)
        {
            base.SelectedCustomerChanged(customer);
            Log.WriteLog(string.Format("Customer changed successfully: New Customer: {0}", currentCustomer.Name));

            //Get Networkinventory of the customer
            currentNetworkInventory = null;
            foreach (NetworkInventory n in list_networkInventories)
            {
                if (n.Customernumber == currentCustomer.Cnumber)
                {
                    currentNetworkInventory = n;
                    Log.WriteLog(string.Format("NetworkInventory for customer {0} found", currentCustomer.Name));
                }
            }

            //Get LicenseInventory of the customer
            currentLicenseInventory = null;
            foreach (LicenseInventory li in list_licenseInventories)
            {
                if (li.Customernumber == currentCustomer.Cnumber)
                {
                    currentLicenseInventory = li;
                    Log.WriteLog(string.Format("LicenseInventory for customer {0} found", currentCustomer.Name));
                }
            }

            //Get latest SystemInventory of the customer
            currentSystemInventory = null;
            foreach (SystemInventory si in list_systemInventories)
            {
                if (si.Customernumber == currentCustomer.Cnumber)
                {
                    currentSystemInventory = si;
                    Log.WriteLog(string.Format("SystemInventory for customer {0} found", currentCustomer.Name));
                }
            }

            //Get latest Audit of the customer
            currentAudit = null;
            foreach (Audit a in list_Audits)
            {
                if (a.CustomerNumber == currentCustomer.Cnumber)
                {
                    currentAudit = a;
                    Log.WriteLog(string.Format("Audit for customer {0} found", currentCustomer.Name));
                }
            }
            UpdateView(false);


        }
 public ArrayList GetAudits()
 {
     ArrayList list_audits = new ArrayList();
     try
     {
         connection = new SQLiteConnection("Data Source=" + dbpath + "; Version=3; MultipleActiveResultSets=True; foreign keys=true;");
         connection.Open();
         command = new SQLiteCommand(connection);
         command.CommandText = "SELECT * FROM audit;";
         SQLiteDataReader r = command.ExecuteReader();
         while (r.Read())
         {
             Audit a = new Audit(r.GetInt32(0), r.GetInt32(1), r.GetInt32(2));
             a.Date = r.GetDateTime(3);
             SQLiteCommand command2 = new SQLiteCommand(connection);
             command2.CommandText = "SELECT * FROM results WHERE auditNumber=@auditNumber;";
             command2.Parameters.AddWithValue("@auditNumber", a.AuditNumber);
             SQLiteDataReader r_inner = command2.ExecuteReader();
             while (r_inner.Read())
             {
                 a.AddResult(r_inner.GetInt32(1), r_inner.GetInt32(2));
             }
             list_audits.Add(a);
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("Error SQL Reading Audits", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Log.WriteLog(e.Message);
     }
     connection.Close();
     return list_audits;
 }
 public void SaveAudit(Audit a)
 {
     try
     {
         connection = new SQLiteConnection("Data Source=" + dbpath + ";Version=3; foreign keys=true;");
         connection.Open();
         command = new SQLiteCommand(connection);
         command.CommandText = "INSERT INTO audit (auditNumber, customerNumber, systemInventoryNumber, date) VALUES(@auditNumber, @customerNumber, @systemInventoryNumber, @date);";
         command.Parameters.AddWithValue("@auditNumber", a.AuditNumber);
         command.Parameters.AddWithValue("@customerNumber", a.CustomerNumber);
         command.Parameters.AddWithValue("@systemInventoryNumber", a.SystemInventoryNumber);
         command.Parameters.AddWithValue("@date", a.Date);
         command.ExecuteNonQuery();
         //Write Results
         for (int i = 0; i < a.Results.Count; i++)
         {
             Tuple<int, int> t = (Tuple<int, int>)a.Results[i];
             command.CommandText = "INSERT INTO results (auditNumber, licenseNumber, result) VALUES(@auditNumber, @licenseNumber, @result);";
             command.Parameters.AddWithValue("@auditNumber", a.AuditNumber);
             command.Parameters.AddWithValue("@licenseNumber", t.Item1);
             command.Parameters.AddWithValue("@result", t.Item2);
             command.ExecuteNonQuery();
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("Error SQL Write Audit", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Log.WriteLog(e.Message);
     }
     connection.Close();
 }
        public void cmbInventory_SelectedIndexChanged(Object inventory)
        {
            if (!cmbInventorySelectedIndex)
            {
                //extract Date from cmbInventory
                string input = (string)inventory;
                Log.WriteLog(string.Format("Inventory changed successfully: New Inventory: {0}", input));
                string[] splitted_input = input.Split();
                input = splitted_input[1] + " " + splitted_input[2];
                DateTime inventoryDate = DateTime.Parse(input);
                //get new System Inventory
                foreach (SystemInventory si in list_systemInventories)
                {
                    if (DateTime.Compare(si.Date, inventoryDate)==0)
                    {
                        currentSystemInventory = si;
                        Log.WriteLog(string.Format("Belonging SystemInventory found: {0}", currentSystemInventory.SystemInventoryNumber));
                    }
                }
                //get auidt belonging system inventory if it exists
                currentAudit = null;
                foreach (Audit a in list_audits)
                {
                    if (a.CustomerNumber == currentCustomer.Cnumber && a.SystemInventoryNumber == currentSystemInventory.SystemInventoryNumber)
                    {
                        currentAudit = a;
                        Log.WriteLog(string.Format("Belonging Audit found: {0}", currentAudit.AuditNumber));
                    }
                }
                UpdateView(false);
            }

        }
        //functions
        public void Compare()
        {
            if (currentCustomer == null)
            {
                MessageBox.Show("Kein Kunde ausgewählt. Bitte einen Kunden auswählen und Audit erneut starten.", "Kein Kunde ausgewählt.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                currentAudit = new Audit(list_audits.Count, currentCustomer.Cnumber, currentSystemInventory.SystemInventoryNumber);
                //Count Systems
                //
                //get List of system types
                List<string> types = new List<string>();
                foreach (ClientSystem c in currentSystemInventory.List_Systems)
                {
                    //Add  only types of systems with detailed Information and types which aren't in the list already
                    if ((!types.Contains(c.Type)) && (c.Type != null) && !(c.Type.Equals("")))
                    {
                        types.Add(c.Type);
                    }
                }
                //count occurence of types in Systeminventory
                int[] count = new int[types.Count];
                for (int i = 0; i < types.Count; i++)
                {
                    count[i] = 0;
                    foreach (ClientSystem c in currentSystemInventory.List_Systems)
                    {
                        //do this only for systems with detailed information
                        if (c.Type != null && !(c.Type.Equals("")))
                        {
                            if (c.Type.Equals(types[i]))
                            {
                                count[i]++;
                                Log.WriteLog(String.Format("{0} occures {1} times", types[i], count[i]));
                            }
                        }
                    }
                }
                //Compare against Licennse Inventory
                for (int i = 0; i < types.Count; i++)
                {
                    int licenses = 0;
                    //Get the licensenumber of the current type (if licensetype is known)
                    int licensenumber = -1;
                    foreach (License l in list_allAvailableLicenses)
                    {
                        if (l.Name.Equals(types[i]))
                        {
                            licensenumber = l.LicenseNumber;
                        }
                    }
                    if (licensenumber == -1)
                    {
                        //licensetype unknown, learn it (shouldn't happen because license should be learned during inventoryprocess)
                        License newlicense = new License(list_allAvailableLicenses.Count, types[i]);
                        list_allAvailableLicenses.Add(newlicense);
                        db.SaveLicense(newlicense);
                        licensenumber = newlicense.LicenseNumber;
                    }
                    //Get the corresponding count of the licensinventory, if the license isn't in the inventory the count is 0 (initialised)
                    for (int x = 0; x < currentLicenseInventory.Inventory.Count; x++)
                    {
                        Tuple<int, int> t = (Tuple<int, int>)currentLicenseInventory.Inventory[x];
                        if (t.Item1 == licensenumber)
                        {
                            licenses = t.Item2;
                            x = currentLicenseInventory.Inventory.Count;
                        }
                    }
                    //Add result to Audit, Licensenumber and number of free licenses of this type
                    currentAudit.AddResult(licensenumber, licenses - count[i]);
                }
                //Add licenses of invetory to results which aren't already added
                ArrayList helpList = new ArrayList();
                foreach (Tuple<int, int> tuplelicense in currentLicenseInventory.Inventory)
                {
                    bool contains = false;
                    Tuple<int, int> helpTuple = new Tuple<int, int>(-1, -1);
                    foreach (Tuple<int, int> t in currentAudit.Results)
                    {
                        helpTuple = (Tuple<int, int>)t;
                        if (helpTuple.Item1 == tuplelicense.Item1)
                        {
                            contains = true;
                            break;
                        }
                    }
                    if (!contains)
                    {
                        currentAudit.AddResult(tuplelicense.Item1, tuplelicense.Item2);
                    }
                }
                list_audits.Add(currentAudit);
                db.SaveAudit(currentAudit);
                callingController.UpdateInformation();
                MessageBox.Show("Audit abgeschlossen.", "Audit abgeschlossen", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            UpdateView(false);

        }
        public override void SelectedCustomerChanged(Object customer)
        {
            base.SelectedCustomerChanged(customer);
            currentLicenseInventory = null;
            currentSystemInventory = null;
            currentAudit = null;
            Log.WriteLog(string.Format("Customer changed successfully: New Customer: {0}", currentCustomer.Name));
            //Get Licenseinventory of the customer or Display Message
            foreach (LicenseInventory li in list_licenseInventories)
            {
                if (li.Customernumber == currentCustomer.Cnumber)
                {
                    currentLicenseInventory = li;
                    Log.WriteLog(string.Format("Licenseinventory for customer {0} found", currentCustomer.Name));
                    view.EnableAudit();
                }
            }
            if (currentLicenseInventory == null)
            {
                MessageBox.Show("Kein Lizenzinventar für diesen Kunden gefunden. Bitte erstellen Sie zuerst ein Lizenzinventar.", "Kein Lizenzinventar gefunden", MessageBoxButtons.OK, MessageBoxIcon.Error);
                view.DisableAudit();
            }
            //Get latest Audit
            foreach (Audit a in list_audits)
            {
                if (a.CustomerNumber == currentCustomer.Cnumber)
                {
                    if (currentAudit == null)
                    {
                        currentAudit = a;
                        Log.WriteLog(string.Format("Audit for customer {0} found", currentCustomer.Name));
                    }
                    else if (DateTime.Compare(a.Date, currentAudit.Date) > 0)
                    {
                        currentAudit = a;
                        Log.WriteLog(string.Format("Newer Audit for customer {0} found", currentCustomer.Name));
                    }

                }
            }
            if (currentAudit != null)
            {
                //Get belonging SystemInventory of the current Audit
                foreach (SystemInventory si in list_systemInventories)
                {
                    if (si.SystemInventoryNumber == currentAudit.SystemInventoryNumber)
                    {
                        currentSystemInventory = si;
                        Log.WriteLog("Belonging Systeminventory for current Audit found.");
                        view.EnableAudit();
                        MessageBox.Show(String.Format("Audit für diesen Kunden gefunden. Audit vom {0} für Systeminventar vom {1} wird dargestellt.\nBitte beachten Sie, dass sich das Lizenzinventar des Kunden seitdem geändert haben kann und das aktuell dargestellte Lizenzinventar nicht mit dem Ergebnis des Audits in Verbindung steht.",
                            currentAudit.Date, currentSystemInventory.Date), "Audit vorhanden", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    }
                }
            }
            //Get Systeminventory of the customer or Display Message in case no audit with belonging systeminventory was found
            if (currentSystemInventory == null)
            {
                foreach (SystemInventory si in list_systemInventories)
                {
                    if (si.Customernumber == currentCustomer.Cnumber)
                    {
                        currentSystemInventory = si;
                        Log.WriteLog(string.Format("Systeminventory for customer {0} found", currentCustomer.Name));
                        view.EnableAudit();
                    }
                }
                if (currentSystemInventory == null)
                {
                    MessageBox.Show("Kein Systeminventar für diesen Kunden gefunden. Bitte führen Sie zuerst ein Netzwerkinventarisierung durch.", "Kein Systeminventar gefunden", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    view.DisableAudit();
                }
            }
            UpdateView(false);
        }