174c4e60d4f51f5c959e47d7bc4aaf2a9a64a22a
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Bernd Wurst Updated copyright notice (2...

Bernd Wurst authored 11 years ago

5) Written 2008-2013 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

16) 
17) require_once('inc/base.php');
18) require_once('inc/security.php');
19) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

20) include("external/php-iban/php-iban.php");
21) 
22) 
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

23) function my_invoices()
24) {
25)   $c = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Kleine Korrekturen Ausführu...

Bernd Wurst authored 10 years ago

26)   $result = db_query("SELECT id,datum,betrag,bezahlt,abbuchung,sepamandat FROM kundendaten.ausgestellte_rechnungen WHERE kunde={$c} ORDER BY id DESC");
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

27)   $ret = array();
28)   while($line = mysql_fetch_assoc($result))
29)   	array_push($ret, $line);
30)   return $ret;
31) }
32) 
33) 
34) function get_pdf($id)
35) {
36)   $c = (int) $_SESSION['customerinfo']['customerno'];
37)   $id = (int) $id;
38)   $result = db_query("SELECT pdfdata FROM kundendaten.ausgestellte_rechnungen WHERE kunde={$c} AND id={$id}");
39)   if (mysql_num_rows($result) == 0)
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

40) 	  system_failure('Ungültige Rechnungsnummer oder nicht eingeloggt');
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

41)   return mysql_fetch_object($result)->pdfdata;
42) 
43) }
44) 
45) 
46) function invoice_details($id)
47) {
48)   $c = (int) $_SESSION['customerinfo']['customerno'];
49)   $id = (int) $id;
50)   $result = db_query("SELECT kunde,datum,betrag,bezahlt,abbuchung FROM kundendaten.ausgestellte_rechnungen WHERE kunde={$c} AND id={$id}");
51)   if (mysql_num_rows($result) == 0)
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

52)   	system_failure('Ungültige Rechnungsnummer oder nicht eingeloggt');
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

53)   return mysql_fetch_assoc($result);
54) }
55) 
56) function invoice_items($id)
57) {
58)   $c = (int) $_SESSION['customerinfo']['customerno'];
59)   $id = (int) $id;
Bernd Wurst Einheit bei Rechnungsposten...

Bernd Wurst authored 11 years ago

60)   $result = db_query("SELECT id, beschreibung, datum, enddatum, betrag, einheit, brutto, mwst, anzahl FROM kundendaten.rechnungsposten WHERE rechnungsnummer={$id} AND kunde={$c}");
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

61)   if (mysql_num_rows($result) == 0)
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

62)   	system_failure('Ungültige Rechnungsnummer oder nicht eingeloggt');
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

63)   $ret = array();
64)   while($line = mysql_fetch_assoc($result))
65)   array_push($ret, $line);
66)   return $ret;
67) }
68) 
69) 
70) function upcoming_items()
71) {
72)   $c = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Einheit bei Rechnungsposten...

Bernd Wurst authored 11 years ago

73)   $result = db_query("SELECT anzahl, beschreibung, startdatum, enddatum, betrag, einheit, brutto, mwst FROM kundendaten.upcoming_items WHERE kunde={$c} ORDER BY startdatum ASC");
bernd Kunden können Ihre Rechnung...

bernd authored 16 years ago

74)   $ret = array();
75)   while($line = mysql_fetch_assoc($result))
76) 	  array_push($ret, $line);
77)   return $ret;
78) }
79) 
80) 
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

81) function generate_qrcode_image($id) 
82) {
83)   $invoice = invoice_details($id);
84)   $customerno = $invoice['kunde'];
85)   $amount = 'EUR'.sprintf('%.2f', $invoice['betrag']);
86)   $datum = $invoice['datum'];
87)   $data = 'BCD
88) 001
89) 1
90) SCT
91) GENODES1VBK
92) schokokeks.org GbR
93) DE91602911200041512006
94) '.$amount.'
95) 
96) 
Bernd Wurst Beschreibung entfernt, mach...

Bernd Wurst authored 10 years ago

