Print.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 + "' order by to_number(LP_DETNO)", "select");
  241. StringBuilder sb = new StringBuilder();
  242. if (!PrintFile.Exists)
  243. {
  244. MessageBox.Show("打印文件不存在");
  245. return false;
  246. }
  247. System.DateTime dtFileChange;
  248. System.DateTime dtFileLastWrite;
  249. string filechangetime = PrintFile.LastWriteTime.ToString();
  250. bool changeIsNewer = false;
  251. if (System.DateTime.TryParse(filechangetime, out dtFileChange) &&
  252. System.DateTime.TryParse(filelastwritetime, out dtFileLastWrite))
  253. {
  254. changeIsNewer = dtFileChange > dtFileLastWrite;
  255. }
  256. else
  257. {
  258. // 如果解析失败(比如 la_lastwritetime 为空),则按“需要更新”处理
  259. changeIsNewer = true;
  260. }
  261. if (changeIsNewer)
  262. {
  263. lbl.Stop();
  264. lbl = new Engine(true);
  265. BaseUtil.WriteLbl();
  266. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  267. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  268. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  269. }
  270. format = lbl.Documents.Open(LabelName);
  271. if (format == null)
  272. {
  273. MessageBox.Show("标签文件打开失败");
  274. return false;
  275. }
  276. //执行全部的SQL
  277. for (int i = 0; i < dt.Rows.Count; i++)
  278. {
  279. string sql = dt.Rows[i]["lp_sql"].ToString();
  280. string lp_name = dt.Rows[i]["lp_name"].ToString();
  281. string lp_ifnull = dt.Rows[i]["lp_ifnull"].ToString();
  282. try
  283. {
  284. Regex ConnoteA = new Regex("{\\w+}");
  285. foreach (Match mch in ConnoteA.Matches(sql))
  286. {
  287. string x = mch.Value.Trim();
  288. sql = sql.Replace(x, "'" + SnCode + "'");
  289. }
  290. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  291. if (Param.Rows.Count == 0)
  292. {
  293. if (lp_ifnull != "0")
  294. {
  295. MessageBox.Show("参数" + lp_name + "为空,不允许打印");
  296. return false;
  297. }
  298. continue;
  299. }
  300. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  301. if (Param.Rows.Count > 0)
  302. {
  303. if (lp_ifnull != "0" && Param.Rows[0][0].ToString() == "")
  304. {
  305. MessageBox.Show("参数" + lp_name + "为空,不允许打印");
  306. return false;
  307. }
  308. int LoopTime = Param.Rows.Count > 1000 ? 1000 : Param.Rows.Count;
  309. for (int j = 0; j < LoopTime; j++)
  310. {
  311. for (int k = 0; k < format.SubStrings.Count; k++)
  312. {
  313. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  314. {
  315. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  316. }
  317. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  318. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  319. {
  320. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  321. }
  322. }
  323. }
  324. }
  325. }
  326. catch (System.Exception)
  327. {
  328. MessageBox.Show("SQL维护不正确");
  329. }
  330. }
  331. LogManager.DoLog(sb.ToString());
  332. //保存本次赋值进行打印
  333. format.PrintSetup.PrinterName = PrinterName;
  334. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  335. format.Print();
  336. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  337. try
  338. {
  339. for (int k = 0; k < format.SubStrings.Count; k++)
  340. {
  341. if (format.SubStrings[k].Value != null)
  342. {
  343. format.SubStrings[k].Value = null;
  344. }
  345. }
  346. }
  347. catch (Exception)
  348. {
  349. }
  350. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, LabelName);
  351. return true;
  352. }
  353. 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)
  354. {
  355. ErrorMessage = "";
  356. DataTable dt = new DataTable();
  357. if (IfRePrint != "-1" && (SystemInf.CurrentDB == "HAOQ" || SystemInf.CurrentDB == "OPD") && LabelType != "栈板标")
  358. {
  359. if (LabelType == "")
  360. {
  361. }
  362. if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标" || LabelType == "彩盒标")
  363. {
  364. 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");
  365. }
  366. else
  367. {
  368. 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");
  369. }
  370. ////如果已经打印过了,则不允许再打印
  371. if (dt.Rows.Count > 0)
  372. {
  373. ErrorMessage = SnCode + LabelType + "已打印";
  374. return false;
  375. }
  376. }
  377. string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
  378. FileInfo PrintFile = new FileInfo(LabelName);
  379. //打开模板路径
  380. //查询模板对应的取值SQL和参数名称
  381. 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");
  382. StringBuilder sb = new StringBuilder();
  383. if (!PrintFile.Exists)
  384. {
  385. MessageBox.Show("打印文件不存在");
  386. return false;
  387. }
  388. string filechangetime = PrintFile.LastWriteTime.ToString();
  389. if (filechangetime != filelastwritetime)
  390. {
  391. lbl.Stop();
  392. lbl = new Engine(true);
  393. BaseUtil.WriteLbl();
  394. filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
  395. string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')";
  396. dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'");
  397. }
  398. format = lbl.Documents.Open(LabelName);
  399. if (format == null)
  400. {
  401. MessageBox.Show("标签文件打开失败");
  402. return false;
  403. }
  404. //执行全部的SQL
  405. for (int i = 0; i < dt.Rows.Count; i++)
  406. {
  407. string sql = dt.Rows[i]["lp_sql"].ToString();
  408. try
  409. {
  410. Regex ConnoteA = new Regex("{\\w+}");
  411. foreach (Match mch in ConnoteA.Matches(sql))
  412. {
  413. string x = mch.Value.Trim();
  414. sql = sql.Replace(x, "'" + pd_pdno + "'");
  415. }
  416. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  417. if (Param.Rows.Count == 0)
  418. continue;
  419. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  420. if (Param.Rows.Count > 0)
  421. {
  422. int LoopTime = Param.Rows.Count > 1000 ? 1000 : Param.Rows.Count;
  423. for (int j = 0; j < LoopTime; j++)
  424. {
  425. for (int k = 0; k < format.SubStrings.Count; k++)
  426. {
  427. if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  428. {
  429. format.SubStrings[k].Value = Param.Rows[0][0].ToString();
  430. }
  431. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  432. if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  433. {
  434. format.SubStrings[k].Value = Param.Rows[j][0].ToString();
  435. }
  436. }
  437. }
  438. }
  439. }
  440. catch (System.Exception)
  441. {
  442. MessageBox.Show("SQL维护不正确");
  443. }
  444. }
  445. LogManager.DoLog(sb.ToString());
  446. //保存本次赋值进行打印
  447. format.PrintSetup.PrinterName = PrinterName;
  448. format.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
  449. format.Print();
  450. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  451. try
  452. {
  453. for (int k = 0; k < format.SubStrings.Count; k++)
  454. {
  455. if (format.SubStrings[k].Value != null)
  456. format.SubStrings[k].Value = "";
  457. }
  458. }
  459. catch (Exception)
  460. {
  461. }
  462. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, LabelName);
  463. return true;
  464. }
  465. }
  466. }