narsil/Tanshu.Accounts.Repository/VoucherSettlementBI.cs
tanshu 3ca8b29e04 Regression: BillItemKey added the compare methods back
Regression: PrintLocation added the compare methods back
Breaking: Kot.Code is now integers
Breaking: Kot Update is now via Stored Procedure to get DB Values
Breaking: Reprints Insert is now via Stored Procedure to get DV Values
Breaking: Voucher.BillID and KotID are now integers
Breaking: Voucher Insert/Update is now via Stored Procedures to get DV Values also Dirty Checking for Voucher has been overwritten to set dirty for LastEditDate update
Fix: Login forms simplified
Feature: PrintLocation and Products are cached application wide.
2014-11-02 13:33:31 +05:30

84 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
using NHibernate;
using System.Linq;
using Tanshu.Common.Helpers;
namespace Tanshu.Accounts.Repository
{
public class VoucherSettlementBI : UnitOfWork<VoucherSettlement>
{
public static void UpdateSettlements(IList<VoucherSettlement> list, decimal amount)
{
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)
{
if (balance != 0)
list.Add(new VoucherSettlement() { Amount = balance, Settled = SettleOption.Unsettled });
}
else
list.Single(x => x.Settled == SettleOption.Unsettled).Amount = balance;
}
public void SettleVoucher(User user, Guid voucherID, IDictionary<SettleOption, decimal> values)
{
var voucher = _session.Get<Voucher>(voucherID);
for (var i = voucher.Settlements.Count - 1; i >= 0; i--)
{
var item = voucher.Settlements[i];
if (item.Settled == SettleOption.Amount || item.Settled == SettleOption.RoundOff || item.Settled == SettleOption.Unsettled)
_session.Update(item);
if (!values.ContainsKey(item.Settled))
{
voucher.Settlements.Remove(item);
_session.Delete(item);
}
else
{
item.Amount = values[item.Settled];
_session.Update(item);
}
}
foreach (var item in values)
{
if (voucher.Settlements.Count(x => x.Settled == item.Key) == 0)
{
var set = new VoucherSettlement { Settled = item.Key, Amount = item.Value, Voucher = voucher };
voucher.Settlements.Add(set);
_session.Save(set);
}
}
voucher.User = user;
_session.Update(voucher);
if (voucher.Settlements.Count(x => x.Settled == SettleOption.Unsettled) == 0 || voucher.Void)
{
var table = _session.QueryOver<FoodTable>()
.Where(x => x.FoodTableID == voucher.Table.FoodTableID && x.VoucherID == voucher.VoucherID)
.SingleOrDefault();
if (table != null)
{
table.VoucherID = null;
table.Status = null;
_session.Update(table);
}
}
}
}
}