Lot of changes. Now table must be explicitly updated and not from inside various functions. This makes for better understanding and lesser mistakes.

Product form now has an IsActive checkbox to show only active products.
This commit is contained in:
tanshu
2014-11-20 13:42:20 +05:30
parent e3f92da5ad
commit b1a9d2daae
9 changed files with 262 additions and 304 deletions

View File

@ -17,6 +17,7 @@ namespace Tanshu.Accounts.Repository
{
var list = _session.QueryOver<Product>()
.Where(query)
.OrderBy(x => x.ProductGroup).Asc
.OrderBy(x => x.SortOrder).Asc
.ThenBy(x => x.Name).Asc
.List();

View File

@ -9,7 +9,7 @@ using Tanshu.Accounts.Contracts;
namespace Tanshu.Accounts.Repository
{
public class VoucherBI : UnitOfWork<Voucher>
public partial class VoucherBI : UnitOfWork<Voucher>
{
public new Guid Insert(Voucher voucher)
{
@ -38,30 +38,10 @@ namespace Tanshu.Accounts.Repository
}
}
}
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount);
foreach (var settlement in voucher.Settlements.Where(x => x.Amount != 0 || x.Settled == SettleOption.Amount))
{
settlement.Voucher = voucher;
_session.Save(settlement);
}
var options = GetSettlementOptions(voucher);
UpdateSettlements(voucher, options);
return addedKot == null ? Guid.Empty : addedKot.KotID;
}
public Guid Insert(Voucher voucher, bool updateTable)
{
var kotID = Insert(voucher);
if (updateTable)
{
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == voucher.Table.FoodTableID).SingleOrDefault();
if (table.VoucherID.HasValue)
throw new ValidationException("A bill exists on this table, cannot overwrite");
var status = !voucher.Printed ? "running" : "printed";
table.VoucherID = voucher.VoucherID;
table.Status = status;
_session.Update(table);
}
return kotID;
}
public new Guid? Update(Voucher voucher)
{
_session.Update(voucher);
@ -90,36 +70,10 @@ namespace Tanshu.Accounts.Repository
}
}
UpdateBillType(voucher);
var amount = -1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount));
VoucherSettlementBI.UpdateSettlements(voucher.Settlements, amount);
foreach (var settlement in voucher.Settlements)
{
settlement.Voucher = voucher;
if (settlement.Amount == 0)
{
if (settlement.VoucherSettlementID != Guid.Empty)
_session.Delete(settlement);
}
else if (settlement.VoucherSettlementID == Guid.Empty)
_session.Save(settlement);
else
_session.Update(settlement);
}
var options = GetSettlementOptions(voucher);
UpdateSettlements(voucher, options);
return addedKot == null ? (Guid?)null : addedKot.KotID;
}
public Guid? Update(Voucher voucher, bool updateTable)
{
var kotID = Update(voucher);
if (updateTable)
{
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == voucher.Table.FoodTableID).SingleOrDefault();
var status = !voucher.Printed ? "running" : "printed";
table.VoucherID = voucher.VoucherID;
table.Status = status;
_session.Update(table);
}
return kotID;
}
public new Voucher Get(Expression<Func<Voucher, bool>> query)
{
var voucher = _session.QueryOver<Voucher>()
@ -148,20 +102,22 @@ namespace Tanshu.Accounts.Repository
NHibernateUtil.Initialize(item);
return voucher;
}
public void VoidBill(Guid voucherID, string reason, bool updateTable)
public void VoidBill(Guid voucherID, string reason)
{
var voucher = _session.Get<Voucher>(voucherID);
voucher.Void = true;
voucher.VoidReason = reason;
_session.Update(voucher);
}
if (updateTable)
{
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == voucher.Table.FoodTableID).SingleOrDefault();
table.VoucherID = null;
table.Status = null;
_session.Update(table);
}
public void UpdateTable(Expression<Func<FoodTable, bool>> query, Guid? newVoucherID, string status)
{
var table = _session.QueryOver<FoodTable>().Where(query).SingleOrDefault();
if (table == null)
throw new ValidationException("Old Voucher on the table does not match");
table.Status = status;
table.VoucherID = newVoucherID;
_session.Update(table);
}
public Guid MergeKot(Guid kotID, string tableName)
@ -197,15 +153,11 @@ namespace Tanshu.Accounts.Repository
_session.Update(newVoucher);
var oldTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == oldVoucher.Table.FoodTableID).SingleOrDefault();
foreach (var item in oldVoucher.Settlements)
{
_session.Delete(item);
}
_session.Delete(oldVoucher);
oldTable.VoucherID = null;
oldTable.Status = null;
_session.Update(oldTable);
return newVoucher.VoucherID;
}
private void UpdateBillType(Voucher voucher)
@ -253,10 +205,6 @@ namespace Tanshu.Accounts.Repository
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == tableID).SingleOrDefault();
var newVoucher = new Voucher(Session.User, oldVoucher.Customer, table, oldVoucher.Waiter, false, false, "");
Insert(newVoucher);
table.VoucherID = newVoucher.VoucherID;
table.Status = "running";
_session.Update(table);
oldVoucher.Kots.Remove(kot);
kot.Voucher = newVoucher;
@ -279,20 +227,6 @@ namespace Tanshu.Accounts.Repository
Insert(first);
Insert(second);
Update(oldVoucher);
var status = oldVoucher.Printed ? "printed" : "running";
var tableFirst = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == first.Table.FoodTableID).SingleOrDefault();
if (tableFirst.VoucherID.HasValue)
throw new ValidationException("A bill exists on this table, cannot overwrite");
tableFirst.VoucherID = first.VoucherID;
tableFirst.Status = status;
_session.Update(tableFirst);
var tableSecond = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == second.Table.FoodTableID).SingleOrDefault();
tableSecond.VoucherID = second.VoucherID;
tableSecond.Status = status;
_session.Update(tableFirst);
}
public void DiscountPrintedBill(Guid oldVoucherID, Voucher newVoucher)
@ -304,30 +238,13 @@ namespace Tanshu.Accounts.Repository
Insert(newVoucher);
Update(oldVoucher);
var table = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == oldVoucher.Table.FoodTableID).SingleOrDefault();
table.VoucherID = newVoucher.VoucherID;
table.Status = "printed";
_session.Update(table);
}
public Guid Move(Guid oldTableID, Guid newTableID)
public void MoveTable(Guid voucherID, Guid tableID)
{
var oldTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == oldTableID).SingleOrDefault();
var newTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == newTableID).SingleOrDefault();
var voucher = _session.Get<Voucher>(oldTable.VoucherID);
newTable.Status = oldTable.Status;
newTable.VoucherID = oldTable.VoucherID;
oldTable.Status = null;
oldTable.VoucherID = null;
var voucher = _session.Get<Voucher>(voucherID);
var newTable = _session.QueryOver<FoodTable>().Where(x => x.FoodTableID == tableID).SingleOrDefault();
voucher.Table = newTable;
_session.Update(newTable);
_session.Update(oldTable);
_session.Update(voucher);
return voucher.VoucherID;
}
}
}

