You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
9.3 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Enums;
using DS.Module.Core.Extensions;
using DS.WMS.Core.Application.Dtos;
using DS.WMS.Core.Application.Entity;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Settlement.Dtos;
using DS.WMS.Core.Settlement.Entity;
using DS.WMS.Core.Settlement.Interface;
using DS.WMS.Core.Sys.Entity;
using SqlSugar;
namespace DS.WMS.Core.Settlement.Method
{
/// <summary>
/// 付费结算服务
/// </summary>
public class PaymentSettlementService : SettlementService<PaymentSettlement>, IPaymentSettlementService
{
/// <summary>
/// 初始化
/// </summary>
/// <param name="provider"></param>
public PaymentSettlementService(IServiceProvider provider) : base(provider)
{
}
/// <summary>
/// 获取分页列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DataResult<List<PaymentSettlementDto>>> GetListAsync(PageRequest request)
{
var query = TenantDb.Queryable<PaymentSettlement>().Select(x => new PaymentSettlementDto
{
Id = x.Id,
ApplicationNO = x.ApplicationNO, //申请编号
SettlementNO = x.SettlementNO, //结算单号
CustomerId = x.CustomerId, //结算单位
CustomerName = x.CustomerName,
Mode = x.Mode, //结算类型
SettlementTypeName = x.SettlementType.StlName, //结算方式
RMBAmount = SqlFunc.Subqueryable<ApplicationDetail>().Where(y => y.ApplicationId == x.Id && y.Currency == RMB_CODE)
.Select(y => SqlFunc.AggregateSum(y.ApplyAmount)),
USDAmount = SqlFunc.Subqueryable<ApplicationDetail>().Where(y => y.ApplicationId == x.Id && y.Currency == USD_CODE)
.Select(y => SqlFunc.AggregateSum(y.ApplyAmount)),
OtherAmount = SqlFunc.Subqueryable<ApplicationDetail>().Where(y => y.ApplicationId == x.Id && y.Currency != RMB_CODE && y.Currency != USD_CODE)
.Select(y => SqlFunc.AggregateSum(y.ApplyAmount)),
Note = x.Note, //备注
CustomerAccount = x.CustomerBank.AccountName + "/" + x.CustomerBank.Currency,
CustomerBank = x.CustomerBank.BankName,
//未开票
UnInvoiceList = SqlFunc.Subqueryable<ApplicationDetail>().InnerJoin<FeeRecord>((d, f) => d.RecordId == f.Id && (f.Amount - f.InvoiceAmount - f.OrderInvoiceAmount) != 0)
.GroupBy((d, f) => f.Currency).ToList((d, f) => new CurrencyAmount { Currency = f.Currency, Amount = f.Amount - f.InvoiceAmount - f.OrderInvoiceAmount }),
CreateBy = x.CreateBy, //结算人
CreateTime = x.CreateTime, //创建日期
SettlementDate = x.SettlementDate, //结算日期
IsExportVoucher = default, //是否导出凭证
Currency = x.Currency, //币别
Amount = x.Amount, //原始金额
AccountAmount = x.AccountAmount, //记账资料
PrePayAmount = x.PrePayAmount, //预付支资料
AHSRAmount = x.AHSRAmount, //实收支资料
FinancialAmount = x.FinancialAmount,//财务费用
AdvanceAmount = x.AdvanceAmount, //预收支资料
IsLocked = x.IsLocked, //锁定状态
LockTime = x.LockTime,
LockUserId = x.LockUserId,
UnlockTime = x.UnlockTime,
UnlockUserId = x.UnlockUserId,
IsVoucherDisabled = x.IsVoucherDisabled, //禁用凭证
SaleDeptId = x.SaleDeptId, //所属分部
LedgerVoucherNO = x.LedgerVoucherNO //总账凭证号
//BillType = x.BillType, //单据类型
//Category = x.Category, //业务类别
});
if (!request.QueryCondition.IsNullOrEmpty())
{
var whereList = Db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
query = query.Where(whereList);
}
var result = await query.ToQueryPageAsync(request.PageCondition);
if (result.Data.Count > 0)
{
//关联用户名称
var userIds = result.Data.Select(x => x.CreateBy)
.Union(result.Data.Where(x => x.LockUserId.HasValue).Select(x => x.LockUserId.Value))
.Union(result.Data.Where(x => x.UnlockUserId.HasValue).Select(x => x.UnlockUserId.Value))
.Distinct();
var users = await Db.Queryable<SysUser>().Where(x => userIds.Contains(x.Id)).Select(x => new { x.Id, x.UserName }).ToListAsync();
var orgIds = result.Data.Select(x => x.SaleDeptId).Distinct().ToList();
var orgs = await Db.Queryable<SysOrg>().Where(x => orgIds.Contains(x.Id)).Select(x => new { x.Id, x.OrgName }).ToListAsync();
foreach (var item in result.Data)
{
item.CreateByName = users.Find(x => x.Id == item.CreateBy)?.UserName;
item.LockUser = users.Find(x => x.Id == item.LockUserId)?.UserName;
item.UnlockUser = users.Find(x => x.Id == item.UnlockUserId)?.UserName;
item.SaleDeptName = orgs.Find(x => x.Id == item.SaleDeptId)?.OrgName;
}
}
return result;
}
/// <summary>
/// 获取付费申请分页列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DataResult<List<PaymentApplicationDto>>> GetApplicationListAsync(PageRequest request)
{
var query = TenantDb.Queryable<PaymentApplication>().Where(x => x.Status == PaymentApplicationStatus.AuditPassed)
.InnerJoin<ApplicationDetail>((a, d) => a.Id == d.ApplicationId)
//未结算金额=(金额-已结算金额-申请金额+申请金额已结算) != 0
.InnerJoin<FeeRecord>((a, d, f) => d.RecordId == f.Id && (f.Amount - f.SettlementAmount - d.ApplyAmount + f.OrderSettlementAmount) != 0)
.Select(a => new PaymentApplicationDto
{
Id = a.Id,
ApplicationNO = a.ApplicationNO,
Status = a.Status,
CustomerId = a.CustomerId,
CustomerName = a.CustomerName, //结算单位
CustomerBankId = a.CustomerBankId,
CustomerBank = a.CustomerBank.BankName, //结算对象银行
CustomerAccount = a.CustomerBank.Account, //结算对象账号
Currency = a.Currency,
AmountRMB = a.AmountRMB, //RMB申请金额
AmountUSD = a.AmountUSD, //USD申请金额
//RMB未结金额
UnSettlementRMB = SqlFunc.Subqueryable<ApplicationDetail>().InnerJoin<FeeRecord>((d, f) => d.RecordId == f.Id).Where(d => d.Currency == RMB_CODE)
.Select((d, f) => SqlFunc.AggregateSum(f.Amount - f.SettlementAmount - d.ApplyAmount + f.OrderSettlementAmount)),
//USD未结金额
UnSettlementUSD = SqlFunc.Subqueryable<ApplicationDetail>().InnerJoin<FeeRecord>((d, f) => d.RecordId == f.Id).Where(d => d.Currency == USD_CODE)
.Select((d, f) => SqlFunc.AggregateSum(f.Amount - f.SettlementAmount - d.ApplyAmount + f.OrderSettlementAmount)),
SaleDeptId = a.SaleDeptId, //所属分部
SettlementTypeId = a.SettlementTypeId,
SettlementTypeName = a.SettlementType.StlName, //结算方式
CreateBy = a.CreateBy, //申请人
CreateTime = a.CreateTime, //申请日期
PayAmount = 0, //审核实付金额
PaymentDate = a.PaymentDate, //申请支付日期
AuditerId = a.AuditerId,
AuditerName = a.AuditerName,
AuditTime = a.AuditTime,
Note = a.Note
});
if (!request.QueryCondition.IsNullOrEmpty())
{
var whereList = Db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
query = query.Where(whereList);
}
var result = await query.ToQueryPageAsync(request.PageCondition);
if (result.Data.Count > 0)
{
var userIds = result.Data.Select(x => x.CreateBy).Distinct();
var users = await Db.Queryable<SysUser>().Where(x => userIds.Contains(x.Id)).Select(x => new { x.Id, x.UserName }).ToListAsync();
var orgIds = result.Data.Select(x => x.SaleDeptId).Distinct().ToList();
var orgs = await Db.Queryable<SysOrg>().Where(x => orgIds.Contains(x.Id)).Select(x => new { x.Id, x.OrgName }).ToListAsync();
foreach (var item in result.Data)
{
item.CreateByName = users.Find(x => x.Id == item.CreateBy)?.UserName;
item.SaleDeptName = orgs.Find(x => x.Id == item.SaleDeptId)?.OrgName;
}
}
return result;
}
}
}