69617949bd
Important! : This version will not work. It is pre-alpha and saved in case of catastrophic failure Refactor: Remove dependency on Fluent Nhibernate. Refactor: All Primary keys are now Guids. Refactor: Class Mappings changed from AutoMap to Explicit Mappings. Breakage: All Cascading is now disabled and entities must be explicitly saved/updated/deleted Breakage: Auto Commiting is now off and "SaveChanges()" needs to be called on all BIs. Refactor: Changed the pattern where all relevant db code for an operation is basically in the same function. Chore: Removed Advance and Payments options.
85 lines
3.4 KiB
C#
85 lines
3.4 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;
|
|
voucher.LastEditDate = DbValues.Date;
|
|
_session.Update(voucher);
|
|
|
|
if (voucher.Settlements.Count(x => x.Settled == SettleOption.Unsettled) == 0 || voucher.Void)
|
|
{
|
|
var table = _session.QueryOver<FoodTable>()
|
|
.Where(x => x.Name == voucher.TableID && x.VoucherID == voucher.VoucherID)
|
|
.SingleOrDefault();
|
|
if (table != null)
|
|
{
|
|
table.VoucherID = null;
|
|
table.Status = null;
|
|
_session.Update(table);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|