CPosPrinter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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(int print_type): m_socket(m_io)
  7. {
  8. m_type = print_type;
  9. if(print_type == 1)
  10. {
  11. Init();
  12. }
  13. else
  14. {
  15. InitChufang();
  16. }
  17. }
  18. CPosPrinter::~CPosPrinter()
  19. {
  20. if (m_type == 2)
  21. {
  22. m_socket.close();
  23. }
  24. }
  25. bool CPosPrinter::Init()
  26. {
  27. //遍历USB设备,找到POS打印机路径
  28. //设备路径
  29. TCHAR * szDevicePath[MAX_DEVICE];
  30. //设置中文字符
  31. setlocale(LC_CTYPE, "chs");
  32. TCHAR* Port = NULL;
  33. //分配需要的空间
  34. for(int i = 0; i < MAX_DEVICE; i++)
  35. {
  36. szDevicePath[i] = new TCHAR[256];
  37. }
  38. //取设备路径
  39. int nDevice = GetDevicePath((LPGUID)&USB_GUID, szDevicePath);
  40. int i = 0;
  41. while(i < nDevice)
  42. {
  43. Port = szDevicePath[i++];
  44. LOG_INFO("device.Port = " << Port);
  45. }
  46. m_hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
  47. 0, NULL,
  48. OPEN_EXISTING,
  49. FILE_ATTRIBUTE_NORMAL, NULL);
  50. if(m_hPort == INVALID_HANDLE_VALUE)
  51. {
  52. // 打开端口失败
  53. return false;
  54. }
  55. return true;
  56. }
  57. bool CPosPrinter::InitChufang()
  58. {
  59. try
  60. {
  61. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("192.168.10.47"), 9100);
  62. m_socket.connect(ep);
  63. }
  64. catch(std::exception& e)
  65. {
  66. std::cout << e.what() << std::endl;
  67. }
  68. return true;
  69. }
  70. void CPosPrinter::PrintWaimaiOrder(string order_id, std::string order_no)
  71. {
  72. CWaimaiOrder order;
  73. order.InitData(order_id, order_no);
  74. PrintWaimaiOrder(order);
  75. }
  76. void CPosPrinter::PrintWaimaiOrder(CWaimaiOrder& order)
  77. {
  78. std::string guige = CSetting::GetParam("setting_printer_guige");
  79. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  80. int n_lianshu = atoi(lianshu.c_str());
  81. for(int i = 0; i < n_lianshu; i++)
  82. {
  83. POS_Reset();
  84. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  85. POS_TextOut(shop_name, true, 1);
  86. POS_FeedLine();
  87. POS_FeedLine();
  88. if(order.m_is_selftake == "1")
  89. {
  90. POS_TextOut("到店自取订单", false, 1);
  91. POS_FeedLine();
  92. POS_FeedLine();
  93. POS_TextOut("取餐号:" + CLewaimaiString::UTF8ToANSI(order.m_take_food_code), true, 1);
  94. POS_FeedLine();
  95. }
  96. else
  97. {
  98. POS_TextOut("外卖订单", false, 1);
  99. POS_FeedLine();
  100. }
  101. POS_FeedLine();
  102. //订单来源
  103. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  104. POS_TextOut(from_type);
  105. POS_FeedLine();
  106. //订单号
  107. string order_no = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_no);
  108. POS_TextOut(order_no);
  109. POS_FeedLine();
  110. //姓名
  111. if(order.m_is_selftake != "1")
  112. {
  113. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  114. POS_TextOut(name);
  115. POS_FeedLine();
  116. }
  117. //电话
  118. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  119. POS_TextOut(phone);
  120. POS_FeedLine();
  121. //地址
  122. if(order.m_is_selftake != "1")
  123. {
  124. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  125. POS_TextOut(address);
  126. POS_FeedLine();
  127. }
  128. //预设选项
  129. if(order.m_order_field.size() > 1)
  130. {
  131. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  132. {
  133. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  134. POS_TextOut(order_field, false, 0);
  135. POS_FeedLine();
  136. }
  137. }
  138. //下单时间
  139. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  140. POS_TextOut(order_date);
  141. POS_FeedLine();
  142. //配送时间
  143. if(order.m_is_selftake == "1")
  144. {
  145. string date = "自取时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  146. POS_TextOut(date);
  147. POS_FeedLine();
  148. }
  149. else
  150. {
  151. string date = "配送时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  152. POS_TextOut(date);
  153. POS_FeedLine();
  154. }
  155. //准备开始打印商品详情
  156. string lines;
  157. if(guige == "58")
  158. {
  159. lines = "--------------------------------";
  160. }
  161. else
  162. {
  163. lines = "------------------------------------------------";
  164. }
  165. POS_TextOut(lines);
  166. POS_FeedLine();
  167. //商品标题
  168. if(guige == "58")
  169. {
  170. POS_TextOut("商品 单价 数量 金额");
  171. }
  172. else
  173. {
  174. POS_TextOut("商品 单价 数量 金额");
  175. }
  176. POS_FeedLine();
  177. //商品内容
  178. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  179. {
  180. std::string food_name = (*it).m_food_name;
  181. std::string food_price = (*it).m_item_price;
  182. std::string quantity = (*it).m_quantity;
  183. //计算总价
  184. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  185. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  186. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  187. //先输出商品名字
  188. std::vector<string> m_names;
  189. if(guige == "58")
  190. {
  191. m_names = HandleFoodname(food_name, 1);
  192. }
  193. else
  194. {
  195. m_names = HandleFoodname(food_name, 2);
  196. }
  197. std::string firstLine = m_names[0];
  198. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  199. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  200. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  201. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  202. POS_TextOut(firstLineShow, false, 0);
  203. POS_FeedLine();
  204. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  205. {
  206. POS_TextOut((*it), false, 0);
  207. POS_FeedLine();
  208. }
  209. }
  210. //判断是否有备注
  211. if(order.m_memo.length() > 0)
  212. {
  213. POS_FeedLine();
  214. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  215. POS_TextOut(address, false, 0);
  216. POS_FeedLine();
  217. }
  218. //结束商品详情打印
  219. POS_TextOut(lines);
  220. POS_FeedLine();
  221. //开始打印其他的费用
  222. bool is_other_money = false;
  223. if(order.m_delivery > 0)
  224. {
  225. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  226. POS_TextOut(delivery, false, 0);
  227. POS_FeedLine();
  228. is_other_money = true;
  229. }
  230. if(order.m_is_dabao == "1")
  231. {
  232. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  233. POS_TextOut(dabao, false, 0);
  234. POS_FeedLine();
  235. is_other_money = true;
  236. }
  237. //开始显示增值服务费
  238. if(order.m_addservie.size() > 0)
  239. {
  240. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  241. {
  242. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  243. POS_TextOut(addservice, false, 0);
  244. POS_FeedLine();
  245. is_other_money = true;
  246. }
  247. is_other_money = true;
  248. }
  249. if(order.m_discount < 10)
  250. {
  251. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  252. POS_TextOut(discount, false, 0);
  253. POS_FeedLine();
  254. is_other_money = true;
  255. }
  256. if(order.m_is_member_discount == "1")
  257. {
  258. string member_discount = "会员优惠:-" + order.m_member_discount;
  259. POS_TextOut(member_discount, false, 0);
  260. POS_FeedLine();
  261. is_other_money = true;
  262. }
  263. if(order.m_is_firstcut == "1")
  264. {
  265. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  266. POS_TextOut(firstcut, false, 0);
  267. POS_FeedLine();
  268. is_other_money = true;
  269. }
  270. //满减
  271. string::size_type position = order.m_promotion.find(":");
  272. if(position != order.m_promotion.npos)
  273. {
  274. string promotion_value;
  275. promotion_value.assign(order.m_promotion, position + 1);
  276. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  277. POS_TextOut(promotion, false, 0);
  278. POS_FeedLine();
  279. is_other_money = true;
  280. }
  281. if(order.m_coupon > 0)
  282. {
  283. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  284. POS_TextOut(coupon, false, 0);
  285. POS_FeedLine();
  286. is_other_money = true;
  287. }
  288. if(atof(order.m_goods_coupon_value.c_str()) > 0.0001)
  289. {
  290. string coupon = "商品券:-" + order.m_goods_coupon_value;
  291. POS_TextOut(coupon, false, 0);
  292. POS_FeedLine();
  293. is_other_money = true;
  294. }
  295. if(is_other_money)
  296. {
  297. POS_TextOut(lines);
  298. POS_FeedLine();
  299. }
  300. //最后显示总价
  301. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, 2);
  302. POS_FeedLine();
  303. //显示付款方式
  304. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, 2);
  305. POS_FeedLine();
  306. POS_FeedLine();
  307. POS_CutPaper();
  308. }
  309. }
  310. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  311. //获取CreateFile的USB端口号
  312. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  313. // 根据GUID获得设备路径
  314. // lpGuid: GUID指针
  315. // pszDevicePath: 设备路径指针的指针,用于返回找到的路径
  316. // 返回: 成功得到的设备路径个数,可能不止1个
  317. int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
  318. {
  319. HDEVINFO hDevInfoSet;
  320. SP_DEVINFO_DATA spDevInfoData;
  321. SP_DEVICE_INTERFACE_DATA ifData;
  322. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  323. int nCount;
  324. int nTotle;
  325. BOOL bResult;
  326. wstring strUSBPrint = TEXT("USB 打印支持");
  327. // 取得一个该GUID相关的设备信息集句柄
  328. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  329. NULL, // 无关键字
  330. NULL, // 不指定父窗口句柄
  331. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  332. // 失败...
  333. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  334. {
  335. printf("failed \r\n");
  336. return 0;
  337. }
  338. // 申请设备接口数据空间
  339. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  340. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  341. nTotle = -1;
  342. nCount = 0;
  343. bResult = TRUE;
  344. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  345. while(bResult)
  346. {
  347. nTotle++;
  348. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  349. // 枚举符合该GUID的设备接口
  350. bResult = ::SetupDiEnumDeviceInfo(
  351. hDevInfoSet, // 设备信息集句柄
  352. (ULONG)nTotle, // 设备信息集里的设备序号
  353. &spDevInfoData); // 设备接口信息
  354. if(bResult)
  355. {
  356. DWORD DataT;
  357. TCHAR buf[MAX_PATH];
  358. DWORD nSize = 0;
  359. // get Friendly Name or Device Description
  360. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  361. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  362. {
  363. }
  364. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  365. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  366. {
  367. }
  368. else
  369. {
  370. lstrcpy(buf, _T("Unknown"));
  371. }
  372. _tprintf(_T("buf = %s \r\n"), buf);
  373. //是否是要找的设备类型
  374. if(_tcscmp(buf, strUSBPrint.c_str()) != 0)
  375. {
  376. continue;
  377. }
  378. _tprintf(_T("OK\r\n"));
  379. ifData.cbSize = sizeof(ifData);
  380. // 枚舉符合該GUID的設備接口
  381. bResult = ::SetupDiEnumDeviceInterfaces(
  382. hDevInfoSet, // 設備信息集句柄
  383. NULL, // 不需額外的設備描述
  384. lpGuid, // GUID
  385. (ULONG)nTotle, // 設備信息集里的設備序號
  386. &ifData); // 設備接口信息
  387. if(bResult)
  388. {
  389. // 取得该设备接口的细节(设备路径)
  390. bResult = SetupDiGetInterfaceDeviceDetail(
  391. hDevInfoSet, // 设备信息集句柄
  392. &ifData, // 设备接口信息
  393. pDetail, // 设备接口细节(设备路径)
  394. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  395. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  396. NULL); // 不需额外的设备描述
  397. if(bResult)
  398. {
  399. // 复制设备路径到输出缓冲区
  400. ::_tcscpy_s(pszDevicePath[nCount], 256, pDetail->DevicePath);
  401. // 调整计数值
  402. nCount++;
  403. _tprintf(_T("Cnt = %d,pDetail->DevicePath =%s\r\n"), nCount, pDetail->DevicePath);
  404. }
  405. }
  406. }
  407. }
  408. // 释放设备接口数据空间
  409. ::GlobalFree(pDetail);
  410. // 关闭设备信息集句柄
  411. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  412. return nCount;
  413. }
  414. int CPosPrinter::WriteData(string msg)
  415. {
  416. if(m_type == 1)
  417. {
  418. DWORD dwWrite;
  419. return WriteFile(m_hPort, msg.c_str(), (DWORD)msg.length(), &dwWrite, NULL);
  420. }
  421. else
  422. {
  423. try
  424. {
  425. m_socket.write_some(boost::asio::buffer(msg.c_str(), msg.length()));
  426. }
  427. catch (const std::exception& e)
  428. {
  429. LOG_INFO(e.what());
  430. }
  431. return 0;
  432. }
  433. }
  434. int CPosPrinter::WriteBuf(char *buf, int len)
  435. {
  436. if(m_type == 1)
  437. {
  438. DWORD dwWrite;
  439. return WriteFile(m_hPort, buf, len, &dwWrite, NULL);
  440. }
  441. else
  442. {
  443. try
  444. {
  445. m_socket.write_some(boost::asio::buffer(buf, len));
  446. }
  447. catch (const std::exception& e)
  448. {
  449. LOG_INFO(e.what());
  450. }
  451. return 0;
  452. }
  453. }
  454. int CPosPrinter::POS_Reset(void)
  455. {
  456. char s[2] = {0x1B, 0x40};
  457. WriteBuf(s, 2);
  458. return 0;
  459. }
  460. int CPosPrinter::POS_FeedLine(void)
  461. {
  462. char s[1] = {0x0A};
  463. WriteBuf(s, 1);
  464. return 0;
  465. }
  466. int CPosPrinter::POS_Feed(void)
  467. {
  468. char s[3] = { 0x1B, 0x4A, 0x00 };
  469. WriteBuf(s, 3);
  470. return 0;
  471. }
  472. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  473. {
  474. string s;
  475. s = "\x1D\x50\xB4\xB4";
  476. WriteData(s);
  477. s = "\x1B\x53";
  478. WriteData(s);
  479. return 0;
  480. }
  481. int CPosPrinter::POS_SET_MOVE_X()
  482. {
  483. char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  484. WriteBuf(s2, 6);
  485. return 0;
  486. }
  487. int CPosPrinter::POS_MOVE_X()
  488. {
  489. char s[1] = { 0x09 };
  490. WriteBuf(s, 1);
  491. return 0;
  492. }
  493. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  494. {
  495. char cx = (char)(x);
  496. char cy = (char)(y);
  497. char s1[4] = { 0x1B, 0x24, cx, cy };
  498. WriteBuf(s1, 4);
  499. return 0;
  500. }
  501. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  502. {
  503. char cx = (char)(x);
  504. char cy = (char)(y);
  505. char s1[4] = { 0x1D, 0x57, cx, cy };
  506. WriteBuf(s1, 4);
  507. return 0;
  508. }
  509. /*
  510. *align_type:0 左对齐 1 居中对齐 2右对齐
  511. **/
  512. int CPosPrinter::POS_TextOut(string abc, bool is_double, int align_type)
  513. {
  514. if(is_double)
  515. {
  516. char s1[3] = { 0x1B, 0x21, 0x30 };
  517. WriteBuf(s1, 3);
  518. char s2[3] = { 0x1C, 0x21, 0x0c };
  519. WriteBuf(s2, 3);
  520. }
  521. else
  522. {
  523. char s1[3] = { 0x1B, 0x21, 0x00 };
  524. WriteBuf(s1, 3);
  525. char s2[3] = { 0x1C, 0x57, 0x00 };
  526. WriteBuf(s2, 3);
  527. }
  528. if(align_type == 0)
  529. {
  530. char s1[3] = { 0x1B, 0x61, 0x00 };
  531. WriteBuf(s1, 3);
  532. }
  533. else if(align_type == 1)
  534. {
  535. char s1[3] = { 0x1B, 0x61, 0x01 };
  536. WriteBuf(s1, 3);
  537. }
  538. else if(align_type == 2)
  539. {
  540. char s1[3] = { 0x1B, 0x61, 0x02 };
  541. WriteBuf(s1, 3);
  542. }
  543. else
  544. {
  545. }
  546. WriteData(abc);
  547. return 0;
  548. }
  549. int CPosPrinter::POS_CutPaper()
  550. {
  551. char s[4] = { 0x1D, 0x56, 0x41, 0x00 };
  552. WriteBuf(s, 4);
  553. return 0;
  554. }
  555. int CPosPrinter::POS_OutQRCode()
  556. {
  557. char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
  558. char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
  559. 0x6e, 0x74, 0x65, 0x72
  560. };
  561. char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
  562. WriteBuf(QRCode1, 8);
  563. WriteBuf(QRCode2, 16);
  564. WriteBuf(QRCode3, 8);
  565. return 0;
  566. }
  567. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  568. {
  569. nHanzi = 0;
  570. nZimu = 0;
  571. const char* buffer = s.c_str();
  572. while(*buffer++ != '\0')
  573. {
  574. if(!(*buffer >= 0 && *buffer <= 127))
  575. {
  576. //汉字
  577. buffer++;
  578. nHanzi++;
  579. }
  580. else
  581. {
  582. //字母
  583. nZimu++;
  584. }
  585. }
  586. }
  587. /*
  588. *规格 1:58mm 2:80mm
  589. **/
  590. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  591. {
  592. std::vector<std::string> newnameArray;
  593. int nHanzi, nZimu;
  594. CalWord(oldname, nHanzi, nZimu);
  595. int nWidth = nHanzi * 2 + nZimu;
  596. int maxWidth;
  597. if(guige == 1)
  598. {
  599. maxWidth = 15;
  600. }
  601. else
  602. {
  603. maxWidth = 28;
  604. }
  605. if(nWidth <= maxWidth)
  606. {
  607. //对于nWidth补空格
  608. for(int i = 0; i < maxWidth - nWidth; i++)
  609. {
  610. oldname += " ";
  611. }
  612. newnameArray.push_back(oldname);
  613. return newnameArray;
  614. }
  615. //宽度大于15的情况,如果超过了,就要进行换行截取
  616. const char* s = oldname.c_str();
  617. int nTmp = 0;
  618. int nTotal = 0;
  619. while(*s++ != '\0')
  620. {
  621. nTmp++;
  622. nTotal++;
  623. if(!(*(s) >= 0 && *(s) <= 127))
  624. {
  625. //汉字的情况
  626. s++;
  627. nTmp++;
  628. nTotal++;
  629. }
  630. if(nTmp == maxWidth)
  631. {
  632. //这里开始要换行了
  633. string newnameItem;
  634. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  635. newnameArray.push_back(newnameItem);
  636. nTmp = 0;
  637. }
  638. else if(nTmp == maxWidth - 1)
  639. {
  640. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  641. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  642. {
  643. //下一个是中文,也要换行了,补齐一个空格
  644. string newnameItem;
  645. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  646. newnameItem += " ";
  647. newnameArray.push_back(newnameItem);
  648. nTmp = 0;
  649. }
  650. }
  651. }
  652. if(nTmp > 0)
  653. {
  654. //处理分隔后的最后一行
  655. string newnameItem;
  656. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  657. for(int i = 0; i < maxWidth - nTmp; i++)
  658. {
  659. newnameItem += " ";
  660. }
  661. newnameArray.push_back(newnameItem);
  662. }
  663. return newnameArray;
  664. }
  665. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  666. {
  667. double price = atof(oldprice.c_str());
  668. if(price < 10.00)
  669. {
  670. //单位数
  671. if(guige == 1)
  672. {
  673. return " " + oldprice;
  674. }
  675. else
  676. {
  677. return " " + oldprice;
  678. }
  679. }
  680. else if(price > 9.99 && price < 100.00)
  681. {
  682. //双位数
  683. if(guige == 1)
  684. {
  685. return " " + oldprice;
  686. }
  687. else
  688. {
  689. return " " + oldprice;
  690. }
  691. }
  692. else if(price > 99.99 && price < 1000.00)
  693. {
  694. //三位数
  695. if(guige == 1)
  696. {
  697. return " " + oldprice;
  698. }
  699. else
  700. {
  701. return " " + oldprice;
  702. }
  703. }
  704. else
  705. {
  706. //四位数
  707. if(guige == 1)
  708. {
  709. return " " + oldprice;
  710. }
  711. else
  712. {
  713. return " " + oldprice;
  714. }
  715. }
  716. }
  717. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  718. {
  719. int n = atoi(oldquantity.c_str());
  720. if(n < 10)
  721. {
  722. if(guige == 1)
  723. {
  724. return " " + oldquantity;
  725. }
  726. else
  727. {
  728. return " " + oldquantity;
  729. }
  730. }
  731. else if(n >= 10 && n <= 99)
  732. {
  733. if(guige == 1)
  734. {
  735. return " " + oldquantity;
  736. }
  737. else
  738. {
  739. return " " + oldquantity;
  740. }
  741. }
  742. else
  743. {
  744. if(guige == 1)
  745. {
  746. return "" + oldquantity;
  747. }
  748. else
  749. {
  750. return " " + oldquantity;
  751. }
  752. }
  753. }
  754. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  755. {
  756. double price = atof(oldprice.c_str());
  757. if(price < 10.00)
  758. {
  759. //单位数
  760. if(guige == 1)
  761. {
  762. return " " + oldprice;
  763. }
  764. else
  765. {
  766. return " " + oldprice;
  767. }
  768. }
  769. else if(price > 9.99 && price < 100.00)
  770. {
  771. //双位数
  772. if(guige == 1)
  773. {
  774. return " " + oldprice;
  775. }
  776. else
  777. {
  778. return " " + oldprice;
  779. }
  780. }
  781. else if(price > 99.99 && price < 1000.00)
  782. {
  783. //三位数
  784. if(guige == 1)
  785. {
  786. return " " + oldprice;
  787. }
  788. else
  789. {
  790. return " " + oldprice;
  791. }
  792. }
  793. else
  794. {
  795. //四位数
  796. if(guige == 1)
  797. {
  798. return " " + oldprice;
  799. }
  800. else
  801. {
  802. return " " + oldprice;
  803. }
  804. }
  805. }