CPosPrinter.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  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. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  524. POS_TextOut(phone, false, setting_printer_phone_big);
  525. POS_FeedLine();
  526. //地址
  527. bool setting_printer_address_big = false;
  528. if(CSetting::GetParam("setting_printer_address_big") == "1")
  529. {
  530. setting_printer_address_big = true;
  531. }
  532. if(order.m_is_selftake != "1")
  533. {
  534. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  535. POS_TextOut(address, false, setting_printer_address_big);
  536. POS_FeedLine();
  537. }
  538. //预设选项
  539. if(order.m_order_field.size() > 1)
  540. {
  541. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  542. {
  543. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  544. POS_TextOut(order_field, false, false, 0);
  545. POS_FeedLine();
  546. }
  547. }
  548. //准备开始打印商品详情
  549. string lines;
  550. if(guige == "58")
  551. {
  552. lines = "--------------------------------";
  553. }
  554. else
  555. {
  556. lines = "------------------------------------------------";
  557. }
  558. POS_TextOut(lines);
  559. POS_FeedLine();
  560. //商品标题
  561. bool setting_printer_shangpin_big = false;
  562. if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
  563. {
  564. setting_printer_shangpin_big = true;
  565. }
  566. if(guige == "58")
  567. {
  568. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  569. }
  570. else
  571. {
  572. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  573. }
  574. POS_FeedLine();
  575. if (order.m_manzeng_name.length() > 0)
  576. {
  577. CWaimaiOrderItem manzengItem;
  578. manzengItem.m_food_name = CLewaimaiString::ANSIToUTF8("【满赠】" + CLewaimaiString::UTF8ToANSI(order.m_manzeng_name));
  579. order.m_order_items.push_back(manzengItem);
  580. }
  581. //商品内容
  582. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  583. {
  584. std::string food_name = (*it).m_food_name;
  585. std::string food_price = (*it).m_item_price;
  586. std::string quantity = (*it).m_quantity;
  587. //计算总价
  588. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  589. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  590. if (food_total_price == "0.00")
  591. {
  592. //这里是特殊处理满赠的展示
  593. food_total_price = "";
  594. }
  595. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  596. //先输出商品名字
  597. std::vector<string> m_names;
  598. if(guige == "58")
  599. {
  600. m_names = HandleFoodname(food_name, 1);
  601. }
  602. else
  603. {
  604. m_names = HandleFoodname(food_name, 2);
  605. }
  606. std::string firstLine = m_names[0];
  607. int nGuige;
  608. if(guige == "58")
  609. {
  610. nGuige = 1;
  611. }
  612. else
  613. {
  614. nGuige = 2;
  615. }
  616. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  617. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  618. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  619. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  620. POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
  621. POS_FeedLine();
  622. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  623. {
  624. POS_TextOut((*it), false, setting_printer_shangpin_big);
  625. POS_FeedLine();
  626. }
  627. }
  628. //判断是否有备注
  629. if(order.m_memo.length() > 0)
  630. {
  631. bool setting_printer_memo_big = false;
  632. if(CSetting::GetParam("setting_printer_memo_big") == "1")
  633. {
  634. setting_printer_memo_big = true;
  635. }
  636. POS_FeedLine();
  637. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  638. POS_TextOut(address, false, setting_printer_memo_big);
  639. POS_FeedLine();
  640. }
  641. //结束商品详情打印
  642. POS_TextOut(lines);
  643. POS_FeedLine();
  644. //开始打印其他的费用
  645. bool is_other_money = false;
  646. bool setting_printer_price_big = false;
  647. if(CSetting::GetParam("setting_printer_price_big") == "1")
  648. {
  649. setting_printer_price_big = true;
  650. }
  651. if(order.m_delivery > 0)
  652. {
  653. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  654. POS_TextOut(delivery, false, setting_printer_price_big);
  655. POS_FeedLine();
  656. is_other_money = true;
  657. }
  658. if(order.m_is_dabao == "1")
  659. {
  660. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  661. POS_TextOut(dabao, false, setting_printer_price_big);
  662. POS_FeedLine();
  663. is_other_money = true;
  664. }
  665. //开始显示增值服务费
  666. if(order.m_addservie.size() > 0)
  667. {
  668. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  669. {
  670. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  671. POS_TextOut(addservice, false, setting_printer_price_big);
  672. POS_FeedLine();
  673. is_other_money = true;
  674. }
  675. is_other_money = true;
  676. }
  677. if(order.m_discount < 10)
  678. {
  679. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  680. POS_TextOut(discount, false, setting_printer_price_big);
  681. POS_FeedLine();
  682. is_other_money = true;
  683. }
  684. if(order.m_is_member_discount == "1")
  685. {
  686. string member_discount = "会员优惠:-" + order.m_member_discount;
  687. POS_TextOut(member_discount, false, setting_printer_price_big);
  688. POS_FeedLine();
  689. is_other_money = true;
  690. }
  691. if(order.m_is_firstcut == "1")
  692. {
  693. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  694. POS_TextOut(firstcut, false, setting_printer_price_big);
  695. POS_FeedLine();
  696. is_other_money = true;
  697. }
  698. //满减
  699. string::size_type position = order.m_promotion.find(":");
  700. if(position != order.m_promotion.npos)
  701. {
  702. string promotion_value;
  703. promotion_value.assign(order.m_promotion, position + 1);
  704. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  705. POS_TextOut(promotion, false, setting_printer_price_big);
  706. POS_FeedLine();
  707. is_other_money = true;
  708. }
  709. if(order.m_coupon > 0)
  710. {
  711. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  712. POS_TextOut(coupon, false, setting_printer_price_big);
  713. POS_FeedLine();
  714. is_other_money = true;
  715. }
  716. if(is_other_money)
  717. {
  718. POS_TextOut(lines);
  719. POS_FeedLine();
  720. }
  721. //最后显示总价
  722. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  723. POS_FeedLine();
  724. //显示付款方式
  725. bool setting_printer_pay_big = false;
  726. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  727. {
  728. setting_printer_pay_big = true;
  729. }
  730. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  731. POS_FeedLine();
  732. POS_FeedLine();;
  733. POS_FeedLine();
  734. //准备开始打印二维码
  735. std::string order_no = order.m_order_no;
  736. order_no = CLewaimaiString::UTF8ToANSI(order_no);
  737. if (order.m_show_qiangdan_qrcode == "1")
  738. {
  739. POS_OutQRCode(order_no);
  740. POS_FeedLine();
  741. POS_FeedLine();
  742. POS_TextOut("请使用配送员app扫码绑定订单", false, false, 1);
  743. }
  744. //走纸几行再切
  745. POS_FeedLine();
  746. POS_FeedLine();
  747. POS_FeedLine();
  748. POS_FeedLine();
  749. POS_FeedLine();
  750. POS_CutPaper();
  751. }
  752. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  753. POS_Reset();
  754. //关闭设备
  755. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  756. {
  757. CloseHandle((*it).hPort);
  758. }
  759. if (m_type == 2)
  760. {
  761. m_socket.close();
  762. }
  763. LOG_INFO("_______________________________结束打印收银小票,订单编号:" << order.m_order_num.c_str() << "_________________________");
  764. LOG_INFO(" ");
  765. }
  766. void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
  767. {
  768. m_type = 1;
  769. std::string printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  770. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  771. //连接usb端口
  772. InitOneUsb(ws_printer_usb);
  773. //初始化标签打印机
  774. BIAOQIAN_Reset();
  775. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  776. //先计算商品的总数量
  777. int foodNum = 0;
  778. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  779. {
  780. std::string quantity = (*it).m_quantity;
  781. foodNum += atoi(quantity.c_str());
  782. }
  783. //商品内容
  784. int curFoodNum = 0;
  785. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  786. {
  787. std::string food_name = (*it).m_food_name;
  788. std::string food_price = (*it).m_item_price;
  789. std::string quantity = (*it).m_quantity;
  790. //每份商品打印一个标签
  791. int nQuantity = atoi(quantity.c_str());
  792. for(int i = 0; i < nQuantity; i++)
  793. {
  794. std::string textData = "";
  795. curFoodNum++;
  796. std::string restaurant_number = order.m_restaurant_number;
  797. std::string order_num_info = "外卖 #" + CLewaimaiString::UTF8ToANSI(restaurant_number);
  798. textData += BIAOQIAN_TEXTGet(order_num_info, 16, 24, 1, 1);
  799. //打印份数
  800. std::string numInfo = to_string(curFoodNum) + "/" + to_string(foodNum);
  801. textData += BIAOQIAN_TEXTGet(numInfo, 160, 24, 1, 1);
  802. //每行最多显示12个汉字,这里要计算一下换行(要先转成ANSI格式)
  803. std::string handle_food_name = CLewaimaiString::UTF8ToANSI(food_name);
  804. std::vector<std::string> foodNameVector = HandleBiaoqianFoodname(handle_food_name);
  805. int nRow = 0;
  806. //考虑是否用大号字体
  807. bool is_big_name = false;
  808. if(foodNameVector.size() <= 2)
  809. {
  810. //不超过2行,可以用大的字体
  811. is_big_name = true;
  812. }
  813. LOG_INFO("foodNameVector size:" << foodNameVector.size());
  814. for(std::vector<std::string>::iterator it = foodNameVector.begin(); it != foodNameVector.end(); it++)
  815. {
  816. LOG_INFO("nRow:" << nRow << ", 准备打印商品名字:" << (*it).c_str());
  817. if(is_big_name)
  818. {
  819. textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 64 * nRow, 1, 2);
  820. }
  821. else
  822. {
  823. textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 32 * nRow, 1, 1);
  824. }
  825. nRow++;
  826. }
  827. //打印价格
  828. std::string priceInfo = CLewaimaiString::UTF8ToANSI(food_price) + "元";
  829. textData += BIAOQIAN_TEXTGet(priceInfo, 16, 188, 1, 1);
  830. //打印订单号
  831. std::string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  832. textData += BIAOQIAN_TEXTGet(order_num, 16, 218, 1, 1);
  833. WriteData(textData);
  834. BIAOQIAN_PRINT();
  835. BIAOQIAN_CLS();
  836. }
  837. }
  838. }
  839. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  840. {
  841. //设置模式,后面输出数据不会错
  842. m_type = 2;
  843. //读取厨房打印机信息
  844. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  845. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  846. {
  847. ChufangPrinter printer = *it;
  848. std::string ip = printer.ip;
  849. //初始化连接
  850. try
  851. {
  852. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  853. m_socket.connect(ep);
  854. }
  855. catch(std::exception& e)
  856. {
  857. std::string err = e.what();
  858. LOG_INFO("厨房网口打印机连接失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  859. MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(ip)).c_str(), L"打印机连接失败", MB_OK);
  860. //连接失败了,处理下一个厨房打印机
  861. continue;
  862. }
  863. std::string guige = printer.guige;
  864. std::string fendan = printer.fendan;
  865. std::string fenlei = printer.fenlei;
  866. std::string fenlei_ids = printer.fenlei_ids;
  867. std::vector<CWaimaiOrderItem> cur_printer_use;
  868. if (fenlei == "0")
  869. {
  870. cur_printer_use = order.m_order_items;
  871. }
  872. else
  873. {
  874. //如果开启了分类打印,就要比对了
  875. std::map<string, bool> ids_map;
  876. std::vector<string> ids = CLewaimaiString::Split(fenlei_ids, ",");
  877. for (std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
  878. {
  879. ids_map[(*it)] = true;
  880. }
  881. for (std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  882. {
  883. if (ids_map.find((*it).m_type_id) != ids_map.end())
  884. {
  885. //如果当前商品的分类,在厨房打印机设置的分类里面,才加入
  886. cur_printer_use.push_back(*it);
  887. }
  888. }
  889. }
  890. if (cur_printer_use.size() == 0)
  891. {
  892. //如果这个厨房打印机没有商品可以打印(开启了商品分类打印可能出现),就直接跳过
  893. m_socket.close();
  894. continue;
  895. }
  896. if(fendan == "0")
  897. {
  898. POS_Reset();
  899. POS_TextOut("派工单", true, true, 1);
  900. POS_FeedLine();
  901. POS_FeedLine();
  902. string order_type = "#" + order.m_restaurant_number + " ";
  903. if(order.m_is_selftake == "1")
  904. {
  905. POS_TextOut(order_type + "到店自取订单", false, true, 1);
  906. POS_FeedLine();
  907. POS_FeedLine();
  908. }
  909. else
  910. {
  911. POS_TextOut(order_type + "外卖订单", false, true, 1);
  912. POS_FeedLine();
  913. POS_FeedLine();
  914. }
  915. //预设选项
  916. if(order.m_order_field.size() > 1)
  917. {
  918. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  919. {
  920. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  921. POS_TextOut(order_field, false, true, 0);
  922. POS_FeedLine();
  923. }
  924. }
  925. //准备开始打印商品详情
  926. string lines;
  927. if(guige == "58")
  928. {
  929. lines = "--------------------------------";
  930. }
  931. else
  932. {
  933. lines = "------------------------------------------------";
  934. }
  935. POS_TextOut(lines);
  936. POS_FeedLine();
  937. //商品标题
  938. if(guige == "58")
  939. {
  940. POS_TextOut("商品 单价 数量 金额", false, true);
  941. }
  942. else
  943. {
  944. POS_TextOut("商品 单价 数量 金额", false, true);
  945. }
  946. POS_FeedLine();
  947. //商品内容
  948. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  949. {
  950. std::string food_name = (*it).m_food_name;
  951. std::string food_price = (*it).m_item_price;
  952. std::string quantity = (*it).m_quantity;
  953. //计算总价
  954. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  955. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  956. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  957. //先输出商品名字
  958. std::vector<string> m_names;
  959. if(guige == "58")
  960. {
  961. m_names = HandleFoodname(food_name, 1);
  962. }
  963. else
  964. {
  965. m_names = HandleFoodname(food_name, 2);
  966. }
  967. std::string firstLine = m_names[0];
  968. int nGuige;
  969. if(guige == "58")
  970. {
  971. nGuige = 1;
  972. }
  973. else
  974. {
  975. nGuige = 2;
  976. }
  977. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  978. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  979. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  980. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  981. POS_TextOut(firstLineShow, false, true, 0);
  982. POS_FeedLine();
  983. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  984. {
  985. POS_TextOut((*it), false, true, 0);
  986. POS_FeedLine();
  987. }
  988. }
  989. //判断是否有备注
  990. if(order.m_memo.length() > 0)
  991. {
  992. POS_FeedLine();
  993. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  994. POS_TextOut(address, false, true, 0);
  995. POS_FeedLine();
  996. }
  997. //结束商品详情打印
  998. POS_TextOut(lines);
  999. POS_FeedLine();
  1000. POS_FeedLine();
  1001. POS_FeedLine();
  1002. POS_FeedLine();
  1003. POS_FeedLine();
  1004. POS_FeedLine();
  1005. POS_CutPaper();
  1006. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1007. POS_Reset();
  1008. }
  1009. else
  1010. {
  1011. POS_Reset();
  1012. //分单模式下,每个商品打印一张单
  1013. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  1014. {
  1015. POS_TextOut("派工单", true, true, 1);
  1016. POS_FeedLine();
  1017. POS_FeedLine();
  1018. string order_type = "#" + order.m_restaurant_number + " ";
  1019. if(order.m_is_selftake == "1")
  1020. {
  1021. POS_TextOut(order_type + "到店自取订单", false, true, 1);
  1022. POS_FeedLine();
  1023. POS_FeedLine();
  1024. }
  1025. else
  1026. {
  1027. POS_TextOut(order_type + "外卖订单", false, true, 1);
  1028. POS_FeedLine();
  1029. POS_FeedLine();
  1030. }
  1031. //预设选项
  1032. if(order.m_order_field.size() > 1)
  1033. {
  1034. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  1035. {
  1036. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  1037. POS_TextOut(order_field, false, true, 0);
  1038. POS_FeedLine();
  1039. }
  1040. }
  1041. //准备开始打印商品详情
  1042. string lines;
  1043. if(guige == "58")
  1044. {
  1045. lines = "--------------------------------";
  1046. }
  1047. else
  1048. {
  1049. lines = "------------------------------------------------";
  1050. }
  1051. POS_TextOut(lines);
  1052. POS_FeedLine();
  1053. //商品标题
  1054. if(guige == "58")
  1055. {
  1056. POS_TextOut("商品 单价 数量 金额", false, true);
  1057. }
  1058. else
  1059. {
  1060. POS_TextOut("商品 单价 数量 金额", false, true);
  1061. }
  1062. POS_FeedLine();
  1063. std::string food_name = (*it).m_food_name;
  1064. std::string food_price = (*it).m_item_price;
  1065. std::string quantity = (*it).m_quantity;
  1066. //计算总价
  1067. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  1068. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  1069. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  1070. //先输出商品名字
  1071. std::vector<string> m_names;
  1072. if(guige == "58")
  1073. {
  1074. m_names = HandleFoodname(food_name, 1);
  1075. }
  1076. else
  1077. {
  1078. m_names = HandleFoodname(food_name, 2);
  1079. }
  1080. std::string firstLine = m_names[0];
  1081. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  1082. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  1083. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  1084. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  1085. POS_TextOut(firstLineShow, false, true, 0);
  1086. POS_FeedLine();
  1087. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  1088. {
  1089. POS_TextOut((*it), false, true, 0);
  1090. POS_FeedLine();
  1091. }
  1092. //判断是否有备注
  1093. if(order.m_memo.length() > 0)
  1094. {
  1095. POS_FeedLine();
  1096. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  1097. POS_TextOut(address, false, true, 0);
  1098. POS_FeedLine();
  1099. }
  1100. //结束商品详情打印
  1101. POS_TextOut(lines);
  1102. POS_FeedLine();
  1103. POS_FeedLine();
  1104. POS_FeedLine();
  1105. POS_FeedLine();
  1106. POS_FeedLine();
  1107. POS_FeedLine();
  1108. POS_CutPaper();
  1109. }
  1110. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1111. POS_Reset();
  1112. }
  1113. m_socket.close();
  1114. }
  1115. }
  1116. std::vector<std::wstring> CPosPrinter::getUsbDevices()
  1117. {
  1118. return m_usb_devices;
  1119. }
  1120. int CPosPrinter::WriteData(string msg)
  1121. {
  1122. return WriteBuf((unsigned char*)msg.c_str(), msg.length());
  1123. }
  1124. /*
  1125. *这个函数只负责把数据拼接到打印缓存里面,不负责实际的发送数据
  1126. **/
  1127. int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
  1128. {
  1129. if(m_type == 1)
  1130. {
  1131. //USB、串口、并口,走这里
  1132. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  1133. {
  1134. HANDLE hPort = (*it).hPort;
  1135. //同步写数据
  1136. DWORD dwWrite;
  1137. BOOL ret = WriteFile(hPort, buf, len, &dwWrite, NULL);
  1138. if (!ret)
  1139. {
  1140. LOG_INFO("WriteFile Failed!, hPort:"<<hPort<<", type:"<<(*it).type);
  1141. }
  1142. }
  1143. return 1;
  1144. }
  1145. else
  1146. {
  1147. //网口走这里
  1148. try
  1149. {
  1150. m_socket.write_some(boost::asio::buffer(buf, len));
  1151. }
  1152. catch(const std::exception& e)
  1153. {
  1154. LOG_INFO("网口打印机发送消息失败,错误信息:" << e.what());
  1155. }
  1156. return 1;
  1157. }
  1158. }
  1159. /*
  1160. *测试打印机是否连接
  1161. **/
  1162. bool CPosPrinter::PortTest(HANDLE hPort)
  1163. {
  1164. //这里的话目前USB、串口、并口用的都是同步读写模式,有的打印机(比如一些发票打印机)CreateFile会成功,但是WriteFile会一直阻塞卡着,如果检测的话,
  1165. //在这里会一直卡住,因此这里的检测就不检测了,默认所有的都是连通的
  1166. //因此,对于智能识别模式,可能会出现无法打印(因为被某些usb打印设备阻塞),这种情况就只能手动选择USB模式,然后重新启动软件
  1167. return true;
  1168. //标签打印机的查询状态指令
  1169. char chInitCode[2] = { 0x1b, 0x40};
  1170. DWORD dwWrite;
  1171. BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, NULL);
  1172. if(rc)
  1173. {
  1174. LOG_INFO("porttest success, handle:" << hPort);
  1175. return true;
  1176. }
  1177. else
  1178. {
  1179. LOG_INFO("porttest fail, handle:" << hPort);
  1180. return true;
  1181. }
  1182. return false;
  1183. }
  1184. int CPosPrinter::POS_Reset(void)
  1185. {
  1186. unsigned char s[2] = {0x1B, 0x40};
  1187. WriteBuf(s, 2);
  1188. return 0;
  1189. }
  1190. int CPosPrinter::POS_FeedLine(void)
  1191. {
  1192. unsigned char s[1] = {0x0A};
  1193. WriteBuf(s, 1);
  1194. return 0;
  1195. }
  1196. int CPosPrinter::POS_Feed(void)
  1197. {
  1198. unsigned char s[3] = { 0x1B, 0x4A, 0x00 };
  1199. WriteBuf(s, 3);
  1200. return 0;
  1201. }
  1202. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  1203. {
  1204. string s;
  1205. s = "\x1D\x50\xB4\xB4";
  1206. WriteData(s);
  1207. s = "\x1B\x53";
  1208. WriteData(s);
  1209. return 0;
  1210. }
  1211. int CPosPrinter::POS_SET_MOVE_X()
  1212. {
  1213. unsigned char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  1214. WriteBuf(s2, 6);
  1215. return 0;
  1216. }
  1217. int CPosPrinter::POS_MOVE_X()
  1218. {
  1219. unsigned char s[1] = { 0x09 };
  1220. WriteBuf(s, 1);
  1221. return 0;
  1222. }
  1223. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  1224. {
  1225. unsigned char cx = (unsigned char)(x);
  1226. unsigned char cy = (unsigned char)(y);
  1227. unsigned char s1[4] = { 0x1B, 0x24, cx, cy };
  1228. WriteBuf(s1, 4);
  1229. return 0;
  1230. }
  1231. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  1232. {
  1233. unsigned char cx = (unsigned char)(x);
  1234. unsigned char cy = (unsigned char)(y);
  1235. unsigned char s1[4] = { 0x1D, 0x57, cx, cy };
  1236. WriteBuf(s1, 4);
  1237. return 0;
  1238. }
  1239. /*
  1240. *align_type:0 左对齐 1 居中对齐 2右对齐
  1241. **/
  1242. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1243. {
  1244. if(is_double_width && is_double_height)
  1245. {
  1246. unsigned char s1[3] = { 0x1B, 0x21, 0x30 };
  1247. WriteBuf(s1, 3);
  1248. unsigned char s2[3] = { 0x1C, 0x21, 0x0c };
  1249. WriteBuf(s2, 3);
  1250. }
  1251. else if(is_double_width && !is_double_height)
  1252. {
  1253. unsigned char s1[3] = { 0x1B, 0x21, 0x20 };
  1254. WriteBuf(s1, 3);
  1255. unsigned char s2[3] = { 0x1C, 0x21, 0x04 };
  1256. WriteBuf(s2, 3);
  1257. }
  1258. else if(!is_double_width && is_double_height)
  1259. {
  1260. unsigned char s1[3] = { 0x1B, 0x21, 0x10 };
  1261. WriteBuf(s1, 3);
  1262. unsigned char s2[3] = { 0x1C, 0x21, 0x08 };
  1263. WriteBuf(s2, 3);
  1264. }
  1265. else
  1266. {
  1267. unsigned char s1[3] = { 0x1B, 0x21, 0x00 };
  1268. WriteBuf(s1, 3);
  1269. unsigned char s2[3] = { 0x1C, 0x57, 0x00 };
  1270. WriteBuf(s2, 3);
  1271. }
  1272. if(align_type == 0)
  1273. {
  1274. unsigned char s1[3] = { 0x1B, 0x61, 0x00 };
  1275. WriteBuf(s1, 3);
  1276. }
  1277. else if(align_type == 1)
  1278. {
  1279. unsigned char s1[3] = { 0x1B, 0x61, 0x01 };
  1280. WriteBuf(s1, 3);
  1281. }
  1282. else if(align_type == 2)
  1283. {
  1284. unsigned char s1[3] = { 0x1B, 0x61, 0x02 };
  1285. WriteBuf(s1, 3);
  1286. }
  1287. else
  1288. {
  1289. }
  1290. WriteData(abc);
  1291. return 0;
  1292. }
  1293. /*
  1294. *打印机切纸,直接切不走纸
  1295. **/
  1296. int CPosPrinter::POS_CutPaper()
  1297. {
  1298. unsigned char s[4] = { 0x1D, 0x56, 0x01};
  1299. WriteBuf(s, 3);
  1300. return 0;
  1301. }
  1302. /*
  1303. *先转换成ANSI格式才能传进来
  1304. **/
  1305. void CPosPrinter::POS_OutQRCode(std::string dataString)
  1306. {
  1307. QRcode * qrCode = nullptr;
  1308. qrCode = QRcode_encodeString(dataString.c_str(), 0, QR_ECLEVEL_H, QR_MODE_8, 1);
  1309. if (qrCode == nullptr)
  1310. {
  1311. printf("error occur\n");
  1312. return ;
  1313. }
  1314. unsigned int unWidth = qrCode->width;
  1315. unsigned int unWidthAdjusted = unWidth * 8;
  1316. unsigned int unHeightAjusted = unWidthAdjusted;
  1317. unsigned char* realData = new unsigned char[unWidthAdjusted * unHeightAjusted];
  1318. memset(realData, 0, unWidthAdjusted * unHeightAjusted);
  1319. unsigned char* pSourceData = qrCode->data;
  1320. for (unsigned int i = 0; i < unHeightAjusted; i++)
  1321. {
  1322. int realHeight = i / 8;
  1323. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1324. {
  1325. int realWeight = j / 8;
  1326. if (pSourceData[unWidth * realHeight + realWeight] & 1)
  1327. {
  1328. //表示这个像素要打印
  1329. realData[unWidthAdjusted * i + j] = 1;
  1330. }
  1331. else
  1332. {
  1333. realData[unWidthAdjusted * i + j] = 0;
  1334. }
  1335. }
  1336. }
  1337. //设置行间距为0
  1338. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1339. WriteBuf(data, 3);
  1340. //设置图像居中对齐
  1341. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1342. WriteBuf(data2, 3);
  1343. //选择位图模式
  1344. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1345. escBmp[3] = (unsigned char)(unWidthAdjusted % 256);
  1346. escBmp[4] = (unsigned char)(unWidthAdjusted / 256);
  1347. //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
  1348. unsigned char change_line_tag = '\n';
  1349. //存储每行数据的结构
  1350. unsigned char* dataTmp = new unsigned char[unWidthAdjusted * 3];
  1351. //循环图片像素打印图片
  1352. for (unsigned int i = 0; i < (unHeightAjusted / 24 + 1); i++)
  1353. {
  1354. //设置模式为位图模式
  1355. WriteBuf(escBmp, 5);
  1356. //重新开始打印一行,置零
  1357. memset(dataTmp, 0, unWidthAdjusted * 3);
  1358. //循环宽
  1359. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1360. {
  1361. for (unsigned int k = 0; k < 24; k++)
  1362. {
  1363. //找到有像素的区域,也就是未超出位图高度的区域
  1364. if (((i * 24) + k) < unHeightAjusted)
  1365. {
  1366. if (realData[unWidthAdjusted * (i * 24 + k) + j] == 1)
  1367. {
  1368. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1369. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1370. }
  1371. }
  1372. }
  1373. }
  1374. WriteBuf(dataTmp, unWidthAdjusted * 3);
  1375. //换行符,这里必须要发这个,不然图片会乱
  1376. WriteBuf(&change_line_tag, 1);
  1377. }
  1378. WriteBuf(&change_line_tag, 1);
  1379. //还原默认的行间距
  1380. unsigned char data3[2] = { 0x1B, 0x32 };
  1381. WriteBuf(data3, 2);
  1382. delete[] dataTmp;
  1383. QRcode_free(qrCode);
  1384. delete[] realData;
  1385. return ;
  1386. }
  1387. void CPosPrinter::POS_OutBmp(std::wstring ImagePath)
  1388. {
  1389. //初始化
  1390. POS_Reset();
  1391. //设置行间距为0
  1392. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1393. WriteBuf(data, 3);
  1394. //设置图像居中对齐
  1395. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1396. WriteBuf(data2, 3);
  1397. CBitmapHelper helper;
  1398. helper.LoadImage(ImagePath.c_str());
  1399. Bitmap* bmp = helper.getBmp();
  1400. //先进行图片预处理
  1401. unsigned int nWidth = bmp->GetWidth();
  1402. unsigned int nHeight = bmp->GetHeight();
  1403. //压缩尺寸
  1404. if (nWidth > 380)
  1405. {
  1406. int newWidth = 380;
  1407. int newHeight = (int)(nHeight / (nWidth / 380.0));
  1408. helper.ScaleBitmap(newWidth, newHeight);;
  1409. nWidth = newWidth;
  1410. nHeight = newHeight;
  1411. }
  1412. //二值化
  1413. helper.Image2Values();
  1414. //获得最新的图像指针
  1415. bmp = helper.getBmp();
  1416. //选择位图模式
  1417. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1418. escBmp[3] = (unsigned char)(nWidth % 256);
  1419. escBmp[4] = (unsigned char)(nWidth / 256);
  1420. Gdiplus::Color pixelColor;
  1421. //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
  1422. unsigned char change_line_tag = '\n';
  1423. //存储每行数据的结构
  1424. unsigned char* dataTmp = new unsigned char[nWidth * 3];
  1425. //循环图片像素打印图片
  1426. for (unsigned int i = 0; i < (nHeight / 24 + 1); i++)
  1427. {
  1428. //设置模式为位图模式
  1429. WriteBuf(escBmp, 5);
  1430. //重新开始打印一行,置零
  1431. memset(dataTmp, 0, nWidth * 3);
  1432. //循环宽
  1433. for (unsigned int j = 0; j < nWidth; j++)
  1434. {
  1435. for (unsigned int k = 0; k < 24; k++)
  1436. {
  1437. //找到有像素的区域,也就是未超出位图高度的区域
  1438. if (((i * 24) + k) < nHeight)
  1439. {
  1440. bmp->GetPixel(j, (i * 24) + k, &pixelColor);
  1441. unsigned char r = pixelColor.GetR();
  1442. if (r == 0)
  1443. {
  1444. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1445. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1446. }
  1447. }
  1448. }
  1449. }
  1450. WriteBuf(dataTmp, nWidth * 3);
  1451. //换行符,这里必须要发这个,不然图片会乱
  1452. WriteBuf(&change_line_tag, 1);
  1453. }
  1454. WriteBuf(&change_line_tag, 1);
  1455. //还原默认的行间距
  1456. unsigned char data3[2] = { 0x1B, 0x32 };
  1457. WriteBuf(data3, 2);
  1458. delete[] dataTmp;
  1459. }
  1460. void CPosPrinter::BIAOQIAN_Reset()
  1461. {
  1462. char endTag[3] = {0x0d, 0x0a, 0x00};
  1463. std::string size = "SIZE 40 mm, 30 mm";
  1464. size += endTag;
  1465. std::string gap = "GAP 2 mm,0 mm";
  1466. gap += endTag;
  1467. std::string speed = "SPEED 4.0";
  1468. speed += endTag;
  1469. std::string density = "DENSITY 12";
  1470. density += endTag;
  1471. std::string REFERENCE = "REFERENCE 0,0";
  1472. REFERENCE += endTag;
  1473. std::string PEER = "SET PEEL OFF";
  1474. PEER += endTag;
  1475. std::string TEAR = "SET TEAR ON";
  1476. TEAR += endTag;
  1477. std::string cls = "CLS";
  1478. cls += endTag;
  1479. std::string data = size + gap + speed + density + REFERENCE + PEER + TEAR + cls;
  1480. WriteData(data);
  1481. }
  1482. /*
  1483. *前进一张纸
  1484. **/
  1485. void CPosPrinter::BIAOQIAN_FORMFEED()
  1486. {
  1487. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1488. std::string formfeed = "FORMFEED";
  1489. formfeed += endTag;
  1490. WriteData(formfeed);
  1491. }
  1492. void CPosPrinter::BIAOQIAN_TEXTOUT(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1493. {
  1494. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1495. string text = "TEXT ";
  1496. text += to_string(x) + "," + to_string(y) + ",";
  1497. text += "\"TSS24.BF2\",";
  1498. text += to_string(0) + ",";
  1499. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1500. text += "\"" + content + "\"";
  1501. text += endTag;
  1502. WriteData(text);
  1503. }
  1504. std::string CPosPrinter::BIAOQIAN_TEXTGet(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1505. {
  1506. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1507. string text = "TEXT ";
  1508. text += to_string(x) + "," + to_string(y) + ",";
  1509. text += "\"TSS24.BF2\",";
  1510. text += to_string(0) + ",";
  1511. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1512. text += "\"" + content + "\"";
  1513. text += endTag;
  1514. return text;
  1515. }
  1516. void CPosPrinter::BIAOQIAN_PRINT()
  1517. {
  1518. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1519. std::string print = "PRINT 1";
  1520. print += endTag;
  1521. WriteData(print);
  1522. }
  1523. void CPosPrinter::BIAOQIAN_CLS()
  1524. {
  1525. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1526. std::string cls = "CLS";
  1527. cls += endTag;
  1528. WriteData(cls);
  1529. }
  1530. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1531. {
  1532. nHanzi = 0;
  1533. nZimu = 0;
  1534. const char* buffer = s.c_str();
  1535. while(*buffer != '\0')
  1536. {
  1537. if(!(*buffer >= 0 && *buffer <= 127))
  1538. {
  1539. //汉字
  1540. buffer++;
  1541. buffer++;
  1542. nHanzi++;
  1543. }
  1544. else
  1545. {
  1546. //字母
  1547. buffer++;
  1548. nZimu++;
  1549. }
  1550. }
  1551. }
  1552. /*
  1553. *规格 1:58mm 2:80mm
  1554. **/
  1555. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1556. {
  1557. std::vector<std::string> newnameArray;
  1558. int nHanzi, nZimu;
  1559. CalWord(oldname, nHanzi, nZimu);
  1560. int nWidth = nHanzi * 2 + nZimu;
  1561. int maxWidth;
  1562. if(guige == 1)
  1563. {
  1564. maxWidth = 15;
  1565. }
  1566. else
  1567. {
  1568. maxWidth = 28;
  1569. }
  1570. if(nWidth <= maxWidth)
  1571. {
  1572. //对于nWidth补空格
  1573. for(int i = 0; i < maxWidth - nWidth; i++)
  1574. {
  1575. oldname += " ";
  1576. }
  1577. newnameArray.push_back(oldname);
  1578. return newnameArray;
  1579. }
  1580. //宽度大于15的情况,如果超过了,就要进行换行截取
  1581. const char* s = oldname.c_str();
  1582. int nTmp = 0;
  1583. int nTotal = 0;
  1584. while(*s != '\0')
  1585. {
  1586. if(!(*s >= 0 && *s <= 127))
  1587. {
  1588. //汉字的情况
  1589. s++;
  1590. nTmp++;
  1591. nTotal++;
  1592. s++;
  1593. nTmp++;
  1594. nTotal++;
  1595. }
  1596. else
  1597. {
  1598. //字母的情况
  1599. s++;
  1600. nTmp++;
  1601. nTotal++;
  1602. }
  1603. if(nTmp == maxWidth)
  1604. {
  1605. //这里开始要换行了
  1606. string newnameItem;
  1607. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1608. newnameArray.push_back(newnameItem);
  1609. nTmp = 0;
  1610. }
  1611. else if(nTmp == maxWidth - 1)
  1612. {
  1613. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1614. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1615. {
  1616. //下一个是中文,也要换行了,补齐一个空格
  1617. string newnameItem;
  1618. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1619. newnameItem += " ";
  1620. newnameArray.push_back(newnameItem);
  1621. nTmp = 0;
  1622. }
  1623. }
  1624. }
  1625. if(nTmp > 0)
  1626. {
  1627. //处理分隔后的最后一行
  1628. string newnameItem;
  1629. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1630. for(int i = 0; i < maxWidth - nTmp; i++)
  1631. {
  1632. newnameItem += " ";
  1633. }
  1634. newnameArray.push_back(newnameItem);
  1635. }
  1636. return newnameArray;
  1637. }
  1638. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1639. {
  1640. double price = atof(oldprice.c_str());
  1641. if(price < 10.00)
  1642. {
  1643. //单位数
  1644. if(guige == 1)
  1645. {
  1646. return " " + oldprice;
  1647. }
  1648. else
  1649. {
  1650. return " " + oldprice;
  1651. }
  1652. }
  1653. else if(price > 9.99 && price < 100.00)
  1654. {
  1655. //双位数
  1656. if(guige == 1)
  1657. {
  1658. return " " + oldprice;
  1659. }
  1660. else
  1661. {
  1662. return " " + oldprice;
  1663. }
  1664. }
  1665. else if(price > 99.99 && price < 1000.00)
  1666. {
  1667. //三位数
  1668. if(guige == 1)
  1669. {
  1670. return " " + oldprice;
  1671. }
  1672. else
  1673. {
  1674. return " " + oldprice;
  1675. }
  1676. }
  1677. else
  1678. {
  1679. //四位数
  1680. if(guige == 1)
  1681. {
  1682. return " " + oldprice;
  1683. }
  1684. else
  1685. {
  1686. return " " + oldprice;
  1687. }
  1688. }
  1689. }
  1690. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1691. {
  1692. int n = atoi(oldquantity.c_str());
  1693. if(n < 10)
  1694. {
  1695. if(guige == 1)
  1696. {
  1697. return " " + oldquantity;
  1698. }
  1699. else
  1700. {
  1701. return " " + oldquantity;
  1702. }
  1703. }
  1704. else if(n >= 10 && n <= 99)
  1705. {
  1706. if(guige == 1)
  1707. {
  1708. return " " + oldquantity;
  1709. }
  1710. else
  1711. {
  1712. return " " + oldquantity;
  1713. }
  1714. }
  1715. else
  1716. {
  1717. if(guige == 1)
  1718. {
  1719. return "" + oldquantity;
  1720. }
  1721. else
  1722. {
  1723. return " " + oldquantity;
  1724. }
  1725. }
  1726. }
  1727. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1728. {
  1729. double price = atof(oldprice.c_str());
  1730. if(price < 10.00)
  1731. {
  1732. //单位数
  1733. if(guige == 1)
  1734. {
  1735. return " " + oldprice;
  1736. }
  1737. else
  1738. {
  1739. return " " + oldprice;
  1740. }
  1741. }
  1742. else if(price > 9.99 && price < 100.00)
  1743. {
  1744. //双位数
  1745. if(guige == 1)
  1746. {
  1747. return " " + oldprice;
  1748. }
  1749. else
  1750. {
  1751. return " " + oldprice;
  1752. }
  1753. }
  1754. else if(price > 99.99 && price < 1000.00)
  1755. {
  1756. //三位数
  1757. if(guige == 1)
  1758. {
  1759. return " " + oldprice;
  1760. }
  1761. else
  1762. {
  1763. return " " + oldprice;
  1764. }
  1765. }
  1766. else
  1767. {
  1768. //四位数
  1769. if(guige == 1)
  1770. {
  1771. return " " + oldprice;
  1772. }
  1773. else
  1774. {
  1775. return " " + oldprice;
  1776. }
  1777. }
  1778. }
  1779. std::vector<std::string> CPosPrinter::HandleBiaoqianFoodname(std::string oldname)
  1780. {
  1781. std::vector<std::string> newnameArray;
  1782. int nHanzi, nZimu;
  1783. CalWord(oldname, nHanzi, nZimu);
  1784. int nWidth = nHanzi * 2 + nZimu;
  1785. //40 *30mmm的标签,宽度最大24
  1786. int maxWidth = 24;
  1787. if(nWidth <= maxWidth)
  1788. {
  1789. //对于nWidth补空格
  1790. for(int i = 0; i < maxWidth - nWidth; i++)
  1791. {
  1792. oldname += " ";
  1793. }
  1794. newnameArray.push_back(oldname);
  1795. return newnameArray;
  1796. }
  1797. //宽度大于15的情况,如果超过了,就要进行换行截取
  1798. const char* s = oldname.c_str();
  1799. int nTmp = 0;
  1800. int nTotal = 0;
  1801. while(*s != '\0')
  1802. {
  1803. if(!(*s >= 0 && *s <= 127))
  1804. {
  1805. //汉字的情况
  1806. s++;
  1807. nTmp++;
  1808. nTotal++;
  1809. s++;
  1810. nTmp++;
  1811. nTotal++;
  1812. }
  1813. else
  1814. {
  1815. //字母的情况
  1816. s++;
  1817. nTmp++;
  1818. nTotal++;
  1819. }
  1820. if(nTmp == maxWidth)
  1821. {
  1822. //这里开始要换行了
  1823. string newnameItem;
  1824. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1825. newnameArray.push_back(newnameItem);
  1826. nTmp = 0;
  1827. }
  1828. else if(nTmp == maxWidth - 1)
  1829. {
  1830. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1831. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1832. {
  1833. //下一个是中文,也要换行了,补齐一个空格
  1834. string newnameItem;
  1835. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1836. newnameItem += " ";
  1837. newnameArray.push_back(newnameItem);
  1838. nTmp = 0;
  1839. }
  1840. }
  1841. }
  1842. if(nTmp > 0)
  1843. {
  1844. //处理分隔后的最后一行
  1845. string newnameItem;
  1846. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1847. for(int i = 0; i < maxWidth - nTmp; i++)
  1848. {
  1849. newnameItem += " ";
  1850. }
  1851. newnameArray.push_back(newnameItem);
  1852. }
  1853. return newnameArray;
  1854. }