CPosPrinter.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  1. #include "../pch/pch.h"
  2. #include "CPosPrinter.h"
  3. #include "../tool/CComHelper.h"
  4. #include "../helper/CBitmapHelper.h"
  5. #include <qrencode/qrencode.h>
  6. CPosPrinter::CPosPrinter(): m_socket(m_io)
  7. {
  8. }
  9. CPosPrinter::~CPosPrinter()
  10. {
  11. }
  12. /*
  13. *usbType是1表示只查找收银小票打印机,会排除已知的标签打印机
  14. *usbType为2表示所有usb打印机都查找,包含标签与非标签
  15. **/
  16. void CPosPrinter::InitUsb(int usbType)
  17. {
  18. //设置中文字符
  19. setlocale(LC_CTYPE, "chs");
  20. //取设备路径
  21. int nDevice = GetDevicePath((LPGUID)&USB_GUID, usbType);
  22. LOG_INFO("可用的USB打印机数量:" << nDevice);
  23. //添加usb端口
  24. int i = 0;
  25. while(i < nDevice)
  26. {
  27. std::string setting_biaoqian_printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  28. std::wstring ws_setting_biaoqian_printer_usb = CLewaimaiString::UTF8ToUnicode(setting_biaoqian_printer_usb);
  29. if (CSetting::GetParam("setting_is_new_waimai_biaoqian_printer") == "1" && ws_setting_biaoqian_printer_usb == m_usb_devices[i])
  30. {
  31. //碰到了一个标签打印机
  32. i++;
  33. continue;
  34. }
  35. LOG_INFO("准备打开端口 Port = " << m_usb_devices[i].c_str());
  36. HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  37. if(hPort == INVALID_HANDLE_VALUE)
  38. {
  39. LOG_INFO("打开USB端口失败, Port = " << m_usb_devices[i].c_str());
  40. // 打开端口失败
  41. DWORD error = GetLastError();
  42. if(error == 2)
  43. {
  44. //没有指定的文件
  45. LOG_INFO("没有找对对应的usb端口");
  46. }
  47. else if(error == 5)
  48. {
  49. LOG_INFO("usb端口被占用!");
  50. }
  51. continue;
  52. }
  53. LOG_INFO("打开usb端口成功,准备进行打印机检测! hPort:" << hPort);
  54. if(PortTest(hPort) == true)
  55. {
  56. //端口测试连通,保存起来
  57. PrinterHandle newHandle;
  58. newHandle.hPort = hPort;
  59. newHandle.type = 1;
  60. m_hPorts.push_back(newHandle);
  61. }
  62. else
  63. {
  64. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  65. CloseHandle(hPort);
  66. }
  67. i++;
  68. }
  69. }
  70. bool CPosPrinter::InitOneUsb(wstring usb_path)
  71. {
  72. if (usb_path == L"")
  73. {
  74. return false;
  75. }
  76. HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  77. if(hPort == INVALID_HANDLE_VALUE)
  78. {
  79. LOG_INFO("打开USB端口失败, Port = " << usb_path.c_str());
  80. // 打开端口失败
  81. DWORD error = GetLastError();
  82. if(error == 2)
  83. {
  84. //没有指定的文件
  85. LOG_INFO("没有找对对应的usb端口");
  86. }
  87. else if(error == 5)
  88. {
  89. LOG_INFO("usb端口被占用!");
  90. }
  91. return false;
  92. }
  93. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  94. if(PortTest(hPort) == true)
  95. {
  96. //端口测试连通,保存起来
  97. PrinterHandle newHandle;
  98. newHandle.hPort = hPort;
  99. newHandle.type = 1;
  100. m_hPorts.push_back(newHandle);
  101. }
  102. else
  103. {
  104. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  105. CloseHandle(hPort);
  106. return false;
  107. }
  108. return true;
  109. }
  110. bool CPosPrinter::InitBingkou()
  111. {
  112. std::wstring LptStr = L"lpt1";
  113. HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  114. if(hPort == INVALID_HANDLE_VALUE)
  115. {
  116. LOG_INFO("打开并口失败, Port = " << LptStr.c_str());
  117. DWORD error = GetLastError();
  118. if(error == 2)
  119. {
  120. //没有指定的文件
  121. LOG_INFO("本机器没有并口!");
  122. }
  123. else if(error == 5)
  124. {
  125. LOG_INFO("并口被占用!");
  126. }
  127. return false;
  128. }
  129. else
  130. {
  131. //这个表示并口可以使用
  132. LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
  133. if(PortTest(hPort) == true)
  134. {
  135. //并口测试连通,保存起来
  136. PrinterHandle newHandle;
  137. newHandle.hPort = hPort;
  138. newHandle.type = 2;
  139. m_hPorts.push_back(newHandle);
  140. }
  141. else
  142. {
  143. //并口打印机没有连接,那么就直接关闭掉并口,避免占用
  144. CloseHandle(hPort);
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. void CPosPrinter::InitCom()
  151. {
  152. CComHelper helper;
  153. std::vector<std::wstring> comVector = helper.getComPort();
  154. for(std::vector<std::wstring>::iterator it = comVector.begin(); it != comVector.end(); it++)
  155. {
  156. std::wstring com2Str = *it;
  157. HANDLE hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  158. if(hPort == INVALID_HANDLE_VALUE)
  159. {
  160. DWORD error = GetLastError();
  161. if(error == 2)
  162. {
  163. //没有指定的文件
  164. LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
  165. }
  166. else if(error == 5)
  167. {
  168. LOG_INFO("串口被占用 " << com2Str.c_str());
  169. }
  170. }
  171. else
  172. {
  173. LOG_INFO(com2Str.c_str() << " success, handle:" << hPort);
  174. //扎到了可用的串口,设置串口波特率
  175. DCB dcb;
  176. dcb.DCBlength = sizeof(dcb);
  177. GetCommState(hPort, &dcb);
  178. //佳博的串口打印机,默认是这个波特率
  179. dcb.BaudRate = 19200;
  180. if(!SetCommState(hPort, &dcb))
  181. {
  182. LOG_INFO("set baudRate failed!");
  183. CloseHandle(hPort);
  184. continue;
  185. }
  186. //设定通讯端口超时参数
  187. COMMTIMEOUTS tmouts;
  188. tmouts.ReadIntervalTimeout = 100;
  189. tmouts.ReadTotalTimeoutMultiplier = 100;
  190. tmouts.ReadTotalTimeoutConstant = 100;
  191. tmouts.WriteTotalTimeoutConstant = 100;
  192. tmouts.WriteTotalTimeoutMultiplier = 100;
  193. SetCommTimeouts(hPort, &tmouts);
  194. //设置端口缓冲
  195. SetupComm(hPort, 1024, 1024);
  196. //清除通讯端口缓存
  197. PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
  198. //保存端口
  199. PrinterHandle newHandle;
  200. newHandle.hPort = hPort;
  201. newHandle.type = 3;
  202. m_hPorts.push_back(newHandle);
  203. }
  204. }
  205. }
  206. /*
  207. *找到所有可用的小票打印机,这里只初始化USB,不包含并口、串口、网口
  208. *智能识别模式不再自动处理串口,串口的需要人工去选择类型
  209. **/
  210. void CPosPrinter::InitShouyin()
  211. {
  212. //开始添加usb
  213. InitUsb();
  214. }
  215. /*
  216. *获取CreateFile的USB端口号
  217. **/
  218. int CPosPrinter::GetDevicePath(LPGUID lpGuid, int usbType)
  219. {
  220. HDEVINFO hDevInfoSet;
  221. SP_DEVINFO_DATA spDevInfoData;
  222. SP_DEVICE_INTERFACE_DATA ifData;
  223. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  224. int nCount;
  225. int nTotle;
  226. BOOL bResult;
  227. //这2个字符串,用于根据usb的名字对比是否为打印机设备
  228. wstring strUSBPrint = TEXT("USB 打印支持");
  229. //xp上是英文
  230. wstring strUSBPrint_EN = L"USB Printing Support";
  231. // 取得一个该GUID相关的设备信息集句柄
  232. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  233. NULL, // 无关键字
  234. NULL, // 不指定父窗口句柄
  235. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  236. // 失败...
  237. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  238. {
  239. LOG_INFO("SetupDiGetClassDevs failed \r\n");
  240. return 0;
  241. }
  242. // 申请设备接口数据空间
  243. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  244. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  245. nTotle = -1;
  246. nCount = 0;
  247. bResult = TRUE;
  248. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  249. while(bResult)
  250. {
  251. nTotle++;
  252. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  253. // 枚举符合该GUID的设备接口
  254. bResult = ::SetupDiEnumDeviceInfo(
  255. hDevInfoSet, // 设备信息集句柄
  256. (ULONG)nTotle, // 设备信息集里的设备序号
  257. &spDevInfoData); // 设备接口信息
  258. if(bResult)
  259. {
  260. DWORD DataT;
  261. TCHAR buf[MAX_PATH] = { 0 };
  262. DWORD nSize = 0;
  263. // get Friendly Name or Device Description
  264. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  265. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  266. {
  267. }
  268. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  269. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  270. {
  271. }
  272. else
  273. {
  274. lstrcpy(buf, _T("Unknown"));
  275. }
  276. wstring ws_buf = buf;
  277. //是否是要找的设备类型
  278. if(ws_buf != strUSBPrint && ws_buf != strUSBPrint_EN)
  279. {
  280. LOG_INFO("not good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  281. continue;
  282. }
  283. LOG_INFO("good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  284. ifData.cbSize = sizeof(ifData);
  285. // 枚舉符合該GUID的設備接口
  286. bResult = ::SetupDiEnumDeviceInterfaces(
  287. hDevInfoSet, // 設備信息集句柄
  288. NULL, // 不需額外的設備描述
  289. lpGuid, // GUID
  290. (ULONG)nTotle, // 設備信息集里的設備序號
  291. &ifData); // 設備接口信息
  292. if(bResult)
  293. {
  294. // 取得该设备接口的细节(设备路径)
  295. bResult = SetupDiGetInterfaceDeviceDetail(
  296. hDevInfoSet, // 设备信息集句柄
  297. &ifData, // 设备接口信息
  298. pDetail, // 设备接口细节(设备路径)
  299. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  300. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  301. NULL); // 不需额外的设备描述
  302. if(bResult)
  303. {
  304. wstring DevicePath = pDetail->DevicePath;
  305. wstring vid = DevicePath.substr(DevicePath.find(_T("vid_"), 0) + 4, 4);
  306. wstring pid = DevicePath.substr(DevicePath.find(_T("pid_"), 0) + 4, 4);
  307. LOG_INFO("Vid:" << vid.c_str() << ", Pid:" << pid.c_str());
  308. if(usbType == 1 && GetPrinterType(vid, pid) == 2)
  309. {
  310. //标签打印机,暂时不处理打印
  311. LOG_INFO("标签打印机,暂时不打印!");
  312. continue;
  313. }
  314. // 复制设备路径到输出缓冲区
  315. m_usb_devices.push_back(DevicePath);
  316. // 调整计数值
  317. nCount++;
  318. LOG_INFO("Cnt = " << nCount << ",pDetail->DevicePath =" << pDetail->DevicePath);
  319. }
  320. }
  321. }
  322. else
  323. {
  324. //LOG_INFO("SetupDiEnumDeviceInfo FAILED!@");
  325. }
  326. }
  327. // 释放设备接口数据空间
  328. ::GlobalFree(pDetail);
  329. // 关闭设备信息集句柄
  330. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  331. return nCount;
  332. }
  333. /*
  334. *返回打印机的类型, 1:普通热敏打印机 2:标签打印机 3:发票打印机
  335. *这里因为不同厂家对vid pid定义不一样,有时候会出现佳博的标签打印机的vid和pid,在别的品牌上变成了热敏打印机,这里如果排除掉别的品牌的热敏打印机就无法打印了,所以这里不能轻易排除
  336. **/
  337. int CPosPrinter::GetPrinterType(wstring vid, wstring pid)
  338. {
  339. if(vid == L"6868" && pid == L"0500")
  340. {
  341. //佳博标签打印机
  342. return 2;
  343. }
  344. else if (vid == L"XXX" && pid == L"XXX")
  345. {
  346. //发票打印机
  347. return 3;
  348. }
  349. return 1;
  350. }
  351. /*
  352. *打印收银小票
  353. **/
  354. void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
  355. {
  356. LOG_INFO("_______________________________开始打印收银小票,订单编号:"<<order.m_order_num.c_str()<<"_________________________");
  357. //设置模式,后面输出数据不会错
  358. m_type = 1;
  359. std::string printer_leixing = CSetting::GetParam("setting_printer_leixing");
  360. if(printer_leixing == "auto")
  361. {
  362. InitShouyin();
  363. }
  364. else if(printer_leixing == "usb")
  365. {
  366. std::string printer_usb = CSetting::GetParam("setting_printer_usb");
  367. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  368. bool ret = InitOneUsb(ws_printer_usb);
  369. if (ret == false)
  370. {
  371. return;
  372. }
  373. }
  374. else if(printer_leixing == "bingkou")
  375. {
  376. bool ret = InitBingkou();
  377. if (ret == false)
  378. {
  379. return;
  380. }
  381. }
  382. else if(printer_leixing == "chuankou")
  383. {
  384. InitCom();
  385. }
  386. else if(printer_leixing == "wangkou")
  387. {
  388. m_type = 2;
  389. std::string wangkou_ip = CSetting::GetParam("setting_printer_wangkou_ip");
  390. //初始化连接
  391. try
  392. {
  393. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(wangkou_ip.c_str()), 9100);
  394. m_socket.connect(ep);
  395. }
  396. catch(std::exception& e)
  397. {
  398. std::string err = e.what();
  399. LOG_INFO("网口打印机连接失败,IP地址:" << wangkou_ip.c_str() << ",错误信息:" << err.c_str());
  400. MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(wangkou_ip)).c_str(), L"打印机连接失败", MB_OK);
  401. }
  402. }
  403. //读取当前收银打印机的设置
  404. std::string guige = CSetting::GetParam("setting_printer_guige");
  405. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  406. int n_lianshu = atoi(lianshu.c_str());
  407. //正式开始打印
  408. for(int i = 0; i < n_lianshu; i++)
  409. {
  410. POS_Reset();
  411. //打印店铺名字
  412. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  413. POS_TextOut(shop_name, true, true, 1);
  414. POS_FeedLine();
  415. POS_FeedLine();
  416. //打印小票自定义名称
  417. string printer_tip_name = CLewaimaiString::UTF8ToANSI(order.m_printer_tip_name);
  418. if (printer_tip_name.length() > 0)
  419. {
  420. POS_TextOut(printer_tip_name, false, true, 1);
  421. POS_FeedLine();
  422. POS_FeedLine();
  423. }
  424. //打印预约、即时配送的提醒
  425. /*
  426. bool setting_printer_peisongshijian_big = false;
  427. if (CSetting::GetParam("setting_printer_peisongshijian_big") == "1")
  428. {
  429. setting_printer_peisongshijian_big = true;
  430. }
  431. }*/
  432. if (order.m_is_selftake == "1")
  433. {
  434. string date = "【自取单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  435. POS_TextOut(date, false, true);
  436. POS_TextOut("取货", false, false);
  437. POS_FeedLine();
  438. }
  439. else
  440. {
  441. if (order.m_is_book == "1")
  442. {
  443. //预约单
  444. string date = "【预约单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  445. POS_TextOut(date, false, true);
  446. POS_TextOut("送达", false, false);
  447. POS_FeedLine();
  448. }
  449. else
  450. {
  451. //非预约单
  452. string date = "【即时单】立即送达";
  453. POS_TextOut(date, false, true, 1);
  454. POS_FeedLine();
  455. }
  456. }
  457. POS_FeedLine();
  458. //打印取货方式
  459. /*
  460. bool setting_printer_quhuo_big = false;
  461. if(CSetting::GetParam("setting_printer_quhuo_big") == "1")
  462. {
  463. setting_printer_quhuo_big = true;
  464. }
  465. if(order.m_is_selftake == "1")
  466. {
  467. POS_TextOut("到店自取订单", false, setting_printer_quhuo_big, 1);
  468. POS_FeedLine();
  469. POS_FeedLine();
  470. }
  471. else
  472. {
  473. POS_TextOut("外卖订单", false, setting_printer_quhuo_big, 1);
  474. POS_FeedLine();
  475. }
  476. POS_FeedLine();
  477. */
  478. //订单来源
  479. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  480. POS_TextOut(from_type);
  481. POS_FeedLine();
  482. //订单号
  483. bool setting_printer_dingdanhao_big = false;
  484. if(CSetting::GetParam("setting_printer_dingdanhao_big") == "1")
  485. {
  486. setting_printer_dingdanhao_big = true;
  487. }
  488. string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  489. POS_TextOut(order_num, false, setting_printer_dingdanhao_big);
  490. POS_FeedLine();
  491. //下单时间
  492. bool setting_printer_xiadanshijian_big = false;
  493. if(CSetting::GetParam("setting_printer_xiadanshijian_big") == "1")
  494. {
  495. setting_printer_xiadanshijian_big = true;
  496. }
  497. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  498. POS_TextOut(order_date, false, setting_printer_xiadanshijian_big);
  499. POS_FeedLine();
  500. //姓名
  501. bool setting_printer_name_big = false;
  502. if(CSetting::GetParam("setting_printer_name_big") == "1")
  503. {
  504. setting_printer_name_big = true;
  505. }
  506. if(order.m_is_selftake != "1")
  507. {
  508. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  509. //把下单次数也一起打印
  510. if (order.m_showordernum == "1")
  511. {
  512. name += "(第 " + CLewaimaiString::UTF8ToANSI(order.m_customer_order_total) + " 次下单)";
  513. }
  514. POS_TextOut(name, false, setting_printer_name_big);
  515. POS_FeedLine();
  516. }
  517. //电话
  518. bool setting_printer_phone_big = false;
  519. if(CSetting::GetParam("setting_printer_phone_big") == "1")
  520. {
  521. setting_printer_phone_big = true;
  522. }
  523. //看看电话是否要脱敏
  524. if (order.m_show_whole_phone == "0")
  525. {
  526. order.m_phone.replace(3, 4, 4, '*');
  527. }
  528. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  529. POS_TextOut(phone, false, setting_printer_phone_big);
  530. POS_FeedLine();
  531. //地址
  532. bool setting_printer_address_big = false;
  533. if(CSetting::GetParam("setting_printer_address_big") == "1")
  534. {
  535. setting_printer_address_big = true;
  536. }
  537. if(order.m_is_selftake != "1")
  538. {
  539. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  540. POS_TextOut(address, false, setting_printer_address_big);
  541. POS_FeedLine();
  542. }
  543. //预设选项
  544. if(order.m_order_field.size() > 1)
  545. {
  546. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  547. {
  548. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  549. POS_TextOut(order_field, false, false, 0);
  550. POS_FeedLine();
  551. }
  552. }
  553. //准备开始打印商品详情
  554. string lines;
  555. if(guige == "58")
  556. {
  557. lines = "--------------------------------";
  558. }
  559. else
  560. {
  561. lines = "------------------------------------------------";
  562. }
  563. POS_TextOut(lines);
  564. POS_FeedLine();
  565. //商品标题
  566. bool setting_printer_shangpin_big = false;
  567. if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
  568. {
  569. setting_printer_shangpin_big = true;
  570. }
  571. if(guige == "58")
  572. {
  573. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  574. }
  575. else
  576. {
  577. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  578. }
  579. POS_FeedLine();
  580. if (order.m_manzeng_name.length() > 0)
  581. {
  582. CWaimaiOrderItem manzengItem;
  583. manzengItem.m_food_name = CLewaimaiString::ANSIToUTF8("【满赠】" + CLewaimaiString::UTF8ToANSI(order.m_manzeng_name));
  584. order.m_order_items.push_back(manzengItem);
  585. }
  586. //商品内容
  587. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  588. {
  589. std::string food_name = (*it).m_food_name;
  590. std::string food_price = (*it).m_item_price;
  591. std::string quantity = (*it).m_quantity;
  592. //计算总价
  593. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  594. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  595. if (food_total_price == "0.00")
  596. {
  597. //这里是特殊处理满赠的展示
  598. food_total_price = "";
  599. }
  600. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  601. //先输出商品名字
  602. std::vector<string> m_names;
  603. if(guige == "58")
  604. {
  605. m_names = HandleFoodname(food_name, 1);
  606. }
  607. else
  608. {
  609. m_names = HandleFoodname(food_name, 2);
  610. }
  611. std::string firstLine = m_names[0];
  612. int nGuige;
  613. if(guige == "58")
  614. {
  615. nGuige = 1;
  616. }
  617. else
  618. {
  619. nGuige = 2;
  620. }
  621. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  622. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  623. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  624. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  625. POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
  626. POS_FeedLine();
  627. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  628. {
  629. POS_TextOut((*it), false, setting_printer_shangpin_big);
  630. POS_FeedLine();
  631. }
  632. }
  633. //判断是否有备注
  634. if(order.m_memo.length() > 0)
  635. {
  636. bool setting_printer_memo_big = false;
  637. if(CSetting::GetParam("setting_printer_memo_big") == "1")
  638. {
  639. setting_printer_memo_big = true;
  640. }
  641. POS_FeedLine();
  642. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  643. POS_TextOut(address, false, setting_printer_memo_big);
  644. POS_FeedLine();
  645. }
  646. //结束商品详情打印
  647. POS_TextOut(lines);
  648. POS_FeedLine();
  649. //开始打印其他的费用
  650. bool is_other_money = false;
  651. bool setting_printer_price_big = false;
  652. if(CSetting::GetParam("setting_printer_price_big") == "1")
  653. {
  654. setting_printer_price_big = true;
  655. }
  656. if(order.m_delivery > 0)
  657. {
  658. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  659. POS_TextOut(delivery, false, setting_printer_price_big);
  660. POS_FeedLine();
  661. is_other_money = true;
  662. }
  663. if(order.m_is_dabao == "1")
  664. {
  665. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  666. POS_TextOut(dabao, false, setting_printer_price_big);
  667. POS_FeedLine();
  668. is_other_money = true;
  669. }
  670. //开始显示增值服务费
  671. if(order.m_addservie.size() > 0)
  672. {
  673. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  674. {
  675. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  676. POS_TextOut(addservice, false, setting_printer_price_big);
  677. POS_FeedLine();
  678. is_other_money = true;
  679. }
  680. is_other_money = true;
  681. }
  682. if(order.m_discount < 10)
  683. {
  684. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  685. POS_TextOut(discount, false, setting_printer_price_big);
  686. POS_FeedLine();
  687. is_other_money = true;
  688. }
  689. if(order.m_is_member_discount == "1")
  690. {
  691. string member_discount = "会员优惠:-" + order.m_member_discount;
  692. POS_TextOut(member_discount, false, setting_printer_price_big);
  693. POS_FeedLine();
  694. is_other_money = true;
  695. }
  696. if(order.m_is_firstcut == "1")
  697. {
  698. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  699. POS_TextOut(firstcut, false, setting_printer_price_big);
  700. POS_FeedLine();
  701. is_other_money = true;
  702. }
  703. //满减
  704. string::size_type position = order.m_promotion.find(":");
  705. if(position != order.m_promotion.npos)
  706. {
  707. string promotion_value;
  708. promotion_value.assign(order.m_promotion, position + 1);
  709. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  710. POS_TextOut(promotion, false, setting_printer_price_big);
  711. POS_FeedLine();
  712. is_other_money = true;
  713. }
  714. if(order.m_coupon > 0)
  715. {
  716. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  717. POS_TextOut(coupon, false, setting_printer_price_big);
  718. POS_FeedLine();
  719. is_other_money = true;
  720. }
  721. if(is_other_money)
  722. {
  723. POS_TextOut(lines);
  724. POS_FeedLine();
  725. }
  726. //最后显示总价
  727. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  728. POS_FeedLine();
  729. //显示付款方式
  730. bool setting_printer_pay_big = false;
  731. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  732. {
  733. setting_printer_pay_big = true;
  734. }
  735. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  736. POS_FeedLine();
  737. POS_FeedLine();;
  738. POS_FeedLine();
  739. //准备开始打印二维码
  740. std::string order_no = order.m_order_no;
  741. order_no = CLewaimaiString::UTF8ToANSI(order_no);
  742. if (order.m_show_qiangdan_qrcode == "1")
  743. {
  744. POS_OutQRCode(order_no);
  745. POS_FeedLine();
  746. POS_FeedLine();
  747. POS_TextOut("请使用配送员app扫码绑定订单", false, false, 1);
  748. }
  749. //走纸几行再切
  750. POS_FeedLine();
  751. POS_FeedLine();
  752. POS_FeedLine();
  753. POS_FeedLine();
  754. POS_FeedLine();
  755. POS_CutPaper();
  756. }
  757. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  758. POS_Reset();
  759. //关闭设备
  760. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  761. {
  762. CloseHandle((*it).hPort);
  763. }
  764. if (m_type == 2)
  765. {
  766. m_socket.close();
  767. }
  768. LOG_INFO("_______________________________结束打印收银小票,订单编号:" << order.m_order_num.c_str() << "_________________________");
  769. LOG_INFO(" ");
  770. }
  771. void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
  772. {
  773. m_type = 1;
  774. std::string printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  775. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  776. //读取标签纸的规格和方向
  777. int biaoqian_width, biaoqian_height;
  778. std::string printer_guige = CSetting::GetParam("setting_biaoqian_printer_guige");
  779. if (printer_guige == "40*30mm")
  780. {
  781. biaoqian_width = 40;
  782. biaoqian_height = 30;
  783. }
  784. else if (printer_guige == "35*25mm")
  785. {
  786. biaoqian_width = 35;
  787. biaoqian_height = 25;
  788. }
  789. else if (printer_guige == "30*25mm")
  790. {
  791. biaoqian_width = 30;
  792. biaoqian_height = 25;
  793. }
  794. else if (printer_guige == "30*20mm")
  795. {
  796. biaoqian_width = 30;
  797. biaoqian_height = 20;
  798. }
  799. else
  800. {
  801. biaoqian_width = 40;
  802. biaoqian_height = 30;
  803. }
  804. int nDIRECTION;
  805. std::string printer_fangxiang = CSetting::GetParam("setting_biaoqian_printer_fangxiang");
  806. if (printer_fangxiang == "1")
  807. {
  808. nDIRECTION = 1;
  809. }
  810. else
  811. {
  812. nDIRECTION = 0;
  813. }
  814. //连接usb端口
  815. InitOneUsb(ws_printer_usb);
  816. //初始化标签打印机
  817. BIAOQIAN_Reset(biaoqian_width, biaoqian_height, nDIRECTION);
  818. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  819. //先计算商品的总数量
  820. int foodNum = 0;
  821. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  822. {
  823. std::string quantity = (*it).m_quantity;
  824. foodNum += atoi(quantity.c_str());
  825. }
  826. //商品内容
  827. int curFoodNum = 0;
  828. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  829. {
  830. std::string food_name = (*it).m_food_name;
  831. std::string food_price = (*it).m_item_price;
  832. std::string quantity = (*it).m_quantity;
  833. //每份商品打印一个标签
  834. int nQuantity = atoi(quantity.c_str());
  835. for(int i = 0; i < nQuantity; i++)
  836. {
  837. std::string textData = "";
  838. curFoodNum++;
  839. //打印小票自定义名称
  840. string printer_tip_name = CLewaimaiString::UTF8ToANSI(order.m_printer_tip_name);
  841. if (printer_tip_name.length() == 0)
  842. {
  843. printer_tip_name = "外卖";
  844. }
  845. std::string restaurant_number = order.m_restaurant_number;
  846. std::string order_num_info = printer_tip_name + " #" + CLewaimaiString::UTF8ToANSI(restaurant_number);
  847. //纸张宽度40mm的,是 320个点位(每mm8个),刚好可以放13个汉字,每个汉字宽度差不多是24,商品换行显示的时候,
  848. //每行最多12个汉字,也就是空24个,因此这里设置12,刚好左右各空一半
  849. textData += BIAOQIAN_TEXTGet(order_num_info, 12, 6, 1, 1);
  850. //打印份数
  851. std::string numInfo = to_string(curFoodNum) + "/" + to_string(foodNum);
  852. textData += BIAOQIAN_TEXTGet(numInfo, 180, 6, 1, 1);
  853. //每行最多显示12个汉字,这里要计算一下换行(要先转成ANSI格式)
  854. std::string handle_food_name = CLewaimaiString::UTF8ToANSI(food_name);
  855. std::vector<std::string> foodNameVector = HandleBiaoqianFoodname(handle_food_name, biaoqian_width);
  856. int nRow = 0;
  857. //考虑是否用大号字体
  858. bool is_big_name = false;
  859. if(foodNameVector.size() <= 2)
  860. {
  861. //不超过2行,可以用大的字体
  862. is_big_name = true;
  863. }
  864. //LOG_INFO("foodNameVector size:" << foodNameVector.size());
  865. for(std::vector<std::string>::iterator it = foodNameVector.begin(); it != foodNameVector.end(); it++)
  866. {
  867. //LOG_INFO("nRow:" << nRow << ", 准备打印商品名字:" << (*it).c_str());
  868. if(is_big_name)
  869. {
  870. textData += BIAOQIAN_TEXTGet(*it, 12, 42 + 60 * nRow, 1, 2);
  871. }
  872. else
  873. {
  874. textData += BIAOQIAN_TEXTGet(*it, 12, 42 + 30 * nRow, 1, 1);
  875. }
  876. nRow++;
  877. }
  878. int nPriceY;
  879. if (biaoqian_height == 30)
  880. {
  881. nPriceY = 170;
  882. }
  883. else if (biaoqian_height == 25)
  884. {
  885. nPriceY = 130;
  886. }
  887. else if (biaoqian_height == 20)
  888. {
  889. nPriceY = 90;
  890. }
  891. //打印价格
  892. std::string priceInfo = CLewaimaiString::UTF8ToANSI(food_price) + "元";
  893. textData += BIAOQIAN_TEXTGet(priceInfo, 12, nPriceY, 1, 1);
  894. //打印订单号
  895. std::string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  896. textData += BIAOQIAN_TEXTGet(order_num, 12, nPriceY + 30, 1, 1);
  897. WriteData(textData);
  898. BIAOQIAN_PRINT();
  899. BIAOQIAN_CLS();
  900. }
  901. }
  902. }
  903. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  904. {
  905. //设置模式,后面输出数据不会错
  906. m_type = 2;
  907. //读取厨房打印机信息
  908. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  909. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  910. {
  911. ChufangPrinter printer = *it;
  912. std::string ip = printer.ip;
  913. //初始化连接
  914. try
  915. {
  916. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  917. m_socket.connect(ep);
  918. }
  919. catch(std::exception& e)
  920. {
  921. std::string err = e.what();
  922. LOG_INFO("厨房网口打印机连接失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  923. MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(ip)).c_str(), L"打印机连接失败", MB_OK);
  924. //连接失败了,处理下一个厨房打印机
  925. continue;
  926. }
  927. std::string guige = printer.guige;
  928. std::string fendan = printer.fendan;
  929. std::string fenlei = printer.fenlei;
  930. std::string fenlei_ids = printer.fenlei_ids;
  931. std::vector<CWaimaiOrderItem> cur_printer_use;
  932. if (fenlei == "0")
  933. {
  934. cur_printer_use = order.m_order_items;
  935. }
  936. else
  937. {
  938. //如果开启了分类打印,就要比对了
  939. std::map<string, bool> ids_map;
  940. std::vector<string> ids = CLewaimaiString::Split(fenlei_ids, ",");
  941. for (std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
  942. {
  943. ids_map[(*it)] = true;
  944. }
  945. for (std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  946. {
  947. if (ids_map.find((*it).m_type_id) != ids_map.end())
  948. {
  949. //如果当前商品的分类,在厨房打印机设置的分类里面,才加入
  950. cur_printer_use.push_back(*it);
  951. }
  952. }
  953. }
  954. if (cur_printer_use.size() == 0)
  955. {
  956. //如果这个厨房打印机没有商品可以打印(开启了商品分类打印可能出现),就直接跳过
  957. m_socket.close();
  958. continue;
  959. }
  960. if(fendan == "0")
  961. {
  962. POS_Reset();
  963. POS_TextOut("派工单", true, true, 1);
  964. POS_FeedLine();
  965. POS_FeedLine();
  966. string order_type = "#" + order.m_restaurant_number + " ";
  967. if(order.m_is_selftake == "1")
  968. {
  969. POS_TextOut(order_type + "到店自取订单", false, true, 1);
  970. POS_FeedLine();
  971. POS_FeedLine();
  972. }
  973. else
  974. {
  975. POS_TextOut(order_type + "外卖订单", false, true, 1);
  976. POS_FeedLine();
  977. POS_FeedLine();
  978. }
  979. //预设选项
  980. if(order.m_order_field.size() > 1)
  981. {
  982. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  983. {
  984. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  985. POS_TextOut(order_field, false, true, 0);
  986. POS_FeedLine();
  987. }
  988. }
  989. //准备开始打印商品详情
  990. string lines;
  991. if(guige == "58")
  992. {
  993. lines = "--------------------------------";
  994. }
  995. else
  996. {
  997. lines = "------------------------------------------------";
  998. }
  999. POS_TextOut(lines);
  1000. POS_FeedLine();
  1001. //商品标题
  1002. if(guige == "58")
  1003. {
  1004. POS_TextOut("商品 单价 数量 金额", false, true);
  1005. }
  1006. else
  1007. {
  1008. POS_TextOut("商品 单价 数量 金额", false, true);
  1009. }
  1010. POS_FeedLine();
  1011. //商品内容
  1012. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  1013. {
  1014. std::string food_name = (*it).m_food_name;
  1015. std::string food_price = (*it).m_item_price;
  1016. std::string quantity = (*it).m_quantity;
  1017. //计算总价
  1018. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  1019. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  1020. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  1021. //先输出商品名字
  1022. std::vector<string> m_names;
  1023. if(guige == "58")
  1024. {
  1025. m_names = HandleFoodname(food_name, 1);
  1026. }
  1027. else
  1028. {
  1029. m_names = HandleFoodname(food_name, 2);
  1030. }
  1031. std::string firstLine = m_names[0];
  1032. int nGuige;
  1033. if(guige == "58")
  1034. {
  1035. nGuige = 1;
  1036. }
  1037. else
  1038. {
  1039. nGuige = 2;
  1040. }
  1041. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  1042. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  1043. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  1044. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  1045. POS_TextOut(firstLineShow, false, true, 0);
  1046. POS_FeedLine();
  1047. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  1048. {
  1049. POS_TextOut((*it), false, true, 0);
  1050. POS_FeedLine();
  1051. }
  1052. }
  1053. //判断是否有备注
  1054. if(order.m_memo.length() > 0)
  1055. {
  1056. POS_FeedLine();
  1057. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  1058. POS_TextOut(address, false, true, 0);
  1059. POS_FeedLine();
  1060. }
  1061. //结束商品详情打印
  1062. POS_TextOut(lines);
  1063. POS_FeedLine();
  1064. POS_FeedLine();
  1065. POS_FeedLine();
  1066. POS_FeedLine();
  1067. POS_FeedLine();
  1068. POS_FeedLine();
  1069. POS_CutPaper();
  1070. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1071. POS_Reset();
  1072. }
  1073. else
  1074. {
  1075. POS_Reset();
  1076. //分单模式下,每个商品打印一张单
  1077. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  1078. {
  1079. POS_TextOut("派工单", true, true, 1);
  1080. POS_FeedLine();
  1081. POS_FeedLine();
  1082. string order_type = "#" + order.m_restaurant_number + " ";
  1083. if(order.m_is_selftake == "1")
  1084. {
  1085. POS_TextOut(order_type + "到店自取订单", false, true, 1);
  1086. POS_FeedLine();
  1087. POS_FeedLine();
  1088. }
  1089. else
  1090. {
  1091. POS_TextOut(order_type + "外卖订单", false, true, 1);
  1092. POS_FeedLine();
  1093. POS_FeedLine();
  1094. }
  1095. //预设选项
  1096. if(order.m_order_field.size() > 1)
  1097. {
  1098. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  1099. {
  1100. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  1101. POS_TextOut(order_field, false, true, 0);
  1102. POS_FeedLine();
  1103. }
  1104. }
  1105. //准备开始打印商品详情
  1106. string lines;
  1107. if(guige == "58")
  1108. {
  1109. lines = "--------------------------------";
  1110. }
  1111. else
  1112. {
  1113. lines = "------------------------------------------------";
  1114. }
  1115. POS_TextOut(lines);
  1116. POS_FeedLine();
  1117. //商品标题
  1118. if(guige == "58")
  1119. {
  1120. POS_TextOut("商品 单价 数量 金额", false, true);
  1121. }
  1122. else
  1123. {
  1124. POS_TextOut("商品 单价 数量 金额", false, true);
  1125. }
  1126. POS_FeedLine();
  1127. std::string food_name = (*it).m_food_name;
  1128. std::string food_price = (*it).m_item_price;
  1129. std::string quantity = (*it).m_quantity;
  1130. //计算总价
  1131. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  1132. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  1133. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  1134. //先输出商品名字
  1135. std::vector<string> m_names;
  1136. if(guige == "58")
  1137. {
  1138. m_names = HandleFoodname(food_name, 1);
  1139. }
  1140. else
  1141. {
  1142. m_names = HandleFoodname(food_name, 2);
  1143. }
  1144. std::string firstLine = m_names[0];
  1145. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  1146. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  1147. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  1148. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  1149. POS_TextOut(firstLineShow, false, true, 0);
  1150. POS_FeedLine();
  1151. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  1152. {
  1153. POS_TextOut((*it), false, true, 0);
  1154. POS_FeedLine();
  1155. }
  1156. //判断是否有备注
  1157. if(order.m_memo.length() > 0)
  1158. {
  1159. POS_FeedLine();
  1160. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  1161. POS_TextOut(address, false, true, 0);
  1162. POS_FeedLine();
  1163. }
  1164. //结束商品详情打印
  1165. POS_TextOut(lines);
  1166. POS_FeedLine();
  1167. POS_FeedLine();
  1168. POS_FeedLine();
  1169. POS_FeedLine();
  1170. POS_FeedLine();
  1171. POS_FeedLine();
  1172. POS_CutPaper();
  1173. }
  1174. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1175. POS_Reset();
  1176. }
  1177. m_socket.close();
  1178. }
  1179. }
  1180. std::vector<std::wstring> CPosPrinter::getUsbDevices()
  1181. {
  1182. return m_usb_devices;
  1183. }
  1184. int CPosPrinter::WriteData(string msg)
  1185. {
  1186. return WriteBuf((unsigned char*)msg.c_str(), msg.length());
  1187. }
  1188. /*
  1189. *这个函数只负责把数据拼接到打印缓存里面,不负责实际的发送数据
  1190. **/
  1191. int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
  1192. {
  1193. if(m_type == 1)
  1194. {
  1195. //USB、串口、并口,走这里
  1196. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  1197. {
  1198. HANDLE hPort = (*it).hPort;
  1199. //同步写数据
  1200. DWORD dwWrite;
  1201. BOOL ret = WriteFile(hPort, buf, len, &dwWrite, NULL);
  1202. if (!ret)
  1203. {
  1204. LOG_INFO("WriteFile Failed!, hPort:"<<hPort<<", type:"<<(*it).type);
  1205. }
  1206. }
  1207. return 1;
  1208. }
  1209. else
  1210. {
  1211. //网口走这里
  1212. try
  1213. {
  1214. m_socket.write_some(boost::asio::buffer(buf, len));
  1215. }
  1216. catch(const std::exception& e)
  1217. {
  1218. LOG_INFO("网口打印机发送消息失败,错误信息:" << e.what());
  1219. }
  1220. return 1;
  1221. }
  1222. }
  1223. /*
  1224. *测试打印机是否连接
  1225. **/
  1226. bool CPosPrinter::PortTest(HANDLE hPort)
  1227. {
  1228. //这里的话目前USB、串口、并口用的都是同步读写模式,有的打印机(比如一些发票打印机)CreateFile会成功,但是WriteFile会一直阻塞卡着,如果检测的话,
  1229. //在这里会一直卡住,因此这里的检测就不检测了,默认所有的都是连通的
  1230. //因此,对于智能识别模式,可能会出现无法打印(因为被某些usb打印设备阻塞),这种情况就只能手动选择USB模式,然后重新启动软件
  1231. return true;
  1232. //标签打印机的查询状态指令
  1233. char chInitCode[2] = { 0x1b, 0x40};
  1234. DWORD dwWrite;
  1235. BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, NULL);
  1236. if(rc)
  1237. {
  1238. LOG_INFO("porttest success, handle:" << hPort);
  1239. return true;
  1240. }
  1241. else
  1242. {
  1243. LOG_INFO("porttest fail, handle:" << hPort);
  1244. return true;
  1245. }
  1246. return false;
  1247. }
  1248. int CPosPrinter::POS_Reset(void)
  1249. {
  1250. unsigned char s[2] = {0x1B, 0x40};
  1251. WriteBuf(s, 2);
  1252. return 0;
  1253. }
  1254. int CPosPrinter::POS_FeedLine(void)
  1255. {
  1256. unsigned char s[1] = {0x0A};
  1257. WriteBuf(s, 1);
  1258. return 0;
  1259. }
  1260. int CPosPrinter::POS_Feed(void)
  1261. {
  1262. unsigned char s[3] = { 0x1B, 0x4A, 0x00 };
  1263. WriteBuf(s, 3);
  1264. return 0;
  1265. }
  1266. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  1267. {
  1268. string s;
  1269. s = "\x1D\x50\xB4\xB4";
  1270. WriteData(s);
  1271. s = "\x1B\x53";
  1272. WriteData(s);
  1273. return 0;
  1274. }
  1275. int CPosPrinter::POS_SET_MOVE_X()
  1276. {
  1277. unsigned char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  1278. WriteBuf(s2, 6);
  1279. return 0;
  1280. }
  1281. int CPosPrinter::POS_MOVE_X()
  1282. {
  1283. unsigned char s[1] = { 0x09 };
  1284. WriteBuf(s, 1);
  1285. return 0;
  1286. }
  1287. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  1288. {
  1289. unsigned char cx = (unsigned char)(x);
  1290. unsigned char cy = (unsigned char)(y);
  1291. unsigned char s1[4] = { 0x1B, 0x24, cx, cy };
  1292. WriteBuf(s1, 4);
  1293. return 0;
  1294. }
  1295. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  1296. {
  1297. unsigned char cx = (unsigned char)(x);
  1298. unsigned char cy = (unsigned char)(y);
  1299. unsigned char s1[4] = { 0x1D, 0x57, cx, cy };
  1300. WriteBuf(s1, 4);
  1301. return 0;
  1302. }
  1303. /*
  1304. *align_type:0 左对齐 1 居中对齐 2右对齐
  1305. **/
  1306. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1307. {
  1308. if(is_double_width && is_double_height)
  1309. {
  1310. unsigned char s1[3] = { 0x1B, 0x21, 0x30 };
  1311. WriteBuf(s1, 3);
  1312. unsigned char s2[3] = { 0x1C, 0x21, 0x0c };
  1313. WriteBuf(s2, 3);
  1314. }
  1315. else if(is_double_width && !is_double_height)
  1316. {
  1317. unsigned char s1[3] = { 0x1B, 0x21, 0x20 };
  1318. WriteBuf(s1, 3);
  1319. unsigned char s2[3] = { 0x1C, 0x21, 0x04 };
  1320. WriteBuf(s2, 3);
  1321. }
  1322. else if(!is_double_width && is_double_height)
  1323. {
  1324. unsigned char s1[3] = { 0x1B, 0x21, 0x10 };
  1325. WriteBuf(s1, 3);
  1326. unsigned char s2[3] = { 0x1C, 0x21, 0x08 };
  1327. WriteBuf(s2, 3);
  1328. }
  1329. else
  1330. {
  1331. unsigned char s1[3] = { 0x1B, 0x21, 0x00 };
  1332. WriteBuf(s1, 3);
  1333. unsigned char s2[3] = { 0x1C, 0x57, 0x00 };
  1334. WriteBuf(s2, 3);
  1335. }
  1336. if(align_type == 0)
  1337. {
  1338. unsigned char s1[3] = { 0x1B, 0x61, 0x00 };
  1339. WriteBuf(s1, 3);
  1340. }
  1341. else if(align_type == 1)
  1342. {
  1343. unsigned char s1[3] = { 0x1B, 0x61, 0x01 };
  1344. WriteBuf(s1, 3);
  1345. }
  1346. else if(align_type == 2)
  1347. {
  1348. unsigned char s1[3] = { 0x1B, 0x61, 0x02 };
  1349. WriteBuf(s1, 3);
  1350. }
  1351. else
  1352. {
  1353. }
  1354. WriteData(abc);
  1355. return 0;
  1356. }
  1357. /*
  1358. *打印机切纸,直接切不走纸
  1359. **/
  1360. int CPosPrinter::POS_CutPaper()
  1361. {
  1362. unsigned char s[4] = { 0x1D, 0x56, 0x01};
  1363. WriteBuf(s, 3);
  1364. return 0;
  1365. }
  1366. /*
  1367. *先转换成ANSI格式才能传进来
  1368. **/
  1369. void CPosPrinter::POS_OutQRCode(std::string dataString)
  1370. {
  1371. QRcode * qrCode = nullptr;
  1372. qrCode = QRcode_encodeString(dataString.c_str(), 0, QR_ECLEVEL_H, QR_MODE_8, 1);
  1373. if (qrCode == nullptr)
  1374. {
  1375. printf("error occur\n");
  1376. return ;
  1377. }
  1378. unsigned int unWidth = qrCode->width;
  1379. unsigned int unWidthAdjusted = unWidth * 8;
  1380. unsigned int unHeightAjusted = unWidthAdjusted;
  1381. unsigned char* realData = new unsigned char[unWidthAdjusted * unHeightAjusted];
  1382. memset(realData, 0, unWidthAdjusted * unHeightAjusted);
  1383. unsigned char* pSourceData = qrCode->data;
  1384. for (unsigned int i = 0; i < unHeightAjusted; i++)
  1385. {
  1386. int realHeight = i / 8;
  1387. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1388. {
  1389. int realWeight = j / 8;
  1390. if (pSourceData[unWidth * realHeight + realWeight] & 1)
  1391. {
  1392. //表示这个像素要打印
  1393. realData[unWidthAdjusted * i + j] = 1;
  1394. }
  1395. else
  1396. {
  1397. realData[unWidthAdjusted * i + j] = 0;
  1398. }
  1399. }
  1400. }
  1401. //设置行间距为0
  1402. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1403. WriteBuf(data, 3);
  1404. //设置图像居中对齐
  1405. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1406. WriteBuf(data2, 3);
  1407. //选择位图模式
  1408. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1409. escBmp[3] = (unsigned char)(unWidthAdjusted % 256);
  1410. escBmp[4] = (unsigned char)(unWidthAdjusted / 256);
  1411. //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
  1412. unsigned char change_line_tag = '\n';
  1413. //存储每行数据的结构
  1414. unsigned char* dataTmp = new unsigned char[unWidthAdjusted * 3];
  1415. //循环图片像素打印图片
  1416. for (unsigned int i = 0; i < (unHeightAjusted / 24 + 1); i++)
  1417. {
  1418. //设置模式为位图模式
  1419. WriteBuf(escBmp, 5);
  1420. //重新开始打印一行,置零
  1421. memset(dataTmp, 0, unWidthAdjusted * 3);
  1422. //循环宽
  1423. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1424. {
  1425. for (unsigned int k = 0; k < 24; k++)
  1426. {
  1427. //找到有像素的区域,也就是未超出位图高度的区域
  1428. if (((i * 24) + k) < unHeightAjusted)
  1429. {
  1430. if (realData[unWidthAdjusted * (i * 24 + k) + j] == 1)
  1431. {
  1432. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1433. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1434. }
  1435. }
  1436. }
  1437. }
  1438. WriteBuf(dataTmp, unWidthAdjusted * 3);
  1439. //换行符,这里必须要发这个,不然图片会乱
  1440. WriteBuf(&change_line_tag, 1);
  1441. }
  1442. WriteBuf(&change_line_tag, 1);
  1443. //还原默认的行间距
  1444. unsigned char data3[2] = { 0x1B, 0x32 };
  1445. WriteBuf(data3, 2);
  1446. delete[] dataTmp;
  1447. QRcode_free(qrCode);
  1448. delete[] realData;
  1449. return ;
  1450. }
  1451. void CPosPrinter::POS_OutBmp(std::wstring ImagePath)
  1452. {
  1453. //初始化
  1454. POS_Reset();
  1455. //设置行间距为0
  1456. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1457. WriteBuf(data, 3);
  1458. //设置图像居中对齐
  1459. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1460. WriteBuf(data2, 3);
  1461. CBitmapHelper helper;
  1462. helper.LoadImage(ImagePath.c_str());
  1463. Bitmap* bmp = helper.getBmp();
  1464. //先进行图片预处理
  1465. unsigned int nWidth = bmp->GetWidth();
  1466. unsigned int nHeight = bmp->GetHeight();
  1467. //压缩尺寸
  1468. if (nWidth > 380)
  1469. {
  1470. int newWidth = 380;
  1471. int newHeight = (int)(nHeight / (nWidth / 380.0));
  1472. helper.ScaleBitmap(newWidth, newHeight);;
  1473. nWidth = newWidth;
  1474. nHeight = newHeight;
  1475. }
  1476. //二值化
  1477. helper.Image2Values();
  1478. //获得最新的图像指针
  1479. bmp = helper.getBmp();
  1480. //选择位图模式
  1481. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1482. escBmp[3] = (unsigned char)(nWidth % 256);
  1483. escBmp[4] = (unsigned char)(nWidth / 256);
  1484. Gdiplus::Color pixelColor;
  1485. //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
  1486. unsigned char change_line_tag = '\n';
  1487. //存储每行数据的结构
  1488. unsigned char* dataTmp = new unsigned char[nWidth * 3];
  1489. //循环图片像素打印图片
  1490. for (unsigned int i = 0; i < (nHeight / 24 + 1); i++)
  1491. {
  1492. //设置模式为位图模式
  1493. WriteBuf(escBmp, 5);
  1494. //重新开始打印一行,置零
  1495. memset(dataTmp, 0, nWidth * 3);
  1496. //循环宽
  1497. for (unsigned int j = 0; j < nWidth; j++)
  1498. {
  1499. for (unsigned int k = 0; k < 24; k++)
  1500. {
  1501. //找到有像素的区域,也就是未超出位图高度的区域
  1502. if (((i * 24) + k) < nHeight)
  1503. {
  1504. bmp->GetPixel(j, (i * 24) + k, &pixelColor);
  1505. unsigned char r = pixelColor.GetR();
  1506. if (r == 0)
  1507. {
  1508. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1509. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1510. }
  1511. }
  1512. }
  1513. }
  1514. WriteBuf(dataTmp, nWidth * 3);
  1515. //换行符,这里必须要发这个,不然图片会乱
  1516. WriteBuf(&change_line_tag, 1);
  1517. }
  1518. WriteBuf(&change_line_tag, 1);
  1519. //还原默认的行间距
  1520. unsigned char data3[2] = { 0x1B, 0x32 };
  1521. WriteBuf(data3, 2);
  1522. delete[] dataTmp;
  1523. }
  1524. void CPosPrinter::BIAOQIAN_Reset(int nWidth, int nHeight, int nDirection)
  1525. {
  1526. char endTag[3] = {0x0d, 0x0a, 0x00};
  1527. std::string size = "SIZE " + to_string(nWidth) + " mm," + to_string(nHeight) + " mm";
  1528. size += endTag;
  1529. std::string gap = "GAP 2 mm,0 mm";
  1530. gap += endTag;
  1531. std::string speed = "SPEED 4.0";
  1532. speed += endTag;
  1533. std::string density = "DENSITY 12";
  1534. density += endTag;
  1535. std::string DIRECTION = "DIRECTION " + to_string(nDirection);
  1536. DIRECTION += endTag;
  1537. std::string REFERENCE = "REFERENCE 0,0";
  1538. REFERENCE += endTag;
  1539. std::string PEER = "SET PEEL OFF";
  1540. PEER += endTag;
  1541. std::string TEAR = "SET TEAR ON";
  1542. TEAR += endTag;
  1543. std::string cls = "CLS";
  1544. cls += endTag;
  1545. std::string data = size + gap + speed + density + DIRECTION + REFERENCE + PEER + TEAR + cls;
  1546. WriteData(data);
  1547. }
  1548. /*
  1549. *前进一张纸
  1550. **/
  1551. void CPosPrinter::BIAOQIAN_FORMFEED()
  1552. {
  1553. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1554. std::string formfeed = "FORMFEED";
  1555. formfeed += endTag;
  1556. WriteData(formfeed);
  1557. }
  1558. void CPosPrinter::BIAOQIAN_TEXTOUT(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1559. {
  1560. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1561. string text = "TEXT ";
  1562. text += to_string(x) + "," + to_string(y) + ",";
  1563. text += "\"TSS24.BF2\",";
  1564. text += to_string(0) + ",";
  1565. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1566. text += "\"" + content + "\"";
  1567. text += endTag;
  1568. WriteData(text);
  1569. }
  1570. std::string CPosPrinter::BIAOQIAN_TEXTGet(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1571. {
  1572. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1573. string text = "TEXT ";
  1574. text += to_string(x) + "," + to_string(y) + ",";
  1575. text += "\"TSS24.BF2\",";
  1576. text += to_string(0) + ",";
  1577. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1578. text += "\"" + content + "\"";
  1579. text += endTag;
  1580. return text;
  1581. }
  1582. void CPosPrinter::BIAOQIAN_PRINT()
  1583. {
  1584. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1585. std::string print = "PRINT 1";
  1586. print += endTag;
  1587. WriteData(print);
  1588. }
  1589. void CPosPrinter::BIAOQIAN_CLS()
  1590. {
  1591. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1592. std::string cls = "CLS";
  1593. cls += endTag;
  1594. WriteData(cls);
  1595. }
  1596. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1597. {
  1598. nHanzi = 0;
  1599. nZimu = 0;
  1600. const char* buffer = s.c_str();
  1601. while(*buffer != '\0')
  1602. {
  1603. if(!(*buffer >= 0 && *buffer <= 127))
  1604. {
  1605. //汉字
  1606. buffer++;
  1607. buffer++;
  1608. nHanzi++;
  1609. }
  1610. else
  1611. {
  1612. //字母
  1613. buffer++;
  1614. nZimu++;
  1615. }
  1616. }
  1617. }
  1618. /*
  1619. *规格 1:58mm 2:80mm
  1620. **/
  1621. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1622. {
  1623. std::vector<std::string> newnameArray;
  1624. int nHanzi, nZimu;
  1625. CalWord(oldname, nHanzi, nZimu);
  1626. int nWidth = nHanzi * 2 + nZimu;
  1627. int maxWidth;
  1628. if(guige == 1)
  1629. {
  1630. maxWidth = 15;
  1631. }
  1632. else
  1633. {
  1634. maxWidth = 28;
  1635. }
  1636. if(nWidth <= maxWidth)
  1637. {
  1638. //对于nWidth补空格
  1639. for(int i = 0; i < maxWidth - nWidth; i++)
  1640. {
  1641. oldname += " ";
  1642. }
  1643. newnameArray.push_back(oldname);
  1644. return newnameArray;
  1645. }
  1646. //宽度大于15的情况,如果超过了,就要进行换行截取
  1647. const char* s = oldname.c_str();
  1648. int nTmp = 0;
  1649. int nTotal = 0;
  1650. while(*s != '\0')
  1651. {
  1652. if(!(*s >= 0 && *s <= 127))
  1653. {
  1654. //汉字的情况
  1655. s++;
  1656. nTmp++;
  1657. nTotal++;
  1658. s++;
  1659. nTmp++;
  1660. nTotal++;
  1661. }
  1662. else
  1663. {
  1664. //字母的情况
  1665. s++;
  1666. nTmp++;
  1667. nTotal++;
  1668. }
  1669. if(nTmp == maxWidth)
  1670. {
  1671. //这里开始要换行了
  1672. string newnameItem;
  1673. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1674. newnameArray.push_back(newnameItem);
  1675. nTmp = 0;
  1676. }
  1677. else if(nTmp == maxWidth - 1)
  1678. {
  1679. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1680. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1681. {
  1682. //下一个是中文,也要换行了,补齐一个空格
  1683. string newnameItem;
  1684. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1685. newnameItem += " ";
  1686. newnameArray.push_back(newnameItem);
  1687. nTmp = 0;
  1688. }
  1689. }
  1690. }
  1691. if(nTmp > 0)
  1692. {
  1693. //处理分隔后的最后一行
  1694. string newnameItem;
  1695. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1696. for(int i = 0; i < maxWidth - nTmp; i++)
  1697. {
  1698. newnameItem += " ";
  1699. }
  1700. newnameArray.push_back(newnameItem);
  1701. }
  1702. return newnameArray;
  1703. }
  1704. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1705. {
  1706. double price = atof(oldprice.c_str());
  1707. if(price < 10.00)
  1708. {
  1709. //单位数
  1710. if(guige == 1)
  1711. {
  1712. return " " + oldprice;
  1713. }
  1714. else
  1715. {
  1716. return " " + oldprice;
  1717. }
  1718. }
  1719. else if(price > 9.99 && price < 100.00)
  1720. {
  1721. //双位数
  1722. if(guige == 1)
  1723. {
  1724. return " " + oldprice;
  1725. }
  1726. else
  1727. {
  1728. return " " + oldprice;
  1729. }
  1730. }
  1731. else if(price > 99.99 && price < 1000.00)
  1732. {
  1733. //三位数
  1734. if(guige == 1)
  1735. {
  1736. return " " + oldprice;
  1737. }
  1738. else
  1739. {
  1740. return " " + oldprice;
  1741. }
  1742. }
  1743. else
  1744. {
  1745. //四位数
  1746. if(guige == 1)
  1747. {
  1748. return " " + oldprice;
  1749. }
  1750. else
  1751. {
  1752. return " " + oldprice;
  1753. }
  1754. }
  1755. }
  1756. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1757. {
  1758. int n = atoi(oldquantity.c_str());
  1759. if(n < 10)
  1760. {
  1761. if(guige == 1)
  1762. {
  1763. return " " + oldquantity;
  1764. }
  1765. else
  1766. {
  1767. return " " + oldquantity;
  1768. }
  1769. }
  1770. else if(n >= 10 && n <= 99)
  1771. {
  1772. if(guige == 1)
  1773. {
  1774. return " " + oldquantity;
  1775. }
  1776. else
  1777. {
  1778. return " " + oldquantity;
  1779. }
  1780. }
  1781. else
  1782. {
  1783. if(guige == 1)
  1784. {
  1785. return "" + oldquantity;
  1786. }
  1787. else
  1788. {
  1789. return " " + oldquantity;
  1790. }
  1791. }
  1792. }
  1793. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1794. {
  1795. double price = atof(oldprice.c_str());
  1796. if(price < 10.00)
  1797. {
  1798. //单位数
  1799. if(guige == 1)
  1800. {
  1801. return " " + oldprice;
  1802. }
  1803. else
  1804. {
  1805. return " " + oldprice;
  1806. }
  1807. }
  1808. else if(price > 9.99 && price < 100.00)
  1809. {
  1810. //双位数
  1811. if(guige == 1)
  1812. {
  1813. return " " + oldprice;
  1814. }
  1815. else
  1816. {
  1817. return " " + oldprice;
  1818. }
  1819. }
  1820. else if(price > 99.99 && price < 1000.00)
  1821. {
  1822. //三位数
  1823. if(guige == 1)
  1824. {
  1825. return " " + oldprice;
  1826. }
  1827. else
  1828. {
  1829. return " " + oldprice;
  1830. }
  1831. }
  1832. else
  1833. {
  1834. //四位数
  1835. if(guige == 1)
  1836. {
  1837. return " " + oldprice;
  1838. }
  1839. else
  1840. {
  1841. return " " + oldprice;
  1842. }
  1843. }
  1844. }
  1845. std::vector<std::string> CPosPrinter::HandleBiaoqianFoodname(std::string oldname, int nBiaoqianWidth)
  1846. {
  1847. std::vector<std::string> newnameArray;
  1848. int nHanzi, nZimu;
  1849. CalWord(oldname, nHanzi, nZimu);
  1850. //文字内容的实际宽度,1个汉字算2个宽度,1个英文或者数字、标点算1个(40mm宽度最多打印12个汉字 )
  1851. int nWidth = nHanzi * 2 + nZimu;
  1852. //40 *30mmm的标签,宽度最大24
  1853. int maxWidth;
  1854. if (nBiaoqianWidth == 40)
  1855. {
  1856. maxWidth = 24;
  1857. }
  1858. else if (nBiaoqianWidth == 35)
  1859. {
  1860. maxWidth = 21;
  1861. }
  1862. else if (nBiaoqianWidth == 30)
  1863. {
  1864. maxWidth = 18;
  1865. }
  1866. else
  1867. {
  1868. maxWidth = 24;
  1869. }
  1870. if(nWidth <= maxWidth)
  1871. {
  1872. //对于nWidth补空格
  1873. for(int i = 0; i < maxWidth - nWidth; i++)
  1874. {
  1875. oldname += " ";
  1876. }
  1877. newnameArray.push_back(oldname);
  1878. return newnameArray;
  1879. }
  1880. //宽度大于15的情况,如果超过了,就要进行换行截取
  1881. const char* s = oldname.c_str();
  1882. int nTmp = 0;
  1883. int nTotal = 0;
  1884. while(*s != '\0')
  1885. {
  1886. if(!(*s >= 0 && *s <= 127))
  1887. {
  1888. //汉字的情况
  1889. s++;
  1890. nTmp++;
  1891. nTotal++;
  1892. s++;
  1893. nTmp++;
  1894. nTotal++;
  1895. }
  1896. else
  1897. {
  1898. //字母的情况
  1899. s++;
  1900. nTmp++;
  1901. nTotal++;
  1902. }
  1903. if(nTmp == maxWidth)
  1904. {
  1905. //这里开始要换行了
  1906. string newnameItem;
  1907. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1908. newnameArray.push_back(newnameItem);
  1909. nTmp = 0;
  1910. }
  1911. else if(nTmp == maxWidth - 1)
  1912. {
  1913. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1914. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1915. {
  1916. //下一个是中文,也要换行了,补齐一个空格
  1917. string newnameItem;
  1918. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1919. newnameItem += " ";
  1920. newnameArray.push_back(newnameItem);
  1921. nTmp = 0;
  1922. }
  1923. }
  1924. }
  1925. if(nTmp > 0)
  1926. {
  1927. //处理分隔后的最后一行
  1928. string newnameItem;
  1929. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1930. for(int i = 0; i < maxWidth - nTmp; i++)
  1931. {
  1932. newnameItem += " ";
  1933. }
  1934. newnameArray.push_back(newnameItem);
  1935. }
  1936. return newnameArray;
  1937. }