库存条码打印.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. using LabelManager2;
  2. using Seagull.BarTender.Print;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Printing;
  8. using System.Linq;
  9. using System.Media;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using static System.Runtime.CompilerServices.RuntimeHelpers;
  13. namespace UAS_BARCODEIO
  14. {
  15. public partial class 库存条码打印 : Form
  16. {
  17. ApplicationClass lbl;
  18. DataHelper dh = new DataHelper();
  19. Document SingleDoc;
  20. Document OutDoc;
  21. DataTable Dbfind;
  22. //自适应屏幕
  23. AutoSizeFormClass asc = new AutoSizeFormClass();
  24. public 库存条码打印()
  25. {
  26. InitializeComponent();
  27. }
  28. delegate void BindDataSource(DataGridView dgv, DataTable dt);//定义委托
  29. void bindingsource(DataGridView dgv, DataTable dt)
  30. {
  31. //dgv.AutoGenerateColumns = false;
  32. //dgv.DataSource = null;
  33. //dgv.DataSource = dt;
  34. if (dgv.InvokeRequired)
  35. {
  36. dgv.Invoke(new BindDataSource(bindingsource), new object[] { dgv, dt });
  37. }
  38. else
  39. {
  40. dgv.AutoGenerateColumns = false;
  41. dgv.DataSource = dt;
  42. }
  43. }
  44. private void Form1_Load(object sender, EventArgs e)
  45. {
  46. pr_code.TableName = "product";
  47. pr_code.SelectField = "pr_code # 物料编号 ,pr_orispeccode # 原厂型号 ,pr_brand # 品牌,pr_zxbzs # 最小包装";
  48. pr_code.FormName = Name;
  49. pr_code.DBTitle = "物料资料查询";
  50. pr_code.SetValueField = new string[] { "pr_code", "pr_orispeccode", "pr_brand", "pr_zxbzs" };
  51. pr_code.Condition = "";
  52. pr_code.DbChange += nr_rule_DBChange;
  53. SystemInf.dh = dh;
  54. try
  55. {
  56. lbl = new ApplicationClass();
  57. }
  58. catch (Exception ex)
  59. {
  60. Console.WriteLine(ex.Message);
  61. throw;
  62. }
  63. SingleDoc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\BARCODE.lab");
  64. OutDoc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\BOXCODESTOCK.lab");
  65. asc.controllInitializeSize(this);
  66. asc.controlAutoSize(this);
  67. PrintDocument print = new PrintDocument();
  68. string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
  69. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  70. {
  71. PrinterList.Items.Add(sPrint);
  72. if (sPrint == sDefault)
  73. PrinterList.SelectedIndex = PrinterList.Items.IndexOf(sPrint);
  74. }
  75. }
  76. private void nr_rule_DBChange(object sender, EventArgs e)
  77. {
  78. Dbfind = pr_code.ReturnData;
  79. BaseUtil.SetFormValue(this.Controls, Dbfind);
  80. }
  81. private void pi_inoutnokeydown(object sender, KeyEventArgs e)
  82. {
  83. if (e.KeyCode == Keys.Enter)
  84. {
  85. }
  86. }
  87. private void Barcode_KeyDown(object sender, KeyEventArgs e)
  88. {
  89. if (e.KeyCode == Keys.Enter)
  90. {
  91. DataTable dt = (DataTable)dh.ExecuteSql("select esd_caption,esd_enable,el_type from cs_exportsettingdetail left join CS_EXPORTSETTING" +
  92. " on es_id=esd_esid where es_custcode='" + pr_brand.Text + "'", "select");
  93. if (dt.Rows.Count == 0)
  94. {
  95. MessageBox.Show("无条码解析规则");
  96. Play("NG");
  97. return;
  98. }
  99. string split = dt.Rows[0]["el_type"].ToString();
  100. string[] str = Barcode.Text.Split(split.ToCharArray()[0]);
  101. //分割后的字符串数组需要大于设置的参数解析
  102. if (str.Length < dt.Rows.Count)
  103. {
  104. MessageBox.Show("无条码解析规则");
  105. Play("NG");
  106. return;
  107. }
  108. for (int i = 0; i < dt.Rows.Count; i++)
  109. {
  110. string name = dt.Rows[i]["esd_caption"].ToString();
  111. string index = dt.Rows[i]["esd_enable"].ToString();
  112. switch (name)
  113. {
  114. case "LOTNO":
  115. LOTNO.Text = str[int.Parse(index) - 1];
  116. break;
  117. case "DC":
  118. DC.Text = str[int.Parse(index) - 1];
  119. break;
  120. case "PROD":
  121. pr_orispeccode3.Text = str[int.Parse(index) - 1];
  122. break;
  123. case "QTY":
  124. INQTY.Text = str[int.Parse(index) - 1];
  125. break;
  126. case "SCCODE":
  127. sc_code.Text = str[int.Parse(index) - 1];
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. string madedate;
  134. string outqty;
  135. string lotno;
  136. string spec;
  137. LogicHandler.GetWeekData(DC.Text, INQTY.Text, pr_brand.Text.ToUpper(), pr_orispeccode3.Text, LOTNO.Text, out madedate, out outqty, out lotno, out spec);
  138. DC.Text = madedate;
  139. INQTY.Text = outqty;
  140. LOTNO.Text = lotno;
  141. pr_orispeccode3.Text = spec;
  142. Barcode.Clear();
  143. GenBarCode.PerformClick();
  144. Play("OK");
  145. }
  146. }
  147. bool AutoSized = false;
  148. private void 入库条码规则解析_AutoSizeChanged(object sender, EventArgs e)
  149. {
  150. if (!AutoSized)
  151. {
  152. asc.controlAutoSize(this);
  153. AutoSized = true;
  154. }
  155. }
  156. private void GenBarCode_Click(object sender, EventArgs e)
  157. {
  158. if (pr_orispeccode.Text != pr_orispeccode3.Text)
  159. {
  160. MessageBox.Show("解析型号和单据型号不对应");
  161. Play("NG");
  162. return;
  163. }
  164. int Minpackage = 0;
  165. //本次入库数
  166. int NowInqty = int.Parse(INQTY.Text);
  167. if (!int.TryParse(pr_zxbzs.Text, out Minpackage))
  168. {
  169. MessageBox.Show("最小包装数错误,料号" + pr_code.Text);
  170. Play("NG");
  171. return;
  172. }
  173. string outboxcode = "";
  174. if (GenBoxCode.Checked)
  175. {
  176. string[] param = new string[] { pr_code.Text, "", outboxcode };
  177. dh.CallProcedure("SP_GETBOX", ref param);
  178. outboxcode = param[2].Replace("BOX:", "");
  179. }
  180. //生成条码数量
  181. int barcount = NowInqty % Minpackage == 0 ? NowInqty / Minpackage : (NowInqty / Minpackage) + 1;
  182. List<string> BAR_inqty = new List<string>();
  183. List<string> bar_code = new List<string>();
  184. List<string> bar_vendbarcode = new List<string>();
  185. List<string> bar_madedate = new List<string>();
  186. List<string> bar_sccode = new List<string>();
  187. for (int i = 0; i < barcount; i++)
  188. {
  189. string barcode = "";
  190. int OneUnit = 0;
  191. string[] param = new string[] { pr_code.Text, "", barcode };
  192. dh.CallProcedure("SP_GETBARCODE", ref param);
  193. barcode = param[2].Replace("BARCODE:", "");
  194. //如果最后有尾数的话
  195. if (i == barcount - 1)
  196. {
  197. if (NowInqty % Minpackage == 0)
  198. {
  199. OneUnit = Minpackage;
  200. }
  201. else
  202. {
  203. OneUnit = NowInqty % Minpackage;
  204. }
  205. }
  206. else
  207. {
  208. OneUnit = Minpackage;
  209. }
  210. BAR_inqty.Add(OneUnit.ToString());
  211. bar_code.Add(barcode);
  212. bar_vendbarcode.Add(LOTNO.Text);
  213. bar_madedate.Add(DC.Text);
  214. bar_sccode.Add(sc_code.Text);
  215. }
  216. string sql = "insert into barcode(BAR_ID, bar_code, bar_prodcode, BAR_remain,bar_vendbarcode, " +
  217. "bar_madedate, BAR_PRODID, BAR_STATUS, BAR_CHECKMAN, BAR_INDATE,BAR_OUTBOXCODE1,bar_sccode)select barcode_seq.nextval,:bar_code," +
  218. "'" + pr_code.Text + "',:bar_remain,:bar_vendbarcode,to_date(:bar_madedate,'yyyy-mm-dd'),pr_id,1,'管理员',sysdate,'" + outboxcode + "',:bar_sccode " +
  219. " from product where pr_code='" + pr_code.Text + "'";
  220. dh.BatchInsert(sql, new string[] { "bar_code", "bar_remain", "bar_vendbarcode", "bar_madedate", "bar_sccode" }, bar_code.ToArray(), BAR_inqty.ToArray(), bar_vendbarcode.ToArray(), bar_madedate.ToArray(), bar_sccode.ToArray());
  221. LoadBarcodeioData(pr_code.Text);
  222. }
  223. private void LoadBarcodeioData(string pr_code)
  224. {
  225. string sql = "select 0 CheckBox,bar_code,bar_remain,bar_indate,BAR_OUTBOXCODE1,bar_vendbarcode,to_char(bar_madedate,'yyyy-mm-dd')bar_madedate,pr_detail,pr_spec,bar_prodcode,pr_orispeccode,bar_sccode from barcode left join product on bar_prodcode=pr_code where to_char(bar_indate,'yyyy-mm-dd')='" + bar_indate.Value.ToString("yyyy-MM-dd") + "' and bar_prodcode='" + pr_code + "'";
  226. DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
  227. BaseUtil.FillDgvWithDataTable(BarcodeIO, dt);
  228. }
  229. private void PrintBarCode_Click(object sender, EventArgs e)
  230. {
  231. for (int i = 0; i < BarcodeIO.Rows.Count; i++)
  232. {
  233. if (BarcodeIO.Rows[i].Cells["CheckBox"].FormattedValue != null && BarcodeIO.Rows[i].Cells["CheckBox"].FormattedValue.ToString() == "True")
  234. {
  235. for (int j = 0; j < SingleDoc.Variables.FreeVariables.Count; j++)
  236. {
  237. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "BARCODE")
  238. {
  239. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  240. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["bar_code"].Value.ToString();
  241. }
  242. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "DC")
  243. {
  244. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  245. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["bar_madedate"].Value.ToString();
  246. }
  247. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "LOTNO")
  248. {
  249. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  250. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["bar_vendbarcode"].Value.ToString();
  251. }
  252. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "QTY")
  253. {
  254. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  255. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["bar_remain"].Value.ToString();
  256. }
  257. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "PRCODE")
  258. {
  259. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  260. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["bar_prodcode"].Value.ToString();
  261. }
  262. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "PRSPEC")
  263. {
  264. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  265. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["pr_spec1"].Value.ToString();
  266. }
  267. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "PRDETAIL")
  268. {
  269. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  270. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["pr_detail1"].Value.ToString();
  271. }
  272. if (SingleDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "ORISPEC")
  273. {
  274. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  275. SingleDoc.Variables.FreeVariables.Item(j + 1).Value = BarcodeIO.Rows[i].Cells["pr_orispeccode4"].Value.ToString();
  276. }
  277. }
  278. SingleDoc.Printer.SwitchTo(PrinterList.Text);
  279. SingleDoc.PrintDocument();
  280. }
  281. }
  282. }
  283. private void ChooseAll_Click(object sender, EventArgs e)
  284. {
  285. for (int i = 0; i < BarcodeIO.Rows.Count; i++)
  286. {
  287. BarcodeIO.Rows[i].Cells["CheckBox"].Value = true;
  288. }
  289. }
  290. private void PrintBox_Click(object sender, EventArgs e)
  291. {
  292. List<string> outboxcode = new List<string>();
  293. for (int i = 0; i < BarcodeIO.Rows.Count; i++)
  294. {
  295. if (BarcodeIO.Rows[i].Cells["CheckBox"].FormattedValue != null && BarcodeIO.Rows[i].Cells["CheckBox"].FormattedValue.ToString() == "True")
  296. {
  297. if (!outboxcode.Contains(BarcodeIO.Rows[i].Cells["BAR_OUTBOXCODE1"].FormattedValue))
  298. {
  299. outboxcode.Add(BarcodeIO.Rows[i].Cells["BAR_OUTBOXCODE1"].Value.ToString());
  300. }
  301. }
  302. }
  303. string[] box = outboxcode.ToArray();
  304. for (int i = 0; i < box.Length; i++)
  305. {
  306. string sql = "select BAR_OUTBOXCODE1,sum(BAR_remain)BAR_remain,max(bar_vendbarcode)bar_vendbarcode,max(to_char(bar_madedate,'yyyy-mm-dd'))bar_madedate," +
  307. "max(pr_detail)pr_detail,max(pr_spec)pr_spec,max(bar_prodcode)bar_prodcode,max(pr_orispeccode)pr_orispeccode,max(pr_detail)pr_detail from barcode left join product on bar_prodcode=pr_code where BAR_OUTBOXCODE1='" + box[i] + "' group by BAR_OUTBOXCODE1";
  308. DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
  309. for (int j = 0; j < SingleDoc.Variables.FreeVariables.Count; j++)
  310. {
  311. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "BARCODE")
  312. {
  313. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  314. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["BAR_OUTBOXCODE1"].ToString();
  315. }
  316. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "DC")
  317. {
  318. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  319. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["bar_madedate"].ToString();
  320. }
  321. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "LOTNO")
  322. {
  323. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  324. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["bar_vendbarcode"].ToString();
  325. }
  326. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "QTY")
  327. {
  328. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  329. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["BAR_remain"].ToString();
  330. }
  331. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "PRCODE")
  332. {
  333. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  334. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["bar_prodcode"].ToString();
  335. }
  336. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "PRSPEC")
  337. {
  338. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  339. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["pr_spec"].ToString();
  340. }
  341. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "PRDETAIL")
  342. {
  343. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  344. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["pr_detail"].ToString();
  345. }
  346. if (OutDoc.Variables.FreeVariables.Item(j + 1).Name.ToUpper() == "ORISPEC")
  347. {
  348. if (BarcodeIO.Rows[i].Cells["bar_madedate"].Value != null)
  349. OutDoc.Variables.FreeVariables.Item(j + 1).Value = dt.Rows[0]["pr_orispeccode"].ToString();
  350. }
  351. }
  352. OutDoc.Printer.SwitchTo(PrinterList.Text);
  353. OutDoc.PrintDocument();
  354. }
  355. }
  356. private void QueryBarcode_Click(object sender, EventArgs e)
  357. {
  358. LoadBarcodeioData(pr_code.Text);
  359. }
  360. Thread thread;
  361. string FileName = "";
  362. public void Play(string Type)
  363. {
  364. if (Type == "OK")
  365. {
  366. FileName = System.Windows.Forms.Application.StartupPath + @"\Resources\Sound\8378.wav";
  367. thread = new Thread(PlaySound);
  368. thread.Start();
  369. }
  370. else
  371. {
  372. FileName = System.Windows.Forms.Application.StartupPath + @"\Resources\Sound\5185.wav";
  373. thread = new Thread(PlaySound);
  374. thread.Start();
  375. }
  376. }
  377. private void PlaySound()
  378. {
  379. //要加载COM组件:Microsoft speech object Library
  380. if (!System.IO.File.Exists(FileName))
  381. {
  382. return;
  383. }
  384. try
  385. {
  386. SoundPlayer player = new SoundPlayer();
  387. player.SoundLocation = FileName;
  388. player.Load();
  389. player.Play();
  390. }
  391. catch (Exception e)
  392. {
  393. MessageBox.Show(e.Message);
  394. }
  395. }
  396. }
  397. }