CPosPrinter.cpp 60 KB

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