[password]123456[/password]
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.K3.Core.MFG.EntityHelper;
namespace YEA.K3.Finance.Business.Plugin.Converts
{
[Description("单据转换插件:销售订单发货通知单下推Packing list有栈板拆分非整数行")]
public class CpSalesOrderConvertPackingListPallet : AbstractConvertPlugIn
{
//Cartons(PCS) =QTY(PCS)/(QTY/ctn);Pallets(PCS)=QTY(PCS)/QTY/PLT
//1)如果箱数和栈板数都要拆,即Cartons(PCS)>1且不为整数且Pallets(PCS)>1且不为整数;
//则需拆分行QTY(PCS)={QTY/ctn和QTY/PLT在(0,原QTY(PCS))范围内最大公倍数(整数)};拆分行(自动新增)QTY(PCS)=原QTY(PCS)-原行的QTY(PCS)。
//2)如果箱数要拆,栈板不用拆,即Cartons(PCS)>1且不为整数,Pallets(PCS)<1或者为整数,
//则需拆分行QTY(PCS)=QTY/ctn在(0,原QTY(PCS))的最大整倍数;拆分行(自动新增)QTY(PCS)=原QTY(PCS)-原行的QTY(PCS)
//3)如果箱数不用拆,栈板要拆,即Cartons(PCS)为整数或者小于1,且Pallets(PCS)>1不为整数;
//则需拆分行QTY(PCS)=QTY/PLT在(0,原QTY(PCS))的最大整倍数;拆分行(自动新增)QTY(PCS)=原QTY(PCS)-原行的QTY(PCS)
//注意:拆分出来的行也要进行校验;QTY(PCS)的值变化时,要自动触发N. W.(KG)、G.W.(KG)、Volume(CBM),Cartons pcs,Pallets(PCS),栈板占比的值更新,其余字段同原分录行一致。
public override void OnAfterCreateLink(CreateLinkEventArgs e)
{
Entity entity = e.TargetBusinessInfo.GetEntity("FEntity");
ExtendedDataEntity[] bills = e.TargetExtendedDataEntities.FindByEntityKey("FBillHead"); //Packing list有栈板
List
foreach (var bill in bills)
{
// 取单据体集合
DynamicObjectCollection rowObjs = entity.DynamicProperty.GetValue(bill.DataEntity)
as DynamicObjectCollection;
int rowCount = rowObjs.Count;
int newRowCount = 1;
for (int i = rowObjs.Count - 1; i >= 0; i--)
{
#region 处理拆分行逻辑
DynamicObject rowObj = rowObjs[i];
decimal qty = Convert.ToDecimal(rowObj["F_YEA_Qty"]); //QTY(PCS)
DynamicObject fumbObj = rowObj["F_YEA_Fnum"] as DynamicObject; //
decimal qtyCtn = Convert.ToDecimal(fumbObj == null ? 0 : (fumbObj["F_YEA_QTY_CTN"]));
//QTY/ctn(PCS)
decimal pkg = Math.Round(qty/(qtyCtn == 0 ? 1 : qtyCtn), 3); //PKG
decimal pkgZs = Convert.ToDecimal(Math.Floor(pkg)); //pkg整数部分
if (pkgZs > 0)
{
decimal xs = pkg - pkgZs; //pkg小数部分
if (xs > 0)
{
#region 需要关联复制行
rowObj["F_YEA_Qty"] = qtyCtn*pkgZs;
DynamicObject newRowObj = (DynamicObject) rowObj.Clone(false, true);
// 通过复制的方式,产生新行:确保字段值、关联关系与原行一致
newRowObj["F_YEA_Qty"] = qty - (qtyCtn*pkgZs);
rowObjs.Insert(i + 1, newRowObj); // 把新行,插入到单据中,排在当前行之后
// 为新行创建一个ExtendedDataEntity对象,表单服务策略需要此对象
ExtendedDataEntity newRow = new ExtendedDataEntity(
newRowObj, bill.DataEntityIndex, rowCount + newRowCount);
newRows.Add(newRow);
newRowCount++;
#endregion
}
}
decimal qty1 = Convert.ToDecimal(rowObj["F_YEA_Qty"]); //QTY(PCS)
decimal qtyPln = Convert.ToDecimal(fumbObj == null ? 0 : (fumbObj["F_YEA_QTY_PLT"]));
//QTY/Pln(PCS)
decimal pallets = Math.Round(qty1/(qtyPln == 0 ? 1 : qtyPln), 3); //Pallets
decimal palletsZs = Convert.ToDecimal(Math.Floor(pallets)); //pallets 整数部分
if (palletsZs > 0)
{
decimal xs = pallets - palletsZs; //pkg小数部分
if (xs > 0)
{
#region 需要关联复制行
rowObj["F_YEA_Qty"] = qtyPln*palletsZs;
DynamicObject newRowObj = (DynamicObject) rowObj.Clone(false, true);
// 通过复制的方式,产生新行:确保字段值、关联关系与原行一致
newRowObj["F_YEA_Qty"] = qty1 - (qtyPln*palletsZs);
rowObjs.Insert(i + 1, newRowObj); // 把新行,插入到单据中,排在当前行之后
// 为新行创建一个ExtendedDataEntity对象,表单服务策略需要此对象
ExtendedDataEntity newRow = new ExtendedDataEntity(
newRowObj, bill.DataEntityIndex, rowCount + newRowCount);
newRows.Add(newRow);
newRowCount++;
#endregion
}
}
#endregion
}
#region 处理起止&Ctn no.
List
.OrderBy(((Func
.ThenByDescending(
((Func
rowObjs.Clear();
int k = 0;//记录处理的序号
decimal beginNum = 1M;//起箱
for (int i = 0; i < collection1.Count; i++)
{
int seq = Convert.ToInt32(collection1[i]["seq"]); //序号
DynamicObject fumbObj = collection1[i]["F_YEA_Fnum"] as DynamicObject; //品名对照表
decimal qtyCtn = Convert.ToDecimal(fumbObj == null ? 0 : (fumbObj["F_YEA_QTY_CTN"]));
//QTY/ctn(PCS)
decimal qty = Convert.ToDecimal(collection1[i]["F_YEA_Qty"]); //Qty(PCS)
decimal pkg = Math.Round(qty/(qtyCtn == 0 ? 1 : qtyCtn), 3); //PKG
decimal pkgZs = Convert.ToDecimal(Math.Floor(pkg)); //pkg整数部分
if (k != seq)
{
beginNum = 1M; //起箱
k = seq;
}
collection1[i]["F_YEA_begin"] = beginNum; //起
collection1[i]["F_YEA_end"] = 0M; //止
if (pkgZs > 0)
{
beginNum += pkgZs;
collection1[i]["F_YEA_end"] = beginNum - 1; //止
}
collection1[i]["F_YEA_ctnno"] = GetCtnNo(collection1[i]); //Ctn no.
rowObjs.Add(collection1[i]);
}
#endregion
// 把新拆分出来的单据体行,加入到下推结果中
// 特别说明:如果去掉此语句,新拆分的行,不会执行表单服务策略
e.TargetExtendedDataEntities.AddExtendedDataEntities("FEntity", newRows.ToArray());
}
}
public override void AfterConvert(AfterConvertEventArgs e)
{
ExtendedDataEntity[] array = e.Result.FindByEntityKey("FBillHead"); //Packing list无栈板
if (array != null && array.Length > 0)
{
ExtendedDataEntity[] array2 = array;
for (int j = 0; j < array2.Length; j++)
{
ExtendedDataEntity extendedDataEntity = array2[j];
DynamicObjectCollection entityCollection = extendedDataEntity["FEntity"] as DynamicObjectCollection;
#region 处理排序
// Pallets(PCS)=【Q'TY (PCS)】/【QTY/PLT(PCS)】
List
.OrderByDescending(
(Func
.ToList();
entityCollection.Clear();
for (int i = 0; i < collection.Count; i++)
{
collection[i]["seq"] = i + 1;
entityCollection.Add(collection[i]);
}
#endregion
}
}
base.AfterConvert(e);
}
///
/// 获取Ctn no.
///
///
///
private string GetCtnNo(DynamicObject rowObj)
{
string ctn = Convert.ToString(rowObj["F_YEA_ctn"]); //Ctn
string begin = Convert.ToString(rowObj["F_YEA_begin"]);
string end = Convert.ToString(rowObj["F_YEA_end"]);
return ctn + begin + "-" + ctn + end;
}
}
}[/code]
推荐阅读