Packing_CartonBoxSNWeigh.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using LabelManager2;
  2. using Microsoft.Win32;
  3. using Seagull.BarTender.Print;
  4. using System;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.IO.Ports;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. using UAS_MES_NEW.PublicForm;
  16. using UAS_MES_NEW.PublicMethod;
  17. namespace UAS_MES_NEW.Packing
  18. {
  19. public partial class Packing_CartonBoxSNWeigh : Form
  20. {
  21. AutoSizeFormClass asc = new AutoSizeFormClass();
  22. DataHelper dh;
  23. DataTable dt;
  24. LogStringBuilder sql = new LogStringBuilder();
  25. ApplicationClass lbl;
  26. string outboxcode = "";
  27. Document doc;
  28. Thread thread;
  29. bool LockSn = false;
  30. Regex re = new Regex("\\d+.\\d+\\w+");
  31. string PR_CHECKCARTONW = "0";
  32. //创建串口实例
  33. SerialPort serialPort1 = new SerialPort();
  34. //true的时候表示从串口读取数据
  35. bool GetData = true;
  36. //箱内总数
  37. int pa_totalqty;
  38. //称量的标准重量
  39. Double Weight;
  40. //最大重量
  41. Double MaxWeight;
  42. //最小重量
  43. Double MinWeight;
  44. DataTable ListC = new DataTable();
  45. Thread InitPrint;
  46. public Packing_CartonBoxSNWeigh()
  47. {
  48. InitializeComponent();
  49. try { UAS_MES_NEW.I18n.LanguageManager.Localize(this); } catch { }
  50. }
  51. protected override void OnVisibleChanged(EventArgs e)
  52. {
  53. base.OnVisibleChanged(e);
  54. if (!IsHandleCreated)
  55. {
  56. this.Close();
  57. }
  58. }
  59. private void 卡通箱称重_Load(object sender, EventArgs e)
  60. {
  61. CheckForIllegalCrossThreadCalls = false;
  62. asc.controllInitializeSize(this);
  63. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  64. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  65. InitPrint = new Thread(InPrint);
  66. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  67. BaseUtil.SetFormCenter(stw);
  68. stw.ShowDialog();
  69. StartWeight.PerformClick();
  70. dh = SystemInf.dh;
  71. ControlLockTimer.Tick += ControlLockTimer_Tick;
  72. ControlLockTimer.Interval = 100;
  73. StepCount.StepCode = User.CurrentStepCode;
  74. StepCount.Source = User.UserSourceCode;
  75. StepCount.LineCode = User.UserLineCode;
  76. StepCount.Dh = dh;
  77. StepCount.Start();
  78. }
  79. private void ControlLockTimer_Tick(object sender, EventArgs e)
  80. {
  81. this.Activate();
  82. sncode.Focus();
  83. }
  84. Engine engine;
  85. private void InPrint()
  86. {
  87. try
  88. {
  89. engine = new Engine(true);
  90. BaseUtil.WriteLbl();
  91. }
  92. catch (Exception)
  93. {
  94. OperateResult.AppendText("未正确安装BarTender软件\n", Color.Red);
  95. }
  96. }
  97. private void 卡通箱称重_SizeChanged(object sender, EventArgs e)
  98. {
  99. weight.Location = new Point(weight_label.Location.X + weight_label.Size.Width - 10, weight_label.Location.Y - 20);
  100. asc.controlAutoSize(this);
  101. }
  102. private void sncode_KeyDown(object sender, KeyEventArgs e)
  103. {
  104. if (e.KeyCode == Keys.Enter)
  105. {
  106. sql.Clear();
  107. sql.Append("select max(ms_id) from makeserial where ms_sncode in (select '" + sncode.Text + "' from dual ");
  108. sql.Append("union select sn from makesnrelation where beforesn='" + sncode.Text + "' and sn<>' ' union select ");
  109. sql.Append("beforesn from makesnrelation where sn='" + sncode.Text + "' and beforesn<>' ') and ms_status in(1,2,0)");
  110. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  111. string MSID = dt.Rows[0][0].ToString();
  112. if (MSID == "")
  113. {
  114. OperateResult.AppendText(">>序列号" + sncode.Text + "不存在\n", Color.Red, sncode);
  115. return;
  116. }
  117. outboxcode = dh.getFieldDataByCondition("makeserial", "ms_outboxcode", "ms_id='" + MSID + "'").ToString();
  118. if (outboxcode == "")
  119. {
  120. OperateResult.AppendText(">>序列号" + sncode.Text + "尚未装箱\n", Color.Red, sncode);
  121. return;
  122. }
  123. sql.Clear();
  124. sql.Append("select pa_totalqty,ma_code,ma_qty,ma_salecode,PA_STANDARDQTY,PA_CURRENTQTY,pr_detail,pr_cartonunit,pr_code,pr_cartongw,");
  125. sql.Append("pr_cartonmaxw,pr_cartonminw,nvl(PR_CHECKCARTONW,'0') PR_CHECKCARTONW,nvl(pr_sendchecktype,'LineCode')pr_sendchecktype from package left join packagedetail ");
  126. sql.Append("on pa_id=pd_paid left join makeserial on ms_sncode=pd_barcode and ms_makecode=pa_makecode left join product on pr_code=pd_prodcode left join make on ma_code=pd_makecode ");
  127. sql.Append(" where pa_outboxcode='" + outboxcode + "' and pa_nextstep='" + User.CurrentStepCode + "'");
  128. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  129. //填充打印文件选项的DataGridView
  130. if (dt.Rows.Count > 0)
  131. {
  132. BaseUtil.SetFormValue(this.Controls, dt);
  133. string ErrorMessage;
  134. //重量的临时变量
  135. PR_CHECKCARTONW = dt.Rows[0]["PR_CHECKCARTONW"].ToString();
  136. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  137. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  138. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  139. string pa_pr_cartonunit = dt.Rows[0]["pr_cartonunit"].ToString();
  140. string PA_STANDARDQTY = dt.Rows[0]["PA_STANDARDQTY"].ToString();
  141. string PA_CURRENTQTY = dt.Rows[0]["PA_CURRENTQTY"].ToString();
  142. Weight = double.Parse(_weight == "" ? "0" : _weight);
  143. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  144. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  145. pa_totalqty = int.Parse(dt.Rows[0]["pa_totalqty"].ToString());
  146. if (Weight - MinWeight == MaxWeight - Weight)
  147. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_cartonunit"].ToString();
  148. else
  149. pr_cartongw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_cartonunit"].ToString();
  150. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  151. //需要检查称重重量
  152. if (PR_CHECKCARTONW != "0" && PA_STANDARDQTY == PA_CURRENTQTY)
  153. {
  154. //称量合格或不合格都记录重量
  155. if (ActualWeight >= MinWeight && ActualWeight <= MaxWeight && ActualWeight != 0)
  156. {
  157. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量合格:" + weight.Text.Trim(), outboxcode, "");
  158. LogicHandler.RecordProdWeight(outboxcode, "CARTON", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  159. OperateResult.AppendText(">>箱号" + outboxcode + "检测合格\n", Color.Green);
  160. }
  161. else
  162. {
  163. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量不合格:" + weight.Text.Trim(), outboxcode, "");
  164. LogicHandler.RecordProdWeight(outboxcode, "CARTON", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  165. OperateResult.AppendText(">>箱号" + outboxcode + "检测未通过\n", Color.Red, sncode);
  166. return;
  167. }
  168. }
  169. else
  170. {
  171. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量完成:" + weight.Text.Trim(), outboxcode, "");
  172. LogicHandler.RecordProdWeight(outboxcode, "CARTON", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  173. }
  174. if (AutoPrint.Checked)
  175. {
  176. if (PrintLabel.Items.Count > 0)
  177. {
  178. Confirm.PerformClick();
  179. }
  180. else
  181. {
  182. OperateResult.AppendText(">>产品" + pr_code.Text + "未维护卡通标签\n", Color.Red);
  183. }
  184. }
  185. OperateResult.AppendText(">>箱号" + outboxcode + "称重完成\n", Color.Green);
  186. //if (LogicHandler.CartonBoxStepPass(ma_code.Text, User.UserSourceCode, outboxcode, User.UserCode, "卡通箱:" + outboxcode + "整箱过站", out ErrorMessage))
  187. //{
  188. // dh.UpdateByCondition("package", "pa_printcount= nvl(pa_printcount,0)+1,pa_weight='" + ActualWeight + "',pa_status=1,pa_packageqty=pa_currentqty,pa_totalqty=pa_currentqty", "pa_outboxcode='" + outboxcode + "'");
  189. // dh.UpdateByCondition("package", "pa_nextstep='" + dh.getFieldDataByCondition("packagedetail left join makeserial on ms_sncode=pd_barcode and ms_makecode=pd_makecode", "max(ms_nextstepcode)", "pd_outboxcode='" + outboxcode + "'").ToString() + "' ,PA_CURRENTSTEP = '' ", "pa_outboxcode='" + outboxcode + "'");
  190. //}
  191. //else
  192. //{
  193. // OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  194. //}
  195. if (!LogicHandler.OutBoxStepPass(outboxcode, ma_code.Text, User.UserSourceCode, User.UserCode, "卡通箱:" + outboxcode + "整箱过站", "卡通箱整箱过站", out ErrorMessage))
  196. {
  197. OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  198. return;
  199. }
  200. dh.UpdateByCondition("package", "pa_printcount= nvl(pa_printcount,0)+1,pa_weight='" + ActualWeight + "',pa_status=1,pa_packageqty=pa_currentqty,pa_totalqty=pa_currentqty", "pa_outboxcode='" + outboxcode + "'");
  201. dh.UpdateByCondition("package", "pa_nextstep='" + dh.getFieldDataByCondition("packagedetail left join makeserial on ms_sncode=pd_barcode and ms_makecode=pd_makecode", "max(ms_nextstepcode)", "pd_outboxcode='" + outboxcode + "'").ToString() + "' ,PA_CURRENTSTEP = '' ", "pa_outboxcode='" + outboxcode + "'");
  202. LoadCheckQTY();
  203. sncode.Clear();
  204. }
  205. else OperateResult.AppendText(">>箱号" + outboxcode + "当前执行工序不是" + User.CurrentStepCode + "\n", Color.Red, sncode);
  206. }
  207. }
  208. private void getSerialData()
  209. {
  210. if (serialPort1.IsOpen)
  211. {
  212. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  213. {
  214. SystemInf.OpenPort.Add(serialPort1.PortName);
  215. try
  216. {
  217. while (GetData)
  218. {
  219. try
  220. {
  221. weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value;
  222. }
  223. catch (Exception)
  224. {
  225. GetData = false;
  226. }
  227. }
  228. }
  229. catch (IOException ex) { OperateResult.AppendText(">>" + ex.Message + "\n", Color.Red, sncode); }
  230. }
  231. else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red, sncode);
  232. }
  233. }
  234. //确认打印
  235. private void Confirm_Click(object sender, EventArgs e)
  236. {
  237. //doc = lbl.Documents.Open(PrintLabel.Text);
  238. string ErrorMessage = "";
  239. if (!Print.BarTender(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode, int.Parse(PrintNum.Text), ma_code.Text, pr_code.Text, "卡通箱标", "0", out ErrorMessage))
  240. {
  241. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  242. }
  243. }
  244. //停止进程,关闭串口
  245. private void StopWeight_Click(object sender, EventArgs e)
  246. {
  247. if (serialPort1.IsOpen)
  248. {
  249. GetData = false;
  250. serialPort1.Close();
  251. SystemInf.OpenPort.Remove(serialPort1.PortName);
  252. thread.Abort();
  253. }
  254. }
  255. //关闭窗口的时候停止进程,不再读取串口数据
  256. private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e)
  257. {
  258. BaseUtil.ClosePrint(lbl);
  259. StopWeight.PerformClick();
  260. InitPrint.Abort();
  261. if (serialPort1.IsOpen)
  262. {
  263. GetData = false;
  264. serialPort1.Close();
  265. SystemInf.OpenPort.Remove(serialPort1.PortName);
  266. thread.Interrupt();
  267. }
  268. //thread.Abort();
  269. }
  270. private void StartWeight_Click(object sender, EventArgs e)
  271. {
  272. thread = new Thread(getSerialData);
  273. try
  274. {
  275. GetData = true;
  276. serialPort1.PortName = this.ComList.Text;
  277. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  278. serialPort1.Open();
  279. thread.Start();
  280. }
  281. catch (Exception mes)
  282. {
  283. if (BaudRate.Text == "" || ComList.Text == "")
  284. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  285. else
  286. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  287. }
  288. }
  289. private void pr_code_TextChanged(object sender, EventArgs e)
  290. {
  291. dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='卡通箱标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  292. PrintLabel.DataSource = dt;
  293. PrintLabel.DisplayMember = "la_url";
  294. PrintLabel.ValueMember = "la_id";
  295. }
  296. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  297. {
  298. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  299. {
  300. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  301. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  302. }
  303. }
  304. private void SendCheck_Click(object sender, EventArgs e)
  305. {
  306. DataTable dt = (DataTable)dh.ExecuteSql("select wm_concat(pa_outboxcode) pa_outboxcode from package where pa_checkno='" + ob_checkno.Text + "' and pa_status=0 ", "select");
  307. if (dt.Rows[0][0].ToString() != "")
  308. {
  309. OperateResult.AppendText(">>批次" + ob_checkno.Text + "存在箱号" + dt.Rows[0][0].ToString() + "未封箱,请先进行封箱\n", Color.Red);
  310. return;
  311. }
  312. sql.Clear();
  313. sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'");
  314. dh.ExecuteSql(sql.GetString(), "select");
  315. ob_nowcheckqty.Text = "";
  316. ob_batchqty.Text = "";
  317. ob_nowcheckqty.ForeColor = Color.Black;
  318. SendCheck.Enabled = false;
  319. OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green);
  320. LogicHandler.InsertMakeProcess("", ma_code.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
  321. //记录操作日志
  322. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text);
  323. ob_checkno.Text = "";
  324. }
  325. bool AutoCut;
  326. private void LoadCheckQTY()
  327. {
  328. sql.Clear();
  329. string condition = "";
  330. int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  331. int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  332. if (nowcheckqty + 1 == batchqty && AutoCut)
  333. {
  334. condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'";
  335. }
  336. else
  337. {
  338. condition = "and ob_status='ENTERING' ";
  339. }
  340. if (pr_sendchecktype.Text == "SaleCode")
  341. {
  342. condition += " and ob_salecode='" + ma_salecode.Text + "'";
  343. }
  344. sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where ");
  345. sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and ");
  346. sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition);
  347. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  348. if (dt.Rows.Count > 0)
  349. {
  350. ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString();
  351. ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString();
  352. ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString();
  353. nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  354. batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  355. if (nowcheckqty >= batchqty)
  356. {
  357. ob_nowcheckqty.ForeColor = Color.Red;
  358. OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red);
  359. if (AutoCut)
  360. {
  361. OperateResult.AppendText(">>批次" + ob_checkno.Text + "自动断批\n", Color.Green);
  362. }
  363. }
  364. }
  365. else
  366. {
  367. ob_batchqty.Text = "";
  368. ob_nowcheckqty.Text = "";
  369. ob_checkno.Text = "";
  370. SendCheck.Enabled = false;
  371. }
  372. if (ob_batchqty.Text != "")
  373. {
  374. SendCheck.Enabled = true;
  375. }
  376. else
  377. {
  378. SendCheck.Enabled = false;
  379. }
  380. }
  381. private void ob_checkno_TextChanged(object sender, EventArgs e)
  382. {
  383. if (ob_checkno.Text != "")
  384. {
  385. string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString();
  386. if (Cut == "" || Cut == "0")
  387. AutoCut = false;
  388. else
  389. AutoCut = true;
  390. SendCheck.Enabled = true;
  391. }
  392. }
  393. private void RefreshWeigh_Click(object sender, EventArgs e)
  394. {
  395. sql.Clear();
  396. sql.Append("select pr_cartonmaxw,pr_cartonunit,pr_cartonminw,pr_cartongw from product where pr_code='" + pr_code.Text + "'");
  397. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  398. //填充打印文件选项的DataGridView
  399. if (dt.Rows.Count > 0)
  400. {
  401. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  402. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  403. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  404. Weight = double.Parse(_weight == "" ? "0" : _weight);
  405. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  406. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  407. if (Weight - MinWeight == MaxWeight - Weight)
  408. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_cartonunit"].ToString();
  409. else
  410. pr_cartongw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_cartonunit"].ToString();
  411. }
  412. }
  413. private void Make_CartonBoxSNWeigh_KeyDown(object sender, KeyEventArgs e)
  414. {
  415. if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Q)
  416. {
  417. if (!LockSn)
  418. {
  419. ControlLockTimer.Start();
  420. LockSn = true;
  421. Lock_label.Visible = true;
  422. }
  423. else
  424. {
  425. ControlLockTimer.Stop();
  426. LockSn = false;
  427. Lock_label.Visible = false;
  428. }
  429. }
  430. }
  431. }
  432. }