CPosPrinter.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. #include "../pch/pch.h"
  2. #include "CPosPrinter.h"
  3. #include <winioctl.h>
  4. #include <setupapi.h>
  5. using boost::asio::ip::tcp;
  6. CPosPrinter::CPosPrinter(): m_socket(m_io)
  7. {
  8. }
  9. CPosPrinter::~CPosPrinter()
  10. {
  11. }
  12. /*
  13. *找到所有可用的小票打印机,包括USB、并口、串口3个类型
  14. **/
  15. bool CPosPrinter::InitShouyin()
  16. {
  17. //先找USB的
  18. //设备路径
  19. TCHAR* szDevicePath[MAX_DEVICE];
  20. //设置中文字符
  21. setlocale(LC_CTYPE, "chs");
  22. TCHAR* Port = NULL;
  23. //分配需要的空间
  24. for(int i = 0; i < MAX_DEVICE; i++)
  25. {
  26. szDevicePath[i] = new TCHAR[MAX_PATH];
  27. }
  28. //取设备路径
  29. int nDevice = GetDevicePath((LPGUID)&USB_GUID, szDevicePath);
  30. LOG_INFO("可用的USB打印机数量:" << nDevice);
  31. //添加usb端口
  32. int i = 0;
  33. while(i < nDevice)
  34. {
  35. Port = szDevicePath[i++];
  36. LOG_INFO("准备打开端口 Port = " << Port);
  37. HANDLE hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
  38. if(hPort == INVALID_HANDLE_VALUE)
  39. {
  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. }
  68. //开始添加并口的端口
  69. std::wstring LptStr = L"lpt1";
  70. HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
  71. if(hPort == INVALID_HANDLE_VALUE)
  72. {
  73. DWORD error = GetLastError();
  74. if(error == 2)
  75. {
  76. //没有指定的文件
  77. LOG_INFO("本机器没有并口!");
  78. }
  79. else if(error == 5)
  80. {
  81. LOG_INFO("并口被占用!");
  82. }
  83. }
  84. else
  85. {
  86. //这个表示并口可以使用
  87. LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
  88. if(PortTest(hPort) == true)
  89. {
  90. //并口测试连通,保存起来
  91. PrinterHandle newHandle;
  92. newHandle.hPort = hPort;
  93. newHandle.type = 2;
  94. m_hPorts.push_back(newHandle);
  95. }
  96. else
  97. {
  98. //并口打印机没有连接,那么就直接关闭掉并口,避免占用
  99. CloseHandle(hPort);
  100. }
  101. }
  102. //开始添加串口
  103. int comNum = 10;
  104. for(int i = 1; i <= 10; i++)
  105. {
  106. std::wstring com2Str = L"com" + CLewaimaiString::ANSIToUnicode(to_string(i));
  107. hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  108. if(hPort == INVALID_HANDLE_VALUE)
  109. {
  110. DWORD error = GetLastError();
  111. if(error == 2)
  112. {
  113. //没有指定的文件
  114. LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
  115. }
  116. else if(error == 5)
  117. {
  118. LOG_INFO("串口被占用 " << com2Str.c_str());
  119. }
  120. }
  121. else
  122. {
  123. LOG_INFO(com2Str.c_str() << " success, handle:" << hPort);
  124. //扎到了可用的串口,设置串口波特率
  125. DCB dcb;
  126. dcb.DCBlength = sizeof(dcb);
  127. GetCommState(hPort, &dcb);
  128. //佳博的串口打印机,默认是这个波特率
  129. dcb.BaudRate = 19200;
  130. if(!SetCommState(hPort, &dcb))
  131. {
  132. LOG_INFO("set baudRate failed!");
  133. CloseHandle(hPort);
  134. continue;
  135. }
  136. //设定通讯端口超时参数
  137. COMMTIMEOUTS tmouts;
  138. tmouts.ReadIntervalTimeout = 100;
  139. tmouts.ReadTotalTimeoutMultiplier = 100;
  140. tmouts.ReadTotalTimeoutConstant = 100;
  141. tmouts.WriteTotalTimeoutConstant = 100;
  142. tmouts.WriteTotalTimeoutMultiplier = 100;
  143. SetCommTimeouts(hPort, &tmouts);
  144. //设置端口缓冲
  145. SetupComm(hPort, 1024, 1024);
  146. //清除通讯端口缓存
  147. PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
  148. //保存端口
  149. PrinterHandle newHandle;
  150. newHandle.hPort = hPort;
  151. newHandle.type = 3;
  152. m_hPorts.push_back(newHandle);
  153. }
  154. }
  155. return true;
  156. }
  157. /*
  158. *获取CreateFile的USB端口号
  159. **/
  160. int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
  161. {
  162. HDEVINFO hDevInfoSet;
  163. SP_DEVINFO_DATA spDevInfoData;
  164. SP_DEVICE_INTERFACE_DATA ifData;
  165. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  166. int nCount;
  167. int nTotle;
  168. BOOL bResult;
  169. //这2个字符串,用于根据usb的名字对比是否为打印机设备
  170. wstring strUSBPrint = TEXT("USB 打印支持");
  171. //xp上是英文
  172. wstring strUSBPrint_EN = L"USB Printing Support";
  173. // 取得一个该GUID相关的设备信息集句柄
  174. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  175. NULL, // 无关键字
  176. NULL, // 不指定父窗口句柄
  177. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  178. // 失败...
  179. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  180. {
  181. LOG_INFO("SetupDiGetClassDevs failed \r\n");
  182. return 0;
  183. }
  184. // 申请设备接口数据空间
  185. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  186. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  187. nTotle = -1;
  188. nCount = 0;
  189. bResult = TRUE;
  190. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  191. while(bResult)
  192. {
  193. nTotle++;
  194. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  195. // 枚举符合该GUID的设备接口
  196. bResult = ::SetupDiEnumDeviceInfo(
  197. hDevInfoSet, // 设备信息集句柄
  198. (ULONG)nTotle, // 设备信息集里的设备序号
  199. &spDevInfoData); // 设备接口信息
  200. if(bResult)
  201. {
  202. DWORD DataT;
  203. TCHAR buf[MAX_PATH] = { 0 };
  204. DWORD nSize = 0;
  205. // get Friendly Name or Device Description
  206. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  207. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  208. {
  209. }
  210. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  211. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  212. {
  213. }
  214. else
  215. {
  216. lstrcpy(buf, _T("Unknown"));
  217. }
  218. wstring ws_buf = buf;
  219. //是否是要找的设备类型
  220. if(ws_buf != strUSBPrint && ws_buf != strUSBPrint_EN)
  221. {
  222. LOG_INFO("not good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  223. continue;
  224. }
  225. LOG_INFO("good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  226. ifData.cbSize = sizeof(ifData);
  227. // 枚舉符合該GUID的設備接口
  228. bResult = ::SetupDiEnumDeviceInterfaces(
  229. hDevInfoSet, // 設備信息集句柄
  230. NULL, // 不需額外的設備描述
  231. lpGuid, // GUID
  232. (ULONG)nTotle, // 設備信息集里的設備序號
  233. &ifData); // 設備接口信息
  234. if(bResult)
  235. {
  236. // 取得该设备接口的细节(设备路径)
  237. bResult = SetupDiGetInterfaceDeviceDetail(
  238. hDevInfoSet, // 设备信息集句柄
  239. &ifData, // 设备接口信息
  240. pDetail, // 设备接口细节(设备路径)
  241. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  242. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  243. NULL); // 不需额外的设备描述
  244. if(bResult)
  245. {
  246. // 复制设备路径到输出缓冲区
  247. ::_tcscpy_s(pszDevicePath[nCount], 256, pDetail->DevicePath);
  248. // 调整计数值
  249. nCount++;
  250. //LOG_INFO("Cnt = " << nCount << ",pDetail->DevicePath =" << pDetail->DevicePath);
  251. }
  252. }
  253. }
  254. else
  255. {
  256. //LOG_INFO("SetupDiEnumDeviceInfo FAILED!@");
  257. }
  258. }
  259. // 释放设备接口数据空间
  260. ::GlobalFree(pDetail);
  261. // 关闭设备信息集句柄
  262. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  263. return nCount;
  264. }
  265. void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
  266. {
  267. //设置模式,后面输出数据不会错
  268. m_type = 1;
  269. //初始化收银打印机的链接
  270. bool ret = InitShouyin();
  271. if(ret == false)
  272. {
  273. LOG_INFO("打开收银打印机端口失败!");
  274. return;
  275. }
  276. //读取当前收银打印机的设置
  277. std::string guige = CSetting::GetParam("setting_printer_guige");
  278. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  279. int n_lianshu = atoi(lianshu.c_str());
  280. //正式开始打印
  281. for(int i = 0; i < n_lianshu; i++)
  282. {
  283. POS_Reset();
  284. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  285. POS_TextOut(shop_name, true, true, 1);
  286. POS_FeedLine();
  287. POS_FeedLine();
  288. bool setting_printer_quhuo_big = false;
  289. if(CSetting::GetParam("setting_printer_quhuo_big") == "1")
  290. {
  291. setting_printer_quhuo_big = true;
  292. }
  293. if(order.m_is_selftake == "1")
  294. {
  295. POS_TextOut("到店自取订单", false, setting_printer_quhuo_big, 1);
  296. POS_FeedLine();
  297. POS_FeedLine();
  298. POS_TextOut("取餐号:" + CLewaimaiString::UTF8ToANSI(order.m_take_food_code), true, 1);
  299. POS_FeedLine();
  300. }
  301. else
  302. {
  303. POS_TextOut("外卖订单", false, setting_printer_quhuo_big, 1);
  304. POS_FeedLine();
  305. }
  306. POS_FeedLine();
  307. //订单来源
  308. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  309. POS_TextOut(from_type);
  310. POS_FeedLine();
  311. //订单号
  312. bool setting_printer_dingdanhao_big = false;
  313. if(CSetting::GetParam("setting_printer_dingdanhao_big") == "1")
  314. {
  315. setting_printer_dingdanhao_big = true;
  316. }
  317. string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  318. POS_TextOut(order_num, false, setting_printer_dingdanhao_big);
  319. POS_FeedLine();
  320. //下单时间
  321. bool setting_printer_xiadanshijian_big = false;
  322. if(CSetting::GetParam("setting_printer_xiadanshijian_big") == "1")
  323. {
  324. setting_printer_xiadanshijian_big = true;
  325. }
  326. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  327. POS_TextOut(order_date, false, setting_printer_xiadanshijian_big);
  328. POS_FeedLine();
  329. //配送时间
  330. bool setting_printer_peisongshijian_big = false;
  331. if(CSetting::GetParam("setting_printer_peisongshijian_big") == "1")
  332. {
  333. setting_printer_peisongshijian_big = true;
  334. }
  335. if(order.m_is_selftake == "1")
  336. {
  337. string date = "自取时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  338. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  339. POS_FeedLine();
  340. }
  341. else
  342. {
  343. string date = "配送时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  344. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  345. POS_FeedLine();
  346. }
  347. //姓名
  348. bool setting_printer_name_big = false;
  349. if(CSetting::GetParam("setting_printer_name_big") == "1")
  350. {
  351. setting_printer_name_big = true;
  352. }
  353. if(order.m_is_selftake != "1")
  354. {
  355. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  356. POS_TextOut(name, false, setting_printer_name_big);
  357. POS_FeedLine();
  358. }
  359. //电话
  360. bool setting_printer_phone_big = false;
  361. if(CSetting::GetParam("setting_printer_phone_big") == "1")
  362. {
  363. setting_printer_phone_big = true;
  364. }
  365. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  366. POS_TextOut(phone, false, setting_printer_phone_big);
  367. POS_FeedLine();
  368. //地址
  369. bool setting_printer_address_big = false;
  370. if(CSetting::GetParam("setting_printer_address_big") == "1")
  371. {
  372. setting_printer_address_big = true;
  373. }
  374. if(order.m_is_selftake != "1")
  375. {
  376. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  377. POS_TextOut(address, false, setting_printer_address_big);
  378. POS_FeedLine();
  379. }
  380. //预设选项
  381. if(order.m_order_field.size() > 1)
  382. {
  383. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  384. {
  385. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  386. POS_TextOut(order_field, false, false, 0);
  387. POS_FeedLine();
  388. }
  389. }
  390. //准备开始打印商品详情
  391. string lines;
  392. if(guige == "58")
  393. {
  394. lines = "--------------------------------";
  395. }
  396. else
  397. {
  398. lines = "------------------------------------------------";
  399. }
  400. POS_TextOut(lines);
  401. POS_FeedLine();
  402. //商品标题
  403. bool setting_printer_shangpin_big = false;
  404. if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
  405. {
  406. setting_printer_shangpin_big = true;
  407. }
  408. if(guige == "58")
  409. {
  410. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  411. }
  412. else
  413. {
  414. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  415. }
  416. POS_FeedLine();
  417. //商品内容
  418. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  419. {
  420. std::string food_name = (*it).m_food_name;
  421. std::string food_price = (*it).m_item_price;
  422. std::string quantity = (*it).m_quantity;
  423. //计算总价
  424. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  425. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  426. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  427. //先输出商品名字
  428. std::vector<string> m_names;
  429. if(guige == "58")
  430. {
  431. m_names = HandleFoodname(food_name, 1);
  432. }
  433. else
  434. {
  435. m_names = HandleFoodname(food_name, 2);
  436. }
  437. std::string firstLine = m_names[0];
  438. int nGuige;
  439. if(guige == "58")
  440. {
  441. nGuige = 1;
  442. }
  443. else
  444. {
  445. nGuige = 2;
  446. }
  447. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  448. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  449. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  450. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  451. POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
  452. POS_FeedLine();
  453. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  454. {
  455. POS_TextOut((*it), false, setting_printer_shangpin_big);
  456. POS_FeedLine();
  457. }
  458. }
  459. //判断是否有备注
  460. if(order.m_memo.length() > 0)
  461. {
  462. bool setting_printer_memo_big = false;
  463. if(CSetting::GetParam("setting_printer_memo_big") == "1")
  464. {
  465. setting_printer_memo_big = true;
  466. }
  467. POS_FeedLine();
  468. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  469. POS_TextOut(address, false, setting_printer_memo_big);
  470. POS_FeedLine();
  471. }
  472. //结束商品详情打印
  473. POS_TextOut(lines);
  474. POS_FeedLine();
  475. //开始打印其他的费用
  476. bool is_other_money = false;
  477. bool setting_printer_price_big = false;
  478. if(CSetting::GetParam("setting_printer_price_big") == "1")
  479. {
  480. setting_printer_price_big = true;
  481. }
  482. if(order.m_delivery > 0)
  483. {
  484. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  485. POS_TextOut(delivery, false, setting_printer_price_big);
  486. POS_FeedLine();
  487. is_other_money = true;
  488. }
  489. if(order.m_is_dabao == "1")
  490. {
  491. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  492. POS_TextOut(dabao, false, setting_printer_price_big);
  493. POS_FeedLine();
  494. is_other_money = true;
  495. }
  496. //开始显示增值服务费
  497. if(order.m_addservie.size() > 0)
  498. {
  499. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  500. {
  501. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  502. POS_TextOut(addservice, false, setting_printer_price_big);
  503. POS_FeedLine();
  504. is_other_money = true;
  505. }
  506. is_other_money = true;
  507. }
  508. if(order.m_discount < 10)
  509. {
  510. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  511. POS_TextOut(discount, false, setting_printer_price_big);
  512. POS_FeedLine();
  513. is_other_money = true;
  514. }
  515. if(order.m_is_member_discount == "1")
  516. {
  517. string member_discount = "会员优惠:-" + order.m_member_discount;
  518. POS_TextOut(member_discount, false, setting_printer_price_big);
  519. POS_FeedLine();
  520. is_other_money = true;
  521. }
  522. if(order.m_is_firstcut == "1")
  523. {
  524. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  525. POS_TextOut(firstcut, false, setting_printer_price_big);
  526. POS_FeedLine();
  527. is_other_money = true;
  528. }
  529. //满减
  530. string::size_type position = order.m_promotion.find(":");
  531. if(position != order.m_promotion.npos)
  532. {
  533. string promotion_value;
  534. promotion_value.assign(order.m_promotion, position + 1);
  535. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  536. POS_TextOut(promotion, false, setting_printer_price_big);
  537. POS_FeedLine();
  538. is_other_money = true;
  539. }
  540. if(order.m_coupon > 0)
  541. {
  542. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  543. POS_TextOut(coupon, false, setting_printer_price_big);
  544. POS_FeedLine();
  545. is_other_money = true;
  546. }
  547. if(atof(order.m_goods_coupon_value.c_str()) > 0.0001)
  548. {
  549. string coupon = "商品券:-" + order.m_goods_coupon_value;
  550. POS_TextOut(coupon, false, setting_printer_price_big);
  551. POS_FeedLine();
  552. is_other_money = true;
  553. }
  554. if(is_other_money)
  555. {
  556. POS_TextOut(lines);
  557. POS_FeedLine();
  558. }
  559. //最后显示总价
  560. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  561. POS_FeedLine();
  562. //显示付款方式
  563. bool setting_printer_pay_big = false;
  564. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  565. {
  566. setting_printer_pay_big = true;
  567. }
  568. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  569. POS_FeedLine();
  570. //走纸几行再切
  571. POS_FeedLine();
  572. POS_FeedLine();
  573. POS_FeedLine();
  574. POS_FeedLine();
  575. POS_FeedLine();
  576. POS_CutPaper();
  577. }
  578. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  579. POS_Reset();
  580. //关闭设备
  581. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  582. {
  583. CloseHandle((*it).hPort);
  584. }
  585. }
  586. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  587. {
  588. //设置模式,后面输出数据不会错
  589. m_type = 2;
  590. //读取厨房打印机信息
  591. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  592. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  593. {
  594. ChufangPrinter printer = *it;
  595. std::string ip = printer.ip;
  596. //初始化连接
  597. try
  598. {
  599. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  600. m_socket.connect(ep);
  601. }
  602. catch(std::exception& e)
  603. {
  604. std::string err = e.what();
  605. LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  606. //连接失败了,处理下一个厨房打印机
  607. continue;
  608. }
  609. std::string guige = printer.guige;
  610. std::string fendan = printer.fendan;
  611. std::string fenlei = printer.fenlei;
  612. std::string fenlei_ids = printer.fenlei_ids;
  613. std::vector<CWaimaiOrderItem> cur_printer_use;
  614. if(fenlei == "0")
  615. {
  616. cur_printer_use = order.m_order_items;
  617. }
  618. else
  619. {
  620. //如果开启了分类打印,就要比对了
  621. std::map<string, bool> ids_map;
  622. std::vector<string> ids = CLewaimaiString::Split(fenlei_ids, ",");
  623. for(std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
  624. {
  625. ids_map[(*it)] = true;
  626. }
  627. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  628. {
  629. if(ids_map.find((*it).m_type_id) != ids_map.end())
  630. {
  631. //如果当前商品的分类,在厨房打印机设置的分类里面,才加入
  632. cur_printer_use.push_back(*it);
  633. }
  634. }
  635. }
  636. if(fendan == "0")
  637. {
  638. POS_Reset();
  639. POS_TextOut("派工单", true, true, 1);
  640. POS_FeedLine();
  641. POS_FeedLine();
  642. string order_type = "#" + order.m_restaurant_number + " ";
  643. if(order.m_is_selftake == "1")
  644. {
  645. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  646. POS_FeedLine();
  647. POS_FeedLine();
  648. }
  649. else
  650. {
  651. POS_TextOut(order_type + "外卖订单", false, false, 1);
  652. POS_FeedLine();
  653. POS_FeedLine();
  654. }
  655. //预设选项
  656. if(order.m_order_field.size() > 1)
  657. {
  658. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  659. {
  660. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  661. POS_TextOut(order_field, false, false, 0);
  662. POS_FeedLine();
  663. }
  664. }
  665. //准备开始打印商品详情
  666. string lines;
  667. if(guige == "58")
  668. {
  669. lines = "--------------------------------";
  670. }
  671. else
  672. {
  673. lines = "------------------------------------------------";
  674. }
  675. POS_TextOut(lines);
  676. POS_FeedLine();
  677. //商品标题
  678. if(guige == "58")
  679. {
  680. POS_TextOut("商品 单价 数量 金额");
  681. }
  682. else
  683. {
  684. POS_TextOut("商品 单价 数量 金额");
  685. }
  686. POS_FeedLine();
  687. //商品内容
  688. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  689. {
  690. std::string food_name = (*it).m_food_name;
  691. std::string food_price = (*it).m_item_price;
  692. std::string quantity = (*it).m_quantity;
  693. //计算总价
  694. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  695. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  696. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  697. //先输出商品名字
  698. std::vector<string> m_names;
  699. if(guige == "58")
  700. {
  701. m_names = HandleFoodname(food_name, 1);
  702. }
  703. else
  704. {
  705. m_names = HandleFoodname(food_name, 2);
  706. }
  707. std::string firstLine = m_names[0];
  708. int nGuige;
  709. if(guige == "58")
  710. {
  711. nGuige = 1;
  712. }
  713. else
  714. {
  715. nGuige = 2;
  716. }
  717. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  718. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  719. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  720. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  721. POS_TextOut(firstLineShow, false, false, 0);
  722. POS_FeedLine();
  723. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  724. {
  725. POS_TextOut((*it), false, false, 0);
  726. POS_FeedLine();
  727. }
  728. }
  729. //判断是否有备注
  730. if(order.m_memo.length() > 0)
  731. {
  732. POS_FeedLine();
  733. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  734. POS_TextOut(address, false, false, 0);
  735. POS_FeedLine();
  736. }
  737. //结束商品详情打印
  738. POS_TextOut(lines);
  739. POS_FeedLine();
  740. POS_FeedLine();
  741. POS_FeedLine();
  742. POS_FeedLine();
  743. POS_FeedLine();
  744. POS_FeedLine();
  745. POS_CutPaper();
  746. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  747. POS_Reset();
  748. }
  749. else
  750. {
  751. POS_Reset();
  752. //分单模式下,每个商品打印一张单
  753. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  754. {
  755. POS_TextOut("派工单", true, true, 1);
  756. POS_FeedLine();
  757. POS_FeedLine();
  758. string order_type = "#" + order.m_restaurant_number + " ";
  759. if(order.m_is_selftake == "1")
  760. {
  761. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  762. POS_FeedLine();
  763. POS_FeedLine();
  764. }
  765. else
  766. {
  767. POS_TextOut(order_type + "外卖订单", false, false, 1);
  768. POS_FeedLine();
  769. POS_FeedLine();
  770. }
  771. //预设选项
  772. if(order.m_order_field.size() > 1)
  773. {
  774. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  775. {
  776. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  777. POS_TextOut(order_field, false, false, 0);
  778. POS_FeedLine();
  779. }
  780. }
  781. //准备开始打印商品详情
  782. string lines;
  783. if(guige == "58")
  784. {
  785. lines = "--------------------------------";
  786. }
  787. else
  788. {
  789. lines = "------------------------------------------------";
  790. }
  791. POS_TextOut(lines);
  792. POS_FeedLine();
  793. //商品标题
  794. if(guige == "58")
  795. {
  796. POS_TextOut("商品 单价 数量 金额");
  797. }
  798. else
  799. {
  800. POS_TextOut("商品 单价 数量 金额");
  801. }
  802. POS_FeedLine();
  803. std::string food_name = (*it).m_food_name;
  804. std::string food_price = (*it).m_item_price;
  805. std::string quantity = (*it).m_quantity;
  806. //计算总价
  807. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  808. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  809. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  810. //先输出商品名字
  811. std::vector<string> m_names;
  812. if(guige == "58")
  813. {
  814. m_names = HandleFoodname(food_name, 1);
  815. }
  816. else
  817. {
  818. m_names = HandleFoodname(food_name, 2);
  819. }
  820. std::string firstLine = m_names[0];
  821. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  822. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  823. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  824. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  825. POS_TextOut(firstLineShow, false, false, 0);
  826. POS_FeedLine();
  827. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  828. {
  829. POS_TextOut((*it), false, false, 0);
  830. POS_FeedLine();
  831. }
  832. //判断是否有备注
  833. if(order.m_memo.length() > 0)
  834. {
  835. POS_FeedLine();
  836. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  837. POS_TextOut(address, false, false, 0);
  838. POS_FeedLine();
  839. }
  840. //结束商品详情打印
  841. POS_TextOut(lines);
  842. POS_FeedLine();
  843. POS_FeedLine();
  844. POS_FeedLine();
  845. POS_FeedLine();
  846. POS_FeedLine();
  847. POS_FeedLine();
  848. POS_CutPaper();
  849. }
  850. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  851. POS_Reset();
  852. }
  853. m_socket.close();
  854. }
  855. }
  856. int CPosPrinter::WriteData(string msg)
  857. {
  858. return WriteBuf(msg.c_str(), msg.length());
  859. }
  860. int CPosPrinter::WriteBuf(const char* buf, int len)
  861. {
  862. if(m_type == 1)
  863. {
  864. DWORD dwWrite;
  865. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  866. {
  867. HANDLE hPort = (*it).hPort;
  868. if((*it).type == 3)
  869. {
  870. //串口,同步写数据
  871. LOG_INFO("before com writefile handle:" << hPort);
  872. WriteFile(hPort, buf, len, &dwWrite, NULL);
  873. LOG_INFO("after com writefile handle:" << hPort);
  874. }
  875. else
  876. {
  877. //usb和并口,异步写数据
  878. OVERLAPPED overlap;
  879. memset(&overlap, 0, sizeof(overlap));
  880. BOOL rc = WriteFile(hPort, buf, len, &dwWrite, &overlap);
  881. if(rc)
  882. {
  883. LOG_INFO("writefile success immediately, handle:" << hPort);
  884. }
  885. else
  886. {
  887. if(GetLastError() == ERROR_IO_PENDING)
  888. {
  889. LOG_INFO("Request queued, waiting... handle:" << hPort);
  890. WaitForSingleObject(hPort, 5000);
  891. LOG_INFO("Request completed, handle:" << hPort);
  892. DWORD numread;
  893. rc = GetOverlappedResult(
  894. hPort,
  895. &overlap,
  896. &numread,
  897. FALSE
  898. );
  899. }
  900. }
  901. }
  902. }
  903. return 1;
  904. }
  905. else
  906. {
  907. try
  908. {
  909. m_socket.write_some(boost::asio::buffer(buf, len));
  910. }
  911. catch(const std::exception& e)
  912. {
  913. LOG_INFO(e.what());
  914. }
  915. return 0;
  916. }
  917. }
  918. /*
  919. *测试这个并口是否连通的
  920. **/
  921. bool CPosPrinter::PortTest(HANDLE hPort)
  922. {
  923. char chInitCode[] = "\x1B\x40";
  924. DWORD dwWrite;
  925. OVERLAPPED overlap;
  926. memset(&overlap, 0, sizeof(overlap));
  927. BOOL rc = WriteFile(hPort, chInitCode, (DWORD)2L, &dwWrite, &overlap);
  928. if(rc)
  929. {
  930. LOG_INFO("writefile success immediately, handle:" << hPort);
  931. return true;
  932. }
  933. else
  934. {
  935. if(GetLastError() == ERROR_IO_PENDING)
  936. {
  937. DWORD ret = WaitForSingleObject(hPort, 500);
  938. if(ret == 0)
  939. {
  940. LOG_INFO("printer is connect, handle:" << hPort);
  941. return true;
  942. }
  943. else
  944. {
  945. LOG_INFO("printer is not connect, handle:" << hPort);
  946. }
  947. }
  948. }
  949. return false;
  950. }
  951. int CPosPrinter::POS_Reset(void)
  952. {
  953. char s[2] = {0x1B, 0x40};
  954. WriteBuf(s, 2);
  955. return 0;
  956. }
  957. int CPosPrinter::POS_FeedLine(void)
  958. {
  959. char s[1] = {0x0A};
  960. WriteBuf(s, 1);
  961. return 0;
  962. }
  963. int CPosPrinter::POS_Feed(void)
  964. {
  965. char s[3] = { 0x1B, 0x4A, 0x00 };
  966. WriteBuf(s, 3);
  967. return 0;
  968. }
  969. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  970. {
  971. string s;
  972. s = "\x1D\x50\xB4\xB4";
  973. WriteData(s);
  974. s = "\x1B\x53";
  975. WriteData(s);
  976. return 0;
  977. }
  978. int CPosPrinter::POS_SET_MOVE_X()
  979. {
  980. char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  981. WriteBuf(s2, 6);
  982. return 0;
  983. }
  984. int CPosPrinter::POS_MOVE_X()
  985. {
  986. char s[1] = { 0x09 };
  987. WriteBuf(s, 1);
  988. return 0;
  989. }
  990. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  991. {
  992. char cx = (char)(x);
  993. char cy = (char)(y);
  994. char s1[4] = { 0x1B, 0x24, cx, cy };
  995. WriteBuf(s1, 4);
  996. return 0;
  997. }
  998. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  999. {
  1000. char cx = (char)(x);
  1001. char cy = (char)(y);
  1002. char s1[4] = { 0x1D, 0x57, cx, cy };
  1003. WriteBuf(s1, 4);
  1004. return 0;
  1005. }
  1006. /*
  1007. *align_type:0 左对齐 1 居中对齐 2右对齐
  1008. **/
  1009. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1010. {
  1011. if(is_double_width && is_double_height)
  1012. {
  1013. char s1[3] = { 0x1B, 0x21, 0x30 };
  1014. WriteBuf(s1, 3);
  1015. char s2[3] = { 0x1C, 0x21, 0x0c };
  1016. WriteBuf(s2, 3);
  1017. }
  1018. else if(is_double_width && !is_double_height)
  1019. {
  1020. char s1[3] = { 0x1B, 0x21, 0x20 };
  1021. WriteBuf(s1, 3);
  1022. char s2[3] = { 0x1C, 0x21, 0x04 };
  1023. WriteBuf(s2, 3);
  1024. }
  1025. else if(!is_double_width && is_double_height)
  1026. {
  1027. char s1[3] = { 0x1B, 0x21, 0x10 };
  1028. WriteBuf(s1, 3);
  1029. char s2[3] = { 0x1C, 0x21, 0x08 };
  1030. WriteBuf(s2, 3);
  1031. }
  1032. else
  1033. {
  1034. char s1[3] = { 0x1B, 0x21, 0x00 };
  1035. WriteBuf(s1, 3);
  1036. char s2[3] = { 0x1C, 0x57, 0x00 };
  1037. WriteBuf(s2, 3);
  1038. }
  1039. if(align_type == 0)
  1040. {
  1041. char s1[3] = { 0x1B, 0x61, 0x00 };
  1042. WriteBuf(s1, 3);
  1043. }
  1044. else if(align_type == 1)
  1045. {
  1046. char s1[3] = { 0x1B, 0x61, 0x01 };
  1047. WriteBuf(s1, 3);
  1048. }
  1049. else if(align_type == 2)
  1050. {
  1051. char s1[3] = { 0x1B, 0x61, 0x02 };
  1052. WriteBuf(s1, 3);
  1053. }
  1054. else
  1055. {
  1056. }
  1057. WriteData(abc);
  1058. return 0;
  1059. }
  1060. /*
  1061. *打印机切纸,直接切不走纸
  1062. **/
  1063. int CPosPrinter::POS_CutPaper()
  1064. {
  1065. char s[4] = { 0x1D, 0x56, 0x01};
  1066. WriteBuf(s, 3);
  1067. return 0;
  1068. }
  1069. int CPosPrinter::POS_OutQRCode()
  1070. {
  1071. char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
  1072. char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
  1073. 0x6e, 0x74, 0x65, 0x72
  1074. };
  1075. char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
  1076. WriteBuf(QRCode1, 8);
  1077. WriteBuf(QRCode2, 16);
  1078. WriteBuf(QRCode3, 8);
  1079. return 0;
  1080. }
  1081. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1082. {
  1083. nHanzi = 0;
  1084. nZimu = 0;
  1085. const char* buffer = s.c_str();
  1086. while(*buffer != '\0')
  1087. {
  1088. if(!(*buffer >= 0 && *buffer <= 127))
  1089. {
  1090. //汉字
  1091. buffer++;
  1092. buffer++;
  1093. nHanzi++;
  1094. }
  1095. else
  1096. {
  1097. //字母
  1098. buffer++;
  1099. nZimu++;
  1100. }
  1101. }
  1102. }
  1103. /*
  1104. *规格 1:58mm 2:80mm
  1105. **/
  1106. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1107. {
  1108. std::vector<std::string> newnameArray;
  1109. int nHanzi, nZimu;
  1110. CalWord(oldname, nHanzi, nZimu);
  1111. int nWidth = nHanzi * 2 + nZimu;
  1112. int maxWidth;
  1113. if(guige == 1)
  1114. {
  1115. maxWidth = 15;
  1116. }
  1117. else
  1118. {
  1119. maxWidth = 28;
  1120. }
  1121. if(nWidth <= maxWidth)
  1122. {
  1123. //对于nWidth补空格
  1124. for(int i = 0; i < maxWidth - nWidth; i++)
  1125. {
  1126. oldname += " ";
  1127. }
  1128. newnameArray.push_back(oldname);
  1129. return newnameArray;
  1130. }
  1131. //宽度大于15的情况,如果超过了,就要进行换行截取
  1132. const char* s = oldname.c_str();
  1133. int nTmp = 0;
  1134. int nTotal = 0;
  1135. while(*s != '\0')
  1136. {
  1137. if(!(*s >= 0 && *s <= 127))
  1138. {
  1139. //汉字的情况
  1140. s++;
  1141. nTmp++;
  1142. nTotal++;
  1143. s++;
  1144. nTmp++;
  1145. nTotal++;
  1146. }
  1147. else
  1148. {
  1149. //字母的情况
  1150. s++;
  1151. nTmp++;
  1152. nTotal++;
  1153. }
  1154. if(nTmp == maxWidth)
  1155. {
  1156. //这里开始要换行了
  1157. string newnameItem;
  1158. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1159. newnameArray.push_back(newnameItem);
  1160. nTmp = 0;
  1161. }
  1162. else if(nTmp == maxWidth - 1)
  1163. {
  1164. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1165. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1166. {
  1167. //下一个是中文,也要换行了,补齐一个空格
  1168. string newnameItem;
  1169. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1170. newnameItem += " ";
  1171. newnameArray.push_back(newnameItem);
  1172. nTmp = 0;
  1173. }
  1174. }
  1175. }
  1176. if(nTmp > 0)
  1177. {
  1178. //处理分隔后的最后一行
  1179. string newnameItem;
  1180. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1181. for(int i = 0; i < maxWidth - nTmp; i++)
  1182. {
  1183. newnameItem += " ";
  1184. }
  1185. newnameArray.push_back(newnameItem);
  1186. }
  1187. return newnameArray;
  1188. }
  1189. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1190. {
  1191. double price = atof(oldprice.c_str());
  1192. if(price < 10.00)
  1193. {
  1194. //单位数
  1195. if(guige == 1)
  1196. {
  1197. return " " + oldprice;
  1198. }
  1199. else
  1200. {
  1201. return " " + oldprice;
  1202. }
  1203. }
  1204. else if(price > 9.99 && price < 100.00)
  1205. {
  1206. //双位数
  1207. if(guige == 1)
  1208. {
  1209. return " " + oldprice;
  1210. }
  1211. else
  1212. {
  1213. return " " + oldprice;
  1214. }
  1215. }
  1216. else if(price > 99.99 && price < 1000.00)
  1217. {
  1218. //三位数
  1219. if(guige == 1)
  1220. {
  1221. return " " + oldprice;
  1222. }
  1223. else
  1224. {
  1225. return " " + oldprice;
  1226. }
  1227. }
  1228. else
  1229. {
  1230. //四位数
  1231. if(guige == 1)
  1232. {
  1233. return " " + oldprice;
  1234. }
  1235. else
  1236. {
  1237. return " " + oldprice;
  1238. }
  1239. }
  1240. }
  1241. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1242. {
  1243. int n = atoi(oldquantity.c_str());
  1244. if(n < 10)
  1245. {
  1246. if(guige == 1)
  1247. {
  1248. return " " + oldquantity;
  1249. }
  1250. else
  1251. {
  1252. return " " + oldquantity;
  1253. }
  1254. }
  1255. else if(n >= 10 && n <= 99)
  1256. {
  1257. if(guige == 1)
  1258. {
  1259. return " " + oldquantity;
  1260. }
  1261. else
  1262. {
  1263. return " " + oldquantity;
  1264. }
  1265. }
  1266. else
  1267. {
  1268. if(guige == 1)
  1269. {
  1270. return "" + oldquantity;
  1271. }
  1272. else
  1273. {
  1274. return " " + oldquantity;
  1275. }
  1276. }
  1277. }
  1278. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1279. {
  1280. double price = atof(oldprice.c_str());
  1281. if(price < 10.00)
  1282. {
  1283. //单位数
  1284. if(guige == 1)
  1285. {
  1286. return " " + oldprice;
  1287. }
  1288. else
  1289. {
  1290. return " " + oldprice;
  1291. }
  1292. }
  1293. else if(price > 9.99 && price < 100.00)
  1294. {
  1295. //双位数
  1296. if(guige == 1)
  1297. {
  1298. return " " + oldprice;
  1299. }
  1300. else
  1301. {
  1302. return " " + oldprice;
  1303. }
  1304. }
  1305. else if(price > 99.99 && price < 1000.00)
  1306. {
  1307. //三位数
  1308. if(guige == 1)
  1309. {
  1310. return " " + oldprice;
  1311. }
  1312. else
  1313. {
  1314. return " " + oldprice;
  1315. }
  1316. }
  1317. else
  1318. {
  1319. //四位数
  1320. if(guige == 1)
  1321. {
  1322. return " " + oldprice;
  1323. }
  1324. else
  1325. {
  1326. return " " + oldprice;
  1327. }
  1328. }
  1329. }