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

142 lines
4.7 KiB
C#

using System.Linq;
using System.Windows.Forms;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Repository;
using Tanshu.Common;
namespace Tanshu.Accounts.PointOfSale.Sales
{
public class BillHelperFunctions
{
private readonly OrderedDictionary<BillItemKey, BillInventory> bill;
private readonly BindingSource _bindingSource;
private readonly BillItemKey _newKey;
public BillHelperFunctions(BindingSource bindingSource, OrderedDictionary<BillItemKey, BillInventory> bill,
int productID)
{
this._bindingSource = bindingSource;
this.bill = bill;
_newKey = new BillItemKey(productID, 0);
}
public void SetDiscount(string name, decimal discount)
{
if (discount > 1 || discount < 0)
return;
decimal maxDiscount = VoucherBI.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);
foreach (var item in bill)
{
if (item.Value.ProductID == _newKey.ProductID)
item.Value.Discount = discount;
}
}
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;
foreach (var item in bill)
{
if (item.Value.ProductID == _newKey.ProductID)
item.Value.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;
}
#region Add Product
public BillInventory AddProduct()
{
BillInventory product;
if (bill.ContainsKey(_newKey))
{
_bindingSource.CurrencyManager.Position = bill.IndexOfKey(_newKey);
product = bill[_newKey];
product.Quantity += 1;
}
else
{
//Has only old
product = VoucherBI.GetDefaultSaleBillItem(_newKey.ProductID);
foreach (var item in bill)
{
if (item.Key.ProductID == _newKey.ProductID)
{
product.Discount = item.Value.Discount;
product.Price = item.Value.Price;
}
}
product = AddProduct(product);
}
return product;
}
private BillInventory AddProduct(BillInventory product)
{
bill.Add(new BillItemKey(product.ProductID, 0), product);
_bindingSource.DataSource = bill.Values.ToList();
_bindingSource.CurrencyManager.Position = _bindingSource.CurrencyManager.Count - 1;
return product;
}
#endregion
#region Quantity
public void SetQuantity(BillInventory product, decimal quantity, bool prompt)
{
if (product.Printed)
return;
if (prompt && !GetInput("Price", ref quantity))
return;
if (!prompt)
quantity += product.Quantity;
SetQuantity(product, quantity);
}
private void SetQuantity(BillInventory product, decimal quantity)
{
if (quantity < 0 && !Session.IsAllowed(RoleConstants.EDIT_PRINTED_PRODUCT))
return;
var total = quantity + bill.Where(item => item.Value.ProductID == _newKey.ProductID).Sum(item => item.Value.Quantity);
if (total < 0)
quantity -= total;
product.Quantity = quantity;
}
#endregion
}
}