1、调用下推单据的方法
//传递参数,自行定义参数类retMatchGeneration
retMGen.FSTOCKID = this.Model.GetValue("FSTOCKID", rowIndex) as DynamicObject; //收货仓库
retMGen.FSTOCKLOCID = this.Model.GetValue("FStockLocId", rowIndex) as DynamicObject; //仓位
retMGen.F_T_SHIPNAME = this.Model.GetValue("F_T_ShipName", rowIndex).ToString(); //快递公司
retMGen.F_T_SHIPNO = this.Model.GetValue("F_T_ShipNo", rowIndex).ToString(); //快递单号
retMGen.F_T_BARCODE = this.Model.GetValue("F_T_BarCode", rowIndex).ToString(); //条码
//源单数据SQL,按项目实际需求编写
strSql = "select * from T_SAL_OUTSTOCK t1 inner join T_SAL_OUTSTOCKENTRY t2 on t1.FID=t2.FID where t1.FBILLNO='" + strSOSBillno + "'";
//销售出库单 下推 销售退货单
operResult = this.PushDown_OUTSTOCK_RETURNSTOCK("SAL_OUTSTOCK", "SAL_RETURNSTOCK", strSql, "73383412199a402bb58439509e089077", retMGen);
2、传递需要修改的内容,通过修改元数据,可以在保存目标单据元数据前,按实际需求影响下推时的数据。
/// 下推 单据转换 销售出库单 下推 销售退货单
/// </summary>
/// <param name="source">源单 业务对象标识</param>
/// <param name="target">目标单 业务对象标识</param>
/// <param name="getSourceSql">源单 查询语句</param>
/// <param name="sargetBillTypeId">目标单 转换目标类型</param>
/// <param name="retMatchGen">传递参数类</param>
/// <returns></returns>
public IOperationResult PushDown_OUTSTOCK_RETURNSTOCK(string source, string target, string getSourceSql, string sargetBillTypeId, retMatchGeneration retMatchGen)
{
IOperationResult result = new OperationResult();
IOperationResult saveResult = new OperationResult();
List<long> stockOrgIds = new List<long>();
List<string> materialNumbers = new List<string>();
object systemProfile = CommonServiceHelper.GetSystemProfile(this.Context, 0L, "STK_StockParameter", "RecInvCheckMidData", false);
bool recordMidData = false;
if (systemProfile != null && !string.IsNullOrWhiteSpace(systemProfile.ToString()))
{
recordMidData = Convert.ToBoolean(systemProfile);
}
//systemProfile = CommonServiceHelper.GetSystemProfile(ctx, 0L, "STK_StockParameter", "CleanReleaseLink", false);
//bool cleanReleaseLink = false;
//if (systemProfile != null && !string.IsNullOrWhiteSpace(systemProfile.ToString()))
//{
// cleanReleaseLink = Convert.ToBoolean(systemProfile);
//}
//获取单据转换规则
ConvertRuleElement ruleElement = ServiceHelper.GetService<IConvertService>().GetConvertRules(this.Context, source, target).FirstOrDefault();
//如下代码 直接通过查询数据库获取单据转换源单数据
ListSelectedRowCollection rows = new ListSelectedRowCollection();
int i = 0;
using (IDataReader reader = DBUtils.ExecuteReader(this.Context, getSourceSql))
{
while (reader.Read())
{
ListSelectedRow row = new ListSelectedRow(reader["FID"].ToString(), reader["FEntryID"].ToString(), i++, source);
row.EntryEntityKey = "FEntity";
rows.Add(row);
}
}
if (rows.Count() == 0) { return new OperationResult(); }
PushArgs pushArgs = new PushArgs(ruleElement, rows.ToArray());
pushArgs.TargetBillTypeId = sargetBillTypeId;
////转换生成目标单
//ConvertOperationResult convertResult = ServiceHelper.GetService<IConvertService>().Push(this.Context, pushArgs);
//转换生成目标单
ConvertOperationResult convertResult = ConvertServiceHelper.Push(this.Context, pushArgs, OperateOption.Create());
//合并转换操作结果
result.MergeResult(convertResult);
//目标单据数据集合
DynamicObject[] destObjs = convertResult.TargetDataEntities.Select(r => r.DataEntity).ToArray();
stockOrgIds.Add(Convert.ToInt64(destObjs[0]["StockOrgId_id"])); //库存组织明细 校对即时库存参数
//根据实际情况,处理目标单据数据
DynamicObjectCollection col_FEntityDetail = destObjs[0]["SAL_RETURNSTOCKENTRY"] as DynamicObjectCollection;
//int sum = 0;
foreach (var item in col_FEntityDetail)
{
DynamicObject dyoMater = (DynamicObject)item["MaterialId"];
materialNumbers.Add(Convert.ToString(dyoMater["Number"])); //物料明细 校对即时库存参数
//应退数量
item["Mustqty"] = retMatchGen.FQTY;
//实退数量
item["RealQty"] = retMatchGen.FQTY;
//库存基本数量
item["BaseunitQty"] = retMatchGen.FQTY;
//库存更新标示
item["STOCKFLAG"] = 1;
//金额 单价*数量,数量默认都是1,直接取单价。
item["Amount"] = Math.Round(Convert.ToDecimal( item["Price"])* retMatchGen.FQTY,2);
//金额(本位币) 单价*数量,数量默认都是1,直接取单价。
item["Amount_LC"] = Math.Round(Convert.ToDecimal(item["Price"]) * retMatchGen.FQTY,2);
//decimal iTaxRate = Convert.ToDecimal(item["TaxRate"].ToString()); //税率
//税额 单价*数量*税率/100,数量默认都是1,直接取单价。
item["TaxAmount"] = Math.Round(retMatchGen.FQTY * Convert.ToDecimal(item["Price"].ToString()) * Convert.ToDecimal(item["TaxRate"].ToString()) / 100, 2);
//税额(本位币) 单价*数量*税率/100,数量默认都是1,直接取单价。
item["TaxAmount_LC"] = Math.Round(retMatchGen.FQTY * Convert.ToDecimal(item["Price"].ToString()) * Convert.ToDecimal(item["TaxRate"].ToString()) / 100, 2);
//价税合计 含税单价*数量,数量默认都是1,直接取含税单价。
item["AllAmount"] = Math.Round(Convert.ToDecimal(item["TaxPrice"]) * retMatchGen.FQTY, 2) ;
//价税合计(本位币) 含税单价*数量,数量默认都是1,直接取含税单价。
item["AllAmount_LC"] = Math.Round(Convert.ToDecimal(item["TaxPrice"]) * retMatchGen.FQTY, 2) ;
//计价数量
item["PriceUnitQty"] = retMatchGen.FQTY;
//计价基本数量
item["PriceBaseQty"] = retMatchGen.FQTY;
//销售数量
item["SalUnitQty"] = retMatchGen.FQTY;
//销售基本数量
item["SalBaseQty"] = retMatchGen.FQTY;
// 未关联应收数量(计价单位)
item["ARNOTJOINQTY"] = retMatchGen.FQTY;
// 退货快递公司
item["F_HSM_THKDGS"] = retMatchGen.F_T_SHIPNAME;
// 退货快递单号
item["F_HSM_FHKDDH"] = retMatchGen.F_T_SHIPNO;
// 条码
item["F_T_BarCode"] = retMatchGen.F_T_BARCODE ;
//退货类型
item["ReturnType_id"] = retMatchGen.FRETURNTYPE["id"];
item["ReturnType"] = retMatchGen.FRETURNTYPE;
//仓库
item["StockId_id"] = retMatchGen.FSTOCKID["id"];
item["StockId"] = retMatchGen.FSTOCKID;
//仓位
item["StocklocId_id"] = retMatchGen.FSTOCKLOCID["id"];
item["StocklocId"] = retMatchGen.FSTOCKLOCID;
// 备注2 京东退货单号
//item["F_HSM_NOTE"] = retHSMNote;
//sum = sum + 1;//测试用,暂时设置 “含税单价” 每条都是1
}
//DynamicObjectCollection col_FEntityPlan = destObjs[0]["AP_PAYABLEPLAN"] as DynamicObjectCollection;
//int iPAYAMOUNTFOR = sum / col_FEntityPlan.Count;//“价税合计” 总金额平均分配到每一条付款计划 “应付金额” 字段
//foreach (var item in col_FEntityPlan)
//{
// item["PAYAMOUNTFOR"] = iPAYAMOUNTFOR;
//}
////设置单据头 “价税合计” 即【明细】分录的“含税单价”合计
//destObjs[0]["FALLAMOUNTFOR"] = sum;
//目标单元数据
FormMetadata destFormMetadata = ServiceHelper.GetService<IMetaDataService>().Load(this.Context, target) as FormMetadata;
//保存目标单据
IOperationResult result1 = ServiceHelper.GetService<ISaveService>().Save(this.Context, destFormMetadata.BusinessInfo, destObjs, OperateOption.Create());
if (result1.IsSuccess)
{
saveResult = result1;
string fid = Convert.ToString(result1.SuccessDataEnity.First()["id"]);
//提交目标单据
IOperationResult result2 = BusinessDataServiceHelper.Submit(this.Context, destFormMetadata.BusinessInfo, new string[] { fid }, "Submit", OperateOption.Create());
if (result2.IsSuccess)
{
saveResult = result2;
//审核目标单据
IOperationResult result3 = BusinessDataServiceHelper.Audit(this.Context, destFormMetadata.BusinessInfo, new string[] { fid }, OperateOption.Create());//, "Audit"
if (result2.IsSuccess)
{
saveResult = result3;
//StockServiceHelper.StockCheck(ctx, stockOrgIds, materialNumbers, recordMidData, cleanReleaseLink); //校对即时库存
StockServiceHelper.StockCheck(this.Context, stockOrgIds, materialNumbers, recordMidData); //校对即时库存
}
}
}
//保存提交并审核目标单据 可独立使用
//IOperationResult saveResult1 = ServiceHelper.GetService<ISaveService>().SaveAndAudit(this.Context, destFormMetadata.BusinessInfo, destObjs, OperateOption.Create());
//合并保存操作结果
result.MergeResult(saveResult);
//////保存目标单据
//////IOperationResult saveResult = ServiceHelper.GetService<ISaveService>().Save(this.Context, destFormMetadata.BusinessInfo, destObjs, OperateOption.Create());
//////保存并提交目标单据
//////IOperationResult result2 = BusinessDataServiceHelper.Submit(ctx, FormMetadata.BusinessInfo, new string[] { fid }, "Submit", operateOption);
////IOperationResult saveResult1 = ServiceHelper.GetService<ISaveService>().SaveAndAudit(this.Context, destFormMetadata.BusinessInfo, destObjs, OperateOption.Create());
//////合并保存操作结果
////result.MergeResult(saveResult1);
//根据操作结果构造返回结果
//if ((result.ValidationErrors != null && result.ValidationErrors.Count > 0) || (result.OperateResult != null && result.OperateResult.Count > 0))
//{
// result.IsSuccess = false;
//}
return result;
}
推荐阅读