生成条码.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. //如果含有内容不符合的选项,进行提示
  177. string str = "";
  178. foreach (string ss in NotPass.Values)
  179. {
  180. str += ss + "\n";
  181. }
  182. if (str != "")
  183. MessageBox.Show(str);
  184. }
  185. //新的生成箱号的方法
  186. public string BarcodeMethod1(string pd_id, string pr_id, string pib_barcode)
  187. {
  188. if (pib_barcode != "")
  189. {
  190. if (FirstCode)
  191. {
  192. serialnum = serialnum + 1;
  193. }
  194. //第一次的时候去获取数据库查询出来的值
  195. else
  196. {
  197. serialnum = int.Parse(pib_barcode.Substring(pib_barcode.Length - 4)) + 1;
  198. FirstCode = true;
  199. }
  200. }
  201. else
  202. {
  203. serialnum = serialnum + 1;
  204. }
  205. string serialcode = serialnum.ToString();
  206. for (int i = serialnum.ToString().Length; i < 4; i++)
  207. {
  208. serialcode = "0" + serialcode;
  209. }
  210. return pd_id + "-" + pr_id + "-" + serialcode;
  211. }
  212. //生成箱号的方法
  213. public string BarcodeMethod(string pr_code, string ve_id, string pr_id)
  214. {
  215. StringBuilder code = new StringBuilder();
  216. DataTable dt1 = new DataTable();
  217. string date = "0";
  218. string[] field = { "bs_lenprid", "bs_datestr", "bs_lennum", "bs_maxnum", "bs_maxdate", "bs_lenveid" };
  219. dt1 = dh.getFieldsDataByCondition("barcodeSet", field, "bs_type='BATCH'");
  220. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenprid"].ToString()), pr_id));
  221. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenveid"].ToString()), ve_id));
  222. switch (dt1.Rows[0]["bs_datestr"].ToString())
  223. {
  224. case "YYMMDD":
  225. SimpleDateFormat YMD = new SimpleDateFormat("yyMMdd");
  226. date = YMD.Format(new DateTime());
  227. break;
  228. case "YYMM":
  229. SimpleDateFormat YM = new SimpleDateFormat("yyMM");
  230. date = YM.Format(new DateTime());
  231. break;
  232. case "MMDD":
  233. SimpleDateFormat MD = new SimpleDateFormat("MMdd");
  234. date = MD.Format(new DateTime());
  235. break;
  236. default:
  237. break;
  238. }
  239. code.Append(date);
  240. dh.UpdateByCondition("barcodeSet", "bs_maxdate='" + date + "'", "bs_type='BATCH'");
  241. 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)))
  242. {
  243. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), "1"));// 流水重新开始
  244. dh.UpdateByCondition("barcodeSet", "bs_maxnum=2", "bs_type='BATCH'");
  245. }
  246. else
  247. {
  248. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), dt1.Rows[0]["bs_maxnum"].ToString()));// 当前流水号
  249. dh.UpdateByCondition("barcodeSet", "bs_maxnum=bs_maxnum+1", "bs_type='BATCH'");// 流水号增加1
  250. }
  251. return code.ToString();
  252. }
  253. private static string lpad(int length, string number)
  254. {
  255. while (number.Length < length)
  256. {
  257. number = "0" + number;
  258. }
  259. number = number.Substring(number.Length - length, length);
  260. return number;
  261. }
  262. /// <summary>
  263. /// 重绘指定列的背景色
  264. /// </summary>
  265. /// <param name="sender"></param>
  266. /// <param name="e"></param>
  267. private void ProdIoInfDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  268. {
  269. bool mouseOver = e.CellBounds.Contains(this.PointToClient(Cursor.Position));
  270. if (e.ColumnIndex > 0)
  271. if (ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pr_zxbzs" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pd_qty" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "mid_qty")
  272. {
  273. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
  274. e.Graphics.FillRectangle(mouseOver ? solidBrush : Brushes.LightSeaGreen, e.CellBounds);
  275. Rectangle border = e.CellBounds;
  276. border.Width -= 1;
  277. e.Graphics.DrawRectangle(Pens.White, border);
  278. e.PaintContent(e.CellBounds);
  279. e.Handled = true;
  280. }
  281. }
  282. /// <summary>
  283. /// 计算中盘尾数的方法
  284. /// </summary>
  285. /// <param name="sender"></param>
  286. /// <param name="e"></param>
  287. private void ProdIoInfDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  288. {
  289. object pr_zxbzs = ProdIoInfDGV.Rows[e.RowIndex].Cells["pr_zxbzs"].Value;
  290. object pd_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_qty"].Value;
  291. object mid_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_qty"].Value;
  292. object pd_totalqty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_totalqty"].Value;
  293. if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
  294. {
  295. int 最小包装量 = int.Parse(pr_zxbzs.ToString());
  296. int 中盘容量 = int.Parse(mid_qty.ToString());
  297. int 本次数量 = int.Parse(pd_qty.ToString());
  298. int 总数 = int.Parse(pd_totalqty.ToString());
  299. if (最小包装量 > 0)
  300. {
  301. if (本次数量 > 总数)
  302. {
  303. MessageBox.Show("本次数量不能大于总数");
  304. return;
  305. }
  306. if (中盘容量 <= 0 || 最小包装量 <= 0)
  307. {
  308. MessageBox.Show("中盘容量和单盘数量必须是正整数");
  309. return;
  310. }
  311. //计算中盘数量
  312. int mid_num = 本次数量 / (最小包装量 * 中盘容量);
  313. //计算中盘尾数
  314. if (本次数量 % (最小包装量 * 中盘容量) == 0)
  315. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num;
  316. else
  317. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num + 1;
  318. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
  319. }
  320. }
  321. }
  322. //设置全部中盒容量
  323. private void SetMidCapacity_Click(object sender, EventArgs e)
  324. {
  325. for (int i = 0; i < ProdIoInfDGV.Rows.Count; i++)
  326. {
  327. ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value = MidCapacity.Text;
  328. 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")
  329. {
  330. object pr_zxbzs = ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].Value;
  331. object pd_qty = ProdIoInfDGV.Rows[i].Cells["pd_qty"].Value;
  332. object mid_qty = ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value;
  333. object pd_totalqty = ProdIoInfDGV.Rows[i].Cells["pd_totalqty"].Value;
  334. if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
  335. {
  336. int 最小包装量 = int.Parse(pr_zxbzs.ToString());
  337. int 中盘容量 = int.Parse(mid_qty.ToString());
  338. int 本次数量 = int.Parse(pd_qty.ToString());
  339. int 总数 = int.Parse(pd_totalqty.ToString());
  340. //计算中盘数量
  341. int mid_num = 本次数量 / (最小包装量 * 中盘容量);
  342. //计算中盘尾数
  343. if (本次数量 % (最小包装量 * 中盘容量) == 0)
  344. ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num;
  345. else
  346. ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num + 1;
  347. ProdIoInfDGV.Rows[i].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
  348. }
  349. }
  350. }
  351. }
  352. private void LoadData()
  353. {
  354. dt = (DataTable)dh.ExecuteSql("select pi_class,pi_id from prodinout where pi_inoutno='" + pi_inoutno.Text + "'", "select");
  355. if (dt.Rows.Count > 0)
  356. {
  357. pi_id = dt.Rows[0]["pi_id"].ToString();
  358. BaseUtil.SetFormValue(this.Controls, dt);
  359. //查询出入库的类型
  360. dt = (DataTable)dh.ExecuteSql("select ds_inorout from documentsetup where ds_name='" + pi_class.Text + "'", "select");
  361. string pd_inoroutqty = "";
  362. string pd_barcodeioqty = "";
  363. //判断出入库类型,查询的时候设置不同的字段
  364. if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "IN")
  365. {
  366. pd_inoroutqty = "pd_inqty";
  367. pd_barcodeioqty = "pd_barcodeinqty";
  368. }
  369. else if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "OUT")
  370. {
  371. pd_inoroutqty = "pd_outqty";
  372. pd_barcodeioqty = "pd_barcodeoutqty";
  373. }
  374. sql.Clear();
  375. sql.Append("select * from prodiobarcode_view where pd_piid='" + pi_id + "'");
  376. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  377. BaseUtil.FillDgvWithDataTable(ProdIoInfDGV, dt);
  378. }
  379. else
  380. {
  381. MessageBox.Show("当前单据不存在");
  382. pi_inoutno.Text = "";
  383. }
  384. }
  385. }
  386. }