Refactor: Refactored the bill inventory in the hope of making it less

error prone and more understandable.
Refactor: Also upgrade path to moving from Price/FullPrice to HasHappyHour/
          IsHappyHour
Must have a few errors.
This commit is contained in:
tanshu
2016-03-29 15:06:46 +05:30
parent 4ac6f66041
commit bb2db24837
9 changed files with 295 additions and 323 deletions

View File

@ -33,7 +33,17 @@ namespace Tanshu.Accounts.PointOfSale
if (_saleForm.BindingSource.Position == -1)
return null;
var item = _bill.ElementAt(_saleForm.BindingSource.Position);
return item.Key.BillItemType == BillItemType.Product ? item.Value : null;
return item.Key.BillItemType != BillItemType.Kot ? item.Value : null;
}
}
public BillItemKey CurrentKey
{
get
{
if (_saleForm.BindingSource.Position == -1)
return null;
var item = _bill.ElementAt(_saleForm.BindingSource.Position);
return item.Key;
}
}
public BillItemKey CurrentKot
@ -60,7 +70,7 @@ namespace Tanshu.Accounts.PointOfSale
if (_bill.ContainsKey(newKey))
{
_saleForm.BindingSource.CurrencyManager.Position = _bill.IndexOfKey(newKey);
_bill[newKey].Quantity += 1;
_bill[newKey].inventory.Quantity += 1;
}
else
{
@ -68,30 +78,29 @@ namespace Tanshu.Accounts.PointOfSale
var old = _bill.FirstOrDefault(x => x.Key.BillItemType == BillItemType.Product && x.Key.ProductID == newKey.ProductID);
if (old.Key != null)
{
billItemValue.Discount = old.Value.Discount;
billItemValue.Price = old.Value.Price;
billItemValue.inventory.Discount = old.Value.inventory.Discount;
billItemValue.inventory.Price = old.Value.inventory.Price;
}
_bill.Add(newKey, billItemValue);
_saleForm.BindingSource.DataSource = _bill.Values.ToList();
_saleForm.BindingSource.DataSource = _bill.ToList();
_saleForm.BindingSource.CurrencyManager.Position = _saleForm.BindingSource.CurrencyManager.Count - 1;
using (var bi = new ProductGroupModifierBI())
if (bi.HasCompulsoryModifier(product.ProductGroup.ProductGroupID))
{
var item = CurrentProduct;
ShowModifiers(item);
ShowModifiers();
}
}
ShowAmount();
}
public void ShowModifiers(BillItemValue item)
public void ShowModifiers()
{
if (item.Printed)
return;
using (var frm = new ModifierForm(Cache.ProductGroupModifiers(item.Product.ProductGroup.ProductGroupID), item.Modifiers))
var item = CurrentProduct;
if (item == null || CurrentKey.KotID != Guid.Empty)
return; // No Product or Old Product
using (var frm = new ModifierForm(Cache.ProductGroupModifiers(item.inventory.Product.ProductGroup.ProductGroupID), item.inventory.InventoryModifier))
{
frm.ShowDialog();
item.Modifiers = frm.Selection;
}
ShowAmount();
}
@ -112,14 +121,14 @@ namespace Tanshu.Accounts.PointOfSale
if (discount > 1 || discount < 0)
return; // throw new ValidationException("Invalid Discount Amount");
foreach (var item in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && outList.Contains(x.Value.Product.ProductGroup.GroupType)))
foreach (var item in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && outList.Contains(x.Value.inventory.Product.ProductGroup.GroupType)))
{
var product = item.Value.Product;
var product = item.Value.inventory.Product;
var maxDiscount = product.ProductGroup.DiscountLimit;
if (discount > item.Value.Product.ProductGroup.DiscountLimit)
if (discount > item.Value.inventory.Product.ProductGroup.DiscountLimit)
MessageBox.Show(string.Format("Maximum discount for {0} is {1:P}", product.Name, maxDiscount),
"Excessive Discount", MessageBoxButtons.OK, MessageBoxIcon.Warning);
item.Value.Discount = discount;
item.Value.inventory.Discount = discount;
}
}
}
@ -129,36 +138,37 @@ namespace Tanshu.Accounts.PointOfSale
public void SetQuantity(decimal quantity, bool prompt)
{
var item = CurrentProduct;
if (!Allowed(item))
return;
if (item.Printed)
return;
var key = CurrentKey;
if (item == null || key.KotID != Guid.Empty)
return; // No Product or Old Product
if (prompt && !GetInput("Quantity", ref quantity))
return;
if (!prompt)
quantity += item.Quantity;
quantity += item.inventory.Quantity;
if (quantity < 0 && !Session.IsAllowed("Edit Printed Product"))
return;
var total = quantity + _bill.Where(x => x.Key.ProductID == item.ProductID && x.Key.BillItemType == BillItemType.Product && x.Value.Printed).Sum(x => x.Value.Quantity);
//TODO: check if he kotid of the item is not null
//var total = quantity + _bill.Where(x => x.Key.ProductID == item.ProductID && x.Key.BillItemType == BillItemType.Product && x.Value.Printed).Sum(x => x.Value.Quantity);
var total = quantity + _bill.Where(x => x.Key.ProductID == key.ProductID && x.Key.BillItemType == key.BillItemType).Sum(x => x.Value.inventory.Quantity);
if (total < 0)
quantity -= total;
item.Quantity = quantity;
item.inventory.Quantity = quantity;
ShowAmount();
}
public void SetRate()
public void SetPrice()
{
var item = CurrentProduct;
if (item == null)
throw new ValidationException("No Product Selected");
if (!Session.IsAllowed("Change Rate"))
throw new PermissionException("Rate Change not Allowed");
var rate = item.Price;
if (!GetInput("Price", ref rate))
throw new PermissionException("Price Change not Allowed");
var price = item.inventory.Price;
if (!GetInput("Price", ref price))
return;
if (rate == 0 && !Session.IsAllowed("NC Product"))
if (price == 0 && !Session.IsAllowed("NC Product"))
throw new PermissionException("NC of Product is not Allowed");
foreach (var sub in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.ProductID == item.ProductID))
sub.Value.Price = rate;
foreach (var sub in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.ProductID == item.inventory.Product.ProductID))
sub.Value.inventory.Price = price;
ShowAmount();
}
@ -198,26 +208,7 @@ namespace Tanshu.Accounts.PointOfSale
}
private void ShowAmount()
{
var taxAmount = _bill.Values.Sum(b => b.ServiceTaxAmount + b.VatAmount);
var discountAmount = _bill.Values.Sum(b => b.DiscountAmount);
var grossAmount = _bill.Values.Sum(b => b.GrossAmount);
var valueAmount = _bill.Values.Sum(b => b.Value);
var serviceChargeAmount = _bill.Values.Sum(b => b.ServiceChargeAmount);
_saleForm.ShowAmount(discountAmount, grossAmount, serviceChargeAmount, taxAmount, valueAmount,
_bill.Values.ToList());
}
private static bool Allowed(BillItemValue item)
{
return item != null;
}
private static void IsPrintedOrVoid(Voucher voucher, out bool isPrinted, out bool isVoid)
{
using (var bi = new VoucherBI())
{
var dbVoucher = bi.Get(x => x.VoucherID == voucher.VoucherID);
isPrinted = dbVoucher.Printed;
isVoid = dbVoucher.Void;
}
_saleForm.ShowAmount(_bill.Discount, _bill.NetAmount, _bill.ServiceCharge, _bill.Tax, _bill.Amount, _bill);
}
private static bool IsPrintedOrVoid(Voucher voucher)
{
@ -277,7 +268,7 @@ namespace Tanshu.Accounts.PointOfSale
public void CancelBillChanges()
{
if (_bill.Values.Any(i => i.Printed == false) &&
if (_bill.Any(x => x.Key.BillItemType != BillItemType.Kot) &&
MessageBox.Show("Abandon Changes?", "Abandon Changes", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
return;
@ -291,7 +282,7 @@ namespace Tanshu.Accounts.PointOfSale
var newKotKey = new BillItemKey(Guid.Empty);
var newKotItem = new BillItemValue();
_bill.Add(newKotKey, newKotItem);
_saleForm.ClearBill(_bill.Values.ToList());
_saleForm.ClearBill(_bill);
}
public SaleFormState FormLoad()
{
@ -312,11 +303,7 @@ namespace Tanshu.Accounts.PointOfSale
}
internal void SettleBill()
{
if (_voucher.VoucherID == Guid.Empty)
return;
if (!_voucher.Printed)
return;
if (!Session.IsAllowed("Settle Bill"))
if (_voucher.VoucherID == Guid.Empty || !_voucher.Printed || !Session.IsAllowed("Settle Bill"))
return;
IDictionary<SettleOption, decimal> options;
var amount = -1 * _voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
@ -353,9 +340,7 @@ namespace Tanshu.Accounts.PointOfSale
}
internal void MoveKot()
{
if (_voucher.VoucherID == Guid.Empty)
return;
if (IsPrintedOrVoid(_voucher))
if (_voucher.VoucherID == Guid.Empty || IsPrintedOrVoid(_voucher))
return;
var kot = CurrentKot;
if (kot == null)
@ -487,7 +472,12 @@ namespace Tanshu.Accounts.PointOfSale
return;
bool isPrinted = false, isVoid = false;
if (_voucher.VoucherID != Guid.Empty)
IsPrintedOrVoid(_voucher, out isPrinted, out isVoid);
using (var bi = new VoucherBI())
{
var dbVoucher = bi.Get(x => x.VoucherID == _voucher.VoucherID);
isPrinted = dbVoucher.Printed;
isVoid = dbVoucher.Void;
}
if (isVoid)
{
MessageBox.Show(string.Format("This Bill is already void.\nReason: {0}", _voucher.VoidReason), "Bill already Voided", MessageBoxButtons.OK, MessageBoxIcon.Error);
@ -497,7 +487,8 @@ namespace Tanshu.Accounts.PointOfSale
return;
#endregion
var amount = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty).Sum(x => x.Value.GrossAmount);
var amount = _bill.NetAmount;
SetDiscount();
if (isPrinted)
{
@ -556,8 +547,8 @@ namespace Tanshu.Accounts.PointOfSale
if (splitList == null || splitList.Count == 0)
return;
var listFirst = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty && splitList.Contains(x.Value.Product.ProductGroup.GroupType));
var listSecond = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty && !splitList.Contains(x.Value.Product.ProductGroup.GroupType));
var listFirst = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty && splitList.Contains(x.Value.inventory.Product.ProductGroup.GroupType));
var listSecond = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty && !splitList.Contains(x.Value.inventory.Product.ProductGroup.GroupType));
if (listFirst.Count() == 0 || listSecond.Count() == 0)
return; // all or none items selected to be moved
@ -661,8 +652,9 @@ namespace Tanshu.Accounts.PointOfSale
}
private void SaveReprintOrDiscountBill(decimal oldAmount)
{
var amountChanged = oldAmount != _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty).Sum(x => x.Value.GrossAmount);
var amountChanged = oldAmount != _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty).Sum(x => x.Value.inventory.Net);
var itemsChanged = _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty).Count() != 0;
if (amountChanged || itemsChanged) // Discount or Products changed
{
#region new voucherFirst
@ -704,7 +696,7 @@ namespace Tanshu.Accounts.PointOfSale
_voucher.Printed = finalBill;
_voucher.Void = false;
_voucher.Narration = "";
var kot = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty && x.Value.Quantity != 0));
var kot = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty && x.Value.inventory.Quantity != 0));
if (kot == null)
return null;
_voucher.Kots.Add(kot);
@ -732,12 +724,12 @@ namespace Tanshu.Accounts.PointOfSale
foreach (var item in _bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID != Guid.Empty))
{
var i = voucher.Kots.Single(x => x.KotID == item.Key.KotID).Inventories.Single(x => x.Product.ProductID == item.Key.ProductID);
i.Discount = item.Value.Discount;
i.Price = item.Value.Price;
i.Discount = item.Value.inventory.Discount;
i.Price = item.Value.inventory.Price;
}
if (!_voucher.Printed)
{
var kot = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty && x.Value.Quantity != 0));
var kot = GetKot(_bill.Where(x => x.Key.BillItemType == BillItemType.Product && x.Key.KotID == Guid.Empty && x.Value.inventory.Quantity != 0));
if (kot != null)
voucher.Kots.Add(kot);
}
@ -760,7 +752,7 @@ namespace Tanshu.Accounts.PointOfSale
var oldInv = kot.Inventories.SingleOrDefault(x => x.Product.ProductID == item.Key.ProductID);
if (oldInv != null)
{
oldInv.Quantity += item.Value.Quantity;
oldInv.Quantity += item.Value.inventory.Quantity;
if (oldInv.Quantity == 0)
kot.Inventories.Remove(oldInv);
}
@ -768,20 +760,20 @@ namespace Tanshu.Accounts.PointOfSale
{
var inv = new Inventory
{
Product = item.Value.Product,
Quantity = item.Value.Quantity,
Price = item.Value.Price,
FullPrice = item.Value.FullPrice,
Discount = item.Value.Discount,
ServiceCharge = item.Value.ServiceCharge,
IsScTaxable = item.Value.IsScTaxable,
ServiceTaxRate = item.Value.ServiceTaxRate,
VatRate = item.Value.VatRate,
ServiceTax = item.Value.ServiceTax,
Vat = item.Value.Vat
Product = item.Value.inventory.Product,
Quantity = item.Value.inventory.Quantity,
Price = item.Value.inventory.Price,
FullPrice = item.Value.inventory.FullPrice,
Discount = item.Value.inventory.Discount,
ServiceCharge = item.Value.inventory.ServiceCharge,
IsScTaxable = item.Value.inventory.IsScTaxable,
ServiceTaxRate = item.Value.inventory.ServiceTaxRate,
VatRate = item.Value.inventory.VatRate,
ServiceTax = item.Value.inventory.ServiceTax,
Vat = item.Value.inventory.Vat
};
foreach (var mod in item.Value.Modifiers)
inv.InventoryModifier.Add(new InventoryModifier { Modifier = mod });
foreach (var mod in item.Value.inventory.InventoryModifier)
inv.InventoryModifier.Add(new InventoryModifier { Modifier = mod.Modifier });
kot.Inventories.Add(inv);
}
}

