| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Data;
|
|---|
| 4 | using System.Diagnostics;
|
|---|
| 5 | using System.Text;
|
|---|
| 6 | using System.Windows.Forms;
|
|---|
| 7 | using Baza_Reklam.Classes.Helpers;
|
|---|
| 8 | using Baza_Reklam.Classes.Model;
|
|---|
| 9 | using Baza_Reklam.Classes.Repositories;
|
|---|
| 10 |
|
|---|
| 11 | namespace Baza_Reklam
|
|---|
| 12 | {
|
|---|
| 13 | public partial class AddClient : Form
|
|---|
| 14 | {
|
|---|
| 15 | private readonly string _connectionString = ConnString.getConnString().Value;
|
|---|
| 16 |
|
|---|
| 17 | private readonly int _clientId;
|
|---|
| 18 | private REKLAMADataSet.KLIENCIRow _client;
|
|---|
| 19 | private bool _reload;
|
|---|
| 20 |
|
|---|
| 21 | public REKLAMADataSet.KLIENCIRow Client
|
|---|
| 22 | {
|
|---|
| 23 | get { return _client; }
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | public bool Reload
|
|---|
| 27 | {
|
|---|
| 28 | get { return _reload; }
|
|---|
| 29 | set { _reload = value; }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | public AddClient()
|
|---|
| 33 | {
|
|---|
| 34 | InitializeComponent();
|
|---|
| 35 | Init();
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public AddClient(int clientId, BindingSource bindingSource)
|
|---|
| 39 | {
|
|---|
| 40 | InitializeComponent();
|
|---|
| 41 | rEKLAMADataSet = (REKLAMADataSet)bindingSource.DataSource;
|
|---|
| 42 | clientsBindingSource = bindingSource;
|
|---|
| 43 | _clientId = clientId;
|
|---|
| 44 |
|
|---|
| 45 | // rebind controls to use passed binding source
|
|---|
| 46 | ReBindControls(this, clientsBindingSource);
|
|---|
| 47 |
|
|---|
| 48 | Init();
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | private void Init()
|
|---|
| 52 | {
|
|---|
| 53 | LoadTitleList();
|
|---|
| 54 | clientsAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
|
|---|
| 55 | clientTitlesAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
|
|---|
| 56 |
|
|---|
| 57 | ComboBoxHelper.Bind(CountryRepository.FindAll(), cbCountry);
|
|---|
| 58 | cbCountry.DisplayMember = "Name";
|
|---|
| 59 | cbCountry.ValueMember = "Name";
|
|---|
| 60 |
|
|---|
| 61 | ComboBoxHelper.Bind(StateRepository.FindAll(), cbState);
|
|---|
| 62 | cbState.DisplayMember = "Name";
|
|---|
| 63 | cbState.ValueMember = "Name";
|
|---|
| 64 |
|
|---|
| 65 | ComboBoxHelper.Bind(new AgentRepository(_connectionString).FindAllActive(), cbUserName);
|
|---|
| 66 | cbUserName.DisplayMember = "LoginName";
|
|---|
| 67 | cbUserName.ValueMember = "LoginName";
|
|---|
| 68 |
|
|---|
| 69 | ComboBoxHelper.Bind(new AgentRepository(_connectionString).FindAllActive(), cbAgentExpo);
|
|---|
| 70 | cbAgentExpo.DisplayMember = "LoginName";
|
|---|
| 71 | cbAgentExpo.ValueMember = "LoginName";
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | private void AddClient_Load(object sender, EventArgs e)
|
|---|
| 75 | {
|
|---|
| 76 | Reload = false;
|
|---|
| 77 | bool update = false;
|
|---|
| 78 | if (_clientId == 0) // add new client
|
|---|
| 79 | {
|
|---|
| 80 | _client = rEKLAMADataSet.KLIENCI.NewKLIENCIRow();
|
|---|
| 81 | _client.Last_Modify = DateTime.Now;
|
|---|
| 82 | _client.data = DateTime.Now;
|
|---|
| 83 | _client.UserName = User.Instance().Login;
|
|---|
| 84 | _client.AgentExpo = User.Instance().Login;
|
|---|
| 85 | rEKLAMADataSet.KLIENCI.AddKLIENCIRow(_client);
|
|---|
| 86 | clientsBindingSource.MoveLast();
|
|---|
| 87 | cbCountry.SelectedIndex = -1;
|
|---|
| 88 | cbState.SelectedIndex = -1;
|
|---|
| 89 | cbActive.Checked = true;
|
|---|
| 90 | }
|
|---|
| 91 | else // update selected client
|
|---|
| 92 | {
|
|---|
| 93 | update = true;
|
|---|
| 94 | _client = (REKLAMADataSet.KLIENCIRow)((DataRowView)clientsBindingSource.Current).Row;
|
|---|
| 95 |
|
|---|
| 96 | InitClientTitles();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | SetStateUserNameAndAgentExpo(_client, update);
|
|---|
| 100 |
|
|---|
| 101 | SetFormCaption(update);
|
|---|
| 102 |
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | private void InitClientTitles()
|
|---|
| 106 | {
|
|---|
| 107 | clientTitlesAdapter.FillByCustomerId(rEKLAMADataSet.KLIENCI_TYTUL, _client.CustomerID);
|
|---|
| 108 |
|
|---|
| 109 | lbClientTitles.DataSource = rEKLAMADataSet.KLIENCI_TYTUL;
|
|---|
| 110 | lbClientTitles.DisplayMember = rEKLAMADataSet.KLIENCI_TYTUL.TYTULColumn.ColumnName;
|
|---|
| 111 | lbClientTitles.ValueMember = rEKLAMADataSet.KLIENCI_TYTUL.ID_KLIENCI_TYTULColumn.ColumnName;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | private void SetFormCaption(bool update)
|
|---|
| 115 | {
|
|---|
| 116 | Text = update ? "Edytuj dane klienta" : "Dodaj klienta";
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | private void SetStateUserNameAndAgentExpo(REKLAMADataSet.KLIENCIRow client, bool update)
|
|---|
| 120 | {
|
|---|
| 121 | if (update)
|
|---|
| 122 | {
|
|---|
| 123 | if (!client.IsStateNull())
|
|---|
| 124 | {
|
|---|
| 125 | int stateIndex = cbState.FindString(client.State);
|
|---|
| 126 | if (stateIndex < 0)
|
|---|
| 127 | {
|
|---|
| 128 | cbState.Items.Insert(0, new State(client.State));
|
|---|
| 129 | cbState.SelectedIndex = 0;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | int userNameIndex = cbUserName.FindString(client.UserName);
|
|---|
| 134 | if (userNameIndex < 0)
|
|---|
| 135 | cbUserName.Items.Insert(0, new Agent(client.UserName.Trim().ToLower()));
|
|---|
| 136 |
|
|---|
| 137 | int agentExpoIndex = cbAgentExpo.FindString(client.AgentExpo);
|
|---|
| 138 | if (agentExpoIndex < 0)
|
|---|
| 139 | cbAgentExpo.Items.Insert(0, new Agent(client.AgentExpo.Trim().ToLower()));
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | cbUserName.SelectedIndex = cbUserName.FindString(client.UserName.Trim().ToLower());
|
|---|
| 143 | cbAgentExpo.SelectedIndex = cbAgentExpo.FindString(client.AgentExpo.Trim().ToLower());
|
|---|
| 144 |
|
|---|
| 145 | cbUserName.Enabled = (DbUtils.idAgencji(client.UserName) == User.Instance().IdAgencji);
|
|---|
| 146 | cbAgentExpo.Enabled = (DbUtils.idAgencji(client.AgentExpo) == User.Instance().IdAgencji);
|
|---|
| 147 |
|
|---|
| 148 | //See ticket #169
|
|---|
| 149 | //if (!User.Instance().St_kierownik)
|
|---|
| 150 | //{
|
|---|
| 151 | // cbUserName.Enabled = client.UserName.Trim().ToLower() == User.Instance().Symbol_agenta.Trim().ToLower();
|
|---|
| 152 | // cbAgentExpo.Enabled = client.AgentExpo.Trim().ToLower() == User.Instance().Symbol_agenta.Trim().ToLower();
|
|---|
| 153 | //}
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | private static void ReBindControls(Control control, BindingSource bindingSource)
|
|---|
| 157 | {
|
|---|
| 158 | foreach (Control ctrl in control.Controls)
|
|---|
| 159 | {
|
|---|
| 160 | if (ctrl.DataBindings.Count == 1)
|
|---|
| 161 | {
|
|---|
| 162 | string property = ctrl.DataBindings[0].PropertyName;
|
|---|
| 163 | string dataMember = ctrl.DataBindings[0].BindingMemberInfo.BindingField;
|
|---|
| 164 |
|
|---|
| 165 | ctrl.DataBindings.Clear();
|
|---|
| 166 | ctrl.DataBindings.Add(property, bindingSource, dataMember, true);
|
|---|
| 167 | Debug.WriteLine(ctrl.Name + ": " + ctrl.DataBindings[0].PropertyName + "; " + ctrl.DataBindings[0].BindingMemberInfo.BindingField);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | ReBindControls(ctrl, bindingSource);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | private void LoadTitleList()
|
|---|
| 175 | {
|
|---|
| 176 | SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter titleListAdapter = new SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter();
|
|---|
| 177 | titleListAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
|
|---|
| 178 | lbTitles.DataSource = titleListAdapter.GetData();
|
|---|
| 179 | lbTitles.DisplayMember = "SYMB";
|
|---|
| 180 | lbTitles.ValueMember = "SYMB";
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | private void btnSave_Click(object sender, EventArgs e)
|
|---|
| 184 | {
|
|---|
| 185 | if (ValidateChildren())
|
|---|
| 186 | {
|
|---|
| 187 | Cursor = Cursors.WaitCursor;
|
|---|
| 188 |
|
|---|
| 189 | Client.Last_Modify = DateTime.Now;
|
|---|
| 190 | Client.Modify_User = User.Instance().Login;
|
|---|
| 191 | clientsBindingSource.EndEdit();
|
|---|
| 192 | clientsAdapter.Update(rEKLAMADataSet.KLIENCI);
|
|---|
| 193 | Cursor = Cursors.Default;
|
|---|
| 194 | Reload = true;
|
|---|
| 195 | Close();
|
|---|
| 196 | return;
|
|---|
| 197 | }
|
|---|
| 198 | Reload = false;
|
|---|
| 199 | ShowValidatorMessage();
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | private void ShowValidatorMessage()
|
|---|
| 203 | {
|
|---|
| 204 | MessageBox.Show("Proszê wprowadziæ wszystkie wymagane dane:\n" + GetErrorSummary(), "Baza Reklam - walidacja danych");
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | private string GetErrorSummary()
|
|---|
| 208 | {
|
|---|
| 209 | StringBuilder stringBuilder = new StringBuilder();
|
|---|
| 210 | if (!string.IsNullOrEmpty(errorProvider.GetError(txtName1)))
|
|---|
| 211 | stringBuilder.AppendLine(" - " + errorProvider.GetError(txtName1));
|
|---|
| 212 |
|
|---|
| 213 | if (!string.IsNullOrEmpty(errorProvider.GetError(txtCity)))
|
|---|
| 214 | stringBuilder.AppendLine(" - " + errorProvider.GetError(txtCity));
|
|---|
| 215 |
|
|---|
| 216 | if (!string.IsNullOrEmpty(errorProvider.GetError(cbCountry)))
|
|---|
| 217 | stringBuilder.AppendLine(" - " + errorProvider.GetError(cbCountry));
|
|---|
| 218 |
|
|---|
| 219 | if (!string.IsNullOrEmpty(errorProvider.GetError(txtClientCode)))
|
|---|
| 220 | stringBuilder.AppendLine(" - " + errorProvider.GetError(txtClientCode));
|
|---|
| 221 |
|
|---|
| 222 | if (!string.IsNullOrEmpty(errorProvider.GetError(txtNameInMagazine)))
|
|---|
| 223 | stringBuilder.AppendLine(" - " + errorProvider.GetError(txtNameInMagazine));
|
|---|
| 224 |
|
|---|
| 225 | return stringBuilder.ToString();
|
|---|
| 226 |
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | private void btnCancel_Click(object sender, EventArgs e)
|
|---|
| 230 | {
|
|---|
| 231 | rEKLAMADataSet.KLIENCI.RejectChanges();
|
|---|
| 232 | Close();
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | private void btnCopyAddress_Click(object sender, EventArgs e)
|
|---|
| 236 | {
|
|---|
| 237 | List<string> address = new List<string>();
|
|---|
| 238 | address.Add(txtName1.Text);
|
|---|
| 239 | address.Add(txtAddress.Text);
|
|---|
| 240 | address.Add(txtPostCode.Text + " " + txtCity.Text);
|
|---|
| 241 | if (!string.IsNullOrEmpty(cbState.Text.Trim()))
|
|---|
| 242 | address.Add(cbState.Text);
|
|---|
| 243 |
|
|---|
| 244 | address.Add(cbCountry.Text);
|
|---|
| 245 |
|
|---|
| 246 | txtInvoiceAddress.Lines = address.ToArray();
|
|---|
| 247 | txtCorespondenceAddress.Lines = address.ToArray();
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | private void btnAddTitle_Click(object sender, EventArgs e)
|
|---|
| 251 | {
|
|---|
| 252 | if (lbTitles.SelectedValue == null)
|
|---|
| 253 | {
|
|---|
| 254 | MessageBox.Show("Proszê wybraæ tytu³ do dodania");
|
|---|
| 255 | return;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | clientTitlesAdapter.Insert(lbTitles.SelectedValue.ToString(), _client.CustomerID, User.Instance().Login, DateTime.Now);
|
|---|
| 259 | clientTitlesAdapter.FillByCustomerId(rEKLAMADataSet.KLIENCI_TYTUL, _client.CustomerID);
|
|---|
| 260 |
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | private void btnRemoveTitle_Click(object sender, EventArgs e)
|
|---|
| 264 | {
|
|---|
| 265 | if (lbClientTitles.SelectedItem == null) return;
|
|---|
| 266 |
|
|---|
| 267 | DataRow row = rEKLAMADataSet.KLIENCI_TYTUL.Select(rEKLAMADataSet.KLIENCI_TYTUL.ID_KLIENCI_TYTULColumn.ColumnName + "=" + lbClientTitles.SelectedValue)[0];
|
|---|
| 268 | row.Delete();
|
|---|
| 269 | clientTitlesAdapter.Update(rEKLAMADataSet.KLIENCI_TYTUL);
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | private void txtName1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
|
|---|
| 273 | {
|
|---|
| 274 | ValidationHelper.IsTextBoxEmpty(txtName1, "Proszê podaæ nazwê klienta.", e, errorProvider);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | private void cbCountry_Validating(object sender, System.ComponentModel.CancelEventArgs e)
|
|---|
| 278 | {
|
|---|
| 279 | ValidationHelper.IsComboValueSelected(cbCountry, "Proszê wybraæ kraj klienta.", e, errorProvider);
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | private void txtCity_Validating(object sender, System.ComponentModel.CancelEventArgs e)
|
|---|
| 283 | {
|
|---|
| 284 | ValidationHelper.IsTextBoxEmpty(txtCity, "Proszê podaæ miasto klienta.", e, errorProvider);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | private void txtNameInMagazine_Validating(object sender, System.ComponentModel.CancelEventArgs e)
|
|---|
| 288 | {
|
|---|
| 289 | ValidationHelper.IsTextBoxEmpty(txtNameInMagazine, "Proszê podaæ nazwê klienta, która uka¿e siê w gazecie.", e, errorProvider);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | private void txtClientCode_Validating(object sender, System.ComponentModel.CancelEventArgs e)
|
|---|
| 293 | {
|
|---|
| 294 | if (string.IsNullOrEmpty(txtClientCode.Text)) return;
|
|---|
| 295 |
|
|---|
| 296 | StringBuilder sb = new StringBuilder();
|
|---|
| 297 |
|
|---|
| 298 | if (!DbUtils.kodKlientaUnikalny(txtClientCode.Text, Client.UserName, Client.CustomerID))
|
|---|
| 299 | {
|
|---|
| 300 | e.Cancel = true;
|
|---|
| 301 | sb.Append("Proszê podaæ inny kod klienta. Podany kod ju¿ istnieje dla wybranego agenta.");
|
|---|
| 302 | }
|
|---|
| 303 | else
|
|---|
| 304 | e.Cancel = false;
|
|---|
| 305 |
|
|---|
| 306 | errorProvider.SetError(txtClientCode, sb.ToString());
|
|---|
| 307 | }
|
|---|
| 308 | }
|
|---|
| 309 | } |
|---|