#region 函数说明:同步归还单
///
/// 同步归还单
///
///
///
///
///
public AjaxResultModel 同步归还单(PublicFunctions.CallBackEvent.CallBackEventEnum CallBackEvent, PublicFunctions.WorkFlow.WorkFlowEngineModel.WorkFlowInstance WorkFlowInstanceObject, PublicFunctions.WorkFlow.WorkFlowEngineModel.WorkFlowStepSetting WorkFlowStepSettingObject)
{
AjaxResultModel AjaxResultObject = new AjaxResultModel();
using (PublicFunctions.DBHelper.RawDBHelper.SqlDataFactory context = new PublicFunctions.DBHelper.RawDBHelper.SqlDataFactory(PublicSetting.RawDBHelper_CRM_ConnectionStringKey))
{
Dictionary 归还单主表 = context.GetCellRowDictionary("select * from 归还单主表 where WorkFlowGUID='" + WorkFlowInstanceObject.WorkFlowGUID + "'");
if (归还单主表.Count > 0)
{
string ErrorMsg = "";
string sql = "select *,序列号管理=(select 序列号管理 from 订单产品表 where 产品编码=归还单子表.产品编码) from 归还单子表 where WorkFlowGUID='" + WorkFlowInstanceObject.WorkFlowGUID + "'";
DataTable 归还单子表 = new DataTable();
if (context.GetDataAll(ref 归还单子表, sql, ref ErrorMsg))
{
foreach (DataRow datarow in 归还单子表.Rows)
{
decimal 已分配的数量 = 0;
decimal 已分配的成本 = 0;
decimal 已分配的单价 = 0;
decimal 未归还数量 = ConvertData.ConvertDecimal(datarow["数量"]);
if (未归还数量 > 0)
{
#region 代码块说明:正式分配
string 关联ID借货单子表 = ConvertData.ConvertString(datarow["关联ID借货单子表"]);
DataTable 出库流水表 = new DataTable();
if (context.GetDataAll(ref 出库流水表, "select * from 出库流水表 where 同步记录ID=" + 关联ID借货单子表 + " and 出库数量>0", ref ErrorMsg))
{
foreach (DataRow 出库流水 in 出库流水表.Rows)
{
decimal 出库数量 = ConvertData.ConvertDecimal(出库流水["出库数量"]);
decimal 成本单价 = ConvertData.ConvertDecimal(出库流水["成本单价"]);
decimal 归还数量 = Math.Min(未归还数量, 出库数量);
string UpdateSQL = "update 出库流水表 set 出库数量=出库数量-" + 归还数量 + ",成本金额=成本单价*(出库数量-" + 归还数量 + "),销售成本金额=销售成本单价*(出库数量-" + 归还数量 + ") where ID=" + 出库流水["ID"];
if (context.ExecuteTransaction(UpdateSQL, out ErrorMsg))
{
#region 代码块说明:正式入库
string 产品编码 = ConvertData.ConvertString(datarow["产品编码"]);
bool 序列号管理 = ConvertData.ConvertToBool(datarow["序列号管理"]);
string 序列号 = 序列号管理 ? ConvertData.ConvertString(datarow["序列号"]) : 产品编码;
Dictionary NewDictionary = new Dictionary();
NewDictionary["同步记录ID"] = ConvertData.ConvertString(datarow["ID"]);
NewDictionary["WorkFlowGUID"] = WorkFlowInstanceObject.WorkFlowGUID;
NewDictionary["WorkFlowInstanceCode"] = WorkFlowInstanceObject.WorkFlowInstanceCode;
NewDictionary["AgencyID"] = PublicFunctions.PublicFunctions.GetDataFromDictionary("AgencyID", 归还单主表);
NewDictionary["DepartmentID"] = PublicFunctions.PublicFunctions.GetDataFromDictionary("DepartmentID", 归还单主表);
NewDictionary["DataTableName"] = "归还单";
NewDictionary["供应商"] = "";
NewDictionary["入库日期"] = PublicFunctions.PublicFunctions.GetDataFromDictionary("归还日期", 归还单主表);
NewDictionary["仓库"] = ConvertData.ConvertString(datarow["仓库"]);
NewDictionary["序列号"] = 序列号;
NewDictionary["产品编码"] = 产品编码;
NewDictionary["数量"] = 序列号管理 ? "1" : ConvertData.ConvertString(datarow["数量"]);
NewDictionary["成本单价"] = ConvertData.ConvertString(出库流水["成本单价"]);
NewDictionary["成本金额"] = ConvertData.ConvertString(出库流水["成本金额"]);
NewDictionary["销售成本单价"] = ConvertData.ConvertString(出库流水["销售成本单价"]);
NewDictionary["已出库数量"] = "0";
NewDictionary["未出库数量"] = NewDictionary["数量"];
NewDictionary["占用数量"] = "0";
NewDictionary["可用数量"] = NewDictionary["数量"];
NewDictionary["UserGuid"] = ServiceData.UserInfo.UserGuid;
NewDictionary["AddDate"] = DateTime.Now.ToString();
NewDictionary["UpdateUserGuid"] = ServiceData.UserInfo.UserGuid;
NewDictionary["UpdateDate"] = DateTime.Now.ToString();
context.InsertDataReturnID("入库流水表", NewDictionary);
#endregion
已分配的数量 += 归还数量;
已分配的成本 += 归还数量 * 成本单价;
未归还数量 -= 归还数量;
if (未归还数量 <= 0)
{
break;
}
}
}
}
已分配的单价 = 已分配的数量 > 0 ? (已分配的成本 / 已分配的数量) : 0;
#endregion
#region 代码块说明:更新同步归还单的数量、单价及金额
context.ExecuteTransaction("update 归还单子表 set 数量='" + 已分配的数量 + "',单价='" + 已分配的单价 + "',金额='" + 已分配的成本 + "' where ID=" + datarow["ID"], out ErrorMsg);
#endregion
}
}
}
}
}
AjaxResultObject.IsSuccess = true;
return AjaxResultObject;
}
#endregion