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); } }
// generate invoices protected void btnGenerate_Click(object sender, EventArgs e) { Carrier carrier = null; CarrierInvoiceProfile profile = null; Invoice invoice = null; int carrierID = Convert.ToInt32(ddlCarrier.SelectedValue); decimal invoiceAmount = 0; int policyID = 0; int profileID = Convert.ToInt32(this.ddlInvoiceProfile.SelectedValue); if (carrierID == 0 || profileID == 0) { return; } try { profile = CarrierInvoiceProfileManager.Get(profileID); carrier = CarrierManager.Get(carrierID); } catch (Exception ex) { Core.EmailHelper.emailError(ex); showErrorMessage(); return; } try { using (TransactionScope scope = new TransactionScope()) { foreach (GridViewRow row in gvCarrierPolicy.Rows) { if (row.RowType == DataControlRowType.DataRow) { CheckBox cbxSelect = row.FindControl("cbxSelect") as CheckBox; if (cbxSelect.Checked) { invoice = new Invoice(); //invoice.ClientID = clientID; //invoice.CarrierID = carrierID; invoice.InvoiceDate = DateTime.Now; invoice.InvoiceTypeID = profile.InvoiceType; invoice.CarrierInvoiceProfileID = profileID; invoice.IsVoid = false; invoice.BillToAddress1 = carrier.AddressLine1; invoice.BillToAddress2 = carrier.AddressLine2; invoice.BillToAddress3 = string.Empty; invoice.BillToName = carrier.CarrierName; policyID = (int)gvCarrierPolicy.DataKeys[row.RowIndex].Values[0]; //invoice.PolicyID = policyID; //invoice.LeadId = (int)gvCarrierPolicy.DataKeys[row.RowIndex].Values[1]; //invoice.PolicyTypeID = (int)gvCarrierPolicy.DataKeys[row.RowIndex].Values[2]; Label lblInvoiceAmount = row.FindControl("lblInvoiceAmount") as Label; decimal.TryParse(lblInvoiceAmount.Text, out invoiceAmount); invoice.TotalAmount = invoiceAmount; invoice.InvoiceNumber = InvoiceManager.GetNextInvoiceNumber(clientID); int invoiceID = InvoiceManager.Save(invoice); InvoiceDetail invoiceDetail = new InvoiceDetail(); invoiceDetail.InvoiceID = invoiceID; invoiceDetail.isBillable = true; invoiceDetail.Qty = 1; invoiceDetail.Rate = invoiceAmount; invoiceDetail.LineAmount = invoiceAmount; invoiceDetail.LineDate = DateTime.Now; invoiceDetail.LineDescription = "As per contract"; InvoiceDetailManager.Save(invoiceDetail); // flag policy as invoiced CRM.Data.Entities.LeadPolicy policy = LeadPolicyManager.Get(policyID); if (policy != null) { policy.IsInvoiced = true; LeadPolicyManager.Save(policy); } } } } // foreach // complete transaction scope.Complete(); lblMessage.Text = "Invoice(s) have been generated."; lblMessage.CssClass = "ok"; } } catch (Exception ex) { Core.EmailHelper.emailError(ex); showErrorMessage(); } finally { // refresh those policies to be invoiced bindPoliciesReadyForInvoice(); } }
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); } }