Commit 40c38680 by Neo Turing

增加发票申请及流程审批,发票关联到账等功能

parent c63f4bf2
......@@ -4,3 +4,4 @@
/.vs
/Src/obj
/Src/bin/Debug
using Kivii.Finances.Entities;
using Kivii.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kivii.Finances
{
public static class ApplyExtension
{
public static void ExcuteCompleted(this List<InvoiceApply> allApplys, IDbConnection conn = null)
{
if (allApplys.IsNullOrEmpty()) return;
var applys = allApplys.Where(o => o.AmountInvoice < o.Amount).ToList();
if (applys.IsNullOrEmpty()) return;
var applyKvids = applys.ConvertAll(p => p.Kvid);
bool useTransaction = conn == null;//是否启用事务,如果外部未传入conn,启用内部事务
if (conn == null) conn = KiviiContext.GetOpenedDbConnection<InvoiceApply>();
var queryInvoices = conn.From<Invoice>();
queryInvoices.Where(o => o.OffsetKvid == Guid.Empty && o.RootKvid == o.Kvid && Sql.In(o.ApplyKvid, applyKvids));
queryInvoices.Select(o => new { o.Kvid, o.Amount, o.ApplyKvid });
var invoices = conn.Select(queryInvoices);
if (invoices.IsNullOrEmpty()) return;
var relations = conn.Select<InvoiceApply>(o => o.OffsetKvid == Guid.Empty && o.OperateType == InvoiceApplyType.Related && Sql.In(o.ParentKvid, applyKvids));
List<InvoiceApply> preUpdates = new List<InvoiceApply>();
foreach (var apply in applys)
{
if (apply.AmountInvoice == apply.Amount) continue;
var currentInvoices = invoices.Where(o => o.ApplyKvid == apply.Kvid).ToList();
if (currentInvoices.IsNullOrEmpty()) continue;
var amountSum = currentInvoices.Sum(o => o.Amount);
List<InvoiceApply> applyRelations = null;
if (!relations.IsNullOrEmpty()) applyRelations = relations.Where(o => o.ParentKvid == apply.Kvid).ToList();
if (amountSum != apply.Amount) throw new Exception($"{apply.PayerName}:发票金额{currentInvoices.Sum(o => o.Amount)}和申请金额{apply.Amount}不一致");
apply.AmountInvoice = amountSum;
apply.AddOnlyProperties(o => o.AmountInvoice);
if (apply.AmountInvoice == apply.Amount)
{
apply.Status = (int)InvoiceApplyStatus.Completed;
apply.AddOnlyProperties(o => o.Status);
}
preUpdates.Add(apply);
if (!applyRelations.IsNullOrEmpty())
{
foreach (var item in applyRelations)
{
item.AmountInvoice = item.Amount;
item.AddOnlyProperties(o => o.AmountInvoice);
if (item.AmountInvoice == item.Amount)
{
item.Status = (int)InvoiceApplyStatus.Completed;
item.AddOnlyProperties(o => o.Status);
}
preUpdates.Add(item);
}
}
}
if (preUpdates.IsNullOrEmpty()) return;
IDbTransaction trans = null;//事务,如果外部来了Connection,不生成事务
if (useTransaction) trans = conn.OpenTransaction();
try
{
if (!preUpdates.IsNullOrEmpty())
{
foreach (var item in preUpdates)
{
if (!item.OnlyProperties.IsNullOrEmpty()) conn.UpdateOnly(item);
}
}
trans?.Commit();
}
catch (Exception ex)
{
trans?.Rollback();
throw ex;
}
}
}
}
......@@ -59,11 +59,13 @@
<Compile Include="Entities\InvoiceDetail.cs" />
<Compile Include="Entities\InvoiceTitle.cs" />
<Compile Include="Entities\Payment.cs" />
<Compile Include="Extensions\ApplyExtension.cs" />
<Compile Include="Extensions\Extension.cs" />
<Compile Include="Extensions\InvoiceExtension.cs" />
<Compile Include="Extensions\PaymentExtension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transforms\RestfulAccount.cs" />
<Compile Include="Transforms\RestfulApply.cs" />
<Compile Include="Transforms\RestfulInvoice.cs" />
<Compile Include="Transforms\RestfulInvoiceDetail.cs" />
<Compile Include="Transforms\RestfulInvoiceTitle.cs" />
......
......@@ -37,5 +37,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.4.2025.5070")]
[assembly: AssemblyFileVersion("5.4.2025.5070")]
[assembly: AssemblyVersion("5.4.2025.5220")]
[assembly: AssemblyFileVersion("5.4.2025.5220")]
......@@ -55,7 +55,6 @@ namespace Kivii.Finances.Transforms
(Payments.Exists(o => o.Amount <= 0)).ThrowIfTrue("未指定到账开票金额!");
(Payments.Sum(o => o.Amount) != Items.Sum(o => o.Amount)).ThrowIfTrue("到账开票金额和发票金额不一致!");
}
#region 启用缓存,将当前人创建批次数据存入,处理完毕或者接口报错后清除,确保不会重复创建
var cache = KiviiContext.GetCacheClient();
var cacheKey = KiviiContext.GetUrnKey($"{this.GetType().FullName}_Request_{KiviiContext.CurrentMember.FullName}_{KiviiContext.CurrentMember.Kvid}");
......@@ -72,6 +71,10 @@ namespace Kivii.Finances.Transforms
// 数据库连接初始化(移到外层以确保异常时能正确关闭)
var conn = KiviiContext.GetOpenedDbConnection<Invoice>();
var applyKvids = Items.ConvertAll(o => o.ApplyKvid).Distinct().ToList();
var applys = conn.SelectByIds<InvoiceApply>(applyKvids);
var trans = null as IDbTransaction;
var lockAcquired = false;
var lockName = $"invoice_lock_{KiviiContext.CurrentMember.Kvid}";
......@@ -144,7 +147,7 @@ namespace Kivii.Finances.Transforms
var invoice = new Invoice();
invoice.RootKvid = invoiceKvid;
invoice.Kvid = invoiceKvid;
//invoice.ApplyKvid = ApplyKvid;
invoice.ApplyKvid = info.ApplyKvid;
invoice.OwnerKvid = info.OwnerKvid;
invoice.OwnerName = info.OwnerName;
if (invoice.OwnerKvid == Guid.Empty && invoice.OwnerName.IsNullOrEmpty())
......@@ -320,7 +323,21 @@ namespace Kivii.Finances.Transforms
// 缓存清理失败仅记录警告
KiviiContext.Logger.Warn($"缓存清理失败: {cacheEx.Message}");
}
if (!applys.IsNullOrEmpty())
{
try
{
var th = KiviiContext.NewThread("发票申请标记完成", () =>
{
applys.ExcuteCompleted();
});
th.Start();
}
catch(Exception ex)
{
throw ex;
}
}
return rtns;
}
catch (Exception ex)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment