private void InvoiceToolStripMenuItem_Click(object sender, EventArgs e) { Invoice1 inv = new Invoice1(); this.Hide(); inv.Show(); }
private void Button2_Click(object sender, EventArgs e) { ButtonPrintClick(); con = new SqlConnection(cs); con.Open(); int[] toolNos = new int[5]; string toolNumbers = ""; if (comboTool1.Text != "") { toolNos[0] = int.Parse(comboTool1.Text); toolNumbers = comboTool1.Text; } if (comboTool2.Text != "") { toolNos[1] = int.Parse(comboTool2.Text); toolNumbers = toolNumbers + "," + comboTool2.Text; } if (comboTool3.Text != "") { toolNos[2] = int.Parse(comboTool3.Text); toolNumbers = toolNumbers + "," + comboTool3.Text; } context.Invoices.Add(new Invoice { Accessories = "", AccessoriesSubtotal = decimal.Parse(lblSubTotalAcc.Text.Substring(1, lblSubTotalAcc.Text.Length - 3)), AddressWhereUsed = invModel.addressWhereUsed, Collection = decimal.Parse(txtCollection.Text.Substring(1, txtCollection.Text.Length - 3)), Delivery = decimal.Parse(txtDelivery.Text.Substring(1, txtDelivery.Text.Length - 3)), Discount = decimal.Parse(txtDiscount.Text.Substring(1, txtDiscount.Text.Length - 3)), EndOfHire = dateTimeEndHire.Value.Date, EquipmentDueBack = dateTimeDueBack.Value.Date, EquipmentNos = toolNumbers, EquipmentSubtotal = decimal.Parse(lblSubTotal.Text.Substring(1, lblSubTotal.Text.Length - 3)), GrandTotal = decimal.Parse(lblGrandTotal.Text.Substring(1, lblGrandTotal.Text.Length - 3)), HirerAddress = invModel.hirerAddress, HirerCarMakeModel = invModel.hirerCarMakeModel, HirerCarReg = invModel.hirerCarRegNo, HirerIdentityNo = invModel.hirerIdentityNo, HirerName = invModel.hirerName, HirerTelNo = invModel.hirerTelNo, HowDidYouKnowUs = txtGetToKnowUs.Text, InitialPaymentReceived = decimal.Parse(txtInitialAmount.Text.Substring(1, txtInitialAmount.Text.Length - 3)), InvoiceDate = dateTimeInvoice.Value.Date, InvoiceNo = txtInvoiceNo.Text, Other = decimal.Parse(txtOther.Text.Substring(1, txtOther.Text.Length - 3)), SpecialInstructions = invModel.specialInstructions, StartOfHire = dateTimeStart.Value.Date }); try { context.SaveChanges(); var confirmed = MessageBox.Show("Invoice has been added to the database successfully - Add another Invoice?", "Attempt To Add New Invoice", MessageBoxButtons.YesNo); var customerId = (from a in context.Customers where a.ID_Number == invModel.hirerIdentityNo select a.CustomerId).First(); for (int i = 0; i < toolNos.Length; i++) { if (i == 0) { context.Hire_Orders.Add(new Hire_Order { Customer_ID = customerId, Machine_ID = toolNos[i], Invoice_Number = txtInvoiceNo.Text, Cost_Per_Day = decimal.Parse(lblRate1.Text.Substring(1, lblRate1.Text.Length - 2)), Date_Booked_Out = dateTimeStart.Value, Date_Booked_In = dateTimeDueBack.Value, Total_Cost = decimal.Parse(lblFee1.Text.Substring(1, lblFee1.Text.Length - 2)) }); } if (i == 1) { context.Hire_Orders.Add(new Hire_Order { Customer_ID = customerId, Machine_ID = toolNos[i], Invoice_Number = txtInvoiceNo.Text, Cost_Per_Day = decimal.Parse(lblRate2.Text.Substring(1, lblRate2.Text.Length - 2)), Date_Booked_Out = dateTimeStart.Value, Date_Booked_In = dateTimeDueBack.Value, Total_Cost = decimal.Parse(lblFee2.Text.Substring(1, lblFee2.Text.Length - 2)) }); } if (i == 2) { context.Hire_Orders.Add(new Hire_Order { Customer_ID = customerId, Machine_ID = toolNos[i], Invoice_Number = txtInvoiceNo.Text, Cost_Per_Day = decimal.Parse(lblRate3.Text.Substring(1, lblRate3.Text.Length - 2)), Date_Booked_Out = dateTimeStart.Value, Date_Booked_In = dateTimeDueBack.Value, Total_Cost = decimal.Parse(lblFee3.Text.Substring(1, lblFee3.Text.Length - 2)) }); } } context.SaveChanges(); if (confirmed == DialogResult.Yes) { Invoice1 invoice = new Invoice1(); this.Close(); invoice.Show(); } else { Home home = new Home(); this.Close(); home.Show(); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, "Attempt To Add New Invoice", MessageBoxButtons.OK); this.Close(); } finally { con.Close(); } }