From c63bc623041e9d700bb120eba6b2cb66ea9a7dfa Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 11 Jan 2011 01:19:11 +0530 Subject: [PATCH] Moved SaleForm code to Controller, have to put Exception handlers for PermissionException --- Tanshu.Accounts.Contracts/Roles.cs | 11 +- .../Authentication/PermissionException.cs | 16 + .../Controllers/BillController.cs | 443 ++++++++++++++ .../Controllers/DisplayController.cs | 31 + .../Controllers/ISaleForm.cs | 18 + Tanshu.Accounts.PointOfSale/MainForm.cs | 2 +- .../Sales/SalesForm.cs | 546 +++--------------- .../Tanshu.Accounts.PointOfSale.csproj | 3 + .../Updates/UpdateSales.cs | 2 +- 9 files changed, 592 insertions(+), 480 deletions(-) create mode 100644 Tanshu.Accounts.PointOfSale/Authentication/PermissionException.cs create mode 100644 Tanshu.Accounts.PointOfSale/Controllers/BillController.cs create mode 100644 Tanshu.Accounts.PointOfSale/Controllers/DisplayController.cs create mode 100644 Tanshu.Accounts.PointOfSale/Controllers/ISaleForm.cs diff --git a/Tanshu.Accounts.Contracts/Roles.cs b/Tanshu.Accounts.Contracts/Roles.cs index f88ba03..540c29c 100644 --- a/Tanshu.Accounts.Contracts/Roles.cs +++ b/Tanshu.Accounts.Contracts/Roles.cs @@ -9,10 +9,19 @@ namespace Tanshu.Accounts.Contracts { public const string SALES_CHECKOUT = "Sales/Checkout"; public const string SALES_SALES_BILL = "Sales/SalesBill"; - public const string SECURITY_MANAGE_ROLES = "Security/ManageRoles"; public const string SALES_SALE_DETAIL = "Sales/SaleDetail"; + public const string SALES_VOID_BILL = "Sales/VoidPrintedBill"; + public const string SALES_EDIT_BILL = "Sales/EditBill"; + public const string SALES_PRINT_KOT = "Sales/PrintKOT"; + public const string SALES_PRINT_BILL = "Sales/PrintBill"; + public const string SALES_CHANGE_RATE = "Sales/ChangeRate"; + public const string SALES_EDIT_PRINTED_PRODUCT = "Sales/EditPrintedProduct"; + public const string MASTER_PRODUCTS = "Master/Products"; + + public const string SECURITY_MANAGE_ROLES = "Security/ManageRoles"; public const string LOG_VIEW = "Log/View"; public const string MASTER_OWNER = "Master/Owner"; + } } diff --git a/Tanshu.Accounts.PointOfSale/Authentication/PermissionException.cs b/Tanshu.Accounts.PointOfSale/Authentication/PermissionException.cs new file mode 100644 index 0000000..58b04ba --- /dev/null +++ b/Tanshu.Accounts.PointOfSale/Authentication/PermissionException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Tanshu.Accounts.PointOfSale +{ + public class PermissionException : Exception + { + public PermissionException(string message) + : base(message) + { + } + + } +} diff --git a/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs b/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs new file mode 100644 index 0000000..9ee67c6 --- /dev/null +++ b/Tanshu.Accounts.PointOfSale/Controllers/BillController.cs @@ -0,0 +1,443 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Tanshu.Accounts.BI; +using Tanshu.Accounts.Helpers; +using Tanshu.Accounts.Contracts; +using System.Windows.Forms; +using Tanshu.Common; + +namespace Tanshu.Accounts.PointOfSale +{ + public class BillController + { + private SaleVoucherBO billInfo; + private Dictionary bill = new Dictionary(); + Guid? newBillID; + + ISaleForm saleForm; + private CustomerBO customer = new CustomerBI().GetCustomer(new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5")); + public void InitGui(ISaleForm saleForm) + { + this.saleForm = saleForm; + this.saleForm.SetCustomerDisplay(customer.Name); + this.saleForm.SetUserName(Session.User.Name); + + } + private void InitComponents() + { + InitModel(); + InitView(); + } + + private void InitView() + { + throw new NotImplementedException(); + } + + private void InitModel() + { + throw new NotImplementedException(); + } + public void AddProductToGrid(Guid productID, BindingSource bindingSource) + { + BillHelperFunctions.AddProductToGrid(productID, bindingSource, bill); + ShowAmount(); + } + public void ShowCustomerList(bool reset) + { + if ((customer.CustomerID == new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5")) && (!reset)) + { + using (SelectCustomer selectCustomer = new SelectCustomer(new CustomerBI().GetFilteredCustomers, true)) + { + selectCustomer.customerEvent += new CustomerEventHandler(selectCustomer_customerEvent); + selectCustomer.ShowDialog(); + if (selectCustomer.SelectedItem != null) + { + customer = selectCustomer.SelectedItem; + saleForm.SetCustomerDisplay(customer.Name); + } + else + { + customer = new CustomerBI().GetCustomer(new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5")); + } + } + } + else + { + customer = new CustomerBI().GetCustomer(new Guid("F016CBAD-206C-42C0-BB1D-6006CE57BAB5")); + saleForm.SetCustomerDisplay(customer.Name); + } + } + + #region Save + // if (btnWaiter.Tag == null) + //btnWaiter.Tag = new WaiterBI().GetWaiters()[0].WaiterID; + + public void Save(bool print, Guid waiterID, string tableID) + { + using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_EDIT_BILL)) + { + if ((billInfo != null) && (new SaleVoucherBI().IsBillPrinted(billInfo.VoucherID)) && (!roleBI.IsAllowed)) + { + throw new PermissionException("You do not have the permission to reprint a bill."); + } + } + if (bill.Count != 0) + { + Guid? saved; + if (billInfo == null) + saved = AddNewSale(print, waiterID, tableID); + else + saved = UpdateSale(print, waiterID, tableID); + if (saved.HasValue) + { + if (newBillID.HasValue) + saleForm.CloseWindow(); + else + PrintBill(print, saved.Value); + } + ClearBill(); + } + } + private Guid? AddNewSale(bool finalBill, Guid waiterID, string tableID) + { + if (billInfo != null) + { + MessageBox.Show("Error in AddNewSale, there is a previous sale in memory", "Error"); + return null; + } + + + #region SaleVoucher + UserBO user = Session.User; + SaleVoucherBO saleVoucher = new SaleVoucherBO + { + CustomerID = customer.CustomerID, + Paid = false, + //Paid = finalBill, + TableID = tableID, + WaiterID = waiterID, + Printed = finalBill, + Void = false, + Date = DateTime.Now, + Narration = "", + Ref = "", + Type = 'S', + UserID = user.UserID + }; + #endregion + #region Inventories + List iList = new SaleVoucherBI().SaleInventory(bill.Values, null); + #endregion + new SaleVoucherBI().Insert(saleVoucher, iList); + return saleVoucher.VoucherID; + + } + private Guid? UpdateSale(bool finalBill, Guid waiterID, string tableID) + { + UserBO user = Session.User; + #region Voucher and SaleVoucher + SaleVoucherBO saleVoucher = new SaleVoucherBO + { + VoucherID = billInfo.VoucherID, + timestamp = (byte[])billInfo.timestamp, + UserID = billInfo.UserID, + Date = billInfo.Date, + CreationDate = DateTime.Now, + LastEditDate = DateTime.Now, + Narration = billInfo.Narration, + Ref = "", + Type = 'S', + Alarm = billInfo.Alarm, + BillID = billInfo.BillID, + CustomerID = customer.CustomerID, + KotID = billInfo.KotID, + Paid = billInfo.Paid, + Printed = billInfo.Printed || finalBill, + TableID = tableID, + Void = billInfo.Void, + VoidReason = billInfo.VoidReason, + WaiterID = waiterID, + SaleTimestamp = (byte[])billInfo.SaleTimestamp + }; + if ((!billInfo.Printed) && finalBill) + saleVoucher.Date = null; + #endregion + #region Inventory + List iList = new SaleVoucherBI().SaleInventory(bill.Values, billInfo.VoucherID); + #endregion + new SaleVoucherBI().Update(saleVoucher, iList); + return saleVoucher.VoucherID; + } + #endregion + public void VoidBill() + { + if (billInfo != null) + { + if (billInfo.Printed) + { + using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_VOID_BILL)) + { + if (roleBI.IsAllowed) + { + + if (MessageBox.Show("Are you sure you want to void this bill?", "Void Bill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) + { + SelectVoidReason voidReason = new SelectVoidReason(GetVoidReason, true); + voidReason.ShowDialog(); + if (voidReason.SelectedItem != null) + { + new SaleVoucherBI().VoidBill(billInfo.VoucherID, voidReason.SelectedItem.Description); + ClearBill(); + } + else + { + MessageBox.Show("Please Select a reason if you want to void the bill", "Bill NOT Voided", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); + } + } + } + else + throw new PermissionException("You do not have the permission to void a bill."); + } + } + } + } + CustomerBO selectCustomer_customerEvent(object sender, CustomerEventArgs e) + { + using (CustomersForm form = new CustomersForm(e.CustomerID, e.Phone)) + { + form.ShowDialog(); + return form.Customer; + } + } + private void PrintBill(bool finalBill, Guid voucherID) + { + using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_PRINT_KOT)) + if (!roleBI.IsAllowed) + throw new PermissionException("You are not allowed to print KOT"); + + if (!finalBill) + Accounts.Print.Thermal.PrintWaiterKot("KOT", voucherID, bill.Values.ToList()); + else + { + using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_PRINT_BILL)) + { + if (roleBI.IsAllowed) + { + Accounts.Print.Thermal.PrintBill(Session.printer(), true, voucherID, bill.Values.ToList()); + Accounts.Print.Thermal.PrintCustomerKot("KOT", voucherID, bill.Values.ToList()); + + } + else + throw new PermissionException("You are not allowed to print KOT"); + } + } + } + + private List GetVoidReason(Dictionary filter) + { + List list = new List(); + list.Add(new StringType("Discount")); + list.Add(new StringType("Printing Fault")); + list.Add(new StringType("Item Changed")); + list.Add(new StringType("Quantity Reduced")); + list.Add(new StringType("Costing Bill for Party")); + list.Add(new StringType("Cashier Mistake")); + list.Add(new StringType("Management Freesale")); + list.Add(new StringType("Other")); + return list.Where(i => i.Description.ToLower().Contains(filter["Name"].ToLower().Trim())).ToList(); + } + public void SetNewBillID(Guid voucherID) + { + this.newBillID = voucherID; + } + public SalesBillItemBO CurrentProduct(int index) + { + if ((index != -1) && (index < bill.Count)) + return bill.ElementAt(index).Value; + else + return null; + + //if (dgvProducts.Rows.Count == 0) + // return null; + //SalesBillItemBO product = bill.ElementAt(dgvProducts.CurrentRow.Index).Value; + //return product; + } + private void ShowAmount() + { + decimal grossTax = bill.Values.Sum(b => b.TaxAmount); + decimal discount = bill.Values.Sum(b => b.DiscountAmount); + decimal grossAmount = bill.Values.Sum(b => b.GrossAmount); + decimal amount = bill.Values.Sum(b => b.Value); + //bill.Values.ToList(); + saleForm.ShowAmount(grossTax, discount, grossAmount, amount, bill.Values.ToList()); + } + public void SetDiscount(SalesBillItemBO product, decimal discount) + { + if (product == null) + return; + BillHelperFunctions.SetDiscount(product.productID, discount, customer, bill); + ShowAmount(); + } + public void SetAmount(SalesBillItemBO product, BindingSource bindingSource, decimal amount) + { + if (product == null) + return; + BillHelperFunctions.SetAmount(product, amount, bindingSource, bill); + ShowAmount(); + } + public bool ProductRemove(SalesBillItemBO product) + { + if (product == null) + return false; + if (product.Printed > 0) + { + using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_EDIT_PRINTED_PRODUCT)) + { + if (roleBI.IsAllowed) + { + if (MessageBox.Show(string.Format("Already {0} items have been printed.\n\rAre you sure you want to delete this item?", product.Printed), "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + return false; + //If BillList(Location).Printed <> 0 Then ItemsDeleted.Add("Deleted " & BillList(Location).Printed & " " & BillList(Location).Name & " from Bill No " & mBillNo) + bill.Remove(new BillItemKey(product.productID, product.Printed == 0)); + ShowAmount(); + } + else + throw new PermissionException("You are not allowed to delete already printed products"); + return true; + } + } + return false; + } + public void SetQuantity(SalesBillItemBO product, BindingSource bindingSource, decimal quantity, bool absolute, bool prompt) + { + if (product == null) + return; + BillHelperFunctions.SetQuantity(product, quantity, absolute, prompt, bindingSource, bill); + ShowAmount(); + } + + public void ChangeRate(int index) + { + using (RoleBI roleBI = RoleFactoryBI.GetRoleBI(Roles.SALES_CHANGE_RATE)) + { + if (roleBI.IsAllowed) + { + SalesBillItemBO product = bill.ElementAt(index).Value; + decimal rate = 0; + InputBoxResult result = InputBox.Show("Enter Rate", "Rate", product.Price.ToString(), InputBox_Validating); + if (result.OK) + rate = Convert.ToDecimal(result.Text); + if (rate != 0) + { + BillHelperFunctions.SetRate(product.productID, rate, bill); + ShowAmount(); + } + } + else + throw new PermissionException("You do not have permission to Change the Rate"); + + } + } + private void InputBox_Validating(object sender, InputBoxValidatingArgs e) + { + } + private void LoadBill(Guid voucherID) + { + ClearBill(); + List iList = new List(); + new SaleVoucherBI().GetSaleVoucher(voucherID, ref billInfo, ref iList); + + this.customer = new CustomerBI().GetCustomer(billInfo.CustomerID); + saleForm.ShowInfo(billInfo.BillID, billInfo.KotID, billInfo.CreationDate, billInfo.Date.Value, billInfo.LastEditDate, customer.Name, billInfo.TableID, billInfo.WaiterID, new WaiterBI().GetWaiter(billInfo.WaiterID).Name); + + foreach (InventoryDisplayBO inventory in iList) + { + if (inventory.ComplexProductID.HasValue) + { + BillItemKey key = new BillItemKey(inventory.ComplexProductID.Value, inventory.Quantity == 0); + if (!bill.ContainsKey(key)) + { + decimal rate = 0, quantity = 0; + string name = ""; + new SaleVoucherBI().GetComplexBillInformation(voucherID, inventory.ComplexProductID.Value, ref rate, ref quantity, ref name); + bill.Add(key, new SalesBillItemBO + { + productID = inventory.ComplexProductID.Value, + Discount = inventory.Discount, + Name = name, + Price = rate, + Printed = quantity, + Quantity = quantity, + Tax = inventory.Tax, + }); + + } + } + else + { + BillItemKey key = new BillItemKey(inventory.ProductID, inventory.Quantity == 0); + bill.Add(key, new SalesBillItemBO + { + productID = inventory.ProductID, + Discount = inventory.Discount, + Name = inventory.ProductName, + Price = inventory.Rate, + Printed = inventory.Quantity, + Quantity = inventory.Quantity, + Tax = inventory.Tax, + }); + } + } + ShowAmount(); + + } + public void LoadBillFromTable() + { + InputBoxResult result = InputBox.Show("Enter Table Number", "Table", "0", InputBox_Validating); + if (result.OK) + { + string tableID = result.Text.Trim(); + if ((tableID != "C") && (tableID != "") && (!tableID.Contains("."))) + { + Guid? tID = new SaleVoucherBI().GetPendingVoucherID(tableID); + if (tID.HasValue) + { + LoadBill(tID.Value); + } + } + else + ClearBill(); + } + } + + public void CancelBillChanges() + { + if (bill.Count != 0) + if (MessageBox.Show("Cancel current bill?", "Cancel bill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) + return; + ClearBill(); + + } + public void ClearBill() + { + billInfo = null; + ShowCustomerList(true); + bill.Clear(); + saleForm.ClearBill(bill); + } + public void FormLoad() + { + if (newBillID.HasValue) + { + LoadBill(newBillID.Value); + } + + } + } +} +// How to load a bill +//LoadBill(((PendingBillsBO)bsPending.Current).voucherID); +// ChangeFormState(SaleFormState.Billing); \ No newline at end of file diff --git a/Tanshu.Accounts.PointOfSale/Controllers/DisplayController.cs b/Tanshu.Accounts.PointOfSale/Controllers/DisplayController.cs new file mode 100644 index 0000000..4829326 --- /dev/null +++ b/Tanshu.Accounts.PointOfSale/Controllers/DisplayController.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Tanshu.Accounts.PointOfSale +{ + public class DisplayController + { + ISaleForm saleForm; + public void InitGui(ISaleForm saleForm) + { + this.saleForm = saleForm; + } + private void InitComponents() + { + InitModel(); + InitView(); + } + + private void InitView() + { + throw new NotImplementedException(); + } + + private void InitModel() + { + throw new NotImplementedException(); + } + } +} diff --git a/Tanshu.Accounts.PointOfSale/Controllers/ISaleForm.cs b/Tanshu.Accounts.PointOfSale/Controllers/ISaleForm.cs new file mode 100644 index 0000000..cd416a8 --- /dev/null +++ b/Tanshu.Accounts.PointOfSale/Controllers/ISaleForm.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Tanshu.Accounts.Contracts; + +namespace Tanshu.Accounts.PointOfSale +{ + public interface ISaleForm + { + void ClearBill(Dictionary bill); + void SetCustomerDisplay(string name); + void CloseWindow(); + void ShowAmount(decimal grossTax, decimal discount, decimal grossAmount, decimal amount, List bill); + void ShowInfo(string billID, string kotID, DateTime creationDate, DateTime date, DateTime lastEditDate, string customer, string tableID, Guid waiterID, string waiter); + void SetUserName(string name); + } +} diff --git a/Tanshu.Accounts.PointOfSale/MainForm.cs b/Tanshu.Accounts.PointOfSale/MainForm.cs index f27c50f..85bda79 100644 --- a/Tanshu.Accounts.PointOfSale/MainForm.cs +++ b/Tanshu.Accounts.PointOfSale/MainForm.cs @@ -227,7 +227,7 @@ namespace Tanshu.Accounts.PointOfSale if (roleBI.IsAllowed) { new ProductBI().UpdateShortName(); - using (SalesForm frmSale = new SalesForm()) + using (SalesForm frmSale = new SalesForm(new BillController())) frmSale.ShowDialog(); } } diff --git a/Tanshu.Accounts.PointOfSale/Sales/SalesForm.cs b/Tanshu.Accounts.PointOfSale/Sales/SalesForm.cs index 34e5292..5410d02 100644 --- a/Tanshu.Accounts.PointOfSale/Sales/SalesForm.cs +++ b/Tanshu.Accounts.PointOfSale/Sales/SalesForm.cs @@ -13,33 +13,31 @@ using Tanshu.Common; namespace Tanshu.Accounts.PointOfSale { - public partial class SalesForm : Form + public partial class SalesForm : Form, ISaleForm { + BillController billController; List