CPosPrinter.cpp 60 KB

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