using DS.Module.Core;
using DS.WMS.Core.Fee.Entity;
namespace DS.WMS.Core.Fee.Dtos
{
///
/// 费用汇总统计模型
///
public class FeeStatistics
{
//默认本币
static readonly string LocalCurrency = "CNY";
readonly IEnumerable _source;
///
/// 使用指定的数据源初始化统计。
///
/// 数据源
public FeeStatistics(IEnumerable source)
{
_source = source;
var localReceivable = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency == LocalCurrency).Sum(x => x.Amount).GetValueOrDefault();
//其他币种按汇率转换为本币金额,下同
var otherReceivable = (from s in _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency != LocalCurrency)
group s by new { s.Currency, s.ExchangeRate } into g
select new
{
g.Key,
Items = g
}).Sum(x => x.Items.Sum(y => y.Amount * x.Key.ExchangeRate)).GetValueOrDefault();
ReceivableTotal = localReceivable + otherReceivable;//应收合计
var localPayable = _source.Where(x => x.FeeType == FeeType.Payable).Sum(x => x.Amount).GetValueOrDefault();
var otherPayable = (from s in _source.Where(x => x.FeeType == FeeType.Payable && x.Currency != LocalCurrency)
group s by new { s.Currency, s.ExchangeRate } into g
select new
{
g.Key,
Items = g
}).Sum(x => x.Items.Sum(y => y.Amount * x.Key.ExchangeRate)).GetValueOrDefault();
PayableTotal = localPayable + otherPayable;//应付合计
var localNoTaxReceivable = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency == LocalCurrency).Sum(x => x.NoTaxAmount);
var otherNoTaxReceivable = (from s in _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency != LocalCurrency)
group s by new { s.Currency, s.ExchangeRate } into g
select new
{
g.Key,
Items = g
}).Sum(x => x.Items.Sum(y => y.NoTaxAmount * x.Key.ExchangeRate)).GetValueOrDefault();
NoTaxReceivableTotal = localNoTaxReceivable + NoTaxReceivableTotal;
var localNoTaxPayable = _source.Where(x => x.FeeType == FeeType.Payable && x.Currency == LocalCurrency).Sum(x => x.NoTaxAmount);
var otherNoTaxPayable = (from s in _source.Where(x => x.FeeType == FeeType.Payable && x.Currency != LocalCurrency)
group s by new { s.Currency, s.ExchangeRate } into g
select new
{
g.Key,
Items = g
}).Sum(x => x.Items.Sum(y => y.NoTaxAmount * x.Key.ExchangeRate)).GetValueOrDefault();
NoTaxPayableTotal = localNoTaxPayable + otherNoTaxPayable;
//人民币
ReceivableCNY = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency == "CNY").Sum(x => x.Amount).GetValueOrDefault();
PayableCNY = _source.Where(x => x.FeeType == FeeType.Payable && x.Currency == "CNY").Sum(x => x.Amount).GetValueOrDefault();
NoTaxReceivableCNY = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency == "CNY").Sum(x => x.NoTaxAmount);
NoTaxPayableCNY = _source.Where(x => x.FeeType == FeeType.Payable && x.Currency == "CNY").Sum(x => x.NoTaxAmount);
//美元
ReceivableUSD = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency == "USD").Sum(x => x.Amount).GetValueOrDefault();
PayableUSD = _source.Where(x => x.FeeType == FeeType.Payable && x.Currency == "USD").Sum(x => x.Amount).GetValueOrDefault();
NoTaxReceivableUSD = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency == "USD").Sum(x => x.NoTaxAmount);
NoTaxPayableUSD = _source.Where(x => x.FeeType == FeeType.Payable && x.Currency == "USD").Sum(x => x.NoTaxAmount);
//其他
ReceivableOther = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency != "USD" && x.Currency != "CNY").Sum(x => x.Amount).GetValueOrDefault();
PayableOther = _source.Where(x => x.FeeType == FeeType.Payable && x.Currency == "USD" && x.Currency != "CNY").Sum(x => x.Amount).GetValueOrDefault();
NoTaxReceivableOther = _source.Where(x => x.FeeType == FeeType.Receivable && x.Currency == "USD" && x.Currency != "CNY").Sum(x => x.NoTaxAmount);
NoTaxPayableOther = _source.Where(x => x.FeeType == FeeType.Payable && x.Currency == "USD" && x.Currency != "CNY").Sum(x => x.NoTaxAmount);
//按客户统计
ByCustomers = (from s in _source
group s by s.CustomerName into g
select new CustomerFeeStatistics
{
CustomerName = g.Key,
ReceivableTotal = g.Where(x => x.FeeType == FeeType.Receivable).Sum(x => x.Amount).GetValueOrDefault(),
PayableTotal = g.Where(x => x.FeeType == FeeType.Payable).Sum(x => x.Amount).GetValueOrDefault(),
ReceivableCNY = g.Where(x => x.FeeType == FeeType.Receivable && x.Currency == "CNY").Sum(x => x.Amount).GetValueOrDefault(),
PayableCNY = g.Where(x => x.FeeType == FeeType.Payable && x.Currency == "CNY").Sum(x => x.Amount).GetValueOrDefault(),
ReceivableUSD = g.Where(x => x.FeeType == FeeType.Receivable && x.Currency == "USD").Sum(x => x.Amount).GetValueOrDefault(),
PayableUSD = g.Where(x => x.FeeType == FeeType.Payable && x.Currency == "USD").Sum(x => x.Amount).GetValueOrDefault()
}).ToList();
}
///
/// 应收款总计
///
public decimal ReceivableTotal { get; private set; }
///
/// 不含税应收款总计
///
public decimal NoTaxReceivableTotal { get; private set; }
///
/// 应付款总计
///
public decimal PayableTotal { get; private set; }
///
/// 不含税应付款总计
///
public decimal NoTaxPayableTotal { get; private set; }
///
/// 利润总计
///
public decimal ProfitTotal { get { return ReceivableTotal - PayableTotal; } }
///
/// 利润率
///
public string ProfitMargin
{
get
{
if (PayableTotal == 0)
return string.Empty;
//利润率=利润÷成本×100%
return string.Concat((ReceivableTotal / PayableTotal) * 100, "%");
}
}
///
/// 不含税利润总计
///
public decimal NoTaxProfitTotal { get { return NoTaxReceivableTotal - NoTaxPayableTotal; } }
///
/// 人民币应收款
///
public decimal ReceivableCNY { get; private set; }
///
/// 不含税人民币应收款
///
public decimal NoTaxReceivableCNY { get; private set; }
///
/// 人民币应付款
///
public decimal PayableCNY { get; private set; }
///
/// 不含税人民币应付款
///
public decimal NoTaxPayableCNY { get; private set; }
///
/// 人民币利润
///
public decimal ProfitCNY { get { return ReceivableCNY - PayableCNY; } }
///
/// 不含税人民币利润
///
public decimal NoTaxProfitCNY { get { return NoTaxReceivableCNY - NoTaxPayableCNY; } }
///
/// 美元应收款
///
public decimal ReceivableUSD { get; private set; }
///
/// 不含税美元应收款
///
public decimal NoTaxReceivableUSD { get; private set; }
///
/// 美元应付款
///
public decimal PayableUSD { get; private set; }
///
/// 不含税美元应付款
///
public decimal NoTaxPayableUSD { get; private set; }
///
/// 美元利润
///
public decimal ProfitUSD { get { return ReceivableUSD - PayableUSD; } }
///
/// 不含税美元利润
///
public decimal NoTaxProfitUSD { get { return NoTaxReceivableUSD - NoTaxPayableUSD; } }
///
/// 其他币种应收款
///
public decimal ReceivableOther { get; private set; }
///
/// 不含税其他币种应收款
///
public decimal NoTaxReceivableOther { get; private set; }
///
/// 其他币种应付款
///
public decimal PayableOther { get; private set; }
///
/// 不含税其他币种应付款
///
public decimal NoTaxPayableOther { get; private set; }
///
/// 其他币种利润
///
public decimal ProfitOther { get { return ReceivableOther - PayableOther; } }
///
/// 不含税其他币种利润
///
public decimal NoTaxProfitOther { get { return NoTaxReceivableOther - NoTaxPayableOther; } }
///
/// 按客户统计
///
public List ByCustomers { get; private set; }
}
///
/// 费用记录按客户统计
///
public class CustomerFeeStatistics
{
///
/// 客户名称
///
public string CustomerName { get; set; }
///
/// 应收款总计
///
public virtual decimal ReceivableTotal { get; set; }
///
/// 应付款总计
///
public virtual decimal PayableTotal { get; set; }
///
/// 人民币应收款
///
public virtual decimal ReceivableCNY { get; set; }
///
/// 人民币应付款
///
public virtual decimal PayableCNY { get; set; }
///
/// 美元应收款
///
public virtual decimal ReceivableUSD { get; set; }
///
/// 美元应付款
///
public virtual decimal PayableUSD { get; set; }
}
public class FeeStatisticsRequest
{
///
/// 查询条件
///
public string QueryCondition { get; set; }
}
}