narsil/Tanshu.Accounts.PointOfSale/MainForm.cs

342 lines
11 KiB
C#

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Management;
using Tanshu.Accounts.PointOfSale.Sales;
using Tanshu.Accounts.Repository;
using Tanshu.Common;
using Tanshu.Common.KeyboardControl;
namespace Tanshu.Accounts.PointOfSale
{
public partial class MainForm : Form
{
public MainForm()
{
//new SchemaExport(SessionManager.Configuration).Create(false, true);
InitializeComponent();
Text = "Point of Sale: Login (" + Environment.MachineName + ")";
}
private bool logSql
{
get
{
var settingValue = ConfigurationManager.AppSettings["log-sql"];
return settingValue == "true";
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
LoginUser(new KeyboardLogin());
}
private void btnSale_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Sales"))
using (var frmSale = new SalesForm(new BillController(null)))
{
frmSale.ShowDialog();
Cache.Invalidate();
}
}
private void btnProduct_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Products"))
using (var frm = new ProductListForm())
frm.ShowDialog();
}
private void btnProductGroup_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Products"))
using (var frm = new ProductGroupListForm())
frm.ShowDialog();
}
private void btnCustomer_Click(object sender, EventArgs e)
{
using (var frm = new CustomerListForm())
frm.ShowDialog();
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void btnUsers_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Roles"))
using (var frm = new UserListForm())
frm.ShowDialog();
}
private void btnUserRoles_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Roles"))
using (var frm = new AssignUserGroups())
frm.ShowDialog();
}
private void btnChangePassword_Click(object sender, EventArgs e)
{
using (var frm = new ChangePassword(new KeyboardControl()))
frm.ShowDialog();
}
private void btnCashierCheckout_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Cashier Checkout"))
using (var frm = new CheckoutForm())
frm.ShowDialog();
}
private void btnSaleAnalysis_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Sales Analysis"))
using (var frm = new frmSaleAnalysisForm())
frm.ShowDialog();
}
private void btnSaleDetail_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Sales Detail"))
using (var frm = new SaleDetailForm())
frm.ShowDialog();
}
private void MainForm_Load(object sender, EventArgs e)
{
try
{
SessionManager.Initialize(logSql);
}
catch (SqlException)
{
MessageBox.Show("Unable to connect to the server.\nPlease check that the server is on and both this computer and the server both are connected to the network.", "Server Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
#if (DEBUG)
MessageBox.Show("This software does not print kots!!!", "Debug Mode", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
#endif
CheckMachine();
CheckRoles();
}
private void CheckMachine()
{
if (string.IsNullOrEmpty(Cache.Location))
{
MessageBox.Show("No Machine Location");
using (var frm = new MachineEditForm(Environment.MachineName))
frm.ShowDialog();
if (string.IsNullOrEmpty(Cache.Location))
{
MessageBox.Show("Machine Location not set");
Close();
}
}
}
private void CheckRoles()
{
btnSale.Visible = Session.IsAllowed("Sales");
btnCustomer.Visible = Session.IsAllowed("Customers");
btnOpenBill.Visible = Session.IsAllowed("Open Bill");
btnCashierCheckout.Visible = Session.IsAllowed("Cashier Checkout");
btnSaleAnalysis.Visible = Session.IsAllowed("Sales Analysis");
btnTaxAnalysis.Visible = Session.IsAllowed("Tax Analysis");
btnSaleDetail.Visible = Session.IsAllowed("Sales Detail");
btnBillDetails.Visible = Session.IsAllowed("Bill Details");
btnBeerConsumption.Visible = Session.IsAllowed("Beer Consumption");
btnVoidOrReprints.Visible = Session.IsAllowed("Void or Reprinted Bill Report");
btnDiscountReport.Visible = Session.IsAllowed("Discount Report");
btnProduct.Visible = Session.IsAllowed("Products");
btnProductGroup.Visible = Session.IsAllowed("Products");
btnReorderTables.Visible = Session.IsAllowed("Tables");
btnMachines.Visible = Session.IsAllowed("Machines");
btnModifiers.Visible = Session.IsAllowed("Modifiers");
btnTaxes.Visible = Session.IsAllowed("Discount Report");
btnUsers.Visible = Session.IsAllowed("Users");
btnUserRoles.Visible = Session.IsAllowed("Users");
btnGroupRoles.Visible = Session.IsAllowed("Roles");
#if (DEBUG)
btnManagement.Visible = true;
#else
btnManagement.Visible = false;
#endif
btnChangePassword.Visible = Session.IsAuthenticated;
}
private void btnGroupRoles_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Roles"))
using (var frm = new AssignGroupRoles())
frm.ShowDialog();
}
private void btnSwipeLogin_Click(object sender, EventArgs e)
{
LoginUser(new MsrLogin());
}
private void LoginUser(ILogin login)
{
if (!Session.IsAuthenticated)
{
if (login.LoginUser())
{
Text = "Point of Sale: " + Session.User.Name + " (" + Environment.MachineName + ")";
btnLogin.Text = "Logout";
btnSwipeLogin.Visible = false;
}
}
else
{
login.LogoutUser();
Text = "Point of Sale: Login (" + Environment.MachineName + ")";
btnLogin.Text = "Login";
btnSwipeLogin.Visible = true;
}
CheckRoles();
}
private void btnOpenBill_Click(object sender, EventArgs e)
{
if (!Session.IsAllowed("Open Bill"))
return;
var result = InputBox.Show("Bill Number", "0", InputBox_Validating);
if (!result.OK)
return;
Expression<Func<Voucher, bool>> query;
int billID;
if (Regex.IsMatch(result.Text, @"^\d{2,}-\d{4}$"))
{
billID = int.Parse(result.Text.Replace("-", ""));
query = x => x.BillID == billID && (x.VoucherType == VoucherType.Regular || x.VoucherType == VoucherType.TakeAway);
}
else if (Regex.IsMatch(result.Text, @"^NC-\d+$"))
{
billID = int.Parse(result.Text.Replace("NC-", ""));
query = x => x.BillID == billID && x.VoucherType == VoucherType.NoCharge;
}
else if (Regex.IsMatch(result.Text, @"^ST-\d+$"))
{
billID = int.Parse(result.Text.Replace("ST-", ""));
query = x => x.BillID == billID && x.VoucherType == VoucherType.Staff;
}
else
return;
Voucher voucher;
using (var bi = new VoucherBI())
voucher = bi.Get(query);
if (voucher == null)
return;
if (Session.IsAllowed("Sales"))
using (var frmSale = new SalesForm(new BillController(voucher.VoucherID)))
frmSale.ShowDialog();
}
private static void InputBox_Validating(object sender, InputBoxValidatingArgs e)
{
if (Regex.IsMatch(e.Text, @"^\d+-\d\d\d\d")) return;
e.Cancel = true;
e.Message = "Bill No should be in format ##-####";
}
private void btnBillDetails_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Bill Details"))
using (var frm = new BillDetailsForm())
frm.ShowDialog();
}
private void btnVoidOrReprints_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Void or Reprinted Bill Report"))
using (var frm = new VoidReprintedForm())
frm.ShowDialog();
}
private void btnDiscountReport_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Discount Report"))
using (var frm = new DiscountReportForm())
frm.ShowDialog();
}
private void btnManagement_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Owner"))
using (var frm = new ManagementForm())
frm.ShowDialog();
}
private void btnReorderTables_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Tables"))
using (var frm = new ReorderTableForm())
{
frm.ShowDialog();
}
}
private void btnModifiers_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Modifiers"))
using (var frm = new ModifierListForm())
{
frm.ShowDialog();
}
}
private void btnMachines_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Machines"))
using (var frm = new MachineListForm())
{
frm.ShowDialog();
}
}
private void btnTaxAnalysis_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Tax Analysis"))
using (var frm = new TaxAnalysisForm())
frm.ShowDialog();
}
private void btnBeerConsumption_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Beer Consumption"))
using (var frm = new BeerConsumptionForm())
frm.ShowDialog();
}
private void btnTaxes_Click(object sender, EventArgs e)
{
if (Session.IsAllowed("Taxes"))
using (var frm = new TaxListForm())
frm.ShowDialog();
}
}
}