using ExtensionMethods; using Kingdee.BOS.App.Data; using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Core.Metadata.EntityElement; using Kingdee.BOS.Core.Metadata.Util; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.Util; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using static System.Net.WebRequestMethods; namespace UseGetFmaterialData { [Description("根据日期,物料Id更新参考单价、取价方向、参考金额-控件触发、新增加载触发"), HotUpdate] public class DataChangedEventFormPlugIn : AbstractDynamicFormPlugIn { //对应控件数值改变时触发 public override void DataChanged(DataChangedEventArgs e) { base.DataChanged(e); //表头日期 if (e.Field.Key.EqualsIgnoreCase("FDATE")) { if (OrgIdCheck()) { Entity details = null; //其他出库单 明细表 if (this.View.UserParameterKey.Equals("STK_MisDelivery")) details = this.View.BusinessInfo.GetEntity("FEntity"); //直接调拨单 明细表 if (this.View.UserParameterKey.Equals("STK_TransferDirect")) details = this.View.BusinessInfo.GetEntity("FBillEntry"); if (details != null) { var entrys = this.View.Model.GetEntityDataObject(details); if (entrys != null && entrys.Any()) { var dateValue = e.NewValue == null ? string.Empty : e.NewValue.ToString(); foreach (var entry in entrys) { var rowIndex = this.View.Model.GetRowIndex(details, entry); var tempValue = entry["MaterialId_Id"]; var rowValue = tempValue == null ? string.Empty : tempValue.ToString(); UpdReferPriceAndExplain(dateValue, rowValue, rowIndex); } TotalReferAmount(entrys, details); } } } } //子表物料id if (e.Field.Key.EqualsIgnoreCase("FMaterialId")) { if (OrgIdCheck()) { var rowValue = e.NewValue == null ? string.Empty : e.NewValue.ToString(); var tempValue = this.View.Model.GetValue("FDATE"); var dateValue = tempValue == null ? string.Empty : tempValue.ToString(); UpdReferPriceAndExplain(dateValue, rowValue, e.Row); TotalReferAmount(null, null); } } //数量 if (e.Field.Key.EqualsIgnoreCase("FQty")) { if (OrgIdCheck()) { //参考单价 var referPrice = this.View.Model.GetValue("FReferPrice", e.Row).ToDecimalR(); var amount = (e.NewValue.ToDecimal() * referPrice).ToDecimalR(); //参考金额 this.View.Model.SetValue("FReferAmount", amount == 0 ? "" : amount.ToString(), e.Row); //小数类型参考金额控件 //this.View.Model.SetValue("FReferAmountM", amount, e.Row); TotalReferAmount(null, null); } } } //新增加载时触发 public override void AfterCreateNewData(EventArgs e) { base.AfterCreateNewData(e); if (OrgIdCheck()) { Entity details = null; //其他出库单 明细表 if (this.View.UserParameterKey.Equals("STK_MisDelivery")) details = this.View.BusinessInfo.GetEntity("FEntity"); //直接调拨单 明细表 if (this.View.UserParameterKey.Equals("STK_TransferDirect")) details = this.View.BusinessInfo.GetEntity("FBillEntry"); if (details != null) { var entrys = this.View.Model.GetEntityDataObject(details); if (entrys != null && entrys.Any()) { var tempValue2 = this.View.Model.GetValue("FDATE"); var dateValue = tempValue2.IsNullOrEmptyOrWhiteSpace() ? string.Empty : tempValue2.ToString(); foreach (var entry in entrys) { var rowIndex = this.View.Model.GetRowIndex(details, entry); var tempValue = entry["MaterialId_Id"]; var rowValue = tempValue.IsNullOrEmptyOrWhiteSpace() ? string.Empty : tempValue.ToString(); UpdReferPriceAndExplain(dateValue, rowValue, rowIndex); } TotalReferAmount(entrys, details); } } } } /// /// 参考金额汇总 /// /// public void TotalReferAmount(DynamicObjectCollection entrys, Entity details) { if (entrys == null) { //其他出库单 明细表 if (this.View.UserParameterKey.Equals("STK_MisDelivery")) details = this.View.BusinessInfo.GetEntity("FEntity"); //直接调拨单 明细表 if (this.View.UserParameterKey.Equals("STK_TransferDirect")) details = this.View.BusinessInfo.GetEntity("FBillEntry"); if (details != null) entrys = this.View.Model.GetEntityDataObject(details); } var total = 0M; if (entrys != null && entrys.Any()) { foreach (var entry in entrys) { var rowIndex = this.View.Model.GetRowIndex(details, entry); var tAmount = this.View.Model.GetValue("FReferAmount", rowIndex).ToDecimal(); total += tAmount; } } try { this.View.Model.SetValue("F_GAT_Decimal1", total); } catch { try { this.View.Model.SetValue("FTotalReferAmount", total); } catch { } finally { } } finally { } } /// /// 检测组织 /// /// public bool OrgIdCheck() { //直接调拨单 调出库存组织 var org = this.View.Model.GetValue("FStockOutOrgId") as DynamicObject; //其他出库单 库存组织 if (org == null) org = this.View.Model.GetValue("FStockOrgId") as DynamicObject; return org != null && Convert.ToInt32(org["Id"]) == 101542; } /// /// 更新对应行数的参考单价与取价方向 /// /// 表头日期 /// 子表物料id(物料编码) /// private void UpdReferPriceAndExplain(string date, string materialId, int row) { var returnFlag = false; if (date.IsNullOrEmptyOrWhiteSpace()) returnFlag = true; if (!returnFlag && (materialId.IsNullOrEmptyOrWhiteSpace() || materialId.Equals("0"))) returnFlag = true; if (returnFlag) { //参考单价 this.View.Model.SetValue("FReferPrice", "", row); //参考金额 this.View.Model.SetValue("FReferAmount", "", row); ////小数类型参考金额控件 //this.View.Model.SetValue("FReferAmountM", 0, row); //参考方向 this.View.Model.SetValue("FExplain", "没有找到价格", row); } else { var sqlL = $"EXEC GetFmaterialData {materialId},'{date}'"; var dataSet = DBUtils.ExecuteDynamicObject(this.Context, $"/*dialect*/{sqlL}"); if (dataSet != null && dataSet.Any()) { var price = dataSet[0]["Fprice"].ToDecimalR(); //数量 var qty = this.View.Model.GetValue("FQty", row).ToDecimal(); var amount = (qty * price).ToDecimalR(); //参考单价 this.View.Model.SetValue("FReferPrice", (price == 0 ? "" : price.ToString()), row); //参考金额 this.View.Model.SetValue("FReferAmount", (amount == 0 ? "" : amount.ToString()), row); //小数类型参考金额控件 //this.View.Model.SetValue("FReferAmountM", amount, row); //参考方向 this.View.Model.SetValue("FExplain", dataSet[0]["FNOTE"], row); } else { //参考单价 this.View.Model.SetValue("FReferPrice", "", row); //参考金额 this.View.Model.SetValue("FReferAmount", "", row); //小数类型参考金额控件 //this.View.Model.SetValue("FReferAmountM", 0, row); //参考方向 this.View.Model.SetValue("FExplain", "没有找到价格", row); } } } } }