97) RE '.$id.' KD '.$customerno.' vom '.$datum;
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

98)   
99)   $descriptorspec = array(
100)     0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
101)     1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
102)     2 => array("pipe", "w") 
103)   );
104) 
105)   $process = proc_open('qrencode -t PNG -o - -l M', $descriptorspec, $pipes);
106) 
107)   if (is_resource($process)) {
108)     // $pipes sieht nun so aus:
109)     // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
110)     // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
111) 
112)     fwrite($pipes[0], $data);
113)     fclose($pipes[0]);
114) 
115)     $pngdata = stream_get_contents($pipes[1]);
116)     fclose($pipes[1]);
117) 
118)     // Es ist wichtig, dass Sie alle Pipes schließen bevor Sie
119)     // proc_close aufrufen, um Deadlocks zu vermeiden
120)     $return_value = proc_close($process);
121)   
122)     return $pngdata;
123)   } else {
124)     warning('Es ist ein interner Fehler im Webinterface aufgetreten, aufgrund dessen kein QR-Code erstellt werden kann. Sollte dieser Fehler mehrfach auftreten, kontaktieren Sie bitte die Administratoren.');
125)   }
126) }
127) 
128) 
129) function generate_bezahlcode_image($id) 
130) {
131)   $invoice = invoice_details($id);
132)   $customerno = $invoice['kunde'];
133)   $amount = str_replace('.', '%2C', sprintf('%.2f', $invoice['betrag']));
134)   $datum = $invoice['datum'];
135)   $data = 'bank://singlepaymentsepa?name=schokokeks.org%20GbR&reason=RE%20'.$id.'%20KD%20'.$customerno.'%20vom%20'.$datum.'&iban=DE91602911200041512006&bic=GENODES1VBK&amount='.$amount;
136)   
137)   $descriptorspec = array(
138)     0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
139)     1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
140)     2 => array("pipe", "w") 
141)   );
142) 
143)   $process = proc_open('qrencode -t PNG -o -', $descriptorspec, $pipes);
144) 
145)   if (is_resource($process)) {
146)     // $pipes sieht nun so aus:
147)     // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
148)     // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
149) 
150)     fwrite($pipes[0], $data);
151)     fclose($pipes[0]);
152) 
153)     $pngdata = stream_get_contents($pipes[1]);
154)     fclose($pipes[1]);
155) 
156)     // Es ist wichtig, dass Sie alle Pipes schließen bevor Sie
157)     // proc_close aufrufen, um Deadlocks zu vermeiden
158)     $return_value = proc_close($process);
159)   
160)     return $pngdata;
161)   } else {
162)     warning('Es ist ein interner Fehler im Webinterface aufgetreten, aufgrund dessen kein QR-Code erstellt werden kann. Sollte dieser Fehler mehrfach auftreten, kontaktieren Sie bitte die Administratoren.');
163)   }
164) }
165) 
166) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

167) function get_sepamandate() 
168) {
169)   $cid = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

170)   $result = db_query("SELECT id, mandatsreferenz, glaeubiger_id, erteilt, medium, gueltig_ab, gueltig_bis, erstlastschrift, kontoinhaber, adresse, iban, bic, bankname FROM kundendaten.sepamandat WHERE kunde={$cid}");
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

171)   $ret = array();
172)   while ($entry = mysql_fetch_assoc($result)) {
173)     array_push($ret, $entry);
174)   }
175)   return $ret;
176) }
177) 
178) 
179) function yesterday($date) 
180) {
181)   $date = mysql_real_escape_string($date);
182)   $result = db_query("SELECT '{$date}' - INTERVAL 1 DAY");
183)   return mysql_fetch_array($result)[0];
184) }
185) 
186) 
187) function invalidate_sepamandat($id, $date) 
188) {
189)   $cid = (int) $_SESSION['customerinfo']['customerno'];
190)   $id = (int) $id;
191)   $date = mysql_real_escape_string($date);
192)   db_query("UPDATE kundendaten.sepamandat SET gueltig_bis='{$date}' WHERE id={$id} AND kunde={$cid}");
193) }
194) 
195) 
196) function sepamandat($name, $adresse, $iban, $bankname, $bic, $gueltig_ab)
197) {
198)   $cid = (int) $_SESSION['customerinfo']['customerno'];
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

