UAS_出货标签管理.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Text.RegularExpressions;
  6. using System.Text;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Diagnostics;
  10. using UAS_LabelMachine.PublicMethod;
  11. using UAS_LabelMachine.Entity;
  12. using System.Reflection;
  13. namespace UAS_LabelMachine
  14. {
  15. public partial class UAS_出货标签打印 : Form
  16. {
  17. //自适应屏幕
  18. AutoSizeFormClass asc = new AutoSizeFormClass();
  19. DataHelper dh;
  20. DataTable dt;
  21. /// <summary>
  22. /// 已经打印过的Vivo外箱号
  23. /// </summary>
  24. StringBuilder sql = new StringBuilder();
  25. /// <summary>
  26. /// 当前品牌
  27. /// </summary>
  28. string Brand;
  29. string PI_ID;
  30. /// <summary>
  31. /// 当前扫描的项目
  32. /// </summary>
  33. int CurrentItemIndex = 0;
  34. /// <summary>
  35. /// 当前扫描的所在行
  36. /// </summary>
  37. int CurrentRowIndex = 0;
  38. /// <summary>
  39. /// 正则表达式用于项目匹配
  40. /// </summary>
  41. Regex reg;
  42. /*标识供应商物料编号采集是否通过*/
  43. bool CollectVeProdCodePass = true;
  44. bool CollectQTYPass = true;
  45. bool logout = false;
  46. //每个不同序号存在的
  47. object[] ItemData;
  48. Dictionary<string, string> CollectData;
  49. /// <summary>
  50. /// 是否全选
  51. /// </summary>
  52. bool AllChecked = false;
  53. /*用于存放采集项目的Grid信息*/
  54. Dictionary<string, Dictionary<string, string>> SiItem;
  55. int CloumnCount = 0;
  56. //使用二维数组进行排序
  57. ArrayList<ArrayList<string>> ScanData;
  58. ArrayList<string> GetData;
  59. public UAS_出货标签打印(string Master)
  60. {
  61. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
  62. this.UpdateStyles();
  63. InitializeComponent();
  64. Type dgvType = this.LabelInf.GetType();
  65. PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
  66. pi.SetValue(this.LabelInf, true, null);
  67. Text = Text + "-" + Master;
  68. }
  69. protected override void WndProc(ref Message m)
  70. {
  71. //拦截双击标题栏、移动窗体的系统消息
  72. if (m.Msg != 0xA3)
  73. {
  74. base.WndProc(ref m);
  75. }
  76. }
  77. private void 贴标机条码打印_Load(object sender, EventArgs e)
  78. {
  79. //杀死之前全部未关闭的进程
  80. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  81. for (int i = 0; i < processes.Length; i++)
  82. {
  83. processes[i].Kill();
  84. }
  85. //用计时器重置数据库链接
  86. LogManager.DoLog("程序启动,登陆人员【" + User.UserName + "】");
  87. dh = SystemInf.dh;
  88. CheckForIllegalCrossThreadCalls = false;
  89. CloumnCount = LabelInf.Columns.Count;
  90. pi_inoutno.Focus();
  91. //将本地读取的打印机设置进Combox,并选中默认打印机
  92. sg_brand.FormName = Name;
  93. sg_brand.SetValueField = new string[] { "sg_brand" };
  94. sg_brand.SelectField = "sg_code # 策略编号 ,sg_name # 策略名称,sg_brand # 品牌,sg_separator # 分隔符";
  95. sg_brand.TableName = "scangroup";
  96. sg_brand.DbChange += Sg_brand_DbChange;
  97. pr_code.FormName = Name;
  98. pr_code.SetValueField = new string[] { "pr_code" };
  99. pr_code.TableName = "prodiodetail left join product on pd_prodcode=pr_code";
  100. pr_code.SelectField = "pd_pdno # 行号,pr_code # 物料编号";
  101. cu_code.FormName = Name;
  102. cu_code.SetValueField = new string[] { "cu_code" };
  103. cu_code.TableName = "customer";
  104. cu_code.SelectField = "cu_code # 客户编号,cu_name # 客户名称";
  105. Point pt = new Point();
  106. //禁止所有列的排序
  107. foreach (DataGridViewColumn dgv in LabelInf.Columns)
  108. {
  109. dgv.SortMode = DataGridViewColumnSortMode.NotSortable;
  110. }
  111. int ScreenWidth = Screen.GetWorkingArea(pt).Width;
  112. //设置获取当前屏幕大小自动全屏但是保留任务栏
  113. Rectangle ScreenArea = Screen.GetWorkingArea(this);
  114. Top = 0;
  115. Left = 0;
  116. Width = ScreenArea.Width;
  117. Height = ScreenArea.Height;
  118. asc.controllInitializeSize(this);
  119. asc.controlAutoSize(this);
  120. }
  121. private void Sg_brand_DbChange(object sender, EventArgs e)
  122. {
  123. DataTable dt = sg_brand.ReturnData;
  124. BaseUtil.SetFormValue(this.Controls, dt);
  125. }
  126. //只执行一次窗体自适应
  127. bool AutoSized = false;
  128. private void 贴标机条码打印_SizeChanged(object sender, EventArgs e)
  129. {
  130. if (!AutoSized)
  131. {
  132. asc.controlAutoSize(this);
  133. AutoSized = true;
  134. }
  135. }
  136. private void GenerateBarCode_Click(object sender, EventArgs e)
  137. {
  138. 生成条码 form = new 生成条码(pi_inoutno.Text);
  139. form.FormClosed += LoadGridData;
  140. BaseUtil.SetFormCenter(form);
  141. form.ShowDialog();
  142. }
  143. private void CollectionSetting_Click(object sender, EventArgs e)
  144. {
  145. 采集策略 form = new 采集策略(sg_brand.Text);
  146. form.WindowState = FormWindowState.Maximized;
  147. form.FormClosed += sg_brand_UserControlTextChanged;
  148. BaseUtil.SetFormCenter(form);
  149. form.ShowDialog();
  150. }
  151. private void LabelFormClose(object sender, EventArgs e)
  152. {
  153. }
  154. /// <summary>
  155. /// 保存明细
  156. /// </summary>
  157. /// <param name="sender"></param>
  158. /// <param name="e"></param>
  159. private void SaveGrid_Click(object sender, EventArgs e)
  160. {
  161. DataTable savedt = (LabelInf.DataSource as DataTable);
  162. if (savedt != null && savedt.GetChanges() != null)
  163. {
  164. MidSource.EndEdit();
  165. dh.SaveDataTable(savedt.GetChanges(), "ProdioBarCode", "pib_id");
  166. MessageBox.Show("保存成功!");
  167. }
  168. }
  169. //放大镜选择后出发的事件
  170. private void sg_brand_UserControlTextChanged(object sender, EventArgs e)
  171. {
  172. //取已启用的的按照采集次序排序
  173. DataTable dt = (DataTable)dh.ExecuteSql("select si_item,si_detno,sg_separator,si_kind,si_expression,si_expressionitem,si_index,si_indexstring,si_length from scanitem left join scangroup on si_sgid=sg_id where sg_brand='" + sg_brand.Text + "' and si_enable<>0 order by si_detno,si_innerdetno", "select");
  174. if (dt.Rows.Count == 0)
  175. {
  176. dt = (DataTable)dh.ExecuteSql("select si_item,si_detno,sg_separator,si_kind,si_expression,si_expressionitem,si_index,si_indexstring,si_length from scanitem left join scangroup on si_sgid=sg_id where nvl(sg_brand,' ')=' ' and si_enable<>0 order by si_detno,si_innerdetno", "select");
  177. }
  178. BaseUtil.FillDgvWithDataTable(Si_ItemDGV, dt);
  179. if (dt.Rows.Count > 0)
  180. {
  181. sg_separator.Text = dt.Rows[0]["sg_separator"].ToString();
  182. }
  183. //按DetNo排列之后的采集项
  184. ScanData = new ArrayList<ArrayList<string>>();
  185. //每个采集项目的子项
  186. GetData = new ArrayList<string>();
  187. //将数据添加进一个List的二维数组中
  188. for (int i = 0; i < Si_ItemDGV.RowCount; i++)
  189. {
  190. //如果不包含这个则添加进数组
  191. if (!GetData.Contains(Si_ItemDGV.Rows[i].Cells["si_item"].Value.ToString()))
  192. {
  193. GetData.Add(Si_ItemDGV.Rows[i].Cells["si_item"].Value.ToString());
  194. }
  195. //如果和后一个的采集次序相同
  196. if (((i + 1) < Si_ItemDGV.RowCount) && Si_ItemDGV.Rows[i].Cells["si_detno"].Value.ToString() == Si_ItemDGV.Rows[i + 1].Cells["si_detno"].Value.ToString())
  197. {
  198. GetData.Add(Si_ItemDGV.Rows[i + 1].Cells["si_item"].Value.ToString());
  199. }
  200. else
  201. {
  202. ScanData.Add(GetData);
  203. //添加完一次数据需要一个新的List
  204. GetData = new ArrayList<string>();
  205. }
  206. }
  207. RemindUser();
  208. if (ScanData.ToArray().Length > 0)
  209. {
  210. object[] arr = (ScanData.ToArray()[0] as ArrayList<string>).ToArray();
  211. if (arr.Contains("MPN") || arr.Contains("CPN"))
  212. {
  213. AutoMatch.CheckState = CheckState.Checked;
  214. AutoMatch.Enabled = true;
  215. }
  216. else
  217. {
  218. AutoMatch.CheckState = CheckState.Unchecked;
  219. AutoMatch.Enabled = false;
  220. }
  221. }
  222. }
  223. //输入框Enter事件
  224. private void Input_KeyDown(object sender, KeyEventArgs e)
  225. {
  226. if (e.KeyCode == Keys.Enter)
  227. {
  228. if (Input.Text == "")
  229. {
  230. MessageBox.Show("采集的数据不能为空");
  231. return;
  232. }
  233. if (Si_ItemDGV.Rows.Count == 0)
  234. {
  235. MessageBox.Show("未维护采集策略");
  236. return;
  237. }
  238. CollectInputData();
  239. DataTable savedt = (LabelInf.DataSource as DataTable);
  240. if (savedt != null && savedt.GetChanges() != null)
  241. {
  242. MidSource.EndEdit();
  243. dh.SaveDataTable(savedt.GetChanges(), "ProdioBarCode", "pib_id");
  244. }
  245. }
  246. }
  247. /// <summary>
  248. /// 设置自动匹配的数据的行号
  249. /// </summary>
  250. /// <returns></returns>
  251. private void SetAutoMatchRow()
  252. {
  253. //采集策略的第一组数据
  254. string[] arr = new string[ItemData.Length];
  255. if (sg_separator.Text == "")
  256. {
  257. for (int i = 0; i < arr.Length; i++)
  258. {
  259. arr[i] = Input.Text;
  260. }
  261. }
  262. else
  263. {
  264. arr = Input.Text.Split(sg_separator.Text.ToCharArray()).ToArray();
  265. }
  266. bool FindAutoMatch = false;
  267. for (int i = 0; i < LabelInf.RowCount; i++)
  268. {
  269. for (int j = 0; j < arr.Length; j++)
  270. {
  271. string orispeccode = LabelInf.Rows[i].Cells["pr_orispeccode"].Value.ToString();
  272. if ((MatchStr(arr[j].ToString(), "MPN").Contains(orispeccode) || MatchStr(arr[j].ToString(), "MPN").Trim().Contains(orispeccode) || orispeccode == MatchStr(arr[j].ToString(), "MPN") || LabelInf.Rows[i].Cells["pd_custprodcode"].Value.ToString() == MatchStr(arr[j].ToString(), "CPN")) && LabelInf.Rows[i].Cells["pib_ifpick"].FormattedValue.ToString() != "True")
  273. {
  274. CurrentRowIndex = i;
  275. if (CurrentRowIndex - 5 > 0)
  276. LabelInf.FirstDisplayedScrollingRowIndex = CurrentRowIndex - 5;
  277. else
  278. LabelInf.FirstDisplayedScrollingRowIndex = 0;
  279. Brand = LabelInf.Rows[CurrentRowIndex].Cells["pib_brand"].FormattedValue.ToString();
  280. if (Brand != "")
  281. sg_brand.Text = Brand;
  282. FindAutoMatch = true;
  283. break;
  284. }
  285. }
  286. if (FindAutoMatch)
  287. break;
  288. }
  289. }
  290. /// <summary>
  291. /// 采集数据
  292. /// </summary>
  293. private void CollectInputData()
  294. {
  295. LogManager.DoLog("采集数据【" + Input.Text + "】,使用采集策略编号【" + sg_brand.Text + "】");
  296. //按DetNo排列之后的采集项
  297. ScanData = new ArrayList<ArrayList<string>>();
  298. //每个采集项目的子项
  299. GetData = new ArrayList<string>();
  300. //用于保存采集策略的具体信息
  301. SiItem = new Dictionary<string, Dictionary<string, string>>();
  302. for (int i = 0; i < Si_ItemDGV.RowCount; i++)
  303. {
  304. //如果不包含这个则添加进数组
  305. if (!GetData.Contains(Si_ItemDGV.Rows[i].Cells["si_item"].Value.ToString()))
  306. GetData.Add(Si_ItemDGV.Rows[i].Cells["si_item"].Value.ToString());
  307. //如果和后一个的采集次序相同
  308. if (((i + 1) < Si_ItemDGV.RowCount) && Si_ItemDGV.Rows[i].Cells["si_detno"].Value.ToString() == Si_ItemDGV.Rows[i + 1].Cells["si_detno"].Value.ToString())
  309. GetData.Add(Si_ItemDGV.Rows[i + 1].Cells["si_item"].Value.ToString());
  310. else
  311. {
  312. ScanData.Add(GetData);
  313. //添加完一次数据需要一个新的List
  314. GetData = new ArrayList<string>();
  315. }
  316. Dictionary<string, string> item = new Dictionary<string, string>();
  317. for (int j = 0; j < Si_ItemDGV.ColumnCount; j++)
  318. {
  319. item.Add(Si_ItemDGV.Columns[j].DataPropertyName, Si_ItemDGV.Rows[i].Cells[j].Value.ToString());
  320. }
  321. SiItem.Add(Si_ItemDGV.Rows[i].Cells["si_item"].Value.ToString().ToUpper(), item);
  322. }
  323. //采集项目的数组
  324. ItemData = (ScanData.ToArray()[CurrentItemIndex] as ArrayList<string>).ToArray();
  325. //分隔符不为空的时候 //采集的项和Grid的数目不等
  326. //用户采集的数据的分割数组
  327. CollectData = new Dictionary<string, string>();
  328. if (sg_separator.Text == "")
  329. {
  330. for (int i = 0; i < ItemData.Length; i++)
  331. CollectData.Add(ItemData[i].ToString().ToUpper(), Input.Text);
  332. }
  333. else
  334. {
  335. int DataCount = Input.Text.Split(sg_separator.Text.ToCharArray()).Length;
  336. if (DataCount > ItemData.Length)
  337. {
  338. MessageBox.Show("采集数据大于采集项次");
  339. return;
  340. }
  341. else if (DataCount < ItemData.Length)
  342. {
  343. MessageBox.Show("采集数据小于采集项次");
  344. return;
  345. }
  346. for (int i = 0; i < DataCount; i++)
  347. CollectData.Add(ItemData[i].ToString().ToUpper(), Input.Text.Split(sg_separator.Text.ToCharArray())[i]);
  348. }
  349. //首先判断当前采集的个数,如果采集的个数为1则不对数据进行分隔符验证
  350. if (ItemData.Length == 1)
  351. {
  352. CollectData.Clear();
  353. CollectData.Add(ItemData[0].ToString().ToUpper(), Input.Text);
  354. //勾选了自动匹配调用该函数
  355. if (AutoMatch.Checked)
  356. SetAutoMatchRow();
  357. //将筛选之后的值赋给Cell
  358. SetDataToCell(ref CollectData, ItemData[0].ToString().ToUpper());
  359. }
  360. //如果本次采集的数据分割后和当前的采集项目个数不一样提示用户错误
  361. else if (CollectData.ToArray().Length == ItemData.Length)
  362. {
  363. for (int i = 0; i < ItemData.Length; i++)
  364. {
  365. //勾选了自动匹配调用该函数
  366. if (AutoMatch.Checked)
  367. SetAutoMatchRow();
  368. //将筛选之后的值赋给Cell
  369. SetDataToCell(ref CollectData, ItemData[i].ToString().ToUpper());
  370. }
  371. }
  372. else
  373. {
  374. MessageBox.Show("所采集的数据个数和采集项目不符");
  375. return;
  376. }
  377. //采集项目的索引+1
  378. CurrentItemIndex = CurrentItemIndex + 1;
  379. //如果已经采集完了最后一个
  380. if (CurrentItemIndex == ScanData.ToArray().Length)
  381. {
  382. //数据校验均已通过则勾选上已采集
  383. if (CollectVeProdCodePass && CollectQTYPass)
  384. {
  385. //如果按盒号采集,所有的此盒的均更新为
  386. DataTable dt = (DataTable)MidSource.DataSource;
  387. LabelInf.Rows[CurrentRowIndex].Cells["pib_ifpick"].Value = true;
  388. LabelInf.Rows[CurrentRowIndex].Cells["Choose"].Value = true;
  389. string datecode = LabelInf.Rows[CurrentRowIndex].Cells["pib_datecode"].Value.ToString();
  390. string lotno = LabelInf.Rows[CurrentRowIndex].Cells["pib_lotno"].Value.ToString();
  391. string pib_id = LabelInf.Rows[CurrentRowIndex].Cells["pib_id1"].Value.ToString();
  392. ErrorType.Clear();
  393. dh.ExecuteSql("update prodiobarcode set pib_datecode='" + datecode + "',pib_lotno='" + lotno + "',pib_ifpick=-1 where pib_id='" + pib_id + "'", "update");
  394. LabelInf.Invalidate();
  395. }
  396. //采集未通过的时候
  397. else
  398. {
  399. ErrorType.Clear();
  400. //如果自动采集采集未成功需要移动到下一行
  401. if (!AutoMatch.Checked)
  402. SetRowIndexToCollectRow();
  403. }
  404. //当前项目已采集完成,重置采集项目
  405. CurrentItemIndex = 0;
  406. //未勾选自动匹配,设置到下一个未采集的行
  407. if (!AutoMatch.Checked)
  408. SetRowIndexToCollectRow();
  409. }
  410. RemindUser();
  411. //提醒用户需要采集的数据
  412. MessageLog.AppendText(">>扫描到数据" + Input.Text + "\n", Color.Blue);
  413. //如果所采集的行的物料不一样的话,重新计算箱内容量
  414. if (CurrentRowIndex - 1 > 0)
  415. {
  416. string LastRowProd = LabelInf.Rows[CurrentRowIndex - 1].Cells["pib_prodcode"].Value.ToString();
  417. string CurrentRowProd = LabelInf.Rows[CurrentRowIndex].Cells["pib_prodcode"].Value.ToString();
  418. }
  419. if (CleanInputAfterCollect.Checked)
  420. Input.Text = "";
  421. }
  422. //设置行的索引到当前需要采集的行
  423. private void SetRowIndexToCollectRow()
  424. {
  425. //获取当前需要采集的行
  426. for (int i = CurrentRowIndex; i < LabelInf.RowCount; i++)
  427. {
  428. if (LabelInf.Rows[i].Cells["pib_ifpick"].FormattedValue.ToString() != "True")
  429. {
  430. griddetno.Text = LabelInf.Rows[i].Cells["pib_pdno"].FormattedValue.ToString();
  431. pr_code.Text = LabelInf.Rows[i].Cells["pib_prodcode"].FormattedValue.ToString();
  432. CurrentRowIndex = LabelInf.Rows[i].Cells["pib_prodcode"].RowIndex;
  433. break;
  434. }
  435. }
  436. }
  437. /// <summary>
  438. /// 提醒用户当前采集的项目
  439. /// </summary>
  440. private void RemindUser()
  441. {
  442. SetRowIndexToCollectRow();
  443. if (ScanData.ToArray().Length > 0)
  444. {
  445. object[] arr = (ScanData.ToArray()[CurrentItemIndex] as ArrayList<string>).ToArray();
  446. string Inf = "";
  447. for (int i = 0; i < arr.Length; i++)
  448. {
  449. Inf += arr[i] + " ";
  450. }
  451. MessageLog.AppendText(">>当前采集行" + (CurrentRowIndex + 1) + ",请采集" + Inf + "\n", Color.Green);
  452. }
  453. }
  454. List<string> ErrorType = new List<string>();
  455. /// <summary>
  456. /// 自定义函数 将匹配之后的值设置到Cell中,传递引用,每次赋值之后从List中移除已使用过的
  457. /// </summary>
  458. /// <param name="data"></param>
  459. /// <param name="kind"></param>
  460. private void SetDataToCell(ref Dictionary<string, string> setdata, string kind)
  461. {
  462. int length = setdata.Count;
  463. for (int j = 0; j < length; j++)
  464. {
  465. string[] data = new string[setdata.Count];
  466. for (int i = 0; i < length; i++)
  467. {
  468. data[i] = setdata[kind];
  469. }
  470. for (int i = 0; i < data.Length; i++)
  471. {
  472. DataGridViewCell cell = null;
  473. string Matchstr = "";
  474. switch (kind.ToString().ToUpper())
  475. {
  476. case "DATECODE":
  477. cell = LabelInf.Rows[CurrentRowIndex].Cells["pib_datecode"];
  478. Matchstr = MatchStr(data[j], "DATECODE");
  479. setdata.Remove("DATECODE");
  480. length--;
  481. break;
  482. case "LOTNO":
  483. cell = LabelInf.Rows[CurrentRowIndex].Cells["pib_lotno"];
  484. Matchstr = MatchStr(data[j], "LOTNO");
  485. setdata.Remove("LOTNO");
  486. length--;
  487. break;
  488. case "MPN":
  489. cell = LabelInf.Rows[CurrentRowIndex].Cells["pr_orispeccode"];
  490. Matchstr = MatchStr(data[j], "MPN");
  491. //采集的供应商号不匹配的话
  492. if (cell.Value.ToString() == Matchstr || Matchstr.Trim().Contains(cell.Value.ToString()) || Matchstr.Contains(cell.Value.ToString()))
  493. {
  494. ErrorType.Add("OK_MPN");
  495. LabelInf.Refresh();
  496. CollectVeProdCodePass = true;
  497. }
  498. else
  499. {
  500. ErrorType.Add("NG_MPN");
  501. CollectVeProdCodePass = false;
  502. //添加需要重绘的Cell
  503. LabelInf.Refresh();
  504. MessageLog.AppendText(">>供应商物料编号不匹配\n", Color.Red);
  505. }
  506. setdata.Remove("MPN");
  507. length--;
  508. break;
  509. case "QTY":
  510. cell = LabelInf.Rows[CurrentRowIndex].Cells["pib_qty"];
  511. Matchstr = MatchStr(data[j], "QTY");
  512. //如果采集的数量不相等的话
  513. if (cell.Value.ToString() != Matchstr)
  514. {
  515. CollectQTYPass = false;
  516. LabelInf.Refresh();
  517. MessageLog.AppendText(">>数量不匹配\n", Color.Red);
  518. }
  519. else
  520. {
  521. LabelInf.Refresh();
  522. CollectQTYPass = true;
  523. }
  524. setdata.Remove("QTY");
  525. length--;
  526. break;
  527. case "CPN":
  528. cell = LabelInf.Rows[CurrentRowIndex].Cells["pd_custprodcode"];
  529. Matchstr = MatchStr(data[j], "CPN");
  530. //如果采集的数量不相等的话
  531. if (cell.Value.ToString() != Matchstr)
  532. {
  533. ErrorType.Add("NG_CPN");
  534. CollectQTYPass = false;
  535. LabelInf.Refresh();
  536. MessageLog.AppendText(">>CPN不匹配\n", Color.Red);
  537. }
  538. else
  539. {
  540. ErrorType.Add("OK_CPN");
  541. LabelInf.Refresh();
  542. CollectQTYPass = true;
  543. }
  544. setdata.Remove("CPN");
  545. length--;
  546. break;
  547. default:
  548. break;
  549. }
  550. //如果数据为空或者数据不为空的时候但是和需要采集的数据不相等的情况下进行采集
  551. //MPN和QTY只做比较不需要赋值
  552. if (cell.Value.ToString() == "" || cell.Value.ToString() != Matchstr)
  553. {
  554. string MatchResult = "";
  555. if (kind != "MPN" && kind != "QTY" && kind != "CPN")
  556. {
  557. DataTable dt = (DataTable)MidSource.DataSource;
  558. cell.Value = Matchstr;
  559. MatchResult = "成功";
  560. }
  561. else
  562. MatchResult = "失败";
  563. switch (SiItem[kind]["si_kind"])
  564. {
  565. case "索引字符":
  566. LogManager.DoLog("采集项" + kind + ",匹配结果:【" + MatchResult + "】,匹配方式:" + SiItem[kind]["si_kind"] + ",使用字符" + SiItem[kind]["si_indexstring"] + ",匹配后字符串" + Matchstr);
  567. break;
  568. case "起始位置":
  569. LogManager.DoLog("采集项" + kind + ",匹配结果:【" + MatchResult + "】,匹配方式:" + SiItem[kind]["si_kind"] + ",从第" + SiItem[kind]["si_index"] + "位开始匹配,匹配后字符串" + Matchstr);
  570. break;
  571. case "二次解析":
  572. LogManager.DoLog("采集项" + kind + ",匹配结果:【" + MatchResult + "】,匹配方式:" + SiItem[kind]["si_kind"] + ",匹配后字符串" + Matchstr);
  573. break;
  574. case "全部":
  575. LogManager.DoLog("采集项" + kind + ",匹配结果:【" + MatchResult + "】,匹配方式:" + SiItem[kind]["si_kind"] + ",匹配后字符串" + Matchstr);
  576. break;
  577. default:
  578. break;
  579. }
  580. }
  581. else
  582. LogManager.DoLog("采集项" + kind + ",匹配结果:【成功】,匹配方式:" + SiItem[kind]["si_kind"] + ",,匹配后字符串" + Matchstr);
  583. }
  584. }
  585. }
  586. /// <summary>
  587. /// 自定义函数 根据匹配规则
  588. /// si_item的二维数组,str是需要赋值的字符串,item表示DateCode,LotNo等
  589. /// </summary>
  590. /// <param name="SiItem"></param>
  591. /// <param name="str"></param>
  592. /// <param name="item"></param>
  593. /// <returns></returns>
  594. private string MatchStr(string str, string item)
  595. {
  596. string kind = SiItem[item]["si_kind"];
  597. //起始字符不为空的时候
  598. switch (kind)
  599. {
  600. case "起始位置":
  601. if (SiItem[item]["si_index"] != "")
  602. {
  603. try
  604. {
  605. //长度不为空的时候按照指定的长度去取数据
  606. if (SiItem[item]["si_length"] != "")
  607. str = str.Substring(int.Parse(SiItem[item]["si_index"]), int.Parse(SiItem[item]["si_length"]));
  608. //长度为空的时候取index之后的全部数据
  609. else
  610. str = str.Substring(int.Parse(SiItem[item]["si_index"]));
  611. }
  612. catch (Exception e) { LogManager.DoLog(e.StackTrace); }
  613. }
  614. break;
  615. case "索引字符":
  616. try
  617. {
  618. if (SiItem[item]["si_indexstring"] != "")
  619. {
  620. //长度不为空的时候按照指定的长度去取数据
  621. if (SiItem[item]["si_length"] != "")
  622. str = str.Substring(str.IndexOf(SiItem[item]["si_indexstring"]) + 1, int.Parse(SiItem[item]["si_length"]));
  623. //长度为空的时候取index之后的全部数据
  624. else
  625. str = str.Substring(str.IndexOf(SiItem[item]["si_indexstring"]) + 1);
  626. }
  627. }
  628. catch (Exception e) { LogManager.DoLog(e.StackTrace); }
  629. break;
  630. case "全部":
  631. break;
  632. default:
  633. break;
  634. }
  635. if (SiItem[item]["si_expression"] != "")
  636. {
  637. string log = "使用正则表达式" + SiItem[item]["si_expression"] + "匹配数据" + str;
  638. reg = new Regex(SiItem[item]["si_expression"]);
  639. try
  640. {
  641. str = reg.Matches(str)[int.Parse(SiItem[item]["si_expressionitem"].ToString()) - 1].Value;
  642. }
  643. catch (Exception)
  644. {
  645. }
  646. log += ",匹配后数据" + str;
  647. LogManager.DoLog(log);
  648. }
  649. return str;
  650. }
  651. //关闭窗口前提示用户确认
  652. private void 贴标机条码打印_FormClosing(object sender, FormClosingEventArgs e)
  653. {
  654. //如果不是注销的话
  655. if (!logout)
  656. {
  657. string close = MessageBox.Show(this.ParentForm, "是否关闭", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  658. if (close.ToString() != "Yes")
  659. e.Cancel = true;
  660. else
  661. {
  662. LogManager.DoLog("关闭程序");
  663. }
  664. }
  665. }
  666. /// <summary>
  667. /// 出入库单录入框的回车事件
  668. /// </summary>
  669. /// <param name="sender"></param>
  670. /// <param name="e"></param>
  671. private void pi_inoutno_KeyDown(object sender, KeyEventArgs e)
  672. {
  673. if (e.KeyCode == Keys.Enter)
  674. {
  675. sql.Clear();
  676. sql.Append("select pi_id,pi_cardcode,to_char(pi_date,'yyyymmdd')pi_date from prodinout where pi_inoutno='" + pi_inoutno.Text + "' ");
  677. DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  678. ErrorType.Clear();
  679. if (dt.Rows.Count > 0)
  680. {
  681. cu_code.Text = dt.Rows[0]["pi_cardcode"].ToString();
  682. pi_date.Text = dt.Rows[0]["pi_date"].ToString();
  683. PI_ID = dt.Rows[0]["pi_id"].ToString();
  684. dt = (DataTable)dh.ExecuteSql("select 1 from prodiobarcode where pib_inoutno='" + pi_inoutno.Text + "'", "select");
  685. if (dh.GetConfig("AutoBarcode", "ProdInOut!Sale").ToString() != "" && dt.Rows.Count == 0)
  686. {
  687. string Error = "";
  688. string[] param = new string[] { PI_ID, "", Error };
  689. dh.CallProcedure("GetCustBarcode", param);
  690. }
  691. LoadGridData(sender, e);
  692. //设置当前的最大箱号
  693. CurrentItemIndex = 0;
  694. LogManager.DoLog("输入单号【" + pi_inoutno.Text + "】");
  695. }
  696. else
  697. MessageBox.Show("当前出入库单号不存在或者未审核!");
  698. }
  699. }
  700. private void CleanDetail_Click(object sender, EventArgs e)
  701. {
  702. ArrayList<string> DeleteID = new ArrayList<string>();
  703. for (int i = 0; i < LabelInf.RowCount; i++)
  704. {
  705. if (LabelInf.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
  706. DeleteID.Add(LabelInf.Rows[i].Cells["pib_id1"].Value.ToString());
  707. }
  708. //勾选了删除的明细之后
  709. if (DeleteID.ToArray().Length > 0)
  710. {
  711. string close = MessageBox.Show(this.ParentForm, "删除后不可恢复,是否确认删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  712. if (close.ToString() == "Yes")
  713. {
  714. dh.BatchInsert("delete from prodiobarcode where pib_id=:pib_id", new string[] { "pib_id" }, DeleteID.ToArray());
  715. MessageBox.Show("删除成功");
  716. LoadGridData(sender, e);
  717. }
  718. }
  719. else
  720. MessageBox.Show("尚未勾选需要删除的明细");
  721. }
  722. private void LoadGridData()
  723. {
  724. LoadGridData(new object(), new EventArgs());
  725. }
  726. /// <summary>
  727. /// 自定义函数 加载明细行的数据,多处使用添加进函数
  728. /// </summary>
  729. /// <param name="sender"></param>
  730. /// <param name="e"></param>
  731. private void LoadGridData(object sender, EventArgs e)
  732. {
  733. sql.Clear();
  734. sql.Append("select 0 choose,pd_custprodcode,pd_custprodspec,pr_orispeccode,pd_pocode,pib_madein,pib_custbarcode,pib_custoutboxcode,pib_id,pib_datecode1,pib_pdid,pib_piid,pib_pdno,pib_prodcode,nvl(nvl(pd_brand,pib_brand),pr_brand)pib_brand, pr_vendprodcode,");
  735. sql.Append("pib_lotno,pib_datecode,pib_qty,pr_spec,pr_detail,pr_zxbzs,pr_unit,pib_barcode,pib_outboxcode1,pib_outboxcode2,nvl(pib_ifpick,0)pib_ifpick,nvl(pib_ifprint,0)pib_ifprint");
  736. sql.Append(" from prodiobarcode left join prodiodetail on pib_piid=pd_piid and pd_pdno=pib_pdno and ");
  737. sql.Append(" pd_prodcode=pib_prodcode left join product on pr_code=pib_prodcode left join sale on sa_code=pib_ordercode ");
  738. sql.Append("where pib_piid='" + PI_ID + "' order by pd_pdno");
  739. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  740. MidSource.DataSource = dt;
  741. BaseUtil.FillDgvWithDataTable(LabelInf, (DataTable)MidSource.DataSource);
  742. //有数据的话默认取第一条的品牌去取采集策略
  743. TotalCount.Text = LabelInf.RowCount.ToString();
  744. if (LabelInf.RowCount > 0)
  745. {
  746. Brand = LabelInf.Rows[0].Cells["pib_brand"].FormattedValue.ToString();
  747. if (Brand != "")
  748. sg_brand.Text = Brand;
  749. }
  750. //绑定数据之后往下找到未采集的数据显示在当前采集的栏目
  751. for (int i = 0; i < LabelInf.RowCount; i++)
  752. {
  753. if (LabelInf.Rows[i].Cells["pib_ifpick"].FormattedValue.ToString() != "True")
  754. {
  755. griddetno.Text = LabelInf.Rows[i].Cells["pib_pdno"].FormattedValue.ToString();
  756. pr_code.Text = LabelInf.Rows[i].Cells["pib_prodcode"].FormattedValue.ToString();
  757. pib_id.Text = LabelInf.Rows[i].Cells["pib_id1"].FormattedValue.ToString();
  758. CurrentRowIndex = LabelInf.Rows[i].Cells["pib_prodcode"].RowIndex;
  759. break;
  760. }
  761. }
  762. }
  763. /// <summary>
  764. /// 重绘Cell的颜色
  765. /// </summary>
  766. /// <param name="sender"></param>
  767. /// <param name="e"></param>
  768. private void LabelInf_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  769. {
  770. bool mouseOver = e.CellBounds.Contains(this.PointToClient(Cursor.Position));
  771. if (e.ColumnIndex > 0)
  772. {
  773. if (LabelInf.Columns[e.ColumnIndex].Name == "pib_custbarcode" || LabelInf.Columns[e.ColumnIndex].Name == "pib_barcode" || LabelInf.Columns[e.ColumnIndex].Name == "pib_madein" || LabelInf.Columns[e.ColumnIndex].Name == "pib_lotno" || LabelInf.Columns[e.ColumnIndex].Name == "pib_datecode" || LabelInf.Columns[e.ColumnIndex].Name == "pib_outboxcode1" || LabelInf.Columns[e.ColumnIndex].Name == "pib_outboxcode2" || LabelInf.Columns[e.ColumnIndex].Name == "pib_brand")
  774. {
  775. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
  776. e.Graphics.FillRectangle(mouseOver ? solidBrush : Brushes.LightSeaGreen, e.CellBounds);
  777. Rectangle border = e.CellBounds;
  778. border.Width -= 1;
  779. e.Graphics.DrawRectangle(Pens.White, border);
  780. e.PaintContent(e.CellBounds);
  781. e.Handled = true;
  782. }
  783. if (e.RowIndex >= 0)
  784. {
  785. if (LabelInf.Rows[e.RowIndex].Cells["pib_ifpick"].FormattedValue.ToString() == "True")
  786. {
  787. if (LabelInf.Columns[e.ColumnIndex].Name == "pr_orispeccode" || LabelInf.Columns[e.ColumnIndex].Name == "pd_custprodcode")
  788. {
  789. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
  790. e.Graphics.FillRectangle(Brushes.ForestGreen, e.CellBounds);
  791. Rectangle border = e.CellBounds;
  792. border.Width -= 1;
  793. e.Graphics.DrawRectangle(Pens.Black, border);
  794. e.PaintContent(e.CellBounds);
  795. e.Handled = true;
  796. }
  797. }
  798. }
  799. if (e.RowIndex == CurrentRowIndex)
  800. {
  801. if (LabelInf.Columns[e.ColumnIndex].Name == "pr_orispeccode")
  802. {
  803. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
  804. if (ErrorType.Count > 0)
  805. {
  806. if (ErrorType.Contains("OK_MPN"))
  807. {
  808. e.Graphics.FillRectangle(Brushes.ForestGreen, e.CellBounds);
  809. Rectangle border = e.CellBounds;
  810. border.Width -= 1;
  811. e.Graphics.DrawRectangle(Pens.Black, border);
  812. e.PaintContent(e.CellBounds);
  813. e.Handled = true;
  814. }
  815. else if (ErrorType.Contains("NG_MPN"))
  816. {
  817. e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
  818. Rectangle border = e.CellBounds;
  819. border.Width -= 1;
  820. e.Graphics.DrawRectangle(Pens.Black, border);
  821. e.PaintContent(e.CellBounds);
  822. e.Handled = true;
  823. }
  824. }
  825. }
  826. if (LabelInf.Columns[e.ColumnIndex].Name == "pd_custprodcode")
  827. {
  828. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
  829. if (ErrorType.Count > 0)
  830. {
  831. if (ErrorType.Contains("OK_CPN"))
  832. {
  833. e.Graphics.FillRectangle(Brushes.ForestGreen, e.CellBounds);
  834. Rectangle border = e.CellBounds;
  835. border.Width -= 1;
  836. e.Graphics.DrawRectangle(Pens.Black, border);
  837. e.PaintContent(e.CellBounds);
  838. e.Handled = true;
  839. }
  840. else if (ErrorType.Contains("NG_CPN"))
  841. {
  842. e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
  843. Rectangle border = e.CellBounds;
  844. border.Width -= 1;
  845. e.Graphics.DrawRectangle(Pens.Black, border);
  846. e.PaintContent(e.CellBounds);
  847. e.Handled = true;
  848. }
  849. }
  850. }
  851. }
  852. }
  853. }
  854. private void pr_code_SearchIconClick(object sender, EventArgs e)
  855. {
  856. pr_code.Condition = " pd_inoutno='" + pi_inoutno.Text + "'";
  857. }
  858. private void pr_code_UserControlTextChanged(object sender, EventArgs e)
  859. {
  860. //用户在重新勾选后重置采集项目的索引
  861. for (int i = 0; i < LabelInf.RowCount; i++)
  862. {
  863. if (pr_code.Text == LabelInf.Rows[i].Cells["pib_prodcode"].Value.ToString() && LabelInf.Rows[i].Cells["pib_ifpick"].FormattedValue.ToString() != "True")
  864. {
  865. CurrentRowIndex = i;
  866. break;
  867. }
  868. }
  869. }
  870. private void Refresh_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  871. {
  872. KeyEventArgs e2 = new KeyEventArgs(Keys.Enter);
  873. pi_inoutno_KeyDown(sender, e2);
  874. }
  875. /// <summary>
  876. /// 采集明细切换
  877. /// </summary>
  878. /// <param name="sender"></param>
  879. /// <param name="e"></param>
  880. private void griddetno_KeyDown(object sender, KeyEventArgs e)
  881. {
  882. if (Keys.Enter == e.KeyData)
  883. {
  884. bool FindDetno = false;
  885. for (int i = 0; i < LabelInf.RowCount; i++)
  886. {
  887. if (LabelInf.Rows[i].Cells["pib_pdno"].Value.ToString() == griddetno.Text && LabelInf.Rows[i].Cells["pib_ifpick"].FormattedValue.ToString() != "True")
  888. {
  889. FindDetno = true;
  890. CurrentItemIndex = 0;
  891. CurrentRowIndex = i;
  892. RemindUser();
  893. break;
  894. }
  895. }
  896. if (!FindDetno)
  897. {
  898. MessageBox.Show("不存在未采集的明细序号" + griddetno.Text);
  899. return;
  900. }
  901. }
  902. }
  903. private void LogingOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  904. {
  905. string close = MessageBox.Show(this.ParentForm, "是否注销", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  906. if (close.ToString() == "Yes")
  907. {
  908. Login login = new Login();
  909. //注销的时候需要将拼接的连接字符串置空
  910. DataHelper.DBConnectionString = null;
  911. logout = true;
  912. this.Hide();
  913. login.ShowDialog();
  914. this.Close();
  915. }
  916. }
  917. private void LabelInf_DataError(object sender, DataGridViewDataErrorEventArgs e) { }
  918. private void ChooseAll_Click(object sender, EventArgs e)
  919. {
  920. if (AllChecked)
  921. {
  922. foreach (DataGridViewRow dr in LabelInf.Rows)
  923. dr.Cells[0].Value = false;
  924. AllChecked = false;
  925. }
  926. else
  927. {
  928. foreach (DataGridViewRow dr in LabelInf.Rows)
  929. dr.Cells[0].Value = true;
  930. AllChecked = true;
  931. }
  932. }
  933. private void ExportData_Click(object sender, EventArgs e)
  934. {
  935. ExportFileDialog.Description = "选择导出的路径";
  936. DialogResult result = ExportFileDialog.ShowDialog();
  937. if (result == DialogResult.OK)
  938. {
  939. ExcelHandler eh = new ExcelHandler();
  940. DataTable dt = ((DataTable)MidSource.DataSource).Copy();
  941. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  942. {
  943. for (int j = 0; j < LabelInf.Columns.Count; j++)
  944. {
  945. //去除ID列
  946. if (dt.Columns[i].ColumnName.ToLower().Contains("id") || dt.Columns[i].ColumnName.ToLower() == "pib_barcode" || dt.Columns[i].ColumnName.ToLower() == "pib_pdno" || dt.Columns[i].ColumnName.ToLower() == "pib_ifpick" || dt.Columns[i].ColumnName.ToLower() == "pib_ifprint" || dt.Columns[i].ColumnName.ToLower() == "pib_datecode1" || dt.Columns[i].ColumnName.ToLower() == "pr_vendprodcode")
  947. {
  948. dt.Columns.RemoveAt(i);
  949. break;
  950. }
  951. switch (dt.Columns[i].ColumnName.ToLower())
  952. {
  953. case "pib_lotno":
  954. dt.Columns[i].ColumnName = "批次号";
  955. break;
  956. case "pib_datecode":
  957. dt.Columns[i].ColumnName = "生产日期";
  958. break;
  959. case "pib_custbarcode":
  960. dt.Columns[i].ColumnName = "最小产品包装条码";
  961. break;
  962. default:
  963. break;
  964. }
  965. if (dt.Columns[i].ColumnName.ToLower() == LabelInf.Columns[j].DataPropertyName.ToLower())
  966. {
  967. dt.Columns[i].ColumnName = LabelInf.Columns[j].HeaderText;
  968. break;
  969. }
  970. }
  971. }
  972. eh.ExportExcel(dt, ExportFileDialog.SelectedPath, pi_date.Text + "-" + pi_inoutno.Text);
  973. string close = MessageBox.Show(this.ParentForm, "导出成功,是否打开文件", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  974. if (close.ToString() == "Yes")
  975. System.Diagnostics.Process.Start(ExportFileDialog.SelectedPath + "\\" + pi_date.Text + "-" + pi_inoutno.Text + ".xls");
  976. }
  977. }
  978. private void CleanBarCode_Click(object sender, EventArgs e)
  979. {
  980. string close = MessageBox.Show(this.ParentForm, "是否清除该出货单条码", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  981. if (close.ToString() == "Yes")
  982. {
  983. dh.ExecuteSql("delete from prodiobarcode where pib_inoutno='" + pi_inoutno.Text + "'", "delete");
  984. LoadGridData(sender, e);
  985. }
  986. }
  987. }
  988. }