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

Report.lib.php 4.0KB

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. use League\Csv\Writer;
  8. use League\Csv\HTMLConverter;
  9. use odsPhpGenerator\ods;
  10. use odsPhpGenerator\odsTable;
  11. use odsPhpGenerator\odsTableRow;
  12. use odsPhpGenerator\odsTableColumn;
  13. use odsPhpGenerator\odsTableCellString;
  14. use odsPhpGenerator\odsStyleTableColumn;
  15. use odsPhpGenerator\odsStyleTableCell;
  16. class Report {
  17. private $title = "";
  18. private $header = [];
  19. private $data = [];
  20. public function __construct(string $title = "", array $header = [], array $data = []) {
  21. $this->title = $title;
  22. $this->header = $header;
  23. $this->data = $data;
  24. }
  25. public function setHeader(array $header) {
  26. $this->header = $header;
  27. }
  28. public function addDataRow(array $columns) {
  29. $this->data[] = $columns;
  30. }
  31. public function getHeader(): array {
  32. return $this->header;
  33. }
  34. public function getData(): array {
  35. return $this->data;
  36. }
  37. public function output(string $format) {
  38. switch ($format) {
  39. case "ods":
  40. $this->toODS();
  41. break;
  42. case "html":
  43. $this->toHTML();
  44. break;
  45. case "csv":
  46. default:
  47. $this->toCSV();
  48. break;
  49. }
  50. }
  51. private function toODS() {
  52. $ods = new ods();
  53. $styleColumn = new odsStyleTableColumn();
  54. $styleColumn->setUseOptimalColumnWidth(true);
  55. $headerstyle = new odsStyleTableCell();
  56. $headerstyle->setFontWeight("bold");
  57. $table = new odsTable($this->title);
  58. for ($i = 0; $i < count($this->header); $i++) {
  59. $table->addTableColumn(new odsTableColumn($styleColumn));
  60. }
  61. $row = new odsTableRow();
  62. foreach ($this->header as $cell) {
  63. $row->addCell(new odsTableCellString($cell, $headerstyle));
  64. }
  65. $table->addRow($row);
  66. foreach ($this->data as $cols) {
  67. $row = new odsTableRow();
  68. foreach ($cols as $cell) {
  69. $row->addCell(new odsTableCellString($cell));
  70. }
  71. $table->addRow($row);
  72. }
  73. $ods->addTable($table);
  74. // The @ is a workaround to silence the tempnam notice,
  75. // which breaks the file. This is apparently the intended behavior:
  76. // https://bugs.php.net/bug.php?id=69489
  77. @$ods->downloadOdsFile($this->title . "_" . date("Y-m-d_Hi") . ".ods");
  78. }
  79. private function toHTML() {
  80. global $SECURE_NONCE;
  81. $data = array_merge([$this->header], $this->data);
  82. // HTML exporter doesn't like null values
  83. for ($i = 0; $i < count($data); $i++) {
  84. for ($j = 0; $j < count($data[$i]); $j++) {
  85. if (is_null($data[$i][$j])) {
  86. $data[$i][$j] = '';
  87. }
  88. }
  89. }
  90. header('Content-type: text/html');
  91. $converter = new HTMLConverter();
  92. $out = "<!DOCTYPE html>\n"
  93. . "<meta charset=\"utf-8\">\n"
  94. . "<meta name=\"viewport\" content=\"width=device-width\">\n"
  95. . "<title>" . $this->title . "_" . date("Y-m-d_Hi") . "</title>\n"
  96. . <<<STYLE
  97. <style nonce="$SECURE_NONCE">
  98. .table-csv-data {
  99. border-collapse: collapse;
  100. }
  101. .table-csv-data tr:first-child {
  102. font-weight: bold;
  103. }
  104. .table-csv-data tr td {
  105. border: 1px solid black;
  106. }
  107. </style>
  108. STYLE
  109. . $converter->convert($data);
  110. echo $out;
  111. }
  112. private function toCSV() {
  113. $csv = Writer::createFromString('');
  114. $data = array_merge([$this->header], $this->data);
  115. $csv->insertAll($data);
  116. header('Content-type: text/csv');
  117. header('Content-Disposition: attachment; filename="' . $this->title . "_" . date("Y-m-d_Hi") . ".csv" . '"');
  118. echo $csv;
  119. }
  120. }