199)   $first_date = date('Y-m-d');
200)   $invoices = my_invoices();
201)   foreach ($invoices as $i) {
202)     if ($i['bezahlt'] == 0 && $i['datum'] < $first_date) {
203)       $first_date = $i['datum'];
204)     }
205)   }
206)   if ($gueltig_ab < date('Y-m-d') && $gueltig_ab != $first_date) {
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

207)     system_failure('Das Mandat kann nicht rückwirkend erteilt werden. Bitte geben Sie ein Datum in der Zukunft an.');
208)   }
209)   $alte_mandate = get_sepamandate();
210)   $referenzen = array();
211)   foreach ($alte_mandate as $mandat) {
212)     if ($mandat['gueltig_bis'] == NULL || $mandat['gueltig_bis'] >= $gueltig_ab) {
213)       DEBUG('Altes Mandat wird für ungültig erklärt.');
214)       DEBUG($mandat);
215)       invalidate_sepamandat($mandat['id'], yesterday($gueltig_ab));
216)     }
217)     array_push($referenzen, $mandat['mandatsreferenz']);
218)   }
219)   $counter = 1;
220)   $referenz = sprintf('K%04d-M%03d', $cid, $counter);
221)   while (in_array($referenz, $referenzen)) {
222)     $counter++;
223)     $referenz = sprintf('K%04d-M%03d', $cid, $counter);
224)   }
225)   DEBUG('Nächste freie Mandatsreferenz: '. $referenz);
226) 
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

227)   $glaeubiger_id = config('glaeubiger_id');
228) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

229)   $today = date('Y-m-d');
Bernd Wurst Speichere Gläubiger-ID mit...

Bernd Wurst authored 10 years ago

230)   db_query("INSERT INTO kundendaten.sepamandat (mandatsreferenz, glaeubiger_id, kunde, erteilt, medium, gueltig_ab, kontoinhaber, adresse, iban, bic, bankname) VALUES ('{$referenz}', '${glaeubiger_id}', {$cid}, '{$today}', 'online', '{$gueltig_ab}', '{$name}', '{$adresse}', '{$iban}', '{$bic}', '{$bankname}')");
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

231) }
232) 
233) 
Bernd Wurst QR-Codes für Überweisung be...

Bernd Wurst authored 10 years ago

234) 
Bernd Wurst Vervollständige Bankname un...

Bernd Wurst authored 10 years ago

235) function get_bank_info($iban) 
236) {
237)   if (strlen($iban) != 22 || substr($iban, 0, 2) != 'DE') {
238)     // Geht nur bei deutschen IBANs
239)     echo 'Fehler!';
240)     echo '$iban = '.$iban;
241)     echo 'strlen($iban): '.strlen($iban);
242)     echo 'substr($iban, 0, 2): '.substr($iban, 0, 2);
243)     return NULL;
244)   }
245)   $blz = substr($iban, 4, 8);
246)   // FIXME: Liste der BLZs muss vorhanden sein!
247)   $bankinfofile = dirname(__FILE__).'/bankinfo.txt';
248)   $f = file($bankinfofile);
249)   $match = '';
250)   foreach ($f as $line) {
251)     if (substr($line, 0, 9) == $blz.'1') {
252)       $match = $line;
253)       break;
254)     }
255)   }
256)   $bank = array();
257)   $bank['name'] = iconv('latin1', 'utf8', chop(substr($match, 9,58)));
258)   $bank['bic'] = chop(substr($match, 139,11));
259)   return $bank;
260) }
261) 
262) 
Bernd Wurst Ermögliche das Berechnen de...

Bernd Wurst authored 10 years ago

263) function find_iban($blz, $kto) 
264) {
265)   $iban = sprintf('DE00%08s%010s', $blz, $kto);
266)   $iban = iban_set_checksum($iban);
267)   return $iban;
268) }
269) 
270)