Commit b5df7b68 by 陶然

优化账单申请OA,新增发票回写接口

parent 4ba00004
......@@ -33,7 +33,46 @@ namespace Kivii.Finances.Seeyon.Extensions
}
}
public static FormMain_InvoiceApplication0710 ConvertToForm(this List<InvoiceApply> applys)
public static ResponseForm<ResponseData> RequestBillForm0710(this JsonServiceClient client, FormMain_InvoiceApplication0710 form, IAuthSession session)
{
if (client == null) throw new ArgumentNullException("client");
if (form == null) throw new ArgumentNullException("form");
var token = client.RequestRestToken(session.FullName);
var request = new RequestForm<RequestForm_InvoiceApply>();
request.appName = Configs.appName;//"collaboration";
request.data = new RequestData<RequestForm_InvoiceApply>();
request.data.templateCode = Configs.templateCode;//"JYHTSQ";
request.data.draft = "0";
//request.data.senderLoginName = token.bindingUser.loginName;
request.data.subject = $"预借发票申请({token.bindingUser.name} {DateTime.Now:yyyy-MM-dd HH:mm})";
request.data.data = new RequestForm_InvoiceApply();
request.data.data.formmain_0710 = new FormMain_InvoiceApplication0710();
request.data.data.formmain_0710 = form;
var rtns = client.Post<ResponseForm<ResponseData>>($"{Configs.RouteRequestForm}?token={token.id}", request);
return rtns;
}
public static ResponseForm<ResponseData> RequestBillForm0712(this JsonServiceClient client, FormMain_AdvanceInvoice0712 form, IAuthSession session)
{
if (client == null) throw new ArgumentNullException("client");
if (form == null) throw new ArgumentNullException("form");
var token = client.RequestRestToken(session.FullName);
var request = new RequestForm<RequestForm_InvoiceApply>();
request.appName = Configs.appName;//"collaboration";
request.data = new RequestData<RequestForm_InvoiceApply>();
request.data.templateCode = Configs.templateCode;//"JYHTSQ";
request.data.draft = "0";
//request.data.senderLoginName = token.bindingUser.loginName;
request.data.subject = $"预借发票申请({token.bindingUser.name} {DateTime.Now:yyyy-MM-dd HH:mm})";
request.data.data = new RequestForm_InvoiceApply();
request.data.data.formmain_0712 = new FormMain_AdvanceInvoice0712();
request.data.data.formmain_0712 = form;
var rtns = client.Post<ResponseForm<ResponseData>>($"{Configs.RouteRequestForm}?token={token.id}", request);
return rtns;
}
public static FormMain_InvoiceApplication0710 PayedConvertToForm(this List<InvoiceApply> applys)
{
var apply = applys[0];
var bizKvids = applys.ConvertAll(o => o.Kvid.ToString());
......@@ -47,21 +86,24 @@ namespace Kivii.Finances.Seeyon.Extensions
rtns.申请日期 = DateTime.Now.ToString("yyyy-MM-dd");
rtns.申请部门 = KiviiContext.CurrentMember.DepartmentName;
rtns.申请人 = apply.OperatorName;
rtns.票据类型 = apply.Type == "增值税专用发票" ? "-4189929216282050282" : "4529935671729733794";
rtns.数量 = "1";
rtns.总金额 = amount.ToString();
rtns.金额大写 = amount.ConvertToChinese();
rtns.付款单位名称 = apply.PayerName;
rtns.纳税人识别号 = apply.PayerTaxNumber;
rtns.地址电话 = $"{apply.PayerCompanyAddress}{apply.PayerPhone}";
rtns.开户行及账号 = $"{apply.PayerRegisteredBank}{apply.PayerBankAccount}";
rtns.发票类型 = apply.Type == "增值税专用发票" ? "-4189929216282050282" : "4529935671729733794";
rtns.收款单位 = apply.PayeeName;
rtns.收款单位纳税人识别号 = apply.PayeeTaxNumber;
rtns.收款单位地址电话 = $"{apply.PayeeCompanyAddress}{apply.PayeePhone}";
rtns.收款单位开户行及账号 = $"{apply.PayeeRegisteredBank}{apply.PayeeBankAccount}";
rtns.申请原因 = apply.Remark;
rtns.数量 = "1";
rtns.总金额 = amount.ToString();
rtns.金额大写 = amount.ConvertToChinese();
rtns.收款单位 = apply.PayeeName;
return rtns;
}
public static FormMain_InvoiceApplication0710 ConvertToForm(this InvoiceApply apply)
public static FormMain_InvoiceApplication0710 PayedConvertToForm(this InvoiceApply apply)
{
var bizKvids = new List<string>();
bizKvids.Add(apply.Kvid.ToString());
......@@ -73,38 +115,84 @@ namespace Kivii.Finances.Seeyon.Extensions
rtns.申请日期 = DateTime.Now.ToString("yyyy-MM-dd");
rtns.申请部门 = KiviiContext.CurrentMember.DepartmentName;
rtns.申请人 = apply.OperatorName;
rtns.票据类型 = apply.Type == "增值税专用发票" ? "-4189929216282050282" : "4529935671729733794";
rtns.数量 = "1";
rtns.总金额 = apply.Amount.ToString();
rtns.金额大写 = apply.Amount.ConvertToChinese();
rtns.付款单位名称 = apply.PayerName;
rtns.纳税人识别号 = apply.PayerTaxNumber;
rtns.地址电话 = $"{apply.PayerCompanyAddress}{apply.PayerPhone}";
rtns.开户行及账号 = $"{apply.PayerRegisteredBank}{apply.PayerBankAccount}";
rtns.发票类型 = apply.Type == "增值税专用发票" ? "-4189929216282050282" : "4529935671729733794";
rtns.收款单位 = apply.PayeeName;
rtns.收款单位纳税人识别号 = apply.PayeeTaxNumber;
rtns.收款单位地址电话 = $"{apply.PayeeCompanyAddress}{apply.PayeePhone}";
rtns.收款单位开户行及账号 = $"{apply.PayeeRegisteredBank}{apply.PayeeBankAccount}";
rtns.申请原因 = apply.Remark;
rtns.收款单位 = apply.PayeeName;
return rtns;
}
public static FormMain_AdvanceInvoice0712 DebitConvertToForm(this List<InvoiceApply> applys)
{
var apply = applys[0];
var bizKvids = applys.ConvertAll(o => o.Kvid.ToString());
//var strBizKvids = JsonSerializer.SerializeToString(bizKvids);
var amount = applys.Sum(o => o.Amount);
var rtns = new FormMain_AdvanceInvoice0712();
//var settlementType = "code|MonthlySettlement";//默认月结
rtns.业务类型 = typeof(BillApply).FullName;
rtns.业务编号 = bizKvids;
rtns.单据编号 = apply.SerialNumber;
rtns.申请日期 = DateTime.Now.ToString("yyyy-MM-dd");
rtns.申请部门 = KiviiContext.CurrentMember.DepartmentName;
rtns.申请人 = apply.OperatorName;
rtns.票据类型 = apply.Type == "增值税专用发票" ? "-4189929216282050282" : "4529935671729733794";
rtns.数量 = "1";
rtns.总金额 = apply.Amount.ToString();
rtns.金额大写 = apply.Amount.ConvertToChinese();
rtns.总金额 = amount.ToString();
rtns.金额大写 = amount.ConvertToChinese();
rtns.付款单位名称 = apply.PayerName;
rtns.纳税人识别号 = apply.PayerTaxNumber;
rtns.地址电话 = $"{apply.PayerCompanyAddress}{apply.PayerPhone}";
rtns.开户行及账号 = $"{apply.PayerRegisteredBank}{apply.PayerBankAccount}";
rtns.收款单位 = apply.PayeeName;
rtns.收款单位纳税人识别号 = apply.PayeeTaxNumber;
rtns.收款单位地址电话 = $"{apply.PayeeCompanyAddress}{apply.PayeePhone}";
rtns.收款单位开户行及账号 = $"{apply.PayeeRegisteredBank}{apply.PayeeBankAccount}";
rtns.申请原因 = apply.Remark;
rtns.收款单位 = apply.PayeeName;
return rtns;
}
public static ResponseForm<ResponseData> RequestBillForm(this JsonServiceClient client, FormMain_InvoiceApplication0710 form, IAuthSession session)
public static FormMain_AdvanceInvoice0712 DebitConvertToForm(this InvoiceApply apply)
{
if (client == null) throw new ArgumentNullException("client");
if (form == null) throw new ArgumentNullException("form");
var token = client.RequestRestToken(session.FullName);
var request = new RequestForm<RequestForm_InvoiceApply>();
request.appName = Configs.appName;//"collaboration";
request.data = new RequestData<RequestForm_InvoiceApply>();
request.data.templateCode = Configs.templateCode;//"JYHTSQ";
request.data.draft = "0";
//request.data.senderLoginName = token.bindingUser.loginName;
request.data.subject = $"预借发票申请({token.bindingUser.name} {DateTime.Now:yyyy-MM-dd HH:mm})";
request.data.data = new RequestForm_InvoiceApply();
request.data.data.formmain_0710 = new FormMain_InvoiceApplication0710();
request.data.data.formmain_0710 = form;
var rtns = client.Post<ResponseForm<ResponseData>>($"{Configs.RouteRequestForm}?token={token.id}", request);
var bizKvids = new List<string>();
bizKvids.Add(apply.Kvid.ToString());
var rtns = new FormMain_AdvanceInvoice0712();
//var settlementType = "code|MonthlySettlement";//默认月结
rtns.业务类型 = typeof(BillApply).FullName;
rtns.业务编号 = bizKvids;
rtns.单据编号 = apply.SerialNumber;
rtns.申请日期 = DateTime.Now.ToString("yyyy-MM-dd");
rtns.申请部门 = KiviiContext.CurrentMember.DepartmentName;
rtns.申请人 = apply.OperatorName;
rtns.票据类型 = apply.Type == "增值税专用发票" ? "-4189929216282050282" : "4529935671729733794";
rtns.数量 = "1";
rtns.总金额 = apply.Amount.ToString();
rtns.金额大写 = apply.Amount.ConvertToChinese();
rtns.付款单位名称 = apply.PayerName;
rtns.纳税人识别号 = apply.PayerTaxNumber;
rtns.地址电话 = $"{apply.PayerCompanyAddress}{apply.PayerPhone}";
rtns.开户行及账号 = $"{apply.PayerRegisteredBank}{apply.PayerBankAccount}";
rtns.收款单位 = apply.PayeeName;
rtns.收款单位纳税人识别号 = apply.PayeeTaxNumber;
rtns.收款单位地址电话 = $"{apply.PayeeCompanyAddress}{apply.PayeePhone}";
rtns.收款单位开户行及账号 = $"{apply.PayeeRegisteredBank}{apply.PayeeBankAccount}";
rtns.申请原因 = apply.Remark;
rtns.收款单位 = apply.PayeeName;
return rtns;
}
public static string ConvertToChinese(this decimal Num)
{
string[] DX_SZ = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" };//大写数字
......@@ -177,6 +265,5 @@ namespace Kivii.Finances.Seeyon.Extensions
return NumStr_R;
}
}
}
......@@ -15,8 +15,11 @@ namespace Kivii.Finances.Seeyon.Entities
{
[Api(Description = "账单申请")]
[Alias(Configs.TableNameApply)]
internal class BillApply : InvoiceApply, IEntityIsSeeyonApply
public class BillApply : InvoiceApply, IEntityIsSeeyonApply
{
[Ignore]
public List<Guid> Kvids { get; set; }
public object OnGetApplyResult(Guid Kvid, object Result, string Message)
{
if (Kvid == Guid.Empty) throw new Exception("缺少业务编号!");
......@@ -40,7 +43,7 @@ namespace Kivii.Finances.Seeyon.Entities
log = new EntityLog<InvoiceApply>();
log.OwnerKvid = apply.Kvid;
log.Title = "账单借票";
log.Type = "流转";
log.Type = "OA申请";
log.Summary = "OA审批通过,可进行开票";
log.Remark = Message;
log.CreatorName = "OA审批";
......@@ -58,7 +61,7 @@ namespace Kivii.Finances.Seeyon.Entities
log = new EntityLog<InvoiceApply>();
log.OwnerKvid = apply.Kvid;
log.Title = "账单借票";
log.Type = "流转";
log.Type = "OA申请";
log.Summary = "OA审批不通过,驳回了账单借票审批";
log.Remark = Message;
log.CreatorName = "OA审批";
......@@ -101,7 +104,7 @@ namespace Kivii.Finances.Seeyon.Entities
log = new EntityLog<InvoiceApply>();
log.OwnerKvid = apply.Kvid;
log.Title = "账单借票";
log.Type = "流转";
log.Type = "OA申请";
log.Summary = "OA审批通过,可进行开票";
log.Remark = Message;
log.CreatorName = "OA审批";
......@@ -119,7 +122,7 @@ namespace Kivii.Finances.Seeyon.Entities
log = new EntityLog<InvoiceApply>();
log.OwnerKvid = apply.Kvid;
log.Title = "账单借票";
log.Type = "流转";
log.Type = "OA申请";
log.Summary = "OA审批不通过,驳回了账单借票审批";
log.Remark = Message;
log.CreatorName = "OA审批";
......@@ -142,11 +145,16 @@ namespace Kivii.Finances.Seeyon.Entities
InvoiceApply apply = conn.SingleById<InvoiceApply>(Kvid);
apply.ThrowIfNull("未找到此业务信息!");
if (apply.Status > (int)InvoiceApplyStatus.ProcessAdoption) throw new Exception("当前账单借票非待审批状态,无法申请审批!");
var applyForm = apply.ConvertToForm();
var session = KiviiContext.Request.GetSession();
var rtns = new RestfulUpdateResponse<InvoiceApply>();
rtns.Results = new List<InvoiceApply>();
var client = new JsonServiceClient(Configs.BaseUrl);
var resp = client.RequestBillForm(applyForm, session);
if (apply.OperateType == InvoiceApplyType.Payment)
{
var applyForm = apply.PayedConvertToForm();
var resp = client.RequestBillForm0710(applyForm, session);
if (resp.code != "0") throw new Exception($"OA接口调用失败,原因:{resp.message}");
apply.BizId = resp.data.subject;
apply.AddOnlyProperties(o => o.BizId);
......@@ -157,13 +165,35 @@ namespace Kivii.Finances.Seeyon.Entities
conn.UpdateOnly(apply);
var _log = new EntityLog<InvoiceApply>();
_log.OwnerKvid = apply.Kvid;
_log.Title = "账单借票";
_log.Type = "流转";
_log.Summary = $"提交了账单借票,审批将在OA执行";
_log.Title = "账单到账开票申请";
_log.Type = "OA申请";
_log.Summary = $"提交了账单到账开票申请,审批将在OA执行";
conn.Insert(_log);
var rtns = new RestfulUpdateResponse<InvoiceApply>();
rtns.Results = new List<InvoiceApply>();
rtns.Results.Add(apply);
}
else
{
var applyForm = apply.DebitConvertToForm();
var resp = client.RequestBillForm0712(applyForm, session);
if (resp.code != "0") throw new Exception($"OA接口调用失败,原因:{resp.message}");
apply.BizId = resp.data.subject;
apply.AddOnlyProperties(o => o.BizId);
apply.BizType = typeof(BillApply).FullName;
apply.AddOnlyProperties(o => o.BizType);
apply.Status = (int)InvoiceApplyStatus.ProcessAdoption;
apply.AddOnlyProperties(o => o.Status);
conn.UpdateOnly(apply);
var _log = new EntityLog<InvoiceApply>();
_log.OwnerKvid = apply.Kvid;
_log.Title = "账单借票申请";
_log.Type = "OA申请";
_log.Summary = $"提交了账单借票申请,审批将在OA执行";
conn.Insert(_log);
rtns.Results.Add(apply);
}
return rtns;
}
......@@ -181,24 +211,19 @@ namespace Kivii.Finances.Seeyon.Entities
var groupPayer = applys.GroupBy(a => a.PayerName);
if (groupPayer.Count() != 1) throw new Exception("不允许多个借票抬头同时申请!");
//var applySum = new InvoiceApply();
//applySum.PopulateWith(applys[0]);
//applySum.Amount = applys.Sum(a => a.Amount);
//applySum.Remark = applys[0].Remark;
var applyForm = applys.ConvertToForm();
//var bizKvids = applys.ConvertAll(o => o.Kvid);
//var strBizKvids = JsonSerializer.SerializeToString(bizKvids);
//applyForm.业务编号 = strBizKvids;
var groupType = applys.GroupBy(o => o.OperateType);
if (groupType.Count() != 1) throw new Exception("不允许多个申请类型同时申请!");
var session = KiviiContext.Request.GetSession();
var client = new JsonServiceClient(Configs.BaseUrl);
var resp = client.RequestBillForm(applyForm, session);
if (resp.code != "0") throw new Exception($"OA接口调用失败,原因:{resp.message}");
var rtns = new RestfulUpdateResponse<InvoiceApply>();
rtns.Results = new List<InvoiceApply>();
var client = new JsonServiceClient(Configs.BaseUrl);
if (groupType.FirstOrDefault().Key == InvoiceApplyType.Payment)
{
var applyForm = applys.PayedConvertToForm();
var resp = client.RequestBillForm0710(applyForm, session);
if (resp.code != "0") throw new Exception($"OA接口调用失败,原因:{resp.message}");
foreach (var apply in applys)
{
apply.BizId = resp.data.subject;
......@@ -210,27 +235,50 @@ namespace Kivii.Finances.Seeyon.Entities
conn.UpdateOnly(apply);
var _log = new EntityLog<InvoiceApply>();
_log.OwnerKvid = apply.Kvid;
_log.Title = "账单借票";
_log.Type = "流转";
_log.Summary = $"提交了账单借票,审批将在OA执行";
_log.Title = "账单到账开票申请";
_log.Type = "OA申请";
_log.Summary = $"提交了账单到账开票申请,审批将在OA执行";
conn.Insert(_log);
rtns.Results.Add(apply);
}
}
else
{
var applyForm = applys.DebitConvertToForm();
var resp = client.RequestBillForm0712(applyForm, session);
if (resp.code != "0") throw new Exception($"OA接口调用失败,原因:{resp.message}");
foreach (var apply in applys)
{
apply.BizId = resp.data.subject;
apply.AddOnlyProperties(o => o.BizId);
apply.BizType = typeof(BillApply).FullName;
apply.AddOnlyProperties(o => o.BizType);
apply.Status = (int)InvoiceApplyStatus.ProcessAdoption;
apply.AddOnlyProperties(o => o.Status);
conn.UpdateOnly(apply);
var _log = new EntityLog<InvoiceApply>();
_log.OwnerKvid = apply.Kvid;
_log.Title = "账单借票申请";
_log.Type = "OA申请";
_log.Summary = $"提交了账单借票申请,审批将在OA执行";
conn.Insert(_log);
rtns.Results.Add(apply);
}
}
return rtns;
}
[Ignore]
public List<Guid> Kvids { get; set; }
}
public class RequestForm_InvoiceApply
{
public FormMain_InvoiceApplication0710 formmain_0710 { get; set; }
public FormMain_AdvanceInvoice0712 formmain_0712 { get; set; }
}
#region OA表单
/// <summary>
/// 发起流程表单接口【到账发票申请】
/// </summary>
......@@ -259,6 +307,15 @@ namespace Kivii.Finances.Seeyon.Entities
public string 申请人 { get; set; }
/// <summary>
/// 票据类型{普通:4529935671729733794 专票:-4189929216282050282}
/// </summary>
public string 票据类型 { get; set; }
public string 数量 { get; set; }
public string 总金额 { get; set; }
public string 付款单位名称 { get; set; }
public string 纳税人识别号 { get; set; }
......@@ -267,26 +324,24 @@ namespace Kivii.Finances.Seeyon.Entities
public string 开户行及账号 { get; set; }
/// <summary>
/// 发票类型{普通:4529935671729733794 专票:-4189929216282050282}
/// </summary>
public string 发票类型 { get; set; }
public string 收款单位 { get; set; }
public string 申请原因 { get; set; }
public string 收款单位纳税人识别号 { get; set; }
public string 数量 { get; set; }
public string 收款单位地址电话 { get; set; }
public string 总金额 { get; set; }
public string 收款单位开户行及账号 { get; set; }
public string 金额大写 { get; set; }
public string 收款单位 { get; set; }
public string 申请原因 { get; set; }
}
/// <summary>
/// 发起流程表单接口【发票当月作废申请】
/// 发起流程表单接口【预借发票/收据申请】
/// </summary>
public class FormMain_InvoiceVoid0712
public class FormMain_AdvanceInvoice0712
{
/// <summary>
/// 唯一编号,BizKvid
......@@ -311,24 +366,41 @@ namespace Kivii.Finances.Seeyon.Entities
public string 申请人 { get; set; }
public string 原发票号 { get; set; }
/// <summary>
/// 原发票类型{普通:4529935671729733794 专票:-4189929216282050282}
/// 票据类型{普通:4529935671729733794 专票:-4189929216282050282}
/// </summary>
public string 原发票类型 { get; set; }
public string 票据类型 { get; set; }
public string 原发票开票单位 { get; set; }
public string 数量 { get; set; }
public string 原因 { get; set; }
public string 总金额 { get; set; }
public string 申请原因 { get; set; }
public string 付款单位名称 { get; set; }
public string 纳税人识别号 { get; set; }
public string 地址电话 { get; set; }
public string 开户行及账号 { get; set; }
public string 收款单位 { get; set; }
public string 收款单位纳税人识别号 { get; set; }
public string 收款单位地址电话 { get; set; }
public string 收款单位开户行及账号 { get; set; }
public string 金额大写 { get; set; }
public string 原开票金额 { get; set; }
}
/// <summary>
/// 发起流程表单接口【跨月发票红冲申请】
/// 发起流程表单接口【发票当月作废申请】
/// </summary>
public class FormMain_InvoiceRedOffset0338
public class FormMain_InvoiceVoid0712
{
/// <summary>
/// 唯一编号,BizKvid
......@@ -353,13 +425,6 @@ namespace Kivii.Finances.Seeyon.Entities
public string 申请人 { get; set; }
/// <summary>
/// 是:-9179045864512534208 否:-7622688603542042575
/// </summary>
public string 是否为预借发票 { get; set; }
public string 原因 { get; set; }
public string 原发票号 { get; set; }
/// <summary>
......@@ -369,16 +434,15 @@ namespace Kivii.Finances.Seeyon.Entities
public string 原发票开票单位 { get; set; }
public string 原因 { get; set; }
public string 原开票金额 { get; set; }
public string 冲红发票号码 { get; set; }
}
/// <summary>
/// 发起流程表单接口【预借发票/收据申请】
/// 发起流程表单接口【跨月发票红冲申请】
/// </summary>
public class FormMain_AdvanceInvoice0712
public class FormMain_InvoiceRedOffset0338
{
/// <summary>
/// 唯一编号,BizKvid
......@@ -397,31 +461,32 @@ namespace Kivii.Finances.Seeyon.Entities
/// <summary>
/// 格式:2023-10-11
/// </summary>
public string 日期 { get; set; }
public string 所属部门 { get; set; }
public string 申请日期 { get; set; }
public string 经办人 { get; set; }
public string 申请部门 { get; set; }
public string 付款单位名称 { get; set; }
public string 申请人 { get; set; }
public string 纳税人识别号 { get; set; }
/// <summary>
/// 是:-9179045864512534208 否:-7622688603542042575
/// </summary>
public string 是否为预借发票 { get; set; }
public string 地址电话 { get; set; }
public string 原因 { get; set; }
public string 开户行及账 { get; set; }
public string 原发票 { get; set; }
/// <summary>
/// 发票类型{普通:4529935671729733794 专票:-4189929216282050282}
/// 发票类型{普通:4529935671729733794 专票:-4189929216282050282}
/// </summary>
public string 发票类型 { get; set; }
public string 发票类型 { get; set; }
public string 申请原因 { get; set; }
public string 原发票开票单位 { get; set; }
public string 数量 { get; set; }
public string 金额 { get; set; }
public string 原开票金额 { get; set; }
public string 收款单位 { get; set; }
public string 冲红发票号码 { get; set; }
}
#endregion
}
......@@ -54,6 +54,7 @@
<Compile Include="Configs.cs" />
<Compile Include="Entities\BillApply.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RestfulBillApply.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
using Kivii.Finances.Entities;
using Kivii.Finances.Seeyon.Entities;
using Kivii.Linq;
using Kivii.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kivii.Finances.Seeyon
{
[Api(Description = "OA发票回写调用")]
public class BillApplyResult: RestfulExecution<BillApply>
{
public Guid Kvid { get; set; }//InvoiceApplyKvid
public List<Guid> Kvids { get; set; }//InvoiceApplyKvids
public string Result { get; set; }// 审批通过/审批不通过
public List<Invoice> Invoices { get; set; }// 发票信息,最少要有发票号码和开票时间
public string Message { get; set; }
public override object OnExecution(IRequest req, IResponse res)
{
if (Kvid == Guid.Empty && Kvids.IsNullOrEmpty()) throw new Exception("业务编号不能为空!");
if (Kvids.IsNullOrEmpty()) Kvids = new List<Guid>();
if (Kvid != Guid.Empty) Kvids.Add(Kvid);
if(Result.IsNullOrEmpty()) throw new Exception("请传入审批结果:【审批通过/审批不通过】!");
var conn = KiviiContext.GetOpenedDbConnection<InvoiceApply>();
var applys = conn.SelectByIds<InvoiceApply>(Kvids);
if (applys.IsNullOrEmpty()) throw new Exception("未找到指定的申请信息!");
if (applys.Exists(o => o.Status > (int)InvoiceApplyStatus.AutoExecuting)) throw new Exception("申请信息状态发生变动,无法回写!");
var rtns = new RestfulExecutionResponse<InvoiceApply>();
rtns.Results = new List<InvoiceApply>();
if (Result == "审批通过")
{
var trans = conn.OpenTransaction();
try
{
if (Invoices.IsNullOrEmpty())
{
foreach (var apply in applys)
{
if (apply.Metadata.IsNullOrEmpty()) apply.Metadata = new Dictionary<string, string>();
apply.Metadata["SeeyonResult"] = Result;
apply.Metadata["SeeyonMessage"] = Message;
apply.AddOnlyProperties(o => o.Metadata);
apply.Status = (int)InvoiceApplyStatus.FinancialApproval;
apply.AddOnlyProperties(o => o.Status);
conn.UpdateOnly(apply);
rtns.Results.Add(apply);
var log = new EntityLog<InvoiceApply>();
log.OwnerKvid = apply.Kvid;
log.Title = "账单借票";
log.Type = "OA申请";
log.Summary = "OA审批通过,可进行开票";
log.Remark = Message;
log.CreatorName = "OA审批";
conn.Insert(log);
}
}//回写没传入发票信息,那就更新对应申请状态
else if (applys.Count != 1)
{
foreach (var apply in applys)
{
if (apply.Metadata.IsNullOrEmpty()) apply.Metadata = new Dictionary<string, string>();
apply.Metadata["SeeyonResult"] = Result;
apply.Metadata["SeeyonMessage"] = Message;
apply.AddOnlyProperties(o => o.Metadata);
apply.Status = (int)InvoiceApplyStatus.FinancialApproval;
apply.AddOnlyProperties(o => o.Status);
conn.UpdateOnly(apply);
rtns.Results.Add(apply);
var log = new EntityLog<InvoiceApply>();
log.OwnerKvid = apply.Kvid;
log.Title = "账单借票";
log.Type = "OA申请";
log.Summary = "OA审批通过,可进行开票";
log.Remark = Message;
log.CreatorName = "OA审批";
conn.Insert(log);
}
}//如果申请超过1条,那也不做处理,只更新申请状态
else//只有一个申请的时候,那就把对应的发票记录下来
{
//var logs = new List<EntityLog<InvoiceApply>>();
var updateApplys = new List<InvoiceApply>();
var insertInvoices = new List<Invoice>();
var insertInvoiceDetails = new List<InvoiceDetail>();
var apply = applys[0];
(apply.Amount - apply.AmountInvoice != Invoices.Sum(o => o.Amount)).ThrowIfTrue("当前申请金额不等于要录入的发票金额!");
var applyRelations = conn.Select<InvoiceApply>(o => o.ParentKvid == apply.Kvid && o.OperateType == InvoiceApplyType.Related && o.OffsetKvid == Guid.Empty);
foreach (var info in Invoices)
{
#region 开票成功的情况处理
var time = info.OperateTime == DateTime.MinValue ? DateTime.Now : info.OperateTime;//开票时间
var number = info.SerialNumber;//开票号码,唯一
var type = apply.Type;//开票类型
var currentAmount = info.Amount;
var taxRate = 0.03m;
var invoiceKvid = Guid.NewGuid();
var invoice = new Invoice();
invoice.RootKvid = invoiceKvid;
invoice.Kvid = invoiceKvid;
invoice.ApplyKvid = apply.Kvid;
invoice.OwnerKvid = apply.OwnerKvid;
invoice.OwnerName = apply.OwnerName;
invoice.Currency = CurrencyUnit.CNY;
invoice.Type = type;
invoice.Category = apply.OperateType.ToString();
invoice.OperateTime = time;
invoice.SerialNumber = number;
invoice.Amount = currentAmount;
invoice.AmountUntaxed = Math.Round(currentAmount / (1 + taxRate), 2);
invoice.AmountTax = currentAmount - invoice.AmountUntaxed;
invoice.TaxRate = taxRate;
invoice.PayeeKvid = apply.PayeeKvid;
invoice.PayeeName = apply.PayeeName;
invoice.PayeeTaxNumber = apply.PayeeTaxNumber;
invoice.PayeeCompanyAddress = apply.PayeeCompanyAddress;
invoice.PayeePhone = apply.PayeePhone;
invoice.PayeeRegisteredBank = apply.PayeeRegisteredBank;
invoice.PayeeBankAccount = apply.PayeeBankAccount;
invoice.PayerKvid = apply.PayerKvid;
invoice.PayerName = apply.PayerName;
invoice.PayerTaxNumber = apply.PayerTaxNumber;
invoice.PayerCompanyAddress = apply.PayerCompanyAddress;
invoice.PayerPhone = apply.PayerPhone;
invoice.PayerRegisteredBank = apply.PayerRegisteredBank;
invoice.PayerBankAccount = apply.PayerBankAccount;
invoice.OperatorName = apply.OperatorName;
invoice.Remark = apply.Remark;
invoice.Summary = "OA回传发票信息!";
invoice.Metadata = new Dictionary<string, string>();
invoice.Metadata["PayeeOperatorName"] = apply.Metadata.ContainsKey("PayeeOperatorName") ? apply.Metadata["PayeeOperatorName"] : "";
invoice.Metadata["ReviewerName"] = apply.Metadata.ContainsKey("ReviewerName") ? apply.Metadata["ReviewerName"] : "";
insertInvoices.Add(invoice);
//conn.Insert(invoice);
//foreach (var infoDetail in info.Details)
//{
var detail = new InvoiceDetail();
//detail.PopulateInstance(infoDetail);
detail.InvoiceKvid = invoiceKvid;
detail.TaxRate = invoice.TaxRate;
detail.GoodsFullName = "发票";
detail.AmountUntaxed = invoice.AmountUntaxed;
detail.Amount = invoice.Amount;
detail.AmountTax = invoice.AmountTax;
detail.Quantity = 1;
detail.QuantityUnitPrice = Math.Round(detail.Amount / detail.Quantity, 2);
detail.QuantityUnitPriceUntaxed = Math.Round(detail.AmountUntaxed / detail.Quantity, 2);
insertInvoiceDetails.Add(detail);
//}
#endregion
#region 更新申请状态
if (!applyRelations.IsNullOrEmpty())
{
foreach (var item in applyRelations)
{
//无需更新
if (item.AmountInvoice >= item.Amount) continue;
var relationAmount = item.Amount - item.AmountInvoice;
var amount = relationAmount <= invoice.Amount ? relationAmount : invoice.Amount;
item.AmountInvoice += amount;
item.AddOnlyProperties(o => o.AmountInvoice);
if (item.AmountInvoice == item.Amount)
{
item.Status = (int)InvoiceApplyStatus.Completed;
item.AddOnlyProperties(o => o.Status);
}
updateApplys.Add(item);
//conn.UpdateOnly(item);
}
}
apply.AmountInvoice += invoice.Amount;
apply.AddOnlyProperties(o => o.AmountInvoice);
if (apply.AmountInvoice == apply.Amount)
{
apply.Status = (int)InvoiceApplyStatus.Completed;
apply.AddOnlyProperties(o => o.Status);
}
updateApplys.Add(apply);
//conn.UpdateOnly(apply);
#endregion
}
#region 收款与发票关联
var payments = conn.Select<Payment>(o => Sql.In(o.Kvid, applyRelations.ConvertAll(p => p.BizKvid)));//Type:Bank,Split,Pos,Cash,WeChat...
List<Payment> parentPayments = new List<Payment>();//也要更新父级的AmountInvoice
if (!payments.IsNullOrEmpty())
{
var correlatedInvoices = insertInvoices.Correlating(payments);
insertInvoices.AddRange(correlatedInvoices);
var currentRootKvids = payments.Where(o => o.Type == PaymentType.Split).ToList().ConvertAll(p => p.RootKvid);
if (!currentRootKvids.IsNullOrEmpty()) parentPayments = conn.Select<Payment>(o => o.OffsetKvid == Guid.Empty && Sql.In(o.Kvid, currentRootKvids));
}
#endregion
if (!updateApplys.IsNullOrEmpty())
{
updateApplys.ForEach(o => conn.UpdateOnly(o));
rtns.Results.AddRange(updateApplys);
}
if (!insertInvoices.IsNullOrEmpty())
{
foreach (var item in insertInvoices)
{
if (item.Type == "Relation") item.SerialNumber = item.GetSubSerialNumber(conn);
conn.Insert(item);
}
}
if (!insertInvoiceDetails.IsNullOrEmpty()) insertInvoiceDetails.ForEach(o => conn.Insert(o));
if (!payments.IsNullOrEmpty())
{
payments.ForEach(o => conn.UpdateOnly(o));
}
if (!parentPayments.IsNullOrEmpty())
{
parentPayments.ForEach(o => o.RecalculateAmountInvoice(conn));
}
}
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
else
{
var trans = conn.OpenTransaction();
try
{
foreach (var apply in applys)
{
if (apply.Metadata.IsNullOrEmpty()) apply.Metadata = new Dictionary<string, string>();
apply.Metadata["SeeyonResult"] = Result;
apply.Metadata["SeeyonMessage"] = Message;
apply.AddOnlyProperties(o => o.Metadata);
apply.Status = (int)InvoiceApplyStatus.ApplyReject;
apply.AddOnlyProperties(o => o.Status);
conn.UpdateOnly(apply);
rtns.Results.Add(apply);
var log = new EntityLog<InvoiceApply>();
log.OwnerKvid = apply.Kvid;
log.Title = "账单借票";
log.Type = "OA申请";
log.Summary = "OA审批不通过,驳回了账单借票审批";
log.Remark = Message;
log.CreatorName = "OA审批";
conn.Insert(log);
}
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
return rtns;
}
}
}
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