private void btnDelete_Click(object sender, EventArgs e) { if (ShowConfirmationDialog(Resources.DeleteInvoiceConfirmationMsg) == DialogResult.Yes) { if (dgvInvoices.Rows.Count > 0) { var purchaseInvoiceId = int.Parse(dgvInvoices.SelectedRows[0].Cells[0].Value.ToString()); var purchaseInvoice = PurchaseInvoiceManager.GetPurchaseInvoiceById(purchaseInvoiceId); var purchaseInvoiceItems = purchaseInvoice.PurchaseInvoiceItems.ToList(); MaterialManager.UpdateQuantitiesAfterDeletingPurchaseInvoice(purchaseInvoiceItems); PurchaseInvoiceItemManager.DeletePurchaseInvoiceItems(purchaseInvoiceItems); PurchaseInvoicePaymentManager.DeletePurchaseInvoicePayments(purchaseInvoice.PurchaseInvoicePayments .ToList()); PurchaseInvoiceManager.DeletePurchaseInvoice(purchaseInvoice); ResetForm(); } } }
private void SavePurchaseInvoice() { ErrorProvider.Clear(); var isFormValid = true; Supplier newSupplier = null; if (txtSupplierName.Text.IsNullOrEmptyOrWhiteSpace()) { isFormValid = false; ErrorProvider.SetError(txtSupplierName, Resources.ThisFieldIsRequired); } else if (!SuppliersNames.Contains(txtSupplierName.Text.FullTrim())) { isFormValid = ShowConfirmationDialog(Resources.SupplierNotExists) == DialogResult.Yes; newSupplier = new Supplier { Name = txtSupplierName.Text.FullTrim() }; } if (!PurchaseInvoiceItemVms.Any()) { isFormValid = false; ShowErrorMsg(Resources.InvoiceWithoutItems); } if (!isFormValid) { return; } if (!PurchaseInvoiceItemVms.Any()) { ShowErrorMsg(Resources.NoItemsAdded); return; } if (newSupplier != null) { SupplierManager.AddSupplier(newSupplier); } var purchaseInvoice = new PurchaseInvoice { SupplierId = SupplierManager.GetSupplierIdByName(txtSupplierName.Text.FullTrim()), Date = dtInvoiceDate.Value, Total = (decimal)dblInTotal.Value, Paid = (decimal)dblInPaid.Value, Discount = (decimal)dblInDiscount.Value }; PurchaseInvoiceManager.AddPurchaseInvoice(purchaseInvoice); PurchaseInvoiceItemManager.AddPurchaseInvoiceItems(PurchaseInvoiceItemVms.Select(item => new PurchaseInvoiceItem { InvoiceId = purchaseInvoice.Id, MaterialId = item.MaterialId, Quantity = item.Quantity, UnitPrice = item.UnitPrice, Notes = item.Notes }).ToList()); if (purchaseInvoice.Paid > 0) { PurchaseInvoicePaymentManager.AddPurchaseInvoicePayment(new PurchaseInvoicePayment { InvoiceId = purchaseInvoice.Id, Date = dtInvoiceDate.Value, Paid = (decimal)dblInPaid.Value }); } MaterialManager.UpdateQuantitiesAfterCreatingPurchaseInvoice(PurchaseInvoiceItemVms); ShowInfoMsg(Resources.InvoiceCreatedSuccessfully); Close(); }