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; /// /// 库存数量字段、输入数量字段、结余数量字段、记录唯一值标识 /// 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; /// /// 系统模块实体对象 /// /// The system model. public ModuleModel SysModel { get; private set; } /// /// 是否点击下一个 /// public bool isNextOne = false; /// /// 分配完成后回调 /// 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; } /// /// 初始化数据 /// /// 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.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().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().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) + ""; } } /// /// 初始化界面 /// 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(); } /// /// 说明:计算剩余数量 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.Single. public double CalcYfpAmount() { DataTable table = this.gcMain.GridControl.DataSource as DataTable; double total = 0.0; DataRow[] rowItems = table.Rows.Cast().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; } /// /// 说明:获取差量 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.Double. public double GetSyAmount() { return Convert.ToDouble(lblSY.Text); } /// /// 说明:创建CheckBoxEdit /// 创建人:龚宇超 /// 创建日期:2017-08-21 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The grid column. 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); } /// /// 选择状态改变触发事件 /// /// /// 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); } } /// /// 说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新 /// 创建人:龚宇超 /// 创建日期:2017-11-02 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. public double CalcOldAmount() { DataTable table = this.gcMain.GridControl.DataSource as DataTable; double total = 0.0; DataRow[] rowItems = table.Rows.Cast().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; } /// /// 说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新 /// 创建人:龚宇超 /// 创建日期:2017-11-02 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. protected void OnGridViewRightCallBack(object sender, EventArgs e) { try { this.SearchObj.SearchLastGrid(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:取消 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void OnBtnCancelClick(object sender, EventArgs e) { this.Close(); } /// /// 说明:自动分配 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. 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 listRows = new List(); 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; } /// /// 说明:数量字段发生变化时 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. 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); } } /// /// 说明:行颜色 /// 创建人:龚宇超 /// 创建日期: