Print.cs 22 KB

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