narsil/Tanshu.Accounts.PointOfSale/Sales/CustomersForm.cs

103 lines
3.0 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
using System;
using System.Windows.Forms;
using Tanshu.Accounts.Repository;
2010-03-02 17:56:21 +00:00
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.PointOfSale
{
public partial class CustomersForm : Form
{
private int? _customerID;
private Customer _customer;
private readonly string phone;
2010-03-02 17:56:21 +00:00
#region Form Load
public CustomersForm(int? customerID, string phone)
2010-03-02 17:56:21 +00:00
{
InitializeComponent();
this._customerID = customerID;
2010-03-02 17:56:21 +00:00
this.phone = phone;
}
private void CustomersForm_Load(object sender, EventArgs e)
{
if (_customerID.HasValue)
2010-03-02 17:56:21 +00:00
{
using (var bi = new CustomerBI(false))
_customer = bi.Get(x => x.CustomerID == _customerID.Value);
2010-03-02 17:56:21 +00:00
btnDelete.Enabled = true;
txtName.Text = _customer.Name;
txtPhone.Text = _customer.Phone;
txtAddress.Text = _customer.Address;
txtRemarks.Text = _customer.Remarks;
chkImportant.Checked = _customer.Important;
2010-03-02 17:56:21 +00:00
}
else
txtPhone.Text = phone;
btnSave.Text = _customerID.HasValue ? "&Update" : "&Save";
2010-03-02 17:56:21 +00:00
}
#endregion
#region Buttons
private void btnSave_Click(object sender, EventArgs e)
{
if (!ValidateValues())
{
MessageBox.Show("Missing Information: Please check the form.");
txtName.Focus();
}
else
{
Save();
btnCancel_Click(sender, e);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
using (var bi = new CustomerBI())
bi.Delete(x => x.CustomerID == _customer.CustomerID);
2010-03-02 17:56:21 +00:00
btnCancel_Click(sender, e);
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region Add / Edit / Delete
private bool ValidateValues()
{
return txtName.Text.Trim().Length != 0;
2010-03-02 17:56:21 +00:00
}
2010-03-02 17:56:21 +00:00
private void Save()
{
if (_customer == null)
_customer = new Customer();
_customer.Name = txtName.Text;
_customer.Phone = txtPhone.Text;
_customer.Address = txtAddress.Text;
_customer.Remarks = txtRemarks.Text;
_customer.Important = chkImportant.Checked;
using (var bi = new CustomerBI())
if (_customer.CustomerID == 0)
bi.Insert(_customer);
else
bi.Update(_customer);
2010-03-02 17:56:21 +00:00
}
private void Delete(int customerID)
2010-03-02 17:56:21 +00:00
{
using (var bi = new CustomerBI())
bi.Delete(x => x.CustomerID == customerID);
2010-03-02 17:56:21 +00:00
}
#endregion
public Customer Customer
2010-03-02 17:56:21 +00:00
{
get { return _customer; }
2010-03-02 17:56:21 +00:00
}
}
}