using LabelManager2; using System; using System.Data; using System.Text; using System.Windows.Forms; using UAS_MES.DataOperate; using UAS_MES.Entity; using UAS_MES.PublicMethod; using System.Drawing; using System.Threading; using UAS_MES.PublicForm; using System.IO.Ports; using System.IO; using System.Text.RegularExpressions; namespace UAS_MES.Make { public partial class Make_PackageCollectionWeigh : Form { DataHelper dh; DataTable dt; LogStringBuilder sql = new LogStringBuilder(); AutoSizeFormClass asc = new AutoSizeFormClass(); ApplicationClass lbl; Thread thread; DataTable Dbfind; string ErrorMessage = ""; string oMakeCode = ""; string oMsID = ""; //称量的标准重量 int Weight; //最大重量 int MaxWeight; //最小重量 int MinWeight; //创建串口实例 SerialPort serialPort1 = new SerialPort(); //true的时候表示从串口读取数据 bool GetData = true; System.DateTime[] indate; public Make_PackageCollectionWeigh() { InitializeComponent(); } //创建打印进程 private void NewPrint() { try { lbl = new ApplicationClass(); BaseUtil.WriteLbl(lbl); } catch (Exception ex) { OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red); } } private void 包装采集_Load(object sender, EventArgs e) { pa_outboxcode.ShowClickIcon = false; ComList.Text = BaseUtil.GetCacheData("PortName").ToString(); Baurate.Text = BaseUtil.GetCacheData("BaudRate").ToString(); //设置DbFind的必须的数据 PrintNum.Value = 1; asc.controllInitializeSize(this); thread = new Thread(NewPrint); SetLoadingWindow stw = new SetLoadingWindow(thread, "初始化打印程序"); BaseUtil.SetFormCenter(stw); stw.ShowDialog(); dh = new DataHelper(); thread = new Thread(getSerialData); try { GetData = true; serialPort1.PortName = ComList.Text; serialPort1.BaudRate = int.Parse(Baurate.Text); serialPort1.Open(); thread.Start(); } catch (Exception mes) { if (Baurate.Text == "" || ComList.Text == "") OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red); else OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red); } } private void getSerialData() { if (serialPort1.IsOpen) { if (!SystemInf.OpenPort.Contains(serialPort1.PortName)) { SystemInf.OpenPort.Add(serialPort1.PortName); try { while (GetData) { try { weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", ""); if (pr_cartonunit.Text == "kg") weight.Text = (float.Parse(weight.Text) / 1000).ToString(); } catch (Exception) { GetData = false; } } } catch (IOException ex) { MessageBox.Show(ex.Message); } } else MessageBox.Show("端口已被占用,请关闭其他窗口"); } } private void 包装采集_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void 包装采集_Activated(object sender, EventArgs e) { pa_outboxcode.Focus(); } //刷新表单的数据的数据 private void LoadData() { //加载表单数据 string Err = ""; if (pa_outboxcode.Text != "") { sql.Clear(); sql.Append("select pa_outboxcode,pr_code,pa_status,pr_packrule,pr_detail,pa_packageqty,pr_outboxinnerqty,pa_standardqty,pa_currentqty,pr_cartonminw, "); sql.Append("pr_cartonmaxw,pr_cartonunit,pr_cartongw from package left join product on pa_prodcode=pr_code where pa_outboxcode='" + pa_outboxcode.Text + "'"); Err = "箱号"; } else { sql.Clear(); sql.Append("select ms_makecode,pr_cartonunit,pa_status,pr_packrule,pr_code,pr_detail,pr_outboxinnerqty,pa_standardqty,pa_packageqty,pa_currentqty,"); sql.Append("pa_outboxcode,pr_cartonminw,pr_cartonmaxw,pr_cartongw from makeserial left join product on ms_prodcode=pr_code left "); sql.Append("join packagedetail on pd_barcode=ms_sncode left join package on pa_id =pd_paid where ms_sncode='" + sn_code.Text + "'"); Err = "序列号"; } dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) { if (pr_code.Text != "") { dt.Columns.Remove(dt.Columns["pa_standardqty"]); } //重量的临时变量 string _weight = dt.Rows[0]["pr_cartongw"].ToString(); string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString(); string _minweight = dt.Rows[0]["pr_cartonminw"].ToString(); //赋值重量单位 Weight = int.Parse(_weight == "" ? "0" : _weight); MaxWeight = int.Parse(_maxweight == "" ? "0" : _maxweight); MinWeight = int.Parse(_minweight == "" ? "0" : _minweight); BaseUtil.SetFormValue(this.Controls, dt); if (Weight - MinWeight == MaxWeight - Weight) pr_cartonboxgw.Text = Weight + "±" + (MaxWeight - Weight); else pr_cartonboxgw.Text = MinWeight + "-" + MaxWeight; } else OperateResult.AppendText(">>" + Err + "不存在\n", Color.Red); } private void LoadGridData() { //加载Grid数据 dt = (DataTable)dh.ExecuteSql("select pa_outboxcode,pd_barcode,pd_innerqty,pa_indate from packageDetail left join package on pa_id=pd_paid where pa_outboxcode='" + pa_outboxcode.Text + "'", "select"); BaseUtil.FillDgvWithDataTable(PackageDetail, dt); //更新界面的采集数量 pa_currentqty.Text = dh.getFieldDataByCondition("package", "pa_currentqty", "pa_outboxcode='" + pa_outboxcode.Text + "'").ToString(); } private void InsertDetail() { if (int.Parse(pa_currentqty.Text == "" ? "0" : pa_currentqty.Text) < int.Parse(pr_outboxinnerqty.Text)) { sql.Clear(); sql.Append("insert into packagedetail(pd_id,pd_paid,pd_outboxcode,pd_innerqty,pd_barcode) "); sql.Append("select packagedetail_seq.nextval,pa_id,pa_outboxcode,pa_totalqty,'" + sn_code.Text + "' from package where pa_outboxcode='" + pa_outboxcode.Text + "'"); dh.ExecuteSql(sql.GetString(), "insert"); dh.UpdateByCondition("Package", "pa_packageqty=pa_packageqty+1,pa_currentqty=pa_currentqty+1", "pa_outboxcode='" + pa_outboxcode.Text + "'"); dh.UpdateByCondition("makeserial", "ms_outboxcode='" + pa_outboxcode.Text + "'", "ms_id='" + oMsID + "'"); LoadCheckQTY(); if (LogicHandler.SetStepResult(ms_makecode.Text, User.UserSourceCode, sn_code.Text, "包装采集", "装箱成功", User.UserCode, out ErrorMessage)) { OperateResult.AppendText(">>序列号" + sn_code.Text + "采集成功!\n", Color.Green); //满箱更新状态为1 if (int.Parse(pa_currentqty.Text == "" ? "0" : pa_currentqty.Text) + 1 == int.Parse(pr_outboxinnerqty.Text)) { //检查重量合格 if (float.Parse(weight.Text) > MinWeight && float.Parse(weight.Text) < MaxWeight) { dh.UpdateByCondition("package", "pa_status=1", "pa_outboxcode='" + pa_outboxcode.Text + "'"); if (AutoPrint.Checked) Print_Click(new object(), new EventArgs()); } else OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "重量不符合标准重量\n", Color.Red); } LoadGridData(); sn_code.Clear(); } } else OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "已采集满\n", Color.Red); } private void Print_Click(object sender, EventArgs e) { if (PrintLabel.Items.Count != 0) { if (dh.getFieldDataByCondition("package", "pa_status", "pa_outboxcode='" + pa_outboxcode.Text + "'").ToString() == "1") { dh.ExecuteSql("update package set pa_printcount=pa_printcount+1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update"); OperateResult.AppendText(">>开始打印箱号" + pa_outboxcode.Text + "\n", Color.Green); Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), PrintList.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]); } else OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "尚未封箱,请封箱后执行打印\n", Color.Red); } else OperateResult.AppendText(">>产品" + pr_code.Text + "未维护打印标签\n", Color.Red); } //加载工单信息和装箱明细信息 private void pa_code_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { LoadData(); LoadGridData(); } } //输入序列号的回车事件 private void barcode_KeyDown(object sender, KeyEventArgs e) { //当用户输入回车键的时候 if (e.KeyCode == Keys.Enter) { if (LogicHandler.CheckStepSNAndMacode("", User.UserSourceCode, sn_code.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage)) { if (pa_outboxcode.Text == "") { if (dh.getFieldDataByCondition("makeserial", "ms_outboxcode", "ms_id='" + oMsID + "'").ToString() != "") { LoadData(); LoadGridData(); } else { LoadData(); pa_outboxcode.GeneratePaCode_Click(new object(), new EventArgs()); } } //按工单核对装箱 switch (pr_packrule.Text) { case "M": if (dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_id='" + oMsID + "'").ToString() != ms_makecode.Text) { OperateResult.AppendText(">>序列号" + sn_code.Text + "所属工单和当前箱号工单不相等\n", Color.Red, sn_code); return; } break; case "S": if (dh.CheckExist("package", "pa_outboxcode='" + pa_outboxcode.Text + "'")) { sql.Clear(); sql.Append("select 1 from package left join make on pa_salecode=ma_salecode left join makeserial "); sql.Append("on ma_code=ms_makecode where pa_outboxcode='" + pa_outboxcode.Text + "' and ms_id='" + oMsID + "'"); DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count == 0) { OperateResult.AppendText(">>序列号" + sn_code.Text + "所属订单号和当前箱号订单不相等\n", Color.Red, sn_code); return; } } break; case "": if (dh.getFieldDataByCondition("makeserial", "ms_prodcode", "ms_id='" + oMsID + "'").ToString() != pr_code.Text) { OperateResult.AppendText(">>序列号" + sn_code.Text + "对应物料和该箱所装物料不同\n", Color.Red, sn_code); return; } break; default: break; } //判断箱内总数必须大于0 if (pr_outboxinnerqty.Text == "" || pr_outboxinnerqty.Text == "0") { OperateResult.AppendText(">>箱内容量必须大于0\n", Color.Red); return; } //如果未勾选了取消录入 if (!Cancel.Checked) { //判断序列号是否已经装箱 if (dh.getFieldDataByCondition("makeserial", "ms_outboxcode", "ms_id='" + oMsID + "'").ToString() != "") { OperateResult.AppendText(">>序列号" + sn_code.Text + "已经装箱!\n", Color.Red, sn_code); } else { //满箱之后采集下一个之前自动清除内容,生成新的箱号 if ((AutoGenBoxCode.Checked && pa_currentqty.Text != "" && pr_outboxinnerqty.Text == pa_currentqty.Text) || pa_status.Text == "1") { pa_currentqty.Text = ""; pa_outboxcode.Text = ""; pa_status.Text = "0"; BaseUtil.CleanDGVData(PackageDetail); pa_outboxcode.GeneratePaCode_Click(new object(), new EventArgs()); } //箱号不存在的情况 if (!dh.CheckExist("package", "pa_outboxcode='" + pa_outboxcode.Text + "'")) { //插入Package主表箱号信息 sql.Clear(); sql.Append("insert into package (pa_id,pa_outboxcode,pa_packageqty,pa_totalqty,pa_status,pa_indate,pa_makecode,pa_prodcode,pa_salecode,"); sql.Append("pa_custcode,pa_type,pa_packtype) select package_seq.nextval,'" + pa_outboxcode.Text + "',0," + pr_outboxinnerqty.Text + ",0,"); sql.Append("sysdate,'" + ms_makecode.Text + "', ma_prodcode,ma_salecode,ma_custcode,1,'" + pr_packrule.Text + "' from make where ma_code = '" + ms_makecode.Text + "'"); dh.ExecuteSql(sql.GetString(), "insert"); OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "采集成功!\n", Color.Green); InsertDetail(); } else { if (dh.getRowCount("makeserial", "ms_sncode='" + sn_code.Text + "'") > 0) InsertDetail(); else OperateResult.AppendText(">>序列号" + sn_code.Text + "不存在\n", Color.Red, sn_code); } } } //勾选了取消箱号先验证箱号存不存在,存在的话移除掉,重新加载一次数据 else { if (dh.getFieldDataByCondition("package", "pa_status", "pa_outboxcode='" + pa_outboxcode.Text + "'").ToString() != "1") { string outboxcode = dh.getFieldDataByCondition("makeserial", "ms_outboxcode", "ms_id='" + oMsID + "'").ToString(); if (outboxcode != "") { if (outboxcode == pa_outboxcode.Text) { //删除明细行的数据 dh.ExecuteSql("delete from packagedetail where pd_barcode='" + sn_code.Text + "' and pd_paid=(select pa_id from package where pa_outboxcode='" + pa_outboxcode.Text + "')", "delete"); //更新已装数 dh.UpdateByCondition("Package", "pa_packageqty=pa_packageqty-1,pa_currentqty=pa_currentqty-1", "pa_outboxcode='" + pa_outboxcode.Text + "'"); //清除序列号箱号 dh.UpdateByCondition("makeserial", "ms_outboxcode=''", "ms_id='" + oMsID + "'"); OperateResult.AppendText(">>已从该箱中移除序列号" + sn_code.Text + "\n", Color.Green, sn_code); LoadGridData(); } else OperateResult.AppendText(">>序列号" + sn_code.Text + "不在箱" + pa_outboxcode.Text + "内\n", Color.Red); } else OperateResult.AppendText(">>序列号" + sn_code.Text + "尚未装箱\n", Color.Red); } else OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "已经封箱,不允许取消采集\n", Color.Red); } } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); } } private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } private void pr_code_TextChanged(object sender, EventArgs e) { pa_outboxcode.MakeCode = ms_makecode.Text; pa_outboxcode.ProdCode = pr_code.Text; pa_outboxcode.Caller = "PACKAGE"; dt = (DataTable)dh.ExecuteSql("select pl_labelcode||':'||pl_labelname pl_laname,pl_labelname,pl_labelcode,pl_labelurl,pl_indate from productlabel where pl_prodcode='" + pr_code.Text + "' order by pl_isdefault desc ", "select"); PrintLabel.DataSource = dt; PrintLabel.DisplayMember = "pl_laname"; PrintLabel.ValueMember = "pl_labelcode"; ftpOperater ftp = new ftpOperater(); indate = new System.DateTime[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { BaseUtil.GetPrintLabel(dt.Rows[i]["pl_labelname"].ToString(), dt.Rows[i]["pl_labelurl"].ToString(), dt.Rows[i]["pl_indate"].ToString()); indate[i] = Convert.ToDateTime(dt.Rows[i]["pl_indate"].ToString()); } } private void 包装采集_FormClosing(object sender, FormClosingEventArgs e) { BaseUtil.ClosePrint(lbl); if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Interrupt(); } dh.Dispose(); } private void Packing_Click(object sender, EventArgs e) { if (dh.CheckExist("package", "pa_outboxcode='" + pa_outboxcode.Text + "' and pa_status<>1")) { dh.UpdateByCondition("package", "pa_status=1,pa_totalqty=pa_currentqty,pa_packageqty=pa_currentqty", "pa_outboxcode='" + pa_outboxcode.Text + "'"); pa_status.Text = "1"; OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "封箱成功\n", Color.Green); } else OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "错误或者已封箱\n", Color.Red); } private void SendCheck_Click(object sender, EventArgs e) { sql.Clear(); sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'"); dh.ExecuteSql(sql.GetString(), "select"); ob_nowcheckqty.Text = ""; ob_batchqty.Text = ""; ob_nowcheckqty.ForeColor = Color.Black; SendCheck.Enabled = false; OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green); LogicHandler.InsertMakeProcess(sn_code.Text, oMakeCode, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode); //记录操作日志 LogicHandler.DoCommandLog(User.UserCode, oMakeCode, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text); ob_checkno.Text = ""; } bool AutoCut; private void LoadCheckQTY() { sql.Clear(); string condition = ""; int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text); int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text); if (nowcheckqty + 1 == batchqty && AutoCut) { condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'"; } else { condition = "and ob_status='ENTERING' "; } sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where "); sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and "); sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition); DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) { ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString(); ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString(); ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString(); nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text); batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text); if (nowcheckqty == batchqty) { ob_nowcheckqty.ForeColor = Color.Red; OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red); } } else { ob_batchqty.Text = ""; ob_nowcheckqty.Text = ""; ob_checkno.Text = ""; SendCheck.Enabled = false; } if (ob_batchqty.Text != "") { SendCheck.Enabled = true; } else { SendCheck.Enabled = false; } } private void ob_checkno_TextChanged(object sender, EventArgs e) { if (ob_checkno.Text != "") { string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString(); if (Cut == "" || Cut == "0") AutoCut = false; else AutoCut = true; SendCheck.Enabled = true; } } } }