Packing_CartonBoxPass.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_CartonBoxPass : 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. string PR_CHECKCARTONW = "0";
  28. //创建串口实例
  29. //true的时候表示从串口读取数据
  30. bool GetData = true;
  31. //箱内总数
  32. int pa_totalqty;
  33. //称量的标准重量
  34. Double Weight;
  35. //最大重量
  36. Double MaxWeight;
  37. //最小重量
  38. Double MinWeight;
  39. DataTable ListC = new DataTable();
  40. Thread InitPrint;
  41. public Packing_CartonBoxPass()
  42. {
  43. InitializeComponent();
  44. }
  45. protected override void OnVisibleChanged(EventArgs e)
  46. {
  47. base.OnVisibleChanged(e);
  48. if (!IsHandleCreated)
  49. {
  50. this.Close();
  51. }
  52. }
  53. private void 卡通箱称重_Load(object sender, EventArgs e)
  54. {
  55. CheckForIllegalCrossThreadCalls = false;
  56. asc.controllInitializeSize(this);
  57. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  58. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  59. InitPrint = new Thread(InPrint);
  60. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  61. BaseUtil.SetFormCenter(stw);
  62. stw.ShowDialog();
  63. dh = SystemInf.dh;
  64. StepCount.StepCode = User.CurrentStepCode;
  65. StepCount.Source = User.UserSourceCode;
  66. StepCount.LineCode = User.UserLineCode;
  67. StepCount.Dh = dh;
  68. StepCount.Start();
  69. }
  70. private void InPrint()
  71. {
  72. try
  73. {
  74. engine = new Engine(true);
  75. }
  76. catch (Exception ex)
  77. {
  78. OperateResult.AppendText("未正确安装BarTender软件\n" + ex.Message, Color.Red);
  79. }
  80. }
  81. private void 卡通箱称重_SizeChanged(object sender, EventArgs e)
  82. {
  83. weight.Location = new Point(weight_label.Location.X + weight_label.Size.Width - 10, weight_label.Location.Y - 20);
  84. asc.controlAutoSize(this);
  85. }
  86. private void sncode_KeyDown(object sender, KeyEventArgs e)
  87. {
  88. if (e.KeyCode == Keys.Enter)
  89. {
  90. //根据箱号查询表单数据
  91. sql.Clear();
  92. sql.Append("select pa_totalqty,ma_code,ma_qty,ma_salecode,PA_STANDARDQTY,PA_CURRENTQTY,pr_detail,pr_cartonunit,pr_code,pr_cartongw,");
  93. sql.Append("pr_cartonmaxw,pr_cartonminw,nvl(PR_CHECKCARTONW,'0') PR_CHECKCARTONW,nvl(pr_sendchecktype,'LineCode')pr_sendchecktype from package left join packagedetail ");
  94. 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 ");
  95. sql.Append(" where pa_outboxcode='" + outboxcode.Text + "' and pa_nextstep='" + User.CurrentStepCode + "'");
  96. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  97. //填充打印文件选项的DataGridView
  98. if (dt.Rows.Count > 0)
  99. {
  100. BaseUtil.SetFormValue(this.Controls, dt);
  101. string ErrorMessage;
  102. //重量的临时变量
  103. PR_CHECKCARTONW = dt.Rows[0]["PR_CHECKCARTONW"].ToString();
  104. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  105. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  106. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  107. string pa_pr_cartonunit = dt.Rows[0]["pr_cartonunit"].ToString();
  108. string PA_STANDARDQTY = dt.Rows[0]["PA_STANDARDQTY"].ToString();
  109. string PA_CURRENTQTY = dt.Rows[0]["PA_CURRENTQTY"].ToString();
  110. Weight = double.Parse(_weight == "" ? "0" : _weight);
  111. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  112. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  113. pa_totalqty = int.Parse(dt.Rows[0]["pa_totalqty"].ToString());
  114. if (Weight - MinWeight == MaxWeight - Weight)
  115. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_cartonunit"].ToString();
  116. else
  117. pr_cartongw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_cartonunit"].ToString();
  118. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  119. //需要检查称重重量
  120. 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 + "'");
  121. if (AutoPrint.Checked)
  122. {
  123. if (PrintLabel.Items.Count > 0)
  124. {
  125. Confirm.PerformClick();
  126. }
  127. else
  128. {
  129. OperateResult.AppendText(">>产品" + pr_code.Text + "未维护卡通标签\n", Color.Red);
  130. }
  131. }
  132. if (!LogicHandler.OutBoxStepPass(outboxcode.Text, ma_code.Text, User.UserSourceCode, User.UserCode, "卡通箱:" + outboxcode.Text + "整箱过站", "卡通箱整箱过站", out ErrorMessage))
  133. {
  134. OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, outboxcode);
  135. return;
  136. }
  137. 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 + "'");
  138. LoadCheckQTY();
  139. outboxcode.Clear();
  140. }
  141. else OperateResult.AppendText(">>箱号" + outboxcode.Text + "当前执行工序不是" + User.CurrentStepCode + "\n", Color.Red, outboxcode);
  142. }
  143. }
  144. Engine engine;
  145. //确认打印
  146. private void Confirm_Click(object sender, EventArgs e)
  147. {
  148. string ErrorMessage = "";
  149. if (!Print.BarTender(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))
  150. {
  151. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  152. }
  153. }
  154. //关闭窗口的时候停止进程,不再读取串口数据
  155. private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e)
  156. {
  157. BaseUtil.ClosePrint(lbl);
  158. InitPrint.Abort();
  159. //thread.Abort();
  160. }
  161. private void pr_code_TextChanged(object sender, EventArgs e)
  162. {
  163. 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");
  164. PrintLabel.DataSource = dt;
  165. PrintLabel.DisplayMember = "la_url";
  166. PrintLabel.ValueMember = "la_id";
  167. }
  168. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  169. {
  170. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  171. {
  172. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  173. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  174. }
  175. }
  176. private void SendCheck_Click(object sender, EventArgs e)
  177. {
  178. 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");
  179. if (dt.Rows[0][0].ToString() != "")
  180. {
  181. OperateResult.AppendText(">>批次" + ob_checkno.Text + "存在箱号" + dt.Rows[0][0].ToString() + "未封箱,请先进行封箱\n", Color.Red);
  182. return;
  183. }
  184. sql.Clear();
  185. sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'");
  186. dh.ExecuteSql(sql.GetString(), "select");
  187. ob_nowcheckqty.Text = "";
  188. ob_batchqty.Text = "";
  189. ob_nowcheckqty.ForeColor = Color.Black;
  190. SendCheck.Enabled = false;
  191. OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green);
  192. LogicHandler.InsertMakeProcess("", ma_code.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
  193. //记录操作日志
  194. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text);
  195. ob_checkno.Text = "";
  196. }
  197. bool AutoCut;
  198. private void LoadCheckQTY()
  199. {
  200. sql.Clear();
  201. string condition = "";
  202. int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  203. int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  204. if (nowcheckqty + 1 == batchqty && AutoCut)
  205. {
  206. condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'";
  207. }
  208. else
  209. {
  210. condition = "and ob_status='ENTERING' ";
  211. }
  212. if (pr_sendchecktype.Text == "SaleCode")
  213. {
  214. condition += " and ob_salecode='" + ma_salecode.Text + "'";
  215. }
  216. sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where ");
  217. sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and ");
  218. sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition);
  219. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  220. if (dt.Rows.Count > 0)
  221. {
  222. ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString();
  223. ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString();
  224. ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString();
  225. nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  226. batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  227. if (nowcheckqty >= batchqty)
  228. {
  229. ob_nowcheckqty.ForeColor = Color.Red;
  230. OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red);
  231. if (AutoCut)
  232. {
  233. OperateResult.AppendText(">>批次" + ob_checkno.Text + "自动断批\n", Color.Green);
  234. }
  235. }
  236. }
  237. else
  238. {
  239. ob_batchqty.Text = "";
  240. ob_nowcheckqty.Text = "";
  241. ob_checkno.Text = "";
  242. SendCheck.Enabled = false;
  243. }
  244. if (ob_batchqty.Text != "")
  245. {
  246. SendCheck.Enabled = true;
  247. }
  248. else
  249. {
  250. SendCheck.Enabled = false;
  251. }
  252. }
  253. private void ob_checkno_TextChanged(object sender, EventArgs e)
  254. {
  255. if (ob_checkno.Text != "")
  256. {
  257. string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString();
  258. if (Cut == "" || Cut == "0")
  259. AutoCut = false;
  260. else
  261. AutoCut = true;
  262. SendCheck.Enabled = true;
  263. }
  264. }
  265. private void RefreshWeigh_Click(object sender, EventArgs e)
  266. {
  267. sql.Clear();
  268. sql.Append("select pr_cartonmaxw,pr_cartonunit,pr_cartonminw,pr_cartongw from product where pr_code='" + pr_code.Text + "'");
  269. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  270. //填充打印文件选项的DataGridView
  271. if (dt.Rows.Count > 0)
  272. {
  273. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  274. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  275. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  276. Weight = double.Parse(_weight == "" ? "0" : _weight);
  277. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  278. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  279. if (Weight - MinWeight == MaxWeight - Weight)
  280. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_cartonunit"].ToString();
  281. else
  282. pr_cartongw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_cartonunit"].ToString();
  283. }
  284. }
  285. }
  286. }