金蝶外部调用核销接口文档:外部调用核销接口说明 (kingdee.com)
标准的核销接口:是根据应收单收款计划分录和收款单明细行进行核销
但是第三方系统能够给的只有,应收单号、收款单号、核销金额、销售订单号。
那么需要我们对数据进行加工,代码如下:
/// <summary>
/// 实体关系子类
/// </summary>
public class MatchReversalData
{
/// <summary>
/// 应收单
/// </summary>
public MatchReversalDataDetail Receivable { get; set; }
/// <summary>
/// 收款单
/// </summary>
public MatchReversalDataDetail Receivebill { get; set; }
/// <summary>
/// 核销金额
/// </summary>
public decimal Amount { get; set; }
/// <summary>
/// 核销ID
/// </summary>
public int MatchMethodID { get; set; }
/// <summary>
/// 销售订单反写应收金额
/// </summary>
public List<PayPlan> PayPlanDetail { get; set; }
}
public class MatchReversalDataDetail
{
/// <summary>
/// 单号
/// </summary>
public string BillNo { get; set; }
/// <summary>
/// 销售订单号
/// </summary>
public string SaleOrderNo { get; set; }
}
/// <summary>
/// 通用根据单号和销售订单号的核销方案
/// </summary>
/// <param name="context"></param>
/// <param name="datas"></param>
/// <returns></returns>
public static ResponseStatus GeneralMatchReversal(Context context, List<MatchReversalData> datas)
{
ResponseStatus response = new ResponseStatus();
//循环去调用(一个单调用一次)
foreach (var item in datas)
{
foreach (var itemRow in BuildSpecialMatchData(context, item))
{
//参数组装
MatchParameters para = new MatchParameters();
para.MatchType = "3";
//外部匹配核销,38是手工特殊核销,也就是先走匹配,匹配之后剩余的金额会进行特殊核销
para.MatchMethodID = item.MatchMethodID > 0 ? item.MatchMethodID : 36;
para.UserId = context.UserId;
//核销方案内码 688881 20008
para.SelectedSchemeId = new List<object> { 20008 };
var o = Kingdee.K3.FIN.Contracts.ServiceFactory.GetService<IMatchService>(context);
IOperationResult operationResult = o.SpecialMatchForUser(context, para, itemRow);
if (operationResult.ValidationErrors.Count > 0)
{
string errorStr = string.Join("|", operationResult.ValidationErrors.Select(x => x.Message));
response.Errors.Add(new ErrorsItem()
{
Message = errorStr,
FieldName = JsonConvert.SerializeObject(itemRow)
});
}
else
{
response.SuccessMessages.AddRange(operationResult.OperateResult.Select(x => x.Message));
}
}
}
return response;
}
/// <summary>
/// 根据数据构建核销数据
/// </summary>
/// <param name="Datas"></param>
/// <returns></returns>
private static List<List<SpecialMatchDateForUser>> BuildSpecialMatchData(Context context, MatchReversalData Datas)
{
List<List<SpecialMatchDateForUser>> dataList = new List<List<SpecialMatchDateForUser>>();
//应收在前,收款在后
var receivable = CommonHelper.GetDynamicObject(context, "AR_receivable", string.Format("FBILLNO='{0}'", Datas.Receivable.BillNo));
var Receivebill = CommonHelper.GetDynamicObject(context, "AR_RECEIVEBILL", string.Format("FBILLNO='{0}'", Datas.Receivebill.BillNo));
//两个都能找到才进行下一步
if (receivable.isSuccess & Receivebill.isSuccess)
{
DynamicObjectCollection SKD = Receivebill.data["RECEIVEBILLENTRY"] as DynamicObjectCollection;
DynamicObjectCollection YSD = receivable.data["AP_PAYABLEPLAN"] as DynamicObjectCollection;
decimal YHXJE = 0;
while (YHXJE < Datas.Amount)
{
DynamicObject SKMX;
DynamicObject YSMX;
if (string.IsNullOrWhiteSpace(Datas.Receivebill.SaleOrderNo))
{
SKMX = SKD.Where(c => Convert.ToDecimal(c["FNOTVERIFICATEAMOUNT"]) > 0).FirstOrDefault();
}
else
{
SKMX = SKD.Where(c => CommonHelper.ObjectToString(c["FSALEORDERNO"]) == Datas.Receivebill.SaleOrderNo && Convert.ToDecimal(c["FNOTVERIFICATEAMOUNT"]) > 0).FirstOrDefault();
}
if (string.IsNullOrWhiteSpace(Datas.Receivable.SaleOrderNo))
{
YSMX = YSD.Where(c => Convert.ToDecimal(c["FNOTVERIFICATEAMOUNT"]) > 0).FirstOrDefault();
}
else
{
YSMX = YSD.Where(c => CommonHelper.ObjectToString(c["ORDERBILLNO"]) == Datas.Receivable.SaleOrderNo && Convert.ToDecimal(c["FNOTVERIFICATEAMOUNT"]) > 0).FirstOrDefault();
}
if (SKMX != null && YSMX != null)
{
//三者之间最小
decimal minValue = new decimal[] { Datas.Amount - YHXJE, Convert.ToDecimal(SKMX["FNOTVERIFICATEAMOUNT"]), Convert.ToDecimal(YSMX["FNOTVERIFICATEAMOUNT"]) }.Min();
SKMX["FNOTVERIFICATEAMOUNT"] = Convert.ToDecimal(SKMX["FNOTVERIFICATEAMOUNT"]) - minValue;
YSMX["FNOTVERIFICATEAMOUNT"] = Convert.ToDecimal(YSMX["FNOTVERIFICATEAMOUNT"]) - minValue;
YHXJE += minValue;
List<SpecialMatchDateForUser> newRow = new List<SpecialMatchDateForUser>();
//生成应收核销数据
newRow.Add(new SpecialMatchDateForUser()
{
FBillId = Convert.ToInt64(receivable.data[0]),
FEntryID = Convert.ToInt64(YSMX[0]),
FFormId = "AR_receivable",
FMatchType = "3",
FTheMatchAmount = minValue
});
//生成收款核销数据
newRow.Add(new SpecialMatchDateForUser()
{
FBillId = Convert.ToInt64(Receivebill.data[0]),
FEntryID = Convert.ToInt64(SKMX[0]),
FFormId = "AR_RECEIVEBILL",
FMatchType = "3",
FTheMatchAmount = minValue
});
dataList.Add(newRow);
}
else
{
break;
}
}
}
return dataList;
}
根据规则,即可实现应收单号和收款单号的核销。