| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- using NPOI.SS.Util;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using UAS_LabelMachine.PublicMethod;
- namespace UAS_LabelMachine
- {
- public partial class 生成条码 : Form
- {
- AutoSizeFormClass asc = new AutoSizeFormClass();
- DataHelper dh;
- DataTable dt;
- StringBuilder sql = new StringBuilder();
- string pi_id;
- //用于提示超出数量的物料
- Dictionary<string, string> NotPass = new Dictionary<string, string>();
- string Condition = "";
- //生成条码的流水号
- int serialnum = 0;
- //是否生成过条码
- bool FirstCode = false;
- public 生成条码(string PI_INOUTNO)
- {
- InitializeComponent();
- pi_inoutno.Text = PI_INOUTNO;
- }
- private void 生成条码_Load(object sender, EventArgs e)
- {
- dh = new DataHelper();
- ChooseAll.ChooseAll(ProdIoInfDGV);
- //如果传进了出入库单号则默认执行一次取数据
- if (pi_inoutno.Text != "")
- {
- KeyEventArgs e2 = new KeyEventArgs(Keys.Enter);
- bi_inoutno_KeyDown(sender, e2);
- }
- pr_kind.Text = "全部";
- asc.controllInitializeSize(this);
- Width = Width - 1;
- }
- private void bi_inoutno_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- LoadData();
- }
- }
- private void 生成条码_SizeChanged(object sender, EventArgs e)
- {
- asc.controlAutoSize(this);
- }
- /// <summary>
- /// 筛选按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Screen_Click(object sender, EventArgs e)
- {
- switch (pr_kind.Text)
- {
- case "全部":
- Condition = "";
- break;
- case "单件管控":
- Condition = " and pr_tracekind=1";
- break;
- case "批管控":
- Condition = " and pr_tracekind=2";
- break;
- default:
- break;
- }
- KeyEventArgs e2 = new KeyEventArgs(Keys.Enter);
- bi_inoutno_KeyDown(sender, e2);
- }
- /// <summary>
- /// 生成条码
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void GenerateBarCode_Click(object sender, EventArgs e)
- {
- //有错误需要提醒的内容
- int CheckedRowCount = 0;
- //遍历整个Grid,勾选的项目全部进行条码生成
- for (int i = 0; i < ProdIoInfDGV.RowCount; i++)
- {
- if (ProdIoInfDGV.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
- {
- CheckedRowCount++;
- List<string> pib_inqty = new List<string>();
- string pd_id = ProdIoInfDGV.Rows[i].Cells["pd_id"].FormattedValue.ToString();
- string pd_prodcode = ProdIoInfDGV.Rows[i].Cells["pd_prodcode"].FormattedValue.ToString();
- string pr_id = ProdIoInfDGV.Rows[i].Cells["pr_id"].FormattedValue.ToString();
- string pd_ordercode = ProdIoInfDGV.Rows[i].Cells["pd_ordercode"].FormattedValue.ToString();
- string pd_pdno = ProdIoInfDGV.Rows[i].Cells["pd_pdno"].FormattedValue.ToString();
- string pd_orderdetno = ProdIoInfDGV.Rows[i].Cells["pd_orderdetno"].FormattedValue.ToString();
- //总数
- int pd_totalqty = int.Parse(ProdIoInfDGV.Rows[i].Cells["pd_totalqty"].FormattedValue.ToString());
- //本次数量
- int pd_qty = int.Parse(ProdIoInfDGV.Rows[i].Cells["pd_qty"].FormattedValue.ToString());
- //最小包装数
- int pr_zxbzs = int.Parse(ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].FormattedValue.ToString());
- //中盒容量
- int mid_qty = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_qty"].FormattedValue.ToString());
- //如果中盘盒数量为1且有尾数则表示一箱未装满
- int mid_num = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_num"].FormattedValue.ToString());
- //中盒尾数
- int mid_remain = int.Parse(ProdIoInfDGV.Rows[i].Cells["mid_remain"].FormattedValue.ToString());
- //本次数量不能大于总数
- //if (pd_qty > pd_totalqty)
- //{
- // if (!NotPass.ContainsKey(pd_pdno + pd_prodcode))
- // {
- // NotPass.Add(pd_pdno + pd_prodcode, pd_prodcode + "本次数量不能大于总数");
- // }
- // CheckedRowCount--;
- // continue;
- //}
- ////后台查询已生成数量,本次数量不能大于已生成数量
- //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());
- //if (pd_qty > pd_totalqty - qty || pd_qty == 0)
- //{
- // if (!NotPass.ContainsKey(pd_pdno + pd_prodcode))
- // {
- // NotPass.Add(pd_pdno + pd_prodcode, "行号" + pd_pdno + ",物料号" + pd_prodcode + "已生成数量" + qty + ",剩余可以用数量" + (pd_totalqty - qty));
- // }
- // CheckedRowCount--;
- // continue;
- //}
- string pib_barcode = dh.getFieldDataByCondition("prodiobarcode", "max(pib_barcode)", "PIB_INOUTNO='" + pi_inoutno.Text + "'").ToString();
- //中盒数量*中盒容量=需要打印的单盘标签
- ArrayList<string> midcode = new ArrayList<string>();
- ArrayList<string> barcode = new ArrayList<string>();
- //循环中盒号的个数,取当前出入库单最大 的中盒号+1
- for (int j = 0; j < mid_num; j++)
- {
- //获取中盘的编号
- string mid_code = dh.getFieldDataByCondition("PRODIOBARCODE", "nvl(max(to_number(PIB_OUTBOXCODE1)),0)+" + (j + 1), "PIB_INOUTNO='" + pi_inoutno.Text + "'").ToString();
- //如果尾数不为0,并且已经遍历到了最后一箱(未装满的箱)
- int count = 0;
- if (mid_remain != 0 && j + 1 == mid_num)
- {
- //剩下的尾数刚好够整数的最小包或者加上一个未装满的最小包
- count = mid_remain % pr_zxbzs == 0 ? mid_remain / pr_zxbzs : (mid_remain / pr_zxbzs) + 1;
- }
- else
- {
- //循环中盒的箱内容量
- count = mid_qty;
- }
- for (int k = 0; k < count; k++)
- {
- //将箱号添加进List
- barcode.Add(BarcodeMethod1(pd_id, pr_id, pib_barcode));
- midcode.Add(mid_code);
- pib_inqty.Add(pr_zxbzs.ToString());
- }
- }
- sql.Clear();
- sql.Append("insert into prodiobarcode (PIB_ID,PIB_PRODCODE,PIB_INDATE,PIB_INOUTNO,PIB_PIID,PIB_BARCODE,PIB_PDNO,");
- sql.Append("PIB_PDID,PIB_PICLASS,PIB_QTY,PIB_PRODID,PIB_OUTBOXCODE1,PIB_IFPRINT,PIB_IFPICK,PIB_ORDERCODE,PIB_CUSTPO,pib_orderdetno)");
- sql.Append(" values (prodiobarcode_seq.nextval,'" + pd_prodcode + "',sysdate,'" + pi_inoutno.Text + "'," + pi_id + ",:barcode,'" + pd_pdno + "','" + pd_id + "',");
- sql.Append("'" + pi_class.Text + "',:pib_inqty,'" + pr_id + "',:midcode,0,0,'" + pd_ordercode + "','','" + pd_orderdetno + "')");
- dh.BatchInsert(sql.ToString(), new string[] { "barcode", "pib_inqty", "midcode" }, barcode.ToArray(), pib_inqty.ToArray(), midcode.ToArray());
- }
- }
- if (CheckedRowCount > 0)
- {
- LoadData();
- MessageBox.Show("生成箱号成功!");
- }
- //如果含有内容不符合的选项,进行提示
- string str = "";
- foreach (string ss in NotPass.Values)
- {
- str += ss + "\n";
- }
- if (str != "")
- MessageBox.Show(str);
- }
- //新的生成箱号的方法
- public string BarcodeMethod1(string pd_id, string pr_id, string pib_barcode)
- {
- if (pib_barcode != "")
- {
- if (FirstCode)
- {
- serialnum = serialnum + 1;
- }
- //第一次的时候去获取数据库查询出来的值
- else
- {
- serialnum = int.Parse(pib_barcode.Substring(pib_barcode.Length - 4)) + 1;
- FirstCode = true;
- }
- }
- else
- {
- serialnum = serialnum + 1;
- }
- string serialcode = serialnum.ToString();
- for (int i = serialnum.ToString().Length; i < 4; i++)
- {
- serialcode = "0" + serialcode;
- }
- return pd_id + "-" + pr_id + "-" + serialcode;
- }
- //生成箱号的方法
- public string BarcodeMethod(string pr_code, string ve_id, string pr_id)
- {
- StringBuilder code = new StringBuilder();
- DataTable dt1 = new DataTable();
- string date = "0";
- string[] field = { "bs_lenprid", "bs_datestr", "bs_lennum", "bs_maxnum", "bs_maxdate", "bs_lenveid" };
- dt1 = dh.getFieldsDataByCondition("barcodeSet", field, "bs_type='BATCH'");
- code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenprid"].ToString()), pr_id));
- code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lenveid"].ToString()), ve_id));
- switch (dt1.Rows[0]["bs_datestr"].ToString())
- {
- case "YYMMDD":
- SimpleDateFormat YMD = new SimpleDateFormat("yyMMdd");
- date = YMD.Format(new DateTime());
- break;
- case "YYMM":
- SimpleDateFormat YM = new SimpleDateFormat("yyMM");
- date = YM.Format(new DateTime());
- break;
- case "MMDD":
- SimpleDateFormat MD = new SimpleDateFormat("MMdd");
- date = MD.Format(new DateTime());
- break;
- default:
- break;
- }
- code.Append(date);
- dh.UpdateByCondition("barcodeSet", "bs_maxdate='" + date + "'", "bs_type='BATCH'");
- 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)))
- {
- code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), "1"));// 流水重新开始
- dh.UpdateByCondition("barcodeSet", "bs_maxnum=2", "bs_type='BATCH'");
- }
- else
- {
- code.Append(lpad(int.Parse(dt1.Rows[0]["bs_lennum"].ToString()), dt1.Rows[0]["bs_maxnum"].ToString()));// 当前流水号
- dh.UpdateByCondition("barcodeSet", "bs_maxnum=bs_maxnum+1", "bs_type='BATCH'");// 流水号增加1
- }
- return code.ToString();
- }
- private static string lpad(int length, string number)
- {
- while (number.Length < length)
- {
- number = "0" + number;
- }
- number = number.Substring(number.Length - length, length);
- return number;
- }
- /// <summary>
- /// 重绘指定列的背景色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ProdIoInfDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
- {
- bool mouseOver = e.CellBounds.Contains(this.PointToClient(Cursor.Position));
- if (e.ColumnIndex > 0)
- if (ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pr_zxbzs" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "pd_qty" || ProdIoInfDGV.Columns[e.ColumnIndex].Name == "mid_qty")
- {
- SolidBrush solidBrush = new SolidBrush(Color.FromArgb(51, 153, 255));
- e.Graphics.FillRectangle(mouseOver ? solidBrush : Brushes.LightSeaGreen, e.CellBounds);
- Rectangle border = e.CellBounds;
- border.Width -= 1;
- e.Graphics.DrawRectangle(Pens.White, border);
- e.PaintContent(e.CellBounds);
- e.Handled = true;
- }
- }
- /// <summary>
- /// 计算中盘尾数的方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ProdIoInfDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
- {
- object pr_zxbzs = ProdIoInfDGV.Rows[e.RowIndex].Cells["pr_zxbzs"].Value;
- object pd_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_qty"].Value;
- object mid_qty = ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_qty"].Value;
- object pd_totalqty = ProdIoInfDGV.Rows[e.RowIndex].Cells["pd_totalqty"].Value;
- if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
- {
- int 最小包装量 = int.Parse(pr_zxbzs.ToString());
- int 中盘容量 = int.Parse(mid_qty.ToString());
- int 本次数量 = int.Parse(pd_qty.ToString());
- int 总数 = int.Parse(pd_totalqty.ToString());
- if (最小包装量 > 0)
- {
- if (本次数量 > 总数)
- {
- MessageBox.Show("本次数量不能大于总数");
- return;
- }
- if (中盘容量 <= 0 || 最小包装量 <= 0)
- {
- MessageBox.Show("中盘容量和单盘数量必须是正整数");
- return;
- }
- //计算中盘数量
- int mid_num = 本次数量 / (最小包装量 * 中盘容量);
- //计算中盘尾数
- if (本次数量 % (最小包装量 * 中盘容量) == 0)
- ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num;
- else
- ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_num"].Value = mid_num + 1;
- ProdIoInfDGV.Rows[e.RowIndex].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
- }
- }
- }
- //设置全部中盒容量
- private void SetMidCapacity_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < ProdIoInfDGV.Rows.Count; i++)
- {
- ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value = MidCapacity.Text;
- 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")
- {
- object pr_zxbzs = ProdIoInfDGV.Rows[i].Cells["pr_zxbzs"].Value;
- object pd_qty = ProdIoInfDGV.Rows[i].Cells["pd_qty"].Value;
- object mid_qty = ProdIoInfDGV.Rows[i].Cells["mid_qty"].Value;
- object pd_totalqty = ProdIoInfDGV.Rows[i].Cells["pd_totalqty"].Value;
- if (pr_zxbzs != null && pd_qty != null && mid_qty != null)
- {
- int 最小包装量 = int.Parse(pr_zxbzs.ToString());
- int 中盘容量 = int.Parse(mid_qty.ToString());
- int 本次数量 = int.Parse(pd_qty.ToString());
- int 总数 = int.Parse(pd_totalqty.ToString());
- //计算中盘数量
- int mid_num = 本次数量 / (最小包装量 * 中盘容量);
- //计算中盘尾数
- if (本次数量 % (最小包装量 * 中盘容量) == 0)
- ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num;
- else
- ProdIoInfDGV.Rows[i].Cells["mid_num"].Value = mid_num + 1;
- ProdIoInfDGV.Rows[i].Cells["mid_remain"].Value = 本次数量 - mid_num * 最小包装量 * 中盘容量;
- }
- }
- }
- }
- private void LoadData()
- {
- dt = (DataTable)dh.ExecuteSql("select pi_class,pi_id from prodinout where pi_inoutno='" + pi_inoutno.Text + "'", "select");
- if (dt.Rows.Count > 0)
- {
- pi_id = dt.Rows[0]["pi_id"].ToString();
- BaseUtil.SetFormValue(this.Controls, dt);
- //查询出入库的类型
- dt = (DataTable)dh.ExecuteSql("select ds_inorout from documentsetup where ds_name='" + pi_class.Text + "'", "select");
- string pd_inoroutqty = "";
- string pd_barcodeioqty = "";
- //判断出入库类型,查询的时候设置不同的字段
- if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "IN")
- {
- pd_inoroutqty = "pd_inqty";
- pd_barcodeioqty = "pd_barcodeinqty";
- }
- else if (dt.Rows[0]["ds_inorout"].ToString().ToUpper() == "OUT")
- {
- pd_inoroutqty = "pd_outqty";
- pd_barcodeioqty = "pd_barcodeoutqty";
- }
- sql.Clear();
- sql.Append("select * from prodiobarcode_view where pd_piid='" + pi_id + "'");
- dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
- BaseUtil.FillDgvWithDataTable(ProdIoInfDGV, dt);
- }
- else
- {
- MessageBox.Show("当前单据不存在");
- pi_inoutno.Text = "";
- }
- }
- }
- }
|