Files
lserp_cs_6.0/插件库/Lskj.Control/FrmBaseSelectBatch.cs
2026-07-02 10:11:39 +00:00

597 lines
25 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lskj.Control.Model;
using Lskj.Model;
using Lskj.Business.Impl;
using Lskj.Data;
using Lskj.Util;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using Lskj.Business;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Lskj.Core;
namespace Lskj.Control
{
public partial class FrmBaseSelectBatch : BaseForm
{
string ParmaryKey;
/// <summary>
/// 库存数量字段、输入数量字段、结余数量字段、记录唯一值标识
/// </summary>
public string KcAmountField, AmountField, JYAmountField, RecordPrimaryField;
public DataTable QueryTable = new DataTable();
public DataTable GridColumns = new DataTable();
public string SearchSQL = "";
public MyControl SearchObj = new MyControl();
// 传入参数
public MyControl ControlObj = null;
public GridControlEx ParentGridControl;
public DynamicBaseSelectModel Model;
public DataRow SelectRowItem = null;
public DataRow[] FpRows = null; // 已分配数据行
public DataRow[] FpOtherRows = null; // 已分配其他数据行
// 输出参数
public DataRow[] SelectedRows = null;
/// <summary>
/// 系统模块实体对象
/// </summary>
/// <value>The system model.</value>
public ModuleModel SysModel { get; private set; }
/// <summary>
/// 是否点击下一个
/// </summary>
public bool isNextOne = false;
/// <summary>
/// 分配完成后回调
/// </summary>
public event EventHandler OnAfterFPCallback;
public FrmBaseSelectBatch() : this(null, null, null)
{
}
public FrmBaseSelectBatch(DynamicBaseSelectModel model, MyControl controlObj, GridControlEx gridControl)
{
InitializeComponent();
this.Model = model;
this.ControlObj = controlObj;
this.ParentGridControl = gridControl;
this.gcMain.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDown;
this.gcMain.GridView.CellValueChanging += new CellValueChangedEventHandler(OnGridViewCellValueChanging);
//this.gcMain.GridView.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(GridView_RowStyle);
this.gcMain.RefreshTotal = false;
}
/// <summary>
/// 初始化数据
/// </summary>
/// <param name="selectRowItem"></param>
public void InitData(DataRow selectRowItem)
{
this.SelectRowItem = selectRowItem;
this.SearchObj = new MyControl(ReplaceHelper.ReplaceRowParam(SelectRowItem, SearchSQL), gcMain, null, null, null);
this.PlSearch.Height = this.SearchObj.InitSearchControl(QueryTable, this.PlSearch);
if (!string.IsNullOrEmpty(KcAmountField))
{
this.lblSY.Text = this.lblTotal.Text = Convert.ToDouble(SelectRowItem[AmountField] + "") + "";
this.lblFP.Text = "0";
}
this.gcMain.GridControl.DataSource = null;
this.SearchObj.SearchGrid();//为啥取上一次?
if (this.FpOtherRows != null && this.FpOtherRows.Length > 0)
{
DataTable table = this.gcMain.GridControl.DataSource as DataTable;
foreach (DataRow item in this.FpOtherRows)
{
DataRow rowItem = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[RecordPrimaryField] + "" == item[RecordPrimaryField] + "");
if (rowItem != null)
{
rowItem[KcAmountField] = Convert.ToDouble(rowItem[KcAmountField]) - Convert.ToDouble(item[AmountField]);
}
}
double total = Convert.ToDouble(this.lblTotal.Text);
double ypfAmount = CalcYfpAmount();
this.lblFP.Text = ypfAmount + "";
this.lblSY.Text = (total - ypfAmount) + "";
}
if (this.FpRows != null && this.FpRows.Length > 0)
{
DataTable table = this.gcMain.GridControl.DataSource as DataTable;
foreach (DataRow item in this.FpRows)
{
DataRow rowItem = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[RecordPrimaryField] + "" == item[RecordPrimaryField] + "");
if (rowItem != null)
{
rowItem[AmountField] = Convert.ToDouble(item[AmountField]);
rowItem[JYAmountField] = Convert.ToDouble(rowItem[KcAmountField]) - Convert.ToDouble(rowItem[AmountField]);
}
}
double total = Convert.ToDouble(this.lblTotal.Text);
double ypfAmount = CalcYfpAmount();
this.lblFP.Text = ypfAmount + "";
this.lblSY.Text = (total - ypfAmount) + "";
}
}
/// <summary>
/// 初始化界面
/// </summary>
public void InitControl()
{
WaitForm.ShowForm();
try
{
DataRow modelRow = MainImpl.GetSystemdllTab(this.Model.ModuleCode);
if (modelRow == null)
{
MessageUtil.Show("模块编号[" + this.Model.ModuleCode + "],配置错误!\r\n该模块信息未找到!");
this.Close();
}
this.lblTotal.Text = Model.AmountValue;
this.SysModel = new ModuleModel(modelRow);
this.ParmaryKey = BaseImpl.GetBasePrimaryKey(this.Model.ModuleCode);
this.QueryTable = BaseModuleImpl.GetCustomQueryFields(this.SysModel.CondKey);
this.GridColumns = BaseModuleImpl.GetBaseGridColumns(this.Model.ModuleCode);
this.SearchSQL = this.ControlObj.ReplaceParentControlValue(this.SysModel.MenuSql);
if (this.QueryTable == null || this.QueryTable.Rows.Count > 0)
{
this.PlSearch.Visible = false;
}
if (this.GridColumns != null && this.GridColumns.Rows.Count > 0)
{
DataRow[] rows = this.GridColumns.Select("tm_tagid=1");
if (rows == null || rows.Length == 0)
{
MessageUtil.Show("未配置数量字段,基础档案库存数量字段标识设置为1!");
this.Close();
}
else
{
this.KcAmountField = rows[0]["fieldname"] + "";
}
rows = this.GridColumns.Select("tm_tagid=2");
if (rows == null || rows.Length == 0)
{
MessageUtil.Show("未配置数量字段,基础档案数量字段标识设置为2,可虚拟字段!");
this.Close();
}
else
{
this.AmountField = rows[0]["fieldname"] + "";
}
rows = this.GridColumns.Select("tm_tagid=3");
if (rows == null || rows.Length == 0)
{
MessageUtil.Show("未配置数量字段,基础档案数量字段标识设置为3,可虚拟字段!");
this.Close();
}
else
{
this.JYAmountField = rows[0]["fieldname"] + "";
}
rows = this.GridColumns.Select("tm_tagid=4");
if (rows == null || rows.Length == 0)
{
MessageUtil.Show("未配置唯一值字段,基础档案数量字段标识设置为4!");
this.Close();
}
else
{
this.RecordPrimaryField = rows[0]["fieldname"] + "";
}
}
this.gcMain.SetEditColumns(this.GridColumns, GridCustomColumnStruct.BaseMainGridView + this.SysModel.FormKey);
InitCheckEdit();
this.gcMain.SetGridRowColors(BaseModuleImpl.GetBaseGridRowColors(this.Model.ModuleCode));
this.gcMain.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(this.Model.ModuleCode), this.Model, OnGridViewRightCallBack, this.SearchObj, null);
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseModule);
}
Lskj.Control.Model.AutoSizeChange.ControllInitializeSize(this);
WaitForm.HideForm();
}
/// <summary>
/// <para>说明:计算剩余数量</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <returns>System.Single.</returns>
public double CalcYfpAmount()
{
DataTable table = this.gcMain.GridControl.DataSource as DataTable;
double total = 0.0;
DataRow[] rowItems = table.Rows.Cast<DataRow>().Where(x => x.RowState == DataRowState.Modified).ToArray();
DataRow rowItem = this.gcMain.GridView.GetFocusedDataRow();
if (rowItems != null && rowItems.Length > 0)
{
foreach (DataRow item in rowItems)
{
if (item == rowItem) continue;
total += Convert.ToDouble(item[AmountField] + "");
}
}
return total;
}
/// <summary>
/// <para>说明:获取差量</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <returns>System.Double.</returns>
public double GetSyAmount()
{
return Convert.ToDouble(lblSY.Text);
}
/// <summary>
/// <para>说明:创建CheckBoxEdit</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-21 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="gridColumn">The grid column.</param>
public void InitCheckEdit()
{
GridColumn gridColumn = new GridColumn();
RepositoryItemCheckEdit checkEdit = new RepositoryItemCheckEdit();
checkEdit.ValueChecked = true;
checkEdit.ValueUnchecked = false;
gridColumn.ColumnEdit = checkEdit;
gridColumn.Caption = "直接分配";
gridColumn.FieldName = "checkEdit";
gridColumn.Name = "checkEdit";
gridColumn.Visible = true;
gridColumn.VisibleIndex = 0;
gridColumn.ColumnEdit = checkEdit;
checkEdit.CheckedChanged += new EventHandler(OnCheckedChanged);
this.gcMain.GridControl.RepositoryItems.Add(checkEdit);
this.gcMain.GridView.Columns.Add(gridColumn);
}
/// <summary>
/// 选择状态改变触发事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnCheckedChanged(object sender, EventArgs e)
{
try
{
int rowIndex = this.gcMain.GridView.GetFocusedDataSourceRowIndex();
double total = Convert.ToDouble(this.lblTotal.Text);
double totalCopy = total - CalcOldAmount();
double fpAmount = 0.0;
CheckEdit checkEdit = sender as CheckEdit;
// 分配规则,根据当前行自动分配直到分完为止
DataRow item = this.gcMain.GridView.GetFocusedDataRow();
double kcAmount = Convert.ToDouble(item[KcAmountField] + "");
if (checkEdit.Checked)
{
double check = Convert.ToDouble(item[AmountField] + "");
if (check == 0.0)
{
if (kcAmount >= totalCopy)
{
item[AmountField] = totalCopy;
totalCopy = 0;
}
else
{
if (fpAmount + kcAmount > totalCopy)
{
// 最后一次分配
item[AmountField] = kcAmount - (totalCopy - fpAmount);
}
else
{
item[AmountField] = kcAmount;
}
}
}
}
else
{
item[AmountField] = 0;
}
fpAmount = Convert.ToDouble(item[AmountField]);
this.gcMain.OnGridViewCellValueChanged(this.gcMain, new CellValueChangedEventArgs(rowIndex, this.gcMain.GridView.Columns[AmountField], fpAmount));
item[JYAmountField] = Convert.ToDouble(item[KcAmountField]) - Convert.ToDouble(item[AmountField]);
double ypfAmount = CalcOldAmount();
this.lblFP.Text = ypfAmount + "";
this.lblSY.Text = (total - ypfAmount) + "";
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-11-02 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
public double CalcOldAmount()
{
DataTable table = this.gcMain.GridControl.DataSource as DataTable;
double total = 0.0;
DataRow[] rowItems = table.Rows.Cast<DataRow>().Where(x => x.RowState == DataRowState.Modified).ToArray();
//DataRow rowItem = this.gcMain.GridView.GetFocusedDataRow();
if (rowItems != null && rowItems.Length > 0)
{
foreach (DataRow item in rowItems)
{
//if (item == rowItem) continue;
total += Convert.ToDouble(item[AmountField] + "");
}
}
return total;
}
/// <summary>
/// <para>说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-11-02 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected void OnGridViewRightCallBack(object sender, EventArgs e)
{
try
{
this.SearchObj.SearchLastGrid();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:取消</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void OnBtnCancelClick(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// <para>说明:自动分配</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
public void OnBtnFPClick(object sender, EventArgs e)
{
if (OnAfterFPCallback != null)
{
OnAfterFPCallback(this, null);
}
}
public bool FP()
{
bool success = false;
// 分配规则,根据当前行自动分配直到分完为止
try
{
DataTable table = this.gcMain.GridControl.DataSource as DataTable;
if (table == null || table.Rows.Count == 0)
{
return false;
}
double total = Convert.ToDouble(this.lblTotal.Text);
double totalCopy = total - CalcOldAmount();
double fpAmount = 0.0;
int rowIndex = this.gcMain.GridView.GetFocusedDataSourceRowIndex();
DataRow item = null;
for (int i = rowIndex; i < table.Rows.Count; i++)
{
item = table.Rows[i];
double kcAmount = Convert.ToDouble(item[KcAmountField] + "");
double check = Convert.ToDouble(item[AmountField] + "");
totalCopy = total - CalcOldAmount();
if (check == 0.0)
{
if (kcAmount >= totalCopy)
{
item[AmountField] = totalCopy;
totalCopy = 0;
}
else
{
//if (fpAmount + kcAmount > total)
//{
// // 最后一次分配
// item[AmountField] = kcAmount - (total - fpAmount);
//}
//else
//{
item[AmountField] = kcAmount;
//}
}
}
fpAmount = Convert.ToDouble(item[AmountField]);
item[JYAmountField] = Convert.ToDouble(item[KcAmountField]) - Convert.ToDouble(item[AmountField]);
this.gcMain.GridView.SelectRowHandler(i);
this.gcMain.OnGridViewCellValueChanged(this.gcMain, new CellValueChangedEventArgs(i, this.gcMain.GridView.Columns[AmountField], fpAmount));
}
//double ypfAmount = CalcYfpAmount() + (item == null ? 0 : Convert.ToDouble(item[AmountField]));
double ypfAmount = CalcOldAmount();
this.lblFP.Text = ypfAmount + "";
this.lblSY.Text = (total - ypfAmount) + "";
if (Convert.ToDouble(this.lblFP.Text) != 0)
{
try
{
List<DataRow> listRows = new List<DataRow>();
foreach (DataRow dataRow in table.Rows)
{
if (dataRow.RowState == DataRowState.Modified && !string.IsNullOrEmpty(dataRow[AmountField] + "") && Convert.ToDouble(dataRow[AmountField] + "") > 0)
{
listRows.Add(dataRow);
}
}
this.SelectedRows = listRows.ToArray();
this.isNextOne = true;
success = true;
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("继续分配出错!" + Message);
success = false;
}
}
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("自动分配出错!" + Message);
}
return success;
}
/// <summary>
/// <para>说明:数量字段发生变化时</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs"/> instance containing the event data.</param>
void OnGridViewCellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
try
{
if (e.Column.Name == this.AmountField)
{
double total = Convert.ToDouble(lblTotal.Text);
double yfpAmount = CalcYfpAmount();
double fpAmount;
if (string.IsNullOrWhiteSpace(e.Value.ToString()))
{
this.gcMain.GridView.SetRowCellValue(e.RowHandle, e.Column, 0);
fpAmount = 0;
}
else
{
// 计算剩余数量
fpAmount = Convert.ToDouble(e.Value + "");
}
// 验证是否超出单行总数
DataRow rowItem = this.gcMain.GridView.GetFocusedDataRow();
double kcAmount = Convert.ToDouble(rowItem[KcAmountField] + "");
if (fpAmount > kcAmount)
{
fpAmount = kcAmount;
this.gcMain.GridView.SetRowCellValue(e.RowHandle, e.Column, fpAmount);
}
// 验证是否超总数
if (yfpAmount + fpAmount > total)
{
fpAmount = total - yfpAmount;
this.gcMain.GridView.SetRowCellValue(e.RowHandle, e.Column, fpAmount);
}
rowItem[JYAmountField] = Convert.ToDouble(rowItem[KcAmountField]) - fpAmount;
fpAmount = yfpAmount + fpAmount;
this.lblFP.Text = fpAmount + "";
this.lblSY.Text = (total - fpAmount) + "";
}
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:行颜色</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs"/> instance containing the event data.</param>
void GridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
try
{
DataRow rowItem = this.gcMain.GridView.GetDataRow(e.RowHandle);
double total = Convert.ToDouble(rowItem[KcAmountField]);
double fpAmount = Convert.ToDouble(rowItem[AmountField]);
if (fpAmount > 0)
{
e.Appearance.BackColor = total - fpAmount <= 0 ? Color.Green : Color.Red;
}
}
catch (Exception)
{
}
}
}
}