protected void gv_onSorting(object sender, GridViewSortEventArgs e)
        {
            GridView gridView   = sender as GridView;
            bool     descending = false;

            IQueryable <InvoiceServiceType> services = null;
            int clientID = Core.SessionHelper.getClientId();

            using (InvoiceServiceTypeManager repository = new InvoiceServiceTypeManager()) {
                services = repository.GetAll(clientID);

                gridView.DataSource = services.orderByExtension(e.SortExpression, descending);

                gridView.DataBind();
            }

            if (ViewState[e.SortExpression] == null)
            {
                descending = false;
            }
            else
            {
                descending = !(bool)ViewState[e.SortExpression];
            }

            ViewState[e.SortExpression] = descending;
        }
        protected void loadServices()
        {
            List <InvoiceServiceType> services = null;

            int clientID = Core.SessionHelper.getClientId();

            using (InvoiceServiceTypeManager repository = new InvoiceServiceTypeManager()) {
                services = repository.GetAll(clientID).ToList();
            }

            gvServices.DataSource = services;
            gvServices.DataBind();
        }
        public static string getServiceTypeDetail(int id)
        {
            InvoiceServiceType serviceType = null;
            string             result      = null;

            using (InvoiceServiceTypeManager repository = new InvoiceServiceTypeManager()) {
                serviceType = repository.Get(id);
            }

            if (serviceType != null)
            {
                result = string.Format("{0}|{1}", serviceType.DefaultQty, serviceType.ServiceDescription);
            }

            return(result);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblMessage.Visible = false;
            lblMessage.Text    = string.Empty;

            int clientID = Core.SessionHelper.getClientId();
            int id       = 0;
            int unitID   = 0;

            InvoiceServiceType service = null;

            Page.Validate("service");
            if (!Page.IsValid)
            {
                return;
            }

            try {
                int.TryParse(hfId.Value, out id);

                using (InvoiceServiceTypeManager repository = new InvoiceServiceTypeManager()) {
                    if (id == 0)
                    {
                        service           = new InvoiceServiceType();
                        service.ClientID  = clientID;
                        service.isActive  = true;
                        service.IsTaxable = false;
                    }
                    else
                    {
                        service = repository.Get(id);
                    }

                    service.MinimumFee = txtMinimumFee.Value == null ? 0 : Convert.ToDecimal(txtMinimumFee.Value);
                    service.IsTaxable  = cbxIsTaxable.Checked;
                    service.DefaultQty = txtDefaultQty.Value == null ? 0 : Convert.ToDecimal(txtDefaultQty.Value);

                    service.ServiceRate        = txtRate.Value == null ? 0 : Convert.ToDecimal(txtRate.Value);
                    service.ServicePercentage  = txtPercentage.Value == null ? 0 : Convert.ToDecimal(txtPercentage.Value);
                    service.ServiceDescription = txtServiceDescription.Text.Trim();

                    service.EarningCode = txtEarningCode.Text.Trim();

                    //int.TryParse(ddlUnit.SelectedValue, out unitID);

                    //if (ddlUnit.SelectedIndex > 0)
                    //	service.UnitID = unitID;
                    //else
                    //	service.UnitID = null;


                    repository.Save(service);
                }



                lblMessage.Text     = "Service type saved successfully.";
                lblMessage.CssClass = "ok";

                pnlEdit.Visible = false;
                pnlGrid.Visible = true;

                loadServices();
            }

            catch (Exception ex) {
                lblMessage.Text     = "Unable to save service type information!";
                lblMessage.CssClass = "error";

                Core.EmailHelper.emailError(ex);
            }
        }
        //private void EnableUnitTextBox(string unitType) {
        //	if (unitDescriptions.Contains(unitType.ToLower())) {
        //		lblRatePrompt.Text = "Percentage (%)";
        //	}
        //	else {
        //		lblRatePrompt.Text = "Rate ($)";
        //	}

        //}

        //protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e) {
        //	EnableUnitTextBox(ddlUnit.SelectedItem.Text);
        //}



        protected void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = 0;
            InvoiceServiceType service = null;

            clearFields();

            if (e.CommandName == "DoEdit")
            {
                hfId.Value = e.CommandArgument.ToString();
                id         = Convert.ToInt32(e.CommandArgument.ToString());

                lblMessage.Text = string.Empty;

                using (InvoiceServiceTypeManager repository = new InvoiceServiceTypeManager()) {
                    service = repository.Get(id);


                    if (service != null)
                    {
                        pnlEdit.Visible = true;

                        pnlGrid.Visible = false;

                        //bindEarningCodes();

                        txtServiceDescription.Text = service.ServiceDescription;

                        txtRate.Value       = service.ServiceRate ?? 0;
                        txtPercentage.Value = service.ServicePercentage ?? 0;
                        txtDefaultQty.Value = service.DefaultQty ?? 0;
                        txtMinimumFee.Value = service.MinimumFee ?? 0;
                        txtEarningCode.Text = service.EarningCode;

                        //if (service.UnitID != null) {
                        //	ddlUnit.SelectedValue = service.UnitID.ToString();

                        //	EnableUnitTextBox(service.InvoiceServiceUnit.UnitDescription);
                        //}



                        lblMessage.Text = "";
                    }
                }
            }
            else if (e.CommandName == "DoDelete")
            {
                id = Convert.ToInt32(e.CommandArgument.ToString());

                using (InvoiceServiceTypeManager repository = new InvoiceServiceTypeManager()) {
                    service = repository.Get(id);

                    if (service != null)
                    {
                        service.isActive = false;

                        repository.Save(service);
                    }
                }
            }

            loadServices();
        }