|
|
|
@ -4,6 +4,7 @@ using DS.Module.Core.Data;
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
using DS.WMS.Core.Fee.Entity;
|
|
|
|
|
using DS.WMS.Core.Fee.Interface;
|
|
|
|
|
using DS.WMS.Core.Op.Dtos;
|
|
|
|
|
using DS.WMS.Core.Op.Entity;
|
|
|
|
|
using DS.WMS.Core.Op.Interface.TaskInteraction;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
@ -31,178 +32,186 @@ namespace DS.WMS.Core.Fee.Method
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据业务ID与类型生成费用
|
|
|
|
|
/// 根据开船日生成费用
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bsId">业务ID</param>
|
|
|
|
|
/// <param name="businessType">业务类型</param>
|
|
|
|
|
/// <param name="etd">开船日</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task GenerateFeesAsync(long bsId, BusinessType businessType = BusinessType.OceanShippingExport)
|
|
|
|
|
public async Task GenerateFeesAsync(DateTime etd)
|
|
|
|
|
{
|
|
|
|
|
var order = await TenantDb.Queryable<SeaExport>().Where(x => x.Id == bsId).Select(
|
|
|
|
|
x => new { x.CustomerId, x.CustomerName }).FirstAsync();
|
|
|
|
|
if (order == null)
|
|
|
|
|
DateTime dt = DateTime.Now;
|
|
|
|
|
var list = await TenantDb.Queryable<FeeCustTemplate>()
|
|
|
|
|
.Where(x => !x.IsDisabled && SqlFunc.Between(dt, x.StartTime, x.EndTime) && (x.IsShared || x.CustomerId != null))
|
|
|
|
|
.Select(x => new FeeCustTemplate
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
CustomerId = x.CustomerId,
|
|
|
|
|
FeeType = x.FeeType,
|
|
|
|
|
FeeCategoryId = x.FeeCategoryId,
|
|
|
|
|
FeeCategoryName = x.FeeCategoryName,
|
|
|
|
|
Priority = x.Priority,
|
|
|
|
|
IsShared = x.IsShared,
|
|
|
|
|
POLCode = x.POLCode,
|
|
|
|
|
PODCode = x.PODCode,
|
|
|
|
|
LaneId = x.LaneId,
|
|
|
|
|
SourceId = x.SourceId,
|
|
|
|
|
CarrierId = x.CarrierId,
|
|
|
|
|
ForwarderId = x.ForwarderId,
|
|
|
|
|
MBLFrtCode = x.MBLFrtCode,
|
|
|
|
|
Condition = x.Condition
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DateTime dt = DateTime.Now;
|
|
|
|
|
if (etd == default)
|
|
|
|
|
etd = DateTime.Now.Date;
|
|
|
|
|
|
|
|
|
|
var tids = list.Select(x => x.Id);
|
|
|
|
|
var orders = await TenantDb.Queryable<SeaExport>().Where(x => SqlFunc.DateIsSame(x.ETD, etd) &&
|
|
|
|
|
SqlFunc.Subqueryable<FeeCustTemplateRecord>().Where(y => y.BusinessId == x.Id && y.BusinessType == BusinessType.OceanShippingExport && tids.Contains(y.TemplateId)).NotAny())
|
|
|
|
|
.Select<SeaExportRes>().ToListAsync();
|
|
|
|
|
if (orders.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
List<FeeRecord> feeList = [];
|
|
|
|
|
await TenantDb.Ado.BeginTranAsync();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var list = await TenantDb.Queryable<FeeCustTemplate>()
|
|
|
|
|
.Where(x => !x.IsDisabled && x.BusinessType == businessType && SqlFunc.Between(dt, x.StartTime, x.EndTime) &&
|
|
|
|
|
!SqlFunc.IsNullOrEmpty(x.Condition) && (x.IsShared || x.CustomerId == order.CustomerId))
|
|
|
|
|
.Select(x => new
|
|
|
|
|
{
|
|
|
|
|
x.Id,
|
|
|
|
|
x.CustomerId,
|
|
|
|
|
x.FeeType,
|
|
|
|
|
x.Priority,
|
|
|
|
|
x.IsShared,
|
|
|
|
|
x.Condition
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var conditionModels = list.Select(x => new
|
|
|
|
|
{
|
|
|
|
|
x.Id,
|
|
|
|
|
ConditionModel = JsonConvert.DeserializeObject<ConditionContent>(x.Condition)
|
|
|
|
|
}).ToList();
|
|
|
|
|
var data = await actionService.Value.GetBusinessDataAsync(bsId, businessType, conditionModels.Select(x => x.ConditionModel));
|
|
|
|
|
if (data == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
List<FeeRecord> feeList = [];
|
|
|
|
|
var custList = list.Where(x => x.CustomerId == order.CustomerId).OrderBy(x => x.Priority).ToList();
|
|
|
|
|
foreach (var item in custList) //遍历客户费用模板,查找匹配项
|
|
|
|
|
foreach (var order in orders)
|
|
|
|
|
{
|
|
|
|
|
var conditionModel = conditionModels.Find(x => x.Id == item.Id)?.ConditionModel;
|
|
|
|
|
if (actionService.Value.IsMatch(data, conditionModel))
|
|
|
|
|
var custList = list.Where(x => x.CustomerId == order.CustomerId).OrderBy(x => x.Priority);
|
|
|
|
|
foreach (var template in custList) //遍历客户费用模板,查找匹配项
|
|
|
|
|
{
|
|
|
|
|
var details = await TenantDb.Queryable<FeeCustTemplateDetail>().Where(y => y.TemplateId == item.Id)
|
|
|
|
|
.Select(y => new
|
|
|
|
|
{
|
|
|
|
|
y.CustomerId,
|
|
|
|
|
y.CustomerName,
|
|
|
|
|
y.CustomerType,
|
|
|
|
|
y.FeeId,
|
|
|
|
|
y.FeeCode,
|
|
|
|
|
y.FeeName,
|
|
|
|
|
y.Unit,
|
|
|
|
|
y.IsCtn,
|
|
|
|
|
y.Currency,
|
|
|
|
|
y.UnitPrice,
|
|
|
|
|
y.ExchangeRate,
|
|
|
|
|
y.TaxRate,
|
|
|
|
|
y.AccTaxRate,
|
|
|
|
|
y.Tax,
|
|
|
|
|
y.TaxUnitPrice,
|
|
|
|
|
y.IsInvoice,
|
|
|
|
|
y.IsAdvancedPay
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
var fees = details.Select(x => new FeeRecord
|
|
|
|
|
var fees = await CreateFeesIfMatchAsync(order, template);
|
|
|
|
|
if (fees != null)
|
|
|
|
|
{
|
|
|
|
|
BusinessId = bsId,
|
|
|
|
|
BusinessType = businessType,
|
|
|
|
|
FeeType = item.FeeType,
|
|
|
|
|
FeeId = x.FeeId,
|
|
|
|
|
FeeCode = x.FeeCode,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
CustomerId = x.CustomerId,
|
|
|
|
|
CustomerName = x.CustomerName,
|
|
|
|
|
CustomerType = x.CustomerType?.ToString(),
|
|
|
|
|
Unit = x.Unit,
|
|
|
|
|
UnitPrice = x.UnitPrice.GetValueOrDefault(),
|
|
|
|
|
Quantity = x.IsCtn ? 1 : 0,
|
|
|
|
|
Note = x.IsCtn.ToString().ToLowerInvariant(), //临时存储
|
|
|
|
|
Currency = x.Currency,
|
|
|
|
|
ExchangeRate = x.ExchangeRate,
|
|
|
|
|
TaxRate = x.TaxRate.GetValueOrDefault(),
|
|
|
|
|
AccTaxRate = x.AccTaxRate.GetValueOrDefault(),
|
|
|
|
|
Tax = x.Tax.GetValueOrDefault(),
|
|
|
|
|
TaxUnitPrice = x.TaxUnitPrice.GetValueOrDefault(),
|
|
|
|
|
IsInvoice = x.IsInvoice,
|
|
|
|
|
IsAdvancedPay = x.IsAdvancedPay,
|
|
|
|
|
});
|
|
|
|
|
feeList.AddRange(fees);
|
|
|
|
|
break;
|
|
|
|
|
feeList.AddRange(fees);
|
|
|
|
|
|
|
|
|
|
var record = new FeeCustTemplateRecord
|
|
|
|
|
{
|
|
|
|
|
BusinessId = order.CustomerId,
|
|
|
|
|
BusinessType = BusinessType.OceanShippingExport,
|
|
|
|
|
CreateBy = long.Parse(User.UserId),
|
|
|
|
|
CreateTime = dt,
|
|
|
|
|
FeeCategoryId = template.FeeCategoryId,
|
|
|
|
|
FeeType = template.FeeType,
|
|
|
|
|
TemplateId = template.Id
|
|
|
|
|
};
|
|
|
|
|
await TenantDb.Insertable(record).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (feeList.Count == 0) //未找到客户模板,开始匹配共享模板
|
|
|
|
|
{
|
|
|
|
|
var sharedList = list.Where(x => x.IsShared).OrderBy(x => x.Priority).ToList();
|
|
|
|
|
foreach (var item in sharedList)
|
|
|
|
|
//未找到客户模板,开始匹配共享模板
|
|
|
|
|
if (feeList.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
var conditionModel = conditionModels.Find(x => x.Id == item.Id)?.ConditionModel;
|
|
|
|
|
if (actionService.Value.IsMatch(data, conditionModel))
|
|
|
|
|
var sharedList = list.Where(x => x.IsShared).OrderBy(x => x.Priority).ToList();
|
|
|
|
|
foreach (var template in sharedList)
|
|
|
|
|
{
|
|
|
|
|
var details = await TenantDb.Queryable<FeeCustTemplateDetail>().Where(y => y.TemplateId == item.Id)
|
|
|
|
|
.Select(y => new
|
|
|
|
|
{
|
|
|
|
|
y.CustomerId,
|
|
|
|
|
y.CustomerName,
|
|
|
|
|
y.CustomerType,
|
|
|
|
|
y.FeeId,
|
|
|
|
|
y.FeeCode,
|
|
|
|
|
y.FeeName,
|
|
|
|
|
y.Unit,
|
|
|
|
|
y.IsCtn,
|
|
|
|
|
y.Currency,
|
|
|
|
|
y.UnitPrice,
|
|
|
|
|
y.ExchangeRate,
|
|
|
|
|
y.TaxRate,
|
|
|
|
|
y.AccTaxRate,
|
|
|
|
|
y.Tax,
|
|
|
|
|
y.TaxUnitPrice,
|
|
|
|
|
y.IsInvoice,
|
|
|
|
|
y.IsAdvancedPay
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
var fees = details.Select(x => new FeeRecord
|
|
|
|
|
var fees = await CreateFeesIfMatchAsync(order, template);
|
|
|
|
|
if (fees != null)
|
|
|
|
|
{
|
|
|
|
|
BusinessId = bsId,
|
|
|
|
|
BusinessType = businessType,
|
|
|
|
|
FeeType = item.FeeType,
|
|
|
|
|
FeeId = x.FeeId,
|
|
|
|
|
FeeCode = x.FeeCode,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
CustomerId = x.CustomerId,
|
|
|
|
|
CustomerName = x.CustomerName,
|
|
|
|
|
CustomerType = x.CustomerType?.ToString(),
|
|
|
|
|
Unit = x.Unit,
|
|
|
|
|
UnitPrice = x.UnitPrice.GetValueOrDefault(),
|
|
|
|
|
Quantity = x.IsCtn ? 1 : 0,
|
|
|
|
|
Note = x.IsCtn.ToString().ToLowerInvariant(), //临时存储
|
|
|
|
|
Currency = x.Currency,
|
|
|
|
|
ExchangeRate = x.ExchangeRate,
|
|
|
|
|
TaxRate = x.TaxRate.GetValueOrDefault(),
|
|
|
|
|
AccTaxRate = x.AccTaxRate.GetValueOrDefault(),
|
|
|
|
|
Tax = x.Tax.GetValueOrDefault(),
|
|
|
|
|
TaxUnitPrice = x.TaxUnitPrice.GetValueOrDefault(),
|
|
|
|
|
IsInvoice = x.IsInvoice,
|
|
|
|
|
IsAdvancedPay = x.IsAdvancedPay,
|
|
|
|
|
});
|
|
|
|
|
feeList.AddRange(fees);
|
|
|
|
|
break;
|
|
|
|
|
feeList.AddRange(fees);
|
|
|
|
|
|
|
|
|
|
var record = new FeeCustTemplateRecord
|
|
|
|
|
{
|
|
|
|
|
BusinessId = order.CustomerId,
|
|
|
|
|
BusinessType = BusinessType.OceanShippingExport,
|
|
|
|
|
CreateBy = long.Parse(User.UserId),
|
|
|
|
|
CreateTime = dt,
|
|
|
|
|
FeeCategoryId = template.FeeCategoryId,
|
|
|
|
|
FeeType = template.FeeType,
|
|
|
|
|
TemplateId = template.Id
|
|
|
|
|
};
|
|
|
|
|
await TenantDb.Insertable(record).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (feeList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var result = await feeService.Value.SaveAsync(feeList, true);
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
//写入当前业务的费用
|
|
|
|
|
if (feeList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
//记录失败日志
|
|
|
|
|
await new ApplicationException(result.Message).LogAsync(Db);
|
|
|
|
|
var result = await feeService.Value.SaveAsync(feeList, true, false);
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
//记录失败日志
|
|
|
|
|
await new ApplicationException(result.Message).LogAsync(Db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
feeList.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await TenantDb.Ado.CommitTranAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await TenantDb.Ado.RollbackTranAsync();
|
|
|
|
|
await ex.LogAsync(Db);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<List<FeeRecord>?> CreateFeesIfMatchAsync(SeaExportRes order, FeeCustTemplate template)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(template.POLCode) && template.POLCode != order.LoadPortCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(template.PODCode) && template.PODCode != order.DischargePortCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(template.MBLFrtCode) && template.MBLFrtCode != order.MBLFrtCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (template.LaneId.HasValue && template.LaneId != order.LaneId)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (template.CarrierId.HasValue && template.CarrierId != order.CarrierId)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (template.SourceId.HasValue && template.SourceId != order.SourceId)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (template.ForwarderId.HasValue && template.ForwarderId != order.ForwarderId)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(template.Condition)) //设置了自定义匹配条件
|
|
|
|
|
{
|
|
|
|
|
var conditionModel = JsonConvert.DeserializeObject<ConditionContent>(template.Condition);
|
|
|
|
|
if (!actionService.Value.IsMatch(order, conditionModel))
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (await TenantDb.Queryable<FeeCustTemplateRecord>().AnyAsync(x => x.BusinessId == order.Id && x.BusinessType == BusinessType.OceanShippingExport &&
|
|
|
|
|
x.FeeType == template.FeeType && x.FeeCategoryId == template.FeeCategoryId))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return await TenantDb.Queryable<FeeCustTemplateDetail>().Where(y => y.TemplateId == template.Id)
|
|
|
|
|
.Select(x => new FeeRecord
|
|
|
|
|
{
|
|
|
|
|
BusinessId = order.Id,
|
|
|
|
|
BusinessType = BusinessType.OceanShippingExport,
|
|
|
|
|
FeeType = template.FeeType,
|
|
|
|
|
FeeId = x.FeeId,
|
|
|
|
|
FeeCode = x.FeeCode,
|
|
|
|
|
FeeName = x.FeeName,
|
|
|
|
|
CustomerId = x.CustomerId,
|
|
|
|
|
CustomerName = x.CustomerName,
|
|
|
|
|
CustomerType = x.CustomerType,
|
|
|
|
|
Unit = x.Unit,
|
|
|
|
|
UnitPrice = SqlFunc.IsNull(x.UnitPrice.Value, 0),
|
|
|
|
|
//Quantity = x.IsCtn ? 1 : 0,
|
|
|
|
|
Currency = x.Currency,
|
|
|
|
|
ExchangeRate = x.ExchangeRate,
|
|
|
|
|
TaxRate = SqlFunc.IsNull(x.TaxRate.Value, 0),
|
|
|
|
|
AccTaxRate = SqlFunc.IsNull(x.AccTaxRate.Value, 0),
|
|
|
|
|
Tax = SqlFunc.IsNull(x.Tax.Value, 0),
|
|
|
|
|
TaxUnitPrice = SqlFunc.IsNull(x.TaxUnitPrice.Value, 0),
|
|
|
|
|
IsInvoice = x.IsInvoice,
|
|
|
|
|
IsAdvancedPay = x.IsAdvancedPay,
|
|
|
|
|
Remark = template.FeeCategoryName,
|
|
|
|
|
TemplateId = x.TemplateId,
|
|
|
|
|
InputMethod = InputMethod.Automatic
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列表
|
|
|
|
|
/// </summary>
|
|
|
|
|