生成条码.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. //总数
  101. int pd_totalqty = int.Parse(ProdIoInfDGV.Rows[i].Cells["pd_totalqty"].FormattedValue.ToString());
  102. //本次数量
  103. int pd_qty = int.Parse(ProdIoInfDGV.Rows[i].Cells["pd_qty"].FormattedValue.ToString());
  104. //最小包装数
  105. int pr_zxbzs = int.Parse(ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].FormattedValue.ToString());
  106. //中盒容量
  107. int mid_qty = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_qty"].FormattedValue.ToString());
  108. //如果中盘盒数量为1且有尾数则表示一箱未装满
  109. int mid_num = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_num"].FormattedValue.ToString());
  110. //中盒尾数
  111. int mid_remain = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_remain"].FormattedValue.ToString());
  112. //本次数量不能大于总数
  113. //if (pd_qty > pd_totalqty)
  114. //{
  115. // if (!NotPass.ContainsKey(pd_pdno + pd_prodcode))
  116. // {
  117. // NotPass.Add(pd_pdno + pd_prodcode, pd_prodcode + "本次数量不能大于总数");
  118. // }
  119. // CheckedRowCount--;
  120. // continue;
  121. //}
  122. ////后台查询已生成数量,本次数量不能大于已生成数量
  123. //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());
  124. //if (pd_qty > pd_totalqty - qty || pd_qty == 0)
  125. //{
  126. // if (!NotPass.ContainsKey(pd_pdno + pd_prodcode))
  127. // {
  128. // NotPass.Add(pd_pdno + pd_prodcode, "行号" + pd_pdno + ",物料号" + pd_prodcode + "已生成数量" + qty + ",剩余可以用数量" + (pd_totalqty - qty));
  129. // }
  130. // CheckedRowCount--;
  131. // continue;
  132. //}
  133. string pib_barcode = dh.getFieldDataByCondition("prodiobarcode", "max(pib_barcode)", "pib_pdid=" + pd_id).ToString();
  134. //中盒数量*中盒容量=需要打印的单盘标签
  135. ArrayList<string> midcode = new ArrayList<string>();
  136. ArrayList<string> barcode = new ArrayList<string>();
  137. //循环中盒号的个数,取当前出入库单最大的中盒号+1
  138. for (int j = 0; j < mid_num; j++)
  139. {
  140. //获取中盘的编号
  141. string mid_code = dh.getFieldDataByCondition("PRODIOBARCODE", "nvl(max(PIB_OUTBOXCODE1),0)+" + (j + 1), "PIB_INOUTNO='" + pi_inoutno.Text + "'").ToString();
  142. //如果尾数不为0,并且已经遍历到了最后一箱(未装满的箱)
  143. int count = 0;
  144. if (mid_remain != 0 && j + 1 == mid_num)
  145. {
  146. //剩下的尾数刚好够整数的最小包或者加上一个未装满的最小包
  147. count = mid_remain % pr_zxbzs == 0 ? mid_remain / pr_zxbzs : (mid_remain / pr_zxbzs) + 1;
  148. }
  149. else
  150. {
  151. //循环中盒的箱内容量
  152. count = mid_qty;
  153. }
  154. for (int k = 0; k < count; k++)
  155. {
  156. //将箱号添加进List
  157. barcode.Add(BarcodeMethod1(pd_id, pr_id, pib_barcode));
  158. midcode.Add(mid_code);
  159. pib_inqty.Add(pr_zxbzs.ToString());
  160. }
  161. }
  162. sql.Clear();
  163. sql.Append("insert into prodiobarcode (PIB_ID,PIB_PRODCODE,PIB_INDATE,PIB_INOUTNO,PIB_PIID,PIB_BARCODE,PIB_PDNO,");
  164. sql.Append("PIB_PDID,PIB_PICLASS,PIB_QTY,PIB_PRODID,PIB_OUTBOXCODE1,PIB_IFPRINT,PIB_IFPICK,PIB_ORDERCODE,PIB_CUSTPO)");
  165. sql.Append(" values (prodiobarcode_seq.nextval,'" + pd_prodcode + "',sysdate,'" + pi_inoutno.Text + "'," + pi_id + ",:barcode,'" + pd_pdno + "','" + pd_id + "',");
  166. sql.Append("'" + pi_class.Text + "',:pib_inqty,'" + pr_id + "',:midcode,0,0,'" + pd_ordercode + "','')");
  167. dh.BatchInsert(sql.ToString(), new string[] { "barcode", "pib_inqty", "midcode" }, barcode.ToArray(), pib_inqty.ToArray(), midcode.ToArray());
  168. }
  169. }
  170. if (CheckedRowCount > 0)
  171. {
  172. LoadData();
  173. MessageBox.Show("生成箱号成功!");
  174. }
  175. //如果含有内容不符合的选项,进行提示
  176. string str = "";
  177. foreach (string ss in NotPass.Values)
  178. {
  179. str += ss + "\n";
  180. }
  181. if (str != "")
  182. MessageBox.Show(str);
  183. }
  184. //新的生成箱号的方法
  185. public string BarcodeMethod1(string pd_id, string pr_id, string pib_barcode)
  186. {
  187. if (pib_barcode != "")
  188. {
  189. if (FirstCode)
  190. {
  191. serialnum = serialnum + 1;
  192. }
  193. //第一次的时候去获取数据库查询出来的值
  194. else
  195. {
  196. serialnum = int.Parse(pib_barcode.Substring(pib_barcode.Length - 4)) + 1;
  197. FirstCode = true;
  198. }
  199. }
  200. else
  201. {
  202. serialnum = serialnum + 1;
  203. }
  204. string serialcode = serialnum.ToString();
  205. for (int i = serialnum.ToString().Length; i < 4; i++)
  206. {
  207. serialcode = "0" + serialcode;
  208. }
  209. return pd_id + "-" + pr_id + "-" + serialcode;
  210. }
  211. //生成箱号的方法
  212. public string BarcodeMethod(string pr_code, string ve_id, string pr_id)
  213. {
  214. StringBuilder code = new StringBuilder();
  215. DataTable dt1 = new DataTable();
  216. string date = "0";
  217. string[] field = { "bs_lenprid", "bs_datestr", "bs_lennum", "bs_maxnum", "bs_maxdate", "bs_lenveid" };
  218. dt1 = dh.getFieldsDataByCondition("barcodeSet", field, "bs_type='BATCH'");
  219. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenprid"].ToString()), pr_id));
  220. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenveid"].ToString()), ve_id));
  221. switch (dt1.Rows[0]["bs_datestr"].ToString())
  222. {
  223. case "YYMMDD":
  224. SimpleDateFormat YMD = new SimpleDateFormat("yyMMdd");
  225. date = YMD.Format(new DateTime());
  226. break;
  227. case "YYMM":
  228. SimpleDateFormat YM = new SimpleDateFormat("yyMM");
  229. date = YM.Format(new DateTime());
  230. break;
  231. case "MMDD":
  232. SimpleDateFormat MD = new SimpleDateFormat("MMdd");
  233. date = MD.Format(new DateTime());
  234. break;
  235. default:
  236. break;
  237. }
  238. code.Append(date);
  239. dh.UpdateByCondition("barcodeSet", "bs_maxdate='" + date + "'", "bs_type='BATCH'");
  240. 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)))
  241. {
  242. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), "1"));// 流水重新开始
  243. dh.UpdateByCondition("barcodeSet", "bs_maxnum=2", "bs_type='BATCH'");
  244. }
  245. else
  246. {
  247. code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), dt1.Rows[0]["bs_maxnum"].ToString()));// 当前流水号
  248. dh.UpdateByCondition("barcodeSet", "bs_maxnum=bs_maxnum+1", "bs_type='BATCH'");// 流水号增加1
  249. }
  250. return code.ToString();
  251. }
  252. private static string lpad(int length, string number)
  253. {
  254. while (number.Length < length)
  255. {
  256. number = "0" + number;
  257. }
  258. number = number.Substring(number.Length - length, length);
  259. return number;
  260. }
  261. /// <summary>
  262. /// 重绘指定列的背景色
  263. /// </summary>
  264. /// <param name="sender"></param>
  265. /// <param name="e"></param>
  266. private void ProdIoInfDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  267. {
  268. bool mouseOver = e.CellBounds.Contains(this.PointToClient(Cursor.Position));
  269. if (e.ColumnIndex > 0)
  270. if (ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pr_zxbzs" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pd_qty" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "mid_qty")
  271. {
  272. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
  273. e.Graphics.FillRectangle(mouseOver ? solidBrush : Brushes.LightSeaGreen, e.CellBounds);
  274. Rectangle border = e.CellBounds;
  275. border.Width -= 1;
  276. e.Graphics.DrawRectangle(Pens.White, border);
  277. e.PaintContent(e.CellBounds);
  278. e.Handled = true;
  279. }
  280. }
  281. /// <summary>
  282. /// 计算中盘尾数的方法
  283. /// </summary>
  284. /// <param name="sender"></param>
  285. /// <param name="e"></param>
  286. private void ProdIoInfDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  287. {
  288. object pr_zxbzs = ProdIoInfDGV.Rows[e.RowIndex].Cells["pr_zxbzs"].Value;
  289. object pd_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_qty"].Value;
  290. object mid_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_qty"].Value;
  291. object pd_totalqty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_totalqty"].Value;
  292. if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
  293. {
  294. int 最小包装量 = int.Parse(pr_zxbzs.ToString());
  295. int 中盘容量 = int.Parse(mid_qty.ToString());
  296. int 本次数量 = int.Parse(pd_qty.ToString());
  297. int 总数 = int.Parse(pd_totalqty.ToString());
  298. if (最小包装量 > 0)
  299. {
  300. if (本次数量 > 总数)
  301. {
  302. MessageBox.Show("本次数量不能大于总数");
  303. return;
  304. }
  305. if (中盘容量 <= 0 || 最小包装量 <= 0)
  306. {
  307. MessageBox.Show("中盘容量和单盘数量必须是正整数");
  308. return;
  309. }
  310. //计算中盘数量
  311. int mid_num = 本次数量 / (最小包装量 * 中盘容量);
  312. //计算中盘尾数
  313. if (本次数量 % (最小包装量 * 中盘容量) == 0)
  314. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num;
  315. else
  316. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num + 1;
  317. ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
  318. }
  319. }
  320. }
  321. //设置全部中盒容量
  322. private void SetMidCapacity_Click(object sender, EventArgs e)
  323. {
  324. for (int i = 0; i < ProdIoInfDGV.Rows.Count; i++)
  325. {
  326. ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value = MidCapacity.Text;
  327. 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")
  328. {
  329. object pr_zxbzs = ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].Value;
  330. object pd_qty = ProdIoInfDGV.Rows[i].Cells["pd_qty"].Value;
  331. object mid_qty = ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value;
  332. object pd_totalqty = ProdIoInfDGV.Rows[i].Cells["pd_totalqty"].Value;
  333. if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
  334. {
  335. int 最小包装量 = int.Parse(pr_zxbzs.ToString());
  336. int 中盘容量 = int.Parse(mid_qty.ToString());
  337. int 本次数量 = int.Parse(pd_qty.ToString());
  338. int 总数 = int.Parse(pd_totalqty.ToString());
  339. //计算中盘数量
  340. int mid_num = 本次数量 / (最小包装量 * 中盘容量);
  341. //计算中盘尾数
  342. if (本次数量 % (最小包装量 * 中盘容量) == 0)
  343. ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num;
  344. else
  345. ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num + 1;
  346. ProdIoInfDGV.Rows[i].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
  347. }
  348. }
  349. }
  350. }
  351. private void LoadData()
  352. {
  353. dt = (DataTable)dh.ExecuteSql("select pi_class,pi_id from prodinout where pi_inoutno='" + pi_inoutno.Text + "'", "select");
  354. if (dt.Rows.Count > 0)
  355. {
  356. pi_id = dt.Rows[0]["pi_id"].ToString();
  357. BaseUtil.SetFormValue(this.Controls, dt);
  358. //查询出入库的类型
  359. dt = (DataTable)dh.ExecuteSql("select ds_inorout from documentsetup where ds_name='" + pi_class.Text + "'", "select");
  360. string pd_inoroutqty = "";
  361. string pd_barcodeioqty = "";
  362. //判断出入库类型,查询的时候设置不同的字段
  363. if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "IN")
  364. {
  365. pd_inoroutqty = "pd_inqty";
  366. pd_barcodeioqty = "pd_barcodeinqty";
  367. }
  368. else if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "OUT")
  369. {
  370. pd_inoroutqty = "pd_outqty";
  371. pd_barcodeioqty = "pd_barcodeoutqty";
  372. }
  373. sql.Clear();
  374. sql.Append("select pd_piid,pd_id,pd_pdno,pd_prodcode," + pd_inoroutqty + " as pd_totalqty,pr_zxbzs,pr_brand,pr_id,");
  375. sql.Append(pd_inoroutqty + "-nvl((select sum(nvl(pib_qty,0)) from PRODIOBARCODE where PIB_PIID='" + pi_id + "' and pib_pdid=pd_id ");
  376. sql.Append("and pib_prodcode=pd_prodcode),0) as pd_qty,pr_detail,pr_spec,pd_ordercode,pr_unit from prodiodetail ");
  377. sql.Append("left join product on pr_code=pd_prodcode where pd_piid=" + pi_id + Condition + " and ");
  378. sql.Append(pd_inoroutqty + "-nvl(" + pd_barcodeioqty + ",0)>0 ");
  379. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  380. BaseUtil.FillDgvWithDataTable(ProdIoInfDGV, dt);
  381. }
  382. else
  383. {
  384. MessageBox.Show("当前单据不存在");
  385. pi_inoutno.Text = "";
  386. }
  387. }
  388. }
  389. }