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 static class BillHelperFunctions { #region Discount public static void SetDiscount(int productID, decimal discount, Customer customer, OrderedDictionary bill) { #region InputBox if (discount == -1) { InputBoxResult result = InputBox.Show("Discount", "0", InputBox_Validating); if (result.OK) { if (!decimal.TryParse(result.Text, out discount)) return; discount /= 100; } } if (discount == -1) return; #endregion #region Max Discount decimal maxDiscount = SaleVoucherBI.GetProductDiscountLimit(productID); if ((discount > maxDiscount) && customer.CustomerID != 1) { MessageBox.Show(string.Format("Maximum discount for this product is {0:P}", maxDiscount), "Excessive Discount", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if ((discount > maxDiscount) && customer.CustomerID == 1) { MessageBox.Show(string.Format("Maximum discount for this product is {0:P} Discount Disallowed", maxDiscount), "Excessive Discount", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } #endregion if (bill.ContainsKey(new BillItemKey(productID, true))) SetDiscount(bill[new BillItemKey(productID, true)], discount); if (bill.ContainsKey(new BillItemKey(productID, false))) SetDiscount(bill[new BillItemKey(productID, false)], discount); return; } private static void SetDiscount(BillInventory product, decimal discount) { product.Discount = discount; } #endregion #region Add Product public static BillInventory AddProductToGrid(int productID, BindingSource bindingSource, OrderedDictionary bill) { BillInventory product; if ((!bill.ContainsKey(new BillItemKey(productID, false))) && (!bill.ContainsKey(new BillItemKey(productID, true)))) { //No new or old product = AddNewProduct(productID, bindingSource, bill); } else if (bill.ContainsKey(new BillItemKey(productID, true))) { //Has new or both BillItemKey key = new BillItemKey(productID, true); bindingSource.CurrencyManager.Position = ProductPosition(key, bill); product = bill[key]; SetQuantity(product, 1, false); } else { //Has only old product = bill[new BillItemKey(productID, false)]; if (product.Additional <= -1) SetQuantity(product, 1, false); else if (product.Additional < 0) { decimal quantity = 1 + product.Additional; SetQuantity(product, 1, false); decimal rate = bill[new BillItemKey(productID, false)].Discount; decimal discount = bill[new BillItemKey(productID, false)].Price; product = AddNewProduct(productID, bindingSource, bill); SetDiscount(product, discount); SetRate(productID, rate, bill); SetQuantity(product, quantity, true); } else { decimal discount = bill[new BillItemKey(productID, false)].Discount; decimal rate = bill[new BillItemKey(productID, false)].Price; product = AddNewProduct(productID, bindingSource, bill); SetDiscount(product, discount); SetRate(productID, rate, bill); } } return product; } private static BillInventory AddNewProduct(int productID, BindingSource bindingSource, OrderedDictionary bill) { BillItemKey key = new BillItemKey(productID, true); BillInventory product = SaleVoucherBI.GetDefaultSaleBillItem(productID); product.Quantity = 1; bill.Add(key, product); bindingSource.DataSource = bill.Values; bindingSource.CurrencyManager.Position = bindingSource.CurrencyManager.Count + 1; return product; } private static int ProductPosition(BillItemKey key, OrderedDictionary bill) { for (int i = 0; i < bill.Count; i++) { if (bill.Keys.ElementAt(i) == key) return i; } return 0; } #endregion #region Quantity public static void SetQuantity(BillInventory product, decimal quantity, bool absolute, bool prompt, BindingSource bindingSource, OrderedDictionary bill) { #region Prompt if (prompt) { InputBoxResult result = InputBox.Show("Quantity", (product.Quantity + 1).ToString(), InputBox_Validating); if (result.OK) { if (!decimal.TryParse(result.Text, out quantity)) return; absolute = true; } } if (quantity == 0) return; #endregion CheckQuantity(product, quantity, absolute); if (product.Printed == 0) { SetQuantity(product, quantity, absolute); } else if (bill.ContainsKey(new BillItemKey(product.ProductID, true))) { BillInventory otherProduct = bill[new BillItemKey(product.ProductID, true)]; if (absolute) SetQuantity(otherProduct, quantity - product.Printed, absolute); else SetQuantity(otherProduct, quantity, absolute); } else { if (product.Additional < 0) { if (!absolute) quantity += product.Additional; product.Quantity = product.Printed; } if (absolute) quantity -= product.Quantity; if (quantity > 0) { BillInventory otherProduct = AddProductToGrid(product.ProductID, bindingSource, bill); SetQuantity(otherProduct, quantity, true); } else if ((quantity < 0) && (Thread.CurrentPrincipal.IsInRole("Sales/EditPrintedProduct"))) { SetQuantity(product, quantity, false); } } } private static bool CheckQuantity(BillInventory product, decimal quantity, bool absolute) { if (!absolute) quantity = product.Quantity + quantity; if (quantity < 0) return false; else if ((quantity < product.Printed) && (!Thread.CurrentPrincipal.IsInRole("Sales/EditPrintedProduct"))) return false; else return true; } private static void SetQuantity(BillInventory product, decimal quantity, bool absolute) { if (!absolute) { quantity = product.Quantity + quantity; } if (quantity <= 0) return; product.Quantity = quantity; } #endregion #region Amount public static void SetAmount(BillInventory product, decimal amount, BindingSource bindingSource, OrderedDictionary bill) { if (amount == -1) { InputBoxResult result = InputBox.Show("Amount", (product.Value).ToString(), InputBox_Validating); if (result.OK) { amount = Convert.ToDecimal(result.Text); } } if (amount == -1) return; else { SetQuantity(product, amount / (product.Price * (1 + product.Tax) * (1 - product.Discount)), true, false, bindingSource, bill); } } #endregion #region Rate public static void SetRate(int productID, decimal rate, OrderedDictionary bill) { if (bill.ContainsKey(new BillItemKey(productID, true))) bill[new BillItemKey(productID, true)].Price = rate; if (bill.ContainsKey(new BillItemKey(productID, false))) bill[new BillItemKey(productID, false)].Price = rate; } #endregion private static void InputBox_Validating(object sender, InputBoxValidatingArgs e) { } } }