NoteFormatterServer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Servers\printer;
  3. /**
  4. * 打印机格式化服务
  5. */
  6. class NoteFormatterServer
  7. {
  8. /**
  9. * 58mm 系列打印机模板
  10. */
  11. public const ROW_MAX_CHAR_LEN = 32;//行最大字符长度
  12. private const MAX_NAME_CHAR_LEN = 20;//最大名称字符长度
  13. private const LAST_ROW_MAX_NAME_CHAR_LEN = 16;//最后一行最大名称字符长度
  14. private const MAX_QUANTITY_CHAR_LEN = 6;//数量最大长度
  15. private const MAX_PRICE_CHAR_LEN = 6;//价格最大长度
  16. /**
  17. * 错误信息
  18. * @var string
  19. */
  20. private $errorMsg = '';
  21. static private $server = '';
  22. private function __construct()
  23. {
  24. }
  25. /**
  26. * 创建对象
  27. * @return NoteFormatterServer
  28. */
  29. static function creatServer()
  30. {
  31. if (empty(self::$server)) {
  32. self::$server = new NoteFormatterServer();
  33. }
  34. return self::$server;
  35. }
  36. /**
  37. * 格式化菜品列表(用于58mm打印机)
  38. * 注意:默认字体排版,若是字体宽度倍大后不适用
  39. * 58mm打印机一行可打印32个字符 汉子按照2个字符算
  40. * 分3列: 名称20字符一般用16字符4空格填充 数量6字符 单价6字符,不足用英文空格填充 名称过长换行
  41. *
  42. */
  43. public static function formatPrintOrderItem($foodName, $quantity, $price,$is_yly=0)
  44. {
  45. $orderNameEmpty = str_repeat(" ", self::MAX_NAME_CHAR_LEN);
  46. $foodNameLen = self::CalcGbkLenForPrint($foodName);
  47. // print("foodNameLen=".$foodNameLen."\n");
  48. $quantityStr = '*' . $quantity;
  49. $quantityLen = self::CalcAsciiLenForPrint($quantityStr);
  50. // print("quantityLen=".$quantityLen."\n");
  51. $priceStr = '' . round($price, 2);
  52. $priceLen = self::CalcAsciiLenForPrint($priceStr);
  53. // print("priceLen=".$priceLen);
  54. $result = $foodName;
  55. $mod = $foodNameLen % self::ROW_MAX_CHAR_LEN;
  56. // print("mod=".$mod."\n");
  57. if ($mod <= self::LAST_ROW_MAX_NAME_CHAR_LEN) {
  58. // 保证各个列的宽度固定,不足部分,利用空格填充
  59. //make sure all the column length fixed, fill with space if not enough
  60. $result = $result . str_repeat(" ", self::MAX_NAME_CHAR_LEN - $mod);
  61. } else {
  62. // 另起新行
  63. // new line
  64. if ($is_yly){
  65. $result = $result . "\n";
  66. }else{
  67. $result = $result . "<BR>";
  68. }
  69. $result = $result . $orderNameEmpty;
  70. }
  71. $result = $result . $quantityStr . str_repeat(" ", self::MAX_QUANTITY_CHAR_LEN - $quantityLen);
  72. $result = $result . $priceStr . str_repeat(" ", self::MAX_PRICE_CHAR_LEN - $priceLen);
  73. return $result;
  74. }
  75. /**
  76. * 大趋智能格式化菜品列表(用于58mm打印机)
  77. * @param $foodName
  78. * @param $quantity
  79. * @param $price
  80. * @return string
  81. */
  82. public static function trenditFormatOrderItem($foodName, $quantity, $price)
  83. {
  84. $orderNameEmpty = str_repeat(" ", self::MAX_NAME_CHAR_LEN);
  85. $foodNameLen = self::CalcGbkLenForPrint($foodName);
  86. // print("foodNameLen=".$foodNameLen."\n");
  87. $quantityStr = '*' . $quantity;
  88. $quantityLen = self::CalcAsciiLenForPrint($quantityStr);
  89. // print("quantityLen=".$quantityLen."\n");
  90. $priceStr = '' . round($price, 2);
  91. $priceLen = self::CalcAsciiLenForPrint($priceStr);
  92. // print("priceLen=".$priceLen);
  93. $result = $foodName;
  94. $mod = $foodNameLen % self::ROW_MAX_CHAR_LEN;
  95. // print("mod=".$mod."\n");
  96. if ($mod <= self::LAST_ROW_MAX_NAME_CHAR_LEN) {
  97. // 保证各个列的宽度固定,不足部分,利用空格填充
  98. //make sure all the column length fixed, fill with space if not enough
  99. $result = $result . str_repeat(" ", self::MAX_NAME_CHAR_LEN - $mod);
  100. } else {
  101. // 另起新行
  102. // new line
  103. // $result = $result . '<BR>';
  104. $result = $result . $orderNameEmpty;
  105. }
  106. $result = $result . $quantityStr . str_repeat(" ", self::MAX_QUANTITY_CHAR_LEN - $quantityLen);
  107. $result = $result . $priceStr . str_repeat(" ", self::MAX_PRICE_CHAR_LEN - $priceLen);
  108. $result = '<LEFT><font# height=2>'.$result . '</font#></LEFT><BR>';
  109. return $result;
  110. }
  111. public static function CalcGbkLenForPrint($data)
  112. {
  113. return (strlen($data) + mb_strlen($data, "UTF8")) / 2;
  114. }
  115. public static function CalcAsciiLenForPrint($data)
  116. {
  117. return strlen($data);
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function getErrorMsg(): string
  123. {
  124. return $this->errorMsg;
  125. }
  126. }