生成条码.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. using NPOI.SS.Util;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using UAS_LabelMachine.PublicMethod;
  9. namespace UAS_LabelMachine
  10. {
  11. public partial class 生成条码 : Form
  12. {
  13. AutoSizeFormClass asc = new AutoSizeFormClass();
  14. DataHelper dh;
  15. DataTable dt;
  16. StringBuilder sql = new StringBuilder();
  17. string pi_id;
  18. //用于提示超出数量的物料
  19. Dictionary<string, string> NotPass = new Dictionary<string, string>();
  20. string Condition = "";
  21. //生成条码的流水号
  22. int serialnum = 0;
  23. //是否生成过条码
  24. bool FirstCode = false;
  25. public 生成条码(string PI_INOUTNO)
  26. {
  27. InitializeComponent();
  28. pi_inoutno.Text = PI_INOUTNO;
  29. }
  30. private void 生成条码_Load(object sender, EventArgs e)
  31. {
  32. dh = new DataHelper();
  33. ChooseAll.ChooseAll(ProdIoInfDGV);
  34. //如果传进了出入库单号则默认执行一次取数据
  35. if (pi_inoutno.Text != "")
  36. {
  37. KeyEventArgs e2 = new KeyEventArgs(Keys.Enter);
  38. bi_inoutno_KeyDown(sender, e2);
  39. }
  40. pr_kind.Text = "全部";
  41. asc.controllInitializeSize(this);
  42. Width = Width - 1;
  43. }
  44. private void bi_inoutno_KeyDown(object sender, KeyEventArgs e)
  45. {
  46. if (e.KeyCode == Keys.Enter)
  47. {
  48. LoadData();
  49. }
  50. }
  51. private void 生成条码_SizeChanged(object sender, EventArgs e)
  52. {
  53. asc.controlAutoSize(this);
  54. }
  55. /// <summary>
  56. /// 筛选按钮
  57. /// </summary>
  58. /// <param name="sender"></param>
  59. /// <param name="e"></param>
  60. private void Screen_Click(object sender, EventArgs e)
  61. {
  62. switch (pr_kind.Text)
  63. {
  64. case "全部":
  65. Condition = "";
  66. break;
  67. case "单件管控":
  68. Condition = " and pr_tracekind=1";
  69. break;
  70. case "批管控":
  71. Condition = " and pr_tracekind=2";
  72. break;
  73. default:
  74. break;
  75. }
  76. KeyEventArgs e2 = new KeyEventArgs(Keys.Enter);
  77. bi_inoutno_KeyDown(sender, e2);
  78. }
  79. /// <summary>
  80. /// 生成条码
  81. /// </summary>
  82. /// <param name="sender"></param>
  83. /// <param name="e"></param>
  84. private void GenerateBarCode_Click(object sender, EventArgs e)
  85. {
  86. //有错误需要提醒的内容
  87. int CheckedRowCount = 0;
  88. //遍历整个Grid,勾选的项目全部进行条码生成
  89. for (int i = 0; i < ProdIoInfDGV.RowCount; i++)
  90. {
  91. if (ProdIoInfDGV.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
  92. {
  93. CheckedRowCount++;
  94. List<string> pib_inqty = new List<string>();
  95. string pd_id = ProdIoInfDGV.Rows[i].Cells["pd_id"].FormattedValue.ToString();
  96. string pd_prodcode = ProdIoInfDGV.Rows[i].Cells["pd_prodcode"].FormattedValue.ToString();
  97. string pr_id = ProdIoInfDGV.Rows[i].Cells["pr_id"].FormattedValue.ToString();
  98. string pd_ordercode = ProdIoInfDGV.Rows[i].Cells["pd_ordercode"].FormattedValue.ToString();
  99. string pd_pdno = ProdIoInfDGV.Rows[i].Cells["pd_pdno"].FormattedValue.ToString();
  100. string pd_orderdetno = ProdIoInfDGV.Rows[i].Cells["pd_orderdetno"].FormattedValue.ToString();
  101. //总数
  102. int pd_totalqty = int.Parse(ProdIoInfDGV.Rows[i].Cells["pd_totalqty"].FormattedValue.ToString());
  103. //本次数量
  104. int pd_qty = int.Parse(ProdIoInfDGV.Rows[i].Cells["pd_qty"].FormattedValue.ToString());
  105. //最小包装数
  106. int pr_zxbzs = int.Parse(ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].FormattedValue.ToString());
  107. //中盒容量
  108. int mid_qty = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_qty"].FormattedValue.ToString());
  109. //如果中盘盒数量为1且有尾数则表示一箱未装满
  110. int mid_num = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_num"].FormattedValue.ToString());
  111. //中盒尾数
  112. int mid_remain = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_remain"].FormattedValue.ToString());
  113. //本次数量不能大于总数
  114. //if (pd_qty > pd_totalqty)
  115. //{
  116. // if (!NotPass.ContainsKey(pd_pdno + pd_prodcode))
  117. // {
  118. // NotPass.Add(pd_pdno + pd_prodcode, pd_prodcode + "本次数量不能大于总数");
  119. // }
  120. // CheckedRowCount--;
  121. // continue;
  122. //}
  123. ////后台查询已生成数量,本次数量不能大于已生成数量
  124. //int qty = int.Parse(dh.getFieldDataByCondition("PRODIOBARCODE left join prodiodetail on pib_pdid=pd_id and pib_prodcode=pd_prodcode", "nvl(sum(nvl(pib_qty,0)),0)", " PIB_PIID='" + pi_id + "' and pd_pdno='" + pd_pdno + "' and pib_pdid=pd_id and pib_prodcode=pd_prodcode and pd_prodcode='" + pd_prodcode + "'").ToString());
  125. //if (pd_qty > pd_totalqty - qty || pd_qty == 0)
  126. //{
  127. // if (!NotPass.ContainsKey(pd_pdno + pd_prodcode))
  128. // {
  129. // NotPass.Add(pd_pdno + pd_prodcode, "行号" + pd_pdno + ",物料号" + pd_prodcode + "已生成数量" + qty + ",剩余可以用数量" + (pd_totalqty - qty));
  130. // }
  131. // CheckedRowCount--;
  132. // continue;
  133. //}
  134. string pib_barcode = dh.getFieldDataByCondition("prodiobarcode", "max(pib_barcode)", "PIB_INOUTNO='" + pi_inoutno.Text + "'").ToString();
  135. //中盒数量*中盒容量=需要打印的单盘标签
  136. ArrayList<string> midcode = new ArrayList<string>();
  137. ArrayList<string> barcode = new ArrayList<string>();
  138. //循环中盒号的个数,取当前出入库单最大 的中盒号+1
  139. for (int j = 0; j < mid_num; j++)
  140. {
  141. //获取中盘的编号
  142. string mid_code = dh.getFieldDataByCondition("PRODIOBARCODE", "nvl(max(to_number(PIB_OUTBOXCODE1)),0)+" + (j + 1), "PIB_INOUTNO='" + pi_inoutno.Text + "'").ToString();
  143. //如果尾数不为0,并且已经遍历到了最后一箱(未装满的箱)
  144. int count = 0;
  145. if (mid_remain != 0 && j + 1 == mid_num)
  146. {
  147. //剩下的尾数刚好够整数的最小包或者加上一个未装满的最小包
  148. count = mid_remain % pr_zxbzs == 0 ? mid_remain / pr_zxbzs : (mid_remain / pr_zxbzs) + 1;
  149. }
  150. else
  151. {
  152. //循环中盒的箱内容量
  153. count = mid_qty;
  154. }
  155. for (int k = 0; k < count; k++)
  156. {
  157. //将箱号添加进List
  158. barcode.Add(BarcodeMethod1(pd_id, pr_id, pib_barcode));
  159. midcode.Add(mid_code);
  160. pib_inqty.Add(pr_zxbzs.ToString());
  161. }
  162. }
  163. sql.Clear();
  164. sql.Append("insert into prodiobarcode (PIB_ID,PIB_PRODCODE,PIB_INDATE,PIB_INOUTNO,PIB_PIID,PIB_BARCODE,PIB_PDNO,");
  165. sql.Append("PIB_PDID,PIB_PICLASS,PIB_QTY,PIB_PRODID,PIB_OUTBOXCODE1,PIB_IFPRINT,PIB_IFPICK,PIB_ORDERCODE,PIB_CUSTPO,pib_orderdetno)");
  166. sql.Append(" values (prodiobarcode_seq.nextval,'" + pd_prodcode + "',sysdate,'" + pi_inoutno.Text + "'," + pi_id + ",:barcode,'" + pd_pdno + "','" + pd_id + "',");
  167. sql.Append("'" + pi_class.Text + "',:pib_inqty,'" + pr_id + "',:midcode,0,0,'" + pd_ordercode + "','','" + pd_orderdetno + "')");
  168. dh.BatchInsert(sql.ToString(), new string[] { "barcode", "pib_inqty", "midcode" }, barcode.ToArray(), pib_inqty.ToArray(), midcode.ToArray());
  169. }
  170. }
  171. if (CheckedRowCount > 0)
  172. {
  173. LoadData();
  174. MessageBox.Show("生成箱号成功!");
  175. }
  176. else
  177. {
  178. MessageBox.Show("未勾选需要生成的明细!");
  179. }
  180. //如果含有内容不符合的选项,进行提示
  181. string str = "";
  182. foreach (string ss in NotPass.Values)
  183. {
  184. str += ss + "\n";
  185. }
  186. if (str != "")
  187. MessageBox.Show(str);
  188. }
  189. //新的生成箱号的方法
  190. public string BarcodeMethod1(string pd_id, string pr_id, string pib_barcode)
  191. {
  192. if (pib_barcode != "")
  193. {
  194. if (FirstCode)
  195. {
  196. serialnum = serialnum + 1;
  197. }
  198. //第一次的时候去获取数据库查询出来的值
  199. else
  200. {
  201. serialnum = int.Parse(pib_barcode.Substring(pib_barcode.Length - 4)) + 1;
  202. FirstCode = true;
  203. }
  204. }
  205. else
  206. {
  207. serialnum = serialnum + 1;
  208. }
  209. string serialcode = serialnum.ToString();
  210. for (int i = serialnum.ToString().Length; i < 4; i++)
  211. {
  212. serialcode = "0" + serialcode;
  213. }
  214. return pd_id + "-" + pr_id + "-" + serialcode;
  215. }
  216. //生成箱号的方法
  217. public string BarcodeMethod(string pr_code, string ve_id, string pr_id)
  218. {
  219. StringBuilder code = new StringBuilder();
  220. DataTable dt1 = new DataTable();
  221. string date = "0";
  222. string[] field = { "bs_lenprid", "bs_datestr", "bs_lennum", "bs_maxnum", "bs_maxdate", "bs_lenveid" };
  223. dt1 = dh.getFieldsDataByCondition("barcodeSet", field, "bs_type='BATCH'");
  224. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenprid"].ToString()), pr_id));
  225. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenveid"].ToString()), ve_id));
  226. switch (dt1.Rows[0]["bs_datestr"].ToString())
  227. {
  228. case "YYMMDD":
  229. SimpleDateFormat YMD = new SimpleDateFormat("yyMMdd");
  230. date = YMD.Format(new DateTime());
  231. break;
  232. case "YYMM":
  233. SimpleDateFormat YM = new SimpleDateFormat("yyMM");
  234. date = YM.Format(new DateTime());
  235. break;
  236. case "MMDD":
  237. SimpleDateFormat MD = new SimpleDateFormat("MMdd");
  238. date = MD.Format(new DateTime());
  239. break;
  240. default:
  241. break;
  242. }
  243. code.Append(date);
  244. dh.UpdateByCondition("barcodeSet", "bs_maxdate='" + date + "'", "bs_type='BATCH'");
  245. if (!("").Equals(dt1.Rows[0]["bs_maxdate"]) && null != dt1.Rows[0]["bs_maxdate"] && (!date.Equals("0")) && (int.Parse(dt1.Rows[0]["bs_maxdate"].ToString()) > int.Parse(date)))
  246. {
  247. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), "1"));// 流水重新开始
  248. dh.UpdateByCondition("barcodeSet", "bs_maxnum=2", "bs_type='BATCH'");
  249. }
  250. else
  251. {
  252. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), dt1.Rows[0]["bs_maxnum"].ToString()));// 当前流水号
  253. dh.UpdateByCondition("barcodeSet", "bs_maxnum=bs_maxnum+1", "bs_type='BATCH'");// 流水号增加1
  254. }
  255. return code.ToString();
  256. }
  257. private static string lpad(int length, string number)
  258. {
  259. while (number.Length < length)
  260. {
  261. number = "0" + number;
  262. }
  263. number = number.Substring(number.Length - length, length);
  264. return number;
  265. }
  266. /// <summary>
  267. /// 重绘指定列的背景色
  268. /// </summary>
  269. /// <param name="sender"></param>
  270. /// <param name="e"></param>
  271. private void ProdIoInfDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  272. {
  273. bool mouseOver = e.CellBounds.Contains(this.PointToClient(Cursor.Position));
  274. if (e.ColumnIndex > 0)
  275. if (ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pr_zxbzs" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pd_qty" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "mid_qty")
  276. {
  277. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
  278. e.Graphics.FillRectangle(mouseOver ? solidBrush : Brushes.LightSeaGreen, e.CellBounds);
  279. Rectangle border = e.CellBounds;
  280. border.Width -= 1;
  281. e.Graphics.DrawRectangle(Pens.White, border);
  282. e.PaintContent(e.CellBounds);
  283. e.Handled = true;
  284. }
  285. }
  286. /// <summary>
  287. /// 计算中盘尾数的方法
  288. /// </summary>
  289. /// <param name="sender"></param>
  290. /// <param name="e"></param>
  291. private void ProdIoInfDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  292. {
  293. object pr_zxbzs = ProdIoInfDGV.Rows[e.RowIndex].Cells["pr_zxbzs"].Value;
  294. object pd_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_qty"].Value;
  295. object mid_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_qty"].Value;
  296. object pd_totalqty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_totalqty"].Value;
  297. if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
  298. {
  299. int 最小包装量 = int.Parse(pr_zxbzs.ToString());
  300. int 中盘容量 = int.Parse(mid_qty.ToString());
  301. int 本次数量 = int.Parse(pd_qty.ToString());
  302. int 总数 = int.Parse(pd_totalqty.ToString());
  303. if (最小包装量 > 0)
  304. {
  305. if (本次数量 > 总数)
  306. {
  307. MessageBox.Show("本次数量不能大于总数");
  308. return;
  309. }
  310. if (中盘容量 <= 0 || 最小包装量 <= 0)
  311. {
  312. MessageBox.Show("中盘容量和单盘数量必须是正整数");
  313. return;
  314. }
  315. //计算中盘数量
  316. int mid_num = 本次数量 / (最小包装量 * 中盘容量);
  317. //计算中盘尾数
  318. if (本次数量 % (最小包装量 * 中盘容量) == 0)
  319. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num;
  320. else
  321. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num + 1;
  322. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
  323. }
  324. }
  325. }
  326. //设置全部中盒容量
  327. private void SetMidCapacity_Click(object sender, EventArgs e)
  328. {
  329. for (int i = 0; i < ProdIoInfDGV.Rows.Count; i++)
  330. {
  331. ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value = MidCapacity.Text;
  332. if (ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].Value.ToString() != "" && ProdIoInfDGV.Rows[i].Cells["pd_qty"].Value.ToString() != "" && ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value.ToString() != "" && ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].Value.ToString() != "0")
  333. {
  334. object pr_zxbzs = ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].Value;
  335. object pd_qty = ProdIoInfDGV.Rows[i].Cells["pd_qty"].Value;
  336. object mid_qty = ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value;
  337. object pd_totalqty = ProdIoInfDGV.Rows[i].Cells["pd_totalqty"].Value;
  338. if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
  339. {
  340. int 最小包装量 = int.Parse(pr_zxbzs.ToString());
  341. int 中盘容量 = int.Parse(mid_qty.ToString());
  342. int 本次数量 = int.Parse(pd_qty.ToString());
  343. int 总数 = int.Parse(pd_totalqty.ToString());
  344. //计算中盘数量
  345. int mid_num = 本次数量 / (最小包装量 * 中盘容量);
  346. //计算中盘尾数
  347. if (本次数量 % (最小包装量 * 中盘容量) == 0)
  348. ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num;
  349. else
  350. ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num + 1;
  351. ProdIoInfDGV.Rows[i].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
  352. }
  353. }
  354. }
  355. }
  356. private void LoadData()
  357. {
  358. dt = (DataTable)dh.ExecuteSql("select pi_class,pi_id from prodinout where pi_inoutno='" + pi_inoutno.Text + "'", "select");
  359. if (dt.Rows.Count > 0)
  360. {
  361. pi_id = dt.Rows[0]["pi_id"].ToString();
  362. BaseUtil.SetFormValue(this.Controls, dt);
  363. //查询出入库的类型
  364. dt = (DataTable)dh.ExecuteSql("select ds_inorout from documentsetup where ds_name='" + pi_class.Text + "'", "select");
  365. string pd_inoroutqty = "";
  366. string pd_barcodeioqty = "";
  367. //判断出入库类型,查询的时候设置不同的字段
  368. if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "IN")
  369. {
  370. pd_inoroutqty = "pd_inqty";
  371. pd_barcodeioqty = "pd_barcodeinqty";
  372. }
  373. else if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "OUT")
  374. {
  375. pd_inoroutqty = "pd_outqty";
  376. pd_barcodeioqty = "pd_barcodeoutqty";
  377. }
  378. sql.Clear();
  379. sql.Append("select * from prodiobarcode_view where pd_piid='" + pi_id + "'");
  380. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  381. BaseUtil.FillDgvWithDataTable(ProdIoInfDGV, dt);
  382. }
  383. else
  384. {
  385. MessageBox.Show("当前单据不存在");
  386. pi_inoutno.Text = "";
  387. }
  388. }
  389. }
  390. }