private void btnSaveInvoice_Click(object sender, EventArgs e) { try { int invoiceId = string.IsNullOrEmpty(txtInvoiceNumber.Text) ? 0 : Convert.ToInt32(txtInvoiceNumber.Text); var invoiceDetails = invoiceService.GetInvoice(invoiceId); var comapnyDetails = (from company in companyService.GetAllCompany() .Where(v => v.Status == true) select company).SingleOrDefault(); Invoice invoice = new Invoice(); invoice.InvoiceId = invoiceId; invoice.CompanyId = comapnyDetails.CompanyId; invoice.ClientId = ClientId; invoice.IssueDate = dtpIssueDate.Value.Date; invoice.PurchaseOrderNo = string.IsNullOrEmpty(txtPONumber.Text) ? String.Empty : txtPONumber.Text; invoice.PaymentTermId = Convert.ToInt32(cmbPaymentTerms.SelectedValue); invoice.DueDate = dtpDueDate.Value.Date; invoice.Discount = string.IsNullOrEmpty(txtDiscount.Text) ? decimal.Zero : Convert.ToDecimal(txtDiscount.Text); invoice.RoundOffTotal = chkRoundOff.Checked; invoice.MarkInvoicePaid = chkMarkInvoicePaid.Checked; if (chkMarkInvoicePaid.Checked) { invoice.PaymentTypeId = Convert.ToInt32(cmbPaymentType.SelectedValue); invoice.AmountPaid = string.IsNullOrEmpty(txtAmountPaid.Text) ? decimal.Zero : Convert.ToDecimal(txtAmountPaid.Text); if (invoiceDetails == null || invoiceDetails.InvoiceId == 0) { invoice.PaymentDate = DateTime.Now.Date; } else { if (invoiceDetails.AmountPaid > 0) { if (invoiceDetails.AmountPaid == invoice.AmountPaid) { invoice.PaymentDate = invoiceDetails.PaymentDate; } else { invoice.PaymentDate = DateTime.Now.Date; } } } string paymentStatus = String.Empty; if (invoice.AmountPaid == 0) { paymentStatus = ePaymentStatus.Unpaid.ToString(); } else if (invoice.AmountPaid > 0) { if (invoice.AmountPaid < TotalValue) { paymentStatus = ePaymentStatus.Partial.ToString(); } else if (invoice.AmountPaid == TotalValue) { paymentStatus = ePaymentStatus.Paid.ToString(); } else { paymentStatus = ePaymentStatus.Paid.ToString(); } } if (invoice.PaymentDate.Date > invoice.DueDate.Date) { if (String.IsNullOrEmpty(paymentStatus)) { paymentStatus = String.Concat(paymentStatus, ",", ePaymentStatus.Overdue.ToString()); } else { paymentStatus = ePaymentStatus.Overdue.ToString(); } } invoice.PaymentStatus = paymentStatus; } else { invoice.PaymentTypeId = 0; invoice.AmountPaid = decimal.Zero; } invoice.Notes = string.IsNullOrEmpty(txtNotes.Text) ? String.Empty : txtNotes.Text; invoice.TotalAmount = TotalValue; invoice.NotesForClient = string.IsNullOrEmpty(txtNoteForClient.Text) ? String.Empty : txtNoteForClient.Text; invoice.PrivateNotes = string.IsNullOrEmpty(txtprivateNotes.Text) ? String.Empty : txtprivateNotes.Text; invoice.Status = true; if (invoiceDetails == null || invoiceDetails.InvoiceId == 0) { using (TransactionScope scope = new TransactionScope()) { invoiceService.AddInvoice(invoice); lstInvoiceProduct = lstInvoiceProduct.OrderBy(s => s.InvoiceProductId).ToList <InvoiceProduct>(); for (int i = 0; i < lstInvoiceProduct.Count; i++) { InvoiceProduct invoiceProduct = new InvoiceProduct(); invoiceProduct.InvoiceId = lstInvoiceProduct[i].InvoiceId; invoiceProduct.ProductId = lstInvoiceProduct[i].ProductId; invoiceProduct.ProductName = lstInvoiceProduct[i].ProductName; invoiceProduct.Description = lstInvoiceProduct[i].Description; invoiceProduct.Quantity = lstInvoiceProduct[i].Quantity; invoiceProduct.TaxId = lstInvoiceProduct[i].TaxId; invoiceProduct.TaxValue = lstInvoiceProduct[i].TaxValue; invoiceProduct.UnitPrice = lstInvoiceProduct[i].UnitPrice; invoiceProduct.TotalPrice = lstInvoiceProduct[i].TotalPrice; invoiceService.AddInvoiceProducts(invoiceProduct); } scope.Complete(); } } else { using (TransactionScope scope = new TransactionScope()) { invoiceService.UpdateInvoice(invoice); for (int i = 0; i < lstInvoiceProduct.Count; i++) { InvoiceProduct invoiceProduct = new InvoiceProduct(); invoiceProduct.InvoiceId = lstInvoiceProduct[i].InvoiceId; invoiceProduct.ProductId = lstInvoiceProduct[i].ProductId; invoiceProduct.ProductName = lstInvoiceProduct[i].ProductName; invoiceProduct.Description = lstInvoiceProduct[i].Description; invoiceProduct.Quantity = lstInvoiceProduct[i].Quantity; invoiceProduct.TaxId = lstInvoiceProduct[i].TaxId; invoiceProduct.TaxValue = lstInvoiceProduct[i].TaxValue; invoiceProduct.UnitPrice = lstInvoiceProduct[i].UnitPrice; invoiceProduct.TotalPrice = lstInvoiceProduct[i].TotalPrice; invoiceService.UpdateInvoiceProducts(invoiceProduct); } scope.Complete(); } } CustomMessageBox.Show(string.Format(Constants.SUCCESSFULL_ADD_INVOICE_MESSAGE, invoice.InvoiceId), Constants.CONSTANT_INFORMATION, Sleek_Bill.Controls.CustomMessageBox.eDialogButtons.OK, CustomImages.GetDialogImage(Sleek_Bill.Controls.CustomImages.eCustomDialogImages.Success)); this.Close(); } catch (Exception ex) { MessageBox.Show("Error Saving invoice: " + ex.Message); } }