Packing_CartonBoxSNWeigh.cs 21 KB

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