using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace PrintDemo { public class PrintService { static byte[] b_init = PrinterCmdUtils.init_printer(); static byte[] b_nextLine = PrinterCmdUtils.nextLine(1); static byte[] b_next2Line = PrinterCmdUtils.nextLine(2); static byte[] b_next3Line = PrinterCmdUtils.nextLine(3); static byte[] b_center = PrinterCmdUtils.alignCenter(); static byte[] b_left = PrinterCmdUtils.alignLeft(); static byte[] b_breakPartial = PrinterCmdUtils.feedPaperCutPartial(); public static byte[] OrderPrint(DataRow saleBillRow, DataTable saleDetailTable) { try { string _strTake = saleBillRow["TABLE_NUMBER"].ToString() == "2000" ? "预约" : "堂食"; byte[][] _bTicketTop = { b_init, b_center,Encoding.GetEncoding("gbk").GetBytes("制作单"),b_nextLine, b_left,PrinterCmdUtils.boldOn(),PrinterCmdUtils.fontSizeSetBig(2), Encoding.GetEncoding("gbk").GetBytes(GetLine("餐桌号:" + saleBillRow["TABLE_NUMBER"].ToString() ,_strTake,20,' ')),b_nextLine, b_left,Encoding.GetEncoding("gbk").GetBytes("联系人:" ),b_nextLine, PrinterCmdUtils.alignRight(),Encoding.GetEncoding("gbk").GetBytes( saleBillRow["ORDER_PERSON"].ToString() + "[" + saleBillRow["ORDER_PERSONTEL"].ToString() + "]"),b_nextLine, b_left,Encoding.GetEncoding("gbk").GetBytes(_strTake=="预约"?"取餐时间:":"交易时间"),b_nextLine, PrinterCmdUtils.alignRight(),Encoding.GetEncoding("gbk").GetBytes(_strTake=="预约"?saleBillRow["RESERVATION_DATE"].ToString():saleBillRow["ORDER_DATE"].ToString()),b_nextLine, b_center,PrinterCmdUtils.boldOff(),PrinterCmdUtils.fontSizeSetBig(1),Encoding.GetEncoding("gbk").GetBytes("----------------------------------------"),b_nextLine, b_left,Encoding.GetEncoding("gbk").GetBytes("商品名称 数量"),b_nextLine }; List _ListGoods = new List(); for (int i = 0; i < saleDetailTable.Rows.Count; i++) { //_ListGoods.Add(saleDetailTable.Rows[i]["COMMODITY_NAME"].ToString()); decimal _GoodCount = 0; decimal.TryParse(saleDetailTable.Rows[i]["ORDER_COUNT"].ToString(), out _GoodCount); _ListGoods.Add(GetLine(saleDetailTable.Rows[i]["COMMODITY_NAME"].ToString(), "*" + _GoodCount.ToString("F2"), 48, ' ')); } byte[][] _bTicketGoods = new byte[_ListGoods.Count * 2 + 1][]; _bTicketGoods[0] = PrinterCmdUtils.fontSizeSetBig(1); for (int i = 0; i < _ListGoods.Count * 2; i += 2) { _bTicketGoods[i + 1] = Encoding.GetEncoding("gbk").GetBytes(_ListGoods[i / 2]); _bTicketGoods[i + 2] = b_nextLine; } decimal _amount = 0; decimal.TryParse(saleBillRow["ORDER_AMOUNT"].ToString(), out _amount); byte[][] _bTicketEnd = { b_nextLine,b_center,Encoding.GetEncoding("gbk").GetBytes("----------------------------------------"), b_next2Line,b_left,PrinterCmdUtils.boldOn(),PrinterCmdUtils.fontSizeSetBig(2), Encoding.GetEncoding("gbk").GetBytes(GetLine("合计:", _amount.ToString("F2"),20,' ')), b_next2Line, b_breakPartial }; byte[][] _bTicket = new byte[_bTicketTop.Length + _bTicketEnd.Length + _bTicketGoods.Length][]; Array.Copy(_bTicketTop, _bTicket, _bTicketTop.Length); Array.Copy(_bTicketGoods, 0, _bTicket, _bTicketTop.Length, _bTicketGoods.Length); Array.Copy(_bTicketEnd, 0, _bTicket, _bTicketTop.Length + _bTicketGoods.Length, _bTicketEnd.Length); return PrinterCmdUtils.byteMerger(_bTicket); } catch (Exception ex) { throw ex; } } public static string GetLine(string chars1, string chars2, int length, char paddingChar = '\0') { int len = (length - GetSingleLength(chars1) - GetSingleLength(chars2)) + chars2.Length; string chars = chars1 + chars2.PadLeft(len, ' '); return chars; } private static int GetSingleLength(string input) { if (string.IsNullOrEmpty(input)) { return 0; } return System.Text.RegularExpressions.Regex.Replace(input, @"[^\x00-\xff]", "aa").Length;//计算得到该字符串对应单字节字符串的长度 } /// /// 会员充值小票 /// /// /// /// /// /// /// public static byte[] charge(String shop, String num, String count, String way, String time) { String shop_f = " 会员充值(" + shop + ")"; String num_f = " 会员号码:" + num; String count_f = " 充值金额:" + count; String way_f = " 充值方式:" + way; String time_f = " 充值时间:" + time; try { byte[] b_title = System.Text.Encoding.GetEncoding("gbk").GetBytes(shop_f); byte[] b_num = System.Text.Encoding.GetEncoding("gbk").GetBytes(num_f); byte[] b_count = System.Text.Encoding.GetEncoding("gbk").GetBytes(count_f); byte[] b_way = System.Text.Encoding.GetEncoding("gbk").GetBytes(way_f); byte[] b_time = System.Text.Encoding.GetEncoding("gbk").GetBytes(time_f); byte[] b_no = System.Text.Encoding.GetEncoding("gbk").GetBytes(" ----------------------------------------"); byte[] b_breakPartial = PrinterCmdUtils.feedPaperCutPartial(); byte[][] charge_0 = { b_init, b_center, b_title, b_next2Line, b_left, b_count, b_nextLine, b_way, b_nextLine, b_time, b_nextLine, b_no, b_nextLine, b_breakPartial, PrinterCmdUtils.open_money() }; byte[][] charge_1 = { b_init, b_center, b_title, b_next2Line, b_left, b_num, b_nextLine, b_count, b_nextLine, b_way, b_nextLine, b_time, b_nextLine, b_no, b_nextLine, b_breakPartial, PrinterCmdUtils.open_money() }; if (String.IsNullOrEmpty(num)) { return PrinterCmdUtils.byteMerger(charge_0); } else { return PrinterCmdUtils.byteMerger(charge_1); } } catch (Exception) { throw; } } /// /// 收银小票 Json对象拼接 /// /// /// /// /// public static byte[] jsonParse(String head, String recv, String foot, String url) { try { //二维码生成 EscCommand esc = new EscCommand(); esc.AddSelectErrorCorrectionLevelForQRCode((byte)0x31); esc.AddSelectSizeOfModuleForQRCode((byte)8); esc.AddStoreQRCodeData(url); esc.AddPrintQRCode(); ArrayList datas = esc.GetCommand(); Byte[] Bytes = new Byte[datas.Count]; for (int i = 0; i < datas.Count; i++) { Bytes[i] = (Byte)datas[i]; } byte[] bytes = EscCommand.ToPrimitive(Bytes); //打印小票头部 List headString = new List(); List mHeads = JsonConvert.DeserializeObject>(head); headString.Add(" " + mHeads[0].Ordertime); headString.Add(" NO." + mHeads[0].Shopno); headString.Add(FileUtils.getFixedWidthString(" 当班人员:" + mHeads[0].Shopuser, 36) + "餐牌号:" + mHeads[0].Card); headString.Add(" ---------------------------------------------"); List newString = new List(); List mTickets = JsonConvert.DeserializeObject>(recv); String nameTitle = " 名称 数量 售价 优惠 总额"; newString.Add(nameTitle); for (int i = 0; i < mTickets.Count; i++) { StringBuilder item = new StringBuilder(); if (("1".Equals(mTickets[i].Type) || "2".Equals(mTickets[i].Type))) { if ("".Equals(mTickets[i].Parentid)) { if (mTickets[i].Name.Length > 8) { item.Append(FileUtils.getFixedWidthString(mTickets[i].Name.Substring(0, 8), 19)); item.Append(mTickets[i].Num.PadRight(4)); item.Append(String.Format("{0:F2}", mTickets[i].Price).PadRight(8)); item.Append(String.Format("{0:F2}", mTickets[i].Rebetmoney).PadRight(8)); item.Append(String.Format("{0:F2}", mTickets[i].Money).PadRight(6)); newString.Add(" " + item.ToString()); newString.Add(" " + mTickets[i].Name.Substring(8, mTickets[i].Name.Length - 8)); } else { item.Append(FileUtils.getFixedWidthString(mTickets[i].Name, 19)); item.Append(mTickets[i].Num.PadRight(4)); item.Append(String.Format("{0:F2}", mTickets[i].Price).PadRight(8)); item.Append(String.Format("{0:F2}", mTickets[i].Rebetmoney).PadRight(8)); item.Append(String.Format("{0:F2}", mTickets[i].Money).PadRight(6)); newString.Add(" " + item.ToString()); } } else { newString.Add(" " + mTickets[i].Name); } } } newString.Add(" ---------------------------------------------"); List footString = new List(); List mFoots = JsonConvert.DeserializeObject>(foot); String moneytotal = ((float.Parse(mFoots[0].DecAmmBalance) + float.Parse(mFoots[0].DecGroupBuyMoney))).ToString("f2"); String money = ((float.Parse(mFoots[0].DecRebeatBalance) + float.Parse(mFoots[0].DecGroupBuyMoney))).ToString("f2"); footString.Add(FileUtils.getFixedWidthString(" 餐品数量:", 20) + FileUtils.getFixedWidthString(mFoots[0].Counts, 10) + "点餐金额: " + moneytotal.PadRight(4)); if (float.Parse(mFoots[0].DecWipeMoney) != 0.0f) footString.Add(FileUtils.getFixedWidthString(" 抹零优惠:", 40) + mFoots[0].DecWipeMoney); if (float.Parse(mFoots[0].DecMemberCoupon) != 0.0f) footString.Add(FileUtils.getFixedWidthString(" 会员优惠:", 40) + mFoots[0].DecMemberCoupon); if (float.Parse(money) != 0.0f) { footString.Add(FileUtils.getFixedWidthString(" 券类优惠:", 40) + money.PadRight(4)); for (int i = 0; i < mTickets.Count; i++) { if (!"1".Equals(mTickets[i].Type) && !"2".Equals(mTickets[i].Type)) { footString.Add(FileUtils.getFixedWidthString(" " + mTickets[i].Name, 20) + mTickets[i].Num); } else { if (!string.IsNullOrEmpty(mTickets[i].RebetName)) footString.Add(FileUtils.getFixedWidthString(" " + mTickets[i].RebetName, 20) + 1); } } } footString.Add(" ---------------------------------------------"); if (float.Parse(mFoots[0].DecPaidBalance) != 0.0f) footString.Add(FileUtils.getFixedWidthString(" 折后支付:", 40) + mFoots[0].DecPaidBalance); if (float.Parse(mFoots[0].Membermoney) != 0.0f) footString.Add(FileUtils.getFixedWidthString(" 会员支付:", 40) + mFoots[0].Membermoney); if (float.Parse(mFoots[0].Cashmoney) != 0.0f) footString.Add(FileUtils.getFixedWidthString(" 现金支付:", 40) + mFoots[0].Cashmoney); if (!string.IsNullOrEmpty(mFoots[0].Bankmoney)) footString.Add(FileUtils.getFixedWidthString(" " + mFoots[0].Bankmoney.Split(':')[0], 40) + mFoots[0].Bankmoney.Split(':')[1]); if (float.Parse(mFoots[0].Givechange) != 0.0f) footString.Add(FileUtils.getFixedWidthString(" 现金找零:", 40) + mFoots[0].Givechange); if (float.Parse(mFoots[0].RechMoney) != 0.0f) footString.Add(FileUtils.getFixedWidthString(" 充值金额:", 40) + mFoots[0].RechMoney); ///////////////////////////////////////////////////////////////////////////////////////// byte[][] headByte = new byte[15][]; //头部数组 headByte[0] = b_init; headByte[1] = b_center; headByte[2] = System.Text.Encoding.GetEncoding("gbk").GetBytes("欢迎光临"); headByte[3] = b_nextLine; headByte[4] = System.Text.Encoding.GetEncoding("gbk").GetBytes(mHeads[0].Shopbrand); headByte[5] = b_next2Line; headByte[6] = b_left; for (int i = 0; i < 8; i += 2) { headByte[i + 7] = System.Text.Encoding.GetEncoding("gbk").GetBytes(headString[i / 2]); headByte[i + 8] = b_nextLine; } byte[][] contentByte = new byte[newString.Count * 2][]; //中间体 for (int i = 0; i < newString.Count * 2; i += 2) { contentByte[i] = System.Text.Encoding.GetEncoding("gbk").GetBytes(newString[i / 2]); contentByte[i + 1] = b_nextLine; } byte[][] footByte = new byte[footString.Count * 2 + 23][]; //底部数组 for (int i = 0; i < footString.Count * 2; i += 2) { footByte[i] = System.Text.Encoding.GetEncoding("gbk").GetBytes(footString[i / 2]); footByte[i + 1] = b_nextLine; } footByte[footString.Count * 2] = System.Text.Encoding.GetEncoding("gbk").GetBytes(" ---------------------------------------------"); if (string.IsNullOrEmpty(mHeads[0].Memberid)) { footByte[footString.Count * 2 + 1] = b_center; footByte[footString.Count * 2 + 2] = b_center; } else { footByte[footString.Count * 2 + 1] = b_nextLine; footByte[footString.Count * 2 + 2] = System.Text.Encoding.GetEncoding("gbk").GetBytes(" 会员号:" + mHeads[0].Memberid); } footByte[footString.Count * 2 + 3] = b_next2Line; footByte[footString.Count * 2 + 4] = b_center; footByte[footString.Count * 2 + 5] = System.Text.Encoding.GetEncoding("gbk").GetBytes("谢谢惠顾!欢迎再次光临!"); footByte[footString.Count * 2 + 6] = b_nextLine; footByte[footString.Count * 2 + 7] = System.Text.Encoding.GetEncoding("gbk").GetBytes("如需开发票,请凭小票在一周内到门店开取"); footByte[footString.Count * 2 + 8] = b_next2Line; footByte[footString.Count * 2 + 9] = b_left; footByte[footString.Count * 2 + 10] = System.Text.Encoding.GetEncoding("gbk").GetBytes(" 门店地址:" + mHeads[0].Shopaddress); footByte[footString.Count * 2 + 11] = b_nextLine; footByte[footString.Count * 2 + 12] = System.Text.Encoding.GetEncoding("gbk").GetBytes(" 联系电话:" + mHeads[0].Shoptel); footByte[footString.Count * 2 + 13] = b_nextLine; footByte[footString.Count * 2 + 14] = System.Text.Encoding.GetEncoding("gbk").GetBytes(" 品牌网址:" + mHeads[0].Url); footByte[footString.Count * 2 + 15] = b_next2Line; footByte[footString.Count * 2 + 16] = b_center; footByte[footString.Count * 2 + 17] = b_next2Line; footByte[footString.Count * 2 + 18] = System.Text.Encoding.GetEncoding("gbk").GetBytes("微信扫一扫 畅享免费WiFi"); footByte[footString.Count * 2 + 19] = b_next2Line; footByte[footString.Count * 2 + 20] = bytes; footByte[footString.Count * 2 + 21] = b_breakPartial; footByte[footString.Count * 2 + 22] = PrinterCmdUtils.open_money(); byte[][] totalByte = new byte[38 + newString.Count * 2 + footString.Count * 2][]; Array.Copy(headByte, totalByte, headByte.Length); Array.Copy(contentByte, 0, totalByte, headByte.Length, contentByte.Length); Array.Copy(footByte, 0, totalByte, headByte.Length + contentByte.Length, footByte.Length); return PrinterCmdUtils.byteMerger(totalByte); } catch (Exception ex) { //Logger.Trace("Head:" + head); //Logger.Trace("Recv:" + recv); //Logger.Trace("Foot:" + foot); //Logger.Trace("url:" + url); throw ex; } } } }