using framework_business; using framework_library; using System; using System.Collections; using System.Reflection; using System.Web.UI; using System.Web.UI.WebControls; public partial class controls_admin_customers : System.Web.UI.UserControl { #region methods /// /// Method to Bind Customer Data /// private void BindCustomerData() { try { ArrayList customerList = xData.GetTypedByCriteriaSpecific("recId", typeof(oCustomer), "isActive", "1", "customer", "pal_", true); rptCustomers.DataSource = customerList; rptCustomers.DataBind(); } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, Session["customer"]); Response.Redirect("/error", false); } } /// /// Populate Form Values /// /// private void PopulatePageFormValues(ref oCustomer _Customer) { try { txtCode.Value = _Customer.code; txtCustomer.Value = _Customer.customer; txtPrimaryPerson.Value = _Customer.primaryPerson; txtEmail.Value = _Customer.email; txtTel.Value = _Customer.tel; chkActive.Checked = _Customer.isActive; txtLicensedDate.Value = _Customer.licenceDate.ToString("yyyy-MM-dd"); txtVersion.Value = _Customer.versionNo; txtversionDate.Value = _Customer.versionDate.ToString("yyyy-MM-dd"); chkIsPublished.Checked = _Customer.isPublished; } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, 0); Response.Redirect("/error", false); } } /// /// Save Form Values /// /// private void SavePageFormValues(ref oCustomer _Customer) { try { _Customer.customer = txtCustomer.Value; _Customer.primaryPerson = txtPrimaryPerson.Value; _Customer.email = txtEmail.Value; _Customer.tel = txtTel.Value; _Customer.isActive = chkActive.Checked; _Customer.dateUpdated = DateTime.Now; _Customer.isPublished = chkIsPublished.Checked; } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, 0); Response.Redirect("/error", false); } } /// /// Method to Toggle Panels /// /// private void TogglePanels(string panelName) { switch (panelName) { case "pnlCustomerPage": pnlCustomerPage.Visible = true; pnlCustomerList.Visible = false; break; case "pnlCustomerList": pnlCustomerPage.Visible = false; pnlCustomerList.Visible = true; break; } //clear result pnlResult.Visible = false; } #endregion #region events /// /// Page Load Event /// /// /// protected void Page_Load(object sender, EventArgs e) { try { if (!Page.IsPostBack) { BindCustomerData(); if (utils.verifySession("customer")) { oCustomer customer = (oCustomer)Session["customer"]; } } } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, Session["customer"]); Response.Redirect("/error", false); } } /// /// New Customer Click /// /// /// protected void btnNewCustomer_Click(object sender, EventArgs e) { try { //show Content Specific TogglePanels("pnlCustomerPage"); Session["CustomerId"] = 0; utils.disposeSession("Customer"); oCustomer Customer = new oCustomer(); PopulatePageFormValues(ref Customer); } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, Session["customer"]); Response.Redirect("/error", false); } } /// /// Edit the Customer page /// /// /// protected void lnkEdit_Click(object sender, EventArgs e) { oCustomer _Customer = new oCustomer(); ArrayList catList = new ArrayList(); try { if (sender.GetType() == typeof(LinkButton)) { // int recId = int.Parse(((LinkButton)sender).CommandArgument); if (recId > 0) { Session["CustomerId"] = recId; //get cat catList = xData.GetTypedByCriteriaSpecific("recId", typeof(oCustomer), "recId", recId.ToString(), "", "pal_", true); //enumerate list foreach (oCustomer cat in catList) { //assign object _Customer = cat; //populate page form PopulatePageFormValues(ref _Customer); Session["Customer"] = _Customer; break; } //show Customer Form TogglePanels("pnlCustomerPage"); } } } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, Session["customer"]); Response.Redirect("/error", false); } } /// /// Remove the Customer /// /// /// protected void lnkRemove_Click(object sender, EventArgs e) { try { if (sender.GetType() == typeof(LinkButton)) { // int recId = int.Parse(((LinkButton)sender).CommandArgument); //Delete cat if (xData.DeleteTyped("recId", recId.ToString(), typeof(oCustomer), "pal_", true)) { //rebind pages grid BindCustomerData(); pnlResult.Visible = true; //display successful result lblResult.Text = "the selected Customer was deleted successfully."; } } } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, Session["customer"]); Response.Redirect("/error", false); } } /// /// Cancel /// /// /// protected void btnCancel_Click(object sender, EventArgs e) { try { Session["CustomerId"] = 0; utils.disposeSession("Customer"); BindCustomerData(); //show list TogglePanels("pnlCustomerList"); } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, Session["customer"]); Response.Redirect("/error", false); } } /// /// Save Click /// /// /// protected void btnSave_Click(object sender, EventArgs e) { oCustomer Customer = new oCustomer(); try { if (utils.verifySession("Customer"))//update mode { Customer = (oCustomer)Session["Customer"]; //save form values SavePageFormValues(ref Customer); if (xData.UpdateTyped("recId", Customer.recId.ToString(), typeof(oCustomer), Customer, "pal_", true)) { Session["CustomerId"] = Customer.recId; Session["Customer"] = Customer; BindCustomerData(); pnlResult.Visible = true; lblResult.Text = "your changes were updated successfully."; } } else { //save form values SavePageFormValues(ref Customer); string prefix = String.Empty; //create customer code if (Customer.customer.Length > 3) { if (Customer.customer.Substring(0, 4).Contains(" ")) { if (Customer.customer.Length > 7) { prefix = Customer.customer.Substring(Customer.customer.IndexOf(" ") + 1, 4); } else prefix = Customer.customer.Substring(0, 4); } else { prefix = Customer.customer.Substring(0, 4); } } else prefix = Customer.customer; oCustomerCode custCode = new oCustomerCode(); foreach (oCustomerCode cCode in xData.GetTypedByCriteriaSpecific("recId", typeof(oCustomerCode), "prefix", prefix, "prefix", "pal_", true)) { custCode = cCode; break; } custCode.prefix = prefix.ToUpper(); custCode.lastNumberUsed += 1; Customer.code = custCode.prefix + "-" + custCode.lastNumberUsed; if (!xData.VerifyExists("recId", typeof(oCustomer), "email,code", Customer.email + "," + Customer.code, "", "pal_", true)) { if (custCode.recId > 0) { xData.UpdateTyped("recId", custCode.recId.ToString(), typeof(oCustomerCode), custCode, "pal_", true); } else { custCode.recId = xData.SaveTyped("recId", typeof(oCustomerCode), custCode, "pal_", true); } //create license oLicence lic = new oLicence(); lic.customerName = Customer.customer; lic.customerCode = Customer.code; lic.licenceDate = DateTime.Now; lic.licenceKey = licenceHandler.CreateLicence(lic).licenceKey; Customer.licenceDate = lic.licenceDate; Customer.licenceKey = lic.licenceKey; //default verson no Customer.versionNo = "1.0.0.0"; Customer.versionDate = DateTime.Now; Customer.isPublished = false; //setup initial values for customer Customer.dateCreated = DateTime.Now; Customer.recId = xData.SaveTyped("recId", typeof(oCustomer), Customer, "pal_", true); if (Customer.recId > 0) { Session["CustomerId"] = Customer.recId; Session["Customer"] = Customer; BindCustomerData(); PopulatePageFormValues(ref Customer); pnlResult.Visible = true; lblResult.Text = "the Customer was created successfully."; } } else { pnlResult.Visible = true; lblResult.Text = "An customer with this email and code already exists on the system"; } } } catch (Exception ex) { exception.HandleException("Customers:", MethodBase.GetCurrentMethod().Name, ex, Session["customer"]); Response.Redirect("/error", false); } } #endregion }