narsil/Tanshu.Accounts.PointOfSale/Sales/CustomersForm.cs
tanshu 69617949bd Important! : Need to update to new schema using SQL Scripts
Important! : This version will not work.  It is pre-alpha and saved in case of catastrophic failure
Refactor: Remove dependency on Fluent Nhibernate.
Refactor: All Primary keys are now Guids.
Refactor: Class Mappings changed from AutoMap to Explicit Mappings.
Breakage: All Cascading is now disabled and entities must be explicitly saved/updated/deleted
Breakage: Auto Commiting is now off and "SaveChanges()" needs to be called on all BIs.
Refactor: Changed the pattern where all relevant db code for an operation is basically in the same function.
Chore: Removed Advance and Payments options.
2014-10-12 15:11:45 +05:30

112 lines
3.2 KiB
C#

using System;
using System.Windows.Forms;
using Tanshu.Accounts.Repository;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
namespace Tanshu.Accounts.PointOfSale
{
public partial class CustomersForm : Form
{
private Guid? _customerID;
private Customer _customer;
private readonly string phone;
#region Form Load
public CustomersForm(Guid? customerID, string phone)
{
InitializeComponent();
this._customerID = customerID;
this.phone = phone;
}
private void CustomersForm_Load(object sender, EventArgs e)
{
if (_customerID.HasValue)
{
using (var bi = new CustomerBI())
_customer = bi.Get(x => x.CustomerID == _customerID.Value);
btnDelete.Enabled = true;
txtName.Text = _customer.Name;
txtPhone.Text = _customer.Phone;
txtAddress.Text = _customer.Address;
txtRemarks.Text = _customer.Remarks;
chkImportant.Checked = _customer.Important;
}
else
txtPhone.Text = phone;
btnSave.Text = _customerID.HasValue ? "&Update" : "&Save";
}
#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);
bi.SaveChanges();
}
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;
}
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 == Guid.Empty)
bi.Insert(_customer);
else
bi.Update(_customer);
bi.SaveChanges();
}
}
private void Delete(Guid customerID)
{
using (var bi = new CustomerBI())
{
bi.Delete(x => x.CustomerID == customerID);
bi.SaveChanges();
}
}
#endregion
public Customer Customer
{
get { return _customer; }
}
}
}