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.Info.Entity;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using SqlSugar;
namespace DS.WMS.Core.Application.Method
{
///
/// 费用申请单审核服务
///
public class InvoiceApplicationAuditService : ApplicationAuditService, IInvoiceApplicationAuditService
{
///
/// 初始化
///
///
public InvoiceApplicationAuditService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
///
/// 获取待审核列表
///
///
///
public async Task>> GetListAsync(PageRequest request)
{
var query = CreateListQuery();
if (!request.QueryCondition.IsNullOrEmpty())
{
var whereList = Db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
int? index = null;
foreach (var item in whereList)
{
ConditionalModel? model = item as ConditionalModel;
if (model == null)
continue;
//设置了状态筛选
if (string.Equals(model.FieldName, nameof(InvoiceApplicationDto.IsAudited)) && bool.TryParse(model.FieldValue, out bool isAudited))
{
if (isAudited)
query = query.Where(x => x.Status == InvoiceApplicationStatus.AuditPassed || x.Status == InvoiceApplicationStatus.AuditRejected);
else
query = query.Where(x => x.Status == InvoiceApplicationStatus.AuditSubmittd);
index = whereList.IndexOf(item);
break;
}
}
if (index.HasValue)
whereList.RemoveAt(index.Value);
query = query.Where(whereList);
}
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().Where(x => x.Status == InvoiceApplicationStatus.AuditSubmittd)
.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)
.Select((a, d, s, b) => 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,
InvoiceCurrency = a.InvoiceCurrency,
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)
{
var auditType = callback.AuditType;
if (auditType != TaskBaseTypeEnum.APPLICATION_INVOICE_AUDIT)
return;
if (callback.FlowStatus == FlowStatusEnum.Approve)
{
application.Status = InvoiceApplicationStatus.AuditPassed;
application.Reason = string.Empty;
}
else if (callback.FlowStatus == FlowStatusEnum.Reject)
application.Status = InvoiceApplicationStatus.AuditRejected;
await base.OnUpdateStatusAsync(callback, application);
}
}
}