Packing_CartonBoxWeigh.cs 21 KB

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