using System; using System.Collections.Generic; using System.Linq; using System.Text; using Tanshu.Common; using Tanshu.Accounts.Contracts; using System.Windows.Forms; using System.Threading; using Tanshu.Accounts.Repository; using Tanshu.Accounts.Entities; namespace Tanshu.Accounts.PointOfSale { public class BillHelperFunctions { BindingSource bindingSource; OrderedDictionary bill; BillItemKey oldKey; BillItemKey newKey; public BillHelperFunctions(BindingSource bindingSource, OrderedDictionary bill, int productID) { this.bindingSource = bindingSource; this.bill = bill; this.oldKey = new BillItemKey(productID, false); this.newKey = new BillItemKey(productID, true); } public void SetDiscount(string name, decimal discount) { if (discount > 1 || discount < 0) return; decimal maxDiscount = SaleVoucherBI.GetProductDiscountLimit(newKey.ProductID); if (discount > maxDiscount) MessageBox.Show(string.Format("Maximum discount for {0} is {1:P}", name, maxDiscount), "Excessive Discount", MessageBoxButtons.OK, MessageBoxIcon.Warning); if (bill.ContainsKey(oldKey)) bill[oldKey].Discount = discount; if (bill.ContainsKey(newKey)) bill[newKey].Discount = discount; return; } #region Add Product public BillInventory AddProduct() { BillInventory product; if ((!bill.ContainsKey(oldKey)) && (!bill.ContainsKey(newKey))) { //No new or old product = SaleVoucherBI.GetDefaultSaleBillItem(newKey.ProductID); product = AddProduct(product); } else if (bill.ContainsKey(newKey)) { //Has new or both bindingSource.CurrencyManager.Position = bill.IndexOfKey(newKey); product = bill[newKey]; product.Quantity += 1; } else { //Has only old product = SaleVoucherBI.GetDefaultSaleBillItem(oldKey.ProductID); product.Discount = bill[oldKey].Discount; product.Price = bill[oldKey].Price; product = AddProduct(product); } return product; } private BillInventory AddProduct(BillInventory product) { bill.Add(new BillItemKey(product.ProductID, true), product); bindingSource.DataSource = bill.Values; bindingSource.CurrencyManager.Position = bindingSource.CurrencyManager.Count + 1; return product; } #endregion #region Quantity public void SetQuantity(BillInventory product, decimal quantity, bool prompt) { if (prompt && !GetInput("Price", ref quantity)) return; if (product.Printed == 0) { // new if (!prompt) quantity += product.Quantity; SetQuantity(product, quantity); } else if (bill.ContainsKey(newKey)) { BillInventory newProduct = bill[newKey]; if (prompt) SetQuantity(newProduct, quantity - product.Printed); else SetQuantity(newProduct, newProduct.Quantity + quantity); } else { ////Has only old if (prompt) quantity -= product.Printed; if (quantity > 0 || Session.IsAllowed(RoleConstants.EDIT_PRINTED_PRODUCT)) { var newProduct = SaleVoucherBI.GetDefaultSaleBillItem(product.ProductID); newProduct.Price = product.Price; newProduct.Discount = product.Discount; newProduct = AddProduct(newProduct); SetQuantity(newProduct, quantity); } } } private void SetQuantity(BillInventory product, decimal quantity) { if (quantity < 0 && !Session.IsAllowed(RoleConstants.EDIT_PRINTED_PRODUCT)) return; if (bill.ContainsKey(oldKey)) { var totalQuantity = bill[oldKey].Quantity + quantity; if (totalQuantity < 0) quantity += 0 - totalQuantity; } product.Quantity = quantity; } #endregion public void SetPrice(BillInventory product) { if (!Allowed(product, RoleConstants.CHANGE_RATE)) return; decimal rate = product.Price; if (!GetInput("Price", ref rate)) return; if (rate == 0 && !Session.IsAllowed(RoleConstants.ZERO_RATE)) return; if (bill.ContainsKey(new BillItemKey(product.ProductID, true))) bill[new BillItemKey(product.ProductID, true)].Price = rate; if (bill.ContainsKey(new BillItemKey(product.ProductID, false))) bill[new BillItemKey(product.ProductID, false)].Price = rate; } private void InputBox_Validating(object sender, InputBoxValidatingArgs e) { } private bool Allowed(BillInventory item, RoleConstants role) { if (item == null) return false; if (!Session.IsAllowed(role)) return false; return true; } private bool GetInput(string prompt, ref decimal amount) { InputBoxResult result = InputBox.Show(prompt, amount.ToString(), InputBox_Validating); if (!result.OK) return false; if (!decimal.TryParse(result.Text, out amount)) return false; return true; } } }