Commit 86829e4a by 陶然

优化接口, 新增账单申请借票接口

parent d039a180
......@@ -83,6 +83,7 @@
<Compile Include="Transforms\RestfulAccount.cs" />
<Compile Include="Transforms\RestfulAccountDetail.cs" />
<Compile Include="Transforms\RestfulBill.cs" />
<Compile Include="Transforms\RestfulBill.InvoiceApply.cs" />
<Compile Include="Transforms\RestfulInvoice.cs" />
<Compile Include="Transforms\RestfulInvoice.Debit.cs" />
<Compile Include="Transforms\RestfulInvoice.Offset.Deficit.cs" />
......
......@@ -33,5 +33,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.4.2023.9150")]
[assembly: AssemblyFileVersion("5.4.2023.9150")]
[assembly: AssemblyVersion("5.4.2023.9210")]
[assembly: AssemblyFileVersion("5.4.2023.9210")]
using Kivii.Finances.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.Transforms
{
[Api(Description = "账单转发票申请")]
[RequiresAnyRole(SystemRoles.Everyone)]
public class BillConvertToApply : RestfulExecution<Bill>
{
public Bill Item { get; set; }
public List<Bill> Items { get; set; }
/// <summary>
/// 账单合并申请
/// </summary>
public bool Merge { get; set; } = false;
public override object OnExecution(IRequest req, IResponse res)
{
(Item == null && Items.IsNullOrEmpty()).ThrowIfTrue("请传入要申请的账单信息!");
if (Items.IsNullOrEmpty()) Items = new List<Bill>();
if (Item != null) Items.Add(Item);
var rtns = new RestfulCreateResponse<InvoiceApply>();
rtns.Results = new List<InvoiceApply>();
if (Merge)
{
var apply = new InvoiceApply();
apply.OwnerKvid = KiviiContext.CurrentMember.DepartmentKvid;
apply.OwnerName = KiviiContext.CurrentMember.DepartmentName;
apply.OperatorName = KiviiContext.CurrentMember.FullName;
apply.OperatorKvid = KiviiContext.CurrentMember.Kvid;
apply.OperateTime = DateTime.Now;
apply.Currency = Items[0].Currency;
apply.Type = "增值税专用发票";
apply.Amount = Items.Sum(o => o.Amount);
apply.PayerName = Items[0].PayerName;
apply.PayeeName = Items[0].PayeeName;
apply.OperateType = InvoiceApplyType.Debit;
apply.Details = new List<InvoiceApplyDetail>();
var detail = new InvoiceApplyDetail();
detail.GoodsFullName = "检测费";
detail.GoodsModel = "3040601000000000000";
detail.TaxRate = (decimal)0.03;
detail.Quantity = 1;
detail.Amount = apply.Amount;
detail.QuantityUnit = "次";
detail.AmountUntaxed = detail.Amount / (1 + detail.TaxRate);
detail.AmountTax = detail.Amount - detail.AmountUntaxed;
detail.QuantityUnitPrice = detail.Amount / detail.Quantity;
detail.QuantityUnitPriceUntaxed = detail.AmountUntaxed / detail.Quantity;
apply.Details.Add(detail);
rtns.Results.Add(apply);
}
else
{
foreach (var item in Items)
{
var apply = new InvoiceApply();
apply.OwnerKvid = KiviiContext.CurrentMember.DepartmentKvid;
apply.OwnerName = KiviiContext.CurrentMember.DepartmentName;
apply.OperatorName = KiviiContext.CurrentMember.FullName;
apply.OperatorKvid = KiviiContext.CurrentMember.Kvid;
apply.OperateTime = DateTime.Now;
apply.Currency = item.Currency;
apply.Type = "增值税专用发票";
apply.Amount = item.Amount;
apply.PayerName = item.PayerName;
apply.PayeeName = item.PayeeName;
apply.OperateType = InvoiceApplyType.Debit;
apply.Details = new List<InvoiceApplyDetail>();
var detail = new InvoiceApplyDetail();
detail.GoodsFullName = "检测费";
detail.GoodsModel = "3040601000000000000";
detail.TaxRate = (decimal)0.03;
detail.Quantity = 1;
detail.Amount = apply.Amount;
detail.QuantityUnit = "次";
detail.AmountUntaxed = detail.Amount / (1 + detail.TaxRate);
detail.AmountTax = detail.Amount - detail.AmountUntaxed;
detail.QuantityUnitPrice = detail.Amount / detail.Quantity;
detail.QuantityUnitPriceUntaxed = detail.AmountUntaxed / detail.Quantity;
apply.Details.Add(detail);
rtns.Results.Add(apply);
}
}
return rtns;
}
}
[Api(Description = "账单申请开票")]
[RequiresAnyRole(SystemRoles.Everyone)]
public class InvoiceApplyFromBill : RestfulExecution<InvoiceApply>
{
public InvoiceApply Item { get; set; }
public List<Guid> BillKvids { get; set; }
//发票开具最大额度限制 默认10万
public decimal Limit { get; set; } = 100000;
public override object OnExecution(IRequest req, IResponse res)
{
Item.ThrowIfNull("请传入要申请的内容!");
if (Item.PayerName.IsNullOrEmpty()) throw new Exception("请输入发票抬头!");
if (Item.Currency == CurrencyUnit.Unsupported) throw new Exception("不支持的货币单位!");
if (Item.Amount <= 0) throw new Exception("申请金额不能小于0!");
if (Item.Details.IsNullOrEmpty()) throw new Exception("请传入申请明细!");
if (Item.Amount != Item.Details.Sum(o => o.Amount)) throw new Exception("总金额和明细总额不一致!");
if (Item.Details.Exists(o => o.GoodsFullName.IsNullOrEmpty())) throw new Exception("明细名称存在空值!");
if (Item.Details.Exists(o => o.TaxRate < 0) || Item.Details.Exists(o => o.TaxRate > 1)) throw new Exception("存在明细税率设置范围不在0-1之间!");
if (Item.Details.Exists(o => o.Quantity <= 0)) throw new Exception("请填写数量!");
if (Item.Details.Count > 1)
{
var group = Item.Details.GroupBy(o => o.GoodsId);
foreach (var kv in group)
{
var sum = kv.Sum(o => o.Amount);
if (sum > Limit) throw new Exception($"组别:{kv.Key},总额:{sum}元,金额总和不能超过限额{Limit}元!");
}
}
var session = Request.GetSession();
var kvid = Guid.NewGuid();
Item.Kvid = kvid;
Item.RootKvid = kvid;
Item.ParentKvid = Guid.Empty;
//Item.Status = (int)InvoiceApplyStatus.ProcessAdoption;
if (Item.OperateTime == DateTime.MinValue) Item.OperateTime = DateTime.Now;
//申请人为当前登录人
if (Item.OperatorName.IsNullOrEmpty())
{
Item.OperatorName = KiviiContext.CurrentMember.FullName;
Item.OperatorKvid = KiviiContext.CurrentMember.Kvid;
}
//申请所属部门
if (Item.OwnerName.IsNullOrEmpty())
{
Item.OwnerKvid = KiviiContext.CurrentMember.DepartmentKvid;
Item.OwnerName = KiviiContext.CurrentMember.DepartmentName;
}
#region 计算(不根据前端传来)
foreach (var detail in Item.Details)
{
detail.ApplyKvid = kvid;
detail.AmountUntaxed = Math.Round(detail.Amount / (1 + detail.TaxRate), 2);
detail.AmountTax = detail.Amount - detail.AmountUntaxed;
detail.QuantityUnitPriceUntaxed = Math.Round(detail.AmountUntaxed / detail.Quantity, 2);
detail.QuantityUnitPrice = Math.Round(detail.Amount / detail.Quantity, 2);
}
#endregion
var rtns = new RestfulCreateResponse<InvoiceApply>();
rtns.Results = new List<InvoiceApply>();
var conn = KiviiContext.GetOpenedDbConnection<InvoiceApply>();
var bills = new List<Bill>();
if (!BillKvids.IsNullOrEmpty())
{
var queryBills = conn.From<Bill>();
queryBills.Where(o => Sql.In(o.Kvid, BillKvids));
queryBills.Select(o => new
{
o.Kvid,
o.BizId,
o.BizKvid,
o.BizType
});
bills = conn.Select(queryBills);
}
if (bills.Exists(o => o.BizKvid != Guid.Empty)) throw new Exception("存在账单已申请,请勿重复操作!");
var trans = conn.OpenTransaction();
try
{
//#region 对接 提交OA表单
//var client = new JsonServiceClient(Configs.BaseUrl);
//var response = client.RequestApplyDemo(Item, session);
//if (response.code != "0") throw new Exception($"OA接口调用失败,原因:{response.message}");
//#endregion
//Item.BizType = typeof(ResponseData).FullName;
//Item.BizId = response.data.subject;
foreach (var detail in Item.Details)
{
conn.Insert(detail);
}
conn.Insert(Item);
Item.RemoveAllOnlyProperties();
rtns.Results.Add(Item);
if (!bills.IsNullOrEmpty())
{
var updateLamda = conn.From<Bill>();
updateLamda = updateLamda.Update(o => new { o.BizId, o.BizKvid, o.BizType });
updateLamda = updateLamda.Where(o => Sql.In(o.Kvid, bills.ConvertAll(p => p.Kvid)));
conn.UpdateOnly(new Bill { BizType = typeof(InvoiceApply).FullName, BizKvid = Item.Kvid, BizId = Item.SerialNumber }, updateLamda);
}
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