2011-06-29 20:27:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Tanshu.Accounts.Entities;
|
|
|
|
|
using Tanshu.Accounts.Entities.Auth;
|
|
|
|
|
using NHibernate;
|
|
|
|
|
using System.Linq;
|
2011-08-23 07:10:05 +00:00
|
|
|
|
using Tanshu.Common.Helpers;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
2014-11-20 08:12:20 +00:00
|
|
|
|
public partial class VoucherBI : UnitOfWork<Voucher>
|
2011-06-29 20:27:07 +00:00
|
|
|
|
{
|
2014-11-20 08:12:20 +00:00
|
|
|
|
private IDictionary<SettleOption, decimal> GetSettlementOptions(Voucher voucher)
|
2011-06-29 20:27:07 +00:00
|
|
|
|
{
|
2014-11-20 08:12:20 +00:00
|
|
|
|
IDictionary<SettleOption, decimal> options = new Dictionary<SettleOption, decimal>();
|
|
|
|
|
foreach (var item in voucher.Settlements)
|
2011-06-29 20:27:07 +00:00
|
|
|
|
{
|
2014-11-20 08:12:20 +00:00
|
|
|
|
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);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
}
|
2014-11-20 08:12:20 +00:00
|
|
|
|
return options;
|
2011-06-29 20:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 08:12:20 +00:00
|
|
|
|
public void UpdateSettlements(Voucher voucher, IDictionary<SettleOption, decimal> values)
|
2011-06-29 20:27:07 +00:00
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
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;
|
|
|
|
|
if (roundoff != 0)
|
|
|
|
|
values.Add(SettleOption.RoundOff, roundoff);
|
|
|
|
|
var unsettled = values.Sum(x => x.Value) * -1;
|
|
|
|
|
if (unsettled != 0)
|
|
|
|
|
values.Add(SettleOption.Unsettled, unsettled);
|
|
|
|
|
|
2011-06-29 20:27:07 +00:00
|
|
|
|
for (var i = voucher.Settlements.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
var item = voucher.Settlements[i];
|
2014-11-11 10:23:54 +00:00
|
|
|
|
if (!values.ContainsKey(item.Settled) || (values[item.Settled] == 0 && item.Settled != SettleOption.Amount))
|
2014-10-12 09:41:45 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
voucher.Settlements.Remove(item);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Delete(item);
|
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
else
|
2014-10-12 09:41:45 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
item.Amount = values[item.Settled];
|
2014-10-12 09:41:45 +00:00
|
|
|
|
_session.Update(item);
|
2014-11-11 10:23:54 +00:00
|
|
|
|
values.Remove(item.Settled);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
foreach (var item in values)
|
|
|
|
|
{
|
2014-11-11 10:23:54 +00:00
|
|
|
|
var set = new VoucherSettlement { Settled = item.Key, Amount = item.Value, Voucher = voucher };
|
|
|
|
|
voucher.Settlements.Add(set);
|
|
|
|
|
_session.Save(set);
|
2014-10-12 09:41:45 +00:00
|
|
|
|
}
|
2014-11-20 08:12:20 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2011-06-29 20:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|