private void saveInvoice() { string[] billToValues = null; int clientID = 0; int invoiceID = 0; int InvoiceLineID = 0; Invoice invoice = null; CRM.Data.Entities.LeadPolicy policy = null; InvoiceDetail invoiceDetailLine = null; InvoiceDetail 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 Invoice(); // get id for current lead invoice.ClaimID = Core.SessionHelper.getClaimID(); invoice.IsVoid = false; // assign client //invoice.ClientID = clientID; // hide print button //btnPrint.Visible = false; } else { invoice = InvoiceManager.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 = InvoiceManager.GetNextInvoiceNumber(clientID); invoice.InvoiceNumber = nextInvoiceNumber; } invoiceID = InvoiceManager.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 = InvoiceDetailManager.Get(InvoiceLineID); } else { invoiceDetail = new InvoiceDetail(); } // 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 InvoiceDetailManager.Save(invoiceDetail); // clear ViewState["InvoiceLineID"] = "0"; } } // update invoice total after adding new detail line invoice = InvoiceManager.Get(invoiceID); invoice.TotalAmount = invoice.InvoiceDetail.Where(x => x.isBillable == true).Sum(x => x.LineAmount); taxAmount = (invoice.TotalAmount ?? 0) * (invoice.TaxRate / 100); InvoiceManager.Save(invoice); // update claim diarycomment updateClaimDiary(invoice, invoiceDetail); // complete transaction scope.Complete(); } // refresh invoice detail lines bindInvoiceDetails(invoiceID); // refresh invoice number on UI txtInvoiceNumber.Text = (invoice.InvoiceNumber ?? 0).ToString(); clearFields(); showToolbarButtons(); refreshParentWindow(); } catch (Exception ex) { lblMessage.Text = "Error while saving invoice."; lblMessage.CssClass = "error"; Core.EmailHelper.emailError(ex); } }
private void saveInvoice() { int clientID = 0; int invoiceID = 0; int InvoiceLineID = 0; Invoice invoice = null; InvoiceDetail invoiceDetailLine = null; InvoiceDetail invoiceDetail = null; int nextInvoiceNumber = 0; decimal taxAmount = 0; clientID = Core.SessionHelper.getClientId(); invoiceID = ViewState["InvoiceID"] == null ? 0 : Convert.ToInt32(ViewState["InvoiceID"].ToString()); if (invoiceID == 0) { invoice = new Invoice(); // get id for current lead invoice.ClaimID = Core.SessionHelper.getClaimID(); invoice.IsVoid = false; } else { invoice = InvoiceManager.Get(invoiceID); } //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(); try { using (TransactionScope scope = new TransactionScope()) { if (invoiceID == 0) { // assign next invoice number to new invoice nextInvoiceNumber = InvoiceManager.GetNextInvoiceNumber(clientID); invoice.InvoiceNumber = nextInvoiceNumber; } invoiceID = InvoiceManager.Save(invoice); // save newly generated invoice id ViewState["InvoiceID"] = invoiceID.ToString(); #region get detail line from gridview foreach (GridViewRow row in gvTimeExpense.Rows) { // get detail line from grid invoiceDetailLine = getInvoiceDetailLine(row); if (invoiceDetailLine != null) { if (invoiceDetailLine.InvoiceLineID > 0) { invoiceDetail = InvoiceDetailManager.Get(InvoiceLineID); } else { invoiceDetail = new InvoiceDetail(); } // update fields invoiceDetail.InvoiceID = invoiceID; invoiceDetail.ServiceTypeID = invoiceDetailLine.ServiceTypeID; invoiceDetail.ExpenseTypeID = invoiceDetailLine.ExpenseTypeID; 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 InvoiceDetailManager.Save(invoiceDetail); } } // foreach #endregion // update invoice total after adding invoice = InvoiceManager.Get(invoiceID); invoice.TotalAmount = invoice.InvoiceDetail.Where(x => x.isBillable == true).Sum(x => x.LineAmount); taxAmount = (invoice.TotalAmount ?? 0) * (invoice.TaxRate / 100); InvoiceManager.Save(invoice); // add invoice to claim diary comment addInvoiceToClaimDiary(invoice, invoiceDetail); // 2014-05-02 apply rule using (SpecificExpenseTypePerCarrier ruleEngine = new SpecificExpenseTypePerCarrier()) { RuleException ruleException = ruleEngine.TestRule(clientID, invoice); if (ruleException != null) { ruleException.UserID = Core.SessionHelper.getUserId(); ruleEngine.AddException(ruleException); CheckSendMail(ruleException); } } // complete transaction scope.Complete(); } // using // refresh invoice number on UI txtInvoiceNumber.Text = (invoice.InvoiceNumber ?? 0).ToString(); showToolbarButtons(); lblMessage.Text = "Invoice was generated successfully."; lblMessage.CssClass = "ok"; } catch (Exception ex) { lblMessage.Text = "Error while saving invoice."; lblMessage.CssClass = "error"; Core.EmailHelper.emailError(ex); } }
// process gridview commands for detail lines protected void gvInvoiceLines_RowCommand(object sender, GridViewCommandEventArgs e) { int invoiceID = 0; int invoiceLineID = 0; Invoice invoice = null; InvoiceDetail invoiceLine = null; TextBox txtLineAmount; switch (e.CommandName) { case "DoSave": Page.Validate("invoice"); if (!Page.IsValid) { return; } Page.Validate("invoiceLine"); if (!Page.IsValid) { return; } saveInvoice(); break; case "DoEdit": invoiceLineID = Convert.ToInt32(e.CommandArgument); invoiceLine = InvoiceDetailManager.Get(invoiceLineID); if (invoiceLine != null) { ViewState["InvoiceLineID"] = invoiceLineID.ToString(); // date WebDatePicker txtDate = gvInvoiceLines.FooterRow.FindControl("txtDate") as WebDatePicker; if (txtDate != null && invoiceLine.LineDate != null) { txtDate.Text = string.Format("{0:MM/dd/yyyy}", invoiceLine.LineDate); } // service description DropDownList cbx = gvInvoiceLines.FooterRow.FindControl("cbxServiceDescription") as DropDownList; if (cbx != null && invoiceLine.ServiceTypeID != null) { ListItem item = cbx.Items.FindByText(invoiceLine.LineDescription.Trim()); if (item != null) { cbx.SelectedIndex = cbx.Items.IndexOf(item); } } else { ListItem item = new ListItem(invoiceLine.LineDescription, invoiceLine.LineDescription); cbx.Items.Add(item); cbx.Text = invoiceLine.LineDescription; } // quantity WebNumericEditor txtQty = gvInvoiceLines.FooterRow.FindControl("txtQty") as WebNumericEditor; if (txtQty != null && invoiceLine.Qty != null) { txtQty.Text = invoiceLine.Qty.ToString(); } // rate WebNumericEditor txtRate = gvInvoiceLines.FooterRow.FindControl("txtRate") as WebNumericEditor; if (txtRate != null) { txtRate.Value = invoiceLine.Rate; } //// service unit //Label lblUnitDescription = gvInvoiceLines.FooterRow.FindControl("lblUnitDescription") as Label; //if (lblUnitDescription != null && invoiceLine.InvoiceServiceType != null && invoiceLine.InvoiceServiceType.InvoiceServiceUnit != null) // lblUnitDescription.Text = invoiceLine.InvoiceServiceType.InvoiceServiceUnit.UnitDescription; // total amount txtLineAmount = gvInvoiceLines.FooterRow.FindControl("txtLineAmount") as TextBox; if (txtLineAmount != null) { txtLineAmount.Text = string.Format("{0:N2}", invoiceLine.LineAmount ?? 0); } //billable CheckBox cbxBillable = gvInvoiceLines.FooterRow.FindControl("cbxBillable") as CheckBox; if (cbxBillable != null && invoiceLine.isBillable != null) { cbxBillable.Checked = invoiceLine.isBillable ?? false; } // comments WebTextEditor txtComments = gvInvoiceLines.FooterRow.FindControl("txtComments") as WebTextEditor; if (txtComments != null && invoiceLine.Comments != null) { txtComments.Text = invoiceLine.Comments; } // show cancel icon ImageButton ibtnCancel = gvInvoiceLines.FooterRow.FindControl("ibtnCancel") as ImageButton; ibtnCancel.Visible = true; } break; case "DoDelete": invoiceLineID = Convert.ToInt32(e.CommandArgument); invoiceID = ViewState["InvoiceID"] != null?Convert.ToInt32(ViewState["InvoiceID"]) : 0; if (invoiceLineID > 0 && invoiceID > 0) { try { using (TransactionScope scope = new TransactionScope()) { deleteInvoiceComment(invoiceLineID); InvoiceDetailManager.Delete(invoiceLineID); // update invoice total after deleting detail line invoice = InvoiceManager.Get(invoiceID); if (invoice != null && invoice.InvoiceDetail != null) { invoice.TotalAmount = invoice.InvoiceDetail.Where(x => x.isBillable == true).Sum(x => x.LineAmount); InvoiceManager.Save(invoice); } // complete transaction scope.Complete(); refreshParentWindow(); } bindInvoiceDetails(invoiceID); } catch (Exception ex) { lblMessage.Text = "Error while deleting invoice detail line."; lblMessage.CssClass = "error"; Core.EmailHelper.emailError(ex); } } break; } }