|
|
|
|
using System.Text;
|
|
|
|
|
using AngleSharp.Dom;
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
using DS.Module.SqlSugar;
|
|
|
|
|
using DS.Module.UserModule;
|
|
|
|
|
using DS.WMS.Core.Fee.Dtos;
|
|
|
|
|
using DS.WMS.Core.Fee.Entity;
|
|
|
|
|
using DS.WMS.Core.Fee.Interface;
|
|
|
|
|
using DS.WMS.Core.Flow.Dtos;
|
|
|
|
|
using DS.WMS.Core.Flow.Entity;
|
|
|
|
|
using DS.WMS.Core.Flow.Interface;
|
|
|
|
|
using DS.WMS.Core.Info.Entity;
|
|
|
|
|
using DS.WMS.Core.Op.Entity;
|
|
|
|
|
using DS.WMS.Core.Sys.Entity;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Masuit.Tools.Systems;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.Core.Fee.Method
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 费用记录实现
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class FeeRecordService : IFeeRecordService
|
|
|
|
|
{
|
|
|
|
|
const long PERMISSION_ID = 1793490768447541248;
|
|
|
|
|
const string DefaultCurrency = "CNY";//默认本币
|
|
|
|
|
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ISqlSugarClient db;
|
|
|
|
|
private readonly IUser user;
|
|
|
|
|
private readonly ISaasDbService saasService;
|
|
|
|
|
readonly IClientFlowInstanceService flowService;
|
|
|
|
|
|
|
|
|
|
public FeeRecordService(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
|
|
user = _serviceProvider.GetRequiredService<IUser>();
|
|
|
|
|
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
|
|
|
|
|
|
|
|
|
|
flowService = _serviceProvider.GetRequiredService<IClientFlowInstanceService>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult<List<FeeRecordRes>>> GetListByPageAsync(PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
long userId = long.Parse(user.UserId);
|
|
|
|
|
//序列化查询条件
|
|
|
|
|
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
|
|
|
|
|
var data = await tenantDb.Queryable<FeeRecord>()
|
|
|
|
|
.Where(x => x.IsOpen || (!x.IsOpen && x.CreateBy == userId))
|
|
|
|
|
.Where(whereList)
|
|
|
|
|
.Select<FeeRecordRes>()
|
|
|
|
|
.ToQueryPageAsync(request.PageCondition);
|
|
|
|
|
|
|
|
|
|
//关联用户名称
|
|
|
|
|
var userIds = data.Data.Where(x => x.UpdateBy.HasValue).Select(x => x.UpdateBy.Value)
|
|
|
|
|
//.Union(data.Data.Where(x => x.SubmitBy.HasValue).Select(x => x.SubmitBy.Value))
|
|
|
|
|
.Union(data.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 data.Data)
|
|
|
|
|
{
|
|
|
|
|
item.CreateByName = users.Find(x => x.Id == item.CreateBy)?.UserName;
|
|
|
|
|
|
|
|
|
|
if (item.UpdateBy.HasValue)
|
|
|
|
|
{
|
|
|
|
|
item.UpdateByName = users.Find(x => x.Id == item.UpdateBy.Value)?.UserName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if (item.SubmitBy.HasValue)
|
|
|
|
|
//{
|
|
|
|
|
// item.SubmitByName = users.Find(x => x.Id == item.SubmitBy.Value)?.UserName;
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult<List<FeeRecord>>> GetListAsync(string query)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
long userId = long.Parse(user.UserId);
|
|
|
|
|
var src = tenantDb.Queryable<FeeRecord>().Where(x => x.IsOpen || (!x.IsOpen && x.CreateBy == userId));
|
|
|
|
|
if (!query.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
//序列化查询条件
|
|
|
|
|
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(query);
|
|
|
|
|
src = src.Where(whereList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data = await src.ToListAsync();
|
|
|
|
|
return new DataResult<List<FeeRecord>>(ResultCode.Success) { Data = data };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检查业务是否已费用锁定
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bid">业务ID</param>
|
|
|
|
|
/// <param name="businessType">业务类型</param>
|
|
|
|
|
/// <param name="type">锁定范围</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
internal async Task<bool> IsFeeLockedAsync(long bid, BusinessType businessType, FeeType type = FeeType.All)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
bool? isFeeLocking = null;
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case FeeType.Receivable:
|
|
|
|
|
break;
|
|
|
|
|
case FeeType.Payable:
|
|
|
|
|
break;
|
|
|
|
|
case FeeType.All:
|
|
|
|
|
isFeeLocking = await tenantDb.Queryable<BusinessFeeStatus>().Where(
|
|
|
|
|
x => x.BusinessId == bid && x.BusinessType == businessType).Select(x => x.IsFeeLocking).FirstAsync();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return isFeeLocking.GetValueOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 费用提交
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">要提交的费用记录</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> SaveAsync(IEnumerable<FeeRecord> items)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
|
|
|
|
|
var first = items.Select(x => new { x.BusinessType, x.BusinessId }).FirstOrDefault();
|
|
|
|
|
if (await IsFeeLockedAsync(first.BusinessId, first.BusinessType))
|
|
|
|
|
return DataResult.Failed("当前业务已费用锁定,禁止提交", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Ado.BeginTranAsync();
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
var feeIds = items.Where(x => x.Id > 0).Select(x => x.Id).ToArray();
|
|
|
|
|
//包含修改的项,需要检测费用状态再修改
|
|
|
|
|
if (feeIds.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var fees = await tenantDb.Queryable<FeeRecord>().Where(x => feeIds.Contains(x.Id)).Select(x => new FeeRecord
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
FeeStatus = x.FeeStatus
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
foreach (var fe in fees)
|
|
|
|
|
{
|
|
|
|
|
if (fe.FeeStatus != FeeStatus.Entering && fe.FeeStatus != FeeStatus.RejectSubmission)
|
|
|
|
|
{
|
|
|
|
|
sb.Append($"费用【{fe.FeeName}】状态不正确,无法修改;");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sb.Length > 0)
|
|
|
|
|
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//若计价货币单位不等于默认货币则尝试获取最新汇率
|
|
|
|
|
await FetchExchangeRateAsync(tenantDb, items);
|
|
|
|
|
|
|
|
|
|
var createList = items.Where(x => x.Id == 0).ToList();
|
|
|
|
|
await tenantDb.Insertable(createList).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
var updateList = items.Where(x => x.Id > 0).ToList();
|
|
|
|
|
await tenantDb.Updateable(updateList).IgnoreColumns(x => new
|
|
|
|
|
{
|
|
|
|
|
x.FeeStatus,
|
|
|
|
|
x.BusinessId,
|
|
|
|
|
x.BusinessType,
|
|
|
|
|
x.CreateBy,
|
|
|
|
|
x.CreateTime,
|
|
|
|
|
x.DeleteBy,
|
|
|
|
|
x.Deleted,
|
|
|
|
|
x.DeleteTime,
|
|
|
|
|
x.SubmitDate,
|
|
|
|
|
x.SubmitBy,
|
|
|
|
|
x.AuditBy,
|
|
|
|
|
x.AuditOperator,
|
|
|
|
|
x.AuditDate
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
await tenantDb.Ado.CommitTranAsync();
|
|
|
|
|
|
|
|
|
|
var list = createList.Union(updateList).Select(x => x.Adapt<FeeRecordRes>());
|
|
|
|
|
return DataResult.Successed("保存成功", list, MultiLanguageConst.DataUpdateSuccess);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Ado.RollbackTranAsync();
|
|
|
|
|
await ex.LogAsync(db);
|
|
|
|
|
return DataResult.Failed("保存失败", MultiLanguageConst.DataUpdateFailed);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
await WriteBackStatusAsync(tenantDb, first.BusinessId, first.BusinessType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据模板ID创建
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bid">业务ID</param>
|
|
|
|
|
/// <param name="businessType">业务类型</param>
|
|
|
|
|
/// <param name="tidArray">模板ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> CreateByTemplateAsync(long bid, BusinessType businessType, params long[] tidArray)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
|
|
|
|
|
bool hasExists = tenantDb.Queryable<FeeRecord>().LeftJoin<FeeTemplateDetail>((x, y) =>
|
|
|
|
|
x.FeeId == y.FeeId && x.FeeType == y.FeeType).Any(
|
|
|
|
|
(x, y) => x.BusinessId == bid && x.BusinessType == businessType && tidArray.Contains(y.TemplateId));
|
|
|
|
|
if (hasExists)
|
|
|
|
|
return DataResult.Failed("费用记录已存在", MultiLanguageConst.FeeRecordExist);
|
|
|
|
|
|
|
|
|
|
var details = await tenantDb.Queryable<FeeTemplateDetail>().Where(x => tidArray.Contains(x.TemplateId) && !x.Deleted).Select(x => new
|
|
|
|
|
{
|
|
|
|
|
x.FeeType,
|
|
|
|
|
x.FeeId,
|
|
|
|
|
x.FeeCode,
|
|
|
|
|
x.FeeName,
|
|
|
|
|
x.FeeFrt,
|
|
|
|
|
x.FeeGroup,
|
|
|
|
|
x.CustomerName,
|
|
|
|
|
x.CustomerType,
|
|
|
|
|
x.CustomerId,
|
|
|
|
|
x.Unit,
|
|
|
|
|
x.UnitPrice,
|
|
|
|
|
x.Currency,
|
|
|
|
|
x.ExchangeRate,
|
|
|
|
|
x.Tax,
|
|
|
|
|
x.TaxRate,
|
|
|
|
|
x.AccTaxRate,
|
|
|
|
|
x.IsAdvancedPay,
|
|
|
|
|
x.IsInvoice,
|
|
|
|
|
x.SaleOrgId,
|
|
|
|
|
x.Note
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
|
|
|
|
|
List<FeeRecord> records = new List<FeeRecord>(details.Count);
|
|
|
|
|
foreach (var item in details)
|
|
|
|
|
{
|
|
|
|
|
var record = item.Adapt<FeeRecord>();
|
|
|
|
|
record.BusinessId = bid;
|
|
|
|
|
record.SubmitDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
records.Add(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//若计价货币单位不等于默认货币则尝试获取最新汇率
|
|
|
|
|
await FetchExchangeRateAsync(tenantDb, records);
|
|
|
|
|
int result = await tenantDb.Insertable(records).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
await WriteBackStatusAsync(tenantDb, bid, businessType);
|
|
|
|
|
|
|
|
|
|
return result > 0 ? DataResult.Successed("保存成功", records, MultiLanguageConst.DataCreateSuccess) : DataResult.Successed("操作失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids">费用记录ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> DeleteAsync(params long[] ids)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
|
|
|
|
|
var model = await tenantDb.Queryable<FeeRecord>().Where(x => ids.Contains(x.Id)).Select(x => new { x.BusinessId, x.BusinessType }).FirstAsync();
|
|
|
|
|
if (await IsFeeLockedAsync(model.BusinessId, model.BusinessType))
|
|
|
|
|
return DataResult.Failed("当前业务已费用锁定,禁止修改", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
if (await tenantDb.Queryable<FeeRecord>().AnyAsync(x => ids.Contains(x.Id) && (x.FeeStatus != FeeStatus.Entering && x.FeeStatus != FeeStatus.RejectSubmission)))
|
|
|
|
|
return DataResult.Failed("只能删除状态为‘录入’或‘驳回提交’的费用", MultiLanguageConst.FeeRecordDelete);
|
|
|
|
|
|
|
|
|
|
int result = await tenantDb.Deleteable<FeeRecord>(x => ids.Contains(x.Id)).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
await WriteBackStatusAsync(tenantDb, model.BusinessId, model.BusinessType);
|
|
|
|
|
|
|
|
|
|
return result > 0 ? DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess) : DataResult.Failed("删除失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发起审批/申请删除工作流
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="auditType">审批类型</param>
|
|
|
|
|
/// <param name="remark">备注</param>
|
|
|
|
|
/// <param name="idArray">费用记录ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> SubmitForApprovalAsync(FeeAuditType auditType, string remark, params long[] idArray)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
var fees = await tenantDb.Queryable<FeeRecord>().Where(x => idArray.Contains(x.Id)).Select(
|
|
|
|
|
x => new FeeRecord
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
FeeStatus = x.FeeStatus,
|
|
|
|
|
FlowId = x.FlowId,
|
|
|
|
|
BusinessId = x.BusinessId
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
if (fees.IsNullOrEmpty())
|
|
|
|
|
return DataResult.Failed($"未能获取费用信息,提交失败", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
var bid = fees[0].BusinessId;
|
|
|
|
|
var bType = fees[0].BusinessType;
|
|
|
|
|
//业务状态检测
|
|
|
|
|
if (await IsFeeLockedAsync(bid, bType))
|
|
|
|
|
return DataResult.Failed("当前业务已费用锁定,禁止提交", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
if (fees.Any(x => x.FlowId.HasValue))
|
|
|
|
|
return DataResult.Failed($"当前费用包含正在审批中的费用,无法提交", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
if (fees.Any(x => x.FeeStatus == FeeStatus.PartialSettlement || x.FeeStatus == FeeStatus.SettlementCompleted))
|
|
|
|
|
return DataResult.Failed($"当前审批费用包含已结算/部分结算的费用,无法提交", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
DataResult result = DataResult.Failed(string.Empty, MultiLanguageConst.Operation_Failed);
|
|
|
|
|
await tenantDb.Ado.BeginTranAsync();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (auditType == FeeAuditType.ApplyAudit)
|
|
|
|
|
{
|
|
|
|
|
result = ApplyAudit(tenantDb, fees);
|
|
|
|
|
}
|
|
|
|
|
else if (auditType == FeeAuditType.ApplyDeletion)
|
|
|
|
|
{
|
|
|
|
|
result = ApplyDelete(tenantDb, fees, remark);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Ado.RollbackTranAsync();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await tenantDb.Updateable(fees).UpdateColumns(x => new
|
|
|
|
|
{
|
|
|
|
|
x.Id,
|
|
|
|
|
x.FeeStatus,
|
|
|
|
|
x.SubmitBy,
|
|
|
|
|
x.SubmitDate,
|
|
|
|
|
x.FlowId
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
await tenantDb.Ado.CommitTranAsync();
|
|
|
|
|
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Ado.RollbackTranAsync();
|
|
|
|
|
await ex.LogAsync(db);
|
|
|
|
|
return DataResult.Failed("提交失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//录入->提交审核
|
|
|
|
|
DataResult ApplyAudit(SqlSugarScopeProvider tenantDb, List<FeeRecord> fees)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
foreach (var fe in fees)
|
|
|
|
|
{
|
|
|
|
|
if (fe.FeeStatus != FeeStatus.Entering && fe.FeeStatus != FeeStatus.RejectSubmission)
|
|
|
|
|
{
|
|
|
|
|
sb.Append($"费用【{fe.FeeName}】状态不正确,无法提交审批;");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sb.Length > 0)
|
|
|
|
|
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
var template = FindTemplate(FeeAuditType.ApplyAudit.ToString());
|
|
|
|
|
if (template == null)
|
|
|
|
|
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
foreach (var item in fees)
|
|
|
|
|
{
|
|
|
|
|
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq
|
|
|
|
|
{
|
|
|
|
|
BusinessId = item.Id,
|
|
|
|
|
BusinessType = item.BusinessType,
|
|
|
|
|
TemplateId = template.Id
|
|
|
|
|
});
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
var instance = result.Data as FlowInstance;
|
|
|
|
|
flowService.StartFlowInstance(instance.Id.ToString());
|
|
|
|
|
|
|
|
|
|
//变更状态为提交审核
|
|
|
|
|
item.FeeStatus = FeeStatus.AuditSubmitted;
|
|
|
|
|
item.SubmitBy = long.Parse(user.UserId);
|
|
|
|
|
item.SubmitDate = dtNow;
|
|
|
|
|
item.FlowId = instance.Id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//审核通过->申请删除
|
|
|
|
|
DataResult ApplyDelete(SqlSugarScopeProvider tenantDb, List<FeeRecord> fees, string reason)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
foreach (var fe in fees)
|
|
|
|
|
{
|
|
|
|
|
if (fe.FeeStatus != FeeStatus.AuditPassed && fe.FeeStatus != FeeStatus.RejectApplication)
|
|
|
|
|
{
|
|
|
|
|
sb.Append($"费用【{fe.FeeName}】状态不正确,无法提交审批;");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sb.Length > 0)
|
|
|
|
|
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
var template = FindTemplate(FeeAuditType.ApplyDeletion.ToString());
|
|
|
|
|
if (template == null)
|
|
|
|
|
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
foreach (var item in fees)
|
|
|
|
|
{
|
|
|
|
|
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq
|
|
|
|
|
{
|
|
|
|
|
BusinessId = item.Id,
|
|
|
|
|
BusinessType = item.BusinessType,
|
|
|
|
|
TemplateId = template.Id
|
|
|
|
|
});
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
var instance = result.Data as FlowInstance;
|
|
|
|
|
flowService.StartFlowInstance(instance.Id.ToString());
|
|
|
|
|
|
|
|
|
|
//变更状态为申请删除
|
|
|
|
|
item.FeeStatus = FeeStatus.ApplyDeletion;
|
|
|
|
|
item.SubmitBy = long.Parse(user.UserId);
|
|
|
|
|
item.SubmitDate = dtNow;
|
|
|
|
|
item.FlowId = instance.Id;
|
|
|
|
|
item.Reason = reason;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发起费用修改申请
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">费用修改信息</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> SubmitForModificationAsync(IEnumerable<FeeModification> items)
|
|
|
|
|
{
|
|
|
|
|
var idList = items.Select(x => x.FeeRecordId).Distinct().ToList();
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
var fees = await tenantDb.Queryable<FeeRecord>().Where(x => idList.Contains(x.Id)).Select(x => new FeeRecord
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
FeeStatus = x.FeeStatus
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
if (fees.Count == 0)
|
|
|
|
|
return DataResult.Failed("未能获取费用信息", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
foreach (var fe in fees)
|
|
|
|
|
{
|
|
|
|
|
if (fe.FeeStatus != FeeStatus.AuditPassed && fe.FeeStatus != FeeStatus.RejectApplication)
|
|
|
|
|
{
|
|
|
|
|
sb.Append($"费用【{fe.FeeName}】状态不正确,无法提交审批;");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (sb.Length > 0)
|
|
|
|
|
return DataResult.Failed(sb.ToString(), MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
var template = FindTemplate(FeeAuditType.ApplyModification.ToString());
|
|
|
|
|
if (template == null)
|
|
|
|
|
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
await tenantDb.Ado.BeginTranAsync();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var fee in fees)
|
|
|
|
|
{
|
|
|
|
|
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq
|
|
|
|
|
{
|
|
|
|
|
BusinessId = fee.Id,
|
|
|
|
|
BusinessType = fee.BusinessType,
|
|
|
|
|
TemplateId = template.Id
|
|
|
|
|
});
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
var instance = result.Data as FlowInstance;
|
|
|
|
|
flowService.StartFlowInstance(instance.Id.ToString());
|
|
|
|
|
|
|
|
|
|
//变更状态为申请修改
|
|
|
|
|
fee.FeeStatus = FeeStatus.ApplyModification;
|
|
|
|
|
//fee.SubmitBy = long.Parse(user.UserId);
|
|
|
|
|
//fee.SubmitDate = dtNow;
|
|
|
|
|
fee.FlowId = instance.Id;
|
|
|
|
|
fee.Reason = items.FirstOrDefault(x => x.FeeRecordId == fee.Id)?.Reason;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var list = items.ToList();
|
|
|
|
|
await tenantDb.Insertable(list).ExecuteCommandAsync();
|
|
|
|
|
await tenantDb.Updateable(fees).UpdateColumns(x => new { x.FeeStatus, x.FlowId }).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
await tenantDb.Ado.CommitTranAsync();
|
|
|
|
|
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Ado.RollbackTranAsync();
|
|
|
|
|
await ex.LogAsync(db);
|
|
|
|
|
return DataResult.Failed("提交失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 撤销审批
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idArray">费用记录ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> WithdrawAsync(params long[] idArray)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
var fees = await tenantDb.Queryable<FeeRecord>().Where(x => idArray.Contains(x.Id)).Select(
|
|
|
|
|
x => new FeeRecord
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
FeeStatus = x.FeeStatus,
|
|
|
|
|
FlowId = x.FlowId
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
|
|
|
|
|
if (fees.IsNullOrEmpty())
|
|
|
|
|
return DataResult.Failed("未能找到费用记录", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
//未在审批状态中
|
|
|
|
|
var fees2 = fees.FindAll(x => x.FlowId == null).ToList();
|
|
|
|
|
if (fees2.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Join("; ", fees2.Select(x => $"{x.FeeName}"));
|
|
|
|
|
return DataResult.Failed($"以下费用项:{msg} 未在审批状态中,无需撤销", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var flows = fees.Select(x => new FlowInstance { Id = x.FlowId.Value, FlowStatus = (int)FlowStatusEnum.Draft, MakerList = string.Empty }).ToList();
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Ado.BeginTranAsync();
|
|
|
|
|
await db.Updateable(flows).UpdateColumns(x => new { x.FlowStatus, x.MakerList }).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
foreach (var item in fees)
|
|
|
|
|
{
|
|
|
|
|
switch (item.FeeStatus)
|
|
|
|
|
{
|
|
|
|
|
case FeeStatus.AuditSubmitted:
|
|
|
|
|
item.FeeStatus = FeeStatus.Entering;
|
|
|
|
|
break;
|
|
|
|
|
case FeeStatus.ApplyModification:
|
|
|
|
|
item.FeeStatus = FeeStatus.AuditPassed;
|
|
|
|
|
|
|
|
|
|
//删除暂存数据
|
|
|
|
|
var entity = await tenantDb.Queryable<FeeModification>().OrderByDescending(
|
|
|
|
|
x => x.CreateTime).Select(x => new { x.Id, x.FeeRecordId }).FirstAsync(x => x.FeeRecordId == item.Id);
|
|
|
|
|
if (entity != null)
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Deleteable<FeeModification>().Where(x => x.Id == entity.Id).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case FeeStatus.ApplyDeletion:
|
|
|
|
|
item.FeeStatus = FeeStatus.AuditPassed;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.SubmitBy = long.Parse(user.UserId);
|
|
|
|
|
item.SubmitDate = dtNow;
|
|
|
|
|
item.FlowId = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await tenantDb.Updateable(fees).UpdateColumns(x => new { x.Id, x.FeeStatus, x.SubmitBy, x.SubmitDate, x.FlowId }).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
await tenantDb.Ado.CommitTranAsync();
|
|
|
|
|
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await tenantDb.Ado.RollbackTranAsync();
|
|
|
|
|
await ex.LogAsync(db);
|
|
|
|
|
return DataResult.Failed("提交失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 整单审核
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bid">业务ID</param>
|
|
|
|
|
/// <param name="type">业务类型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> SubmitBusinessAuditAsync(long bid, BusinessType type)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
var entity = await tenantDb.Queryable<BusinessFeeStatus>().Where(x => x.BusinessId == bid &&
|
|
|
|
|
x.BusinessType == BusinessType.OceanShippingExport).Select(x => new BusinessFeeStatus
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
IsFeeLocking = x.IsFeeLocking,
|
|
|
|
|
BillAuditStatus = x.BillAuditStatus,
|
|
|
|
|
FlowId = x.FlowId
|
|
|
|
|
}).FirstAsync();
|
|
|
|
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
return DataResult.Failed("未能找到业务信息", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
if (entity.IsFeeLocking.GetValueOrDefault())
|
|
|
|
|
return DataResult.Failed("当前业务已费用锁定,禁止提交", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
if (entity.FlowId.HasValue)
|
|
|
|
|
return DataResult.Failed($"当前业务正在审批中,无法提交", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
if (entity.BillAuditStatus == BillAuditStatus.AuditPassed)
|
|
|
|
|
return DataResult.Failed($"当前业务的费用状态为:{entity.BillAuditStatus.GetDescription()},无法提交", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
var template = FindTemplate(FeeAuditType.Business.ToString());
|
|
|
|
|
if (template == null)
|
|
|
|
|
return DataResult.Failed("未能找到审批模板", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
var result = flowService.CreateFlowInstance(new CreateFlowInstanceReq
|
|
|
|
|
{
|
|
|
|
|
BusinessId = entity.Id,
|
|
|
|
|
BusinessType = entity.BusinessType,
|
|
|
|
|
TemplateId = template.Id
|
|
|
|
|
});
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
var instance = result.Data as FlowInstance;
|
|
|
|
|
flowService.StartFlowInstance(instance.Id.ToString());
|
|
|
|
|
|
|
|
|
|
//变更状态为提交审核
|
|
|
|
|
entity.BillAuditStatus = BillAuditStatus.AuditSubmitted;
|
|
|
|
|
entity.BillFeeStatusTime = DateTime.Now;
|
|
|
|
|
entity.FlowId = instance.Id;
|
|
|
|
|
|
|
|
|
|
await tenantDb.Updateable(entity).UpdateColumns(x => new
|
|
|
|
|
{
|
|
|
|
|
x.BillAuditStatus,
|
|
|
|
|
x.BillFeeStatusTime,
|
|
|
|
|
x.FlowId
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
//修改关联费用状态为提交审核
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return DataResult.Successed("提交成功!", MultiLanguageConst.DataUpdateSuccess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DataResult.Failed("创建工作流失败", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置发票启用状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="enabled">是否启用</param>
|
|
|
|
|
/// <param name="idArray">费用记录ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> SetInvoiceEnabledAsync(bool enabled, params long[] idArray)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
var list = idArray.Select(x => new FeeRecord { Id = x, IsInvoice = enabled }).ToList();
|
|
|
|
|
int rows = await tenantDb.Updateable(list).UpdateColumns(x => new { x.IsInvoice }).ExecuteCommandAsync();
|
|
|
|
|
return rows > 0 ? DataResult.Successed("设置成功!", MultiLanguageConst.DataUpdateSuccess) : DataResult.Failed("设置失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置费用对象
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="customerId">费用对象ID</param>
|
|
|
|
|
/// <param name="customerType">客户类别</param>
|
|
|
|
|
/// <param name="idArray">费用记录ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> SetCustomerAsync(long customerId, string customerType, params long[] idArray)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
var model = await tenantDb.Queryable<InfoClient>().Where(x => x.Id == customerId).Select(x => new
|
|
|
|
|
{
|
|
|
|
|
Id = customerId,
|
|
|
|
|
x.CodeName,
|
|
|
|
|
x.Name
|
|
|
|
|
}).FirstAsync();
|
|
|
|
|
if (model == null)
|
|
|
|
|
return DataResult.Failed("未能找到费用对象信息,设置失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
|
|
|
|
|
var list = idArray.Select(x => new FeeRecord
|
|
|
|
|
{
|
|
|
|
|
Id = x,
|
|
|
|
|
CustomerId = customerId,
|
|
|
|
|
CustomerCode = model.CodeName,
|
|
|
|
|
CustomerName = model.Name,
|
|
|
|
|
CustomerType = customerType
|
|
|
|
|
}).ToList();
|
|
|
|
|
int rows = await tenantDb.Updateable(list).UpdateColumns(x => new { x.CustomerId, x.CustomerCode, x.CustomerName, x.CustomerType }).ExecuteCommandAsync();
|
|
|
|
|
return rows > 0 ? DataResult.Successed("设置成功!", MultiLanguageConst.DataUpdateSuccess) : DataResult.Failed("设置失败!", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取费用核算单打印信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="businessType">业务类型</param>
|
|
|
|
|
/// <param name="idArray">费用记录ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult<CostAccountingForm>> GetPrintInfoAsync(BusinessType businessType, params long[] idArray)
|
|
|
|
|
{
|
|
|
|
|
CostAccountingForm form = null;
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
switch (businessType)
|
|
|
|
|
{
|
|
|
|
|
case BusinessType.OceanShippingExport:
|
|
|
|
|
form = await GetOceanShippingExportAsync(tenantDb, idArray);
|
|
|
|
|
break;
|
|
|
|
|
case BusinessType.OceanShippingImport:
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return DataResult<CostAccountingForm>.Failed(
|
|
|
|
|
$"不支持业务类型为【{businessType.GetDescription()}】的费用打印", MultiLanguageConst.Operation_Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (form != null)
|
|
|
|
|
{
|
|
|
|
|
long userId = long.Parse(user.UserId);
|
|
|
|
|
form.Creator = db.Queryable<SysUser>().Where(x => x.Id == userId).Select(x => x.NickName).First();
|
|
|
|
|
|
|
|
|
|
form.ReceivableRMB = form.Details.FindAll(x => x.Type == FeeType.Receivable && x.Currency == "CNY").Sum(x => x.Amount);
|
|
|
|
|
form.PayableRMB = form.Details.FindAll(x => x.Type == FeeType.Payable && x.Currency == "CNY").Sum(x => x.Amount);
|
|
|
|
|
form.ReceivableUSD = form.Details.FindAll(x => x.Type == FeeType.Receivable && x.Currency == "USD").Sum(x => x.Amount);
|
|
|
|
|
form.PayableUSD = form.Details.FindAll(x => x.Type == FeeType.Payable && x.Currency == "USD").Sum(x => x.Amount);
|
|
|
|
|
form.ReceivableOther = form.Details.FindAll(x => x.Type == FeeType.Receivable && (x.Currency != "CNY" && x.Currency != "USD")).Sum(x => x.Amount);
|
|
|
|
|
form.PayableOther = form.Details.FindAll(x => x.Type == FeeType.Payable && (x.Currency == "CNY" && x.Currency != "USD")).Sum(x => x.Amount);
|
|
|
|
|
|
|
|
|
|
//获取美元汇率
|
|
|
|
|
var fees = new List<FeeRecord> {
|
|
|
|
|
new FeeRecord { Currency = "USD", FeeType = FeeType.Receivable },
|
|
|
|
|
new FeeRecord { Currency = "USD", FeeType = FeeType.Payable }
|
|
|
|
|
};
|
|
|
|
|
await FetchExchangeRateAsync(tenantDb, fees);
|
|
|
|
|
form.ExchangeRate = fees[0].ExchangeRate.HasValue ? fees[0].ExchangeRate.Value : 1;
|
|
|
|
|
form.TotalReceivable = Math.Round(form.ReceivableUSD * form.ExchangeRate, 4, MidpointRounding.ToEven) + form.ReceivableRMB + form.ReceivableOther;
|
|
|
|
|
form.TotalPayable = Math.Round(form.PayableUSD * form.ExchangeRate, 4, MidpointRounding.ToEven) + form.PayableRMB + form.PayableOther;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DataResult<CostAccountingForm>.Success(form);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取海运出口打印数据
|
|
|
|
|
async Task<CostAccountingForm> GetOceanShippingExportAsync(SqlSugarScopeProvider tenantDb, params long[] idArray)
|
|
|
|
|
{
|
|
|
|
|
CostAccountingForm form = null;
|
|
|
|
|
var list = await tenantDb.Queryable<FeeRecord>().InnerJoin<SeaExport>((x, y) => x.BusinessId == y.Id)
|
|
|
|
|
.Where((x, y) => idArray.Contains(x.Id)
|
|
|
|
|
//&& x.FeeStatus == FeeStatus.SettlementCompleted
|
|
|
|
|
).Select((x, y) => new
|
|
|
|
|
{
|
|
|
|
|
x.FeeType,
|
|
|
|
|
x.FeeName,
|
|
|
|
|
x.Currency,
|
|
|
|
|
x.ExchangeRate,
|
|
|
|
|
x.Amount,
|
|
|
|
|
x.CustomerName,
|
|
|
|
|
y.CustomerNo, //业务编号
|
|
|
|
|
y.AccountDate, //会计期间
|
|
|
|
|
y.ETA,
|
|
|
|
|
y.ETD,
|
|
|
|
|
y.Voyno,
|
|
|
|
|
y.MBLNO,
|
|
|
|
|
y.Carrier,
|
|
|
|
|
y.LoadPort,
|
|
|
|
|
y.DischargePort,
|
|
|
|
|
y.CntrTotal, //Volume
|
|
|
|
|
y.IssueType //放单方式
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
return form;
|
|
|
|
|
|
|
|
|
|
var item = list[0];
|
|
|
|
|
form = new CostAccountingForm
|
|
|
|
|
{
|
|
|
|
|
BusinessNo = item.CustomerNo,
|
|
|
|
|
AccountingPeriod = item.AccountDate,
|
|
|
|
|
ETA = item.ETA,
|
|
|
|
|
ETD = item.ETD,
|
|
|
|
|
Voy = item.Voyno,
|
|
|
|
|
MBLNo = item.MBLNO,
|
|
|
|
|
Carrier = item.Carrier,
|
|
|
|
|
POL = item.LoadPort,
|
|
|
|
|
POD = item.DischargePort,
|
|
|
|
|
Volume = item.CntrTotal,
|
|
|
|
|
ReleaseType = item.IssueType,
|
|
|
|
|
PrintTime = DateTime.Now,
|
|
|
|
|
Details = list.Select(x => new CostAccountingDetail
|
|
|
|
|
{
|
|
|
|
|
Amount = x.Amount.GetValueOrDefault(),
|
|
|
|
|
Currency = x.Currency,
|
|
|
|
|
CustomerName = x.CustomerName,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
Type = x.FeeType
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return form;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 回写业务表费用状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tenantDb"></param>
|
|
|
|
|
/// <param name="businessId">业务ID</param>
|
|
|
|
|
/// <param name="businessType">业务类型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task WriteBackStatusAsync(SqlSugarScopeProvider tenantDb, long businessId, BusinessType businessType)
|
|
|
|
|
{
|
|
|
|
|
var fees = await tenantDb.Queryable<FeeRecord>().Where(x => x.BusinessId == businessId && x.BusinessType == businessType)
|
|
|
|
|
.Select(x => new FeeRecord
|
|
|
|
|
{
|
|
|
|
|
FeeType = x.FeeType,
|
|
|
|
|
FeeStatus = x.FeeStatus
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
if (fees.IsNullOrEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var arFeeStatus = DetermineStatus(fees.FindAll(x => x.FeeType == FeeType.Receivable));
|
|
|
|
|
var apFeeStatus = DetermineStatus(fees.FindAll(x => x.FeeType == FeeType.Payable));
|
|
|
|
|
|
|
|
|
|
if (arFeeStatus != null || apFeeStatus != null)
|
|
|
|
|
{
|
|
|
|
|
var upt = tenantDb.Updateable<BusinessFeeStatus>();
|
|
|
|
|
if (arFeeStatus != null)
|
|
|
|
|
{
|
|
|
|
|
upt = upt.SetColumns(x => x.ARFeeStatus == arFeeStatus);
|
|
|
|
|
}
|
|
|
|
|
if (apFeeStatus != null)
|
|
|
|
|
{
|
|
|
|
|
upt = upt.SetColumns(x => x.APFeeStatus == apFeeStatus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await upt.Where(x => x.BusinessType == businessType && x.BusinessId == businessId).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await ex.LogAsync(db);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//业务表费用状态判定
|
|
|
|
|
static BillFeeStatus? DetermineStatus(List<FeeRecord> fees)
|
|
|
|
|
{
|
|
|
|
|
BillFeeStatus? billFeeStatus = null;
|
|
|
|
|
if (fees.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.NotEntered;
|
|
|
|
|
}
|
|
|
|
|
//全状态
|
|
|
|
|
else if (fees.All(x => x.FeeStatus == FeeStatus.Entering))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.Entering;
|
|
|
|
|
}
|
|
|
|
|
else if (fees.All(x => x.FeeStatus == FeeStatus.AuditSubmitted))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.AuditSubmitted;
|
|
|
|
|
}
|
|
|
|
|
else if (fees.All(x => x.FeeStatus == FeeStatus.AuditPassed))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.AuditPassed;
|
|
|
|
|
}
|
|
|
|
|
else if (fees.All(x => x.FeeStatus == FeeStatus.RejectSubmission))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.RejectSubmission;
|
|
|
|
|
}
|
|
|
|
|
else if (fees.All(x => x.FeeStatus == FeeStatus.PartialSettlement))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.PartialSettlement;
|
|
|
|
|
}
|
|
|
|
|
else if (fees.All(x => x.FeeStatus == FeeStatus.SettlementCompleted))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.SettlementCompleted;
|
|
|
|
|
}
|
|
|
|
|
//部分状态
|
|
|
|
|
else if (fees.Any(x => x.FeeStatus == FeeStatus.Entering))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.PartialEntering;
|
|
|
|
|
}
|
|
|
|
|
else if (fees.Any(x => x.FeeStatus == FeeStatus.AuditSubmitted))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.PartialSubmitted;
|
|
|
|
|
}
|
|
|
|
|
else if (fees.Any(x => x.FeeStatus == FeeStatus.AuditPassed))
|
|
|
|
|
{
|
|
|
|
|
billFeeStatus = BillFeeStatus.PartialAudited;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return billFeeStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查找模板
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="auditType">审批类型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
FlowTemplateTenant FindTemplate(string auditType)
|
|
|
|
|
{
|
|
|
|
|
return db.Queryable<FlowTemplateTenant>().Where(x =>
|
|
|
|
|
x.Status == StatusEnum.Enable &&
|
|
|
|
|
x.PermissionId == PERMISSION_ID &&
|
|
|
|
|
x.AuditType == auditType).First();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取汇率
|
|
|
|
|
async Task FetchExchangeRateAsync(SqlSugarScopeProvider tenantDb, IEnumerable<FeeRecord> items)
|
|
|
|
|
{
|
|
|
|
|
var exRecords = items.Where(x => !x.ExchangeRate.HasValue && x.Currency != DefaultCurrency).ToList();
|
|
|
|
|
if (exRecords.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var codes = exRecords.Select(x => x.Currency).Distinct().ToList();
|
|
|
|
|
var currencies = await tenantDb.Queryable<FeeCurrency>().Where(x => codes.Contains(x.CodeName)).Includes(x => x.Exchanges).ToListAsync();
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
foreach (var item in exRecords)
|
|
|
|
|
{
|
|
|
|
|
var currency = currencies.Find(x => x.CodeName == item.Currency);
|
|
|
|
|
if (currency != null)
|
|
|
|
|
{
|
|
|
|
|
item.ExchangeRate = currency.DefaultRate;
|
|
|
|
|
if (!currency.Exchanges.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
//取当前时间范围内的最新一条
|
|
|
|
|
var exchange = currency.Exchanges.FindAll(x => x.Status == StatusEnum.Enable &&
|
|
|
|
|
x.StartDate >= dtNow && x.EndDate <= dtNow).OrderByDescending(x => x.UpdateTime).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (exchange != null)
|
|
|
|
|
item.ExchangeRate = item.FeeType == FeeType.Receivable ? exchange.DRValue : exchange.CRValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|