An easy point of sale system with automatic inventory tracking. https://netsyms.biz/apps/nickelbox/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ReceiptLine.lib.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. /**
  8. * A single variable-width line on a receipt, with different formatting options
  9. */
  10. class ReceiptLine {
  11. const LINEFORMAT_PLAIN = 1;
  12. const LINEFORMAT_BOLD = 2;
  13. const LINEFORMAT_HR = 4;
  14. const LINEFORMAT_CENTER = 8;
  15. const LINEFORMAT_BLANK = 16;
  16. private $left = "";
  17. private $middle = "";
  18. private $right = "";
  19. private $format;
  20. function __construct($l = "", $m = "", $r = "", $f = ReceiptLine::LINEFORMAT_PLAIN) {
  21. $this->left = $l;
  22. $this->middle = $m;
  23. $this->right = $r;
  24. $this->format = $f;
  25. }
  26. function getLeft() {
  27. return $this->left;
  28. }
  29. function getMiddle() {
  30. return $this->middle;
  31. }
  32. function getRight() {
  33. return $this->right;
  34. }
  35. function hasFormat($format) {
  36. return ($this->format & $format);
  37. }
  38. function getHtml() {
  39. if ($this->hasFormat($this::LINEFORMAT_HR)) {
  40. return "<hr />";
  41. }
  42. if ($this->hasFormat($this::LINEFORMAT_BLANK)) {
  43. return "<br />";
  44. }
  45. $html = "";
  46. if (!empty($this->left) || $this->left === 0 || $this->left === "0") {
  47. $html .= '<span>' . htmlspecialchars($this->left) . '&nbsp;</span>';
  48. }
  49. if (!empty($this->middle) || $this->middle === 0 || $this->middle === "0") {
  50. $html .= '<span>' . htmlspecialchars($this->middle) . '&nbsp;</span>';
  51. }
  52. if (!empty($this->right) || $this->right === 0 || $this->right === "0") {
  53. $html .= '<span>' . htmlspecialchars($this->right) . '</span>';
  54. }
  55. $classes = ["flex"];
  56. if ($this->hasFormat($this::LINEFORMAT_BOLD)) {
  57. $classes[] = "bold";
  58. }
  59. if ($this->hasFormat($this::LINEFORMAT_CENTER)) {
  60. $classes[] = "centered";
  61. }
  62. $classstr = implode(" ", $classes);
  63. return "<div class=\"$classstr\">$html</div>";
  64. }
  65. function getPlainText($width) {
  66. if ($this->hasFormat($this::LINEFORMAT_HR)) {
  67. return str_repeat("-", $width);
  68. }
  69. if ($this->hasFormat($this::LINEFORMAT_BLANK)) {
  70. return str_repeat(" ", $width);
  71. }
  72. $left = $this->left;
  73. $middle = $this->middle;
  74. $right = $this->right;
  75. $leftln = strlen($left);
  76. $middleln = strlen($middle);
  77. $rightln = strlen($right);
  78. if ($middleln > 0) {
  79. $middleln++;
  80. $middle = " " . $middle;
  81. }
  82. if ($rightln > 0) {
  83. $rightln++;
  84. $right = " " . $right;
  85. }
  86. $strln = $leftln + $middleln + $rightln;
  87. if ($strln < $width) {
  88. if ($this->hasFormat($this::LINEFORMAT_CENTER)) {
  89. return str_pad($left . $middle . $right, $width, " ", STR_PAD_BOTH);
  90. } else {
  91. $middle = str_pad($middle, $width - $leftln - $rightln, " ", STR_PAD_BOTH);
  92. }
  93. } else if ($strln > $width) {
  94. $loseln = $strln - $width;
  95. $left = substr($this->left, 0, $leftln - $loseln);
  96. }
  97. return $left . $middle . $right;
  98. }
  99. function getArray($width = 64) {
  100. $data = [
  101. "left" => $this->left,
  102. "middle" => $this->middle,
  103. "right" => $this->right,
  104. "text" => $this->getPlainText($width),
  105. "format" => []
  106. ];
  107. if ($this->hasFormat($this::LINEFORMAT_PLAIN)) {
  108. $data['format'][] = "plain";
  109. }
  110. if ($this->hasFormat($this::LINEFORMAT_BOLD)) {
  111. $data['format'][] = "bold";
  112. }
  113. if ($this->hasFormat($this::LINEFORMAT_HR)) {
  114. $data['format'][] = "hr";
  115. }
  116. if ($this->hasFormat($this::LINEFORMAT_CENTER)) {
  117. $data['format'][] = "center";
  118. }
  119. if ($this->hasFormat($this::LINEFORMAT_BLANK)) {
  120. $data['format'][] = "blank";
  121. }
  122. return $data;
  123. }
  124. }