View File

@ -1,4 +1,5 @@
using Tanshu.Accounts.Contracts;
using System.Linq;
using Tanshu.Accounts.Contracts;
using Tanshu.Common;
using Tanshu.Accounts.Entities;
@ -7,7 +8,8 @@ namespace Tanshu.Accounts.PointOfSale
public class BillDict : OrderedDictionary<BillItemKey, BillItemValue>
{
public delegate void ItemChangedHandler();
public void Load(Voucher voucher) {
public void Load(Voucher voucher)
{
foreach (var kot in voucher.Kots)
{
var kotKey = new BillItemKey(kot.KotID);
@ -16,30 +18,45 @@ namespace Tanshu.Accounts.PointOfSale
foreach (var inv in kot.Inventories)
{
var key = new BillItemKey(inv.Product.ProductID, kot.KotID);
var item = new BillItemValue(inv.Product)
{
ProductID = inv.Product.ProductID,
Discount = inv.Discount,
Name =
inv.Product.Units == string.Empty
? inv.Product.Name
: inv.Product.Name + " (" + inv.Product.Units + ")",
Price = inv.Price,
Printed = true,
Quantity = inv.Quantity,
IsScTaxable = inv.IsScTaxable,
ServiceTaxRate = inv.ServiceTaxRate,
VatRate = inv.VatRate,
ServiceTax = inv.ServiceTax,
Vat = inv.Vat,
ServiceCharge = inv.ServiceCharge,
};
foreach (var mod in inv.InventoryModifier)
item.Modifiers.Add(mod.Modifier);
var item = new BillItemValue(inv);
this.Add(key, item);
}
}
}
public decimal Tax
{
get
{
return this.Where(x => x.Key.BillItemType != BillItemType.Kot).Sum(i => i.Value.inventory.ServiceTaxAmount + i.Value.inventory.VatAmount);
}
}
public decimal Discount
{
get
{
return this.Where(x => x.Key.BillItemType != BillItemType.Kot).Sum(i => i.Value.inventory.DiscountAmount);
}
}
public decimal ServiceCharge
{
get
{
return this.Where(x => x.Key.BillItemType != BillItemType.Kot).Sum(i => i.Value.inventory.ServiceChargeAmount);
}
}
public decimal NetAmount
{
get
{
return this.Where(x => x.Key.BillItemType != BillItemType.Kot).Sum(i => i.Value.inventory.Net);
}
}
public decimal Amount
{
get
{
return this.Where(x => x.Key.BillItemType != BillItemType.Kot).Sum(i => i.Value.inventory.Amount);
}
}
}
}

View File

@ -11,10 +11,10 @@ namespace Tanshu.Accounts.PointOfSale
{
public interface ISaleForm
{
void ClearBill(List<BillItemValue> bill);
void ClearBill(BillDict bill);
void SetCustomerDisplay(string name);
void CloseWindow();
void ShowAmount(decimal discountAmount, decimal grossAmount, decimal serviceChargeAmount, decimal taxAmount, decimal valueAmount, List<BillItemValue> bill);
void ShowAmount(decimal discountAmount, decimal grossAmount, decimal serviceChargeAmount, decimal taxAmount, decimal valueAmount, BillDict bill);
void ShowInfo(Voucher voucher);
void SetUserName(string name);
BindingSource BindingSource { get; }