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

259 lines
10 KiB
C#
Raw Permalink Normal View History

2010-03-02 17:56:21 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Tanshu.Common;
using Tanshu.Accounts.BI;
using Tanshu.Accounts.Contracts;
using System.Windows.Forms;
using System.Threading;
namespace Tanshu.Accounts.PointOfSale
{
public static class BillHelperFunctions
{
#region Discount
public static void SetDiscount(Guid productID, decimal discount, CustomerBO customer, Dictionary<BillItemKey, SalesBillItemBO> bill)
{
#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;
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.ProductDAO(connection))
{
maxDiscount = dao.GetProductDiscountLimit(productID);
}
}
if (discount >= 1)
{
MessageBox.Show(string.Format("Discount cannot be 100% or more", maxDiscount), "100% Discount", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2011-12-05 09:23:02 +00:00
if (discount > maxDiscount)
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
if (Thread.CurrentPrincipal.IsInRole("High Discount Allowed"))
{
MessageBox.Show(string.Format("Maximum discount for this product is {0:P}", maxDiscount), "Excessive Discount", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show(string.Format("Maximum discount for this product is {0:P} Discount Disallowed", maxDiscount), "Excessive Discount", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2010-03-02 17:56:21 +00:00
}
#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(SalesBillItemBO product, decimal discount)
{
product.Discount = discount;
}
#endregion
#region Add Product
public static SalesBillItemBO AddProductToGrid(Guid productID, BindingSource bindingSource, Dictionary<BillItemKey, SalesBillItemBO> bill)
{
SalesBillItemBO product;
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 SalesBillItemBO AddNewProduct(Guid productID, BindingSource bindingSource, Dictionary<BillItemKey, SalesBillItemBO> bill)
{
BillItemKey key = new BillItemKey(productID, true);
SalesBillItemBO product;
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.ProductDAO(connection))
{
product = dao.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, Dictionary<BillItemKey, SalesBillItemBO> 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(SalesBillItemBO product, decimal quantity, bool absolute, bool prompt, BindingSource bindingSource, Dictionary<BillItemKey, SalesBillItemBO> bill)
{
#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)))
{
2011-12-05 09:23:02 +00:00
var 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)
{
SalesBillItemBO 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(SalesBillItemBO 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(SalesBillItemBO 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(SalesBillItemBO product, decimal amount, BindingSource bindingSource, Dictionary<BillItemKey, SalesBillItemBO> bill)
{
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.Vat + product.ServiceTax) * (1 - product.Discount)), true, false, bindingSource, bill);
2010-03-02 17:56:21 +00:00
}
}
#endregion
#region Rate
public static void SetRate(Guid productID, decimal rate, Dictionary<BillItemKey, SalesBillItemBO> 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)
{
}
}
}