protected void gvInvoices_RowCommand(object sender, GridViewCommandEventArgs e) { int invoiceID = 0; CRM.Data.Entities.LeadInvoice invoice = null; if (e.CommandName == "DoDelete") { invoiceID = Convert.ToInt32(e.CommandArgument); invoice = LeadInvoiceManager.Get(invoiceID); if (invoice != null) { invoice.isVoid = true; LeadInvoiceManager.Save(invoice); // refresh list bindData(); } } }
private void saveInvoice() { string[] billToValues = null; int clientID = 0; int invoiceID = 0; int InvoiceLineID = 0; Data.Entities.LeadInvoice invoice = null; CRM.Data.Entities.LeadPolicy policy = null; LeadInvoiceDetail invoiceDetailLine = null; LeadInvoiceDetail invoiceDetail = null; int nextInvoiceNumber = 0; int policyID = 0; decimal taxAmount = 0; billToValues = ddlBillTo.SelectedValue.Split(new char[] { '|' }); // get invoice id invoiceID = Convert.ToInt32(ViewState["InvoiceID"].ToString()); clientID = Core.SessionHelper.getClientId(); // current policy being edited policyID = Session["policyID"] == null ? 0 : Convert.ToInt32(Session["policyID"]); policy = LeadPolicyManager.GetByID(policyID); if (invoiceID == 0) { invoice = new Data.Entities.LeadInvoice(); // get id for current lead invoice.LeadId = Core.SessionHelper.getLeadId(); invoice.isVoid = false; // assign client invoice.ClientID = clientID; // hide print button //btnPrint.Visible = false; } else { invoice = LeadInvoiceManager.Get(invoiceID); // show print button //btnPrint.Visible = true; } invoice.PolicyID = policy.PolicyType; invoice.InvoiceDate = Convert.ToDateTime(txtInvoiceDate.Text); invoice.DueDate = Convert.ToDateTime(txtDueDate.Text); invoice.BillToName = txtBillTo.Text.Trim(); invoice.BillToAddress1 = txtBillToAddress1.Text.Trim(); invoice.BillToAddress2 = txtBillToAddress2.Text.Trim(); invoice.BillToAddress3 = txtBillToAddress3.Text.Trim(); invoice.AdjusterID = policy.AdjusterID; invoice.AdjusterInvoiceNumber = txtReferenceNumber.Text.Trim(); try { using (TransactionScope scope = new TransactionScope()) { if (invoiceID == 0) { // assign next invoice number to new invoice nextInvoiceNumber = LeadInvoiceManager.GetNextInvoiceNumber(clientID); invoice.InvoiceNumber = nextInvoiceNumber; } invoiceID = LeadInvoiceManager.Save(invoice); // save newly generated invoice id ViewState["InvoiceID"] = invoiceID.ToString(); // check for add/edit invoice detail line InvoiceLineID = ViewState["InvoiceLineID"] == null ? 0 : Convert.ToInt32(ViewState["InvoiceLineID"]); if (InvoiceLineID > 0) { invoiceDetail = LeadInvoiceDetailManager.Get(InvoiceLineID); } else { invoiceDetail = new LeadInvoiceDetail(); } // get detail line from gridview footer if (invoiceDetail != null) { invoiceDetailLine = getInvoiceDetailLine(); if (invoiceDetailLine.LineDate != null && !string.IsNullOrEmpty(invoiceDetailLine.LineDescription) && invoiceDetailLine.Qty > 0 && invoiceDetailLine.Rate > 0) { // update fields invoiceDetail.InvoiceID = invoiceID; invoiceDetail.InvoiceLineID = InvoiceLineID; invoiceDetail.ServiceTypeID = invoiceDetailLine.ServiceTypeID; invoiceDetail.Comments = invoiceDetailLine.Comments; invoiceDetail.isBillable = invoiceDetailLine.isBillable; invoiceDetail.LineAmount = invoiceDetailLine.LineAmount; invoiceDetail.LineDate = invoiceDetailLine.LineDate; invoiceDetail.LineDescription = invoiceDetailLine.LineDescription; invoiceDetail.Qty = invoiceDetailLine.Qty; invoiceDetail.Rate = invoiceDetailLine.Rate; invoiceDetail.UnitDescription = invoiceDetailLine.UnitDescription; invoiceDetail.Total = invoiceDetailLine.Total; // save invoice detail LeadInvoiceDetailManager.Save(invoiceDetail); // clear ViewState["InvoiceLineID"] = "0"; // update invoice total after adding invoice = LeadInvoiceManager.Get(invoiceID); invoice.TotalAmount = invoice.LeadInvoiceDetail.Where(x => x.isBillable == true).Sum(x => x.LineAmount); taxAmount = (invoice.TotalAmount ?? 0) * ((invoice.TaxRate ?? 0) / 100); //invoice.TotalAmount = invoice.TotalAmount + taxAmount; LeadInvoiceManager.Save(invoice); // update comment updateInvoiceComment(invoice, invoiceDetail); } } // complete transaction scope.Complete(); } // refresh invoice detail lines bindInvoiceDetails(invoiceID); clearFields(); showToolbarButtons(); } catch (Exception ex) { lblMessage.Text = "Error while saving invoice."; lblMessage.CssClass = "error"; Core.EmailHelper.emailError(ex); } }