Ejemplo n.º 1
0
        // process gridview commands
        protected void gvInvoiceLines_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int invoiceID     = 0;
            int invoiceLineID = 0;

            CRM.Data.Entities.LeadInvoiceDetail invoiceLine = null;
            TextBox txtLineAmount;

            switch (e.CommandName)
            {
            case "DoEdit":
                invoiceLineID = Convert.ToInt32(e.CommandArgument);

                invoiceLine = LeadInvoiceDetailManager.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 && invoiceLine.InvoiceServiceType != null && invoiceLine.InvoiceServiceType.ServiceRate != null)
                    {
                        txtRate.Text = invoiceLine.InvoiceServiceType.ServiceRate.ToString();
                    }

                    // 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);
                if (invoiceLineID > 0)
                {
                    using (TransactionScope scope = new TransactionScope()) {
                        try {
                            LeadInvoiceDetailManager.Delete(invoiceLineID);

                            deleteInvoiceComment(invoiceLineID);

                            invoiceID = Convert.ToInt32(ViewState["InvoiceID"].ToString());

                            bindInvoiceDetails(invoiceID);

                            // complete transaction
                            scope.Complete();
                        }
                        catch (Exception ex) {
                            lblMessage.Text     = "Error while deleting invoice detail line.";
                            lblMessage.CssClass = "error";
                            Core.EmailHelper.emailError(ex);
                        }
                    }
                }

                break;
            }
        }