private void btnDelete_Click(object sender, EventArgs e) { if (ShowConfirmationDialog(Resources.DeleteInvoiceConfirmationMsg) == DialogResult.Yes) { if (dgvInvoices.Rows.Count > 0) { var invoiceId = int.Parse(dgvInvoices.SelectedRows[0].Cells[0].Value.ToString()); var invoice = InvoiceManager.GetInvoiceById(invoiceId); var invoiceItems = invoice.InvoiceItems.ToList(); MaterialManager.UpdateQuantitiesAfterDeletingInvoice(invoiceItems); InvoiceItemManager.DeleteInvoiceItems(invoiceItems); InvoicePaymentManager.DeleteInvoicePayments(invoice.InvoicePayments.ToList()); InvoiceManager.DeleteInvoice(invoice); ResetForm(); } } }
private void SaveInvoice() { ErrorProvider.Clear(); var isFormValid = true; Client newClient = null; if (txtClientName.Text.IsNullOrEmptyOrWhiteSpace()) { isFormValid = false; ErrorProvider.SetError(txtClientName, Resources.ThisFieldIsRequired); } else if (!ClientsNames.Contains(txtClientName.Text.FullTrim())) { isFormValid = ShowConfirmationDialog(Resources.ClientNotExists) == DialogResult.Yes; newClient = new Client { Name = txtClientName.Text.FullTrim() }; } if (!InvoiceItemVms.Any()) { isFormValid = false; ShowErrorMsg(Resources.InvoiceWithoutItems); } if (!isFormValid) { return; } if (!InvoiceItemVms.Any()) { ShowErrorMsg(Resources.NoItemsAdded); return; } if (newClient != null) { ClientManager.AddClient(newClient); } var invoice = new Invoice { ClientId = ClientManager.GetClientIdByName(txtClientName.Text.FullTrim()), Date = dtInvoiceDate.Value, Total = (decimal)dblInTotal.Value, Paid = (decimal)dblInPaid.Value, Discount = (decimal)dblInDiscount.Value }; InvoiceManager.AddInvoice(invoice); InvoiceItemManager.AddInvoiceItems(InvoiceItemVms.Select(item => new InvoiceItem { InvoiceId = invoice.Id, MaterialId = item.MaterialId, Quantity = item.Quantity, Price = item.TotalPrice, Notes = item.Notes }).ToList()); if (invoice.Paid > 0) { InvoicePaymentManager.AddInvoicePayment(new InvoicePayment { InvoiceId = invoice.Id, Date = dtInvoiceDate.Value, Paid = (decimal)dblInPaid.Value }); } MaterialManager.UpdateQuantitiesAfterCreatingInvoice(InvoiceItemVms); ShowInfoMsg(Resources.InvoiceCreatedSuccessfully); Close(); }