Packing_CartonBoxWeigh.cs 20 KB

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