narsil/Tanshu.Accounts.PointOfSale/Sales/BillHelperFunctions.cs

238 lines
9.3 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
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;
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.PointOfSale
{
public static class BillHelperFunctions
{
#region Discount
public static void SetDiscount(int productID, decimal discount, Customer customer, OrderedDictionary<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
#region InputBox
if (discount == -1)
{
InputBoxResult result = InputBox.Show("Discount Rate", "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 = new SaleVoucherBI().GetProductDiscountLimit(productID);
if ((discount > maxDiscount) && customer.CustomerID != 1)
2010-03-02 17:56:21 +00:00
{
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)
2010-03-02 17:56:21 +00:00
{
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)
2010-03-02 17:56:21 +00:00
{
product.Discount = discount;
}
#endregion
#region Add Product
public static BillInventory AddProductToGrid(int productID, BindingSource bindingSource, OrderedDictionary<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
BillInventory product;
2010-03-02 17:56:21 +00:00
if ((!bill.ContainsKey(new BillItemKey(productID, true))) && (!bill.ContainsKey(new BillItemKey(productID, false))))
{
//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<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
BillItemKey key = new BillItemKey(productID, true);
BillInventory product = new SaleVoucherBI().GetDefaultSaleBillItem(productID);
2010-03-02 17:56:21 +00:00
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<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
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<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
#region Prompt
if (prompt)
{
InputBoxResult result = InputBox.Show("Enter Quantity", "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)))
2010-03-02 17:56:21 +00:00
{
BillInventory otherProduct = bill[new BillItemKey(product.ProductID, true)];
2010-03-02 17:56:21 +00:00
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);
2010-03-02 17:56:21 +00:00
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)
2010-03-02 17:56:21 +00:00
{
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)
2010-03-02 17:56:21 +00:00
{
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<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
if (amount == -1)
{
InputBoxResult result = InputBox.Show("Enter Amount", "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<BillItemKey, BillInventory> bill)
2010-03-02 17:56:21 +00:00
{
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)
{
}
}
}