using ExtensionMethods;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace UseGetFmaterialData
{
///
/// 【单据插件】保存前事件
/// 触发顺序:BeforeDoOperation->BeforeSave
///
[Description("【单据插件】保存前事件"), HotUpdate]
public class BeforeSaveEventBillPlugIn : AbstractBillPlugIn
{
public override void BeforeSave(BeforeSaveEventArgs e)
{
base.BeforeSave(e);
this.View.ShowMessage("插件触发了保存前事件:BeforeSave");
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");
IOperationResult opResult = new OperationResult();
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();
string errMsg = string.Empty;
UpdReferPriceAndExplain(dateValue, rowValue, rowIndex, ref errMsg);
if (!errMsg.IsNullOrEmpty())
{
var number = (entry["MaterialId"] as DynamicObject)["Number"];
opResult.OperateResult.Add(new OperateResult
{
Name = number.ToString(),
Message = errMsg,
SuccessStatus = false
});
}
}
TotalReferAmount(entrys, details);
if (opResult.OperateResult.Any())
{
e.Cancel = true;
this.View.ShowOperateResult(opResult.OperateResult);
}
}
}
}
}
///
/// 参考金额汇总
///
///
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);
//参考金额汇总
this.View.Model.SetValue("FTotalReferAmount", total);
}
catch
{
try
{
this.View.Model.SetValue("FTotalReferAmount", total);
//参考金额汇总
this.View.Model.SetValue("F_GAT_Decimal1", 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, ref string errMsg)
{
var isHasEmpty = false;
var returnFlag = false;
if (date.IsNullOrEmptyOrWhiteSpace())
isHasEmpty = 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);
if (price == 0 || amount == 0)
{
errMsg = "物料没有找到参考单价与参考金额,或数量为0";
return;
}
var cSql = $@"
SELECT top 1
A.FBILLNO,C.FTAXPRICE
FROM
T_STK_INSTOCK A
INNER JOIN T_STK_INSTOCKENTRY B ON A.FID =B.FID
INNER JOIN T_STK_INSTOCKENTRY_F C ON C.FENTRYID = B.FENTRYID
WHERE
A.FDOCUMENTSTATUS = 'C'
AND A.FCANCELSTATUS = 'A'
AND A.FSTOCKORGID = 101542
AND B.FMATERIALID = {materialId}
AND FDATE<= '{date}'
AND FAPPROVEDATE <= '{date}'
ORDER BY
FDATE DESC,FAPPROVEDATE DESC ";
var inDataSet = DBUtils.ExecuteDynamicObject(this.Context, $"/*dialect*/{cSql}");
if (inDataSet.Any())
{
var taxPrice = inDataSet[0]["FTAXPRICE"].ToDecimalR();
if (taxPrice == 0)
{
errMsg = "物料没有找到采购入库单单价";
return;
}
var xAbs = Math.Abs((price - taxPrice) / taxPrice);
if (xAbs > 0.05M)
{
errMsg = "物料采购入库单价与参考单价相差值不能大于5%";
return;
}
}
else
{
errMsg = "物料没有找到采购入库单";
return;
}
}
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);
}
}
}
}
}