Files
lserp_cs_6.0/插件库/Lskj.Control/FrmBaseSelect.cs
cyf ab56a9bcf7 基线 SVN r240
SVN-Revision: r240
2025-02-06 06:46:06 +00:00

697 lines
28 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 FrmBaseSelect : BaseForm
{
string ParmaryKey;
/// <summary>
/// 库存数量字段、输入数量字段、结余数量字段、记录唯一值标识
/// </summary>
public string KcAmountField, AmountField, JYAmountField, RecordPrimaryField;
DataTable QueryTable = new DataTable();
DataTable GridColumns = new DataTable();
MyControl SearchObj = new MyControl();
// 传入参数
MyControl ControlObj = null;
GridControlEx ParentGridControl;
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;
public FrmBaseSelect()
: this(null, null, null)
{
}
public FrmBaseSelect(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 DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(OnGridViewCellValueChanging);
//this.gcMain.GridView.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(GridView_RowStyle);
this.btnNextData.Enabled = false;
this.lblFP.TextChanged += new EventHandler(onLblFPextChanged);
this.gcMain.RefreshTotal = false;
}
private void onLblFPextChanged(object sender, EventArgs e)
{
if (Convert.ToDouble(this.lblFP.Text)== 0)
{
this.btnNextData.Enabled = false;
}
else
{
this.btnNextData.Enabled = true;
}
}
/// <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>说明:初始化</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected override void OnLoad(EventArgs e)
{
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);
//List<string> paramList = ReplaceHelper.GetParamFields(this.SysModel.MenuSql);
//foreach (string item in paramList)
//{
// if (item.Contains("{#"))
// {
// string fieldName = item.Replace("{#", "").Replace("}", "");
// ControlModel controlModel = this.ControlObj.ControlModels.Find(x => x.FieldName.Equals(fieldName, StringComparison.OrdinalIgnoreCase));
// if (string.IsNullOrEmpty(this.ControlObj.GetControlValue(controlModel)))
// {
// MessageUtil.Show("请先录入[" + controlModel.LabelText + "]");
// this.Close();
// break;
// }
// }
// else if (item.Contains("{"))
// {
// string fieldName = item.Replace("{", "").Replace("}", "");
// if (string.IsNullOrEmpty(this.SelectRowItem[fieldName] + ""))
// {
// MessageUtil.Show("请先录入[" + this.ParentGridControl.GridView.Columns[fieldName].Caption + "]");
// this.Close();
// break;
// }
// }
//}
string searchSQL = this.ControlObj.ReplaceParentControlValue(this.SysModel.MenuSql);
this.SearchObj = new MyControl(ReplaceHelper.ReplaceRowParam(SelectRowItem, searchSQL), gcMain, null, null, null);
this.PlSearch.Height = this.SearchObj.InitSearchControl(QueryTable, this.PlSearch);
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"] + "";
}
}
if (string.IsNullOrEmpty(KcAmountField))
{
this.lblTotal.Text = this.lblSY.Text = this.lblFP.Text = "0";
this.btnFP.Enabled = this.btnCancel.Enabled = false;
}
else
{
this.lblSY.Text = this.lblTotal.Text = Convert.ToDouble(SelectRowItem[AmountField] + "") + "";
this.lblFP.Text = "0";
}
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);
this.SearchObj.SearchLastGrid();
//
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) + "";
}
}
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>说明:创建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 OnBtnOKClick(object sender, EventArgs e)
{
try
{
this.isNextOne = false;
List<DataRow> listRows = new List<DataRow>();
DataTable table = this.gcMain.gridControl.DataSource as DataTable;
foreach (DataRow item in table.Rows)
{
if (item.RowState == DataRowState.Modified && !string.IsNullOrEmpty(item[AmountField] + "")&& Convert.ToDouble(item[AmountField] + "") != 0)
{
listRows.Add(item);
}
}
this.SelectedRows = listRows.ToArray();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
// this.SelectRowItem = null;
this.Close();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("确定出错!" + Message);
}
}
//点击下一个
private void OnTheNextClick(object sender, EventArgs e)
{
try
{
List<DataRow> listRows = new List<DataRow>();
DataTable table = this.gcMain.gridControl.DataSource as DataTable;
foreach (DataRow item in table.Rows)
{
if (item.RowState == DataRowState.Modified && !string.IsNullOrEmpty(item[AmountField] + "") && Convert.ToDouble(item[AmountField] + "") > 0)
{
listRows.Add(item);
}
}
this.SelectedRows = listRows.ToArray();
this.isNextOne = true;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("确定出错!" + 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>
private void OnBtnFPClick(object sender, EventArgs e)
{
// 分配规则,根据当前行自动分配直到分完为止
try
{
DataTable table = this.gcMain.GridControl.DataSource as DataTable;
if (table == null || table.Rows.Count == 0) return;
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) + "";
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("自动分配出错!" + 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="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)
{
}
}
}
}