using System; using System.ComponentModel.Design; using System.Linq; using System.Windows.Forms; using Common; //20160509 - v1.2 - added stain %, price increase % -maanie //20161031 - v1.3 - added 2 additional email addresses - maanie //20181115 - added DoublePrintBagLabel - maanie //20230831 - added active field namespace PersonalLaundry { public partial class CustomerForm : Form { public event EventHandler CustomerSaved; OperatorLogin operatorLogin; ConsoleForm console; int customerId; public CustomerForm(OperatorLogin operatorLogin, ConsoleForm console, int customerId) { this.operatorLogin = operatorLogin; this.console = console; this.customerId = customerId; InitializeComponent(); } public CustomerForm(OperatorLogin operatorLogin, ConsoleForm console) { this.operatorLogin = operatorLogin; this.console = console; InitializeComponent(); } private void CloseButton_Click(object sender, EventArgs e) { Close(); } private void CustomerForm_Load(object sender, EventArgs e) { LoadBillingProfiles(); LoadCompanies(); if (customerId < 0) return; if (customerId == 0) { StainExpressPercentage.Text = "50"; PriceIncreasePercentage.Text = "0"; } else LoadCustomer(customerId); } private void LoadBillingProfiles() { using (var db = new DataModel()) { BillingProfileComboBox.DataSource = db.BillingProfiles.Where(t => t.BillingProfileId > 0).ToList(); BillingProfileComboBox.DisplayMember = "ProfileName"; BillingProfileComboBox.ValueMember = "BillingProfileId"; } } private void LoadCustomer(int i) { using (var db = new DataModel()) { var customer = db.Customers.FirstOrDefault(c => c.CustomerId == i); if (customer == null) return; CustomerName.Text = customer.Name; cmbCompany.SelectedValue = customer.CompanyID; ContactName.Text = customer.ContactName; EmailAddress.Text = customer.EmailAddress; //20161031 - v1.3 - added 2 additional email addresses - maanie EmailAddress2.Text = customer.EmailAddress2; EmailAddress3.Text = customer.EmailAddress3; AddressLine1.Text = customer.AddressLine1; AddressLine2.Text = customer.AddressLine2; City.Text = customer.AddressCity; PostCode.Text = customer.AddressPostCode; VATNumber.Text = customer.VATNumber; PastelReference.Text = customer.PastelCode; BillingProfileComboBox.SelectedIndex = BillingProfileComboBox.FindString(customer.BillingProfile.ProfileName, 0); CreatedBy.Text = customer.OperatorLogin.Username; //20160509 - v1.2 - added stain %, price increase % -maanie StainExpressPercentage.Text = customer.StainExpressPercentage.ToString(); PriceIncreasePercentage.Text = customer.PriceIncreasePercentage.ToString(); BagLabelComboBox.SelectedIndex = Convert.ToInt32(customer.DoublePrintBagLabel); //20181115 - added DoublePrintBagLabel - maanie ActiveCheckBox.Checked = customer.IsActive; //20230831 - added active field } } private void LoadCompanies() { using (var db = new DataModel()) { cmbCompany.DataSource = db.Companies.ToList(); cmbCompany.DisplayMember = "CompanyName"; cmbCompany.ValueMember = "CompanyID"; } } private void Save() { using (var db = new DataModel()) { Customer customer; if (customerId == 0) { //20160509 - v1.2 - added stain %, price increase % - maanie //20161031 - v1.3 - added 2 additional email addresses - maanie customer = new Customer { Name = CustomerName.Text.Trim(), CompanyID = int.Parse(cmbCompany.SelectedValue.ToString()), ContactName = ContactName.Text.Trim(), AddressLine1 = AddressLine1.Text.Trim(), AddressLine2 = AddressLine2.Text.Trim(), AddressCity = City.Text.Trim(), AddressPostCode = PostCode.Text.Trim(), BillingProfileId = int.Parse(BillingProfileComboBox.SelectedValue.ToString()), OperatorLoginId = operatorLogin.OperatorLoginId, EmailAddress = EmailAddress.Text.Trim(), EmailAddress2 = EmailAddress2.Text.Trim(), EmailAddress3 = EmailAddress3.Text.Trim(), VATNumber = VATNumber.Text.Trim(), StainExpressPercentage = (StainExpressPercentage.Text.Trim() == "" ? 0 : double.Parse(StainExpressPercentage.Text.Trim())), PriceIncreasePercentage = (PriceIncreasePercentage.Text.Trim() == "" ? 0 : double.Parse(PriceIncreasePercentage.Text.Trim())), DoublePrintBagLabel = Convert.ToBoolean(BagLabelComboBox.SelectedIndex), //20181115 - added DoublePrintBagLabel - maanie IsActive = ActiveCheckBox.Checked //20230831 - added active field }; db.Customers.Add(customer); db.SaveChanges(); if (CustomerSaved != null) CustomerSaved(this, new EventArgs()); customer = db.Customers.FirstOrDefault(c => c.Name == CustomerName.Text.Trim()); if (customer == null) return; customerId = customer.CustomerId; MessageBox.Show(@"Saved!", @"Customer", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadCustomer(customerId); console.PopulateMenu(); return; } customer = db.Customers.FirstOrDefault(c => c.CustomerId == customerId); if (customer == null) return; customer.Name = CustomerName.Text.Trim(); customer.CompanyID = int.Parse(cmbCompany.SelectedValue.ToString()); customer.ContactName = ContactName.Text.Trim(); customer.AddressLine1 = AddressLine1.Text.Trim(); customer.AddressLine2 = AddressLine2.Text.Trim(); customer.AddressCity = City.Text.Trim(); customer.AddressPostCode = PostCode.Text.Trim(); customer.BillingProfileId = int.Parse(BillingProfileComboBox.SelectedValue.ToString()); customer.VATNumber = VATNumber.Text.Trim(); customer.EmailAddress = EmailAddress.Text.Trim(); //20161031 - v1.3 - added 2 additional email addresses - maanie customer.EmailAddress2 = EmailAddress2.Text.Trim(); customer.EmailAddress3 = EmailAddress3.Text.Trim(); customer.PastelCode = PastelReference.Text.Trim(); //20160509 - v1.2 - added stain %, price increase % -maanie customer.StainExpressPercentage = (StainExpressPercentage.Text.Trim() == "" ? 0 : double.Parse(StainExpressPercentage.Text.Trim())); customer.PriceIncreasePercentage = (PriceIncreasePercentage.Text.Trim() == "" ? 0 : double.Parse(PriceIncreasePercentage.Text.Trim())); customer.DoublePrintBagLabel = Convert.ToBoolean(BagLabelComboBox.SelectedIndex); //20181115 - added DoublePrintBagLabel - maanie customer.IsActive = ActiveCheckBox.Checked; //20230831 - added active field db.SaveChanges(); if (CustomerSaved != null) CustomerSaved(this, new EventArgs()); MessageBox.Show(@"Saved!", @"Customer", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadCustomer(customerId); console.PopulateMenu(); } } private void CustomerForm_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show(@"Would you like to save changes?", @"Save Customer", MessageBoxButtons.YesNo) != DialogResult.Yes) return; Save(); } } }