Packing_CartonBoxSNWeigh.cs 21 KB

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