|
|
|
@ -15,7 +15,7 @@ using SqlSugar;
|
|
|
|
|
namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发票开具服务
|
|
|
|
|
/// 发票开出接口服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class InvoiceIssuanceService : ApplicationServiceBase, IInvoiceIssuanceService
|
|
|
|
|
{
|
|
|
|
@ -167,21 +167,22 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> UpdateInvoiceNumberAsync(string sn)
|
|
|
|
|
{
|
|
|
|
|
var result2 = DataResult.Success;
|
|
|
|
|
var invResult = DataResult.Success;
|
|
|
|
|
var invoices = await TenantDb.Queryable<Entity.Invoice>()
|
|
|
|
|
.Where(x => x.SN == sn && !SqlFunc.IsNullOrEmpty(x.PDFUrl))
|
|
|
|
|
.Where(x => x.SN == sn && !SqlFunc.IsNullOrEmpty(x.InvoiceNO))
|
|
|
|
|
.Select(x => new Entity.Invoice
|
|
|
|
|
{
|
|
|
|
|
SN = x.SN,
|
|
|
|
|
BillNO = x.BillNO,
|
|
|
|
|
InvoiceNO = x.InvoiceNO,
|
|
|
|
|
Type = x.Type,
|
|
|
|
|
ApiCode = x.ApiCode,
|
|
|
|
|
ApiStatus = x.ApiStatus,
|
|
|
|
|
PDFUrl = x.PDFUrl
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
result2.Data = invoices;
|
|
|
|
|
invResult.Data = invoices;
|
|
|
|
|
if (invoices.Count > 0)
|
|
|
|
|
return result2;
|
|
|
|
|
return invResult;
|
|
|
|
|
|
|
|
|
|
var order = await TenantDb.Queryable<Entity.Invoice>().Where(x => x.SN == sn).FirstAsync();
|
|
|
|
|
//如果开票中所属机构为空,则取用户的orgId
|
|
|
|
@ -212,6 +213,18 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
|
|
|
|
|
if (queryResult.data.Order?.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var billNumbers = queryResult.data.Order.Select(x => x.orderNo);
|
|
|
|
|
//获取发票ID及类型
|
|
|
|
|
var list = await TenantDb.Queryable<Entity.Invoice>().Where(x => billNumbers.Contains(x.BillNO))
|
|
|
|
|
.Select(x => new
|
|
|
|
|
{
|
|
|
|
|
x.Id,
|
|
|
|
|
x.BillNO,
|
|
|
|
|
x.Type,
|
|
|
|
|
x.Mode
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
long userId = long.Parse(User.UserId);
|
|
|
|
|
foreach (var item in queryResult.data.Order)
|
|
|
|
|
{
|
|
|
|
|
var inv = new Entity.Invoice
|
|
|
|
@ -221,8 +234,18 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
InvoiceNO = item.fphm,
|
|
|
|
|
ApiCode = item.State.ToString(),
|
|
|
|
|
ApiStatus = item.UpMessage,
|
|
|
|
|
PDFUrl = item.FileUrl
|
|
|
|
|
PDFUrl = item.FileUrl,
|
|
|
|
|
IsLocked = true,
|
|
|
|
|
LockTime = dtNow,
|
|
|
|
|
LockUserId = userId
|
|
|
|
|
};
|
|
|
|
|
var storedItem = list.Find(x => x.BillNO == inv.BillNO);
|
|
|
|
|
if (storedItem == null)
|
|
|
|
|
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData));
|
|
|
|
|
|
|
|
|
|
inv.Id = storedItem.Id;
|
|
|
|
|
inv.Type = storedItem.Type;
|
|
|
|
|
inv.Mode = storedItem.Mode;
|
|
|
|
|
invoices.Add(inv);
|
|
|
|
|
|
|
|
|
|
switch (item.State)
|
|
|
|
@ -242,28 +265,33 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
await TenantDb.Ado.BeginTranAsync();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var billNumbers = invoices.Select(x => x.BillNO);
|
|
|
|
|
//获取发票费用明细ID
|
|
|
|
|
var ids = await TenantDb.Queryable<Entity.Invoice>()
|
|
|
|
|
.InnerJoin<ApplicationDetail>((i, d) => i.Id == d.ApplicationId)
|
|
|
|
|
.Where((i, d) => billNumbers.Contains(i.BillNO) && i.Type == InvoiceType.Blue)
|
|
|
|
|
.Select((i, d) => d.Id).ToArrayAsync();
|
|
|
|
|
if (ids.Length > 0)
|
|
|
|
|
var redIds = invoices.Where(x => x.Type == InvoiceType.Red).Select(x => x.Id).ToList();
|
|
|
|
|
//开出红票时,需要删除蓝票的费用明细
|
|
|
|
|
if (redIds.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
//获取蓝票费用明细ID
|
|
|
|
|
var ids = await TenantDb.Queryable<ApplicationDetail>()
|
|
|
|
|
.InnerJoin<Entity.Invoice>((d, i) => d.ApplicationId == i.Id)
|
|
|
|
|
.Where((d, i) => redIds.Contains(i.RedId.Value))
|
|
|
|
|
.Select((d, i) => d.Id).ToArrayAsync();
|
|
|
|
|
//删除蓝票费用明细
|
|
|
|
|
await InvoiceService.Value.DeleteDetailAsync(ids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await TenantDb.Updateable(invoices).WhereColumns(x => x.BillNO)
|
|
|
|
|
.UpdateColumns(x => new
|
|
|
|
|
{
|
|
|
|
|
x.InvoiceNO,
|
|
|
|
|
x.ApiCode,
|
|
|
|
|
x.ApiStatus,
|
|
|
|
|
x.PDFUrl
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
await InvoiceService.Value.RefreshApplicationStatus(invoices.Select(x => x.InvoiceNO)!);
|
|
|
|
|
await TenantDb.Updateable(invoices).UpdateColumns(x => new
|
|
|
|
|
{
|
|
|
|
|
x.InvoiceNO,
|
|
|
|
|
x.ApiCode,
|
|
|
|
|
x.ApiStatus,
|
|
|
|
|
x.PDFUrl,
|
|
|
|
|
x.IsLocked,
|
|
|
|
|
x.LockUserId,
|
|
|
|
|
x.LockTime
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
//回写发票申请的状态
|
|
|
|
|
if (invoices.Exists(x => x.Mode == InvoiceMode.Applcation && x.Type == InvoiceType.Blue))
|
|
|
|
|
await InvoiceService.Value.RefreshApplicationStatus(invoices.Select(x => x.InvoiceNO)!);
|
|
|
|
|
|
|
|
|
|
await TenantDb.Ado.CommitTranAsync();
|
|
|
|
|
}
|
|
|
|
@ -275,8 +303,8 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result2.Data = invoices;
|
|
|
|
|
return result2;
|
|
|
|
|
invResult.Data = invoices;
|
|
|
|
|
return invResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -350,6 +378,7 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await TenantDb.InsertNav(redInvoice).Include(x => x.Details).Include(x => x.InvoiceDetails).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
blueInvoice.RedId = redInvoice.Id;
|
|
|
|
|
blueInvoice.IsSetRed = true;
|
|
|
|
|
blueInvoice.RedCode = request.chyyDm;
|
|
|
|
@ -407,6 +436,40 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发送邮件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">发票ID</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DataResult> SendMailAsync(long id)
|
|
|
|
|
{
|
|
|
|
|
var inv = await TenantDb.Queryable<Entity.Invoice>().Where(x => x.Id == id)
|
|
|
|
|
.Select(x => new
|
|
|
|
|
{
|
|
|
|
|
orderNo = x.BillNO,
|
|
|
|
|
x.SaleDeptId,
|
|
|
|
|
x.InvoiceNO
|
|
|
|
|
}).FirstAsync();
|
|
|
|
|
if (inv == null)
|
|
|
|
|
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData));
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(inv.InvoiceNO))
|
|
|
|
|
return DataResult.Failed("发票尚未开出");
|
|
|
|
|
|
|
|
|
|
//如果开票中所属机构为空,则取用户的orgId
|
|
|
|
|
var userOrgId = (inv.SaleDeptId != null && inv.SaleDeptId > 0) ? inv.SaleDeptId : User.OrgId;
|
|
|
|
|
//接口请求key密钥
|
|
|
|
|
var orgauthinfo = Db.Queryable<SysOrgAuth>().Where(t => t.OrgId == userOrgId).First();
|
|
|
|
|
api.DefaultHeaders.Clear();
|
|
|
|
|
api.DefaultHeaders.Add("USER_KEY", orgauthinfo.Key);
|
|
|
|
|
api.DefaultHeaders.Add("USER_SECRET", orgauthinfo.Secret);
|
|
|
|
|
var result = await api.PostAsync<InvoiceResult<SetRedConfirmation>>(AppSetting.app(["InvoiceApi", "BaseUrl"]) + "/api/Invoice/InvoiceToEmil", inv);
|
|
|
|
|
if (result.Data == null || !result.Data.success)
|
|
|
|
|
return DataResult.Failed(result.Data == null ? "请求失败" : result.Data.msg);
|
|
|
|
|
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加租户信息
|
|
|
|
|
/// </summary>
|
|
|
|
|