CPosPrinter.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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. }
  299. else
  300. {
  301. POS_TextOut("外卖订单", false, setting_printer_quhuo_big, 1);
  302. POS_FeedLine();
  303. }
  304. POS_FeedLine();
  305. //订单来源
  306. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  307. POS_TextOut(from_type);
  308. POS_FeedLine();
  309. //订单号
  310. bool setting_printer_dingdanhao_big = false;
  311. if(CSetting::GetParam("setting_printer_dingdanhao_big") == "1")
  312. {
  313. setting_printer_dingdanhao_big = true;
  314. }
  315. string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  316. POS_TextOut(order_num, false, setting_printer_dingdanhao_big);
  317. POS_FeedLine();
  318. //下单时间
  319. bool setting_printer_xiadanshijian_big = false;
  320. if(CSetting::GetParam("setting_printer_xiadanshijian_big") == "1")
  321. {
  322. setting_printer_xiadanshijian_big = true;
  323. }
  324. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  325. POS_TextOut(order_date, false, setting_printer_xiadanshijian_big);
  326. POS_FeedLine();
  327. //配送时间
  328. bool setting_printer_peisongshijian_big = false;
  329. if(CSetting::GetParam("setting_printer_peisongshijian_big") == "1")
  330. {
  331. setting_printer_peisongshijian_big = true;
  332. }
  333. if(order.m_is_selftake == "1")
  334. {
  335. string date = "自取时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  336. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  337. POS_FeedLine();
  338. }
  339. else
  340. {
  341. string date = "配送时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  342. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  343. POS_FeedLine();
  344. }
  345. //姓名
  346. bool setting_printer_name_big = false;
  347. if(CSetting::GetParam("setting_printer_name_big") == "1")
  348. {
  349. setting_printer_name_big = true;
  350. }
  351. if(order.m_is_selftake != "1")
  352. {
  353. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  354. POS_TextOut(name, false, setting_printer_name_big);
  355. POS_FeedLine();
  356. }
  357. //电话
  358. bool setting_printer_phone_big = false;
  359. if(CSetting::GetParam("setting_printer_phone_big") == "1")
  360. {
  361. setting_printer_phone_big = true;
  362. }
  363. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  364. POS_TextOut(phone, false, setting_printer_phone_big);
  365. POS_FeedLine();
  366. //地址
  367. bool setting_printer_address_big = false;
  368. if(CSetting::GetParam("setting_printer_address_big") == "1")
  369. {
  370. setting_printer_address_big = true;
  371. }
  372. if(order.m_is_selftake != "1")
  373. {
  374. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  375. POS_TextOut(address, false, setting_printer_address_big);
  376. POS_FeedLine();
  377. }
  378. //预设选项
  379. if(order.m_order_field.size() > 1)
  380. {
  381. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  382. {
  383. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  384. POS_TextOut(order_field, false, false, 0);
  385. POS_FeedLine();
  386. }
  387. }
  388. //准备开始打印商品详情
  389. string lines;
  390. if(guige == "58")
  391. {
  392. lines = "--------------------------------";
  393. }
  394. else
  395. {
  396. lines = "------------------------------------------------";
  397. }
  398. POS_TextOut(lines);
  399. POS_FeedLine();
  400. //商品标题
  401. bool setting_printer_shangpin_big = false;
  402. if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
  403. {
  404. setting_printer_shangpin_big = true;
  405. }
  406. if(guige == "58")
  407. {
  408. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  409. }
  410. else
  411. {
  412. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  413. }
  414. POS_FeedLine();
  415. //商品内容
  416. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  417. {
  418. std::string food_name = (*it).m_food_name;
  419. std::string food_price = (*it).m_item_price;
  420. std::string quantity = (*it).m_quantity;
  421. //计算总价
  422. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  423. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  424. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  425. //先输出商品名字
  426. std::vector<string> m_names;
  427. if(guige == "58")
  428. {
  429. m_names = HandleFoodname(food_name, 1);
  430. }
  431. else
  432. {
  433. m_names = HandleFoodname(food_name, 2);
  434. }
  435. std::string firstLine = m_names[0];
  436. int nGuige;
  437. if(guige == "58")
  438. {
  439. nGuige = 1;
  440. }
  441. else
  442. {
  443. nGuige = 2;
  444. }
  445. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  446. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  447. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  448. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  449. POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
  450. POS_FeedLine();
  451. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  452. {
  453. POS_TextOut((*it), false, setting_printer_shangpin_big);
  454. POS_FeedLine();
  455. }
  456. }
  457. //判断是否有备注
  458. if(order.m_memo.length() > 0)
  459. {
  460. bool setting_printer_memo_big = false;
  461. if(CSetting::GetParam("setting_printer_memo_big") == "1")
  462. {
  463. setting_printer_memo_big = true;
  464. }
  465. POS_FeedLine();
  466. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  467. POS_TextOut(address, false, setting_printer_memo_big);
  468. POS_FeedLine();
  469. }
  470. //结束商品详情打印
  471. POS_TextOut(lines);
  472. POS_FeedLine();
  473. //开始打印其他的费用
  474. bool is_other_money = false;
  475. bool setting_printer_price_big = false;
  476. if(CSetting::GetParam("setting_printer_price_big") == "1")
  477. {
  478. setting_printer_price_big = true;
  479. }
  480. if(order.m_delivery > 0)
  481. {
  482. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  483. POS_TextOut(delivery, false, setting_printer_price_big);
  484. POS_FeedLine();
  485. is_other_money = true;
  486. }
  487. if(order.m_is_dabao == "1")
  488. {
  489. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  490. POS_TextOut(dabao, false, setting_printer_price_big);
  491. POS_FeedLine();
  492. is_other_money = true;
  493. }
  494. //开始显示增值服务费
  495. if(order.m_addservie.size() > 0)
  496. {
  497. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  498. {
  499. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  500. POS_TextOut(addservice, false, setting_printer_price_big);
  501. POS_FeedLine();
  502. is_other_money = true;
  503. }
  504. is_other_money = true;
  505. }
  506. if(order.m_discount < 10)
  507. {
  508. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  509. POS_TextOut(discount, false, setting_printer_price_big);
  510. POS_FeedLine();
  511. is_other_money = true;
  512. }
  513. if(order.m_is_member_discount == "1")
  514. {
  515. string member_discount = "会员优惠:-" + order.m_member_discount;
  516. POS_TextOut(member_discount, false, setting_printer_price_big);
  517. POS_FeedLine();
  518. is_other_money = true;
  519. }
  520. if(order.m_is_firstcut == "1")
  521. {
  522. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  523. POS_TextOut(firstcut, false, setting_printer_price_big);
  524. POS_FeedLine();
  525. is_other_money = true;
  526. }
  527. //满减
  528. string::size_type position = order.m_promotion.find(":");
  529. if(position != order.m_promotion.npos)
  530. {
  531. string promotion_value;
  532. promotion_value.assign(order.m_promotion, position + 1);
  533. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  534. POS_TextOut(promotion, false, setting_printer_price_big);
  535. POS_FeedLine();
  536. is_other_money = true;
  537. }
  538. if(order.m_coupon > 0)
  539. {
  540. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  541. POS_TextOut(coupon, false, setting_printer_price_big);
  542. POS_FeedLine();
  543. is_other_money = true;
  544. }
  545. if(is_other_money)
  546. {
  547. POS_TextOut(lines);
  548. POS_FeedLine();
  549. }
  550. //最后显示总价
  551. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  552. POS_FeedLine();
  553. //显示付款方式
  554. bool setting_printer_pay_big = false;
  555. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  556. {
  557. setting_printer_pay_big = true;
  558. }
  559. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  560. POS_FeedLine();
  561. //走纸几行再切
  562. POS_FeedLine();
  563. POS_FeedLine();
  564. POS_FeedLine();
  565. POS_FeedLine();
  566. POS_FeedLine();
  567. POS_CutPaper();
  568. }
  569. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  570. POS_Reset();
  571. //关闭设备
  572. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  573. {
  574. CloseHandle((*it).hPort);
  575. }
  576. }
  577. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  578. {
  579. //设置模式,后面输出数据不会错
  580. m_type = 2;
  581. //读取厨房打印机信息
  582. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  583. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  584. {
  585. ChufangPrinter printer = *it;
  586. std::string ip = printer.ip;
  587. //初始化连接
  588. try
  589. {
  590. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  591. m_socket.connect(ep);
  592. }
  593. catch(std::exception& e)
  594. {
  595. std::string err = e.what();
  596. LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  597. //连接失败了,处理下一个厨房打印机
  598. continue;
  599. }
  600. std::string guige = printer.guige;
  601. std::string fendan = printer.fendan;
  602. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  603. if(fendan == "0")
  604. {
  605. POS_Reset();
  606. POS_TextOut("派工单", true, true, 1);
  607. POS_FeedLine();
  608. POS_FeedLine();
  609. string order_type = "#" + order.m_restaurant_number + " ";
  610. if (order.m_is_selftake == "1")
  611. {
  612. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  613. POS_FeedLine();
  614. POS_FeedLine();
  615. }
  616. else
  617. {
  618. POS_TextOut(order_type + "外卖订单", false, false, 1);
  619. POS_FeedLine();
  620. POS_FeedLine();
  621. }
  622. //预设选项
  623. if(order.m_order_field.size() > 1)
  624. {
  625. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  626. {
  627. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  628. POS_TextOut(order_field, false, false, 0);
  629. POS_FeedLine();
  630. }
  631. }
  632. //准备开始打印商品详情
  633. string lines;
  634. if(guige == "58")
  635. {
  636. lines = "--------------------------------";
  637. }
  638. else
  639. {
  640. lines = "------------------------------------------------";
  641. }
  642. POS_TextOut(lines);
  643. POS_FeedLine();
  644. //商品标题
  645. if(guige == "58")
  646. {
  647. POS_TextOut("商品 单价 数量 金额");
  648. }
  649. else
  650. {
  651. POS_TextOut("商品 单价 数量 金额");
  652. }
  653. POS_FeedLine();
  654. //商品内容
  655. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  656. {
  657. std::string food_name = (*it).m_food_name;
  658. std::string food_price = (*it).m_item_price;
  659. std::string quantity = (*it).m_quantity;
  660. //计算总价
  661. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  662. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  663. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  664. //先输出商品名字
  665. std::vector<string> m_names;
  666. if(guige == "58")
  667. {
  668. m_names = HandleFoodname(food_name, 1);
  669. }
  670. else
  671. {
  672. m_names = HandleFoodname(food_name, 2);
  673. }
  674. std::string firstLine = m_names[0];
  675. int nGuige;
  676. if(guige == "58")
  677. {
  678. nGuige = 1;
  679. }
  680. else
  681. {
  682. nGuige = 2;
  683. }
  684. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  685. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  686. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  687. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  688. POS_TextOut(firstLineShow, false, false, 0);
  689. POS_FeedLine();
  690. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  691. {
  692. POS_TextOut((*it), false, false, 0);
  693. POS_FeedLine();
  694. }
  695. }
  696. //判断是否有备注
  697. if(order.m_memo.length() > 0)
  698. {
  699. POS_FeedLine();
  700. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  701. POS_TextOut(address, false, false, 0);
  702. POS_FeedLine();
  703. }
  704. //结束商品详情打印
  705. POS_TextOut(lines);
  706. POS_FeedLine();
  707. POS_FeedLine();
  708. POS_FeedLine();
  709. POS_FeedLine();
  710. POS_FeedLine();
  711. POS_FeedLine();
  712. POS_CutPaper();
  713. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  714. POS_Reset();
  715. }
  716. else
  717. {
  718. POS_Reset();
  719. //分单模式下,每个商品打印一张单
  720. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  721. {
  722. POS_TextOut("派工单", true, true, 1);
  723. POS_FeedLine();
  724. POS_FeedLine();
  725. string order_type = "#" + order.m_restaurant_number + " ";
  726. if (order.m_is_selftake == "1")
  727. {
  728. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  729. POS_FeedLine();
  730. POS_FeedLine();
  731. }
  732. else
  733. {
  734. POS_TextOut(order_type + "外卖订单", false, false, 1);
  735. POS_FeedLine();
  736. POS_FeedLine();
  737. }
  738. //预设选项
  739. if(order.m_order_field.size() > 1)
  740. {
  741. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  742. {
  743. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  744. POS_TextOut(order_field, false, false, 0);
  745. POS_FeedLine();
  746. }
  747. }
  748. //准备开始打印商品详情
  749. string lines;
  750. if(guige == "58")
  751. {
  752. lines = "--------------------------------";
  753. }
  754. else
  755. {
  756. lines = "------------------------------------------------";
  757. }
  758. POS_TextOut(lines);
  759. POS_FeedLine();
  760. //商品标题
  761. if(guige == "58")
  762. {
  763. POS_TextOut("商品 单价 数量 金额");
  764. }
  765. else
  766. {
  767. POS_TextOut("商品 单价 数量 金额");
  768. }
  769. POS_FeedLine();
  770. std::string food_name = (*it).m_food_name;
  771. std::string food_price = (*it).m_item_price;
  772. std::string quantity = (*it).m_quantity;
  773. //计算总价
  774. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  775. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  776. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  777. //先输出商品名字
  778. std::vector<string> m_names;
  779. if(guige == "58")
  780. {
  781. m_names = HandleFoodname(food_name, 1);
  782. }
  783. else
  784. {
  785. m_names = HandleFoodname(food_name, 2);
  786. }
  787. std::string firstLine = m_names[0];
  788. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  789. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  790. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  791. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  792. POS_TextOut(firstLineShow, false, false, 0);
  793. POS_FeedLine();
  794. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  795. {
  796. POS_TextOut((*it), false, false, 0);
  797. POS_FeedLine();
  798. }
  799. //判断是否有备注
  800. if(order.m_memo.length() > 0)
  801. {
  802. POS_FeedLine();
  803. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  804. POS_TextOut(address, false, false, 0);
  805. POS_FeedLine();
  806. }
  807. //结束商品详情打印
  808. POS_TextOut(lines);
  809. POS_FeedLine();
  810. POS_FeedLine();
  811. POS_FeedLine();
  812. POS_FeedLine();
  813. POS_FeedLine();
  814. POS_FeedLine();
  815. POS_CutPaper();
  816. }
  817. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  818. POS_Reset();
  819. }
  820. m_socket.close();
  821. }
  822. }
  823. int CPosPrinter::WriteData(string msg)
  824. {
  825. return WriteBuf(msg.c_str(), msg.length());
  826. }
  827. int CPosPrinter::WriteBuf(const char* buf, int len)
  828. {
  829. if(m_type == 1)
  830. {
  831. DWORD dwWrite;
  832. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  833. {
  834. HANDLE hPort = (*it).hPort;
  835. if((*it).type == 3)
  836. {
  837. //串口,同步写数据
  838. LOG_INFO("before com writefile handle:" << hPort);
  839. WriteFile(hPort, buf, len, &dwWrite, NULL);
  840. LOG_INFO("after com writefile handle:" << hPort);
  841. }
  842. else
  843. {
  844. //usb和并口,异步写数据
  845. OVERLAPPED overlap;
  846. memset(&overlap, 0, sizeof(overlap));
  847. BOOL rc = WriteFile(hPort, buf, len, &dwWrite, &overlap);
  848. if(rc)
  849. {
  850. LOG_INFO("writefile success immediately, handle:" << hPort);
  851. }
  852. else
  853. {
  854. if(GetLastError() == ERROR_IO_PENDING)
  855. {
  856. LOG_INFO("Request queued, waiting... handle:" << hPort);
  857. WaitForSingleObject(hPort, 5000);
  858. LOG_INFO("Request completed, handle:" << hPort);
  859. DWORD numread;
  860. rc = GetOverlappedResult(
  861. hPort,
  862. &overlap,
  863. &numread,
  864. FALSE
  865. );
  866. }
  867. }
  868. }
  869. }
  870. return 1;
  871. }
  872. else
  873. {
  874. try
  875. {
  876. m_socket.write_some(boost::asio::buffer(buf, len));
  877. }
  878. catch(const std::exception& e)
  879. {
  880. LOG_INFO(e.what());
  881. }
  882. return 0;
  883. }
  884. }
  885. /*
  886. *测试这个并口是否连通的
  887. **/
  888. bool CPosPrinter::PortTest(HANDLE hPort)
  889. {
  890. char chInitCode[] = "\x1B\x40";
  891. DWORD dwWrite;
  892. OVERLAPPED overlap;
  893. memset(&overlap, 0, sizeof(overlap));
  894. BOOL rc = WriteFile(hPort, chInitCode, (DWORD)2L, &dwWrite, &overlap);
  895. if(rc)
  896. {
  897. LOG_INFO("writefile success immediately, handle:" << hPort);
  898. return true;
  899. }
  900. else
  901. {
  902. if(GetLastError() == ERROR_IO_PENDING)
  903. {
  904. DWORD ret = WaitForSingleObject(hPort, 500);
  905. if(ret == 0)
  906. {
  907. LOG_INFO("printer is connect, handle:" << hPort);
  908. return true;
  909. }
  910. else
  911. {
  912. LOG_INFO("printer is not connect, handle:" << hPort);
  913. }
  914. }
  915. }
  916. return false;
  917. }
  918. int CPosPrinter::POS_Reset(void)
  919. {
  920. char s[2] = {0x1B, 0x40};
  921. WriteBuf(s, 2);
  922. return 0;
  923. }
  924. int CPosPrinter::POS_FeedLine(void)
  925. {
  926. char s[1] = {0x0A};
  927. WriteBuf(s, 1);
  928. return 0;
  929. }
  930. int CPosPrinter::POS_Feed(void)
  931. {
  932. char s[3] = { 0x1B, 0x4A, 0x00 };
  933. WriteBuf(s, 3);
  934. return 0;
  935. }
  936. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  937. {
  938. string s;
  939. s = "\x1D\x50\xB4\xB4";
  940. WriteData(s);
  941. s = "\x1B\x53";
  942. WriteData(s);
  943. return 0;
  944. }
  945. int CPosPrinter::POS_SET_MOVE_X()
  946. {
  947. char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  948. WriteBuf(s2, 6);
  949. return 0;
  950. }
  951. int CPosPrinter::POS_MOVE_X()
  952. {
  953. char s[1] = { 0x09 };
  954. WriteBuf(s, 1);
  955. return 0;
  956. }
  957. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  958. {
  959. char cx = (char)(x);
  960. char cy = (char)(y);
  961. char s1[4] = { 0x1B, 0x24, cx, cy };
  962. WriteBuf(s1, 4);
  963. return 0;
  964. }
  965. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  966. {
  967. char cx = (char)(x);
  968. char cy = (char)(y);
  969. char s1[4] = { 0x1D, 0x57, cx, cy };
  970. WriteBuf(s1, 4);
  971. return 0;
  972. }
  973. /*
  974. *align_type:0 左对齐 1 居中对齐 2右对齐
  975. **/
  976. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  977. {
  978. if(is_double_width && is_double_height)
  979. {
  980. char s1[3] = { 0x1B, 0x21, 0x30 };
  981. WriteBuf(s1, 3);
  982. char s2[3] = { 0x1C, 0x21, 0x0c };
  983. WriteBuf(s2, 3);
  984. }
  985. else if(is_double_width && !is_double_height)
  986. {
  987. char s1[3] = { 0x1B, 0x21, 0x20 };
  988. WriteBuf(s1, 3);
  989. char s2[3] = { 0x1C, 0x21, 0x04 };
  990. WriteBuf(s2, 3);
  991. }
  992. else if(!is_double_width && is_double_height)
  993. {
  994. char s1[3] = { 0x1B, 0x21, 0x10 };
  995. WriteBuf(s1, 3);
  996. char s2[3] = { 0x1C, 0x21, 0x08 };
  997. WriteBuf(s2, 3);
  998. }
  999. else
  1000. {
  1001. char s1[3] = { 0x1B, 0x21, 0x00 };
  1002. WriteBuf(s1, 3);
  1003. char s2[3] = { 0x1C, 0x57, 0x00 };
  1004. WriteBuf(s2, 3);
  1005. }
  1006. if(align_type == 0)
  1007. {
  1008. char s1[3] = { 0x1B, 0x61, 0x00 };
  1009. WriteBuf(s1, 3);
  1010. }
  1011. else if(align_type == 1)
  1012. {
  1013. char s1[3] = { 0x1B, 0x61, 0x01 };
  1014. WriteBuf(s1, 3);
  1015. }
  1016. else if(align_type == 2)
  1017. {
  1018. char s1[3] = { 0x1B, 0x61, 0x02 };
  1019. WriteBuf(s1, 3);
  1020. }
  1021. else
  1022. {
  1023. }
  1024. WriteData(abc);
  1025. return 0;
  1026. }
  1027. /*
  1028. *打印机切纸,直接切不走纸
  1029. **/
  1030. int CPosPrinter::POS_CutPaper()
  1031. {
  1032. char s[4] = { 0x1D, 0x56, 0x01};
  1033. WriteBuf(s, 3);
  1034. return 0;
  1035. }
  1036. int CPosPrinter::POS_OutQRCode()
  1037. {
  1038. char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
  1039. char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
  1040. 0x6e, 0x74, 0x65, 0x72
  1041. };
  1042. char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
  1043. WriteBuf(QRCode1, 8);
  1044. WriteBuf(QRCode2, 16);
  1045. WriteBuf(QRCode3, 8);
  1046. return 0;
  1047. }
  1048. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1049. {
  1050. nHanzi = 0;
  1051. nZimu = 0;
  1052. const char* buffer = s.c_str();
  1053. while(*buffer != '\0')
  1054. {
  1055. if(!(*buffer >= 0 && *buffer <= 127))
  1056. {
  1057. //汉字
  1058. buffer++;
  1059. buffer++;
  1060. nHanzi++;
  1061. }
  1062. else
  1063. {
  1064. //字母
  1065. buffer++;
  1066. nZimu++;
  1067. }
  1068. }
  1069. }
  1070. /*
  1071. *规格 1:58mm 2:80mm
  1072. **/
  1073. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1074. {
  1075. std::vector<std::string> newnameArray;
  1076. int nHanzi, nZimu;
  1077. CalWord(oldname, nHanzi, nZimu);
  1078. int nWidth = nHanzi * 2 + nZimu;
  1079. int maxWidth;
  1080. if(guige == 1)
  1081. {
  1082. maxWidth = 15;
  1083. }
  1084. else
  1085. {
  1086. maxWidth = 28;
  1087. }
  1088. if(nWidth <= maxWidth)
  1089. {
  1090. //对于nWidth补空格
  1091. for(int i = 0; i < maxWidth - nWidth; i++)
  1092. {
  1093. oldname += " ";
  1094. }
  1095. newnameArray.push_back(oldname);
  1096. return newnameArray;
  1097. }
  1098. //宽度大于15的情况,如果超过了,就要进行换行截取
  1099. const char* s = oldname.c_str();
  1100. int nTmp = 0;
  1101. int nTotal = 0;
  1102. while(*s != '\0')
  1103. {
  1104. if(!(*s >= 0 && *s <= 127))
  1105. {
  1106. //汉字的情况
  1107. s++;
  1108. nTmp++;
  1109. nTotal++;
  1110. s++;
  1111. nTmp++;
  1112. nTotal++;
  1113. }
  1114. else
  1115. {
  1116. //字母的情况
  1117. s++;
  1118. nTmp++;
  1119. nTotal++;
  1120. }
  1121. if(nTmp == maxWidth)
  1122. {
  1123. //这里开始要换行了
  1124. string newnameItem;
  1125. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1126. newnameArray.push_back(newnameItem);
  1127. nTmp = 0;
  1128. }
  1129. else if(nTmp == maxWidth - 1)
  1130. {
  1131. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1132. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1133. {
  1134. //下一个是中文,也要换行了,补齐一个空格
  1135. string newnameItem;
  1136. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1137. newnameItem += " ";
  1138. newnameArray.push_back(newnameItem);
  1139. nTmp = 0;
  1140. }
  1141. }
  1142. }
  1143. if(nTmp > 0)
  1144. {
  1145. //处理分隔后的最后一行
  1146. string newnameItem;
  1147. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1148. for(int i = 0; i < maxWidth - nTmp; i++)
  1149. {
  1150. newnameItem += " ";
  1151. }
  1152. newnameArray.push_back(newnameItem);
  1153. }
  1154. return newnameArray;
  1155. }
  1156. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1157. {
  1158. double price = atof(oldprice.c_str());
  1159. if(price < 10.00)
  1160. {
  1161. //单位数
  1162. if(guige == 1)
  1163. {
  1164. return " " + oldprice;
  1165. }
  1166. else
  1167. {
  1168. return " " + oldprice;
  1169. }
  1170. }
  1171. else if(price > 9.99 && price < 100.00)
  1172. {
  1173. //双位数
  1174. if(guige == 1)
  1175. {
  1176. return " " + oldprice;
  1177. }
  1178. else
  1179. {
  1180. return " " + oldprice;
  1181. }
  1182. }
  1183. else if(price > 99.99 && price < 1000.00)
  1184. {
  1185. //三位数
  1186. if(guige == 1)
  1187. {
  1188. return " " + oldprice;
  1189. }
  1190. else
  1191. {
  1192. return " " + oldprice;
  1193. }
  1194. }
  1195. else
  1196. {
  1197. //四位数
  1198. if(guige == 1)
  1199. {
  1200. return " " + oldprice;
  1201. }
  1202. else
  1203. {
  1204. return " " + oldprice;
  1205. }
  1206. }
  1207. }
  1208. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1209. {
  1210. int n = atoi(oldquantity.c_str());
  1211. if(n < 10)
  1212. {
  1213. if(guige == 1)
  1214. {
  1215. return " " + oldquantity;
  1216. }
  1217. else
  1218. {
  1219. return " " + oldquantity;
  1220. }
  1221. }
  1222. else if(n >= 10 && n <= 99)
  1223. {
  1224. if(guige == 1)
  1225. {
  1226. return " " + oldquantity;
  1227. }
  1228. else
  1229. {
  1230. return " " + oldquantity;
  1231. }
  1232. }
  1233. else
  1234. {
  1235. if(guige == 1)
  1236. {
  1237. return "" + oldquantity;
  1238. }
  1239. else
  1240. {
  1241. return " " + oldquantity;
  1242. }
  1243. }
  1244. }
  1245. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1246. {
  1247. double price = atof(oldprice.c_str());
  1248. if(price < 10.00)
  1249. {
  1250. //单位数
  1251. if(guige == 1)
  1252. {
  1253. return " " + oldprice;
  1254. }
  1255. else
  1256. {
  1257. return " " + oldprice;
  1258. }
  1259. }
  1260. else if(price > 9.99 && price < 100.00)
  1261. {
  1262. //双位数
  1263. if(guige == 1)
  1264. {
  1265. return " " + oldprice;
  1266. }
  1267. else
  1268. {
  1269. return " " + oldprice;
  1270. }
  1271. }
  1272. else if(price > 99.99 && price < 1000.00)
  1273. {
  1274. //三位数
  1275. if(guige == 1)
  1276. {
  1277. return " " + oldprice;
  1278. }
  1279. else
  1280. {
  1281. return " " + oldprice;
  1282. }
  1283. }
  1284. else
  1285. {
  1286. //四位数
  1287. if(guige == 1)
  1288. {
  1289. return " " + oldprice;
  1290. }
  1291. else
  1292. {
  1293. return " " + oldprice;
  1294. }
  1295. }
  1296. }