Make_CartonBoxWeigh.cs 19 KB

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