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.Application.Interface;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Sys.Entity;
using SqlSugar;
namespace DS.WMS.Core.Application.Method
{
///
/// 费用申请单审核服务
///
public class InvoiceApplicationAuditService : ApplicationAuditService, IInvoiceApplicationAuditService
{
public override TaskBaseTypeEnum AuditType => TaskBaseTypeEnum.APPLICATION_INVOICE_AUDIT;
///
/// 初始化
///
///
public InvoiceApplicationAuditService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
///
/// 获取待审核列表
///
///
///
public async Task>> GetListAsync(PageRequest request)
{
var query = CreateListQuery();
if (!request.QueryCondition.IsNullOrEmpty())
{
var whereList = request.GetConditionalModels(Db);
query = query.Where(whereList);
}
//设置了状态筛选
if (request.OtherQueryCondition.GetValueOrDefault())
query = query.Where(a => a.Status == InvoiceApplicationStatus.AuditSubmittd);
var result = await query.GroupBy(x => x.Id).ToQueryPageAsync(request.PageCondition);
if (result.Data.Count > 0)
{
var orgIds = result.Data.Select(x => x.SaleDeptId).Distinct().ToList();
var orgs = await Db.Queryable().Where(x => orgIds.Contains(x.Id)).Select(x => new { x.Id, x.OrgName }).ToListAsync();
foreach (var item in result.Data)
{
item.SaleDeptName = orgs.Find(x => x.Id == item.SaleDeptId)?.OrgName;
}
}
return result;
}
internal ISugarQueryable CreateListQuery()
{
var query1 = TenantDb.Queryable()
.InnerJoin((a, d) => a.Id == d.ApplicationId)
.InnerJoin((a, d, f) => d.RecordId == f.Id)
//.LeftJoin((a, d, f, s) => f.BusinessId == s.Id)
//.LeftJoin((a, d, f, s, b) => a.CustomerBankId == b.Id)
.GroupBy(a => a.Id)
.Select((a, d, f) => new InvoiceApplicationDto
{
Id = a.Id,
ApplicationNO = a.ApplicationNO,
Status = a.Status,
CustomerId = a.CustomerId,
CustomerName = a.CustomerName, //委托单位
InvoiceHeader = a.InvoiceHeader,
Currency = a.Currency,
ApplyAmount = a.ApplyAmount,
InvoiceAmount = a.InvoiceAmount,
CreateTime = a.CreateTime,//申请开票日期+申请时间
CreateBy = a.CreateBy, //申请人
Category = a.Category, //申请类型
TaxRate = a.TaxRate, //发票税率
InvoiceNO = a.InvoiceNO, //发票号
Note = a.Note,
InvoiceRemark = a.InvoiceRemark, //开票要求
SaleDeptId = a.SaleDeptId,
//原币金额
OriginalAmountList = SqlFunc.Subqueryable().Where(y => a.Id == y.ApplicationId)
.GroupBy(y => y.OriginalCurrency).ToList(y => new CurrencyAmount { Currency = y.OriginalCurrency, Amount = y.OriginalAmount }),
});
return TenantDb.UnionAll(new List> { query1 });
}
protected override DataResult PreAudit(List applications)
{
if (applications.Exists(x => x.Status != InvoiceApplicationStatus.AuditSubmittd))
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.ApplicationIsNotAuditing));
return DataResult.Success;
}
protected override async Task OnUpdateStatusAsync(FlowCallback callback, InvoiceApplication application)
{
if (callback.FlowStatus == FlowStatusEnum.Approve)
{
application.Status = InvoiceApplicationStatus.AuditPassed;
application.Reason = string.Empty;
}
else if (callback.FlowStatus == FlowStatusEnum.Reject)
{
application.Status = InvoiceApplicationStatus.AuditRejected;
application.Reason = callback.RejectReason;
}
await base.OnUpdateStatusAsync(callback, application);
}
}
}