Rename generic export.php to less generic exportxls.php, fix XLS export.
[memberdb.git] / include / xlsexport.php
1 <?php
2
3 function action_export_payments() {
4         global $EARNING_TYPES, $EXPENSE_TYPES;
5
6     error_reporting(error_reporting() & ~E_DEPRECATED & ~E_STRICT);
7         require_once 'vendor/autoload.php';
8
9         // Creating a workbook
10         $workbook = new Spreadsheet_Excel_Writer();
11         $workbook->setVersion(8);
12
13         // sending HTTP headers
14         $workbook->send('Finanzen.xls');
15
16         // Creating a worksheet
17
18         $payments = db_get_finished_payments(0, 1000000000);
19
20
21         $row = 0;
22         $col = 0;
23         $current_year = '';
24
25         foreach ($payments as $payment) {
26                 $year = substr($payment['date'], 0, 4);
27                 if ($year != $current_year) {
28                         $current_year = $year;
29                         $worksheet =& $workbook->addWorksheet($year);
30                         $worksheet->setInputEncoding('UTF-8');
31                         $row = 0;
32
33                         $worksheet->write($row, $col++, 'Id');
34                         $worksheet->write($row, $col++, 'Datum');
35                         $worksheet->write($row, $col++, 'Kategorie');
36                         $worksheet->write($row, $col++, 'Betrag');
37                         $worksheet->write($row, $col++, 'Beschreibung');
38                         $worksheet->write($row, $col++, 'Mitglied');
39                         $worksheet->write($row, $col++, 'Kontostand');
40                         $worksheet->write($row, $col++, 'Kassenstand');
41                         $worksheet->write($row, $col++, 'Gesamt');
42                         $row++;
43                         $col = 0;
44                 }
45                 $worksheet->write($row, $col++, $payment['id']);
46                 $worksheet->write($row, $col++, $payment['date']);
47                 $worksheet->write($row, $col++, $payment['ptype'] == 'earning'? $EARNING_TYPES[$payment['type']] : $EXPENSE_TYPES[$payment['type']]);
48                 $worksheet->write($row, $col++, ($payment['ptype'] == 'earning' ? 1 : -1)  * $payment['value']);
49                 $worksheet->write($row, $col++, $payment['description']);
50                 $worksheet->write($row, $col++, $payment['nickname']);
51                 $worksheet->write($row, $col++, $payment['bank']);
52                 $worksheet->write($row, $col++, $payment['cash']);
53                 $worksheet->write($row, $col++, $payment['total']);
54
55                 $col = 0;
56                 $row++;
57         }
58
59         // Let's send the file
60         $workbook->close();
61
62         $content = ob_get_contents();
63         ob_end_clean();
64         echo $content;
65         exit();
66 }
67
68
69