Packing_CartonBoxSNWeigh.cs 21 KB

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