|
|
|
|
using System.Text;
|
|
|
|
|
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.Sys.Entity;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.Core.Application.Method
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发票申请服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class InvoiceApplicationService : ApplicationService<InvoiceApplication>, IInvoiceApplicationService
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
|
|
public InvoiceApplicationService(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取分页列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult<List<InvoiceApplicationDto>>> GetListAsync(PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
List<IConditionalModel> whereList = [];
|
|
|
|
|
if (!request.QueryCondition.IsNullOrEmpty())
|
|
|
|
|
whereList = Db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
|
|
|
|
|
|
|
|
|
|
var result = await TenantDb.Queryable<InvoiceApplication>().Includes(x => x.Details).Where(whereList)
|
|
|
|
|
.Select((x) => new InvoiceApplicationDto
|
|
|
|
|
{
|
|
|
|
|
ApplicationNO = x.ApplicationNO,
|
|
|
|
|
Status = x.Status,
|
|
|
|
|
CustomerName = x.CustomerName,
|
|
|
|
|
InvoiceHeader = x.InvoiceHeader,
|
|
|
|
|
Currency = x.Currency,
|
|
|
|
|
ApplyAmount = x.ApplyAmount,
|
|
|
|
|
OriginalAmountList = x.Details.Select(y => y.OriginalCurrency + " " + y.OriginalAmount).ToList(),
|
|
|
|
|
InvoiceCurrency = x.InvoiceCurrency,
|
|
|
|
|
InvoiceAmount = x.InvoiceAmount,
|
|
|
|
|
CreateTime = x.CreateTime, //申请时间
|
|
|
|
|
CreateBy = x.CreateBy, //申请人ID
|
|
|
|
|
InvoiceNO = x.InvoiceNO,
|
|
|
|
|
InvoiceBillNO = x.InvoiceBillNO, //实际开出票号
|
|
|
|
|
Note = x.Note,
|
|
|
|
|
Reason = x.Reason,
|
|
|
|
|
RestAmount = 0, //未结算
|
|
|
|
|
InvoiceDate = x.InvoiceDate,
|
|
|
|
|
Category = x.Category, //申请类型
|
|
|
|
|
Rate = x.Rate,
|
|
|
|
|
PushMode = x.PushMode,
|
|
|
|
|
Email = x.Email,
|
|
|
|
|
CellPhoneNO = x.CellPhoneNO,
|
|
|
|
|
InvoiceRemark = x.InvoiceRemark //开票要求
|
|
|
|
|
}).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();
|
|
|
|
|
foreach (var item in result.Data)
|
|
|
|
|
{
|
|
|
|
|
item.CreateByName = users.Find(x => x.Id == item.CreateBy)?.UserName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override DataResult PreSave(InvoiceApplication application, InvoiceApplication dbValue)
|
|
|
|
|
{
|
|
|
|
|
if (dbValue != null && dbValue.Status != InvoiceApplicationStatus.Pending && dbValue.Status != InvoiceApplicationStatus.AuditRejected)
|
|
|
|
|
return DataResult.Failed("只能修改状态为:未提交/审核驳回的申请单");
|
|
|
|
|
|
|
|
|
|
//if (application.Details.Count > 0 && !string.Equals(dbValue.Currency, application.Currency, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
// return DataResult.Failed("提交申请单币别需与原申请单币别一致");
|
|
|
|
|
|
|
|
|
|
//if (application.Details.Count > 0)
|
|
|
|
|
//{
|
|
|
|
|
// if (!application.Currency.IsNullOrEmpty() && application.Details.Any(x => x.Currency != application.Currency))
|
|
|
|
|
// return DataResult.Failed($"申请单不是原币申请,所有明细币别必须为 {application.Currency}");
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DataResult CalculateAmount(InvoiceApplication application, List<FeeRecord> fees)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new();
|
|
|
|
|
foreach (var detail in application.Details)
|
|
|
|
|
{
|
|
|
|
|
var fee = fees.Find(x => x.Id == detail.RecordId);
|
|
|
|
|
if (fee == null)
|
|
|
|
|
{
|
|
|
|
|
sb.Append($"未能关联明细【{detail.FeeName}】的费用信息;");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
detail.Record = fee;
|
|
|
|
|
|
|
|
|
|
//未申请开票金额=(金额-已开票金额-申请开票金额+申请开票金额已开票
|
|
|
|
|
var restAmount = fee.Amount.GetValueOrDefault() - fee.InvoiceAmount.GetValueOrDefault() - fee.OrderInvoiceAmount.GetValueOrDefault() + fee.OrderInvSettlementAmount;
|
|
|
|
|
if (detail.OriginalAmount > 0 && detail.OriginalAmount > restAmount)
|
|
|
|
|
{
|
|
|
|
|
sb.Append($"申请单明细【{detail.FeeName}】的申请开票金额不能超出原费用的金额;");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (detail.OriginalAmount < 0 && detail.OriginalAmount < restAmount)
|
|
|
|
|
{
|
|
|
|
|
sb.Append($"申请单明细【{detail.FeeName}】的申请开票金额不能超出原费用的金额;");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//更新费用记录的已开票金额
|
|
|
|
|
fee.OrderInvoiceAmount += detail.OriginalAmount;
|
|
|
|
|
//类别固定为发票申请
|
|
|
|
|
detail.Category = FeeCategory.InvoiceApplication;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sb.Length > 0)
|
|
|
|
|
return DataResult.Failed(sb.ToString());
|
|
|
|
|
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task UpdateFeesAsync(List<FeeRecord> fees)
|
|
|
|
|
{
|
|
|
|
|
await TenantDb.Updateable(fees).UpdateColumns(x => new { x.OrderInvoiceAmount }).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DataResult PreDelete(List<InvoiceApplication> applications)
|
|
|
|
|
{
|
|
|
|
|
if (applications.Any(x => x.Status != InvoiceApplicationStatus.Pending && x.Status != InvoiceApplicationStatus.AuditRejected))
|
|
|
|
|
return DataResult.Failed("只能删除状态为‘未提交’或‘审核驳回’的申请单", MultiLanguageConst.FeeRecordDelete);
|
|
|
|
|
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnDeleteDetailAsync(List<ApplicationDetail> details)
|
|
|
|
|
{
|
|
|
|
|
//还原费用表的已开票金额
|
|
|
|
|
var fees = details.Select(x => new FeeRecord { Id = x.RecordId, OrderInvoiceAmount = x.OriginalAmount }).ToList();
|
|
|
|
|
await TenantDb.Updateable(fees)
|
|
|
|
|
.PublicSetColumns(it => it.OrderInvoiceAmount, "-")
|
|
|
|
|
.UpdateColumns(x => new { x.OrderInvoiceAmount })
|
|
|
|
|
.ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|