View File

@ -8,35 +8,25 @@ using Tanshu.Common.Helpers;
namespace Tanshu.Accounts.Repository
{
public class VoucherSettlementBI : UnitOfWork<VoucherSettlement>
public partial class VoucherBI : UnitOfWork<Voucher>
{
public static void UpdateSettlements(IList<VoucherSettlement> list, decimal amount)
private IDictionary<SettleOption, decimal> GetSettlementOptions(Voucher voucher)
{
amount = Math.Round(amount, 5);
if (list.Count(x => x.Settled == SettleOption.Amount) == 0)
list.Add(new VoucherSettlement() { Amount = amount, Settled = SettleOption.Amount });
else
list.Single(x => x.Settled == SettleOption.Amount).Amount = amount;
var roundoff = Math.Round(amount) - amount;
if (list.Count(x => x.Settled == SettleOption.RoundOff) == 0)
list.Add(new VoucherSettlement() { Amount = roundoff, Settled = SettleOption.RoundOff });
else
list.Single(x => x.Settled == SettleOption.RoundOff).Amount = roundoff;
var balance = list.Where(x => x.Settled != SettleOption.Unsettled).Sum(x => x.Amount) * -1;
if (list.Count(x => x.Settled == SettleOption.Unsettled) == 0)
IDictionary<SettleOption, decimal> options = new Dictionary<SettleOption, decimal>();
foreach (var item in voucher.Settlements)
{
if (balance != 0)
list.Add(new VoucherSettlement() { Amount = balance, Settled = SettleOption.Unsettled });
if (item.Settled == SettleOption.Amount || item.Settled == SettleOption.RoundOff || item.Settled == SettleOption.Unsettled)
continue;
if (options.ContainsKey(item.Settled))
options[item.Settled] += item.Amount;
else
options.Add(item.Settled, item.Amount);
}
else
list.Single(x => x.Settled == SettleOption.Unsettled).Amount = balance;
return options;
}
public void SettleVoucher(User user, Guid voucherID, IDictionary<SettleOption, decimal> values)
public void UpdateSettlements(Voucher voucher, IDictionary<SettleOption, decimal> values)
{
var voucher = _session.Get<Voucher>(voucherID);
var amount = Math.Round(-1 * voucher.Kots.Sum(x => x.Inventories.Sum(y => y.Amount)), 5);
values.Add(SettleOption.Amount, Math.Round(amount, 5));
var roundoff = Math.Round(amount) - amount;
@ -46,21 +36,6 @@ namespace Tanshu.Accounts.Repository
if (unsettled != 0)
values.Add(SettleOption.Unsettled, unsettled);
UpdateOld(voucher, values);
AddNew(voucher, values);
voucher.User = user;
_session.Update(voucher);
var table = _session.QueryOver<FoodTable>()
.Where(x => x.FoodTableID == voucher.Table.FoodTableID && x.VoucherID == voucher.VoucherID)
.SingleOrDefault();
table.VoucherID = null;
table.Status = null;
_session.Update(table);
}
private void UpdateOld(Voucher voucher, IDictionary<SettleOption, decimal> values)
{
for (var i = voucher.Settlements.Count - 1; i >= 0; i--)
{
var item = voucher.Settlements[i];
@ -76,15 +51,21 @@ namespace Tanshu.Accounts.Repository
values.Remove(item.Settled);
}
}
}
private void AddNew(Voucher voucher, IDictionary<SettleOption, decimal> values)
{
foreach (var item in values)
{
var set = new VoucherSettlement { Settled = item.Key, Amount = item.Value, Voucher = voucher };
voucher.Settlements.Add(set);
_session.Save(set);
}
}
public void SettleVoucher(User user, Guid voucherID, IDictionary<SettleOption, decimal> values)
{
var voucher = _session.Get<Voucher>(voucherID);
UpdateSettlements(voucher, values);
voucher.User = user;
_session.Update(voucher);
}
}
}