public bool CreatePrivateCustomer(string firstName, string lastName, string address, int zipCode, int phoneNumber) { if (phoneNumber.ToString().Length != 8) { MessageBox.Show("Telefon nummer er ikke gyldigt"); return(false); } if (DoesPrivateCustomerExists(phoneNumber) == true) { MessageBox.Show("Der findes allerede en kunde med dette nummer i systemet"); return(false); } else { tempPrivateCustomer = new PrivateCustomer(firstName, lastName, address, zipCode, phoneNumber, 0, 0); return(true); } }
public void CreateBooking(string date, int workingHours, int hourlyPay, string bookingDescription, bool isNewCustomer, int tempId) { booking = new booking(tempPrivateCustomer, date, workingHours, hourlyPay, bookingDescription, false, 0); //checks if it needs to create a new customer or just refrence an already existing one. if (isNewCustomer == true) { //Is a private type customer if (tempPrivateCustomer != null) { String query = "INSERT INTO PrivateCustomer (firstName,lastname,address,zipCode,phoneNumber,amountSpent) VALUES (@firstName, @lastName, @address, @zipCode, @phoneNumber, @amountSpent)"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(query, connection)) { viewAmountSpent(tempId); command.Parameters.AddWithValue("@firstName", tempPrivateCustomer.FirstName); command.Parameters.AddWithValue("@lastName", tempPrivateCustomer.LastName); command.Parameters.AddWithValue("@address", tempPrivateCustomer.Address); command.Parameters.AddWithValue("@zipCode", tempPrivateCustomer.ZipCode); command.Parameters.AddWithValue("@phoneNumber", tempPrivateCustomer.PhoneNumber); command.Parameters.AddWithValue("@amountSpent", booking.HourlyPay * booking.WorkingHours); connection.Open(); int result = command.ExecuteNonQuery(); // Check Error if (result < 0) { MessageBox.Show("Error while inserting private customer into database"); } } DataSet dataSet = Execute("SELECT id FROM PrivateCustomer ORDER BY id ASC"); DataTable customerTable = dataSet.Tables[0]; List <int> privateCustomers = new List <int>(); foreach (DataRow itemRow in customerTable.Rows) { privateCustomers.Add( (int)itemRow["id"]); } id = privateCustomers[privateCustomers.Count - 1]; } //is a corporate type customer else { String query = "INSERT INTO CorporateCustomer (companyName,seNumber,phoneNumber,amountSpent) VALUES (@companyName, @seNumber, @phoneNumber, @amountSpent)"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(query, connection)) { viewAmountSpent(tempId); command.Parameters.AddWithValue("@companyName", tempCorporateCustomer.CompanyName); command.Parameters.AddWithValue("@seNumber", tempCorporateCustomer.SeNumber); command.Parameters.AddWithValue("@phoneNumber", tempCorporateCustomer.PhoneNumber); command.Parameters.AddWithValue("@amountSpent", booking.HourlyPay * booking.WorkingHours); connection.Open(); int result = command.ExecuteNonQuery(); // Check Error if (result < 0) { MessageBox.Show("Error while inserting private customer into database. Restart and try again"); } } DataSet dataSet = Execute("SELECT id FROM CorporateCustomer ORDER BY id ASC"); DataTable customerTable = dataSet.Tables[0]; List <int> privateCustomers = new List <int>(); foreach (DataRow itemRow in customerTable.Rows) { privateCustomers.Add( (int)itemRow["id"]); } id = privateCustomers[privateCustomers.Count - 1]; } //After creation of customer create booking. String bookingQuery = "INSERT INTO Booking (workingHours,hourlyPay,bookingDescription,workComplete,foreignKey,date) VALUES (@workingHours, @hourlyPay, @bookingDescription, @workComplete, @foreignKey, @date)"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(bookingQuery, connection)) { viewAmountSpent(tempId); command.Parameters.AddWithValue("@workingHours", booking.WorkingHours); command.Parameters.AddWithValue("@hourlyPay", booking.HourlyPay); command.Parameters.AddWithValue("@bookingDescription", booking.BookingDescription); command.Parameters.AddWithValue("@workComplete", booking.WorkComplete); command.Parameters.AddWithValue("@foreignKey", id); command.Parameters.AddWithValue("@date", booking.Date); connection.Open(); int result = command.ExecuteNonQuery(); // Check Error if (result < 0) { MessageBox.Show("Error while inserting private customer into database"); } } } //creates a booking with an id refrence to an already existing customer else { //After creation of customer create booking. String bookingQuery = "INSERT INTO Booking (workingHours,hourlyPay,bookingDescription,workComplete,foreignKey,date) VALUES (@workingHours, @hourlyPay, @bookingDescription, @workComplete, @foreignKey, @date)"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(bookingQuery, connection)) { command.Parameters.AddWithValue("@workingHours", booking.WorkingHours); command.Parameters.AddWithValue("@hourlyPay", booking.HourlyPay); command.Parameters.AddWithValue("@bookingDescription", booking.BookingDescription); command.Parameters.AddWithValue("@workComplete", booking.WorkComplete); command.Parameters.AddWithValue("@foreignKey", tempId); command.Parameters.AddWithValue("@date", booking.Date); connection.Open(); int result = command.ExecuteNonQuery(); viewAmountSpent(tempId); // Check Error if (result < 0) { MessageBox.Show("Error while inserting private customer into database"); } } } tempCorporateCustomer = null; tempPrivateCustomer = null; }