PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/report/class.Report.php

https://github.com/axxtel/agilebill
PHP | 681 lines | 525 code | 93 blank | 63 comment | 80 complexity | d6410c4489821dcdabf6d52bb309985a MD5 | raw file
  1. <?php
  2. /**
  3. * AgileBill - Open Billing Software
  4. *
  5. * This body of work is free software; you can redistribute it and/or
  6. * modify it under the terms of the Open AgileBill License
  7. * License as published at http://www.agileco.com/agilebill/license1-4.txt
  8. *
  9. * For questions, help, comments, discussion, etc., please join the
  10. * Agileco community forums at http://forum.agileco.com/
  11. *
  12. * @link http://www.agileco.com/
  13. * @copyright 2004-2008 Agileco, LLC.
  14. * @license http://www.agileco.com/agilebill/license1-4.txt
  15. * @author Tony Landis <tony@agileco.com> and Thralling Penguin, LLC <http://www.thrallingpenguin.com>
  16. * @package AgileBill
  17. * @version 1.4.93
  18. */
  19. class BASE_ReportFormatter {
  20. var $output_path;
  21. var $output;
  22. function setOutputDirectory($d)
  23. {
  24. $this->output_path = $d;
  25. }
  26. function getOutput()
  27. {
  28. return $this->output;
  29. }
  30. }
  31. class HTML_ReportFormatter extends BASE_ReportFormatter {
  32. var $indent = 0;
  33. var $coldata;
  34. var $level;
  35. var $div_count;
  36. function setLevel(&$level)
  37. {
  38. $this->level =& $level;
  39. }
  40. function setIndent($amount)
  41. {
  42. if($amount == "") return;
  43. $this->indent = $amount;
  44. }
  45. function addRow()
  46. {
  47. $this->coldata = array();
  48. echo " <tr>\n";
  49. }
  50. function endRow()
  51. {
  52. /* output the coldata */
  53. #if($this->indent) {
  54. # echo " <td width=\"{$this->indent}\">&nbsp;</td>\n";
  55. #}
  56. $w = 100 / count($this->coldata);
  57. foreach($this->coldata as $col) {
  58. $tag = "td";
  59. if (is_a($col[1],'ReportStyle')) {
  60. $s = "style=\"".$col[1]->doHTML()."\"";
  61. if($col[1]->is_heading) $tag = "th scope=\"col\"";
  62. } else {
  63. $s = "";
  64. }
  65. echo " <{$tag} {$s} width=\"{$w}%\">\n";
  66. echo " {$col[0]}\n";
  67. echo " </{$tag}>\n";
  68. }
  69. echo " </tr>\n";
  70. }
  71. function addColumn($col, $style = '')
  72. {
  73. /* take a column and a style */
  74. $this->coldata[] = array($col, $style);
  75. }
  76. function addTable($style, $heading)
  77. {
  78. $i = $this->indent / 30; if($i<0) $i = 0;
  79. ++$i;
  80. $this->div_count = 0;
  81. if($style != 'report_heading') {
  82. echo '<div id="level_'.$i.'">';
  83. $this->div_count += 1;
  84. }
  85. if($this->indent == 0) {
  86. if($style != 'level') {
  87. echo '<div id="'.$style.'">';
  88. $this->div_count += 1;
  89. }
  90. }
  91. echo $heading;
  92. echo '<table width="100%" border="0" cellspacing="0">';
  93. echo "\n";
  94. }
  95. function endTable()
  96. {
  97. $i = $this->indent / 30; if($i<0) $i = 0;
  98. ++$i;
  99. echo "</table>\n";
  100. if($this->div_count == 1)
  101. echo "</div>\n";
  102. if($this->indent == 0) {
  103. if($this->div_count == 2)
  104. echo "</div>\n";
  105. }
  106. }
  107. function startDocument($title = '')
  108. {
  109. ob_start();
  110. echo "<html>\n";
  111. echo "<head>\n";
  112. echo "<title>{$title}</title>\n";
  113. echo "<link rel=\"stylesheet\" href=\"".URL."themes/default_admin/report_style.css\" type=\"text/css\">";
  114. echo "</head>\n<body>\n";
  115. }
  116. function endDocument()
  117. {
  118. echo "</body>\n</html>\n";
  119. $content = ob_get_contents();
  120. ob_end_clean();
  121. $this->output = $this->output_path."/report.html";
  122. $fp = fopen($this->output,'w');
  123. if($fp) {
  124. fwrite($fp,$content);
  125. fclose($fp);
  126. } else {
  127. echo 'Could not write to output file.';
  128. }
  129. }
  130. function addBreak()
  131. {
  132. echo "<br /><br /><br />";
  133. }
  134. function insertImage($file,$width,$height)
  135. {
  136. $file = basename($file);
  137. echo "<center><img src=\"$file\" width=\"$width\" height=\"$height\" /></center>\n";
  138. }
  139. function write($s)
  140. {
  141. echo $s;
  142. }
  143. }
  144. /**
  145. * Outputs the report in DOS text format
  146. */
  147. class TXT_ReportFormatter extends BASE_ReportFormatter {
  148. var $indent = 0;
  149. var $aindent;
  150. var $coldata;
  151. var $level;
  152. function setLevel(&$level)
  153. {
  154. $this->level =& $level;
  155. }
  156. function setIndent($amount)
  157. {
  158. if($amount == "") return;
  159. $this->indent = $amount;
  160. }
  161. function addRow()
  162. {
  163. $this->coldata = array();
  164. }
  165. function endRow()
  166. {
  167. /* output the coldata */
  168. if($this->indent) {
  169. $n = intval($this->indent) / 30;
  170. echo str_repeat(" ", $n);
  171. }
  172. $w = (100 / count($this->coldata))/100 * 80;
  173. foreach($this->coldata as $col) {
  174. echo str_pad($col[0], $w);
  175. }
  176. echo "\r\n";
  177. }
  178. function addColumn($col, $style = '')
  179. {
  180. /* take a column and a style */
  181. $this->coldata[] = array($col, $style);
  182. }
  183. function addTable($style, $heading=false)
  184. {
  185. if($this->indent) {
  186. echo "\r\n";
  187. $this->aindent = $this->indent;
  188. } else {
  189. echo "\r\n\r\n";
  190. }
  191. }
  192. function endTable()
  193. {
  194. if($this->indent && $this->indent != $this->aindent) echo "\r\n";
  195. }
  196. function startDocument($title = '')
  197. {
  198. ob_start();
  199. }
  200. function endDocument()
  201. {
  202. $content = ob_get_contents();
  203. ob_end_clean();
  204. $this->output = $this->output_path."/report.txt";
  205. $fp = fopen($this->output,'w');
  206. if($fp) {
  207. fwrite($fp,$content);
  208. fclose($fp);
  209. } else {
  210. echo 'Could not write to output file.';
  211. }
  212. }
  213. function addBreak()
  214. {
  215. echo "\r\n\r\n";
  216. }
  217. function insertImage($file,$width,$height)
  218. {
  219. $file = basename($file);
  220. echo "\r\nSee file: $file\r\n";
  221. }
  222. function write($s)
  223. {
  224. }
  225. }
  226. class PDF_ReportFormatter extends BASE_ReportFormatter {
  227. var $indent = 0;
  228. var $aindent;
  229. var $coldata;
  230. var $pdf;
  231. var $y;
  232. var $level;
  233. function write($s)
  234. {
  235. }
  236. function setIndent($amount)
  237. {
  238. if($amount == "") return;
  239. $this->indent = intval($amount);
  240. }
  241. function addRow()
  242. {
  243. $this->coldata = array();
  244. }
  245. function endRow()
  246. {
  247. $x = 9;
  248. $n = 0;
  249. /* output the coldata */
  250. if($this->indent) {
  251. $n = $this->indent / 30;
  252. $x += ($n * 9);
  253. }
  254. $w = round((100 / count($this->coldata))/100 * (190 - ($n*9)));
  255. $tx = $x;
  256. foreach($this->coldata as $col) {
  257. if (is_a($col[1],'ReportStyle')) {
  258. /* set the bounding area */
  259. $col[1]->setBounds($tx,$this->y,$tx + $w,$this->y + 5);
  260. $col[1]->doPDF($this->pdf);
  261. } else {
  262. $tmp = new ReportStyle;
  263. $tmp->doPDF($this->pdf);
  264. }
  265. #echo "x={$tx} y={$this->y} data={$col[0]} style={$col[1]}<br>";
  266. $this->pdf->Text($tx, $this->y,$col[0]);
  267. $tx += $w;
  268. }
  269. $this->addY(5, true);
  270. }
  271. function addY($y, $repeatLabels = false)
  272. {
  273. $this->y += $y;
  274. if ($this->y > 275) {
  275. $this->pdf->AddPage();
  276. $this->y = 5;
  277. if($repeatLabels)
  278. $this->level->addHeaderLabels();
  279. }
  280. }
  281. function setLevel(&$level)
  282. {
  283. $this->level =& $level;
  284. }
  285. function addColumn($col, $style = '')
  286. {
  287. /* take a column and a style */
  288. $this->coldata[] = array($col, $style);
  289. }
  290. function addTable($style, $heading='')
  291. {
  292. if($this->indent) {
  293. $this->addY(5);
  294. $this->aindent = $this->indent;
  295. } else {
  296. $this->addY(10);
  297. }
  298. # if we're close to the end, goto next page
  299. if($this->y > 250) $this->addY(1000);
  300. }
  301. function endTable()
  302. {
  303. if($this->indent && $this->indent != $this->aindent) $this->addY(5);
  304. }
  305. function startDocument($title = '')
  306. {
  307. require_once(PATH_INCLUDES.'pdf/fpdi.php');
  308. require_once(PATH_INCLUDES.'pdf/fpdf_tpl.php');
  309. require_once(PATH_INCLUDES.'pdf/fpdf.php');
  310. $this->pdf = new fpdi;
  311. $this->pdf->addPage();
  312. $this->y = 5;
  313. }
  314. function endDocument()
  315. {
  316. $file = $this->output_path."/report.pdf";
  317. $this->output = $file;
  318. $this->pdf->Output($file,'F');
  319. }
  320. function addBreak()
  321. {
  322. $this->addY(10);
  323. }
  324. function insertImage($file,$width,$height)
  325. {
  326. $w = $width / 11.81102 * 2.5;
  327. $h = $height / 11.81102 * 2.5;
  328. $y = $this->y + 5;
  329. $x = 105 - ($w/2);
  330. #echo 'Place image at 0,'.$y." $w x $h<br>";
  331. $this->pdf->Image($file, $x, $y, $w, $h);
  332. $this->y = $y + $h + 5;
  333. }
  334. }
  335. class ReportStyle {
  336. var $font_weight;
  337. var $font_family;
  338. var $font_height;
  339. var $bg_color;
  340. var $is_heading;
  341. var $tx, $ty, $bx, $by;
  342. function ReportStyle()
  343. {
  344. $this->font_weight = '';
  345. $this->font_family = 'times';
  346. $this->font_height = 10;
  347. $this->is_heading = false;
  348. }
  349. function fontFamily($f)
  350. {
  351. $this->font_family = $f;
  352. }
  353. function fontHeight($h)
  354. {
  355. $this->font_height = $h;
  356. }
  357. function bold()
  358. {
  359. $this->font_weight = 'bold';
  360. }
  361. function backgroundColor($r,$g,$b)
  362. {
  363. $this->bg_color = array($r,$g,$b);
  364. }
  365. function setBounds($x1,$y1,$x2,$y2)
  366. {
  367. $this->tx = $x1; $this->ty = $y1;
  368. $this->bx = $x2; $this->by = $y2;
  369. #echo "BOUNDS: $x1 x $y1 x $x2 x $y2<br>";
  370. }
  371. /**
  372. * Generates the PDF style
  373. */
  374. function doPDF(&$pdf)
  375. {
  376. if(is_array($this->bg_color)) {
  377. $x1 = $this->tx;
  378. $y1 = $this->ty - ($this->font_height * 0.352777778);
  379. $w = $this->bx - $this->tx;
  380. $h = $this->by - $this->ty;
  381. $pdf->SetFillColor($this->bg_color[0],$this->bg_color[1],$this->bg_color[2]);
  382. $pdf->Rect($x1,$y1,$w,$h,'F');
  383. #echo "BOX: $x1 x $y1 x $w x $h<br>";
  384. }
  385. $b = ($this->font_weight == 'bold' ? "B" : "");
  386. $pdf->SetFont($this->font_family,$b,$this->font_height);
  387. }
  388. /**
  389. * Generates the HTML style
  390. */
  391. function doHTML()
  392. {
  393. $s = "font-family: {$this->font_family}; font-size: {$this->font_height}pt;";
  394. if($this->font_weight == 'bold')
  395. $s .= " font-weight: bold;";
  396. if(is_array($this->bg_color)) {
  397. $s .= " background-color: #";
  398. $s .= str_pad(dechex($this->bg_color[0]), 2, '0', STR_PAD_LEFT);
  399. $s .= str_pad(dechex($this->bg_color[1]), 2, '0', STR_PAD_LEFT);
  400. $s .= str_pad(dechex($this->bg_color[2]), 2, '0', STR_PAD_LEFT);
  401. $s .= ";";
  402. }
  403. $s="";
  404. return $s;
  405. }
  406. }
  407. /**
  408. * This class is a adaptor pattern that serves to place a break in the formatting of the report.
  409. */
  410. class Report_BreakAdaptor {
  411. function display($grouping, $aggregate, &$formatter)
  412. {
  413. $formatter->addBreak();
  414. }
  415. }
  416. require_once PATH_MODULES.'report/class.Level.php';
  417. class Report_DivAdaptor extends Level_Base {
  418. var $id;
  419. var $lev_setting;
  420. var $lev_fields;
  421. var $lev_items;
  422. var $indent_html;
  423. var $SQL_filtered;
  424. var $group_level;
  425. var $grouping_criteria;
  426. var $has_title;
  427. var $formatter;
  428. function display($grouping, $aggregate, &$formatter)
  429. {
  430. $this->formatter =& $formatter;
  431. #echo "<div id=\"{$this->id}\">";
  432. if(isset($this->lev_items) && is_array($this->lev_items)) {
  433. foreach ($this->lev_items as $item) {
  434. echo "<div id=\"{$this->id}\">";
  435. $c = $this->formatter->indent;
  436. $item->display($grouping, $aggregate, $this->formatter);
  437. $this->formatter->indent = $c;
  438. echo "</div>";
  439. }
  440. }
  441. #echo "</div>";
  442. }
  443. }
  444. class Reporting {
  445. var $has_header;
  446. var $rep_title;
  447. var $rep_subtitle_1;
  448. var $rep_subtitle_2;
  449. var $rep_date_start;
  450. var $rep_date_end;
  451. var $rep_image_src;
  452. var $rep_image_width;
  453. var $rep_image_height;
  454. var $rep_image_alt;
  455. var $rep_desc;
  456. var $rep_items;
  457. var $rep_formatter;
  458. function Reporting (&$formatter, $has_header = false)
  459. {
  460. $this->has_header = $has_header;
  461. /*
  462. TODO: Implement this crap at a later point
  463. $this->rep_title = $rep_title;
  464. $this->rep_subtitle_1 = $rep_subtitle_1;
  465. $this->rep_subtitle_2 = $rep_subtitle_2;
  466. $this->rep_date_start = $rep_date_start;
  467. $this->rep_date_end = $rep_date_end;
  468. $this->rep_desc = $rep_desc;
  469. $this->rep_image_src = $rep_image_src;
  470. $this->rep_image_width = $rep_image_width;
  471. $this->rep_image_height = $rep_image_height;
  472. $this->rep_image_alt = $rep_image_alt;
  473. */
  474. $this->rep_formatter =& $formatter;
  475. }
  476. function setTitle($t, $style = '')
  477. {
  478. if($style=='') $style = new ReportStyle;
  479. $style->fontHeight(16);
  480. $style->fontFamily('arial');
  481. $this->rep_title = array($t,$style);
  482. }
  483. function setSubtitle1($t, $style = '')
  484. {
  485. if($style=='') $style = new ReportStyle;
  486. $style->fontHeight(14);
  487. $style->fontFamily('arial');
  488. $this->rep_subtitle_1 = array($t,$style);
  489. }
  490. function setSubtitle2($t, $style = '')
  491. {
  492. if($style=='') $style = new ReportStyle;
  493. $style->fontHeight(12);
  494. $style->fontFamily('arial');
  495. $this->rep_subtitle_2 = array($t,$style);
  496. }
  497. function addBreak ()
  498. {
  499. $this->rep_items[] = new Report_BreakAdaptor;
  500. }
  501. function addDiv($id)
  502. {
  503. $item = new Report_DivAdaptor;
  504. $item->id = $id;
  505. $this->rep_items[] = $item;
  506. }
  507. function append (&$item)
  508. {
  509. $this->rep_items[] =& $item;
  510. }
  511. function display ()
  512. {
  513. $this->rep_formatter->startDocument($this->rep_title[0]);
  514. if ($this->has_header) {
  515. $this->displayHeader();
  516. }
  517. if(is_array($this->rep_items)) {
  518. foreach ($this->rep_items as $item) {
  519. $cur = $this->rep_formatter->indent;
  520. $item->display(Null, Null, $this->rep_formatter);
  521. $this->rep_formatter->indent = $cur;
  522. }
  523. }
  524. $this->rep_formatter->endDocument();
  525. }
  526. function displayHeader ()
  527. {
  528. if(is_a($this->rep_formatter,'HTML_ReportFormatter')) {
  529. if ($this->rep_title != '') {
  530. echo '<h1>'.$this->rep_title[0].'</h1>';
  531. }
  532. if ($this->rep_subtitle_1 != '') {
  533. echo '<h2>'.$this->rep_subtitle_1[0].'</h2>';
  534. }
  535. if ($this->rep_subtitle_2 != '') {
  536. echo '<h3>'.$this->rep_subtitle_2[0].'</h3>';
  537. }
  538. } else {
  539. $this->rep_formatter->addTable('report_heading');
  540. if ($this->rep_image_src != '') {
  541. echo "<td class='rc-rep-image-cell'>";
  542. $width_html = '';
  543. $height_html = '';
  544. $alt_html = '';
  545. if ($this->rep_image_width != '') {
  546. $width_html = " width='$this->rep_image_width'";
  547. }
  548. echo "<td class='rc-rep-image-cell'$width_html>";
  549. if ($this->rep_image_height != '') {
  550. $height_html = " height='$this->rep_image_height'";
  551. }
  552. if ($this->rep_image_alt != '') {
  553. $alt_html = " alt='$this->rep_image_alt'";
  554. }
  555. echo "<img class='rc-rep-image' src='$this->rep_image_src' $width_html $height_html $alt_html >
  556. </td>";
  557. }
  558. $d = "";
  559. if ($this->rep_title != '') {
  560. $this->rep_formatter->addRow();
  561. $this->rep_formatter->addColumn($this->rep_title[0],$this->rep_title[1]);
  562. $this->rep_formatter->endRow();
  563. }
  564. if ($this->rep_subtitle_1 != '') {
  565. $this->rep_formatter->addRow();
  566. $this->rep_formatter->addColumn($this->rep_subtitle_1[0],$this->rep_subtitle_1[1]);
  567. $this->rep_formatter->endRow();
  568. }
  569. if ($this->rep_subtitle_2 != '') {
  570. $this->rep_formatter->addRow();
  571. $this->rep_formatter->addColumn($this->rep_subtitle_2[0],$this->rep_subtitle_2[1]);
  572. $this->rep_formatter->endRow();
  573. }
  574. # $d .= "<h3>Report generated on " . date("d/m/y") . "</h3>";
  575. if ($this->rep_desc != '') {
  576. $d .= "<h3>$this->rep_desc</h3>";
  577. }
  578. $this->rep_formatter->addRow();
  579. $this->rep_formatter->addColumn(' ');
  580. $this->rep_formatter->endRow();
  581. $this->rep_formatter->endTable();
  582. }
  583. }
  584. }
  585. ?>