Print.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using UAS_MES_NEW.DataOperate;
  2. using LabelManager2;
  3. using System.Data;
  4. using System.Text;
  5. using System.IO;
  6. using UAS_MES_NEW.Entity;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. using Seagull.BarTender.Print;
  10. using System;
  11. namespace UAS_MES_NEW.PublicMethod
  12. {
  13. class Print
  14. {
  15. static DataHelper dh = SystemInf.dh;
  16. //CodeSoft打印的驱动和文件
  17. static Document doc;
  18. ////CodeSoft的打印机
  19. //string CodeSpft_Printer;
  20. static LabelFormatDocument format;
  21. public Print() { }
  22. public static bool CodeSoft(string iCaller, Document doc, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  23. {
  24. ErrorMessage = "";
  25. DataTable dt = new DataTable();
  26. if (IfRePrint != "-1")
  27. {
  28. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  29. {
  30. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  31. }
  32. else
  33. {
  34. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  35. }
  36. ////如果已经打印过了,则不允许再打印
  37. if (dt.Rows.Count > 0)
  38. {
  39. ErrorMessage = SnCode + LabelType + "已打印";
  40. return false;
  41. }
  42. }
  43. //打开模板路径
  44. //查询模板对应的取值SQL和参数名称
  45. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql,nvl(lp_ifnull,0)lp_ifnull from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  46. StringBuilder sb = new StringBuilder();
  47. if (doc == null)
  48. {
  49. MessageBox.Show("打印文件不存在");
  50. return false;
  51. }
  52. //执行全部的SQL
  53. for (int i = 0; i < dt.Rows.Count; i++)
  54. {
  55. string sql = dt.Rows[i]["lp_sql"].ToString();
  56. string lp_name = dt.Rows[i]["lp_name"].ToString();
  57. string lp_ifnull = dt.Rows[i]["lp_ifnull"].ToString();
  58. try
  59. {
  60. Regex ConnoteA = new Regex("{\\w+}");
  61. foreach (Match mch in ConnoteA.Matches(sql))
  62. {
  63. string x = mch.Value.Trim();
  64. sql = sql.Replace(x, "'" + SnCode + "'");
  65. }
  66. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  67. if (Param.Rows.Count == 0)
  68. {
  69. if (lp_ifnull != "0")
  70. {
  71. MessageBox.Show("参数" + lp_name + "为空,不允许打印");
  72. return false;
  73. }
  74. continue;
  75. }
  76. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  77. if (Param.Rows.Count > 0)
  78. {
  79. int LoopTime = Param.Rows.Count > 1000 ? 1000 : Param.Rows.Count;
  80. for (int j = 0; j < LoopTime; j++)
  81. {
  82. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  83. {
  84. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  85. {
  86. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  87. }
  88. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  89. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  90. {
  91. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  92. }
  93. }
  94. }
  95. }
  96. }
  97. catch (System.Exception)
  98. {
  99. MessageBox.Show("SQL维护不正确");
  100. }
  101. }
  102. LogManager.DoLog(sb.ToString());
  103. //保存本次赋值进行打印
  104. doc.Printer.SwitchTo(PrinterName);
  105. doc.PrintDocument(PrintNum);
  106. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  107. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  108. {
  109. doc.Variables.FormVariables.Item(k + 1).Value = null;
  110. }
  111. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, LabelName);
  112. return true;
  113. }
  114. public static bool CodeSoft(string iCaller, ref ApplicationClass lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  115. {
  116. ErrorMessage = "";
  117. DataTable dt = new DataTable();
  118. if (IfRePrint != "-1")
  119. {
  120. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标")
  121. {
  122. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  123. }
  124. else
  125. {
  126. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  127. }
  128. ////如果已经打印过了,则不允许再打印
  129. if (dt.Rows.Count > 0)
  130. {
  131. ErrorMessage = SnCode + LabelType + "已打印";
  132. return false;
  133. }
  134. }
  135. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  136. FileInfo PrintFile = new FileInfo(LabelName);
  137. //打开模板路径
  138. //查询模板对应的取值SQL和参数名称
  139. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "' order by to_number(lp_detno)", "select");
  140. StringBuilder sb = new StringBuilder();
  141. if (!PrintFile.Exists)
  142. {
  143. MessageBox.Show("打印文件不存在");
  144. return false;
  145. }
  146. string filechangetime = PrintFile.LastWriteTime.ToString();
  147. if (filechangetime != filelastwritetime)
  148. {
  149. lbl.Quit();
  150. lbl = new ApplicationClass();
  151. BaseUtil.WriteLbl();
  152. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  153. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  154. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  155. }
  156. doc = lbl.Documents.Open(LabelName, true);
  157. if (doc == null)
  158. {
  159. MessageBox.Show("标签文件打开失败");
  160. return false;
  161. }
  162. //执行全部的SQL
  163. for (int i = 0; i < dt.Rows.Count; i++)
  164. {
  165. string sql = dt.Rows[i]["lp_sql"].ToString();
  166. try
  167. {
  168. Regex ConnoteA = new Regex("{\\w+}");
  169. foreach (Match mch in ConnoteA.Matches(sql))
  170. {
  171. string x = mch.Value.Trim();
  172. sql = sql.Replace(x, "'" + SnCode + "'");
  173. }
  174. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  175. if (Param.Rows.Count == 0)
  176. continue;
  177. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  178. if (Param.Rows.Count > 0)
  179. {
  180. int LoopTime = Param.Rows.Count > 1000 ? 1000 : Param.Rows.Count;
  181. for (int j = 0; j < LoopTime; j++)
  182. {
  183. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  184. {
  185. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  186. {
  187. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  188. }
  189. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  190. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  191. {
  192. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  193. }
  194. }
  195. }
  196. }
  197. }
  198. catch (System.Exception)
  199. {
  200. MessageBox.Show("SQL维护不正确");
  201. }
  202. }
  203. LogManager.DoLog(sb.ToString());
  204. //保存本次赋值进行打印
  205. doc.Printer.SwitchTo(PrinterName);
  206. doc.PrintDocument(PrintNum);
  207. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  208. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  209. {
  210. doc.Variables.FormVariables.Item(k + 1).Value = null;
  211. }
  212. LogicHandler.doLabelPrintLog(SnCode, LabelType + LabelName, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, LabelName);
  213. return true;
  214. }
  215. public static bool BarTender(string iCaller, ref Engine lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  216. {
  217. ErrorMessage = "";
  218. DataTable dt = new DataTable();
  219. if (IfRePrint != "-1" && LabelType != "栈板标")
  220. {
  221. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标" || LabelType == "彩盒标")
  222. {
  223. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "' and lpl_file='" + LabelName + "'", "select");
  224. }
  225. else
  226. {
  227. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "' and lpl_file='" + LabelName + "'", "select");
  228. }
  229. ////如果已经打印过了,则不允许再打印
  230. if (dt.Rows.Count > 0)
  231. {
  232. ErrorMessage = SnCode + LabelType + "已打印";
  233. return false;
  234. }
  235. }
  236. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  237. FileInfo PrintFile = new FileInfo(LabelName);
  238. //打开模板路径
  239. //查询模板对应的取值SQL和参数名称
  240. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql,nvl(lp_ifnull,0)lp_ifnull from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  241. StringBuilder sb = new StringBuilder();
  242. if (!PrintFile.Exists)
  243. {
  244. MessageBox.Show("打印文件不存在");
  245. return false;
  246. }
  247. string filechangetime = PrintFile.LastWriteTime.ToString();
  248. if (filechangetime != filelastwritetime)
  249. {
  250. lbl.Stop();
  251. lbl = new Engine(true);
  252. BaseUtil.WriteLbl();
  253. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  254. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  255. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  256. }
  257. format = lbl.Documents.Open(LabelName);
  258. if (format == null)
  259. {
  260. MessageBox.Show("标签文件打开失败");
  261. return false;
  262. }
  263. //执行全部的SQL
  264. for (int i = 0; i < dt.Rows.Count; i++)
  265. {
  266. string sql = dt.Rows[i]["lp_sql"].ToString();
  267. string lp_name = dt.Rows[i]["lp_name"].ToString();
  268. string lp_ifnull = dt.Rows[i]["lp_ifnull"].ToString();
  269. try
  270. {
  271. Regex ConnoteA = new Regex("{\\w+}");
  272. foreach (Match mch in ConnoteA.Matches(sql))
  273. {
  274. string x = mch.Value.Trim();
  275. sql = sql.Replace(x, "'" + SnCode + "'");
  276. }
  277. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  278. if (Param.Rows.Count == 0)
  279. {
  280. if (lp_ifnull != "0")
  281. {
  282. MessageBox.Show("参数" + lp_name + "为空,不允许打印");
  283. return false;
  284. }
  285. continue;
  286. }
  287. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  288. if (Param.Rows.Count > 0)
  289. {
  290. if (lp_ifnull != "0" && Param.Rows[0][0].ToString() == "")
  291. {
  292. MessageBox.Show("参数" + lp_name + "为空,不允许打印");
  293. return false;
  294. }
  295. int LoopTime = Param.Rows.Count > 1000 ? 1000 : Param.Rows.Count;
  296. for (int j = 0; j < LoopTime; j++)
  297. {
  298. for (int k = 0; k < format.SubStrings.Count; k++)
  299. {
  300. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  301. {
  302. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  303. }
  304. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  305. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  306. {
  307. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  308. }
  309. }
  310. }
  311. }
  312. }
  313. catch (System.Exception)
  314. {
  315. MessageBox.Show("SQL维护不正确");
  316. }
  317. }
  318. LogManager.DoLog(sb.ToString());
  319. //保存本次赋值进行打印
  320. format.PrintSetup.PrinterName = PrinterName;
  321. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  322. format.Print();
  323. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  324. try
  325. {
  326. for (int k = 0; k < format.SubStrings.Count; k++)
  327. {
  328. if (format.SubStrings[k].Value != null)
  329. {
  330. format.SubStrings[k].Value = null;
  331. }
  332. }
  333. }
  334. catch (Exception)
  335. {
  336. }
  337. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, LabelName);
  338. return true;
  339. }
  340. public static bool BarTender(string iCaller, ref Engine lbl, string LabelName, string LaID, string PrinterName, string pd_pdno, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  341. {
  342. ErrorMessage = "";
  343. DataTable dt = new DataTable();
  344. if (IfRePrint != "-1" && (SystemInf.CurrentDB == "HAOQ" || SystemInf.CurrentDB == "OPD") && LabelType != "栈板标")
  345. {
  346. if (LabelType == "")
  347. {
  348. }
  349. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标" || LabelType == "彩盒标")
  350. {
  351. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  352. }
  353. else
  354. {
  355. dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  356. }
  357. ////如果已经打印过了,则不允许再打印
  358. if (dt.Rows.Count > 0)
  359. {
  360. ErrorMessage = SnCode + LabelType + "已打印";
  361. return false;
  362. }
  363. }
  364. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  365. FileInfo PrintFile = new FileInfo(LabelName);
  366. //打开模板路径
  367. //查询模板对应的取值SQL和参数名称
  368. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  369. StringBuilder sb = new StringBuilder();
  370. if (!PrintFile.Exists)
  371. {
  372. MessageBox.Show("打印文件不存在");
  373. return false;
  374. }
  375. string filechangetime = PrintFile.LastWriteTime.ToString();
  376. if (filechangetime != filelastwritetime)
  377. {
  378. lbl.Stop();
  379. lbl = new Engine(true);
  380. BaseUtil.WriteLbl();
  381. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  382. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  383. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  384. }
  385. format = lbl.Documents.Open(LabelName);
  386. if (format == null)
  387. {
  388. MessageBox.Show("标签文件打开失败");
  389. return false;
  390. }
  391. //执行全部的SQL
  392. for (int i = 0; i < dt.Rows.Count; i++)
  393. {
  394. string sql = dt.Rows[i]["lp_sql"].ToString();
  395. try
  396. {
  397. Regex ConnoteA = new Regex("{\\w+}");
  398. foreach (Match mch in ConnoteA.Matches(sql))
  399. {
  400. string x = mch.Value.Trim();
  401. sql = sql.Replace(x, "'" + pd_pdno + "'");
  402. }
  403. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  404. if (Param.Rows.Count == 0)
  405. continue;
  406. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  407. if (Param.Rows.Count > 0)
  408. {
  409. int LoopTime = Param.Rows.Count > 1000 ? 1000 : Param.Rows.Count;
  410. for (int j = 0; j < LoopTime; j++)
  411. {
  412. for (int k = 0; k < format.SubStrings.Count; k++)
  413. {
  414. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  415. {
  416. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  417. }
  418. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  419. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  420. {
  421. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  422. }
  423. }
  424. }
  425. }
  426. }
  427. catch (System.Exception)
  428. {
  429. MessageBox.Show("SQL维护不正确");
  430. }
  431. }
  432. LogManager.DoLog(sb.ToString());
  433. //保存本次赋值进行打印
  434. format.PrintSetup.PrinterName = PrinterName;
  435. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  436. format.Print();
  437. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  438. try
  439. {
  440. for (int k = 0; k < format.SubStrings.Count; k++)
  441. {
  442. if (format.SubStrings[k].Value != null)
  443. format.SubStrings[k].Value = "";
  444. }
  445. }
  446. catch (Exception)
  447. {
  448. }
  449. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, LabelName);
  450. return true;
  451. }
  452. }
  453. }