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 WMS.Frm.Base; using WMS.Common; using WMS.Model.Base; using System.Text.RegularExpressions; using System.Reflection; using DevExpress.XtraGrid.Columns; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid.Views.Base; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; using DevExpress.XtraReports.UI; using WMS.Business; using WMS.Business.Base; using WMS.FrmPrint; namespace WMS.FrmBaseData { public partial class FrmBaseCtlGoods : FormBase { #region 全局变量的声明 /// /// 容器Model /// public PalletModel sourceModel = new PalletModel(); /// /// 容器管理Model /// BaseCtlGoodsModel baseCtlGoodsModel = null; /// /// 本窗体gridView数据集合 /// List sourceList = null; /// /// 复选框选中数据集合 /// List checkList; /// /// 更换容器 追加新容器ID 条件 /// List listCondiction; /// /// 加一个int变量主要为了判断是否有checkbox选中项 /// int i = 0; /// /// 体积累加 /// decimal sumVolume; /// /// 全选判断 /// bool m_status = false; string path = string.Empty; private HSSFWorkbook _workbook; // private XSSFWorkbook _xworkbookl; private ISheet _sheet; #endregion #region 构造函数 /// /// 无参构造 /// public FrmBaseCtlGoods() { InitializeComponent(); } #endregion #region 窗体加载 /// /// 窗体加载 /// /// /// private void FrmBaseCtlGoods_Load(object sender, EventArgs e) { //通用查询 FrmSelectModle = new BaseCtlGoodsModel(); //下拉 lkpTranstationType.LoadData(); BindData(); if (frmButtonItem.ContainsKey("Clear")) { frmButtonItem["Clear"].Enabled = true; } if (frmButtonItem.ContainsKey("Export")) { frmButtonItem["Export"].Enabled = true; } if (frmButtonItem.ContainsKey("Unbound")) { frmButtonItem["Unbound"].Enabled = true; } if (frmButtonItem.ContainsKey("Print")) { frmButtonItem["Print"].Enabled = true; } if (frmButtonItem.ContainsKey("Search")) { frmButtonItem["Search"].Enabled = true; } if (frmButtonItem.ContainsKey("Refresh")) { frmButtonItem["Refresh"].Enabled = true; } if (frmButtonItem.ContainsKey("Confirm")) { frmButtonItem["Confirm"].Enabled = true; } if (frmButtonItem.ContainsKey("Save")) { frmButtonItem["Save"].Enabled = false; } //加载时 注册 EditValueChanged事件 为变更搬运人服务 // DictionarycarryReplace.t_dictonary.EditValueChanged += new System.EventHandler(t_dictonary_EditValueChanged); } #endregion #region 事件 #region CheckBox全选的实现:3事件 /// /// Click 事件 调用DevControlHelper 帮助类 /// /// /// private void gridViewCtlGoods_Click(object sender, EventArgs e) { if (DevControlHelper.ClickGridCheckBox(this.gridViewCtlGoods, "IsCheck", m_status)) { m_status = !m_status; } } /// /// CustomDrawColumnHeader 事件 /// /// /// private void gridViewCtlGoods_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e) { //if (e.Column != null && e.Column.FieldName == "IsCheck") //{ // e.Info.InnerElements.Clear(); // e.Painter.DrawObject(e.Info); // DevControlHelper.DrawCheckBox(e, m_status); // e.Handled = true; //} } /// /// DataSourceChanged 事件 /// /// /// private void gridViewCtlGoods_DataSourceChanged(object sender, EventArgs e) { GridColumn column = this.gridViewCtlGoods.Columns.ColumnByFieldName("IsCheck"); if (column != null) { column.Width = 80; column.OptionsColumn.ShowCaption = m_status; column.ColumnEdit = new RepositoryItemCheckEdit(); } } #endregion #region 变更搬运人EditValueChanged 事件 //变更搬运人 private void t_dictonary_EditValueChanged(object sender, EventArgs e) { //获得选中数据行 GetCheckData(); if (i == 0) { SystemCommon.ShowInfoMessageBox("请至少选择一种商品才能进行变更搬运人操作!"); //this.DictionarycarryReplace.TextBoxFlag = string.Empty; //this.DictionarycarryReplace.TextBoxValue = string.Empty; } if (i > 0) { foreach (BaseCtlGoodsModel list in sourceList) { if (!string.IsNullOrEmpty(list.User_ID) || !string.IsNullOrEmpty(list.NAME)) { //list.User_ID = this.DictionarycarryReplace.TextBoxFlag; //list.NAME = this.DictionarycarryReplace.TextBoxValue; } } this.gridViewCtlGoods.RefreshData(); this.lkpTranstationType.Focus(); } } #endregion #region 未查询到数据时的提示CustomDrawEmptyForeground 事件 private void gridViewCtlGoods_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e) { ColumnView columnView = sender as ColumnView; BindingSource bindingSource = this.gridViewCtlGoods.DataSource as BindingSource; if (bindingSource.Count == 0) { string str = "<对不起,没有查询到你所想要的数据!>"; Font f = new Font("宋体", 11, FontStyle.Regular); Rectangle r = new Rectangle(e.Bounds.Top + 25, e.Bounds.Left + 25, e.Bounds.Right - 25, e.Bounds.Height - 25); e.Graphics.DrawString(str, f, Brushes.Gray, r); } } #endregion #endregion #region 辅助方法 #region 公共菜单 按钮 方法 /// /// 查询 /// public void Search() { //将挂起的更改 绑定到数据源 bgsConditions.EndEdit(); BindData(); } /// /// 打印 /// public void Print() { //if (DialogResult.No == SystemCommon.ShowMessageBoxResult("是否打印选中的库位条码")) //{ // return; //} List listLoc = new List(); for (int handle = 0; handle < gridViewCtlGoods.RowCount; handle++) { string id = gridViewCtlGoods.GetRowCellValue(handle, "IsCheck").ToString(); if (gridViewCtlGoods.GetRowCellValue(handle, "IsCheck").ToString() == "1") { //BarCodeReport.GetInstance(id, 7).PrintDialog(); } //StorageAreaLocationModel loc = new StorageAreaLocationModel(); //loc = GVStorageAreaLocation.GetRow(handle) as StorageAreaLocationModel; //listLoc.Add(loc); } //listLoc.RemoveAll(p=>p.LOCATION_ID == null||p.LOCATION_ID == string.Empty); } /// /// 重置 /// public void Refresh() { this.lkpTranstationType.Text = string.Empty; this.lkpTranstationType.EditValue = string.Empty; this.lkpTranstationType.Properties.ShowPopupShadow = false; this.Dictionarycarry.TextBoxFlag = string.Empty; this.Dictionarycarry.TextBoxValue = string.Empty; BindData(); if (frmButtonItem.ContainsKey("Save")) { frmButtonItem["Save"].Enabled = false; } } /// /// 清空 /// public void Clear() { this.lkpTranstationType.EditValue = string.Empty; this.lkpTranstationType.Text = string.Empty; this.Dictionarycarry.TextBoxFlag = string.Empty; this.Dictionarycarry.TextBoxValue = string.Empty; } /// /// 导出到Excel /// public void Export() { DataToExcel(); } //选托盘 按钮 /* public void spallet() { //托盘窗体 FrmPallet frmPallet = new FrmPallet(this); GetCheckData(); //选托盘方法 SearchPallet(frmPallet); } */ /// /// 解除绑定 /// public void Unbound() { //获得选中数据 GetCheckData(); Unbounds(); BindData(); } /// /// 保存 /// public void Save() { #region 验证所更换的容器是否是当前正在使用的容器 foreach (BaseCtlGoodsModel mm in checkList) { if (mm.CTL_PALLET_ID == this.Dictionarycarry.TextBoxFlag) { SystemCommon.ShowInfoMessageBox("当前容器即为该选择容器,请重新选择!"); mm.IsCheck = false; return; } if (string.IsNullOrEmpty(mm.VC_DICTIONARY_ID)) { SystemCommon.ShowInfoMessageBox("请选择这批物料的事务类型!"); return; } } #endregion SaveSelectPallet(); this.gridViewCtlGoods.RefreshData(); BindData(); if (frmButtonItem.ContainsKey("Save")) { frmButtonItem["Save"].Enabled = false; } } /// /// 确认 /// public void Confirm() { Replace(); //listCondiction.AddRange(checkList); if (frmButtonItem.ContainsKey("Save")) { frmButtonItem["Save"].Enabled = true; } } /// /// 导入Excel 到数据 /// public void Audit() { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { path = openFileDialog1.FileName; ReadExecl(path); } List tabletolist = ConvertHelper.ConvertToList(table); //bgsPallet.DataSource = tabletolist; //this.gridViewPallet.RefreshData(); } DataTable table = new DataTable(); private DataTable ReadExecl(string path) { try { using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { //HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls //XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx _workbook = new HSSFWorkbook(stream); //_xworkbookl = new XSSFWorkbook(stream); _sheet = _workbook.GetSheetAt(0); IRow headerRow = _sheet.GetRow(0);//第一行为标题行 int cellCount = headerRow.LastCellNum;//LastCellNum = PhysicalNumberOfCells int rowCount = _sheet.LastRowNum;//LastRowNum = PhysicalNumberOfRows - 1 for (int i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } for (int i = (_sheet.FirstRowNum + 1); i <= rowCount; i++) { IRow row = _sheet.GetRow(i); DataRow dataRow = table.NewRow(); if (row != null) { for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) { dataRow[j] = row.GetCell(j).ToString(); } } } table.Rows.Add(dataRow); } return table; } } catch (Exception e) { SystemCommon.ShowErrorMessageBox("读取EXCEL失败!" + e.Message); return table; } } #endregion #region 通用查询 /// /// 通用查询 /// /// public override void LoadListData(DataTable table) { sourceList = ConvertHelper.ConvertToList(table); bgsConditions.DataSource = sourceList; gridViewCtlGoods.RefreshData(); } #endregion #region 数据绑定 /// /// 数据绑定 /// private void BindData() { #region 查询条件 //搬运人ID //baseCtlGoodsModel = bgsConditions.DataSource as BaseCtlGoodsModel; //baseCtlGoodsModel.User_ID = this.Dictionarycarry.TextBoxFlag; // bgsConditions.DataSource = new BaseCtlGoodsModel(); baseCtlGoodsModel = new BaseCtlGoodsModel(); baseCtlGoodsModel.CTL_PALLET_ID = this.txtPallet_id.Text; bgsConditions.DataSource = baseCtlGoodsModel; #endregion try { BaseCtlGoodsModel model = bgsConditions.DataSource as BaseCtlGoodsModel; sourceList = IBussFactory.Instance().GetDataList(model); bgsBaseCtlGoods.DataSource = sourceList; this.gridViewCtlGoods.RefreshData(); } catch (Exception ex) { SystemCommon.ShowErrorMessageBox("网络连接错误!请检查当前网络环境是否可用!"); } } #endregion #region 获得checkbox选中项数据 /// /// 获得check 选中项 /// private void GetCheckData() { checkList = new List(); //赋值为初始值 是为了再次操作时 重新计量 i = 0; sumVolume = 0; this.lkpTranstationType.Focus(); foreach (BaseCtlGoodsModel model in sourceList) { if (model.IsCheck == true) { checkList.Add(model); //累加商品体积 sumVolume += model.GVOLUME; i++; } } } #endregion #region 解绑按钮方法 /// /// 解绑按钮方法 /// private void Unbounds() { string strmodel; string errtex; if (i == 0) { SystemCommon.ShowInfoMessageBox("请至少选择一种商品才能进行解绑操作!!"); return; } #region 操作 解除绑定 if (i == 1) { foreach (BaseCtlGoodsModel list in checkList) { //1.无商品 有托盘 可解绑 if (string.IsNullOrEmpty(list.GOODS_ID) && !string.IsNullOrEmpty(list.CTL_PALLET_ID)) { //listCondiction.Add(list); DialogResult dr = MessageBox.Show("确定要解除绑定吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dr == DialogResult.Yes) { try { //删除表中的 托盘数据信息 errtex = IBussFactory.Instance().DeleteData(list); if (!string.IsNullOrEmpty(errtex)) { SystemCommon.ShowInfoMessageBox("解除绑定失败!"); } else { SystemCommon.ShowInfoMessageBox("解除绑定成功!"); this.gridViewCtlGoods.RefreshData(); } } catch (Exception ex) { SystemCommon.ShowErrorMessageBox("网络连接错误!请检查当前网络环境是否可用!"); } } return; } //2.有商品 有托盘 不能解绑 if (!string.IsNullOrEmpty(list.GOODS_ID) && !string.IsNullOrEmpty(list.CTL_PALLET_ID)) { SystemCommon.ShowInfoMessageBox("该容器上放有物料商品,必须把物料移到其他容器才可以解除绑定!"); return; } } } #endregion if (i > 1) { SystemCommon.ShowInfoMessageBox("解除绑定只允许单条解绑,请选择一条数据!"); } } #endregion #region 选托盘按钮方法 /********选托盘按钮方法 private void SearchPallet(FrmPallet frmPallet) { if (i == 0) { SystemCommon.ShowInfoMessageBox("请至少选择一种商品!!"); return; } if (i > 0) { DialogResult rlt = frmPallet.ShowDialog(); if (rlt == DialogResult.OK) { foreach (BaseCtlGoodsModel model in checkList) { model.CTL_PALLET_ID = sourceModel.PALLET_ID; model.PALLET_NAME = sourceModel.PALLET_NAME; model.VOLUME = sourceModel.VOLUME; model.BEARWEIGHT = sourceModel.BEARWEIGHT; } this.gridViewCtlGoods.RefreshData(); } //判断商品总体积是否大于托盘最大体积 if (sumVolume >= sourceModel.VOLUME) { DialogResult dr = MessageBox.Show("所选商品体积大于托盘最大体积容量,是否放入容器?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dr == DialogResult.Yes) { SystemCommon.ShowInfoMessageBox("放入成功"); } } } } */ ////// #endregion #region 导出数据到Excel文件方法 /// /// 导出数据到Excel /// private void DataToExcel() { SaveFileDialog fileDialog = new SaveFileDialog(); fileDialog.Title = "导出Excel"; fileDialog.Filter = "Excel文件(*.xls)|*.xls"; DialogResult dialogResult = fileDialog.ShowDialog(this); if (dialogResult == DialogResult.OK) { DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions(); this.gridViewCtlGoods.ExportToXls(fileDialog.FileName); DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } #endregion #region 保存换容器方法 /// /// 保存换容器数据 /// private void SaveSelectPallet() { this.lkpTranstationType.Focus(); foreach (BaseCtlGoodsModel s in sourceList) { s.OPERATORID = userData.USER_ID; s.OPERATEDATE = DateTime.Now; if (s.IsCheck == true) { listCondiction.Add(s); } } try { string errtex = IBussFactory.Instance().Operator(listCondiction, 1); if (!string.IsNullOrEmpty(errtex)) { SystemCommon.ShowInfoMessageBox("操作失败!"); } else { SystemCommon.ShowInfoMessageBox("操作成功!"); foreach (BaseCtlGoodsModel m in sourceList) { m.PALLET_NEWID = ""; m.IsCheck = false; } this.Dictionarycarry.TextBoxFlag = string.Empty; this.Dictionarycarry.TextBoxValue = string.Empty; } } catch (Exception ex) { SystemCommon.ShowErrorMessageBox("网络连接错误!请检查当前网络环境是否可用!"); } BindData(); } #endregion #region 确认按钮显示 新编号 /// /// 追加新 容器编号(为了替换原先的容器) /// private void Replace() { listCondiction = new List(); GetCheckData(); if (i <= 0) { SystemCommon.ShowInfoMessageBox("请至少选择一种商品才能进行更换容器操作!"); return; } else if (i > 0) { foreach (BaseCtlGoodsModel model in checkList) { //选中项不含商品时 if (string.IsNullOrEmpty(model.GOODS_ID)) { SystemCommon.ShowInfoMessageBox("所选数据中含有空容器,不用进行更换,建议解除绑定!"); model.IsCheck = false; this.Dictionarycarry.TextBoxFlag = string.Empty; this.Dictionarycarry.TextBoxValue = string.Empty; this.gridViewCtlGoods.RefreshData(); return; } else if (string.IsNullOrEmpty(this.Dictionarycarry.TextBoxFlag.Trim())) { SystemCommon.ShowInfoMessageBox("请先选择一种容器才能进行更换!"); return; } model.PALLET_NEWID = this.Dictionarycarry.TextBoxFlag; } } this.gridViewCtlGoods.RefreshData(); } #endregion #region lkpTranstationType_EditValueChanged事件 private void lkpTranstationType_EditValueChanged(object sender, EventArgs e) { this.lkpTranstationType.Focus(); if (string.IsNullOrEmpty(this.lkpTranstationType.EditValue.ToString())) { return; } //bgsBaseCtlGoods.DataSource = sourceList.FindAll(r => r.VC_DICTIONARY_ID == this.lkpTranstationType.EditValue.ToString()); foreach (BaseCtlGoodsModel bcg in sourceList) { if (bcg.IsCheck == true) { if (string.IsNullOrEmpty(bcg.GOODS_ID)) { bcg.VC_DICTIONARY_ID = string.Empty; bcg.VC_DICTIONARY_NAME = string.Empty; } else { bcg.VC_DICTIONARY_ID = this.lkpTranstationType.EditValue.ToString(); bcg.VC_DICTIONARY_NAME = this.lkpTranstationType.Text; } } } bgsBaseCtlGoods.DataSource = sourceList; this.gridViewCtlGoods.RefreshData(); } #endregion #endregion } }