An easy point of sale system with automatic inventory tracking. https://netsyms.biz/apps/nickelbox/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. }
  125. class Receipt {
  126. private $lines = [];
  127. private $header = [];
  128. private $footer = [];
  129. function __construct() {
  130. }
  131. function appendLine(ReceiptLine $line) {
  132. $this->lines[] = $line;
  133. }
  134. function appendLines($lines) {
  135. foreach ($lines as $l) {
  136. $this->lines[] = $l;
  137. }
  138. }
  139. function appendHeader(ReceiptLine $line) {
  140. $this->header[] = $line;
  141. }
  142. function appendFooter(ReceiptLine $line) {
  143. $this->footer[] = $line;
  144. }
  145. function appendBreak() {
  146. $this->lines[] = new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR);
  147. }
  148. function appendBlank() {
  149. $this->lines[] = new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_BLANK);
  150. }
  151. function getHtml($title = "") {
  152. global $SECURE_NONCE;
  153. $html = <<<END
  154. <!DOCTYPE html>
  155. <meta charset="UTF-8">
  156. <meta name="viewport" content="width=device-width, initial-scale=1">
  157. <title>$title</title>
  158. <style nonce="$SECURE_NONCE">
  159. .flex {
  160. display: flex;
  161. justify-content: space-between;
  162. margin: 0;
  163. }
  164. .bold {
  165. font-weight: bold;
  166. }
  167. .centered {
  168. justify-content: center;
  169. }
  170. </style>
  171. END;
  172. if (count($this->header) > 0) {
  173. foreach ($this->header as $line) {
  174. $html .= $line->getHtml() . "\n";
  175. }
  176. $html .= (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getHtml();
  177. }
  178. foreach ($this->lines as $line) {
  179. $html .= $line->getHtml() . "\n";
  180. }
  181. if (count($this->footer) > 0) {
  182. $html .= (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getHtml();
  183. foreach ($this->footer as $line) {
  184. $html .= $line->getHtml() . "\n";
  185. }
  186. }
  187. return $html;
  188. }
  189. function getPlainText($width) {
  190. $lines = [];
  191. if (count($this->header) > 0) {
  192. foreach ($this->header as $line) {
  193. $lines[] = $line->getPlainText($width);
  194. }
  195. $lines[] = (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getPlainText($width);
  196. }
  197. foreach ($this->lines as $line) {
  198. $lines[] = $line->getPlainText($width);
  199. }
  200. if (count($this->footer) > 0) {
  201. $lines[] = (new ReceiptLine("", "", "", ReceiptLine::LINEFORMAT_HR))->getPlainText($width);
  202. foreach ($this->footer as $line) {
  203. $lines[] = $line->getPlainText($width);
  204. }
  205. }
  206. return implode("\n", $lines);
  207. }
  208. function getArray($width = 64) {
  209. $header = [];
  210. $lines = [];
  211. $footer = [];
  212. foreach ($this->header as $line) {
  213. $header[] = $line->getArray($width);
  214. }
  215. foreach ($this->lines as $line) {
  216. $lines[] = $line->getArray($width);
  217. }
  218. foreach ($this->footer as $line) {
  219. $footer[] = $line->getArray($width);
  220. }
  221. return ["header" => $header, "lines" => $lines, "footer" => $footer];
  222. }
  223. function getJson($width = 64) {
  224. return json_encode($this->getArray($width));
  225. }
  226. }