Add project files.
This commit is contained in:
214
StockInfoCore/frmPerson.cs
Normal file
214
StockInfoCore/frmPerson.cs
Normal file
@ -0,0 +1,214 @@
|
||||
using DataDomain;
|
||||
using Helpers;
|
||||
using StockDAL.Interface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace StockInfoCore
|
||||
{
|
||||
public partial class frmPerson : Form
|
||||
{
|
||||
private readonly IPersonRepository _personRepository;
|
||||
private readonly IAddressRepository _addressRepository;
|
||||
private bool radioOk = false;
|
||||
|
||||
public frmPerson(IPersonRepository personRepository, IAddressRepository addressRepository)
|
||||
{
|
||||
InitializeComponent();
|
||||
_personRepository = personRepository;
|
||||
_addressRepository = addressRepository;
|
||||
}
|
||||
|
||||
public int PersonId { get; set; }
|
||||
public int HomeAddressId { get; set; }
|
||||
public int CheckHomeAddressId { get; set; }
|
||||
public int InvoiceAddressId { get; set; }
|
||||
public int CheckInvoiceAddressId { get; set; }
|
||||
|
||||
|
||||
private void btnAddSave_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
(Person person, bool OK) = validatePerson();
|
||||
if (OK)
|
||||
{
|
||||
person = _personRepository.SavePerson(person);
|
||||
initializeAllFields();
|
||||
PersonId = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private (Person person, bool OK) validatePerson()
|
||||
{
|
||||
var person = new Person();
|
||||
person.Id = PersonId;
|
||||
person.FirstName = txtFirstName.Text;
|
||||
person.LastName = txtLastName.Text;
|
||||
person.NickName = txtNickName.Text;
|
||||
person.ClearingNo = txtClearingNo.Text.IsNumeric()?int.Parse(txtClearingNo.Text):0;
|
||||
person.AccountNo = txtAccountNr.Text.IsNumeric() ? int.Parse(txtAccountNr.Text) : 0;
|
||||
person.Born = txtPersonNr.Text;
|
||||
person.Comments = txtComment.Text;
|
||||
if (rdbHome.Checked)
|
||||
{
|
||||
HomeAddressId = AddressSave(HomeAddressId);
|
||||
}
|
||||
else
|
||||
{
|
||||
InvoiceAddressId = AddressSave(InvoiceAddressId);
|
||||
}
|
||||
person.HomeAddress = HomeAddressId;
|
||||
CheckHomeAddressId = HomeAddressId;
|
||||
person.InvoiceAddress = InvoiceAddressId;
|
||||
CheckInvoiceAddressId = InvoiceAddressId;
|
||||
return (person, true);
|
||||
}
|
||||
|
||||
private int AddressSave(int AddressId)
|
||||
{
|
||||
var retval = 0;
|
||||
bool changed = false;
|
||||
Address address = new Address();
|
||||
(address.Id, changed) = checkInt(AddressId.ToString());
|
||||
(address.Street, changed) = checkString(txtStreet.Text);
|
||||
(address.Street2, changed) = checkString(txtStreet2.Text);
|
||||
(address.Zipcode, changed) = checkInt(txtZipCode.Text);
|
||||
(address.Destination, changed) = checkString(txtDestination.Text);
|
||||
(address.Nation, changed) = checkString(txtNation.Text);
|
||||
if (changed)
|
||||
{
|
||||
retval =_addressRepository.SaveAddress(address).Id;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
private (string, bool ) checkString(string text)
|
||||
{
|
||||
bool chgd = !string.IsNullOrWhiteSpace(text);
|
||||
return (text, chgd);
|
||||
}
|
||||
private (int, bool) checkInt(string text)
|
||||
{
|
||||
bool chgd = false;
|
||||
if(!string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
chgd = int.Parse(text) > 0;
|
||||
}
|
||||
return (chgd?int.Parse(text):0, chgd);
|
||||
}
|
||||
|
||||
private void frmPerson_Load(object sender, EventArgs e)
|
||||
{
|
||||
var person = _personRepository.GetPersonById(PersonId);
|
||||
initializeAllFields();
|
||||
if (person != null)
|
||||
{
|
||||
HomeAddressId = person.HomeAddress;
|
||||
CheckHomeAddressId = person.HomeAddress;
|
||||
InvoiceAddressId = person.InvoiceAddress;
|
||||
CheckInvoiceAddressId = person.InvoiceAddress;
|
||||
fillFieldsFromPerson(person);
|
||||
}
|
||||
}
|
||||
|
||||
private void fillFieldsFromPerson(Person person)
|
||||
{
|
||||
txtFirstName.Text = person.FirstName;
|
||||
txtLastName.Text = person.LastName;
|
||||
txtNickName.Text = person.NickName;
|
||||
txtPersonNr.Text = person.Born;
|
||||
txtComment.Text = person.Comments;
|
||||
txtClearingNo.Text = person.ClearingNo.ToString();
|
||||
txtAccountNr.Text = person.AccountNo.ToString();
|
||||
ShowAddressFrom(HomeAddressId);
|
||||
}
|
||||
|
||||
private void ShowAddressFrom(int AddrId)
|
||||
{
|
||||
var address = _addressRepository.GetAddressById(AddrId);
|
||||
if (address != null)
|
||||
{
|
||||
txtStreet.Text = address.Street;
|
||||
txtStreet2.Text = address.Street2;
|
||||
txtZipCode.Text = address.Zipcode.ToString();
|
||||
txtDestination.Text = address.Destination;
|
||||
txtNation.Text = address.Nation;
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeAllFields()
|
||||
{
|
||||
txtFirstName.Text = "";
|
||||
txtLastName.Text = "";
|
||||
txtNickName.Text = "";
|
||||
txtPersonNr.Text = "";
|
||||
txtComment.Text = "";
|
||||
txtClearingNo.Text = "";
|
||||
txtAccountNr.Text = "";
|
||||
ClearAddress();
|
||||
HomeAddressId = 0;
|
||||
InvoiceAddressId = 0;
|
||||
CheckHomeAddressId = 0;
|
||||
CheckInvoiceAddressId = 0;
|
||||
rdbHome.Checked = true;
|
||||
}
|
||||
|
||||
private void ClearAddress()
|
||||
{
|
||||
txtStreet.Text = "";
|
||||
txtStreet2.Text = "";
|
||||
txtZipCode.Text = "";
|
||||
txtDestination.Text = "";
|
||||
txtNation.Text = "";
|
||||
}
|
||||
|
||||
private void rdbInvoiceAddr_CheckedChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (!radioOk)
|
||||
{
|
||||
if (rdbInvoiceAddr.Checked)
|
||||
{
|
||||
HomeAddressId = AddressSave(HomeAddressId);
|
||||
ClearAddress();
|
||||
ShowAddressFrom(InvoiceAddressId);
|
||||
radioOk = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
InvoiceAddressId = AddressSave(InvoiceAddressId);
|
||||
ClearAddress();
|
||||
ShowAddressFrom(HomeAddressId);
|
||||
radioOk = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
radioOk = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void btnClose_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (HomeAddressId != CheckHomeAddressId || InvoiceAddressId != CheckInvoiceAddressId)
|
||||
{
|
||||
MessageBox.Show("NB Save info first to insure not losing addresses");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user