|
|
|
@ -5,6 +5,7 @@ using DS.WMS.Core.Application.Dtos;
|
|
|
|
|
using DS.WMS.Core.Application.Entity;
|
|
|
|
|
using DS.WMS.Core.Application.Method;
|
|
|
|
|
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.Sys.Entity;
|
|
|
|
@ -152,8 +153,6 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
public async Task<DataResult<TEntity>> SaveAsync(InvoiceRequest<TEntity> request)
|
|
|
|
|
{
|
|
|
|
|
var invoice = request.Invoice;
|
|
|
|
|
if (invoice.InvoiceDate == default)
|
|
|
|
|
invoice.InvoiceDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
if (invoice.Currency.IsNullOrEmpty())
|
|
|
|
|
invoice.Currency = FeeCurrency.RMB_CODE;
|
|
|
|
@ -172,14 +171,21 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
return DataResult<TEntity>.Failed(result.Message, result.MultiCode);
|
|
|
|
|
|
|
|
|
|
if (invoice.InvoiceDate == default)
|
|
|
|
|
invoice.InvoiceDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
invoice.OperatorId ??= long.Parse(User.UserId);
|
|
|
|
|
|
|
|
|
|
//按发票申请
|
|
|
|
|
if (request.Applications != null && request.Applications.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var ids = request.Applications.Select(x => x.ApplicationId);
|
|
|
|
|
var details = await TenantDb.Queryable<ApplicationDetail>()
|
|
|
|
|
.InnerJoin<InvoiceApplication>((x, y) => x.ApplicationId == y.Id)
|
|
|
|
|
.Where((x, y) => ids.Contains(x.ApplicationId) && x.Category == DetailCategory.InvoiceApplication)
|
|
|
|
|
.Select((x, y) => new
|
|
|
|
|
.LeftJoin<InfoClientBank>((x, y, z) => y.CustomerId == z.ClientId && z.Currency == invoice.Currency &&
|
|
|
|
|
z.IsInvoiceDefault.GetValueOrDefault())
|
|
|
|
|
.Where((x, y, z) => ids.Contains(x.ApplicationId) && x.Category == DetailCategory.InvoiceApplication)
|
|
|
|
|
.Select((x, y, z) => new
|
|
|
|
|
{
|
|
|
|
|
x.Id,
|
|
|
|
|
x.ApplicationId,
|
|
|
|
@ -194,26 +200,56 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
x.OriginalCurrency,
|
|
|
|
|
x.ProcessedAmount,
|
|
|
|
|
x.OriginalProcessedAmount,
|
|
|
|
|
y.CustomerId
|
|
|
|
|
|
|
|
|
|
y.CustomerId,
|
|
|
|
|
y.TaxRate,
|
|
|
|
|
y.TaxID,
|
|
|
|
|
y.InvoiceHeader,
|
|
|
|
|
y.CustomerAddTel,
|
|
|
|
|
y.SaleDeptId,
|
|
|
|
|
y.PushMode,
|
|
|
|
|
y.CellPhoneNO,
|
|
|
|
|
y.Email,
|
|
|
|
|
|
|
|
|
|
z.BankAccountNo,
|
|
|
|
|
z.BankName
|
|
|
|
|
}).ToListAsync();
|
|
|
|
|
|
|
|
|
|
invoice.Details ??= new List<ApplicationDetail>(details.Count);
|
|
|
|
|
//税率不一致
|
|
|
|
|
if (details.GroupBy(x => x.TaxRate).Select(x => x.Key).Count() > 1)
|
|
|
|
|
return DataResult<TEntity>.FailedWithDesc(nameof(MultiLanguageConst.InvoiceCustomerOnlyOne));
|
|
|
|
|
|
|
|
|
|
invoice.Details ??= new List<ApplicationDetail>(details.Count);
|
|
|
|
|
foreach (var item in details)
|
|
|
|
|
{
|
|
|
|
|
if (request.Invoice.CustomerId == 0)
|
|
|
|
|
if (invoice.CustomerId == 0)
|
|
|
|
|
{
|
|
|
|
|
request.Invoice.CustomerId = item.CustomerId;
|
|
|
|
|
request.Invoice.CustomerName = item.CustomerName;
|
|
|
|
|
invoice.CustomerId = item.CustomerId;
|
|
|
|
|
invoice.CustomerName = item.CustomerName;
|
|
|
|
|
invoice.TaxRate = item.TaxRate;
|
|
|
|
|
invoice.InvoiceHeader = item.InvoiceHeader;
|
|
|
|
|
invoice.CustomerTaxID = item.TaxID;
|
|
|
|
|
invoice.CustomerAddressTel = item.CustomerAddTel;
|
|
|
|
|
invoice.CustomerAccount = item.BankAccountNo;
|
|
|
|
|
invoice.CustomerBankName = item.BankName;
|
|
|
|
|
invoice.SaleDeptId = item.SaleDeptId;
|
|
|
|
|
invoice.PushMode = item.PushMode;
|
|
|
|
|
invoice.CellPhoneNO = item.CellPhoneNO;
|
|
|
|
|
invoice.Email = item.Email;
|
|
|
|
|
}
|
|
|
|
|
else if (request.Invoice.CustomerId != item.CustomerId) //校验开票单位是否一致
|
|
|
|
|
else if (invoice.CustomerId != item.CustomerId) //校验开票单位是否一致
|
|
|
|
|
{
|
|
|
|
|
return DataResult<TEntity>.FailedWithDesc(nameof(MultiLanguageConst.InvoiceCustomerOnlyOne));
|
|
|
|
|
}
|
|
|
|
|
else if (invoice.TaxRate != item.TaxRate) //校验税率是否一致
|
|
|
|
|
{
|
|
|
|
|
return DataResult<TEntity>.FailedWithDesc(nameof(MultiLanguageConst.InconsistentTaxRates));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//需转换为费用明细
|
|
|
|
|
var detail = new ApplicationDetail
|
|
|
|
|
{
|
|
|
|
|
ApplicationId = request.Invoice.Id,
|
|
|
|
|
ApplicationId = invoice.Id,
|
|
|
|
|
RefId = item.ApplicationId,
|
|
|
|
|
DetailId = item.Id,
|
|
|
|
|
RecordId = item.RecordId,
|
|
|
|
@ -222,7 +258,7 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
FeeId = item.FeeId,
|
|
|
|
|
FeeName = item.FeeName,
|
|
|
|
|
FeeType = item.FeeType,
|
|
|
|
|
Currency = FeeCurrency.RMB_CODE,
|
|
|
|
|
Currency = invoice.Currency,
|
|
|
|
|
OriginalCurrency = item.Currency,
|
|
|
|
|
ApplyAmount = item.ApplyAmount - item.ProcessedAmount,
|
|
|
|
|
OriginalAmount = item.OriginalAmount - item.OriginalProcessedAmount
|
|
|
|
@ -250,6 +286,12 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
invoice.CustomerName = first.CustomerName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ids = request.Details.Select(x => x.RecordId).Distinct();
|
|
|
|
|
var fees = await TenantDb.Queryable<FeeRecord>().Where(x => ids.Contains(x.Id)).Select(x => new { x.Id, x.TaxRate }).ToListAsync();
|
|
|
|
|
//税率不一致
|
|
|
|
|
if (fees.GroupBy(x => x.TaxRate).Select(x => x.Key).Count() > 1 || (invoice.Id > 0 && invoice.TaxRate != fees[0].TaxRate))
|
|
|
|
|
return DataResult<TEntity>.FailedWithDesc(nameof(MultiLanguageConst.InvoiceCustomerOnlyOne));
|
|
|
|
|
|
|
|
|
|
//将请求明细转换为数据库的费用明细
|
|
|
|
|
invoice.Details = request.Details.Select(x => new ApplicationDetail
|
|
|
|
|
{
|
|
|
|
@ -269,6 +311,41 @@ 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//补充销方信息
|
|
|
|
|
invoice.TaxID = await Db.Queryable<SysOrg>().Where(x => x.Id == User.OrgId).Select(x => x.LicenseCode).FirstAsync();
|
|
|
|
|
var orgBank = await Db.Queryable<SysBank>().Where(x => x.LinkId == User.OrgId && x.Currency == invoice.Currency).OrderByDescending(x => x.IsDefault).Select(x => new
|
|
|
|
|
{
|
|
|
|
|
x.BankAccountNo,
|
|
|
|
|
x.BankName
|
|
|
|
|
}).FirstAsync();
|
|
|
|
|
if (orgBank != null)
|
|
|
|
|
{
|
|
|
|
|
invoice.BankName = orgBank.BankName;
|
|
|
|
|
invoice.Account = orgBank.BankAccountNo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (invoice.Details?.Count > 0)
|
|
|
|
@ -301,9 +378,8 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
//创建时需要生成申请单编号
|
|
|
|
|
var sequence = CommonService.Value.GetSequenceNext<TEntity>();
|
|
|
|
|
if (!sequence.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return DataResult<TEntity>.Failed(sequence.Message, MultiLanguageConst.SequenceSetNotExist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
invoice.BillNO = sequence.Data;
|
|
|
|
|
//关联导航属性插入
|
|
|
|
|
await TenantDb.InsertNav(invoice).Include(x => x.Details).ExecuteCommandAsync();
|
|
|
|
@ -313,25 +389,20 @@ namespace DS.WMS.Core.Invoice.Method
|
|
|
|
|
buildOption = BuildOption.Update;
|
|
|
|
|
await TenantDb.Updateable(invoice).UpdateColumns(x => new
|
|
|
|
|
{
|
|
|
|
|
x.InvoiceNO,
|
|
|
|
|
x.InvoiceDate,
|
|
|
|
|
x.Account,
|
|
|
|
|
x.BankName,
|
|
|
|
|
//x.Currency,
|
|
|
|
|
x.ReceiptCurrency,
|
|
|
|
|
x.CustomerId,
|
|
|
|
|
x.CustomerName,
|
|
|
|
|
x.AutualCustomerName,
|
|
|
|
|
x.InvoiceHeader,
|
|
|
|
|
x.CustomerAddress,
|
|
|
|
|
x.CustomerPhone,
|
|
|
|
|
x.CustomerBankName,
|
|
|
|
|
x.CustomerAccount,
|
|
|
|
|
x.OperatorId,
|
|
|
|
|
x.TaxID,
|
|
|
|
|
x.TaxRate,
|
|
|
|
|
x.OrgId,
|
|
|
|
|
x.SaleDeptId
|
|
|
|
|
x.SaleDeptId,
|
|
|
|
|
x.Category,
|
|
|
|
|
x.CategoryCode,
|
|
|
|
|
x.IsOverseasInvoice,
|
|
|
|
|
x.Payee,
|
|
|
|
|
x.Checker,
|
|
|
|
|
x.PushMode,
|
|
|
|
|
x.CellPhoneNO,
|
|
|
|
|
x.Email,
|
|
|
|
|
x.Note
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
if (invoice.Details?.Count > 0)
|
|
|
|
|