| | |
| | | private const int COL_QTY = 4; |
| | | private const int COL_ISPRINTED = 5; |
| | | private const int COL_PRINTDATE = 6; |
| | | private const int COL_PRINTERNAME = 7; |
| | | private const int COL_PRINTERNAME = 7; |
| | | private const int COL_REMARK = 8; // 新增:内外箱标识 |
| | | |
| | | // 报表相关 |
| | | private GridppReport Report; |
| | |
| | | |
| | | // 初始状态:按钮禁用,定时器未启动 |
| | | btnToggleAutoPrint.Enabled = false; |
| | | timer1.Interval = 60000; |
| | | timer1.Interval = 30000; |
| | | timer1.Stop(); |
| | | } |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | string templateName = GetConfigKey(AppDomain.CurrentDomain.BaseDirectory + "./Config/PWD.config", "PrintTemplate"); |
| | | if (string.IsNullOrEmpty(templateName) || templateName == "undefined") |
| | | { |
| | | LogService.Write("自动打印: 未配置打印模板,跳过"); |
| | | return; |
| | | } |
| | | |
| | | string safeAlias = alias.Replace("'", "''"); |
| | | |
| | | string sql = $@" |
| | |
| | | a.HQty AS 数量, |
| | | a.HIsPrintSuccess AS 是否打印成功, |
| | | a.HLastPrintDate AS 打印时间, |
| | | a.HRemark AS 内外箱, |
| | | a.HPrintName AS 打印机名称 |
| | | FROM Gy_BarCodeBill_Split a |
| | | INNER JOIN Gy_Material b ON a.HMaterID = b.HItemID |
| | | WHERE a.HIsPrintSuccess = 0 AND ISNULL(a.HPrintName,'') = '{safeAlias}' |
| | | ORDER BY a.HItemId"; |
| | | |
| | | DataSet ds = oCn.RunProcReturn(sql, "AutoPrintQuery"); |
| | | DataSet ds = oCn.RunProcReturn(sql, "Gy_BarCodeBill_Split"); |
| | | if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) |
| | | return; |
| | | |
| | | List<string> barcodeList = new List<string>(); |
| | | List<int> idList = new List<int>(); |
| | | // 收集所有待打印条码信息 |
| | | List<Tuple<int, string, string>> items = new List<Tuple<int, string, string>>(); |
| | | foreach (DataRow dr in ds.Tables[0].Rows) |
| | | { |
| | | string barcode = dr["条码编号"].ToString().Trim(); |
| | | if (string.IsNullOrEmpty(barcode)) continue; |
| | | barcodeList.Add(barcode.Replace("'", "''")); |
| | | idList.Add(Convert.ToInt32(dr["id"])); |
| | | int id = Convert.ToInt32(dr["id"]); |
| | | string remark = dr["内外箱"]?.ToString() ?? ""; |
| | | items.Add(Tuple.Create(id, barcode, remark)); |
| | | } |
| | | |
| | | if (barcodeList.Count == 0) return; |
| | | if (items.Count == 0) return; |
| | | |
| | | string inClause = string.Join(",", barcodeList.Select(b => "'" + b + "'")); |
| | | string viewSql = $@" |
| | | SELECT |
| | | 条码编号, |
| | | 物料代码, |
| | | 物料名称, |
| | | 规格型号, |
| | | 批号, |
| | | 数量, |
| | | 供应商, |
| | | 组托单号, |
| | | 生产日期, |
| | | 源单单号 |
| | | FROM h_v_IF_BarCodeBillList_Split |
| | | WHERE 条码编号 IN ({inClause})"; |
| | | // 按类型(内外箱)分组 |
| | | var groups = items.GroupBy(x => x.Item3); |
| | | int totalPrinted = 0; |
| | | |
| | | DataSet dsView = oCn.RunProcReturn(viewSql, "AutoPrintView"); |
| | | if (dsView == null || dsView.Tables.Count == 0 || dsView.Tables[0].Rows.Count == 0) |
| | | return; |
| | | |
| | | DataTable dtPrint = new DataTable(); |
| | | dtPrint.Columns.Add("条码编号", typeof(string)); |
| | | dtPrint.Columns.Add("物料代码", typeof(string)); |
| | | dtPrint.Columns.Add("物料名称", typeof(string)); |
| | | dtPrint.Columns.Add("规格型号", typeof(string)); |
| | | dtPrint.Columns.Add("批号", typeof(string)); |
| | | dtPrint.Columns.Add("数量", typeof(string)); |
| | | dtPrint.Columns.Add("供应商", typeof(string)); |
| | | dtPrint.Columns.Add("组托单号", typeof(string)); |
| | | dtPrint.Columns.Add("生产日期", typeof(string)); |
| | | dtPrint.Columns.Add("源单单号", typeof(string)); |
| | | |
| | | foreach (DataRow dr in dsView.Tables[0].Rows) |
| | | foreach (var group in groups) |
| | | { |
| | | DataRow newRow = dtPrint.NewRow(); |
| | | newRow["条码编号"] = dr["条码编号"].ToString(); |
| | | newRow["物料代码"] = dr["物料代码"].ToString(); |
| | | newRow["物料名称"] = dr["物料名称"].ToString(); |
| | | newRow["规格型号"] = dr["规格型号"].ToString(); |
| | | newRow["批号"] = dr["批号"].ToString(); |
| | | object qty = dr["数量"]; |
| | | newRow["数量"] = qty == DBNull.Value ? "0" : Convert.ToDecimal(qty).ToString("0"); |
| | | newRow["供应商"] = dr["供应商"].ToString(); |
| | | newRow["组托单号"] = dr["组托单号"].ToString(); |
| | | object prodDate = dr["生产日期"]; |
| | | newRow["生产日期"] = prodDate == DBNull.Value ? "" : Convert.ToDateTime(prodDate).ToString("yyyy-MM-dd"); |
| | | newRow["源单单号"] = dr["源单单号"].ToString(); |
| | | dtPrint.Rows.Add(newRow); |
| | | string remark = group.Key; |
| | | string templateName = GetTemplateNameByRemark(remark); |
| | | if (string.IsNullOrEmpty(templateName) || templateName == "undefined") |
| | | { |
| | | LogService.Write($"自动打印: 未找到类型“{remark}”对应的模板,跳过该组"); |
| | | continue; |
| | | } |
| | | |
| | | // 构建该组的条码列表和ID列表 |
| | | List<string> barcodeList = group.Select(x => x.Item2.Replace("'", "''")).ToList(); |
| | | List<int> idList = group.Select(x => x.Item1).ToList(); |
| | | |
| | | string inClause = string.Join(",", barcodeList.Select(b => "'" + b + "'")); |
| | | |
| | | // 查询视图数据 |
| | | string viewSql = $@" |
| | | SELECT |
| | | 条码编号, |
| | | 物料代码, |
| | | 物料名称, |
| | | 规格型号, |
| | | 批号, |
| | | 数量, |
| | | 供应商, |
| | | 组托单号, |
| | | 生产日期, |
| | | 源单单号 |
| | | FROM h_v_IF_BarCodeBillList_Split |
| | | WHERE 条码编号 IN ({inClause})"; |
| | | |
| | | DataSet dsView = oCn.RunProcReturn(viewSql, "h_v_IF_BarCodeBillList_Split"); |
| | | if (dsView == null || dsView.Tables.Count == 0 || dsView.Tables[0].Rows.Count == 0) |
| | | { |
| | | LogService.Write($"自动打印: 类型“{remark}”的条码详情查询无结果,跳过"); |
| | | continue; |
| | | } |
| | | |
| | | // 构建打印数据表 |
| | | DataTable dtPrint = new DataTable(); |
| | | dtPrint.Columns.Add("条码编号", typeof(string)); |
| | | dtPrint.Columns.Add("物料代码", typeof(string)); |
| | | dtPrint.Columns.Add("物料名称", typeof(string)); |
| | | dtPrint.Columns.Add("规格型号", typeof(string)); |
| | | dtPrint.Columns.Add("批号", typeof(string)); |
| | | dtPrint.Columns.Add("数量", typeof(string)); |
| | | dtPrint.Columns.Add("供应商", typeof(string)); |
| | | dtPrint.Columns.Add("组托单号", typeof(string)); |
| | | dtPrint.Columns.Add("生产日期", typeof(string)); |
| | | dtPrint.Columns.Add("源单单号", typeof(string)); |
| | | |
| | | foreach (DataRow dr in dsView.Tables[0].Rows) |
| | | { |
| | | DataRow newRow = dtPrint.NewRow(); |
| | | newRow["条码编号"] = dr["条码编号"].ToString(); |
| | | newRow["物料代码"] = dr["物料代码"].ToString(); |
| | | newRow["物料名称"] = dr["物料名称"].ToString(); |
| | | newRow["规格型号"] = dr["规格型号"].ToString(); |
| | | newRow["批号"] = dr["批号"].ToString(); |
| | | object qty = dr["数量"]; |
| | | newRow["数量"] = qty == DBNull.Value ? "0" : Convert.ToDecimal(qty).ToString("0"); |
| | | newRow["供应商"] = dr["供应商"].ToString(); |
| | | newRow["组托单号"] = dr["组托单号"].ToString(); |
| | | object prodDate = dr["生产日期"]; |
| | | newRow["生产日期"] = prodDate == DBNull.Value ? "" : Convert.ToDateTime(prodDate).ToString("yyyy-MM-dd"); |
| | | newRow["源单单号"] = dr["源单单号"].ToString(); |
| | | dtPrint.Rows.Add(newRow); |
| | | } |
| | | |
| | | // 创建临时DataGridView(含全选列) |
| | | DataGridView tempGrid = new DataGridView(); |
| | | tempGrid.DataSource = dtPrint; |
| | | DataGridViewCheckBoxColumn selectCol = new DataGridViewCheckBoxColumn(); |
| | | selectCol.HeaderText = "选择"; |
| | | selectCol.Name = "选择"; |
| | | tempGrid.Columns.Insert(0, selectCol); |
| | | foreach (DataGridViewRow row in tempGrid.Rows) |
| | | { |
| | | row.Cells["选择"].Value = true; |
| | | } |
| | | |
| | | // 设置报表并打印 |
| | | Sub_SetReport(templateName, tempGrid); |
| | | Report.Printer.PrinterName = txtPrinter.Text.Trim(); |
| | | Report.Print(false); |
| | | |
| | | // 更新该组条码状态为已打印 |
| | | if (idList.Count > 0) |
| | | { |
| | | string ids = string.Join(",", idList); |
| | | string updateSql = $@"UPDATE Gy_BarCodeBill_Split SET HIsPrintSuccess = 1, HLastPrintDate = GETDATE() WHERE HItemId IN ({ids})"; |
| | | oCn.RunProc(updateSql); |
| | | } |
| | | |
| | | totalPrinted += barcodeList.Count; |
| | | LogService.Write($"自动打印: 类型“{remark}”打印完成,共 {barcodeList.Count} 个条码。"); |
| | | } |
| | | |
| | | DataGridView tempGrid = new DataGridView(); |
| | | tempGrid.DataSource = dtPrint; |
| | | DataGridViewCheckBoxColumn selectCol = new DataGridViewCheckBoxColumn(); |
| | | selectCol.HeaderText = "选择"; |
| | | selectCol.Name = "选择"; |
| | | tempGrid.Columns.Insert(0, selectCol); |
| | | foreach (DataGridViewRow row in tempGrid.Rows) |
| | | { |
| | | row.Cells["选择"].Value = true; |
| | | } |
| | | |
| | | Sub_SetReport(templateName, tempGrid); |
| | | Report.Printer.PrinterName = txtPrinter.Text.Trim(); |
| | | //Report.PrintPreview(true); // 弹出预览窗口 |
| | | Report.Print(false); |
| | | |
| | | if (idList.Count > 0) |
| | | { |
| | | string ids = string.Join(",", idList); |
| | | string updateSql = $@"UPDATE Gy_BarCodeBill_Split SET HIsPrintSuccess = 1, HLastPrintDate = GETDATE() WHERE HItemId IN ({ids})"; |
| | | oCn.RunProc(updateSql); |
| | | } |
| | | |
| | | // 刷新界面数据 |
| | | LoadData(); |
| | | LogService.Write($"自动打印完成,共打印 {barcodeList.Count} 个条码。"); |
| | | LogService.Write($"自动打印全部完成,总计打印 {totalPrinted} 个条码。"); |
| | | })); |
| | | } |
| | | catch (Exception ex) |
| | |
| | | #region 表格初始化 |
| | | private void InitGrids() |
| | | { |
| | | grdUnprinted.ColumnCount = 8; |
| | | grdUnprinted.ColumnCount = 9; |
| | | grdUnprinted.Columns[COL_ID].HeaderText = "ID"; |
| | | grdUnprinted.Columns[COL_ID].Visible = false; |
| | | grdUnprinted.Columns[COL_BARCODE].HeaderText = "条码编号"; |
| | |
| | | grdUnprinted.Columns[COL_PRINTDATE].Width = 150; |
| | | grdUnprinted.Columns[COL_PRINTERNAME].HeaderText = "打印机名称"; |
| | | grdUnprinted.Columns[COL_PRINTERNAME].Width = 200; |
| | | grdUnprinted.Columns[COL_REMARK].HeaderText = "内外箱"; |
| | | grdUnprinted.Columns[COL_REMARK].Visible = false; |
| | | |
| | | grdUnprinted.ReadOnly = true; |
| | | grdUnprinted.SelectionMode = DataGridViewSelectionMode.FullRowSelect; |
| | | grdUnprinted.MultiSelect = true; |
| | | |
| | | |
| | | grdPrinted.ColumnCount = 8; |
| | | grdPrinted.ColumnCount = 9; |
| | | grdPrinted.Columns[COL_ID].HeaderText = "ID"; |
| | | grdPrinted.Columns[COL_ID].Visible = false; |
| | | grdPrinted.Columns[COL_BARCODE].HeaderText = "条码编号"; |
| | |
| | | grdPrinted.Columns[COL_PRINTDATE].Width = 180; |
| | | grdPrinted.Columns[COL_PRINTERNAME].HeaderText = "打印机名称"; |
| | | grdPrinted.Columns[COL_PRINTERNAME].Width = 200; |
| | | grdPrinted.Columns[COL_REMARK].HeaderText = "内外箱"; |
| | | grdPrinted.Columns[COL_REMARK].Visible = false; |
| | | |
| | | grdPrinted.ReadOnly = true; |
| | | grdPrinted.SelectionMode = DataGridViewSelectionMode.FullRowSelect; |
| | |
| | | a.HQty AS 数量, |
| | | a.HIsPrintSuccess AS 是否打印成功, |
| | | a.HLastPrintDate AS 打印时间, |
| | | a.HRemark AS 内外箱, |
| | | a.HPrintName AS 打印机名称 |
| | | FROM Gy_BarCodeBill_Split a |
| | | INNER JOIN Gy_Material b ON a.HMaterID = b.HItemID |
| | |
| | | |
| | | foreach (DataRow dr in ds.Tables[0].Rows) |
| | | { |
| | | object[] rowData = new object[8]; // 8列 |
| | | object[] rowData = new object[9]; // 8列 |
| | | rowData[COL_ID] = dr["id"]; |
| | | rowData[COL_BARCODE] = dr["条码编号"].ToString(); |
| | | rowData[COL_MATERIALCODE] = dr["物料代码"]?.ToString() ?? ""; |
| | | rowData[COL_MATERIALNAME] = dr["物料名称"]?.ToString() ?? ""; |
| | | rowData[COL_REMARK] = dr["内外箱"]?.ToString() ?? ""; |
| | | |
| | | // 数量(索引4) |
| | | object qtyObj = dr["数量"]; |
| | |
| | | return; |
| | | } |
| | | |
| | | string templateName = GetConfigKey_Print(AppDomain.CurrentDomain.BaseDirectory + "./Config/PWD.config", "PrintTemplate"); |
| | | // ---------- 改动1:先收集选中行的 remark,并检查是否一致 ---------- |
| | | List<string> remarkList = new List<string>(); |
| | | foreach (DataGridViewRow row in currentGrid.SelectedRows) |
| | | { |
| | | object remarkObj = row.Cells[COL_REMARK].Value; |
| | | string remark = remarkObj?.ToString() ?? ""; |
| | | remarkList.Add(remark); |
| | | } |
| | | if (remarkList.Distinct().Count() > 1) |
| | | { |
| | | MessageBox.Show("选中条码包含内箱和外箱,请分别选择同一类型打印。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
| | | return; |
| | | } |
| | | string firstRemark = remarkList.FirstOrDefault(); |
| | | |
| | | // ---------- 改动2:根据 remark 动态获取模板 ---------- |
| | | string templateName = GetTemplateNameByRemark(firstRemark); |
| | | if (string.IsNullOrEmpty(templateName) || templateName == "undefined") |
| | | { |
| | | MessageBox.Show("未配置打印模板(PrintTemplate),请在配置文件中设置。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| | | MessageBox.Show($"未找到类型“{firstRemark}”对应的打印模板,请检查配置。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| | | return; |
| | | } |
| | | |
| | |
| | | timer1?.Stop(); |
| | | timer1?.Dispose(); |
| | | } |
| | | private string GetTemplateNameByRemark(string remark) |
| | | { |
| | | string configPath = AppDomain.CurrentDomain.BaseDirectory + "./Config/PWD.config"; |
| | | // 根据备注内容判断,若包含"外箱"则使用外箱模板,否则默认内箱 |
| | | if (!string.IsNullOrEmpty(remark) && remark.Trim().Contains("外箱码")) |
| | | return GetConfigKey_Print(configPath, "PrintTemplate2"); |
| | | else |
| | | return GetConfigKey_Print(configPath, "PrintTemplate"); |
| | | } |
| | | } |
| | | } |