|
|
|
@ -9,13 +9,11 @@ using DS.WMS.Core.Fee.Entity;
|
|
|
|
|
using DS.WMS.Core.Info.Entity;
|
|
|
|
|
using DS.WMS.Core.Invoice.Dtos;
|
|
|
|
|
using DS.WMS.Core.Invoice.Interface;
|
|
|
|
|
using DS.WMS.Core.Settlement.Entity;
|
|
|
|
|
using DS.WMS.Core.Sys.Entity;
|
|
|
|
|
using DS.WMS.Core.Sys.Interface;
|
|
|
|
|
using LanguageExt.Pretty;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Masuit.Tools.Models;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Org.BouncyCastle.Asn1.Cmp;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.Core.Invoice.Method
|
|
|
|
@ -227,7 +225,8 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
if (invoice.PushModeValues.Length > 0)
|
|
|
|
|
invoice.PushMode = string.Join(",", invoice.PushModeValues.Select(x => (int)x));
|
|
|
|
|
|
|
|
|
|
if (request.Invoice.Mode == InvoiceMode.Applcation && request.Applications != null && request.Applications.Count > 0) //按发票申请开出
|
|
|
|
|
//按申请开票
|
|
|
|
|
if (request.Invoice.Mode == InvoiceMode.Applcation && request.Applications?.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var ids = request.Applications.Select(x => x.ApplicationId);
|
|
|
|
|
var details = await TenantDb.Queryable<ApplicationDetail>()
|
|
|
|
@ -339,21 +338,36 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
invoice.Details.Add(detail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataResult result2;
|
|
|
|
|
//执行开票金额分配
|
|
|
|
|
foreach (var app in request.Applications)
|
|
|
|
|
{
|
|
|
|
|
var details2 = invoice.Details.Where(x => x.RefId == app.ApplicationId).OrderBy(x => x.ApplyAmount).ToList();
|
|
|
|
|
if (app.AmountRMB.HasValue)
|
|
|
|
|
{
|
|
|
|
|
result2 = AssignAmount(details2.FindAll(x => x.OriginalCurrency == FeeCurrency.RMB_CODE), app.AmountRMB.Value);
|
|
|
|
|
if (!result2.Succeeded)
|
|
|
|
|
return DataResult<TEntity>.Failed(result2.Message, result2.MultiCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (app.AmountUSD.HasValue)
|
|
|
|
|
{
|
|
|
|
|
result2 = AssignAmount(details2.FindAll(x => x.OriginalCurrency == FeeCurrency.USD_CODE), app.AmountUSD.Value);
|
|
|
|
|
if (!result2.Succeeded)
|
|
|
|
|
return DataResult<TEntity>.Failed(result2.Message, result2.MultiCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////执行开票金额分配
|
|
|
|
|
//foreach (var app in request.Applications)
|
|
|
|
|
//{
|
|
|
|
|
// var details2 = invoice.Details.Where(x => x.RefId == app.ApplicationId).OrderBy(x => x.ApplyAmount).ToList();
|
|
|
|
|
// if (app.AmountRMB.HasValue)
|
|
|
|
|
// {
|
|
|
|
|
// var result2 = AssignAmount(details2.FindAll(x => x.OriginalCurrency == FeeCurrency.RMB_CODE), app.AmountRMB.Value);
|
|
|
|
|
// if (!result2.Succeeded)
|
|
|
|
|
// return DataResult<TEntity>.Failed(result2.Message, result2.MultiCode);
|
|
|
|
|
// }
|
|
|
|
|
if (app.AmountOther.HasValue)
|
|
|
|
|
{
|
|
|
|
|
result2 = AssignAmount(details2.FindAll(x => x.OriginalCurrency != FeeCurrency.RMB_CODE && x.OriginalCurrency != FeeCurrency.USD_CODE), app.AmountOther.Value);
|
|
|
|
|
if (!result2.Succeeded)
|
|
|
|
|
return DataResult<TEntity>.Failed(result2.Message, result2.MultiCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (request.Invoice.Mode == InvoiceMode.Free) //自由开票
|
|
|
|
|
//自由开票
|
|
|
|
|
else if (request.Invoice.Mode == InvoiceMode.Free)
|
|
|
|
|
{
|
|
|
|
|
if (request.BizList?.Count > 0)
|
|
|
|
|
{
|
|
|
|
@ -409,32 +423,32 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
OriginalAmount = x.OriginalAmount,
|
|
|
|
|
OriginalCurrency = x.OriginalCurrency ?? (invoice.Currency.IsNullOrEmpty() ? x.Currency : invoice.Currency),
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
//补充购方信息
|
|
|
|
|
invoice.CustomerTaxID = await TenantDb.Queryable<InfoClient>().Where(x => x.Id == invoice.CustomerId).Select(x => x.TaxNo).FirstAsync();
|
|
|
|
|
var header = await TenantDb.Queryable<InvoiceHeader>().Where(x => x.RelativeId == invoice.CustomerId)
|
|
|
|
|
.OrderByDescending(x => x.Id).FirstAsync();
|
|
|
|
|
if (header != null)
|
|
|
|
|
{
|
|
|
|
|
invoice.InvoiceHeader = header.Header;
|
|
|
|
|
invoice.CustomerAddressTel = header.AddressTel;
|
|
|
|
|
}
|
|
|
|
|
var clientBank = await TenantDb.Queryable<InfoClientBank>().Where(x => x.ClientId == invoice.CustomerId && x.Currency == invoice.Currency)
|
|
|
|
|
.OrderByDescending(x => x.IsInvoiceDefault).Select(x => new
|
|
|
|
|
{
|
|
|
|
|
x.Account,
|
|
|
|
|
x.BankName
|
|
|
|
|
}).FirstAsync();
|
|
|
|
|
if (clientBank != null)
|
|
|
|
|
{
|
|
|
|
|
invoice.CustomerAccount = clientBank.Account;
|
|
|
|
|
invoice.CustomerBankName = clientBank.BankName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (invoice.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
//补充购方信息
|
|
|
|
|
invoice.CustomerTaxID = await TenantDb.Queryable<InfoClient>().Where(x => x.Id == invoice.CustomerId).Select(x => x.TaxNo).FirstAsync();
|
|
|
|
|
var header = await TenantDb.Queryable<InvoiceHeader>().Where(x => x.RelativeId == invoice.CustomerId)
|
|
|
|
|
.OrderByDescending(x => x.Id).FirstAsync();
|
|
|
|
|
if (header != null)
|
|
|
|
|
{
|
|
|
|
|
invoice.InvoiceHeader = header.Header;
|
|
|
|
|
invoice.CustomerAddressTel = header.AddressTel;
|
|
|
|
|
}
|
|
|
|
|
var clientBank = await TenantDb.Queryable<InfoClientBank>().Where(x => x.ClientId == invoice.CustomerId && x.Currency == invoice.Currency)
|
|
|
|
|
.OrderByDescending(x => x.IsInvoiceDefault).Select(x => new
|
|
|
|
|
{
|
|
|
|
|
x.Account,
|
|
|
|
|
x.BankName
|
|
|
|
|
}).FirstAsync();
|
|
|
|
|
if (clientBank != null)
|
|
|
|
|
{
|
|
|
|
|
invoice.CustomerAccount = clientBank.Account;
|
|
|
|
|
invoice.CustomerBankName = clientBank.BankName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//补充销方信息
|
|
|
|
|
var org = await Db.Queryable<SysOrg>().Where(x => x.Id == User.OrgId).Select(x => new { x.OrgFullName, x.LicenseCode }).FirstAsync();
|
|
|
|
|
if (org != null)
|
|
|
|
@ -877,6 +891,10 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
if (invoices.Any(x => !string.IsNullOrEmpty(x.ApiCode)))
|
|
|
|
|
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.InvoiceIsIssued));
|
|
|
|
|
|
|
|
|
|
var ids = invoices.Select(x => x.Id);
|
|
|
|
|
if (TenantDb.Queryable<ApplicationDetail>().Any(x => ids.Contains(x.RefId.Value) && x.Category == DetailCategory.InvoiceSettlement))
|
|
|
|
|
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.InvoiceIsSettled));
|
|
|
|
|
|
|
|
|
|
return DataResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -888,12 +906,12 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected virtual async Task OnDeleteDetailAsync(List<TEntity> invoices, DeleteOption deleteOption)
|
|
|
|
|
{
|
|
|
|
|
var appIds = invoices.Select(x => x.Id);
|
|
|
|
|
var ids = invoices.Select(x => x.Id);
|
|
|
|
|
|
|
|
|
|
if (deleteOption == DeleteOption.DetailOnly)
|
|
|
|
|
{
|
|
|
|
|
var excludeIds = invoices.SelectMany(x => x.Details).Select(x => x.Id);
|
|
|
|
|
var details = await TenantDb.Queryable<ApplicationDetail>().Where(x => appIds.Contains(x.ApplicationId) && !excludeIds.Contains(x.Id))
|
|
|
|
|
var details = await TenantDb.Queryable<ApplicationDetail>().Where(x => ids.Contains(x.ApplicationId) && !excludeIds.Contains(x.Id))
|
|
|
|
|
.Select(x => new ApplicationDetail
|
|
|
|
|
{
|
|
|
|
|
Id = x.Id,
|
|
|
|
@ -919,7 +937,7 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
else if (deleteOption == DeleteOption.Entire)
|
|
|
|
|
{
|
|
|
|
|
//删除发票主表则同时删除对应发票明细
|
|
|
|
|
await TenantDb.Deleteable<InvoiceDetail>().Where(x => appIds.Contains(x.ApplicationId)).ExecuteCommandAsync();
|
|
|
|
|
await TenantDb.Deleteable<InvoiceDetail>().Where(x => ids.Contains(x.ApplicationId)).ExecuteCommandAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var item in invoices)
|
|
|
|
|